2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
39 #include "xfs_trans.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_alloc.h"
50 #include "xfs_btree.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode_item.h"
56 #include "xfs_inode.h"
58 #include "xfs_da_btree.h"
60 #include "xfs_attr_leaf.h"
61 #include "xfs_dir_leaf.h"
62 #include "xfs_dir2_data.h"
63 #include "xfs_dir2_leaf.h"
64 #include "xfs_dir2_block.h"
65 #include "xfs_dir2_node.h"
66 #include "xfs_error.h"
72 * Routines to implement directories as Btrees of hashed names.
75 /*========================================================================
76 * Function prototypes for the kernel.
77 *========================================================================*/
80 * Routines used for growing the Btree.
82 STATIC
int xfs_da_root_split(xfs_da_state_t
*state
,
83 xfs_da_state_blk_t
*existing_root
,
84 xfs_da_state_blk_t
*new_child
);
85 STATIC
int xfs_da_node_split(xfs_da_state_t
*state
,
86 xfs_da_state_blk_t
*existing_blk
,
87 xfs_da_state_blk_t
*split_blk
,
88 xfs_da_state_blk_t
*blk_to_add
,
91 STATIC
void xfs_da_node_rebalance(xfs_da_state_t
*state
,
92 xfs_da_state_blk_t
*node_blk_1
,
93 xfs_da_state_blk_t
*node_blk_2
);
94 STATIC
void xfs_da_node_add(xfs_da_state_t
*state
,
95 xfs_da_state_blk_t
*old_node_blk
,
96 xfs_da_state_blk_t
*new_node_blk
);
99 * Routines used for shrinking the Btree.
101 STATIC
int xfs_da_root_join(xfs_da_state_t
*state
,
102 xfs_da_state_blk_t
*root_blk
);
103 STATIC
int xfs_da_node_toosmall(xfs_da_state_t
*state
, int *retval
);
104 STATIC
void xfs_da_node_remove(xfs_da_state_t
*state
,
105 xfs_da_state_blk_t
*drop_blk
);
106 STATIC
void xfs_da_node_unbalance(xfs_da_state_t
*state
,
107 xfs_da_state_blk_t
*src_node_blk
,
108 xfs_da_state_blk_t
*dst_node_blk
);
113 STATIC uint
xfs_da_node_lasthash(xfs_dabuf_t
*bp
, int *count
);
114 STATIC
int xfs_da_node_order(xfs_dabuf_t
*node1_bp
, xfs_dabuf_t
*node2_bp
);
115 STATIC xfs_dabuf_t
*xfs_da_buf_make(int nbuf
, xfs_buf_t
**bps
, inst_t
*ra
);
118 /*========================================================================
119 * Routines used for growing the Btree.
120 *========================================================================*/
123 * Create the initial contents of an intermediate node.
126 xfs_da_node_create(xfs_da_args_t
*args
, xfs_dablk_t blkno
, int level
,
127 xfs_dabuf_t
**bpp
, int whichfork
)
129 xfs_da_intnode_t
*node
;
135 error
= xfs_da_get_buf(tp
, args
->dp
, blkno
, -1, &bp
, whichfork
);
140 INT_ZERO(node
->hdr
.info
.forw
, ARCH_CONVERT
);
141 INT_ZERO(node
->hdr
.info
.back
, ARCH_CONVERT
);
142 INT_SET(node
->hdr
.info
.magic
, ARCH_CONVERT
, XFS_DA_NODE_MAGIC
);
143 INT_ZERO(node
->hdr
.info
.pad
, ARCH_CONVERT
);
144 INT_ZERO(node
->hdr
.count
, ARCH_CONVERT
);
145 INT_SET(node
->hdr
.level
, ARCH_CONVERT
, level
);
147 xfs_da_log_buf(tp
, bp
,
148 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
155 * Split a leaf node, rebalance, then possibly split
156 * intermediate nodes, rebalance, etc.
159 xfs_da_split(xfs_da_state_t
*state
)
161 xfs_da_state_blk_t
*oldblk
, *newblk
, *addblk
;
162 xfs_da_intnode_t
*node
;
164 int max
, action
, error
, i
;
167 * Walk back up the tree splitting/inserting/adjusting as necessary.
168 * If we need to insert and there isn't room, split the node, then
169 * decide which fragment to insert the new block from below into.
170 * Note that we may split the root this way, but we need more fixup.
172 max
= state
->path
.active
- 1;
173 ASSERT((max
>= 0) && (max
< XFS_DA_NODE_MAXDEPTH
));
174 ASSERT(state
->path
.blk
[max
].magic
== XFS_ATTR_LEAF_MAGIC
||
175 state
->path
.blk
[max
].magic
== XFS_DIRX_LEAF_MAGIC(state
->mp
));
177 addblk
= &state
->path
.blk
[max
]; /* initial dummy value */
178 for (i
= max
; (i
>= 0) && addblk
; state
->path
.active
--, i
--) {
179 oldblk
= &state
->path
.blk
[i
];
180 newblk
= &state
->altpath
.blk
[i
];
183 * If a leaf node then
184 * Allocate a new leaf node, then rebalance across them.
185 * else if an intermediate node then
186 * We split on the last layer, must we split the node?
188 switch (oldblk
->magic
) {
189 case XFS_ATTR_LEAF_MAGIC
:
193 error
= xfs_attr_leaf_split(state
, oldblk
, newblk
);
194 if ((error
!= 0) && (error
!= ENOSPC
)) {
195 return(error
); /* GROT: attr is inconsistent */
202 * Entry wouldn't fit, split the leaf again.
204 state
->extravalid
= 1;
206 state
->extraafter
= 0; /* before newblk */
207 error
= xfs_attr_leaf_split(state
, oldblk
,
210 state
->extraafter
= 1; /* after newblk */
211 error
= xfs_attr_leaf_split(state
, newblk
,
215 return(error
); /* GROT: attr inconsistent */
219 case XFS_DIR_LEAF_MAGIC
:
220 ASSERT(XFS_DIR_IS_V1(state
->mp
));
221 error
= xfs_dir_leaf_split(state
, oldblk
, newblk
);
222 if ((error
!= 0) && (error
!= ENOSPC
)) {
223 return(error
); /* GROT: dir is inconsistent */
230 * Entry wouldn't fit, split the leaf again.
232 state
->extravalid
= 1;
234 state
->extraafter
= 0; /* before newblk */
235 error
= xfs_dir_leaf_split(state
, oldblk
,
238 return(error
); /* GROT: dir incon. */
241 state
->extraafter
= 1; /* after newblk */
242 error
= xfs_dir_leaf_split(state
, newblk
,
245 return(error
); /* GROT: dir incon. */
249 case XFS_DIR2_LEAFN_MAGIC
:
250 ASSERT(XFS_DIR_IS_V2(state
->mp
));
251 error
= xfs_dir2_leafn_split(state
, oldblk
, newblk
);
256 case XFS_DA_NODE_MAGIC
:
257 error
= xfs_da_node_split(state
, oldblk
, newblk
, addblk
,
259 xfs_da_buf_done(addblk
->bp
);
262 return(error
); /* GROT: dir is inconsistent */
264 * Record the newly split block for the next time thru?
274 * Update the btree to show the new hashval for this child.
276 xfs_da_fixhashpath(state
, &state
->path
);
278 * If we won't need this block again, it's getting dropped
279 * from the active path by the loop control, so we need
280 * to mark it done now.
282 if (i
> 0 || !addblk
)
283 xfs_da_buf_done(oldblk
->bp
);
289 * Split the root node.
291 ASSERT(state
->path
.active
== 0);
292 oldblk
= &state
->path
.blk
[0];
293 error
= xfs_da_root_split(state
, oldblk
, addblk
);
295 xfs_da_buf_done(oldblk
->bp
);
296 xfs_da_buf_done(addblk
->bp
);
298 return(error
); /* GROT: dir is inconsistent */
302 * Update pointers to the node which used to be block 0 and
303 * just got bumped because of the addition of a new root node.
304 * There might be three blocks involved if a double split occurred,
305 * and the original block 0 could be at any position in the list.
308 node
= oldblk
->bp
->data
;
309 if (!INT_ISZERO(node
->hdr
.info
.forw
, ARCH_CONVERT
)) {
310 if (INT_GET(node
->hdr
.info
.forw
, ARCH_CONVERT
) == addblk
->blkno
) {
313 ASSERT(state
->extravalid
);
314 bp
= state
->extrablk
.bp
;
317 INT_SET(node
->hdr
.info
.back
, ARCH_CONVERT
, oldblk
->blkno
);
318 xfs_da_log_buf(state
->args
->trans
, bp
,
319 XFS_DA_LOGRANGE(node
, &node
->hdr
.info
,
320 sizeof(node
->hdr
.info
)));
322 node
= oldblk
->bp
->data
;
323 if (INT_GET(node
->hdr
.info
.back
, ARCH_CONVERT
)) {
324 if (INT_GET(node
->hdr
.info
.back
, ARCH_CONVERT
) == addblk
->blkno
) {
327 ASSERT(state
->extravalid
);
328 bp
= state
->extrablk
.bp
;
331 INT_SET(node
->hdr
.info
.forw
, ARCH_CONVERT
, oldblk
->blkno
);
332 xfs_da_log_buf(state
->args
->trans
, bp
,
333 XFS_DA_LOGRANGE(node
, &node
->hdr
.info
,
334 sizeof(node
->hdr
.info
)));
336 xfs_da_buf_done(oldblk
->bp
);
337 xfs_da_buf_done(addblk
->bp
);
343 * Split the root. We have to create a new root and point to the two
344 * parts (the split old root) that we just created. Copy block zero to
345 * the EOF, extending the inode in process.
347 STATIC
int /* error */
348 xfs_da_root_split(xfs_da_state_t
*state
, xfs_da_state_blk_t
*blk1
,
349 xfs_da_state_blk_t
*blk2
)
351 xfs_da_intnode_t
*node
, *oldroot
;
359 xfs_dir2_leaf_t
*leaf
;
362 * Copy the existing (incorrect) block from the root node position
363 * to a free space somewhere.
366 ASSERT(args
!= NULL
);
367 error
= xfs_da_grow_inode(args
, &blkno
);
373 error
= xfs_da_get_buf(tp
, dp
, blkno
, -1, &bp
, args
->whichfork
);
378 oldroot
= blk1
->bp
->data
;
379 if (INT_GET(oldroot
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) {
380 size
= (int)((char *)&oldroot
->btree
[INT_GET(oldroot
->hdr
.count
, ARCH_CONVERT
)] -
383 ASSERT(XFS_DIR_IS_V2(mp
));
384 ASSERT(INT_GET(oldroot
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
);
385 leaf
= (xfs_dir2_leaf_t
*)oldroot
;
386 size
= (int)((char *)&leaf
->ents
[INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)] -
389 memcpy(node
, oldroot
, size
);
390 xfs_da_log_buf(tp
, bp
, 0, size
- 1);
391 xfs_da_buf_done(blk1
->bp
);
396 * Set up the new root node.
398 error
= xfs_da_node_create(args
,
399 args
->whichfork
== XFS_DATA_FORK
&&
400 XFS_DIR_IS_V2(mp
) ? mp
->m_dirleafblk
: 0,
401 INT_GET(node
->hdr
.level
, ARCH_CONVERT
) + 1, &bp
, args
->whichfork
);
405 INT_SET(node
->btree
[0].hashval
, ARCH_CONVERT
, blk1
->hashval
);
406 INT_SET(node
->btree
[0].before
, ARCH_CONVERT
, blk1
->blkno
);
407 INT_SET(node
->btree
[1].hashval
, ARCH_CONVERT
, blk2
->hashval
);
408 INT_SET(node
->btree
[1].before
, ARCH_CONVERT
, blk2
->blkno
);
409 INT_SET(node
->hdr
.count
, ARCH_CONVERT
, 2);
412 if (INT_GET(oldroot
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
) {
413 ASSERT(blk1
->blkno
>= mp
->m_dirleafblk
&&
414 blk1
->blkno
< mp
->m_dirfreeblk
);
415 ASSERT(blk2
->blkno
>= mp
->m_dirleafblk
&&
416 blk2
->blkno
< mp
->m_dirfreeblk
);
420 /* Header is already logged by xfs_da_node_create */
421 xfs_da_log_buf(tp
, bp
,
422 XFS_DA_LOGRANGE(node
, node
->btree
,
423 sizeof(xfs_da_node_entry_t
) * 2));
430 * Split the node, rebalance, then add the new entry.
432 STATIC
int /* error */
433 xfs_da_node_split(xfs_da_state_t
*state
, xfs_da_state_blk_t
*oldblk
,
434 xfs_da_state_blk_t
*newblk
,
435 xfs_da_state_blk_t
*addblk
,
436 int treelevel
, int *result
)
438 xfs_da_intnode_t
*node
;
443 node
= oldblk
->bp
->data
;
444 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
447 * With V2 the extra block is data or freespace.
449 useextra
= state
->extravalid
&& XFS_DIR_IS_V1(state
->mp
);
450 newcount
= 1 + useextra
;
452 * Do we have to split the node?
454 if ((INT_GET(node
->hdr
.count
, ARCH_CONVERT
) + newcount
) > state
->node_ents
) {
456 * Allocate a new node, add to the doubly linked chain of
457 * nodes, then move some of our excess entries into it.
459 error
= xfs_da_grow_inode(state
->args
, &blkno
);
461 return(error
); /* GROT: dir is inconsistent */
463 error
= xfs_da_node_create(state
->args
, blkno
, treelevel
,
464 &newblk
->bp
, state
->args
->whichfork
);
466 return(error
); /* GROT: dir is inconsistent */
467 newblk
->blkno
= blkno
;
468 newblk
->magic
= XFS_DA_NODE_MAGIC
;
469 xfs_da_node_rebalance(state
, oldblk
, newblk
);
470 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
479 * Insert the new entry(s) into the correct block
480 * (updating last hashval in the process).
482 * xfs_da_node_add() inserts BEFORE the given index,
483 * and as a result of using node_lookup_int() we always
484 * point to a valid entry (not after one), but a split
485 * operation always results in a new block whose hashvals
486 * FOLLOW the current block.
488 * If we had double-split op below us, then add the extra block too.
490 node
= oldblk
->bp
->data
;
491 if (oldblk
->index
<= INT_GET(node
->hdr
.count
, ARCH_CONVERT
)) {
493 xfs_da_node_add(state
, oldblk
, addblk
);
495 if (state
->extraafter
)
497 xfs_da_node_add(state
, oldblk
, &state
->extrablk
);
498 state
->extravalid
= 0;
502 xfs_da_node_add(state
, newblk
, addblk
);
504 if (state
->extraafter
)
506 xfs_da_node_add(state
, newblk
, &state
->extrablk
);
507 state
->extravalid
= 0;
515 * Balance the btree elements between two intermediate nodes,
516 * usually one full and one empty.
518 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
521 xfs_da_node_rebalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*blk1
,
522 xfs_da_state_blk_t
*blk2
)
524 xfs_da_intnode_t
*node1
, *node2
, *tmpnode
;
525 xfs_da_node_entry_t
*btree_s
, *btree_d
;
529 node1
= blk1
->bp
->data
;
530 node2
= blk2
->bp
->data
;
532 * Figure out how many entries need to move, and in which direction.
533 * Swap the nodes around if that makes it simpler.
535 if ((INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) > 0) && (INT_GET(node2
->hdr
.count
, ARCH_CONVERT
) > 0) &&
536 ((INT_GET(node2
->btree
[ 0 ].hashval
, ARCH_CONVERT
) < INT_GET(node1
->btree
[ 0 ].hashval
, ARCH_CONVERT
)) ||
537 (INT_GET(node2
->btree
[ INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
) <
538 INT_GET(node1
->btree
[ INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
)))) {
543 ASSERT(INT_GET(node1
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
544 ASSERT(INT_GET(node2
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
545 count
= (INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) - INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)) / 2;
548 tp
= state
->args
->trans
;
550 * Two cases: high-to-low and low-to-high.
554 * Move elements in node2 up to make a hole.
556 if ((tmp
= INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)) > 0) {
557 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
558 btree_s
= &node2
->btree
[0];
559 btree_d
= &node2
->btree
[count
];
560 memmove(btree_d
, btree_s
, tmp
);
564 * Move the req'd B-tree elements from high in node1 to
567 INT_MOD(node2
->hdr
.count
, ARCH_CONVERT
, count
);
568 tmp
= count
* (uint
)sizeof(xfs_da_node_entry_t
);
569 btree_s
= &node1
->btree
[INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) - count
];
570 btree_d
= &node2
->btree
[0];
571 memcpy(btree_d
, btree_s
, tmp
);
572 INT_MOD(node1
->hdr
.count
, ARCH_CONVERT
, -(count
));
576 * Move the req'd B-tree elements from low in node2 to
580 tmp
= count
* (uint
)sizeof(xfs_da_node_entry_t
);
581 btree_s
= &node2
->btree
[0];
582 btree_d
= &node1
->btree
[INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)];
583 memcpy(btree_d
, btree_s
, tmp
);
584 INT_MOD(node1
->hdr
.count
, ARCH_CONVERT
, count
);
585 xfs_da_log_buf(tp
, blk1
->bp
,
586 XFS_DA_LOGRANGE(node1
, btree_d
, tmp
));
589 * Move elements in node2 down to fill the hole.
591 tmp
= INT_GET(node2
->hdr
.count
, ARCH_CONVERT
) - count
;
592 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
593 btree_s
= &node2
->btree
[count
];
594 btree_d
= &node2
->btree
[0];
595 memmove(btree_d
, btree_s
, tmp
);
596 INT_MOD(node2
->hdr
.count
, ARCH_CONVERT
, -(count
));
600 * Log header of node 1 and all current bits of node 2.
602 xfs_da_log_buf(tp
, blk1
->bp
,
603 XFS_DA_LOGRANGE(node1
, &node1
->hdr
, sizeof(node1
->hdr
)));
604 xfs_da_log_buf(tp
, blk2
->bp
,
605 XFS_DA_LOGRANGE(node2
, &node2
->hdr
,
607 sizeof(node2
->btree
[0]) * INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)));
610 * Record the last hashval from each block for upward propagation.
611 * (note: don't use the swapped node pointers)
613 node1
= blk1
->bp
->data
;
614 node2
= blk2
->bp
->data
;
615 blk1
->hashval
= INT_GET(node1
->btree
[ INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
616 blk2
->hashval
= INT_GET(node2
->btree
[ INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
619 * Adjust the expected index for insertion.
621 if (blk1
->index
>= INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)) {
622 blk2
->index
= blk1
->index
- INT_GET(node1
->hdr
.count
, ARCH_CONVERT
);
623 blk1
->index
= INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) + 1; /* make it invalid */
628 * Add a new entry to an intermediate node.
631 xfs_da_node_add(xfs_da_state_t
*state
, xfs_da_state_blk_t
*oldblk
,
632 xfs_da_state_blk_t
*newblk
)
634 xfs_da_intnode_t
*node
;
635 xfs_da_node_entry_t
*btree
;
639 node
= oldblk
->bp
->data
;
641 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
642 ASSERT((oldblk
->index
>= 0) && (oldblk
->index
<= INT_GET(node
->hdr
.count
, ARCH_CONVERT
)));
643 ASSERT(newblk
->blkno
!= 0);
644 if (state
->args
->whichfork
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
))
645 ASSERT(newblk
->blkno
>= mp
->m_dirleafblk
&&
646 newblk
->blkno
< mp
->m_dirfreeblk
);
649 * We may need to make some room before we insert the new node.
652 btree
= &node
->btree
[ oldblk
->index
];
653 if (oldblk
->index
< INT_GET(node
->hdr
.count
, ARCH_CONVERT
)) {
654 tmp
= (INT_GET(node
->hdr
.count
, ARCH_CONVERT
) - oldblk
->index
) * (uint
)sizeof(*btree
);
655 memmove(btree
+ 1, btree
, tmp
);
657 INT_SET(btree
->hashval
, ARCH_CONVERT
, newblk
->hashval
);
658 INT_SET(btree
->before
, ARCH_CONVERT
, newblk
->blkno
);
659 xfs_da_log_buf(state
->args
->trans
, oldblk
->bp
,
660 XFS_DA_LOGRANGE(node
, btree
, tmp
+ sizeof(*btree
)));
661 INT_MOD(node
->hdr
.count
, ARCH_CONVERT
, +1);
662 xfs_da_log_buf(state
->args
->trans
, oldblk
->bp
,
663 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
666 * Copy the last hash value from the oldblk to propagate upwards.
668 oldblk
->hashval
= INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
671 /*========================================================================
672 * Routines used for shrinking the Btree.
673 *========================================================================*/
676 * Deallocate an empty leaf node, remove it from its parent,
677 * possibly deallocating that block, etc...
680 xfs_da_join(xfs_da_state_t
*state
)
682 xfs_da_state_blk_t
*drop_blk
, *save_blk
;
686 drop_blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
687 save_blk
= &state
->altpath
.blk
[ state
->path
.active
-1 ];
688 ASSERT(state
->path
.blk
[0].magic
== XFS_DA_NODE_MAGIC
);
689 ASSERT(drop_blk
->magic
== XFS_ATTR_LEAF_MAGIC
||
690 drop_blk
->magic
== XFS_DIRX_LEAF_MAGIC(state
->mp
));
693 * Walk back up the tree joining/deallocating as necessary.
694 * When we stop dropping blocks, break out.
696 for ( ; state
->path
.active
>= 2; drop_blk
--, save_blk
--,
697 state
->path
.active
--) {
699 * See if we can combine the block with a neighbor.
700 * (action == 0) => no options, just leave
701 * (action == 1) => coalesce, then unlink
702 * (action == 2) => block empty, unlink it
704 switch (drop_blk
->magic
) {
705 case XFS_ATTR_LEAF_MAGIC
:
709 error
= xfs_attr_leaf_toosmall(state
, &action
);
716 xfs_attr_leaf_unbalance(state
, drop_blk
, save_blk
);
719 case XFS_DIR_LEAF_MAGIC
:
720 ASSERT(XFS_DIR_IS_V1(state
->mp
));
721 error
= xfs_dir_leaf_toosmall(state
, &action
);
726 xfs_dir_leaf_unbalance(state
, drop_blk
, save_blk
);
728 case XFS_DIR2_LEAFN_MAGIC
:
729 ASSERT(XFS_DIR_IS_V2(state
->mp
));
730 error
= xfs_dir2_leafn_toosmall(state
, &action
);
735 xfs_dir2_leafn_unbalance(state
, drop_blk
, save_blk
);
737 case XFS_DA_NODE_MAGIC
:
739 * Remove the offending node, fixup hashvals,
740 * check for a toosmall neighbor.
742 xfs_da_node_remove(state
, drop_blk
);
743 xfs_da_fixhashpath(state
, &state
->path
);
744 error
= xfs_da_node_toosmall(state
, &action
);
749 xfs_da_node_unbalance(state
, drop_blk
, save_blk
);
752 xfs_da_fixhashpath(state
, &state
->altpath
);
753 error
= xfs_da_blk_unlink(state
, drop_blk
, save_blk
);
754 xfs_da_state_kill_altpath(state
);
757 error
= xfs_da_shrink_inode(state
->args
, drop_blk
->blkno
,
764 * We joined all the way to the top. If it turns out that
765 * we only have one entry in the root, make the child block
768 xfs_da_node_remove(state
, drop_blk
);
769 xfs_da_fixhashpath(state
, &state
->path
);
770 error
= xfs_da_root_join(state
, &state
->path
.blk
[0]);
775 * We have only one entry in the root. Copy the only remaining child of
776 * the old root to block 0 as the new root node.
779 xfs_da_root_join(xfs_da_state_t
*state
, xfs_da_state_blk_t
*root_blk
)
781 xfs_da_intnode_t
*oldroot
;
783 xfs_da_blkinfo_t
*blkinfo
;
790 ASSERT(args
!= NULL
);
791 ASSERT(root_blk
->magic
== XFS_DA_NODE_MAGIC
);
792 oldroot
= root_blk
->bp
->data
;
793 ASSERT(INT_GET(oldroot
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
794 ASSERT(INT_ISZERO(oldroot
->hdr
.info
.forw
, ARCH_CONVERT
));
795 ASSERT(INT_ISZERO(oldroot
->hdr
.info
.back
, ARCH_CONVERT
));
798 * If the root has more than one child, then don't do anything.
800 if (INT_GET(oldroot
->hdr
.count
, ARCH_CONVERT
) > 1)
804 * Read in the (only) child block, then copy those bytes into
805 * the root block's buffer and free the original child block.
807 child
= INT_GET(oldroot
->btree
[ 0 ].before
, ARCH_CONVERT
);
809 error
= xfs_da_read_buf(args
->trans
, args
->dp
, child
, -1, &bp
,
815 if (INT_GET(oldroot
->hdr
.level
, ARCH_CONVERT
) == 1) {
816 ASSERT(INT_GET(blkinfo
->magic
, ARCH_CONVERT
) == XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
817 INT_GET(blkinfo
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
);
819 ASSERT(INT_GET(blkinfo
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
821 ASSERT(INT_ISZERO(blkinfo
->forw
, ARCH_CONVERT
));
822 ASSERT(INT_ISZERO(blkinfo
->back
, ARCH_CONVERT
));
823 memcpy(root_blk
->bp
->data
, bp
->data
, state
->blocksize
);
824 xfs_da_log_buf(args
->trans
, root_blk
->bp
, 0, state
->blocksize
- 1);
825 error
= xfs_da_shrink_inode(args
, child
, bp
);
830 * Check a node block and its neighbors to see if the block should be
831 * collapsed into one or the other neighbor. Always keep the block
832 * with the smaller block number.
833 * If the current block is over 50% full, don't try to join it, return 0.
834 * If the block is empty, fill in the state structure and return 2.
835 * If it can be collapsed, fill in the state structure and return 1.
836 * If nothing can be done, return 0.
839 xfs_da_node_toosmall(xfs_da_state_t
*state
, int *action
)
841 xfs_da_intnode_t
*node
;
842 xfs_da_state_blk_t
*blk
;
843 xfs_da_blkinfo_t
*info
;
844 int count
, forward
, error
, retval
, i
;
849 * Check for the degenerate case of the block being over 50% full.
850 * If so, it's not worth even looking to see if we might be able
851 * to coalesce with a sibling.
853 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
854 info
= blk
->bp
->data
;
855 ASSERT(INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
856 node
= (xfs_da_intnode_t
*)info
;
857 count
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
858 if (count
> (state
->node_ents
>> 1)) {
859 *action
= 0; /* blk over 50%, don't try to join */
860 return(0); /* blk over 50%, don't try to join */
864 * Check for the degenerate case of the block being empty.
865 * If the block is empty, we'll simply delete it, no need to
866 * coalesce it with a sibling block. We choose (aribtrarily)
867 * to merge with the forward block unless it is NULL.
871 * Make altpath point to the block we want to keep and
872 * path point to the block we want to drop (this one).
874 forward
= (!INT_ISZERO(info
->forw
, ARCH_CONVERT
));
875 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
876 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
889 * Examine each sibling block to see if we can coalesce with
890 * at least 25% free space to spare. We need to figure out
891 * whether to merge with the forward or the backward block.
892 * We prefer coalescing with the lower numbered sibling so as
893 * to shrink a directory over time.
895 /* start with smaller blk num */
896 forward
= (INT_GET(info
->forw
, ARCH_CONVERT
)
897 < INT_GET(info
->back
, ARCH_CONVERT
));
898 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
900 blkno
= INT_GET(info
->forw
, ARCH_CONVERT
);
902 blkno
= INT_GET(info
->back
, ARCH_CONVERT
);
905 error
= xfs_da_read_buf(state
->args
->trans
, state
->args
->dp
,
906 blkno
, -1, &bp
, state
->args
->whichfork
);
911 node
= (xfs_da_intnode_t
*)info
;
912 count
= state
->node_ents
;
913 count
-= state
->node_ents
>> 2;
914 count
-= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
916 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
917 count
-= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
918 xfs_da_brelse(state
->args
->trans
, bp
);
920 break; /* fits with at least 25% to spare */
928 * Make altpath point to the block we want to keep (the lower
929 * numbered block) and path point to the block we want to drop.
931 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
932 if (blkno
< blk
->blkno
) {
933 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
943 error
= xfs_da_path_shift(state
, &state
->path
, forward
,
958 * Walk back up the tree adjusting hash values as necessary,
959 * when we stop making changes, return.
962 xfs_da_fixhashpath(xfs_da_state_t
*state
, xfs_da_state_path_t
*path
)
964 xfs_da_state_blk_t
*blk
;
965 xfs_da_intnode_t
*node
;
966 xfs_da_node_entry_t
*btree
;
967 xfs_dahash_t lasthash
=0;
970 level
= path
->active
-1;
971 blk
= &path
->blk
[ level
];
972 switch (blk
->magic
) {
974 case XFS_ATTR_LEAF_MAGIC
:
975 lasthash
= xfs_attr_leaf_lasthash(blk
->bp
, &count
);
980 case XFS_DIR_LEAF_MAGIC
:
981 ASSERT(XFS_DIR_IS_V1(state
->mp
));
982 lasthash
= xfs_dir_leaf_lasthash(blk
->bp
, &count
);
986 case XFS_DIR2_LEAFN_MAGIC
:
987 ASSERT(XFS_DIR_IS_V2(state
->mp
));
988 lasthash
= xfs_dir2_leafn_lasthash(blk
->bp
, &count
);
992 case XFS_DA_NODE_MAGIC
:
993 lasthash
= xfs_da_node_lasthash(blk
->bp
, &count
);
998 for (blk
--, level
--; level
>= 0; blk
--, level
--) {
999 node
= blk
->bp
->data
;
1000 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1001 btree
= &node
->btree
[ blk
->index
];
1002 if (INT_GET(btree
->hashval
, ARCH_CONVERT
) == lasthash
)
1004 blk
->hashval
= lasthash
;
1005 INT_SET(btree
->hashval
, ARCH_CONVERT
, lasthash
);
1006 xfs_da_log_buf(state
->args
->trans
, blk
->bp
,
1007 XFS_DA_LOGRANGE(node
, btree
, sizeof(*btree
)));
1009 lasthash
= INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
1014 * Remove an entry from an intermediate node.
1017 xfs_da_node_remove(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
)
1019 xfs_da_intnode_t
*node
;
1020 xfs_da_node_entry_t
*btree
;
1023 node
= drop_blk
->bp
->data
;
1024 ASSERT(drop_blk
->index
< INT_GET(node
->hdr
.count
, ARCH_CONVERT
));
1025 ASSERT(drop_blk
->index
>= 0);
1028 * Copy over the offending entry, or just zero it out.
1030 btree
= &node
->btree
[drop_blk
->index
];
1031 if (drop_blk
->index
< (INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1)) {
1032 tmp
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
) - drop_blk
->index
- 1;
1033 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
1034 memmove(btree
, btree
+ 1, tmp
);
1035 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
1036 XFS_DA_LOGRANGE(node
, btree
, tmp
));
1037 btree
= &node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ];
1039 memset((char *)btree
, 0, sizeof(xfs_da_node_entry_t
));
1040 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
1041 XFS_DA_LOGRANGE(node
, btree
, sizeof(*btree
)));
1042 INT_MOD(node
->hdr
.count
, ARCH_CONVERT
, -1);
1043 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
1044 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
1047 * Copy the last hash value from the block to propagate upwards.
1050 drop_blk
->hashval
= INT_GET(btree
->hashval
, ARCH_CONVERT
);
1054 * Unbalance the btree elements between two intermediate nodes,
1055 * move all Btree elements from one node into another.
1058 xfs_da_node_unbalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
,
1059 xfs_da_state_blk_t
*save_blk
)
1061 xfs_da_intnode_t
*drop_node
, *save_node
;
1062 xfs_da_node_entry_t
*btree
;
1066 drop_node
= drop_blk
->bp
->data
;
1067 save_node
= save_blk
->bp
->data
;
1068 ASSERT(INT_GET(drop_node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1069 ASSERT(INT_GET(save_node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1070 tp
= state
->args
->trans
;
1073 * If the dying block has lower hashvals, then move all the
1074 * elements in the remaining block up to make a hole.
1076 if ((INT_GET(drop_node
->btree
[ 0 ].hashval
, ARCH_CONVERT
) < INT_GET(save_node
->btree
[ 0 ].hashval
, ARCH_CONVERT
)) ||
1077 (INT_GET(drop_node
->btree
[ INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
) <
1078 INT_GET(save_node
->btree
[ INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
)))
1080 btree
= &save_node
->btree
[ INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
) ];
1081 tmp
= INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
) * (uint
)sizeof(xfs_da_node_entry_t
);
1082 memmove(btree
, &save_node
->btree
[0], tmp
);
1083 btree
= &save_node
->btree
[0];
1084 xfs_da_log_buf(tp
, save_blk
->bp
,
1085 XFS_DA_LOGRANGE(save_node
, btree
,
1086 (INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
) + INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
)) *
1087 sizeof(xfs_da_node_entry_t
)));
1089 btree
= &save_node
->btree
[ INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
) ];
1090 xfs_da_log_buf(tp
, save_blk
->bp
,
1091 XFS_DA_LOGRANGE(save_node
, btree
,
1092 INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
) *
1093 sizeof(xfs_da_node_entry_t
)));
1097 * Move all the B-tree elements from drop_blk to save_blk.
1099 tmp
= INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
) * (uint
)sizeof(xfs_da_node_entry_t
);
1100 memcpy(btree
, &drop_node
->btree
[0], tmp
);
1101 INT_MOD(save_node
->hdr
.count
, ARCH_CONVERT
, INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
));
1103 xfs_da_log_buf(tp
, save_blk
->bp
,
1104 XFS_DA_LOGRANGE(save_node
, &save_node
->hdr
,
1105 sizeof(save_node
->hdr
)));
1108 * Save the last hashval in the remaining block for upward propagation.
1110 save_blk
->hashval
= INT_GET(save_node
->btree
[ INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
1113 /*========================================================================
1114 * Routines used for finding things in the Btree.
1115 *========================================================================*/
1118 * Walk down the Btree looking for a particular filename, filling
1119 * in the state structure as we go.
1121 * We will set the state structure to point to each of the elements
1122 * in each of the nodes where either the hashval is or should be.
1124 * We support duplicate hashval's so for each entry in the current
1125 * node that could contain the desired hashval, descend. This is a
1126 * pruned depth-first tree search.
1129 xfs_da_node_lookup_int(xfs_da_state_t
*state
, int *result
)
1131 xfs_da_state_blk_t
*blk
;
1132 xfs_da_blkinfo_t
*curr
;
1133 xfs_da_intnode_t
*node
;
1134 xfs_da_node_entry_t
*btree
;
1136 int probe
, span
, max
, error
, retval
;
1137 xfs_dahash_t hashval
;
1138 xfs_da_args_t
*args
;
1143 * Descend thru the B-tree searching each level for the right
1144 * node to use, until the right hashval is found.
1146 if (args
->whichfork
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(state
->mp
))
1147 blkno
= state
->mp
->m_dirleafblk
;
1150 for (blk
= &state
->path
.blk
[0], state
->path
.active
= 1;
1151 state
->path
.active
<= XFS_DA_NODE_MAXDEPTH
;
1152 blk
++, state
->path
.active
++) {
1154 * Read the next node down in the tree.
1157 error
= xfs_da_read_buf(args
->trans
, args
->dp
, blkno
,
1158 -1, &blk
->bp
, args
->whichfork
);
1161 state
->path
.active
--;
1164 curr
= blk
->bp
->data
;
1165 ASSERT(INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
||
1166 INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
1167 INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
);
1170 * Search an intermediate node for a match.
1172 blk
->magic
= INT_GET(curr
->magic
, ARCH_CONVERT
);
1173 if (INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) {
1174 node
= blk
->bp
->data
;
1175 blk
->hashval
= INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
1178 * Binary search. (note: small blocks will skip loop)
1180 max
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
1181 probe
= span
= max
/ 2;
1182 hashval
= args
->hashval
;
1183 for (btree
= &node
->btree
[probe
]; span
> 4;
1184 btree
= &node
->btree
[probe
]) {
1186 if (INT_GET(btree
->hashval
, ARCH_CONVERT
) < hashval
)
1188 else if (INT_GET(btree
->hashval
, ARCH_CONVERT
) > hashval
)
1193 ASSERT((probe
>= 0) && (probe
< max
));
1194 ASSERT((span
<= 4) || (INT_GET(btree
->hashval
, ARCH_CONVERT
) == hashval
));
1197 * Since we may have duplicate hashval's, find the first
1198 * matching hashval in the node.
1200 while ((probe
> 0) && (INT_GET(btree
->hashval
, ARCH_CONVERT
) >= hashval
)) {
1204 while ((probe
< max
) && (INT_GET(btree
->hashval
, ARCH_CONVERT
) < hashval
)) {
1210 * Pick the right block to descend on.
1214 blkno
= INT_GET(node
->btree
[ max
-1 ].before
, ARCH_CONVERT
);
1217 blkno
= INT_GET(btree
->before
, ARCH_CONVERT
);
1221 else if (INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
) {
1222 blk
->hashval
= xfs_attr_leaf_lasthash(blk
->bp
, NULL
);
1226 else if (INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DIR_LEAF_MAGIC
) {
1227 blk
->hashval
= xfs_dir_leaf_lasthash(blk
->bp
, NULL
);
1230 else if (INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
) {
1231 blk
->hashval
= xfs_dir2_leafn_lasthash(blk
->bp
, NULL
);
1237 * A leaf block that ends in the hashval that we are interested in
1238 * (final hashval == search hashval) means that the next block may
1239 * contain more entries with the same hashval, shift upward to the
1240 * next leaf and keep searching.
1243 if (blk
->magic
== XFS_DIR_LEAF_MAGIC
) {
1244 ASSERT(XFS_DIR_IS_V1(state
->mp
));
1245 retval
= xfs_dir_leaf_lookup_int(blk
->bp
, args
,
1247 } else if (blk
->magic
== XFS_DIR2_LEAFN_MAGIC
) {
1248 ASSERT(XFS_DIR_IS_V2(state
->mp
));
1249 retval
= xfs_dir2_leafn_lookup_int(blk
->bp
, args
,
1250 &blk
->index
, state
);
1253 else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1254 retval
= xfs_attr_leaf_lookup_int(blk
->bp
, args
);
1255 blk
->index
= args
->index
;
1256 args
->blkno
= blk
->blkno
;
1259 if (((retval
== ENOENT
) || (retval
== ENOATTR
)) &&
1260 (blk
->hashval
== args
->hashval
)) {
1261 error
= xfs_da_path_shift(state
, &state
->path
, 1, 1,
1269 else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1270 /* path_shift() gives ENOENT */
1271 retval
= XFS_ERROR(ENOATTR
);
1281 /*========================================================================
1283 *========================================================================*/
1286 * Link a new block into a doubly linked list of blocks (of whatever type).
1289 xfs_da_blk_link(xfs_da_state_t
*state
, xfs_da_state_blk_t
*old_blk
,
1290 xfs_da_state_blk_t
*new_blk
)
1292 xfs_da_blkinfo_t
*old_info
, *new_info
, *tmp_info
;
1293 xfs_da_args_t
*args
;
1294 int before
=0, error
;
1298 * Set up environment.
1301 ASSERT(args
!= NULL
);
1302 old_info
= old_blk
->bp
->data
;
1303 new_info
= new_blk
->bp
->data
;
1304 ASSERT(old_blk
->magic
== XFS_DA_NODE_MAGIC
||
1305 old_blk
->magic
== XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
1306 old_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1307 ASSERT(old_blk
->magic
== INT_GET(old_info
->magic
, ARCH_CONVERT
));
1308 ASSERT(new_blk
->magic
== INT_GET(new_info
->magic
, ARCH_CONVERT
));
1309 ASSERT(old_blk
->magic
== new_blk
->magic
);
1311 switch (old_blk
->magic
) {
1313 case XFS_ATTR_LEAF_MAGIC
:
1314 before
= xfs_attr_leaf_order(old_blk
->bp
, new_blk
->bp
);
1317 case XFS_DIR_LEAF_MAGIC
:
1318 ASSERT(XFS_DIR_IS_V1(state
->mp
));
1319 before
= xfs_dir_leaf_order(old_blk
->bp
, new_blk
->bp
);
1321 case XFS_DIR2_LEAFN_MAGIC
:
1322 ASSERT(XFS_DIR_IS_V2(state
->mp
));
1323 before
= xfs_dir2_leafn_order(old_blk
->bp
, new_blk
->bp
);
1325 case XFS_DA_NODE_MAGIC
:
1326 before
= xfs_da_node_order(old_blk
->bp
, new_blk
->bp
);
1331 * Link blocks in appropriate order.
1335 * Link new block in before existing block.
1337 INT_SET(new_info
->forw
, ARCH_CONVERT
, old_blk
->blkno
);
1338 new_info
->back
= old_info
->back
; /* INT_: direct copy */
1339 if (INT_GET(old_info
->back
, ARCH_CONVERT
)) {
1340 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1341 INT_GET(old_info
->back
,
1342 ARCH_CONVERT
), -1, &bp
,
1347 tmp_info
= bp
->data
;
1348 ASSERT(INT_GET(tmp_info
->magic
, ARCH_CONVERT
) == INT_GET(old_info
->magic
, ARCH_CONVERT
));
1349 ASSERT(INT_GET(tmp_info
->forw
, ARCH_CONVERT
) == old_blk
->blkno
);
1350 INT_SET(tmp_info
->forw
, ARCH_CONVERT
, new_blk
->blkno
);
1351 xfs_da_log_buf(args
->trans
, bp
, 0, sizeof(*tmp_info
)-1);
1352 xfs_da_buf_done(bp
);
1354 INT_SET(old_info
->back
, ARCH_CONVERT
, new_blk
->blkno
);
1357 * Link new block in after existing block.
1359 new_info
->forw
= old_info
->forw
; /* INT_: direct copy */
1360 INT_SET(new_info
->back
, ARCH_CONVERT
, old_blk
->blkno
);
1361 if (INT_GET(old_info
->forw
, ARCH_CONVERT
)) {
1362 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1363 INT_GET(old_info
->forw
, ARCH_CONVERT
), -1, &bp
,
1368 tmp_info
= bp
->data
;
1369 ASSERT(INT_GET(tmp_info
->magic
, ARCH_CONVERT
)
1370 == INT_GET(old_info
->magic
, ARCH_CONVERT
));
1371 ASSERT(INT_GET(tmp_info
->back
, ARCH_CONVERT
)
1373 INT_SET(tmp_info
->back
, ARCH_CONVERT
, new_blk
->blkno
);
1374 xfs_da_log_buf(args
->trans
, bp
, 0, sizeof(*tmp_info
)-1);
1375 xfs_da_buf_done(bp
);
1377 INT_SET(old_info
->forw
, ARCH_CONVERT
, new_blk
->blkno
);
1380 xfs_da_log_buf(args
->trans
, old_blk
->bp
, 0, sizeof(*tmp_info
) - 1);
1381 xfs_da_log_buf(args
->trans
, new_blk
->bp
, 0, sizeof(*tmp_info
) - 1);
1386 * Compare two intermediate nodes for "order".
1389 xfs_da_node_order(xfs_dabuf_t
*node1_bp
, xfs_dabuf_t
*node2_bp
)
1391 xfs_da_intnode_t
*node1
, *node2
;
1393 node1
= node1_bp
->data
;
1394 node2
= node2_bp
->data
;
1395 ASSERT((INT_GET(node1
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) &&
1396 (INT_GET(node2
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
));
1397 if ((INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) > 0) && (INT_GET(node2
->hdr
.count
, ARCH_CONVERT
) > 0) &&
1398 ((INT_GET(node2
->btree
[ 0 ].hashval
, ARCH_CONVERT
) <
1399 INT_GET(node1
->btree
[ 0 ].hashval
, ARCH_CONVERT
)) ||
1400 (INT_GET(node2
->btree
[ INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
) <
1401 INT_GET(node1
->btree
[ INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
)))) {
1408 * Pick up the last hashvalue from an intermediate node.
1411 xfs_da_node_lasthash(xfs_dabuf_t
*bp
, int *count
)
1413 xfs_da_intnode_t
*node
;
1416 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1418 *count
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
1419 if (INT_ISZERO(node
->hdr
.count
, ARCH_CONVERT
))
1421 return(INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
));
1425 * Unlink a block from a doubly linked list of blocks.
1428 xfs_da_blk_unlink(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
,
1429 xfs_da_state_blk_t
*save_blk
)
1431 xfs_da_blkinfo_t
*drop_info
, *save_info
, *tmp_info
;
1432 xfs_da_args_t
*args
;
1437 * Set up environment.
1440 ASSERT(args
!= NULL
);
1441 save_info
= save_blk
->bp
->data
;
1442 drop_info
= drop_blk
->bp
->data
;
1443 ASSERT(save_blk
->magic
== XFS_DA_NODE_MAGIC
||
1444 save_blk
->magic
== XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
1445 save_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1446 ASSERT(save_blk
->magic
== INT_GET(save_info
->magic
, ARCH_CONVERT
));
1447 ASSERT(drop_blk
->magic
== INT_GET(drop_info
->magic
, ARCH_CONVERT
));
1448 ASSERT(save_blk
->magic
== drop_blk
->magic
);
1449 ASSERT((INT_GET(save_info
->forw
, ARCH_CONVERT
) == drop_blk
->blkno
) ||
1450 (INT_GET(save_info
->back
, ARCH_CONVERT
) == drop_blk
->blkno
));
1451 ASSERT((INT_GET(drop_info
->forw
, ARCH_CONVERT
) == save_blk
->blkno
) ||
1452 (INT_GET(drop_info
->back
, ARCH_CONVERT
) == save_blk
->blkno
));
1455 * Unlink the leaf block from the doubly linked chain of leaves.
1457 if (INT_GET(save_info
->back
, ARCH_CONVERT
) == drop_blk
->blkno
) {
1458 save_info
->back
= drop_info
->back
; /* INT_: direct copy */
1459 if (INT_GET(drop_info
->back
, ARCH_CONVERT
)) {
1460 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1461 INT_GET(drop_info
->back
,
1462 ARCH_CONVERT
), -1, &bp
,
1467 tmp_info
= bp
->data
;
1468 ASSERT(INT_GET(tmp_info
->magic
, ARCH_CONVERT
) == INT_GET(save_info
->magic
, ARCH_CONVERT
));
1469 ASSERT(INT_GET(tmp_info
->forw
, ARCH_CONVERT
) == drop_blk
->blkno
);
1470 INT_SET(tmp_info
->forw
, ARCH_CONVERT
, save_blk
->blkno
);
1471 xfs_da_log_buf(args
->trans
, bp
, 0,
1472 sizeof(*tmp_info
) - 1);
1473 xfs_da_buf_done(bp
);
1476 save_info
->forw
= drop_info
->forw
; /* INT_: direct copy */
1477 if (INT_GET(drop_info
->forw
, ARCH_CONVERT
)) {
1478 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1479 INT_GET(drop_info
->forw
, ARCH_CONVERT
), -1, &bp
,
1484 tmp_info
= bp
->data
;
1485 ASSERT(INT_GET(tmp_info
->magic
, ARCH_CONVERT
)
1486 == INT_GET(save_info
->magic
, ARCH_CONVERT
));
1487 ASSERT(INT_GET(tmp_info
->back
, ARCH_CONVERT
)
1488 == drop_blk
->blkno
);
1489 INT_SET(tmp_info
->back
, ARCH_CONVERT
, save_blk
->blkno
);
1490 xfs_da_log_buf(args
->trans
, bp
, 0,
1491 sizeof(*tmp_info
) - 1);
1492 xfs_da_buf_done(bp
);
1496 xfs_da_log_buf(args
->trans
, save_blk
->bp
, 0, sizeof(*save_info
) - 1);
1501 * Move a path "forward" or "!forward" one block at the current level.
1503 * This routine will adjust a "path" to point to the next block
1504 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1505 * Btree, including updating pointers to the intermediate nodes between
1506 * the new bottom and the root.
1509 xfs_da_path_shift(xfs_da_state_t
*state
, xfs_da_state_path_t
*path
,
1510 int forward
, int release
, int *result
)
1512 xfs_da_state_blk_t
*blk
;
1513 xfs_da_blkinfo_t
*info
;
1514 xfs_da_intnode_t
*node
;
1515 xfs_da_args_t
*args
;
1516 xfs_dablk_t blkno
=0;
1520 * Roll up the Btree looking for the first block where our
1521 * current index is not at the edge of the block. Note that
1522 * we skip the bottom layer because we want the sibling block.
1525 ASSERT(args
!= NULL
);
1526 ASSERT(path
!= NULL
);
1527 ASSERT((path
->active
> 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1528 level
= (path
->active
-1) - 1; /* skip bottom layer in path */
1529 for (blk
= &path
->blk
[level
]; level
>= 0; blk
--, level
--) {
1530 ASSERT(blk
->bp
!= NULL
);
1531 node
= blk
->bp
->data
;
1532 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1533 if (forward
&& (blk
->index
< INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1)) {
1535 blkno
= INT_GET(node
->btree
[ blk
->index
].before
, ARCH_CONVERT
);
1537 } else if (!forward
&& (blk
->index
> 0)) {
1539 blkno
= INT_GET(node
->btree
[ blk
->index
].before
, ARCH_CONVERT
);
1544 *result
= XFS_ERROR(ENOENT
); /* we're out of our tree */
1545 ASSERT(args
->oknoent
);
1550 * Roll down the edge of the subtree until we reach the
1551 * same depth we were at originally.
1553 for (blk
++, level
++; level
< path
->active
; blk
++, level
++) {
1555 * Release the old block.
1556 * (if it's dirty, trans won't actually let go)
1559 xfs_da_brelse(args
->trans
, blk
->bp
);
1562 * Read the next child block.
1565 error
= xfs_da_read_buf(args
->trans
, args
->dp
, blkno
, -1,
1566 &blk
->bp
, args
->whichfork
);
1569 ASSERT(blk
->bp
!= NULL
);
1570 info
= blk
->bp
->data
;
1571 ASSERT(INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
||
1572 INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
1573 INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
);
1574 blk
->magic
= INT_GET(info
->magic
, ARCH_CONVERT
);
1575 if (INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) {
1576 node
= (xfs_da_intnode_t
*)info
;
1577 blk
->hashval
= INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
1581 blk
->index
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1;
1582 blkno
= INT_GET(node
->btree
[ blk
->index
].before
, ARCH_CONVERT
);
1584 ASSERT(level
== path
->active
-1);
1586 switch(blk
->magic
) {
1588 case XFS_ATTR_LEAF_MAGIC
:
1589 blk
->hashval
= xfs_attr_leaf_lasthash(blk
->bp
,
1593 case XFS_DIR_LEAF_MAGIC
:
1594 ASSERT(XFS_DIR_IS_V1(state
->mp
));
1595 blk
->hashval
= xfs_dir_leaf_lasthash(blk
->bp
,
1598 case XFS_DIR2_LEAFN_MAGIC
:
1599 ASSERT(XFS_DIR_IS_V2(state
->mp
));
1600 blk
->hashval
= xfs_dir2_leafn_lasthash(blk
->bp
,
1604 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
||
1606 XFS_DIRX_LEAF_MAGIC(state
->mp
));
1616 /*========================================================================
1618 *========================================================================*/
1621 * Implement a simple hash on a character string.
1622 * Rotate the hash value by 7 bits, then XOR each character in.
1623 * This is implemented with some source-level loop unrolling.
1626 xfs_da_hashname(uchar_t
*name
, int namelen
)
1630 #define ROTL(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
1633 * This is the old one-byte-at-a-time version.
1635 for (hash
= 0; namelen
> 0; namelen
--) {
1636 hash
= *name
++ ^ ROTL(hash
, 7);
1641 * Do four characters at a time as long as we can.
1643 for (hash
= 0; namelen
>= 4; namelen
-= 4, name
+= 4) {
1644 hash
= (name
[0] << 21) ^ (name
[1] << 14) ^ (name
[2] << 7) ^
1645 (name
[3] << 0) ^ ROTL(hash
, 7 * 4);
1648 * Now do the rest of the characters.
1652 return (name
[0] << 14) ^ (name
[1] << 7) ^ (name
[2] << 0) ^
1655 return (name
[0] << 7) ^ (name
[1] << 0) ^ ROTL(hash
, 7 * 2);
1657 return (name
[0] << 0) ^ ROTL(hash
, 7 * 1);
1664 return 0; /* keep gcc happy */
1668 * Add a block to the btree ahead of the file.
1669 * Return the new block number to the caller.
1672 xfs_da_grow_inode(xfs_da_args_t
*args
, xfs_dablk_t
*new_blkno
)
1674 xfs_fileoff_t bno
, b
;
1675 xfs_bmbt_irec_t map
;
1676 xfs_bmbt_irec_t
*mapp
;
1678 int nmap
, error
, w
, count
, c
, got
, i
, mapi
;
1685 w
= args
->whichfork
;
1688 * For new directories adjust the file offset and block count.
1690 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
)) {
1691 bno
= mp
->m_dirleafblk
;
1692 count
= mp
->m_dirblkfsbs
;
1698 * Find a spot in the file space to put the new block.
1700 if ((error
= xfs_bmap_first_unused(tp
, dp
, count
, &bno
, w
))) {
1703 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
))
1704 ASSERT(bno
>= mp
->m_dirleafblk
&& bno
< mp
->m_dirfreeblk
);
1706 * Try mapping it in one filesystem block.
1709 ASSERT(args
->firstblock
!= NULL
);
1710 if ((error
= xfs_bmapi(tp
, dp
, bno
, count
,
1711 XFS_BMAPI_AFLAG(w
)|XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
|
1713 args
->firstblock
, args
->total
, &map
, &nmap
,
1723 * If we didn't get it and the block might work if fragmented,
1724 * try without the CONTIG flag. Loop until we get it all.
1726 else if (nmap
== 0 && count
> 1) {
1727 mapp
= kmem_alloc(sizeof(*mapp
) * count
, KM_SLEEP
);
1728 for (b
= bno
, mapi
= 0; b
< bno
+ count
; ) {
1729 nmap
= MIN(XFS_BMAP_MAX_NMAP
, count
);
1730 c
= (int)(bno
+ count
- b
);
1731 if ((error
= xfs_bmapi(tp
, dp
, b
, c
,
1732 XFS_BMAPI_AFLAG(w
)|XFS_BMAPI_WRITE
|
1734 args
->firstblock
, args
->total
,
1735 &mapp
[mapi
], &nmap
, args
->flist
))) {
1736 kmem_free(mapp
, sizeof(*mapp
) * count
);
1742 b
= mapp
[mapi
- 1].br_startoff
+
1743 mapp
[mapi
- 1].br_blockcount
;
1750 * Count the blocks we got, make sure it matches the total.
1752 for (i
= 0, got
= 0; i
< mapi
; i
++)
1753 got
+= mapp
[i
].br_blockcount
;
1754 if (got
!= count
|| mapp
[0].br_startoff
!= bno
||
1755 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
1758 kmem_free(mapp
, sizeof(*mapp
) * count
);
1759 return XFS_ERROR(ENOSPC
);
1762 kmem_free(mapp
, sizeof(*mapp
) * count
);
1763 *new_blkno
= (xfs_dablk_t
)bno
;
1765 * For version 1 directories, adjust the file size if it changed.
1767 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V1(mp
)) {
1769 if ((error
= xfs_bmap_last_offset(tp
, dp
, &bno
, w
)))
1771 size
= XFS_FSB_TO_B(mp
, bno
);
1772 if (size
!= dp
->i_d
.di_size
) {
1773 dp
->i_d
.di_size
= size
;
1774 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1781 * Ick. We need to always be able to remove a btree block, even
1782 * if there's no space reservation because the filesystem is full.
1783 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
1784 * It swaps the target block with the last block in the file. The
1785 * last block in the file can always be removed since it can't cause
1786 * a bmap btree split to do that.
1789 xfs_da_swap_lastblock(xfs_da_args_t
*args
, xfs_dablk_t
*dead_blknop
,
1790 xfs_dabuf_t
**dead_bufp
)
1792 xfs_dablk_t dead_blkno
, last_blkno
, sib_blkno
, par_blkno
;
1793 xfs_dabuf_t
*dead_buf
, *last_buf
, *sib_buf
, *par_buf
;
1794 xfs_fileoff_t lastoff
;
1798 int error
, w
, entno
, level
, dead_level
;
1799 xfs_da_blkinfo_t
*dead_info
, *sib_info
;
1800 xfs_da_intnode_t
*par_node
, *dead_node
;
1801 xfs_dir_leafblock_t
*dead_leaf
;
1802 xfs_dir2_leaf_t
*dead_leaf2
;
1803 xfs_dahash_t dead_hash
;
1805 dead_buf
= *dead_bufp
;
1806 dead_blkno
= *dead_blknop
;
1809 w
= args
->whichfork
;
1810 ASSERT(w
== XFS_DATA_FORK
);
1812 if (XFS_DIR_IS_V2(mp
)) {
1813 lastoff
= mp
->m_dirfreeblk
;
1814 error
= xfs_bmap_last_before(tp
, ip
, &lastoff
, w
);
1816 error
= xfs_bmap_last_offset(tp
, ip
, &lastoff
, w
);
1819 if (unlikely(lastoff
== 0)) {
1820 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW
,
1822 return XFS_ERROR(EFSCORRUPTED
);
1825 * Read the last block in the btree space.
1827 last_blkno
= (xfs_dablk_t
)lastoff
- mp
->m_dirblkfsbs
;
1828 if ((error
= xfs_da_read_buf(tp
, ip
, last_blkno
, -1, &last_buf
, w
)))
1831 * Copy the last block into the dead buffer and log it.
1833 memcpy(dead_buf
->data
, last_buf
->data
, mp
->m_dirblksize
);
1834 xfs_da_log_buf(tp
, dead_buf
, 0, mp
->m_dirblksize
- 1);
1835 dead_info
= dead_buf
->data
;
1837 * Get values from the moved block.
1839 if (INT_GET(dead_info
->magic
, ARCH_CONVERT
) == XFS_DIR_LEAF_MAGIC
) {
1840 ASSERT(XFS_DIR_IS_V1(mp
));
1841 dead_leaf
= (xfs_dir_leafblock_t
*)dead_info
;
1844 INT_GET(dead_leaf
->entries
[INT_GET(dead_leaf
->hdr
.count
, ARCH_CONVERT
) - 1].hashval
, ARCH_CONVERT
);
1845 } else if (INT_GET(dead_info
->magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
) {
1846 ASSERT(XFS_DIR_IS_V2(mp
));
1847 dead_leaf2
= (xfs_dir2_leaf_t
*)dead_info
;
1849 dead_hash
= INT_GET(dead_leaf2
->ents
[INT_GET(dead_leaf2
->hdr
.count
, ARCH_CONVERT
) - 1].hashval
, ARCH_CONVERT
);
1851 ASSERT(INT_GET(dead_info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1852 dead_node
= (xfs_da_intnode_t
*)dead_info
;
1853 dead_level
= INT_GET(dead_node
->hdr
.level
, ARCH_CONVERT
);
1854 dead_hash
= INT_GET(dead_node
->btree
[INT_GET(dead_node
->hdr
.count
, ARCH_CONVERT
) - 1].hashval
, ARCH_CONVERT
);
1856 sib_buf
= par_buf
= NULL
;
1858 * If the moved block has a left sibling, fix up the pointers.
1860 if ((sib_blkno
= INT_GET(dead_info
->back
, ARCH_CONVERT
))) {
1861 if ((error
= xfs_da_read_buf(tp
, ip
, sib_blkno
, -1, &sib_buf
, w
)))
1863 sib_info
= sib_buf
->data
;
1865 INT_GET(sib_info
->forw
, ARCH_CONVERT
) != last_blkno
||
1866 INT_GET(sib_info
->magic
, ARCH_CONVERT
) != INT_GET(dead_info
->magic
, ARCH_CONVERT
))) {
1867 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
1868 XFS_ERRLEVEL_LOW
, mp
);
1869 error
= XFS_ERROR(EFSCORRUPTED
);
1872 INT_SET(sib_info
->forw
, ARCH_CONVERT
, dead_blkno
);
1873 xfs_da_log_buf(tp
, sib_buf
,
1874 XFS_DA_LOGRANGE(sib_info
, &sib_info
->forw
,
1875 sizeof(sib_info
->forw
)));
1876 xfs_da_buf_done(sib_buf
);
1880 * If the moved block has a right sibling, fix up the pointers.
1882 if ((sib_blkno
= INT_GET(dead_info
->forw
, ARCH_CONVERT
))) {
1883 if ((error
= xfs_da_read_buf(tp
, ip
, sib_blkno
, -1, &sib_buf
, w
)))
1885 sib_info
= sib_buf
->data
;
1887 INT_GET(sib_info
->back
, ARCH_CONVERT
) != last_blkno
1888 || INT_GET(sib_info
->magic
, ARCH_CONVERT
)
1889 != INT_GET(dead_info
->magic
, ARCH_CONVERT
))) {
1890 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
1891 XFS_ERRLEVEL_LOW
, mp
);
1892 error
= XFS_ERROR(EFSCORRUPTED
);
1895 INT_SET(sib_info
->back
, ARCH_CONVERT
, dead_blkno
);
1896 xfs_da_log_buf(tp
, sib_buf
,
1897 XFS_DA_LOGRANGE(sib_info
, &sib_info
->back
,
1898 sizeof(sib_info
->back
)));
1899 xfs_da_buf_done(sib_buf
);
1902 par_blkno
= XFS_DIR_IS_V1(mp
) ? 0 : mp
->m_dirleafblk
;
1905 * Walk down the tree looking for the parent of the moved block.
1908 if ((error
= xfs_da_read_buf(tp
, ip
, par_blkno
, -1, &par_buf
, w
)))
1910 par_node
= par_buf
->data
;
1912 INT_GET(par_node
->hdr
.info
.magic
, ARCH_CONVERT
) != XFS_DA_NODE_MAGIC
||
1913 (level
>= 0 && level
!= INT_GET(par_node
->hdr
.level
, ARCH_CONVERT
) + 1))) {
1914 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
1915 XFS_ERRLEVEL_LOW
, mp
);
1916 error
= XFS_ERROR(EFSCORRUPTED
);
1919 level
= INT_GET(par_node
->hdr
.level
, ARCH_CONVERT
);
1921 entno
< INT_GET(par_node
->hdr
.count
, ARCH_CONVERT
) &&
1922 INT_GET(par_node
->btree
[entno
].hashval
, ARCH_CONVERT
) < dead_hash
;
1925 if (unlikely(entno
== INT_GET(par_node
->hdr
.count
, ARCH_CONVERT
))) {
1926 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
1927 XFS_ERRLEVEL_LOW
, mp
);
1928 error
= XFS_ERROR(EFSCORRUPTED
);
1931 par_blkno
= INT_GET(par_node
->btree
[entno
].before
, ARCH_CONVERT
);
1932 if (level
== dead_level
+ 1)
1934 xfs_da_brelse(tp
, par_buf
);
1938 * We're in the right parent block.
1939 * Look for the right entry.
1943 entno
< INT_GET(par_node
->hdr
.count
, ARCH_CONVERT
) &&
1944 INT_GET(par_node
->btree
[entno
].before
, ARCH_CONVERT
) != last_blkno
;
1947 if (entno
< INT_GET(par_node
->hdr
.count
, ARCH_CONVERT
))
1949 par_blkno
= INT_GET(par_node
->hdr
.info
.forw
, ARCH_CONVERT
);
1950 xfs_da_brelse(tp
, par_buf
);
1952 if (unlikely(par_blkno
== 0)) {
1953 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
1954 XFS_ERRLEVEL_LOW
, mp
);
1955 error
= XFS_ERROR(EFSCORRUPTED
);
1958 if ((error
= xfs_da_read_buf(tp
, ip
, par_blkno
, -1, &par_buf
, w
)))
1960 par_node
= par_buf
->data
;
1962 INT_GET(par_node
->hdr
.level
, ARCH_CONVERT
) != level
||
1963 INT_GET(par_node
->hdr
.info
.magic
, ARCH_CONVERT
) != XFS_DA_NODE_MAGIC
)) {
1964 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
1965 XFS_ERRLEVEL_LOW
, mp
);
1966 error
= XFS_ERROR(EFSCORRUPTED
);
1972 * Update the parent entry pointing to the moved block.
1974 INT_SET(par_node
->btree
[entno
].before
, ARCH_CONVERT
, dead_blkno
);
1975 xfs_da_log_buf(tp
, par_buf
,
1976 XFS_DA_LOGRANGE(par_node
, &par_node
->btree
[entno
].before
,
1977 sizeof(par_node
->btree
[entno
].before
)));
1978 xfs_da_buf_done(par_buf
);
1979 xfs_da_buf_done(dead_buf
);
1980 *dead_blknop
= last_blkno
;
1981 *dead_bufp
= last_buf
;
1985 xfs_da_brelse(tp
, par_buf
);
1987 xfs_da_brelse(tp
, sib_buf
);
1988 xfs_da_brelse(tp
, last_buf
);
1993 * Remove a btree block from a directory or attribute.
1996 xfs_da_shrink_inode(xfs_da_args_t
*args
, xfs_dablk_t dead_blkno
,
1997 xfs_dabuf_t
*dead_buf
)
2000 int done
, error
, w
, count
;
2007 w
= args
->whichfork
;
2010 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
))
2011 count
= mp
->m_dirblkfsbs
;
2016 * Remove extents. If we get ENOSPC for a dir we have to move
2017 * the last block to the place we want to kill.
2019 if ((error
= xfs_bunmapi(tp
, dp
, dead_blkno
, count
,
2020 XFS_BMAPI_AFLAG(w
)|XFS_BMAPI_METADATA
,
2021 0, args
->firstblock
, args
->flist
,
2022 &done
)) == ENOSPC
) {
2023 if (w
!= XFS_DATA_FORK
)
2025 if ((error
= xfs_da_swap_lastblock(args
, &dead_blkno
,
2034 xfs_da_binval(tp
, dead_buf
);
2036 * Adjust the directory size for version 1.
2038 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V1(mp
)) {
2039 if ((error
= xfs_bmap_last_offset(tp
, dp
, &bno
, w
)))
2041 size
= XFS_FSB_TO_B(dp
->i_mount
, bno
);
2042 if (size
!= dp
->i_d
.di_size
) {
2043 dp
->i_d
.di_size
= size
;
2044 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2049 xfs_da_binval(tp
, dead_buf
);
2054 * See if the mapping(s) for this btree block are valid, i.e.
2055 * don't contain holes, are logically contiguous, and cover the whole range.
2058 xfs_da_map_covers_blocks(
2060 xfs_bmbt_irec_t
*mapp
,
2067 for (i
= 0, off
= bno
; i
< nmap
; i
++) {
2068 if (mapp
[i
].br_startblock
== HOLESTARTBLOCK
||
2069 mapp
[i
].br_startblock
== DELAYSTARTBLOCK
) {
2072 if (off
!= mapp
[i
].br_startoff
) {
2075 off
+= mapp
[i
].br_blockcount
;
2077 return off
== bno
+ count
;
2082 * Used for get_buf, read_buf, read_bufr, and reada_buf.
2089 xfs_daddr_t
*mappedbnop
,
2095 xfs_buf_t
*bp
= NULL
;
2099 xfs_bmbt_irec_t map
;
2100 xfs_bmbt_irec_t
*mapp
;
2101 xfs_daddr_t mappedbno
;
2109 if (whichfork
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
))
2110 nfsb
= mp
->m_dirblkfsbs
;
2113 mappedbno
= *mappedbnop
;
2115 * Caller doesn't have a mapping. -2 means don't complain
2116 * if we land in a hole.
2118 if (mappedbno
== -1 || mappedbno
== -2) {
2120 * Optimize the one-block case.
2126 xfs_bmapi_single(trans
, dp
, whichfork
, &fsb
,
2127 (xfs_fileoff_t
)bno
))) {
2131 if (fsb
== NULLFSBLOCK
) {
2134 map
.br_startblock
= fsb
;
2135 map
.br_startoff
= (xfs_fileoff_t
)bno
;
2136 map
.br_blockcount
= 1;
2140 mapp
= kmem_alloc(sizeof(*mapp
) * nfsb
, KM_SLEEP
);
2142 if ((error
= xfs_bmapi(trans
, dp
, (xfs_fileoff_t
)bno
,
2144 XFS_BMAPI_METADATA
|
2145 XFS_BMAPI_AFLAG(whichfork
),
2146 NULL
, 0, mapp
, &nmap
, NULL
)))
2150 map
.br_startblock
= XFS_DADDR_TO_FSB(mp
, mappedbno
);
2151 map
.br_startoff
= (xfs_fileoff_t
)bno
;
2152 map
.br_blockcount
= nfsb
;
2156 if (!xfs_da_map_covers_blocks(nmap
, mapp
, bno
, nfsb
)) {
2157 error
= mappedbno
== -2 ? 0 : XFS_ERROR(EFSCORRUPTED
);
2158 if (unlikely(error
== EFSCORRUPTED
)) {
2159 if (xfs_error_level
>= XFS_ERRLEVEL_LOW
) {
2161 cmn_err(CE_ALERT
, "xfs_da_do_buf: bno %lld\n",
2163 cmn_err(CE_ALERT
, "dir: inode %lld\n",
2164 (long long)dp
->i_ino
);
2165 for (i
= 0; i
< nmap
; i
++) {
2167 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d\n",
2169 (long long)mapp
[i
].br_startoff
,
2170 (long long)mapp
[i
].br_startblock
,
2171 (long long)mapp
[i
].br_blockcount
,
2175 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2176 XFS_ERRLEVEL_LOW
, mp
);
2180 if (caller
!= 3 && nmap
> 1) {
2181 bplist
= kmem_alloc(sizeof(*bplist
) * nmap
, KM_SLEEP
);
2186 * Turn the mapping(s) into buffer(s).
2188 for (i
= 0; i
< nmap
; i
++) {
2191 mappedbno
= XFS_FSB_TO_DADDR(mp
, mapp
[i
].br_startblock
);
2193 *mappedbnop
= mappedbno
;
2194 nmapped
= (int)XFS_FSB_TO_BB(mp
, mapp
[i
].br_blockcount
);
2197 bp
= xfs_trans_get_buf(trans
, mp
->m_ddev_targp
,
2198 mappedbno
, nmapped
, 0);
2199 error
= bp
? XFS_BUF_GETERROR(bp
) : XFS_ERROR(EIO
);
2206 error
= xfs_trans_read_buf(mp
, trans
, mp
->m_ddev_targp
,
2207 mappedbno
, nmapped
, 0, &bp
);
2211 xfs_baread(mp
->m_ddev_targp
, mappedbno
, nmapped
);
2219 xfs_trans_brelse(trans
, bp
);
2225 if (whichfork
== XFS_ATTR_FORK
) {
2226 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_ATTR_BTREE
,
2227 XFS_ATTR_BTREE_REF
);
2229 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_DIR_BTREE
,
2234 bplist
[nbplist
++] = bp
;
2238 * Build a dabuf structure.
2241 rbp
= xfs_da_buf_make(nbplist
, bplist
, ra
);
2243 rbp
= xfs_da_buf_make(1, &bp
, ra
);
2247 * For read_buf, check the magic number.
2250 xfs_dir2_data_t
*data
;
2251 xfs_dir2_free_t
*free
;
2252 xfs_da_blkinfo_t
*info
;
2258 magic
= INT_GET(info
->magic
, ARCH_CONVERT
);
2259 magic1
= INT_GET(data
->hdr
.magic
, ARCH_CONVERT
);
2261 XFS_TEST_ERROR((magic
!= XFS_DA_NODE_MAGIC
) &&
2262 (magic
!= XFS_DIR_LEAF_MAGIC
) &&
2263 (magic
!= XFS_ATTR_LEAF_MAGIC
) &&
2264 (magic
!= XFS_DIR2_LEAF1_MAGIC
) &&
2265 (magic
!= XFS_DIR2_LEAFN_MAGIC
) &&
2266 (magic1
!= XFS_DIR2_BLOCK_MAGIC
) &&
2267 (magic1
!= XFS_DIR2_DATA_MAGIC
) &&
2268 (INT_GET(free
->hdr
.magic
, ARCH_CONVERT
) != XFS_DIR2_FREE_MAGIC
),
2269 mp
, XFS_ERRTAG_DA_READ_BUF
,
2270 XFS_RANDOM_DA_READ_BUF
))) {
2271 xfs_buftrace("DA READ ERROR", rbp
->bps
[0]);
2272 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2273 XFS_ERRLEVEL_LOW
, mp
, info
);
2274 error
= XFS_ERROR(EFSCORRUPTED
);
2275 xfs_da_brelse(trans
, rbp
);
2281 kmem_free(bplist
, sizeof(*bplist
) * nmap
);
2284 kmem_free(mapp
, sizeof(*mapp
) * nfsb
);
2291 for (i
= 0; i
< nbplist
; i
++)
2292 xfs_trans_brelse(trans
, bplist
[i
]);
2293 kmem_free(bplist
, sizeof(*bplist
) * nmap
);
2297 kmem_free(mapp
, sizeof(*mapp
) * nfsb
);
2304 * Get a buffer for the dir/attr block.
2311 xfs_daddr_t mappedbno
,
2315 return xfs_da_do_buf(trans
, dp
, bno
, &mappedbno
, bpp
, whichfork
, 0,
2316 (inst_t
*)__return_address
);
2320 * Get a buffer for the dir/attr block, fill in the contents.
2327 xfs_daddr_t mappedbno
,
2331 return xfs_da_do_buf(trans
, dp
, bno
, &mappedbno
, bpp
, whichfork
, 1,
2332 (inst_t
*)__return_address
);
2336 * Readahead the dir/attr block.
2348 if (xfs_da_do_buf(trans
, dp
, bno
, &rval
, NULL
, whichfork
, 3,
2349 (inst_t
*)__return_address
))
2356 * Calculate the number of bits needed to hold i different values.
2359 xfs_da_log2_roundup(uint i
)
2363 for (rval
= 0; rval
< NBBY
* sizeof(i
); rval
++) {
2364 if ((1 << rval
) >= i
)
2370 kmem_zone_t
*xfs_da_state_zone
; /* anchor for state struct zone */
2371 kmem_zone_t
*xfs_dabuf_zone
; /* dabuf zone */
2374 * Allocate a dir-state structure.
2375 * We don't put them on the stack since they're large.
2378 xfs_da_state_alloc(void)
2380 return kmem_zone_zalloc(xfs_da_state_zone
, KM_SLEEP
);
2384 * Kill the altpath contents of a da-state structure.
2387 xfs_da_state_kill_altpath(xfs_da_state_t
*state
)
2391 for (i
= 0; i
< state
->altpath
.active
; i
++) {
2392 if (state
->altpath
.blk
[i
].bp
) {
2393 if (state
->altpath
.blk
[i
].bp
!= state
->path
.blk
[i
].bp
)
2394 xfs_da_buf_done(state
->altpath
.blk
[i
].bp
);
2395 state
->altpath
.blk
[i
].bp
= NULL
;
2398 state
->altpath
.active
= 0;
2402 * Free a da-state structure.
2405 xfs_da_state_free(xfs_da_state_t
*state
)
2409 xfs_da_state_kill_altpath(state
);
2410 for (i
= 0; i
< state
->path
.active
; i
++) {
2411 if (state
->path
.blk
[i
].bp
)
2412 xfs_da_buf_done(state
->path
.blk
[i
].bp
);
2414 if (state
->extravalid
&& state
->extrablk
.bp
)
2415 xfs_da_buf_done(state
->extrablk
.bp
);
2417 memset((char *)state
, 0, sizeof(*state
));
2419 kmem_zone_free(xfs_da_state_zone
, state
);
2422 #ifdef XFS_DABUF_DEBUG
2423 xfs_dabuf_t
*xfs_dabuf_global_list
;
2424 lock_t xfs_dabuf_global_lock
;
2431 STATIC xfs_dabuf_t
*
2432 xfs_da_buf_make(int nbuf
, xfs_buf_t
**bps
, inst_t
*ra
)
2440 dabuf
= kmem_zone_alloc(xfs_dabuf_zone
, KM_SLEEP
);
2442 dabuf
= kmem_alloc(XFS_DA_BUF_SIZE(nbuf
), KM_SLEEP
);
2444 #ifdef XFS_DABUF_DEBUG
2446 dabuf
->target
= XFS_BUF_TARGET(bps
[0]);
2447 dabuf
->blkno
= XFS_BUF_ADDR(bps
[0]);
2452 dabuf
->bbcount
= (short)BTOBB(XFS_BUF_COUNT(bp
));
2453 dabuf
->data
= XFS_BUF_PTR(bp
);
2457 for (i
= 0, dabuf
->bbcount
= 0; i
< nbuf
; i
++) {
2458 dabuf
->bps
[i
] = bp
= bps
[i
];
2459 dabuf
->bbcount
+= BTOBB(XFS_BUF_COUNT(bp
));
2461 dabuf
->data
= kmem_alloc(BBTOB(dabuf
->bbcount
), KM_SLEEP
);
2462 for (i
= off
= 0; i
< nbuf
; i
++, off
+= XFS_BUF_COUNT(bp
)) {
2464 memcpy((char *)dabuf
->data
+ off
, XFS_BUF_PTR(bp
),
2468 #ifdef XFS_DABUF_DEBUG
2473 s
= mutex_spinlock(&xfs_dabuf_global_lock
);
2474 for (p
= xfs_dabuf_global_list
; p
; p
= p
->next
) {
2475 ASSERT(p
->blkno
!= dabuf
->blkno
||
2476 p
->target
!= dabuf
->target
);
2479 if (xfs_dabuf_global_list
)
2480 xfs_dabuf_global_list
->prev
= dabuf
;
2481 dabuf
->next
= xfs_dabuf_global_list
;
2482 xfs_dabuf_global_list
= dabuf
;
2483 mutex_spinunlock(&xfs_dabuf_global_lock
, s
);
2493 xfs_da_buf_clean(xfs_dabuf_t
*dabuf
)
2500 ASSERT(dabuf
->nbuf
> 1);
2502 for (i
= off
= 0; i
< dabuf
->nbuf
;
2503 i
++, off
+= XFS_BUF_COUNT(bp
)) {
2505 memcpy(XFS_BUF_PTR(bp
), (char *)dabuf
->data
+ off
,
2515 xfs_da_buf_done(xfs_dabuf_t
*dabuf
)
2518 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2520 xfs_da_buf_clean(dabuf
);
2521 if (dabuf
->nbuf
> 1)
2522 kmem_free(dabuf
->data
, BBTOB(dabuf
->bbcount
));
2523 #ifdef XFS_DABUF_DEBUG
2527 s
= mutex_spinlock(&xfs_dabuf_global_lock
);
2529 dabuf
->prev
->next
= dabuf
->next
;
2531 xfs_dabuf_global_list
= dabuf
->next
;
2533 dabuf
->next
->prev
= dabuf
->prev
;
2534 mutex_spinunlock(&xfs_dabuf_global_lock
, s
);
2536 memset(dabuf
, 0, XFS_DA_BUF_SIZE(dabuf
->nbuf
));
2538 if (dabuf
->nbuf
== 1)
2539 kmem_zone_free(xfs_dabuf_zone
, dabuf
);
2541 kmem_free(dabuf
, XFS_DA_BUF_SIZE(dabuf
->nbuf
));
2545 * Log transaction from a dabuf.
2548 xfs_da_log_buf(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
, uint first
, uint last
)
2556 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2557 if (dabuf
->nbuf
== 1) {
2558 ASSERT(dabuf
->data
== (void *)XFS_BUF_PTR(dabuf
->bps
[0]));
2559 xfs_trans_log_buf(tp
, dabuf
->bps
[0], first
, last
);
2563 ASSERT(first
<= last
);
2564 for (i
= off
= 0; i
< dabuf
->nbuf
; i
++, off
+= XFS_BUF_COUNT(bp
)) {
2567 l
= f
+ XFS_BUF_COUNT(bp
) - 1;
2573 xfs_trans_log_buf(tp
, bp
, f
- off
, l
- off
);
2575 * B_DONE is set by xfs_trans_log buf.
2576 * If we don't set it on a new buffer (get not read)
2577 * then if we don't put anything in the buffer it won't
2578 * be set, and at commit it it released into the cache,
2579 * and then a read will fail.
2581 else if (!(XFS_BUF_ISDONE(bp
)))
2588 * Release dabuf from a transaction.
2589 * Have to free up the dabuf before the buffers are released,
2590 * since the synchronization on the dabuf is really the lock on the buffer.
2593 xfs_da_brelse(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
)
2600 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2601 if ((nbuf
= dabuf
->nbuf
) == 1) {
2605 bplist
= kmem_alloc(nbuf
* sizeof(*bplist
), KM_SLEEP
);
2606 memcpy(bplist
, dabuf
->bps
, nbuf
* sizeof(*bplist
));
2608 xfs_da_buf_done(dabuf
);
2609 for (i
= 0; i
< nbuf
; i
++)
2610 xfs_trans_brelse(tp
, bplist
[i
]);
2612 kmem_free(bplist
, nbuf
* sizeof(*bplist
));
2616 * Invalidate dabuf from a transaction.
2619 xfs_da_binval(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
)
2626 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2627 if ((nbuf
= dabuf
->nbuf
) == 1) {
2631 bplist
= kmem_alloc(nbuf
* sizeof(*bplist
), KM_SLEEP
);
2632 memcpy(bplist
, dabuf
->bps
, nbuf
* sizeof(*bplist
));
2634 xfs_da_buf_done(dabuf
);
2635 for (i
= 0; i
< nbuf
; i
++)
2636 xfs_trans_binval(tp
, bplist
[i
]);
2638 kmem_free(bplist
, nbuf
* sizeof(*bplist
));
2642 * Get the first daddr from a dabuf.
2645 xfs_da_blkno(xfs_dabuf_t
*dabuf
)
2647 ASSERT(dabuf
->nbuf
);
2648 ASSERT(dabuf
->data
);
2649 return XFS_BUF_ADDR(dabuf
->bps
[0]);