2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_da_format.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_inode.h"
30 #include "xfs_dir2_priv.h"
31 #include "xfs_error.h"
32 #include "xfs_trace.h"
33 #include "xfs_trans.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_cksum.h"
38 * Function declarations.
40 static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
42 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
43 xfs_da_state_blk_t *blk1,
44 xfs_da_state_blk_t *blk2);
45 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
46 int index, xfs_da_state_blk_t *dblk,
48 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
49 xfs_da_state_blk_t *fblk);
52 * Check internal consistency of a leafn block.
55 #define xfs_dir3_leaf_check(dp, bp) \
57 if (!xfs_dir3_leafn_check((dp), (bp))) \
66 struct xfs_dir2_leaf *leaf = bp->b_addr;
67 struct xfs_dir3_icleaf_hdr leafhdr;
69 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
71 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
72 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
73 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
75 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
78 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
81 #define xfs_dir3_leaf_check(dp, bp)
88 struct xfs_mount *mp = bp->b_target->bt_mount;
89 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
91 if (xfs_sb_version_hascrc(&mp->m_sb)) {
92 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
94 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
96 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
98 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
101 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
105 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
111 xfs_dir3_free_read_verify(
114 struct xfs_mount *mp = bp->b_target->bt_mount;
116 if (xfs_sb_version_hascrc(&mp->m_sb) &&
117 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
118 xfs_buf_ioerror(bp, -EFSBADCRC);
119 else if (!xfs_dir3_free_verify(bp))
120 xfs_buf_ioerror(bp, -EFSCORRUPTED);
123 xfs_verifier_error(bp);
127 xfs_dir3_free_write_verify(
130 struct xfs_mount *mp = bp->b_target->bt_mount;
131 struct xfs_buf_log_item *bip = bp->b_fspriv;
132 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
134 if (!xfs_dir3_free_verify(bp)) {
135 xfs_buf_ioerror(bp, -EFSCORRUPTED);
136 xfs_verifier_error(bp);
140 if (!xfs_sb_version_hascrc(&mp->m_sb))
144 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
146 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
149 const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
150 .verify_read = xfs_dir3_free_read_verify,
151 .verify_write = xfs_dir3_free_write_verify,
156 __xfs_dir3_free_read(
157 struct xfs_trans *tp,
158 struct xfs_inode *dp,
160 xfs_daddr_t mappedbno,
161 struct xfs_buf **bpp)
165 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
166 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
168 /* try read returns without an error or *bpp if it lands in a hole */
169 if (!err && tp && *bpp)
170 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
176 struct xfs_trans *tp,
177 struct xfs_inode *dp,
179 struct xfs_buf **bpp)
181 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
185 xfs_dir2_free_try_read(
186 struct xfs_trans *tp,
187 struct xfs_inode *dp,
189 struct xfs_buf **bpp)
191 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
195 xfs_dir3_free_get_buf(
198 struct xfs_buf **bpp)
200 struct xfs_trans *tp = args->trans;
201 struct xfs_inode *dp = args->dp;
202 struct xfs_mount *mp = dp->i_mount;
205 struct xfs_dir3_icfree_hdr hdr;
207 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
208 -1, &bp, XFS_DATA_FORK);
212 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
213 bp->b_ops = &xfs_dir3_free_buf_ops;
216 * Initialize the new block to be empty, and remember
217 * its first slot as our empty slot.
219 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
220 memset(&hdr, 0, sizeof(hdr));
222 if (xfs_sb_version_hascrc(&mp->m_sb)) {
223 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
225 hdr.magic = XFS_DIR3_FREE_MAGIC;
227 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
228 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
229 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_uuid);
231 hdr.magic = XFS_DIR2_FREE_MAGIC;
232 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
238 * Log entries from a freespace block.
241 xfs_dir2_free_log_bests(
242 struct xfs_da_args *args,
244 int first, /* first entry to log */
245 int last) /* last entry to log */
247 xfs_dir2_free_t *free; /* freespace structure */
251 bests = args->dp->d_ops->free_bests_p(free);
252 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
253 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
254 xfs_trans_log_buf(args->trans, bp,
255 (uint)((char *)&bests[first] - (char *)free),
256 (uint)((char *)&bests[last] - (char *)free +
257 sizeof(bests[0]) - 1));
261 * Log header from a freespace block.
264 xfs_dir2_free_log_header(
265 struct xfs_da_args *args,
269 xfs_dir2_free_t *free; /* freespace structure */
272 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
273 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
275 xfs_trans_log_buf(args->trans, bp, 0,
276 args->dp->d_ops->free_hdr_size - 1);
280 * Convert a leaf-format directory to a node-format directory.
281 * We need to change the magic number of the leaf block, and copy
282 * the freespace table out of the leaf block into its own block.
285 xfs_dir2_leaf_to_node(
286 xfs_da_args_t *args, /* operation arguments */
287 struct xfs_buf *lbp) /* leaf buffer */
289 xfs_inode_t *dp; /* incore directory inode */
290 int error; /* error return value */
291 struct xfs_buf *fbp; /* freespace buffer */
292 xfs_dir2_db_t fdb; /* freespace block number */
293 xfs_dir2_free_t *free; /* freespace structure */
294 __be16 *from; /* pointer to freespace entry */
295 int i; /* leaf freespace index */
296 xfs_dir2_leaf_t *leaf; /* leaf structure */
297 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
298 int n; /* count of live freespc ents */
299 xfs_dir2_data_off_t off; /* freespace entry value */
300 __be16 *to; /* pointer to freespace entry */
301 xfs_trans_t *tp; /* transaction pointer */
302 struct xfs_dir3_icfree_hdr freehdr;
304 trace_xfs_dir2_leaf_to_node(args);
309 * Add a freespace block to the directory.
311 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
314 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
316 * Get the buffer for the new freespace block.
318 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
323 dp->d_ops->free_hdr_from_disk(&freehdr, free);
325 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
326 ASSERT(be32_to_cpu(ltp->bestcount) <=
327 (uint)dp->i_d.di_size / args->geo->blksize);
330 * Copy freespace entries from the leaf block to the new block.
331 * Count active entries.
333 from = xfs_dir2_leaf_bests_p(ltp);
334 to = dp->d_ops->free_bests_p(free);
335 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
336 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
338 *to = cpu_to_be16(off);
342 * Now initialize the freespace block header.
345 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
347 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
348 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
349 xfs_dir2_free_log_header(args, fbp);
352 * Converting the leaf to a leafnode is just a matter of changing the
353 * magic number and the ops. Do the change directly to the buffer as
354 * it's less work (and less code) than decoding the header to host
355 * format and back again.
357 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
358 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
360 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
361 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
362 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
363 xfs_dir3_leaf_log_header(args, lbp);
364 xfs_dir3_leaf_check(dp, lbp);
369 * Add a leaf entry to a leaf block in a node-form directory.
370 * The other work necessary is done from the caller.
372 static int /* error */
374 struct xfs_buf *bp, /* leaf buffer */
375 xfs_da_args_t *args, /* operation arguments */
376 int index) /* insertion pt for new entry */
378 int compact; /* compacting stale leaves */
379 xfs_inode_t *dp; /* incore directory inode */
380 int highstale; /* next stale entry */
381 xfs_dir2_leaf_t *leaf; /* leaf structure */
382 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
383 int lfloghigh; /* high leaf entry logging */
384 int lfloglow; /* low leaf entry logging */
385 int lowstale; /* previous stale entry */
386 struct xfs_dir3_icleaf_hdr leafhdr;
387 struct xfs_dir2_leaf_entry *ents;
389 trace_xfs_dir2_leafn_add(args, index);
393 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
394 ents = dp->d_ops->leaf_ents_p(leaf);
397 * Quick check just to make sure we are not going to index
398 * into other peoples memory
401 return -EFSCORRUPTED;
404 * If there are already the maximum number of leaf entries in
405 * the block, if there are no stale entries it won't fit.
406 * Caller will do a split. If there are stale entries we'll do
410 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
413 compact = leafhdr.stale > 1;
416 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
417 ASSERT(index == leafhdr.count ||
418 be32_to_cpu(ents[index].hashval) >= args->hashval);
420 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
424 * Compact out all but one stale leaf entry. Leaves behind
425 * the entry closest to index.
428 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
429 &highstale, &lfloglow, &lfloghigh);
430 else if (leafhdr.stale) {
432 * Set impossible logging indices for this case.
434 lfloglow = leafhdr.count;
439 * Insert the new entry, log everything.
441 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
442 highstale, &lfloglow, &lfloghigh);
444 lep->hashval = cpu_to_be32(args->hashval);
445 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
446 args->blkno, args->index));
448 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
449 xfs_dir3_leaf_log_header(args, bp);
450 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
451 xfs_dir3_leaf_check(dp, bp);
457 xfs_dir2_free_hdr_check(
458 struct xfs_inode *dp,
462 struct xfs_dir3_icfree_hdr hdr;
464 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
466 ASSERT((hdr.firstdb %
467 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
468 ASSERT(hdr.firstdb <= db);
469 ASSERT(db < hdr.firstdb + hdr.nvalid);
472 #define xfs_dir2_free_hdr_check(dp, bp, db)
476 * Return the last hash value in the leaf.
477 * Stale entries are ok.
479 xfs_dahash_t /* hash value */
480 xfs_dir2_leafn_lasthash(
481 struct xfs_inode *dp,
482 struct xfs_buf *bp, /* leaf buffer */
483 int *count) /* count of entries in leaf */
485 struct xfs_dir2_leaf *leaf = bp->b_addr;
486 struct xfs_dir2_leaf_entry *ents;
487 struct xfs_dir3_icleaf_hdr leafhdr;
489 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
491 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
492 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
495 *count = leafhdr.count;
499 ents = dp->d_ops->leaf_ents_p(leaf);
500 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
504 * Look up a leaf entry for space to add a name in a node-format leaf block.
505 * The extrablk in state is a freespace block.
508 xfs_dir2_leafn_lookup_for_addname(
509 struct xfs_buf *bp, /* leaf buffer */
510 xfs_da_args_t *args, /* operation arguments */
511 int *indexp, /* out: leaf entry index */
512 xfs_da_state_t *state) /* state to fill in */
514 struct xfs_buf *curbp = NULL; /* current data/free buffer */
515 xfs_dir2_db_t curdb = -1; /* current data block number */
516 xfs_dir2_db_t curfdb = -1; /* current free block number */
517 xfs_inode_t *dp; /* incore directory inode */
518 int error; /* error return value */
519 int fi; /* free entry index */
520 xfs_dir2_free_t *free = NULL; /* free block structure */
521 int index; /* leaf entry index */
522 xfs_dir2_leaf_t *leaf; /* leaf structure */
523 int length; /* length of new data entry */
524 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
525 xfs_mount_t *mp; /* filesystem mount point */
526 xfs_dir2_db_t newdb; /* new data block number */
527 xfs_dir2_db_t newfdb; /* new free block number */
528 xfs_trans_t *tp; /* transaction pointer */
529 struct xfs_dir2_leaf_entry *ents;
530 struct xfs_dir3_icleaf_hdr leafhdr;
536 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
537 ents = dp->d_ops->leaf_ents_p(leaf);
539 xfs_dir3_leaf_check(dp, bp);
540 ASSERT(leafhdr.count > 0);
543 * Look up the hash value in the leaf entries.
545 index = xfs_dir2_leaf_search_hash(args, bp);
547 * Do we have a buffer coming in?
549 if (state->extravalid) {
550 /* If so, it's a free block buffer, get the block number. */
551 curbp = state->extrablk.bp;
552 curfdb = state->extrablk.blkno;
553 free = curbp->b_addr;
554 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
555 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
557 length = dp->d_ops->data_entsize(args->namelen);
559 * Loop over leaf entries with the right hash value.
561 for (lep = &ents[index];
562 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
565 * Skip stale leaf entries.
567 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
570 * Pull the data block number from the entry.
572 newdb = xfs_dir2_dataptr_to_db(args->geo,
573 be32_to_cpu(lep->address));
575 * For addname, we're looking for a place to put the new entry.
576 * We want to use a data block with an entry of equal
577 * hash value to ours if there is one with room.
579 * If this block isn't the data block we already have
580 * in hand, take a look at it.
582 if (newdb != curdb) {
587 * Convert the data block to the free block
588 * holding its freespace information.
590 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
592 * If it's not the one we have in hand, read it in.
594 if (newfdb != curfdb) {
596 * If we had one before, drop it.
599 xfs_trans_brelse(tp, curbp);
601 error = xfs_dir2_free_read(tp, dp,
602 xfs_dir2_db_to_da(args->geo,
607 free = curbp->b_addr;
609 xfs_dir2_free_hdr_check(dp, curbp, curdb);
612 * Get the index for our entry.
614 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
616 * If it has room, return it.
618 bests = dp->d_ops->free_bests_p(free);
619 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
620 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
621 XFS_ERRLEVEL_LOW, mp);
622 if (curfdb != newfdb)
623 xfs_trans_brelse(tp, curbp);
624 return -EFSCORRUPTED;
627 if (be16_to_cpu(bests[fi]) >= length)
631 /* Didn't find any space */
634 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
636 /* Giving back a free block. */
637 state->extravalid = 1;
638 state->extrablk.bp = curbp;
639 state->extrablk.index = fi;
640 state->extrablk.blkno = curfdb;
643 * Important: this magic number is not in the buffer - it's for
644 * buffer type information and therefore only the free/data type
645 * matters here, not whether CRCs are enabled or not.
647 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
649 state->extravalid = 0;
652 * Return the index, that will be the insertion point.
659 * Look up a leaf entry in a node-format leaf block.
660 * The extrablk in state a data block.
663 xfs_dir2_leafn_lookup_for_entry(
664 struct xfs_buf *bp, /* leaf buffer */
665 xfs_da_args_t *args, /* operation arguments */
666 int *indexp, /* out: leaf entry index */
667 xfs_da_state_t *state) /* state to fill in */
669 struct xfs_buf *curbp = NULL; /* current data/free buffer */
670 xfs_dir2_db_t curdb = -1; /* current data block number */
671 xfs_dir2_data_entry_t *dep; /* data block entry */
672 xfs_inode_t *dp; /* incore directory inode */
673 int error; /* error return value */
674 int index; /* leaf entry index */
675 xfs_dir2_leaf_t *leaf; /* leaf structure */
676 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
677 xfs_mount_t *mp; /* filesystem mount point */
678 xfs_dir2_db_t newdb; /* new data block number */
679 xfs_trans_t *tp; /* transaction pointer */
680 enum xfs_dacmp cmp; /* comparison result */
681 struct xfs_dir2_leaf_entry *ents;
682 struct xfs_dir3_icleaf_hdr leafhdr;
688 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
689 ents = dp->d_ops->leaf_ents_p(leaf);
691 xfs_dir3_leaf_check(dp, bp);
692 ASSERT(leafhdr.count > 0);
695 * Look up the hash value in the leaf entries.
697 index = xfs_dir2_leaf_search_hash(args, bp);
699 * Do we have a buffer coming in?
701 if (state->extravalid) {
702 curbp = state->extrablk.bp;
703 curdb = state->extrablk.blkno;
706 * Loop over leaf entries with the right hash value.
708 for (lep = &ents[index];
709 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
712 * Skip stale leaf entries.
714 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
717 * Pull the data block number from the entry.
719 newdb = xfs_dir2_dataptr_to_db(args->geo,
720 be32_to_cpu(lep->address));
722 * Not adding a new entry, so we really want to find
723 * the name given to us.
725 * If it's a different data block, go get it.
727 if (newdb != curdb) {
729 * If we had a block before that we aren't saving
730 * for a CI name, drop it
732 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
733 curdb != state->extrablk.blkno))
734 xfs_trans_brelse(tp, curbp);
736 * If needing the block that is saved with a CI match,
737 * use it otherwise read in the new data block.
739 if (args->cmpresult != XFS_CMP_DIFFERENT &&
740 newdb == state->extrablk.blkno) {
741 ASSERT(state->extravalid);
742 curbp = state->extrablk.bp;
744 error = xfs_dir3_data_read(tp, dp,
745 xfs_dir2_db_to_da(args->geo,
751 xfs_dir3_data_check(dp, curbp);
755 * Point to the data entry.
757 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
758 xfs_dir2_dataptr_to_off(args->geo,
759 be32_to_cpu(lep->address)));
761 * Compare the entry and if it's an exact match, return
762 * EEXIST immediately. If it's the first case-insensitive
763 * match, store the block & inode number and continue looking.
765 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
766 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
767 /* If there is a CI match block, drop it */
768 if (args->cmpresult != XFS_CMP_DIFFERENT &&
769 curdb != state->extrablk.blkno)
770 xfs_trans_brelse(tp, state->extrablk.bp);
771 args->cmpresult = cmp;
772 args->inumber = be64_to_cpu(dep->inumber);
773 args->filetype = dp->d_ops->data_get_ftype(dep);
775 state->extravalid = 1;
776 state->extrablk.bp = curbp;
777 state->extrablk.blkno = curdb;
778 state->extrablk.index = (int)((char *)dep -
779 (char *)curbp->b_addr);
780 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
781 curbp->b_ops = &xfs_dir3_data_buf_ops;
782 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
783 if (cmp == XFS_CMP_EXACT)
787 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
789 if (args->cmpresult == XFS_CMP_DIFFERENT) {
790 /* Giving back last used data block. */
791 state->extravalid = 1;
792 state->extrablk.bp = curbp;
793 state->extrablk.index = -1;
794 state->extrablk.blkno = curdb;
795 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
796 curbp->b_ops = &xfs_dir3_data_buf_ops;
797 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
799 /* If the curbp is not the CI match block, drop it */
800 if (state->extrablk.bp != curbp)
801 xfs_trans_brelse(tp, curbp);
804 state->extravalid = 0;
811 * Look up a leaf entry in a node-format leaf block.
812 * If this is an addname then the extrablk in state is a freespace block,
813 * otherwise it's a data block.
816 xfs_dir2_leafn_lookup_int(
817 struct xfs_buf *bp, /* leaf buffer */
818 xfs_da_args_t *args, /* operation arguments */
819 int *indexp, /* out: leaf entry index */
820 xfs_da_state_t *state) /* state to fill in */
822 if (args->op_flags & XFS_DA_OP_ADDNAME)
823 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
825 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
829 * Move count leaf entries from source to destination leaf.
830 * Log entries and headers. Stale entries are preserved.
833 xfs_dir3_leafn_moveents(
834 xfs_da_args_t *args, /* operation arguments */
835 struct xfs_buf *bp_s, /* source */
836 struct xfs_dir3_icleaf_hdr *shdr,
837 struct xfs_dir2_leaf_entry *sents,
838 int start_s,/* source leaf index */
839 struct xfs_buf *bp_d, /* destination */
840 struct xfs_dir3_icleaf_hdr *dhdr,
841 struct xfs_dir2_leaf_entry *dents,
842 int start_d,/* destination leaf index */
843 int count) /* count of leaves to copy */
845 int stale; /* count stale leaves copied */
847 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
850 * Silently return if nothing to do.
856 * If the destination index is not the end of the current
857 * destination leaf entries, open up a hole in the destination
858 * to hold the new entries.
860 if (start_d < dhdr->count) {
861 memmove(&dents[start_d + count], &dents[start_d],
862 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
863 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
864 count + dhdr->count - 1);
867 * If the source has stale leaves, count the ones in the copy range
868 * so we can update the header correctly.
871 int i; /* temp leaf index */
873 for (i = start_s, stale = 0; i < start_s + count; i++) {
874 if (sents[i].address ==
875 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
881 * Copy the leaf entries from source to destination.
883 memcpy(&dents[start_d], &sents[start_s],
884 count * sizeof(xfs_dir2_leaf_entry_t));
885 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
888 * If there are source entries after the ones we copied,
889 * delete the ones we copied by sliding the next ones down.
891 if (start_s + count < shdr->count) {
892 memmove(&sents[start_s], &sents[start_s + count],
893 count * sizeof(xfs_dir2_leaf_entry_t));
894 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
898 * Update the headers and log them.
900 shdr->count -= count;
901 shdr->stale -= stale;
902 dhdr->count += count;
903 dhdr->stale += stale;
907 * Determine the sort order of two leaf blocks.
908 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
911 xfs_dir2_leafn_order(
912 struct xfs_inode *dp,
913 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
914 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
916 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
917 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
918 struct xfs_dir2_leaf_entry *ents1;
919 struct xfs_dir2_leaf_entry *ents2;
920 struct xfs_dir3_icleaf_hdr hdr1;
921 struct xfs_dir3_icleaf_hdr hdr2;
923 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
924 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
925 ents1 = dp->d_ops->leaf_ents_p(leaf1);
926 ents2 = dp->d_ops->leaf_ents_p(leaf2);
928 if (hdr1.count > 0 && hdr2.count > 0 &&
929 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
930 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
931 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
937 * Rebalance leaf entries between two leaf blocks.
938 * This is actually only called when the second block is new,
939 * though the code deals with the general case.
940 * A new entry will be inserted in one of the blocks, and that
941 * entry is taken into account when balancing.
944 xfs_dir2_leafn_rebalance(
945 xfs_da_state_t *state, /* btree cursor */
946 xfs_da_state_blk_t *blk1, /* first btree block */
947 xfs_da_state_blk_t *blk2) /* second btree block */
949 xfs_da_args_t *args; /* operation arguments */
950 int count; /* count (& direction) leaves */
951 int isleft; /* new goes in left leaf */
952 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
953 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
954 int mid; /* midpoint leaf index */
955 #if defined(DEBUG) || defined(XFS_WARN)
956 int oldstale; /* old count of stale leaves */
958 int oldsum; /* old total leaf count */
959 int swap; /* swapped leaf blocks */
960 struct xfs_dir2_leaf_entry *ents1;
961 struct xfs_dir2_leaf_entry *ents2;
962 struct xfs_dir3_icleaf_hdr hdr1;
963 struct xfs_dir3_icleaf_hdr hdr2;
964 struct xfs_inode *dp = state->args->dp;
968 * If the block order is wrong, swap the arguments.
970 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
971 xfs_da_state_blk_t *tmp; /* temp for block swap */
977 leaf1 = blk1->bp->b_addr;
978 leaf2 = blk2->bp->b_addr;
979 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
980 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
981 ents1 = dp->d_ops->leaf_ents_p(leaf1);
982 ents2 = dp->d_ops->leaf_ents_p(leaf2);
984 oldsum = hdr1.count + hdr2.count;
985 #if defined(DEBUG) || defined(XFS_WARN)
986 oldstale = hdr1.stale + hdr2.stale;
991 * If the old leaf count was odd then the new one will be even,
992 * so we need to divide the new count evenly.
995 xfs_dahash_t midhash; /* middle entry hash value */
997 if (mid >= hdr1.count)
998 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
1000 midhash = be32_to_cpu(ents1[mid].hashval);
1001 isleft = args->hashval <= midhash;
1004 * If the old count is even then the new count is odd, so there's
1005 * no preferred side for the new entry.
1006 * Pick the left one.
1011 * Calculate moved entry count. Positive means left-to-right,
1012 * negative means right-to-left. Then move the entries.
1014 count = hdr1.count - mid + (isleft == 0);
1016 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1017 hdr1.count - count, blk2->bp,
1018 &hdr2, ents2, 0, count);
1020 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1021 blk1->bp, &hdr1, ents1,
1024 ASSERT(hdr1.count + hdr2.count == oldsum);
1025 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1027 /* log the changes made when moving the entries */
1028 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1029 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
1030 xfs_dir3_leaf_log_header(args, blk1->bp);
1031 xfs_dir3_leaf_log_header(args, blk2->bp);
1033 xfs_dir3_leaf_check(dp, blk1->bp);
1034 xfs_dir3_leaf_check(dp, blk2->bp);
1037 * Mark whether we're inserting into the old or new leaf.
1039 if (hdr1.count < hdr2.count)
1040 state->inleaf = swap;
1041 else if (hdr1.count > hdr2.count)
1042 state->inleaf = !swap;
1044 state->inleaf = swap ^ (blk1->index <= hdr1.count);
1046 * Adjust the expected index for insertion.
1049 blk2->index = blk1->index - hdr1.count;
1052 * Finally sanity check just to make sure we are not returning a
1055 if (blk2->index < 0) {
1058 xfs_alert(dp->i_mount,
1059 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1060 __func__, blk1->index);
1065 xfs_dir3_data_block_free(
1066 xfs_da_args_t *args,
1067 struct xfs_dir2_data_hdr *hdr,
1068 struct xfs_dir2_free *free,
1071 struct xfs_buf *fbp,
1076 struct xfs_dir3_icfree_hdr freehdr;
1077 struct xfs_inode *dp = args->dp;
1079 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1080 bests = dp->d_ops->free_bests_p(free);
1083 * Data block is not empty, just set the free entry to the new
1086 bests[findex] = cpu_to_be16(longest);
1087 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1091 /* One less used entry in the free table. */
1095 * If this was the last entry in the table, we can trim the table size
1096 * back. There might be other entries at the end referring to
1097 * non-existent data blocks, get those too.
1099 if (findex == freehdr.nvalid - 1) {
1100 int i; /* free entry index */
1102 for (i = findex - 1; i >= 0; i--) {
1103 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1106 freehdr.nvalid = i + 1;
1109 /* Not the last entry, just punch it out. */
1110 bests[findex] = cpu_to_be16(NULLDATAOFF);
1114 dp->d_ops->free_hdr_to_disk(free, &freehdr);
1115 xfs_dir2_free_log_header(args, fbp);
1118 * If there are no useful entries left in the block, get rid of the
1121 if (!freehdr.nused) {
1124 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1128 } else if (error != -ENOSPC || args->total != 0)
1131 * It's possible to get ENOSPC if there is no
1132 * space reservation. In this case some one
1133 * else will eventually get rid of this block.
1137 /* Log the free entry that changed, unless we got rid of it. */
1139 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1144 * Remove an entry from a node directory.
1145 * This removes the leaf entry and the data entry,
1146 * and updates the free block if necessary.
1148 static int /* error */
1149 xfs_dir2_leafn_remove(
1150 xfs_da_args_t *args, /* operation arguments */
1151 struct xfs_buf *bp, /* leaf buffer */
1152 int index, /* leaf entry index */
1153 xfs_da_state_blk_t *dblk, /* data block */
1154 int *rval) /* resulting block needs join */
1156 xfs_dir2_data_hdr_t *hdr; /* data block header */
1157 xfs_dir2_db_t db; /* data block number */
1158 struct xfs_buf *dbp; /* data block buffer */
1159 xfs_dir2_data_entry_t *dep; /* data block entry */
1160 xfs_inode_t *dp; /* incore directory inode */
1161 xfs_dir2_leaf_t *leaf; /* leaf structure */
1162 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1163 int longest; /* longest data free entry */
1164 int off; /* data block entry offset */
1165 int needlog; /* need to log data header */
1166 int needscan; /* need to rescan data frees */
1167 xfs_trans_t *tp; /* transaction pointer */
1168 struct xfs_dir2_data_free *bf; /* bestfree table */
1169 struct xfs_dir3_icleaf_hdr leafhdr;
1170 struct xfs_dir2_leaf_entry *ents;
1172 trace_xfs_dir2_leafn_remove(args, index);
1177 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1178 ents = dp->d_ops->leaf_ents_p(leaf);
1181 * Point to the entry we're removing.
1186 * Extract the data block and offset from the entry.
1188 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1189 ASSERT(dblk->blkno == db);
1190 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
1191 ASSERT(dblk->index == off);
1194 * Kill the leaf entry by marking it stale.
1195 * Log the leaf block changes.
1198 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1199 xfs_dir3_leaf_log_header(args, bp);
1201 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1202 xfs_dir3_leaf_log_ents(args, bp, index, index);
1205 * Make the data entry free. Keep track of the longest freespace
1206 * in the data block in case it changes.
1210 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1211 bf = dp->d_ops->data_bestfree_p(hdr);
1212 longest = be16_to_cpu(bf[0].length);
1213 needlog = needscan = 0;
1214 xfs_dir2_data_make_free(args, dbp, off,
1215 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1217 * Rescan the data block freespaces for bestfree.
1218 * Log the data block header if needed.
1221 xfs_dir2_data_freescan(dp, hdr, &needlog);
1223 xfs_dir2_data_log_header(args, dbp);
1224 xfs_dir3_data_check(dp, dbp);
1226 * If the longest data block freespace changes, need to update
1227 * the corresponding freeblock entry.
1229 if (longest < be16_to_cpu(bf[0].length)) {
1230 int error; /* error return value */
1231 struct xfs_buf *fbp; /* freeblock buffer */
1232 xfs_dir2_db_t fdb; /* freeblock block number */
1233 int findex; /* index in freeblock entries */
1234 xfs_dir2_free_t *free; /* freeblock structure */
1237 * Convert the data block number to a free block,
1238 * read in the free block.
1240 fdb = dp->d_ops->db_to_fdb(args->geo, db);
1241 error = xfs_dir2_free_read(tp, dp,
1242 xfs_dir2_db_to_da(args->geo, fdb),
1249 struct xfs_dir3_icfree_hdr freehdr;
1250 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1251 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
1252 (fdb - xfs_dir2_byte_to_db(args->geo,
1253 XFS_DIR2_FREE_OFFSET)));
1257 * Calculate which entry we need to fix.
1259 findex = dp->d_ops->db_to_fdindex(args->geo, db);
1260 longest = be16_to_cpu(bf[0].length);
1262 * If the data block is now empty we can get rid of it
1265 if (longest == args->geo->blksize -
1266 dp->d_ops->data_entry_offset) {
1268 * Try to punch out the data block.
1270 error = xfs_dir2_shrink_inode(args, db, dbp);
1276 * We can get ENOSPC if there's no space reservation.
1277 * In this case just drop the buffer and some one else
1278 * will eventually get rid of the empty block.
1280 else if (!(error == -ENOSPC && args->total == 0))
1284 * If we got rid of the data block, we can eliminate that entry
1285 * in the free block.
1287 error = xfs_dir3_data_block_free(args, hdr, free,
1288 fdb, findex, fbp, longest);
1293 xfs_dir3_leaf_check(dp, bp);
1295 * Return indication of whether this leaf block is empty enough
1296 * to justify trying to join it with a neighbor.
1298 *rval = (dp->d_ops->leaf_hdr_size +
1299 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
1300 args->geo->magicpct;
1305 * Split the leaf entries in the old block into old and new blocks.
1308 xfs_dir2_leafn_split(
1309 xfs_da_state_t *state, /* btree cursor */
1310 xfs_da_state_blk_t *oldblk, /* original block */
1311 xfs_da_state_blk_t *newblk) /* newly created block */
1313 xfs_da_args_t *args; /* operation arguments */
1314 xfs_dablk_t blkno; /* new leaf block number */
1315 int error; /* error return value */
1316 struct xfs_inode *dp;
1319 * Allocate space for a new leaf node.
1323 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1324 error = xfs_da_grow_inode(args, &blkno);
1329 * Initialize the new leaf block.
1331 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1332 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1336 newblk->blkno = blkno;
1337 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1339 * Rebalance the entries across the two leaves, link the new
1340 * block into the leaves.
1342 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1343 error = xfs_da3_blk_link(state, oldblk, newblk);
1348 * Insert the new entry in the correct block.
1351 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1353 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1355 * Update last hashval in each block since we added the name.
1357 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1358 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1359 xfs_dir3_leaf_check(dp, oldblk->bp);
1360 xfs_dir3_leaf_check(dp, newblk->bp);
1365 * Check a leaf block and its neighbors to see if the block should be
1366 * collapsed into one or the other neighbor. Always keep the block
1367 * with the smaller block number.
1368 * If the current block is over 50% full, don't try to join it, return 0.
1369 * If the block is empty, fill in the state structure and return 2.
1370 * If it can be collapsed, fill in the state structure and return 1.
1371 * If nothing can be done, return 0.
1374 xfs_dir2_leafn_toosmall(
1375 xfs_da_state_t *state, /* btree cursor */
1376 int *action) /* resulting action to take */
1378 xfs_da_state_blk_t *blk; /* leaf block */
1379 xfs_dablk_t blkno; /* leaf block number */
1380 struct xfs_buf *bp; /* leaf buffer */
1381 int bytes; /* bytes in use */
1382 int count; /* leaf live entry count */
1383 int error; /* error return value */
1384 int forward; /* sibling block direction */
1385 int i; /* sibling counter */
1386 xfs_dir2_leaf_t *leaf; /* leaf structure */
1387 int rval; /* result from path_shift */
1388 struct xfs_dir3_icleaf_hdr leafhdr;
1389 struct xfs_dir2_leaf_entry *ents;
1390 struct xfs_inode *dp = state->args->dp;
1393 * Check for the degenerate case of the block being over 50% full.
1394 * If so, it's not worth even looking to see if we might be able
1395 * to coalesce with a sibling.
1397 blk = &state->path.blk[state->path.active - 1];
1398 leaf = blk->bp->b_addr;
1399 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1400 ents = dp->d_ops->leaf_ents_p(leaf);
1401 xfs_dir3_leaf_check(dp, blk->bp);
1403 count = leafhdr.count - leafhdr.stale;
1404 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
1405 if (bytes > (state->args->geo->blksize >> 1)) {
1407 * Blk over 50%, don't try to join.
1413 * Check for the degenerate case of the block being empty.
1414 * If the block is empty, we'll simply delete it, no need to
1415 * coalesce it with a sibling block. We choose (arbitrarily)
1416 * to merge with the forward block unless it is NULL.
1420 * Make altpath point to the block we want to keep and
1421 * path point to the block we want to drop (this one).
1423 forward = (leafhdr.forw != 0);
1424 memcpy(&state->altpath, &state->path, sizeof(state->path));
1425 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1429 *action = rval ? 2 : 0;
1433 * Examine each sibling block to see if we can coalesce with
1434 * at least 25% free space to spare. We need to figure out
1435 * whether to merge with the forward or the backward block.
1436 * We prefer coalescing with the lower numbered sibling so as
1437 * to shrink a directory over time.
1439 forward = leafhdr.forw < leafhdr.back;
1440 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1441 struct xfs_dir3_icleaf_hdr hdr2;
1443 blkno = forward ? leafhdr.forw : leafhdr.back;
1447 * Read the sibling leaf block.
1449 error = xfs_dir3_leafn_read(state->args->trans, dp,
1455 * Count bytes in the two blocks combined.
1457 count = leafhdr.count - leafhdr.stale;
1458 bytes = state->args->geo->blksize -
1459 (state->args->geo->blksize >> 2);
1462 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
1463 ents = dp->d_ops->leaf_ents_p(leaf);
1464 count += hdr2.count - hdr2.stale;
1465 bytes -= count * sizeof(ents[0]);
1468 * Fits with at least 25% to spare.
1472 xfs_trans_brelse(state->args->trans, bp);
1475 * Didn't like either block, give up.
1483 * Make altpath point to the block we want to keep (the lower
1484 * numbered block) and path point to the block we want to drop.
1486 memcpy(&state->altpath, &state->path, sizeof(state->path));
1487 if (blkno < blk->blkno)
1488 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1491 error = xfs_da3_path_shift(state, &state->path, forward, 0,
1496 *action = rval ? 0 : 1;
1501 * Move all the leaf entries from drop_blk to save_blk.
1502 * This is done as part of a join operation.
1505 xfs_dir2_leafn_unbalance(
1506 xfs_da_state_t *state, /* cursor */
1507 xfs_da_state_blk_t *drop_blk, /* dead block */
1508 xfs_da_state_blk_t *save_blk) /* surviving block */
1510 xfs_da_args_t *args; /* operation arguments */
1511 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1512 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1513 struct xfs_dir3_icleaf_hdr savehdr;
1514 struct xfs_dir3_icleaf_hdr drophdr;
1515 struct xfs_dir2_leaf_entry *sents;
1516 struct xfs_dir2_leaf_entry *dents;
1517 struct xfs_inode *dp = state->args->dp;
1520 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1521 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1522 drop_leaf = drop_blk->bp->b_addr;
1523 save_leaf = save_blk->bp->b_addr;
1525 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1526 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1527 sents = dp->d_ops->leaf_ents_p(save_leaf);
1528 dents = dp->d_ops->leaf_ents_p(drop_leaf);
1531 * If there are any stale leaf entries, take this opportunity
1535 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1537 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1540 * Move the entries from drop to the appropriate end of save.
1542 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1543 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1544 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1545 save_blk->bp, &savehdr, sents, 0,
1548 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1549 save_blk->bp, &savehdr, sents,
1550 savehdr.count, drophdr.count);
1551 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1553 /* log the changes made when moving the entries */
1554 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1555 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
1556 xfs_dir3_leaf_log_header(args, save_blk->bp);
1557 xfs_dir3_leaf_log_header(args, drop_blk->bp);
1559 xfs_dir3_leaf_check(dp, save_blk->bp);
1560 xfs_dir3_leaf_check(dp, drop_blk->bp);
1564 * Top-level node form directory addname routine.
1567 xfs_dir2_node_addname(
1568 xfs_da_args_t *args) /* operation arguments */
1570 xfs_da_state_blk_t *blk; /* leaf block for insert */
1571 int error; /* error return value */
1572 int rval; /* sub-return value */
1573 xfs_da_state_t *state; /* btree cursor */
1575 trace_xfs_dir2_node_addname(args);
1578 * Allocate and initialize the state (btree cursor).
1580 state = xfs_da_state_alloc();
1582 state->mp = args->dp->i_mount;
1584 * Look up the name. We're not supposed to find it, but
1585 * this gives us the insertion point.
1587 error = xfs_da3_node_lookup_int(state, &rval);
1590 if (rval != -ENOENT) {
1594 * Add the data entry to a data block.
1595 * Extravalid is set to a freeblock found by lookup.
1597 rval = xfs_dir2_node_addname_int(args,
1598 state->extravalid ? &state->extrablk : NULL);
1602 blk = &state->path.blk[state->path.active - 1];
1603 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1605 * Add the new leaf entry.
1607 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1610 * It worked, fix the hash values up the btree.
1612 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1613 xfs_da3_fixhashpath(state, &state->path);
1616 * It didn't work, we need to split the leaf block.
1618 if (args->total == 0) {
1619 ASSERT(rval == -ENOSPC);
1623 * Split the leaf block and insert the new entry.
1625 rval = xfs_da3_split(state);
1628 xfs_da_state_free(state);
1633 * Add the data entry for a node-format directory name addition.
1634 * The leaf entry is added in xfs_dir2_leafn_add.
1635 * We may enter with a freespace block that the lookup found.
1637 static int /* error */
1638 xfs_dir2_node_addname_int(
1639 xfs_da_args_t *args, /* operation arguments */
1640 xfs_da_state_blk_t *fblk) /* optional freespace block */
1642 xfs_dir2_data_hdr_t *hdr; /* data block header */
1643 xfs_dir2_db_t dbno; /* data block number */
1644 struct xfs_buf *dbp; /* data block buffer */
1645 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1646 xfs_inode_t *dp; /* incore directory inode */
1647 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1648 int error; /* error return value */
1649 xfs_dir2_db_t fbno; /* freespace block number */
1650 struct xfs_buf *fbp; /* freespace buffer */
1651 int findex; /* freespace entry index */
1652 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1653 xfs_dir2_db_t ifbno; /* initial freespace block no */
1654 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1655 int length; /* length of the new entry */
1656 int logfree; /* need to log free entry */
1657 xfs_mount_t *mp; /* filesystem mount point */
1658 int needlog; /* need to log data header */
1659 int needscan; /* need to rescan data frees */
1660 __be16 *tagp; /* data entry tag pointer */
1661 xfs_trans_t *tp; /* transaction pointer */
1663 struct xfs_dir3_icfree_hdr freehdr;
1664 struct xfs_dir2_data_free *bf;
1669 length = dp->d_ops->data_entsize(args->namelen);
1671 * If we came in with a freespace block that means that lookup
1672 * found an entry with our hash value. This is the freespace
1673 * block for that data entry.
1678 * Remember initial freespace block number.
1680 ifbno = fblk->blkno;
1682 findex = fblk->index;
1683 bests = dp->d_ops->free_bests_p(free);
1684 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1687 * This means the free entry showed that the data block had
1688 * space for our entry, so we remembered it.
1689 * Use that data block.
1692 ASSERT(findex < freehdr.nvalid);
1693 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1694 ASSERT(be16_to_cpu(bests[findex]) >= length);
1695 dbno = freehdr.firstdb + findex;
1698 * The data block looked at didn't have enough room.
1699 * We'll start at the beginning of the freespace entries.
1706 * Didn't come in with a freespace block, so no data block.
1714 * If we don't have a data block yet, we're going to scan the
1715 * freespace blocks looking for one. Figure out what the
1716 * highest freespace block number is.
1719 xfs_fileoff_t fo; /* freespace block number */
1721 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
1723 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1727 * While we haven't identified a data block, search the freeblock
1728 * data for a good data block. If we find a null freeblock entry,
1729 * indicating a hole in the data blocks, remember that.
1731 while (dbno == -1) {
1733 * If we don't have a freeblock in hand, get the next one.
1737 * Happens the first time through unless lookup gave
1738 * us a freespace block to start with.
1741 fbno = xfs_dir2_byte_to_db(args->geo,
1742 XFS_DIR2_FREE_OFFSET);
1744 * If it's ifbno we already looked at it.
1749 * If it's off the end we're done.
1751 if (fbno >= lastfbno)
1754 * Read the block. There can be holes in the
1755 * freespace blocks, so this might not succeed.
1756 * This should be really rare, so there's no reason
1759 error = xfs_dir2_free_try_read(tp, dp,
1760 xfs_dir2_db_to_da(args->geo, fbno),
1770 * Look at the current free entry. Is it good enough?
1772 * The bests initialisation should be where the bufer is read in
1773 * the above branch. But gcc is too stupid to realise that bests
1774 * and the freehdr are actually initialised if they are placed
1775 * there, so we have to do it here to avoid warnings. Blech.
1777 bests = dp->d_ops->free_bests_p(free);
1778 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1779 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1780 be16_to_cpu(bests[findex]) >= length)
1781 dbno = freehdr.firstdb + findex;
1784 * Are we done with the freeblock?
1786 if (++findex == freehdr.nvalid) {
1790 xfs_trans_brelse(tp, fbp);
1792 if (fblk && fblk->bp)
1798 * If we don't have a data block, we need to allocate one and make
1799 * the freespace entries refer to it.
1801 if (unlikely(dbno == -1)) {
1803 * Not allowed to allocate, return failure.
1805 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
1809 * Allocate and initialize the new data block.
1811 if (unlikely((error = xfs_dir2_grow_inode(args,
1812 XFS_DIR2_DATA_SPACE,
1814 (error = xfs_dir3_data_init(args, dbno, &dbp))))
1818 * If (somehow) we have a freespace block, get rid of it.
1821 xfs_trans_brelse(tp, fbp);
1822 if (fblk && fblk->bp)
1826 * Get the freespace block corresponding to the data block
1827 * that was just allocated.
1829 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
1830 error = xfs_dir2_free_try_read(tp, dp,
1831 xfs_dir2_db_to_da(args->geo, fbno),
1837 * If there wasn't a freespace block, the read will
1838 * return a NULL fbp. Allocate and initialize a new one.
1841 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1846 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
1848 "%s: dir ino %llu needed freesp block %lld for\n"
1849 " data block %lld, got %lld ifbno %llu lastfbno %d",
1850 __func__, (unsigned long long)dp->i_ino,
1851 (long long)dp->d_ops->db_to_fdb(
1853 (long long)dbno, (long long)fbno,
1854 (unsigned long long)ifbno, lastfbno);
1857 " fblk 0x%p blkno %llu index %d magic 0x%x",
1859 (unsigned long long)fblk->blkno,
1863 xfs_alert(mp, " ... fblk is NULL");
1865 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1866 XFS_ERRLEVEL_LOW, mp);
1867 return -EFSCORRUPTED;
1871 * Get a buffer for the new block.
1873 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1877 bests = dp->d_ops->free_bests_p(free);
1878 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1881 * Remember the first slot as our empty slot.
1884 (fbno - xfs_dir2_byte_to_db(args->geo,
1885 XFS_DIR2_FREE_OFFSET)) *
1886 dp->d_ops->free_max_bests(args->geo);
1889 bests = dp->d_ops->free_bests_p(free);
1890 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1894 * Set the freespace block index from the data block number.
1896 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
1898 * If it's after the end of the current entries in the
1899 * freespace block, extend that table.
1901 if (findex >= freehdr.nvalid) {
1902 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
1903 freehdr.nvalid = findex + 1;
1905 * Tag new entry so nused will go up.
1907 bests[findex] = cpu_to_be16(NULLDATAOFF);
1910 * If this entry was for an empty data block
1911 * (this should always be true) then update the header.
1913 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1915 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1916 xfs_dir2_free_log_header(args, fbp);
1919 * Update the real value in the table.
1920 * We haven't allocated the data entry yet so this will
1924 bf = dp->d_ops->data_bestfree_p(hdr);
1925 bests[findex] = bf[0].length;
1929 * We had a data block so we don't have to make a new one.
1933 * If just checking, we succeeded.
1935 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
1939 * Read the data block in.
1941 error = xfs_dir3_data_read(tp, dp,
1942 xfs_dir2_db_to_da(args->geo, dbno),
1947 bf = dp->d_ops->data_bestfree_p(hdr);
1950 ASSERT(be16_to_cpu(bf[0].length) >= length);
1952 * Point to the existing unused space.
1954 dup = (xfs_dir2_data_unused_t *)
1955 ((char *)hdr + be16_to_cpu(bf[0].offset));
1956 needscan = needlog = 0;
1958 * Mark the first part of the unused space, inuse for us.
1960 xfs_dir2_data_use_free(args, dbp, dup,
1961 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
1962 &needlog, &needscan);
1964 * Fill in the new entry and log it.
1966 dep = (xfs_dir2_data_entry_t *)dup;
1967 dep->inumber = cpu_to_be64(args->inumber);
1968 dep->namelen = args->namelen;
1969 memcpy(dep->name, args->name, dep->namelen);
1970 dp->d_ops->data_put_ftype(dep, args->filetype);
1971 tagp = dp->d_ops->data_entry_tag_p(dep);
1972 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1973 xfs_dir2_data_log_entry(args, dbp, dep);
1975 * Rescan the block for bestfree if needed.
1978 xfs_dir2_data_freescan(dp, hdr, &needlog);
1980 * Log the data block header if needed.
1983 xfs_dir2_data_log_header(args, dbp);
1985 * If the freespace entry is now wrong, update it.
1987 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
1988 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
1989 bests[findex] = bf[0].length;
1993 * Log the freespace entry if needed.
1996 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1998 * Return the data block and offset in args, then drop the data block.
2000 args->blkno = (xfs_dablk_t)dbno;
2001 args->index = be16_to_cpu(*tagp);
2006 * Lookup an entry in a node-format directory.
2007 * All the real work happens in xfs_da3_node_lookup_int.
2008 * The only real output is the inode number of the entry.
2011 xfs_dir2_node_lookup(
2012 xfs_da_args_t *args) /* operation arguments */
2014 int error; /* error return value */
2015 int i; /* btree level */
2016 int rval; /* operation return value */
2017 xfs_da_state_t *state; /* btree cursor */
2019 trace_xfs_dir2_node_lookup(args);
2022 * Allocate and initialize the btree cursor.
2024 state = xfs_da_state_alloc();
2026 state->mp = args->dp->i_mount;
2028 * Fill in the path to the entry in the cursor.
2030 error = xfs_da3_node_lookup_int(state, &rval);
2033 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2034 /* If a CI match, dup the actual name and return -EEXIST */
2035 xfs_dir2_data_entry_t *dep;
2037 dep = (xfs_dir2_data_entry_t *)
2038 ((char *)state->extrablk.bp->b_addr +
2039 state->extrablk.index);
2040 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2043 * Release the btree blocks and leaf block.
2045 for (i = 0; i < state->path.active; i++) {
2046 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2047 state->path.blk[i].bp = NULL;
2050 * Release the data block if we have it.
2052 if (state->extravalid && state->extrablk.bp) {
2053 xfs_trans_brelse(args->trans, state->extrablk.bp);
2054 state->extrablk.bp = NULL;
2056 xfs_da_state_free(state);
2061 * Remove an entry from a node-format directory.
2064 xfs_dir2_node_removename(
2065 struct xfs_da_args *args) /* operation arguments */
2067 struct xfs_da_state_blk *blk; /* leaf block */
2068 int error; /* error return value */
2069 int rval; /* operation return value */
2070 struct xfs_da_state *state; /* btree cursor */
2072 trace_xfs_dir2_node_removename(args);
2075 * Allocate and initialize the btree cursor.
2077 state = xfs_da_state_alloc();
2079 state->mp = args->dp->i_mount;
2081 /* Look up the entry we're deleting, set up the cursor. */
2082 error = xfs_da3_node_lookup_int(state, &rval);
2086 /* Didn't find it, upper layer screwed up. */
2087 if (rval != -EEXIST) {
2092 blk = &state->path.blk[state->path.active - 1];
2093 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2094 ASSERT(state->extravalid);
2096 * Remove the leaf and data entries.
2097 * Extrablk refers to the data block.
2099 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2100 &state->extrablk, &rval);
2104 * Fix the hash values up the btree.
2106 xfs_da3_fixhashpath(state, &state->path);
2108 * If we need to join leaf blocks, do it.
2110 if (rval && state->path.active > 1)
2111 error = xfs_da3_join(state);
2113 * If no errors so far, try conversion to leaf format.
2116 error = xfs_dir2_node_to_leaf(state);
2118 xfs_da_state_free(state);
2123 * Replace an entry's inode number in a node-format directory.
2126 xfs_dir2_node_replace(
2127 xfs_da_args_t *args) /* operation arguments */
2129 xfs_da_state_blk_t *blk; /* leaf block */
2130 xfs_dir2_data_hdr_t *hdr; /* data block header */
2131 xfs_dir2_data_entry_t *dep; /* data entry changed */
2132 int error; /* error return value */
2133 int i; /* btree level */
2134 xfs_ino_t inum; /* new inode number */
2135 xfs_dir2_leaf_t *leaf; /* leaf structure */
2136 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2137 int rval; /* internal return value */
2138 xfs_da_state_t *state; /* btree cursor */
2140 trace_xfs_dir2_node_replace(args);
2143 * Allocate and initialize the btree cursor.
2145 state = xfs_da_state_alloc();
2147 state->mp = args->dp->i_mount;
2148 inum = args->inumber;
2150 * Lookup the entry to change in the btree.
2152 error = xfs_da3_node_lookup_int(state, &rval);
2157 * It should be found, since the vnodeops layer has looked it up
2158 * and locked it. But paranoia is good.
2160 if (rval == -EEXIST) {
2161 struct xfs_dir2_leaf_entry *ents;
2163 * Find the leaf entry.
2165 blk = &state->path.blk[state->path.active - 1];
2166 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2167 leaf = blk->bp->b_addr;
2168 ents = args->dp->d_ops->leaf_ents_p(leaf);
2169 lep = &ents[blk->index];
2170 ASSERT(state->extravalid);
2172 * Point to the data entry.
2174 hdr = state->extrablk.bp->b_addr;
2175 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2176 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2177 dep = (xfs_dir2_data_entry_t *)
2179 xfs_dir2_dataptr_to_off(args->geo,
2180 be32_to_cpu(lep->address)));
2181 ASSERT(inum != be64_to_cpu(dep->inumber));
2183 * Fill in the new inode number and log the entry.
2185 dep->inumber = cpu_to_be64(inum);
2186 args->dp->d_ops->data_put_ftype(dep, args->filetype);
2187 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2191 * Didn't find it, and we're holding a data block. Drop it.
2193 else if (state->extravalid) {
2194 xfs_trans_brelse(args->trans, state->extrablk.bp);
2195 state->extrablk.bp = NULL;
2198 * Release all the buffers in the cursor.
2200 for (i = 0; i < state->path.active; i++) {
2201 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2202 state->path.blk[i].bp = NULL;
2204 xfs_da_state_free(state);
2209 * Trim off a trailing empty freespace block.
2210 * Return (in rvalp) 1 if we did it, 0 if not.
2213 xfs_dir2_node_trim_free(
2214 xfs_da_args_t *args, /* operation arguments */
2215 xfs_fileoff_t fo, /* free block number */
2216 int *rvalp) /* out: did something */
2218 struct xfs_buf *bp; /* freespace buffer */
2219 xfs_inode_t *dp; /* incore directory inode */
2220 int error; /* error return code */
2221 xfs_dir2_free_t *free; /* freespace structure */
2222 xfs_trans_t *tp; /* transaction pointer */
2223 struct xfs_dir3_icfree_hdr freehdr;
2228 * Read the freespace block.
2230 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
2234 * There can be holes in freespace. If fo is a hole, there's
2240 dp->d_ops->free_hdr_from_disk(&freehdr, free);
2243 * If there are used entries, there's nothing to do.
2245 if (freehdr.nused > 0) {
2246 xfs_trans_brelse(tp, bp);
2251 * Blow the block away.
2253 error = xfs_dir2_shrink_inode(args,
2254 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2257 * Can't fail with ENOSPC since that only happens with no
2258 * space reservation, when breaking up an extent into two
2259 * pieces. This is the last block of an extent.
2261 ASSERT(error != -ENOSPC);
2262 xfs_trans_brelse(tp, bp);
2266 * Return that we succeeded.