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"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_inode.h"
32 #include "xfs_dir2_priv.h"
33 #include "xfs_error.h"
34 #include "xfs_trace.h"
35 #include "xfs_trans.h"
36 #include "xfs_buf_item.h"
37 #include "xfs_cksum.h"
40 * Function declarations.
42 static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
44 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
45 xfs_da_state_blk_t *blk1,
46 xfs_da_state_blk_t *blk2);
47 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
48 int index, xfs_da_state_blk_t *dblk,
50 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
51 xfs_da_state_blk_t *fblk);
54 * Check internal consistency of a leafn block.
57 #define xfs_dir3_leaf_check(dp, bp) \
59 if (!xfs_dir3_leafn_check((dp), (bp))) \
68 struct xfs_dir2_leaf *leaf = bp->b_addr;
69 struct xfs_dir3_icleaf_hdr leafhdr;
71 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
73 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
74 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
75 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
77 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
80 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
83 #define xfs_dir3_leaf_check(dp, bp)
90 struct xfs_mount *mp = bp->b_target->bt_mount;
91 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
93 if (xfs_sb_version_hascrc(&mp->m_sb)) {
94 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
96 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
98 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
100 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
103 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
107 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
113 xfs_dir3_free_read_verify(
116 struct xfs_mount *mp = bp->b_target->bt_mount;
118 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
119 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
120 XFS_DIR3_FREE_CRC_OFF)) ||
121 !xfs_dir3_free_verify(bp)) {
122 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
123 xfs_buf_ioerror(bp, EFSCORRUPTED);
128 xfs_dir3_free_write_verify(
131 struct xfs_mount *mp = bp->b_target->bt_mount;
132 struct xfs_buf_log_item *bip = bp->b_fspriv;
133 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
135 if (!xfs_dir3_free_verify(bp)) {
136 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
137 xfs_buf_ioerror(bp, EFSCORRUPTED);
141 if (!xfs_sb_version_hascrc(&mp->m_sb))
145 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
147 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_FREE_CRC_OFF);
150 const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
151 .verify_read = xfs_dir3_free_read_verify,
152 .verify_write = xfs_dir3_free_write_verify,
157 __xfs_dir3_free_read(
158 struct xfs_trans *tp,
159 struct xfs_inode *dp,
161 xfs_daddr_t mappedbno,
162 struct xfs_buf **bpp)
166 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
167 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
169 /* try read returns without an error or *bpp if it lands in a hole */
170 if (!err && tp && *bpp)
171 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
177 struct xfs_trans *tp,
178 struct xfs_inode *dp,
180 struct xfs_buf **bpp)
182 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
186 xfs_dir2_free_try_read(
187 struct xfs_trans *tp,
188 struct xfs_inode *dp,
190 struct xfs_buf **bpp)
192 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
196 xfs_dir3_free_get_buf(
197 struct xfs_trans *tp,
198 struct xfs_inode *dp,
200 struct xfs_buf **bpp)
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(mp, 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_trans *tp,
244 int first, /* first entry to log */
245 int last) /* last entry to log */
247 xfs_dir2_free_t *free; /* freespace structure */
251 bests = xfs_dir3_free_bests_p(tp->t_mountp, 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(tp, 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_trans *tp,
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(tp, bp, 0, xfs_dir3_free_hdr_size(tp->t_mountp) - 1);
279 * Convert a leaf-format directory to a node-format directory.
280 * We need to change the magic number of the leaf block, and copy
281 * the freespace table out of the leaf block into its own block.
284 xfs_dir2_leaf_to_node(
285 xfs_da_args_t *args, /* operation arguments */
286 struct xfs_buf *lbp) /* leaf buffer */
288 xfs_inode_t *dp; /* incore directory inode */
289 int error; /* error return value */
290 struct xfs_buf *fbp; /* freespace buffer */
291 xfs_dir2_db_t fdb; /* freespace block number */
292 xfs_dir2_free_t *free; /* freespace structure */
293 __be16 *from; /* pointer to freespace entry */
294 int i; /* leaf freespace index */
295 xfs_dir2_leaf_t *leaf; /* leaf structure */
296 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
297 xfs_mount_t *mp; /* filesystem mount point */
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);
310 * Add a freespace block to the directory.
312 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
315 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
317 * Get the buffer for the new freespace block.
319 error = xfs_dir3_free_get_buf(tp, dp, fdb, &fbp);
324 dp->d_ops->free_hdr_from_disk(&freehdr, free);
326 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
327 ASSERT(be32_to_cpu(ltp->bestcount) <=
328 (uint)dp->i_d.di_size / mp->m_dirblksize);
331 * Copy freespace entries from the leaf block to the new block.
332 * Count active entries.
334 from = xfs_dir2_leaf_bests_p(ltp);
335 to = xfs_dir3_free_bests_p(mp, free);
336 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
337 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
339 *to = cpu_to_be16(off);
343 * Now initialize the freespace block header.
346 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
348 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
349 xfs_dir2_free_log_bests(tp, fbp, 0, freehdr.nvalid - 1);
350 xfs_dir2_free_log_header(tp, fbp);
353 * Converting the leaf to a leafnode is just a matter of changing the
354 * magic number and the ops. Do the change directly to the buffer as
355 * it's less work (and less code) than decoding the header to host
356 * format and back again.
358 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
359 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
361 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
362 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
363 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
364 xfs_dir3_leaf_log_header(tp, dp, lbp);
365 xfs_dir3_leaf_check(dp, lbp);
370 * Add a leaf entry to a leaf block in a node-form directory.
371 * The other work necessary is done from the caller.
373 static int /* error */
375 struct xfs_buf *bp, /* leaf buffer */
376 xfs_da_args_t *args, /* operation arguments */
377 int index) /* insertion pt for new entry */
379 int compact; /* compacting stale leaves */
380 xfs_inode_t *dp; /* incore directory inode */
381 int highstale; /* next stale entry */
382 xfs_dir2_leaf_t *leaf; /* leaf structure */
383 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
384 int lfloghigh; /* high leaf entry logging */
385 int lfloglow; /* low leaf entry logging */
386 int lowstale; /* previous stale entry */
387 xfs_mount_t *mp; /* filesystem mount point */
388 xfs_trans_t *tp; /* transaction pointer */
389 struct xfs_dir3_icleaf_hdr leafhdr;
390 struct xfs_dir2_leaf_entry *ents;
392 trace_xfs_dir2_leafn_add(args, index);
398 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
399 ents = dp->d_ops->leaf_ents_p(leaf);
402 * Quick check just to make sure we are not going to index
403 * into other peoples memory
406 return XFS_ERROR(EFSCORRUPTED);
409 * If there are already the maximum number of leaf entries in
410 * the block, if there are no stale entries it won't fit.
411 * Caller will do a split. If there are stale entries we'll do
415 if (leafhdr.count == dp->d_ops->leaf_max_ents(mp)) {
417 return XFS_ERROR(ENOSPC);
418 compact = leafhdr.stale > 1;
421 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
422 ASSERT(index == leafhdr.count ||
423 be32_to_cpu(ents[index].hashval) >= args->hashval);
425 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
429 * Compact out all but one stale leaf entry. Leaves behind
430 * the entry closest to index.
433 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
434 &highstale, &lfloglow, &lfloghigh);
435 else if (leafhdr.stale) {
437 * Set impossible logging indices for this case.
439 lfloglow = leafhdr.count;
444 * Insert the new entry, log everything.
446 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
447 highstale, &lfloglow, &lfloghigh);
449 lep->hashval = cpu_to_be32(args->hashval);
450 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
451 args->blkno, args->index));
453 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
454 xfs_dir3_leaf_log_header(tp, dp, bp);
455 xfs_dir3_leaf_log_ents(tp, dp, bp, lfloglow, lfloghigh);
456 xfs_dir3_leaf_check(dp, bp);
462 xfs_dir2_free_hdr_check(
463 struct xfs_inode *dp,
467 struct xfs_dir3_icfree_hdr hdr;
469 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
471 ASSERT((hdr.firstdb % xfs_dir3_free_max_bests(dp->i_mount)) == 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(mp, be32_to_cpu(lep->address));
578 * For addname, we're looking for a place to put the new entry.
579 * We want to use a data block with an entry of equal
580 * hash value to ours if there is one with room.
582 * If this block isn't the data block we already have
583 * in hand, take a look at it.
585 if (newdb != curdb) {
590 * Convert the data block to the free block
591 * holding its freespace information.
593 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
595 * If it's not the one we have in hand, read it in.
597 if (newfdb != curfdb) {
599 * If we had one before, drop it.
602 xfs_trans_brelse(tp, curbp);
604 error = xfs_dir2_free_read(tp, dp,
605 xfs_dir2_db_to_da(mp, newfdb),
609 free = curbp->b_addr;
611 xfs_dir2_free_hdr_check(dp, curbp, curdb);
614 * Get the index for our entry.
616 fi = xfs_dir2_db_to_fdindex(mp, curdb);
618 * If it has room, return it.
620 bests = xfs_dir3_free_bests_p(mp, free);
621 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
622 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
623 XFS_ERRLEVEL_LOW, mp);
624 if (curfdb != newfdb)
625 xfs_trans_brelse(tp, curbp);
626 return XFS_ERROR(EFSCORRUPTED);
629 if (be16_to_cpu(bests[fi]) >= length)
633 /* Didn't find any space */
636 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
638 /* Giving back a free block. */
639 state->extravalid = 1;
640 state->extrablk.bp = curbp;
641 state->extrablk.index = fi;
642 state->extrablk.blkno = curfdb;
645 * Important: this magic number is not in the buffer - it's for
646 * buffer type information and therefore only the free/data type
647 * matters here, not whether CRCs are enabled or not.
649 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
651 state->extravalid = 0;
654 * Return the index, that will be the insertion point.
657 return XFS_ERROR(ENOENT);
661 * Look up a leaf entry in a node-format leaf block.
662 * The extrablk in state a data block.
665 xfs_dir2_leafn_lookup_for_entry(
666 struct xfs_buf *bp, /* leaf buffer */
667 xfs_da_args_t *args, /* operation arguments */
668 int *indexp, /* out: leaf entry index */
669 xfs_da_state_t *state) /* state to fill in */
671 struct xfs_buf *curbp = NULL; /* current data/free buffer */
672 xfs_dir2_db_t curdb = -1; /* current data block number */
673 xfs_dir2_data_entry_t *dep; /* data block entry */
674 xfs_inode_t *dp; /* incore directory inode */
675 int error; /* error return value */
676 int index; /* leaf entry index */
677 xfs_dir2_leaf_t *leaf; /* leaf structure */
678 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
679 xfs_mount_t *mp; /* filesystem mount point */
680 xfs_dir2_db_t newdb; /* new data block number */
681 xfs_trans_t *tp; /* transaction pointer */
682 enum xfs_dacmp cmp; /* comparison result */
683 struct xfs_dir2_leaf_entry *ents;
684 struct xfs_dir3_icleaf_hdr leafhdr;
690 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
691 ents = dp->d_ops->leaf_ents_p(leaf);
693 xfs_dir3_leaf_check(dp, bp);
694 ASSERT(leafhdr.count > 0);
697 * Look up the hash value in the leaf entries.
699 index = xfs_dir2_leaf_search_hash(args, bp);
701 * Do we have a buffer coming in?
703 if (state->extravalid) {
704 curbp = state->extrablk.bp;
705 curdb = state->extrablk.blkno;
708 * Loop over leaf entries with the right hash value.
710 for (lep = &ents[index];
711 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
714 * Skip stale leaf entries.
716 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
719 * Pull the data block number from the entry.
721 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
723 * Not adding a new entry, so we really want to find
724 * the name given to us.
726 * If it's a different data block, go get it.
728 if (newdb != curdb) {
730 * If we had a block before that we aren't saving
731 * for a CI name, drop it
733 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
734 curdb != state->extrablk.blkno))
735 xfs_trans_brelse(tp, curbp);
737 * If needing the block that is saved with a CI match,
738 * use it otherwise read in the new data block.
740 if (args->cmpresult != XFS_CMP_DIFFERENT &&
741 newdb == state->extrablk.blkno) {
742 ASSERT(state->extravalid);
743 curbp = state->extrablk.bp;
745 error = xfs_dir3_data_read(tp, dp,
746 xfs_dir2_db_to_da(mp, newdb),
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(mp, be32_to_cpu(lep->address)));
760 * Compare the entry and if it's an exact match, return
761 * EEXIST immediately. If it's the first case-insensitive
762 * match, store the block & inode number and continue looking.
764 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
765 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
766 /* If there is a CI match block, drop it */
767 if (args->cmpresult != XFS_CMP_DIFFERENT &&
768 curdb != state->extrablk.blkno)
769 xfs_trans_brelse(tp, state->extrablk.bp);
770 args->cmpresult = cmp;
771 args->inumber = be64_to_cpu(dep->inumber);
772 args->filetype = dp->d_ops->data_get_ftype(dep);
774 state->extravalid = 1;
775 state->extrablk.bp = curbp;
776 state->extrablk.blkno = curdb;
777 state->extrablk.index = (int)((char *)dep -
778 (char *)curbp->b_addr);
779 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
780 curbp->b_ops = &xfs_dir3_data_buf_ops;
781 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
782 if (cmp == XFS_CMP_EXACT)
783 return XFS_ERROR(EEXIST);
786 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
788 if (args->cmpresult == XFS_CMP_DIFFERENT) {
789 /* Giving back last used data block. */
790 state->extravalid = 1;
791 state->extrablk.bp = curbp;
792 state->extrablk.index = -1;
793 state->extrablk.blkno = curdb;
794 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
795 curbp->b_ops = &xfs_dir3_data_buf_ops;
796 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
798 /* If the curbp is not the CI match block, drop it */
799 if (state->extrablk.bp != curbp)
800 xfs_trans_brelse(tp, curbp);
803 state->extravalid = 0;
806 return XFS_ERROR(ENOENT);
810 * Look up a leaf entry in a node-format leaf block.
811 * If this is an addname then the extrablk in state is a freespace block,
812 * otherwise it's a data block.
815 xfs_dir2_leafn_lookup_int(
816 struct xfs_buf *bp, /* leaf buffer */
817 xfs_da_args_t *args, /* operation arguments */
818 int *indexp, /* out: leaf entry index */
819 xfs_da_state_t *state) /* state to fill in */
821 if (args->op_flags & XFS_DA_OP_ADDNAME)
822 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
824 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
828 * Move count leaf entries from source to destination leaf.
829 * Log entries and headers. Stale entries are preserved.
832 xfs_dir3_leafn_moveents(
833 xfs_da_args_t *args, /* operation arguments */
834 struct xfs_buf *bp_s, /* source */
835 struct xfs_dir3_icleaf_hdr *shdr,
836 struct xfs_dir2_leaf_entry *sents,
837 int start_s,/* source leaf index */
838 struct xfs_buf *bp_d, /* destination */
839 struct xfs_dir3_icleaf_hdr *dhdr,
840 struct xfs_dir2_leaf_entry *dents,
841 int start_d,/* destination leaf index */
842 int count) /* count of leaves to copy */
844 struct xfs_trans *tp = args->trans;
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(tp, args->dp, 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(tp, args->dp, bp_d,
886 start_d, start_d + count - 1);
889 * If there are source entries after the ones we copied,
890 * delete the ones we copied by sliding the next ones down.
892 if (start_s + count < shdr->count) {
893 memmove(&sents[start_s], &sents[start_s + count],
894 count * sizeof(xfs_dir2_leaf_entry_t));
895 xfs_dir3_leaf_log_ents(tp, args->dp, bp_s,
896 start_s, start_s + count - 1);
900 * Update the headers and log them.
902 shdr->count -= count;
903 shdr->stale -= stale;
904 dhdr->count += count;
905 dhdr->stale += stale;
909 * Determine the sort order of two leaf blocks.
910 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
913 xfs_dir2_leafn_order(
914 struct xfs_inode *dp,
915 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
916 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
918 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
919 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
920 struct xfs_dir2_leaf_entry *ents1;
921 struct xfs_dir2_leaf_entry *ents2;
922 struct xfs_dir3_icleaf_hdr hdr1;
923 struct xfs_dir3_icleaf_hdr hdr2;
925 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
926 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
927 ents1 = dp->d_ops->leaf_ents_p(leaf1);
928 ents2 = dp->d_ops->leaf_ents_p(leaf2);
930 if (hdr1.count > 0 && hdr2.count > 0 &&
931 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
932 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
933 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
939 * Rebalance leaf entries between two leaf blocks.
940 * This is actually only called when the second block is new,
941 * though the code deals with the general case.
942 * A new entry will be inserted in one of the blocks, and that
943 * entry is taken into account when balancing.
946 xfs_dir2_leafn_rebalance(
947 xfs_da_state_t *state, /* btree cursor */
948 xfs_da_state_blk_t *blk1, /* first btree block */
949 xfs_da_state_blk_t *blk2) /* second btree block */
951 xfs_da_args_t *args; /* operation arguments */
952 int count; /* count (& direction) leaves */
953 int isleft; /* new goes in left leaf */
954 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
955 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
956 int mid; /* midpoint leaf index */
957 #if defined(DEBUG) || defined(XFS_WARN)
958 int oldstale; /* old count of stale leaves */
960 int oldsum; /* old total leaf count */
961 int swap; /* swapped leaf blocks */
962 struct xfs_dir2_leaf_entry *ents1;
963 struct xfs_dir2_leaf_entry *ents2;
964 struct xfs_dir3_icleaf_hdr hdr1;
965 struct xfs_dir3_icleaf_hdr hdr2;
966 struct xfs_inode *dp = state->args->dp;
970 * If the block order is wrong, swap the arguments.
972 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
973 xfs_da_state_blk_t *tmp; /* temp for block swap */
979 leaf1 = blk1->bp->b_addr;
980 leaf2 = blk2->bp->b_addr;
981 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
982 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
983 ents1 = dp->d_ops->leaf_ents_p(leaf1);
984 ents2 = dp->d_ops->leaf_ents_p(leaf2);
986 oldsum = hdr1.count + hdr2.count;
987 #if defined(DEBUG) || defined(XFS_WARN)
988 oldstale = hdr1.stale + hdr2.stale;
993 * If the old leaf count was odd then the new one will be even,
994 * so we need to divide the new count evenly.
997 xfs_dahash_t midhash; /* middle entry hash value */
999 if (mid >= hdr1.count)
1000 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
1002 midhash = be32_to_cpu(ents1[mid].hashval);
1003 isleft = args->hashval <= midhash;
1006 * If the old count is even then the new count is odd, so there's
1007 * no preferred side for the new entry.
1008 * Pick the left one.
1013 * Calculate moved entry count. Positive means left-to-right,
1014 * negative means right-to-left. Then move the entries.
1016 count = hdr1.count - mid + (isleft == 0);
1018 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1019 hdr1.count - count, blk2->bp,
1020 &hdr2, ents2, 0, count);
1022 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1023 blk1->bp, &hdr1, ents1,
1026 ASSERT(hdr1.count + hdr2.count == oldsum);
1027 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1029 /* log the changes made when moving the entries */
1030 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1031 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
1032 xfs_dir3_leaf_log_header(args->trans, dp, blk1->bp);
1033 xfs_dir3_leaf_log_header(args->trans, dp, blk2->bp);
1035 xfs_dir3_leaf_check(dp, blk1->bp);
1036 xfs_dir3_leaf_check(dp, blk2->bp);
1039 * Mark whether we're inserting into the old or new leaf.
1041 if (hdr1.count < hdr2.count)
1042 state->inleaf = swap;
1043 else if (hdr1.count > hdr2.count)
1044 state->inleaf = !swap;
1046 state->inleaf = swap ^ (blk1->index <= hdr1.count);
1048 * Adjust the expected index for insertion.
1051 blk2->index = blk1->index - hdr1.count;
1054 * Finally sanity check just to make sure we are not returning a
1057 if (blk2->index < 0) {
1060 xfs_alert(dp->i_mount,
1061 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1062 __func__, blk1->index);
1067 xfs_dir3_data_block_free(
1068 xfs_da_args_t *args,
1069 struct xfs_dir2_data_hdr *hdr,
1070 struct xfs_dir2_free *free,
1073 struct xfs_buf *fbp,
1076 struct xfs_trans *tp = args->trans;
1079 struct xfs_dir3_icfree_hdr freehdr;
1080 struct xfs_inode *dp = args->dp;
1082 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1084 bests = xfs_dir3_free_bests_p(tp->t_mountp, 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(tp, 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(tp, 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(tp, 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 xfs_mount_t *mp; /* filesystem mount point */
1170 int needlog; /* need to log data header */
1171 int needscan; /* need to rescan data frees */
1172 xfs_trans_t *tp; /* transaction pointer */
1173 struct xfs_dir2_data_free *bf; /* bestfree table */
1174 struct xfs_dir3_icleaf_hdr leafhdr;
1175 struct xfs_dir2_leaf_entry *ents;
1177 trace_xfs_dir2_leafn_remove(args, index);
1183 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1184 ents = dp->d_ops->leaf_ents_p(leaf);
1187 * Point to the entry we're removing.
1192 * Extract the data block and offset from the entry.
1194 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1195 ASSERT(dblk->blkno == db);
1196 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
1197 ASSERT(dblk->index == off);
1200 * Kill the leaf entry by marking it stale.
1201 * Log the leaf block changes.
1204 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
1205 xfs_dir3_leaf_log_header(tp, dp, bp);
1207 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1208 xfs_dir3_leaf_log_ents(tp, dp, bp, index, index);
1211 * Make the data entry free. Keep track of the longest freespace
1212 * in the data block in case it changes.
1216 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1217 bf = dp->d_ops->data_bestfree_p(hdr);
1218 longest = be16_to_cpu(bf[0].length);
1219 needlog = needscan = 0;
1220 xfs_dir2_data_make_free(tp, dp, dbp, off,
1221 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
1223 * Rescan the data block freespaces for bestfree.
1224 * Log the data block header if needed.
1227 xfs_dir2_data_freescan(dp, hdr, &needlog);
1229 xfs_dir2_data_log_header(tp, dp, dbp);
1230 xfs_dir3_data_check(dp, dbp);
1232 * If the longest data block freespace changes, need to update
1233 * the corresponding freeblock entry.
1235 if (longest < be16_to_cpu(bf[0].length)) {
1236 int error; /* error return value */
1237 struct xfs_buf *fbp; /* freeblock buffer */
1238 xfs_dir2_db_t fdb; /* freeblock block number */
1239 int findex; /* index in freeblock entries */
1240 xfs_dir2_free_t *free; /* freeblock structure */
1243 * Convert the data block number to a free block,
1244 * read in the free block.
1246 fdb = xfs_dir2_db_to_fdb(mp, db);
1247 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(mp, fdb),
1254 struct xfs_dir3_icfree_hdr freehdr;
1255 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1256 ASSERT(freehdr.firstdb == xfs_dir3_free_max_bests(mp) *
1257 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1261 * Calculate which entry we need to fix.
1263 findex = xfs_dir2_db_to_fdindex(mp, 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 == mp->m_dirblksize -
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)) <
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 xfs_mount_t *mp; /* filesystem mount point */
1321 struct xfs_inode *dp;
1324 * Allocate space for a new leaf node.
1329 ASSERT(args != NULL);
1330 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1331 error = xfs_da_grow_inode(args, &blkno);
1336 * Initialize the new leaf block.
1338 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(mp, blkno),
1339 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1343 newblk->blkno = blkno;
1344 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1346 * Rebalance the entries across the two leaves, link the new
1347 * block into the leaves.
1349 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1350 error = xfs_da3_blk_link(state, oldblk, newblk);
1355 * Insert the new entry in the correct block.
1358 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1360 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1362 * Update last hashval in each block since we added the name.
1364 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1365 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1366 xfs_dir3_leaf_check(dp, oldblk->bp);
1367 xfs_dir3_leaf_check(dp, newblk->bp);
1372 * Check a leaf block and its neighbors to see if the block should be
1373 * collapsed into one or the other neighbor. Always keep the block
1374 * with the smaller block number.
1375 * If the current block is over 50% full, don't try to join it, return 0.
1376 * If the block is empty, fill in the state structure and return 2.
1377 * If it can be collapsed, fill in the state structure and return 1.
1378 * If nothing can be done, return 0.
1381 xfs_dir2_leafn_toosmall(
1382 xfs_da_state_t *state, /* btree cursor */
1383 int *action) /* resulting action to take */
1385 xfs_da_state_blk_t *blk; /* leaf block */
1386 xfs_dablk_t blkno; /* leaf block number */
1387 struct xfs_buf *bp; /* leaf buffer */
1388 int bytes; /* bytes in use */
1389 int count; /* leaf live entry count */
1390 int error; /* error return value */
1391 int forward; /* sibling block direction */
1392 int i; /* sibling counter */
1393 xfs_dir2_leaf_t *leaf; /* leaf structure */
1394 int rval; /* result from path_shift */
1395 struct xfs_dir3_icleaf_hdr leafhdr;
1396 struct xfs_dir2_leaf_entry *ents;
1397 struct xfs_inode *dp = state->args->dp;
1400 * Check for the degenerate case of the block being over 50% full.
1401 * If so, it's not worth even looking to see if we might be able
1402 * to coalesce with a sibling.
1404 blk = &state->path.blk[state->path.active - 1];
1405 leaf = blk->bp->b_addr;
1406 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
1407 ents = dp->d_ops->leaf_ents_p(leaf);
1408 xfs_dir3_leaf_check(dp, blk->bp);
1410 count = leafhdr.count - leafhdr.stale;
1411 bytes = dp->d_ops->leaf_hdr_size() + count * sizeof(ents[0]);
1412 if (bytes > (state->blocksize >> 1)) {
1414 * Blk over 50%, don't try to join.
1420 * Check for the degenerate case of the block being empty.
1421 * If the block is empty, we'll simply delete it, no need to
1422 * coalesce it with a sibling block. We choose (arbitrarily)
1423 * to merge with the forward block unless it is NULL.
1427 * Make altpath point to the block we want to keep and
1428 * path point to the block we want to drop (this one).
1430 forward = (leafhdr.forw != 0);
1431 memcpy(&state->altpath, &state->path, sizeof(state->path));
1432 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1436 *action = rval ? 2 : 0;
1440 * Examine each sibling block to see if we can coalesce with
1441 * at least 25% free space to spare. We need to figure out
1442 * whether to merge with the forward or the backward block.
1443 * We prefer coalescing with the lower numbered sibling so as
1444 * to shrink a directory over time.
1446 forward = leafhdr.forw < leafhdr.back;
1447 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1448 struct xfs_dir3_icleaf_hdr hdr2;
1450 blkno = forward ? leafhdr.forw : leafhdr.back;
1454 * Read the sibling leaf block.
1456 error = xfs_dir3_leafn_read(state->args->trans, dp,
1462 * Count bytes in the two blocks combined.
1464 count = leafhdr.count - leafhdr.stale;
1465 bytes = state->blocksize - (state->blocksize >> 2);
1468 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
1469 ents = dp->d_ops->leaf_ents_p(leaf);
1470 count += hdr2.count - hdr2.stale;
1471 bytes -= count * sizeof(ents[0]);
1474 * Fits with at least 25% to spare.
1478 xfs_trans_brelse(state->args->trans, bp);
1481 * Didn't like either block, give up.
1489 * Make altpath point to the block we want to keep (the lower
1490 * numbered block) and path point to the block we want to drop.
1492 memcpy(&state->altpath, &state->path, sizeof(state->path));
1493 if (blkno < blk->blkno)
1494 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1497 error = xfs_da3_path_shift(state, &state->path, forward, 0,
1502 *action = rval ? 0 : 1;
1507 * Move all the leaf entries from drop_blk to save_blk.
1508 * This is done as part of a join operation.
1511 xfs_dir2_leafn_unbalance(
1512 xfs_da_state_t *state, /* cursor */
1513 xfs_da_state_blk_t *drop_blk, /* dead block */
1514 xfs_da_state_blk_t *save_blk) /* surviving block */
1516 xfs_da_args_t *args; /* operation arguments */
1517 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1518 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1519 struct xfs_dir3_icleaf_hdr savehdr;
1520 struct xfs_dir3_icleaf_hdr drophdr;
1521 struct xfs_dir2_leaf_entry *sents;
1522 struct xfs_dir2_leaf_entry *dents;
1523 struct xfs_inode *dp = state->args->dp;
1526 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1527 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1528 drop_leaf = drop_blk->bp->b_addr;
1529 save_leaf = save_blk->bp->b_addr;
1531 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1532 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1533 sents = dp->d_ops->leaf_ents_p(save_leaf);
1534 dents = dp->d_ops->leaf_ents_p(drop_leaf);
1537 * If there are any stale leaf entries, take this opportunity
1541 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1543 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1546 * Move the entries from drop to the appropriate end of save.
1548 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1549 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1550 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1551 save_blk->bp, &savehdr, sents, 0,
1554 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1555 save_blk->bp, &savehdr, sents,
1556 savehdr.count, drophdr.count);
1557 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1559 /* log the changes made when moving the entries */
1560 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1561 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
1562 xfs_dir3_leaf_log_header(args->trans, dp, save_blk->bp);
1563 xfs_dir3_leaf_log_header(args->trans, dp, drop_blk->bp);
1565 xfs_dir3_leaf_check(dp, save_blk->bp);
1566 xfs_dir3_leaf_check(dp, drop_blk->bp);
1570 * Top-level node form directory addname routine.
1573 xfs_dir2_node_addname(
1574 xfs_da_args_t *args) /* operation arguments */
1576 xfs_da_state_blk_t *blk; /* leaf block for insert */
1577 int error; /* error return value */
1578 int rval; /* sub-return value */
1579 xfs_da_state_t *state; /* btree cursor */
1581 trace_xfs_dir2_node_addname(args);
1584 * Allocate and initialize the state (btree cursor).
1586 state = xfs_da_state_alloc();
1588 state->mp = args->dp->i_mount;
1589 state->blocksize = state->mp->m_dirblksize;
1590 state->node_ents = state->mp->m_dir_node_ents;
1592 * Look up the name. We're not supposed to find it, but
1593 * this gives us the insertion point.
1595 error = xfs_da3_node_lookup_int(state, &rval);
1598 if (rval != ENOENT) {
1602 * Add the data entry to a data block.
1603 * Extravalid is set to a freeblock found by lookup.
1605 rval = xfs_dir2_node_addname_int(args,
1606 state->extravalid ? &state->extrablk : NULL);
1610 blk = &state->path.blk[state->path.active - 1];
1611 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1613 * Add the new leaf entry.
1615 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1618 * It worked, fix the hash values up the btree.
1620 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1621 xfs_da3_fixhashpath(state, &state->path);
1624 * It didn't work, we need to split the leaf block.
1626 if (args->total == 0) {
1627 ASSERT(rval == ENOSPC);
1631 * Split the leaf block and insert the new entry.
1633 rval = xfs_da3_split(state);
1636 xfs_da_state_free(state);
1641 * Add the data entry for a node-format directory name addition.
1642 * The leaf entry is added in xfs_dir2_leafn_add.
1643 * We may enter with a freespace block that the lookup found.
1645 static int /* error */
1646 xfs_dir2_node_addname_int(
1647 xfs_da_args_t *args, /* operation arguments */
1648 xfs_da_state_blk_t *fblk) /* optional freespace block */
1650 xfs_dir2_data_hdr_t *hdr; /* data block header */
1651 xfs_dir2_db_t dbno; /* data block number */
1652 struct xfs_buf *dbp; /* data block buffer */
1653 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1654 xfs_inode_t *dp; /* incore directory inode */
1655 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1656 int error; /* error return value */
1657 xfs_dir2_db_t fbno; /* freespace block number */
1658 struct xfs_buf *fbp; /* freespace buffer */
1659 int findex; /* freespace entry index */
1660 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1661 xfs_dir2_db_t ifbno; /* initial freespace block no */
1662 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1663 int length; /* length of the new entry */
1664 int logfree; /* need to log free entry */
1665 xfs_mount_t *mp; /* filesystem mount point */
1666 int needlog; /* need to log data header */
1667 int needscan; /* need to rescan data frees */
1668 __be16 *tagp; /* data entry tag pointer */
1669 xfs_trans_t *tp; /* transaction pointer */
1671 struct xfs_dir3_icfree_hdr freehdr;
1672 struct xfs_dir2_data_free *bf;
1677 length = dp->d_ops->data_entsize(args->namelen);
1679 * If we came in with a freespace block that means that lookup
1680 * found an entry with our hash value. This is the freespace
1681 * block for that data entry.
1686 * Remember initial freespace block number.
1688 ifbno = fblk->blkno;
1690 findex = fblk->index;
1691 bests = xfs_dir3_free_bests_p(mp, free);
1692 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1695 * This means the free entry showed that the data block had
1696 * space for our entry, so we remembered it.
1697 * Use that data block.
1700 ASSERT(findex < freehdr.nvalid);
1701 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1702 ASSERT(be16_to_cpu(bests[findex]) >= length);
1703 dbno = freehdr.firstdb + findex;
1706 * The data block looked at didn't have enough room.
1707 * We'll start at the beginning of the freespace entries.
1714 * Didn't come in with a freespace block, so no data block.
1722 * If we don't have a data block yet, we're going to scan the
1723 * freespace blocks looking for one. Figure out what the
1724 * highest freespace block number is.
1727 xfs_fileoff_t fo; /* freespace block number */
1729 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1731 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
1735 * While we haven't identified a data block, search the freeblock
1736 * data for a good data block. If we find a null freeblock entry,
1737 * indicating a hole in the data blocks, remember that.
1739 while (dbno == -1) {
1741 * If we don't have a freeblock in hand, get the next one.
1745 * Happens the first time through unless lookup gave
1746 * us a freespace block to start with.
1749 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1751 * If it's ifbno we already looked at it.
1756 * If it's off the end we're done.
1758 if (fbno >= lastfbno)
1761 * Read the block. There can be holes in the
1762 * freespace blocks, so this might not succeed.
1763 * This should be really rare, so there's no reason
1766 error = xfs_dir2_free_try_read(tp, dp,
1767 xfs_dir2_db_to_da(mp, fbno),
1777 * Look at the current free entry. Is it good enough?
1779 * The bests initialisation should be where the bufer is read in
1780 * the above branch. But gcc is too stupid to realise that bests
1781 * and the freehdr are actually initialised if they are placed
1782 * there, so we have to do it here to avoid warnings. Blech.
1784 bests = xfs_dir3_free_bests_p(mp, free);
1785 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1786 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1787 be16_to_cpu(bests[findex]) >= length)
1788 dbno = freehdr.firstdb + findex;
1791 * Are we done with the freeblock?
1793 if (++findex == freehdr.nvalid) {
1797 xfs_trans_brelse(tp, fbp);
1799 if (fblk && fblk->bp)
1805 * If we don't have a data block, we need to allocate one and make
1806 * the freespace entries refer to it.
1808 if (unlikely(dbno == -1)) {
1810 * Not allowed to allocate, return failure.
1812 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
1813 return XFS_ERROR(ENOSPC);
1816 * Allocate and initialize the new data block.
1818 if (unlikely((error = xfs_dir2_grow_inode(args,
1819 XFS_DIR2_DATA_SPACE,
1821 (error = xfs_dir3_data_init(args, dbno, &dbp))))
1825 * If (somehow) we have a freespace block, get rid of it.
1828 xfs_trans_brelse(tp, fbp);
1829 if (fblk && fblk->bp)
1833 * Get the freespace block corresponding to the data block
1834 * that was just allocated.
1836 fbno = xfs_dir2_db_to_fdb(mp, dbno);
1837 error = xfs_dir2_free_try_read(tp, dp,
1838 xfs_dir2_db_to_da(mp, fbno),
1844 * If there wasn't a freespace block, the read will
1845 * return a NULL fbp. Allocate and initialize a new one.
1848 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1853 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
1855 "%s: dir ino %llu needed freesp block %lld for\n"
1856 " data block %lld, got %lld ifbno %llu lastfbno %d",
1857 __func__, (unsigned long long)dp->i_ino,
1858 (long long)xfs_dir2_db_to_fdb(mp, dbno),
1859 (long long)dbno, (long long)fbno,
1860 (unsigned long long)ifbno, lastfbno);
1863 " fblk 0x%p blkno %llu index %d magic 0x%x",
1865 (unsigned long long)fblk->blkno,
1869 xfs_alert(mp, " ... fblk is NULL");
1871 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1872 XFS_ERRLEVEL_LOW, mp);
1873 return XFS_ERROR(EFSCORRUPTED);
1877 * Get a buffer for the new block.
1879 error = xfs_dir3_free_get_buf(tp, dp, fbno, &fbp);
1883 bests = xfs_dir3_free_bests_p(mp, free);
1884 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1887 * Remember the first slot as our empty slot.
1889 freehdr.firstdb = (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1890 xfs_dir3_free_max_bests(mp);
1893 bests = xfs_dir3_free_bests_p(mp, free);
1894 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1898 * Set the freespace block index from the data block number.
1900 findex = xfs_dir2_db_to_fdindex(mp, dbno);
1902 * If it's after the end of the current entries in the
1903 * freespace block, extend that table.
1905 if (findex >= freehdr.nvalid) {
1906 ASSERT(findex < xfs_dir3_free_max_bests(mp));
1907 freehdr.nvalid = findex + 1;
1909 * Tag new entry so nused will go up.
1911 bests[findex] = cpu_to_be16(NULLDATAOFF);
1914 * If this entry was for an empty data block
1915 * (this should always be true) then update the header.
1917 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1919 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1920 xfs_dir2_free_log_header(tp, fbp);
1923 * Update the real value in the table.
1924 * We haven't allocated the data entry yet so this will
1928 bf = dp->d_ops->data_bestfree_p(hdr);
1929 bests[findex] = bf[0].length;
1933 * We had a data block so we don't have to make a new one.
1937 * If just checking, we succeeded.
1939 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
1943 * Read the data block in.
1945 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, 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(tp, dp, 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(tp, dp, 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(tp, dp, dbp);
1988 * If the freespace entry is now wrong, update it.
1990 bests = xfs_dir3_free_bests_p(mp, 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(tp, 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;
2030 state->blocksize = state->mp->m_dirblksize;
2031 state->node_ents = state->mp->m_dir_node_ents;
2033 * Fill in the path to the entry in the cursor.
2035 error = xfs_da3_node_lookup_int(state, &rval);
2038 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
2039 /* If a CI match, dup the actual name and return EEXIST */
2040 xfs_dir2_data_entry_t *dep;
2042 dep = (xfs_dir2_data_entry_t *)
2043 ((char *)state->extrablk.bp->b_addr +
2044 state->extrablk.index);
2045 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2048 * Release the btree blocks and leaf block.
2050 for (i = 0; i < state->path.active; i++) {
2051 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2052 state->path.blk[i].bp = NULL;
2055 * Release the data block if we have it.
2057 if (state->extravalid && state->extrablk.bp) {
2058 xfs_trans_brelse(args->trans, state->extrablk.bp);
2059 state->extrablk.bp = NULL;
2061 xfs_da_state_free(state);
2066 * Remove an entry from a node-format directory.
2069 xfs_dir2_node_removename(
2070 xfs_da_args_t *args) /* operation arguments */
2072 xfs_da_state_blk_t *blk; /* leaf block */
2073 int error; /* error return value */
2074 int rval; /* operation return value */
2075 xfs_da_state_t *state; /* btree cursor */
2077 trace_xfs_dir2_node_removename(args);
2080 * Allocate and initialize the btree cursor.
2082 state = xfs_da_state_alloc();
2084 state->mp = args->dp->i_mount;
2085 state->blocksize = state->mp->m_dirblksize;
2086 state->node_ents = state->mp->m_dir_node_ents;
2088 * Look up the entry we're deleting, set up the cursor.
2090 error = xfs_da3_node_lookup_int(state, &rval);
2094 * Didn't find it, upper layer screwed up.
2096 if (rval != EEXIST) {
2097 xfs_da_state_free(state);
2100 blk = &state->path.blk[state->path.active - 1];
2101 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2102 ASSERT(state->extravalid);
2104 * Remove the leaf and data entries.
2105 * Extrablk refers to the data block.
2107 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2108 &state->extrablk, &rval);
2112 * Fix the hash values up the btree.
2114 xfs_da3_fixhashpath(state, &state->path);
2116 * If we need to join leaf blocks, do it.
2118 if (rval && state->path.active > 1)
2119 error = xfs_da3_join(state);
2121 * If no errors so far, try conversion to leaf format.
2124 error = xfs_dir2_node_to_leaf(state);
2125 xfs_da_state_free(state);
2130 * Replace an entry's inode number in a node-format directory.
2133 xfs_dir2_node_replace(
2134 xfs_da_args_t *args) /* operation arguments */
2136 xfs_da_state_blk_t *blk; /* leaf block */
2137 xfs_dir2_data_hdr_t *hdr; /* data block header */
2138 xfs_dir2_data_entry_t *dep; /* data entry changed */
2139 int error; /* error return value */
2140 int i; /* btree level */
2141 xfs_ino_t inum; /* new inode number */
2142 xfs_dir2_leaf_t *leaf; /* leaf structure */
2143 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2144 int rval; /* internal return value */
2145 xfs_da_state_t *state; /* btree cursor */
2147 trace_xfs_dir2_node_replace(args);
2150 * Allocate and initialize the btree cursor.
2152 state = xfs_da_state_alloc();
2154 state->mp = args->dp->i_mount;
2155 state->blocksize = state->mp->m_dirblksize;
2156 state->node_ents = state->mp->m_dir_node_ents;
2157 inum = args->inumber;
2159 * Lookup the entry to change in the btree.
2161 error = xfs_da3_node_lookup_int(state, &rval);
2166 * It should be found, since the vnodeops layer has looked it up
2167 * and locked it. But paranoia is good.
2169 if (rval == EEXIST) {
2170 struct xfs_dir2_leaf_entry *ents;
2172 * Find the leaf entry.
2174 blk = &state->path.blk[state->path.active - 1];
2175 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2176 leaf = blk->bp->b_addr;
2177 ents = args->dp->d_ops->leaf_ents_p(leaf);
2178 lep = &ents[blk->index];
2179 ASSERT(state->extravalid);
2181 * Point to the data entry.
2183 hdr = state->extrablk.bp->b_addr;
2184 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2185 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2186 dep = (xfs_dir2_data_entry_t *)
2188 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
2189 ASSERT(inum != be64_to_cpu(dep->inumber));
2191 * Fill in the new inode number and log the entry.
2193 dep->inumber = cpu_to_be64(inum);
2194 args->dp->d_ops->data_put_ftype(dep, args->filetype);
2195 xfs_dir2_data_log_entry(args->trans, args->dp,
2196 state->extrablk.bp, dep);
2200 * Didn't find it, and we're holding a data block. Drop it.
2202 else if (state->extravalid) {
2203 xfs_trans_brelse(args->trans, state->extrablk.bp);
2204 state->extrablk.bp = NULL;
2207 * Release all the buffers in the cursor.
2209 for (i = 0; i < state->path.active; i++) {
2210 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2211 state->path.blk[i].bp = NULL;
2213 xfs_da_state_free(state);
2218 * Trim off a trailing empty freespace block.
2219 * Return (in rvalp) 1 if we did it, 0 if not.
2222 xfs_dir2_node_trim_free(
2223 xfs_da_args_t *args, /* operation arguments */
2224 xfs_fileoff_t fo, /* free block number */
2225 int *rvalp) /* out: did something */
2227 struct xfs_buf *bp; /* freespace buffer */
2228 xfs_inode_t *dp; /* incore directory inode */
2229 int error; /* error return code */
2230 xfs_dir2_free_t *free; /* freespace structure */
2231 xfs_mount_t *mp; /* filesystem mount point */
2232 xfs_trans_t *tp; /* transaction pointer */
2233 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.
2265 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
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.