1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
7 #include "libxfs_priv.h"
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_trace.h"
26 * Routines to implement directories as Btrees of hashed names.
29 /*========================================================================
30 * Function prototypes for the kernel.
31 *========================================================================*/
34 * Routines used for growing the Btree.
36 STATIC
int xfs_da3_root_split(xfs_da_state_t
*state
,
37 xfs_da_state_blk_t
*existing_root
,
38 xfs_da_state_blk_t
*new_child
);
39 STATIC
int xfs_da3_node_split(xfs_da_state_t
*state
,
40 xfs_da_state_blk_t
*existing_blk
,
41 xfs_da_state_blk_t
*split_blk
,
42 xfs_da_state_blk_t
*blk_to_add
,
45 STATIC
void xfs_da3_node_rebalance(xfs_da_state_t
*state
,
46 xfs_da_state_blk_t
*node_blk_1
,
47 xfs_da_state_blk_t
*node_blk_2
);
48 STATIC
void xfs_da3_node_add(xfs_da_state_t
*state
,
49 xfs_da_state_blk_t
*old_node_blk
,
50 xfs_da_state_blk_t
*new_node_blk
);
53 * Routines used for shrinking the Btree.
55 STATIC
int xfs_da3_root_join(xfs_da_state_t
*state
,
56 xfs_da_state_blk_t
*root_blk
);
57 STATIC
int xfs_da3_node_toosmall(xfs_da_state_t
*state
, int *retval
);
58 STATIC
void xfs_da3_node_remove(xfs_da_state_t
*state
,
59 xfs_da_state_blk_t
*drop_blk
);
60 STATIC
void xfs_da3_node_unbalance(xfs_da_state_t
*state
,
61 xfs_da_state_blk_t
*src_node_blk
,
62 xfs_da_state_blk_t
*dst_node_blk
);
67 STATIC
int xfs_da3_blk_unlink(xfs_da_state_t
*state
,
68 xfs_da_state_blk_t
*drop_blk
,
69 xfs_da_state_blk_t
*save_blk
);
72 kmem_zone_t
*xfs_da_state_zone
; /* anchor for state struct zone */
75 * Allocate a dir-state structure.
76 * We don't put them on the stack since they're large.
79 xfs_da_state_alloc(void)
81 return kmem_zone_zalloc(xfs_da_state_zone
, KM_NOFS
);
85 * Kill the altpath contents of a da-state structure.
88 xfs_da_state_kill_altpath(xfs_da_state_t
*state
)
92 for (i
= 0; i
< state
->altpath
.active
; i
++)
93 state
->altpath
.blk
[i
].bp
= NULL
;
94 state
->altpath
.active
= 0;
98 * Free a da-state structure.
101 xfs_da_state_free(xfs_da_state_t
*state
)
103 xfs_da_state_kill_altpath(state
);
105 memset((char *)state
, 0, sizeof(*state
));
107 kmem_cache_free(xfs_da_state_zone
, state
);
110 static inline int xfs_dabuf_nfsb(struct xfs_mount
*mp
, int whichfork
)
112 if (whichfork
== XFS_DATA_FORK
)
113 return mp
->m_dir_geo
->fsbcount
;
114 return mp
->m_attr_geo
->fsbcount
;
118 xfs_da3_node_hdr_from_disk(
119 struct xfs_mount
*mp
,
120 struct xfs_da3_icnode_hdr
*to
,
121 struct xfs_da_intnode
*from
)
123 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
124 struct xfs_da3_intnode
*from3
= (struct xfs_da3_intnode
*)from
;
126 to
->forw
= be32_to_cpu(from3
->hdr
.info
.hdr
.forw
);
127 to
->back
= be32_to_cpu(from3
->hdr
.info
.hdr
.back
);
128 to
->magic
= be16_to_cpu(from3
->hdr
.info
.hdr
.magic
);
129 to
->count
= be16_to_cpu(from3
->hdr
.__count
);
130 to
->level
= be16_to_cpu(from3
->hdr
.__level
);
131 to
->btree
= from3
->__btree
;
132 ASSERT(to
->magic
== XFS_DA3_NODE_MAGIC
);
134 to
->forw
= be32_to_cpu(from
->hdr
.info
.forw
);
135 to
->back
= be32_to_cpu(from
->hdr
.info
.back
);
136 to
->magic
= be16_to_cpu(from
->hdr
.info
.magic
);
137 to
->count
= be16_to_cpu(from
->hdr
.__count
);
138 to
->level
= be16_to_cpu(from
->hdr
.__level
);
139 to
->btree
= from
->__btree
;
140 ASSERT(to
->magic
== XFS_DA_NODE_MAGIC
);
145 xfs_da3_node_hdr_to_disk(
146 struct xfs_mount
*mp
,
147 struct xfs_da_intnode
*to
,
148 struct xfs_da3_icnode_hdr
*from
)
150 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
151 struct xfs_da3_intnode
*to3
= (struct xfs_da3_intnode
*)to
;
153 ASSERT(from
->magic
== XFS_DA3_NODE_MAGIC
);
154 to3
->hdr
.info
.hdr
.forw
= cpu_to_be32(from
->forw
);
155 to3
->hdr
.info
.hdr
.back
= cpu_to_be32(from
->back
);
156 to3
->hdr
.info
.hdr
.magic
= cpu_to_be16(from
->magic
);
157 to3
->hdr
.__count
= cpu_to_be16(from
->count
);
158 to3
->hdr
.__level
= cpu_to_be16(from
->level
);
160 ASSERT(from
->magic
== XFS_DA_NODE_MAGIC
);
161 to
->hdr
.info
.forw
= cpu_to_be32(from
->forw
);
162 to
->hdr
.info
.back
= cpu_to_be32(from
->back
);
163 to
->hdr
.info
.magic
= cpu_to_be16(from
->magic
);
164 to
->hdr
.__count
= cpu_to_be16(from
->count
);
165 to
->hdr
.__level
= cpu_to_be16(from
->level
);
170 * Verify an xfs_da3_blkinfo structure. Note that the da3 fields are only
171 * accessible on v5 filesystems. This header format is common across da node,
172 * attr leaf and dir leaf blocks.
175 xfs_da3_blkinfo_verify(
177 struct xfs_da3_blkinfo
*hdr3
)
179 struct xfs_mount
*mp
= bp
->b_mount
;
180 struct xfs_da_blkinfo
*hdr
= &hdr3
->hdr
;
182 if (!xfs_verify_magic16(bp
, hdr
->magic
))
183 return __this_address
;
185 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
186 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
187 return __this_address
;
188 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
189 return __this_address
;
190 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
191 return __this_address
;
197 static xfs_failaddr_t
201 struct xfs_mount
*mp
= bp
->b_mount
;
202 struct xfs_da_intnode
*hdr
= bp
->b_addr
;
203 struct xfs_da3_icnode_hdr ichdr
;
206 xfs_da3_node_hdr_from_disk(mp
, &ichdr
, hdr
);
208 fa
= xfs_da3_blkinfo_verify(bp
, bp
->b_addr
);
212 if (ichdr
.level
== 0)
213 return __this_address
;
214 if (ichdr
.level
> XFS_DA_NODE_MAXDEPTH
)
215 return __this_address
;
216 if (ichdr
.count
== 0)
217 return __this_address
;
220 * we don't know if the node is for and attribute or directory tree,
221 * so only fail if the count is outside both bounds
223 if (ichdr
.count
> mp
->m_dir_geo
->node_ents
&&
224 ichdr
.count
> mp
->m_attr_geo
->node_ents
)
225 return __this_address
;
227 /* XXX: hash order check? */
233 xfs_da3_node_write_verify(
236 struct xfs_mount
*mp
= bp
->b_mount
;
237 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
238 struct xfs_da3_node_hdr
*hdr3
= bp
->b_addr
;
241 fa
= xfs_da3_node_verify(bp
);
243 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
247 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
251 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
253 xfs_buf_update_cksum(bp
, XFS_DA3_NODE_CRC_OFF
);
257 * leaf/node format detection on trees is sketchy, so a node read can be done on
258 * leaf level blocks when detection identifies the tree as a node format tree
259 * incorrectly. In this case, we need to swap the verifier to match the correct
260 * format of the block being read.
263 xfs_da3_node_read_verify(
266 struct xfs_da_blkinfo
*info
= bp
->b_addr
;
269 switch (be16_to_cpu(info
->magic
)) {
270 case XFS_DA3_NODE_MAGIC
:
271 if (!xfs_buf_verify_cksum(bp
, XFS_DA3_NODE_CRC_OFF
)) {
272 xfs_verifier_error(bp
, -EFSBADCRC
,
277 case XFS_DA_NODE_MAGIC
:
278 fa
= xfs_da3_node_verify(bp
);
280 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
282 case XFS_ATTR_LEAF_MAGIC
:
283 case XFS_ATTR3_LEAF_MAGIC
:
284 bp
->b_ops
= &xfs_attr3_leaf_buf_ops
;
285 bp
->b_ops
->verify_read(bp
);
287 case XFS_DIR2_LEAFN_MAGIC
:
288 case XFS_DIR3_LEAFN_MAGIC
:
289 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
290 bp
->b_ops
->verify_read(bp
);
293 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
298 /* Verify the structure of a da3 block. */
299 static xfs_failaddr_t
300 xfs_da3_node_verify_struct(
303 struct xfs_da_blkinfo
*info
= bp
->b_addr
;
305 switch (be16_to_cpu(info
->magic
)) {
306 case XFS_DA3_NODE_MAGIC
:
307 case XFS_DA_NODE_MAGIC
:
308 return xfs_da3_node_verify(bp
);
309 case XFS_ATTR_LEAF_MAGIC
:
310 case XFS_ATTR3_LEAF_MAGIC
:
311 bp
->b_ops
= &xfs_attr3_leaf_buf_ops
;
312 return bp
->b_ops
->verify_struct(bp
);
313 case XFS_DIR2_LEAFN_MAGIC
:
314 case XFS_DIR3_LEAFN_MAGIC
:
315 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
316 return bp
->b_ops
->verify_struct(bp
);
318 return __this_address
;
322 const struct xfs_buf_ops xfs_da3_node_buf_ops
= {
323 .name
= "xfs_da3_node",
324 .magic16
= { cpu_to_be16(XFS_DA_NODE_MAGIC
),
325 cpu_to_be16(XFS_DA3_NODE_MAGIC
) },
326 .verify_read
= xfs_da3_node_read_verify
,
327 .verify_write
= xfs_da3_node_write_verify
,
328 .verify_struct
= xfs_da3_node_verify_struct
,
332 xfs_da3_node_set_type(
333 struct xfs_trans
*tp
,
336 struct xfs_da_blkinfo
*info
= bp
->b_addr
;
338 switch (be16_to_cpu(info
->magic
)) {
339 case XFS_DA_NODE_MAGIC
:
340 case XFS_DA3_NODE_MAGIC
:
341 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DA_NODE_BUF
);
343 case XFS_ATTR_LEAF_MAGIC
:
344 case XFS_ATTR3_LEAF_MAGIC
:
345 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_ATTR_LEAF_BUF
);
347 case XFS_DIR2_LEAFN_MAGIC
:
348 case XFS_DIR3_LEAFN_MAGIC
:
349 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAFN_BUF
);
352 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, tp
->t_mountp
,
353 info
, sizeof(*info
));
354 xfs_trans_brelse(tp
, bp
);
355 return -EFSCORRUPTED
;
361 struct xfs_trans
*tp
,
362 struct xfs_inode
*dp
,
364 struct xfs_buf
**bpp
,
369 error
= xfs_da_read_buf(tp
, dp
, bno
, 0, bpp
, whichfork
,
370 &xfs_da3_node_buf_ops
);
371 if (error
|| !*bpp
|| !tp
)
373 return xfs_da3_node_set_type(tp
, *bpp
);
377 xfs_da3_node_read_mapped(
378 struct xfs_trans
*tp
,
379 struct xfs_inode
*dp
,
380 xfs_daddr_t mappedbno
,
381 struct xfs_buf
**bpp
,
384 struct xfs_mount
*mp
= dp
->i_mount
;
387 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, mappedbno
,
388 XFS_FSB_TO_BB(mp
, xfs_dabuf_nfsb(mp
, whichfork
)), 0,
389 bpp
, &xfs_da3_node_buf_ops
);
393 if (whichfork
== XFS_ATTR_FORK
)
394 xfs_buf_set_ref(*bpp
, XFS_ATTR_BTREE_REF
);
396 xfs_buf_set_ref(*bpp
, XFS_DIR_BTREE_REF
);
400 return xfs_da3_node_set_type(tp
, *bpp
);
403 /*========================================================================
404 * Routines used for growing the Btree.
405 *========================================================================*/
408 * Create the initial contents of an intermediate node.
412 struct xfs_da_args
*args
,
415 struct xfs_buf
**bpp
,
418 struct xfs_da_intnode
*node
;
419 struct xfs_trans
*tp
= args
->trans
;
420 struct xfs_mount
*mp
= tp
->t_mountp
;
421 struct xfs_da3_icnode_hdr ichdr
= {0};
424 struct xfs_inode
*dp
= args
->dp
;
426 trace_xfs_da_node_create(args
);
427 ASSERT(level
<= XFS_DA_NODE_MAXDEPTH
);
429 error
= xfs_da_get_buf(tp
, dp
, blkno
, &bp
, whichfork
);
432 bp
->b_ops
= &xfs_da3_node_buf_ops
;
433 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DA_NODE_BUF
);
436 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
437 struct xfs_da3_node_hdr
*hdr3
= bp
->b_addr
;
439 memset(hdr3
, 0, sizeof(struct xfs_da3_node_hdr
));
440 ichdr
.magic
= XFS_DA3_NODE_MAGIC
;
441 hdr3
->info
.blkno
= cpu_to_be64(bp
->b_bn
);
442 hdr3
->info
.owner
= cpu_to_be64(args
->dp
->i_ino
);
443 uuid_copy(&hdr3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
445 ichdr
.magic
= XFS_DA_NODE_MAGIC
;
449 xfs_da3_node_hdr_to_disk(dp
->i_mount
, node
, &ichdr
);
450 xfs_trans_log_buf(tp
, bp
,
451 XFS_DA_LOGRANGE(node
, &node
->hdr
, args
->geo
->node_hdr_size
));
458 * Split a leaf node, rebalance, then possibly split
459 * intermediate nodes, rebalance, etc.
463 struct xfs_da_state
*state
)
465 struct xfs_da_state_blk
*oldblk
;
466 struct xfs_da_state_blk
*newblk
;
467 struct xfs_da_state_blk
*addblk
;
468 struct xfs_da_intnode
*node
;
474 trace_xfs_da_split(state
->args
);
477 * Walk back up the tree splitting/inserting/adjusting as necessary.
478 * If we need to insert and there isn't room, split the node, then
479 * decide which fragment to insert the new block from below into.
480 * Note that we may split the root this way, but we need more fixup.
482 max
= state
->path
.active
- 1;
483 ASSERT((max
>= 0) && (max
< XFS_DA_NODE_MAXDEPTH
));
484 ASSERT(state
->path
.blk
[max
].magic
== XFS_ATTR_LEAF_MAGIC
||
485 state
->path
.blk
[max
].magic
== XFS_DIR2_LEAFN_MAGIC
);
487 addblk
= &state
->path
.blk
[max
]; /* initial dummy value */
488 for (i
= max
; (i
>= 0) && addblk
; state
->path
.active
--, i
--) {
489 oldblk
= &state
->path
.blk
[i
];
490 newblk
= &state
->altpath
.blk
[i
];
493 * If a leaf node then
494 * Allocate a new leaf node, then rebalance across them.
495 * else if an intermediate node then
496 * We split on the last layer, must we split the node?
498 switch (oldblk
->magic
) {
499 case XFS_ATTR_LEAF_MAGIC
:
500 error
= xfs_attr3_leaf_split(state
, oldblk
, newblk
);
501 if ((error
!= 0) && (error
!= -ENOSPC
)) {
502 return error
; /* GROT: attr is inconsistent */
509 * Entry wouldn't fit, split the leaf again. The new
510 * extrablk will be consumed by xfs_da3_node_split if
513 state
->extravalid
= 1;
515 state
->extraafter
= 0; /* before newblk */
516 trace_xfs_attr_leaf_split_before(state
->args
);
517 error
= xfs_attr3_leaf_split(state
, oldblk
,
520 state
->extraafter
= 1; /* after newblk */
521 trace_xfs_attr_leaf_split_after(state
->args
);
522 error
= xfs_attr3_leaf_split(state
, newblk
,
526 return error
; /* GROT: attr inconsistent */
529 case XFS_DIR2_LEAFN_MAGIC
:
530 error
= xfs_dir2_leafn_split(state
, oldblk
, newblk
);
535 case XFS_DA_NODE_MAGIC
:
536 error
= xfs_da3_node_split(state
, oldblk
, newblk
, addblk
,
540 return error
; /* GROT: dir is inconsistent */
542 * Record the newly split block for the next time thru?
552 * Update the btree to show the new hashval for this child.
554 xfs_da3_fixhashpath(state
, &state
->path
);
560 * xfs_da3_node_split() should have consumed any extra blocks we added
561 * during a double leaf split in the attr fork. This is guaranteed as
562 * we can't be here if the attr fork only has a single leaf block.
564 ASSERT(state
->extravalid
== 0 ||
565 state
->path
.blk
[max
].magic
== XFS_DIR2_LEAFN_MAGIC
);
568 * Split the root node.
570 ASSERT(state
->path
.active
== 0);
571 oldblk
= &state
->path
.blk
[0];
572 error
= xfs_da3_root_split(state
, oldblk
, addblk
);
577 * Update pointers to the node which used to be block 0 and just got
578 * bumped because of the addition of a new root node. Note that the
579 * original block 0 could be at any position in the list of blocks in
582 * Note: the magic numbers and sibling pointers are in the same physical
583 * place for both v2 and v3 headers (by design). Hence it doesn't matter
584 * which version of the xfs_da_intnode structure we use here as the
585 * result will be the same using either structure.
587 node
= oldblk
->bp
->b_addr
;
588 if (node
->hdr
.info
.forw
) {
589 if (be32_to_cpu(node
->hdr
.info
.forw
) != addblk
->blkno
) {
590 xfs_buf_corruption_error(oldblk
->bp
);
591 error
= -EFSCORRUPTED
;
594 node
= addblk
->bp
->b_addr
;
595 node
->hdr
.info
.back
= cpu_to_be32(oldblk
->blkno
);
596 xfs_trans_log_buf(state
->args
->trans
, addblk
->bp
,
597 XFS_DA_LOGRANGE(node
, &node
->hdr
.info
,
598 sizeof(node
->hdr
.info
)));
600 node
= oldblk
->bp
->b_addr
;
601 if (node
->hdr
.info
.back
) {
602 if (be32_to_cpu(node
->hdr
.info
.back
) != addblk
->blkno
) {
603 xfs_buf_corruption_error(oldblk
->bp
);
604 error
= -EFSCORRUPTED
;
607 node
= addblk
->bp
->b_addr
;
608 node
->hdr
.info
.forw
= cpu_to_be32(oldblk
->blkno
);
609 xfs_trans_log_buf(state
->args
->trans
, addblk
->bp
,
610 XFS_DA_LOGRANGE(node
, &node
->hdr
.info
,
611 sizeof(node
->hdr
.info
)));
619 * Split the root. We have to create a new root and point to the two
620 * parts (the split old root) that we just created. Copy block zero to
621 * the EOF, extending the inode in process.
623 STATIC
int /* error */
625 struct xfs_da_state
*state
,
626 struct xfs_da_state_blk
*blk1
,
627 struct xfs_da_state_blk
*blk2
)
629 struct xfs_da_intnode
*node
;
630 struct xfs_da_intnode
*oldroot
;
631 struct xfs_da_node_entry
*btree
;
632 struct xfs_da3_icnode_hdr nodehdr
;
633 struct xfs_da_args
*args
;
635 struct xfs_inode
*dp
;
636 struct xfs_trans
*tp
;
637 struct xfs_dir2_leaf
*leaf
;
643 trace_xfs_da_root_split(state
->args
);
646 * Copy the existing (incorrect) block from the root node position
647 * to a free space somewhere.
650 error
= xfs_da_grow_inode(args
, &blkno
);
656 error
= xfs_da_get_buf(tp
, dp
, blkno
, &bp
, args
->whichfork
);
660 oldroot
= blk1
->bp
->b_addr
;
661 if (oldroot
->hdr
.info
.magic
== cpu_to_be16(XFS_DA_NODE_MAGIC
) ||
662 oldroot
->hdr
.info
.magic
== cpu_to_be16(XFS_DA3_NODE_MAGIC
)) {
663 struct xfs_da3_icnode_hdr icnodehdr
;
665 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &icnodehdr
, oldroot
);
666 btree
= icnodehdr
.btree
;
667 size
= (int)((char *)&btree
[icnodehdr
.count
] - (char *)oldroot
);
668 level
= icnodehdr
.level
;
671 * we are about to copy oldroot to bp, so set up the type
672 * of bp while we know exactly what it will be.
674 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DA_NODE_BUF
);
676 struct xfs_dir3_icleaf_hdr leafhdr
;
678 leaf
= (xfs_dir2_leaf_t
*)oldroot
;
679 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
, leaf
);
681 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
682 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
);
683 size
= (int)((char *)&leafhdr
.ents
[leafhdr
.count
] -
688 * we are about to copy oldroot to bp, so set up the type
689 * of bp while we know exactly what it will be.
691 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_LEAFN_BUF
);
695 * we can copy most of the information in the node from one block to
696 * another, but for CRC enabled headers we have to make sure that the
697 * block specific identifiers are kept intact. We update the buffer
700 memcpy(node
, oldroot
, size
);
701 if (oldroot
->hdr
.info
.magic
== cpu_to_be16(XFS_DA3_NODE_MAGIC
) ||
702 oldroot
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
)) {
703 struct xfs_da3_intnode
*node3
= (struct xfs_da3_intnode
*)node
;
705 node3
->hdr
.info
.blkno
= cpu_to_be64(bp
->b_bn
);
707 xfs_trans_log_buf(tp
, bp
, 0, size
- 1);
709 bp
->b_ops
= blk1
->bp
->b_ops
;
710 xfs_trans_buf_copy_type(bp
, blk1
->bp
);
715 * Set up the new root node.
717 error
= xfs_da3_node_create(args
,
718 (args
->whichfork
== XFS_DATA_FORK
) ? args
->geo
->leafblk
: 0,
719 level
+ 1, &bp
, args
->whichfork
);
724 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, node
);
725 btree
= nodehdr
.btree
;
726 btree
[0].hashval
= cpu_to_be32(blk1
->hashval
);
727 btree
[0].before
= cpu_to_be32(blk1
->blkno
);
728 btree
[1].hashval
= cpu_to_be32(blk2
->hashval
);
729 btree
[1].before
= cpu_to_be32(blk2
->blkno
);
731 xfs_da3_node_hdr_to_disk(dp
->i_mount
, node
, &nodehdr
);
734 if (oldroot
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
735 oldroot
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
)) {
736 ASSERT(blk1
->blkno
>= args
->geo
->leafblk
&&
737 blk1
->blkno
< args
->geo
->freeblk
);
738 ASSERT(blk2
->blkno
>= args
->geo
->leafblk
&&
739 blk2
->blkno
< args
->geo
->freeblk
);
743 /* Header is already logged by xfs_da_node_create */
744 xfs_trans_log_buf(tp
, bp
,
745 XFS_DA_LOGRANGE(node
, btree
, sizeof(xfs_da_node_entry_t
) * 2));
751 * Split the node, rebalance, then add the new entry.
753 STATIC
int /* error */
755 struct xfs_da_state
*state
,
756 struct xfs_da_state_blk
*oldblk
,
757 struct xfs_da_state_blk
*newblk
,
758 struct xfs_da_state_blk
*addblk
,
762 struct xfs_da_intnode
*node
;
763 struct xfs_da3_icnode_hdr nodehdr
;
768 struct xfs_inode
*dp
= state
->args
->dp
;
770 trace_xfs_da_node_split(state
->args
);
772 node
= oldblk
->bp
->b_addr
;
773 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, node
);
776 * With V2 dirs the extra block is data or freespace.
778 useextra
= state
->extravalid
&& state
->args
->whichfork
== XFS_ATTR_FORK
;
779 newcount
= 1 + useextra
;
781 * Do we have to split the node?
783 if (nodehdr
.count
+ newcount
> state
->args
->geo
->node_ents
) {
785 * Allocate a new node, add to the doubly linked chain of
786 * nodes, then move some of our excess entries into it.
788 error
= xfs_da_grow_inode(state
->args
, &blkno
);
790 return error
; /* GROT: dir is inconsistent */
792 error
= xfs_da3_node_create(state
->args
, blkno
, treelevel
,
793 &newblk
->bp
, state
->args
->whichfork
);
795 return error
; /* GROT: dir is inconsistent */
796 newblk
->blkno
= blkno
;
797 newblk
->magic
= XFS_DA_NODE_MAGIC
;
798 xfs_da3_node_rebalance(state
, oldblk
, newblk
);
799 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
808 * Insert the new entry(s) into the correct block
809 * (updating last hashval in the process).
811 * xfs_da3_node_add() inserts BEFORE the given index,
812 * and as a result of using node_lookup_int() we always
813 * point to a valid entry (not after one), but a split
814 * operation always results in a new block whose hashvals
815 * FOLLOW the current block.
817 * If we had double-split op below us, then add the extra block too.
819 node
= oldblk
->bp
->b_addr
;
820 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, node
);
821 if (oldblk
->index
<= nodehdr
.count
) {
823 xfs_da3_node_add(state
, oldblk
, addblk
);
825 if (state
->extraafter
)
827 xfs_da3_node_add(state
, oldblk
, &state
->extrablk
);
828 state
->extravalid
= 0;
832 xfs_da3_node_add(state
, newblk
, addblk
);
834 if (state
->extraafter
)
836 xfs_da3_node_add(state
, newblk
, &state
->extrablk
);
837 state
->extravalid
= 0;
845 * Balance the btree elements between two intermediate nodes,
846 * usually one full and one empty.
848 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
851 xfs_da3_node_rebalance(
852 struct xfs_da_state
*state
,
853 struct xfs_da_state_blk
*blk1
,
854 struct xfs_da_state_blk
*blk2
)
856 struct xfs_da_intnode
*node1
;
857 struct xfs_da_intnode
*node2
;
858 struct xfs_da_intnode
*tmpnode
;
859 struct xfs_da_node_entry
*btree1
;
860 struct xfs_da_node_entry
*btree2
;
861 struct xfs_da_node_entry
*btree_s
;
862 struct xfs_da_node_entry
*btree_d
;
863 struct xfs_da3_icnode_hdr nodehdr1
;
864 struct xfs_da3_icnode_hdr nodehdr2
;
865 struct xfs_trans
*tp
;
869 struct xfs_inode
*dp
= state
->args
->dp
;
871 trace_xfs_da_node_rebalance(state
->args
);
873 node1
= blk1
->bp
->b_addr
;
874 node2
= blk2
->bp
->b_addr
;
875 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr1
, node1
);
876 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr2
, node2
);
877 btree1
= nodehdr1
.btree
;
878 btree2
= nodehdr2
.btree
;
881 * Figure out how many entries need to move, and in which direction.
882 * Swap the nodes around if that makes it simpler.
884 if (nodehdr1
.count
> 0 && nodehdr2
.count
> 0 &&
885 ((be32_to_cpu(btree2
[0].hashval
) < be32_to_cpu(btree1
[0].hashval
)) ||
886 (be32_to_cpu(btree2
[nodehdr2
.count
- 1].hashval
) <
887 be32_to_cpu(btree1
[nodehdr1
.count
- 1].hashval
)))) {
891 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr1
, node1
);
892 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr2
, node2
);
893 btree1
= nodehdr1
.btree
;
894 btree2
= nodehdr2
.btree
;
898 count
= (nodehdr1
.count
- nodehdr2
.count
) / 2;
901 tp
= state
->args
->trans
;
903 * Two cases: high-to-low and low-to-high.
907 * Move elements in node2 up to make a hole.
909 tmp
= nodehdr2
.count
;
911 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
912 btree_s
= &btree2
[0];
913 btree_d
= &btree2
[count
];
914 memmove(btree_d
, btree_s
, tmp
);
918 * Move the req'd B-tree elements from high in node1 to
921 nodehdr2
.count
+= count
;
922 tmp
= count
* (uint
)sizeof(xfs_da_node_entry_t
);
923 btree_s
= &btree1
[nodehdr1
.count
- count
];
924 btree_d
= &btree2
[0];
925 memcpy(btree_d
, btree_s
, tmp
);
926 nodehdr1
.count
-= count
;
929 * Move the req'd B-tree elements from low in node2 to
933 tmp
= count
* (uint
)sizeof(xfs_da_node_entry_t
);
934 btree_s
= &btree2
[0];
935 btree_d
= &btree1
[nodehdr1
.count
];
936 memcpy(btree_d
, btree_s
, tmp
);
937 nodehdr1
.count
+= count
;
939 xfs_trans_log_buf(tp
, blk1
->bp
,
940 XFS_DA_LOGRANGE(node1
, btree_d
, tmp
));
943 * Move elements in node2 down to fill the hole.
945 tmp
= nodehdr2
.count
- count
;
946 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
947 btree_s
= &btree2
[count
];
948 btree_d
= &btree2
[0];
949 memmove(btree_d
, btree_s
, tmp
);
950 nodehdr2
.count
-= count
;
954 * Log header of node 1 and all current bits of node 2.
956 xfs_da3_node_hdr_to_disk(dp
->i_mount
, node1
, &nodehdr1
);
957 xfs_trans_log_buf(tp
, blk1
->bp
,
958 XFS_DA_LOGRANGE(node1
, &node1
->hdr
,
959 state
->args
->geo
->node_hdr_size
));
961 xfs_da3_node_hdr_to_disk(dp
->i_mount
, node2
, &nodehdr2
);
962 xfs_trans_log_buf(tp
, blk2
->bp
,
963 XFS_DA_LOGRANGE(node2
, &node2
->hdr
,
964 state
->args
->geo
->node_hdr_size
+
965 (sizeof(btree2
[0]) * nodehdr2
.count
)));
968 * Record the last hashval from each block for upward propagation.
969 * (note: don't use the swapped node pointers)
972 node1
= blk1
->bp
->b_addr
;
973 node2
= blk2
->bp
->b_addr
;
974 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr1
, node1
);
975 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr2
, node2
);
976 btree1
= nodehdr1
.btree
;
977 btree2
= nodehdr2
.btree
;
979 blk1
->hashval
= be32_to_cpu(btree1
[nodehdr1
.count
- 1].hashval
);
980 blk2
->hashval
= be32_to_cpu(btree2
[nodehdr2
.count
- 1].hashval
);
983 * Adjust the expected index for insertion.
985 if (blk1
->index
>= nodehdr1
.count
) {
986 blk2
->index
= blk1
->index
- nodehdr1
.count
;
987 blk1
->index
= nodehdr1
.count
+ 1; /* make it invalid */
992 * Add a new entry to an intermediate node.
996 struct xfs_da_state
*state
,
997 struct xfs_da_state_blk
*oldblk
,
998 struct xfs_da_state_blk
*newblk
)
1000 struct xfs_da_intnode
*node
;
1001 struct xfs_da3_icnode_hdr nodehdr
;
1002 struct xfs_da_node_entry
*btree
;
1004 struct xfs_inode
*dp
= state
->args
->dp
;
1006 trace_xfs_da_node_add(state
->args
);
1008 node
= oldblk
->bp
->b_addr
;
1009 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, node
);
1010 btree
= nodehdr
.btree
;
1012 ASSERT(oldblk
->index
>= 0 && oldblk
->index
<= nodehdr
.count
);
1013 ASSERT(newblk
->blkno
!= 0);
1014 if (state
->args
->whichfork
== XFS_DATA_FORK
)
1015 ASSERT(newblk
->blkno
>= state
->args
->geo
->leafblk
&&
1016 newblk
->blkno
< state
->args
->geo
->freeblk
);
1019 * We may need to make some room before we insert the new node.
1022 if (oldblk
->index
< nodehdr
.count
) {
1023 tmp
= (nodehdr
.count
- oldblk
->index
) * (uint
)sizeof(*btree
);
1024 memmove(&btree
[oldblk
->index
+ 1], &btree
[oldblk
->index
], tmp
);
1026 btree
[oldblk
->index
].hashval
= cpu_to_be32(newblk
->hashval
);
1027 btree
[oldblk
->index
].before
= cpu_to_be32(newblk
->blkno
);
1028 xfs_trans_log_buf(state
->args
->trans
, oldblk
->bp
,
1029 XFS_DA_LOGRANGE(node
, &btree
[oldblk
->index
],
1030 tmp
+ sizeof(*btree
)));
1033 xfs_da3_node_hdr_to_disk(dp
->i_mount
, node
, &nodehdr
);
1034 xfs_trans_log_buf(state
->args
->trans
, oldblk
->bp
,
1035 XFS_DA_LOGRANGE(node
, &node
->hdr
,
1036 state
->args
->geo
->node_hdr_size
));
1039 * Copy the last hash value from the oldblk to propagate upwards.
1041 oldblk
->hashval
= be32_to_cpu(btree
[nodehdr
.count
- 1].hashval
);
1044 /*========================================================================
1045 * Routines used for shrinking the Btree.
1046 *========================================================================*/
1049 * Deallocate an empty leaf node, remove it from its parent,
1050 * possibly deallocating that block, etc...
1054 struct xfs_da_state
*state
)
1056 struct xfs_da_state_blk
*drop_blk
;
1057 struct xfs_da_state_blk
*save_blk
;
1061 trace_xfs_da_join(state
->args
);
1063 drop_blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1064 save_blk
= &state
->altpath
.blk
[ state
->path
.active
-1 ];
1065 ASSERT(state
->path
.blk
[0].magic
== XFS_DA_NODE_MAGIC
);
1066 ASSERT(drop_blk
->magic
== XFS_ATTR_LEAF_MAGIC
||
1067 drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1070 * Walk back up the tree joining/deallocating as necessary.
1071 * When we stop dropping blocks, break out.
1073 for ( ; state
->path
.active
>= 2; drop_blk
--, save_blk
--,
1074 state
->path
.active
--) {
1076 * See if we can combine the block with a neighbor.
1077 * (action == 0) => no options, just leave
1078 * (action == 1) => coalesce, then unlink
1079 * (action == 2) => block empty, unlink it
1081 switch (drop_blk
->magic
) {
1082 case XFS_ATTR_LEAF_MAGIC
:
1083 error
= xfs_attr3_leaf_toosmall(state
, &action
);
1088 xfs_attr3_leaf_unbalance(state
, drop_blk
, save_blk
);
1090 case XFS_DIR2_LEAFN_MAGIC
:
1091 error
= xfs_dir2_leafn_toosmall(state
, &action
);
1096 xfs_dir2_leafn_unbalance(state
, drop_blk
, save_blk
);
1098 case XFS_DA_NODE_MAGIC
:
1100 * Remove the offending node, fixup hashvals,
1101 * check for a toosmall neighbor.
1103 xfs_da3_node_remove(state
, drop_blk
);
1104 xfs_da3_fixhashpath(state
, &state
->path
);
1105 error
= xfs_da3_node_toosmall(state
, &action
);
1110 xfs_da3_node_unbalance(state
, drop_blk
, save_blk
);
1113 xfs_da3_fixhashpath(state
, &state
->altpath
);
1114 error
= xfs_da3_blk_unlink(state
, drop_blk
, save_blk
);
1115 xfs_da_state_kill_altpath(state
);
1118 error
= xfs_da_shrink_inode(state
->args
, drop_blk
->blkno
,
1120 drop_blk
->bp
= NULL
;
1125 * We joined all the way to the top. If it turns out that
1126 * we only have one entry in the root, make the child block
1129 xfs_da3_node_remove(state
, drop_blk
);
1130 xfs_da3_fixhashpath(state
, &state
->path
);
1131 error
= xfs_da3_root_join(state
, &state
->path
.blk
[0]);
1137 xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo
*blkinfo
, __u16 level
)
1139 __be16 magic
= blkinfo
->magic
;
1142 ASSERT(magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
1143 magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
) ||
1144 magic
== cpu_to_be16(XFS_ATTR_LEAF_MAGIC
) ||
1145 magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
));
1147 ASSERT(magic
== cpu_to_be16(XFS_DA_NODE_MAGIC
) ||
1148 magic
== cpu_to_be16(XFS_DA3_NODE_MAGIC
));
1150 ASSERT(!blkinfo
->forw
);
1151 ASSERT(!blkinfo
->back
);
1154 #define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1158 * We have only one entry in the root. Copy the only remaining child of
1159 * the old root to block 0 as the new root node.
1163 struct xfs_da_state
*state
,
1164 struct xfs_da_state_blk
*root_blk
)
1166 struct xfs_da_intnode
*oldroot
;
1167 struct xfs_da_args
*args
;
1170 struct xfs_da3_icnode_hdr oldroothdr
;
1172 struct xfs_inode
*dp
= state
->args
->dp
;
1174 trace_xfs_da_root_join(state
->args
);
1176 ASSERT(root_blk
->magic
== XFS_DA_NODE_MAGIC
);
1179 oldroot
= root_blk
->bp
->b_addr
;
1180 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &oldroothdr
, oldroot
);
1181 ASSERT(oldroothdr
.forw
== 0);
1182 ASSERT(oldroothdr
.back
== 0);
1185 * If the root has more than one child, then don't do anything.
1187 if (oldroothdr
.count
> 1)
1191 * Read in the (only) child block, then copy those bytes into
1192 * the root block's buffer and free the original child block.
1194 child
= be32_to_cpu(oldroothdr
.btree
[0].before
);
1196 error
= xfs_da3_node_read(args
->trans
, dp
, child
, &bp
, args
->whichfork
);
1199 xfs_da_blkinfo_onlychild_validate(bp
->b_addr
, oldroothdr
.level
);
1202 * This could be copying a leaf back into the root block in the case of
1203 * there only being a single leaf block left in the tree. Hence we have
1204 * to update the b_ops pointer as well to match the buffer type change
1205 * that could occur. For dir3 blocks we also need to update the block
1206 * number in the buffer header.
1208 memcpy(root_blk
->bp
->b_addr
, bp
->b_addr
, args
->geo
->blksize
);
1209 root_blk
->bp
->b_ops
= bp
->b_ops
;
1210 xfs_trans_buf_copy_type(root_blk
->bp
, bp
);
1211 if (oldroothdr
.magic
== XFS_DA3_NODE_MAGIC
) {
1212 struct xfs_da3_blkinfo
*da3
= root_blk
->bp
->b_addr
;
1213 da3
->blkno
= cpu_to_be64(root_blk
->bp
->b_bn
);
1215 xfs_trans_log_buf(args
->trans
, root_blk
->bp
, 0,
1216 args
->geo
->blksize
- 1);
1217 error
= xfs_da_shrink_inode(args
, child
, bp
);
1222 * Check a node block and its neighbors to see if the block should be
1223 * collapsed into one or the other neighbor. Always keep the block
1224 * with the smaller block number.
1225 * If the current block is over 50% full, don't try to join it, return 0.
1226 * If the block is empty, fill in the state structure and return 2.
1227 * If it can be collapsed, fill in the state structure and return 1.
1228 * If nothing can be done, return 0.
1231 xfs_da3_node_toosmall(
1232 struct xfs_da_state
*state
,
1235 struct xfs_da_intnode
*node
;
1236 struct xfs_da_state_blk
*blk
;
1237 struct xfs_da_blkinfo
*info
;
1240 struct xfs_da3_icnode_hdr nodehdr
;
1246 struct xfs_inode
*dp
= state
->args
->dp
;
1248 trace_xfs_da_node_toosmall(state
->args
);
1251 * Check for the degenerate case of the block being over 50% full.
1252 * If so, it's not worth even looking to see if we might be able
1253 * to coalesce with a sibling.
1255 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1256 info
= blk
->bp
->b_addr
;
1257 node
= (xfs_da_intnode_t
*)info
;
1258 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, node
);
1259 if (nodehdr
.count
> (state
->args
->geo
->node_ents
>> 1)) {
1260 *action
= 0; /* blk over 50%, don't try to join */
1261 return 0; /* blk over 50%, don't try to join */
1265 * Check for the degenerate case of the block being empty.
1266 * If the block is empty, we'll simply delete it, no need to
1267 * coalesce it with a sibling block. We choose (arbitrarily)
1268 * to merge with the forward block unless it is NULL.
1270 if (nodehdr
.count
== 0) {
1272 * Make altpath point to the block we want to keep and
1273 * path point to the block we want to drop (this one).
1275 forward
= (info
->forw
!= 0);
1276 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1277 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1290 * Examine each sibling block to see if we can coalesce with
1291 * at least 25% free space to spare. We need to figure out
1292 * whether to merge with the forward or the backward block.
1293 * We prefer coalescing with the lower numbered sibling so as
1294 * to shrink a directory over time.
1296 count
= state
->args
->geo
->node_ents
;
1297 count
-= state
->args
->geo
->node_ents
>> 2;
1298 count
-= nodehdr
.count
;
1300 /* start with smaller blk num */
1301 forward
= nodehdr
.forw
< nodehdr
.back
;
1302 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
1303 struct xfs_da3_icnode_hdr thdr
;
1305 blkno
= nodehdr
.forw
;
1307 blkno
= nodehdr
.back
;
1310 error
= xfs_da3_node_read(state
->args
->trans
, dp
, blkno
, &bp
,
1311 state
->args
->whichfork
);
1316 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &thdr
, node
);
1317 xfs_trans_brelse(state
->args
->trans
, bp
);
1319 if (count
- thdr
.count
>= 0)
1320 break; /* fits with at least 25% to spare */
1328 * Make altpath point to the block we want to keep (the lower
1329 * numbered block) and path point to the block we want to drop.
1331 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1332 if (blkno
< blk
->blkno
) {
1333 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1336 error
= xfs_da3_path_shift(state
, &state
->path
, forward
,
1350 * Pick up the last hashvalue from an intermediate node.
1353 xfs_da3_node_lasthash(
1354 struct xfs_inode
*dp
,
1358 struct xfs_da3_icnode_hdr nodehdr
;
1360 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, bp
->b_addr
);
1362 *count
= nodehdr
.count
;
1365 return be32_to_cpu(nodehdr
.btree
[nodehdr
.count
- 1].hashval
);
1369 * Walk back up the tree adjusting hash values as necessary,
1370 * when we stop making changes, return.
1373 xfs_da3_fixhashpath(
1374 struct xfs_da_state
*state
,
1375 struct xfs_da_state_path
*path
)
1377 struct xfs_da_state_blk
*blk
;
1378 struct xfs_da_intnode
*node
;
1379 struct xfs_da_node_entry
*btree
;
1380 xfs_dahash_t lasthash
=0;
1383 struct xfs_inode
*dp
= state
->args
->dp
;
1385 trace_xfs_da_fixhashpath(state
->args
);
1387 level
= path
->active
-1;
1388 blk
= &path
->blk
[ level
];
1389 switch (blk
->magic
) {
1390 case XFS_ATTR_LEAF_MAGIC
:
1391 lasthash
= xfs_attr_leaf_lasthash(blk
->bp
, &count
);
1395 case XFS_DIR2_LEAFN_MAGIC
:
1396 lasthash
= xfs_dir2_leaf_lasthash(dp
, blk
->bp
, &count
);
1400 case XFS_DA_NODE_MAGIC
:
1401 lasthash
= xfs_da3_node_lasthash(dp
, blk
->bp
, &count
);
1406 for (blk
--, level
--; level
>= 0; blk
--, level
--) {
1407 struct xfs_da3_icnode_hdr nodehdr
;
1409 node
= blk
->bp
->b_addr
;
1410 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, node
);
1411 btree
= nodehdr
.btree
;
1412 if (be32_to_cpu(btree
[blk
->index
].hashval
) == lasthash
)
1414 blk
->hashval
= lasthash
;
1415 btree
[blk
->index
].hashval
= cpu_to_be32(lasthash
);
1416 xfs_trans_log_buf(state
->args
->trans
, blk
->bp
,
1417 XFS_DA_LOGRANGE(node
, &btree
[blk
->index
],
1420 lasthash
= be32_to_cpu(btree
[nodehdr
.count
- 1].hashval
);
1425 * Remove an entry from an intermediate node.
1428 xfs_da3_node_remove(
1429 struct xfs_da_state
*state
,
1430 struct xfs_da_state_blk
*drop_blk
)
1432 struct xfs_da_intnode
*node
;
1433 struct xfs_da3_icnode_hdr nodehdr
;
1434 struct xfs_da_node_entry
*btree
;
1437 struct xfs_inode
*dp
= state
->args
->dp
;
1439 trace_xfs_da_node_remove(state
->args
);
1441 node
= drop_blk
->bp
->b_addr
;
1442 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, node
);
1443 ASSERT(drop_blk
->index
< nodehdr
.count
);
1444 ASSERT(drop_blk
->index
>= 0);
1447 * Copy over the offending entry, or just zero it out.
1449 index
= drop_blk
->index
;
1450 btree
= nodehdr
.btree
;
1451 if (index
< nodehdr
.count
- 1) {
1452 tmp
= nodehdr
.count
- index
- 1;
1453 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
1454 memmove(&btree
[index
], &btree
[index
+ 1], tmp
);
1455 xfs_trans_log_buf(state
->args
->trans
, drop_blk
->bp
,
1456 XFS_DA_LOGRANGE(node
, &btree
[index
], tmp
));
1457 index
= nodehdr
.count
- 1;
1459 memset(&btree
[index
], 0, sizeof(xfs_da_node_entry_t
));
1460 xfs_trans_log_buf(state
->args
->trans
, drop_blk
->bp
,
1461 XFS_DA_LOGRANGE(node
, &btree
[index
], sizeof(btree
[index
])));
1463 xfs_da3_node_hdr_to_disk(dp
->i_mount
, node
, &nodehdr
);
1464 xfs_trans_log_buf(state
->args
->trans
, drop_blk
->bp
,
1465 XFS_DA_LOGRANGE(node
, &node
->hdr
, state
->args
->geo
->node_hdr_size
));
1468 * Copy the last hash value from the block to propagate upwards.
1470 drop_blk
->hashval
= be32_to_cpu(btree
[index
- 1].hashval
);
1474 * Unbalance the elements between two intermediate nodes,
1475 * move all Btree elements from one node into another.
1478 xfs_da3_node_unbalance(
1479 struct xfs_da_state
*state
,
1480 struct xfs_da_state_blk
*drop_blk
,
1481 struct xfs_da_state_blk
*save_blk
)
1483 struct xfs_da_intnode
*drop_node
;
1484 struct xfs_da_intnode
*save_node
;
1485 struct xfs_da_node_entry
*drop_btree
;
1486 struct xfs_da_node_entry
*save_btree
;
1487 struct xfs_da3_icnode_hdr drop_hdr
;
1488 struct xfs_da3_icnode_hdr save_hdr
;
1489 struct xfs_trans
*tp
;
1492 struct xfs_inode
*dp
= state
->args
->dp
;
1494 trace_xfs_da_node_unbalance(state
->args
);
1496 drop_node
= drop_blk
->bp
->b_addr
;
1497 save_node
= save_blk
->bp
->b_addr
;
1498 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &drop_hdr
, drop_node
);
1499 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &save_hdr
, save_node
);
1500 drop_btree
= drop_hdr
.btree
;
1501 save_btree
= save_hdr
.btree
;
1502 tp
= state
->args
->trans
;
1505 * If the dying block has lower hashvals, then move all the
1506 * elements in the remaining block up to make a hole.
1508 if ((be32_to_cpu(drop_btree
[0].hashval
) <
1509 be32_to_cpu(save_btree
[0].hashval
)) ||
1510 (be32_to_cpu(drop_btree
[drop_hdr
.count
- 1].hashval
) <
1511 be32_to_cpu(save_btree
[save_hdr
.count
- 1].hashval
))) {
1512 /* XXX: check this - is memmove dst correct? */
1513 tmp
= save_hdr
.count
* sizeof(xfs_da_node_entry_t
);
1514 memmove(&save_btree
[drop_hdr
.count
], &save_btree
[0], tmp
);
1517 xfs_trans_log_buf(tp
, save_blk
->bp
,
1518 XFS_DA_LOGRANGE(save_node
, &save_btree
[0],
1519 (save_hdr
.count
+ drop_hdr
.count
) *
1520 sizeof(xfs_da_node_entry_t
)));
1522 sindex
= save_hdr
.count
;
1523 xfs_trans_log_buf(tp
, save_blk
->bp
,
1524 XFS_DA_LOGRANGE(save_node
, &save_btree
[sindex
],
1525 drop_hdr
.count
* sizeof(xfs_da_node_entry_t
)));
1529 * Move all the B-tree elements from drop_blk to save_blk.
1531 tmp
= drop_hdr
.count
* (uint
)sizeof(xfs_da_node_entry_t
);
1532 memcpy(&save_btree
[sindex
], &drop_btree
[0], tmp
);
1533 save_hdr
.count
+= drop_hdr
.count
;
1535 xfs_da3_node_hdr_to_disk(dp
->i_mount
, save_node
, &save_hdr
);
1536 xfs_trans_log_buf(tp
, save_blk
->bp
,
1537 XFS_DA_LOGRANGE(save_node
, &save_node
->hdr
,
1538 state
->args
->geo
->node_hdr_size
));
1541 * Save the last hashval in the remaining block for upward propagation.
1543 save_blk
->hashval
= be32_to_cpu(save_btree
[save_hdr
.count
- 1].hashval
);
1546 /*========================================================================
1547 * Routines used for finding things in the Btree.
1548 *========================================================================*/
1551 * Walk down the Btree looking for a particular filename, filling
1552 * in the state structure as we go.
1554 * We will set the state structure to point to each of the elements
1555 * in each of the nodes where either the hashval is or should be.
1557 * We support duplicate hashval's so for each entry in the current
1558 * node that could contain the desired hashval, descend. This is a
1559 * pruned depth-first tree search.
1562 xfs_da3_node_lookup_int(
1563 struct xfs_da_state
*state
,
1566 struct xfs_da_state_blk
*blk
;
1567 struct xfs_da_blkinfo
*curr
;
1568 struct xfs_da_intnode
*node
;
1569 struct xfs_da_node_entry
*btree
;
1570 struct xfs_da3_icnode_hdr nodehdr
;
1571 struct xfs_da_args
*args
;
1573 xfs_dahash_t hashval
;
1574 xfs_dahash_t btreehashval
;
1580 unsigned int expected_level
= 0;
1582 struct xfs_inode
*dp
= state
->args
->dp
;
1587 * Descend thru the B-tree searching each level for the right
1588 * node to use, until the right hashval is found.
1590 blkno
= args
->geo
->leafblk
;
1591 for (blk
= &state
->path
.blk
[0], state
->path
.active
= 1;
1592 state
->path
.active
<= XFS_DA_NODE_MAXDEPTH
;
1593 blk
++, state
->path
.active
++) {
1595 * Read the next node down in the tree.
1598 error
= xfs_da3_node_read(args
->trans
, args
->dp
, blkno
,
1599 &blk
->bp
, args
->whichfork
);
1602 state
->path
.active
--;
1605 curr
= blk
->bp
->b_addr
;
1606 magic
= be16_to_cpu(curr
->magic
);
1608 if (magic
== XFS_ATTR_LEAF_MAGIC
||
1609 magic
== XFS_ATTR3_LEAF_MAGIC
) {
1610 blk
->magic
= XFS_ATTR_LEAF_MAGIC
;
1611 blk
->hashval
= xfs_attr_leaf_lasthash(blk
->bp
, NULL
);
1615 if (magic
== XFS_DIR2_LEAFN_MAGIC
||
1616 magic
== XFS_DIR3_LEAFN_MAGIC
) {
1617 blk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1618 blk
->hashval
= xfs_dir2_leaf_lasthash(args
->dp
,
1623 if (magic
!= XFS_DA_NODE_MAGIC
&& magic
!= XFS_DA3_NODE_MAGIC
) {
1624 xfs_buf_corruption_error(blk
->bp
);
1625 return -EFSCORRUPTED
;
1628 blk
->magic
= XFS_DA_NODE_MAGIC
;
1631 * Search an intermediate node for a match.
1633 node
= blk
->bp
->b_addr
;
1634 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
, node
);
1635 btree
= nodehdr
.btree
;
1637 /* Tree taller than we can handle; bail out! */
1638 if (nodehdr
.level
>= XFS_DA_NODE_MAXDEPTH
) {
1639 xfs_buf_corruption_error(blk
->bp
);
1640 return -EFSCORRUPTED
;
1643 /* Check the level from the root. */
1644 if (blkno
== args
->geo
->leafblk
)
1645 expected_level
= nodehdr
.level
- 1;
1646 else if (expected_level
!= nodehdr
.level
) {
1647 xfs_buf_corruption_error(blk
->bp
);
1648 return -EFSCORRUPTED
;
1652 max
= nodehdr
.count
;
1653 blk
->hashval
= be32_to_cpu(btree
[max
- 1].hashval
);
1656 * Binary search. (note: small blocks will skip loop)
1658 probe
= span
= max
/ 2;
1659 hashval
= args
->hashval
;
1662 btreehashval
= be32_to_cpu(btree
[probe
].hashval
);
1663 if (btreehashval
< hashval
)
1665 else if (btreehashval
> hashval
)
1670 ASSERT((probe
>= 0) && (probe
< max
));
1671 ASSERT((span
<= 4) ||
1672 (be32_to_cpu(btree
[probe
].hashval
) == hashval
));
1675 * Since we may have duplicate hashval's, find the first
1676 * matching hashval in the node.
1679 be32_to_cpu(btree
[probe
].hashval
) >= hashval
) {
1682 while (probe
< max
&&
1683 be32_to_cpu(btree
[probe
].hashval
) < hashval
) {
1688 * Pick the right block to descend on.
1691 blk
->index
= max
- 1;
1692 blkno
= be32_to_cpu(btree
[max
- 1].before
);
1695 blkno
= be32_to_cpu(btree
[probe
].before
);
1698 /* We can't point back to the root. */
1699 if (XFS_IS_CORRUPT(dp
->i_mount
, blkno
== args
->geo
->leafblk
))
1700 return -EFSCORRUPTED
;
1703 if (XFS_IS_CORRUPT(dp
->i_mount
, expected_level
!= 0))
1704 return -EFSCORRUPTED
;
1707 * A leaf block that ends in the hashval that we are interested in
1708 * (final hashval == search hashval) means that the next block may
1709 * contain more entries with the same hashval, shift upward to the
1710 * next leaf and keep searching.
1713 if (blk
->magic
== XFS_DIR2_LEAFN_MAGIC
) {
1714 retval
= xfs_dir2_leafn_lookup_int(blk
->bp
, args
,
1715 &blk
->index
, state
);
1716 } else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1717 retval
= xfs_attr3_leaf_lookup_int(blk
->bp
, args
);
1718 blk
->index
= args
->index
;
1719 args
->blkno
= blk
->blkno
;
1722 return -EFSCORRUPTED
;
1724 if (((retval
== -ENOENT
) || (retval
== -ENOATTR
)) &&
1725 (blk
->hashval
== args
->hashval
)) {
1726 error
= xfs_da3_path_shift(state
, &state
->path
, 1, 1,
1732 } else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1733 /* path_shift() gives ENOENT */
1743 /*========================================================================
1745 *========================================================================*/
1748 * Compare two intermediate nodes for "order".
1752 struct xfs_inode
*dp
,
1753 struct xfs_buf
*node1_bp
,
1754 struct xfs_buf
*node2_bp
)
1756 struct xfs_da_intnode
*node1
;
1757 struct xfs_da_intnode
*node2
;
1758 struct xfs_da_node_entry
*btree1
;
1759 struct xfs_da_node_entry
*btree2
;
1760 struct xfs_da3_icnode_hdr node1hdr
;
1761 struct xfs_da3_icnode_hdr node2hdr
;
1763 node1
= node1_bp
->b_addr
;
1764 node2
= node2_bp
->b_addr
;
1765 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &node1hdr
, node1
);
1766 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &node2hdr
, node2
);
1767 btree1
= node1hdr
.btree
;
1768 btree2
= node2hdr
.btree
;
1770 if (node1hdr
.count
> 0 && node2hdr
.count
> 0 &&
1771 ((be32_to_cpu(btree2
[0].hashval
) < be32_to_cpu(btree1
[0].hashval
)) ||
1772 (be32_to_cpu(btree2
[node2hdr
.count
- 1].hashval
) <
1773 be32_to_cpu(btree1
[node1hdr
.count
- 1].hashval
)))) {
1780 * Link a new block into a doubly linked list of blocks (of whatever type).
1784 struct xfs_da_state
*state
,
1785 struct xfs_da_state_blk
*old_blk
,
1786 struct xfs_da_state_blk
*new_blk
)
1788 struct xfs_da_blkinfo
*old_info
;
1789 struct xfs_da_blkinfo
*new_info
;
1790 struct xfs_da_blkinfo
*tmp_info
;
1791 struct xfs_da_args
*args
;
1795 struct xfs_inode
*dp
= state
->args
->dp
;
1798 * Set up environment.
1801 ASSERT(args
!= NULL
);
1802 old_info
= old_blk
->bp
->b_addr
;
1803 new_info
= new_blk
->bp
->b_addr
;
1804 ASSERT(old_blk
->magic
== XFS_DA_NODE_MAGIC
||
1805 old_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
||
1806 old_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1808 switch (old_blk
->magic
) {
1809 case XFS_ATTR_LEAF_MAGIC
:
1810 before
= xfs_attr_leaf_order(old_blk
->bp
, new_blk
->bp
);
1812 case XFS_DIR2_LEAFN_MAGIC
:
1813 before
= xfs_dir2_leafn_order(dp
, old_blk
->bp
, new_blk
->bp
);
1815 case XFS_DA_NODE_MAGIC
:
1816 before
= xfs_da3_node_order(dp
, old_blk
->bp
, new_blk
->bp
);
1821 * Link blocks in appropriate order.
1825 * Link new block in before existing block.
1827 trace_xfs_da_link_before(args
);
1828 new_info
->forw
= cpu_to_be32(old_blk
->blkno
);
1829 new_info
->back
= old_info
->back
;
1830 if (old_info
->back
) {
1831 error
= xfs_da3_node_read(args
->trans
, dp
,
1832 be32_to_cpu(old_info
->back
),
1833 &bp
, args
->whichfork
);
1837 tmp_info
= bp
->b_addr
;
1838 ASSERT(tmp_info
->magic
== old_info
->magic
);
1839 ASSERT(be32_to_cpu(tmp_info
->forw
) == old_blk
->blkno
);
1840 tmp_info
->forw
= cpu_to_be32(new_blk
->blkno
);
1841 xfs_trans_log_buf(args
->trans
, bp
, 0, sizeof(*tmp_info
)-1);
1843 old_info
->back
= cpu_to_be32(new_blk
->blkno
);
1846 * Link new block in after existing block.
1848 trace_xfs_da_link_after(args
);
1849 new_info
->forw
= old_info
->forw
;
1850 new_info
->back
= cpu_to_be32(old_blk
->blkno
);
1851 if (old_info
->forw
) {
1852 error
= xfs_da3_node_read(args
->trans
, dp
,
1853 be32_to_cpu(old_info
->forw
),
1854 &bp
, args
->whichfork
);
1858 tmp_info
= bp
->b_addr
;
1859 ASSERT(tmp_info
->magic
== old_info
->magic
);
1860 ASSERT(be32_to_cpu(tmp_info
->back
) == old_blk
->blkno
);
1861 tmp_info
->back
= cpu_to_be32(new_blk
->blkno
);
1862 xfs_trans_log_buf(args
->trans
, bp
, 0, sizeof(*tmp_info
)-1);
1864 old_info
->forw
= cpu_to_be32(new_blk
->blkno
);
1867 xfs_trans_log_buf(args
->trans
, old_blk
->bp
, 0, sizeof(*tmp_info
) - 1);
1868 xfs_trans_log_buf(args
->trans
, new_blk
->bp
, 0, sizeof(*tmp_info
) - 1);
1873 * Unlink a block from a doubly linked list of blocks.
1875 STATIC
int /* error */
1877 struct xfs_da_state
*state
,
1878 struct xfs_da_state_blk
*drop_blk
,
1879 struct xfs_da_state_blk
*save_blk
)
1881 struct xfs_da_blkinfo
*drop_info
;
1882 struct xfs_da_blkinfo
*save_info
;
1883 struct xfs_da_blkinfo
*tmp_info
;
1884 struct xfs_da_args
*args
;
1889 * Set up environment.
1892 ASSERT(args
!= NULL
);
1893 save_info
= save_blk
->bp
->b_addr
;
1894 drop_info
= drop_blk
->bp
->b_addr
;
1895 ASSERT(save_blk
->magic
== XFS_DA_NODE_MAGIC
||
1896 save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
||
1897 save_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1898 ASSERT(save_blk
->magic
== drop_blk
->magic
);
1899 ASSERT((be32_to_cpu(save_info
->forw
) == drop_blk
->blkno
) ||
1900 (be32_to_cpu(save_info
->back
) == drop_blk
->blkno
));
1901 ASSERT((be32_to_cpu(drop_info
->forw
) == save_blk
->blkno
) ||
1902 (be32_to_cpu(drop_info
->back
) == save_blk
->blkno
));
1905 * Unlink the leaf block from the doubly linked chain of leaves.
1907 if (be32_to_cpu(save_info
->back
) == drop_blk
->blkno
) {
1908 trace_xfs_da_unlink_back(args
);
1909 save_info
->back
= drop_info
->back
;
1910 if (drop_info
->back
) {
1911 error
= xfs_da3_node_read(args
->trans
, args
->dp
,
1912 be32_to_cpu(drop_info
->back
),
1913 &bp
, args
->whichfork
);
1917 tmp_info
= bp
->b_addr
;
1918 ASSERT(tmp_info
->magic
== save_info
->magic
);
1919 ASSERT(be32_to_cpu(tmp_info
->forw
) == drop_blk
->blkno
);
1920 tmp_info
->forw
= cpu_to_be32(save_blk
->blkno
);
1921 xfs_trans_log_buf(args
->trans
, bp
, 0,
1922 sizeof(*tmp_info
) - 1);
1925 trace_xfs_da_unlink_forward(args
);
1926 save_info
->forw
= drop_info
->forw
;
1927 if (drop_info
->forw
) {
1928 error
= xfs_da3_node_read(args
->trans
, args
->dp
,
1929 be32_to_cpu(drop_info
->forw
),
1930 &bp
, args
->whichfork
);
1934 tmp_info
= bp
->b_addr
;
1935 ASSERT(tmp_info
->magic
== save_info
->magic
);
1936 ASSERT(be32_to_cpu(tmp_info
->back
) == drop_blk
->blkno
);
1937 tmp_info
->back
= cpu_to_be32(save_blk
->blkno
);
1938 xfs_trans_log_buf(args
->trans
, bp
, 0,
1939 sizeof(*tmp_info
) - 1);
1943 xfs_trans_log_buf(args
->trans
, save_blk
->bp
, 0, sizeof(*save_info
) - 1);
1948 * Move a path "forward" or "!forward" one block at the current level.
1950 * This routine will adjust a "path" to point to the next block
1951 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1952 * Btree, including updating pointers to the intermediate nodes between
1953 * the new bottom and the root.
1957 struct xfs_da_state
*state
,
1958 struct xfs_da_state_path
*path
,
1963 struct xfs_da_state_blk
*blk
;
1964 struct xfs_da_blkinfo
*info
;
1965 struct xfs_da_args
*args
;
1966 struct xfs_da_node_entry
*btree
;
1967 struct xfs_da3_icnode_hdr nodehdr
;
1969 xfs_dablk_t blkno
= 0;
1972 struct xfs_inode
*dp
= state
->args
->dp
;
1974 trace_xfs_da_path_shift(state
->args
);
1977 * Roll up the Btree looking for the first block where our
1978 * current index is not at the edge of the block. Note that
1979 * we skip the bottom layer because we want the sibling block.
1982 ASSERT(args
!= NULL
);
1983 ASSERT(path
!= NULL
);
1984 ASSERT((path
->active
> 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1985 level
= (path
->active
-1) - 1; /* skip bottom layer in path */
1986 for (blk
= &path
->blk
[level
]; level
>= 0; blk
--, level
--) {
1987 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
,
1990 if (forward
&& (blk
->index
< nodehdr
.count
- 1)) {
1992 blkno
= be32_to_cpu(nodehdr
.btree
[blk
->index
].before
);
1994 } else if (!forward
&& (blk
->index
> 0)) {
1996 blkno
= be32_to_cpu(nodehdr
.btree
[blk
->index
].before
);
2001 *result
= -ENOENT
; /* we're out of our tree */
2002 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
2007 * Roll down the edge of the subtree until we reach the
2008 * same depth we were at originally.
2010 for (blk
++, level
++; level
< path
->active
; blk
++, level
++) {
2012 * Read the next child block into a local buffer.
2014 error
= xfs_da3_node_read(args
->trans
, dp
, blkno
, &bp
,
2020 * Release the old block (if it's dirty, the trans doesn't
2021 * actually let go) and swap the local buffer into the path
2022 * structure. This ensures failure of the above read doesn't set
2023 * a NULL buffer in an active slot in the path.
2026 xfs_trans_brelse(args
->trans
, blk
->bp
);
2030 info
= blk
->bp
->b_addr
;
2031 ASSERT(info
->magic
== cpu_to_be16(XFS_DA_NODE_MAGIC
) ||
2032 info
->magic
== cpu_to_be16(XFS_DA3_NODE_MAGIC
) ||
2033 info
->magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
2034 info
->magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
) ||
2035 info
->magic
== cpu_to_be16(XFS_ATTR_LEAF_MAGIC
) ||
2036 info
->magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
));
2040 * Note: we flatten the magic number to a single type so we
2041 * don't have to compare against crc/non-crc types elsewhere.
2043 switch (be16_to_cpu(info
->magic
)) {
2044 case XFS_DA_NODE_MAGIC
:
2045 case XFS_DA3_NODE_MAGIC
:
2046 blk
->magic
= XFS_DA_NODE_MAGIC
;
2047 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &nodehdr
,
2049 btree
= nodehdr
.btree
;
2050 blk
->hashval
= be32_to_cpu(btree
[nodehdr
.count
- 1].hashval
);
2054 blk
->index
= nodehdr
.count
- 1;
2055 blkno
= be32_to_cpu(btree
[blk
->index
].before
);
2057 case XFS_ATTR_LEAF_MAGIC
:
2058 case XFS_ATTR3_LEAF_MAGIC
:
2059 blk
->magic
= XFS_ATTR_LEAF_MAGIC
;
2060 ASSERT(level
== path
->active
-1);
2062 blk
->hashval
= xfs_attr_leaf_lasthash(blk
->bp
, NULL
);
2064 case XFS_DIR2_LEAFN_MAGIC
:
2065 case XFS_DIR3_LEAFN_MAGIC
:
2066 blk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
2067 ASSERT(level
== path
->active
-1);
2069 blk
->hashval
= xfs_dir2_leaf_lasthash(args
->dp
,
2082 /*========================================================================
2084 *========================================================================*/
2087 * Implement a simple hash on a character string.
2088 * Rotate the hash value by 7 bits, then XOR each character in.
2089 * This is implemented with some source-level loop unrolling.
2092 xfs_da_hashname(const uint8_t *name
, int namelen
)
2097 * Do four characters at a time as long as we can.
2099 for (hash
= 0; namelen
>= 4; namelen
-= 4, name
+= 4)
2100 hash
= (name
[0] << 21) ^ (name
[1] << 14) ^ (name
[2] << 7) ^
2101 (name
[3] << 0) ^ rol32(hash
, 7 * 4);
2104 * Now do the rest of the characters.
2108 return (name
[0] << 14) ^ (name
[1] << 7) ^ (name
[2] << 0) ^
2111 return (name
[0] << 7) ^ (name
[1] << 0) ^ rol32(hash
, 7 * 2);
2113 return (name
[0] << 0) ^ rol32(hash
, 7 * 1);
2114 default: /* case 0: */
2121 struct xfs_da_args
*args
,
2122 const unsigned char *name
,
2125 return (args
->namelen
== len
&& memcmp(args
->name
, name
, len
) == 0) ?
2126 XFS_CMP_EXACT
: XFS_CMP_DIFFERENT
;
2130 xfs_da_grow_inode_int(
2131 struct xfs_da_args
*args
,
2135 struct xfs_trans
*tp
= args
->trans
;
2136 struct xfs_inode
*dp
= args
->dp
;
2137 int w
= args
->whichfork
;
2138 xfs_rfsblock_t nblks
= dp
->i_d
.di_nblocks
;
2139 struct xfs_bmbt_irec map
, *mapp
;
2140 int nmap
, error
, got
, i
, mapi
;
2143 * Find a spot in the file space to put the new block.
2145 error
= xfs_bmap_first_unused(tp
, dp
, count
, bno
, w
);
2150 * Try mapping it in one filesystem block.
2153 error
= xfs_bmapi_write(tp
, dp
, *bno
, count
,
2154 xfs_bmapi_aflag(w
)|XFS_BMAPI_METADATA
|XFS_BMAPI_CONTIG
,
2155 args
->total
, &map
, &nmap
);
2163 } else if (nmap
== 0 && count
> 1) {
2168 * If we didn't get it and the block might work if fragmented,
2169 * try without the CONTIG flag. Loop until we get it all.
2171 mapp
= kmem_alloc(sizeof(*mapp
) * count
, 0);
2172 for (b
= *bno
, mapi
= 0; b
< *bno
+ count
; ) {
2173 nmap
= min(XFS_BMAP_MAX_NMAP
, count
);
2174 c
= (int)(*bno
+ count
- b
);
2175 error
= xfs_bmapi_write(tp
, dp
, b
, c
,
2176 xfs_bmapi_aflag(w
)|XFS_BMAPI_METADATA
,
2177 args
->total
, &mapp
[mapi
], &nmap
);
2183 b
= mapp
[mapi
- 1].br_startoff
+
2184 mapp
[mapi
- 1].br_blockcount
;
2192 * Count the blocks we got, make sure it matches the total.
2194 for (i
= 0, got
= 0; i
< mapi
; i
++)
2195 got
+= mapp
[i
].br_blockcount
;
2196 if (got
!= count
|| mapp
[0].br_startoff
!= *bno
||
2197 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
2203 /* account for newly allocated blocks in reserved blocks total */
2204 args
->total
-= dp
->i_d
.di_nblocks
- nblks
;
2213 * Add a block to the btree ahead of the file.
2214 * Return the new block number to the caller.
2218 struct xfs_da_args
*args
,
2219 xfs_dablk_t
*new_blkno
)
2224 trace_xfs_da_grow_inode(args
);
2226 bno
= args
->geo
->leafblk
;
2227 error
= xfs_da_grow_inode_int(args
, &bno
, args
->geo
->fsbcount
);
2229 *new_blkno
= (xfs_dablk_t
)bno
;
2234 * Ick. We need to always be able to remove a btree block, even
2235 * if there's no space reservation because the filesystem is full.
2236 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2237 * It swaps the target block with the last block in the file. The
2238 * last block in the file can always be removed since it can't cause
2239 * a bmap btree split to do that.
2242 xfs_da3_swap_lastblock(
2243 struct xfs_da_args
*args
,
2244 xfs_dablk_t
*dead_blknop
,
2245 struct xfs_buf
**dead_bufp
)
2247 struct xfs_da_blkinfo
*dead_info
;
2248 struct xfs_da_blkinfo
*sib_info
;
2249 struct xfs_da_intnode
*par_node
;
2250 struct xfs_da_intnode
*dead_node
;
2251 struct xfs_dir2_leaf
*dead_leaf2
;
2252 struct xfs_da_node_entry
*btree
;
2253 struct xfs_da3_icnode_hdr par_hdr
;
2254 struct xfs_inode
*dp
;
2255 struct xfs_trans
*tp
;
2256 struct xfs_mount
*mp
;
2257 struct xfs_buf
*dead_buf
;
2258 struct xfs_buf
*last_buf
;
2259 struct xfs_buf
*sib_buf
;
2260 struct xfs_buf
*par_buf
;
2261 xfs_dahash_t dead_hash
;
2262 xfs_fileoff_t lastoff
;
2263 xfs_dablk_t dead_blkno
;
2264 xfs_dablk_t last_blkno
;
2265 xfs_dablk_t sib_blkno
;
2266 xfs_dablk_t par_blkno
;
2273 trace_xfs_da_swap_lastblock(args
);
2275 dead_buf
= *dead_bufp
;
2276 dead_blkno
= *dead_blknop
;
2279 w
= args
->whichfork
;
2280 ASSERT(w
== XFS_DATA_FORK
);
2282 lastoff
= args
->geo
->freeblk
;
2283 error
= xfs_bmap_last_before(tp
, dp
, &lastoff
, w
);
2286 if (XFS_IS_CORRUPT(mp
, lastoff
== 0))
2287 return -EFSCORRUPTED
;
2289 * Read the last block in the btree space.
2291 last_blkno
= (xfs_dablk_t
)lastoff
- args
->geo
->fsbcount
;
2292 error
= xfs_da3_node_read(tp
, dp
, last_blkno
, &last_buf
, w
);
2296 * Copy the last block into the dead buffer and log it.
2298 memcpy(dead_buf
->b_addr
, last_buf
->b_addr
, args
->geo
->blksize
);
2299 xfs_trans_log_buf(tp
, dead_buf
, 0, args
->geo
->blksize
- 1);
2300 dead_info
= dead_buf
->b_addr
;
2302 * Get values from the moved block.
2304 if (dead_info
->magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
) ||
2305 dead_info
->magic
== cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
)) {
2306 struct xfs_dir3_icleaf_hdr leafhdr
;
2307 struct xfs_dir2_leaf_entry
*ents
;
2309 dead_leaf2
= (xfs_dir2_leaf_t
*)dead_info
;
2310 xfs_dir2_leaf_hdr_from_disk(dp
->i_mount
, &leafhdr
,
2312 ents
= leafhdr
.ents
;
2314 dead_hash
= be32_to_cpu(ents
[leafhdr
.count
- 1].hashval
);
2316 struct xfs_da3_icnode_hdr deadhdr
;
2318 dead_node
= (xfs_da_intnode_t
*)dead_info
;
2319 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &deadhdr
, dead_node
);
2320 btree
= deadhdr
.btree
;
2321 dead_level
= deadhdr
.level
;
2322 dead_hash
= be32_to_cpu(btree
[deadhdr
.count
- 1].hashval
);
2324 sib_buf
= par_buf
= NULL
;
2326 * If the moved block has a left sibling, fix up the pointers.
2328 if ((sib_blkno
= be32_to_cpu(dead_info
->back
))) {
2329 error
= xfs_da3_node_read(tp
, dp
, sib_blkno
, &sib_buf
, w
);
2332 sib_info
= sib_buf
->b_addr
;
2333 if (XFS_IS_CORRUPT(mp
,
2334 be32_to_cpu(sib_info
->forw
) != last_blkno
||
2335 sib_info
->magic
!= dead_info
->magic
)) {
2336 error
= -EFSCORRUPTED
;
2339 sib_info
->forw
= cpu_to_be32(dead_blkno
);
2340 xfs_trans_log_buf(tp
, sib_buf
,
2341 XFS_DA_LOGRANGE(sib_info
, &sib_info
->forw
,
2342 sizeof(sib_info
->forw
)));
2346 * If the moved block has a right sibling, fix up the pointers.
2348 if ((sib_blkno
= be32_to_cpu(dead_info
->forw
))) {
2349 error
= xfs_da3_node_read(tp
, dp
, sib_blkno
, &sib_buf
, w
);
2352 sib_info
= sib_buf
->b_addr
;
2353 if (XFS_IS_CORRUPT(mp
,
2354 be32_to_cpu(sib_info
->back
) != last_blkno
||
2355 sib_info
->magic
!= dead_info
->magic
)) {
2356 error
= -EFSCORRUPTED
;
2359 sib_info
->back
= cpu_to_be32(dead_blkno
);
2360 xfs_trans_log_buf(tp
, sib_buf
,
2361 XFS_DA_LOGRANGE(sib_info
, &sib_info
->back
,
2362 sizeof(sib_info
->back
)));
2365 par_blkno
= args
->geo
->leafblk
;
2368 * Walk down the tree looking for the parent of the moved block.
2371 error
= xfs_da3_node_read(tp
, dp
, par_blkno
, &par_buf
, w
);
2374 par_node
= par_buf
->b_addr
;
2375 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &par_hdr
, par_node
);
2376 if (XFS_IS_CORRUPT(mp
,
2377 level
>= 0 && level
!= par_hdr
.level
+ 1)) {
2378 error
= -EFSCORRUPTED
;
2381 level
= par_hdr
.level
;
2382 btree
= par_hdr
.btree
;
2384 entno
< par_hdr
.count
&&
2385 be32_to_cpu(btree
[entno
].hashval
) < dead_hash
;
2388 if (XFS_IS_CORRUPT(mp
, entno
== par_hdr
.count
)) {
2389 error
= -EFSCORRUPTED
;
2392 par_blkno
= be32_to_cpu(btree
[entno
].before
);
2393 if (level
== dead_level
+ 1)
2395 xfs_trans_brelse(tp
, par_buf
);
2399 * We're in the right parent block.
2400 * Look for the right entry.
2404 entno
< par_hdr
.count
&&
2405 be32_to_cpu(btree
[entno
].before
) != last_blkno
;
2408 if (entno
< par_hdr
.count
)
2410 par_blkno
= par_hdr
.forw
;
2411 xfs_trans_brelse(tp
, par_buf
);
2413 if (XFS_IS_CORRUPT(mp
, par_blkno
== 0)) {
2414 error
= -EFSCORRUPTED
;
2417 error
= xfs_da3_node_read(tp
, dp
, par_blkno
, &par_buf
, w
);
2420 par_node
= par_buf
->b_addr
;
2421 xfs_da3_node_hdr_from_disk(dp
->i_mount
, &par_hdr
, par_node
);
2422 if (XFS_IS_CORRUPT(mp
, par_hdr
.level
!= level
)) {
2423 error
= -EFSCORRUPTED
;
2426 btree
= par_hdr
.btree
;
2430 * Update the parent entry pointing to the moved block.
2432 btree
[entno
].before
= cpu_to_be32(dead_blkno
);
2433 xfs_trans_log_buf(tp
, par_buf
,
2434 XFS_DA_LOGRANGE(par_node
, &btree
[entno
].before
,
2435 sizeof(btree
[entno
].before
)));
2436 *dead_blknop
= last_blkno
;
2437 *dead_bufp
= last_buf
;
2441 xfs_trans_brelse(tp
, par_buf
);
2443 xfs_trans_brelse(tp
, sib_buf
);
2444 xfs_trans_brelse(tp
, last_buf
);
2449 * Remove a btree block from a directory or attribute.
2452 xfs_da_shrink_inode(
2453 struct xfs_da_args
*args
,
2454 xfs_dablk_t dead_blkno
,
2455 struct xfs_buf
*dead_buf
)
2457 struct xfs_inode
*dp
;
2458 int done
, error
, w
, count
;
2459 struct xfs_trans
*tp
;
2461 trace_xfs_da_shrink_inode(args
);
2464 w
= args
->whichfork
;
2466 count
= args
->geo
->fsbcount
;
2469 * Remove extents. If we get ENOSPC for a dir we have to move
2470 * the last block to the place we want to kill.
2472 error
= xfs_bunmapi(tp
, dp
, dead_blkno
, count
,
2473 xfs_bmapi_aflag(w
), 0, &done
);
2474 if (error
== -ENOSPC
) {
2475 if (w
!= XFS_DATA_FORK
)
2477 error
= xfs_da3_swap_lastblock(args
, &dead_blkno
,
2485 xfs_trans_binval(tp
, dead_buf
);
2491 struct xfs_inode
*dp
,
2495 struct xfs_buf_map
**mapp
,
2498 struct xfs_mount
*mp
= dp
->i_mount
;
2499 int nfsb
= xfs_dabuf_nfsb(mp
, whichfork
);
2500 struct xfs_bmbt_irec irec
, *irecs
= &irec
;
2501 struct xfs_buf_map
*map
= *mapp
;
2502 xfs_fileoff_t off
= bno
;
2503 int error
= 0, nirecs
, i
;
2506 irecs
= kmem_zalloc(sizeof(irec
) * nfsb
, KM_NOFS
);
2509 error
= xfs_bmapi_read(dp
, bno
, nfsb
, irecs
, &nirecs
,
2510 xfs_bmapi_aflag(whichfork
));
2512 goto out_free_irecs
;
2515 * Use the caller provided map for the single map case, else allocate a
2516 * larger one that needs to be free by the caller.
2519 map
= kmem_zalloc(nirecs
* sizeof(struct xfs_buf_map
), KM_NOFS
);
2521 goto out_free_irecs
;
2525 for (i
= 0; i
< nirecs
; i
++) {
2526 if (irecs
[i
].br_startblock
== HOLESTARTBLOCK
||
2527 irecs
[i
].br_startblock
== DELAYSTARTBLOCK
)
2528 goto invalid_mapping
;
2529 if (off
!= irecs
[i
].br_startoff
)
2530 goto invalid_mapping
;
2532 map
[i
].bm_bn
= XFS_FSB_TO_DADDR(mp
, irecs
[i
].br_startblock
);
2533 map
[i
].bm_len
= XFS_FSB_TO_BB(mp
, irecs
[i
].br_blockcount
);
2534 off
+= irecs
[i
].br_blockcount
;
2537 if (off
!= bno
+ nfsb
)
2538 goto invalid_mapping
;
2547 /* Caller ok with no mapping. */
2548 if (XFS_IS_CORRUPT(mp
, !(flags
& XFS_DABUF_MAP_HOLE_OK
))) {
2549 error
= -EFSCORRUPTED
;
2550 if (xfs_error_level
>= XFS_ERRLEVEL_LOW
) {
2551 xfs_alert(mp
, "%s: bno %u inode %llu",
2552 __func__
, bno
, dp
->i_ino
);
2554 for (i
= 0; i
< nirecs
; i
++) {
2556 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
2557 i
, irecs
[i
].br_startoff
,
2558 irecs
[i
].br_startblock
,
2559 irecs
[i
].br_blockcount
,
2566 goto out_free_irecs
;
2570 * Get a buffer for the dir/attr block.
2574 struct xfs_trans
*tp
,
2575 struct xfs_inode
*dp
,
2577 struct xfs_buf
**bpp
,
2580 struct xfs_mount
*mp
= dp
->i_mount
;
2582 struct xfs_buf_map map
, *mapp
= &map
;
2587 error
= xfs_dabuf_map(dp
, bno
, 0, whichfork
, &mapp
, &nmap
);
2588 if (error
|| nmap
== 0)
2591 error
= xfs_trans_get_buf_map(tp
, mp
->m_ddev_targp
, mapp
, nmap
, 0, &bp
);
2605 * Get a buffer for the dir/attr block, fill in the contents.
2609 struct xfs_trans
*tp
,
2610 struct xfs_inode
*dp
,
2613 struct xfs_buf
**bpp
,
2615 const struct xfs_buf_ops
*ops
)
2617 struct xfs_mount
*mp
= dp
->i_mount
;
2619 struct xfs_buf_map map
, *mapp
= &map
;
2624 error
= xfs_dabuf_map(dp
, bno
, flags
, whichfork
, &mapp
, &nmap
);
2628 error
= xfs_trans_read_buf_map(mp
, tp
, mp
->m_ddev_targp
, mapp
, nmap
, 0,
2633 if (whichfork
== XFS_ATTR_FORK
)
2634 xfs_buf_set_ref(bp
, XFS_ATTR_BTREE_REF
);
2636 xfs_buf_set_ref(bp
, XFS_DIR_BTREE_REF
);
2646 * Readahead the dir/attr block.
2650 struct xfs_inode
*dp
,
2654 const struct xfs_buf_ops
*ops
)
2656 struct xfs_buf_map map
;
2657 struct xfs_buf_map
*mapp
;
2663 error
= xfs_dabuf_map(dp
, bno
, flags
, whichfork
, &mapp
, &nmap
);
2667 xfs_buf_readahead_map(dp
->i_mount
->m_ddev_targp
, mapp
, nmap
, ops
);