1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-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"
25 * Function declarations.
27 static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
29 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
30 xfs_da_state_blk_t *blk1,
31 xfs_da_state_blk_t *blk2);
32 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
33 int index, xfs_da_state_blk_t *dblk,
37 * Check internal consistency of a leafn block.
45 struct xfs_dir2_leaf *leaf = bp->b_addr;
46 struct xfs_dir3_icleaf_hdr leafhdr;
48 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
50 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
51 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
52 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
53 return __this_address;
54 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
55 return __this_address;
57 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
67 fa = xfs_dir3_leafn_check(dp, bp);
70 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
71 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
76 #define xfs_dir3_leaf_check(dp, bp)
83 struct xfs_mount *mp = bp->b_mount;
84 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
86 if (!xfs_verify_magic(bp, hdr->magic))
87 return __this_address;
89 if (xfs_sb_version_hascrc(&mp->m_sb)) {
90 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
92 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
93 return __this_address;
94 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
95 return __this_address;
96 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
97 return __this_address;
100 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
106 xfs_dir3_free_read_verify(
109 struct xfs_mount *mp = bp->b_mount;
112 if (xfs_sb_version_hascrc(&mp->m_sb) &&
113 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
114 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
116 fa = xfs_dir3_free_verify(bp);
118 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
123 xfs_dir3_free_write_verify(
126 struct xfs_mount *mp = bp->b_mount;
127 struct xfs_buf_log_item *bip = bp->b_log_item;
128 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
131 fa = xfs_dir3_free_verify(bp);
133 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
137 if (!xfs_sb_version_hascrc(&mp->m_sb))
141 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
143 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
146 const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
147 .name = "xfs_dir3_free",
148 .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
149 cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
150 .verify_read = xfs_dir3_free_read_verify,
151 .verify_write = xfs_dir3_free_write_verify,
152 .verify_struct = xfs_dir3_free_verify,
155 /* Everything ok in the free block header? */
156 static xfs_failaddr_t
157 xfs_dir3_free_header_check(
158 struct xfs_inode *dp,
162 struct xfs_mount *mp = dp->i_mount;
163 unsigned int firstdb;
166 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
167 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
168 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
170 if (xfs_sb_version_hascrc(&mp->m_sb)) {
171 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
173 if (be32_to_cpu(hdr3->firstdb) != firstdb)
174 return __this_address;
175 if (be32_to_cpu(hdr3->nvalid) > maxbests)
176 return __this_address;
177 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
178 return __this_address;
180 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
182 if (be32_to_cpu(hdr->firstdb) != firstdb)
183 return __this_address;
184 if (be32_to_cpu(hdr->nvalid) > maxbests)
185 return __this_address;
186 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
187 return __this_address;
193 __xfs_dir3_free_read(
194 struct xfs_trans *tp,
195 struct xfs_inode *dp,
197 xfs_daddr_t mappedbno,
198 struct xfs_buf **bpp)
203 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
204 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
208 /* Check things that we can't do in the verifier. */
209 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
211 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
212 xfs_trans_brelse(tp, *bpp);
213 return -EFSCORRUPTED;
216 /* try read returns without an error or *bpp if it lands in a hole */
218 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
225 struct xfs_trans *tp,
226 struct xfs_inode *dp,
228 struct xfs_buf **bpp)
230 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
234 xfs_dir2_free_try_read(
235 struct xfs_trans *tp,
236 struct xfs_inode *dp,
238 struct xfs_buf **bpp)
240 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
244 xfs_dir3_free_get_buf(
247 struct xfs_buf **bpp)
249 struct xfs_trans *tp = args->trans;
250 struct xfs_inode *dp = args->dp;
251 struct xfs_mount *mp = dp->i_mount;
254 struct xfs_dir3_icfree_hdr hdr;
256 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
257 -1, &bp, XFS_DATA_FORK);
261 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
262 bp->b_ops = &xfs_dir3_free_buf_ops;
265 * Initialize the new block to be empty, and remember
266 * its first slot as our empty slot.
268 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
269 memset(&hdr, 0, sizeof(hdr));
271 if (xfs_sb_version_hascrc(&mp->m_sb)) {
272 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
274 hdr.magic = XFS_DIR3_FREE_MAGIC;
276 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
277 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
278 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
280 hdr.magic = XFS_DIR2_FREE_MAGIC;
281 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
287 * Log entries from a freespace block.
290 xfs_dir2_free_log_bests(
291 struct xfs_da_args *args,
293 int first, /* first entry to log */
294 int last) /* last entry to log */
296 xfs_dir2_free_t *free; /* freespace structure */
300 bests = args->dp->d_ops->free_bests_p(free);
301 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
302 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
303 xfs_trans_log_buf(args->trans, bp,
304 (uint)((char *)&bests[first] - (char *)free),
305 (uint)((char *)&bests[last] - (char *)free +
306 sizeof(bests[0]) - 1));
310 * Log header from a freespace block.
313 xfs_dir2_free_log_header(
314 struct xfs_da_args *args,
318 xfs_dir2_free_t *free; /* freespace structure */
321 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
322 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
324 xfs_trans_log_buf(args->trans, bp, 0,
325 args->dp->d_ops->free_hdr_size - 1);
329 * Convert a leaf-format directory to a node-format directory.
330 * We need to change the magic number of the leaf block, and copy
331 * the freespace table out of the leaf block into its own block.
334 xfs_dir2_leaf_to_node(
335 xfs_da_args_t *args, /* operation arguments */
336 struct xfs_buf *lbp) /* leaf buffer */
338 xfs_inode_t *dp; /* incore directory inode */
339 int error; /* error return value */
340 struct xfs_buf *fbp; /* freespace buffer */
341 xfs_dir2_db_t fdb; /* freespace block number */
342 xfs_dir2_free_t *free; /* freespace structure */
343 __be16 *from; /* pointer to freespace entry */
344 int i; /* leaf freespace index */
345 xfs_dir2_leaf_t *leaf; /* leaf structure */
346 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
347 int n; /* count of live freespc ents */
348 xfs_dir2_data_off_t off; /* freespace entry value */
349 __be16 *to; /* pointer to freespace entry */
350 xfs_trans_t *tp; /* transaction pointer */
351 struct xfs_dir3_icfree_hdr freehdr;
353 trace_xfs_dir2_leaf_to_node(args);
358 * Add a freespace block to the directory.
360 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
363 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
365 * Get the buffer for the new freespace block.
367 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
372 dp->d_ops->free_hdr_from_disk(&freehdr, free);
374 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
375 if (be32_to_cpu(ltp->bestcount) >
376 (uint)dp->i_d.di_size / args->geo->blksize)
377 return -EFSCORRUPTED;
380 * Copy freespace entries from the leaf block to the new block.
381 * Count active entries.
383 from = xfs_dir2_leaf_bests_p(ltp);
384 to = dp->d_ops->free_bests_p(free);
385 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
386 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
388 *to = cpu_to_be16(off);
392 * Now initialize the freespace block header.
395 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
397 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
398 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
399 xfs_dir2_free_log_header(args, fbp);
402 * Converting the leaf to a leafnode is just a matter of changing the
403 * magic number and the ops. Do the change directly to the buffer as
404 * it's less work (and less code) than decoding the header to host
405 * format and back again.
407 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
408 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
410 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
411 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
412 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
413 xfs_dir3_leaf_log_header(args, lbp);
414 xfs_dir3_leaf_check(dp, lbp);
419 * Add a leaf entry to a leaf block in a node-form directory.
420 * The other work necessary is done from the caller.
422 static int /* error */
424 struct xfs_buf *bp, /* leaf buffer */
425 struct xfs_da_args *args, /* operation arguments */
426 int index) /* insertion pt for new entry */
428 struct xfs_dir3_icleaf_hdr leafhdr;
429 struct xfs_inode *dp = args->dp;
430 struct xfs_dir2_leaf *leaf = bp->b_addr;
431 struct xfs_dir2_leaf_entry *lep;
432 struct xfs_dir2_leaf_entry *ents;
433 int compact; /* compacting stale leaves */
434 int highstale = 0; /* next stale entry */
435 int lfloghigh; /* high leaf entry logging */
436 int lfloglow; /* low leaf entry logging */
437 int lowstale = 0; /* previous stale entry */
439 trace_xfs_dir2_leafn_add(args, index);
441 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
442 ents = dp->d_ops->leaf_ents_p(leaf);
445 * Quick check just to make sure we are not going to index
446 * into other peoples memory
449 return -EFSCORRUPTED;
452 * If there are already the maximum number of leaf entries in
453 * the block, if there are no stale entries it won't fit.
454 * Caller will do a split. If there are stale entries we'll do
458 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
461 compact = leafhdr.stale > 1;
464 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
465 ASSERT(index == leafhdr.count ||
466 be32_to_cpu(ents[index].hashval) >= args->hashval);
468 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
472 * Compact out all but one stale leaf entry. Leaves behind
473 * the entry closest to index.
476 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
477 &highstale, &lfloglow, &lfloghigh);
478 else if (leafhdr.stale) {
480 * Set impossible logging indices for this case.
482 lfloglow = leafhdr.count;
487 * Insert the new entry, log everything.
489 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
490 highstale, &lfloglow, &lfloghigh);
492 lep->hashval = cpu_to_be32(args->hashval);
493 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
494 args->blkno, args->index));
496 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
497 xfs_dir3_leaf_log_header(args, bp);
498 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
499 xfs_dir3_leaf_check(dp, bp);
505 xfs_dir2_free_hdr_check(
506 struct xfs_inode *dp,
510 struct xfs_dir3_icfree_hdr hdr;
512 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
514 ASSERT((hdr.firstdb %
515 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
516 ASSERT(hdr.firstdb <= db);
517 ASSERT(db < hdr.firstdb + hdr.nvalid);
520 #define xfs_dir2_free_hdr_check(dp, bp, db)
524 * Return the last hash value in the leaf.
525 * Stale entries are ok.
527 xfs_dahash_t /* hash value */
528 xfs_dir2_leaf_lasthash(
529 struct xfs_inode *dp,
530 struct xfs_buf *bp, /* leaf buffer */
531 int *count) /* count of entries in leaf */
533 struct xfs_dir2_leaf *leaf = bp->b_addr;
534 struct xfs_dir2_leaf_entry *ents;
535 struct xfs_dir3_icleaf_hdr leafhdr;
537 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
539 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
540 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
541 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
542 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
545 *count = leafhdr.count;
549 ents = dp->d_ops->leaf_ents_p(leaf);
550 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
554 * Look up a leaf entry for space to add a name in a node-format leaf block.
555 * The extrablk in state is a freespace block.
558 xfs_dir2_leafn_lookup_for_addname(
559 struct xfs_buf *bp, /* leaf buffer */
560 xfs_da_args_t *args, /* operation arguments */
561 int *indexp, /* out: leaf entry index */
562 xfs_da_state_t *state) /* state to fill in */
564 struct xfs_buf *curbp = NULL; /* current data/free buffer */
565 xfs_dir2_db_t curdb = -1; /* current data block number */
566 xfs_dir2_db_t curfdb = -1; /* current free block number */
567 xfs_inode_t *dp; /* incore directory inode */
568 int error; /* error return value */
569 int fi; /* free entry index */
570 xfs_dir2_free_t *free = NULL; /* free block structure */
571 int index; /* leaf entry index */
572 xfs_dir2_leaf_t *leaf; /* leaf structure */
573 int length; /* length of new data entry */
574 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
575 xfs_mount_t *mp; /* filesystem mount point */
576 xfs_dir2_db_t newdb; /* new data block number */
577 xfs_dir2_db_t newfdb; /* new free block number */
578 xfs_trans_t *tp; /* transaction pointer */
579 struct xfs_dir2_leaf_entry *ents;
580 struct xfs_dir3_icleaf_hdr leafhdr;
586 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
587 ents = dp->d_ops->leaf_ents_p(leaf);
589 xfs_dir3_leaf_check(dp, bp);
590 ASSERT(leafhdr.count > 0);
593 * Look up the hash value in the leaf entries.
595 index = xfs_dir2_leaf_search_hash(args, bp);
597 * Do we have a buffer coming in?
599 if (state->extravalid) {
600 /* If so, it's a free block buffer, get the block number. */
601 curbp = state->extrablk.bp;
602 curfdb = state->extrablk.blkno;
603 free = curbp->b_addr;
604 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
605 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
607 length = dp->d_ops->data_entsize(args->namelen);
609 * Loop over leaf entries with the right hash value.
611 for (lep = &ents[index];
612 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
615 * Skip stale leaf entries.
617 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
620 * Pull the data block number from the entry.
622 newdb = xfs_dir2_dataptr_to_db(args->geo,
623 be32_to_cpu(lep->address));
625 * For addname, we're looking for a place to put the new entry.
626 * We want to use a data block with an entry of equal
627 * hash value to ours if there is one with room.
629 * If this block isn't the data block we already have
630 * in hand, take a look at it.
632 if (newdb != curdb) {
637 * Convert the data block to the free block
638 * holding its freespace information.
640 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
642 * If it's not the one we have in hand, read it in.
644 if (newfdb != curfdb) {
646 * If we had one before, drop it.
649 xfs_trans_brelse(tp, curbp);
651 error = xfs_dir2_free_read(tp, dp,
652 xfs_dir2_db_to_da(args->geo,
657 free = curbp->b_addr;
659 xfs_dir2_free_hdr_check(dp, curbp, curdb);
662 * Get the index for our entry.
664 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
666 * If it has room, return it.
668 bests = dp->d_ops->free_bests_p(free);
669 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
670 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
671 XFS_ERRLEVEL_LOW, mp);
672 if (curfdb != newfdb)
673 xfs_trans_brelse(tp, curbp);
674 return -EFSCORRUPTED;
677 if (be16_to_cpu(bests[fi]) >= length)
681 /* Didn't find any space */
684 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
686 /* Giving back a free block. */
687 state->extravalid = 1;
688 state->extrablk.bp = curbp;
689 state->extrablk.index = fi;
690 state->extrablk.blkno = curfdb;
693 * Important: this magic number is not in the buffer - it's for
694 * buffer type information and therefore only the free/data type
695 * matters here, not whether CRCs are enabled or not.
697 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
699 state->extravalid = 0;
702 * Return the index, that will be the insertion point.
709 * Look up a leaf entry in a node-format leaf block.
710 * The extrablk in state a data block.
713 xfs_dir2_leafn_lookup_for_entry(
714 struct xfs_buf *bp, /* leaf buffer */
715 xfs_da_args_t *args, /* operation arguments */
716 int *indexp, /* out: leaf entry index */
717 xfs_da_state_t *state) /* state to fill in */
719 struct xfs_buf *curbp = NULL; /* current data/free buffer */
720 xfs_dir2_db_t curdb = -1; /* current data block number */
721 xfs_dir2_data_entry_t *dep; /* data block entry */
722 xfs_inode_t *dp; /* incore directory inode */
723 int error; /* error return value */
724 int index; /* leaf entry index */
725 xfs_dir2_leaf_t *leaf; /* leaf structure */
726 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
727 xfs_mount_t *mp; /* filesystem mount point */
728 xfs_dir2_db_t newdb; /* new data block number */
729 xfs_trans_t *tp; /* transaction pointer */
730 enum xfs_dacmp cmp; /* comparison result */
731 struct xfs_dir2_leaf_entry *ents;
732 struct xfs_dir3_icleaf_hdr leafhdr;
738 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
739 ents = dp->d_ops->leaf_ents_p(leaf);
741 xfs_dir3_leaf_check(dp, bp);
742 if (leafhdr.count <= 0)
743 return -EFSCORRUPTED;
746 * Look up the hash value in the leaf entries.
748 index = xfs_dir2_leaf_search_hash(args, bp);
750 * Do we have a buffer coming in?
752 if (state->extravalid) {
753 curbp = state->extrablk.bp;
754 curdb = state->extrablk.blkno;
757 * Loop over leaf entries with the right hash value.
759 for (lep = &ents[index];
760 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
763 * Skip stale leaf entries.
765 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
768 * Pull the data block number from the entry.
770 newdb = xfs_dir2_dataptr_to_db(args->geo,
771 be32_to_cpu(lep->address));
773 * Not adding a new entry, so we really want to find
774 * the name given to us.
776 * If it's a different data block, go get it.
778 if (newdb != curdb) {
780 * If we had a block before that we aren't saving
781 * for a CI name, drop it
783 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
784 curdb != state->extrablk.blkno))
785 xfs_trans_brelse(tp, curbp);
787 * If needing the block that is saved with a CI match,
788 * use it otherwise read in the new data block.
790 if (args->cmpresult != XFS_CMP_DIFFERENT &&
791 newdb == state->extrablk.blkno) {
792 ASSERT(state->extravalid);
793 curbp = state->extrablk.bp;
795 error = xfs_dir3_data_read(tp, dp,
796 xfs_dir2_db_to_da(args->geo,
802 xfs_dir3_data_check(dp, curbp);
806 * Point to the data entry.
808 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
809 xfs_dir2_dataptr_to_off(args->geo,
810 be32_to_cpu(lep->address)));
812 * Compare the entry and if it's an exact match, return
813 * EEXIST immediately. If it's the first case-insensitive
814 * match, store the block & inode number and continue looking.
816 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
817 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
818 /* If there is a CI match block, drop it */
819 if (args->cmpresult != XFS_CMP_DIFFERENT &&
820 curdb != state->extrablk.blkno)
821 xfs_trans_brelse(tp, state->extrablk.bp);
822 args->cmpresult = cmp;
823 args->inumber = be64_to_cpu(dep->inumber);
824 args->filetype = dp->d_ops->data_get_ftype(dep);
826 state->extravalid = 1;
827 state->extrablk.bp = curbp;
828 state->extrablk.blkno = curdb;
829 state->extrablk.index = (int)((char *)dep -
830 (char *)curbp->b_addr);
831 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
832 curbp->b_ops = &xfs_dir3_data_buf_ops;
833 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
834 if (cmp == XFS_CMP_EXACT)
838 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
840 if (args->cmpresult == XFS_CMP_DIFFERENT) {
841 /* Giving back last used data block. */
842 state->extravalid = 1;
843 state->extrablk.bp = curbp;
844 state->extrablk.index = -1;
845 state->extrablk.blkno = curdb;
846 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
847 curbp->b_ops = &xfs_dir3_data_buf_ops;
848 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
850 /* If the curbp is not the CI match block, drop it */
851 if (state->extrablk.bp != curbp)
852 xfs_trans_brelse(tp, curbp);
855 state->extravalid = 0;
862 * Look up a leaf entry in a node-format leaf block.
863 * If this is an addname then the extrablk in state is a freespace block,
864 * otherwise it's a data block.
867 xfs_dir2_leafn_lookup_int(
868 struct xfs_buf *bp, /* leaf buffer */
869 xfs_da_args_t *args, /* operation arguments */
870 int *indexp, /* out: leaf entry index */
871 xfs_da_state_t *state) /* state to fill in */
873 if (args->op_flags & XFS_DA_OP_ADDNAME)
874 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
876 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
880 * Move count leaf entries from source to destination leaf.
881 * Log entries and headers. Stale entries are preserved.
884 xfs_dir3_leafn_moveents(
885 xfs_da_args_t *args, /* operation arguments */
886 struct xfs_buf *bp_s, /* source */
887 struct xfs_dir3_icleaf_hdr *shdr,
888 struct xfs_dir2_leaf_entry *sents,
889 int start_s,/* source leaf index */
890 struct xfs_buf *bp_d, /* destination */
891 struct xfs_dir3_icleaf_hdr *dhdr,
892 struct xfs_dir2_leaf_entry *dents,
893 int start_d,/* destination leaf index */
894 int count) /* count of leaves to copy */
896 int stale; /* count stale leaves copied */
898 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
901 * Silently return if nothing to do.
907 * If the destination index is not the end of the current
908 * destination leaf entries, open up a hole in the destination
909 * to hold the new entries.
911 if (start_d < dhdr->count) {
912 memmove(&dents[start_d + count], &dents[start_d],
913 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
914 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
915 count + dhdr->count - 1);
918 * If the source has stale leaves, count the ones in the copy range
919 * so we can update the header correctly.
922 int i; /* temp leaf index */
924 for (i = start_s, stale = 0; i < start_s + count; i++) {
925 if (sents[i].address ==
926 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
932 * Copy the leaf entries from source to destination.
934 memcpy(&dents[start_d], &sents[start_s],
935 count * sizeof(xfs_dir2_leaf_entry_t));
936 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
939 * If there are source entries after the ones we copied,
940 * delete the ones we copied by sliding the next ones down.
942 if (start_s + count < shdr->count) {
943 memmove(&sents[start_s], &sents[start_s + count],
944 count * sizeof(xfs_dir2_leaf_entry_t));
945 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
949 * Update the headers and log them.
951 shdr->count -= count;
952 shdr->stale -= stale;
953 dhdr->count += count;
954 dhdr->stale += stale;
958 * Determine the sort order of two leaf blocks.
959 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
962 xfs_dir2_leafn_order(
963 struct xfs_inode *dp,
964 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
965 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
967 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
968 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
969 struct xfs_dir2_leaf_entry *ents1;
970 struct xfs_dir2_leaf_entry *ents2;
971 struct xfs_dir3_icleaf_hdr hdr1;
972 struct xfs_dir3_icleaf_hdr hdr2;
974 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
975 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
976 ents1 = dp->d_ops->leaf_ents_p(leaf1);
977 ents2 = dp->d_ops->leaf_ents_p(leaf2);
979 if (hdr1.count > 0 && hdr2.count > 0 &&
980 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
981 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
982 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
988 * Rebalance leaf entries between two leaf blocks.
989 * This is actually only called when the second block is new,
990 * though the code deals with the general case.
991 * A new entry will be inserted in one of the blocks, and that
992 * entry is taken into account when balancing.
995 xfs_dir2_leafn_rebalance(
996 xfs_da_state_t *state, /* btree cursor */
997 xfs_da_state_blk_t *blk1, /* first btree block */
998 xfs_da_state_blk_t *blk2) /* second btree block */
1000 xfs_da_args_t *args; /* operation arguments */
1001 int count; /* count (& direction) leaves */
1002 int isleft; /* new goes in left leaf */
1003 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1004 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1005 int mid; /* midpoint leaf index */
1006 #if defined(DEBUG) || defined(XFS_WARN)
1007 int oldstale; /* old count of stale leaves */
1009 int oldsum; /* old total leaf count */
1010 int swap_blocks; /* swapped leaf blocks */
1011 struct xfs_dir2_leaf_entry *ents1;
1012 struct xfs_dir2_leaf_entry *ents2;
1013 struct xfs_dir3_icleaf_hdr hdr1;
1014 struct xfs_dir3_icleaf_hdr hdr2;
1015 struct xfs_inode *dp = state->args->dp;
1019 * If the block order is wrong, swap the arguments.
1021 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1025 leaf1 = blk1->bp->b_addr;
1026 leaf2 = blk2->bp->b_addr;
1027 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1028 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
1029 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1030 ents2 = dp->d_ops->leaf_ents_p(leaf2);
1032 oldsum = hdr1.count + hdr2.count;
1033 #if defined(DEBUG) || defined(XFS_WARN)
1034 oldstale = hdr1.stale + hdr2.stale;
1039 * If the old leaf count was odd then the new one will be even,
1040 * so we need to divide the new count evenly.
1043 xfs_dahash_t midhash; /* middle entry hash value */
1045 if (mid >= hdr1.count)
1046 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
1048 midhash = be32_to_cpu(ents1[mid].hashval);
1049 isleft = args->hashval <= midhash;
1052 * If the old count is even then the new count is odd, so there's
1053 * no preferred side for the new entry.
1054 * Pick the left one.
1059 * Calculate moved entry count. Positive means left-to-right,
1060 * negative means right-to-left. Then move the entries.
1062 count = hdr1.count - mid + (isleft == 0);
1064 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1065 hdr1.count - count, blk2->bp,
1066 &hdr2, ents2, 0, count);
1068 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1069 blk1->bp, &hdr1, ents1,
1072 ASSERT(hdr1.count + hdr2.count == oldsum);
1073 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1075 /* log the changes made when moving the entries */
1076 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1077 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
1078 xfs_dir3_leaf_log_header(args, blk1->bp);
1079 xfs_dir3_leaf_log_header(args, blk2->bp);
1081 xfs_dir3_leaf_check(dp, blk1->bp);
1082 xfs_dir3_leaf_check(dp, blk2->bp);
1085 * Mark whether we're inserting into the old or new leaf.
1087 if (hdr1.count < hdr2.count)
1088 state->inleaf = swap_blocks;
1089 else if (hdr1.count > hdr2.count)
1090 state->inleaf = !swap_blocks;
1092 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
1094 * Adjust the expected index for insertion.
1097 blk2->index = blk1->index - hdr1.count;
1100 * Finally sanity check just to make sure we are not returning a
1103 if (blk2->index < 0) {
1106 xfs_alert(dp->i_mount,
1107 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1108 __func__, blk1->index);
1113 xfs_dir3_data_block_free(
1114 xfs_da_args_t *args,
1115 struct xfs_dir2_data_hdr *hdr,
1116 struct xfs_dir2_free *free,
1119 struct xfs_buf *fbp,
1124 struct xfs_dir3_icfree_hdr freehdr;
1125 struct xfs_inode *dp = args->dp;
1127 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1128 bests = dp->d_ops->free_bests_p(free);
1131 * Data block is not empty, just set the free entry to the new
1134 bests[findex] = cpu_to_be16(longest);
1135 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1139 /* One less used entry in the free table. */
1143 * If this was the last entry in the table, we can trim the table size
1144 * back. There might be other entries at the end referring to
1145 * non-existent data blocks, get those too.
1147 if (findex == freehdr.nvalid - 1) {
1148 int i; /* free entry index */
1150 for (i = findex - 1; i >= 0; i--) {
1151 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1154 freehdr.nvalid = i + 1;
1157 /* Not the last entry, just punch it out. */
1158 bests[findex] = cpu_to_be16(NULLDATAOFF);
1162 dp->d_ops->free_hdr_to_disk(free, &freehdr);
1163 xfs_dir2_free_log_header(args, fbp);
1166 * If there are no useful entries left in the block, get rid of the
1169 if (!freehdr.nused) {
1172 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1176 } else if (error != -ENOSPC || args->total != 0)
1179 * It's possible to get ENOSPC if there is no
1180 * space reservation. In this case some one
1181 * else will eventually get rid of this block.
1185 /* Log the free entry that changed, unless we got rid of it. */
1187 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1192 * Remove an entry from a node directory.
1193 * This removes the leaf entry and the data entry,
1194 * and updates the free block if necessary.
1196 static int /* error */
1197 xfs_dir2_leafn_remove(
1198 xfs_da_args_t *args, /* operation arguments */
1199 struct xfs_buf *bp, /* leaf buffer */
1200 int index, /* leaf entry index */
1201 xfs_da_state_blk_t *dblk, /* data block */
1202 int *rval) /* resulting block needs join */
1204 xfs_dir2_data_hdr_t *hdr; /* data block header */
1205 xfs_dir2_db_t db; /* data block number */
1206 struct xfs_buf *dbp; /* data block buffer */
1207 xfs_dir2_data_entry_t *dep; /* data block entry */
1208 xfs_inode_t *dp; /* incore directory inode */
1209 xfs_dir2_leaf_t *leaf; /* leaf structure */
1210 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1211 int longest; /* longest data free entry */
1212 int off; /* data block entry offset */
1213 int needlog; /* need to log data header */
1214 int needscan; /* need to rescan data frees */
1215 xfs_trans_t *tp; /* transaction pointer */
1216 struct xfs_dir2_data_free *bf; /* bestfree table */
1217 struct xfs_dir3_icleaf_hdr leafhdr;
1218 struct xfs_dir2_leaf_entry *ents;
1220 trace_xfs_dir2_leafn_remove(args, index);
1225 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1226 ents = dp->d_ops->leaf_ents_p(leaf);
1229 * Point to the entry we're removing.
1234 * Extract the data block and offset from the entry.
1236 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1237 ASSERT(dblk->blkno == db);
1238 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
1239 ASSERT(dblk->index == off);
1242 * Kill the leaf entry by marking it stale.
1243 * Log the leaf block changes.
1246 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1247 xfs_dir3_leaf_log_header(args, bp);
1249 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1250 xfs_dir3_leaf_log_ents(args, bp, index, index);
1253 * Make the data entry free. Keep track of the longest freespace
1254 * in the data block in case it changes.
1258 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1259 bf = dp->d_ops->data_bestfree_p(hdr);
1260 longest = be16_to_cpu(bf[0].length);
1261 needlog = needscan = 0;
1262 xfs_dir2_data_make_free(args, dbp, off,
1263 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1265 * Rescan the data block freespaces for bestfree.
1266 * Log the data block header if needed.
1269 xfs_dir2_data_freescan(dp, hdr, &needlog);
1271 xfs_dir2_data_log_header(args, dbp);
1272 xfs_dir3_data_check(dp, dbp);
1274 * If the longest data block freespace changes, need to update
1275 * the corresponding freeblock entry.
1277 if (longest < be16_to_cpu(bf[0].length)) {
1278 int error; /* error return value */
1279 struct xfs_buf *fbp; /* freeblock buffer */
1280 xfs_dir2_db_t fdb; /* freeblock block number */
1281 int findex; /* index in freeblock entries */
1282 xfs_dir2_free_t *free; /* freeblock structure */
1285 * Convert the data block number to a free block,
1286 * read in the free block.
1288 fdb = dp->d_ops->db_to_fdb(args->geo, db);
1289 error = xfs_dir2_free_read(tp, dp,
1290 xfs_dir2_db_to_da(args->geo, fdb),
1297 struct xfs_dir3_icfree_hdr freehdr;
1298 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1299 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
1300 (fdb - xfs_dir2_byte_to_db(args->geo,
1301 XFS_DIR2_FREE_OFFSET)));
1305 * Calculate which entry we need to fix.
1307 findex = dp->d_ops->db_to_fdindex(args->geo, db);
1308 longest = be16_to_cpu(bf[0].length);
1310 * If the data block is now empty we can get rid of it
1313 if (longest == args->geo->blksize -
1314 dp->d_ops->data_entry_offset) {
1316 * Try to punch out the data block.
1318 error = xfs_dir2_shrink_inode(args, db, dbp);
1324 * We can get ENOSPC if there's no space reservation.
1325 * In this case just drop the buffer and some one else
1326 * will eventually get rid of the empty block.
1328 else if (!(error == -ENOSPC && args->total == 0))
1332 * If we got rid of the data block, we can eliminate that entry
1333 * in the free block.
1335 error = xfs_dir3_data_block_free(args, hdr, free,
1336 fdb, findex, fbp, longest);
1341 xfs_dir3_leaf_check(dp, bp);
1343 * Return indication of whether this leaf block is empty enough
1344 * to justify trying to join it with a neighbor.
1346 *rval = (dp->d_ops->leaf_hdr_size +
1347 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
1348 args->geo->magicpct;
1353 * Split the leaf entries in the old block into old and new blocks.
1356 xfs_dir2_leafn_split(
1357 xfs_da_state_t *state, /* btree cursor */
1358 xfs_da_state_blk_t *oldblk, /* original block */
1359 xfs_da_state_blk_t *newblk) /* newly created block */
1361 xfs_da_args_t *args; /* operation arguments */
1362 xfs_dablk_t blkno; /* new leaf block number */
1363 int error; /* error return value */
1364 struct xfs_inode *dp;
1367 * Allocate space for a new leaf node.
1371 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1372 error = xfs_da_grow_inode(args, &blkno);
1377 * Initialize the new leaf block.
1379 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1380 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1384 newblk->blkno = blkno;
1385 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1387 * Rebalance the entries across the two leaves, link the new
1388 * block into the leaves.
1390 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1391 error = xfs_da3_blk_link(state, oldblk, newblk);
1396 * Insert the new entry in the correct block.
1399 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1401 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1403 * Update last hashval in each block since we added the name.
1405 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1406 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
1407 xfs_dir3_leaf_check(dp, oldblk->bp);
1408 xfs_dir3_leaf_check(dp, newblk->bp);
1413 * Check a leaf block and its neighbors to see if the block should be
1414 * collapsed into one or the other neighbor. Always keep the block
1415 * with the smaller block number.
1416 * If the current block is over 50% full, don't try to join it, return 0.
1417 * If the block is empty, fill in the state structure and return 2.
1418 * If it can be collapsed, fill in the state structure and return 1.
1419 * If nothing can be done, return 0.
1422 xfs_dir2_leafn_toosmall(
1423 xfs_da_state_t *state, /* btree cursor */
1424 int *action) /* resulting action to take */
1426 xfs_da_state_blk_t *blk; /* leaf block */
1427 xfs_dablk_t blkno; /* leaf block number */
1428 struct xfs_buf *bp; /* leaf buffer */
1429 int bytes; /* bytes in use */
1430 int count; /* leaf live entry count */
1431 int error; /* error return value */
1432 int forward; /* sibling block direction */
1433 int i; /* sibling counter */
1434 xfs_dir2_leaf_t *leaf; /* leaf structure */
1435 int rval; /* result from path_shift */
1436 struct xfs_dir3_icleaf_hdr leafhdr;
1437 struct xfs_dir2_leaf_entry *ents;
1438 struct xfs_inode *dp = state->args->dp;
1441 * Check for the degenerate case of the block being over 50% full.
1442 * If so, it's not worth even looking to see if we might be able
1443 * to coalesce with a sibling.
1445 blk = &state->path.blk[state->path.active - 1];
1446 leaf = blk->bp->b_addr;
1447 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1448 ents = dp->d_ops->leaf_ents_p(leaf);
1449 xfs_dir3_leaf_check(dp, blk->bp);
1451 count = leafhdr.count - leafhdr.stale;
1452 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
1453 if (bytes > (state->args->geo->blksize >> 1)) {
1455 * Blk over 50%, don't try to join.
1461 * Check for the degenerate case of the block being empty.
1462 * If the block is empty, we'll simply delete it, no need to
1463 * coalesce it with a sibling block. We choose (arbitrarily)
1464 * to merge with the forward block unless it is NULL.
1468 * Make altpath point to the block we want to keep and
1469 * path point to the block we want to drop (this one).
1471 forward = (leafhdr.forw != 0);
1472 memcpy(&state->altpath, &state->path, sizeof(state->path));
1473 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1477 *action = rval ? 2 : 0;
1481 * Examine each sibling block to see if we can coalesce with
1482 * at least 25% free space to spare. We need to figure out
1483 * whether to merge with the forward or the backward block.
1484 * We prefer coalescing with the lower numbered sibling so as
1485 * to shrink a directory over time.
1487 forward = leafhdr.forw < leafhdr.back;
1488 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1489 struct xfs_dir3_icleaf_hdr hdr2;
1491 blkno = forward ? leafhdr.forw : leafhdr.back;
1495 * Read the sibling leaf block.
1497 error = xfs_dir3_leafn_read(state->args->trans, dp,
1503 * Count bytes in the two blocks combined.
1505 count = leafhdr.count - leafhdr.stale;
1506 bytes = state->args->geo->blksize -
1507 (state->args->geo->blksize >> 2);
1510 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
1511 ents = dp->d_ops->leaf_ents_p(leaf);
1512 count += hdr2.count - hdr2.stale;
1513 bytes -= count * sizeof(ents[0]);
1516 * Fits with at least 25% to spare.
1520 xfs_trans_brelse(state->args->trans, bp);
1523 * Didn't like either block, give up.
1531 * Make altpath point to the block we want to keep (the lower
1532 * numbered block) and path point to the block we want to drop.
1534 memcpy(&state->altpath, &state->path, sizeof(state->path));
1535 if (blkno < blk->blkno)
1536 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1539 error = xfs_da3_path_shift(state, &state->path, forward, 0,
1544 *action = rval ? 0 : 1;
1549 * Move all the leaf entries from drop_blk to save_blk.
1550 * This is done as part of a join operation.
1553 xfs_dir2_leafn_unbalance(
1554 xfs_da_state_t *state, /* cursor */
1555 xfs_da_state_blk_t *drop_blk, /* dead block */
1556 xfs_da_state_blk_t *save_blk) /* surviving block */
1558 xfs_da_args_t *args; /* operation arguments */
1559 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1560 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1561 struct xfs_dir3_icleaf_hdr savehdr;
1562 struct xfs_dir3_icleaf_hdr drophdr;
1563 struct xfs_dir2_leaf_entry *sents;
1564 struct xfs_dir2_leaf_entry *dents;
1565 struct xfs_inode *dp = state->args->dp;
1568 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1569 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1570 drop_leaf = drop_blk->bp->b_addr;
1571 save_leaf = save_blk->bp->b_addr;
1573 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1574 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1575 sents = dp->d_ops->leaf_ents_p(save_leaf);
1576 dents = dp->d_ops->leaf_ents_p(drop_leaf);
1579 * If there are any stale leaf entries, take this opportunity
1583 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1585 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1588 * Move the entries from drop to the appropriate end of save.
1590 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1591 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1592 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1593 save_blk->bp, &savehdr, sents, 0,
1596 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1597 save_blk->bp, &savehdr, sents,
1598 savehdr.count, drophdr.count);
1599 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1601 /* log the changes made when moving the entries */
1602 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1603 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
1604 xfs_dir3_leaf_log_header(args, save_blk->bp);
1605 xfs_dir3_leaf_log_header(args, drop_blk->bp);
1607 xfs_dir3_leaf_check(dp, save_blk->bp);
1608 xfs_dir3_leaf_check(dp, drop_blk->bp);
1612 * Add a new data block to the directory at the free space index that the caller
1616 xfs_dir2_node_add_datablk(
1617 struct xfs_da_args *args,
1618 struct xfs_da_state_blk *fblk,
1619 xfs_dir2_db_t *dbno,
1620 struct xfs_buf **dbpp,
1621 struct xfs_buf **fbpp,
1624 struct xfs_inode *dp = args->dp;
1625 struct xfs_trans *tp = args->trans;
1626 struct xfs_mount *mp = dp->i_mount;
1627 struct xfs_dir3_icfree_hdr freehdr;
1628 struct xfs_dir2_data_free *bf;
1629 struct xfs_dir2_data_hdr *hdr;
1630 struct xfs_dir2_free *free = NULL;
1632 struct xfs_buf *fbp;
1633 struct xfs_buf *dbp;
1634 __be16 *bests = NULL;
1637 /* Not allowed to allocate, return failure. */
1638 if (args->total == 0)
1641 /* Allocate and initialize the new data block. */
1642 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1645 error = xfs_dir3_data_init(args, *dbno, &dbp);
1650 * Get the freespace block corresponding to the data block
1651 * that was just allocated.
1653 fbno = dp->d_ops->db_to_fdb(args->geo, *dbno);
1654 error = xfs_dir2_free_try_read(tp, dp,
1655 xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1660 * If there wasn't a freespace block, the read will
1661 * return a NULL fbp. Allocate and initialize a new one.
1664 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1668 if (dp->d_ops->db_to_fdb(args->geo, *dbno) != fbno) {
1670 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1671 __func__, (unsigned long long)dp->i_ino,
1672 (long long)dp->d_ops->db_to_fdb(args->geo, *dbno),
1673 (long long)*dbno, (long long)fbno);
1676 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1677 fblk, (unsigned long long)fblk->blkno,
1678 fblk->index, fblk->magic);
1680 xfs_alert(mp, " ... fblk is NULL");
1682 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1683 return -EFSCORRUPTED;
1686 /* Get a buffer for the new block. */
1687 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1691 bests = dp->d_ops->free_bests_p(free);
1692 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1694 /* Remember the first slot as our empty slot. */
1695 freehdr.firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1696 XFS_DIR2_FREE_OFFSET)) *
1697 dp->d_ops->free_max_bests(args->geo);
1700 bests = dp->d_ops->free_bests_p(free);
1701 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1704 /* Set the freespace block index from the data block number. */
1705 *findex = dp->d_ops->db_to_fdindex(args->geo, *dbno);
1707 /* Extend the freespace table if the new data block is off the end. */
1708 if (*findex >= freehdr.nvalid) {
1709 ASSERT(*findex < dp->d_ops->free_max_bests(args->geo));
1710 freehdr.nvalid = *findex + 1;
1711 bests[*findex] = cpu_to_be16(NULLDATAOFF);
1715 * If this entry was for an empty data block (this should always be
1716 * true) then update the header.
1718 if (bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1720 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1721 xfs_dir2_free_log_header(args, fbp);
1724 /* Update the freespace value for the new block in the table. */
1726 bf = dp->d_ops->data_bestfree_p(hdr);
1727 bests[*findex] = bf[0].length;
1735 xfs_dir2_node_find_freeblk(
1736 struct xfs_da_args *args,
1737 struct xfs_da_state_blk *fblk,
1738 xfs_dir2_db_t *dbnop,
1739 struct xfs_buf **fbpp,
1743 struct xfs_dir3_icfree_hdr freehdr;
1744 struct xfs_dir2_free *free = NULL;
1745 struct xfs_inode *dp = args->dp;
1746 struct xfs_trans *tp = args->trans;
1747 struct xfs_buf *fbp = NULL;
1748 xfs_dir2_db_t firstfbno;
1749 xfs_dir2_db_t lastfbno;
1750 xfs_dir2_db_t ifbno = -1;
1751 xfs_dir2_db_t dbno = -1;
1754 __be16 *bests = NULL;
1759 * If we came in with a freespace block that means that lookup
1760 * found an entry with our hash value. This is the freespace
1761 * block for that data entry.
1766 findex = fblk->index;
1768 /* caller already found the freespace for us. */
1769 bests = dp->d_ops->free_bests_p(free);
1770 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1772 ASSERT(findex < freehdr.nvalid);
1773 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1774 ASSERT(be16_to_cpu(bests[findex]) >= length);
1775 dbno = freehdr.firstdb + findex;
1780 * The data block looked at didn't have enough room.
1781 * We'll start at the beginning of the freespace entries.
1783 ifbno = fblk->blkno;
1784 xfs_trans_brelse(tp, fbp);
1790 * If we don't have a data block yet, we're going to scan the freespace
1791 * data for a data block with enough free space in it.
1793 error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1796 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1797 firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
1799 for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
1800 /* If it's ifbno we already looked at it. */
1805 * Read the block. There can be holes in the freespace blocks,
1806 * so this might not succeed. This should be really rare, so
1807 * there's no reason to avoid it.
1809 error = xfs_dir2_free_try_read(tp, dp,
1810 xfs_dir2_db_to_da(args->geo, fbno),
1818 bests = dp->d_ops->free_bests_p(free);
1819 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1821 /* Scan the free entry array for a large enough free space. */
1822 for (findex = freehdr.nvalid - 1; findex >= 0; findex--) {
1823 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1824 be16_to_cpu(bests[findex]) >= length) {
1825 dbno = freehdr.firstdb + findex;
1830 /* Didn't find free space, go on to next free block */
1831 xfs_trans_brelse(tp, fbp);
1843 * Add the data entry for a node-format directory name addition.
1844 * The leaf entry is added in xfs_dir2_leafn_add.
1845 * We may enter with a freespace block that the lookup found.
1848 xfs_dir2_node_addname_int(
1849 struct xfs_da_args *args, /* operation arguments */
1850 struct xfs_da_state_blk *fblk) /* optional freespace block */
1852 struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1853 struct xfs_dir2_data_entry *dep; /* data entry pointer */
1854 struct xfs_dir2_data_hdr *hdr; /* data block header */
1855 struct xfs_dir2_data_free *bf;
1856 struct xfs_dir2_free *free = NULL; /* freespace block structure */
1857 struct xfs_trans *tp = args->trans;
1858 struct xfs_inode *dp = args->dp;
1859 struct xfs_buf *dbp; /* data block buffer */
1860 struct xfs_buf *fbp; /* freespace buffer */
1861 xfs_dir2_data_aoff_t aoff;
1862 xfs_dir2_db_t dbno; /* data block number */
1863 int error; /* error return value */
1864 int findex; /* freespace entry index */
1865 int length; /* length of the new entry */
1866 int logfree = 0; /* need to log free entry */
1867 int needlog = 0; /* need to log data header */
1868 int needscan = 0; /* need to rescan data frees */
1869 __be16 *tagp; /* data entry tag pointer */
1872 length = dp->d_ops->data_entsize(args->namelen);
1873 error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &findex,
1879 * Now we know if we must allocate blocks, so if we are checking whether
1880 * we can insert without allocation then we can return now.
1882 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1889 * If we don't have a data block, we need to allocate one and make
1890 * the freespace entries refer to it.
1893 /* we're going to have to log the free block index later */
1895 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1898 /* Read the data block in. */
1899 error = xfs_dir3_data_read(tp, dp,
1900 xfs_dir2_db_to_da(args->geo, dbno),
1906 /* setup for data block up now */
1908 bf = dp->d_ops->data_bestfree_p(hdr);
1909 ASSERT(be16_to_cpu(bf[0].length) >= length);
1911 /* Point to the existing unused space. */
1912 dup = (xfs_dir2_data_unused_t *)
1913 ((char *)hdr + be16_to_cpu(bf[0].offset));
1915 /* Mark the first part of the unused space, inuse for us. */
1916 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1917 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1918 &needlog, &needscan);
1920 xfs_trans_brelse(tp, dbp);
1924 /* Fill in the new entry and log it. */
1925 dep = (xfs_dir2_data_entry_t *)dup;
1926 dep->inumber = cpu_to_be64(args->inumber);
1927 dep->namelen = args->namelen;
1928 memcpy(dep->name, args->name, dep->namelen);
1929 dp->d_ops->data_put_ftype(dep, args->filetype);
1930 tagp = dp->d_ops->data_entry_tag_p(dep);
1931 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1932 xfs_dir2_data_log_entry(args, dbp, dep);
1934 /* Rescan the freespace and log the data block if needed. */
1936 xfs_dir2_data_freescan(dp, hdr, &needlog);
1938 xfs_dir2_data_log_header(args, dbp);
1940 /* If the freespace block entry is now wrong, update it. */
1942 bests = dp->d_ops->free_bests_p(free);
1943 if (bests[findex] != bf[0].length) {
1944 bests[findex] = bf[0].length;
1948 /* Log the freespace entry if needed. */
1950 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1952 /* Return the data block and offset in args. */
1953 args->blkno = (xfs_dablk_t)dbno;
1954 args->index = be16_to_cpu(*tagp);
1959 * Top-level node form directory addname routine.
1962 xfs_dir2_node_addname(
1963 xfs_da_args_t *args) /* operation arguments */
1965 xfs_da_state_blk_t *blk; /* leaf block for insert */
1966 int error; /* error return value */
1967 int rval; /* sub-return value */
1968 xfs_da_state_t *state; /* btree cursor */
1970 trace_xfs_dir2_node_addname(args);
1973 * Allocate and initialize the state (btree cursor).
1975 state = xfs_da_state_alloc();
1977 state->mp = args->dp->i_mount;
1979 * Look up the name. We're not supposed to find it, but
1980 * this gives us the insertion point.
1982 error = xfs_da3_node_lookup_int(state, &rval);
1985 if (rval != -ENOENT) {
1989 * Add the data entry to a data block.
1990 * Extravalid is set to a freeblock found by lookup.
1992 rval = xfs_dir2_node_addname_int(args,
1993 state->extravalid ? &state->extrablk : NULL);
1997 blk = &state->path.blk[state->path.active - 1];
1998 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2000 * Add the new leaf entry.
2002 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2005 * It worked, fix the hash values up the btree.
2007 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2008 xfs_da3_fixhashpath(state, &state->path);
2011 * It didn't work, we need to split the leaf block.
2013 if (args->total == 0) {
2014 ASSERT(rval == -ENOSPC);
2018 * Split the leaf block and insert the new entry.
2020 rval = xfs_da3_split(state);
2023 xfs_da_state_free(state);
2028 * Lookup an entry in a node-format directory.
2029 * All the real work happens in xfs_da3_node_lookup_int.
2030 * The only real output is the inode number of the entry.
2033 xfs_dir2_node_lookup(
2034 xfs_da_args_t *args) /* operation arguments */
2036 int error; /* error return value */
2037 int i; /* btree level */
2038 int rval; /* operation return value */
2039 xfs_da_state_t *state; /* btree cursor */
2041 trace_xfs_dir2_node_lookup(args);
2044 * Allocate and initialize the btree cursor.
2046 state = xfs_da_state_alloc();
2048 state->mp = args->dp->i_mount;
2050 * Fill in the path to the entry in the cursor.
2052 error = xfs_da3_node_lookup_int(state, &rval);
2055 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2056 /* If a CI match, dup the actual name and return -EEXIST */
2057 xfs_dir2_data_entry_t *dep;
2059 dep = (xfs_dir2_data_entry_t *)
2060 ((char *)state->extrablk.bp->b_addr +
2061 state->extrablk.index);
2062 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2065 * Release the btree blocks and leaf block.
2067 for (i = 0; i < state->path.active; i++) {
2068 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2069 state->path.blk[i].bp = NULL;
2072 * Release the data block if we have it.
2074 if (state->extravalid && state->extrablk.bp) {
2075 xfs_trans_brelse(args->trans, state->extrablk.bp);
2076 state->extrablk.bp = NULL;
2078 xfs_da_state_free(state);
2083 * Remove an entry from a node-format directory.
2086 xfs_dir2_node_removename(
2087 struct xfs_da_args *args) /* operation arguments */
2089 struct xfs_da_state_blk *blk; /* leaf block */
2090 int error; /* error return value */
2091 int rval; /* operation return value */
2092 struct xfs_da_state *state; /* btree cursor */
2094 trace_xfs_dir2_node_removename(args);
2097 * Allocate and initialize the btree cursor.
2099 state = xfs_da_state_alloc();
2101 state->mp = args->dp->i_mount;
2103 /* Look up the entry we're deleting, set up the cursor. */
2104 error = xfs_da3_node_lookup_int(state, &rval);
2108 /* Didn't find it, upper layer screwed up. */
2109 if (rval != -EEXIST) {
2114 blk = &state->path.blk[state->path.active - 1];
2115 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2116 ASSERT(state->extravalid);
2118 * Remove the leaf and data entries.
2119 * Extrablk refers to the data block.
2121 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2122 &state->extrablk, &rval);
2126 * Fix the hash values up the btree.
2128 xfs_da3_fixhashpath(state, &state->path);
2130 * If we need to join leaf blocks, do it.
2132 if (rval && state->path.active > 1)
2133 error = xfs_da3_join(state);
2135 * If no errors so far, try conversion to leaf format.
2138 error = xfs_dir2_node_to_leaf(state);
2140 xfs_da_state_free(state);
2145 * Replace an entry's inode number in a node-format directory.
2148 xfs_dir2_node_replace(
2149 xfs_da_args_t *args) /* operation arguments */
2151 xfs_da_state_blk_t *blk; /* leaf block */
2152 xfs_dir2_data_hdr_t *hdr; /* data block header */
2153 xfs_dir2_data_entry_t *dep; /* data entry changed */
2154 int error; /* error return value */
2155 int i; /* btree level */
2156 xfs_ino_t inum; /* new inode number */
2157 int ftype; /* new file type */
2158 xfs_dir2_leaf_t *leaf; /* leaf structure */
2159 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2160 int rval; /* internal return value */
2161 xfs_da_state_t *state; /* btree cursor */
2163 trace_xfs_dir2_node_replace(args);
2166 * Allocate and initialize the btree cursor.
2168 state = xfs_da_state_alloc();
2170 state->mp = args->dp->i_mount;
2173 * We have to save new inode number and ftype since
2174 * xfs_da3_node_lookup_int() is going to overwrite them
2176 inum = args->inumber;
2177 ftype = args->filetype;
2180 * Lookup the entry to change in the btree.
2182 error = xfs_da3_node_lookup_int(state, &rval);
2187 * It should be found, since the vnodeops layer has looked it up
2188 * and locked it. But paranoia is good.
2190 if (rval == -EEXIST) {
2191 struct xfs_dir2_leaf_entry *ents;
2193 * Find the leaf entry.
2195 blk = &state->path.blk[state->path.active - 1];
2196 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2197 leaf = blk->bp->b_addr;
2198 ents = args->dp->d_ops->leaf_ents_p(leaf);
2199 lep = &ents[blk->index];
2200 ASSERT(state->extravalid);
2202 * Point to the data entry.
2204 hdr = state->extrablk.bp->b_addr;
2205 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2206 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2207 dep = (xfs_dir2_data_entry_t *)
2209 xfs_dir2_dataptr_to_off(args->geo,
2210 be32_to_cpu(lep->address)));
2211 ASSERT(inum != be64_to_cpu(dep->inumber));
2213 * Fill in the new inode number and log the entry.
2215 dep->inumber = cpu_to_be64(inum);
2216 args->dp->d_ops->data_put_ftype(dep, ftype);
2217 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2221 * Didn't find it, and we're holding a data block. Drop it.
2223 else if (state->extravalid) {
2224 xfs_trans_brelse(args->trans, state->extrablk.bp);
2225 state->extrablk.bp = NULL;
2228 * Release all the buffers in the cursor.
2230 for (i = 0; i < state->path.active; i++) {
2231 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2232 state->path.blk[i].bp = NULL;
2234 xfs_da_state_free(state);
2239 * Trim off a trailing empty freespace block.
2240 * Return (in rvalp) 1 if we did it, 0 if not.
2243 xfs_dir2_node_trim_free(
2244 xfs_da_args_t *args, /* operation arguments */
2245 xfs_fileoff_t fo, /* free block number */
2246 int *rvalp) /* out: did something */
2248 struct xfs_buf *bp; /* freespace buffer */
2249 xfs_inode_t *dp; /* incore directory inode */
2250 int error; /* error return code */
2251 xfs_dir2_free_t *free; /* freespace structure */
2252 xfs_trans_t *tp; /* transaction pointer */
2253 struct xfs_dir3_icfree_hdr freehdr;
2261 * Read the freespace block.
2263 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
2267 * There can be holes in freespace. If fo is a hole, there's
2273 dp->d_ops->free_hdr_from_disk(&freehdr, free);
2276 * If there are used entries, there's nothing to do.
2278 if (freehdr.nused > 0) {
2279 xfs_trans_brelse(tp, bp);
2283 * Blow the block away.
2285 error = xfs_dir2_shrink_inode(args,
2286 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2289 * Can't fail with ENOSPC since that only happens with no
2290 * space reservation, when breaking up an extent into two
2291 * pieces. This is the last block of an extent.
2293 ASSERT(error != -ENOSPC);
2294 xfs_trans_brelse(tp, bp);
2298 * Return that we succeeded.