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
);
116 STATIC
int xfs_da_blk_unlink(xfs_da_state_t
*state
,
117 xfs_da_state_blk_t
*drop_blk
,
118 xfs_da_state_blk_t
*save_blk
);
119 STATIC
void xfs_da_state_kill_altpath(xfs_da_state_t
*state
);
121 /*========================================================================
122 * Routines used for growing the Btree.
123 *========================================================================*/
126 * Create the initial contents of an intermediate node.
129 xfs_da_node_create(xfs_da_args_t
*args
, xfs_dablk_t blkno
, int level
,
130 xfs_dabuf_t
**bpp
, int whichfork
)
132 xfs_da_intnode_t
*node
;
138 error
= xfs_da_get_buf(tp
, args
->dp
, blkno
, -1, &bp
, whichfork
);
143 node
->hdr
.info
.forw
= 0;
144 node
->hdr
.info
.back
= 0;
145 INT_SET(node
->hdr
.info
.magic
, ARCH_CONVERT
, XFS_DA_NODE_MAGIC
);
146 node
->hdr
.info
.pad
= 0;
148 INT_SET(node
->hdr
.level
, ARCH_CONVERT
, level
);
150 xfs_da_log_buf(tp
, bp
,
151 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
158 * Split a leaf node, rebalance, then possibly split
159 * intermediate nodes, rebalance, etc.
162 xfs_da_split(xfs_da_state_t
*state
)
164 xfs_da_state_blk_t
*oldblk
, *newblk
, *addblk
;
165 xfs_da_intnode_t
*node
;
167 int max
, action
, error
, i
;
170 * Walk back up the tree splitting/inserting/adjusting as necessary.
171 * If we need to insert and there isn't room, split the node, then
172 * decide which fragment to insert the new block from below into.
173 * Note that we may split the root this way, but we need more fixup.
175 max
= state
->path
.active
- 1;
176 ASSERT((max
>= 0) && (max
< XFS_DA_NODE_MAXDEPTH
));
177 ASSERT(state
->path
.blk
[max
].magic
== XFS_ATTR_LEAF_MAGIC
||
178 state
->path
.blk
[max
].magic
== XFS_DIRX_LEAF_MAGIC(state
->mp
));
180 addblk
= &state
->path
.blk
[max
]; /* initial dummy value */
181 for (i
= max
; (i
>= 0) && addblk
; state
->path
.active
--, i
--) {
182 oldblk
= &state
->path
.blk
[i
];
183 newblk
= &state
->altpath
.blk
[i
];
186 * If a leaf node then
187 * Allocate a new leaf node, then rebalance across them.
188 * else if an intermediate node then
189 * We split on the last layer, must we split the node?
191 switch (oldblk
->magic
) {
192 case XFS_ATTR_LEAF_MAGIC
:
196 error
= xfs_attr_leaf_split(state
, oldblk
, newblk
);
197 if ((error
!= 0) && (error
!= ENOSPC
)) {
198 return(error
); /* GROT: attr is inconsistent */
205 * Entry wouldn't fit, split the leaf again.
207 state
->extravalid
= 1;
209 state
->extraafter
= 0; /* before newblk */
210 error
= xfs_attr_leaf_split(state
, oldblk
,
213 state
->extraafter
= 1; /* after newblk */
214 error
= xfs_attr_leaf_split(state
, newblk
,
218 return(error
); /* GROT: attr inconsistent */
222 case XFS_DIR_LEAF_MAGIC
:
223 ASSERT(XFS_DIR_IS_V1(state
->mp
));
224 error
= xfs_dir_leaf_split(state
, oldblk
, newblk
);
225 if ((error
!= 0) && (error
!= ENOSPC
)) {
226 return(error
); /* GROT: dir is inconsistent */
233 * Entry wouldn't fit, split the leaf again.
235 state
->extravalid
= 1;
237 state
->extraafter
= 0; /* before newblk */
238 error
= xfs_dir_leaf_split(state
, oldblk
,
241 return(error
); /* GROT: dir incon. */
244 state
->extraafter
= 1; /* after newblk */
245 error
= xfs_dir_leaf_split(state
, newblk
,
248 return(error
); /* GROT: dir incon. */
252 case XFS_DIR2_LEAFN_MAGIC
:
253 ASSERT(XFS_DIR_IS_V2(state
->mp
));
254 error
= xfs_dir2_leafn_split(state
, oldblk
, newblk
);
259 case XFS_DA_NODE_MAGIC
:
260 error
= xfs_da_node_split(state
, oldblk
, newblk
, addblk
,
262 xfs_da_buf_done(addblk
->bp
);
265 return(error
); /* GROT: dir is inconsistent */
267 * Record the newly split block for the next time thru?
277 * Update the btree to show the new hashval for this child.
279 xfs_da_fixhashpath(state
, &state
->path
);
281 * If we won't need this block again, it's getting dropped
282 * from the active path by the loop control, so we need
283 * to mark it done now.
285 if (i
> 0 || !addblk
)
286 xfs_da_buf_done(oldblk
->bp
);
292 * Split the root node.
294 ASSERT(state
->path
.active
== 0);
295 oldblk
= &state
->path
.blk
[0];
296 error
= xfs_da_root_split(state
, oldblk
, addblk
);
298 xfs_da_buf_done(oldblk
->bp
);
299 xfs_da_buf_done(addblk
->bp
);
301 return(error
); /* GROT: dir is inconsistent */
305 * Update pointers to the node which used to be block 0 and
306 * just got bumped because of the addition of a new root node.
307 * There might be three blocks involved if a double split occurred,
308 * and the original block 0 could be at any position in the list.
311 node
= oldblk
->bp
->data
;
312 if (node
->hdr
.info
.forw
) {
313 if (INT_GET(node
->hdr
.info
.forw
, ARCH_CONVERT
) == addblk
->blkno
) {
316 ASSERT(state
->extravalid
);
317 bp
= state
->extrablk
.bp
;
320 INT_SET(node
->hdr
.info
.back
, ARCH_CONVERT
, oldblk
->blkno
);
321 xfs_da_log_buf(state
->args
->trans
, bp
,
322 XFS_DA_LOGRANGE(node
, &node
->hdr
.info
,
323 sizeof(node
->hdr
.info
)));
325 node
= oldblk
->bp
->data
;
326 if (INT_GET(node
->hdr
.info
.back
, ARCH_CONVERT
)) {
327 if (INT_GET(node
->hdr
.info
.back
, ARCH_CONVERT
) == addblk
->blkno
) {
330 ASSERT(state
->extravalid
);
331 bp
= state
->extrablk
.bp
;
334 INT_SET(node
->hdr
.info
.forw
, ARCH_CONVERT
, oldblk
->blkno
);
335 xfs_da_log_buf(state
->args
->trans
, bp
,
336 XFS_DA_LOGRANGE(node
, &node
->hdr
.info
,
337 sizeof(node
->hdr
.info
)));
339 xfs_da_buf_done(oldblk
->bp
);
340 xfs_da_buf_done(addblk
->bp
);
346 * Split the root. We have to create a new root and point to the two
347 * parts (the split old root) that we just created. Copy block zero to
348 * the EOF, extending the inode in process.
350 STATIC
int /* error */
351 xfs_da_root_split(xfs_da_state_t
*state
, xfs_da_state_blk_t
*blk1
,
352 xfs_da_state_blk_t
*blk2
)
354 xfs_da_intnode_t
*node
, *oldroot
;
362 xfs_dir2_leaf_t
*leaf
;
365 * Copy the existing (incorrect) block from the root node position
366 * to a free space somewhere.
369 ASSERT(args
!= NULL
);
370 error
= xfs_da_grow_inode(args
, &blkno
);
376 error
= xfs_da_get_buf(tp
, dp
, blkno
, -1, &bp
, args
->whichfork
);
381 oldroot
= blk1
->bp
->data
;
382 if (INT_GET(oldroot
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) {
383 size
= (int)((char *)&oldroot
->btree
[INT_GET(oldroot
->hdr
.count
, ARCH_CONVERT
)] -
386 ASSERT(XFS_DIR_IS_V2(mp
));
387 ASSERT(INT_GET(oldroot
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
);
388 leaf
= (xfs_dir2_leaf_t
*)oldroot
;
389 size
= (int)((char *)&leaf
->ents
[INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)] -
392 memcpy(node
, oldroot
, size
);
393 xfs_da_log_buf(tp
, bp
, 0, size
- 1);
394 xfs_da_buf_done(blk1
->bp
);
399 * Set up the new root node.
401 error
= xfs_da_node_create(args
,
402 args
->whichfork
== XFS_DATA_FORK
&&
403 XFS_DIR_IS_V2(mp
) ? mp
->m_dirleafblk
: 0,
404 INT_GET(node
->hdr
.level
, ARCH_CONVERT
) + 1, &bp
, args
->whichfork
);
408 INT_SET(node
->btree
[0].hashval
, ARCH_CONVERT
, blk1
->hashval
);
409 INT_SET(node
->btree
[0].before
, ARCH_CONVERT
, blk1
->blkno
);
410 INT_SET(node
->btree
[1].hashval
, ARCH_CONVERT
, blk2
->hashval
);
411 INT_SET(node
->btree
[1].before
, ARCH_CONVERT
, blk2
->blkno
);
412 INT_SET(node
->hdr
.count
, ARCH_CONVERT
, 2);
415 if (INT_GET(oldroot
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
) {
416 ASSERT(blk1
->blkno
>= mp
->m_dirleafblk
&&
417 blk1
->blkno
< mp
->m_dirfreeblk
);
418 ASSERT(blk2
->blkno
>= mp
->m_dirleafblk
&&
419 blk2
->blkno
< mp
->m_dirfreeblk
);
423 /* Header is already logged by xfs_da_node_create */
424 xfs_da_log_buf(tp
, bp
,
425 XFS_DA_LOGRANGE(node
, node
->btree
,
426 sizeof(xfs_da_node_entry_t
) * 2));
433 * Split the node, rebalance, then add the new entry.
435 STATIC
int /* error */
436 xfs_da_node_split(xfs_da_state_t
*state
, xfs_da_state_blk_t
*oldblk
,
437 xfs_da_state_blk_t
*newblk
,
438 xfs_da_state_blk_t
*addblk
,
439 int treelevel
, int *result
)
441 xfs_da_intnode_t
*node
;
446 node
= oldblk
->bp
->data
;
447 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
450 * With V2 the extra block is data or freespace.
452 useextra
= state
->extravalid
&& XFS_DIR_IS_V1(state
->mp
);
453 newcount
= 1 + useextra
;
455 * Do we have to split the node?
457 if ((INT_GET(node
->hdr
.count
, ARCH_CONVERT
) + newcount
) > state
->node_ents
) {
459 * Allocate a new node, add to the doubly linked chain of
460 * nodes, then move some of our excess entries into it.
462 error
= xfs_da_grow_inode(state
->args
, &blkno
);
464 return(error
); /* GROT: dir is inconsistent */
466 error
= xfs_da_node_create(state
->args
, blkno
, treelevel
,
467 &newblk
->bp
, state
->args
->whichfork
);
469 return(error
); /* GROT: dir is inconsistent */
470 newblk
->blkno
= blkno
;
471 newblk
->magic
= XFS_DA_NODE_MAGIC
;
472 xfs_da_node_rebalance(state
, oldblk
, newblk
);
473 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
482 * Insert the new entry(s) into the correct block
483 * (updating last hashval in the process).
485 * xfs_da_node_add() inserts BEFORE the given index,
486 * and as a result of using node_lookup_int() we always
487 * point to a valid entry (not after one), but a split
488 * operation always results in a new block whose hashvals
489 * FOLLOW the current block.
491 * If we had double-split op below us, then add the extra block too.
493 node
= oldblk
->bp
->data
;
494 if (oldblk
->index
<= INT_GET(node
->hdr
.count
, ARCH_CONVERT
)) {
496 xfs_da_node_add(state
, oldblk
, addblk
);
498 if (state
->extraafter
)
500 xfs_da_node_add(state
, oldblk
, &state
->extrablk
);
501 state
->extravalid
= 0;
505 xfs_da_node_add(state
, newblk
, addblk
);
507 if (state
->extraafter
)
509 xfs_da_node_add(state
, newblk
, &state
->extrablk
);
510 state
->extravalid
= 0;
518 * Balance the btree elements between two intermediate nodes,
519 * usually one full and one empty.
521 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
524 xfs_da_node_rebalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*blk1
,
525 xfs_da_state_blk_t
*blk2
)
527 xfs_da_intnode_t
*node1
, *node2
, *tmpnode
;
528 xfs_da_node_entry_t
*btree_s
, *btree_d
;
532 node1
= blk1
->bp
->data
;
533 node2
= blk2
->bp
->data
;
535 * Figure out how many entries need to move, and in which direction.
536 * Swap the nodes around if that makes it simpler.
538 if ((INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) > 0) && (INT_GET(node2
->hdr
.count
, ARCH_CONVERT
) > 0) &&
539 ((INT_GET(node2
->btree
[ 0 ].hashval
, ARCH_CONVERT
) < INT_GET(node1
->btree
[ 0 ].hashval
, ARCH_CONVERT
)) ||
540 (INT_GET(node2
->btree
[ INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
) <
541 INT_GET(node1
->btree
[ INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
)))) {
546 ASSERT(INT_GET(node1
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
547 ASSERT(INT_GET(node2
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
548 count
= (INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) - INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)) / 2;
551 tp
= state
->args
->trans
;
553 * Two cases: high-to-low and low-to-high.
557 * Move elements in node2 up to make a hole.
559 if ((tmp
= INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)) > 0) {
560 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
561 btree_s
= &node2
->btree
[0];
562 btree_d
= &node2
->btree
[count
];
563 memmove(btree_d
, btree_s
, tmp
);
567 * Move the req'd B-tree elements from high in node1 to
570 INT_MOD(node2
->hdr
.count
, ARCH_CONVERT
, count
);
571 tmp
= count
* (uint
)sizeof(xfs_da_node_entry_t
);
572 btree_s
= &node1
->btree
[INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) - count
];
573 btree_d
= &node2
->btree
[0];
574 memcpy(btree_d
, btree_s
, tmp
);
575 INT_MOD(node1
->hdr
.count
, ARCH_CONVERT
, -(count
));
579 * Move the req'd B-tree elements from low in node2 to
583 tmp
= count
* (uint
)sizeof(xfs_da_node_entry_t
);
584 btree_s
= &node2
->btree
[0];
585 btree_d
= &node1
->btree
[INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)];
586 memcpy(btree_d
, btree_s
, tmp
);
587 INT_MOD(node1
->hdr
.count
, ARCH_CONVERT
, count
);
588 xfs_da_log_buf(tp
, blk1
->bp
,
589 XFS_DA_LOGRANGE(node1
, btree_d
, tmp
));
592 * Move elements in node2 down to fill the hole.
594 tmp
= INT_GET(node2
->hdr
.count
, ARCH_CONVERT
) - count
;
595 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
596 btree_s
= &node2
->btree
[count
];
597 btree_d
= &node2
->btree
[0];
598 memmove(btree_d
, btree_s
, tmp
);
599 INT_MOD(node2
->hdr
.count
, ARCH_CONVERT
, -(count
));
603 * Log header of node 1 and all current bits of node 2.
605 xfs_da_log_buf(tp
, blk1
->bp
,
606 XFS_DA_LOGRANGE(node1
, &node1
->hdr
, sizeof(node1
->hdr
)));
607 xfs_da_log_buf(tp
, blk2
->bp
,
608 XFS_DA_LOGRANGE(node2
, &node2
->hdr
,
610 sizeof(node2
->btree
[0]) * INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)));
613 * Record the last hashval from each block for upward propagation.
614 * (note: don't use the swapped node pointers)
616 node1
= blk1
->bp
->data
;
617 node2
= blk2
->bp
->data
;
618 blk1
->hashval
= INT_GET(node1
->btree
[ INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
619 blk2
->hashval
= INT_GET(node2
->btree
[ INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
622 * Adjust the expected index for insertion.
624 if (blk1
->index
>= INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)) {
625 blk2
->index
= blk1
->index
- INT_GET(node1
->hdr
.count
, ARCH_CONVERT
);
626 blk1
->index
= INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) + 1; /* make it invalid */
631 * Add a new entry to an intermediate node.
634 xfs_da_node_add(xfs_da_state_t
*state
, xfs_da_state_blk_t
*oldblk
,
635 xfs_da_state_blk_t
*newblk
)
637 xfs_da_intnode_t
*node
;
638 xfs_da_node_entry_t
*btree
;
642 node
= oldblk
->bp
->data
;
644 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
645 ASSERT((oldblk
->index
>= 0) && (oldblk
->index
<= INT_GET(node
->hdr
.count
, ARCH_CONVERT
)));
646 ASSERT(newblk
->blkno
!= 0);
647 if (state
->args
->whichfork
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
))
648 ASSERT(newblk
->blkno
>= mp
->m_dirleafblk
&&
649 newblk
->blkno
< mp
->m_dirfreeblk
);
652 * We may need to make some room before we insert the new node.
655 btree
= &node
->btree
[ oldblk
->index
];
656 if (oldblk
->index
< INT_GET(node
->hdr
.count
, ARCH_CONVERT
)) {
657 tmp
= (INT_GET(node
->hdr
.count
, ARCH_CONVERT
) - oldblk
->index
) * (uint
)sizeof(*btree
);
658 memmove(btree
+ 1, btree
, tmp
);
660 INT_SET(btree
->hashval
, ARCH_CONVERT
, newblk
->hashval
);
661 INT_SET(btree
->before
, ARCH_CONVERT
, newblk
->blkno
);
662 xfs_da_log_buf(state
->args
->trans
, oldblk
->bp
,
663 XFS_DA_LOGRANGE(node
, btree
, tmp
+ sizeof(*btree
)));
664 INT_MOD(node
->hdr
.count
, ARCH_CONVERT
, +1);
665 xfs_da_log_buf(state
->args
->trans
, oldblk
->bp
,
666 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
669 * Copy the last hash value from the oldblk to propagate upwards.
671 oldblk
->hashval
= INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
674 /*========================================================================
675 * Routines used for shrinking the Btree.
676 *========================================================================*/
679 * Deallocate an empty leaf node, remove it from its parent,
680 * possibly deallocating that block, etc...
683 xfs_da_join(xfs_da_state_t
*state
)
685 xfs_da_state_blk_t
*drop_blk
, *save_blk
;
689 drop_blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
690 save_blk
= &state
->altpath
.blk
[ state
->path
.active
-1 ];
691 ASSERT(state
->path
.blk
[0].magic
== XFS_DA_NODE_MAGIC
);
692 ASSERT(drop_blk
->magic
== XFS_ATTR_LEAF_MAGIC
||
693 drop_blk
->magic
== XFS_DIRX_LEAF_MAGIC(state
->mp
));
696 * Walk back up the tree joining/deallocating as necessary.
697 * When we stop dropping blocks, break out.
699 for ( ; state
->path
.active
>= 2; drop_blk
--, save_blk
--,
700 state
->path
.active
--) {
702 * See if we can combine the block with a neighbor.
703 * (action == 0) => no options, just leave
704 * (action == 1) => coalesce, then unlink
705 * (action == 2) => block empty, unlink it
707 switch (drop_blk
->magic
) {
708 case XFS_ATTR_LEAF_MAGIC
:
712 error
= xfs_attr_leaf_toosmall(state
, &action
);
719 xfs_attr_leaf_unbalance(state
, drop_blk
, save_blk
);
722 case XFS_DIR_LEAF_MAGIC
:
723 ASSERT(XFS_DIR_IS_V1(state
->mp
));
724 error
= xfs_dir_leaf_toosmall(state
, &action
);
729 xfs_dir_leaf_unbalance(state
, drop_blk
, save_blk
);
731 case XFS_DIR2_LEAFN_MAGIC
:
732 ASSERT(XFS_DIR_IS_V2(state
->mp
));
733 error
= xfs_dir2_leafn_toosmall(state
, &action
);
738 xfs_dir2_leafn_unbalance(state
, drop_blk
, save_blk
);
740 case XFS_DA_NODE_MAGIC
:
742 * Remove the offending node, fixup hashvals,
743 * check for a toosmall neighbor.
745 xfs_da_node_remove(state
, drop_blk
);
746 xfs_da_fixhashpath(state
, &state
->path
);
747 error
= xfs_da_node_toosmall(state
, &action
);
752 xfs_da_node_unbalance(state
, drop_blk
, save_blk
);
755 xfs_da_fixhashpath(state
, &state
->altpath
);
756 error
= xfs_da_blk_unlink(state
, drop_blk
, save_blk
);
757 xfs_da_state_kill_altpath(state
);
760 error
= xfs_da_shrink_inode(state
->args
, drop_blk
->blkno
,
767 * We joined all the way to the top. If it turns out that
768 * we only have one entry in the root, make the child block
771 xfs_da_node_remove(state
, drop_blk
);
772 xfs_da_fixhashpath(state
, &state
->path
);
773 error
= xfs_da_root_join(state
, &state
->path
.blk
[0]);
778 * We have only one entry in the root. Copy the only remaining child of
779 * the old root to block 0 as the new root node.
782 xfs_da_root_join(xfs_da_state_t
*state
, xfs_da_state_blk_t
*root_blk
)
784 xfs_da_intnode_t
*oldroot
;
786 xfs_da_blkinfo_t
*blkinfo
;
793 ASSERT(args
!= NULL
);
794 ASSERT(root_blk
->magic
== XFS_DA_NODE_MAGIC
);
795 oldroot
= root_blk
->bp
->data
;
796 ASSERT(INT_GET(oldroot
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
797 ASSERT(!oldroot
->hdr
.info
.forw
);
798 ASSERT(!oldroot
->hdr
.info
.back
);
801 * If the root has more than one child, then don't do anything.
803 if (INT_GET(oldroot
->hdr
.count
, ARCH_CONVERT
) > 1)
807 * Read in the (only) child block, then copy those bytes into
808 * the root block's buffer and free the original child block.
810 child
= INT_GET(oldroot
->btree
[ 0 ].before
, ARCH_CONVERT
);
812 error
= xfs_da_read_buf(args
->trans
, args
->dp
, child
, -1, &bp
,
818 if (INT_GET(oldroot
->hdr
.level
, ARCH_CONVERT
) == 1) {
819 ASSERT(INT_GET(blkinfo
->magic
, ARCH_CONVERT
) == XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
820 INT_GET(blkinfo
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
);
822 ASSERT(INT_GET(blkinfo
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
824 ASSERT(!blkinfo
->forw
);
825 ASSERT(!blkinfo
->back
);
826 memcpy(root_blk
->bp
->data
, bp
->data
, state
->blocksize
);
827 xfs_da_log_buf(args
->trans
, root_blk
->bp
, 0, state
->blocksize
- 1);
828 error
= xfs_da_shrink_inode(args
, child
, bp
);
833 * Check a node block and its neighbors to see if the block should be
834 * collapsed into one or the other neighbor. Always keep the block
835 * with the smaller block number.
836 * If the current block is over 50% full, don't try to join it, return 0.
837 * If the block is empty, fill in the state structure and return 2.
838 * If it can be collapsed, fill in the state structure and return 1.
839 * If nothing can be done, return 0.
842 xfs_da_node_toosmall(xfs_da_state_t
*state
, int *action
)
844 xfs_da_intnode_t
*node
;
845 xfs_da_state_blk_t
*blk
;
846 xfs_da_blkinfo_t
*info
;
847 int count
, forward
, error
, retval
, i
;
852 * Check for the degenerate case of the block being over 50% full.
853 * If so, it's not worth even looking to see if we might be able
854 * to coalesce with a sibling.
856 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
857 info
= blk
->bp
->data
;
858 ASSERT(INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
859 node
= (xfs_da_intnode_t
*)info
;
860 count
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
861 if (count
> (state
->node_ents
>> 1)) {
862 *action
= 0; /* blk over 50%, don't try to join */
863 return(0); /* blk over 50%, don't try to join */
867 * Check for the degenerate case of the block being empty.
868 * If the block is empty, we'll simply delete it, no need to
869 * coalesce it with a sibling block. We choose (aribtrarily)
870 * to merge with the forward block unless it is NULL.
874 * Make altpath point to the block we want to keep and
875 * path point to the block we want to drop (this one).
877 forward
= info
->forw
;
878 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
879 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
892 * Examine each sibling block to see if we can coalesce with
893 * at least 25% free space to spare. We need to figure out
894 * whether to merge with the forward or the backward block.
895 * We prefer coalescing with the lower numbered sibling so as
896 * to shrink a directory over time.
898 /* start with smaller blk num */
899 forward
= (INT_GET(info
->forw
, ARCH_CONVERT
)
900 < INT_GET(info
->back
, ARCH_CONVERT
));
901 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
903 blkno
= INT_GET(info
->forw
, ARCH_CONVERT
);
905 blkno
= INT_GET(info
->back
, ARCH_CONVERT
);
908 error
= xfs_da_read_buf(state
->args
->trans
, state
->args
->dp
,
909 blkno
, -1, &bp
, state
->args
->whichfork
);
914 node
= (xfs_da_intnode_t
*)info
;
915 count
= state
->node_ents
;
916 count
-= state
->node_ents
>> 2;
917 count
-= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
919 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
920 count
-= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
921 xfs_da_brelse(state
->args
->trans
, bp
);
923 break; /* fits with at least 25% to spare */
931 * Make altpath point to the block we want to keep (the lower
932 * numbered block) and path point to the block we want to drop.
934 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
935 if (blkno
< blk
->blkno
) {
936 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
946 error
= xfs_da_path_shift(state
, &state
->path
, forward
,
961 * Walk back up the tree adjusting hash values as necessary,
962 * when we stop making changes, return.
965 xfs_da_fixhashpath(xfs_da_state_t
*state
, xfs_da_state_path_t
*path
)
967 xfs_da_state_blk_t
*blk
;
968 xfs_da_intnode_t
*node
;
969 xfs_da_node_entry_t
*btree
;
970 xfs_dahash_t lasthash
=0;
973 level
= path
->active
-1;
974 blk
= &path
->blk
[ level
];
975 switch (blk
->magic
) {
977 case XFS_ATTR_LEAF_MAGIC
:
978 lasthash
= xfs_attr_leaf_lasthash(blk
->bp
, &count
);
983 case XFS_DIR_LEAF_MAGIC
:
984 ASSERT(XFS_DIR_IS_V1(state
->mp
));
985 lasthash
= xfs_dir_leaf_lasthash(blk
->bp
, &count
);
989 case XFS_DIR2_LEAFN_MAGIC
:
990 ASSERT(XFS_DIR_IS_V2(state
->mp
));
991 lasthash
= xfs_dir2_leafn_lasthash(blk
->bp
, &count
);
995 case XFS_DA_NODE_MAGIC
:
996 lasthash
= xfs_da_node_lasthash(blk
->bp
, &count
);
1001 for (blk
--, level
--; level
>= 0; blk
--, level
--) {
1002 node
= blk
->bp
->data
;
1003 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1004 btree
= &node
->btree
[ blk
->index
];
1005 if (INT_GET(btree
->hashval
, ARCH_CONVERT
) == lasthash
)
1007 blk
->hashval
= lasthash
;
1008 INT_SET(btree
->hashval
, ARCH_CONVERT
, lasthash
);
1009 xfs_da_log_buf(state
->args
->trans
, blk
->bp
,
1010 XFS_DA_LOGRANGE(node
, btree
, sizeof(*btree
)));
1012 lasthash
= INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
1017 * Remove an entry from an intermediate node.
1020 xfs_da_node_remove(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
)
1022 xfs_da_intnode_t
*node
;
1023 xfs_da_node_entry_t
*btree
;
1026 node
= drop_blk
->bp
->data
;
1027 ASSERT(drop_blk
->index
< INT_GET(node
->hdr
.count
, ARCH_CONVERT
));
1028 ASSERT(drop_blk
->index
>= 0);
1031 * Copy over the offending entry, or just zero it out.
1033 btree
= &node
->btree
[drop_blk
->index
];
1034 if (drop_blk
->index
< (INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1)) {
1035 tmp
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
) - drop_blk
->index
- 1;
1036 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
1037 memmove(btree
, btree
+ 1, tmp
);
1038 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
1039 XFS_DA_LOGRANGE(node
, btree
, tmp
));
1040 btree
= &node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ];
1042 memset((char *)btree
, 0, sizeof(xfs_da_node_entry_t
));
1043 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
1044 XFS_DA_LOGRANGE(node
, btree
, sizeof(*btree
)));
1045 INT_MOD(node
->hdr
.count
, ARCH_CONVERT
, -1);
1046 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
1047 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
1050 * Copy the last hash value from the block to propagate upwards.
1053 drop_blk
->hashval
= INT_GET(btree
->hashval
, ARCH_CONVERT
);
1057 * Unbalance the btree elements between two intermediate nodes,
1058 * move all Btree elements from one node into another.
1061 xfs_da_node_unbalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
,
1062 xfs_da_state_blk_t
*save_blk
)
1064 xfs_da_intnode_t
*drop_node
, *save_node
;
1065 xfs_da_node_entry_t
*btree
;
1069 drop_node
= drop_blk
->bp
->data
;
1070 save_node
= save_blk
->bp
->data
;
1071 ASSERT(INT_GET(drop_node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1072 ASSERT(INT_GET(save_node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1073 tp
= state
->args
->trans
;
1076 * If the dying block has lower hashvals, then move all the
1077 * elements in the remaining block up to make a hole.
1079 if ((INT_GET(drop_node
->btree
[ 0 ].hashval
, ARCH_CONVERT
) < INT_GET(save_node
->btree
[ 0 ].hashval
, ARCH_CONVERT
)) ||
1080 (INT_GET(drop_node
->btree
[ INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
) <
1081 INT_GET(save_node
->btree
[ INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
)))
1083 btree
= &save_node
->btree
[ INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
) ];
1084 tmp
= INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
) * (uint
)sizeof(xfs_da_node_entry_t
);
1085 memmove(btree
, &save_node
->btree
[0], tmp
);
1086 btree
= &save_node
->btree
[0];
1087 xfs_da_log_buf(tp
, save_blk
->bp
,
1088 XFS_DA_LOGRANGE(save_node
, btree
,
1089 (INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
) + INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
)) *
1090 sizeof(xfs_da_node_entry_t
)));
1092 btree
= &save_node
->btree
[ INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
) ];
1093 xfs_da_log_buf(tp
, save_blk
->bp
,
1094 XFS_DA_LOGRANGE(save_node
, btree
,
1095 INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
) *
1096 sizeof(xfs_da_node_entry_t
)));
1100 * Move all the B-tree elements from drop_blk to save_blk.
1102 tmp
= INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
) * (uint
)sizeof(xfs_da_node_entry_t
);
1103 memcpy(btree
, &drop_node
->btree
[0], tmp
);
1104 INT_MOD(save_node
->hdr
.count
, ARCH_CONVERT
, INT_GET(drop_node
->hdr
.count
, ARCH_CONVERT
));
1106 xfs_da_log_buf(tp
, save_blk
->bp
,
1107 XFS_DA_LOGRANGE(save_node
, &save_node
->hdr
,
1108 sizeof(save_node
->hdr
)));
1111 * Save the last hashval in the remaining block for upward propagation.
1113 save_blk
->hashval
= INT_GET(save_node
->btree
[ INT_GET(save_node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
1116 /*========================================================================
1117 * Routines used for finding things in the Btree.
1118 *========================================================================*/
1121 * Walk down the Btree looking for a particular filename, filling
1122 * in the state structure as we go.
1124 * We will set the state structure to point to each of the elements
1125 * in each of the nodes where either the hashval is or should be.
1127 * We support duplicate hashval's so for each entry in the current
1128 * node that could contain the desired hashval, descend. This is a
1129 * pruned depth-first tree search.
1132 xfs_da_node_lookup_int(xfs_da_state_t
*state
, int *result
)
1134 xfs_da_state_blk_t
*blk
;
1135 xfs_da_blkinfo_t
*curr
;
1136 xfs_da_intnode_t
*node
;
1137 xfs_da_node_entry_t
*btree
;
1139 int probe
, span
, max
, error
, retval
;
1140 xfs_dahash_t hashval
;
1141 xfs_da_args_t
*args
;
1146 * Descend thru the B-tree searching each level for the right
1147 * node to use, until the right hashval is found.
1149 if (args
->whichfork
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(state
->mp
))
1150 blkno
= state
->mp
->m_dirleafblk
;
1153 for (blk
= &state
->path
.blk
[0], state
->path
.active
= 1;
1154 state
->path
.active
<= XFS_DA_NODE_MAXDEPTH
;
1155 blk
++, state
->path
.active
++) {
1157 * Read the next node down in the tree.
1160 error
= xfs_da_read_buf(args
->trans
, args
->dp
, blkno
,
1161 -1, &blk
->bp
, args
->whichfork
);
1164 state
->path
.active
--;
1167 curr
= blk
->bp
->data
;
1168 ASSERT(INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
||
1169 INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
1170 INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
);
1173 * Search an intermediate node for a match.
1175 blk
->magic
= INT_GET(curr
->magic
, ARCH_CONVERT
);
1176 if (INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) {
1177 node
= blk
->bp
->data
;
1178 blk
->hashval
= INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
1181 * Binary search. (note: small blocks will skip loop)
1183 max
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
1184 probe
= span
= max
/ 2;
1185 hashval
= args
->hashval
;
1186 for (btree
= &node
->btree
[probe
]; span
> 4;
1187 btree
= &node
->btree
[probe
]) {
1189 if (INT_GET(btree
->hashval
, ARCH_CONVERT
) < hashval
)
1191 else if (INT_GET(btree
->hashval
, ARCH_CONVERT
) > hashval
)
1196 ASSERT((probe
>= 0) && (probe
< max
));
1197 ASSERT((span
<= 4) || (INT_GET(btree
->hashval
, ARCH_CONVERT
) == hashval
));
1200 * Since we may have duplicate hashval's, find the first
1201 * matching hashval in the node.
1203 while ((probe
> 0) && (INT_GET(btree
->hashval
, ARCH_CONVERT
) >= hashval
)) {
1207 while ((probe
< max
) && (INT_GET(btree
->hashval
, ARCH_CONVERT
) < hashval
)) {
1213 * Pick the right block to descend on.
1217 blkno
= INT_GET(node
->btree
[ max
-1 ].before
, ARCH_CONVERT
);
1220 blkno
= INT_GET(btree
->before
, ARCH_CONVERT
);
1224 else if (INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
) {
1225 blk
->hashval
= xfs_attr_leaf_lasthash(blk
->bp
, NULL
);
1229 else if (INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DIR_LEAF_MAGIC
) {
1230 blk
->hashval
= xfs_dir_leaf_lasthash(blk
->bp
, NULL
);
1233 else if (INT_GET(curr
->magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
) {
1234 blk
->hashval
= xfs_dir2_leafn_lasthash(blk
->bp
, NULL
);
1240 * A leaf block that ends in the hashval that we are interested in
1241 * (final hashval == search hashval) means that the next block may
1242 * contain more entries with the same hashval, shift upward to the
1243 * next leaf and keep searching.
1246 if (blk
->magic
== XFS_DIR_LEAF_MAGIC
) {
1247 ASSERT(XFS_DIR_IS_V1(state
->mp
));
1248 retval
= xfs_dir_leaf_lookup_int(blk
->bp
, args
,
1250 } else if (blk
->magic
== XFS_DIR2_LEAFN_MAGIC
) {
1251 ASSERT(XFS_DIR_IS_V2(state
->mp
));
1252 retval
= xfs_dir2_leafn_lookup_int(blk
->bp
, args
,
1253 &blk
->index
, state
);
1256 else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1257 retval
= xfs_attr_leaf_lookup_int(blk
->bp
, args
);
1258 blk
->index
= args
->index
;
1259 args
->blkno
= blk
->blkno
;
1262 if (((retval
== ENOENT
) || (retval
== ENOATTR
)) &&
1263 (blk
->hashval
== args
->hashval
)) {
1264 error
= xfs_da_path_shift(state
, &state
->path
, 1, 1,
1272 else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1273 /* path_shift() gives ENOENT */
1274 retval
= XFS_ERROR(ENOATTR
);
1284 /*========================================================================
1286 *========================================================================*/
1289 * Link a new block into a doubly linked list of blocks (of whatever type).
1292 xfs_da_blk_link(xfs_da_state_t
*state
, xfs_da_state_blk_t
*old_blk
,
1293 xfs_da_state_blk_t
*new_blk
)
1295 xfs_da_blkinfo_t
*old_info
, *new_info
, *tmp_info
;
1296 xfs_da_args_t
*args
;
1297 int before
=0, error
;
1301 * Set up environment.
1304 ASSERT(args
!= NULL
);
1305 old_info
= old_blk
->bp
->data
;
1306 new_info
= new_blk
->bp
->data
;
1307 ASSERT(old_blk
->magic
== XFS_DA_NODE_MAGIC
||
1308 old_blk
->magic
== XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
1309 old_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1310 ASSERT(old_blk
->magic
== INT_GET(old_info
->magic
, ARCH_CONVERT
));
1311 ASSERT(new_blk
->magic
== INT_GET(new_info
->magic
, ARCH_CONVERT
));
1312 ASSERT(old_blk
->magic
== new_blk
->magic
);
1314 switch (old_blk
->magic
) {
1316 case XFS_ATTR_LEAF_MAGIC
:
1317 before
= xfs_attr_leaf_order(old_blk
->bp
, new_blk
->bp
);
1320 case XFS_DIR_LEAF_MAGIC
:
1321 ASSERT(XFS_DIR_IS_V1(state
->mp
));
1322 before
= xfs_dir_leaf_order(old_blk
->bp
, new_blk
->bp
);
1324 case XFS_DIR2_LEAFN_MAGIC
:
1325 ASSERT(XFS_DIR_IS_V2(state
->mp
));
1326 before
= xfs_dir2_leafn_order(old_blk
->bp
, new_blk
->bp
);
1328 case XFS_DA_NODE_MAGIC
:
1329 before
= xfs_da_node_order(old_blk
->bp
, new_blk
->bp
);
1334 * Link blocks in appropriate order.
1338 * Link new block in before existing block.
1340 INT_SET(new_info
->forw
, ARCH_CONVERT
, old_blk
->blkno
);
1341 new_info
->back
= old_info
->back
; /* INT_: direct copy */
1342 if (INT_GET(old_info
->back
, ARCH_CONVERT
)) {
1343 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1344 INT_GET(old_info
->back
,
1345 ARCH_CONVERT
), -1, &bp
,
1350 tmp_info
= bp
->data
;
1351 ASSERT(INT_GET(tmp_info
->magic
, ARCH_CONVERT
) == INT_GET(old_info
->magic
, ARCH_CONVERT
));
1352 ASSERT(INT_GET(tmp_info
->forw
, ARCH_CONVERT
) == old_blk
->blkno
);
1353 INT_SET(tmp_info
->forw
, ARCH_CONVERT
, new_blk
->blkno
);
1354 xfs_da_log_buf(args
->trans
, bp
, 0, sizeof(*tmp_info
)-1);
1355 xfs_da_buf_done(bp
);
1357 INT_SET(old_info
->back
, ARCH_CONVERT
, new_blk
->blkno
);
1360 * Link new block in after existing block.
1362 new_info
->forw
= old_info
->forw
; /* INT_: direct copy */
1363 INT_SET(new_info
->back
, ARCH_CONVERT
, old_blk
->blkno
);
1364 if (INT_GET(old_info
->forw
, ARCH_CONVERT
)) {
1365 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1366 INT_GET(old_info
->forw
, ARCH_CONVERT
), -1, &bp
,
1371 tmp_info
= bp
->data
;
1372 ASSERT(INT_GET(tmp_info
->magic
, ARCH_CONVERT
)
1373 == INT_GET(old_info
->magic
, ARCH_CONVERT
));
1374 ASSERT(INT_GET(tmp_info
->back
, ARCH_CONVERT
)
1376 INT_SET(tmp_info
->back
, ARCH_CONVERT
, new_blk
->blkno
);
1377 xfs_da_log_buf(args
->trans
, bp
, 0, sizeof(*tmp_info
)-1);
1378 xfs_da_buf_done(bp
);
1380 INT_SET(old_info
->forw
, ARCH_CONVERT
, new_blk
->blkno
);
1383 xfs_da_log_buf(args
->trans
, old_blk
->bp
, 0, sizeof(*tmp_info
) - 1);
1384 xfs_da_log_buf(args
->trans
, new_blk
->bp
, 0, sizeof(*tmp_info
) - 1);
1389 * Compare two intermediate nodes for "order".
1392 xfs_da_node_order(xfs_dabuf_t
*node1_bp
, xfs_dabuf_t
*node2_bp
)
1394 xfs_da_intnode_t
*node1
, *node2
;
1396 node1
= node1_bp
->data
;
1397 node2
= node2_bp
->data
;
1398 ASSERT((INT_GET(node1
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) &&
1399 (INT_GET(node2
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
));
1400 if ((INT_GET(node1
->hdr
.count
, ARCH_CONVERT
) > 0) && (INT_GET(node2
->hdr
.count
, ARCH_CONVERT
) > 0) &&
1401 ((INT_GET(node2
->btree
[ 0 ].hashval
, ARCH_CONVERT
) <
1402 INT_GET(node1
->btree
[ 0 ].hashval
, ARCH_CONVERT
)) ||
1403 (INT_GET(node2
->btree
[ INT_GET(node2
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
) <
1404 INT_GET(node1
->btree
[ INT_GET(node1
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
)))) {
1411 * Pick up the last hashvalue from an intermediate node.
1414 xfs_da_node_lasthash(xfs_dabuf_t
*bp
, int *count
)
1416 xfs_da_intnode_t
*node
;
1419 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1421 *count
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
1422 if (!node
->hdr
.count
)
1424 return(INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
));
1428 * Unlink a block from a doubly linked list of blocks.
1430 STATIC
int /* error */
1431 xfs_da_blk_unlink(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
,
1432 xfs_da_state_blk_t
*save_blk
)
1434 xfs_da_blkinfo_t
*drop_info
, *save_info
, *tmp_info
;
1435 xfs_da_args_t
*args
;
1440 * Set up environment.
1443 ASSERT(args
!= NULL
);
1444 save_info
= save_blk
->bp
->data
;
1445 drop_info
= drop_blk
->bp
->data
;
1446 ASSERT(save_blk
->magic
== XFS_DA_NODE_MAGIC
||
1447 save_blk
->magic
== XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
1448 save_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1449 ASSERT(save_blk
->magic
== INT_GET(save_info
->magic
, ARCH_CONVERT
));
1450 ASSERT(drop_blk
->magic
== INT_GET(drop_info
->magic
, ARCH_CONVERT
));
1451 ASSERT(save_blk
->magic
== drop_blk
->magic
);
1452 ASSERT((INT_GET(save_info
->forw
, ARCH_CONVERT
) == drop_blk
->blkno
) ||
1453 (INT_GET(save_info
->back
, ARCH_CONVERT
) == drop_blk
->blkno
));
1454 ASSERT((INT_GET(drop_info
->forw
, ARCH_CONVERT
) == save_blk
->blkno
) ||
1455 (INT_GET(drop_info
->back
, ARCH_CONVERT
) == save_blk
->blkno
));
1458 * Unlink the leaf block from the doubly linked chain of leaves.
1460 if (INT_GET(save_info
->back
, ARCH_CONVERT
) == drop_blk
->blkno
) {
1461 save_info
->back
= drop_info
->back
; /* INT_: direct copy */
1462 if (INT_GET(drop_info
->back
, ARCH_CONVERT
)) {
1463 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1464 INT_GET(drop_info
->back
,
1465 ARCH_CONVERT
), -1, &bp
,
1470 tmp_info
= bp
->data
;
1471 ASSERT(INT_GET(tmp_info
->magic
, ARCH_CONVERT
) == INT_GET(save_info
->magic
, ARCH_CONVERT
));
1472 ASSERT(INT_GET(tmp_info
->forw
, ARCH_CONVERT
) == drop_blk
->blkno
);
1473 INT_SET(tmp_info
->forw
, ARCH_CONVERT
, save_blk
->blkno
);
1474 xfs_da_log_buf(args
->trans
, bp
, 0,
1475 sizeof(*tmp_info
) - 1);
1476 xfs_da_buf_done(bp
);
1479 save_info
->forw
= drop_info
->forw
; /* INT_: direct copy */
1480 if (INT_GET(drop_info
->forw
, ARCH_CONVERT
)) {
1481 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1482 INT_GET(drop_info
->forw
, ARCH_CONVERT
), -1, &bp
,
1487 tmp_info
= bp
->data
;
1488 ASSERT(INT_GET(tmp_info
->magic
, ARCH_CONVERT
)
1489 == INT_GET(save_info
->magic
, ARCH_CONVERT
));
1490 ASSERT(INT_GET(tmp_info
->back
, ARCH_CONVERT
)
1491 == drop_blk
->blkno
);
1492 INT_SET(tmp_info
->back
, ARCH_CONVERT
, save_blk
->blkno
);
1493 xfs_da_log_buf(args
->trans
, bp
, 0,
1494 sizeof(*tmp_info
) - 1);
1495 xfs_da_buf_done(bp
);
1499 xfs_da_log_buf(args
->trans
, save_blk
->bp
, 0, sizeof(*save_info
) - 1);
1504 * Move a path "forward" or "!forward" one block at the current level.
1506 * This routine will adjust a "path" to point to the next block
1507 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1508 * Btree, including updating pointers to the intermediate nodes between
1509 * the new bottom and the root.
1512 xfs_da_path_shift(xfs_da_state_t
*state
, xfs_da_state_path_t
*path
,
1513 int forward
, int release
, int *result
)
1515 xfs_da_state_blk_t
*blk
;
1516 xfs_da_blkinfo_t
*info
;
1517 xfs_da_intnode_t
*node
;
1518 xfs_da_args_t
*args
;
1519 xfs_dablk_t blkno
=0;
1523 * Roll up the Btree looking for the first block where our
1524 * current index is not at the edge of the block. Note that
1525 * we skip the bottom layer because we want the sibling block.
1528 ASSERT(args
!= NULL
);
1529 ASSERT(path
!= NULL
);
1530 ASSERT((path
->active
> 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1531 level
= (path
->active
-1) - 1; /* skip bottom layer in path */
1532 for (blk
= &path
->blk
[level
]; level
>= 0; blk
--, level
--) {
1533 ASSERT(blk
->bp
!= NULL
);
1534 node
= blk
->bp
->data
;
1535 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1536 if (forward
&& (blk
->index
< INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1)) {
1538 blkno
= INT_GET(node
->btree
[ blk
->index
].before
, ARCH_CONVERT
);
1540 } else if (!forward
&& (blk
->index
> 0)) {
1542 blkno
= INT_GET(node
->btree
[ blk
->index
].before
, ARCH_CONVERT
);
1547 *result
= XFS_ERROR(ENOENT
); /* we're out of our tree */
1548 ASSERT(args
->oknoent
);
1553 * Roll down the edge of the subtree until we reach the
1554 * same depth we were at originally.
1556 for (blk
++, level
++; level
< path
->active
; blk
++, level
++) {
1558 * Release the old block.
1559 * (if it's dirty, trans won't actually let go)
1562 xfs_da_brelse(args
->trans
, blk
->bp
);
1565 * Read the next child block.
1568 error
= xfs_da_read_buf(args
->trans
, args
->dp
, blkno
, -1,
1569 &blk
->bp
, args
->whichfork
);
1572 ASSERT(blk
->bp
!= NULL
);
1573 info
= blk
->bp
->data
;
1574 ASSERT(INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
||
1575 INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DIRX_LEAF_MAGIC(state
->mp
) ||
1576 INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
);
1577 blk
->magic
= INT_GET(info
->magic
, ARCH_CONVERT
);
1578 if (INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) {
1579 node
= (xfs_da_intnode_t
*)info
;
1580 blk
->hashval
= INT_GET(node
->btree
[ INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
, ARCH_CONVERT
);
1584 blk
->index
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
)-1;
1585 blkno
= INT_GET(node
->btree
[ blk
->index
].before
, ARCH_CONVERT
);
1587 ASSERT(level
== path
->active
-1);
1589 switch(blk
->magic
) {
1591 case XFS_ATTR_LEAF_MAGIC
:
1592 blk
->hashval
= xfs_attr_leaf_lasthash(blk
->bp
,
1596 case XFS_DIR_LEAF_MAGIC
:
1597 ASSERT(XFS_DIR_IS_V1(state
->mp
));
1598 blk
->hashval
= xfs_dir_leaf_lasthash(blk
->bp
,
1601 case XFS_DIR2_LEAFN_MAGIC
:
1602 ASSERT(XFS_DIR_IS_V2(state
->mp
));
1603 blk
->hashval
= xfs_dir2_leafn_lasthash(blk
->bp
,
1607 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
||
1609 XFS_DIRX_LEAF_MAGIC(state
->mp
));
1619 /*========================================================================
1621 *========================================================================*/
1624 * Implement a simple hash on a character string.
1625 * Rotate the hash value by 7 bits, then XOR each character in.
1626 * This is implemented with some source-level loop unrolling.
1629 xfs_da_hashname(uchar_t
*name
, int namelen
)
1635 * This is the old one-byte-at-a-time version.
1637 for (hash
= 0; namelen
> 0; namelen
--)
1638 hash
= *name
++ ^ rol32(hash
, 7);
1643 * Do four characters at a time as long as we can.
1645 for (hash
= 0; namelen
>= 4; namelen
-= 4, name
+= 4)
1646 hash
= (name
[0] << 21) ^ (name
[1] << 14) ^ (name
[2] << 7) ^
1647 (name
[3] << 0) ^ rol32(hash
, 7 * 4);
1650 * Now do the rest of the characters.
1654 return (name
[0] << 14) ^ (name
[1] << 7) ^ (name
[2] << 0) ^
1657 return (name
[0] << 7) ^ (name
[1] << 0) ^ rol32(hash
, 7 * 2);
1659 return (name
[0] << 0) ^ rol32(hash
, 7 * 1);
1665 return 0; /* keep gcc happy */
1669 * Add a block to the btree ahead of the file.
1670 * Return the new block number to the caller.
1673 xfs_da_grow_inode(xfs_da_args_t
*args
, xfs_dablk_t
*new_blkno
)
1675 xfs_fileoff_t bno
, b
;
1676 xfs_bmbt_irec_t map
;
1677 xfs_bmbt_irec_t
*mapp
;
1679 int nmap
, error
, w
, count
, c
, got
, i
, mapi
;
1686 w
= args
->whichfork
;
1689 * For new directories adjust the file offset and block count.
1691 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
)) {
1692 bno
= mp
->m_dirleafblk
;
1693 count
= mp
->m_dirblkfsbs
;
1699 * Find a spot in the file space to put the new block.
1701 if ((error
= xfs_bmap_first_unused(tp
, dp
, count
, &bno
, w
))) {
1704 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
))
1705 ASSERT(bno
>= mp
->m_dirleafblk
&& bno
< mp
->m_dirfreeblk
);
1707 * Try mapping it in one filesystem block.
1710 ASSERT(args
->firstblock
!= NULL
);
1711 if ((error
= xfs_bmapi(tp
, dp
, bno
, count
,
1712 XFS_BMAPI_AFLAG(w
)|XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
|
1714 args
->firstblock
, args
->total
, &map
, &nmap
,
1724 * If we didn't get it and the block might work if fragmented,
1725 * try without the CONTIG flag. Loop until we get it all.
1727 else if (nmap
== 0 && count
> 1) {
1728 mapp
= kmem_alloc(sizeof(*mapp
) * count
, KM_SLEEP
);
1729 for (b
= bno
, mapi
= 0; b
< bno
+ count
; ) {
1730 nmap
= MIN(XFS_BMAP_MAX_NMAP
, count
);
1731 c
= (int)(bno
+ count
- b
);
1732 if ((error
= xfs_bmapi(tp
, dp
, b
, c
,
1733 XFS_BMAPI_AFLAG(w
)|XFS_BMAPI_WRITE
|
1735 args
->firstblock
, args
->total
,
1736 &mapp
[mapi
], &nmap
, args
->flist
))) {
1737 kmem_free(mapp
, sizeof(*mapp
) * count
);
1743 b
= mapp
[mapi
- 1].br_startoff
+
1744 mapp
[mapi
- 1].br_blockcount
;
1751 * Count the blocks we got, make sure it matches the total.
1753 for (i
= 0, got
= 0; i
< mapi
; i
++)
1754 got
+= mapp
[i
].br_blockcount
;
1755 if (got
!= count
|| mapp
[0].br_startoff
!= bno
||
1756 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
1759 kmem_free(mapp
, sizeof(*mapp
) * count
);
1760 return XFS_ERROR(ENOSPC
);
1763 kmem_free(mapp
, sizeof(*mapp
) * count
);
1764 *new_blkno
= (xfs_dablk_t
)bno
;
1766 * For version 1 directories, adjust the file size if it changed.
1768 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V1(mp
)) {
1770 if ((error
= xfs_bmap_last_offset(tp
, dp
, &bno
, w
)))
1772 size
= XFS_FSB_TO_B(mp
, bno
);
1773 if (size
!= dp
->i_d
.di_size
) {
1774 dp
->i_d
.di_size
= size
;
1775 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1782 * Ick. We need to always be able to remove a btree block, even
1783 * if there's no space reservation because the filesystem is full.
1784 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
1785 * It swaps the target block with the last block in the file. The
1786 * last block in the file can always be removed since it can't cause
1787 * a bmap btree split to do that.
1790 xfs_da_swap_lastblock(xfs_da_args_t
*args
, xfs_dablk_t
*dead_blknop
,
1791 xfs_dabuf_t
**dead_bufp
)
1793 xfs_dablk_t dead_blkno
, last_blkno
, sib_blkno
, par_blkno
;
1794 xfs_dabuf_t
*dead_buf
, *last_buf
, *sib_buf
, *par_buf
;
1795 xfs_fileoff_t lastoff
;
1799 int error
, w
, entno
, level
, dead_level
;
1800 xfs_da_blkinfo_t
*dead_info
, *sib_info
;
1801 xfs_da_intnode_t
*par_node
, *dead_node
;
1802 xfs_dir_leafblock_t
*dead_leaf
;
1803 xfs_dir2_leaf_t
*dead_leaf2
;
1804 xfs_dahash_t dead_hash
;
1806 dead_buf
= *dead_bufp
;
1807 dead_blkno
= *dead_blknop
;
1810 w
= args
->whichfork
;
1811 ASSERT(w
== XFS_DATA_FORK
);
1813 if (XFS_DIR_IS_V2(mp
)) {
1814 lastoff
= mp
->m_dirfreeblk
;
1815 error
= xfs_bmap_last_before(tp
, ip
, &lastoff
, w
);
1817 error
= xfs_bmap_last_offset(tp
, ip
, &lastoff
, w
);
1820 if (unlikely(lastoff
== 0)) {
1821 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW
,
1823 return XFS_ERROR(EFSCORRUPTED
);
1826 * Read the last block in the btree space.
1828 last_blkno
= (xfs_dablk_t
)lastoff
- mp
->m_dirblkfsbs
;
1829 if ((error
= xfs_da_read_buf(tp
, ip
, last_blkno
, -1, &last_buf
, w
)))
1832 * Copy the last block into the dead buffer and log it.
1834 memcpy(dead_buf
->data
, last_buf
->data
, mp
->m_dirblksize
);
1835 xfs_da_log_buf(tp
, dead_buf
, 0, mp
->m_dirblksize
- 1);
1836 dead_info
= dead_buf
->data
;
1838 * Get values from the moved block.
1840 if (INT_GET(dead_info
->magic
, ARCH_CONVERT
) == XFS_DIR_LEAF_MAGIC
) {
1841 ASSERT(XFS_DIR_IS_V1(mp
));
1842 dead_leaf
= (xfs_dir_leafblock_t
*)dead_info
;
1845 INT_GET(dead_leaf
->entries
[INT_GET(dead_leaf
->hdr
.count
, ARCH_CONVERT
) - 1].hashval
, ARCH_CONVERT
);
1846 } else if (INT_GET(dead_info
->magic
, ARCH_CONVERT
) == XFS_DIR2_LEAFN_MAGIC
) {
1847 ASSERT(XFS_DIR_IS_V2(mp
));
1848 dead_leaf2
= (xfs_dir2_leaf_t
*)dead_info
;
1850 dead_hash
= INT_GET(dead_leaf2
->ents
[INT_GET(dead_leaf2
->hdr
.count
, ARCH_CONVERT
) - 1].hashval
, ARCH_CONVERT
);
1852 ASSERT(INT_GET(dead_info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
);
1853 dead_node
= (xfs_da_intnode_t
*)dead_info
;
1854 dead_level
= INT_GET(dead_node
->hdr
.level
, ARCH_CONVERT
);
1855 dead_hash
= INT_GET(dead_node
->btree
[INT_GET(dead_node
->hdr
.count
, ARCH_CONVERT
) - 1].hashval
, ARCH_CONVERT
);
1857 sib_buf
= par_buf
= NULL
;
1859 * If the moved block has a left sibling, fix up the pointers.
1861 if ((sib_blkno
= INT_GET(dead_info
->back
, ARCH_CONVERT
))) {
1862 if ((error
= xfs_da_read_buf(tp
, ip
, sib_blkno
, -1, &sib_buf
, w
)))
1864 sib_info
= sib_buf
->data
;
1866 INT_GET(sib_info
->forw
, ARCH_CONVERT
) != last_blkno
||
1867 INT_GET(sib_info
->magic
, ARCH_CONVERT
) != INT_GET(dead_info
->magic
, ARCH_CONVERT
))) {
1868 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
1869 XFS_ERRLEVEL_LOW
, mp
);
1870 error
= XFS_ERROR(EFSCORRUPTED
);
1873 INT_SET(sib_info
->forw
, ARCH_CONVERT
, dead_blkno
);
1874 xfs_da_log_buf(tp
, sib_buf
,
1875 XFS_DA_LOGRANGE(sib_info
, &sib_info
->forw
,
1876 sizeof(sib_info
->forw
)));
1877 xfs_da_buf_done(sib_buf
);
1881 * If the moved block has a right sibling, fix up the pointers.
1883 if ((sib_blkno
= INT_GET(dead_info
->forw
, ARCH_CONVERT
))) {
1884 if ((error
= xfs_da_read_buf(tp
, ip
, sib_blkno
, -1, &sib_buf
, w
)))
1886 sib_info
= sib_buf
->data
;
1888 INT_GET(sib_info
->back
, ARCH_CONVERT
) != last_blkno
1889 || INT_GET(sib_info
->magic
, ARCH_CONVERT
)
1890 != INT_GET(dead_info
->magic
, ARCH_CONVERT
))) {
1891 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
1892 XFS_ERRLEVEL_LOW
, mp
);
1893 error
= XFS_ERROR(EFSCORRUPTED
);
1896 INT_SET(sib_info
->back
, ARCH_CONVERT
, dead_blkno
);
1897 xfs_da_log_buf(tp
, sib_buf
,
1898 XFS_DA_LOGRANGE(sib_info
, &sib_info
->back
,
1899 sizeof(sib_info
->back
)));
1900 xfs_da_buf_done(sib_buf
);
1903 par_blkno
= XFS_DIR_IS_V1(mp
) ? 0 : mp
->m_dirleafblk
;
1906 * Walk down the tree looking for the parent of the moved block.
1909 if ((error
= xfs_da_read_buf(tp
, ip
, par_blkno
, -1, &par_buf
, w
)))
1911 par_node
= par_buf
->data
;
1913 INT_GET(par_node
->hdr
.info
.magic
, ARCH_CONVERT
) != XFS_DA_NODE_MAGIC
||
1914 (level
>= 0 && level
!= INT_GET(par_node
->hdr
.level
, ARCH_CONVERT
) + 1))) {
1915 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
1916 XFS_ERRLEVEL_LOW
, mp
);
1917 error
= XFS_ERROR(EFSCORRUPTED
);
1920 level
= INT_GET(par_node
->hdr
.level
, ARCH_CONVERT
);
1922 entno
< INT_GET(par_node
->hdr
.count
, ARCH_CONVERT
) &&
1923 INT_GET(par_node
->btree
[entno
].hashval
, ARCH_CONVERT
) < dead_hash
;
1926 if (unlikely(entno
== INT_GET(par_node
->hdr
.count
, ARCH_CONVERT
))) {
1927 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
1928 XFS_ERRLEVEL_LOW
, mp
);
1929 error
= XFS_ERROR(EFSCORRUPTED
);
1932 par_blkno
= INT_GET(par_node
->btree
[entno
].before
, ARCH_CONVERT
);
1933 if (level
== dead_level
+ 1)
1935 xfs_da_brelse(tp
, par_buf
);
1939 * We're in the right parent block.
1940 * Look for the right entry.
1944 entno
< INT_GET(par_node
->hdr
.count
, ARCH_CONVERT
) &&
1945 INT_GET(par_node
->btree
[entno
].before
, ARCH_CONVERT
) != last_blkno
;
1948 if (entno
< INT_GET(par_node
->hdr
.count
, ARCH_CONVERT
))
1950 par_blkno
= INT_GET(par_node
->hdr
.info
.forw
, ARCH_CONVERT
);
1951 xfs_da_brelse(tp
, par_buf
);
1953 if (unlikely(par_blkno
== 0)) {
1954 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
1955 XFS_ERRLEVEL_LOW
, mp
);
1956 error
= XFS_ERROR(EFSCORRUPTED
);
1959 if ((error
= xfs_da_read_buf(tp
, ip
, par_blkno
, -1, &par_buf
, w
)))
1961 par_node
= par_buf
->data
;
1963 INT_GET(par_node
->hdr
.level
, ARCH_CONVERT
) != level
||
1964 INT_GET(par_node
->hdr
.info
.magic
, ARCH_CONVERT
) != XFS_DA_NODE_MAGIC
)) {
1965 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
1966 XFS_ERRLEVEL_LOW
, mp
);
1967 error
= XFS_ERROR(EFSCORRUPTED
);
1973 * Update the parent entry pointing to the moved block.
1975 INT_SET(par_node
->btree
[entno
].before
, ARCH_CONVERT
, dead_blkno
);
1976 xfs_da_log_buf(tp
, par_buf
,
1977 XFS_DA_LOGRANGE(par_node
, &par_node
->btree
[entno
].before
,
1978 sizeof(par_node
->btree
[entno
].before
)));
1979 xfs_da_buf_done(par_buf
);
1980 xfs_da_buf_done(dead_buf
);
1981 *dead_blknop
= last_blkno
;
1982 *dead_bufp
= last_buf
;
1986 xfs_da_brelse(tp
, par_buf
);
1988 xfs_da_brelse(tp
, sib_buf
);
1989 xfs_da_brelse(tp
, last_buf
);
1994 * Remove a btree block from a directory or attribute.
1997 xfs_da_shrink_inode(xfs_da_args_t
*args
, xfs_dablk_t dead_blkno
,
1998 xfs_dabuf_t
*dead_buf
)
2001 int done
, error
, w
, count
;
2008 w
= args
->whichfork
;
2011 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
))
2012 count
= mp
->m_dirblkfsbs
;
2017 * Remove extents. If we get ENOSPC for a dir we have to move
2018 * the last block to the place we want to kill.
2020 if ((error
= xfs_bunmapi(tp
, dp
, dead_blkno
, count
,
2021 XFS_BMAPI_AFLAG(w
)|XFS_BMAPI_METADATA
,
2022 0, args
->firstblock
, args
->flist
,
2023 &done
)) == ENOSPC
) {
2024 if (w
!= XFS_DATA_FORK
)
2026 if ((error
= xfs_da_swap_lastblock(args
, &dead_blkno
,
2035 xfs_da_binval(tp
, dead_buf
);
2037 * Adjust the directory size for version 1.
2039 if (w
== XFS_DATA_FORK
&& XFS_DIR_IS_V1(mp
)) {
2040 if ((error
= xfs_bmap_last_offset(tp
, dp
, &bno
, w
)))
2042 size
= XFS_FSB_TO_B(dp
->i_mount
, bno
);
2043 if (size
!= dp
->i_d
.di_size
) {
2044 dp
->i_d
.di_size
= size
;
2045 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2050 xfs_da_binval(tp
, dead_buf
);
2055 * See if the mapping(s) for this btree block are valid, i.e.
2056 * don't contain holes, are logically contiguous, and cover the whole range.
2059 xfs_da_map_covers_blocks(
2061 xfs_bmbt_irec_t
*mapp
,
2068 for (i
= 0, off
= bno
; i
< nmap
; i
++) {
2069 if (mapp
[i
].br_startblock
== HOLESTARTBLOCK
||
2070 mapp
[i
].br_startblock
== DELAYSTARTBLOCK
) {
2073 if (off
!= mapp
[i
].br_startoff
) {
2076 off
+= mapp
[i
].br_blockcount
;
2078 return off
== bno
+ count
;
2083 * Used for get_buf, read_buf, read_bufr, and reada_buf.
2090 xfs_daddr_t
*mappedbnop
,
2096 xfs_buf_t
*bp
= NULL
;
2100 xfs_bmbt_irec_t map
;
2101 xfs_bmbt_irec_t
*mapp
;
2102 xfs_daddr_t mappedbno
;
2110 if (whichfork
== XFS_DATA_FORK
&& XFS_DIR_IS_V2(mp
))
2111 nfsb
= mp
->m_dirblkfsbs
;
2114 mappedbno
= *mappedbnop
;
2116 * Caller doesn't have a mapping. -2 means don't complain
2117 * if we land in a hole.
2119 if (mappedbno
== -1 || mappedbno
== -2) {
2121 * Optimize the one-block case.
2127 xfs_bmapi_single(trans
, dp
, whichfork
, &fsb
,
2128 (xfs_fileoff_t
)bno
))) {
2132 if (fsb
== NULLFSBLOCK
) {
2135 map
.br_startblock
= fsb
;
2136 map
.br_startoff
= (xfs_fileoff_t
)bno
;
2137 map
.br_blockcount
= 1;
2141 mapp
= kmem_alloc(sizeof(*mapp
) * nfsb
, KM_SLEEP
);
2143 if ((error
= xfs_bmapi(trans
, dp
, (xfs_fileoff_t
)bno
,
2145 XFS_BMAPI_METADATA
|
2146 XFS_BMAPI_AFLAG(whichfork
),
2147 NULL
, 0, mapp
, &nmap
, NULL
)))
2151 map
.br_startblock
= XFS_DADDR_TO_FSB(mp
, mappedbno
);
2152 map
.br_startoff
= (xfs_fileoff_t
)bno
;
2153 map
.br_blockcount
= nfsb
;
2157 if (!xfs_da_map_covers_blocks(nmap
, mapp
, bno
, nfsb
)) {
2158 error
= mappedbno
== -2 ? 0 : XFS_ERROR(EFSCORRUPTED
);
2159 if (unlikely(error
== EFSCORRUPTED
)) {
2160 if (xfs_error_level
>= XFS_ERRLEVEL_LOW
) {
2162 cmn_err(CE_ALERT
, "xfs_da_do_buf: bno %lld\n",
2164 cmn_err(CE_ALERT
, "dir: inode %lld\n",
2165 (long long)dp
->i_ino
);
2166 for (i
= 0; i
< nmap
; i
++) {
2168 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d\n",
2170 (long long)mapp
[i
].br_startoff
,
2171 (long long)mapp
[i
].br_startblock
,
2172 (long long)mapp
[i
].br_blockcount
,
2176 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2177 XFS_ERRLEVEL_LOW
, mp
);
2181 if (caller
!= 3 && nmap
> 1) {
2182 bplist
= kmem_alloc(sizeof(*bplist
) * nmap
, KM_SLEEP
);
2187 * Turn the mapping(s) into buffer(s).
2189 for (i
= 0; i
< nmap
; i
++) {
2192 mappedbno
= XFS_FSB_TO_DADDR(mp
, mapp
[i
].br_startblock
);
2194 *mappedbnop
= mappedbno
;
2195 nmapped
= (int)XFS_FSB_TO_BB(mp
, mapp
[i
].br_blockcount
);
2198 bp
= xfs_trans_get_buf(trans
, mp
->m_ddev_targp
,
2199 mappedbno
, nmapped
, 0);
2200 error
= bp
? XFS_BUF_GETERROR(bp
) : XFS_ERROR(EIO
);
2207 error
= xfs_trans_read_buf(mp
, trans
, mp
->m_ddev_targp
,
2208 mappedbno
, nmapped
, 0, &bp
);
2212 xfs_baread(mp
->m_ddev_targp
, mappedbno
, nmapped
);
2220 xfs_trans_brelse(trans
, bp
);
2226 if (whichfork
== XFS_ATTR_FORK
) {
2227 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_ATTR_BTREE
,
2228 XFS_ATTR_BTREE_REF
);
2230 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_DIR_BTREE
,
2235 bplist
[nbplist
++] = bp
;
2239 * Build a dabuf structure.
2242 rbp
= xfs_da_buf_make(nbplist
, bplist
, ra
);
2244 rbp
= xfs_da_buf_make(1, &bp
, ra
);
2248 * For read_buf, check the magic number.
2251 xfs_dir2_data_t
*data
;
2252 xfs_dir2_free_t
*free
;
2253 xfs_da_blkinfo_t
*info
;
2259 magic
= INT_GET(info
->magic
, ARCH_CONVERT
);
2260 magic1
= INT_GET(data
->hdr
.magic
, ARCH_CONVERT
);
2262 XFS_TEST_ERROR((magic
!= XFS_DA_NODE_MAGIC
) &&
2263 (magic
!= XFS_DIR_LEAF_MAGIC
) &&
2264 (magic
!= XFS_ATTR_LEAF_MAGIC
) &&
2265 (magic
!= XFS_DIR2_LEAF1_MAGIC
) &&
2266 (magic
!= XFS_DIR2_LEAFN_MAGIC
) &&
2267 (magic1
!= XFS_DIR2_BLOCK_MAGIC
) &&
2268 (magic1
!= XFS_DIR2_DATA_MAGIC
) &&
2269 (INT_GET(free
->hdr
.magic
, ARCH_CONVERT
) != XFS_DIR2_FREE_MAGIC
),
2270 mp
, XFS_ERRTAG_DA_READ_BUF
,
2271 XFS_RANDOM_DA_READ_BUF
))) {
2272 xfs_buftrace("DA READ ERROR", rbp
->bps
[0]);
2273 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2274 XFS_ERRLEVEL_LOW
, mp
, info
);
2275 error
= XFS_ERROR(EFSCORRUPTED
);
2276 xfs_da_brelse(trans
, rbp
);
2282 kmem_free(bplist
, sizeof(*bplist
) * nmap
);
2285 kmem_free(mapp
, sizeof(*mapp
) * nfsb
);
2292 for (i
= 0; i
< nbplist
; i
++)
2293 xfs_trans_brelse(trans
, bplist
[i
]);
2294 kmem_free(bplist
, sizeof(*bplist
) * nmap
);
2298 kmem_free(mapp
, sizeof(*mapp
) * nfsb
);
2305 * Get a buffer for the dir/attr block.
2312 xfs_daddr_t mappedbno
,
2316 return xfs_da_do_buf(trans
, dp
, bno
, &mappedbno
, bpp
, whichfork
, 0,
2317 (inst_t
*)__return_address
);
2321 * Get a buffer for the dir/attr block, fill in the contents.
2328 xfs_daddr_t mappedbno
,
2332 return xfs_da_do_buf(trans
, dp
, bno
, &mappedbno
, bpp
, whichfork
, 1,
2333 (inst_t
*)__return_address
);
2337 * Readahead the dir/attr block.
2349 if (xfs_da_do_buf(trans
, dp
, bno
, &rval
, NULL
, whichfork
, 3,
2350 (inst_t
*)__return_address
))
2357 * Calculate the number of bits needed to hold i different values.
2360 xfs_da_log2_roundup(uint i
)
2364 for (rval
= 0; rval
< NBBY
* sizeof(i
); rval
++) {
2365 if ((1 << rval
) >= i
)
2371 kmem_zone_t
*xfs_da_state_zone
; /* anchor for state struct zone */
2372 kmem_zone_t
*xfs_dabuf_zone
; /* dabuf zone */
2375 * Allocate a dir-state structure.
2376 * We don't put them on the stack since they're large.
2379 xfs_da_state_alloc(void)
2381 return kmem_zone_zalloc(xfs_da_state_zone
, KM_SLEEP
);
2385 * Kill the altpath contents of a da-state structure.
2388 xfs_da_state_kill_altpath(xfs_da_state_t
*state
)
2392 for (i
= 0; i
< state
->altpath
.active
; i
++) {
2393 if (state
->altpath
.blk
[i
].bp
) {
2394 if (state
->altpath
.blk
[i
].bp
!= state
->path
.blk
[i
].bp
)
2395 xfs_da_buf_done(state
->altpath
.blk
[i
].bp
);
2396 state
->altpath
.blk
[i
].bp
= NULL
;
2399 state
->altpath
.active
= 0;
2403 * Free a da-state structure.
2406 xfs_da_state_free(xfs_da_state_t
*state
)
2410 xfs_da_state_kill_altpath(state
);
2411 for (i
= 0; i
< state
->path
.active
; i
++) {
2412 if (state
->path
.blk
[i
].bp
)
2413 xfs_da_buf_done(state
->path
.blk
[i
].bp
);
2415 if (state
->extravalid
&& state
->extrablk
.bp
)
2416 xfs_da_buf_done(state
->extrablk
.bp
);
2418 memset((char *)state
, 0, sizeof(*state
));
2420 kmem_zone_free(xfs_da_state_zone
, state
);
2423 #ifdef XFS_DABUF_DEBUG
2424 xfs_dabuf_t
*xfs_dabuf_global_list
;
2425 lock_t xfs_dabuf_global_lock
;
2432 STATIC xfs_dabuf_t
*
2433 xfs_da_buf_make(int nbuf
, xfs_buf_t
**bps
, inst_t
*ra
)
2441 dabuf
= kmem_zone_alloc(xfs_dabuf_zone
, KM_SLEEP
);
2443 dabuf
= kmem_alloc(XFS_DA_BUF_SIZE(nbuf
), KM_SLEEP
);
2445 #ifdef XFS_DABUF_DEBUG
2447 dabuf
->target
= XFS_BUF_TARGET(bps
[0]);
2448 dabuf
->blkno
= XFS_BUF_ADDR(bps
[0]);
2453 dabuf
->bbcount
= (short)BTOBB(XFS_BUF_COUNT(bp
));
2454 dabuf
->data
= XFS_BUF_PTR(bp
);
2458 for (i
= 0, dabuf
->bbcount
= 0; i
< nbuf
; i
++) {
2459 dabuf
->bps
[i
] = bp
= bps
[i
];
2460 dabuf
->bbcount
+= BTOBB(XFS_BUF_COUNT(bp
));
2462 dabuf
->data
= kmem_alloc(BBTOB(dabuf
->bbcount
), KM_SLEEP
);
2463 for (i
= off
= 0; i
< nbuf
; i
++, off
+= XFS_BUF_COUNT(bp
)) {
2465 memcpy((char *)dabuf
->data
+ off
, XFS_BUF_PTR(bp
),
2469 #ifdef XFS_DABUF_DEBUG
2474 s
= mutex_spinlock(&xfs_dabuf_global_lock
);
2475 for (p
= xfs_dabuf_global_list
; p
; p
= p
->next
) {
2476 ASSERT(p
->blkno
!= dabuf
->blkno
||
2477 p
->target
!= dabuf
->target
);
2480 if (xfs_dabuf_global_list
)
2481 xfs_dabuf_global_list
->prev
= dabuf
;
2482 dabuf
->next
= xfs_dabuf_global_list
;
2483 xfs_dabuf_global_list
= dabuf
;
2484 mutex_spinunlock(&xfs_dabuf_global_lock
, s
);
2494 xfs_da_buf_clean(xfs_dabuf_t
*dabuf
)
2501 ASSERT(dabuf
->nbuf
> 1);
2503 for (i
= off
= 0; i
< dabuf
->nbuf
;
2504 i
++, off
+= XFS_BUF_COUNT(bp
)) {
2506 memcpy(XFS_BUF_PTR(bp
), (char *)dabuf
->data
+ off
,
2516 xfs_da_buf_done(xfs_dabuf_t
*dabuf
)
2519 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2521 xfs_da_buf_clean(dabuf
);
2522 if (dabuf
->nbuf
> 1)
2523 kmem_free(dabuf
->data
, BBTOB(dabuf
->bbcount
));
2524 #ifdef XFS_DABUF_DEBUG
2528 s
= mutex_spinlock(&xfs_dabuf_global_lock
);
2530 dabuf
->prev
->next
= dabuf
->next
;
2532 xfs_dabuf_global_list
= dabuf
->next
;
2534 dabuf
->next
->prev
= dabuf
->prev
;
2535 mutex_spinunlock(&xfs_dabuf_global_lock
, s
);
2537 memset(dabuf
, 0, XFS_DA_BUF_SIZE(dabuf
->nbuf
));
2539 if (dabuf
->nbuf
== 1)
2540 kmem_zone_free(xfs_dabuf_zone
, dabuf
);
2542 kmem_free(dabuf
, XFS_DA_BUF_SIZE(dabuf
->nbuf
));
2546 * Log transaction from a dabuf.
2549 xfs_da_log_buf(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
, uint first
, uint last
)
2557 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2558 if (dabuf
->nbuf
== 1) {
2559 ASSERT(dabuf
->data
== (void *)XFS_BUF_PTR(dabuf
->bps
[0]));
2560 xfs_trans_log_buf(tp
, dabuf
->bps
[0], first
, last
);
2564 ASSERT(first
<= last
);
2565 for (i
= off
= 0; i
< dabuf
->nbuf
; i
++, off
+= XFS_BUF_COUNT(bp
)) {
2568 l
= f
+ XFS_BUF_COUNT(bp
) - 1;
2574 xfs_trans_log_buf(tp
, bp
, f
- off
, l
- off
);
2576 * B_DONE is set by xfs_trans_log buf.
2577 * If we don't set it on a new buffer (get not read)
2578 * then if we don't put anything in the buffer it won't
2579 * be set, and at commit it it released into the cache,
2580 * and then a read will fail.
2582 else if (!(XFS_BUF_ISDONE(bp
)))
2589 * Release dabuf from a transaction.
2590 * Have to free up the dabuf before the buffers are released,
2591 * since the synchronization on the dabuf is really the lock on the buffer.
2594 xfs_da_brelse(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
)
2601 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2602 if ((nbuf
= dabuf
->nbuf
) == 1) {
2606 bplist
= kmem_alloc(nbuf
* sizeof(*bplist
), KM_SLEEP
);
2607 memcpy(bplist
, dabuf
->bps
, nbuf
* sizeof(*bplist
));
2609 xfs_da_buf_done(dabuf
);
2610 for (i
= 0; i
< nbuf
; i
++)
2611 xfs_trans_brelse(tp
, bplist
[i
]);
2613 kmem_free(bplist
, nbuf
* sizeof(*bplist
));
2617 * Invalidate dabuf from a transaction.
2620 xfs_da_binval(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
)
2627 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2628 if ((nbuf
= dabuf
->nbuf
) == 1) {
2632 bplist
= kmem_alloc(nbuf
* sizeof(*bplist
), KM_SLEEP
);
2633 memcpy(bplist
, dabuf
->bps
, nbuf
* sizeof(*bplist
));
2635 xfs_da_buf_done(dabuf
);
2636 for (i
= 0; i
< nbuf
; i
++)
2637 xfs_trans_binval(tp
, bplist
[i
]);
2639 kmem_free(bplist
, nbuf
* sizeof(*bplist
));
2643 * Get the first daddr from a dabuf.
2646 xfs_da_blkno(xfs_dabuf_t
*dabuf
)
2648 ASSERT(dabuf
->nbuf
);
2649 ASSERT(dabuf
->data
);
2650 return XFS_BUF_ADDR(dabuf
->bps
[0]);