]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
5e656dbb | 2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
693fc8f6 | 3 | * Copyright (c) 2013 Red Hat, Inc. |
da23017d | 4 | * All Rights Reserved. |
5000d01d | 5 | * |
da23017d NS |
6 | * This program is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU General Public License as | |
2bd0ea18 | 8 | * published by the Free Software Foundation. |
5000d01d | 9 | * |
da23017d NS |
10 | * This program is distributed in the hope that it would be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
5000d01d | 14 | * |
da23017d NS |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write the Free Software Foundation, | |
17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
2bd0ea18 NS |
18 | */ |
19 | ||
5e656dbb BN |
20 | #include <xfs.h> |
21 | ||
2bd0ea18 | 22 | /* |
5e656dbb | 23 | * Local function prototypes. |
2bd0ea18 | 24 | */ |
a2ceac1f DC |
25 | static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp, |
26 | int first, int last); | |
27 | static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp); | |
28 | static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp, | |
5e656dbb BN |
29 | int *entno); |
30 | static int xfs_dir2_block_sort(const void *a, const void *b); | |
2bd0ea18 | 31 | |
5e656dbb BN |
32 | static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot; |
33 | ||
34 | /* | |
35 | * One-time startup routine called from xfs_init(). | |
36 | */ | |
37 | void | |
38 | xfs_dir_startup(void) | |
39 | { | |
56b2de80 DC |
40 | xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1); |
41 | xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2); | |
5e656dbb | 42 | } |
2bd0ea18 | 43 | |
693fc8f6 DC |
44 | static bool |
45 | xfs_dir3_block_verify( | |
a2ceac1f DC |
46 | struct xfs_buf *bp) |
47 | { | |
48 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
693fc8f6 DC |
49 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
50 | ||
51 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
52 | if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) | |
53 | return false; | |
54 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid)) | |
55 | return false; | |
56 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) | |
57 | return false; | |
58 | } else { | |
59 | if (hdr3->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) | |
60 | return false; | |
a2ceac1f | 61 | } |
693fc8f6 DC |
62 | if (__xfs_dir2_data_check(NULL, bp)) |
63 | return false; | |
64 | return true; | |
a2ceac1f DC |
65 | } |
66 | ||
67 | static void | |
693fc8f6 | 68 | xfs_dir3_block_read_verify( |
a2ceac1f DC |
69 | struct xfs_buf *bp) |
70 | { | |
693fc8f6 DC |
71 | struct xfs_mount *mp = bp->b_target->bt_mount; |
72 | ||
73 | if ((xfs_sb_version_hascrc(&mp->m_sb) && | |
74 | !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length), | |
75 | XFS_DIR3_DATA_CRC_OFF)) || | |
76 | !xfs_dir3_block_verify(bp)) { | |
77 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); | |
78 | xfs_buf_ioerror(bp, EFSCORRUPTED); | |
79 | } | |
a2ceac1f DC |
80 | } |
81 | ||
82 | static void | |
693fc8f6 | 83 | xfs_dir3_block_write_verify( |
a2ceac1f DC |
84 | struct xfs_buf *bp) |
85 | { | |
693fc8f6 DC |
86 | struct xfs_mount *mp = bp->b_target->bt_mount; |
87 | struct xfs_buf_log_item *bip = bp->b_fspriv; | |
88 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; | |
89 | ||
90 | if (!xfs_dir3_block_verify(bp)) { | |
91 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); | |
92 | xfs_buf_ioerror(bp, EFSCORRUPTED); | |
93 | return; | |
94 | } | |
95 | ||
96 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
97 | return; | |
98 | ||
99 | if (bip) | |
100 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
101 | ||
102 | xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF); | |
a2ceac1f DC |
103 | } |
104 | ||
693fc8f6 DC |
105 | const struct xfs_buf_ops xfs_dir3_block_buf_ops = { |
106 | .verify_read = xfs_dir3_block_read_verify, | |
107 | .verify_write = xfs_dir3_block_write_verify, | |
a2ceac1f DC |
108 | }; |
109 | ||
110 | static int | |
693fc8f6 | 111 | xfs_dir3_block_read( |
a2ceac1f DC |
112 | struct xfs_trans *tp, |
113 | struct xfs_inode *dp, | |
114 | struct xfs_buf **bpp) | |
115 | { | |
116 | struct xfs_mount *mp = dp->i_mount; | |
117 | ||
118 | return xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp, | |
693fc8f6 DC |
119 | XFS_DATA_FORK, &xfs_dir3_block_buf_ops); |
120 | } | |
121 | ||
122 | static void | |
123 | xfs_dir3_block_init( | |
124 | struct xfs_mount *mp, | |
125 | struct xfs_buf *bp, | |
126 | struct xfs_inode *dp) | |
127 | { | |
128 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; | |
129 | ||
130 | bp->b_ops = &xfs_dir3_block_buf_ops; | |
131 | ||
132 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
133 | memset(hdr3, 0, sizeof(*hdr3)); | |
134 | hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC); | |
135 | hdr3->blkno = cpu_to_be64(bp->b_bn); | |
136 | hdr3->owner = cpu_to_be64(dp->i_ino); | |
137 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid); | |
138 | return; | |
139 | ||
140 | } | |
141 | hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); | |
a2ceac1f DC |
142 | } |
143 | ||
144 | static void | |
145 | xfs_dir2_block_need_space( | |
146 | struct xfs_dir2_data_hdr *hdr, | |
147 | struct xfs_dir2_block_tail *btp, | |
148 | struct xfs_dir2_leaf_entry *blp, | |
149 | __be16 **tagpp, | |
150 | struct xfs_dir2_data_unused **dupp, | |
151 | struct xfs_dir2_data_unused **enddupp, | |
152 | int *compact, | |
153 | int len) | |
154 | { | |
155 | struct xfs_dir2_data_free *bf; | |
156 | __be16 *tagp = NULL; | |
157 | struct xfs_dir2_data_unused *dup = NULL; | |
158 | struct xfs_dir2_data_unused *enddup = NULL; | |
159 | ||
160 | *compact = 0; | |
693fc8f6 | 161 | bf = xfs_dir3_data_bestfree_p(hdr); |
a2ceac1f DC |
162 | |
163 | /* | |
164 | * If there are stale entries we'll use one for the leaf. | |
165 | */ | |
166 | if (btp->stale) { | |
167 | if (be16_to_cpu(bf[0].length) >= len) { | |
168 | /* | |
169 | * The biggest entry enough to avoid compaction. | |
170 | */ | |
171 | dup = (xfs_dir2_data_unused_t *) | |
172 | ((char *)hdr + be16_to_cpu(bf[0].offset)); | |
173 | goto out; | |
174 | } | |
175 | ||
176 | /* | |
177 | * Will need to compact to make this work. | |
178 | * Tag just before the first leaf entry. | |
179 | */ | |
180 | *compact = 1; | |
181 | tagp = (__be16 *)blp - 1; | |
182 | ||
183 | /* Data object just before the first leaf entry. */ | |
184 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
185 | ||
186 | /* | |
187 | * If it's not free then the data will go where the | |
188 | * leaf data starts now, if it works at all. | |
189 | */ | |
190 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | |
191 | if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) * | |
192 | (uint)sizeof(*blp) < len) | |
193 | dup = NULL; | |
194 | } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len) | |
195 | dup = NULL; | |
196 | else | |
197 | dup = (xfs_dir2_data_unused_t *)blp; | |
198 | goto out; | |
199 | } | |
200 | ||
201 | /* | |
202 | * no stale entries, so just use free space. | |
203 | * Tag just before the first leaf entry. | |
204 | */ | |
205 | tagp = (__be16 *)blp - 1; | |
206 | ||
207 | /* Data object just before the first leaf entry. */ | |
208 | enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
209 | ||
210 | /* | |
211 | * If it's not free then can't do this add without cleaning up: | |
212 | * the space before the first leaf entry needs to be free so it | |
213 | * can be expanded to hold the pointer to the new entry. | |
214 | */ | |
215 | if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | |
216 | /* | |
217 | * Check out the biggest freespace and see if it's the same one. | |
218 | */ | |
219 | dup = (xfs_dir2_data_unused_t *) | |
220 | ((char *)hdr + be16_to_cpu(bf[0].offset)); | |
221 | if (dup != enddup) { | |
222 | /* | |
223 | * Not the same free entry, just check its length. | |
224 | */ | |
225 | if (be16_to_cpu(dup->length) < len) | |
226 | dup = NULL; | |
227 | goto out; | |
228 | } | |
229 | ||
230 | /* | |
231 | * It is the biggest freespace, can it hold the leaf too? | |
232 | */ | |
233 | if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) { | |
234 | /* | |
235 | * Yes, use the second-largest entry instead if it works. | |
236 | */ | |
237 | if (be16_to_cpu(bf[1].length) >= len) | |
238 | dup = (xfs_dir2_data_unused_t *) | |
239 | ((char *)hdr + be16_to_cpu(bf[1].offset)); | |
240 | else | |
241 | dup = NULL; | |
242 | } | |
243 | } | |
244 | out: | |
245 | *tagpp = tagp; | |
246 | *dupp = dup; | |
247 | *enddupp = enddup; | |
248 | } | |
249 | ||
250 | /* | |
251 | * compact the leaf entries. | |
252 | * Leave the highest-numbered stale entry stale. | |
253 | * XXX should be the one closest to mid but mid is not yet computed. | |
254 | */ | |
255 | static void | |
256 | xfs_dir2_block_compact( | |
257 | struct xfs_trans *tp, | |
258 | struct xfs_buf *bp, | |
259 | struct xfs_dir2_data_hdr *hdr, | |
260 | struct xfs_dir2_block_tail *btp, | |
261 | struct xfs_dir2_leaf_entry *blp, | |
262 | int *needlog, | |
263 | int *lfloghigh, | |
264 | int *lfloglow) | |
265 | { | |
266 | int fromidx; /* source leaf index */ | |
267 | int toidx; /* target leaf index */ | |
268 | int needscan = 0; | |
269 | int highstale; /* high stale index */ | |
270 | ||
271 | fromidx = toidx = be32_to_cpu(btp->count) - 1; | |
272 | highstale = *lfloghigh = -1; | |
273 | for (; fromidx >= 0; fromidx--) { | |
274 | if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { | |
275 | if (highstale == -1) | |
276 | highstale = toidx; | |
277 | else { | |
278 | if (*lfloghigh == -1) | |
279 | *lfloghigh = toidx; | |
280 | continue; | |
281 | } | |
282 | } | |
283 | if (fromidx < toidx) | |
284 | blp[toidx] = blp[fromidx]; | |
285 | toidx--; | |
286 | } | |
287 | *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1); | |
288 | *lfloghigh -= be32_to_cpu(btp->stale) - 1; | |
289 | be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1)); | |
290 | xfs_dir2_data_make_free(tp, bp, | |
291 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), | |
292 | (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)), | |
293 | needlog, &needscan); | |
294 | blp += be32_to_cpu(btp->stale) - 1; | |
295 | btp->stale = cpu_to_be32(1); | |
296 | /* | |
297 | * If we now need to rebuild the bestfree map, do so. | |
298 | * This needs to happen before the next call to use_free. | |
299 | */ | |
300 | if (needscan) | |
301 | xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog); | |
302 | } | |
303 | ||
2bd0ea18 NS |
304 | /* |
305 | * Add an entry to a block directory. | |
306 | */ | |
307 | int /* error */ | |
308 | xfs_dir2_block_addname( | |
309 | xfs_da_args_t *args) /* directory op arguments */ | |
310 | { | |
a2ceac1f | 311 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 312 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 313 | struct xfs_buf *bp; /* buffer for block */ |
2bd0ea18 NS |
314 | xfs_dir2_block_tail_t *btp; /* block tail */ |
315 | int compact; /* need to compact leaf ents */ | |
316 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
317 | xfs_inode_t *dp; /* directory inode */ | |
318 | xfs_dir2_data_unused_t *dup; /* block unused entry */ | |
319 | int error; /* error return value */ | |
0e266570 | 320 | xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */ |
2bd0ea18 NS |
321 | xfs_dahash_t hash; /* hash value of found entry */ |
322 | int high; /* high index for binary srch */ | |
323 | int highstale; /* high stale index */ | |
0e266570 NS |
324 | int lfloghigh=0; /* last final leaf to log */ |
325 | int lfloglow=0; /* first final leaf to log */ | |
2bd0ea18 NS |
326 | int len; /* length of the new entry */ |
327 | int low; /* low index for binary srch */ | |
328 | int lowstale; /* low stale index */ | |
0e266570 | 329 | int mid=0; /* midpoint for binary srch */ |
2bd0ea18 NS |
330 | xfs_mount_t *mp; /* filesystem mount point */ |
331 | int needlog; /* need to log header */ | |
332 | int needscan; /* need to rescan freespace */ | |
5e656dbb | 333 | __be16 *tagp; /* pointer to tag value */ |
2bd0ea18 NS |
334 | xfs_trans_t *tp; /* transaction structure */ |
335 | ||
56b2de80 DC |
336 | trace_xfs_dir2_block_addname(args); |
337 | ||
2bd0ea18 NS |
338 | dp = args->dp; |
339 | tp = args->trans; | |
340 | mp = dp->i_mount; | |
a2ceac1f DC |
341 | |
342 | /* Read the (one and only) directory block into bp. */ | |
693fc8f6 | 343 | error = xfs_dir3_block_read(tp, dp, &bp); |
a2ceac1f | 344 | if (error) |
2bd0ea18 | 345 | return error; |
a2ceac1f | 346 | |
5e656dbb | 347 | len = xfs_dir2_data_entsize(args->namelen); |
a2ceac1f | 348 | |
2bd0ea18 NS |
349 | /* |
350 | * Set up pointers to parts of the block. | |
351 | */ | |
a2ceac1f DC |
352 | hdr = bp->b_addr; |
353 | btp = xfs_dir2_block_tail_p(mp, hdr); | |
5e656dbb | 354 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f | 355 | |
2bd0ea18 | 356 | /* |
a2ceac1f DC |
357 | * Find out if we can reuse stale entries or whether we need extra |
358 | * space for entry and new leaf. | |
2bd0ea18 | 359 | */ |
a2ceac1f DC |
360 | xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup, |
361 | &enddup, &compact, len); | |
362 | ||
2bd0ea18 | 363 | /* |
a2ceac1f | 364 | * Done everything we need for a space check now. |
2bd0ea18 | 365 | */ |
a2ceac1f DC |
366 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
367 | xfs_trans_brelse(tp, bp); | |
368 | if (!dup) | |
369 | return XFS_ERROR(ENOSPC); | |
370 | return 0; | |
2bd0ea18 | 371 | } |
a2ceac1f | 372 | |
2bd0ea18 NS |
373 | /* |
374 | * If we don't have space for the new entry & leaf ... | |
375 | */ | |
376 | if (!dup) { | |
a2ceac1f DC |
377 | /* Don't have a space reservation: return no-space. */ |
378 | if (args->total == 0) | |
2bd0ea18 NS |
379 | return XFS_ERROR(ENOSPC); |
380 | /* | |
381 | * Convert to the next larger format. | |
382 | * Then add the new entry in that format. | |
383 | */ | |
384 | error = xfs_dir2_block_to_leaf(args, bp); | |
2bd0ea18 NS |
385 | if (error) |
386 | return error; | |
387 | return xfs_dir2_leaf_addname(args); | |
388 | } | |
a2ceac1f | 389 | |
2bd0ea18 | 390 | needlog = needscan = 0; |
a2ceac1f | 391 | |
2bd0ea18 NS |
392 | /* |
393 | * If need to compact the leaf entries, do it now. | |
2bd0ea18 | 394 | */ |
49f693fa | 395 | if (compact) { |
a2ceac1f DC |
396 | xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog, |
397 | &lfloghigh, &lfloglow); | |
49f693fa DC |
398 | /* recalculate blp post-compaction */ |
399 | blp = xfs_dir2_block_leaf_p(btp); | |
400 | } else if (btp->stale) { | |
2bd0ea18 | 401 | /* |
a2ceac1f DC |
402 | * Set leaf logging boundaries to impossible state. |
403 | * For the no-stale case they're set explicitly. | |
2bd0ea18 | 404 | */ |
5e656dbb | 405 | lfloglow = be32_to_cpu(btp->count); |
2bd0ea18 NS |
406 | lfloghigh = -1; |
407 | } | |
a2ceac1f | 408 | |
2bd0ea18 NS |
409 | /* |
410 | * Find the slot that's first lower than our hash value, -1 if none. | |
411 | */ | |
5e656dbb | 412 | for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) { |
2bd0ea18 | 413 | mid = (low + high) >> 1; |
5e656dbb | 414 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
2bd0ea18 NS |
415 | break; |
416 | if (hash < args->hashval) | |
417 | low = mid + 1; | |
418 | else | |
419 | high = mid - 1; | |
420 | } | |
5e656dbb | 421 | while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) { |
2bd0ea18 NS |
422 | mid--; |
423 | } | |
424 | /* | |
425 | * No stale entries, will use enddup space to hold new leaf. | |
426 | */ | |
46eca962 | 427 | if (!btp->stale) { |
2bd0ea18 NS |
428 | /* |
429 | * Mark the space needed for the new leaf entry, now in use. | |
430 | */ | |
431 | xfs_dir2_data_use_free(tp, bp, enddup, | |
432 | (xfs_dir2_data_aoff_t) | |
a2ceac1f | 433 | ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) - |
2bd0ea18 NS |
434 | sizeof(*blp)), |
435 | (xfs_dir2_data_aoff_t)sizeof(*blp), | |
436 | &needlog, &needscan); | |
437 | /* | |
438 | * Update the tail (entry count). | |
439 | */ | |
5e656dbb | 440 | be32_add_cpu(&btp->count, 1); |
2bd0ea18 NS |
441 | /* |
442 | * If we now need to rebuild the bestfree map, do so. | |
443 | * This needs to happen before the next call to use_free. | |
444 | */ | |
445 | if (needscan) { | |
a2ceac1f | 446 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
2bd0ea18 NS |
447 | needscan = 0; |
448 | } | |
449 | /* | |
450 | * Adjust pointer to the first leaf entry, we're about to move | |
451 | * the table up one to open up space for the new leaf entry. | |
452 | * Then adjust our index to match. | |
453 | */ | |
454 | blp--; | |
455 | mid++; | |
456 | if (mid) | |
32181a02 | 457 | memmove(blp, &blp[1], mid * sizeof(*blp)); |
2bd0ea18 NS |
458 | lfloglow = 0; |
459 | lfloghigh = mid; | |
460 | } | |
461 | /* | |
462 | * Use a stale leaf for our new entry. | |
463 | */ | |
464 | else { | |
465 | for (lowstale = mid; | |
466 | lowstale >= 0 && | |
a2ceac1f DC |
467 | blp[lowstale].address != |
468 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR); | |
2bd0ea18 NS |
469 | lowstale--) |
470 | continue; | |
471 | for (highstale = mid + 1; | |
5e656dbb | 472 | highstale < be32_to_cpu(btp->count) && |
a2ceac1f DC |
473 | blp[highstale].address != |
474 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR) && | |
2bd0ea18 NS |
475 | (lowstale < 0 || mid - lowstale > highstale - mid); |
476 | highstale++) | |
477 | continue; | |
478 | /* | |
479 | * Move entries toward the low-numbered stale entry. | |
480 | */ | |
481 | if (lowstale >= 0 && | |
5e656dbb | 482 | (highstale == be32_to_cpu(btp->count) || |
2bd0ea18 NS |
483 | mid - lowstale <= highstale - mid)) { |
484 | if (mid - lowstale) | |
32181a02 | 485 | memmove(&blp[lowstale], &blp[lowstale + 1], |
2bd0ea18 NS |
486 | (mid - lowstale) * sizeof(*blp)); |
487 | lfloglow = MIN(lowstale, lfloglow); | |
488 | lfloghigh = MAX(mid, lfloghigh); | |
489 | } | |
490 | /* | |
491 | * Move entries toward the high-numbered stale entry. | |
492 | */ | |
493 | else { | |
5e656dbb | 494 | ASSERT(highstale < be32_to_cpu(btp->count)); |
2bd0ea18 NS |
495 | mid++; |
496 | if (highstale - mid) | |
32181a02 | 497 | memmove(&blp[mid + 1], &blp[mid], |
2bd0ea18 NS |
498 | (highstale - mid) * sizeof(*blp)); |
499 | lfloglow = MIN(mid, lfloglow); | |
500 | lfloghigh = MAX(highstale, lfloghigh); | |
501 | } | |
5e656dbb | 502 | be32_add_cpu(&btp->stale, -1); |
2bd0ea18 NS |
503 | } |
504 | /* | |
505 | * Point to the new data entry. | |
506 | */ | |
507 | dep = (xfs_dir2_data_entry_t *)dup; | |
508 | /* | |
509 | * Fill in the leaf entry. | |
510 | */ | |
5e656dbb BN |
511 | blp[mid].hashval = cpu_to_be32(args->hashval); |
512 | blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, | |
a2ceac1f | 513 | (char *)dep - (char *)hdr)); |
2bd0ea18 NS |
514 | xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh); |
515 | /* | |
516 | * Mark space for the data entry used. | |
517 | */ | |
518 | xfs_dir2_data_use_free(tp, bp, dup, | |
a2ceac1f | 519 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), |
2bd0ea18 NS |
520 | (xfs_dir2_data_aoff_t)len, &needlog, &needscan); |
521 | /* | |
522 | * Create the new data entry. | |
523 | */ | |
5e656dbb | 524 | dep->inumber = cpu_to_be64(args->inumber); |
2bd0ea18 | 525 | dep->namelen = args->namelen; |
32181a02 | 526 | memcpy(dep->name, args->name, args->namelen); |
5e656dbb | 527 | tagp = xfs_dir2_data_entry_tag_p(dep); |
a2ceac1f | 528 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
2bd0ea18 NS |
529 | /* |
530 | * Clean up the bestfree array and log the header, tail, and entry. | |
531 | */ | |
532 | if (needscan) | |
a2ceac1f | 533 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
2bd0ea18 NS |
534 | if (needlog) |
535 | xfs_dir2_data_log_header(tp, bp); | |
536 | xfs_dir2_block_log_tail(tp, bp); | |
537 | xfs_dir2_data_log_entry(tp, bp, dep); | |
538 | xfs_dir2_data_check(dp, bp); | |
2bd0ea18 NS |
539 | return 0; |
540 | } | |
541 | ||
542 | /* | |
543 | * Log leaf entries from the block. | |
544 | */ | |
5e656dbb | 545 | static void |
2bd0ea18 NS |
546 | xfs_dir2_block_log_leaf( |
547 | xfs_trans_t *tp, /* transaction structure */ | |
a2ceac1f | 548 | struct xfs_buf *bp, /* block buffer */ |
2bd0ea18 NS |
549 | int first, /* index of first logged leaf */ |
550 | int last) /* index of last logged leaf */ | |
551 | { | |
a2ceac1f DC |
552 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
553 | xfs_dir2_leaf_entry_t *blp; | |
554 | xfs_dir2_block_tail_t *btp; | |
2bd0ea18 | 555 | |
a2ceac1f | 556 | btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr); |
5e656dbb | 557 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f DC |
558 | xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr), |
559 | (uint)((char *)&blp[last + 1] - (char *)hdr - 1)); | |
2bd0ea18 NS |
560 | } |
561 | ||
562 | /* | |
563 | * Log the block tail. | |
564 | */ | |
5e656dbb | 565 | static void |
2bd0ea18 NS |
566 | xfs_dir2_block_log_tail( |
567 | xfs_trans_t *tp, /* transaction structure */ | |
a2ceac1f | 568 | struct xfs_buf *bp) /* block buffer */ |
2bd0ea18 | 569 | { |
a2ceac1f DC |
570 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
571 | xfs_dir2_block_tail_t *btp; | |
2bd0ea18 | 572 | |
a2ceac1f DC |
573 | btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr); |
574 | xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr), | |
575 | (uint)((char *)(btp + 1) - (char *)hdr - 1)); | |
2bd0ea18 NS |
576 | } |
577 | ||
578 | /* | |
579 | * Look up an entry in the block. This is the external routine, | |
580 | * xfs_dir2_block_lookup_int does the real work. | |
581 | */ | |
582 | int /* error */ | |
583 | xfs_dir2_block_lookup( | |
584 | xfs_da_args_t *args) /* dir lookup arguments */ | |
585 | { | |
a2ceac1f | 586 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 587 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 588 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
589 | xfs_dir2_block_tail_t *btp; /* block tail */ |
590 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
591 | xfs_inode_t *dp; /* incore inode */ | |
592 | int ent; /* entry index */ | |
593 | int error; /* error return value */ | |
594 | xfs_mount_t *mp; /* filesystem mount point */ | |
595 | ||
56b2de80 DC |
596 | trace_xfs_dir2_block_lookup(args); |
597 | ||
2bd0ea18 NS |
598 | /* |
599 | * Get the buffer, look up the entry. | |
600 | * If not found (ENOENT) then return, have no buffer. | |
601 | */ | |
0e266570 | 602 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) |
2bd0ea18 NS |
603 | return error; |
604 | dp = args->dp; | |
605 | mp = dp->i_mount; | |
a2ceac1f | 606 | hdr = bp->b_addr; |
2bd0ea18 | 607 | xfs_dir2_data_check(dp, bp); |
a2ceac1f | 608 | btp = xfs_dir2_block_tail_p(mp, hdr); |
5e656dbb | 609 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
610 | /* |
611 | * Get the offset from the leaf entry, to point to the data. | |
612 | */ | |
a2ceac1f | 613 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
5e656dbb | 614 | xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
2bd0ea18 | 615 | /* |
5e656dbb | 616 | * Fill in inode number, CI name if appropriate, release the block. |
2bd0ea18 | 617 | */ |
5e656dbb BN |
618 | args->inumber = be64_to_cpu(dep->inumber); |
619 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); | |
a2ceac1f | 620 | xfs_trans_brelse(args->trans, bp); |
5e656dbb | 621 | return XFS_ERROR(error); |
2bd0ea18 NS |
622 | } |
623 | ||
624 | /* | |
625 | * Internal block lookup routine. | |
626 | */ | |
5e656dbb | 627 | static int /* error */ |
2bd0ea18 NS |
628 | xfs_dir2_block_lookup_int( |
629 | xfs_da_args_t *args, /* dir lookup arguments */ | |
a2ceac1f | 630 | struct xfs_buf **bpp, /* returned block buffer */ |
2bd0ea18 NS |
631 | int *entno) /* returned entry number */ |
632 | { | |
633 | xfs_dir2_dataptr_t addr; /* data entry address */ | |
a2ceac1f | 634 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 635 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 636 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
637 | xfs_dir2_block_tail_t *btp; /* block tail */ |
638 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
639 | xfs_inode_t *dp; /* incore inode */ | |
640 | int error; /* error return value */ | |
641 | xfs_dahash_t hash; /* found hash value */ | |
642 | int high; /* binary search high index */ | |
643 | int low; /* binary search low index */ | |
644 | int mid; /* binary search current idx */ | |
645 | xfs_mount_t *mp; /* filesystem mount point */ | |
646 | xfs_trans_t *tp; /* transaction pointer */ | |
5e656dbb | 647 | enum xfs_dacmp cmp; /* comparison result */ |
2bd0ea18 NS |
648 | |
649 | dp = args->dp; | |
650 | tp = args->trans; | |
651 | mp = dp->i_mount; | |
a2ceac1f | 652 | |
693fc8f6 | 653 | error = xfs_dir3_block_read(tp, dp, &bp); |
a2ceac1f | 654 | if (error) |
2bd0ea18 | 655 | return error; |
a2ceac1f DC |
656 | |
657 | hdr = bp->b_addr; | |
2bd0ea18 | 658 | xfs_dir2_data_check(dp, bp); |
a2ceac1f | 659 | btp = xfs_dir2_block_tail_p(mp, hdr); |
5e656dbb | 660 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
661 | /* |
662 | * Loop doing a binary search for our hash value. | |
663 | * Find our entry, ENOENT if it's not there. | |
664 | */ | |
5e656dbb | 665 | for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) { |
2bd0ea18 NS |
666 | ASSERT(low <= high); |
667 | mid = (low + high) >> 1; | |
5e656dbb | 668 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
2bd0ea18 NS |
669 | break; |
670 | if (hash < args->hashval) | |
671 | low = mid + 1; | |
672 | else | |
673 | high = mid - 1; | |
674 | if (low > high) { | |
5e656dbb | 675 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
a2ceac1f | 676 | xfs_trans_brelse(tp, bp); |
2bd0ea18 NS |
677 | return XFS_ERROR(ENOENT); |
678 | } | |
679 | } | |
680 | /* | |
681 | * Back up to the first one with the right hash value. | |
682 | */ | |
5e656dbb | 683 | while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) { |
2bd0ea18 NS |
684 | mid--; |
685 | } | |
686 | /* | |
687 | * Now loop forward through all the entries with the | |
688 | * right hash value looking for our name. | |
689 | */ | |
690 | do { | |
5e656dbb | 691 | if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR) |
2bd0ea18 NS |
692 | continue; |
693 | /* | |
694 | * Get pointer to the entry from the leaf. | |
695 | */ | |
696 | dep = (xfs_dir2_data_entry_t *) | |
a2ceac1f | 697 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr)); |
2bd0ea18 | 698 | /* |
5e656dbb BN |
699 | * Compare name and if it's an exact match, return the index |
700 | * and buffer. If it's the first case-insensitive match, store | |
701 | * the index and buffer and continue looking for an exact match. | |
2bd0ea18 | 702 | */ |
5e656dbb BN |
703 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
704 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { | |
705 | args->cmpresult = cmp; | |
2bd0ea18 NS |
706 | *bpp = bp; |
707 | *entno = mid; | |
5e656dbb BN |
708 | if (cmp == XFS_CMP_EXACT) |
709 | return 0; | |
2bd0ea18 | 710 | } |
5e656dbb BN |
711 | } while (++mid < be32_to_cpu(btp->count) && |
712 | be32_to_cpu(blp[mid].hashval) == hash); | |
713 | ||
714 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); | |
715 | /* | |
716 | * Here, we can only be doing a lookup (not a rename or replace). | |
717 | * If a case-insensitive match was found earlier, return success. | |
718 | */ | |
719 | if (args->cmpresult == XFS_CMP_CASE) | |
720 | return 0; | |
2bd0ea18 NS |
721 | /* |
722 | * No match, release the buffer and return ENOENT. | |
723 | */ | |
a2ceac1f | 724 | xfs_trans_brelse(tp, bp); |
2bd0ea18 NS |
725 | return XFS_ERROR(ENOENT); |
726 | } | |
727 | ||
728 | /* | |
729 | * Remove an entry from a block format directory. | |
730 | * If that makes the block small enough to fit in shortform, transform it. | |
731 | */ | |
732 | int /* error */ | |
733 | xfs_dir2_block_removename( | |
734 | xfs_da_args_t *args) /* directory operation args */ | |
735 | { | |
a2ceac1f | 736 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 737 | xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */ |
a2ceac1f | 738 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
739 | xfs_dir2_block_tail_t *btp; /* block tail */ |
740 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
741 | xfs_inode_t *dp; /* incore inode */ | |
742 | int ent; /* block leaf entry index */ | |
743 | int error; /* error return value */ | |
744 | xfs_mount_t *mp; /* filesystem mount point */ | |
745 | int needlog; /* need to log block header */ | |
746 | int needscan; /* need to fixup bestfree */ | |
747 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ | |
748 | int size; /* shortform size */ | |
749 | xfs_trans_t *tp; /* transaction pointer */ | |
750 | ||
56b2de80 DC |
751 | trace_xfs_dir2_block_removename(args); |
752 | ||
2bd0ea18 NS |
753 | /* |
754 | * Look up the entry in the block. Gets the buffer and entry index. | |
755 | * It will always be there, the vnodeops level does a lookup first. | |
756 | */ | |
0e266570 | 757 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
2bd0ea18 NS |
758 | return error; |
759 | } | |
760 | dp = args->dp; | |
761 | tp = args->trans; | |
762 | mp = dp->i_mount; | |
a2ceac1f DC |
763 | hdr = bp->b_addr; |
764 | btp = xfs_dir2_block_tail_p(mp, hdr); | |
5e656dbb | 765 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
766 | /* |
767 | * Point to the data entry using the leaf entry. | |
768 | */ | |
769 | dep = (xfs_dir2_data_entry_t *) | |
a2ceac1f | 770 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
2bd0ea18 NS |
771 | /* |
772 | * Mark the data entry's space free. | |
773 | */ | |
774 | needlog = needscan = 0; | |
775 | xfs_dir2_data_make_free(tp, bp, | |
a2ceac1f | 776 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), |
5e656dbb | 777 | xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan); |
2bd0ea18 NS |
778 | /* |
779 | * Fix up the block tail. | |
780 | */ | |
5e656dbb | 781 | be32_add_cpu(&btp->stale, 1); |
2bd0ea18 NS |
782 | xfs_dir2_block_log_tail(tp, bp); |
783 | /* | |
784 | * Remove the leaf entry by marking it stale. | |
785 | */ | |
5e656dbb | 786 | blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
2bd0ea18 NS |
787 | xfs_dir2_block_log_leaf(tp, bp, ent, ent); |
788 | /* | |
789 | * Fix up bestfree, log the header if necessary. | |
790 | */ | |
791 | if (needscan) | |
a2ceac1f | 792 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
2bd0ea18 NS |
793 | if (needlog) |
794 | xfs_dir2_data_log_header(tp, bp); | |
795 | xfs_dir2_data_check(dp, bp); | |
796 | /* | |
797 | * See if the size as a shortform is good enough. | |
798 | */ | |
a2ceac1f DC |
799 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
800 | if (size > XFS_IFORK_DSIZE(dp)) | |
2bd0ea18 | 801 | return 0; |
a2ceac1f | 802 | |
2bd0ea18 NS |
803 | /* |
804 | * If it works, do the conversion. | |
805 | */ | |
806 | return xfs_dir2_block_to_sf(args, bp, size, &sfh); | |
807 | } | |
808 | ||
809 | /* | |
810 | * Replace an entry in a V2 block directory. | |
811 | * Change the inode number to the new value. | |
812 | */ | |
813 | int /* error */ | |
814 | xfs_dir2_block_replace( | |
815 | xfs_da_args_t *args) /* directory operation args */ | |
816 | { | |
a2ceac1f | 817 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 818 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 819 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
820 | xfs_dir2_block_tail_t *btp; /* block tail */ |
821 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
822 | xfs_inode_t *dp; /* incore inode */ | |
823 | int ent; /* leaf entry index */ | |
824 | int error; /* error return value */ | |
825 | xfs_mount_t *mp; /* filesystem mount point */ | |
826 | ||
56b2de80 DC |
827 | trace_xfs_dir2_block_replace(args); |
828 | ||
2bd0ea18 NS |
829 | /* |
830 | * Lookup the entry in the directory. Get buffer and entry index. | |
831 | * This will always succeed since the caller has already done a lookup. | |
832 | */ | |
0e266570 | 833 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
2bd0ea18 NS |
834 | return error; |
835 | } | |
836 | dp = args->dp; | |
837 | mp = dp->i_mount; | |
a2ceac1f DC |
838 | hdr = bp->b_addr; |
839 | btp = xfs_dir2_block_tail_p(mp, hdr); | |
5e656dbb | 840 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
841 | /* |
842 | * Point to the data entry we need to change. | |
843 | */ | |
844 | dep = (xfs_dir2_data_entry_t *) | |
a2ceac1f | 845 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
5e656dbb | 846 | ASSERT(be64_to_cpu(dep->inumber) != args->inumber); |
2bd0ea18 NS |
847 | /* |
848 | * Change the inode number to the new value. | |
849 | */ | |
5e656dbb | 850 | dep->inumber = cpu_to_be64(args->inumber); |
2bd0ea18 NS |
851 | xfs_dir2_data_log_entry(args->trans, bp, dep); |
852 | xfs_dir2_data_check(dp, bp); | |
2bd0ea18 NS |
853 | return 0; |
854 | } | |
855 | ||
856 | /* | |
857 | * Qsort comparison routine for the block leaf entries. | |
858 | */ | |
859 | static int /* sort order */ | |
860 | xfs_dir2_block_sort( | |
861 | const void *a, /* first leaf entry */ | |
862 | const void *b) /* second leaf entry */ | |
863 | { | |
864 | const xfs_dir2_leaf_entry_t *la; /* first leaf entry */ | |
865 | const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */ | |
866 | ||
867 | la = a; | |
868 | lb = b; | |
5e656dbb BN |
869 | return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 : |
870 | (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0); | |
2bd0ea18 NS |
871 | } |
872 | ||
873 | /* | |
874 | * Convert a V2 leaf directory to a V2 block directory if possible. | |
875 | */ | |
876 | int /* error */ | |
877 | xfs_dir2_leaf_to_block( | |
878 | xfs_da_args_t *args, /* operation arguments */ | |
a2ceac1f DC |
879 | struct xfs_buf *lbp, /* leaf buffer */ |
880 | struct xfs_buf *dbp) /* data buffer */ | |
2bd0ea18 | 881 | { |
5e656dbb | 882 | __be16 *bestsp; /* leaf bests table */ |
a2ceac1f | 883 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 NS |
884 | xfs_dir2_block_tail_t *btp; /* block tail */ |
885 | xfs_inode_t *dp; /* incore directory inode */ | |
886 | xfs_dir2_data_unused_t *dup; /* unused data entry */ | |
887 | int error; /* error return value */ | |
888 | int from; /* leaf from index */ | |
889 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
890 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
891 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
892 | xfs_mount_t *mp; /* file system mount point */ | |
893 | int needlog; /* need to log data header */ | |
894 | int needscan; /* need to scan for bestfree */ | |
895 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ | |
896 | int size; /* bytes used */ | |
5e656dbb | 897 | __be16 *tagp; /* end of entry (tag) */ |
2bd0ea18 NS |
898 | int to; /* block/leaf to index */ |
899 | xfs_trans_t *tp; /* transaction pointer */ | |
900 | ||
56b2de80 DC |
901 | trace_xfs_dir2_leaf_to_block(args); |
902 | ||
2bd0ea18 NS |
903 | dp = args->dp; |
904 | tp = args->trans; | |
905 | mp = dp->i_mount; | |
a2ceac1f DC |
906 | leaf = lbp->b_addr; |
907 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC)); | |
5e656dbb | 908 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
2bd0ea18 NS |
909 | /* |
910 | * If there are data blocks other than the first one, take this | |
911 | * opportunity to remove trailing empty data blocks that may have | |
912 | * been left behind during no-space-reservation operations. | |
913 | * These will show up in the leaf bests table. | |
914 | */ | |
915 | while (dp->i_d.di_size > mp->m_dirblksize) { | |
693fc8f6 DC |
916 | int hdrsz; |
917 | ||
918 | hdrsz = xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb)); | |
5e656dbb BN |
919 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
920 | if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) == | |
693fc8f6 | 921 | mp->m_dirblksize - hdrsz) { |
0e266570 | 922 | if ((error = |
2bd0ea18 | 923 | xfs_dir2_leaf_trim_data(args, lbp, |
5e656dbb | 924 | (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1)))) |
a2ceac1f DC |
925 | return error; |
926 | } else | |
927 | return 0; | |
2bd0ea18 NS |
928 | } |
929 | /* | |
930 | * Read the data block if we don't already have it, give up if it fails. | |
931 | */ | |
a2ceac1f DC |
932 | if (!dbp) { |
933 | error = xfs_dir2_data_read(tp, dp, mp->m_dirdatablk, -1, &dbp); | |
934 | if (error) | |
935 | return error; | |
2bd0ea18 | 936 | } |
a2ceac1f DC |
937 | hdr = dbp->b_addr; |
938 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC)); | |
2bd0ea18 NS |
939 | /* |
940 | * Size of the "leaf" area in the block. | |
941 | */ | |
a2ceac1f | 942 | size = (uint)sizeof(xfs_dir2_block_tail_t) + |
5e656dbb | 943 | (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)); |
2bd0ea18 NS |
944 | /* |
945 | * Look at the last data entry. | |
946 | */ | |
a2ceac1f DC |
947 | tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1; |
948 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
2bd0ea18 NS |
949 | /* |
950 | * If it's not free or is too short we can't do it. | |
951 | */ | |
5e656dbb | 952 | if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG || |
a2ceac1f DC |
953 | be16_to_cpu(dup->length) < size) |
954 | return 0; | |
955 | ||
2bd0ea18 NS |
956 | /* |
957 | * Start converting it to block form. | |
958 | */ | |
693fc8f6 DC |
959 | xfs_dir3_block_init(mp, dbp, dp); |
960 | ||
2bd0ea18 NS |
961 | needlog = 1; |
962 | needscan = 0; | |
963 | /* | |
964 | * Use up the space at the end of the block (blp/btp). | |
965 | */ | |
966 | xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size, | |
967 | &needlog, &needscan); | |
968 | /* | |
969 | * Initialize the block tail. | |
970 | */ | |
a2ceac1f | 971 | btp = xfs_dir2_block_tail_p(mp, hdr); |
5e656dbb | 972 | btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)); |
46eca962 | 973 | btp->stale = 0; |
2bd0ea18 NS |
974 | xfs_dir2_block_log_tail(tp, dbp); |
975 | /* | |
976 | * Initialize the block leaf area. We compact out stale entries. | |
977 | */ | |
5e656dbb BN |
978 | lep = xfs_dir2_block_leaf_p(btp); |
979 | for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) { | |
a2ceac1f DC |
980 | if (leaf->ents[from].address == |
981 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) | |
2bd0ea18 NS |
982 | continue; |
983 | lep[to++] = leaf->ents[from]; | |
984 | } | |
5e656dbb BN |
985 | ASSERT(to == be32_to_cpu(btp->count)); |
986 | xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1); | |
2bd0ea18 NS |
987 | /* |
988 | * Scan the bestfree if we need it and log the data block header. | |
989 | */ | |
990 | if (needscan) | |
a2ceac1f | 991 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
2bd0ea18 NS |
992 | if (needlog) |
993 | xfs_dir2_data_log_header(tp, dbp); | |
994 | /* | |
995 | * Pitch the old leaf block. | |
996 | */ | |
997 | error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp); | |
a2ceac1f DC |
998 | if (error) |
999 | return error; | |
1000 | ||
2bd0ea18 NS |
1001 | /* |
1002 | * Now see if the resulting block can be shrunken to shortform. | |
1003 | */ | |
a2ceac1f DC |
1004 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
1005 | if (size > XFS_IFORK_DSIZE(dp)) | |
1006 | return 0; | |
1007 | ||
2bd0ea18 | 1008 | return xfs_dir2_block_to_sf(args, dbp, size, &sfh); |
2bd0ea18 NS |
1009 | } |
1010 | ||
1011 | /* | |
1012 | * Convert the shortform directory to block form. | |
1013 | */ | |
1014 | int /* error */ | |
1015 | xfs_dir2_sf_to_block( | |
1016 | xfs_da_args_t *args) /* operation arguments */ | |
1017 | { | |
1018 | xfs_dir2_db_t blkno; /* dir-relative block # (0) */ | |
a2ceac1f | 1019 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 1020 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 1021 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 | 1022 | xfs_dir2_block_tail_t *btp; /* block tail pointer */ |
2bd0ea18 NS |
1023 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
1024 | xfs_inode_t *dp; /* incore directory inode */ | |
1025 | int dummy; /* trash */ | |
1026 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ | |
1027 | int endoffset; /* end of data objects */ | |
1028 | int error; /* error return value */ | |
1029 | int i; /* index */ | |
1030 | xfs_mount_t *mp; /* filesystem mount point */ | |
1031 | int needlog; /* need to log block header */ | |
1032 | int needscan; /* need to scan block freespc */ | |
1033 | int newoffset; /* offset from current entry */ | |
1034 | int offset; /* target block offset */ | |
1035 | xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */ | |
a2ceac1f DC |
1036 | xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */ |
1037 | xfs_dir2_sf_hdr_t *sfp; /* shortform header */ | |
5e656dbb | 1038 | __be16 *tagp; /* end of data entry */ |
2bd0ea18 | 1039 | xfs_trans_t *tp; /* transaction pointer */ |
5e656dbb | 1040 | struct xfs_name name; |
2bd0ea18 | 1041 | |
56b2de80 DC |
1042 | trace_xfs_dir2_sf_to_block(args); |
1043 | ||
2bd0ea18 NS |
1044 | dp = args->dp; |
1045 | tp = args->trans; | |
1046 | mp = dp->i_mount; | |
1047 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); | |
1048 | /* | |
1049 | * Bomb out if the shortform directory is way too short. | |
1050 | */ | |
1051 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { | |
2bd0ea18 NS |
1052 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
1053 | return XFS_ERROR(EIO); | |
1054 | } | |
a2ceac1f DC |
1055 | |
1056 | oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data; | |
1057 | ||
2bd0ea18 NS |
1058 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); |
1059 | ASSERT(dp->i_df.if_u1.if_data != NULL); | |
a2ceac1f DC |
1060 | ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count)); |
1061 | ||
2bd0ea18 | 1062 | /* |
a2ceac1f | 1063 | * Copy the directory into a temporary buffer. |
2bd0ea18 NS |
1064 | * Then pitch the incore inode data so we can make extents. |
1065 | */ | |
a2ceac1f DC |
1066 | sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP); |
1067 | memcpy(sfp, oldsfp, dp->i_df.if_bytes); | |
d663096d | 1068 | |
a2ceac1f | 1069 | xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK); |
2bd0ea18 NS |
1070 | dp->i_d.di_size = 0; |
1071 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
a2ceac1f | 1072 | |
2bd0ea18 NS |
1073 | /* |
1074 | * Add block 0 to the inode. | |
1075 | */ | |
1076 | error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno); | |
1077 | if (error) { | |
a2ceac1f | 1078 | kmem_free(sfp); |
2bd0ea18 NS |
1079 | return error; |
1080 | } | |
1081 | /* | |
693fc8f6 | 1082 | * Initialize the data block, then convert it to block format. |
2bd0ea18 | 1083 | */ |
693fc8f6 | 1084 | error = xfs_dir3_data_init(args, blkno, &bp); |
2bd0ea18 | 1085 | if (error) { |
a2ceac1f | 1086 | kmem_free(sfp); |
2bd0ea18 NS |
1087 | return error; |
1088 | } | |
693fc8f6 | 1089 | xfs_dir3_block_init(mp, bp, dp); |
a2ceac1f | 1090 | hdr = bp->b_addr; |
693fc8f6 | 1091 | |
2bd0ea18 NS |
1092 | /* |
1093 | * Compute size of block "tail" area. | |
1094 | */ | |
1095 | i = (uint)sizeof(*btp) + | |
a2ceac1f | 1096 | (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t); |
2bd0ea18 NS |
1097 | /* |
1098 | * The whole thing is initialized to free by the init routine. | |
1099 | * Say we're using the leaf and tail area. | |
1100 | */ | |
693fc8f6 | 1101 | dup = xfs_dir3_data_unused_p(hdr); |
2bd0ea18 NS |
1102 | needlog = needscan = 0; |
1103 | xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog, | |
1104 | &needscan); | |
1105 | ASSERT(needscan == 0); | |
1106 | /* | |
1107 | * Fill in the tail. | |
1108 | */ | |
a2ceac1f DC |
1109 | btp = xfs_dir2_block_tail_p(mp, hdr); |
1110 | btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */ | |
46eca962 | 1111 | btp->stale = 0; |
5e656dbb | 1112 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f | 1113 | endoffset = (uint)((char *)blp - (char *)hdr); |
2bd0ea18 NS |
1114 | /* |
1115 | * Remove the freespace, we'll manage it. | |
1116 | */ | |
1117 | xfs_dir2_data_use_free(tp, bp, dup, | |
a2ceac1f | 1118 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), |
5e656dbb | 1119 | be16_to_cpu(dup->length), &needlog, &needscan); |
2bd0ea18 NS |
1120 | /* |
1121 | * Create entry for . | |
1122 | */ | |
693fc8f6 | 1123 | dep = xfs_dir3_data_dot_entry_p(hdr); |
5e656dbb | 1124 | dep->inumber = cpu_to_be64(dp->i_ino); |
2bd0ea18 NS |
1125 | dep->namelen = 1; |
1126 | dep->name[0] = '.'; | |
5e656dbb | 1127 | tagp = xfs_dir2_data_entry_tag_p(dep); |
a2ceac1f | 1128 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
2bd0ea18 | 1129 | xfs_dir2_data_log_entry(tp, bp, dep); |
5e656dbb BN |
1130 | blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot); |
1131 | blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, | |
a2ceac1f | 1132 | (char *)dep - (char *)hdr)); |
2bd0ea18 NS |
1133 | /* |
1134 | * Create entry for .. | |
1135 | */ | |
693fc8f6 | 1136 | dep = xfs_dir3_data_dotdot_entry_p(hdr); |
a2ceac1f | 1137 | dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp)); |
2bd0ea18 NS |
1138 | dep->namelen = 2; |
1139 | dep->name[0] = dep->name[1] = '.'; | |
5e656dbb | 1140 | tagp = xfs_dir2_data_entry_tag_p(dep); |
a2ceac1f | 1141 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
2bd0ea18 | 1142 | xfs_dir2_data_log_entry(tp, bp, dep); |
5e656dbb BN |
1143 | blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot); |
1144 | blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, | |
a2ceac1f | 1145 | (char *)dep - (char *)hdr)); |
693fc8f6 | 1146 | offset = xfs_dir3_data_first_offset(hdr); |
2bd0ea18 NS |
1147 | /* |
1148 | * Loop over existing entries, stuff them in. | |
1149 | */ | |
a2ceac1f DC |
1150 | i = 0; |
1151 | if (!sfp->count) | |
2bd0ea18 NS |
1152 | sfep = NULL; |
1153 | else | |
5e656dbb | 1154 | sfep = xfs_dir2_sf_firstentry(sfp); |
2bd0ea18 NS |
1155 | /* |
1156 | * Need to preserve the existing offset values in the sf directory. | |
1157 | * Insert holes (unused entries) where necessary. | |
1158 | */ | |
1159 | while (offset < endoffset) { | |
1160 | /* | |
1161 | * sfep is null when we reach the end of the list. | |
1162 | */ | |
1163 | if (sfep == NULL) | |
1164 | newoffset = endoffset; | |
1165 | else | |
5e656dbb | 1166 | newoffset = xfs_dir2_sf_get_offset(sfep); |
2bd0ea18 NS |
1167 | /* |
1168 | * There should be a hole here, make one. | |
1169 | */ | |
1170 | if (offset < newoffset) { | |
a2ceac1f | 1171 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
5e656dbb BN |
1172 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
1173 | dup->length = cpu_to_be16(newoffset - offset); | |
1174 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16( | |
a2ceac1f | 1175 | ((char *)dup - (char *)hdr)); |
2bd0ea18 | 1176 | xfs_dir2_data_log_unused(tp, bp, dup); |
a2ceac1f | 1177 | xfs_dir2_data_freeinsert(hdr, dup, &dummy); |
5e656dbb | 1178 | offset += be16_to_cpu(dup->length); |
2bd0ea18 NS |
1179 | continue; |
1180 | } | |
1181 | /* | |
1182 | * Copy a real entry. | |
1183 | */ | |
a2ceac1f DC |
1184 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset); |
1185 | dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep)); | |
2bd0ea18 | 1186 | dep->namelen = sfep->namelen; |
32181a02 | 1187 | memcpy(dep->name, sfep->name, dep->namelen); |
5e656dbb | 1188 | tagp = xfs_dir2_data_entry_tag_p(dep); |
a2ceac1f | 1189 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
2bd0ea18 | 1190 | xfs_dir2_data_log_entry(tp, bp, dep); |
5e656dbb BN |
1191 | name.name = sfep->name; |
1192 | name.len = sfep->namelen; | |
1193 | blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops-> | |
1194 | hashname(&name)); | |
51ca7008 | 1195 | blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
a2ceac1f DC |
1196 | (char *)dep - (char *)hdr)); |
1197 | offset = (int)((char *)(tagp + 1) - (char *)hdr); | |
1198 | if (++i == sfp->count) | |
2bd0ea18 NS |
1199 | sfep = NULL; |
1200 | else | |
5e656dbb | 1201 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); |
2bd0ea18 | 1202 | } |
d663096d | 1203 | /* Done with the temporary buffer */ |
a2ceac1f | 1204 | kmem_free(sfp); |
2bd0ea18 NS |
1205 | /* |
1206 | * Sort the leaf entries by hash value. | |
1207 | */ | |
5e656dbb | 1208 | xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort); |
5000d01d | 1209 | /* |
2bd0ea18 NS |
1210 | * Log the leaf entry area and tail. |
1211 | * Already logged the header in data_init, ignore needlog. | |
1212 | */ | |
1213 | ASSERT(needscan == 0); | |
5e656dbb | 1214 | xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1); |
2bd0ea18 NS |
1215 | xfs_dir2_block_log_tail(tp, bp); |
1216 | xfs_dir2_data_check(dp, bp); | |
2bd0ea18 NS |
1217 | return 0; |
1218 | } |