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,
35 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
36 xfs_da_state_blk_t *fblk);
39 * Check internal consistency of a leafn block.
47 struct xfs_dir2_leaf *leaf = bp->b_addr;
48 struct xfs_dir3_icleaf_hdr leafhdr;
50 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
52 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
53 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
54 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
55 return __this_address;
56 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
57 return __this_address;
59 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
69 fa = xfs_dir3_leafn_check(dp, bp);
72 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
73 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
78 #define xfs_dir3_leaf_check(dp, bp)
85 struct xfs_mount *mp = bp->b_mount;
86 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
88 if (!xfs_verify_magic(bp, hdr->magic))
89 return __this_address;
91 if (xfs_sb_version_hascrc(&mp->m_sb)) {
92 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
94 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
95 return __this_address;
96 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
97 return __this_address;
98 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
99 return __this_address;
102 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
108 xfs_dir3_free_read_verify(
111 struct xfs_mount *mp = bp->b_mount;
114 if (xfs_sb_version_hascrc(&mp->m_sb) &&
115 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
116 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
118 fa = xfs_dir3_free_verify(bp);
120 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
125 xfs_dir3_free_write_verify(
128 struct xfs_mount *mp = bp->b_mount;
129 struct xfs_buf_log_item *bip = bp->b_log_item;
130 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
133 fa = xfs_dir3_free_verify(bp);
135 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
139 if (!xfs_sb_version_hascrc(&mp->m_sb))
143 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
145 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
148 const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
149 .name = "xfs_dir3_free",
150 .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
151 cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
152 .verify_read = xfs_dir3_free_read_verify,
153 .verify_write = xfs_dir3_free_write_verify,
154 .verify_struct = xfs_dir3_free_verify,
157 /* Everything ok in the free block header? */
158 static xfs_failaddr_t
159 xfs_dir3_free_header_check(
160 struct xfs_inode *dp,
164 struct xfs_mount *mp = dp->i_mount;
165 unsigned int firstdb;
168 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
169 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
170 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
172 if (xfs_sb_version_hascrc(&mp->m_sb)) {
173 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
175 if (be32_to_cpu(hdr3->firstdb) != firstdb)
176 return __this_address;
177 if (be32_to_cpu(hdr3->nvalid) > maxbests)
178 return __this_address;
179 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
180 return __this_address;
182 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
184 if (be32_to_cpu(hdr->firstdb) != firstdb)
185 return __this_address;
186 if (be32_to_cpu(hdr->nvalid) > maxbests)
187 return __this_address;
188 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
189 return __this_address;
195 __xfs_dir3_free_read(
196 struct xfs_trans *tp,
197 struct xfs_inode *dp,
199 xfs_daddr_t mappedbno,
200 struct xfs_buf **bpp)
205 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
206 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
210 /* Check things that we can't do in the verifier. */
211 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
213 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
214 xfs_trans_brelse(tp, *bpp);
215 return -EFSCORRUPTED;
218 /* try read returns without an error or *bpp if it lands in a hole */
220 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
227 struct xfs_trans *tp,
228 struct xfs_inode *dp,
230 struct xfs_buf **bpp)
232 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
236 xfs_dir2_free_try_read(
237 struct xfs_trans *tp,
238 struct xfs_inode *dp,
240 struct xfs_buf **bpp)
242 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
246 xfs_dir3_free_get_buf(
249 struct xfs_buf **bpp)
251 struct xfs_trans *tp = args->trans;
252 struct xfs_inode *dp = args->dp;
253 struct xfs_mount *mp = dp->i_mount;
256 struct xfs_dir3_icfree_hdr hdr;
258 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
259 -1, &bp, XFS_DATA_FORK);
263 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
264 bp->b_ops = &xfs_dir3_free_buf_ops;
267 * Initialize the new block to be empty, and remember
268 * its first slot as our empty slot.
270 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
271 memset(&hdr, 0, sizeof(hdr));
273 if (xfs_sb_version_hascrc(&mp->m_sb)) {
274 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
276 hdr.magic = XFS_DIR3_FREE_MAGIC;
278 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
279 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
280 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
282 hdr.magic = XFS_DIR2_FREE_MAGIC;
283 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
289 * Log entries from a freespace block.
292 xfs_dir2_free_log_bests(
293 struct xfs_da_args *args,
295 int first, /* first entry to log */
296 int last) /* last entry to log */
298 xfs_dir2_free_t *free; /* freespace structure */
302 bests = args->dp->d_ops->free_bests_p(free);
303 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
304 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
305 xfs_trans_log_buf(args->trans, bp,
306 (uint)((char *)&bests[first] - (char *)free),
307 (uint)((char *)&bests[last] - (char *)free +
308 sizeof(bests[0]) - 1));
312 * Log header from a freespace block.
315 xfs_dir2_free_log_header(
316 struct xfs_da_args *args,
320 xfs_dir2_free_t *free; /* freespace structure */
323 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
324 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
326 xfs_trans_log_buf(args->trans, bp, 0,
327 args->dp->d_ops->free_hdr_size - 1);
331 * Convert a leaf-format directory to a node-format directory.
332 * We need to change the magic number of the leaf block, and copy
333 * the freespace table out of the leaf block into its own block.
336 xfs_dir2_leaf_to_node(
337 xfs_da_args_t *args, /* operation arguments */
338 struct xfs_buf *lbp) /* leaf buffer */
340 xfs_inode_t *dp; /* incore directory inode */
341 int error; /* error return value */
342 struct xfs_buf *fbp; /* freespace buffer */
343 xfs_dir2_db_t fdb; /* freespace block number */
344 xfs_dir2_free_t *free; /* freespace structure */
345 __be16 *from; /* pointer to freespace entry */
346 int i; /* leaf freespace index */
347 xfs_dir2_leaf_t *leaf; /* leaf structure */
348 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
349 int n; /* count of live freespc ents */
350 xfs_dir2_data_off_t off; /* freespace entry value */
351 __be16 *to; /* pointer to freespace entry */
352 xfs_trans_t *tp; /* transaction pointer */
353 struct xfs_dir3_icfree_hdr freehdr;
355 trace_xfs_dir2_leaf_to_node(args);
360 * Add a freespace block to the directory.
362 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
365 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
367 * Get the buffer for the new freespace block.
369 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
374 dp->d_ops->free_hdr_from_disk(&freehdr, free);
376 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
377 if (be32_to_cpu(ltp->bestcount) >
378 (uint)dp->i_d.di_size / args->geo->blksize)
379 return -EFSCORRUPTED;
382 * Copy freespace entries from the leaf block to the new block.
383 * Count active entries.
385 from = xfs_dir2_leaf_bests_p(ltp);
386 to = dp->d_ops->free_bests_p(free);
387 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
388 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
390 *to = cpu_to_be16(off);
394 * Now initialize the freespace block header.
397 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
399 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
400 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
401 xfs_dir2_free_log_header(args, fbp);
404 * Converting the leaf to a leafnode is just a matter of changing the
405 * magic number and the ops. Do the change directly to the buffer as
406 * it's less work (and less code) than decoding the header to host
407 * format and back again.
409 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
410 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
412 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
413 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
414 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
415 xfs_dir3_leaf_log_header(args, lbp);
416 xfs_dir3_leaf_check(dp, lbp);
421 * Add a leaf entry to a leaf block in a node-form directory.
422 * The other work necessary is done from the caller.
424 static int /* error */
426 struct xfs_buf *bp, /* leaf buffer */
427 struct xfs_da_args *args, /* operation arguments */
428 int index) /* insertion pt for new entry */
430 struct xfs_dir3_icleaf_hdr leafhdr;
431 struct xfs_inode *dp = args->dp;
432 struct xfs_dir2_leaf *leaf = bp->b_addr;
433 struct xfs_dir2_leaf_entry *lep;
434 struct xfs_dir2_leaf_entry *ents;
435 int compact; /* compacting stale leaves */
436 int highstale = 0; /* next stale entry */
437 int lfloghigh; /* high leaf entry logging */
438 int lfloglow; /* low leaf entry logging */
439 int lowstale = 0; /* previous stale entry */
441 trace_xfs_dir2_leafn_add(args, index);
443 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
444 ents = dp->d_ops->leaf_ents_p(leaf);
447 * Quick check just to make sure we are not going to index
448 * into other peoples memory
451 return -EFSCORRUPTED;
454 * If there are already the maximum number of leaf entries in
455 * the block, if there are no stale entries it won't fit.
456 * Caller will do a split. If there are stale entries we'll do
460 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
463 compact = leafhdr.stale > 1;
466 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
467 ASSERT(index == leafhdr.count ||
468 be32_to_cpu(ents[index].hashval) >= args->hashval);
470 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
474 * Compact out all but one stale leaf entry. Leaves behind
475 * the entry closest to index.
478 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
479 &highstale, &lfloglow, &lfloghigh);
480 else if (leafhdr.stale) {
482 * Set impossible logging indices for this case.
484 lfloglow = leafhdr.count;
489 * Insert the new entry, log everything.
491 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
492 highstale, &lfloglow, &lfloghigh);
494 lep->hashval = cpu_to_be32(args->hashval);
495 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
496 args->blkno, args->index));
498 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
499 xfs_dir3_leaf_log_header(args, bp);
500 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
501 xfs_dir3_leaf_check(dp, bp);
507 xfs_dir2_free_hdr_check(
508 struct xfs_inode *dp,
512 struct xfs_dir3_icfree_hdr hdr;
514 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
516 ASSERT((hdr.firstdb %
517 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
518 ASSERT(hdr.firstdb <= db);
519 ASSERT(db < hdr.firstdb + hdr.nvalid);
522 #define xfs_dir2_free_hdr_check(dp, bp, db)
526 * Return the last hash value in the leaf.
527 * Stale entries are ok.
529 xfs_dahash_t /* hash value */
530 xfs_dir2_leaf_lasthash(
531 struct xfs_inode *dp,
532 struct xfs_buf *bp, /* leaf buffer */
533 int *count) /* count of entries in leaf */
535 struct xfs_dir2_leaf *leaf = bp->b_addr;
536 struct xfs_dir2_leaf_entry *ents;
537 struct xfs_dir3_icleaf_hdr leafhdr;
539 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
541 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
542 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
543 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
544 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
547 *count = leafhdr.count;
551 ents = dp->d_ops->leaf_ents_p(leaf);
552 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
556 * Look up a leaf entry for space to add a name in a node-format leaf block.
557 * The extrablk in state is a freespace block.
560 xfs_dir2_leafn_lookup_for_addname(
561 struct xfs_buf *bp, /* leaf buffer */
562 xfs_da_args_t *args, /* operation arguments */
563 int *indexp, /* out: leaf entry index */
564 xfs_da_state_t *state) /* state to fill in */
566 struct xfs_buf *curbp = NULL; /* current data/free buffer */
567 xfs_dir2_db_t curdb = -1; /* current data block number */
568 xfs_dir2_db_t curfdb = -1; /* current free block number */
569 xfs_inode_t *dp; /* incore directory inode */
570 int error; /* error return value */
571 int fi; /* free entry index */
572 xfs_dir2_free_t *free = NULL; /* free block structure */
573 int index; /* leaf entry index */
574 xfs_dir2_leaf_t *leaf; /* leaf structure */
575 int length; /* length of new data entry */
576 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
577 xfs_mount_t *mp; /* filesystem mount point */
578 xfs_dir2_db_t newdb; /* new data block number */
579 xfs_dir2_db_t newfdb; /* new free block number */
580 xfs_trans_t *tp; /* transaction pointer */
581 struct xfs_dir2_leaf_entry *ents;
582 struct xfs_dir3_icleaf_hdr leafhdr;
588 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
589 ents = dp->d_ops->leaf_ents_p(leaf);
591 xfs_dir3_leaf_check(dp, bp);
592 ASSERT(leafhdr.count > 0);
595 * Look up the hash value in the leaf entries.
597 index = xfs_dir2_leaf_search_hash(args, bp);
599 * Do we have a buffer coming in?
601 if (state->extravalid) {
602 /* If so, it's a free block buffer, get the block number. */
603 curbp = state->extrablk.bp;
604 curfdb = state->extrablk.blkno;
605 free = curbp->b_addr;
606 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
607 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
609 length = dp->d_ops->data_entsize(args->namelen);
611 * Loop over leaf entries with the right hash value.
613 for (lep = &ents[index];
614 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
617 * Skip stale leaf entries.
619 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
622 * Pull the data block number from the entry.
624 newdb = xfs_dir2_dataptr_to_db(args->geo,
625 be32_to_cpu(lep->address));
627 * For addname, we're looking for a place to put the new entry.
628 * We want to use a data block with an entry of equal
629 * hash value to ours if there is one with room.
631 * If this block isn't the data block we already have
632 * in hand, take a look at it.
634 if (newdb != curdb) {
639 * Convert the data block to the free block
640 * holding its freespace information.
642 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
644 * If it's not the one we have in hand, read it in.
646 if (newfdb != curfdb) {
648 * If we had one before, drop it.
651 xfs_trans_brelse(tp, curbp);
653 error = xfs_dir2_free_read(tp, dp,
654 xfs_dir2_db_to_da(args->geo,
659 free = curbp->b_addr;
661 xfs_dir2_free_hdr_check(dp, curbp, curdb);
664 * Get the index for our entry.
666 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
668 * If it has room, return it.
670 bests = dp->d_ops->free_bests_p(free);
671 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
672 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
673 XFS_ERRLEVEL_LOW, mp);
674 if (curfdb != newfdb)
675 xfs_trans_brelse(tp, curbp);
676 return -EFSCORRUPTED;
679 if (be16_to_cpu(bests[fi]) >= length)
683 /* Didn't find any space */
686 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
688 /* Giving back a free block. */
689 state->extravalid = 1;
690 state->extrablk.bp = curbp;
691 state->extrablk.index = fi;
692 state->extrablk.blkno = curfdb;
695 * Important: this magic number is not in the buffer - it's for
696 * buffer type information and therefore only the free/data type
697 * matters here, not whether CRCs are enabled or not.
699 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
701 state->extravalid = 0;
704 * Return the index, that will be the insertion point.
711 * Look up a leaf entry in a node-format leaf block.
712 * The extrablk in state a data block.
715 xfs_dir2_leafn_lookup_for_entry(
716 struct xfs_buf *bp, /* leaf buffer */
717 xfs_da_args_t *args, /* operation arguments */
718 int *indexp, /* out: leaf entry index */
719 xfs_da_state_t *state) /* state to fill in */
721 struct xfs_buf *curbp = NULL; /* current data/free buffer */
722 xfs_dir2_db_t curdb = -1; /* current data block number */
723 xfs_dir2_data_entry_t *dep; /* data block entry */
724 xfs_inode_t *dp; /* incore directory inode */
725 int error; /* error return value */
726 int index; /* leaf entry index */
727 xfs_dir2_leaf_t *leaf; /* leaf structure */
728 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
729 xfs_mount_t *mp; /* filesystem mount point */
730 xfs_dir2_db_t newdb; /* new data block number */
731 xfs_trans_t *tp; /* transaction pointer */
732 enum xfs_dacmp cmp; /* comparison result */
733 struct xfs_dir2_leaf_entry *ents;
734 struct xfs_dir3_icleaf_hdr leafhdr;
740 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
741 ents = dp->d_ops->leaf_ents_p(leaf);
743 xfs_dir3_leaf_check(dp, bp);
744 ASSERT(leafhdr.count > 0);
747 * Look up the hash value in the leaf entries.
749 index = xfs_dir2_leaf_search_hash(args, bp);
751 * Do we have a buffer coming in?
753 if (state->extravalid) {
754 curbp = state->extrablk.bp;
755 curdb = state->extrablk.blkno;
758 * Loop over leaf entries with the right hash value.
760 for (lep = &ents[index];
761 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
764 * Skip stale leaf entries.
766 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
769 * Pull the data block number from the entry.
771 newdb = xfs_dir2_dataptr_to_db(args->geo,
772 be32_to_cpu(lep->address));
774 * Not adding a new entry, so we really want to find
775 * the name given to us.
777 * If it's a different data block, go get it.
779 if (newdb != curdb) {
781 * If we had a block before that we aren't saving
782 * for a CI name, drop it
784 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
785 curdb != state->extrablk.blkno))
786 xfs_trans_brelse(tp, curbp);
788 * If needing the block that is saved with a CI match,
789 * use it otherwise read in the new data block.
791 if (args->cmpresult != XFS_CMP_DIFFERENT &&
792 newdb == state->extrablk.blkno) {
793 ASSERT(state->extravalid);
794 curbp = state->extrablk.bp;
796 error = xfs_dir3_data_read(tp, dp,
797 xfs_dir2_db_to_da(args->geo,
803 xfs_dir3_data_check(dp, curbp);
807 * Point to the data entry.
809 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
810 xfs_dir2_dataptr_to_off(args->geo,
811 be32_to_cpu(lep->address)));
813 * Compare the entry and if it's an exact match, return
814 * EEXIST immediately. If it's the first case-insensitive
815 * match, store the block & inode number and continue looking.
817 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
818 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
819 /* If there is a CI match block, drop it */
820 if (args->cmpresult != XFS_CMP_DIFFERENT &&
821 curdb != state->extrablk.blkno)
822 xfs_trans_brelse(tp, state->extrablk.bp);
823 args->cmpresult = cmp;
824 args->inumber = be64_to_cpu(dep->inumber);
825 args->filetype = dp->d_ops->data_get_ftype(dep);
827 state->extravalid = 1;
828 state->extrablk.bp = curbp;
829 state->extrablk.blkno = curdb;
830 state->extrablk.index = (int)((char *)dep -
831 (char *)curbp->b_addr);
832 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
833 curbp->b_ops = &xfs_dir3_data_buf_ops;
834 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
835 if (cmp == XFS_CMP_EXACT)
839 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
841 if (args->cmpresult == XFS_CMP_DIFFERENT) {
842 /* Giving back last used data block. */
843 state->extravalid = 1;
844 state->extrablk.bp = curbp;
845 state->extrablk.index = -1;
846 state->extrablk.blkno = curdb;
847 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
848 curbp->b_ops = &xfs_dir3_data_buf_ops;
849 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
851 /* If the curbp is not the CI match block, drop it */
852 if (state->extrablk.bp != curbp)
853 xfs_trans_brelse(tp, curbp);
856 state->extravalid = 0;
863 * Look up a leaf entry in a node-format leaf block.
864 * If this is an addname then the extrablk in state is a freespace block,
865 * otherwise it's a data block.
868 xfs_dir2_leafn_lookup_int(
869 struct xfs_buf *bp, /* leaf buffer */
870 xfs_da_args_t *args, /* operation arguments */
871 int *indexp, /* out: leaf entry index */
872 xfs_da_state_t *state) /* state to fill in */
874 if (args->op_flags & XFS_DA_OP_ADDNAME)
875 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
877 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
881 * Move count leaf entries from source to destination leaf.
882 * Log entries and headers. Stale entries are preserved.
885 xfs_dir3_leafn_moveents(
886 xfs_da_args_t *args, /* operation arguments */
887 struct xfs_buf *bp_s, /* source */
888 struct xfs_dir3_icleaf_hdr *shdr,
889 struct xfs_dir2_leaf_entry *sents,
890 int start_s,/* source leaf index */
891 struct xfs_buf *bp_d, /* destination */
892 struct xfs_dir3_icleaf_hdr *dhdr,
893 struct xfs_dir2_leaf_entry *dents,
894 int start_d,/* destination leaf index */
895 int count) /* count of leaves to copy */
897 int stale; /* count stale leaves copied */
899 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
902 * Silently return if nothing to do.
908 * If the destination index is not the end of the current
909 * destination leaf entries, open up a hole in the destination
910 * to hold the new entries.
912 if (start_d < dhdr->count) {
913 memmove(&dents[start_d + count], &dents[start_d],
914 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
915 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
916 count + dhdr->count - 1);
919 * If the source has stale leaves, count the ones in the copy range
920 * so we can update the header correctly.
923 int i; /* temp leaf index */
925 for (i = start_s, stale = 0; i < start_s + count; i++) {
926 if (sents[i].address ==
927 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
933 * Copy the leaf entries from source to destination.
935 memcpy(&dents[start_d], &sents[start_s],
936 count * sizeof(xfs_dir2_leaf_entry_t));
937 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
940 * If there are source entries after the ones we copied,
941 * delete the ones we copied by sliding the next ones down.
943 if (start_s + count < shdr->count) {
944 memmove(&sents[start_s], &sents[start_s + count],
945 count * sizeof(xfs_dir2_leaf_entry_t));
946 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
950 * Update the headers and log them.
952 shdr->count -= count;
953 shdr->stale -= stale;
954 dhdr->count += count;
955 dhdr->stale += stale;
959 * Determine the sort order of two leaf blocks.
960 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
963 xfs_dir2_leafn_order(
964 struct xfs_inode *dp,
965 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
966 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
968 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
969 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
970 struct xfs_dir2_leaf_entry *ents1;
971 struct xfs_dir2_leaf_entry *ents2;
972 struct xfs_dir3_icleaf_hdr hdr1;
973 struct xfs_dir3_icleaf_hdr hdr2;
975 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
976 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
977 ents1 = dp->d_ops->leaf_ents_p(leaf1);
978 ents2 = dp->d_ops->leaf_ents_p(leaf2);
980 if (hdr1.count > 0 && hdr2.count > 0 &&
981 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
982 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
983 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
989 * Rebalance leaf entries between two leaf blocks.
990 * This is actually only called when the second block is new,
991 * though the code deals with the general case.
992 * A new entry will be inserted in one of the blocks, and that
993 * entry is taken into account when balancing.
996 xfs_dir2_leafn_rebalance(
997 xfs_da_state_t *state, /* btree cursor */
998 xfs_da_state_blk_t *blk1, /* first btree block */
999 xfs_da_state_blk_t *blk2) /* second btree block */
1001 xfs_da_args_t *args; /* operation arguments */
1002 int count; /* count (& direction) leaves */
1003 int isleft; /* new goes in left leaf */
1004 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1005 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1006 int mid; /* midpoint leaf index */
1007 #if defined(DEBUG) || defined(XFS_WARN)
1008 int oldstale; /* old count of stale leaves */
1010 int oldsum; /* old total leaf count */
1011 int swap_blocks; /* swapped leaf blocks */
1012 struct xfs_dir2_leaf_entry *ents1;
1013 struct xfs_dir2_leaf_entry *ents2;
1014 struct xfs_dir3_icleaf_hdr hdr1;
1015 struct xfs_dir3_icleaf_hdr hdr2;
1016 struct xfs_inode *dp = state->args->dp;
1020 * If the block order is wrong, swap the arguments.
1022 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1026 leaf1 = blk1->bp->b_addr;
1027 leaf2 = blk2->bp->b_addr;
1028 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1029 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
1030 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1031 ents2 = dp->d_ops->leaf_ents_p(leaf2);
1033 oldsum = hdr1.count + hdr2.count;
1034 #if defined(DEBUG) || defined(XFS_WARN)
1035 oldstale = hdr1.stale + hdr2.stale;
1040 * If the old leaf count was odd then the new one will be even,
1041 * so we need to divide the new count evenly.
1044 xfs_dahash_t midhash; /* middle entry hash value */
1046 if (mid >= hdr1.count)
1047 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
1049 midhash = be32_to_cpu(ents1[mid].hashval);
1050 isleft = args->hashval <= midhash;
1053 * If the old count is even then the new count is odd, so there's
1054 * no preferred side for the new entry.
1055 * Pick the left one.
1060 * Calculate moved entry count. Positive means left-to-right,
1061 * negative means right-to-left. Then move the entries.
1063 count = hdr1.count - mid + (isleft == 0);
1065 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1066 hdr1.count - count, blk2->bp,
1067 &hdr2, ents2, 0, count);
1069 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1070 blk1->bp, &hdr1, ents1,
1073 ASSERT(hdr1.count + hdr2.count == oldsum);
1074 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1076 /* log the changes made when moving the entries */
1077 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1078 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
1079 xfs_dir3_leaf_log_header(args, blk1->bp);
1080 xfs_dir3_leaf_log_header(args, blk2->bp);
1082 xfs_dir3_leaf_check(dp, blk1->bp);
1083 xfs_dir3_leaf_check(dp, blk2->bp);
1086 * Mark whether we're inserting into the old or new leaf.
1088 if (hdr1.count < hdr2.count)
1089 state->inleaf = swap_blocks;
1090 else if (hdr1.count > hdr2.count)
1091 state->inleaf = !swap_blocks;
1093 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
1095 * Adjust the expected index for insertion.
1098 blk2->index = blk1->index - hdr1.count;
1101 * Finally sanity check just to make sure we are not returning a
1104 if (blk2->index < 0) {
1107 xfs_alert(dp->i_mount,
1108 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1109 __func__, blk1->index);
1114 xfs_dir3_data_block_free(
1115 xfs_da_args_t *args,
1116 struct xfs_dir2_data_hdr *hdr,
1117 struct xfs_dir2_free *free,
1120 struct xfs_buf *fbp,
1125 struct xfs_dir3_icfree_hdr freehdr;
1126 struct xfs_inode *dp = args->dp;
1128 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1129 bests = dp->d_ops->free_bests_p(free);
1132 * Data block is not empty, just set the free entry to the new
1135 bests[findex] = cpu_to_be16(longest);
1136 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1140 /* One less used entry in the free table. */
1144 * If this was the last entry in the table, we can trim the table size
1145 * back. There might be other entries at the end referring to
1146 * non-existent data blocks, get those too.
1148 if (findex == freehdr.nvalid - 1) {
1149 int i; /* free entry index */
1151 for (i = findex - 1; i >= 0; i--) {
1152 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1155 freehdr.nvalid = i + 1;
1158 /* Not the last entry, just punch it out. */
1159 bests[findex] = cpu_to_be16(NULLDATAOFF);
1163 dp->d_ops->free_hdr_to_disk(free, &freehdr);
1164 xfs_dir2_free_log_header(args, fbp);
1167 * If there are no useful entries left in the block, get rid of the
1170 if (!freehdr.nused) {
1173 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1177 } else if (error != -ENOSPC || args->total != 0)
1180 * It's possible to get ENOSPC if there is no
1181 * space reservation. In this case some one
1182 * else will eventually get rid of this block.
1186 /* Log the free entry that changed, unless we got rid of it. */
1188 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1193 * Remove an entry from a node directory.
1194 * This removes the leaf entry and the data entry,
1195 * and updates the free block if necessary.
1197 static int /* error */
1198 xfs_dir2_leafn_remove(
1199 xfs_da_args_t *args, /* operation arguments */
1200 struct xfs_buf *bp, /* leaf buffer */
1201 int index, /* leaf entry index */
1202 xfs_da_state_blk_t *dblk, /* data block */
1203 int *rval) /* resulting block needs join */
1205 xfs_dir2_data_hdr_t *hdr; /* data block header */
1206 xfs_dir2_db_t db; /* data block number */
1207 struct xfs_buf *dbp; /* data block buffer */
1208 xfs_dir2_data_entry_t *dep; /* data block entry */
1209 xfs_inode_t *dp; /* incore directory inode */
1210 xfs_dir2_leaf_t *leaf; /* leaf structure */
1211 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1212 int longest; /* longest data free entry */
1213 int off; /* data block entry offset */
1214 int needlog; /* need to log data header */
1215 int needscan; /* need to rescan data frees */
1216 xfs_trans_t *tp; /* transaction pointer */
1217 struct xfs_dir2_data_free *bf; /* bestfree table */
1218 struct xfs_dir3_icleaf_hdr leafhdr;
1219 struct xfs_dir2_leaf_entry *ents;
1221 trace_xfs_dir2_leafn_remove(args, index);
1226 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1227 ents = dp->d_ops->leaf_ents_p(leaf);
1230 * Point to the entry we're removing.
1235 * Extract the data block and offset from the entry.
1237 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1238 ASSERT(dblk->blkno == db);
1239 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
1240 ASSERT(dblk->index == off);
1243 * Kill the leaf entry by marking it stale.
1244 * Log the leaf block changes.
1247 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1248 xfs_dir3_leaf_log_header(args, bp);
1250 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1251 xfs_dir3_leaf_log_ents(args, bp, index, index);
1254 * Make the data entry free. Keep track of the longest freespace
1255 * in the data block in case it changes.
1259 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1260 bf = dp->d_ops->data_bestfree_p(hdr);
1261 longest = be16_to_cpu(bf[0].length);
1262 needlog = needscan = 0;
1263 xfs_dir2_data_make_free(args, dbp, off,
1264 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1266 * Rescan the data block freespaces for bestfree.
1267 * Log the data block header if needed.
1270 xfs_dir2_data_freescan(dp, hdr, &needlog);
1272 xfs_dir2_data_log_header(args, dbp);
1273 xfs_dir3_data_check(dp, dbp);
1275 * If the longest data block freespace changes, need to update
1276 * the corresponding freeblock entry.
1278 if (longest < be16_to_cpu(bf[0].length)) {
1279 int error; /* error return value */
1280 struct xfs_buf *fbp; /* freeblock buffer */
1281 xfs_dir2_db_t fdb; /* freeblock block number */
1282 int findex; /* index in freeblock entries */
1283 xfs_dir2_free_t *free; /* freeblock structure */
1286 * Convert the data block number to a free block,
1287 * read in the free block.
1289 fdb = dp->d_ops->db_to_fdb(args->geo, db);
1290 error = xfs_dir2_free_read(tp, dp,
1291 xfs_dir2_db_to_da(args->geo, fdb),
1298 struct xfs_dir3_icfree_hdr freehdr;
1299 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1300 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
1301 (fdb - xfs_dir2_byte_to_db(args->geo,
1302 XFS_DIR2_FREE_OFFSET)));
1306 * Calculate which entry we need to fix.
1308 findex = dp->d_ops->db_to_fdindex(args->geo, db);
1309 longest = be16_to_cpu(bf[0].length);
1311 * If the data block is now empty we can get rid of it
1314 if (longest == args->geo->blksize -
1315 dp->d_ops->data_entry_offset) {
1317 * Try to punch out the data block.
1319 error = xfs_dir2_shrink_inode(args, db, dbp);
1325 * We can get ENOSPC if there's no space reservation.
1326 * In this case just drop the buffer and some one else
1327 * will eventually get rid of the empty block.
1329 else if (!(error == -ENOSPC && args->total == 0))
1333 * If we got rid of the data block, we can eliminate that entry
1334 * in the free block.
1336 error = xfs_dir3_data_block_free(args, hdr, free,
1337 fdb, findex, fbp, longest);
1342 xfs_dir3_leaf_check(dp, bp);
1344 * Return indication of whether this leaf block is empty enough
1345 * to justify trying to join it with a neighbor.
1347 *rval = (dp->d_ops->leaf_hdr_size +
1348 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
1349 args->geo->magicpct;
1354 * Split the leaf entries in the old block into old and new blocks.
1357 xfs_dir2_leafn_split(
1358 xfs_da_state_t *state, /* btree cursor */
1359 xfs_da_state_blk_t *oldblk, /* original block */
1360 xfs_da_state_blk_t *newblk) /* newly created block */
1362 xfs_da_args_t *args; /* operation arguments */
1363 xfs_dablk_t blkno; /* new leaf block number */
1364 int error; /* error return value */
1365 struct xfs_inode *dp;
1368 * Allocate space for a new leaf node.
1372 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1373 error = xfs_da_grow_inode(args, &blkno);
1378 * Initialize the new leaf block.
1380 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1381 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1385 newblk->blkno = blkno;
1386 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1388 * Rebalance the entries across the two leaves, link the new
1389 * block into the leaves.
1391 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1392 error = xfs_da3_blk_link(state, oldblk, newblk);
1397 * Insert the new entry in the correct block.
1400 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1402 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1404 * Update last hashval in each block since we added the name.
1406 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1407 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
1408 xfs_dir3_leaf_check(dp, oldblk->bp);
1409 xfs_dir3_leaf_check(dp, newblk->bp);
1414 * Check a leaf block and its neighbors to see if the block should be
1415 * collapsed into one or the other neighbor. Always keep the block
1416 * with the smaller block number.
1417 * If the current block is over 50% full, don't try to join it, return 0.
1418 * If the block is empty, fill in the state structure and return 2.
1419 * If it can be collapsed, fill in the state structure and return 1.
1420 * If nothing can be done, return 0.
1423 xfs_dir2_leafn_toosmall(
1424 xfs_da_state_t *state, /* btree cursor */
1425 int *action) /* resulting action to take */
1427 xfs_da_state_blk_t *blk; /* leaf block */
1428 xfs_dablk_t blkno; /* leaf block number */
1429 struct xfs_buf *bp; /* leaf buffer */
1430 int bytes; /* bytes in use */
1431 int count; /* leaf live entry count */
1432 int error; /* error return value */
1433 int forward; /* sibling block direction */
1434 int i; /* sibling counter */
1435 xfs_dir2_leaf_t *leaf; /* leaf structure */
1436 int rval; /* result from path_shift */
1437 struct xfs_dir3_icleaf_hdr leafhdr;
1438 struct xfs_dir2_leaf_entry *ents;
1439 struct xfs_inode *dp = state->args->dp;
1442 * Check for the degenerate case of the block being over 50% full.
1443 * If so, it's not worth even looking to see if we might be able
1444 * to coalesce with a sibling.
1446 blk = &state->path.blk[state->path.active - 1];
1447 leaf = blk->bp->b_addr;
1448 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1449 ents = dp->d_ops->leaf_ents_p(leaf);
1450 xfs_dir3_leaf_check(dp, blk->bp);
1452 count = leafhdr.count - leafhdr.stale;
1453 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
1454 if (bytes > (state->args->geo->blksize >> 1)) {
1456 * Blk over 50%, don't try to join.
1462 * Check for the degenerate case of the block being empty.
1463 * If the block is empty, we'll simply delete it, no need to
1464 * coalesce it with a sibling block. We choose (arbitrarily)
1465 * to merge with the forward block unless it is NULL.
1469 * Make altpath point to the block we want to keep and
1470 * path point to the block we want to drop (this one).
1472 forward = (leafhdr.forw != 0);
1473 memcpy(&state->altpath, &state->path, sizeof(state->path));
1474 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1478 *action = rval ? 2 : 0;
1482 * Examine each sibling block to see if we can coalesce with
1483 * at least 25% free space to spare. We need to figure out
1484 * whether to merge with the forward or the backward block.
1485 * We prefer coalescing with the lower numbered sibling so as
1486 * to shrink a directory over time.
1488 forward = leafhdr.forw < leafhdr.back;
1489 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1490 struct xfs_dir3_icleaf_hdr hdr2;
1492 blkno = forward ? leafhdr.forw : leafhdr.back;
1496 * Read the sibling leaf block.
1498 error = xfs_dir3_leafn_read(state->args->trans, dp,
1504 * Count bytes in the two blocks combined.
1506 count = leafhdr.count - leafhdr.stale;
1507 bytes = state->args->geo->blksize -
1508 (state->args->geo->blksize >> 2);
1511 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
1512 ents = dp->d_ops->leaf_ents_p(leaf);
1513 count += hdr2.count - hdr2.stale;
1514 bytes -= count * sizeof(ents[0]);
1517 * Fits with at least 25% to spare.
1521 xfs_trans_brelse(state->args->trans, bp);
1524 * Didn't like either block, give up.
1532 * Make altpath point to the block we want to keep (the lower
1533 * numbered block) and path point to the block we want to drop.
1535 memcpy(&state->altpath, &state->path, sizeof(state->path));
1536 if (blkno < blk->blkno)
1537 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1540 error = xfs_da3_path_shift(state, &state->path, forward, 0,
1545 *action = rval ? 0 : 1;
1550 * Move all the leaf entries from drop_blk to save_blk.
1551 * This is done as part of a join operation.
1554 xfs_dir2_leafn_unbalance(
1555 xfs_da_state_t *state, /* cursor */
1556 xfs_da_state_blk_t *drop_blk, /* dead block */
1557 xfs_da_state_blk_t *save_blk) /* surviving block */
1559 xfs_da_args_t *args; /* operation arguments */
1560 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1561 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1562 struct xfs_dir3_icleaf_hdr savehdr;
1563 struct xfs_dir3_icleaf_hdr drophdr;
1564 struct xfs_dir2_leaf_entry *sents;
1565 struct xfs_dir2_leaf_entry *dents;
1566 struct xfs_inode *dp = state->args->dp;
1569 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1570 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1571 drop_leaf = drop_blk->bp->b_addr;
1572 save_leaf = save_blk->bp->b_addr;
1574 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1575 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1576 sents = dp->d_ops->leaf_ents_p(save_leaf);
1577 dents = dp->d_ops->leaf_ents_p(drop_leaf);
1580 * If there are any stale leaf entries, take this opportunity
1584 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1586 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1589 * Move the entries from drop to the appropriate end of save.
1591 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1592 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1593 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1594 save_blk->bp, &savehdr, sents, 0,
1597 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1598 save_blk->bp, &savehdr, sents,
1599 savehdr.count, drophdr.count);
1600 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1602 /* log the changes made when moving the entries */
1603 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1604 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
1605 xfs_dir3_leaf_log_header(args, save_blk->bp);
1606 xfs_dir3_leaf_log_header(args, drop_blk->bp);
1608 xfs_dir3_leaf_check(dp, save_blk->bp);
1609 xfs_dir3_leaf_check(dp, drop_blk->bp);
1613 * Top-level node form directory addname routine.
1616 xfs_dir2_node_addname(
1617 xfs_da_args_t *args) /* operation arguments */
1619 xfs_da_state_blk_t *blk; /* leaf block for insert */
1620 int error; /* error return value */
1621 int rval; /* sub-return value */
1622 xfs_da_state_t *state; /* btree cursor */
1624 trace_xfs_dir2_node_addname(args);
1627 * Allocate and initialize the state (btree cursor).
1629 state = xfs_da_state_alloc();
1631 state->mp = args->dp->i_mount;
1633 * Look up the name. We're not supposed to find it, but
1634 * this gives us the insertion point.
1636 error = xfs_da3_node_lookup_int(state, &rval);
1639 if (rval != -ENOENT) {
1643 * Add the data entry to a data block.
1644 * Extravalid is set to a freeblock found by lookup.
1646 rval = xfs_dir2_node_addname_int(args,
1647 state->extravalid ? &state->extrablk : NULL);
1651 blk = &state->path.blk[state->path.active - 1];
1652 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1654 * Add the new leaf entry.
1656 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1659 * It worked, fix the hash values up the btree.
1661 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1662 xfs_da3_fixhashpath(state, &state->path);
1665 * It didn't work, we need to split the leaf block.
1667 if (args->total == 0) {
1668 ASSERT(rval == -ENOSPC);
1672 * Split the leaf block and insert the new entry.
1674 rval = xfs_da3_split(state);
1677 xfs_da_state_free(state);
1682 * Add the data entry for a node-format directory name addition.
1683 * The leaf entry is added in xfs_dir2_leafn_add.
1684 * We may enter with a freespace block that the lookup found.
1686 static int /* error */
1687 xfs_dir2_node_addname_int(
1688 xfs_da_args_t *args, /* operation arguments */
1689 xfs_da_state_blk_t *fblk) /* optional freespace block */
1691 xfs_dir2_data_hdr_t *hdr; /* data block header */
1692 xfs_dir2_db_t dbno; /* data block number */
1693 struct xfs_buf *dbp; /* data block buffer */
1694 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1695 xfs_inode_t *dp; /* incore directory inode */
1696 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1697 int error; /* error return value */
1698 xfs_dir2_db_t fbno; /* freespace block number */
1699 struct xfs_buf *fbp; /* freespace buffer */
1700 int findex; /* freespace entry index */
1701 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1702 xfs_dir2_db_t ifbno; /* initial freespace block no */
1703 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1704 int length; /* length of the new entry */
1705 int logfree; /* need to log free entry */
1706 xfs_mount_t *mp; /* filesystem mount point */
1707 int needlog; /* need to log data header */
1708 int needscan; /* need to rescan data frees */
1709 __be16 *tagp; /* data entry tag pointer */
1710 xfs_trans_t *tp; /* transaction pointer */
1712 struct xfs_dir3_icfree_hdr freehdr;
1713 struct xfs_dir2_data_free *bf;
1714 xfs_dir2_data_aoff_t aoff;
1719 length = dp->d_ops->data_entsize(args->namelen);
1721 * If we came in with a freespace block that means that lookup
1722 * found an entry with our hash value. This is the freespace
1723 * block for that data entry.
1728 * Remember initial freespace block number.
1730 ifbno = fblk->blkno;
1732 findex = fblk->index;
1733 bests = dp->d_ops->free_bests_p(free);
1734 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1737 * This means the free entry showed that the data block had
1738 * space for our entry, so we remembered it.
1739 * Use that data block.
1742 ASSERT(findex < freehdr.nvalid);
1743 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1744 ASSERT(be16_to_cpu(bests[findex]) >= length);
1745 dbno = freehdr.firstdb + findex;
1748 * The data block looked at didn't have enough room.
1749 * We'll start at the beginning of the freespace entries.
1756 * Didn't come in with a freespace block, so no data block.
1764 * If we don't have a data block yet, we're going to scan the
1765 * freespace blocks looking for one. Figure out what the
1766 * highest freespace block number is.
1769 xfs_fileoff_t fo; /* freespace block number */
1771 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
1773 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1777 * While we haven't identified a data block, search the freeblock
1778 * data for a good data block. If we find a null freeblock entry,
1779 * indicating a hole in the data blocks, remember that.
1781 while (dbno == -1) {
1783 * If we don't have a freeblock in hand, get the next one.
1787 * Happens the first time through unless lookup gave
1788 * us a freespace block to start with.
1791 fbno = xfs_dir2_byte_to_db(args->geo,
1792 XFS_DIR2_FREE_OFFSET);
1794 * If it's ifbno we already looked at it.
1799 * If it's off the end we're done.
1801 if (fbno >= lastfbno)
1804 * Read the block. There can be holes in the
1805 * freespace blocks, so this might not succeed.
1806 * This should be really rare, so there's no reason
1809 error = xfs_dir2_free_try_read(tp, dp,
1810 xfs_dir2_db_to_da(args->geo, fbno),
1820 * Look at the current free entry. Is it good enough?
1822 * The bests initialisation should be where the bufer is read in
1823 * the above branch. But gcc is too stupid to realise that bests
1824 * and the freehdr are actually initialised if they are placed
1825 * there, so we have to do it here to avoid warnings. Blech.
1827 bests = dp->d_ops->free_bests_p(free);
1828 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1829 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1830 be16_to_cpu(bests[findex]) >= length)
1831 dbno = freehdr.firstdb + findex;
1834 * Are we done with the freeblock?
1836 if (++findex == freehdr.nvalid) {
1840 xfs_trans_brelse(tp, fbp);
1842 if (fblk && fblk->bp)
1848 * If we don't have a data block, we need to allocate one and make
1849 * the freespace entries refer to it.
1851 if (unlikely(dbno == -1)) {
1853 * Not allowed to allocate, return failure.
1855 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
1859 * Allocate and initialize the new data block.
1861 if (unlikely((error = xfs_dir2_grow_inode(args,
1862 XFS_DIR2_DATA_SPACE,
1864 (error = xfs_dir3_data_init(args, dbno, &dbp))))
1868 * If (somehow) we have a freespace block, get rid of it.
1871 xfs_trans_brelse(tp, fbp);
1872 if (fblk && fblk->bp)
1876 * Get the freespace block corresponding to the data block
1877 * that was just allocated.
1879 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
1880 error = xfs_dir2_free_try_read(tp, dp,
1881 xfs_dir2_db_to_da(args->geo, fbno),
1887 * If there wasn't a freespace block, the read will
1888 * return a NULL fbp. Allocate and initialize a new one.
1891 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1896 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
1898 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
1899 __func__, (unsigned long long)dp->i_ino,
1900 (long long)dp->d_ops->db_to_fdb(
1902 (long long)dbno, (long long)fbno,
1903 (unsigned long long)ifbno, lastfbno);
1906 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1908 (unsigned long long)fblk->blkno,
1912 xfs_alert(mp, " ... fblk is NULL");
1914 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1915 XFS_ERRLEVEL_LOW, mp);
1916 return -EFSCORRUPTED;
1920 * Get a buffer for the new block.
1922 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1926 bests = dp->d_ops->free_bests_p(free);
1927 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1930 * Remember the first slot as our empty slot.
1933 (fbno - xfs_dir2_byte_to_db(args->geo,
1934 XFS_DIR2_FREE_OFFSET)) *
1935 dp->d_ops->free_max_bests(args->geo);
1938 bests = dp->d_ops->free_bests_p(free);
1939 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1943 * Set the freespace block index from the data block number.
1945 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
1947 * If it's after the end of the current entries in the
1948 * freespace block, extend that table.
1950 if (findex >= freehdr.nvalid) {
1951 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
1952 freehdr.nvalid = findex + 1;
1954 * Tag new entry so nused will go up.
1956 bests[findex] = cpu_to_be16(NULLDATAOFF);
1959 * If this entry was for an empty data block
1960 * (this should always be true) then update the header.
1962 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1964 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1965 xfs_dir2_free_log_header(args, fbp);
1968 * Update the real value in the table.
1969 * We haven't allocated the data entry yet so this will
1973 bf = dp->d_ops->data_bestfree_p(hdr);
1974 bests[findex] = bf[0].length;
1978 * We had a data block so we don't have to make a new one.
1982 * If just checking, we succeeded.
1984 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
1988 * Read the data block in.
1990 error = xfs_dir3_data_read(tp, dp,
1991 xfs_dir2_db_to_da(args->geo, dbno),
1996 bf = dp->d_ops->data_bestfree_p(hdr);
1999 ASSERT(be16_to_cpu(bf[0].length) >= length);
2001 * Point to the existing unused space.
2003 dup = (xfs_dir2_data_unused_t *)
2004 ((char *)hdr + be16_to_cpu(bf[0].offset));
2005 needscan = needlog = 0;
2007 * Mark the first part of the unused space, inuse for us.
2009 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
2010 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
2011 &needlog, &needscan);
2013 xfs_trans_brelse(tp, dbp);
2017 * Fill in the new entry and log it.
2019 dep = (xfs_dir2_data_entry_t *)dup;
2020 dep->inumber = cpu_to_be64(args->inumber);
2021 dep->namelen = args->namelen;
2022 memcpy(dep->name, args->name, dep->namelen);
2023 dp->d_ops->data_put_ftype(dep, args->filetype);
2024 tagp = dp->d_ops->data_entry_tag_p(dep);
2025 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
2026 xfs_dir2_data_log_entry(args, dbp, dep);
2028 * Rescan the block for bestfree if needed.
2031 xfs_dir2_data_freescan(dp, hdr, &needlog);
2033 * Log the data block header if needed.
2036 xfs_dir2_data_log_header(args, dbp);
2038 * If the freespace entry is now wrong, update it.
2040 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
2041 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2042 bests[findex] = bf[0].length;
2046 * Log the freespace entry if needed.
2049 xfs_dir2_free_log_bests(args, fbp, findex, findex);
2051 * Return the data block and offset in args, then drop the data block.
2053 args->blkno = (xfs_dablk_t)dbno;
2054 args->index = be16_to_cpu(*tagp);
2059 * Lookup an entry in a node-format directory.
2060 * All the real work happens in xfs_da3_node_lookup_int.
2061 * The only real output is the inode number of the entry.
2064 xfs_dir2_node_lookup(
2065 xfs_da_args_t *args) /* operation arguments */
2067 int error; /* error return value */
2068 int i; /* btree level */
2069 int rval; /* operation return value */
2070 xfs_da_state_t *state; /* btree cursor */
2072 trace_xfs_dir2_node_lookup(args);
2075 * Allocate and initialize the btree cursor.
2077 state = xfs_da_state_alloc();
2079 state->mp = args->dp->i_mount;
2081 * Fill in the path to the entry in the cursor.
2083 error = xfs_da3_node_lookup_int(state, &rval);
2086 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2087 /* If a CI match, dup the actual name and return -EEXIST */
2088 xfs_dir2_data_entry_t *dep;
2090 dep = (xfs_dir2_data_entry_t *)
2091 ((char *)state->extrablk.bp->b_addr +
2092 state->extrablk.index);
2093 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2096 * Release the btree blocks and leaf block.
2098 for (i = 0; i < state->path.active; i++) {
2099 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2100 state->path.blk[i].bp = NULL;
2103 * Release the data block if we have it.
2105 if (state->extravalid && state->extrablk.bp) {
2106 xfs_trans_brelse(args->trans, state->extrablk.bp);
2107 state->extrablk.bp = NULL;
2109 xfs_da_state_free(state);
2114 * Remove an entry from a node-format directory.
2117 xfs_dir2_node_removename(
2118 struct xfs_da_args *args) /* operation arguments */
2120 struct xfs_da_state_blk *blk; /* leaf block */
2121 int error; /* error return value */
2122 int rval; /* operation return value */
2123 struct xfs_da_state *state; /* btree cursor */
2125 trace_xfs_dir2_node_removename(args);
2128 * Allocate and initialize the btree cursor.
2130 state = xfs_da_state_alloc();
2132 state->mp = args->dp->i_mount;
2134 /* Look up the entry we're deleting, set up the cursor. */
2135 error = xfs_da3_node_lookup_int(state, &rval);
2139 /* Didn't find it, upper layer screwed up. */
2140 if (rval != -EEXIST) {
2145 blk = &state->path.blk[state->path.active - 1];
2146 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2147 ASSERT(state->extravalid);
2149 * Remove the leaf and data entries.
2150 * Extrablk refers to the data block.
2152 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2153 &state->extrablk, &rval);
2157 * Fix the hash values up the btree.
2159 xfs_da3_fixhashpath(state, &state->path);
2161 * If we need to join leaf blocks, do it.
2163 if (rval && state->path.active > 1)
2164 error = xfs_da3_join(state);
2166 * If no errors so far, try conversion to leaf format.
2169 error = xfs_dir2_node_to_leaf(state);
2171 xfs_da_state_free(state);
2176 * Replace an entry's inode number in a node-format directory.
2179 xfs_dir2_node_replace(
2180 xfs_da_args_t *args) /* operation arguments */
2182 xfs_da_state_blk_t *blk; /* leaf block */
2183 xfs_dir2_data_hdr_t *hdr; /* data block header */
2184 xfs_dir2_data_entry_t *dep; /* data entry changed */
2185 int error; /* error return value */
2186 int i; /* btree level */
2187 xfs_ino_t inum; /* new inode number */
2188 int ftype; /* new file type */
2189 xfs_dir2_leaf_t *leaf; /* leaf structure */
2190 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2191 int rval; /* internal return value */
2192 xfs_da_state_t *state; /* btree cursor */
2194 trace_xfs_dir2_node_replace(args);
2197 * Allocate and initialize the btree cursor.
2199 state = xfs_da_state_alloc();
2201 state->mp = args->dp->i_mount;
2204 * We have to save new inode number and ftype since
2205 * xfs_da3_node_lookup_int() is going to overwrite them
2207 inum = args->inumber;
2208 ftype = args->filetype;
2211 * Lookup the entry to change in the btree.
2213 error = xfs_da3_node_lookup_int(state, &rval);
2218 * It should be found, since the vnodeops layer has looked it up
2219 * and locked it. But paranoia is good.
2221 if (rval == -EEXIST) {
2222 struct xfs_dir2_leaf_entry *ents;
2224 * Find the leaf entry.
2226 blk = &state->path.blk[state->path.active - 1];
2227 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2228 leaf = blk->bp->b_addr;
2229 ents = args->dp->d_ops->leaf_ents_p(leaf);
2230 lep = &ents[blk->index];
2231 ASSERT(state->extravalid);
2233 * Point to the data entry.
2235 hdr = state->extrablk.bp->b_addr;
2236 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2237 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2238 dep = (xfs_dir2_data_entry_t *)
2240 xfs_dir2_dataptr_to_off(args->geo,
2241 be32_to_cpu(lep->address)));
2242 ASSERT(inum != be64_to_cpu(dep->inumber));
2244 * Fill in the new inode number and log the entry.
2246 dep->inumber = cpu_to_be64(inum);
2247 args->dp->d_ops->data_put_ftype(dep, ftype);
2248 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2252 * Didn't find it, and we're holding a data block. Drop it.
2254 else if (state->extravalid) {
2255 xfs_trans_brelse(args->trans, state->extrablk.bp);
2256 state->extrablk.bp = NULL;
2259 * Release all the buffers in the cursor.
2261 for (i = 0; i < state->path.active; i++) {
2262 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2263 state->path.blk[i].bp = NULL;
2265 xfs_da_state_free(state);
2270 * Trim off a trailing empty freespace block.
2271 * Return (in rvalp) 1 if we did it, 0 if not.
2274 xfs_dir2_node_trim_free(
2275 xfs_da_args_t *args, /* operation arguments */
2276 xfs_fileoff_t fo, /* free block number */
2277 int *rvalp) /* out: did something */
2279 struct xfs_buf *bp; /* freespace buffer */
2280 xfs_inode_t *dp; /* incore directory inode */
2281 int error; /* error return code */
2282 xfs_dir2_free_t *free; /* freespace structure */
2283 xfs_trans_t *tp; /* transaction pointer */
2284 struct xfs_dir3_icfree_hdr freehdr;
2292 * Read the freespace block.
2294 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
2298 * There can be holes in freespace. If fo is a hole, there's
2304 dp->d_ops->free_hdr_from_disk(&freehdr, free);
2307 * If there are used entries, there's nothing to do.
2309 if (freehdr.nused > 0) {
2310 xfs_trans_brelse(tp, bp);
2314 * Blow the block away.
2316 error = xfs_dir2_shrink_inode(args,
2317 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2320 * Can't fail with ENOSPC since that only happens with no
2321 * space reservation, when breaking up an extent into two
2322 * pieces. This is the last block of an extent.
2324 ASSERT(error != -ENOSPC);
2325 xfs_trans_brelse(tp, bp);
2329 * Return that we succeeded.