]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_dir2_leaf.c
mkfs: use geometry generation / helper functions
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_leaf.c
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
4 * All Rights Reserved.
5 *
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
8 * published by the Free Software Foundation.
9 *
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.
14 *
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
18 */
19 #include "libxfs_priv.h"
20 #include "xfs_fs.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_da_format.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_inode.h"
28 #include "xfs_bmap.h"
29 #include "xfs_dir2.h"
30 #include "xfs_dir2_priv.h"
31 #include "xfs_trace.h"
32 #include "xfs_trans.h"
33 #include "xfs_cksum.h"
34
35 /*
36 * Local function declarations.
37 */
38 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
39 int *indexp, struct xfs_buf **dbpp);
40 static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
41 struct xfs_buf *bp, int first, int last);
42 static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
43 struct xfs_buf *bp);
44
45 /*
46 * Check the internal consistency of a leaf1 block.
47 * Pop an assert if something is wrong.
48 */
49 #ifdef DEBUG
50 static xfs_failaddr_t
51 xfs_dir3_leaf1_check(
52 struct xfs_inode *dp,
53 struct xfs_buf *bp)
54 {
55 struct xfs_dir2_leaf *leaf = bp->b_addr;
56 struct xfs_dir3_icleaf_hdr leafhdr;
57
58 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
59
60 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
61 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
62 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
63 return __this_address;
64 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
65 return __this_address;
66
67 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
68 }
69
70 static inline void
71 xfs_dir3_leaf_check(
72 struct xfs_inode *dp,
73 struct xfs_buf *bp)
74 {
75 xfs_failaddr_t fa;
76
77 fa = xfs_dir3_leaf1_check(dp, bp);
78 if (!fa)
79 return;
80 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
81 bp->b_addr, __FILE__, __LINE__, fa);
82 ASSERT(0);
83 }
84 #else
85 #define xfs_dir3_leaf_check(dp, bp)
86 #endif
87
88 xfs_failaddr_t
89 xfs_dir3_leaf_check_int(
90 struct xfs_mount *mp,
91 struct xfs_inode *dp,
92 struct xfs_dir3_icleaf_hdr *hdr,
93 struct xfs_dir2_leaf *leaf)
94 {
95 struct xfs_dir2_leaf_entry *ents;
96 xfs_dir2_leaf_tail_t *ltp;
97 int stale;
98 int i;
99 const struct xfs_dir_ops *ops;
100 struct xfs_dir3_icleaf_hdr leafhdr;
101 struct xfs_da_geometry *geo = mp->m_dir_geo;
102
103 /*
104 * we can be passed a null dp here from a verifier, so we need to go the
105 * hard way to get them.
106 */
107 ops = xfs_dir_get_ops(mp, dp);
108
109 if (!hdr) {
110 ops->leaf_hdr_from_disk(&leafhdr, leaf);
111 hdr = &leafhdr;
112 }
113
114 ents = ops->leaf_ents_p(leaf);
115 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
116
117 /*
118 * XXX (dgc): This value is not restrictive enough.
119 * Should factor in the size of the bests table as well.
120 * We can deduce a value for that from di_size.
121 */
122 if (hdr->count > ops->leaf_max_ents(geo))
123 return __this_address;
124
125 /* Leaves and bests don't overlap in leaf format. */
126 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
127 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
128 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
129 return __this_address;
130
131 /* Check hash value order, count stale entries. */
132 for (i = stale = 0; i < hdr->count; i++) {
133 if (i + 1 < hdr->count) {
134 if (be32_to_cpu(ents[i].hashval) >
135 be32_to_cpu(ents[i + 1].hashval))
136 return __this_address;
137 }
138 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
139 stale++;
140 }
141 if (hdr->stale != stale)
142 return __this_address;
143 return NULL;
144 }
145
146 /*
147 * We verify the magic numbers before decoding the leaf header so that on debug
148 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
149 * to incorrect magic numbers.
150 */
151 static xfs_failaddr_t
152 xfs_dir3_leaf_verify(
153 struct xfs_buf *bp,
154 uint16_t magic)
155 {
156 struct xfs_mount *mp = bp->b_target->bt_mount;
157 struct xfs_dir2_leaf *leaf = bp->b_addr;
158
159 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
160
161 if (xfs_sb_version_hascrc(&mp->m_sb)) {
162 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
163 uint16_t magic3;
164
165 magic3 = (magic == XFS_DIR2_LEAF1_MAGIC) ? XFS_DIR3_LEAF1_MAGIC
166 : XFS_DIR3_LEAFN_MAGIC;
167
168 if (leaf3->info.hdr.magic != cpu_to_be16(magic3))
169 return __this_address;
170 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid))
171 return __this_address;
172 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
173 return __this_address;
174 if (!xfs_log_check_lsn(mp, be64_to_cpu(leaf3->info.lsn)))
175 return __this_address;
176 } else {
177 if (leaf->hdr.info.magic != cpu_to_be16(magic))
178 return __this_address;
179 }
180
181 return xfs_dir3_leaf_check_int(mp, NULL, NULL, leaf);
182 }
183
184 static void
185 __read_verify(
186 struct xfs_buf *bp,
187 uint16_t magic)
188 {
189 struct xfs_mount *mp = bp->b_target->bt_mount;
190 xfs_failaddr_t fa;
191
192 if (xfs_sb_version_hascrc(&mp->m_sb) &&
193 !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
194 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
195 else {
196 fa = xfs_dir3_leaf_verify(bp, magic);
197 if (fa)
198 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
199 }
200 }
201
202 static void
203 __write_verify(
204 struct xfs_buf *bp,
205 uint16_t magic)
206 {
207 struct xfs_mount *mp = bp->b_target->bt_mount;
208 struct xfs_buf_log_item *bip = bp->b_log_item;
209 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
210 xfs_failaddr_t fa;
211
212 fa = xfs_dir3_leaf_verify(bp, magic);
213 if (fa) {
214 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
215 return;
216 }
217
218 if (!xfs_sb_version_hascrc(&mp->m_sb))
219 return;
220
221 if (bip)
222 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
223
224 xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
225 }
226
227 static xfs_failaddr_t
228 xfs_dir3_leaf1_verify(
229 struct xfs_buf *bp)
230 {
231 return xfs_dir3_leaf_verify(bp, XFS_DIR2_LEAF1_MAGIC);
232 }
233
234 static void
235 xfs_dir3_leaf1_read_verify(
236 struct xfs_buf *bp)
237 {
238 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
239 }
240
241 static void
242 xfs_dir3_leaf1_write_verify(
243 struct xfs_buf *bp)
244 {
245 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
246 }
247
248 static xfs_failaddr_t
249 xfs_dir3_leafn_verify(
250 struct xfs_buf *bp)
251 {
252 return xfs_dir3_leaf_verify(bp, XFS_DIR2_LEAFN_MAGIC);
253 }
254
255 static void
256 xfs_dir3_leafn_read_verify(
257 struct xfs_buf *bp)
258 {
259 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
260 }
261
262 static void
263 xfs_dir3_leafn_write_verify(
264 struct xfs_buf *bp)
265 {
266 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
267 }
268
269 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
270 .name = "xfs_dir3_leaf1",
271 .verify_read = xfs_dir3_leaf1_read_verify,
272 .verify_write = xfs_dir3_leaf1_write_verify,
273 .verify_struct = xfs_dir3_leaf1_verify,
274 };
275
276 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
277 .name = "xfs_dir3_leafn",
278 .verify_read = xfs_dir3_leafn_read_verify,
279 .verify_write = xfs_dir3_leafn_write_verify,
280 .verify_struct = xfs_dir3_leafn_verify,
281 };
282
283 int
284 xfs_dir3_leaf_read(
285 struct xfs_trans *tp,
286 struct xfs_inode *dp,
287 xfs_dablk_t fbno,
288 xfs_daddr_t mappedbno,
289 struct xfs_buf **bpp)
290 {
291 int err;
292
293 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
294 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
295 if (!err && tp && *bpp)
296 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
297 return err;
298 }
299
300 int
301 xfs_dir3_leafn_read(
302 struct xfs_trans *tp,
303 struct xfs_inode *dp,
304 xfs_dablk_t fbno,
305 xfs_daddr_t mappedbno,
306 struct xfs_buf **bpp)
307 {
308 int err;
309
310 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
311 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
312 if (!err && tp && *bpp)
313 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
314 return err;
315 }
316
317 /*
318 * Initialize a new leaf block, leaf1 or leafn magic accepted.
319 */
320 static void
321 xfs_dir3_leaf_init(
322 struct xfs_mount *mp,
323 struct xfs_trans *tp,
324 struct xfs_buf *bp,
325 xfs_ino_t owner,
326 uint16_t type)
327 {
328 struct xfs_dir2_leaf *leaf = bp->b_addr;
329
330 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
331
332 if (xfs_sb_version_hascrc(&mp->m_sb)) {
333 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
334
335 memset(leaf3, 0, sizeof(*leaf3));
336
337 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
338 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
339 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
340 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
341 leaf3->info.owner = cpu_to_be64(owner);
342 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
343 } else {
344 memset(leaf, 0, sizeof(*leaf));
345 leaf->hdr.info.magic = cpu_to_be16(type);
346 }
347
348 /*
349 * If it's a leaf-format directory initialize the tail.
350 * Caller is responsible for initialising the bests table.
351 */
352 if (type == XFS_DIR2_LEAF1_MAGIC) {
353 struct xfs_dir2_leaf_tail *ltp;
354
355 ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
356 ltp->bestcount = 0;
357 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
358 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
359 } else {
360 bp->b_ops = &xfs_dir3_leafn_buf_ops;
361 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
362 }
363 }
364
365 int
366 xfs_dir3_leaf_get_buf(
367 xfs_da_args_t *args,
368 xfs_dir2_db_t bno,
369 struct xfs_buf **bpp,
370 uint16_t magic)
371 {
372 struct xfs_inode *dp = args->dp;
373 struct xfs_trans *tp = args->trans;
374 struct xfs_mount *mp = dp->i_mount;
375 struct xfs_buf *bp;
376 int error;
377
378 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
379 ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
380 bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
381
382 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
383 -1, &bp, XFS_DATA_FORK);
384 if (error)
385 return error;
386
387 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
388 xfs_dir3_leaf_log_header(args, bp);
389 if (magic == XFS_DIR2_LEAF1_MAGIC)
390 xfs_dir3_leaf_log_tail(args, bp);
391 *bpp = bp;
392 return 0;
393 }
394
395 /*
396 * Convert a block form directory to a leaf form directory.
397 */
398 int /* error */
399 xfs_dir2_block_to_leaf(
400 xfs_da_args_t *args, /* operation arguments */
401 struct xfs_buf *dbp) /* input block's buffer */
402 {
403 __be16 *bestsp; /* leaf's bestsp entries */
404 xfs_dablk_t blkno; /* leaf block's bno */
405 xfs_dir2_data_hdr_t *hdr; /* block header */
406 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
407 xfs_dir2_block_tail_t *btp; /* block's tail */
408 xfs_inode_t *dp; /* incore directory inode */
409 int error; /* error return code */
410 struct xfs_buf *lbp; /* leaf block's buffer */
411 xfs_dir2_db_t ldb; /* leaf block's bno */
412 xfs_dir2_leaf_t *leaf; /* leaf structure */
413 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
414 int needlog; /* need to log block header */
415 int needscan; /* need to rescan bestfree */
416 xfs_trans_t *tp; /* transaction pointer */
417 struct xfs_dir2_data_free *bf;
418 struct xfs_dir2_leaf_entry *ents;
419 struct xfs_dir3_icleaf_hdr leafhdr;
420
421 trace_xfs_dir2_block_to_leaf(args);
422
423 dp = args->dp;
424 tp = args->trans;
425 /*
426 * Add the leaf block to the inode.
427 * This interface will only put blocks in the leaf/node range.
428 * Since that's empty now, we'll get the root (block 0 in range).
429 */
430 if ((error = xfs_da_grow_inode(args, &blkno))) {
431 return error;
432 }
433 ldb = xfs_dir2_da_to_db(args->geo, blkno);
434 ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
435 /*
436 * Initialize the leaf block, get a buffer for it.
437 */
438 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
439 if (error)
440 return error;
441
442 leaf = lbp->b_addr;
443 hdr = dbp->b_addr;
444 xfs_dir3_data_check(dp, dbp);
445 btp = xfs_dir2_block_tail_p(args->geo, hdr);
446 blp = xfs_dir2_block_leaf_p(btp);
447 bf = dp->d_ops->data_bestfree_p(hdr);
448 ents = dp->d_ops->leaf_ents_p(leaf);
449
450 /*
451 * Set the counts in the leaf header.
452 */
453 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
454 leafhdr.count = be32_to_cpu(btp->count);
455 leafhdr.stale = be32_to_cpu(btp->stale);
456 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
457 xfs_dir3_leaf_log_header(args, lbp);
458
459 /*
460 * Could compact these but I think we always do the conversion
461 * after squeezing out stale entries.
462 */
463 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
464 xfs_dir3_leaf_log_ents(args, lbp, 0, leafhdr.count - 1);
465 needscan = 0;
466 needlog = 1;
467 /*
468 * Make the space formerly occupied by the leaf entries and block
469 * tail be free.
470 */
471 xfs_dir2_data_make_free(args, dbp,
472 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
473 (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
474 (char *)blp),
475 &needlog, &needscan);
476 /*
477 * Fix up the block header, make it a data block.
478 */
479 dbp->b_ops = &xfs_dir3_data_buf_ops;
480 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
481 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
482 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
483 else
484 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
485
486 if (needscan)
487 xfs_dir2_data_freescan(dp, hdr, &needlog);
488 /*
489 * Set up leaf tail and bests table.
490 */
491 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
492 ltp->bestcount = cpu_to_be32(1);
493 bestsp = xfs_dir2_leaf_bests_p(ltp);
494 bestsp[0] = bf[0].length;
495 /*
496 * Log the data header and leaf bests table.
497 */
498 if (needlog)
499 xfs_dir2_data_log_header(args, dbp);
500 xfs_dir3_leaf_check(dp, lbp);
501 xfs_dir3_data_check(dp, dbp);
502 xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
503 return 0;
504 }
505
506 STATIC void
507 xfs_dir3_leaf_find_stale(
508 struct xfs_dir3_icleaf_hdr *leafhdr,
509 struct xfs_dir2_leaf_entry *ents,
510 int index,
511 int *lowstale,
512 int *highstale)
513 {
514 /*
515 * Find the first stale entry before our index, if any.
516 */
517 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
518 if (ents[*lowstale].address ==
519 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
520 break;
521 }
522
523 /*
524 * Find the first stale entry at or after our index, if any.
525 * Stop if the result would require moving more entries than using
526 * lowstale.
527 */
528 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
529 if (ents[*highstale].address ==
530 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
531 break;
532 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
533 break;
534 }
535 }
536
537 struct xfs_dir2_leaf_entry *
538 xfs_dir3_leaf_find_entry(
539 struct xfs_dir3_icleaf_hdr *leafhdr,
540 struct xfs_dir2_leaf_entry *ents,
541 int index, /* leaf table position */
542 int compact, /* need to compact leaves */
543 int lowstale, /* index of prev stale leaf */
544 int highstale, /* index of next stale leaf */
545 int *lfloglow, /* low leaf logging index */
546 int *lfloghigh) /* high leaf logging index */
547 {
548 if (!leafhdr->stale) {
549 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
550
551 /*
552 * Now we need to make room to insert the leaf entry.
553 *
554 * If there are no stale entries, just insert a hole at index.
555 */
556 lep = &ents[index];
557 if (index < leafhdr->count)
558 memmove(lep + 1, lep,
559 (leafhdr->count - index) * sizeof(*lep));
560
561 /*
562 * Record low and high logging indices for the leaf.
563 */
564 *lfloglow = index;
565 *lfloghigh = leafhdr->count++;
566 return lep;
567 }
568
569 /*
570 * There are stale entries.
571 *
572 * We will use one of them for the new entry. It's probably not at
573 * the right location, so we'll have to shift some up or down first.
574 *
575 * If we didn't compact before, we need to find the nearest stale
576 * entries before and after our insertion point.
577 */
578 if (compact == 0)
579 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
580 &lowstale, &highstale);
581
582 /*
583 * If the low one is better, use it.
584 */
585 if (lowstale >= 0 &&
586 (highstale == leafhdr->count ||
587 index - lowstale - 1 < highstale - index)) {
588 ASSERT(index - lowstale - 1 >= 0);
589 ASSERT(ents[lowstale].address ==
590 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
591
592 /*
593 * Copy entries up to cover the stale entry and make room
594 * for the new entry.
595 */
596 if (index - lowstale - 1 > 0) {
597 memmove(&ents[lowstale], &ents[lowstale + 1],
598 (index - lowstale - 1) *
599 sizeof(xfs_dir2_leaf_entry_t));
600 }
601 *lfloglow = MIN(lowstale, *lfloglow);
602 *lfloghigh = MAX(index - 1, *lfloghigh);
603 leafhdr->stale--;
604 return &ents[index - 1];
605 }
606
607 /*
608 * The high one is better, so use that one.
609 */
610 ASSERT(highstale - index >= 0);
611 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
612
613 /*
614 * Copy entries down to cover the stale entry and make room for the
615 * new entry.
616 */
617 if (highstale - index > 0) {
618 memmove(&ents[index + 1], &ents[index],
619 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
620 }
621 *lfloglow = MIN(index, *lfloglow);
622 *lfloghigh = MAX(highstale, *lfloghigh);
623 leafhdr->stale--;
624 return &ents[index];
625 }
626
627 /*
628 * Add an entry to a leaf form directory.
629 */
630 int /* error */
631 xfs_dir2_leaf_addname(
632 xfs_da_args_t *args) /* operation arguments */
633 {
634 __be16 *bestsp; /* freespace table in leaf */
635 int compact; /* need to compact leaves */
636 xfs_dir2_data_hdr_t *hdr; /* data block header */
637 struct xfs_buf *dbp; /* data block buffer */
638 xfs_dir2_data_entry_t *dep; /* data block entry */
639 xfs_inode_t *dp; /* incore directory inode */
640 xfs_dir2_data_unused_t *dup; /* data unused entry */
641 int error; /* error return value */
642 int grown; /* allocated new data block */
643 int highstale; /* index of next stale leaf */
644 int i; /* temporary, index */
645 int index; /* leaf table position */
646 struct xfs_buf *lbp; /* leaf's buffer */
647 xfs_dir2_leaf_t *leaf; /* leaf structure */
648 int length; /* length of new entry */
649 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
650 int lfloglow; /* low leaf logging index */
651 int lfloghigh; /* high leaf logging index */
652 int lowstale; /* index of prev stale leaf */
653 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
654 int needbytes; /* leaf block bytes needed */
655 int needlog; /* need to log data header */
656 int needscan; /* need to rescan data free */
657 __be16 *tagp; /* end of data entry */
658 xfs_trans_t *tp; /* transaction pointer */
659 xfs_dir2_db_t use_block; /* data block number */
660 struct xfs_dir2_data_free *bf; /* bestfree table */
661 struct xfs_dir2_leaf_entry *ents;
662 struct xfs_dir3_icleaf_hdr leafhdr;
663
664 trace_xfs_dir2_leaf_addname(args);
665
666 dp = args->dp;
667 tp = args->trans;
668
669 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
670 if (error)
671 return error;
672
673 /*
674 * Look up the entry by hash value and name.
675 * We know it's not there, our caller has already done a lookup.
676 * So the index is of the entry to insert in front of.
677 * But if there are dup hash values the index is of the first of those.
678 */
679 index = xfs_dir2_leaf_search_hash(args, lbp);
680 leaf = lbp->b_addr;
681 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
682 ents = dp->d_ops->leaf_ents_p(leaf);
683 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
684 bestsp = xfs_dir2_leaf_bests_p(ltp);
685 length = dp->d_ops->data_entsize(args->namelen);
686
687 /*
688 * See if there are any entries with the same hash value
689 * and space in their block for the new entry.
690 * This is good because it puts multiple same-hash value entries
691 * in a data block, improving the lookup of those entries.
692 */
693 for (use_block = -1, lep = &ents[index];
694 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
695 index++, lep++) {
696 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
697 continue;
698 i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
699 ASSERT(i < be32_to_cpu(ltp->bestcount));
700 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
701 if (be16_to_cpu(bestsp[i]) >= length) {
702 use_block = i;
703 break;
704 }
705 }
706 /*
707 * Didn't find a block yet, linear search all the data blocks.
708 */
709 if (use_block == -1) {
710 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
711 /*
712 * Remember a block we see that's missing.
713 */
714 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
715 use_block == -1)
716 use_block = i;
717 else if (be16_to_cpu(bestsp[i]) >= length) {
718 use_block = i;
719 break;
720 }
721 }
722 }
723 /*
724 * How many bytes do we need in the leaf block?
725 */
726 needbytes = 0;
727 if (!leafhdr.stale)
728 needbytes += sizeof(xfs_dir2_leaf_entry_t);
729 if (use_block == -1)
730 needbytes += sizeof(xfs_dir2_data_off_t);
731
732 /*
733 * Now kill use_block if it refers to a missing block, so we
734 * can use it as an indication of allocation needed.
735 */
736 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
737 use_block = -1;
738 /*
739 * If we don't have enough free bytes but we can make enough
740 * by compacting out stale entries, we'll do that.
741 */
742 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
743 leafhdr.stale > 1)
744 compact = 1;
745
746 /*
747 * Otherwise if we don't have enough free bytes we need to
748 * convert to node form.
749 */
750 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
751 /*
752 * Just checking or no space reservation, give up.
753 */
754 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
755 args->total == 0) {
756 xfs_trans_brelse(tp, lbp);
757 return -ENOSPC;
758 }
759 /*
760 * Convert to node form.
761 */
762 error = xfs_dir2_leaf_to_node(args, lbp);
763 if (error)
764 return error;
765 /*
766 * Then add the new entry.
767 */
768 return xfs_dir2_node_addname(args);
769 }
770 /*
771 * Otherwise it will fit without compaction.
772 */
773 else
774 compact = 0;
775 /*
776 * If just checking, then it will fit unless we needed to allocate
777 * a new data block.
778 */
779 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
780 xfs_trans_brelse(tp, lbp);
781 return use_block == -1 ? -ENOSPC : 0;
782 }
783 /*
784 * If no allocations are allowed, return now before we've
785 * changed anything.
786 */
787 if (args->total == 0 && use_block == -1) {
788 xfs_trans_brelse(tp, lbp);
789 return -ENOSPC;
790 }
791 /*
792 * Need to compact the leaf entries, removing stale ones.
793 * Leave one stale entry behind - the one closest to our
794 * insertion index - and we'll shift that one to our insertion
795 * point later.
796 */
797 if (compact) {
798 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
799 &highstale, &lfloglow, &lfloghigh);
800 }
801 /*
802 * There are stale entries, so we'll need log-low and log-high
803 * impossibly bad values later.
804 */
805 else if (leafhdr.stale) {
806 lfloglow = leafhdr.count;
807 lfloghigh = -1;
808 }
809 /*
810 * If there was no data block space found, we need to allocate
811 * a new one.
812 */
813 if (use_block == -1) {
814 /*
815 * Add the new data block.
816 */
817 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
818 &use_block))) {
819 xfs_trans_brelse(tp, lbp);
820 return error;
821 }
822 /*
823 * Initialize the block.
824 */
825 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
826 xfs_trans_brelse(tp, lbp);
827 return error;
828 }
829 /*
830 * If we're adding a new data block on the end we need to
831 * extend the bests table. Copy it up one entry.
832 */
833 if (use_block >= be32_to_cpu(ltp->bestcount)) {
834 bestsp--;
835 memmove(&bestsp[0], &bestsp[1],
836 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
837 be32_add_cpu(&ltp->bestcount, 1);
838 xfs_dir3_leaf_log_tail(args, lbp);
839 xfs_dir3_leaf_log_bests(args, lbp, 0,
840 be32_to_cpu(ltp->bestcount) - 1);
841 }
842 /*
843 * If we're filling in a previously empty block just log it.
844 */
845 else
846 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
847 hdr = dbp->b_addr;
848 bf = dp->d_ops->data_bestfree_p(hdr);
849 bestsp[use_block] = bf[0].length;
850 grown = 1;
851 } else {
852 /*
853 * Already had space in some data block.
854 * Just read that one in.
855 */
856 error = xfs_dir3_data_read(tp, dp,
857 xfs_dir2_db_to_da(args->geo, use_block),
858 -1, &dbp);
859 if (error) {
860 xfs_trans_brelse(tp, lbp);
861 return error;
862 }
863 hdr = dbp->b_addr;
864 bf = dp->d_ops->data_bestfree_p(hdr);
865 grown = 0;
866 }
867 /*
868 * Point to the biggest freespace in our data block.
869 */
870 dup = (xfs_dir2_data_unused_t *)
871 ((char *)hdr + be16_to_cpu(bf[0].offset));
872 ASSERT(be16_to_cpu(dup->length) >= length);
873 needscan = needlog = 0;
874 /*
875 * Mark the initial part of our freespace in use for the new entry.
876 */
877 error = xfs_dir2_data_use_free(args, dbp, dup,
878 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
879 length, &needlog, &needscan);
880 if (error) {
881 xfs_trans_brelse(tp, lbp);
882 return error;
883 }
884 /*
885 * Initialize our new entry (at last).
886 */
887 dep = (xfs_dir2_data_entry_t *)dup;
888 dep->inumber = cpu_to_be64(args->inumber);
889 dep->namelen = args->namelen;
890 memcpy(dep->name, args->name, dep->namelen);
891 dp->d_ops->data_put_ftype(dep, args->filetype);
892 tagp = dp->d_ops->data_entry_tag_p(dep);
893 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
894 /*
895 * Need to scan fix up the bestfree table.
896 */
897 if (needscan)
898 xfs_dir2_data_freescan(dp, hdr, &needlog);
899 /*
900 * Need to log the data block's header.
901 */
902 if (needlog)
903 xfs_dir2_data_log_header(args, dbp);
904 xfs_dir2_data_log_entry(args, dbp, dep);
905 /*
906 * If the bests table needs to be changed, do it.
907 * Log the change unless we've already done that.
908 */
909 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
910 bestsp[use_block] = bf[0].length;
911 if (!grown)
912 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
913 }
914
915 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
916 highstale, &lfloglow, &lfloghigh);
917
918 /*
919 * Fill in the new leaf entry.
920 */
921 lep->hashval = cpu_to_be32(args->hashval);
922 lep->address = cpu_to_be32(
923 xfs_dir2_db_off_to_dataptr(args->geo, use_block,
924 be16_to_cpu(*tagp)));
925 /*
926 * Log the leaf fields and give up the buffers.
927 */
928 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
929 xfs_dir3_leaf_log_header(args, lbp);
930 xfs_dir3_leaf_log_ents(args, lbp, lfloglow, lfloghigh);
931 xfs_dir3_leaf_check(dp, lbp);
932 xfs_dir3_data_check(dp, dbp);
933 return 0;
934 }
935
936 /*
937 * Compact out any stale entries in the leaf.
938 * Log the header and changed leaf entries, if any.
939 */
940 void
941 xfs_dir3_leaf_compact(
942 xfs_da_args_t *args, /* operation arguments */
943 struct xfs_dir3_icleaf_hdr *leafhdr,
944 struct xfs_buf *bp) /* leaf buffer */
945 {
946 int from; /* source leaf index */
947 xfs_dir2_leaf_t *leaf; /* leaf structure */
948 int loglow; /* first leaf entry to log */
949 int to; /* target leaf index */
950 struct xfs_dir2_leaf_entry *ents;
951 struct xfs_inode *dp = args->dp;
952
953 leaf = bp->b_addr;
954 if (!leafhdr->stale)
955 return;
956
957 /*
958 * Compress out the stale entries in place.
959 */
960 ents = dp->d_ops->leaf_ents_p(leaf);
961 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
962 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
963 continue;
964 /*
965 * Only actually copy the entries that are different.
966 */
967 if (from > to) {
968 if (loglow == -1)
969 loglow = to;
970 ents[to] = ents[from];
971 }
972 to++;
973 }
974 /*
975 * Update and log the header, log the leaf entries.
976 */
977 ASSERT(leafhdr->stale == from - to);
978 leafhdr->count -= leafhdr->stale;
979 leafhdr->stale = 0;
980
981 dp->d_ops->leaf_hdr_to_disk(leaf, leafhdr);
982 xfs_dir3_leaf_log_header(args, bp);
983 if (loglow != -1)
984 xfs_dir3_leaf_log_ents(args, bp, loglow, to - 1);
985 }
986
987 /*
988 * Compact the leaf entries, removing stale ones.
989 * Leave one stale entry behind - the one closest to our
990 * insertion index - and the caller will shift that one to our insertion
991 * point later.
992 * Return new insertion index, where the remaining stale entry is,
993 * and leaf logging indices.
994 */
995 void
996 xfs_dir3_leaf_compact_x1(
997 struct xfs_dir3_icleaf_hdr *leafhdr,
998 struct xfs_dir2_leaf_entry *ents,
999 int *indexp, /* insertion index */
1000 int *lowstalep, /* out: stale entry before us */
1001 int *highstalep, /* out: stale entry after us */
1002 int *lowlogp, /* out: low log index */
1003 int *highlogp) /* out: high log index */
1004 {
1005 int from; /* source copy index */
1006 int highstale; /* stale entry at/after index */
1007 int index; /* insertion index */
1008 int keepstale; /* source index of kept stale */
1009 int lowstale; /* stale entry before index */
1010 int newindex=0; /* new insertion index */
1011 int to; /* destination copy index */
1012
1013 ASSERT(leafhdr->stale > 1);
1014 index = *indexp;
1015
1016 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1017
1018 /*
1019 * Pick the better of lowstale and highstale.
1020 */
1021 if (lowstale >= 0 &&
1022 (highstale == leafhdr->count ||
1023 index - lowstale <= highstale - index))
1024 keepstale = lowstale;
1025 else
1026 keepstale = highstale;
1027 /*
1028 * Copy the entries in place, removing all the stale entries
1029 * except keepstale.
1030 */
1031 for (from = to = 0; from < leafhdr->count; from++) {
1032 /*
1033 * Notice the new value of index.
1034 */
1035 if (index == from)
1036 newindex = to;
1037 if (from != keepstale &&
1038 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1039 if (from == to)
1040 *lowlogp = to;
1041 continue;
1042 }
1043 /*
1044 * Record the new keepstale value for the insertion.
1045 */
1046 if (from == keepstale)
1047 lowstale = highstale = to;
1048 /*
1049 * Copy only the entries that have moved.
1050 */
1051 if (from > to)
1052 ents[to] = ents[from];
1053 to++;
1054 }
1055 ASSERT(from > to);
1056 /*
1057 * If the insertion point was past the last entry,
1058 * set the new insertion point accordingly.
1059 */
1060 if (index == from)
1061 newindex = to;
1062 *indexp = newindex;
1063 /*
1064 * Adjust the leaf header values.
1065 */
1066 leafhdr->count -= from - to;
1067 leafhdr->stale = 1;
1068 /*
1069 * Remember the low/high stale value only in the "right"
1070 * direction.
1071 */
1072 if (lowstale >= newindex)
1073 lowstale = -1;
1074 else
1075 highstale = leafhdr->count;
1076 *highlogp = leafhdr->count - 1;
1077 *lowstalep = lowstale;
1078 *highstalep = highstale;
1079 }
1080
1081 /*
1082 * Log the bests entries indicated from a leaf1 block.
1083 */
1084 static void
1085 xfs_dir3_leaf_log_bests(
1086 struct xfs_da_args *args,
1087 struct xfs_buf *bp, /* leaf buffer */
1088 int first, /* first entry to log */
1089 int last) /* last entry to log */
1090 {
1091 __be16 *firstb; /* pointer to first entry */
1092 __be16 *lastb; /* pointer to last entry */
1093 struct xfs_dir2_leaf *leaf = bp->b_addr;
1094 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1095
1096 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1097 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1098
1099 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1100 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1101 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1102 xfs_trans_log_buf(args->trans, bp,
1103 (uint)((char *)firstb - (char *)leaf),
1104 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1105 }
1106
1107 /*
1108 * Log the leaf entries indicated from a leaf1 or leafn block.
1109 */
1110 void
1111 xfs_dir3_leaf_log_ents(
1112 struct xfs_da_args *args,
1113 struct xfs_buf *bp,
1114 int first,
1115 int last)
1116 {
1117 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1118 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1119 struct xfs_dir2_leaf *leaf = bp->b_addr;
1120 struct xfs_dir2_leaf_entry *ents;
1121
1122 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1123 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1124 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1125 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1126
1127 ents = args->dp->d_ops->leaf_ents_p(leaf);
1128 firstlep = &ents[first];
1129 lastlep = &ents[last];
1130 xfs_trans_log_buf(args->trans, bp,
1131 (uint)((char *)firstlep - (char *)leaf),
1132 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1133 }
1134
1135 /*
1136 * Log the header of the leaf1 or leafn block.
1137 */
1138 void
1139 xfs_dir3_leaf_log_header(
1140 struct xfs_da_args *args,
1141 struct xfs_buf *bp)
1142 {
1143 struct xfs_dir2_leaf *leaf = bp->b_addr;
1144
1145 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1146 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1147 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1148 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1149
1150 xfs_trans_log_buf(args->trans, bp,
1151 (uint)((char *)&leaf->hdr - (char *)leaf),
1152 args->dp->d_ops->leaf_hdr_size - 1);
1153 }
1154
1155 /*
1156 * Log the tail of the leaf1 block.
1157 */
1158 STATIC void
1159 xfs_dir3_leaf_log_tail(
1160 struct xfs_da_args *args,
1161 struct xfs_buf *bp)
1162 {
1163 struct xfs_dir2_leaf *leaf = bp->b_addr;
1164 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1165
1166 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1167 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1168 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1169 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1170
1171 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1172 xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1173 (uint)(args->geo->blksize - 1));
1174 }
1175
1176 /*
1177 * Look up the entry referred to by args in the leaf format directory.
1178 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1179 * is also used by the node-format code.
1180 */
1181 int
1182 xfs_dir2_leaf_lookup(
1183 xfs_da_args_t *args) /* operation arguments */
1184 {
1185 struct xfs_buf *dbp; /* data block buffer */
1186 xfs_dir2_data_entry_t *dep; /* data block entry */
1187 xfs_inode_t *dp; /* incore directory inode */
1188 int error; /* error return code */
1189 int index; /* found entry index */
1190 struct xfs_buf *lbp; /* leaf buffer */
1191 xfs_dir2_leaf_t *leaf; /* leaf structure */
1192 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1193 xfs_trans_t *tp; /* transaction pointer */
1194 struct xfs_dir2_leaf_entry *ents;
1195
1196 trace_xfs_dir2_leaf_lookup(args);
1197
1198 /*
1199 * Look up name in the leaf block, returning both buffers and index.
1200 */
1201 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1202 return error;
1203 }
1204 tp = args->trans;
1205 dp = args->dp;
1206 xfs_dir3_leaf_check(dp, lbp);
1207 leaf = lbp->b_addr;
1208 ents = dp->d_ops->leaf_ents_p(leaf);
1209 /*
1210 * Get to the leaf entry and contained data entry address.
1211 */
1212 lep = &ents[index];
1213
1214 /*
1215 * Point to the data entry.
1216 */
1217 dep = (xfs_dir2_data_entry_t *)
1218 ((char *)dbp->b_addr +
1219 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1220 /*
1221 * Return the found inode number & CI name if appropriate
1222 */
1223 args->inumber = be64_to_cpu(dep->inumber);
1224 args->filetype = dp->d_ops->data_get_ftype(dep);
1225 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1226 xfs_trans_brelse(tp, dbp);
1227 xfs_trans_brelse(tp, lbp);
1228 return error;
1229 }
1230
1231 /*
1232 * Look up name/hash in the leaf block.
1233 * Fill in indexp with the found index, and dbpp with the data buffer.
1234 * If not found dbpp will be NULL, and ENOENT comes back.
1235 * lbpp will always be filled in with the leaf buffer unless there's an error.
1236 */
1237 static int /* error */
1238 xfs_dir2_leaf_lookup_int(
1239 xfs_da_args_t *args, /* operation arguments */
1240 struct xfs_buf **lbpp, /* out: leaf buffer */
1241 int *indexp, /* out: index in leaf block */
1242 struct xfs_buf **dbpp) /* out: data buffer */
1243 {
1244 xfs_dir2_db_t curdb = -1; /* current data block number */
1245 struct xfs_buf *dbp = NULL; /* data buffer */
1246 xfs_dir2_data_entry_t *dep; /* data entry */
1247 xfs_inode_t *dp; /* incore directory inode */
1248 int error; /* error return code */
1249 int index; /* index in leaf block */
1250 struct xfs_buf *lbp; /* leaf buffer */
1251 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1252 xfs_dir2_leaf_t *leaf; /* leaf structure */
1253 xfs_mount_t *mp; /* filesystem mount point */
1254 xfs_dir2_db_t newdb; /* new data block number */
1255 xfs_trans_t *tp; /* transaction pointer */
1256 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1257 enum xfs_dacmp cmp; /* name compare result */
1258 struct xfs_dir2_leaf_entry *ents;
1259 struct xfs_dir3_icleaf_hdr leafhdr;
1260
1261 dp = args->dp;
1262 tp = args->trans;
1263 mp = dp->i_mount;
1264
1265 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
1266 if (error)
1267 return error;
1268
1269 *lbpp = lbp;
1270 leaf = lbp->b_addr;
1271 xfs_dir3_leaf_check(dp, lbp);
1272 ents = dp->d_ops->leaf_ents_p(leaf);
1273 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1274
1275 /*
1276 * Look for the first leaf entry with our hash value.
1277 */
1278 index = xfs_dir2_leaf_search_hash(args, lbp);
1279 /*
1280 * Loop over all the entries with the right hash value
1281 * looking to match the name.
1282 */
1283 for (lep = &ents[index];
1284 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1285 lep++, index++) {
1286 /*
1287 * Skip over stale leaf entries.
1288 */
1289 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1290 continue;
1291 /*
1292 * Get the new data block number.
1293 */
1294 newdb = xfs_dir2_dataptr_to_db(args->geo,
1295 be32_to_cpu(lep->address));
1296 /*
1297 * If it's not the same as the old data block number,
1298 * need to pitch the old one and read the new one.
1299 */
1300 if (newdb != curdb) {
1301 if (dbp)
1302 xfs_trans_brelse(tp, dbp);
1303 error = xfs_dir3_data_read(tp, dp,
1304 xfs_dir2_db_to_da(args->geo, newdb),
1305 -1, &dbp);
1306 if (error) {
1307 xfs_trans_brelse(tp, lbp);
1308 return error;
1309 }
1310 curdb = newdb;
1311 }
1312 /*
1313 * Point to the data entry.
1314 */
1315 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1316 xfs_dir2_dataptr_to_off(args->geo,
1317 be32_to_cpu(lep->address)));
1318 /*
1319 * Compare name and if it's an exact match, return the index
1320 * and buffer. If it's the first case-insensitive match, store
1321 * the index and buffer and continue looking for an exact match.
1322 */
1323 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1324 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1325 args->cmpresult = cmp;
1326 *indexp = index;
1327 /* case exact match: return the current buffer. */
1328 if (cmp == XFS_CMP_EXACT) {
1329 *dbpp = dbp;
1330 return 0;
1331 }
1332 cidb = curdb;
1333 }
1334 }
1335 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1336 /*
1337 * Here, we can only be doing a lookup (not a rename or remove).
1338 * If a case-insensitive match was found earlier, re-read the
1339 * appropriate data block if required and return it.
1340 */
1341 if (args->cmpresult == XFS_CMP_CASE) {
1342 ASSERT(cidb != -1);
1343 if (cidb != curdb) {
1344 xfs_trans_brelse(tp, dbp);
1345 error = xfs_dir3_data_read(tp, dp,
1346 xfs_dir2_db_to_da(args->geo, cidb),
1347 -1, &dbp);
1348 if (error) {
1349 xfs_trans_brelse(tp, lbp);
1350 return error;
1351 }
1352 }
1353 *dbpp = dbp;
1354 return 0;
1355 }
1356 /*
1357 * No match found, return -ENOENT.
1358 */
1359 ASSERT(cidb == -1);
1360 if (dbp)
1361 xfs_trans_brelse(tp, dbp);
1362 xfs_trans_brelse(tp, lbp);
1363 return -ENOENT;
1364 }
1365
1366 /*
1367 * Remove an entry from a leaf format directory.
1368 */
1369 int /* error */
1370 xfs_dir2_leaf_removename(
1371 xfs_da_args_t *args) /* operation arguments */
1372 {
1373 __be16 *bestsp; /* leaf block best freespace */
1374 xfs_dir2_data_hdr_t *hdr; /* data block header */
1375 xfs_dir2_db_t db; /* data block number */
1376 struct xfs_buf *dbp; /* data block buffer */
1377 xfs_dir2_data_entry_t *dep; /* data entry structure */
1378 xfs_inode_t *dp; /* incore directory inode */
1379 int error; /* error return code */
1380 xfs_dir2_db_t i; /* temporary data block # */
1381 int index; /* index into leaf entries */
1382 struct xfs_buf *lbp; /* leaf buffer */
1383 xfs_dir2_leaf_t *leaf; /* leaf structure */
1384 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1385 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1386 int needlog; /* need to log data header */
1387 int needscan; /* need to rescan data frees */
1388 xfs_dir2_data_off_t oldbest; /* old value of best free */
1389 struct xfs_dir2_data_free *bf; /* bestfree table */
1390 struct xfs_dir2_leaf_entry *ents;
1391 struct xfs_dir3_icleaf_hdr leafhdr;
1392
1393 trace_xfs_dir2_leaf_removename(args);
1394
1395 /*
1396 * Lookup the leaf entry, get the leaf and data blocks read in.
1397 */
1398 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1399 return error;
1400 }
1401 dp = args->dp;
1402 leaf = lbp->b_addr;
1403 hdr = dbp->b_addr;
1404 xfs_dir3_data_check(dp, dbp);
1405 bf = dp->d_ops->data_bestfree_p(hdr);
1406 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1407 ents = dp->d_ops->leaf_ents_p(leaf);
1408 /*
1409 * Point to the leaf entry, use that to point to the data entry.
1410 */
1411 lep = &ents[index];
1412 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1413 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1414 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1415 needscan = needlog = 0;
1416 oldbest = be16_to_cpu(bf[0].length);
1417 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1418 bestsp = xfs_dir2_leaf_bests_p(ltp);
1419 if (be16_to_cpu(bestsp[db]) != oldbest)
1420 return -EFSCORRUPTED;
1421 /*
1422 * Mark the former data entry unused.
1423 */
1424 xfs_dir2_data_make_free(args, dbp,
1425 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1426 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1427 /*
1428 * We just mark the leaf entry stale by putting a null in it.
1429 */
1430 leafhdr.stale++;
1431 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1432 xfs_dir3_leaf_log_header(args, lbp);
1433
1434 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1435 xfs_dir3_leaf_log_ents(args, lbp, index, index);
1436
1437 /*
1438 * Scan the freespace in the data block again if necessary,
1439 * log the data block header if necessary.
1440 */
1441 if (needscan)
1442 xfs_dir2_data_freescan(dp, hdr, &needlog);
1443 if (needlog)
1444 xfs_dir2_data_log_header(args, dbp);
1445 /*
1446 * If the longest freespace in the data block has changed,
1447 * put the new value in the bests table and log that.
1448 */
1449 if (be16_to_cpu(bf[0].length) != oldbest) {
1450 bestsp[db] = bf[0].length;
1451 xfs_dir3_leaf_log_bests(args, lbp, db, db);
1452 }
1453 xfs_dir3_data_check(dp, dbp);
1454 /*
1455 * If the data block is now empty then get rid of the data block.
1456 */
1457 if (be16_to_cpu(bf[0].length) ==
1458 args->geo->blksize - dp->d_ops->data_entry_offset) {
1459 ASSERT(db != args->geo->datablk);
1460 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1461 /*
1462 * Nope, can't get rid of it because it caused
1463 * allocation of a bmap btree block to do so.
1464 * Just go on, returning success, leaving the
1465 * empty block in place.
1466 */
1467 if (error == -ENOSPC && args->total == 0)
1468 error = 0;
1469 xfs_dir3_leaf_check(dp, lbp);
1470 return error;
1471 }
1472 dbp = NULL;
1473 /*
1474 * If this is the last data block then compact the
1475 * bests table by getting rid of entries.
1476 */
1477 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1478 /*
1479 * Look for the last active entry (i).
1480 */
1481 for (i = db - 1; i > 0; i--) {
1482 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1483 break;
1484 }
1485 /*
1486 * Copy the table down so inactive entries at the
1487 * end are removed.
1488 */
1489 memmove(&bestsp[db - i], bestsp,
1490 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1491 be32_add_cpu(&ltp->bestcount, -(db - i));
1492 xfs_dir3_leaf_log_tail(args, lbp);
1493 xfs_dir3_leaf_log_bests(args, lbp, 0,
1494 be32_to_cpu(ltp->bestcount) - 1);
1495 } else
1496 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1497 }
1498 /*
1499 * If the data block was not the first one, drop it.
1500 */
1501 else if (db != args->geo->datablk)
1502 dbp = NULL;
1503
1504 xfs_dir3_leaf_check(dp, lbp);
1505 /*
1506 * See if we can convert to block form.
1507 */
1508 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1509 }
1510
1511 /*
1512 * Replace the inode number in a leaf format directory entry.
1513 */
1514 int /* error */
1515 xfs_dir2_leaf_replace(
1516 xfs_da_args_t *args) /* operation arguments */
1517 {
1518 struct xfs_buf *dbp; /* data block buffer */
1519 xfs_dir2_data_entry_t *dep; /* data block entry */
1520 xfs_inode_t *dp; /* incore directory inode */
1521 int error; /* error return code */
1522 int index; /* index of leaf entry */
1523 struct xfs_buf *lbp; /* leaf buffer */
1524 xfs_dir2_leaf_t *leaf; /* leaf structure */
1525 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1526 xfs_trans_t *tp; /* transaction pointer */
1527 struct xfs_dir2_leaf_entry *ents;
1528
1529 trace_xfs_dir2_leaf_replace(args);
1530
1531 /*
1532 * Look up the entry.
1533 */
1534 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1535 return error;
1536 }
1537 dp = args->dp;
1538 leaf = lbp->b_addr;
1539 ents = dp->d_ops->leaf_ents_p(leaf);
1540 /*
1541 * Point to the leaf entry, get data address from it.
1542 */
1543 lep = &ents[index];
1544 /*
1545 * Point to the data entry.
1546 */
1547 dep = (xfs_dir2_data_entry_t *)
1548 ((char *)dbp->b_addr +
1549 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1550 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1551 /*
1552 * Put the new inode number in, log it.
1553 */
1554 dep->inumber = cpu_to_be64(args->inumber);
1555 dp->d_ops->data_put_ftype(dep, args->filetype);
1556 tp = args->trans;
1557 xfs_dir2_data_log_entry(args, dbp, dep);
1558 xfs_dir3_leaf_check(dp, lbp);
1559 xfs_trans_brelse(tp, lbp);
1560 return 0;
1561 }
1562
1563 /*
1564 * Return index in the leaf block (lbp) which is either the first
1565 * one with this hash value, or if there are none, the insert point
1566 * for that hash value.
1567 */
1568 int /* index value */
1569 xfs_dir2_leaf_search_hash(
1570 xfs_da_args_t *args, /* operation arguments */
1571 struct xfs_buf *lbp) /* leaf buffer */
1572 {
1573 xfs_dahash_t hash=0; /* hash from this entry */
1574 xfs_dahash_t hashwant; /* hash value looking for */
1575 int high; /* high leaf index */
1576 int low; /* low leaf index */
1577 xfs_dir2_leaf_t *leaf; /* leaf structure */
1578 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1579 int mid=0; /* current leaf index */
1580 struct xfs_dir2_leaf_entry *ents;
1581 struct xfs_dir3_icleaf_hdr leafhdr;
1582
1583 leaf = lbp->b_addr;
1584 ents = args->dp->d_ops->leaf_ents_p(leaf);
1585 args->dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1586
1587 /*
1588 * Note, the table cannot be empty, so we have to go through the loop.
1589 * Binary search the leaf entries looking for our hash value.
1590 */
1591 for (lep = ents, low = 0, high = leafhdr.count - 1,
1592 hashwant = args->hashval;
1593 low <= high; ) {
1594 mid = (low + high) >> 1;
1595 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1596 break;
1597 if (hash < hashwant)
1598 low = mid + 1;
1599 else
1600 high = mid - 1;
1601 }
1602 /*
1603 * Found one, back up through all the equal hash values.
1604 */
1605 if (hash == hashwant) {
1606 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1607 mid--;
1608 }
1609 }
1610 /*
1611 * Need to point to an entry higher than ours.
1612 */
1613 else if (hash < hashwant)
1614 mid++;
1615 return mid;
1616 }
1617
1618 /*
1619 * Trim off a trailing data block. We know it's empty since the leaf
1620 * freespace table says so.
1621 */
1622 int /* error */
1623 xfs_dir2_leaf_trim_data(
1624 xfs_da_args_t *args, /* operation arguments */
1625 struct xfs_buf *lbp, /* leaf buffer */
1626 xfs_dir2_db_t db) /* data block number */
1627 {
1628 __be16 *bestsp; /* leaf bests table */
1629 struct xfs_buf *dbp; /* data block buffer */
1630 xfs_inode_t *dp; /* incore directory inode */
1631 int error; /* error return value */
1632 xfs_dir2_leaf_t *leaf; /* leaf structure */
1633 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1634 xfs_trans_t *tp; /* transaction pointer */
1635
1636 dp = args->dp;
1637 tp = args->trans;
1638 /*
1639 * Read the offending data block. We need its buffer.
1640 */
1641 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(args->geo, db),
1642 -1, &dbp);
1643 if (error)
1644 return error;
1645
1646 leaf = lbp->b_addr;
1647 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1648
1649 #ifdef DEBUG
1650 {
1651 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1652 struct xfs_dir2_data_free *bf = dp->d_ops->data_bestfree_p(hdr);
1653
1654 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1655 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1656 ASSERT(be16_to_cpu(bf[0].length) ==
1657 args->geo->blksize - dp->d_ops->data_entry_offset);
1658 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1659 }
1660 #endif
1661
1662 /*
1663 * Get rid of the data block.
1664 */
1665 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1666 ASSERT(error != -ENOSPC);
1667 xfs_trans_brelse(tp, dbp);
1668 return error;
1669 }
1670 /*
1671 * Eliminate the last bests entry from the table.
1672 */
1673 bestsp = xfs_dir2_leaf_bests_p(ltp);
1674 be32_add_cpu(&ltp->bestcount, -1);
1675 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1676 xfs_dir3_leaf_log_tail(args, lbp);
1677 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1678 return 0;
1679 }
1680
1681 static inline size_t
1682 xfs_dir3_leaf_size(
1683 struct xfs_dir3_icleaf_hdr *hdr,
1684 int counts)
1685 {
1686 int entries;
1687 int hdrsize;
1688
1689 entries = hdr->count - hdr->stale;
1690 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1691 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1692 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1693 else
1694 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1695
1696 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1697 + counts * sizeof(xfs_dir2_data_off_t)
1698 + sizeof(xfs_dir2_leaf_tail_t);
1699 }
1700
1701 /*
1702 * Convert node form directory to leaf form directory.
1703 * The root of the node form dir needs to already be a LEAFN block.
1704 * Just return if we can't do anything.
1705 */
1706 int /* error */
1707 xfs_dir2_node_to_leaf(
1708 xfs_da_state_t *state) /* directory operation state */
1709 {
1710 xfs_da_args_t *args; /* operation arguments */
1711 xfs_inode_t *dp; /* incore directory inode */
1712 int error; /* error return code */
1713 struct xfs_buf *fbp; /* buffer for freespace block */
1714 xfs_fileoff_t fo; /* freespace file offset */
1715 xfs_dir2_free_t *free; /* freespace structure */
1716 struct xfs_buf *lbp; /* buffer for leaf block */
1717 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1718 xfs_dir2_leaf_t *leaf; /* leaf structure */
1719 xfs_mount_t *mp; /* filesystem mount point */
1720 int rval; /* successful free trim? */
1721 xfs_trans_t *tp; /* transaction pointer */
1722 struct xfs_dir3_icleaf_hdr leafhdr;
1723 struct xfs_dir3_icfree_hdr freehdr;
1724
1725 /*
1726 * There's more than a leaf level in the btree, so there must
1727 * be multiple leafn blocks. Give up.
1728 */
1729 if (state->path.active > 1)
1730 return 0;
1731 args = state->args;
1732
1733 trace_xfs_dir2_node_to_leaf(args);
1734
1735 mp = state->mp;
1736 dp = args->dp;
1737 tp = args->trans;
1738 /*
1739 * Get the last offset in the file.
1740 */
1741 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1742 return error;
1743 }
1744 fo -= args->geo->fsbcount;
1745 /*
1746 * If there are freespace blocks other than the first one,
1747 * take this opportunity to remove trailing empty freespace blocks
1748 * that may have been left behind during no-space-reservation
1749 * operations.
1750 */
1751 while (fo > args->geo->freeblk) {
1752 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1753 return error;
1754 }
1755 if (rval)
1756 fo -= args->geo->fsbcount;
1757 else
1758 return 0;
1759 }
1760 /*
1761 * Now find the block just before the freespace block.
1762 */
1763 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1764 return error;
1765 }
1766 /*
1767 * If it's not the single leaf block, give up.
1768 */
1769 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1770 return 0;
1771 lbp = state->path.blk[0].bp;
1772 leaf = lbp->b_addr;
1773 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1774
1775 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1776 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1777
1778 /*
1779 * Read the freespace block.
1780 */
1781 error = xfs_dir2_free_read(tp, dp, args->geo->freeblk, &fbp);
1782 if (error)
1783 return error;
1784 free = fbp->b_addr;
1785 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1786
1787 ASSERT(!freehdr.firstdb);
1788
1789 /*
1790 * Now see if the leafn and free data will fit in a leaf1.
1791 * If not, release the buffer and give up.
1792 */
1793 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1794 xfs_trans_brelse(tp, fbp);
1795 return 0;
1796 }
1797
1798 /*
1799 * If the leaf has any stale entries in it, compress them out.
1800 */
1801 if (leafhdr.stale)
1802 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1803
1804 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1805 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1806 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1807 ? XFS_DIR2_LEAF1_MAGIC
1808 : XFS_DIR3_LEAF1_MAGIC;
1809
1810 /*
1811 * Set up the leaf tail from the freespace block.
1812 */
1813 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1814 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1815
1816 /*
1817 * Set up the leaf bests table.
1818 */
1819 memcpy(xfs_dir2_leaf_bests_p(ltp), dp->d_ops->free_bests_p(free),
1820 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1821
1822 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1823 xfs_dir3_leaf_log_header(args, lbp);
1824 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1825 xfs_dir3_leaf_log_tail(args, lbp);
1826 xfs_dir3_leaf_check(dp, lbp);
1827
1828 /*
1829 * Get rid of the freespace block.
1830 */
1831 error = xfs_dir2_shrink_inode(args,
1832 xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1833 fbp);
1834 if (error) {
1835 /*
1836 * This can't fail here because it can only happen when
1837 * punching out the middle of an extent, and this is an
1838 * isolated block.
1839 */
1840 ASSERT(error != -ENOSPC);
1841 return error;
1842 }
1843 fbp = NULL;
1844 /*
1845 * Now see if we can convert the single-leaf directory
1846 * down to a block form directory.
1847 * This routine always kills the dabuf for the leaf, so
1848 * eliminate it from the path.
1849 */
1850 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1851 state->path.blk[0].bp = NULL;
1852 return error;
1853 }