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"
39 * Function declarations.
41 static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
43 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
44 xfs_da_state_blk_t *blk1,
45 xfs_da_state_blk_t *blk2);
46 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
47 int index, xfs_da_state_blk_t *dblk,
49 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
50 xfs_da_state_blk_t *fblk);
53 * Check internal consistency of a leafn block.
56 #define xfs_dir3_leaf_check(dp, bp) \
58 if (!xfs_dir3_leafn_check((dp), (bp))) \
67 struct xfs_dir2_leaf *leaf = bp->b_addr;
68 struct xfs_dir3_icleaf_hdr leafhdr;
70 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
72 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
73 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
74 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
76 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
79 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
82 #define xfs_dir3_leaf_check(dp, bp)
89 struct xfs_mount *mp = bp->b_target->bt_mount;
90 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
92 if (xfs_sb_version_hascrc(&mp->m_sb)) {
93 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
95 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
97 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
99 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
101 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
104 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
108 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
114 xfs_dir3_free_read_verify(
117 struct xfs_mount *mp = bp->b_target->bt_mount;
119 if (xfs_sb_version_hascrc(&mp->m_sb) &&
120 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
121 xfs_buf_ioerror(bp, -EFSBADCRC);
122 else if (!xfs_dir3_free_verify(bp))
123 xfs_buf_ioerror(bp, -EFSCORRUPTED);
126 xfs_verifier_error(bp);
130 xfs_dir3_free_write_verify(
133 struct xfs_mount *mp = bp->b_target->bt_mount;
134 struct xfs_buf_log_item *bip = bp->b_fspriv;
135 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
137 if (!xfs_dir3_free_verify(bp)) {
138 xfs_buf_ioerror(bp, -EFSCORRUPTED);
139 xfs_verifier_error(bp);
143 if (!xfs_sb_version_hascrc(&mp->m_sb))
147 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
149 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
152 const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
153 .name = "xfs_dir3_free",
154 .verify_read = xfs_dir3_free_read_verify,
155 .verify_write = xfs_dir3_free_write_verify,
160 __xfs_dir3_free_read(
161 struct xfs_trans *tp,
162 struct xfs_inode *dp,
164 xfs_daddr_t mappedbno,
165 struct xfs_buf **bpp)
169 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
170 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
172 /* try read returns without an error or *bpp if it lands in a hole */
173 if (!err && tp && *bpp)
174 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
180 struct xfs_trans *tp,
181 struct xfs_inode *dp,
183 struct xfs_buf **bpp)
185 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
189 xfs_dir2_free_try_read(
190 struct xfs_trans *tp,
191 struct xfs_inode *dp,
193 struct xfs_buf **bpp)
195 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
199 xfs_dir3_free_get_buf(
202 struct xfs_buf **bpp)
204 struct xfs_trans *tp = args->trans;
205 struct xfs_inode *dp = args->dp;
206 struct xfs_mount *mp = dp->i_mount;
209 struct xfs_dir3_icfree_hdr hdr;
211 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
212 -1, &bp, XFS_DATA_FORK);
216 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
217 bp->b_ops = &xfs_dir3_free_buf_ops;
220 * Initialize the new block to be empty, and remember
221 * its first slot as our empty slot.
223 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
224 memset(&hdr, 0, sizeof(hdr));
226 if (xfs_sb_version_hascrc(&mp->m_sb)) {
227 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
229 hdr.magic = XFS_DIR3_FREE_MAGIC;
231 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
232 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
233 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
235 hdr.magic = XFS_DIR2_FREE_MAGIC;
236 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
242 * Log entries from a freespace block.
245 xfs_dir2_free_log_bests(
246 struct xfs_da_args *args,
248 int first, /* first entry to log */
249 int last) /* last entry to log */
251 xfs_dir2_free_t *free; /* freespace structure */
255 bests = args->dp->d_ops->free_bests_p(free);
256 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
257 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
258 xfs_trans_log_buf(args->trans, bp,
259 (uint)((char *)&bests[first] - (char *)free),
260 (uint)((char *)&bests[last] - (char *)free +
261 sizeof(bests[0]) - 1));
265 * Log header from a freespace block.
268 xfs_dir2_free_log_header(
269 struct xfs_da_args *args,
273 xfs_dir2_free_t *free; /* freespace structure */
276 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
277 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
279 xfs_trans_log_buf(args->trans, bp, 0,
280 args->dp->d_ops->free_hdr_size - 1);
284 * Convert a leaf-format directory to a node-format directory.
285 * We need to change the magic number of the leaf block, and copy
286 * the freespace table out of the leaf block into its own block.
289 xfs_dir2_leaf_to_node(
290 xfs_da_args_t *args, /* operation arguments */
291 struct xfs_buf *lbp) /* leaf buffer */
293 xfs_inode_t *dp; /* incore directory inode */
294 int error; /* error return value */
295 struct xfs_buf *fbp; /* freespace buffer */
296 xfs_dir2_db_t fdb; /* freespace block number */
297 xfs_dir2_free_t *free; /* freespace structure */
298 __be16 *from; /* pointer to freespace entry */
299 int i; /* leaf freespace index */
300 xfs_dir2_leaf_t *leaf; /* leaf structure */
301 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
302 int n; /* count of live freespc ents */
303 xfs_dir2_data_off_t off; /* freespace entry value */
304 __be16 *to; /* pointer to freespace entry */
305 xfs_trans_t *tp; /* transaction pointer */
306 struct xfs_dir3_icfree_hdr freehdr;
308 trace_xfs_dir2_leaf_to_node(args);
313 * Add a freespace block to the directory.
315 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
318 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
320 * Get the buffer for the new freespace block.
322 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
327 dp->d_ops->free_hdr_from_disk(&freehdr, free);
329 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
330 ASSERT(be32_to_cpu(ltp->bestcount) <=
331 (uint)dp->i_d.di_size / args->geo->blksize);
334 * Copy freespace entries from the leaf block to the new block.
335 * Count active entries.
337 from = xfs_dir2_leaf_bests_p(ltp);
338 to = dp->d_ops->free_bests_p(free);
339 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
340 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
342 *to = cpu_to_be16(off);
346 * Now initialize the freespace block header.
349 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
351 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
352 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
353 xfs_dir2_free_log_header(args, fbp);
356 * Converting the leaf to a leafnode is just a matter of changing the
357 * magic number and the ops. Do the change directly to the buffer as
358 * it's less work (and less code) than decoding the header to host
359 * format and back again.
361 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
362 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
364 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
365 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
366 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
367 xfs_dir3_leaf_log_header(args, lbp);
368 xfs_dir3_leaf_check(dp, lbp);
373 * Add a leaf entry to a leaf block in a node-form directory.
374 * The other work necessary is done from the caller.
376 static int /* error */
378 struct xfs_buf *bp, /* leaf buffer */
379 xfs_da_args_t *args, /* operation arguments */
380 int index) /* insertion pt for new entry */
382 int compact; /* compacting stale leaves */
383 xfs_inode_t *dp; /* incore directory inode */
384 int highstale; /* next stale entry */
385 xfs_dir2_leaf_t *leaf; /* leaf structure */
386 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
387 int lfloghigh; /* high leaf entry logging */
388 int lfloglow; /* low leaf entry logging */
389 int lowstale; /* previous stale entry */
390 struct xfs_dir3_icleaf_hdr leafhdr;
391 struct xfs_dir2_leaf_entry *ents;
393 trace_xfs_dir2_leafn_add(args, index);
397 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
398 ents = dp->d_ops->leaf_ents_p(leaf);
401 * Quick check just to make sure we are not going to index
402 * into other peoples memory
405 return -EFSCORRUPTED;
408 * If there are already the maximum number of leaf entries in
409 * the block, if there are no stale entries it won't fit.
410 * Caller will do a split. If there are stale entries we'll do
414 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
417 compact = leafhdr.stale > 1;
420 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
421 ASSERT(index == leafhdr.count ||
422 be32_to_cpu(ents[index].hashval) >= args->hashval);
424 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
428 * Compact out all but one stale leaf entry. Leaves behind
429 * the entry closest to index.
432 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
433 &highstale, &lfloglow, &lfloghigh);
434 else if (leafhdr.stale) {
436 * Set impossible logging indices for this case.
438 lfloglow = leafhdr.count;
443 * Insert the new entry, log everything.
445 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
446 highstale, &lfloglow, &lfloghigh);
448 lep->hashval = cpu_to_be32(args->hashval);
449 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
450 args->blkno, args->index));
452 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
453 xfs_dir3_leaf_log_header(args, bp);
454 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
455 xfs_dir3_leaf_check(dp, bp);
461 xfs_dir2_free_hdr_check(
462 struct xfs_inode *dp,
466 struct xfs_dir3_icfree_hdr hdr;
468 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
470 ASSERT((hdr.firstdb %
471 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
472 ASSERT(hdr.firstdb <= db);
473 ASSERT(db < hdr.firstdb + hdr.nvalid);
476 #define xfs_dir2_free_hdr_check(dp, bp, db)
480 * Return the last hash value in the leaf.
481 * Stale entries are ok.
483 xfs_dahash_t /* hash value */
484 xfs_dir2_leafn_lasthash(
485 struct xfs_inode *dp,
486 struct xfs_buf *bp, /* leaf buffer */
487 int *count) /* count of entries in leaf */
489 struct xfs_dir2_leaf *leaf = bp->b_addr;
490 struct xfs_dir2_leaf_entry *ents;
491 struct xfs_dir3_icleaf_hdr leafhdr;
493 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
495 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
496 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
499 *count = leafhdr.count;
503 ents = dp->d_ops->leaf_ents_p(leaf);
504 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
508 * Look up a leaf entry for space to add a name in a node-format leaf block.
509 * The extrablk in state is a freespace block.
512 xfs_dir2_leafn_lookup_for_addname(
513 struct xfs_buf *bp, /* leaf buffer */
514 xfs_da_args_t *args, /* operation arguments */
515 int *indexp, /* out: leaf entry index */
516 xfs_da_state_t *state) /* state to fill in */
518 struct xfs_buf *curbp = NULL; /* current data/free buffer */
519 xfs_dir2_db_t curdb = -1; /* current data block number */
520 xfs_dir2_db_t curfdb = -1; /* current free block number */
521 xfs_inode_t *dp; /* incore directory inode */
522 int error; /* error return value */
523 int fi; /* free entry index */
524 xfs_dir2_free_t *free = NULL; /* free block structure */
525 int index; /* leaf entry index */
526 xfs_dir2_leaf_t *leaf; /* leaf structure */
527 int length; /* length of new data entry */
528 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
529 xfs_mount_t *mp; /* filesystem mount point */
530 xfs_dir2_db_t newdb; /* new data block number */
531 xfs_dir2_db_t newfdb; /* new free block number */
532 xfs_trans_t *tp; /* transaction pointer */
533 struct xfs_dir2_leaf_entry *ents;
534 struct xfs_dir3_icleaf_hdr leafhdr;
540 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
541 ents = dp->d_ops->leaf_ents_p(leaf);
543 xfs_dir3_leaf_check(dp, bp);
544 ASSERT(leafhdr.count > 0);
547 * Look up the hash value in the leaf entries.
549 index = xfs_dir2_leaf_search_hash(args, bp);
551 * Do we have a buffer coming in?
553 if (state->extravalid) {
554 /* If so, it's a free block buffer, get the block number. */
555 curbp = state->extrablk.bp;
556 curfdb = state->extrablk.blkno;
557 free = curbp->b_addr;
558 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
559 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
561 length = dp->d_ops->data_entsize(args->namelen);
563 * Loop over leaf entries with the right hash value.
565 for (lep = &ents[index];
566 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
569 * Skip stale leaf entries.
571 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
574 * Pull the data block number from the entry.
576 newdb = xfs_dir2_dataptr_to_db(args->geo,
577 be32_to_cpu(lep->address));
579 * For addname, we're looking for a place to put the new entry.
580 * We want to use a data block with an entry of equal
581 * hash value to ours if there is one with room.
583 * If this block isn't the data block we already have
584 * in hand, take a look at it.
586 if (newdb != curdb) {
591 * Convert the data block to the free block
592 * holding its freespace information.
594 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
596 * If it's not the one we have in hand, read it in.
598 if (newfdb != curfdb) {
600 * If we had one before, drop it.
603 xfs_trans_brelse(tp, curbp);
605 error = xfs_dir2_free_read(tp, dp,
606 xfs_dir2_db_to_da(args->geo,
611 free = curbp->b_addr;
613 xfs_dir2_free_hdr_check(dp, curbp, curdb);
616 * Get the index for our entry.
618 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
620 * If it has room, return it.
622 bests = dp->d_ops->free_bests_p(free);
623 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
624 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
625 XFS_ERRLEVEL_LOW, mp);
626 if (curfdb != newfdb)
627 xfs_trans_brelse(tp, curbp);
628 return -EFSCORRUPTED;
631 if (be16_to_cpu(bests[fi]) >= length)
635 /* Didn't find any space */
638 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
640 /* Giving back a free block. */
641 state->extravalid = 1;
642 state->extrablk.bp = curbp;
643 state->extrablk.index = fi;
644 state->extrablk.blkno = curfdb;
647 * Important: this magic number is not in the buffer - it's for
648 * buffer type information and therefore only the free/data type
649 * matters here, not whether CRCs are enabled or not.
651 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
653 state->extravalid = 0;
656 * Return the index, that will be the insertion point.
663 * Look up a leaf entry in a node-format leaf block.
664 * The extrablk in state a data block.
667 xfs_dir2_leafn_lookup_for_entry(
668 struct xfs_buf *bp, /* leaf buffer */
669 xfs_da_args_t *args, /* operation arguments */
670 int *indexp, /* out: leaf entry index */
671 xfs_da_state_t *state) /* state to fill in */
673 struct xfs_buf *curbp = NULL; /* current data/free buffer */
674 xfs_dir2_db_t curdb = -1; /* current data block number */
675 xfs_dir2_data_entry_t *dep; /* data block entry */
676 xfs_inode_t *dp; /* incore directory inode */
677 int error; /* error return value */
678 int index; /* leaf entry index */
679 xfs_dir2_leaf_t *leaf; /* leaf structure */
680 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
681 xfs_mount_t *mp; /* filesystem mount point */
682 xfs_dir2_db_t newdb; /* new data block number */
683 xfs_trans_t *tp; /* transaction pointer */
684 enum xfs_dacmp cmp; /* comparison result */
685 struct xfs_dir2_leaf_entry *ents;
686 struct xfs_dir3_icleaf_hdr leafhdr;
692 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
693 ents = dp->d_ops->leaf_ents_p(leaf);
695 xfs_dir3_leaf_check(dp, bp);
696 ASSERT(leafhdr.count > 0);
699 * Look up the hash value in the leaf entries.
701 index = xfs_dir2_leaf_search_hash(args, bp);
703 * Do we have a buffer coming in?
705 if (state->extravalid) {
706 curbp = state->extrablk.bp;
707 curdb = state->extrablk.blkno;
710 * Loop over leaf entries with the right hash value.
712 for (lep = &ents[index];
713 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
716 * Skip stale leaf entries.
718 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
721 * Pull the data block number from the entry.
723 newdb = xfs_dir2_dataptr_to_db(args->geo,
724 be32_to_cpu(lep->address));
726 * Not adding a new entry, so we really want to find
727 * the name given to us.
729 * If it's a different data block, go get it.
731 if (newdb != curdb) {
733 * If we had a block before that we aren't saving
734 * for a CI name, drop it
736 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
737 curdb != state->extrablk.blkno))
738 xfs_trans_brelse(tp, curbp);
740 * If needing the block that is saved with a CI match,
741 * use it otherwise read in the new data block.
743 if (args->cmpresult != XFS_CMP_DIFFERENT &&
744 newdb == state->extrablk.blkno) {
745 ASSERT(state->extravalid);
746 curbp = state->extrablk.bp;
748 error = xfs_dir3_data_read(tp, dp,
749 xfs_dir2_db_to_da(args->geo,
755 xfs_dir3_data_check(dp, curbp);
759 * Point to the data entry.
761 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
762 xfs_dir2_dataptr_to_off(args->geo,
763 be32_to_cpu(lep->address)));
765 * Compare the entry and if it's an exact match, return
766 * EEXIST immediately. If it's the first case-insensitive
767 * match, store the block & inode number and continue looking.
769 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
770 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
771 /* If there is a CI match block, drop it */
772 if (args->cmpresult != XFS_CMP_DIFFERENT &&
773 curdb != state->extrablk.blkno)
774 xfs_trans_brelse(tp, state->extrablk.bp);
775 args->cmpresult = cmp;
776 args->inumber = be64_to_cpu(dep->inumber);
777 args->filetype = dp->d_ops->data_get_ftype(dep);
779 state->extravalid = 1;
780 state->extrablk.bp = curbp;
781 state->extrablk.blkno = curdb;
782 state->extrablk.index = (int)((char *)dep -
783 (char *)curbp->b_addr);
784 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
785 curbp->b_ops = &xfs_dir3_data_buf_ops;
786 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
787 if (cmp == XFS_CMP_EXACT)
791 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
793 if (args->cmpresult == XFS_CMP_DIFFERENT) {
794 /* Giving back last used data block. */
795 state->extravalid = 1;
796 state->extrablk.bp = curbp;
797 state->extrablk.index = -1;
798 state->extrablk.blkno = curdb;
799 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
800 curbp->b_ops = &xfs_dir3_data_buf_ops;
801 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
803 /* If the curbp is not the CI match block, drop it */
804 if (state->extrablk.bp != curbp)
805 xfs_trans_brelse(tp, curbp);
808 state->extravalid = 0;
815 * Look up a leaf entry in a node-format leaf block.
816 * If this is an addname then the extrablk in state is a freespace block,
817 * otherwise it's a data block.
820 xfs_dir2_leafn_lookup_int(
821 struct xfs_buf *bp, /* leaf buffer */
822 xfs_da_args_t *args, /* operation arguments */
823 int *indexp, /* out: leaf entry index */
824 xfs_da_state_t *state) /* state to fill in */
826 if (args->op_flags & XFS_DA_OP_ADDNAME)
827 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
829 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
833 * Move count leaf entries from source to destination leaf.
834 * Log entries and headers. Stale entries are preserved.
837 xfs_dir3_leafn_moveents(
838 xfs_da_args_t *args, /* operation arguments */
839 struct xfs_buf *bp_s, /* source */
840 struct xfs_dir3_icleaf_hdr *shdr,
841 struct xfs_dir2_leaf_entry *sents,
842 int start_s,/* source leaf index */
843 struct xfs_buf *bp_d, /* destination */
844 struct xfs_dir3_icleaf_hdr *dhdr,
845 struct xfs_dir2_leaf_entry *dents,
846 int start_d,/* destination leaf index */
847 int count) /* count of leaves to copy */
849 int stale; /* count stale leaves copied */
851 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
854 * Silently return if nothing to do.
860 * If the destination index is not the end of the current
861 * destination leaf entries, open up a hole in the destination
862 * to hold the new entries.
864 if (start_d < dhdr->count) {
865 memmove(&dents[start_d + count], &dents[start_d],
866 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
867 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
868 count + dhdr->count - 1);
871 * If the source has stale leaves, count the ones in the copy range
872 * so we can update the header correctly.
875 int i; /* temp leaf index */
877 for (i = start_s, stale = 0; i < start_s + count; i++) {
878 if (sents[i].address ==
879 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
885 * Copy the leaf entries from source to destination.
887 memcpy(&dents[start_d], &sents[start_s],
888 count * sizeof(xfs_dir2_leaf_entry_t));
889 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
892 * If there are source entries after the ones we copied,
893 * delete the ones we copied by sliding the next ones down.
895 if (start_s + count < shdr->count) {
896 memmove(&sents[start_s], &sents[start_s + count],
897 count * sizeof(xfs_dir2_leaf_entry_t));
898 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
902 * Update the headers and log them.
904 shdr->count -= count;
905 shdr->stale -= stale;
906 dhdr->count += count;
907 dhdr->stale += stale;
911 * Determine the sort order of two leaf blocks.
912 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
915 xfs_dir2_leafn_order(
916 struct xfs_inode *dp,
917 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
918 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
920 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
921 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
922 struct xfs_dir2_leaf_entry *ents1;
923 struct xfs_dir2_leaf_entry *ents2;
924 struct xfs_dir3_icleaf_hdr hdr1;
925 struct xfs_dir3_icleaf_hdr hdr2;
927 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
928 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
929 ents1 = dp->d_ops->leaf_ents_p(leaf1);
930 ents2 = dp->d_ops->leaf_ents_p(leaf2);
932 if (hdr1.count > 0 && hdr2.count > 0 &&
933 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
934 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
935 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
941 * Rebalance leaf entries between two leaf blocks.
942 * This is actually only called when the second block is new,
943 * though the code deals with the general case.
944 * A new entry will be inserted in one of the blocks, and that
945 * entry is taken into account when balancing.
948 xfs_dir2_leafn_rebalance(
949 xfs_da_state_t *state, /* btree cursor */
950 xfs_da_state_blk_t *blk1, /* first btree block */
951 xfs_da_state_blk_t *blk2) /* second btree block */
953 xfs_da_args_t *args; /* operation arguments */
954 int count; /* count (& direction) leaves */
955 int isleft; /* new goes in left leaf */
956 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
957 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
958 int mid; /* midpoint leaf index */
959 #if defined(DEBUG) || defined(XFS_WARN)
960 int oldstale; /* old count of stale leaves */
962 int oldsum; /* old total leaf count */
963 int swap; /* swapped leaf blocks */
964 struct xfs_dir2_leaf_entry *ents1;
965 struct xfs_dir2_leaf_entry *ents2;
966 struct xfs_dir3_icleaf_hdr hdr1;
967 struct xfs_dir3_icleaf_hdr hdr2;
968 struct xfs_inode *dp = state->args->dp;
972 * If the block order is wrong, swap the arguments.
974 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
975 xfs_da_state_blk_t *tmp; /* temp for block swap */
981 leaf1 = blk1->bp->b_addr;
982 leaf2 = blk2->bp->b_addr;
983 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
984 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
985 ents1 = dp->d_ops->leaf_ents_p(leaf1);
986 ents2 = dp->d_ops->leaf_ents_p(leaf2);
988 oldsum = hdr1.count + hdr2.count;
989 #if defined(DEBUG) || defined(XFS_WARN)
990 oldstale = hdr1.stale + hdr2.stale;
995 * If the old leaf count was odd then the new one will be even,
996 * so we need to divide the new count evenly.
999 xfs_dahash_t midhash; /* middle entry hash value */
1001 if (mid >= hdr1.count)
1002 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
1004 midhash = be32_to_cpu(ents1[mid].hashval);
1005 isleft = args->hashval <= midhash;
1008 * If the old count is even then the new count is odd, so there's
1009 * no preferred side for the new entry.
1010 * Pick the left one.
1015 * Calculate moved entry count. Positive means left-to-right,
1016 * negative means right-to-left. Then move the entries.
1018 count = hdr1.count - mid + (isleft == 0);
1020 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1021 hdr1.count - count, blk2->bp,
1022 &hdr2, ents2, 0, count);
1024 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1025 blk1->bp, &hdr1, ents1,
1028 ASSERT(hdr1.count + hdr2.count == oldsum);
1029 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1031 /* log the changes made when moving the entries */
1032 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1033 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
1034 xfs_dir3_leaf_log_header(args, blk1->bp);
1035 xfs_dir3_leaf_log_header(args, blk2->bp);
1037 xfs_dir3_leaf_check(dp, blk1->bp);
1038 xfs_dir3_leaf_check(dp, blk2->bp);
1041 * Mark whether we're inserting into the old or new leaf.
1043 if (hdr1.count < hdr2.count)
1044 state->inleaf = swap;
1045 else if (hdr1.count > hdr2.count)
1046 state->inleaf = !swap;
1048 state->inleaf = swap ^ (blk1->index <= hdr1.count);
1050 * Adjust the expected index for insertion.
1053 blk2->index = blk1->index - hdr1.count;
1056 * Finally sanity check just to make sure we are not returning a
1059 if (blk2->index < 0) {
1062 xfs_alert(dp->i_mount,
1063 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1064 __func__, blk1->index);
1069 xfs_dir3_data_block_free(
1070 xfs_da_args_t *args,
1071 struct xfs_dir2_data_hdr *hdr,
1072 struct xfs_dir2_free *free,
1075 struct xfs_buf *fbp,
1080 struct xfs_dir3_icfree_hdr freehdr;
1081 struct xfs_inode *dp = args->dp;
1083 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1084 bests = dp->d_ops->free_bests_p(free);
1087 * Data block is not empty, just set the free entry to the new
1090 bests[findex] = cpu_to_be16(longest);
1091 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1095 /* One less used entry in the free table. */
1099 * If this was the last entry in the table, we can trim the table size
1100 * back. There might be other entries at the end referring to
1101 * non-existent data blocks, get those too.
1103 if (findex == freehdr.nvalid - 1) {
1104 int i; /* free entry index */
1106 for (i = findex - 1; i >= 0; i--) {
1107 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1110 freehdr.nvalid = i + 1;
1113 /* Not the last entry, just punch it out. */
1114 bests[findex] = cpu_to_be16(NULLDATAOFF);
1118 dp->d_ops->free_hdr_to_disk(free, &freehdr);
1119 xfs_dir2_free_log_header(args, fbp);
1122 * If there are no useful entries left in the block, get rid of the
1125 if (!freehdr.nused) {
1128 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1132 } else if (error != -ENOSPC || args->total != 0)
1135 * It's possible to get ENOSPC if there is no
1136 * space reservation. In this case some one
1137 * else will eventually get rid of this block.
1141 /* Log the free entry that changed, unless we got rid of it. */
1143 xfs_dir2_free_log_bests(args, fbp, findex, findex);
1148 * Remove an entry from a node directory.
1149 * This removes the leaf entry and the data entry,
1150 * and updates the free block if necessary.
1152 static int /* error */
1153 xfs_dir2_leafn_remove(
1154 xfs_da_args_t *args, /* operation arguments */
1155 struct xfs_buf *bp, /* leaf buffer */
1156 int index, /* leaf entry index */
1157 xfs_da_state_blk_t *dblk, /* data block */
1158 int *rval) /* resulting block needs join */
1160 xfs_dir2_data_hdr_t *hdr; /* data block header */
1161 xfs_dir2_db_t db; /* data block number */
1162 struct xfs_buf *dbp; /* data block buffer */
1163 xfs_dir2_data_entry_t *dep; /* data block entry */
1164 xfs_inode_t *dp; /* incore directory inode */
1165 xfs_dir2_leaf_t *leaf; /* leaf structure */
1166 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1167 int longest; /* longest data free entry */
1168 int off; /* data block entry offset */
1169 int needlog; /* need to log data header */
1170 int needscan; /* need to rescan data frees */
1171 xfs_trans_t *tp; /* transaction pointer */
1172 struct xfs_dir2_data_free *bf; /* bestfree table */
1173 struct xfs_dir3_icleaf_hdr leafhdr;
1174 struct xfs_dir2_leaf_entry *ents;
1176 trace_xfs_dir2_leafn_remove(args, index);
1181 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1182 ents = dp->d_ops->leaf_ents_p(leaf);
1185 * Point to the entry we're removing.
1190 * Extract the data block and offset from the entry.
1192 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1193 ASSERT(dblk->blkno == db);
1194 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
1195 ASSERT(dblk->index == off);
1198 * Kill the leaf entry by marking it stale.
1199 * Log the leaf block changes.
1202 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1203 xfs_dir3_leaf_log_header(args, bp);
1205 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1206 xfs_dir3_leaf_log_ents(args, bp, index, index);
1209 * Make the data entry free. Keep track of the longest freespace
1210 * in the data block in case it changes.
1214 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1215 bf = dp->d_ops->data_bestfree_p(hdr);
1216 longest = be16_to_cpu(bf[0].length);
1217 needlog = needscan = 0;
1218 xfs_dir2_data_make_free(args, dbp, off,
1219 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1221 * Rescan the data block freespaces for bestfree.
1222 * Log the data block header if needed.
1225 xfs_dir2_data_freescan(dp, hdr, &needlog);
1227 xfs_dir2_data_log_header(args, dbp);
1228 xfs_dir3_data_check(dp, dbp);
1230 * If the longest data block freespace changes, need to update
1231 * the corresponding freeblock entry.
1233 if (longest < be16_to_cpu(bf[0].length)) {
1234 int error; /* error return value */
1235 struct xfs_buf *fbp; /* freeblock buffer */
1236 xfs_dir2_db_t fdb; /* freeblock block number */
1237 int findex; /* index in freeblock entries */
1238 xfs_dir2_free_t *free; /* freeblock structure */
1241 * Convert the data block number to a free block,
1242 * read in the free block.
1244 fdb = dp->d_ops->db_to_fdb(args->geo, db);
1245 error = xfs_dir2_free_read(tp, dp,
1246 xfs_dir2_db_to_da(args->geo, fdb),
1253 struct xfs_dir3_icfree_hdr freehdr;
1254 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1255 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
1256 (fdb - xfs_dir2_byte_to_db(args->geo,
1257 XFS_DIR2_FREE_OFFSET)));
1261 * Calculate which entry we need to fix.
1263 findex = dp->d_ops->db_to_fdindex(args->geo, db);
1264 longest = be16_to_cpu(bf[0].length);
1266 * If the data block is now empty we can get rid of it
1269 if (longest == args->geo->blksize -
1270 dp->d_ops->data_entry_offset) {
1272 * Try to punch out the data block.
1274 error = xfs_dir2_shrink_inode(args, db, dbp);
1280 * We can get ENOSPC if there's no space reservation.
1281 * In this case just drop the buffer and some one else
1282 * will eventually get rid of the empty block.
1284 else if (!(error == -ENOSPC && args->total == 0))
1288 * If we got rid of the data block, we can eliminate that entry
1289 * in the free block.
1291 error = xfs_dir3_data_block_free(args, hdr, free,
1292 fdb, findex, fbp, longest);
1297 xfs_dir3_leaf_check(dp, bp);
1299 * Return indication of whether this leaf block is empty enough
1300 * to justify trying to join it with a neighbor.
1302 *rval = (dp->d_ops->leaf_hdr_size +
1303 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
1304 args->geo->magicpct;
1309 * Split the leaf entries in the old block into old and new blocks.
1312 xfs_dir2_leafn_split(
1313 xfs_da_state_t *state, /* btree cursor */
1314 xfs_da_state_blk_t *oldblk, /* original block */
1315 xfs_da_state_blk_t *newblk) /* newly created block */
1317 xfs_da_args_t *args; /* operation arguments */
1318 xfs_dablk_t blkno; /* new leaf block number */
1319 int error; /* error return value */
1320 struct xfs_inode *dp;
1323 * Allocate space for a new leaf node.
1327 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1328 error = xfs_da_grow_inode(args, &blkno);
1333 * Initialize the new leaf block.
1335 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1336 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1340 newblk->blkno = blkno;
1341 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1343 * Rebalance the entries across the two leaves, link the new
1344 * block into the leaves.
1346 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1347 error = xfs_da3_blk_link(state, oldblk, newblk);
1352 * Insert the new entry in the correct block.
1355 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1357 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1359 * Update last hashval in each block since we added the name.
1361 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1362 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1363 xfs_dir3_leaf_check(dp, oldblk->bp);
1364 xfs_dir3_leaf_check(dp, newblk->bp);
1369 * Check a leaf block and its neighbors to see if the block should be
1370 * collapsed into one or the other neighbor. Always keep the block
1371 * with the smaller block number.
1372 * If the current block is over 50% full, don't try to join it, return 0.
1373 * If the block is empty, fill in the state structure and return 2.
1374 * If it can be collapsed, fill in the state structure and return 1.
1375 * If nothing can be done, return 0.
1378 xfs_dir2_leafn_toosmall(
1379 xfs_da_state_t *state, /* btree cursor */
1380 int *action) /* resulting action to take */
1382 xfs_da_state_blk_t *blk; /* leaf block */
1383 xfs_dablk_t blkno; /* leaf block number */
1384 struct xfs_buf *bp; /* leaf buffer */
1385 int bytes; /* bytes in use */
1386 int count; /* leaf live entry count */
1387 int error; /* error return value */
1388 int forward; /* sibling block direction */
1389 int i; /* sibling counter */
1390 xfs_dir2_leaf_t *leaf; /* leaf structure */
1391 int rval; /* result from path_shift */
1392 struct xfs_dir3_icleaf_hdr leafhdr;
1393 struct xfs_dir2_leaf_entry *ents;
1394 struct xfs_inode *dp = state->args->dp;
1397 * Check for the degenerate case of the block being over 50% full.
1398 * If so, it's not worth even looking to see if we might be able
1399 * to coalesce with a sibling.
1401 blk = &state->path.blk[state->path.active - 1];
1402 leaf = blk->bp->b_addr;
1403 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1404 ents = dp->d_ops->leaf_ents_p(leaf);
1405 xfs_dir3_leaf_check(dp, blk->bp);
1407 count = leafhdr.count - leafhdr.stale;
1408 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
1409 if (bytes > (state->args->geo->blksize >> 1)) {
1411 * Blk over 50%, don't try to join.
1417 * Check for the degenerate case of the block being empty.
1418 * If the block is empty, we'll simply delete it, no need to
1419 * coalesce it with a sibling block. We choose (arbitrarily)
1420 * to merge with the forward block unless it is NULL.
1424 * Make altpath point to the block we want to keep and
1425 * path point to the block we want to drop (this one).
1427 forward = (leafhdr.forw != 0);
1428 memcpy(&state->altpath, &state->path, sizeof(state->path));
1429 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1433 *action = rval ? 2 : 0;
1437 * Examine each sibling block to see if we can coalesce with
1438 * at least 25% free space to spare. We need to figure out
1439 * whether to merge with the forward or the backward block.
1440 * We prefer coalescing with the lower numbered sibling so as
1441 * to shrink a directory over time.
1443 forward = leafhdr.forw < leafhdr.back;
1444 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1445 struct xfs_dir3_icleaf_hdr hdr2;
1447 blkno = forward ? leafhdr.forw : leafhdr.back;
1451 * Read the sibling leaf block.
1453 error = xfs_dir3_leafn_read(state->args->trans, dp,
1459 * Count bytes in the two blocks combined.
1461 count = leafhdr.count - leafhdr.stale;
1462 bytes = state->args->geo->blksize -
1463 (state->args->geo->blksize >> 2);
1466 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
1467 ents = dp->d_ops->leaf_ents_p(leaf);
1468 count += hdr2.count - hdr2.stale;
1469 bytes -= count * sizeof(ents[0]);
1472 * Fits with at least 25% to spare.
1476 xfs_trans_brelse(state->args->trans, bp);
1479 * Didn't like either block, give up.
1487 * Make altpath point to the block we want to keep (the lower
1488 * numbered block) and path point to the block we want to drop.
1490 memcpy(&state->altpath, &state->path, sizeof(state->path));
1491 if (blkno < blk->blkno)
1492 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1495 error = xfs_da3_path_shift(state, &state->path, forward, 0,
1500 *action = rval ? 0 : 1;
1505 * Move all the leaf entries from drop_blk to save_blk.
1506 * This is done as part of a join operation.
1509 xfs_dir2_leafn_unbalance(
1510 xfs_da_state_t *state, /* cursor */
1511 xfs_da_state_blk_t *drop_blk, /* dead block */
1512 xfs_da_state_blk_t *save_blk) /* surviving block */
1514 xfs_da_args_t *args; /* operation arguments */
1515 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1516 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1517 struct xfs_dir3_icleaf_hdr savehdr;
1518 struct xfs_dir3_icleaf_hdr drophdr;
1519 struct xfs_dir2_leaf_entry *sents;
1520 struct xfs_dir2_leaf_entry *dents;
1521 struct xfs_inode *dp = state->args->dp;
1524 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1525 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1526 drop_leaf = drop_blk->bp->b_addr;
1527 save_leaf = save_blk->bp->b_addr;
1529 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1530 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1531 sents = dp->d_ops->leaf_ents_p(save_leaf);
1532 dents = dp->d_ops->leaf_ents_p(drop_leaf);
1535 * If there are any stale leaf entries, take this opportunity
1539 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1541 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1544 * Move the entries from drop to the appropriate end of save.
1546 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1547 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1548 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1549 save_blk->bp, &savehdr, sents, 0,
1552 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1553 save_blk->bp, &savehdr, sents,
1554 savehdr.count, drophdr.count);
1555 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1557 /* log the changes made when moving the entries */
1558 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1559 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
1560 xfs_dir3_leaf_log_header(args, save_blk->bp);
1561 xfs_dir3_leaf_log_header(args, drop_blk->bp);
1563 xfs_dir3_leaf_check(dp, save_blk->bp);
1564 xfs_dir3_leaf_check(dp, drop_blk->bp);
1568 * Top-level node form directory addname routine.
1571 xfs_dir2_node_addname(
1572 xfs_da_args_t *args) /* operation arguments */
1574 xfs_da_state_blk_t *blk; /* leaf block for insert */
1575 int error; /* error return value */
1576 int rval; /* sub-return value */
1577 xfs_da_state_t *state; /* btree cursor */
1579 trace_xfs_dir2_node_addname(args);
1582 * Allocate and initialize the state (btree cursor).
1584 state = xfs_da_state_alloc();
1586 state->mp = args->dp->i_mount;
1588 * Look up the name. We're not supposed to find it, but
1589 * this gives us the insertion point.
1591 error = xfs_da3_node_lookup_int(state, &rval);
1594 if (rval != -ENOENT) {
1598 * Add the data entry to a data block.
1599 * Extravalid is set to a freeblock found by lookup.
1601 rval = xfs_dir2_node_addname_int(args,
1602 state->extravalid ? &state->extrablk : NULL);
1606 blk = &state->path.blk[state->path.active - 1];
1607 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1609 * Add the new leaf entry.
1611 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1614 * It worked, fix the hash values up the btree.
1616 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1617 xfs_da3_fixhashpath(state, &state->path);
1620 * It didn't work, we need to split the leaf block.
1622 if (args->total == 0) {
1623 ASSERT(rval == -ENOSPC);
1627 * Split the leaf block and insert the new entry.
1629 rval = xfs_da3_split(state);
1632 xfs_da_state_free(state);
1637 * Add the data entry for a node-format directory name addition.
1638 * The leaf entry is added in xfs_dir2_leafn_add.
1639 * We may enter with a freespace block that the lookup found.
1641 static int /* error */
1642 xfs_dir2_node_addname_int(
1643 xfs_da_args_t *args, /* operation arguments */
1644 xfs_da_state_blk_t *fblk) /* optional freespace block */
1646 xfs_dir2_data_hdr_t *hdr; /* data block header */
1647 xfs_dir2_db_t dbno; /* data block number */
1648 struct xfs_buf *dbp; /* data block buffer */
1649 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1650 xfs_inode_t *dp; /* incore directory inode */
1651 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1652 int error; /* error return value */
1653 xfs_dir2_db_t fbno; /* freespace block number */
1654 struct xfs_buf *fbp; /* freespace buffer */
1655 int findex; /* freespace entry index */
1656 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1657 xfs_dir2_db_t ifbno; /* initial freespace block no */
1658 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1659 int length; /* length of the new entry */
1660 int logfree; /* need to log free entry */
1661 xfs_mount_t *mp; /* filesystem mount point */
1662 int needlog; /* need to log data header */
1663 int needscan; /* need to rescan data frees */
1664 __be16 *tagp; /* data entry tag pointer */
1665 xfs_trans_t *tp; /* transaction pointer */
1667 struct xfs_dir3_icfree_hdr freehdr;
1668 struct xfs_dir2_data_free *bf;
1673 length = dp->d_ops->data_entsize(args->namelen);
1675 * If we came in with a freespace block that means that lookup
1676 * found an entry with our hash value. This is the freespace
1677 * block for that data entry.
1682 * Remember initial freespace block number.
1684 ifbno = fblk->blkno;
1686 findex = fblk->index;
1687 bests = dp->d_ops->free_bests_p(free);
1688 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1691 * This means the free entry showed that the data block had
1692 * space for our entry, so we remembered it.
1693 * Use that data block.
1696 ASSERT(findex < freehdr.nvalid);
1697 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1698 ASSERT(be16_to_cpu(bests[findex]) >= length);
1699 dbno = freehdr.firstdb + findex;
1702 * The data block looked at didn't have enough room.
1703 * We'll start at the beginning of the freespace entries.
1710 * Didn't come in with a freespace block, so no data block.
1718 * If we don't have a data block yet, we're going to scan the
1719 * freespace blocks looking for one. Figure out what the
1720 * highest freespace block number is.
1723 xfs_fileoff_t fo; /* freespace block number */
1725 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
1727 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1731 * While we haven't identified a data block, search the freeblock
1732 * data for a good data block. If we find a null freeblock entry,
1733 * indicating a hole in the data blocks, remember that.
1735 while (dbno == -1) {
1737 * If we don't have a freeblock in hand, get the next one.
1741 * Happens the first time through unless lookup gave
1742 * us a freespace block to start with.
1745 fbno = xfs_dir2_byte_to_db(args->geo,
1746 XFS_DIR2_FREE_OFFSET);
1748 * If it's ifbno we already looked at it.
1753 * If it's off the end we're done.
1755 if (fbno >= lastfbno)
1758 * Read the block. There can be holes in the
1759 * freespace blocks, so this might not succeed.
1760 * This should be really rare, so there's no reason
1763 error = xfs_dir2_free_try_read(tp, dp,
1764 xfs_dir2_db_to_da(args->geo, fbno),
1774 * Look at the current free entry. Is it good enough?
1776 * The bests initialisation should be where the bufer is read in
1777 * the above branch. But gcc is too stupid to realise that bests
1778 * and the freehdr are actually initialised if they are placed
1779 * there, so we have to do it here to avoid warnings. Blech.
1781 bests = dp->d_ops->free_bests_p(free);
1782 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1783 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1784 be16_to_cpu(bests[findex]) >= length)
1785 dbno = freehdr.firstdb + findex;
1788 * Are we done with the freeblock?
1790 if (++findex == freehdr.nvalid) {
1794 xfs_trans_brelse(tp, fbp);
1796 if (fblk && fblk->bp)
1802 * If we don't have a data block, we need to allocate one and make
1803 * the freespace entries refer to it.
1805 if (unlikely(dbno == -1)) {
1807 * Not allowed to allocate, return failure.
1809 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
1813 * Allocate and initialize the new data block.
1815 if (unlikely((error = xfs_dir2_grow_inode(args,
1816 XFS_DIR2_DATA_SPACE,
1818 (error = xfs_dir3_data_init(args, dbno, &dbp))))
1822 * If (somehow) we have a freespace block, get rid of it.
1825 xfs_trans_brelse(tp, fbp);
1826 if (fblk && fblk->bp)
1830 * Get the freespace block corresponding to the data block
1831 * that was just allocated.
1833 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
1834 error = xfs_dir2_free_try_read(tp, dp,
1835 xfs_dir2_db_to_da(args->geo, fbno),
1841 * If there wasn't a freespace block, the read will
1842 * return a NULL fbp. Allocate and initialize a new one.
1845 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1850 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
1852 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
1853 __func__, (unsigned long long)dp->i_ino,
1854 (long long)dp->d_ops->db_to_fdb(
1856 (long long)dbno, (long long)fbno,
1857 (unsigned long long)ifbno, lastfbno);
1860 " fblk 0x%p blkno %llu index %d magic 0x%x",
1862 (unsigned long long)fblk->blkno,
1866 xfs_alert(mp, " ... fblk is NULL");
1868 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1869 XFS_ERRLEVEL_LOW, mp);
1870 return -EFSCORRUPTED;
1874 * Get a buffer for the new block.
1876 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1880 bests = dp->d_ops->free_bests_p(free);
1881 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1884 * Remember the first slot as our empty slot.
1887 (fbno - xfs_dir2_byte_to_db(args->geo,
1888 XFS_DIR2_FREE_OFFSET)) *
1889 dp->d_ops->free_max_bests(args->geo);
1892 bests = dp->d_ops->free_bests_p(free);
1893 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1897 * Set the freespace block index from the data block number.
1899 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
1901 * If it's after the end of the current entries in the
1902 * freespace block, extend that table.
1904 if (findex >= freehdr.nvalid) {
1905 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
1906 freehdr.nvalid = findex + 1;
1908 * Tag new entry so nused will go up.
1910 bests[findex] = cpu_to_be16(NULLDATAOFF);
1913 * If this entry was for an empty data block
1914 * (this should always be true) then update the header.
1916 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1918 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1919 xfs_dir2_free_log_header(args, fbp);
1922 * Update the real value in the table.
1923 * We haven't allocated the data entry yet so this will
1927 bf = dp->d_ops->data_bestfree_p(hdr);
1928 bests[findex] = bf[0].length;
1932 * We had a data block so we don't have to make a new one.
1936 * If just checking, we succeeded.
1938 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
1942 * Read the data block in.
1944 error = xfs_dir3_data_read(tp, dp,
1945 xfs_dir2_db_to_da(args->geo, dbno),
1950 bf = dp->d_ops->data_bestfree_p(hdr);
1953 ASSERT(be16_to_cpu(bf[0].length) >= length);
1955 * Point to the existing unused space.
1957 dup = (xfs_dir2_data_unused_t *)
1958 ((char *)hdr + be16_to_cpu(bf[0].offset));
1959 needscan = needlog = 0;
1961 * Mark the first part of the unused space, inuse for us.
1963 xfs_dir2_data_use_free(args, dbp, dup,
1964 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
1965 &needlog, &needscan);
1967 * Fill in the new entry and log it.
1969 dep = (xfs_dir2_data_entry_t *)dup;
1970 dep->inumber = cpu_to_be64(args->inumber);
1971 dep->namelen = args->namelen;
1972 memcpy(dep->name, args->name, dep->namelen);
1973 dp->d_ops->data_put_ftype(dep, args->filetype);
1974 tagp = dp->d_ops->data_entry_tag_p(dep);
1975 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1976 xfs_dir2_data_log_entry(args, dbp, dep);
1978 * Rescan the block for bestfree if needed.
1981 xfs_dir2_data_freescan(dp, hdr, &needlog);
1983 * Log the data block header if needed.
1986 xfs_dir2_data_log_header(args, dbp);
1988 * If the freespace entry is now wrong, update it.
1990 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
1991 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
1992 bests[findex] = bf[0].length;
1996 * Log the freespace entry if needed.
1999 xfs_dir2_free_log_bests(args, fbp, findex, findex);
2001 * Return the data block and offset in args, then drop the data block.
2003 args->blkno = (xfs_dablk_t)dbno;
2004 args->index = be16_to_cpu(*tagp);
2009 * Lookup an entry in a node-format directory.
2010 * All the real work happens in xfs_da3_node_lookup_int.
2011 * The only real output is the inode number of the entry.
2014 xfs_dir2_node_lookup(
2015 xfs_da_args_t *args) /* operation arguments */
2017 int error; /* error return value */
2018 int i; /* btree level */
2019 int rval; /* operation return value */
2020 xfs_da_state_t *state; /* btree cursor */
2022 trace_xfs_dir2_node_lookup(args);
2025 * Allocate and initialize the btree cursor.
2027 state = xfs_da_state_alloc();
2029 state->mp = args->dp->i_mount;
2031 * Fill in the path to the entry in the cursor.
2033 error = xfs_da3_node_lookup_int(state, &rval);
2036 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2037 /* If a CI match, dup the actual name and return -EEXIST */
2038 xfs_dir2_data_entry_t *dep;
2040 dep = (xfs_dir2_data_entry_t *)
2041 ((char *)state->extrablk.bp->b_addr +
2042 state->extrablk.index);
2043 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2046 * Release the btree blocks and leaf block.
2048 for (i = 0; i < state->path.active; i++) {
2049 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2050 state->path.blk[i].bp = NULL;
2053 * Release the data block if we have it.
2055 if (state->extravalid && state->extrablk.bp) {
2056 xfs_trans_brelse(args->trans, state->extrablk.bp);
2057 state->extrablk.bp = NULL;
2059 xfs_da_state_free(state);
2064 * Remove an entry from a node-format directory.
2067 xfs_dir2_node_removename(
2068 struct xfs_da_args *args) /* operation arguments */
2070 struct xfs_da_state_blk *blk; /* leaf block */
2071 int error; /* error return value */
2072 int rval; /* operation return value */
2073 struct xfs_da_state *state; /* btree cursor */
2075 trace_xfs_dir2_node_removename(args);
2078 * Allocate and initialize the btree cursor.
2080 state = xfs_da_state_alloc();
2082 state->mp = args->dp->i_mount;
2084 /* Look up the entry we're deleting, set up the cursor. */
2085 error = xfs_da3_node_lookup_int(state, &rval);
2089 /* Didn't find it, upper layer screwed up. */
2090 if (rval != -EEXIST) {
2095 blk = &state->path.blk[state->path.active - 1];
2096 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2097 ASSERT(state->extravalid);
2099 * Remove the leaf and data entries.
2100 * Extrablk refers to the data block.
2102 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2103 &state->extrablk, &rval);
2107 * Fix the hash values up the btree.
2109 xfs_da3_fixhashpath(state, &state->path);
2111 * If we need to join leaf blocks, do it.
2113 if (rval && state->path.active > 1)
2114 error = xfs_da3_join(state);
2116 * If no errors so far, try conversion to leaf format.
2119 error = xfs_dir2_node_to_leaf(state);
2121 xfs_da_state_free(state);
2126 * Replace an entry's inode number in a node-format directory.
2129 xfs_dir2_node_replace(
2130 xfs_da_args_t *args) /* operation arguments */
2132 xfs_da_state_blk_t *blk; /* leaf block */
2133 xfs_dir2_data_hdr_t *hdr; /* data block header */
2134 xfs_dir2_data_entry_t *dep; /* data entry changed */
2135 int error; /* error return value */
2136 int i; /* btree level */
2137 xfs_ino_t inum; /* new inode number */
2138 int ftype; /* new file type */
2139 xfs_dir2_leaf_t *leaf; /* leaf structure */
2140 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2141 int rval; /* internal return value */
2142 xfs_da_state_t *state; /* btree cursor */
2144 trace_xfs_dir2_node_replace(args);
2147 * Allocate and initialize the btree cursor.
2149 state = xfs_da_state_alloc();
2151 state->mp = args->dp->i_mount;
2154 * We have to save new inode number and ftype since
2155 * xfs_da3_node_lookup_int() is going to overwrite them
2157 inum = args->inumber;
2158 ftype = args->filetype;
2161 * Lookup the entry to change in the btree.
2163 error = xfs_da3_node_lookup_int(state, &rval);
2168 * It should be found, since the vnodeops layer has looked it up
2169 * and locked it. But paranoia is good.
2171 if (rval == -EEXIST) {
2172 struct xfs_dir2_leaf_entry *ents;
2174 * Find the leaf entry.
2176 blk = &state->path.blk[state->path.active - 1];
2177 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2178 leaf = blk->bp->b_addr;
2179 ents = args->dp->d_ops->leaf_ents_p(leaf);
2180 lep = &ents[blk->index];
2181 ASSERT(state->extravalid);
2183 * Point to the data entry.
2185 hdr = state->extrablk.bp->b_addr;
2186 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2187 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2188 dep = (xfs_dir2_data_entry_t *)
2190 xfs_dir2_dataptr_to_off(args->geo,
2191 be32_to_cpu(lep->address)));
2192 ASSERT(inum != be64_to_cpu(dep->inumber));
2194 * Fill in the new inode number and log the entry.
2196 dep->inumber = cpu_to_be64(inum);
2197 args->dp->d_ops->data_put_ftype(dep, ftype);
2198 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2202 * Didn't find it, and we're holding a data block. Drop it.
2204 else if (state->extravalid) {
2205 xfs_trans_brelse(args->trans, state->extrablk.bp);
2206 state->extrablk.bp = NULL;
2209 * Release all the buffers in the cursor.
2211 for (i = 0; i < state->path.active; i++) {
2212 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2213 state->path.blk[i].bp = NULL;
2215 xfs_da_state_free(state);
2220 * Trim off a trailing empty freespace block.
2221 * Return (in rvalp) 1 if we did it, 0 if not.
2224 xfs_dir2_node_trim_free(
2225 xfs_da_args_t *args, /* operation arguments */
2226 xfs_fileoff_t fo, /* free block number */
2227 int *rvalp) /* out: did something */
2229 struct xfs_buf *bp; /* freespace buffer */
2230 xfs_inode_t *dp; /* incore directory inode */
2231 int error; /* error return code */
2232 xfs_dir2_free_t *free; /* freespace structure */
2233 xfs_trans_t *tp; /* transaction pointer */
2234 struct xfs_dir3_icfree_hdr freehdr;
2239 * Read the freespace block.
2241 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
2245 * There can be holes in freespace. If fo is a hole, there's
2251 dp->d_ops->free_hdr_from_disk(&freehdr, free);
2254 * If there are used entries, there's nothing to do.
2256 if (freehdr.nused > 0) {
2257 xfs_trans_brelse(tp, bp);
2262 * Blow the block away.
2264 error = xfs_dir2_shrink_inode(args,
2265 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2268 * Can't fail with ENOSPC since that only happens with no
2269 * space reservation, when breaking up an extent into two
2270 * pieces. This is the last block of an extent.
2272 ASSERT(error != -ENOSPC);
2273 xfs_trans_brelse(tp, bp);
2277 * Return that we succeeded.