1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_inode.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_trans.h"
20 #include "xfs_attr_leaf.h"
21 #include "xfs_error.h"
22 #include "xfs_trace.h"
23 #include "xfs_buf_item.h"
29 * Routines to implement directories as Btrees of hashed names.
32 /*========================================================================
33 * Function prototypes for the kernel.
34 *========================================================================*/
37 * Routines used for growing the Btree.
39 STATIC int xfs_da3_root_split(xfs_da_state_t *state,
40 xfs_da_state_blk_t *existing_root,
41 xfs_da_state_blk_t *new_child);
42 STATIC int xfs_da3_node_split(xfs_da_state_t *state,
43 xfs_da_state_blk_t *existing_blk,
44 xfs_da_state_blk_t *split_blk,
45 xfs_da_state_blk_t *blk_to_add,
48 STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
49 xfs_da_state_blk_t *node_blk_1,
50 xfs_da_state_blk_t *node_blk_2);
51 STATIC void xfs_da3_node_add(xfs_da_state_t *state,
52 xfs_da_state_blk_t *old_node_blk,
53 xfs_da_state_blk_t *new_node_blk);
56 * Routines used for shrinking the Btree.
58 STATIC int xfs_da3_root_join(xfs_da_state_t *state,
59 xfs_da_state_blk_t *root_blk);
60 STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
61 STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
62 xfs_da_state_blk_t *drop_blk);
63 STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
64 xfs_da_state_blk_t *src_node_blk,
65 xfs_da_state_blk_t *dst_node_blk);
70 STATIC int xfs_da3_blk_unlink(xfs_da_state_t *state,
71 xfs_da_state_blk_t *drop_blk,
72 xfs_da_state_blk_t *save_blk);
75 struct kmem_cache *xfs_da_state_cache; /* anchor for dir/attr state */
78 * Allocate a dir-state structure.
79 * We don't put them on the stack since they're large.
83 struct xfs_da_args *args)
85 struct xfs_da_state *state;
87 state = kmem_cache_zalloc(xfs_da_state_cache, GFP_NOFS | __GFP_NOFAIL);
89 state->mp = args->dp->i_mount;
94 * Kill the altpath contents of a da-state structure.
97 xfs_da_state_kill_altpath(xfs_da_state_t *state)
101 for (i = 0; i < state->altpath.active; i++)
102 state->altpath.blk[i].bp = NULL;
103 state->altpath.active = 0;
107 * Free a da-state structure.
110 xfs_da_state_free(xfs_da_state_t *state)
112 xfs_da_state_kill_altpath(state);
114 memset((char *)state, 0, sizeof(*state));
116 kmem_cache_free(xfs_da_state_cache, state);
119 static inline int xfs_dabuf_nfsb(struct xfs_mount *mp, int whichfork)
121 if (whichfork == XFS_DATA_FORK)
122 return mp->m_dir_geo->fsbcount;
123 return mp->m_attr_geo->fsbcount;
127 xfs_da3_node_hdr_from_disk(
128 struct xfs_mount *mp,
129 struct xfs_da3_icnode_hdr *to,
130 struct xfs_da_intnode *from)
132 if (xfs_has_crc(mp)) {
133 struct xfs_da3_intnode *from3 = (struct xfs_da3_intnode *)from;
135 to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
136 to->back = be32_to_cpu(from3->hdr.info.hdr.back);
137 to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
138 to->count = be16_to_cpu(from3->hdr.__count);
139 to->level = be16_to_cpu(from3->hdr.__level);
140 to->btree = from3->__btree;
141 ASSERT(to->magic == XFS_DA3_NODE_MAGIC);
143 to->forw = be32_to_cpu(from->hdr.info.forw);
144 to->back = be32_to_cpu(from->hdr.info.back);
145 to->magic = be16_to_cpu(from->hdr.info.magic);
146 to->count = be16_to_cpu(from->hdr.__count);
147 to->level = be16_to_cpu(from->hdr.__level);
148 to->btree = from->__btree;
149 ASSERT(to->magic == XFS_DA_NODE_MAGIC);
154 xfs_da3_node_hdr_to_disk(
155 struct xfs_mount *mp,
156 struct xfs_da_intnode *to,
157 struct xfs_da3_icnode_hdr *from)
159 if (xfs_has_crc(mp)) {
160 struct xfs_da3_intnode *to3 = (struct xfs_da3_intnode *)to;
162 ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
163 to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
164 to3->hdr.info.hdr.back = cpu_to_be32(from->back);
165 to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
166 to3->hdr.__count = cpu_to_be16(from->count);
167 to3->hdr.__level = cpu_to_be16(from->level);
169 ASSERT(from->magic == XFS_DA_NODE_MAGIC);
170 to->hdr.info.forw = cpu_to_be32(from->forw);
171 to->hdr.info.back = cpu_to_be32(from->back);
172 to->hdr.info.magic = cpu_to_be16(from->magic);
173 to->hdr.__count = cpu_to_be16(from->count);
174 to->hdr.__level = cpu_to_be16(from->level);
179 * Verify an xfs_da3_blkinfo structure. Note that the da3 fields are only
180 * accessible on v5 filesystems. This header format is common across da node,
181 * attr leaf and dir leaf blocks.
184 xfs_da3_blkinfo_verify(
186 struct xfs_da3_blkinfo *hdr3)
188 struct xfs_mount *mp = bp->b_mount;
189 struct xfs_da_blkinfo *hdr = &hdr3->hdr;
191 if (!xfs_verify_magic16(bp, hdr->magic))
192 return __this_address;
194 if (xfs_has_crc(mp)) {
195 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
196 return __this_address;
197 if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
198 return __this_address;
199 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
200 return __this_address;
206 static xfs_failaddr_t
210 struct xfs_mount *mp = bp->b_mount;
211 struct xfs_da_intnode *hdr = bp->b_addr;
212 struct xfs_da3_icnode_hdr ichdr;
215 xfs_da3_node_hdr_from_disk(mp, &ichdr, hdr);
217 fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
221 if (ichdr.level == 0)
222 return __this_address;
223 if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
224 return __this_address;
225 if (ichdr.count == 0)
226 return __this_address;
229 * we don't know if the node is for and attribute or directory tree,
230 * so only fail if the count is outside both bounds
232 if (ichdr.count > mp->m_dir_geo->node_ents &&
233 ichdr.count > mp->m_attr_geo->node_ents)
234 return __this_address;
236 /* XXX: hash order check? */
242 xfs_da3_node_write_verify(
245 struct xfs_mount *mp = bp->b_mount;
246 struct xfs_buf_log_item *bip = bp->b_log_item;
247 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
250 fa = xfs_da3_node_verify(bp);
252 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
256 if (!xfs_has_crc(mp))
260 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
262 xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
266 * leaf/node format detection on trees is sketchy, so a node read can be done on
267 * leaf level blocks when detection identifies the tree as a node format tree
268 * incorrectly. In this case, we need to swap the verifier to match the correct
269 * format of the block being read.
272 xfs_da3_node_read_verify(
275 struct xfs_da_blkinfo *info = bp->b_addr;
278 switch (be16_to_cpu(info->magic)) {
279 case XFS_DA3_NODE_MAGIC:
280 if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
281 xfs_verifier_error(bp, -EFSBADCRC,
286 case XFS_DA_NODE_MAGIC:
287 fa = xfs_da3_node_verify(bp);
289 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
291 case XFS_ATTR_LEAF_MAGIC:
292 case XFS_ATTR3_LEAF_MAGIC:
293 bp->b_ops = &xfs_attr3_leaf_buf_ops;
294 bp->b_ops->verify_read(bp);
296 case XFS_DIR2_LEAFN_MAGIC:
297 case XFS_DIR3_LEAFN_MAGIC:
298 bp->b_ops = &xfs_dir3_leafn_buf_ops;
299 bp->b_ops->verify_read(bp);
302 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
307 /* Verify the structure of a da3 block. */
308 static xfs_failaddr_t
309 xfs_da3_node_verify_struct(
312 struct xfs_da_blkinfo *info = bp->b_addr;
314 switch (be16_to_cpu(info->magic)) {
315 case XFS_DA3_NODE_MAGIC:
316 case XFS_DA_NODE_MAGIC:
317 return xfs_da3_node_verify(bp);
318 case XFS_ATTR_LEAF_MAGIC:
319 case XFS_ATTR3_LEAF_MAGIC:
320 bp->b_ops = &xfs_attr3_leaf_buf_ops;
321 return bp->b_ops->verify_struct(bp);
322 case XFS_DIR2_LEAFN_MAGIC:
323 case XFS_DIR3_LEAFN_MAGIC:
324 bp->b_ops = &xfs_dir3_leafn_buf_ops;
325 return bp->b_ops->verify_struct(bp);
327 return __this_address;
331 const struct xfs_buf_ops xfs_da3_node_buf_ops = {
332 .name = "xfs_da3_node",
333 .magic16 = { cpu_to_be16(XFS_DA_NODE_MAGIC),
334 cpu_to_be16(XFS_DA3_NODE_MAGIC) },
335 .verify_read = xfs_da3_node_read_verify,
336 .verify_write = xfs_da3_node_write_verify,
337 .verify_struct = xfs_da3_node_verify_struct,
341 xfs_da3_node_set_type(
342 struct xfs_trans *tp,
345 struct xfs_da_blkinfo *info = bp->b_addr;
347 switch (be16_to_cpu(info->magic)) {
348 case XFS_DA_NODE_MAGIC:
349 case XFS_DA3_NODE_MAGIC:
350 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
352 case XFS_ATTR_LEAF_MAGIC:
353 case XFS_ATTR3_LEAF_MAGIC:
354 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_ATTR_LEAF_BUF);
356 case XFS_DIR2_LEAFN_MAGIC:
357 case XFS_DIR3_LEAFN_MAGIC:
358 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
361 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, tp->t_mountp,
362 info, sizeof(*info));
363 xfs_trans_brelse(tp, bp);
364 return -EFSCORRUPTED;
370 struct xfs_trans *tp,
371 struct xfs_inode *dp,
373 struct xfs_buf **bpp,
378 error = xfs_da_read_buf(tp, dp, bno, 0, bpp, whichfork,
379 &xfs_da3_node_buf_ops);
380 if (error || !*bpp || !tp)
382 return xfs_da3_node_set_type(tp, *bpp);
386 xfs_da3_node_read_mapped(
387 struct xfs_trans *tp,
388 struct xfs_inode *dp,
389 xfs_daddr_t mappedbno,
390 struct xfs_buf **bpp,
393 struct xfs_mount *mp = dp->i_mount;
396 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, mappedbno,
397 XFS_FSB_TO_BB(mp, xfs_dabuf_nfsb(mp, whichfork)), 0,
398 bpp, &xfs_da3_node_buf_ops);
402 if (whichfork == XFS_ATTR_FORK)
403 xfs_buf_set_ref(*bpp, XFS_ATTR_BTREE_REF);
405 xfs_buf_set_ref(*bpp, XFS_DIR_BTREE_REF);
409 return xfs_da3_node_set_type(tp, *bpp);
412 /*========================================================================
413 * Routines used for growing the Btree.
414 *========================================================================*/
417 * Create the initial contents of an intermediate node.
421 struct xfs_da_args *args,
424 struct xfs_buf **bpp,
427 struct xfs_da_intnode *node;
428 struct xfs_trans *tp = args->trans;
429 struct xfs_mount *mp = tp->t_mountp;
430 struct xfs_da3_icnode_hdr ichdr = {0};
433 struct xfs_inode *dp = args->dp;
435 trace_xfs_da_node_create(args);
436 ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
438 error = xfs_da_get_buf(tp, dp, blkno, &bp, whichfork);
441 bp->b_ops = &xfs_da3_node_buf_ops;
442 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
445 if (xfs_has_crc(mp)) {
446 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
448 memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
449 ichdr.magic = XFS_DA3_NODE_MAGIC;
450 hdr3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
451 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
452 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
454 ichdr.magic = XFS_DA_NODE_MAGIC;
458 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &ichdr);
459 xfs_trans_log_buf(tp, bp,
460 XFS_DA_LOGRANGE(node, &node->hdr, args->geo->node_hdr_size));
467 * Split a leaf node, rebalance, then possibly split
468 * intermediate nodes, rebalance, etc.
472 struct xfs_da_state *state)
474 struct xfs_da_state_blk *oldblk;
475 struct xfs_da_state_blk *newblk;
476 struct xfs_da_state_blk *addblk;
477 struct xfs_da_intnode *node;
483 trace_xfs_da_split(state->args);
486 * Walk back up the tree splitting/inserting/adjusting as necessary.
487 * If we need to insert and there isn't room, split the node, then
488 * decide which fragment to insert the new block from below into.
489 * Note that we may split the root this way, but we need more fixup.
491 max = state->path.active - 1;
492 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
493 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
494 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
496 addblk = &state->path.blk[max]; /* initial dummy value */
497 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
498 oldblk = &state->path.blk[i];
499 newblk = &state->altpath.blk[i];
502 * If a leaf node then
503 * Allocate a new leaf node, then rebalance across them.
504 * else if an intermediate node then
505 * We split on the last layer, must we split the node?
507 switch (oldblk->magic) {
508 case XFS_ATTR_LEAF_MAGIC:
509 error = xfs_attr3_leaf_split(state, oldblk, newblk);
510 if ((error != 0) && (error != -ENOSPC)) {
511 return error; /* GROT: attr is inconsistent */
518 * Entry wouldn't fit, split the leaf again. The new
519 * extrablk will be consumed by xfs_da3_node_split if
522 state->extravalid = 1;
524 state->extraafter = 0; /* before newblk */
525 trace_xfs_attr_leaf_split_before(state->args);
526 error = xfs_attr3_leaf_split(state, oldblk,
529 state->extraafter = 1; /* after newblk */
530 trace_xfs_attr_leaf_split_after(state->args);
531 error = xfs_attr3_leaf_split(state, newblk,
535 return error; /* GROT: attr inconsistent */
538 case XFS_DIR2_LEAFN_MAGIC:
539 error = xfs_dir2_leafn_split(state, oldblk, newblk);
544 case XFS_DA_NODE_MAGIC:
545 error = xfs_da3_node_split(state, oldblk, newblk, addblk,
549 return error; /* GROT: dir is inconsistent */
551 * Record the newly split block for the next time thru?
561 * Update the btree to show the new hashval for this child.
563 xfs_da3_fixhashpath(state, &state->path);
569 * xfs_da3_node_split() should have consumed any extra blocks we added
570 * during a double leaf split in the attr fork. This is guaranteed as
571 * we can't be here if the attr fork only has a single leaf block.
573 ASSERT(state->extravalid == 0 ||
574 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
577 * Split the root node.
579 ASSERT(state->path.active == 0);
580 oldblk = &state->path.blk[0];
581 error = xfs_da3_root_split(state, oldblk, addblk);
586 * Update pointers to the node which used to be block 0 and just got
587 * bumped because of the addition of a new root node. Note that the
588 * original block 0 could be at any position in the list of blocks in
591 * Note: the magic numbers and sibling pointers are in the same physical
592 * place for both v2 and v3 headers (by design). Hence it doesn't matter
593 * which version of the xfs_da_intnode structure we use here as the
594 * result will be the same using either structure.
596 node = oldblk->bp->b_addr;
597 if (node->hdr.info.forw) {
598 if (be32_to_cpu(node->hdr.info.forw) != addblk->blkno) {
599 xfs_buf_mark_corrupt(oldblk->bp);
600 error = -EFSCORRUPTED;
603 node = addblk->bp->b_addr;
604 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
605 xfs_trans_log_buf(state->args->trans, addblk->bp,
606 XFS_DA_LOGRANGE(node, &node->hdr.info,
607 sizeof(node->hdr.info)));
609 node = oldblk->bp->b_addr;
610 if (node->hdr.info.back) {
611 if (be32_to_cpu(node->hdr.info.back) != addblk->blkno) {
612 xfs_buf_mark_corrupt(oldblk->bp);
613 error = -EFSCORRUPTED;
616 node = addblk->bp->b_addr;
617 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
618 xfs_trans_log_buf(state->args->trans, addblk->bp,
619 XFS_DA_LOGRANGE(node, &node->hdr.info,
620 sizeof(node->hdr.info)));
628 * Split the root. We have to create a new root and point to the two
629 * parts (the split old root) that we just created. Copy block zero to
630 * the EOF, extending the inode in process.
632 STATIC int /* error */
634 struct xfs_da_state *state,
635 struct xfs_da_state_blk *blk1,
636 struct xfs_da_state_blk *blk2)
638 struct xfs_da_intnode *node;
639 struct xfs_da_intnode *oldroot;
640 struct xfs_da_node_entry *btree;
641 struct xfs_da3_icnode_hdr nodehdr;
642 struct xfs_da_args *args;
644 struct xfs_inode *dp;
645 struct xfs_trans *tp;
646 struct xfs_dir2_leaf *leaf;
652 trace_xfs_da_root_split(state->args);
655 * Copy the existing (incorrect) block from the root node position
656 * to a free space somewhere.
659 error = xfs_da_grow_inode(args, &blkno);
665 error = xfs_da_get_buf(tp, dp, blkno, &bp, args->whichfork);
669 oldroot = blk1->bp->b_addr;
670 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
671 oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
672 struct xfs_da3_icnode_hdr icnodehdr;
674 xfs_da3_node_hdr_from_disk(dp->i_mount, &icnodehdr, oldroot);
675 btree = icnodehdr.btree;
676 size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
677 level = icnodehdr.level;
680 * we are about to copy oldroot to bp, so set up the type
681 * of bp while we know exactly what it will be.
683 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
685 struct xfs_dir3_icleaf_hdr leafhdr;
687 leaf = (xfs_dir2_leaf_t *)oldroot;
688 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
690 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
691 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
692 size = (int)((char *)&leafhdr.ents[leafhdr.count] -
697 * we are about to copy oldroot to bp, so set up the type
698 * of bp while we know exactly what it will be.
700 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
704 * we can copy most of the information in the node from one block to
705 * another, but for CRC enabled headers we have to make sure that the
706 * block specific identifiers are kept intact. We update the buffer
709 memcpy(node, oldroot, size);
710 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
711 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
712 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
714 node3->hdr.info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
716 xfs_trans_log_buf(tp, bp, 0, size - 1);
718 bp->b_ops = blk1->bp->b_ops;
719 xfs_trans_buf_copy_type(bp, blk1->bp);
724 * Set up the new root node.
726 error = xfs_da3_node_create(args,
727 (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
728 level + 1, &bp, args->whichfork);
733 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
734 btree = nodehdr.btree;
735 btree[0].hashval = cpu_to_be32(blk1->hashval);
736 btree[0].before = cpu_to_be32(blk1->blkno);
737 btree[1].hashval = cpu_to_be32(blk2->hashval);
738 btree[1].before = cpu_to_be32(blk2->blkno);
740 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
743 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
744 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
745 ASSERT(blk1->blkno >= args->geo->leafblk &&
746 blk1->blkno < args->geo->freeblk);
747 ASSERT(blk2->blkno >= args->geo->leafblk &&
748 blk2->blkno < args->geo->freeblk);
752 /* Header is already logged by xfs_da_node_create */
753 xfs_trans_log_buf(tp, bp,
754 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
760 * Split the node, rebalance, then add the new entry.
762 STATIC int /* error */
764 struct xfs_da_state *state,
765 struct xfs_da_state_blk *oldblk,
766 struct xfs_da_state_blk *newblk,
767 struct xfs_da_state_blk *addblk,
771 struct xfs_da_intnode *node;
772 struct xfs_da3_icnode_hdr nodehdr;
777 struct xfs_inode *dp = state->args->dp;
779 trace_xfs_da_node_split(state->args);
781 node = oldblk->bp->b_addr;
782 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
785 * With V2 dirs the extra block is data or freespace.
787 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
788 newcount = 1 + useextra;
790 * Do we have to split the node?
792 if (nodehdr.count + newcount > state->args->geo->node_ents) {
794 * Allocate a new node, add to the doubly linked chain of
795 * nodes, then move some of our excess entries into it.
797 error = xfs_da_grow_inode(state->args, &blkno);
799 return error; /* GROT: dir is inconsistent */
801 error = xfs_da3_node_create(state->args, blkno, treelevel,
802 &newblk->bp, state->args->whichfork);
804 return error; /* GROT: dir is inconsistent */
805 newblk->blkno = blkno;
806 newblk->magic = XFS_DA_NODE_MAGIC;
807 xfs_da3_node_rebalance(state, oldblk, newblk);
808 error = xfs_da3_blk_link(state, oldblk, newblk);
817 * Insert the new entry(s) into the correct block
818 * (updating last hashval in the process).
820 * xfs_da3_node_add() inserts BEFORE the given index,
821 * and as a result of using node_lookup_int() we always
822 * point to a valid entry (not after one), but a split
823 * operation always results in a new block whose hashvals
824 * FOLLOW the current block.
826 * If we had double-split op below us, then add the extra block too.
828 node = oldblk->bp->b_addr;
829 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
830 if (oldblk->index <= nodehdr.count) {
832 xfs_da3_node_add(state, oldblk, addblk);
834 if (state->extraafter)
836 xfs_da3_node_add(state, oldblk, &state->extrablk);
837 state->extravalid = 0;
841 xfs_da3_node_add(state, newblk, addblk);
843 if (state->extraafter)
845 xfs_da3_node_add(state, newblk, &state->extrablk);
846 state->extravalid = 0;
854 * Balance the btree elements between two intermediate nodes,
855 * usually one full and one empty.
857 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
860 xfs_da3_node_rebalance(
861 struct xfs_da_state *state,
862 struct xfs_da_state_blk *blk1,
863 struct xfs_da_state_blk *blk2)
865 struct xfs_da_intnode *node1;
866 struct xfs_da_intnode *node2;
867 struct xfs_da_node_entry *btree1;
868 struct xfs_da_node_entry *btree2;
869 struct xfs_da_node_entry *btree_s;
870 struct xfs_da_node_entry *btree_d;
871 struct xfs_da3_icnode_hdr nodehdr1;
872 struct xfs_da3_icnode_hdr nodehdr2;
873 struct xfs_trans *tp;
877 struct xfs_inode *dp = state->args->dp;
879 trace_xfs_da_node_rebalance(state->args);
881 node1 = blk1->bp->b_addr;
882 node2 = blk2->bp->b_addr;
883 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
884 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
885 btree1 = nodehdr1.btree;
886 btree2 = nodehdr2.btree;
889 * Figure out how many entries need to move, and in which direction.
890 * Swap the nodes around if that makes it simpler.
892 if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
893 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
894 (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
895 be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
897 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
898 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
899 btree1 = nodehdr1.btree;
900 btree2 = nodehdr2.btree;
904 count = (nodehdr1.count - nodehdr2.count) / 2;
907 tp = state->args->trans;
909 * Two cases: high-to-low and low-to-high.
913 * Move elements in node2 up to make a hole.
915 tmp = nodehdr2.count;
917 tmp *= (uint)sizeof(xfs_da_node_entry_t);
918 btree_s = &btree2[0];
919 btree_d = &btree2[count];
920 memmove(btree_d, btree_s, tmp);
924 * Move the req'd B-tree elements from high in node1 to
927 nodehdr2.count += count;
928 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
929 btree_s = &btree1[nodehdr1.count - count];
930 btree_d = &btree2[0];
931 memcpy(btree_d, btree_s, tmp);
932 nodehdr1.count -= count;
935 * Move the req'd B-tree elements from low in node2 to
939 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
940 btree_s = &btree2[0];
941 btree_d = &btree1[nodehdr1.count];
942 memcpy(btree_d, btree_s, tmp);
943 nodehdr1.count += count;
945 xfs_trans_log_buf(tp, blk1->bp,
946 XFS_DA_LOGRANGE(node1, btree_d, tmp));
949 * Move elements in node2 down to fill the hole.
951 tmp = nodehdr2.count - count;
952 tmp *= (uint)sizeof(xfs_da_node_entry_t);
953 btree_s = &btree2[count];
954 btree_d = &btree2[0];
955 memmove(btree_d, btree_s, tmp);
956 nodehdr2.count -= count;
960 * Log header of node 1 and all current bits of node 2.
962 xfs_da3_node_hdr_to_disk(dp->i_mount, node1, &nodehdr1);
963 xfs_trans_log_buf(tp, blk1->bp,
964 XFS_DA_LOGRANGE(node1, &node1->hdr,
965 state->args->geo->node_hdr_size));
967 xfs_da3_node_hdr_to_disk(dp->i_mount, node2, &nodehdr2);
968 xfs_trans_log_buf(tp, blk2->bp,
969 XFS_DA_LOGRANGE(node2, &node2->hdr,
970 state->args->geo->node_hdr_size +
971 (sizeof(btree2[0]) * nodehdr2.count)));
974 * Record the last hashval from each block for upward propagation.
975 * (note: don't use the swapped node pointers)
978 node1 = blk1->bp->b_addr;
979 node2 = blk2->bp->b_addr;
980 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
981 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
982 btree1 = nodehdr1.btree;
983 btree2 = nodehdr2.btree;
985 blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
986 blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
989 * Adjust the expected index for insertion.
991 if (blk1->index >= nodehdr1.count) {
992 blk2->index = blk1->index - nodehdr1.count;
993 blk1->index = nodehdr1.count + 1; /* make it invalid */
998 * Add a new entry to an intermediate node.
1002 struct xfs_da_state *state,
1003 struct xfs_da_state_blk *oldblk,
1004 struct xfs_da_state_blk *newblk)
1006 struct xfs_da_intnode *node;
1007 struct xfs_da3_icnode_hdr nodehdr;
1008 struct xfs_da_node_entry *btree;
1010 struct xfs_inode *dp = state->args->dp;
1012 trace_xfs_da_node_add(state->args);
1014 node = oldblk->bp->b_addr;
1015 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1016 btree = nodehdr.btree;
1018 ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
1019 ASSERT(newblk->blkno != 0);
1020 if (state->args->whichfork == XFS_DATA_FORK)
1021 ASSERT(newblk->blkno >= state->args->geo->leafblk &&
1022 newblk->blkno < state->args->geo->freeblk);
1025 * We may need to make some room before we insert the new node.
1028 if (oldblk->index < nodehdr.count) {
1029 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
1030 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
1032 btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
1033 btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
1034 xfs_trans_log_buf(state->args->trans, oldblk->bp,
1035 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
1036 tmp + sizeof(*btree)));
1039 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1040 xfs_trans_log_buf(state->args->trans, oldblk->bp,
1041 XFS_DA_LOGRANGE(node, &node->hdr,
1042 state->args->geo->node_hdr_size));
1045 * Copy the last hash value from the oldblk to propagate upwards.
1047 oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1050 /*========================================================================
1051 * Routines used for shrinking the Btree.
1052 *========================================================================*/
1055 * Deallocate an empty leaf node, remove it from its parent,
1056 * possibly deallocating that block, etc...
1060 struct xfs_da_state *state)
1062 struct xfs_da_state_blk *drop_blk;
1063 struct xfs_da_state_blk *save_blk;
1067 trace_xfs_da_join(state->args);
1069 drop_blk = &state->path.blk[ state->path.active-1 ];
1070 save_blk = &state->altpath.blk[ state->path.active-1 ];
1071 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
1072 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
1073 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1076 * Walk back up the tree joining/deallocating as necessary.
1077 * When we stop dropping blocks, break out.
1079 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
1080 state->path.active--) {
1082 * See if we can combine the block with a neighbor.
1083 * (action == 0) => no options, just leave
1084 * (action == 1) => coalesce, then unlink
1085 * (action == 2) => block empty, unlink it
1087 switch (drop_blk->magic) {
1088 case XFS_ATTR_LEAF_MAGIC:
1089 error = xfs_attr3_leaf_toosmall(state, &action);
1094 xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
1096 case XFS_DIR2_LEAFN_MAGIC:
1097 error = xfs_dir2_leafn_toosmall(state, &action);
1102 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
1104 case XFS_DA_NODE_MAGIC:
1106 * Remove the offending node, fixup hashvals,
1107 * check for a toosmall neighbor.
1109 xfs_da3_node_remove(state, drop_blk);
1110 xfs_da3_fixhashpath(state, &state->path);
1111 error = xfs_da3_node_toosmall(state, &action);
1116 xfs_da3_node_unbalance(state, drop_blk, save_blk);
1119 xfs_da3_fixhashpath(state, &state->altpath);
1120 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
1121 xfs_da_state_kill_altpath(state);
1124 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1126 drop_blk->bp = NULL;
1131 * We joined all the way to the top. If it turns out that
1132 * we only have one entry in the root, make the child block
1135 xfs_da3_node_remove(state, drop_blk);
1136 xfs_da3_fixhashpath(state, &state->path);
1137 error = xfs_da3_root_join(state, &state->path.blk[0]);
1143 xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1145 __be16 magic = blkinfo->magic;
1148 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1149 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
1150 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1151 magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1153 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1154 magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1156 ASSERT(!blkinfo->forw);
1157 ASSERT(!blkinfo->back);
1160 #define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1164 * We have only one entry in the root. Copy the only remaining child of
1165 * the old root to block 0 as the new root node.
1169 struct xfs_da_state *state,
1170 struct xfs_da_state_blk *root_blk)
1172 struct xfs_da_intnode *oldroot;
1173 struct xfs_da_args *args;
1176 struct xfs_da3_icnode_hdr oldroothdr;
1178 struct xfs_inode *dp = state->args->dp;
1180 trace_xfs_da_root_join(state->args);
1182 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
1185 oldroot = root_blk->bp->b_addr;
1186 xfs_da3_node_hdr_from_disk(dp->i_mount, &oldroothdr, oldroot);
1187 ASSERT(oldroothdr.forw == 0);
1188 ASSERT(oldroothdr.back == 0);
1191 * If the root has more than one child, then don't do anything.
1193 if (oldroothdr.count > 1)
1197 * Read in the (only) child block, then copy those bytes into
1198 * the root block's buffer and free the original child block.
1200 child = be32_to_cpu(oldroothdr.btree[0].before);
1202 error = xfs_da3_node_read(args->trans, dp, child, &bp, args->whichfork);
1205 xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
1208 * This could be copying a leaf back into the root block in the case of
1209 * there only being a single leaf block left in the tree. Hence we have
1210 * to update the b_ops pointer as well to match the buffer type change
1211 * that could occur. For dir3 blocks we also need to update the block
1212 * number in the buffer header.
1214 memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
1215 root_blk->bp->b_ops = bp->b_ops;
1216 xfs_trans_buf_copy_type(root_blk->bp, bp);
1217 if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1218 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1219 da3->blkno = cpu_to_be64(xfs_buf_daddr(root_blk->bp));
1221 xfs_trans_log_buf(args->trans, root_blk->bp, 0,
1222 args->geo->blksize - 1);
1223 error = xfs_da_shrink_inode(args, child, bp);
1228 * Check a node block and its neighbors to see if the block should be
1229 * collapsed into one or the other neighbor. Always keep the block
1230 * with the smaller block number.
1231 * If the current block is over 50% full, don't try to join it, return 0.
1232 * If the block is empty, fill in the state structure and return 2.
1233 * If it can be collapsed, fill in the state structure and return 1.
1234 * If nothing can be done, return 0.
1237 xfs_da3_node_toosmall(
1238 struct xfs_da_state *state,
1241 struct xfs_da_intnode *node;
1242 struct xfs_da_state_blk *blk;
1243 struct xfs_da_blkinfo *info;
1246 struct xfs_da3_icnode_hdr nodehdr;
1252 struct xfs_inode *dp = state->args->dp;
1254 trace_xfs_da_node_toosmall(state->args);
1257 * Check for the degenerate case of the block being over 50% full.
1258 * If so, it's not worth even looking to see if we might be able
1259 * to coalesce with a sibling.
1261 blk = &state->path.blk[ state->path.active-1 ];
1262 info = blk->bp->b_addr;
1263 node = (xfs_da_intnode_t *)info;
1264 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1265 if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
1266 *action = 0; /* blk over 50%, don't try to join */
1267 return 0; /* blk over 50%, don't try to join */
1271 * Check for the degenerate case of the block being empty.
1272 * If the block is empty, we'll simply delete it, no need to
1273 * coalesce it with a sibling block. We choose (arbitrarily)
1274 * to merge with the forward block unless it is NULL.
1276 if (nodehdr.count == 0) {
1278 * Make altpath point to the block we want to keep and
1279 * path point to the block we want to drop (this one).
1281 forward = (info->forw != 0);
1282 memcpy(&state->altpath, &state->path, sizeof(state->path));
1283 error = xfs_da3_path_shift(state, &state->altpath, forward,
1296 * Examine each sibling block to see if we can coalesce with
1297 * at least 25% free space to spare. We need to figure out
1298 * whether to merge with the forward or the backward block.
1299 * We prefer coalescing with the lower numbered sibling so as
1300 * to shrink a directory over time.
1302 count = state->args->geo->node_ents;
1303 count -= state->args->geo->node_ents >> 2;
1304 count -= nodehdr.count;
1306 /* start with smaller blk num */
1307 forward = nodehdr.forw < nodehdr.back;
1308 for (i = 0; i < 2; forward = !forward, i++) {
1309 struct xfs_da3_icnode_hdr thdr;
1311 blkno = nodehdr.forw;
1313 blkno = nodehdr.back;
1316 error = xfs_da3_node_read(state->args->trans, dp, blkno, &bp,
1317 state->args->whichfork);
1322 xfs_da3_node_hdr_from_disk(dp->i_mount, &thdr, node);
1323 xfs_trans_brelse(state->args->trans, bp);
1325 if (count - thdr.count >= 0)
1326 break; /* fits with at least 25% to spare */
1334 * Make altpath point to the block we want to keep (the lower
1335 * numbered block) and path point to the block we want to drop.
1337 memcpy(&state->altpath, &state->path, sizeof(state->path));
1338 if (blkno < blk->blkno) {
1339 error = xfs_da3_path_shift(state, &state->altpath, forward,
1342 error = xfs_da3_path_shift(state, &state->path, forward,
1356 * Pick up the last hashvalue from an intermediate node.
1359 xfs_da3_node_lasthash(
1360 struct xfs_inode *dp,
1364 struct xfs_da3_icnode_hdr nodehdr;
1366 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, bp->b_addr);
1368 *count = nodehdr.count;
1371 return be32_to_cpu(nodehdr.btree[nodehdr.count - 1].hashval);
1375 * Walk back up the tree adjusting hash values as necessary,
1376 * when we stop making changes, return.
1379 xfs_da3_fixhashpath(
1380 struct xfs_da_state *state,
1381 struct xfs_da_state_path *path)
1383 struct xfs_da_state_blk *blk;
1384 struct xfs_da_intnode *node;
1385 struct xfs_da_node_entry *btree;
1386 xfs_dahash_t lasthash=0;
1389 struct xfs_inode *dp = state->args->dp;
1391 trace_xfs_da_fixhashpath(state->args);
1393 level = path->active-1;
1394 blk = &path->blk[ level ];
1395 switch (blk->magic) {
1396 case XFS_ATTR_LEAF_MAGIC:
1397 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1401 case XFS_DIR2_LEAFN_MAGIC:
1402 lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
1406 case XFS_DA_NODE_MAGIC:
1407 lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
1412 for (blk--, level--; level >= 0; blk--, level--) {
1413 struct xfs_da3_icnode_hdr nodehdr;
1415 node = blk->bp->b_addr;
1416 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1417 btree = nodehdr.btree;
1418 if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
1420 blk->hashval = lasthash;
1421 btree[blk->index].hashval = cpu_to_be32(lasthash);
1422 xfs_trans_log_buf(state->args->trans, blk->bp,
1423 XFS_DA_LOGRANGE(node, &btree[blk->index],
1426 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1431 * Remove an entry from an intermediate node.
1434 xfs_da3_node_remove(
1435 struct xfs_da_state *state,
1436 struct xfs_da_state_blk *drop_blk)
1438 struct xfs_da_intnode *node;
1439 struct xfs_da3_icnode_hdr nodehdr;
1440 struct xfs_da_node_entry *btree;
1443 struct xfs_inode *dp = state->args->dp;
1445 trace_xfs_da_node_remove(state->args);
1447 node = drop_blk->bp->b_addr;
1448 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1449 ASSERT(drop_blk->index < nodehdr.count);
1450 ASSERT(drop_blk->index >= 0);
1453 * Copy over the offending entry, or just zero it out.
1455 index = drop_blk->index;
1456 btree = nodehdr.btree;
1457 if (index < nodehdr.count - 1) {
1458 tmp = nodehdr.count - index - 1;
1459 tmp *= (uint)sizeof(xfs_da_node_entry_t);
1460 memmove(&btree[index], &btree[index + 1], tmp);
1461 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1462 XFS_DA_LOGRANGE(node, &btree[index], tmp));
1463 index = nodehdr.count - 1;
1465 memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
1466 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1467 XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1469 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1470 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1471 XFS_DA_LOGRANGE(node, &node->hdr, state->args->geo->node_hdr_size));
1474 * Copy the last hash value from the block to propagate upwards.
1476 drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
1480 * Unbalance the elements between two intermediate nodes,
1481 * move all Btree elements from one node into another.
1484 xfs_da3_node_unbalance(
1485 struct xfs_da_state *state,
1486 struct xfs_da_state_blk *drop_blk,
1487 struct xfs_da_state_blk *save_blk)
1489 struct xfs_da_intnode *drop_node;
1490 struct xfs_da_intnode *save_node;
1491 struct xfs_da_node_entry *drop_btree;
1492 struct xfs_da_node_entry *save_btree;
1493 struct xfs_da3_icnode_hdr drop_hdr;
1494 struct xfs_da3_icnode_hdr save_hdr;
1495 struct xfs_trans *tp;
1498 struct xfs_inode *dp = state->args->dp;
1500 trace_xfs_da_node_unbalance(state->args);
1502 drop_node = drop_blk->bp->b_addr;
1503 save_node = save_blk->bp->b_addr;
1504 xfs_da3_node_hdr_from_disk(dp->i_mount, &drop_hdr, drop_node);
1505 xfs_da3_node_hdr_from_disk(dp->i_mount, &save_hdr, save_node);
1506 drop_btree = drop_hdr.btree;
1507 save_btree = save_hdr.btree;
1508 tp = state->args->trans;
1511 * If the dying block has lower hashvals, then move all the
1512 * elements in the remaining block up to make a hole.
1514 if ((be32_to_cpu(drop_btree[0].hashval) <
1515 be32_to_cpu(save_btree[0].hashval)) ||
1516 (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1517 be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1518 /* XXX: check this - is memmove dst correct? */
1519 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1520 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1523 xfs_trans_log_buf(tp, save_blk->bp,
1524 XFS_DA_LOGRANGE(save_node, &save_btree[0],
1525 (save_hdr.count + drop_hdr.count) *
1526 sizeof(xfs_da_node_entry_t)));
1528 sindex = save_hdr.count;
1529 xfs_trans_log_buf(tp, save_blk->bp,
1530 XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1531 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
1535 * Move all the B-tree elements from drop_blk to save_blk.
1537 tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1538 memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1539 save_hdr.count += drop_hdr.count;
1541 xfs_da3_node_hdr_to_disk(dp->i_mount, save_node, &save_hdr);
1542 xfs_trans_log_buf(tp, save_blk->bp,
1543 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1544 state->args->geo->node_hdr_size));
1547 * Save the last hashval in the remaining block for upward propagation.
1549 save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
1552 /*========================================================================
1553 * Routines used for finding things in the Btree.
1554 *========================================================================*/
1557 * Walk down the Btree looking for a particular filename, filling
1558 * in the state structure as we go.
1560 * We will set the state structure to point to each of the elements
1561 * in each of the nodes where either the hashval is or should be.
1563 * We support duplicate hashval's so for each entry in the current
1564 * node that could contain the desired hashval, descend. This is a
1565 * pruned depth-first tree search.
1568 xfs_da3_node_lookup_int(
1569 struct xfs_da_state *state,
1572 struct xfs_da_state_blk *blk;
1573 struct xfs_da_blkinfo *curr;
1574 struct xfs_da_intnode *node;
1575 struct xfs_da_node_entry *btree;
1576 struct xfs_da3_icnode_hdr nodehdr;
1577 struct xfs_da_args *args;
1579 xfs_dahash_t hashval;
1580 xfs_dahash_t btreehashval;
1586 unsigned int expected_level = 0;
1588 struct xfs_inode *dp = state->args->dp;
1593 * Descend thru the B-tree searching each level for the right
1594 * node to use, until the right hashval is found.
1596 blkno = args->geo->leafblk;
1597 for (blk = &state->path.blk[0], state->path.active = 1;
1598 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1599 blk++, state->path.active++) {
1601 * Read the next node down in the tree.
1604 error = xfs_da3_node_read(args->trans, args->dp, blkno,
1605 &blk->bp, args->whichfork);
1608 state->path.active--;
1611 curr = blk->bp->b_addr;
1612 magic = be16_to_cpu(curr->magic);
1614 if (magic == XFS_ATTR_LEAF_MAGIC ||
1615 magic == XFS_ATTR3_LEAF_MAGIC) {
1616 blk->magic = XFS_ATTR_LEAF_MAGIC;
1617 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1621 if (magic == XFS_DIR2_LEAFN_MAGIC ||
1622 magic == XFS_DIR3_LEAFN_MAGIC) {
1623 blk->magic = XFS_DIR2_LEAFN_MAGIC;
1624 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1629 if (magic != XFS_DA_NODE_MAGIC && magic != XFS_DA3_NODE_MAGIC) {
1630 xfs_buf_mark_corrupt(blk->bp);
1631 return -EFSCORRUPTED;
1634 blk->magic = XFS_DA_NODE_MAGIC;
1637 * Search an intermediate node for a match.
1639 node = blk->bp->b_addr;
1640 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1641 btree = nodehdr.btree;
1643 /* Tree taller than we can handle; bail out! */
1644 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
1645 xfs_buf_mark_corrupt(blk->bp);
1646 return -EFSCORRUPTED;
1649 /* Check the level from the root. */
1650 if (blkno == args->geo->leafblk)
1651 expected_level = nodehdr.level - 1;
1652 else if (expected_level != nodehdr.level) {
1653 xfs_buf_mark_corrupt(blk->bp);
1654 return -EFSCORRUPTED;
1658 max = nodehdr.count;
1659 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
1662 * Binary search. (note: small blocks will skip loop)
1664 probe = span = max / 2;
1665 hashval = args->hashval;
1668 btreehashval = be32_to_cpu(btree[probe].hashval);
1669 if (btreehashval < hashval)
1671 else if (btreehashval > hashval)
1676 ASSERT((probe >= 0) && (probe < max));
1677 ASSERT((span <= 4) ||
1678 (be32_to_cpu(btree[probe].hashval) == hashval));
1681 * Since we may have duplicate hashval's, find the first
1682 * matching hashval in the node.
1685 be32_to_cpu(btree[probe].hashval) >= hashval) {
1688 while (probe < max &&
1689 be32_to_cpu(btree[probe].hashval) < hashval) {
1694 * Pick the right block to descend on.
1697 blk->index = max - 1;
1698 blkno = be32_to_cpu(btree[max - 1].before);
1701 blkno = be32_to_cpu(btree[probe].before);
1704 /* We can't point back to the root. */
1705 if (XFS_IS_CORRUPT(dp->i_mount, blkno == args->geo->leafblk))
1706 return -EFSCORRUPTED;
1709 if (XFS_IS_CORRUPT(dp->i_mount, expected_level != 0))
1710 return -EFSCORRUPTED;
1713 * A leaf block that ends in the hashval that we are interested in
1714 * (final hashval == search hashval) means that the next block may
1715 * contain more entries with the same hashval, shift upward to the
1716 * next leaf and keep searching.
1719 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
1720 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1721 &blk->index, state);
1722 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1723 retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
1724 blk->index = args->index;
1725 args->blkno = blk->blkno;
1728 return -EFSCORRUPTED;
1730 if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
1731 (blk->hashval == args->hashval)) {
1732 error = xfs_da3_path_shift(state, &state->path, 1, 1,
1738 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1739 /* path_shift() gives ENOENT */
1749 /*========================================================================
1751 *========================================================================*/
1754 * Compare two intermediate nodes for "order".
1758 struct xfs_inode *dp,
1759 struct xfs_buf *node1_bp,
1760 struct xfs_buf *node2_bp)
1762 struct xfs_da_intnode *node1;
1763 struct xfs_da_intnode *node2;
1764 struct xfs_da_node_entry *btree1;
1765 struct xfs_da_node_entry *btree2;
1766 struct xfs_da3_icnode_hdr node1hdr;
1767 struct xfs_da3_icnode_hdr node2hdr;
1769 node1 = node1_bp->b_addr;
1770 node2 = node2_bp->b_addr;
1771 xfs_da3_node_hdr_from_disk(dp->i_mount, &node1hdr, node1);
1772 xfs_da3_node_hdr_from_disk(dp->i_mount, &node2hdr, node2);
1773 btree1 = node1hdr.btree;
1774 btree2 = node2hdr.btree;
1776 if (node1hdr.count > 0 && node2hdr.count > 0 &&
1777 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1778 (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1779 be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1786 * Link a new block into a doubly linked list of blocks (of whatever type).
1790 struct xfs_da_state *state,
1791 struct xfs_da_state_blk *old_blk,
1792 struct xfs_da_state_blk *new_blk)
1794 struct xfs_da_blkinfo *old_info;
1795 struct xfs_da_blkinfo *new_info;
1796 struct xfs_da_blkinfo *tmp_info;
1797 struct xfs_da_args *args;
1801 struct xfs_inode *dp = state->args->dp;
1804 * Set up environment.
1807 ASSERT(args != NULL);
1808 old_info = old_blk->bp->b_addr;
1809 new_info = new_blk->bp->b_addr;
1810 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
1811 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1812 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
1814 switch (old_blk->magic) {
1815 case XFS_ATTR_LEAF_MAGIC:
1816 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1818 case XFS_DIR2_LEAFN_MAGIC:
1819 before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
1821 case XFS_DA_NODE_MAGIC:
1822 before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
1827 * Link blocks in appropriate order.
1831 * Link new block in before existing block.
1833 trace_xfs_da_link_before(args);
1834 new_info->forw = cpu_to_be32(old_blk->blkno);
1835 new_info->back = old_info->back;
1836 if (old_info->back) {
1837 error = xfs_da3_node_read(args->trans, dp,
1838 be32_to_cpu(old_info->back),
1839 &bp, args->whichfork);
1843 tmp_info = bp->b_addr;
1844 ASSERT(tmp_info->magic == old_info->magic);
1845 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1846 tmp_info->forw = cpu_to_be32(new_blk->blkno);
1847 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1849 old_info->back = cpu_to_be32(new_blk->blkno);
1852 * Link new block in after existing block.
1854 trace_xfs_da_link_after(args);
1855 new_info->forw = old_info->forw;
1856 new_info->back = cpu_to_be32(old_blk->blkno);
1857 if (old_info->forw) {
1858 error = xfs_da3_node_read(args->trans, dp,
1859 be32_to_cpu(old_info->forw),
1860 &bp, args->whichfork);
1864 tmp_info = bp->b_addr;
1865 ASSERT(tmp_info->magic == old_info->magic);
1866 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1867 tmp_info->back = cpu_to_be32(new_blk->blkno);
1868 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1870 old_info->forw = cpu_to_be32(new_blk->blkno);
1873 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1874 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
1879 * Unlink a block from a doubly linked list of blocks.
1881 STATIC int /* error */
1883 struct xfs_da_state *state,
1884 struct xfs_da_state_blk *drop_blk,
1885 struct xfs_da_state_blk *save_blk)
1887 struct xfs_da_blkinfo *drop_info;
1888 struct xfs_da_blkinfo *save_info;
1889 struct xfs_da_blkinfo *tmp_info;
1890 struct xfs_da_args *args;
1895 * Set up environment.
1898 ASSERT(args != NULL);
1899 save_info = save_blk->bp->b_addr;
1900 drop_info = drop_blk->bp->b_addr;
1901 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
1902 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1903 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1904 ASSERT(save_blk->magic == drop_blk->magic);
1905 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1906 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1907 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1908 (be32_to_cpu(drop_info->back) == save_blk->blkno));
1911 * Unlink the leaf block from the doubly linked chain of leaves.
1913 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
1914 trace_xfs_da_unlink_back(args);
1915 save_info->back = drop_info->back;
1916 if (drop_info->back) {
1917 error = xfs_da3_node_read(args->trans, args->dp,
1918 be32_to_cpu(drop_info->back),
1919 &bp, args->whichfork);
1923 tmp_info = bp->b_addr;
1924 ASSERT(tmp_info->magic == save_info->magic);
1925 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1926 tmp_info->forw = cpu_to_be32(save_blk->blkno);
1927 xfs_trans_log_buf(args->trans, bp, 0,
1928 sizeof(*tmp_info) - 1);
1931 trace_xfs_da_unlink_forward(args);
1932 save_info->forw = drop_info->forw;
1933 if (drop_info->forw) {
1934 error = xfs_da3_node_read(args->trans, args->dp,
1935 be32_to_cpu(drop_info->forw),
1936 &bp, args->whichfork);
1940 tmp_info = bp->b_addr;
1941 ASSERT(tmp_info->magic == save_info->magic);
1942 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1943 tmp_info->back = cpu_to_be32(save_blk->blkno);
1944 xfs_trans_log_buf(args->trans, bp, 0,
1945 sizeof(*tmp_info) - 1);
1949 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
1954 * Move a path "forward" or "!forward" one block at the current level.
1956 * This routine will adjust a "path" to point to the next block
1957 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1958 * Btree, including updating pointers to the intermediate nodes between
1959 * the new bottom and the root.
1963 struct xfs_da_state *state,
1964 struct xfs_da_state_path *path,
1969 struct xfs_da_state_blk *blk;
1970 struct xfs_da_blkinfo *info;
1971 struct xfs_da_args *args;
1972 struct xfs_da_node_entry *btree;
1973 struct xfs_da3_icnode_hdr nodehdr;
1975 xfs_dablk_t blkno = 0;
1978 struct xfs_inode *dp = state->args->dp;
1980 trace_xfs_da_path_shift(state->args);
1983 * Roll up the Btree looking for the first block where our
1984 * current index is not at the edge of the block. Note that
1985 * we skip the bottom layer because we want the sibling block.
1988 ASSERT(args != NULL);
1989 ASSERT(path != NULL);
1990 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1991 level = (path->active-1) - 1; /* skip bottom layer in path */
1992 for (; level >= 0; level--) {
1993 blk = &path->blk[level];
1994 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
1997 if (forward && (blk->index < nodehdr.count - 1)) {
1999 blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
2001 } else if (!forward && (blk->index > 0)) {
2003 blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
2008 *result = -ENOENT; /* we're out of our tree */
2009 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
2014 * Roll down the edge of the subtree until we reach the
2015 * same depth we were at originally.
2017 for (blk++, level++; level < path->active; blk++, level++) {
2019 * Read the next child block into a local buffer.
2021 error = xfs_da3_node_read(args->trans, dp, blkno, &bp,
2027 * Release the old block (if it's dirty, the trans doesn't
2028 * actually let go) and swap the local buffer into the path
2029 * structure. This ensures failure of the above read doesn't set
2030 * a NULL buffer in an active slot in the path.
2033 xfs_trans_brelse(args->trans, blk->bp);
2037 info = blk->bp->b_addr;
2038 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
2039 info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
2040 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2041 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
2042 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
2043 info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
2047 * Note: we flatten the magic number to a single type so we
2048 * don't have to compare against crc/non-crc types elsewhere.
2050 switch (be16_to_cpu(info->magic)) {
2051 case XFS_DA_NODE_MAGIC:
2052 case XFS_DA3_NODE_MAGIC:
2053 blk->magic = XFS_DA_NODE_MAGIC;
2054 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
2056 btree = nodehdr.btree;
2057 blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
2061 blk->index = nodehdr.count - 1;
2062 blkno = be32_to_cpu(btree[blk->index].before);
2064 case XFS_ATTR_LEAF_MAGIC:
2065 case XFS_ATTR3_LEAF_MAGIC:
2066 blk->magic = XFS_ATTR_LEAF_MAGIC;
2067 ASSERT(level == path->active-1);
2069 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
2071 case XFS_DIR2_LEAFN_MAGIC:
2072 case XFS_DIR3_LEAFN_MAGIC:
2073 blk->magic = XFS_DIR2_LEAFN_MAGIC;
2074 ASSERT(level == path->active-1);
2076 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
2089 /*========================================================================
2091 *========================================================================*/
2094 * Implement a simple hash on a character string.
2095 * Rotate the hash value by 7 bits, then XOR each character in.
2096 * This is implemented with some source-level loop unrolling.
2099 xfs_da_hashname(const uint8_t *name, int namelen)
2104 * Do four characters at a time as long as we can.
2106 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
2107 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
2108 (name[3] << 0) ^ rol32(hash, 7 * 4);
2111 * Now do the rest of the characters.
2115 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2118 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2120 return (name[0] << 0) ^ rol32(hash, 7 * 1);
2121 default: /* case 0: */
2128 struct xfs_da_args *args,
2129 const unsigned char *name,
2132 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2133 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2137 xfs_da_grow_inode_int(
2138 struct xfs_da_args *args,
2142 struct xfs_trans *tp = args->trans;
2143 struct xfs_inode *dp = args->dp;
2144 int w = args->whichfork;
2145 xfs_rfsblock_t nblks = dp->i_nblocks;
2146 struct xfs_bmbt_irec map, *mapp;
2147 int nmap, error, got, i, mapi;
2150 * Find a spot in the file space to put the new block.
2152 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2157 * Try mapping it in one filesystem block.
2160 error = xfs_bmapi_write(tp, dp, *bno, count,
2161 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
2162 args->total, &map, &nmap);
2170 } else if (nmap == 0 && count > 1) {
2175 * If we didn't get it and the block might work if fragmented,
2176 * try without the CONTIG flag. Loop until we get it all.
2178 mapp = kmem_alloc(sizeof(*mapp) * count, 0);
2179 for (b = *bno, mapi = 0; b < *bno + count; ) {
2180 nmap = min(XFS_BMAP_MAX_NMAP, count);
2181 c = (int)(*bno + count - b);
2182 error = xfs_bmapi_write(tp, dp, b, c,
2183 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
2184 args->total, &mapp[mapi], &nmap);
2190 b = mapp[mapi - 1].br_startoff +
2191 mapp[mapi - 1].br_blockcount;
2199 * Count the blocks we got, make sure it matches the total.
2201 for (i = 0, got = 0; i < mapi; i++)
2202 got += mapp[i].br_blockcount;
2203 if (got != count || mapp[0].br_startoff != *bno ||
2204 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
2210 /* account for newly allocated blocks in reserved blocks total */
2211 args->total -= dp->i_nblocks - nblks;
2220 * Add a block to the btree ahead of the file.
2221 * Return the new block number to the caller.
2225 struct xfs_da_args *args,
2226 xfs_dablk_t *new_blkno)
2231 trace_xfs_da_grow_inode(args);
2233 bno = args->geo->leafblk;
2234 error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
2236 *new_blkno = (xfs_dablk_t)bno;
2241 * Ick. We need to always be able to remove a btree block, even
2242 * if there's no space reservation because the filesystem is full.
2243 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2244 * It swaps the target block with the last block in the file. The
2245 * last block in the file can always be removed since it can't cause
2246 * a bmap btree split to do that.
2249 xfs_da3_swap_lastblock(
2250 struct xfs_da_args *args,
2251 xfs_dablk_t *dead_blknop,
2252 struct xfs_buf **dead_bufp)
2254 struct xfs_da_blkinfo *dead_info;
2255 struct xfs_da_blkinfo *sib_info;
2256 struct xfs_da_intnode *par_node;
2257 struct xfs_da_intnode *dead_node;
2258 struct xfs_dir2_leaf *dead_leaf2;
2259 struct xfs_da_node_entry *btree;
2260 struct xfs_da3_icnode_hdr par_hdr;
2261 struct xfs_inode *dp;
2262 struct xfs_trans *tp;
2263 struct xfs_mount *mp;
2264 struct xfs_buf *dead_buf;
2265 struct xfs_buf *last_buf;
2266 struct xfs_buf *sib_buf;
2267 struct xfs_buf *par_buf;
2268 xfs_dahash_t dead_hash;
2269 xfs_fileoff_t lastoff;
2270 xfs_dablk_t dead_blkno;
2271 xfs_dablk_t last_blkno;
2272 xfs_dablk_t sib_blkno;
2273 xfs_dablk_t par_blkno;
2280 trace_xfs_da_swap_lastblock(args);
2282 dead_buf = *dead_bufp;
2283 dead_blkno = *dead_blknop;
2286 w = args->whichfork;
2287 ASSERT(w == XFS_DATA_FORK);
2289 lastoff = args->geo->freeblk;
2290 error = xfs_bmap_last_before(tp, dp, &lastoff, w);
2293 if (XFS_IS_CORRUPT(mp, lastoff == 0))
2294 return -EFSCORRUPTED;
2296 * Read the last block in the btree space.
2298 last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
2299 error = xfs_da3_node_read(tp, dp, last_blkno, &last_buf, w);
2303 * Copy the last block into the dead buffer and log it.
2305 memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
2306 xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
2307 dead_info = dead_buf->b_addr;
2309 * Get values from the moved block.
2311 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2312 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2313 struct xfs_dir3_icleaf_hdr leafhdr;
2314 struct xfs_dir2_leaf_entry *ents;
2316 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
2317 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr,
2319 ents = leafhdr.ents;
2321 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
2323 struct xfs_da3_icnode_hdr deadhdr;
2325 dead_node = (xfs_da_intnode_t *)dead_info;
2326 xfs_da3_node_hdr_from_disk(dp->i_mount, &deadhdr, dead_node);
2327 btree = deadhdr.btree;
2328 dead_level = deadhdr.level;
2329 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
2331 sib_buf = par_buf = NULL;
2333 * If the moved block has a left sibling, fix up the pointers.
2335 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
2336 error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
2339 sib_info = sib_buf->b_addr;
2340 if (XFS_IS_CORRUPT(mp,
2341 be32_to_cpu(sib_info->forw) != last_blkno ||
2342 sib_info->magic != dead_info->magic)) {
2343 error = -EFSCORRUPTED;
2346 sib_info->forw = cpu_to_be32(dead_blkno);
2347 xfs_trans_log_buf(tp, sib_buf,
2348 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2349 sizeof(sib_info->forw)));
2353 * If the moved block has a right sibling, fix up the pointers.
2355 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
2356 error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
2359 sib_info = sib_buf->b_addr;
2360 if (XFS_IS_CORRUPT(mp,
2361 be32_to_cpu(sib_info->back) != last_blkno ||
2362 sib_info->magic != dead_info->magic)) {
2363 error = -EFSCORRUPTED;
2366 sib_info->back = cpu_to_be32(dead_blkno);
2367 xfs_trans_log_buf(tp, sib_buf,
2368 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2369 sizeof(sib_info->back)));
2372 par_blkno = args->geo->leafblk;
2375 * Walk down the tree looking for the parent of the moved block.
2378 error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
2381 par_node = par_buf->b_addr;
2382 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2383 if (XFS_IS_CORRUPT(mp,
2384 level >= 0 && level != par_hdr.level + 1)) {
2385 error = -EFSCORRUPTED;
2388 level = par_hdr.level;
2389 btree = par_hdr.btree;
2391 entno < par_hdr.count &&
2392 be32_to_cpu(btree[entno].hashval) < dead_hash;
2395 if (XFS_IS_CORRUPT(mp, entno == par_hdr.count)) {
2396 error = -EFSCORRUPTED;
2399 par_blkno = be32_to_cpu(btree[entno].before);
2400 if (level == dead_level + 1)
2402 xfs_trans_brelse(tp, par_buf);
2406 * We're in the right parent block.
2407 * Look for the right entry.
2411 entno < par_hdr.count &&
2412 be32_to_cpu(btree[entno].before) != last_blkno;
2415 if (entno < par_hdr.count)
2417 par_blkno = par_hdr.forw;
2418 xfs_trans_brelse(tp, par_buf);
2420 if (XFS_IS_CORRUPT(mp, par_blkno == 0)) {
2421 error = -EFSCORRUPTED;
2424 error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
2427 par_node = par_buf->b_addr;
2428 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2429 if (XFS_IS_CORRUPT(mp, par_hdr.level != level)) {
2430 error = -EFSCORRUPTED;
2433 btree = par_hdr.btree;
2437 * Update the parent entry pointing to the moved block.
2439 btree[entno].before = cpu_to_be32(dead_blkno);
2440 xfs_trans_log_buf(tp, par_buf,
2441 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2442 sizeof(btree[entno].before)));
2443 *dead_blknop = last_blkno;
2444 *dead_bufp = last_buf;
2448 xfs_trans_brelse(tp, par_buf);
2450 xfs_trans_brelse(tp, sib_buf);
2451 xfs_trans_brelse(tp, last_buf);
2456 * Remove a btree block from a directory or attribute.
2459 xfs_da_shrink_inode(
2460 struct xfs_da_args *args,
2461 xfs_dablk_t dead_blkno,
2462 struct xfs_buf *dead_buf)
2464 struct xfs_inode *dp;
2465 int done, error, w, count;
2466 struct xfs_trans *tp;
2468 trace_xfs_da_shrink_inode(args);
2471 w = args->whichfork;
2473 count = args->geo->fsbcount;
2476 * Remove extents. If we get ENOSPC for a dir we have to move
2477 * the last block to the place we want to kill.
2479 error = xfs_bunmapi(tp, dp, dead_blkno, count,
2480 xfs_bmapi_aflag(w), 0, &done);
2481 if (error == -ENOSPC) {
2482 if (w != XFS_DATA_FORK)
2484 error = xfs_da3_swap_lastblock(args, &dead_blkno,
2492 xfs_trans_binval(tp, dead_buf);
2498 struct xfs_inode *dp,
2502 struct xfs_buf_map **mapp,
2505 struct xfs_mount *mp = dp->i_mount;
2506 int nfsb = xfs_dabuf_nfsb(mp, whichfork);
2507 struct xfs_bmbt_irec irec, *irecs = &irec;
2508 struct xfs_buf_map *map = *mapp;
2509 xfs_fileoff_t off = bno;
2510 int error = 0, nirecs, i;
2513 irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_NOFS);
2516 error = xfs_bmapi_read(dp, bno, nfsb, irecs, &nirecs,
2517 xfs_bmapi_aflag(whichfork));
2519 goto out_free_irecs;
2522 * Use the caller provided map for the single map case, else allocate a
2523 * larger one that needs to be free by the caller.
2526 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_NOFS);
2529 goto out_free_irecs;
2534 for (i = 0; i < nirecs; i++) {
2535 if (irecs[i].br_startblock == HOLESTARTBLOCK ||
2536 irecs[i].br_startblock == DELAYSTARTBLOCK)
2537 goto invalid_mapping;
2538 if (off != irecs[i].br_startoff)
2539 goto invalid_mapping;
2541 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2542 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2543 off += irecs[i].br_blockcount;
2546 if (off != bno + nfsb)
2547 goto invalid_mapping;
2556 /* Caller ok with no mapping. */
2557 if (XFS_IS_CORRUPT(mp, !(flags & XFS_DABUF_MAP_HOLE_OK))) {
2558 error = -EFSCORRUPTED;
2559 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2560 xfs_alert(mp, "%s: bno %u inode %llu",
2561 __func__, bno, dp->i_ino);
2563 for (i = 0; i < nirecs; i++) {
2565 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
2566 i, irecs[i].br_startoff,
2567 irecs[i].br_startblock,
2568 irecs[i].br_blockcount,
2575 goto out_free_irecs;
2579 * Get a buffer for the dir/attr block.
2583 struct xfs_trans *tp,
2584 struct xfs_inode *dp,
2586 struct xfs_buf **bpp,
2589 struct xfs_mount *mp = dp->i_mount;
2591 struct xfs_buf_map map, *mapp = ↦
2596 error = xfs_dabuf_map(dp, bno, 0, whichfork, &mapp, &nmap);
2597 if (error || nmap == 0)
2600 error = xfs_trans_get_buf_map(tp, mp->m_ddev_targp, mapp, nmap, 0, &bp);
2614 * Get a buffer for the dir/attr block, fill in the contents.
2618 struct xfs_trans *tp,
2619 struct xfs_inode *dp,
2622 struct xfs_buf **bpp,
2624 const struct xfs_buf_ops *ops)
2626 struct xfs_mount *mp = dp->i_mount;
2628 struct xfs_buf_map map, *mapp = ↦
2633 error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
2637 error = xfs_trans_read_buf_map(mp, tp, mp->m_ddev_targp, mapp, nmap, 0,
2642 if (whichfork == XFS_ATTR_FORK)
2643 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
2645 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2655 * Readahead the dir/attr block.
2659 struct xfs_inode *dp,
2663 const struct xfs_buf_ops *ops)
2665 struct xfs_buf_map map;
2666 struct xfs_buf_map *mapp;
2672 error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
2676 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);