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"
16 #include "xfs_dir2_priv.h"
17 #include "xfs_inode.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 kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
78 * Allocate a dir-state structure.
79 * We don't put them on the stack since they're large.
82 xfs_da_state_alloc(void)
84 return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
88 * Kill the altpath contents of a da-state structure.
91 xfs_da_state_kill_altpath(xfs_da_state_t *state)
95 for (i = 0; i < state->altpath.active; i++)
96 state->altpath.blk[i].bp = NULL;
97 state->altpath.active = 0;
101 * Free a da-state structure.
104 xfs_da_state_free(xfs_da_state_t *state)
106 xfs_da_state_kill_altpath(state);
108 memset((char *)state, 0, sizeof(*state));
110 kmem_zone_free(xfs_da_state_zone, state);
114 xfs_da3_node_hdr_from_disk(
115 struct xfs_mount *mp,
116 struct xfs_da3_icnode_hdr *to,
117 struct xfs_da_intnode *from)
119 if (xfs_sb_version_hascrc(&mp->m_sb)) {
120 struct xfs_da3_intnode *from3 = (struct xfs_da3_intnode *)from;
122 to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
123 to->back = be32_to_cpu(from3->hdr.info.hdr.back);
124 to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
125 to->count = be16_to_cpu(from3->hdr.__count);
126 to->level = be16_to_cpu(from3->hdr.__level);
127 to->btree = from3->__btree;
128 ASSERT(to->magic == XFS_DA3_NODE_MAGIC);
130 to->forw = be32_to_cpu(from->hdr.info.forw);
131 to->back = be32_to_cpu(from->hdr.info.back);
132 to->magic = be16_to_cpu(from->hdr.info.magic);
133 to->count = be16_to_cpu(from->hdr.__count);
134 to->level = be16_to_cpu(from->hdr.__level);
135 to->btree = from->__btree;
136 ASSERT(to->magic == XFS_DA_NODE_MAGIC);
141 xfs_da3_node_hdr_to_disk(
142 struct xfs_mount *mp,
143 struct xfs_da_intnode *to,
144 struct xfs_da3_icnode_hdr *from)
146 if (xfs_sb_version_hascrc(&mp->m_sb)) {
147 struct xfs_da3_intnode *to3 = (struct xfs_da3_intnode *)to;
149 ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
150 to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
151 to3->hdr.info.hdr.back = cpu_to_be32(from->back);
152 to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
153 to3->hdr.__count = cpu_to_be16(from->count);
154 to3->hdr.__level = cpu_to_be16(from->level);
156 ASSERT(from->magic == XFS_DA_NODE_MAGIC);
157 to->hdr.info.forw = cpu_to_be32(from->forw);
158 to->hdr.info.back = cpu_to_be32(from->back);
159 to->hdr.info.magic = cpu_to_be16(from->magic);
160 to->hdr.__count = cpu_to_be16(from->count);
161 to->hdr.__level = cpu_to_be16(from->level);
166 * Verify an xfs_da3_blkinfo structure. Note that the da3 fields are only
167 * accessible on v5 filesystems. This header format is common across da node,
168 * attr leaf and dir leaf blocks.
171 xfs_da3_blkinfo_verify(
173 struct xfs_da3_blkinfo *hdr3)
175 struct xfs_mount *mp = bp->b_mount;
176 struct xfs_da_blkinfo *hdr = &hdr3->hdr;
178 if (!xfs_verify_magic16(bp, hdr->magic))
179 return __this_address;
181 if (xfs_sb_version_hascrc(&mp->m_sb)) {
182 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
183 return __this_address;
184 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
185 return __this_address;
186 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
187 return __this_address;
193 static xfs_failaddr_t
197 struct xfs_mount *mp = bp->b_mount;
198 struct xfs_da_intnode *hdr = bp->b_addr;
199 struct xfs_da3_icnode_hdr ichdr;
202 xfs_da3_node_hdr_from_disk(mp, &ichdr, hdr);
204 fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
208 if (ichdr.level == 0)
209 return __this_address;
210 if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
211 return __this_address;
212 if (ichdr.count == 0)
213 return __this_address;
216 * we don't know if the node is for and attribute or directory tree,
217 * so only fail if the count is outside both bounds
219 if (ichdr.count > mp->m_dir_geo->node_ents &&
220 ichdr.count > mp->m_attr_geo->node_ents)
221 return __this_address;
223 /* XXX: hash order check? */
229 xfs_da3_node_write_verify(
232 struct xfs_mount *mp = bp->b_mount;
233 struct xfs_buf_log_item *bip = bp->b_log_item;
234 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
237 fa = xfs_da3_node_verify(bp);
239 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
243 if (!xfs_sb_version_hascrc(&mp->m_sb))
247 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
249 xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
253 * leaf/node format detection on trees is sketchy, so a node read can be done on
254 * leaf level blocks when detection identifies the tree as a node format tree
255 * incorrectly. In this case, we need to swap the verifier to match the correct
256 * format of the block being read.
259 xfs_da3_node_read_verify(
262 struct xfs_da_blkinfo *info = bp->b_addr;
265 switch (be16_to_cpu(info->magic)) {
266 case XFS_DA3_NODE_MAGIC:
267 if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
268 xfs_verifier_error(bp, -EFSBADCRC,
273 case XFS_DA_NODE_MAGIC:
274 fa = xfs_da3_node_verify(bp);
276 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
278 case XFS_ATTR_LEAF_MAGIC:
279 case XFS_ATTR3_LEAF_MAGIC:
280 bp->b_ops = &xfs_attr3_leaf_buf_ops;
281 bp->b_ops->verify_read(bp);
283 case XFS_DIR2_LEAFN_MAGIC:
284 case XFS_DIR3_LEAFN_MAGIC:
285 bp->b_ops = &xfs_dir3_leafn_buf_ops;
286 bp->b_ops->verify_read(bp);
289 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
294 /* Verify the structure of a da3 block. */
295 static xfs_failaddr_t
296 xfs_da3_node_verify_struct(
299 struct xfs_da_blkinfo *info = bp->b_addr;
301 switch (be16_to_cpu(info->magic)) {
302 case XFS_DA3_NODE_MAGIC:
303 case XFS_DA_NODE_MAGIC:
304 return xfs_da3_node_verify(bp);
305 case XFS_ATTR_LEAF_MAGIC:
306 case XFS_ATTR3_LEAF_MAGIC:
307 bp->b_ops = &xfs_attr3_leaf_buf_ops;
308 return bp->b_ops->verify_struct(bp);
309 case XFS_DIR2_LEAFN_MAGIC:
310 case XFS_DIR3_LEAFN_MAGIC:
311 bp->b_ops = &xfs_dir3_leafn_buf_ops;
312 return bp->b_ops->verify_struct(bp);
314 return __this_address;
318 const struct xfs_buf_ops xfs_da3_node_buf_ops = {
319 .name = "xfs_da3_node",
320 .magic16 = { cpu_to_be16(XFS_DA_NODE_MAGIC),
321 cpu_to_be16(XFS_DA3_NODE_MAGIC) },
322 .verify_read = xfs_da3_node_read_verify,
323 .verify_write = xfs_da3_node_write_verify,
324 .verify_struct = xfs_da3_node_verify_struct,
329 struct xfs_trans *tp,
330 struct xfs_inode *dp,
332 xfs_daddr_t mappedbno,
333 struct xfs_buf **bpp,
338 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
339 which_fork, &xfs_da3_node_buf_ops);
340 if (!err && tp && *bpp) {
341 struct xfs_da_blkinfo *info = (*bpp)->b_addr;
344 switch (be16_to_cpu(info->magic)) {
345 case XFS_DA_NODE_MAGIC:
346 case XFS_DA3_NODE_MAGIC:
347 type = XFS_BLFT_DA_NODE_BUF;
349 case XFS_ATTR_LEAF_MAGIC:
350 case XFS_ATTR3_LEAF_MAGIC:
351 type = XFS_BLFT_ATTR_LEAF_BUF;
353 case XFS_DIR2_LEAFN_MAGIC:
354 case XFS_DIR3_LEAFN_MAGIC:
355 type = XFS_BLFT_DIR_LEAFN_BUF;
358 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
359 tp->t_mountp, info, sizeof(*info));
360 xfs_trans_brelse(tp, *bpp);
362 return -EFSCORRUPTED;
364 xfs_trans_buf_set_type(tp, *bpp, type);
369 /*========================================================================
370 * Routines used for growing the Btree.
371 *========================================================================*/
374 * Create the initial contents of an intermediate node.
378 struct xfs_da_args *args,
381 struct xfs_buf **bpp,
384 struct xfs_da_intnode *node;
385 struct xfs_trans *tp = args->trans;
386 struct xfs_mount *mp = tp->t_mountp;
387 struct xfs_da3_icnode_hdr ichdr = {0};
390 struct xfs_inode *dp = args->dp;
392 trace_xfs_da_node_create(args);
393 ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
395 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, whichfork);
398 bp->b_ops = &xfs_da3_node_buf_ops;
399 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
402 if (xfs_sb_version_hascrc(&mp->m_sb)) {
403 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
405 memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
406 ichdr.magic = XFS_DA3_NODE_MAGIC;
407 hdr3->info.blkno = cpu_to_be64(bp->b_bn);
408 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
409 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
411 ichdr.magic = XFS_DA_NODE_MAGIC;
415 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &ichdr);
416 xfs_trans_log_buf(tp, bp,
417 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
424 * Split a leaf node, rebalance, then possibly split
425 * intermediate nodes, rebalance, etc.
429 struct xfs_da_state *state)
431 struct xfs_da_state_blk *oldblk;
432 struct xfs_da_state_blk *newblk;
433 struct xfs_da_state_blk *addblk;
434 struct xfs_da_intnode *node;
440 trace_xfs_da_split(state->args);
443 * Walk back up the tree splitting/inserting/adjusting as necessary.
444 * If we need to insert and there isn't room, split the node, then
445 * decide which fragment to insert the new block from below into.
446 * Note that we may split the root this way, but we need more fixup.
448 max = state->path.active - 1;
449 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
450 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
451 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
453 addblk = &state->path.blk[max]; /* initial dummy value */
454 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
455 oldblk = &state->path.blk[i];
456 newblk = &state->altpath.blk[i];
459 * If a leaf node then
460 * Allocate a new leaf node, then rebalance across them.
461 * else if an intermediate node then
462 * We split on the last layer, must we split the node?
464 switch (oldblk->magic) {
465 case XFS_ATTR_LEAF_MAGIC:
466 error = xfs_attr3_leaf_split(state, oldblk, newblk);
467 if ((error != 0) && (error != -ENOSPC)) {
468 return error; /* GROT: attr is inconsistent */
475 * Entry wouldn't fit, split the leaf again. The new
476 * extrablk will be consumed by xfs_da3_node_split if
479 state->extravalid = 1;
481 state->extraafter = 0; /* before newblk */
482 trace_xfs_attr_leaf_split_before(state->args);
483 error = xfs_attr3_leaf_split(state, oldblk,
486 state->extraafter = 1; /* after newblk */
487 trace_xfs_attr_leaf_split_after(state->args);
488 error = xfs_attr3_leaf_split(state, newblk,
492 return error; /* GROT: attr inconsistent */
495 case XFS_DIR2_LEAFN_MAGIC:
496 error = xfs_dir2_leafn_split(state, oldblk, newblk);
501 case XFS_DA_NODE_MAGIC:
502 error = xfs_da3_node_split(state, oldblk, newblk, addblk,
506 return error; /* GROT: dir is inconsistent */
508 * Record the newly split block for the next time thru?
518 * Update the btree to show the new hashval for this child.
520 xfs_da3_fixhashpath(state, &state->path);
526 * xfs_da3_node_split() should have consumed any extra blocks we added
527 * during a double leaf split in the attr fork. This is guaranteed as
528 * we can't be here if the attr fork only has a single leaf block.
530 ASSERT(state->extravalid == 0 ||
531 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
534 * Split the root node.
536 ASSERT(state->path.active == 0);
537 oldblk = &state->path.blk[0];
538 error = xfs_da3_root_split(state, oldblk, addblk);
543 * Update pointers to the node which used to be block 0 and just got
544 * bumped because of the addition of a new root node. Note that the
545 * original block 0 could be at any position in the list of blocks in
548 * Note: the magic numbers and sibling pointers are in the same physical
549 * place for both v2 and v3 headers (by design). Hence it doesn't matter
550 * which version of the xfs_da_intnode structure we use here as the
551 * result will be the same using either structure.
553 node = oldblk->bp->b_addr;
554 if (node->hdr.info.forw) {
555 if (be32_to_cpu(node->hdr.info.forw) != addblk->blkno) {
556 xfs_buf_corruption_error(oldblk->bp);
557 error = -EFSCORRUPTED;
560 node = addblk->bp->b_addr;
561 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
562 xfs_trans_log_buf(state->args->trans, addblk->bp,
563 XFS_DA_LOGRANGE(node, &node->hdr.info,
564 sizeof(node->hdr.info)));
566 node = oldblk->bp->b_addr;
567 if (node->hdr.info.back) {
568 if (be32_to_cpu(node->hdr.info.back) != addblk->blkno) {
569 xfs_buf_corruption_error(oldblk->bp);
570 error = -EFSCORRUPTED;
573 node = addblk->bp->b_addr;
574 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
575 xfs_trans_log_buf(state->args->trans, addblk->bp,
576 XFS_DA_LOGRANGE(node, &node->hdr.info,
577 sizeof(node->hdr.info)));
585 * Split the root. We have to create a new root and point to the two
586 * parts (the split old root) that we just created. Copy block zero to
587 * the EOF, extending the inode in process.
589 STATIC int /* error */
591 struct xfs_da_state *state,
592 struct xfs_da_state_blk *blk1,
593 struct xfs_da_state_blk *blk2)
595 struct xfs_da_intnode *node;
596 struct xfs_da_intnode *oldroot;
597 struct xfs_da_node_entry *btree;
598 struct xfs_da3_icnode_hdr nodehdr;
599 struct xfs_da_args *args;
601 struct xfs_inode *dp;
602 struct xfs_trans *tp;
603 struct xfs_dir2_leaf *leaf;
609 trace_xfs_da_root_split(state->args);
612 * Copy the existing (incorrect) block from the root node position
613 * to a free space somewhere.
616 error = xfs_da_grow_inode(args, &blkno);
622 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
626 oldroot = blk1->bp->b_addr;
627 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
628 oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
629 struct xfs_da3_icnode_hdr icnodehdr;
631 xfs_da3_node_hdr_from_disk(dp->i_mount, &icnodehdr, oldroot);
632 btree = icnodehdr.btree;
633 size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
634 level = icnodehdr.level;
637 * we are about to copy oldroot to bp, so set up the type
638 * of bp while we know exactly what it will be.
640 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
642 struct xfs_dir3_icleaf_hdr leafhdr;
643 struct xfs_dir2_leaf_entry *ents;
645 leaf = (xfs_dir2_leaf_t *)oldroot;
646 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
647 ents = dp->d_ops->leaf_ents_p(leaf);
649 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
650 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
651 size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
655 * we are about to copy oldroot to bp, so set up the type
656 * of bp while we know exactly what it will be.
658 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
662 * we can copy most of the information in the node from one block to
663 * another, but for CRC enabled headers we have to make sure that the
664 * block specific identifiers are kept intact. We update the buffer
667 memcpy(node, oldroot, size);
668 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
669 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
670 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
672 node3->hdr.info.blkno = cpu_to_be64(bp->b_bn);
674 xfs_trans_log_buf(tp, bp, 0, size - 1);
676 bp->b_ops = blk1->bp->b_ops;
677 xfs_trans_buf_copy_type(bp, blk1->bp);
682 * Set up the new root node.
684 error = xfs_da3_node_create(args,
685 (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
686 level + 1, &bp, args->whichfork);
691 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
692 btree = nodehdr.btree;
693 btree[0].hashval = cpu_to_be32(blk1->hashval);
694 btree[0].before = cpu_to_be32(blk1->blkno);
695 btree[1].hashval = cpu_to_be32(blk2->hashval);
696 btree[1].before = cpu_to_be32(blk2->blkno);
698 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
701 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
702 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
703 ASSERT(blk1->blkno >= args->geo->leafblk &&
704 blk1->blkno < args->geo->freeblk);
705 ASSERT(blk2->blkno >= args->geo->leafblk &&
706 blk2->blkno < args->geo->freeblk);
710 /* Header is already logged by xfs_da_node_create */
711 xfs_trans_log_buf(tp, bp,
712 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
718 * Split the node, rebalance, then add the new entry.
720 STATIC int /* error */
722 struct xfs_da_state *state,
723 struct xfs_da_state_blk *oldblk,
724 struct xfs_da_state_blk *newblk,
725 struct xfs_da_state_blk *addblk,
729 struct xfs_da_intnode *node;
730 struct xfs_da3_icnode_hdr nodehdr;
735 struct xfs_inode *dp = state->args->dp;
737 trace_xfs_da_node_split(state->args);
739 node = oldblk->bp->b_addr;
740 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
743 * With V2 dirs the extra block is data or freespace.
745 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
746 newcount = 1 + useextra;
748 * Do we have to split the node?
750 if (nodehdr.count + newcount > state->args->geo->node_ents) {
752 * Allocate a new node, add to the doubly linked chain of
753 * nodes, then move some of our excess entries into it.
755 error = xfs_da_grow_inode(state->args, &blkno);
757 return error; /* GROT: dir is inconsistent */
759 error = xfs_da3_node_create(state->args, blkno, treelevel,
760 &newblk->bp, state->args->whichfork);
762 return error; /* GROT: dir is inconsistent */
763 newblk->blkno = blkno;
764 newblk->magic = XFS_DA_NODE_MAGIC;
765 xfs_da3_node_rebalance(state, oldblk, newblk);
766 error = xfs_da3_blk_link(state, oldblk, newblk);
775 * Insert the new entry(s) into the correct block
776 * (updating last hashval in the process).
778 * xfs_da3_node_add() inserts BEFORE the given index,
779 * and as a result of using node_lookup_int() we always
780 * point to a valid entry (not after one), but a split
781 * operation always results in a new block whose hashvals
782 * FOLLOW the current block.
784 * If we had double-split op below us, then add the extra block too.
786 node = oldblk->bp->b_addr;
787 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
788 if (oldblk->index <= nodehdr.count) {
790 xfs_da3_node_add(state, oldblk, addblk);
792 if (state->extraafter)
794 xfs_da3_node_add(state, oldblk, &state->extrablk);
795 state->extravalid = 0;
799 xfs_da3_node_add(state, newblk, addblk);
801 if (state->extraafter)
803 xfs_da3_node_add(state, newblk, &state->extrablk);
804 state->extravalid = 0;
812 * Balance the btree elements between two intermediate nodes,
813 * usually one full and one empty.
815 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
818 xfs_da3_node_rebalance(
819 struct xfs_da_state *state,
820 struct xfs_da_state_blk *blk1,
821 struct xfs_da_state_blk *blk2)
823 struct xfs_da_intnode *node1;
824 struct xfs_da_intnode *node2;
825 struct xfs_da_intnode *tmpnode;
826 struct xfs_da_node_entry *btree1;
827 struct xfs_da_node_entry *btree2;
828 struct xfs_da_node_entry *btree_s;
829 struct xfs_da_node_entry *btree_d;
830 struct xfs_da3_icnode_hdr nodehdr1;
831 struct xfs_da3_icnode_hdr nodehdr2;
832 struct xfs_trans *tp;
836 struct xfs_inode *dp = state->args->dp;
838 trace_xfs_da_node_rebalance(state->args);
840 node1 = blk1->bp->b_addr;
841 node2 = blk2->bp->b_addr;
842 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
843 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
844 btree1 = nodehdr1.btree;
845 btree2 = nodehdr2.btree;
848 * Figure out how many entries need to move, and in which direction.
849 * Swap the nodes around if that makes it simpler.
851 if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
852 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
853 (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
854 be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
858 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
859 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
860 btree1 = nodehdr1.btree;
861 btree2 = nodehdr2.btree;
865 count = (nodehdr1.count - nodehdr2.count) / 2;
868 tp = state->args->trans;
870 * Two cases: high-to-low and low-to-high.
874 * Move elements in node2 up to make a hole.
876 tmp = nodehdr2.count;
878 tmp *= (uint)sizeof(xfs_da_node_entry_t);
879 btree_s = &btree2[0];
880 btree_d = &btree2[count];
881 memmove(btree_d, btree_s, tmp);
885 * Move the req'd B-tree elements from high in node1 to
888 nodehdr2.count += count;
889 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
890 btree_s = &btree1[nodehdr1.count - count];
891 btree_d = &btree2[0];
892 memcpy(btree_d, btree_s, tmp);
893 nodehdr1.count -= count;
896 * Move the req'd B-tree elements from low in node2 to
900 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
901 btree_s = &btree2[0];
902 btree_d = &btree1[nodehdr1.count];
903 memcpy(btree_d, btree_s, tmp);
904 nodehdr1.count += count;
906 xfs_trans_log_buf(tp, blk1->bp,
907 XFS_DA_LOGRANGE(node1, btree_d, tmp));
910 * Move elements in node2 down to fill the hole.
912 tmp = nodehdr2.count - count;
913 tmp *= (uint)sizeof(xfs_da_node_entry_t);
914 btree_s = &btree2[count];
915 btree_d = &btree2[0];
916 memmove(btree_d, btree_s, tmp);
917 nodehdr2.count -= count;
921 * Log header of node 1 and all current bits of node 2.
923 xfs_da3_node_hdr_to_disk(dp->i_mount, node1, &nodehdr1);
924 xfs_trans_log_buf(tp, blk1->bp,
925 XFS_DA_LOGRANGE(node1, &node1->hdr, dp->d_ops->node_hdr_size));
927 xfs_da3_node_hdr_to_disk(dp->i_mount, node2, &nodehdr2);
928 xfs_trans_log_buf(tp, blk2->bp,
929 XFS_DA_LOGRANGE(node2, &node2->hdr,
930 dp->d_ops->node_hdr_size +
931 (sizeof(btree2[0]) * nodehdr2.count)));
934 * Record the last hashval from each block for upward propagation.
935 * (note: don't use the swapped node pointers)
938 node1 = blk1->bp->b_addr;
939 node2 = blk2->bp->b_addr;
940 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
941 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
942 btree1 = nodehdr1.btree;
943 btree2 = nodehdr2.btree;
945 blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
946 blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
949 * Adjust the expected index for insertion.
951 if (blk1->index >= nodehdr1.count) {
952 blk2->index = blk1->index - nodehdr1.count;
953 blk1->index = nodehdr1.count + 1; /* make it invalid */
958 * Add a new entry to an intermediate node.
962 struct xfs_da_state *state,
963 struct xfs_da_state_blk *oldblk,
964 struct xfs_da_state_blk *newblk)
966 struct xfs_da_intnode *node;
967 struct xfs_da3_icnode_hdr nodehdr;
968 struct xfs_da_node_entry *btree;
970 struct xfs_inode *dp = state->args->dp;
972 trace_xfs_da_node_add(state->args);
974 node = oldblk->bp->b_addr;
975 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
976 btree = nodehdr.btree;
978 ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
979 ASSERT(newblk->blkno != 0);
980 if (state->args->whichfork == XFS_DATA_FORK)
981 ASSERT(newblk->blkno >= state->args->geo->leafblk &&
982 newblk->blkno < state->args->geo->freeblk);
985 * We may need to make some room before we insert the new node.
988 if (oldblk->index < nodehdr.count) {
989 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
990 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
992 btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
993 btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
994 xfs_trans_log_buf(state->args->trans, oldblk->bp,
995 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
996 tmp + sizeof(*btree)));
999 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1000 xfs_trans_log_buf(state->args->trans, oldblk->bp,
1001 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
1004 * Copy the last hash value from the oldblk to propagate upwards.
1006 oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1009 /*========================================================================
1010 * Routines used for shrinking the Btree.
1011 *========================================================================*/
1014 * Deallocate an empty leaf node, remove it from its parent,
1015 * possibly deallocating that block, etc...
1019 struct xfs_da_state *state)
1021 struct xfs_da_state_blk *drop_blk;
1022 struct xfs_da_state_blk *save_blk;
1026 trace_xfs_da_join(state->args);
1028 drop_blk = &state->path.blk[ state->path.active-1 ];
1029 save_blk = &state->altpath.blk[ state->path.active-1 ];
1030 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
1031 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
1032 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1035 * Walk back up the tree joining/deallocating as necessary.
1036 * When we stop dropping blocks, break out.
1038 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
1039 state->path.active--) {
1041 * See if we can combine the block with a neighbor.
1042 * (action == 0) => no options, just leave
1043 * (action == 1) => coalesce, then unlink
1044 * (action == 2) => block empty, unlink it
1046 switch (drop_blk->magic) {
1047 case XFS_ATTR_LEAF_MAGIC:
1048 error = xfs_attr3_leaf_toosmall(state, &action);
1053 xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
1055 case XFS_DIR2_LEAFN_MAGIC:
1056 error = xfs_dir2_leafn_toosmall(state, &action);
1061 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
1063 case XFS_DA_NODE_MAGIC:
1065 * Remove the offending node, fixup hashvals,
1066 * check for a toosmall neighbor.
1068 xfs_da3_node_remove(state, drop_blk);
1069 xfs_da3_fixhashpath(state, &state->path);
1070 error = xfs_da3_node_toosmall(state, &action);
1075 xfs_da3_node_unbalance(state, drop_blk, save_blk);
1078 xfs_da3_fixhashpath(state, &state->altpath);
1079 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
1080 xfs_da_state_kill_altpath(state);
1083 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1085 drop_blk->bp = NULL;
1090 * We joined all the way to the top. If it turns out that
1091 * we only have one entry in the root, make the child block
1094 xfs_da3_node_remove(state, drop_blk);
1095 xfs_da3_fixhashpath(state, &state->path);
1096 error = xfs_da3_root_join(state, &state->path.blk[0]);
1102 xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1104 __be16 magic = blkinfo->magic;
1107 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1108 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
1109 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1110 magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1112 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1113 magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1115 ASSERT(!blkinfo->forw);
1116 ASSERT(!blkinfo->back);
1119 #define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1123 * We have only one entry in the root. Copy the only remaining child of
1124 * the old root to block 0 as the new root node.
1128 struct xfs_da_state *state,
1129 struct xfs_da_state_blk *root_blk)
1131 struct xfs_da_intnode *oldroot;
1132 struct xfs_da_args *args;
1135 struct xfs_da3_icnode_hdr oldroothdr;
1137 struct xfs_inode *dp = state->args->dp;
1139 trace_xfs_da_root_join(state->args);
1141 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
1144 oldroot = root_blk->bp->b_addr;
1145 xfs_da3_node_hdr_from_disk(dp->i_mount, &oldroothdr, oldroot);
1146 ASSERT(oldroothdr.forw == 0);
1147 ASSERT(oldroothdr.back == 0);
1150 * If the root has more than one child, then don't do anything.
1152 if (oldroothdr.count > 1)
1156 * Read in the (only) child block, then copy those bytes into
1157 * the root block's buffer and free the original child block.
1159 child = be32_to_cpu(oldroothdr.btree[0].before);
1161 error = xfs_da3_node_read(args->trans, dp, child, -1, &bp,
1165 xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
1168 * This could be copying a leaf back into the root block in the case of
1169 * there only being a single leaf block left in the tree. Hence we have
1170 * to update the b_ops pointer as well to match the buffer type change
1171 * that could occur. For dir3 blocks we also need to update the block
1172 * number in the buffer header.
1174 memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
1175 root_blk->bp->b_ops = bp->b_ops;
1176 xfs_trans_buf_copy_type(root_blk->bp, bp);
1177 if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1178 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1179 da3->blkno = cpu_to_be64(root_blk->bp->b_bn);
1181 xfs_trans_log_buf(args->trans, root_blk->bp, 0,
1182 args->geo->blksize - 1);
1183 error = xfs_da_shrink_inode(args, child, bp);
1188 * Check a node block and its neighbors to see if the block should be
1189 * collapsed into one or the other neighbor. Always keep the block
1190 * with the smaller block number.
1191 * If the current block is over 50% full, don't try to join it, return 0.
1192 * If the block is empty, fill in the state structure and return 2.
1193 * If it can be collapsed, fill in the state structure and return 1.
1194 * If nothing can be done, return 0.
1197 xfs_da3_node_toosmall(
1198 struct xfs_da_state *state,
1201 struct xfs_da_intnode *node;
1202 struct xfs_da_state_blk *blk;
1203 struct xfs_da_blkinfo *info;
1206 struct xfs_da3_icnode_hdr nodehdr;
1212 struct xfs_inode *dp = state->args->dp;
1214 trace_xfs_da_node_toosmall(state->args);
1217 * Check for the degenerate case of the block being over 50% full.
1218 * If so, it's not worth even looking to see if we might be able
1219 * to coalesce with a sibling.
1221 blk = &state->path.blk[ state->path.active-1 ];
1222 info = blk->bp->b_addr;
1223 node = (xfs_da_intnode_t *)info;
1224 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1225 if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
1226 *action = 0; /* blk over 50%, don't try to join */
1227 return 0; /* blk over 50%, don't try to join */
1231 * Check for the degenerate case of the block being empty.
1232 * If the block is empty, we'll simply delete it, no need to
1233 * coalesce it with a sibling block. We choose (arbitrarily)
1234 * to merge with the forward block unless it is NULL.
1236 if (nodehdr.count == 0) {
1238 * Make altpath point to the block we want to keep and
1239 * path point to the block we want to drop (this one).
1241 forward = (info->forw != 0);
1242 memcpy(&state->altpath, &state->path, sizeof(state->path));
1243 error = xfs_da3_path_shift(state, &state->altpath, forward,
1256 * Examine each sibling block to see if we can coalesce with
1257 * at least 25% free space to spare. We need to figure out
1258 * whether to merge with the forward or the backward block.
1259 * We prefer coalescing with the lower numbered sibling so as
1260 * to shrink a directory over time.
1262 count = state->args->geo->node_ents;
1263 count -= state->args->geo->node_ents >> 2;
1264 count -= nodehdr.count;
1266 /* start with smaller blk num */
1267 forward = nodehdr.forw < nodehdr.back;
1268 for (i = 0; i < 2; forward = !forward, i++) {
1269 struct xfs_da3_icnode_hdr thdr;
1271 blkno = nodehdr.forw;
1273 blkno = nodehdr.back;
1276 error = xfs_da3_node_read(state->args->trans, dp,
1277 blkno, -1, &bp, state->args->whichfork);
1282 xfs_da3_node_hdr_from_disk(dp->i_mount, &thdr, node);
1283 xfs_trans_brelse(state->args->trans, bp);
1285 if (count - thdr.count >= 0)
1286 break; /* fits with at least 25% to spare */
1294 * Make altpath point to the block we want to keep (the lower
1295 * numbered block) and path point to the block we want to drop.
1297 memcpy(&state->altpath, &state->path, sizeof(state->path));
1298 if (blkno < blk->blkno) {
1299 error = xfs_da3_path_shift(state, &state->altpath, forward,
1302 error = xfs_da3_path_shift(state, &state->path, forward,
1316 * Pick up the last hashvalue from an intermediate node.
1319 xfs_da3_node_lasthash(
1320 struct xfs_inode *dp,
1324 struct xfs_da3_icnode_hdr nodehdr;
1326 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, bp->b_addr);
1328 *count = nodehdr.count;
1331 return be32_to_cpu(nodehdr.btree[nodehdr.count - 1].hashval);
1335 * Walk back up the tree adjusting hash values as necessary,
1336 * when we stop making changes, return.
1339 xfs_da3_fixhashpath(
1340 struct xfs_da_state *state,
1341 struct xfs_da_state_path *path)
1343 struct xfs_da_state_blk *blk;
1344 struct xfs_da_intnode *node;
1345 struct xfs_da_node_entry *btree;
1346 xfs_dahash_t lasthash=0;
1349 struct xfs_inode *dp = state->args->dp;
1351 trace_xfs_da_fixhashpath(state->args);
1353 level = path->active-1;
1354 blk = &path->blk[ level ];
1355 switch (blk->magic) {
1356 case XFS_ATTR_LEAF_MAGIC:
1357 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1361 case XFS_DIR2_LEAFN_MAGIC:
1362 lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
1366 case XFS_DA_NODE_MAGIC:
1367 lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
1372 for (blk--, level--; level >= 0; blk--, level--) {
1373 struct xfs_da3_icnode_hdr nodehdr;
1375 node = blk->bp->b_addr;
1376 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1377 btree = nodehdr.btree;
1378 if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
1380 blk->hashval = lasthash;
1381 btree[blk->index].hashval = cpu_to_be32(lasthash);
1382 xfs_trans_log_buf(state->args->trans, blk->bp,
1383 XFS_DA_LOGRANGE(node, &btree[blk->index],
1386 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1391 * Remove an entry from an intermediate node.
1394 xfs_da3_node_remove(
1395 struct xfs_da_state *state,
1396 struct xfs_da_state_blk *drop_blk)
1398 struct xfs_da_intnode *node;
1399 struct xfs_da3_icnode_hdr nodehdr;
1400 struct xfs_da_node_entry *btree;
1403 struct xfs_inode *dp = state->args->dp;
1405 trace_xfs_da_node_remove(state->args);
1407 node = drop_blk->bp->b_addr;
1408 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1409 ASSERT(drop_blk->index < nodehdr.count);
1410 ASSERT(drop_blk->index >= 0);
1413 * Copy over the offending entry, or just zero it out.
1415 index = drop_blk->index;
1416 btree = nodehdr.btree;
1417 if (index < nodehdr.count - 1) {
1418 tmp = nodehdr.count - index - 1;
1419 tmp *= (uint)sizeof(xfs_da_node_entry_t);
1420 memmove(&btree[index], &btree[index + 1], tmp);
1421 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1422 XFS_DA_LOGRANGE(node, &btree[index], tmp));
1423 index = nodehdr.count - 1;
1425 memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
1426 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1427 XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1429 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1430 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1431 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
1434 * Copy the last hash value from the block to propagate upwards.
1436 drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
1440 * Unbalance the elements between two intermediate nodes,
1441 * move all Btree elements from one node into another.
1444 xfs_da3_node_unbalance(
1445 struct xfs_da_state *state,
1446 struct xfs_da_state_blk *drop_blk,
1447 struct xfs_da_state_blk *save_blk)
1449 struct xfs_da_intnode *drop_node;
1450 struct xfs_da_intnode *save_node;
1451 struct xfs_da_node_entry *drop_btree;
1452 struct xfs_da_node_entry *save_btree;
1453 struct xfs_da3_icnode_hdr drop_hdr;
1454 struct xfs_da3_icnode_hdr save_hdr;
1455 struct xfs_trans *tp;
1458 struct xfs_inode *dp = state->args->dp;
1460 trace_xfs_da_node_unbalance(state->args);
1462 drop_node = drop_blk->bp->b_addr;
1463 save_node = save_blk->bp->b_addr;
1464 xfs_da3_node_hdr_from_disk(dp->i_mount, &drop_hdr, drop_node);
1465 xfs_da3_node_hdr_from_disk(dp->i_mount, &save_hdr, save_node);
1466 drop_btree = drop_hdr.btree;
1467 save_btree = save_hdr.btree;
1468 tp = state->args->trans;
1471 * If the dying block has lower hashvals, then move all the
1472 * elements in the remaining block up to make a hole.
1474 if ((be32_to_cpu(drop_btree[0].hashval) <
1475 be32_to_cpu(save_btree[0].hashval)) ||
1476 (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1477 be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1478 /* XXX: check this - is memmove dst correct? */
1479 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1480 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1483 xfs_trans_log_buf(tp, save_blk->bp,
1484 XFS_DA_LOGRANGE(save_node, &save_btree[0],
1485 (save_hdr.count + drop_hdr.count) *
1486 sizeof(xfs_da_node_entry_t)));
1488 sindex = save_hdr.count;
1489 xfs_trans_log_buf(tp, save_blk->bp,
1490 XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1491 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
1495 * Move all the B-tree elements from drop_blk to save_blk.
1497 tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1498 memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1499 save_hdr.count += drop_hdr.count;
1501 xfs_da3_node_hdr_to_disk(dp->i_mount, save_node, &save_hdr);
1502 xfs_trans_log_buf(tp, save_blk->bp,
1503 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1504 dp->d_ops->node_hdr_size));
1507 * Save the last hashval in the remaining block for upward propagation.
1509 save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
1512 /*========================================================================
1513 * Routines used for finding things in the Btree.
1514 *========================================================================*/
1517 * Walk down the Btree looking for a particular filename, filling
1518 * in the state structure as we go.
1520 * We will set the state structure to point to each of the elements
1521 * in each of the nodes where either the hashval is or should be.
1523 * We support duplicate hashval's so for each entry in the current
1524 * node that could contain the desired hashval, descend. This is a
1525 * pruned depth-first tree search.
1528 xfs_da3_node_lookup_int(
1529 struct xfs_da_state *state,
1532 struct xfs_da_state_blk *blk;
1533 struct xfs_da_blkinfo *curr;
1534 struct xfs_da_intnode *node;
1535 struct xfs_da_node_entry *btree;
1536 struct xfs_da3_icnode_hdr nodehdr;
1537 struct xfs_da_args *args;
1539 xfs_dahash_t hashval;
1540 xfs_dahash_t btreehashval;
1546 unsigned int expected_level = 0;
1548 struct xfs_inode *dp = state->args->dp;
1553 * Descend thru the B-tree searching each level for the right
1554 * node to use, until the right hashval is found.
1556 blkno = args->geo->leafblk;
1557 for (blk = &state->path.blk[0], state->path.active = 1;
1558 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1559 blk++, state->path.active++) {
1561 * Read the next node down in the tree.
1564 error = xfs_da3_node_read(args->trans, args->dp, blkno,
1565 -1, &blk->bp, args->whichfork);
1568 state->path.active--;
1571 curr = blk->bp->b_addr;
1572 magic = be16_to_cpu(curr->magic);
1574 if (magic == XFS_ATTR_LEAF_MAGIC ||
1575 magic == XFS_ATTR3_LEAF_MAGIC) {
1576 blk->magic = XFS_ATTR_LEAF_MAGIC;
1577 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1581 if (magic == XFS_DIR2_LEAFN_MAGIC ||
1582 magic == XFS_DIR3_LEAFN_MAGIC) {
1583 blk->magic = XFS_DIR2_LEAFN_MAGIC;
1584 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1589 if (magic != XFS_DA_NODE_MAGIC && magic != XFS_DA3_NODE_MAGIC) {
1590 xfs_buf_corruption_error(blk->bp);
1591 return -EFSCORRUPTED;
1594 blk->magic = XFS_DA_NODE_MAGIC;
1597 * Search an intermediate node for a match.
1599 node = blk->bp->b_addr;
1600 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1601 btree = nodehdr.btree;
1603 /* Tree taller than we can handle; bail out! */
1604 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
1605 xfs_buf_corruption_error(blk->bp);
1606 return -EFSCORRUPTED;
1609 /* Check the level from the root. */
1610 if (blkno == args->geo->leafblk)
1611 expected_level = nodehdr.level - 1;
1612 else if (expected_level != nodehdr.level) {
1613 xfs_buf_corruption_error(blk->bp);
1614 return -EFSCORRUPTED;
1618 max = nodehdr.count;
1619 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
1622 * Binary search. (note: small blocks will skip loop)
1624 probe = span = max / 2;
1625 hashval = args->hashval;
1628 btreehashval = be32_to_cpu(btree[probe].hashval);
1629 if (btreehashval < hashval)
1631 else if (btreehashval > hashval)
1636 ASSERT((probe >= 0) && (probe < max));
1637 ASSERT((span <= 4) ||
1638 (be32_to_cpu(btree[probe].hashval) == hashval));
1641 * Since we may have duplicate hashval's, find the first
1642 * matching hashval in the node.
1645 be32_to_cpu(btree[probe].hashval) >= hashval) {
1648 while (probe < max &&
1649 be32_to_cpu(btree[probe].hashval) < hashval) {
1654 * Pick the right block to descend on.
1657 blk->index = max - 1;
1658 blkno = be32_to_cpu(btree[max - 1].before);
1661 blkno = be32_to_cpu(btree[probe].before);
1664 /* We can't point back to the root. */
1665 if (blkno == args->geo->leafblk) {
1666 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW,
1668 return -EFSCORRUPTED;
1672 if (expected_level != 0) {
1673 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, dp->i_mount);
1674 return -EFSCORRUPTED;
1678 * A leaf block that ends in the hashval that we are interested in
1679 * (final hashval == search hashval) means that the next block may
1680 * contain more entries with the same hashval, shift upward to the
1681 * next leaf and keep searching.
1684 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
1685 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1686 &blk->index, state);
1687 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1688 retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
1689 blk->index = args->index;
1690 args->blkno = blk->blkno;
1693 return -EFSCORRUPTED;
1695 if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
1696 (blk->hashval == args->hashval)) {
1697 error = xfs_da3_path_shift(state, &state->path, 1, 1,
1703 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1704 /* path_shift() gives ENOENT */
1714 /*========================================================================
1716 *========================================================================*/
1719 * Compare two intermediate nodes for "order".
1723 struct xfs_inode *dp,
1724 struct xfs_buf *node1_bp,
1725 struct xfs_buf *node2_bp)
1727 struct xfs_da_intnode *node1;
1728 struct xfs_da_intnode *node2;
1729 struct xfs_da_node_entry *btree1;
1730 struct xfs_da_node_entry *btree2;
1731 struct xfs_da3_icnode_hdr node1hdr;
1732 struct xfs_da3_icnode_hdr node2hdr;
1734 node1 = node1_bp->b_addr;
1735 node2 = node2_bp->b_addr;
1736 xfs_da3_node_hdr_from_disk(dp->i_mount, &node1hdr, node1);
1737 xfs_da3_node_hdr_from_disk(dp->i_mount, &node2hdr, node2);
1738 btree1 = node1hdr.btree;
1739 btree2 = node2hdr.btree;
1741 if (node1hdr.count > 0 && node2hdr.count > 0 &&
1742 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1743 (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1744 be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1751 * Link a new block into a doubly linked list of blocks (of whatever type).
1755 struct xfs_da_state *state,
1756 struct xfs_da_state_blk *old_blk,
1757 struct xfs_da_state_blk *new_blk)
1759 struct xfs_da_blkinfo *old_info;
1760 struct xfs_da_blkinfo *new_info;
1761 struct xfs_da_blkinfo *tmp_info;
1762 struct xfs_da_args *args;
1766 struct xfs_inode *dp = state->args->dp;
1769 * Set up environment.
1772 ASSERT(args != NULL);
1773 old_info = old_blk->bp->b_addr;
1774 new_info = new_blk->bp->b_addr;
1775 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
1776 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1777 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
1779 switch (old_blk->magic) {
1780 case XFS_ATTR_LEAF_MAGIC:
1781 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1783 case XFS_DIR2_LEAFN_MAGIC:
1784 before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
1786 case XFS_DA_NODE_MAGIC:
1787 before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
1792 * Link blocks in appropriate order.
1796 * Link new block in before existing block.
1798 trace_xfs_da_link_before(args);
1799 new_info->forw = cpu_to_be32(old_blk->blkno);
1800 new_info->back = old_info->back;
1801 if (old_info->back) {
1802 error = xfs_da3_node_read(args->trans, dp,
1803 be32_to_cpu(old_info->back),
1804 -1, &bp, args->whichfork);
1808 tmp_info = bp->b_addr;
1809 ASSERT(tmp_info->magic == old_info->magic);
1810 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1811 tmp_info->forw = cpu_to_be32(new_blk->blkno);
1812 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1814 old_info->back = cpu_to_be32(new_blk->blkno);
1817 * Link new block in after existing block.
1819 trace_xfs_da_link_after(args);
1820 new_info->forw = old_info->forw;
1821 new_info->back = cpu_to_be32(old_blk->blkno);
1822 if (old_info->forw) {
1823 error = xfs_da3_node_read(args->trans, dp,
1824 be32_to_cpu(old_info->forw),
1825 -1, &bp, args->whichfork);
1829 tmp_info = bp->b_addr;
1830 ASSERT(tmp_info->magic == old_info->magic);
1831 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1832 tmp_info->back = cpu_to_be32(new_blk->blkno);
1833 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1835 old_info->forw = cpu_to_be32(new_blk->blkno);
1838 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1839 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
1844 * Unlink a block from a doubly linked list of blocks.
1846 STATIC int /* error */
1848 struct xfs_da_state *state,
1849 struct xfs_da_state_blk *drop_blk,
1850 struct xfs_da_state_blk *save_blk)
1852 struct xfs_da_blkinfo *drop_info;
1853 struct xfs_da_blkinfo *save_info;
1854 struct xfs_da_blkinfo *tmp_info;
1855 struct xfs_da_args *args;
1860 * Set up environment.
1863 ASSERT(args != NULL);
1864 save_info = save_blk->bp->b_addr;
1865 drop_info = drop_blk->bp->b_addr;
1866 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
1867 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1868 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1869 ASSERT(save_blk->magic == drop_blk->magic);
1870 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1871 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1872 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1873 (be32_to_cpu(drop_info->back) == save_blk->blkno));
1876 * Unlink the leaf block from the doubly linked chain of leaves.
1878 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
1879 trace_xfs_da_unlink_back(args);
1880 save_info->back = drop_info->back;
1881 if (drop_info->back) {
1882 error = xfs_da3_node_read(args->trans, args->dp,
1883 be32_to_cpu(drop_info->back),
1884 -1, &bp, args->whichfork);
1888 tmp_info = bp->b_addr;
1889 ASSERT(tmp_info->magic == save_info->magic);
1890 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1891 tmp_info->forw = cpu_to_be32(save_blk->blkno);
1892 xfs_trans_log_buf(args->trans, bp, 0,
1893 sizeof(*tmp_info) - 1);
1896 trace_xfs_da_unlink_forward(args);
1897 save_info->forw = drop_info->forw;
1898 if (drop_info->forw) {
1899 error = xfs_da3_node_read(args->trans, args->dp,
1900 be32_to_cpu(drop_info->forw),
1901 -1, &bp, args->whichfork);
1905 tmp_info = bp->b_addr;
1906 ASSERT(tmp_info->magic == save_info->magic);
1907 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1908 tmp_info->back = cpu_to_be32(save_blk->blkno);
1909 xfs_trans_log_buf(args->trans, bp, 0,
1910 sizeof(*tmp_info) - 1);
1914 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
1919 * Move a path "forward" or "!forward" one block at the current level.
1921 * This routine will adjust a "path" to point to the next block
1922 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1923 * Btree, including updating pointers to the intermediate nodes between
1924 * the new bottom and the root.
1928 struct xfs_da_state *state,
1929 struct xfs_da_state_path *path,
1934 struct xfs_da_state_blk *blk;
1935 struct xfs_da_blkinfo *info;
1936 struct xfs_da_args *args;
1937 struct xfs_da_node_entry *btree;
1938 struct xfs_da3_icnode_hdr nodehdr;
1940 xfs_dablk_t blkno = 0;
1943 struct xfs_inode *dp = state->args->dp;
1945 trace_xfs_da_path_shift(state->args);
1948 * Roll up the Btree looking for the first block where our
1949 * current index is not at the edge of the block. Note that
1950 * we skip the bottom layer because we want the sibling block.
1953 ASSERT(args != NULL);
1954 ASSERT(path != NULL);
1955 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1956 level = (path->active-1) - 1; /* skip bottom layer in path */
1957 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
1958 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
1961 if (forward && (blk->index < nodehdr.count - 1)) {
1963 blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
1965 } else if (!forward && (blk->index > 0)) {
1967 blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
1972 *result = -ENOENT; /* we're out of our tree */
1973 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1978 * Roll down the edge of the subtree until we reach the
1979 * same depth we were at originally.
1981 for (blk++, level++; level < path->active; blk++, level++) {
1983 * Read the next child block into a local buffer.
1985 error = xfs_da3_node_read(args->trans, dp, blkno, -1, &bp,
1991 * Release the old block (if it's dirty, the trans doesn't
1992 * actually let go) and swap the local buffer into the path
1993 * structure. This ensures failure of the above read doesn't set
1994 * a NULL buffer in an active slot in the path.
1997 xfs_trans_brelse(args->trans, blk->bp);
2001 info = blk->bp->b_addr;
2002 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
2003 info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
2004 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2005 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
2006 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
2007 info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
2011 * Note: we flatten the magic number to a single type so we
2012 * don't have to compare against crc/non-crc types elsewhere.
2014 switch (be16_to_cpu(info->magic)) {
2015 case XFS_DA_NODE_MAGIC:
2016 case XFS_DA3_NODE_MAGIC:
2017 blk->magic = XFS_DA_NODE_MAGIC;
2018 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
2020 btree = nodehdr.btree;
2021 blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
2025 blk->index = nodehdr.count - 1;
2026 blkno = be32_to_cpu(btree[blk->index].before);
2028 case XFS_ATTR_LEAF_MAGIC:
2029 case XFS_ATTR3_LEAF_MAGIC:
2030 blk->magic = XFS_ATTR_LEAF_MAGIC;
2031 ASSERT(level == path->active-1);
2033 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
2035 case XFS_DIR2_LEAFN_MAGIC:
2036 case XFS_DIR3_LEAFN_MAGIC:
2037 blk->magic = XFS_DIR2_LEAFN_MAGIC;
2038 ASSERT(level == path->active-1);
2040 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
2053 /*========================================================================
2055 *========================================================================*/
2058 * Implement a simple hash on a character string.
2059 * Rotate the hash value by 7 bits, then XOR each character in.
2060 * This is implemented with some source-level loop unrolling.
2063 xfs_da_hashname(const uint8_t *name, int namelen)
2068 * Do four characters at a time as long as we can.
2070 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
2071 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
2072 (name[3] << 0) ^ rol32(hash, 7 * 4);
2075 * Now do the rest of the characters.
2079 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2082 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2084 return (name[0] << 0) ^ rol32(hash, 7 * 1);
2085 default: /* case 0: */
2092 struct xfs_da_args *args,
2093 const unsigned char *name,
2096 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2097 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2101 xfs_default_hashname(
2102 struct xfs_name *name)
2104 return xfs_da_hashname(name->name, name->len);
2107 const struct xfs_nameops xfs_default_nameops = {
2108 .hashname = xfs_default_hashname,
2109 .compname = xfs_da_compname
2113 xfs_da_grow_inode_int(
2114 struct xfs_da_args *args,
2118 struct xfs_trans *tp = args->trans;
2119 struct xfs_inode *dp = args->dp;
2120 int w = args->whichfork;
2121 xfs_rfsblock_t nblks = dp->i_d.di_nblocks;
2122 struct xfs_bmbt_irec map, *mapp;
2123 int nmap, error, got, i, mapi;
2126 * Find a spot in the file space to put the new block.
2128 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2133 * Try mapping it in one filesystem block.
2136 error = xfs_bmapi_write(tp, dp, *bno, count,
2137 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
2138 args->total, &map, &nmap);
2146 } else if (nmap == 0 && count > 1) {
2151 * If we didn't get it and the block might work if fragmented,
2152 * try without the CONTIG flag. Loop until we get it all.
2154 mapp = kmem_alloc(sizeof(*mapp) * count, 0);
2155 for (b = *bno, mapi = 0; b < *bno + count; ) {
2156 nmap = min(XFS_BMAP_MAX_NMAP, count);
2157 c = (int)(*bno + count - b);
2158 error = xfs_bmapi_write(tp, dp, b, c,
2159 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
2160 args->total, &mapp[mapi], &nmap);
2166 b = mapp[mapi - 1].br_startoff +
2167 mapp[mapi - 1].br_blockcount;
2175 * Count the blocks we got, make sure it matches the total.
2177 for (i = 0, got = 0; i < mapi; i++)
2178 got += mapp[i].br_blockcount;
2179 if (got != count || mapp[0].br_startoff != *bno ||
2180 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
2186 /* account for newly allocated blocks in reserved blocks total */
2187 args->total -= dp->i_d.di_nblocks - nblks;
2196 * Add a block to the btree ahead of the file.
2197 * Return the new block number to the caller.
2201 struct xfs_da_args *args,
2202 xfs_dablk_t *new_blkno)
2207 trace_xfs_da_grow_inode(args);
2209 bno = args->geo->leafblk;
2210 error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
2212 *new_blkno = (xfs_dablk_t)bno;
2217 * Ick. We need to always be able to remove a btree block, even
2218 * if there's no space reservation because the filesystem is full.
2219 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2220 * It swaps the target block with the last block in the file. The
2221 * last block in the file can always be removed since it can't cause
2222 * a bmap btree split to do that.
2225 xfs_da3_swap_lastblock(
2226 struct xfs_da_args *args,
2227 xfs_dablk_t *dead_blknop,
2228 struct xfs_buf **dead_bufp)
2230 struct xfs_da_blkinfo *dead_info;
2231 struct xfs_da_blkinfo *sib_info;
2232 struct xfs_da_intnode *par_node;
2233 struct xfs_da_intnode *dead_node;
2234 struct xfs_dir2_leaf *dead_leaf2;
2235 struct xfs_da_node_entry *btree;
2236 struct xfs_da3_icnode_hdr par_hdr;
2237 struct xfs_inode *dp;
2238 struct xfs_trans *tp;
2239 struct xfs_mount *mp;
2240 struct xfs_buf *dead_buf;
2241 struct xfs_buf *last_buf;
2242 struct xfs_buf *sib_buf;
2243 struct xfs_buf *par_buf;
2244 xfs_dahash_t dead_hash;
2245 xfs_fileoff_t lastoff;
2246 xfs_dablk_t dead_blkno;
2247 xfs_dablk_t last_blkno;
2248 xfs_dablk_t sib_blkno;
2249 xfs_dablk_t par_blkno;
2256 trace_xfs_da_swap_lastblock(args);
2258 dead_buf = *dead_bufp;
2259 dead_blkno = *dead_blknop;
2262 w = args->whichfork;
2263 ASSERT(w == XFS_DATA_FORK);
2265 lastoff = args->geo->freeblk;
2266 error = xfs_bmap_last_before(tp, dp, &lastoff, w);
2269 if (unlikely(lastoff == 0)) {
2270 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
2272 return -EFSCORRUPTED;
2275 * Read the last block in the btree space.
2277 last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
2278 error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w);
2282 * Copy the last block into the dead buffer and log it.
2284 memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
2285 xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
2286 dead_info = dead_buf->b_addr;
2288 * Get values from the moved block.
2290 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2291 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2292 struct xfs_dir3_icleaf_hdr leafhdr;
2293 struct xfs_dir2_leaf_entry *ents;
2295 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
2296 dp->d_ops->leaf_hdr_from_disk(&leafhdr, dead_leaf2);
2297 ents = dp->d_ops->leaf_ents_p(dead_leaf2);
2299 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
2301 struct xfs_da3_icnode_hdr deadhdr;
2303 dead_node = (xfs_da_intnode_t *)dead_info;
2304 xfs_da3_node_hdr_from_disk(dp->i_mount, &deadhdr, dead_node);
2305 btree = deadhdr.btree;
2306 dead_level = deadhdr.level;
2307 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
2309 sib_buf = par_buf = NULL;
2311 * If the moved block has a left sibling, fix up the pointers.
2313 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
2314 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
2317 sib_info = sib_buf->b_addr;
2319 be32_to_cpu(sib_info->forw) != last_blkno ||
2320 sib_info->magic != dead_info->magic)) {
2321 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
2322 XFS_ERRLEVEL_LOW, mp);
2323 error = -EFSCORRUPTED;
2326 sib_info->forw = cpu_to_be32(dead_blkno);
2327 xfs_trans_log_buf(tp, sib_buf,
2328 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2329 sizeof(sib_info->forw)));
2333 * If the moved block has a right sibling, fix up the pointers.
2335 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
2336 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
2339 sib_info = sib_buf->b_addr;
2341 be32_to_cpu(sib_info->back) != last_blkno ||
2342 sib_info->magic != dead_info->magic)) {
2343 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
2344 XFS_ERRLEVEL_LOW, mp);
2345 error = -EFSCORRUPTED;
2348 sib_info->back = cpu_to_be32(dead_blkno);
2349 xfs_trans_log_buf(tp, sib_buf,
2350 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2351 sizeof(sib_info->back)));
2354 par_blkno = args->geo->leafblk;
2357 * Walk down the tree looking for the parent of the moved block.
2360 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
2363 par_node = par_buf->b_addr;
2364 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2365 if (level >= 0 && level != par_hdr.level + 1) {
2366 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
2367 XFS_ERRLEVEL_LOW, mp);
2368 error = -EFSCORRUPTED;
2371 level = par_hdr.level;
2372 btree = par_hdr.btree;
2374 entno < par_hdr.count &&
2375 be32_to_cpu(btree[entno].hashval) < dead_hash;
2378 if (entno == par_hdr.count) {
2379 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
2380 XFS_ERRLEVEL_LOW, mp);
2381 error = -EFSCORRUPTED;
2384 par_blkno = be32_to_cpu(btree[entno].before);
2385 if (level == dead_level + 1)
2387 xfs_trans_brelse(tp, par_buf);
2391 * We're in the right parent block.
2392 * Look for the right entry.
2396 entno < par_hdr.count &&
2397 be32_to_cpu(btree[entno].before) != last_blkno;
2400 if (entno < par_hdr.count)
2402 par_blkno = par_hdr.forw;
2403 xfs_trans_brelse(tp, par_buf);
2405 if (unlikely(par_blkno == 0)) {
2406 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
2407 XFS_ERRLEVEL_LOW, mp);
2408 error = -EFSCORRUPTED;
2411 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
2414 par_node = par_buf->b_addr;
2415 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2416 if (par_hdr.level != level) {
2417 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
2418 XFS_ERRLEVEL_LOW, mp);
2419 error = -EFSCORRUPTED;
2422 btree = par_hdr.btree;
2426 * Update the parent entry pointing to the moved block.
2428 btree[entno].before = cpu_to_be32(dead_blkno);
2429 xfs_trans_log_buf(tp, par_buf,
2430 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2431 sizeof(btree[entno].before)));
2432 *dead_blknop = last_blkno;
2433 *dead_bufp = last_buf;
2437 xfs_trans_brelse(tp, par_buf);
2439 xfs_trans_brelse(tp, sib_buf);
2440 xfs_trans_brelse(tp, last_buf);
2445 * Remove a btree block from a directory or attribute.
2448 xfs_da_shrink_inode(
2449 struct xfs_da_args *args,
2450 xfs_dablk_t dead_blkno,
2451 struct xfs_buf *dead_buf)
2453 struct xfs_inode *dp;
2454 int done, error, w, count;
2455 struct xfs_trans *tp;
2457 trace_xfs_da_shrink_inode(args);
2460 w = args->whichfork;
2462 count = args->geo->fsbcount;
2465 * Remove extents. If we get ENOSPC for a dir we have to move
2466 * the last block to the place we want to kill.
2468 error = xfs_bunmapi(tp, dp, dead_blkno, count,
2469 xfs_bmapi_aflag(w), 0, &done);
2470 if (error == -ENOSPC) {
2471 if (w != XFS_DATA_FORK)
2473 error = xfs_da3_swap_lastblock(args, &dead_blkno,
2481 xfs_trans_binval(tp, dead_buf);
2486 * See if the mapping(s) for this btree block are valid, i.e.
2487 * don't contain holes, are logically contiguous, and cover the whole range.
2490 xfs_da_map_covers_blocks(
2492 xfs_bmbt_irec_t *mapp,
2499 for (i = 0, off = bno; i < nmap; i++) {
2500 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
2501 mapp[i].br_startblock == DELAYSTARTBLOCK) {
2504 if (off != mapp[i].br_startoff) {
2507 off += mapp[i].br_blockcount;
2509 return off == bno + count;
2513 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
2515 * For the single map case, it is assumed that the caller has provided a pointer
2516 * to a valid xfs_buf_map. For the multiple map case, this function will
2517 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
2518 * map pointer with the allocated map.
2521 xfs_buf_map_from_irec(
2522 struct xfs_mount *mp,
2523 struct xfs_buf_map **mapp,
2525 struct xfs_bmbt_irec *irecs,
2528 struct xfs_buf_map *map;
2531 ASSERT(*nmaps == 1);
2532 ASSERT(nirecs >= 1);
2535 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
2544 for (i = 0; i < *nmaps; i++) {
2545 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
2546 irecs[i].br_startblock != HOLESTARTBLOCK);
2547 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2548 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2554 * Map the block we are given ready for reading. There are three possible return
2556 * -1 - will be returned if we land in a hole and mappedbno == -2 so the
2557 * caller knows not to execute a subsequent read.
2558 * 0 - if we mapped the block successfully
2559 * >0 - positive error number if there was an error.
2563 struct xfs_inode *dp,
2565 xfs_daddr_t mappedbno,
2567 struct xfs_buf_map **map,
2570 struct xfs_mount *mp = dp->i_mount;
2573 struct xfs_bmbt_irec irec;
2574 struct xfs_bmbt_irec *irecs = &irec;
2577 ASSERT(map && *map);
2578 ASSERT(*nmaps == 1);
2580 if (whichfork == XFS_DATA_FORK)
2581 nfsb = mp->m_dir_geo->fsbcount;
2583 nfsb = mp->m_attr_geo->fsbcount;
2586 * Caller doesn't have a mapping. -2 means don't complain
2587 * if we land in a hole.
2589 if (mappedbno == -1 || mappedbno == -2) {
2591 * Optimize the one-block case.
2594 irecs = kmem_zalloc(sizeof(irec) * nfsb,
2598 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2599 &nirecs, xfs_bmapi_aflag(whichfork));
2603 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2604 irecs->br_startoff = (xfs_fileoff_t)bno;
2605 irecs->br_blockcount = nfsb;
2606 irecs->br_state = 0;
2610 if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
2611 /* Caller ok with no mapping. */
2612 if (mappedbno == -2) {
2617 /* Caller expected a mapping, so abort. */
2618 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2621 xfs_alert(mp, "%s: bno %lld dir: inode %lld", __func__,
2622 (long long)bno, (long long)dp->i_ino);
2623 for (i = 0; i < *nmaps; i++) {
2625 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
2627 (long long)irecs[i].br_startoff,
2628 (long long)irecs[i].br_startblock,
2629 (long long)irecs[i].br_blockcount,
2633 XFS_ERROR_REPORT("xfs_da_do_buf(1)", XFS_ERRLEVEL_LOW, mp);
2634 error = -EFSCORRUPTED;
2637 error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2645 * Get a buffer for the dir/attr block.
2649 struct xfs_trans *trans,
2650 struct xfs_inode *dp,
2652 xfs_daddr_t mappedbno,
2653 struct xfs_buf **bpp,
2657 struct xfs_buf_map map;
2658 struct xfs_buf_map *mapp;
2665 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2668 /* mapping a hole is not an error, but we don't continue */
2674 bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2676 error = bp ? bp->b_error : -EIO;
2679 xfs_trans_brelse(trans, bp);
2693 * Get a buffer for the dir/attr block, fill in the contents.
2697 struct xfs_trans *trans,
2698 struct xfs_inode *dp,
2700 xfs_daddr_t mappedbno,
2701 struct xfs_buf **bpp,
2703 const struct xfs_buf_ops *ops)
2706 struct xfs_buf_map map;
2707 struct xfs_buf_map *mapp;
2714 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2717 /* mapping a hole is not an error, but we don't continue */
2723 error = xfs_trans_read_buf_map(dp->i_mount, trans,
2724 dp->i_mount->m_ddev_targp,
2725 mapp, nmap, 0, &bp, ops);
2729 if (whichfork == XFS_ATTR_FORK)
2730 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
2732 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2742 * Readahead the dir/attr block.
2746 struct xfs_inode *dp,
2748 xfs_daddr_t mappedbno,
2750 const struct xfs_buf_ops *ops)
2752 struct xfs_buf_map map;
2753 struct xfs_buf_map *mapp;
2759 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2762 /* mapping a hole is not an error, but we don't continue */
2768 mappedbno = mapp[0].bm_bn;
2769 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);