1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_error.h"
19 #include "xfs_trace.h"
20 #include "xfs_trans.h"
21 #include "xfs_buf_item.h"
24 * Local function declarations.
26 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
27 int *indexp, struct xfs_buf **dbpp);
28 static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
29 struct xfs_buf *bp, int first, int last);
30 static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
34 xfs_dir2_leaf_hdr_from_disk(
36 struct xfs_dir3_icleaf_hdr *to,
37 struct xfs_dir2_leaf *from)
39 if (xfs_sb_version_hascrc(&mp->m_sb)) {
40 struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;
42 to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
43 to->back = be32_to_cpu(from3->hdr.info.hdr.back);
44 to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
45 to->count = be16_to_cpu(from3->hdr.count);
46 to->stale = be16_to_cpu(from3->hdr.stale);
48 ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
49 to->magic == XFS_DIR3_LEAFN_MAGIC);
51 to->forw = be32_to_cpu(from->hdr.info.forw);
52 to->back = be32_to_cpu(from->hdr.info.back);
53 to->magic = be16_to_cpu(from->hdr.info.magic);
54 to->count = be16_to_cpu(from->hdr.count);
55 to->stale = be16_to_cpu(from->hdr.stale);
57 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
58 to->magic == XFS_DIR2_LEAFN_MAGIC);
63 xfs_dir2_leaf_hdr_to_disk(
65 struct xfs_dir2_leaf *to,
66 struct xfs_dir3_icleaf_hdr *from)
68 if (xfs_sb_version_hascrc(&mp->m_sb)) {
69 struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;
71 ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
72 from->magic == XFS_DIR3_LEAFN_MAGIC);
74 to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
75 to3->hdr.info.hdr.back = cpu_to_be32(from->back);
76 to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
77 to3->hdr.count = cpu_to_be16(from->count);
78 to3->hdr.stale = cpu_to_be16(from->stale);
80 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
81 from->magic == XFS_DIR2_LEAFN_MAGIC);
83 to->hdr.info.forw = cpu_to_be32(from->forw);
84 to->hdr.info.back = cpu_to_be32(from->back);
85 to->hdr.info.magic = cpu_to_be16(from->magic);
86 to->hdr.count = cpu_to_be16(from->count);
87 to->hdr.stale = cpu_to_be16(from->stale);
92 * Check the internal consistency of a leaf1 block.
93 * Pop an assert if something is wrong.
101 struct xfs_dir2_leaf *leaf = bp->b_addr;
102 struct xfs_dir3_icleaf_hdr leafhdr;
104 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
106 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
107 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
108 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
109 return __this_address;
110 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
111 return __this_address;
113 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
118 struct xfs_inode *dp,
123 fa = xfs_dir3_leaf1_check(dp, bp);
126 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
127 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
132 #define xfs_dir3_leaf_check(dp, bp)
136 xfs_dir3_leaf_check_int(
137 struct xfs_mount *mp,
138 struct xfs_inode *dp,
139 struct xfs_dir3_icleaf_hdr *hdr,
140 struct xfs_dir2_leaf *leaf)
142 struct xfs_dir2_leaf_entry *ents;
143 xfs_dir2_leaf_tail_t *ltp;
146 const struct xfs_dir_ops *ops;
147 struct xfs_dir3_icleaf_hdr leafhdr;
148 struct xfs_da_geometry *geo = mp->m_dir_geo;
151 * we can be passed a null dp here from a verifier, so we need to go the
152 * hard way to get them.
154 ops = xfs_dir_get_ops(mp, dp);
157 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
161 ents = ops->leaf_ents_p(leaf);
162 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
165 * XXX (dgc): This value is not restrictive enough.
166 * Should factor in the size of the bests table as well.
167 * We can deduce a value for that from di_size.
169 if (hdr->count > ops->leaf_max_ents(geo))
170 return __this_address;
172 /* Leaves and bests don't overlap in leaf format. */
173 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
174 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
175 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
176 return __this_address;
178 /* Check hash value order, count stale entries. */
179 for (i = stale = 0; i < hdr->count; i++) {
180 if (i + 1 < hdr->count) {
181 if (be32_to_cpu(ents[i].hashval) >
182 be32_to_cpu(ents[i + 1].hashval))
183 return __this_address;
185 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
188 if (hdr->stale != stale)
189 return __this_address;
194 * We verify the magic numbers before decoding the leaf header so that on debug
195 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
196 * to incorrect magic numbers.
198 static xfs_failaddr_t
199 xfs_dir3_leaf_verify(
202 struct xfs_mount *mp = bp->b_mount;
203 struct xfs_dir2_leaf *leaf = bp->b_addr;
206 fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
210 return xfs_dir3_leaf_check_int(mp, NULL, NULL, leaf);
214 xfs_dir3_leaf_read_verify(
217 struct xfs_mount *mp = bp->b_mount;
220 if (xfs_sb_version_hascrc(&mp->m_sb) &&
221 !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
222 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
224 fa = xfs_dir3_leaf_verify(bp);
226 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
231 xfs_dir3_leaf_write_verify(
234 struct xfs_mount *mp = bp->b_mount;
235 struct xfs_buf_log_item *bip = bp->b_log_item;
236 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
239 fa = xfs_dir3_leaf_verify(bp);
241 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
245 if (!xfs_sb_version_hascrc(&mp->m_sb))
249 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
251 xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
254 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
255 .name = "xfs_dir3_leaf1",
256 .magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC),
257 cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) },
258 .verify_read = xfs_dir3_leaf_read_verify,
259 .verify_write = xfs_dir3_leaf_write_verify,
260 .verify_struct = xfs_dir3_leaf_verify,
263 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
264 .name = "xfs_dir3_leafn",
265 .magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC),
266 cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) },
267 .verify_read = xfs_dir3_leaf_read_verify,
268 .verify_write = xfs_dir3_leaf_write_verify,
269 .verify_struct = xfs_dir3_leaf_verify,
274 struct xfs_trans *tp,
275 struct xfs_inode *dp,
277 xfs_daddr_t mappedbno,
278 struct xfs_buf **bpp)
282 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
283 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
284 if (!err && tp && *bpp)
285 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
291 struct xfs_trans *tp,
292 struct xfs_inode *dp,
294 xfs_daddr_t mappedbno,
295 struct xfs_buf **bpp)
299 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
300 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
301 if (!err && tp && *bpp)
302 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
307 * Initialize a new leaf block, leaf1 or leafn magic accepted.
311 struct xfs_mount *mp,
312 struct xfs_trans *tp,
317 struct xfs_dir2_leaf *leaf = bp->b_addr;
319 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
321 if (xfs_sb_version_hascrc(&mp->m_sb)) {
322 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
324 memset(leaf3, 0, sizeof(*leaf3));
326 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
327 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
328 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
329 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
330 leaf3->info.owner = cpu_to_be64(owner);
331 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
333 memset(leaf, 0, sizeof(*leaf));
334 leaf->hdr.info.magic = cpu_to_be16(type);
338 * If it's a leaf-format directory initialize the tail.
339 * Caller is responsible for initialising the bests table.
341 if (type == XFS_DIR2_LEAF1_MAGIC) {
342 struct xfs_dir2_leaf_tail *ltp;
344 ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
346 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
347 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
349 bp->b_ops = &xfs_dir3_leafn_buf_ops;
350 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
355 xfs_dir3_leaf_get_buf(
358 struct xfs_buf **bpp,
361 struct xfs_inode *dp = args->dp;
362 struct xfs_trans *tp = args->trans;
363 struct xfs_mount *mp = dp->i_mount;
367 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
368 ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
369 bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
371 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
372 -1, &bp, XFS_DATA_FORK);
376 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
377 xfs_dir3_leaf_log_header(args, bp);
378 if (magic == XFS_DIR2_LEAF1_MAGIC)
379 xfs_dir3_leaf_log_tail(args, bp);
385 * Convert a block form directory to a leaf form directory.
388 xfs_dir2_block_to_leaf(
389 xfs_da_args_t *args, /* operation arguments */
390 struct xfs_buf *dbp) /* input block's buffer */
392 __be16 *bestsp; /* leaf's bestsp entries */
393 xfs_dablk_t blkno; /* leaf block's bno */
394 xfs_dir2_data_hdr_t *hdr; /* block header */
395 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
396 xfs_dir2_block_tail_t *btp; /* block's tail */
397 xfs_inode_t *dp; /* incore directory inode */
398 int error; /* error return code */
399 struct xfs_buf *lbp; /* leaf block's buffer */
400 xfs_dir2_db_t ldb; /* leaf block's bno */
401 xfs_dir2_leaf_t *leaf; /* leaf structure */
402 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
403 int needlog; /* need to log block header */
404 int needscan; /* need to rescan bestfree */
405 xfs_trans_t *tp; /* transaction pointer */
406 struct xfs_dir2_data_free *bf;
407 struct xfs_dir2_leaf_entry *ents;
408 struct xfs_dir3_icleaf_hdr leafhdr;
410 trace_xfs_dir2_block_to_leaf(args);
415 * Add the leaf block to the inode.
416 * This interface will only put blocks in the leaf/node range.
417 * Since that's empty now, we'll get the root (block 0 in range).
419 if ((error = xfs_da_grow_inode(args, &blkno))) {
422 ldb = xfs_dir2_da_to_db(args->geo, blkno);
423 ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
425 * Initialize the leaf block, get a buffer for it.
427 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
433 xfs_dir3_data_check(dp, dbp);
434 btp = xfs_dir2_block_tail_p(args->geo, hdr);
435 blp = xfs_dir2_block_leaf_p(btp);
436 bf = dp->d_ops->data_bestfree_p(hdr);
437 ents = dp->d_ops->leaf_ents_p(leaf);
440 * Set the counts in the leaf header.
442 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
443 leafhdr.count = be32_to_cpu(btp->count);
444 leafhdr.stale = be32_to_cpu(btp->stale);
445 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
446 xfs_dir3_leaf_log_header(args, lbp);
449 * Could compact these but I think we always do the conversion
450 * after squeezing out stale entries.
452 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
453 xfs_dir3_leaf_log_ents(args, lbp, 0, leafhdr.count - 1);
457 * Make the space formerly occupied by the leaf entries and block
460 xfs_dir2_data_make_free(args, dbp,
461 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
462 (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
464 &needlog, &needscan);
466 * Fix up the block header, make it a data block.
468 dbp->b_ops = &xfs_dir3_data_buf_ops;
469 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
470 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
471 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
473 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
476 xfs_dir2_data_freescan(dp, hdr, &needlog);
478 * Set up leaf tail and bests table.
480 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
481 ltp->bestcount = cpu_to_be32(1);
482 bestsp = xfs_dir2_leaf_bests_p(ltp);
483 bestsp[0] = bf[0].length;
485 * Log the data header and leaf bests table.
488 xfs_dir2_data_log_header(args, dbp);
489 xfs_dir3_leaf_check(dp, lbp);
490 xfs_dir3_data_check(dp, dbp);
491 xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
496 xfs_dir3_leaf_find_stale(
497 struct xfs_dir3_icleaf_hdr *leafhdr,
498 struct xfs_dir2_leaf_entry *ents,
504 * Find the first stale entry before our index, if any.
506 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
507 if (ents[*lowstale].address ==
508 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
513 * Find the first stale entry at or after our index, if any.
514 * Stop if the result would require moving more entries than using
517 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
518 if (ents[*highstale].address ==
519 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
521 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
526 struct xfs_dir2_leaf_entry *
527 xfs_dir3_leaf_find_entry(
528 struct xfs_dir3_icleaf_hdr *leafhdr,
529 struct xfs_dir2_leaf_entry *ents,
530 int index, /* leaf table position */
531 int compact, /* need to compact leaves */
532 int lowstale, /* index of prev stale leaf */
533 int highstale, /* index of next stale leaf */
534 int *lfloglow, /* low leaf logging index */
535 int *lfloghigh) /* high leaf logging index */
537 if (!leafhdr->stale) {
538 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
541 * Now we need to make room to insert the leaf entry.
543 * If there are no stale entries, just insert a hole at index.
546 if (index < leafhdr->count)
547 memmove(lep + 1, lep,
548 (leafhdr->count - index) * sizeof(*lep));
551 * Record low and high logging indices for the leaf.
554 *lfloghigh = leafhdr->count++;
559 * There are stale entries.
561 * We will use one of them for the new entry. It's probably not at
562 * the right location, so we'll have to shift some up or down first.
564 * If we didn't compact before, we need to find the nearest stale
565 * entries before and after our insertion point.
568 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
569 &lowstale, &highstale);
572 * If the low one is better, use it.
575 (highstale == leafhdr->count ||
576 index - lowstale - 1 < highstale - index)) {
577 ASSERT(index - lowstale - 1 >= 0);
578 ASSERT(ents[lowstale].address ==
579 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
582 * Copy entries up to cover the stale entry and make room
585 if (index - lowstale - 1 > 0) {
586 memmove(&ents[lowstale], &ents[lowstale + 1],
587 (index - lowstale - 1) *
588 sizeof(xfs_dir2_leaf_entry_t));
590 *lfloglow = min(lowstale, *lfloglow);
591 *lfloghigh = max(index - 1, *lfloghigh);
593 return &ents[index - 1];
597 * The high one is better, so use that one.
599 ASSERT(highstale - index >= 0);
600 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
603 * Copy entries down to cover the stale entry and make room for the
606 if (highstale - index > 0) {
607 memmove(&ents[index + 1], &ents[index],
608 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
610 *lfloglow = min(index, *lfloglow);
611 *lfloghigh = max(highstale, *lfloghigh);
617 * Add an entry to a leaf form directory.
620 xfs_dir2_leaf_addname(
621 struct xfs_da_args *args) /* operation arguments */
623 struct xfs_dir3_icleaf_hdr leafhdr;
624 struct xfs_trans *tp = args->trans;
625 __be16 *bestsp; /* freespace table in leaf */
626 __be16 *tagp; /* end of data entry */
627 struct xfs_buf *dbp; /* data block buffer */
628 struct xfs_buf *lbp; /* leaf's buffer */
629 struct xfs_dir2_leaf *leaf; /* leaf structure */
630 struct xfs_inode *dp = args->dp; /* incore directory inode */
631 struct xfs_dir2_data_hdr *hdr; /* data block header */
632 struct xfs_dir2_data_entry *dep; /* data block entry */
633 struct xfs_dir2_leaf_entry *lep; /* leaf entry table pointer */
634 struct xfs_dir2_leaf_entry *ents;
635 struct xfs_dir2_data_unused *dup; /* data unused entry */
636 struct xfs_dir2_leaf_tail *ltp; /* leaf tail pointer */
637 struct xfs_dir2_data_free *bf; /* bestfree table */
638 int compact; /* need to compact leaves */
639 int error; /* error return value */
640 int grown; /* allocated new data block */
641 int highstale = 0; /* index of next stale leaf */
642 int i; /* temporary, index */
643 int index; /* leaf table position */
644 int length; /* length of new entry */
645 int lfloglow; /* low leaf logging index */
646 int lfloghigh; /* high leaf logging index */
647 int lowstale = 0; /* index of prev stale leaf */
648 int needbytes; /* leaf block bytes needed */
649 int needlog; /* need to log data header */
650 int needscan; /* need to rescan data free */
651 xfs_dir2_db_t use_block; /* data block number */
653 trace_xfs_dir2_leaf_addname(args);
655 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
660 * Look up the entry by hash value and name.
661 * We know it's not there, our caller has already done a lookup.
662 * So the index is of the entry to insert in front of.
663 * But if there are dup hash values the index is of the first of those.
665 index = xfs_dir2_leaf_search_hash(args, lbp);
667 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
668 ents = dp->d_ops->leaf_ents_p(leaf);
669 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
670 bestsp = xfs_dir2_leaf_bests_p(ltp);
671 length = dp->d_ops->data_entsize(args->namelen);
674 * See if there are any entries with the same hash value
675 * and space in their block for the new entry.
676 * This is good because it puts multiple same-hash value entries
677 * in a data block, improving the lookup of those entries.
679 for (use_block = -1, lep = &ents[index];
680 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
682 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
684 i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
685 ASSERT(i < be32_to_cpu(ltp->bestcount));
686 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
687 if (be16_to_cpu(bestsp[i]) >= length) {
693 * Didn't find a block yet, linear search all the data blocks.
695 if (use_block == -1) {
696 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
698 * Remember a block we see that's missing.
700 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
703 else if (be16_to_cpu(bestsp[i]) >= length) {
710 * How many bytes do we need in the leaf block?
714 needbytes += sizeof(xfs_dir2_leaf_entry_t);
716 needbytes += sizeof(xfs_dir2_data_off_t);
719 * Now kill use_block if it refers to a missing block, so we
720 * can use it as an indication of allocation needed.
722 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
725 * If we don't have enough free bytes but we can make enough
726 * by compacting out stale entries, we'll do that.
728 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
733 * Otherwise if we don't have enough free bytes we need to
734 * convert to node form.
736 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
738 * Just checking or no space reservation, give up.
740 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
742 xfs_trans_brelse(tp, lbp);
746 * Convert to node form.
748 error = xfs_dir2_leaf_to_node(args, lbp);
752 * Then add the new entry.
754 return xfs_dir2_node_addname(args);
757 * Otherwise it will fit without compaction.
762 * If just checking, then it will fit unless we needed to allocate
765 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
766 xfs_trans_brelse(tp, lbp);
767 return use_block == -1 ? -ENOSPC : 0;
770 * If no allocations are allowed, return now before we've
773 if (args->total == 0 && use_block == -1) {
774 xfs_trans_brelse(tp, lbp);
778 * Need to compact the leaf entries, removing stale ones.
779 * Leave one stale entry behind - the one closest to our
780 * insertion index - and we'll shift that one to our insertion
784 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
785 &highstale, &lfloglow, &lfloghigh);
788 * There are stale entries, so we'll need log-low and log-high
789 * impossibly bad values later.
791 else if (leafhdr.stale) {
792 lfloglow = leafhdr.count;
796 * If there was no data block space found, we need to allocate
799 if (use_block == -1) {
801 * Add the new data block.
803 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
805 xfs_trans_brelse(tp, lbp);
809 * Initialize the block.
811 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
812 xfs_trans_brelse(tp, lbp);
816 * If we're adding a new data block on the end we need to
817 * extend the bests table. Copy it up one entry.
819 if (use_block >= be32_to_cpu(ltp->bestcount)) {
821 memmove(&bestsp[0], &bestsp[1],
822 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
823 be32_add_cpu(<p->bestcount, 1);
824 xfs_dir3_leaf_log_tail(args, lbp);
825 xfs_dir3_leaf_log_bests(args, lbp, 0,
826 be32_to_cpu(ltp->bestcount) - 1);
829 * If we're filling in a previously empty block just log it.
832 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
834 bf = dp->d_ops->data_bestfree_p(hdr);
835 bestsp[use_block] = bf[0].length;
839 * Already had space in some data block.
840 * Just read that one in.
842 error = xfs_dir3_data_read(tp, dp,
843 xfs_dir2_db_to_da(args->geo, use_block),
846 xfs_trans_brelse(tp, lbp);
850 bf = dp->d_ops->data_bestfree_p(hdr);
854 * Point to the biggest freespace in our data block.
856 dup = (xfs_dir2_data_unused_t *)
857 ((char *)hdr + be16_to_cpu(bf[0].offset));
858 needscan = needlog = 0;
860 * Mark the initial part of our freespace in use for the new entry.
862 error = xfs_dir2_data_use_free(args, dbp, dup,
863 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
864 length, &needlog, &needscan);
866 xfs_trans_brelse(tp, lbp);
870 * Initialize our new entry (at last).
872 dep = (xfs_dir2_data_entry_t *)dup;
873 dep->inumber = cpu_to_be64(args->inumber);
874 dep->namelen = args->namelen;
875 memcpy(dep->name, args->name, dep->namelen);
876 dp->d_ops->data_put_ftype(dep, args->filetype);
877 tagp = dp->d_ops->data_entry_tag_p(dep);
878 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
880 * Need to scan fix up the bestfree table.
883 xfs_dir2_data_freescan(dp, hdr, &needlog);
885 * Need to log the data block's header.
888 xfs_dir2_data_log_header(args, dbp);
889 xfs_dir2_data_log_entry(args, dbp, dep);
891 * If the bests table needs to be changed, do it.
892 * Log the change unless we've already done that.
894 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
895 bestsp[use_block] = bf[0].length;
897 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
900 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
901 highstale, &lfloglow, &lfloghigh);
904 * Fill in the new leaf entry.
906 lep->hashval = cpu_to_be32(args->hashval);
907 lep->address = cpu_to_be32(
908 xfs_dir2_db_off_to_dataptr(args->geo, use_block,
909 be16_to_cpu(*tagp)));
911 * Log the leaf fields and give up the buffers.
913 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
914 xfs_dir3_leaf_log_header(args, lbp);
915 xfs_dir3_leaf_log_ents(args, lbp, lfloglow, lfloghigh);
916 xfs_dir3_leaf_check(dp, lbp);
917 xfs_dir3_data_check(dp, dbp);
922 * Compact out any stale entries in the leaf.
923 * Log the header and changed leaf entries, if any.
926 xfs_dir3_leaf_compact(
927 xfs_da_args_t *args, /* operation arguments */
928 struct xfs_dir3_icleaf_hdr *leafhdr,
929 struct xfs_buf *bp) /* leaf buffer */
931 int from; /* source leaf index */
932 xfs_dir2_leaf_t *leaf; /* leaf structure */
933 int loglow; /* first leaf entry to log */
934 int to; /* target leaf index */
935 struct xfs_dir2_leaf_entry *ents;
936 struct xfs_inode *dp = args->dp;
943 * Compress out the stale entries in place.
945 ents = dp->d_ops->leaf_ents_p(leaf);
946 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
947 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
950 * Only actually copy the entries that are different.
955 ents[to] = ents[from];
960 * Update and log the header, log the leaf entries.
962 ASSERT(leafhdr->stale == from - to);
963 leafhdr->count -= leafhdr->stale;
966 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
967 xfs_dir3_leaf_log_header(args, bp);
969 xfs_dir3_leaf_log_ents(args, bp, loglow, to - 1);
973 * Compact the leaf entries, removing stale ones.
974 * Leave one stale entry behind - the one closest to our
975 * insertion index - and the caller will shift that one to our insertion
977 * Return new insertion index, where the remaining stale entry is,
978 * and leaf logging indices.
981 xfs_dir3_leaf_compact_x1(
982 struct xfs_dir3_icleaf_hdr *leafhdr,
983 struct xfs_dir2_leaf_entry *ents,
984 int *indexp, /* insertion index */
985 int *lowstalep, /* out: stale entry before us */
986 int *highstalep, /* out: stale entry after us */
987 int *lowlogp, /* out: low log index */
988 int *highlogp) /* out: high log index */
990 int from; /* source copy index */
991 int highstale; /* stale entry at/after index */
992 int index; /* insertion index */
993 int keepstale; /* source index of kept stale */
994 int lowstale; /* stale entry before index */
995 int newindex=0; /* new insertion index */
996 int to; /* destination copy index */
998 ASSERT(leafhdr->stale > 1);
1001 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1004 * Pick the better of lowstale and highstale.
1006 if (lowstale >= 0 &&
1007 (highstale == leafhdr->count ||
1008 index - lowstale <= highstale - index))
1009 keepstale = lowstale;
1011 keepstale = highstale;
1013 * Copy the entries in place, removing all the stale entries
1016 for (from = to = 0; from < leafhdr->count; from++) {
1018 * Notice the new value of index.
1022 if (from != keepstale &&
1023 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1029 * Record the new keepstale value for the insertion.
1031 if (from == keepstale)
1032 lowstale = highstale = to;
1034 * Copy only the entries that have moved.
1037 ents[to] = ents[from];
1042 * If the insertion point was past the last entry,
1043 * set the new insertion point accordingly.
1049 * Adjust the leaf header values.
1051 leafhdr->count -= from - to;
1054 * Remember the low/high stale value only in the "right"
1057 if (lowstale >= newindex)
1060 highstale = leafhdr->count;
1061 *highlogp = leafhdr->count - 1;
1062 *lowstalep = lowstale;
1063 *highstalep = highstale;
1067 * Log the bests entries indicated from a leaf1 block.
1070 xfs_dir3_leaf_log_bests(
1071 struct xfs_da_args *args,
1072 struct xfs_buf *bp, /* leaf buffer */
1073 int first, /* first entry to log */
1074 int last) /* last entry to log */
1076 __be16 *firstb; /* pointer to first entry */
1077 __be16 *lastb; /* pointer to last entry */
1078 struct xfs_dir2_leaf *leaf = bp->b_addr;
1079 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1081 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1082 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1084 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1085 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1086 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1087 xfs_trans_log_buf(args->trans, bp,
1088 (uint)((char *)firstb - (char *)leaf),
1089 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1093 * Log the leaf entries indicated from a leaf1 or leafn block.
1096 xfs_dir3_leaf_log_ents(
1097 struct xfs_da_args *args,
1102 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1103 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1104 struct xfs_dir2_leaf *leaf = bp->b_addr;
1105 struct xfs_dir2_leaf_entry *ents;
1107 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1108 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1109 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1110 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1112 ents = args->dp->d_ops->leaf_ents_p(leaf);
1113 firstlep = &ents[first];
1114 lastlep = &ents[last];
1115 xfs_trans_log_buf(args->trans, bp,
1116 (uint)((char *)firstlep - (char *)leaf),
1117 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1121 * Log the header of the leaf1 or leafn block.
1124 xfs_dir3_leaf_log_header(
1125 struct xfs_da_args *args,
1128 struct xfs_dir2_leaf *leaf = bp->b_addr;
1130 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1131 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1132 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1133 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1135 xfs_trans_log_buf(args->trans, bp,
1136 (uint)((char *)&leaf->hdr - (char *)leaf),
1137 args->dp->d_ops->leaf_hdr_size - 1);
1141 * Log the tail of the leaf1 block.
1144 xfs_dir3_leaf_log_tail(
1145 struct xfs_da_args *args,
1148 struct xfs_dir2_leaf *leaf = bp->b_addr;
1149 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1151 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1152 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1153 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1154 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1156 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1157 xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1158 (uint)(args->geo->blksize - 1));
1162 * Look up the entry referred to by args in the leaf format directory.
1163 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1164 * is also used by the node-format code.
1167 xfs_dir2_leaf_lookup(
1168 xfs_da_args_t *args) /* operation arguments */
1170 struct xfs_buf *dbp; /* data block buffer */
1171 xfs_dir2_data_entry_t *dep; /* data block entry */
1172 xfs_inode_t *dp; /* incore directory inode */
1173 int error; /* error return code */
1174 int index; /* found entry index */
1175 struct xfs_buf *lbp; /* leaf buffer */
1176 xfs_dir2_leaf_t *leaf; /* leaf structure */
1177 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1178 xfs_trans_t *tp; /* transaction pointer */
1179 struct xfs_dir2_leaf_entry *ents;
1181 trace_xfs_dir2_leaf_lookup(args);
1184 * Look up name in the leaf block, returning both buffers and index.
1186 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1191 xfs_dir3_leaf_check(dp, lbp);
1193 ents = dp->d_ops->leaf_ents_p(leaf);
1195 * Get to the leaf entry and contained data entry address.
1200 * Point to the data entry.
1202 dep = (xfs_dir2_data_entry_t *)
1203 ((char *)dbp->b_addr +
1204 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1206 * Return the found inode number & CI name if appropriate
1208 args->inumber = be64_to_cpu(dep->inumber);
1209 args->filetype = dp->d_ops->data_get_ftype(dep);
1210 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1211 xfs_trans_brelse(tp, dbp);
1212 xfs_trans_brelse(tp, lbp);
1217 * Look up name/hash in the leaf block.
1218 * Fill in indexp with the found index, and dbpp with the data buffer.
1219 * If not found dbpp will be NULL, and ENOENT comes back.
1220 * lbpp will always be filled in with the leaf buffer unless there's an error.
1222 static int /* error */
1223 xfs_dir2_leaf_lookup_int(
1224 xfs_da_args_t *args, /* operation arguments */
1225 struct xfs_buf **lbpp, /* out: leaf buffer */
1226 int *indexp, /* out: index in leaf block */
1227 struct xfs_buf **dbpp) /* out: data buffer */
1229 xfs_dir2_db_t curdb = -1; /* current data block number */
1230 struct xfs_buf *dbp = NULL; /* data buffer */
1231 xfs_dir2_data_entry_t *dep; /* data entry */
1232 xfs_inode_t *dp; /* incore directory inode */
1233 int error; /* error return code */
1234 int index; /* index in leaf block */
1235 struct xfs_buf *lbp; /* leaf buffer */
1236 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1237 xfs_dir2_leaf_t *leaf; /* leaf structure */
1238 xfs_mount_t *mp; /* filesystem mount point */
1239 xfs_dir2_db_t newdb; /* new data block number */
1240 xfs_trans_t *tp; /* transaction pointer */
1241 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1242 enum xfs_dacmp cmp; /* name compare result */
1243 struct xfs_dir2_leaf_entry *ents;
1244 struct xfs_dir3_icleaf_hdr leafhdr;
1250 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
1256 xfs_dir3_leaf_check(dp, lbp);
1257 ents = dp->d_ops->leaf_ents_p(leaf);
1258 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
1261 * Look for the first leaf entry with our hash value.
1263 index = xfs_dir2_leaf_search_hash(args, lbp);
1265 * Loop over all the entries with the right hash value
1266 * looking to match the name.
1268 for (lep = &ents[index];
1269 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1272 * Skip over stale leaf entries.
1274 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1277 * Get the new data block number.
1279 newdb = xfs_dir2_dataptr_to_db(args->geo,
1280 be32_to_cpu(lep->address));
1282 * If it's not the same as the old data block number,
1283 * need to pitch the old one and read the new one.
1285 if (newdb != curdb) {
1287 xfs_trans_brelse(tp, dbp);
1288 error = xfs_dir3_data_read(tp, dp,
1289 xfs_dir2_db_to_da(args->geo, newdb),
1292 xfs_trans_brelse(tp, lbp);
1298 * Point to the data entry.
1300 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1301 xfs_dir2_dataptr_to_off(args->geo,
1302 be32_to_cpu(lep->address)));
1304 * Compare name and if it's an exact match, return the index
1305 * and buffer. If it's the first case-insensitive match, store
1306 * the index and buffer and continue looking for an exact match.
1308 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1309 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1310 args->cmpresult = cmp;
1312 /* case exact match: return the current buffer. */
1313 if (cmp == XFS_CMP_EXACT) {
1320 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1322 * Here, we can only be doing a lookup (not a rename or remove).
1323 * If a case-insensitive match was found earlier, re-read the
1324 * appropriate data block if required and return it.
1326 if (args->cmpresult == XFS_CMP_CASE) {
1328 if (cidb != curdb) {
1329 xfs_trans_brelse(tp, dbp);
1330 error = xfs_dir3_data_read(tp, dp,
1331 xfs_dir2_db_to_da(args->geo, cidb),
1334 xfs_trans_brelse(tp, lbp);
1342 * No match found, return -ENOENT.
1346 xfs_trans_brelse(tp, dbp);
1347 xfs_trans_brelse(tp, lbp);
1352 * Remove an entry from a leaf format directory.
1355 xfs_dir2_leaf_removename(
1356 xfs_da_args_t *args) /* operation arguments */
1358 __be16 *bestsp; /* leaf block best freespace */
1359 xfs_dir2_data_hdr_t *hdr; /* data block header */
1360 xfs_dir2_db_t db; /* data block number */
1361 struct xfs_buf *dbp; /* data block buffer */
1362 xfs_dir2_data_entry_t *dep; /* data entry structure */
1363 xfs_inode_t *dp; /* incore directory inode */
1364 int error; /* error return code */
1365 xfs_dir2_db_t i; /* temporary data block # */
1366 int index; /* index into leaf entries */
1367 struct xfs_buf *lbp; /* leaf buffer */
1368 xfs_dir2_leaf_t *leaf; /* leaf structure */
1369 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1370 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1371 int needlog; /* need to log data header */
1372 int needscan; /* need to rescan data frees */
1373 xfs_dir2_data_off_t oldbest; /* old value of best free */
1374 struct xfs_dir2_data_free *bf; /* bestfree table */
1375 struct xfs_dir2_leaf_entry *ents;
1376 struct xfs_dir3_icleaf_hdr leafhdr;
1378 trace_xfs_dir2_leaf_removename(args);
1381 * Lookup the leaf entry, get the leaf and data blocks read in.
1383 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1389 xfs_dir3_data_check(dp, dbp);
1390 bf = dp->d_ops->data_bestfree_p(hdr);
1391 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1392 ents = dp->d_ops->leaf_ents_p(leaf);
1394 * Point to the leaf entry, use that to point to the data entry.
1397 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1398 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1399 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1400 needscan = needlog = 0;
1401 oldbest = be16_to_cpu(bf[0].length);
1402 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1403 bestsp = xfs_dir2_leaf_bests_p(ltp);
1404 if (be16_to_cpu(bestsp[db]) != oldbest) {
1405 xfs_buf_corruption_error(lbp);
1406 return -EFSCORRUPTED;
1409 * Mark the former data entry unused.
1411 xfs_dir2_data_make_free(args, dbp,
1412 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1413 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1415 * We just mark the leaf entry stale by putting a null in it.
1418 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1419 xfs_dir3_leaf_log_header(args, lbp);
1421 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1422 xfs_dir3_leaf_log_ents(args, lbp, index, index);
1425 * Scan the freespace in the data block again if necessary,
1426 * log the data block header if necessary.
1429 xfs_dir2_data_freescan(dp, hdr, &needlog);
1431 xfs_dir2_data_log_header(args, dbp);
1433 * If the longest freespace in the data block has changed,
1434 * put the new value in the bests table and log that.
1436 if (be16_to_cpu(bf[0].length) != oldbest) {
1437 bestsp[db] = bf[0].length;
1438 xfs_dir3_leaf_log_bests(args, lbp, db, db);
1440 xfs_dir3_data_check(dp, dbp);
1442 * If the data block is now empty then get rid of the data block.
1444 if (be16_to_cpu(bf[0].length) ==
1445 args->geo->blksize - dp->d_ops->data_entry_offset) {
1446 ASSERT(db != args->geo->datablk);
1447 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1449 * Nope, can't get rid of it because it caused
1450 * allocation of a bmap btree block to do so.
1451 * Just go on, returning success, leaving the
1452 * empty block in place.
1454 if (error == -ENOSPC && args->total == 0)
1456 xfs_dir3_leaf_check(dp, lbp);
1461 * If this is the last data block then compact the
1462 * bests table by getting rid of entries.
1464 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1466 * Look for the last active entry (i).
1468 for (i = db - 1; i > 0; i--) {
1469 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1473 * Copy the table down so inactive entries at the
1476 memmove(&bestsp[db - i], bestsp,
1477 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1478 be32_add_cpu(<p->bestcount, -(db - i));
1479 xfs_dir3_leaf_log_tail(args, lbp);
1480 xfs_dir3_leaf_log_bests(args, lbp, 0,
1481 be32_to_cpu(ltp->bestcount) - 1);
1483 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1486 * If the data block was not the first one, drop it.
1488 else if (db != args->geo->datablk)
1491 xfs_dir3_leaf_check(dp, lbp);
1493 * See if we can convert to block form.
1495 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1499 * Replace the inode number in a leaf format directory entry.
1502 xfs_dir2_leaf_replace(
1503 xfs_da_args_t *args) /* operation arguments */
1505 struct xfs_buf *dbp; /* data block buffer */
1506 xfs_dir2_data_entry_t *dep; /* data block entry */
1507 xfs_inode_t *dp; /* incore directory inode */
1508 int error; /* error return code */
1509 int index; /* index of leaf entry */
1510 struct xfs_buf *lbp; /* leaf buffer */
1511 xfs_dir2_leaf_t *leaf; /* leaf structure */
1512 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1513 xfs_trans_t *tp; /* transaction pointer */
1514 struct xfs_dir2_leaf_entry *ents;
1516 trace_xfs_dir2_leaf_replace(args);
1519 * Look up the entry.
1521 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1526 ents = dp->d_ops->leaf_ents_p(leaf);
1528 * Point to the leaf entry, get data address from it.
1532 * Point to the data entry.
1534 dep = (xfs_dir2_data_entry_t *)
1535 ((char *)dbp->b_addr +
1536 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1537 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1539 * Put the new inode number in, log it.
1541 dep->inumber = cpu_to_be64(args->inumber);
1542 dp->d_ops->data_put_ftype(dep, args->filetype);
1544 xfs_dir2_data_log_entry(args, dbp, dep);
1545 xfs_dir3_leaf_check(dp, lbp);
1546 xfs_trans_brelse(tp, lbp);
1551 * Return index in the leaf block (lbp) which is either the first
1552 * one with this hash value, or if there are none, the insert point
1553 * for that hash value.
1555 int /* index value */
1556 xfs_dir2_leaf_search_hash(
1557 xfs_da_args_t *args, /* operation arguments */
1558 struct xfs_buf *lbp) /* leaf buffer */
1560 xfs_dahash_t hash=0; /* hash from this entry */
1561 xfs_dahash_t hashwant; /* hash value looking for */
1562 int high; /* high leaf index */
1563 int low; /* low leaf index */
1564 xfs_dir2_leaf_t *leaf; /* leaf structure */
1565 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1566 int mid=0; /* current leaf index */
1567 struct xfs_dir2_leaf_entry *ents;
1568 struct xfs_dir3_icleaf_hdr leafhdr;
1571 ents = args->dp->d_ops->leaf_ents_p(leaf);
1572 xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, leaf);
1575 * Note, the table cannot be empty, so we have to go through the loop.
1576 * Binary search the leaf entries looking for our hash value.
1578 for (lep = ents, low = 0, high = leafhdr.count - 1,
1579 hashwant = args->hashval;
1581 mid = (low + high) >> 1;
1582 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1584 if (hash < hashwant)
1590 * Found one, back up through all the equal hash values.
1592 if (hash == hashwant) {
1593 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1598 * Need to point to an entry higher than ours.
1600 else if (hash < hashwant)
1606 * Trim off a trailing data block. We know it's empty since the leaf
1607 * freespace table says so.
1610 xfs_dir2_leaf_trim_data(
1611 xfs_da_args_t *args, /* operation arguments */
1612 struct xfs_buf *lbp, /* leaf buffer */
1613 xfs_dir2_db_t db) /* data block number */
1615 __be16 *bestsp; /* leaf bests table */
1616 struct xfs_buf *dbp; /* data block buffer */
1617 xfs_inode_t *dp; /* incore directory inode */
1618 int error; /* error return value */
1619 xfs_dir2_leaf_t *leaf; /* leaf structure */
1620 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1621 xfs_trans_t *tp; /* transaction pointer */
1626 * Read the offending data block. We need its buffer.
1628 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(args->geo, db),
1634 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1638 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1639 struct xfs_dir2_data_free *bf = dp->d_ops->data_bestfree_p(hdr);
1641 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1642 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1643 ASSERT(be16_to_cpu(bf[0].length) ==
1644 args->geo->blksize - dp->d_ops->data_entry_offset);
1645 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1650 * Get rid of the data block.
1652 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1653 ASSERT(error != -ENOSPC);
1654 xfs_trans_brelse(tp, dbp);
1658 * Eliminate the last bests entry from the table.
1660 bestsp = xfs_dir2_leaf_bests_p(ltp);
1661 be32_add_cpu(<p->bestcount, -1);
1662 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1663 xfs_dir3_leaf_log_tail(args, lbp);
1664 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1668 static inline size_t
1670 struct xfs_dir3_icleaf_hdr *hdr,
1676 entries = hdr->count - hdr->stale;
1677 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1678 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1679 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1681 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1683 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1684 + counts * sizeof(xfs_dir2_data_off_t)
1685 + sizeof(xfs_dir2_leaf_tail_t);
1689 * Convert node form directory to leaf form directory.
1690 * The root of the node form dir needs to already be a LEAFN block.
1691 * Just return if we can't do anything.
1694 xfs_dir2_node_to_leaf(
1695 xfs_da_state_t *state) /* directory operation state */
1697 xfs_da_args_t *args; /* operation arguments */
1698 xfs_inode_t *dp; /* incore directory inode */
1699 int error; /* error return code */
1700 struct xfs_buf *fbp; /* buffer for freespace block */
1701 xfs_fileoff_t fo; /* freespace file offset */
1702 xfs_dir2_free_t *free; /* freespace structure */
1703 struct xfs_buf *lbp; /* buffer for leaf block */
1704 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1705 xfs_dir2_leaf_t *leaf; /* leaf structure */
1706 xfs_mount_t *mp; /* filesystem mount point */
1707 int rval; /* successful free trim? */
1708 xfs_trans_t *tp; /* transaction pointer */
1709 struct xfs_dir3_icleaf_hdr leafhdr;
1710 struct xfs_dir3_icfree_hdr freehdr;
1713 * There's more than a leaf level in the btree, so there must
1714 * be multiple leafn blocks. Give up.
1716 if (state->path.active > 1)
1720 trace_xfs_dir2_node_to_leaf(args);
1726 * Get the last offset in the file.
1728 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1731 fo -= args->geo->fsbcount;
1733 * If there are freespace blocks other than the first one,
1734 * take this opportunity to remove trailing empty freespace blocks
1735 * that may have been left behind during no-space-reservation
1738 while (fo > args->geo->freeblk) {
1739 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1743 fo -= args->geo->fsbcount;
1748 * Now find the block just before the freespace block.
1750 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1754 * If it's not the single leaf block, give up.
1756 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1758 lbp = state->path.blk[0].bp;
1760 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
1762 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1763 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1766 * Read the freespace block.
1768 error = xfs_dir2_free_read(tp, dp, args->geo->freeblk, &fbp);
1772 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1774 ASSERT(!freehdr.firstdb);
1777 * Now see if the leafn and free data will fit in a leaf1.
1778 * If not, release the buffer and give up.
1780 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1781 xfs_trans_brelse(tp, fbp);
1786 * If the leaf has any stale entries in it, compress them out.
1789 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1791 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1792 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1793 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1794 ? XFS_DIR2_LEAF1_MAGIC
1795 : XFS_DIR3_LEAF1_MAGIC;
1798 * Set up the leaf tail from the freespace block.
1800 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1801 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1804 * Set up the leaf bests table.
1806 memcpy(xfs_dir2_leaf_bests_p(ltp), dp->d_ops->free_bests_p(free),
1807 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1809 xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
1810 xfs_dir3_leaf_log_header(args, lbp);
1811 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1812 xfs_dir3_leaf_log_tail(args, lbp);
1813 xfs_dir3_leaf_check(dp, lbp);
1816 * Get rid of the freespace block.
1818 error = xfs_dir2_shrink_inode(args,
1819 xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1823 * This can't fail here because it can only happen when
1824 * punching out the middle of an extent, and this is an
1827 ASSERT(error != -ENOSPC);
1832 * Now see if we can convert the single-leaf directory
1833 * down to a block form directory.
1834 * This routine always kills the dabuf for the leaf, so
1835 * eliminate it from the path.
1837 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1838 state->path.blk[0].bp = NULL;