]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_dir2_leaf.c
xfs: harden directory integrity checks some more
[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_fspriv;
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 xfs_dir2_data_use_free(args, dbp, dup,
878 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
879 &needlog, &needscan);
880 /*
881 * Initialize our new entry (at last).
882 */
883 dep = (xfs_dir2_data_entry_t *)dup;
884 dep->inumber = cpu_to_be64(args->inumber);
885 dep->namelen = args->namelen;
886 memcpy(dep->name, args->name, dep->namelen);
887 dp->d_ops->data_put_ftype(dep, args->filetype);
888 tagp = dp->d_ops->data_entry_tag_p(dep);
889 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
890 /*
891 * Need to scan fix up the bestfree table.
892 */
893 if (needscan)
894 xfs_dir2_data_freescan(dp, hdr, &needlog);
895 /*
896 * Need to log the data block's header.
897 */
898 if (needlog)
899 xfs_dir2_data_log_header(args, dbp);
900 xfs_dir2_data_log_entry(args, dbp, dep);
901 /*
902 * If the bests table needs to be changed, do it.
903 * Log the change unless we've already done that.
904 */
905 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
906 bestsp[use_block] = bf[0].length;
907 if (!grown)
908 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
909 }
910
911 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
912 highstale, &lfloglow, &lfloghigh);
913
914 /*
915 * Fill in the new leaf entry.
916 */
917 lep->hashval = cpu_to_be32(args->hashval);
918 lep->address = cpu_to_be32(
919 xfs_dir2_db_off_to_dataptr(args->geo, use_block,
920 be16_to_cpu(*tagp)));
921 /*
922 * Log the leaf fields and give up the buffers.
923 */
924 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
925 xfs_dir3_leaf_log_header(args, lbp);
926 xfs_dir3_leaf_log_ents(args, lbp, lfloglow, lfloghigh);
927 xfs_dir3_leaf_check(dp, lbp);
928 xfs_dir3_data_check(dp, dbp);
929 return 0;
930 }
931
932 /*
933 * Compact out any stale entries in the leaf.
934 * Log the header and changed leaf entries, if any.
935 */
936 void
937 xfs_dir3_leaf_compact(
938 xfs_da_args_t *args, /* operation arguments */
939 struct xfs_dir3_icleaf_hdr *leafhdr,
940 struct xfs_buf *bp) /* leaf buffer */
941 {
942 int from; /* source leaf index */
943 xfs_dir2_leaf_t *leaf; /* leaf structure */
944 int loglow; /* first leaf entry to log */
945 int to; /* target leaf index */
946 struct xfs_dir2_leaf_entry *ents;
947 struct xfs_inode *dp = args->dp;
948
949 leaf = bp->b_addr;
950 if (!leafhdr->stale)
951 return;
952
953 /*
954 * Compress out the stale entries in place.
955 */
956 ents = dp->d_ops->leaf_ents_p(leaf);
957 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
958 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
959 continue;
960 /*
961 * Only actually copy the entries that are different.
962 */
963 if (from > to) {
964 if (loglow == -1)
965 loglow = to;
966 ents[to] = ents[from];
967 }
968 to++;
969 }
970 /*
971 * Update and log the header, log the leaf entries.
972 */
973 ASSERT(leafhdr->stale == from - to);
974 leafhdr->count -= leafhdr->stale;
975 leafhdr->stale = 0;
976
977 dp->d_ops->leaf_hdr_to_disk(leaf, leafhdr);
978 xfs_dir3_leaf_log_header(args, bp);
979 if (loglow != -1)
980 xfs_dir3_leaf_log_ents(args, bp, loglow, to - 1);
981 }
982
983 /*
984 * Compact the leaf entries, removing stale ones.
985 * Leave one stale entry behind - the one closest to our
986 * insertion index - and the caller will shift that one to our insertion
987 * point later.
988 * Return new insertion index, where the remaining stale entry is,
989 * and leaf logging indices.
990 */
991 void
992 xfs_dir3_leaf_compact_x1(
993 struct xfs_dir3_icleaf_hdr *leafhdr,
994 struct xfs_dir2_leaf_entry *ents,
995 int *indexp, /* insertion index */
996 int *lowstalep, /* out: stale entry before us */
997 int *highstalep, /* out: stale entry after us */
998 int *lowlogp, /* out: low log index */
999 int *highlogp) /* out: high log index */
1000 {
1001 int from; /* source copy index */
1002 int highstale; /* stale entry at/after index */
1003 int index; /* insertion index */
1004 int keepstale; /* source index of kept stale */
1005 int lowstale; /* stale entry before index */
1006 int newindex=0; /* new insertion index */
1007 int to; /* destination copy index */
1008
1009 ASSERT(leafhdr->stale > 1);
1010 index = *indexp;
1011
1012 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1013
1014 /*
1015 * Pick the better of lowstale and highstale.
1016 */
1017 if (lowstale >= 0 &&
1018 (highstale == leafhdr->count ||
1019 index - lowstale <= highstale - index))
1020 keepstale = lowstale;
1021 else
1022 keepstale = highstale;
1023 /*
1024 * Copy the entries in place, removing all the stale entries
1025 * except keepstale.
1026 */
1027 for (from = to = 0; from < leafhdr->count; from++) {
1028 /*
1029 * Notice the new value of index.
1030 */
1031 if (index == from)
1032 newindex = to;
1033 if (from != keepstale &&
1034 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1035 if (from == to)
1036 *lowlogp = to;
1037 continue;
1038 }
1039 /*
1040 * Record the new keepstale value for the insertion.
1041 */
1042 if (from == keepstale)
1043 lowstale = highstale = to;
1044 /*
1045 * Copy only the entries that have moved.
1046 */
1047 if (from > to)
1048 ents[to] = ents[from];
1049 to++;
1050 }
1051 ASSERT(from > to);
1052 /*
1053 * If the insertion point was past the last entry,
1054 * set the new insertion point accordingly.
1055 */
1056 if (index == from)
1057 newindex = to;
1058 *indexp = newindex;
1059 /*
1060 * Adjust the leaf header values.
1061 */
1062 leafhdr->count -= from - to;
1063 leafhdr->stale = 1;
1064 /*
1065 * Remember the low/high stale value only in the "right"
1066 * direction.
1067 */
1068 if (lowstale >= newindex)
1069 lowstale = -1;
1070 else
1071 highstale = leafhdr->count;
1072 *highlogp = leafhdr->count - 1;
1073 *lowstalep = lowstale;
1074 *highstalep = highstale;
1075 }
1076
1077 /*
1078 * Log the bests entries indicated from a leaf1 block.
1079 */
1080 static void
1081 xfs_dir3_leaf_log_bests(
1082 struct xfs_da_args *args,
1083 struct xfs_buf *bp, /* leaf buffer */
1084 int first, /* first entry to log */
1085 int last) /* last entry to log */
1086 {
1087 __be16 *firstb; /* pointer to first entry */
1088 __be16 *lastb; /* pointer to last entry */
1089 struct xfs_dir2_leaf *leaf = bp->b_addr;
1090 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1091
1092 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1093 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1094
1095 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1096 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1097 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1098 xfs_trans_log_buf(args->trans, bp,
1099 (uint)((char *)firstb - (char *)leaf),
1100 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1101 }
1102
1103 /*
1104 * Log the leaf entries indicated from a leaf1 or leafn block.
1105 */
1106 void
1107 xfs_dir3_leaf_log_ents(
1108 struct xfs_da_args *args,
1109 struct xfs_buf *bp,
1110 int first,
1111 int last)
1112 {
1113 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1114 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1115 struct xfs_dir2_leaf *leaf = bp->b_addr;
1116 struct xfs_dir2_leaf_entry *ents;
1117
1118 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1119 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1120 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1121 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1122
1123 ents = args->dp->d_ops->leaf_ents_p(leaf);
1124 firstlep = &ents[first];
1125 lastlep = &ents[last];
1126 xfs_trans_log_buf(args->trans, bp,
1127 (uint)((char *)firstlep - (char *)leaf),
1128 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1129 }
1130
1131 /*
1132 * Log the header of the leaf1 or leafn block.
1133 */
1134 void
1135 xfs_dir3_leaf_log_header(
1136 struct xfs_da_args *args,
1137 struct xfs_buf *bp)
1138 {
1139 struct xfs_dir2_leaf *leaf = bp->b_addr;
1140
1141 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1142 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1143 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1144 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1145
1146 xfs_trans_log_buf(args->trans, bp,
1147 (uint)((char *)&leaf->hdr - (char *)leaf),
1148 args->dp->d_ops->leaf_hdr_size - 1);
1149 }
1150
1151 /*
1152 * Log the tail of the leaf1 block.
1153 */
1154 STATIC void
1155 xfs_dir3_leaf_log_tail(
1156 struct xfs_da_args *args,
1157 struct xfs_buf *bp)
1158 {
1159 struct xfs_dir2_leaf *leaf = bp->b_addr;
1160 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1161
1162 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1163 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1164 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1165 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1166
1167 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1168 xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1169 (uint)(args->geo->blksize - 1));
1170 }
1171
1172 /*
1173 * Look up the entry referred to by args in the leaf format directory.
1174 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1175 * is also used by the node-format code.
1176 */
1177 int
1178 xfs_dir2_leaf_lookup(
1179 xfs_da_args_t *args) /* operation arguments */
1180 {
1181 struct xfs_buf *dbp; /* data block buffer */
1182 xfs_dir2_data_entry_t *dep; /* data block entry */
1183 xfs_inode_t *dp; /* incore directory inode */
1184 int error; /* error return code */
1185 int index; /* found entry index */
1186 struct xfs_buf *lbp; /* leaf buffer */
1187 xfs_dir2_leaf_t *leaf; /* leaf structure */
1188 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1189 xfs_trans_t *tp; /* transaction pointer */
1190 struct xfs_dir2_leaf_entry *ents;
1191
1192 trace_xfs_dir2_leaf_lookup(args);
1193
1194 /*
1195 * Look up name in the leaf block, returning both buffers and index.
1196 */
1197 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1198 return error;
1199 }
1200 tp = args->trans;
1201 dp = args->dp;
1202 xfs_dir3_leaf_check(dp, lbp);
1203 leaf = lbp->b_addr;
1204 ents = dp->d_ops->leaf_ents_p(leaf);
1205 /*
1206 * Get to the leaf entry and contained data entry address.
1207 */
1208 lep = &ents[index];
1209
1210 /*
1211 * Point to the data entry.
1212 */
1213 dep = (xfs_dir2_data_entry_t *)
1214 ((char *)dbp->b_addr +
1215 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1216 /*
1217 * Return the found inode number & CI name if appropriate
1218 */
1219 args->inumber = be64_to_cpu(dep->inumber);
1220 args->filetype = dp->d_ops->data_get_ftype(dep);
1221 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1222 xfs_trans_brelse(tp, dbp);
1223 xfs_trans_brelse(tp, lbp);
1224 return error;
1225 }
1226
1227 /*
1228 * Look up name/hash in the leaf block.
1229 * Fill in indexp with the found index, and dbpp with the data buffer.
1230 * If not found dbpp will be NULL, and ENOENT comes back.
1231 * lbpp will always be filled in with the leaf buffer unless there's an error.
1232 */
1233 static int /* error */
1234 xfs_dir2_leaf_lookup_int(
1235 xfs_da_args_t *args, /* operation arguments */
1236 struct xfs_buf **lbpp, /* out: leaf buffer */
1237 int *indexp, /* out: index in leaf block */
1238 struct xfs_buf **dbpp) /* out: data buffer */
1239 {
1240 xfs_dir2_db_t curdb = -1; /* current data block number */
1241 struct xfs_buf *dbp = NULL; /* data buffer */
1242 xfs_dir2_data_entry_t *dep; /* data entry */
1243 xfs_inode_t *dp; /* incore directory inode */
1244 int error; /* error return code */
1245 int index; /* index in leaf block */
1246 struct xfs_buf *lbp; /* leaf buffer */
1247 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1248 xfs_dir2_leaf_t *leaf; /* leaf structure */
1249 xfs_mount_t *mp; /* filesystem mount point */
1250 xfs_dir2_db_t newdb; /* new data block number */
1251 xfs_trans_t *tp; /* transaction pointer */
1252 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1253 enum xfs_dacmp cmp; /* name compare result */
1254 struct xfs_dir2_leaf_entry *ents;
1255 struct xfs_dir3_icleaf_hdr leafhdr;
1256
1257 dp = args->dp;
1258 tp = args->trans;
1259 mp = dp->i_mount;
1260
1261 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
1262 if (error)
1263 return error;
1264
1265 *lbpp = lbp;
1266 leaf = lbp->b_addr;
1267 xfs_dir3_leaf_check(dp, lbp);
1268 ents = dp->d_ops->leaf_ents_p(leaf);
1269 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1270
1271 /*
1272 * Look for the first leaf entry with our hash value.
1273 */
1274 index = xfs_dir2_leaf_search_hash(args, lbp);
1275 /*
1276 * Loop over all the entries with the right hash value
1277 * looking to match the name.
1278 */
1279 for (lep = &ents[index];
1280 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1281 lep++, index++) {
1282 /*
1283 * Skip over stale leaf entries.
1284 */
1285 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1286 continue;
1287 /*
1288 * Get the new data block number.
1289 */
1290 newdb = xfs_dir2_dataptr_to_db(args->geo,
1291 be32_to_cpu(lep->address));
1292 /*
1293 * If it's not the same as the old data block number,
1294 * need to pitch the old one and read the new one.
1295 */
1296 if (newdb != curdb) {
1297 if (dbp)
1298 xfs_trans_brelse(tp, dbp);
1299 error = xfs_dir3_data_read(tp, dp,
1300 xfs_dir2_db_to_da(args->geo, newdb),
1301 -1, &dbp);
1302 if (error) {
1303 xfs_trans_brelse(tp, lbp);
1304 return error;
1305 }
1306 curdb = newdb;
1307 }
1308 /*
1309 * Point to the data entry.
1310 */
1311 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1312 xfs_dir2_dataptr_to_off(args->geo,
1313 be32_to_cpu(lep->address)));
1314 /*
1315 * Compare name and if it's an exact match, return the index
1316 * and buffer. If it's the first case-insensitive match, store
1317 * the index and buffer and continue looking for an exact match.
1318 */
1319 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1320 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1321 args->cmpresult = cmp;
1322 *indexp = index;
1323 /* case exact match: return the current buffer. */
1324 if (cmp == XFS_CMP_EXACT) {
1325 *dbpp = dbp;
1326 return 0;
1327 }
1328 cidb = curdb;
1329 }
1330 }
1331 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1332 /*
1333 * Here, we can only be doing a lookup (not a rename or remove).
1334 * If a case-insensitive match was found earlier, re-read the
1335 * appropriate data block if required and return it.
1336 */
1337 if (args->cmpresult == XFS_CMP_CASE) {
1338 ASSERT(cidb != -1);
1339 if (cidb != curdb) {
1340 xfs_trans_brelse(tp, dbp);
1341 error = xfs_dir3_data_read(tp, dp,
1342 xfs_dir2_db_to_da(args->geo, cidb),
1343 -1, &dbp);
1344 if (error) {
1345 xfs_trans_brelse(tp, lbp);
1346 return error;
1347 }
1348 }
1349 *dbpp = dbp;
1350 return 0;
1351 }
1352 /*
1353 * No match found, return -ENOENT.
1354 */
1355 ASSERT(cidb == -1);
1356 if (dbp)
1357 xfs_trans_brelse(tp, dbp);
1358 xfs_trans_brelse(tp, lbp);
1359 return -ENOENT;
1360 }
1361
1362 /*
1363 * Remove an entry from a leaf format directory.
1364 */
1365 int /* error */
1366 xfs_dir2_leaf_removename(
1367 xfs_da_args_t *args) /* operation arguments */
1368 {
1369 __be16 *bestsp; /* leaf block best freespace */
1370 xfs_dir2_data_hdr_t *hdr; /* data block header */
1371 xfs_dir2_db_t db; /* data block number */
1372 struct xfs_buf *dbp; /* data block buffer */
1373 xfs_dir2_data_entry_t *dep; /* data entry structure */
1374 xfs_inode_t *dp; /* incore directory inode */
1375 int error; /* error return code */
1376 xfs_dir2_db_t i; /* temporary data block # */
1377 int index; /* index into leaf entries */
1378 struct xfs_buf *lbp; /* leaf buffer */
1379 xfs_dir2_leaf_t *leaf; /* leaf structure */
1380 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1381 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1382 int needlog; /* need to log data header */
1383 int needscan; /* need to rescan data frees */
1384 xfs_dir2_data_off_t oldbest; /* old value of best free */
1385 struct xfs_dir2_data_free *bf; /* bestfree table */
1386 struct xfs_dir2_leaf_entry *ents;
1387 struct xfs_dir3_icleaf_hdr leafhdr;
1388
1389 trace_xfs_dir2_leaf_removename(args);
1390
1391 /*
1392 * Lookup the leaf entry, get the leaf and data blocks read in.
1393 */
1394 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1395 return error;
1396 }
1397 dp = args->dp;
1398 leaf = lbp->b_addr;
1399 hdr = dbp->b_addr;
1400 xfs_dir3_data_check(dp, dbp);
1401 bf = dp->d_ops->data_bestfree_p(hdr);
1402 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1403 ents = dp->d_ops->leaf_ents_p(leaf);
1404 /*
1405 * Point to the leaf entry, use that to point to the data entry.
1406 */
1407 lep = &ents[index];
1408 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1409 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1410 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1411 needscan = needlog = 0;
1412 oldbest = be16_to_cpu(bf[0].length);
1413 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1414 bestsp = xfs_dir2_leaf_bests_p(ltp);
1415 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1416 /*
1417 * Mark the former data entry unused.
1418 */
1419 xfs_dir2_data_make_free(args, dbp,
1420 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1421 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1422 /*
1423 * We just mark the leaf entry stale by putting a null in it.
1424 */
1425 leafhdr.stale++;
1426 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1427 xfs_dir3_leaf_log_header(args, lbp);
1428
1429 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1430 xfs_dir3_leaf_log_ents(args, lbp, index, index);
1431
1432 /*
1433 * Scan the freespace in the data block again if necessary,
1434 * log the data block header if necessary.
1435 */
1436 if (needscan)
1437 xfs_dir2_data_freescan(dp, hdr, &needlog);
1438 if (needlog)
1439 xfs_dir2_data_log_header(args, dbp);
1440 /*
1441 * If the longest freespace in the data block has changed,
1442 * put the new value in the bests table and log that.
1443 */
1444 if (be16_to_cpu(bf[0].length) != oldbest) {
1445 bestsp[db] = bf[0].length;
1446 xfs_dir3_leaf_log_bests(args, lbp, db, db);
1447 }
1448 xfs_dir3_data_check(dp, dbp);
1449 /*
1450 * If the data block is now empty then get rid of the data block.
1451 */
1452 if (be16_to_cpu(bf[0].length) ==
1453 args->geo->blksize - dp->d_ops->data_entry_offset) {
1454 ASSERT(db != args->geo->datablk);
1455 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1456 /*
1457 * Nope, can't get rid of it because it caused
1458 * allocation of a bmap btree block to do so.
1459 * Just go on, returning success, leaving the
1460 * empty block in place.
1461 */
1462 if (error == -ENOSPC && args->total == 0)
1463 error = 0;
1464 xfs_dir3_leaf_check(dp, lbp);
1465 return error;
1466 }
1467 dbp = NULL;
1468 /*
1469 * If this is the last data block then compact the
1470 * bests table by getting rid of entries.
1471 */
1472 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1473 /*
1474 * Look for the last active entry (i).
1475 */
1476 for (i = db - 1; i > 0; i--) {
1477 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1478 break;
1479 }
1480 /*
1481 * Copy the table down so inactive entries at the
1482 * end are removed.
1483 */
1484 memmove(&bestsp[db - i], bestsp,
1485 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1486 be32_add_cpu(&ltp->bestcount, -(db - i));
1487 xfs_dir3_leaf_log_tail(args, lbp);
1488 xfs_dir3_leaf_log_bests(args, lbp, 0,
1489 be32_to_cpu(ltp->bestcount) - 1);
1490 } else
1491 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1492 }
1493 /*
1494 * If the data block was not the first one, drop it.
1495 */
1496 else if (db != args->geo->datablk)
1497 dbp = NULL;
1498
1499 xfs_dir3_leaf_check(dp, lbp);
1500 /*
1501 * See if we can convert to block form.
1502 */
1503 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1504 }
1505
1506 /*
1507 * Replace the inode number in a leaf format directory entry.
1508 */
1509 int /* error */
1510 xfs_dir2_leaf_replace(
1511 xfs_da_args_t *args) /* operation arguments */
1512 {
1513 struct xfs_buf *dbp; /* data block buffer */
1514 xfs_dir2_data_entry_t *dep; /* data block entry */
1515 xfs_inode_t *dp; /* incore directory inode */
1516 int error; /* error return code */
1517 int index; /* index of leaf entry */
1518 struct xfs_buf *lbp; /* leaf buffer */
1519 xfs_dir2_leaf_t *leaf; /* leaf structure */
1520 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1521 xfs_trans_t *tp; /* transaction pointer */
1522 struct xfs_dir2_leaf_entry *ents;
1523
1524 trace_xfs_dir2_leaf_replace(args);
1525
1526 /*
1527 * Look up the entry.
1528 */
1529 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1530 return error;
1531 }
1532 dp = args->dp;
1533 leaf = lbp->b_addr;
1534 ents = dp->d_ops->leaf_ents_p(leaf);
1535 /*
1536 * Point to the leaf entry, get data address from it.
1537 */
1538 lep = &ents[index];
1539 /*
1540 * Point to the data entry.
1541 */
1542 dep = (xfs_dir2_data_entry_t *)
1543 ((char *)dbp->b_addr +
1544 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1545 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1546 /*
1547 * Put the new inode number in, log it.
1548 */
1549 dep->inumber = cpu_to_be64(args->inumber);
1550 dp->d_ops->data_put_ftype(dep, args->filetype);
1551 tp = args->trans;
1552 xfs_dir2_data_log_entry(args, dbp, dep);
1553 xfs_dir3_leaf_check(dp, lbp);
1554 xfs_trans_brelse(tp, lbp);
1555 return 0;
1556 }
1557
1558 /*
1559 * Return index in the leaf block (lbp) which is either the first
1560 * one with this hash value, or if there are none, the insert point
1561 * for that hash value.
1562 */
1563 int /* index value */
1564 xfs_dir2_leaf_search_hash(
1565 xfs_da_args_t *args, /* operation arguments */
1566 struct xfs_buf *lbp) /* leaf buffer */
1567 {
1568 xfs_dahash_t hash=0; /* hash from this entry */
1569 xfs_dahash_t hashwant; /* hash value looking for */
1570 int high; /* high leaf index */
1571 int low; /* low leaf index */
1572 xfs_dir2_leaf_t *leaf; /* leaf structure */
1573 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1574 int mid=0; /* current leaf index */
1575 struct xfs_dir2_leaf_entry *ents;
1576 struct xfs_dir3_icleaf_hdr leafhdr;
1577
1578 leaf = lbp->b_addr;
1579 ents = args->dp->d_ops->leaf_ents_p(leaf);
1580 args->dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1581
1582 /*
1583 * Note, the table cannot be empty, so we have to go through the loop.
1584 * Binary search the leaf entries looking for our hash value.
1585 */
1586 for (lep = ents, low = 0, high = leafhdr.count - 1,
1587 hashwant = args->hashval;
1588 low <= high; ) {
1589 mid = (low + high) >> 1;
1590 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1591 break;
1592 if (hash < hashwant)
1593 low = mid + 1;
1594 else
1595 high = mid - 1;
1596 }
1597 /*
1598 * Found one, back up through all the equal hash values.
1599 */
1600 if (hash == hashwant) {
1601 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1602 mid--;
1603 }
1604 }
1605 /*
1606 * Need to point to an entry higher than ours.
1607 */
1608 else if (hash < hashwant)
1609 mid++;
1610 return mid;
1611 }
1612
1613 /*
1614 * Trim off a trailing data block. We know it's empty since the leaf
1615 * freespace table says so.
1616 */
1617 int /* error */
1618 xfs_dir2_leaf_trim_data(
1619 xfs_da_args_t *args, /* operation arguments */
1620 struct xfs_buf *lbp, /* leaf buffer */
1621 xfs_dir2_db_t db) /* data block number */
1622 {
1623 __be16 *bestsp; /* leaf bests table */
1624 struct xfs_buf *dbp; /* data block buffer */
1625 xfs_inode_t *dp; /* incore directory inode */
1626 int error; /* error return value */
1627 xfs_dir2_leaf_t *leaf; /* leaf structure */
1628 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1629 xfs_trans_t *tp; /* transaction pointer */
1630
1631 dp = args->dp;
1632 tp = args->trans;
1633 /*
1634 * Read the offending data block. We need its buffer.
1635 */
1636 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(args->geo, db),
1637 -1, &dbp);
1638 if (error)
1639 return error;
1640
1641 leaf = lbp->b_addr;
1642 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1643
1644 #ifdef DEBUG
1645 {
1646 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1647 struct xfs_dir2_data_free *bf = dp->d_ops->data_bestfree_p(hdr);
1648
1649 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1650 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1651 ASSERT(be16_to_cpu(bf[0].length) ==
1652 args->geo->blksize - dp->d_ops->data_entry_offset);
1653 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1654 }
1655 #endif
1656
1657 /*
1658 * Get rid of the data block.
1659 */
1660 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1661 ASSERT(error != -ENOSPC);
1662 xfs_trans_brelse(tp, dbp);
1663 return error;
1664 }
1665 /*
1666 * Eliminate the last bests entry from the table.
1667 */
1668 bestsp = xfs_dir2_leaf_bests_p(ltp);
1669 be32_add_cpu(&ltp->bestcount, -1);
1670 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1671 xfs_dir3_leaf_log_tail(args, lbp);
1672 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1673 return 0;
1674 }
1675
1676 static inline size_t
1677 xfs_dir3_leaf_size(
1678 struct xfs_dir3_icleaf_hdr *hdr,
1679 int counts)
1680 {
1681 int entries;
1682 int hdrsize;
1683
1684 entries = hdr->count - hdr->stale;
1685 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1686 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1687 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1688 else
1689 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1690
1691 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1692 + counts * sizeof(xfs_dir2_data_off_t)
1693 + sizeof(xfs_dir2_leaf_tail_t);
1694 }
1695
1696 /*
1697 * Convert node form directory to leaf form directory.
1698 * The root of the node form dir needs to already be a LEAFN block.
1699 * Just return if we can't do anything.
1700 */
1701 int /* error */
1702 xfs_dir2_node_to_leaf(
1703 xfs_da_state_t *state) /* directory operation state */
1704 {
1705 xfs_da_args_t *args; /* operation arguments */
1706 xfs_inode_t *dp; /* incore directory inode */
1707 int error; /* error return code */
1708 struct xfs_buf *fbp; /* buffer for freespace block */
1709 xfs_fileoff_t fo; /* freespace file offset */
1710 xfs_dir2_free_t *free; /* freespace structure */
1711 struct xfs_buf *lbp; /* buffer for leaf block */
1712 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1713 xfs_dir2_leaf_t *leaf; /* leaf structure */
1714 xfs_mount_t *mp; /* filesystem mount point */
1715 int rval; /* successful free trim? */
1716 xfs_trans_t *tp; /* transaction pointer */
1717 struct xfs_dir3_icleaf_hdr leafhdr;
1718 struct xfs_dir3_icfree_hdr freehdr;
1719
1720 /*
1721 * There's more than a leaf level in the btree, so there must
1722 * be multiple leafn blocks. Give up.
1723 */
1724 if (state->path.active > 1)
1725 return 0;
1726 args = state->args;
1727
1728 trace_xfs_dir2_node_to_leaf(args);
1729
1730 mp = state->mp;
1731 dp = args->dp;
1732 tp = args->trans;
1733 /*
1734 * Get the last offset in the file.
1735 */
1736 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1737 return error;
1738 }
1739 fo -= args->geo->fsbcount;
1740 /*
1741 * If there are freespace blocks other than the first one,
1742 * take this opportunity to remove trailing empty freespace blocks
1743 * that may have been left behind during no-space-reservation
1744 * operations.
1745 */
1746 while (fo > args->geo->freeblk) {
1747 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1748 return error;
1749 }
1750 if (rval)
1751 fo -= args->geo->fsbcount;
1752 else
1753 return 0;
1754 }
1755 /*
1756 * Now find the block just before the freespace block.
1757 */
1758 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1759 return error;
1760 }
1761 /*
1762 * If it's not the single leaf block, give up.
1763 */
1764 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1765 return 0;
1766 lbp = state->path.blk[0].bp;
1767 leaf = lbp->b_addr;
1768 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1769
1770 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1771 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1772
1773 /*
1774 * Read the freespace block.
1775 */
1776 error = xfs_dir2_free_read(tp, dp, args->geo->freeblk, &fbp);
1777 if (error)
1778 return error;
1779 free = fbp->b_addr;
1780 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1781
1782 ASSERT(!freehdr.firstdb);
1783
1784 /*
1785 * Now see if the leafn and free data will fit in a leaf1.
1786 * If not, release the buffer and give up.
1787 */
1788 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1789 xfs_trans_brelse(tp, fbp);
1790 return 0;
1791 }
1792
1793 /*
1794 * If the leaf has any stale entries in it, compress them out.
1795 */
1796 if (leafhdr.stale)
1797 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1798
1799 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1800 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1801 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1802 ? XFS_DIR2_LEAF1_MAGIC
1803 : XFS_DIR3_LEAF1_MAGIC;
1804
1805 /*
1806 * Set up the leaf tail from the freespace block.
1807 */
1808 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1809 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1810
1811 /*
1812 * Set up the leaf bests table.
1813 */
1814 memcpy(xfs_dir2_leaf_bests_p(ltp), dp->d_ops->free_bests_p(free),
1815 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1816
1817 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1818 xfs_dir3_leaf_log_header(args, lbp);
1819 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1820 xfs_dir3_leaf_log_tail(args, lbp);
1821 xfs_dir3_leaf_check(dp, lbp);
1822
1823 /*
1824 * Get rid of the freespace block.
1825 */
1826 error = xfs_dir2_shrink_inode(args,
1827 xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1828 fbp);
1829 if (error) {
1830 /*
1831 * This can't fail here because it can only happen when
1832 * punching out the middle of an extent, and this is an
1833 * isolated block.
1834 */
1835 ASSERT(error != -ENOSPC);
1836 return error;
1837 }
1838 fbp = NULL;
1839 /*
1840 * Now see if we can convert the single-leaf directory
1841 * down to a block form directory.
1842 * This routine always kills the dabuf for the leaf, so
1843 * eliminate it from the path.
1844 */
1845 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1846 state->path.blk[0].bp = NULL;
1847 return error;
1848 }