2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_alloc.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_dir2_data.h"
40 #include "xfs_dir2_leaf.h"
41 #include "xfs_dir2_block.h"
42 #include "xfs_dir2_node.h"
43 #include "xfs_error.h"
44 #include "xfs_trace.h"
49 * Routines to implement directories as Btrees of hashed names.
52 /*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
57 * Routines used for growing the Btree.
59 STATIC
int xfs_da_root_split(xfs_da_state_t
*state
,
60 xfs_da_state_blk_t
*existing_root
,
61 xfs_da_state_blk_t
*new_child
);
62 STATIC
int xfs_da_node_split(xfs_da_state_t
*state
,
63 xfs_da_state_blk_t
*existing_blk
,
64 xfs_da_state_blk_t
*split_blk
,
65 xfs_da_state_blk_t
*blk_to_add
,
68 STATIC
void xfs_da_node_rebalance(xfs_da_state_t
*state
,
69 xfs_da_state_blk_t
*node_blk_1
,
70 xfs_da_state_blk_t
*node_blk_2
);
71 STATIC
void xfs_da_node_add(xfs_da_state_t
*state
,
72 xfs_da_state_blk_t
*old_node_blk
,
73 xfs_da_state_blk_t
*new_node_blk
);
76 * Routines used for shrinking the Btree.
78 STATIC
int xfs_da_root_join(xfs_da_state_t
*state
,
79 xfs_da_state_blk_t
*root_blk
);
80 STATIC
int xfs_da_node_toosmall(xfs_da_state_t
*state
, int *retval
);
81 STATIC
void xfs_da_node_remove(xfs_da_state_t
*state
,
82 xfs_da_state_blk_t
*drop_blk
);
83 STATIC
void xfs_da_node_unbalance(xfs_da_state_t
*state
,
84 xfs_da_state_blk_t
*src_node_blk
,
85 xfs_da_state_blk_t
*dst_node_blk
);
90 STATIC uint
xfs_da_node_lasthash(xfs_dabuf_t
*bp
, int *count
);
91 STATIC
int xfs_da_node_order(xfs_dabuf_t
*node1_bp
, xfs_dabuf_t
*node2_bp
);
92 STATIC xfs_dabuf_t
*xfs_da_buf_make(int nbuf
, xfs_buf_t
**bps
, inst_t
*ra
);
93 STATIC
int xfs_da_blk_unlink(xfs_da_state_t
*state
,
94 xfs_da_state_blk_t
*drop_blk
,
95 xfs_da_state_blk_t
*save_blk
);
96 STATIC
void xfs_da_state_kill_altpath(xfs_da_state_t
*state
);
98 /*========================================================================
99 * Routines used for growing the Btree.
100 *========================================================================*/
103 * Create the initial contents of an intermediate node.
106 xfs_da_node_create(xfs_da_args_t
*args
, xfs_dablk_t blkno
, int level
,
107 xfs_dabuf_t
**bpp
, int whichfork
)
109 xfs_da_intnode_t
*node
;
115 error
= xfs_da_get_buf(tp
, args
->dp
, blkno
, -1, &bp
, whichfork
);
120 node
->hdr
.info
.forw
= 0;
121 node
->hdr
.info
.back
= 0;
122 node
->hdr
.info
.magic
= cpu_to_be16(XFS_DA_NODE_MAGIC
);
123 node
->hdr
.info
.pad
= 0;
125 node
->hdr
.level
= cpu_to_be16(level
);
127 xfs_da_log_buf(tp
, bp
,
128 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
135 * Split a leaf node, rebalance, then possibly split
136 * intermediate nodes, rebalance, etc.
139 xfs_da_split(xfs_da_state_t
*state
)
141 xfs_da_state_blk_t
*oldblk
, *newblk
, *addblk
;
142 xfs_da_intnode_t
*node
;
144 int max
, action
, error
, i
;
147 * Walk back up the tree splitting/inserting/adjusting as necessary.
148 * If we need to insert and there isn't room, split the node, then
149 * decide which fragment to insert the new block from below into.
150 * Note that we may split the root this way, but we need more fixup.
152 max
= state
->path
.active
- 1;
153 ASSERT((max
>= 0) && (max
< XFS_DA_NODE_MAXDEPTH
));
154 ASSERT(state
->path
.blk
[max
].magic
== XFS_ATTR_LEAF_MAGIC
||
155 state
->path
.blk
[max
].magic
== XFS_DIR2_LEAFN_MAGIC
);
157 addblk
= &state
->path
.blk
[max
]; /* initial dummy value */
158 for (i
= max
; (i
>= 0) && addblk
; state
->path
.active
--, i
--) {
159 oldblk
= &state
->path
.blk
[i
];
160 newblk
= &state
->altpath
.blk
[i
];
163 * If a leaf node then
164 * Allocate a new leaf node, then rebalance across them.
165 * else if an intermediate node then
166 * We split on the last layer, must we split the node?
168 switch (oldblk
->magic
) {
169 case XFS_ATTR_LEAF_MAGIC
:
170 error
= xfs_attr_leaf_split(state
, oldblk
, newblk
);
171 if ((error
!= 0) && (error
!= ENOSPC
)) {
172 return(error
); /* GROT: attr is inconsistent */
179 * Entry wouldn't fit, split the leaf again.
181 state
->extravalid
= 1;
183 state
->extraafter
= 0; /* before newblk */
184 error
= xfs_attr_leaf_split(state
, oldblk
,
187 state
->extraafter
= 1; /* after newblk */
188 error
= xfs_attr_leaf_split(state
, newblk
,
192 return(error
); /* GROT: attr inconsistent */
195 case XFS_DIR2_LEAFN_MAGIC
:
196 error
= xfs_dir2_leafn_split(state
, oldblk
, newblk
);
201 case XFS_DA_NODE_MAGIC
:
202 error
= xfs_da_node_split(state
, oldblk
, newblk
, addblk
,
204 xfs_da_buf_done(addblk
->bp
);
207 return(error
); /* GROT: dir is inconsistent */
209 * Record the newly split block for the next time thru?
219 * Update the btree to show the new hashval for this child.
221 xfs_da_fixhashpath(state
, &state
->path
);
223 * If we won't need this block again, it's getting dropped
224 * from the active path by the loop control, so we need
225 * to mark it done now.
227 if (i
> 0 || !addblk
)
228 xfs_da_buf_done(oldblk
->bp
);
234 * Split the root node.
236 ASSERT(state
->path
.active
== 0);
237 oldblk
= &state
->path
.blk
[0];
238 error
= xfs_da_root_split(state
, oldblk
, addblk
);
240 xfs_da_buf_done(oldblk
->bp
);
241 xfs_da_buf_done(addblk
->bp
);
243 return(error
); /* GROT: dir is inconsistent */
247 * Update pointers to the node which used to be block 0 and
248 * just got bumped because of the addition of a new root node.
249 * There might be three blocks involved if a double split occurred,
250 * and the original block 0 could be at any position in the list.
253 node
= oldblk
->bp
->data
;
254 if (node
->hdr
.info
.forw
) {
255 if (be32_to_cpu(node
->hdr
.info
.forw
) == addblk
->blkno
) {
258 ASSERT(state
->extravalid
);
259 bp
= state
->extrablk
.bp
;
262 node
->hdr
.info
.back
= cpu_to_be32(oldblk
->blkno
);
263 xfs_da_log_buf(state
->args
->trans
, bp
,
264 XFS_DA_LOGRANGE(node
, &node
->hdr
.info
,
265 sizeof(node
->hdr
.info
)));
267 node
= oldblk
->bp
->data
;
268 if (node
->hdr
.info
.back
) {
269 if (be32_to_cpu(node
->hdr
.info
.back
) == addblk
->blkno
) {
272 ASSERT(state
->extravalid
);
273 bp
= state
->extrablk
.bp
;
276 node
->hdr
.info
.forw
= cpu_to_be32(oldblk
->blkno
);
277 xfs_da_log_buf(state
->args
->trans
, bp
,
278 XFS_DA_LOGRANGE(node
, &node
->hdr
.info
,
279 sizeof(node
->hdr
.info
)));
281 xfs_da_buf_done(oldblk
->bp
);
282 xfs_da_buf_done(addblk
->bp
);
288 * Split the root. We have to create a new root and point to the two
289 * parts (the split old root) that we just created. Copy block zero to
290 * the EOF, extending the inode in process.
292 STATIC
int /* error */
293 xfs_da_root_split(xfs_da_state_t
*state
, xfs_da_state_blk_t
*blk1
,
294 xfs_da_state_blk_t
*blk2
)
296 xfs_da_intnode_t
*node
, *oldroot
;
304 xfs_dir2_leaf_t
*leaf
;
307 * Copy the existing (incorrect) block from the root node position
308 * to a free space somewhere.
311 ASSERT(args
!= NULL
);
312 error
= xfs_da_grow_inode(args
, &blkno
);
318 error
= xfs_da_get_buf(tp
, dp
, blkno
, -1, &bp
, args
->whichfork
);
323 oldroot
= blk1
->bp
->data
;
324 if (be16_to_cpu(oldroot
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
) {
325 size
= (int)((char *)&oldroot
->btree
[be16_to_cpu(oldroot
->hdr
.count
)] -
328 ASSERT(be16_to_cpu(oldroot
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
329 leaf
= (xfs_dir2_leaf_t
*)oldroot
;
330 size
= (int)((char *)&leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
)] -
333 memcpy(node
, oldroot
, size
);
334 xfs_da_log_buf(tp
, bp
, 0, size
- 1);
335 xfs_da_buf_done(blk1
->bp
);
340 * Set up the new root node.
342 error
= xfs_da_node_create(args
,
343 (args
->whichfork
== XFS_DATA_FORK
) ? mp
->m_dirleafblk
: 0,
344 be16_to_cpu(node
->hdr
.level
) + 1, &bp
, args
->whichfork
);
348 node
->btree
[0].hashval
= cpu_to_be32(blk1
->hashval
);
349 node
->btree
[0].before
= cpu_to_be32(blk1
->blkno
);
350 node
->btree
[1].hashval
= cpu_to_be32(blk2
->hashval
);
351 node
->btree
[1].before
= cpu_to_be32(blk2
->blkno
);
352 node
->hdr
.count
= cpu_to_be16(2);
355 if (be16_to_cpu(oldroot
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
) {
356 ASSERT(blk1
->blkno
>= mp
->m_dirleafblk
&&
357 blk1
->blkno
< mp
->m_dirfreeblk
);
358 ASSERT(blk2
->blkno
>= mp
->m_dirleafblk
&&
359 blk2
->blkno
< mp
->m_dirfreeblk
);
363 /* Header is already logged by xfs_da_node_create */
364 xfs_da_log_buf(tp
, bp
,
365 XFS_DA_LOGRANGE(node
, node
->btree
,
366 sizeof(xfs_da_node_entry_t
) * 2));
373 * Split the node, rebalance, then add the new entry.
375 STATIC
int /* error */
376 xfs_da_node_split(xfs_da_state_t
*state
, xfs_da_state_blk_t
*oldblk
,
377 xfs_da_state_blk_t
*newblk
,
378 xfs_da_state_blk_t
*addblk
,
379 int treelevel
, int *result
)
381 xfs_da_intnode_t
*node
;
386 node
= oldblk
->bp
->data
;
387 ASSERT(be16_to_cpu(node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
390 * With V2 dirs the extra block is data or freespace.
392 useextra
= state
->extravalid
&& state
->args
->whichfork
== XFS_ATTR_FORK
;
393 newcount
= 1 + useextra
;
395 * Do we have to split the node?
397 if ((be16_to_cpu(node
->hdr
.count
) + newcount
) > state
->node_ents
) {
399 * Allocate a new node, add to the doubly linked chain of
400 * nodes, then move some of our excess entries into it.
402 error
= xfs_da_grow_inode(state
->args
, &blkno
);
404 return(error
); /* GROT: dir is inconsistent */
406 error
= xfs_da_node_create(state
->args
, blkno
, treelevel
,
407 &newblk
->bp
, state
->args
->whichfork
);
409 return(error
); /* GROT: dir is inconsistent */
410 newblk
->blkno
= blkno
;
411 newblk
->magic
= XFS_DA_NODE_MAGIC
;
412 xfs_da_node_rebalance(state
, oldblk
, newblk
);
413 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
422 * Insert the new entry(s) into the correct block
423 * (updating last hashval in the process).
425 * xfs_da_node_add() inserts BEFORE the given index,
426 * and as a result of using node_lookup_int() we always
427 * point to a valid entry (not after one), but a split
428 * operation always results in a new block whose hashvals
429 * FOLLOW the current block.
431 * If we had double-split op below us, then add the extra block too.
433 node
= oldblk
->bp
->data
;
434 if (oldblk
->index
<= be16_to_cpu(node
->hdr
.count
)) {
436 xfs_da_node_add(state
, oldblk
, addblk
);
438 if (state
->extraafter
)
440 xfs_da_node_add(state
, oldblk
, &state
->extrablk
);
441 state
->extravalid
= 0;
445 xfs_da_node_add(state
, newblk
, addblk
);
447 if (state
->extraafter
)
449 xfs_da_node_add(state
, newblk
, &state
->extrablk
);
450 state
->extravalid
= 0;
458 * Balance the btree elements between two intermediate nodes,
459 * usually one full and one empty.
461 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
464 xfs_da_node_rebalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*blk1
,
465 xfs_da_state_blk_t
*blk2
)
467 xfs_da_intnode_t
*node1
, *node2
, *tmpnode
;
468 xfs_da_node_entry_t
*btree_s
, *btree_d
;
472 node1
= blk1
->bp
->data
;
473 node2
= blk2
->bp
->data
;
475 * Figure out how many entries need to move, and in which direction.
476 * Swap the nodes around if that makes it simpler.
478 if ((be16_to_cpu(node1
->hdr
.count
) > 0) && (be16_to_cpu(node2
->hdr
.count
) > 0) &&
479 ((be32_to_cpu(node2
->btree
[0].hashval
) < be32_to_cpu(node1
->btree
[0].hashval
)) ||
480 (be32_to_cpu(node2
->btree
[be16_to_cpu(node2
->hdr
.count
)-1].hashval
) <
481 be32_to_cpu(node1
->btree
[be16_to_cpu(node1
->hdr
.count
)-1].hashval
)))) {
486 ASSERT(be16_to_cpu(node1
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
487 ASSERT(be16_to_cpu(node2
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
488 count
= (be16_to_cpu(node1
->hdr
.count
) - be16_to_cpu(node2
->hdr
.count
)) / 2;
491 tp
= state
->args
->trans
;
493 * Two cases: high-to-low and low-to-high.
497 * Move elements in node2 up to make a hole.
499 if ((tmp
= be16_to_cpu(node2
->hdr
.count
)) > 0) {
500 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
501 btree_s
= &node2
->btree
[0];
502 btree_d
= &node2
->btree
[count
];
503 memmove(btree_d
, btree_s
, tmp
);
507 * Move the req'd B-tree elements from high in node1 to
510 be16_add_cpu(&node2
->hdr
.count
, count
);
511 tmp
= count
* (uint
)sizeof(xfs_da_node_entry_t
);
512 btree_s
= &node1
->btree
[be16_to_cpu(node1
->hdr
.count
) - count
];
513 btree_d
= &node2
->btree
[0];
514 memcpy(btree_d
, btree_s
, tmp
);
515 be16_add_cpu(&node1
->hdr
.count
, -count
);
518 * Move the req'd B-tree elements from low in node2 to
522 tmp
= count
* (uint
)sizeof(xfs_da_node_entry_t
);
523 btree_s
= &node2
->btree
[0];
524 btree_d
= &node1
->btree
[be16_to_cpu(node1
->hdr
.count
)];
525 memcpy(btree_d
, btree_s
, tmp
);
526 be16_add_cpu(&node1
->hdr
.count
, count
);
527 xfs_da_log_buf(tp
, blk1
->bp
,
528 XFS_DA_LOGRANGE(node1
, btree_d
, tmp
));
531 * Move elements in node2 down to fill the hole.
533 tmp
= be16_to_cpu(node2
->hdr
.count
) - count
;
534 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
535 btree_s
= &node2
->btree
[count
];
536 btree_d
= &node2
->btree
[0];
537 memmove(btree_d
, btree_s
, tmp
);
538 be16_add_cpu(&node2
->hdr
.count
, -count
);
542 * Log header of node 1 and all current bits of node 2.
544 xfs_da_log_buf(tp
, blk1
->bp
,
545 XFS_DA_LOGRANGE(node1
, &node1
->hdr
, sizeof(node1
->hdr
)));
546 xfs_da_log_buf(tp
, blk2
->bp
,
547 XFS_DA_LOGRANGE(node2
, &node2
->hdr
,
549 sizeof(node2
->btree
[0]) * be16_to_cpu(node2
->hdr
.count
)));
552 * Record the last hashval from each block for upward propagation.
553 * (note: don't use the swapped node pointers)
555 node1
= blk1
->bp
->data
;
556 node2
= blk2
->bp
->data
;
557 blk1
->hashval
= be32_to_cpu(node1
->btree
[be16_to_cpu(node1
->hdr
.count
)-1].hashval
);
558 blk2
->hashval
= be32_to_cpu(node2
->btree
[be16_to_cpu(node2
->hdr
.count
)-1].hashval
);
561 * Adjust the expected index for insertion.
563 if (blk1
->index
>= be16_to_cpu(node1
->hdr
.count
)) {
564 blk2
->index
= blk1
->index
- be16_to_cpu(node1
->hdr
.count
);
565 blk1
->index
= be16_to_cpu(node1
->hdr
.count
) + 1; /* make it invalid */
570 * Add a new entry to an intermediate node.
573 xfs_da_node_add(xfs_da_state_t
*state
, xfs_da_state_blk_t
*oldblk
,
574 xfs_da_state_blk_t
*newblk
)
576 xfs_da_intnode_t
*node
;
577 xfs_da_node_entry_t
*btree
;
580 node
= oldblk
->bp
->data
;
581 ASSERT(be16_to_cpu(node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
582 ASSERT((oldblk
->index
>= 0) && (oldblk
->index
<= be16_to_cpu(node
->hdr
.count
)));
583 ASSERT(newblk
->blkno
!= 0);
584 if (state
->args
->whichfork
== XFS_DATA_FORK
)
585 ASSERT(newblk
->blkno
>= state
->mp
->m_dirleafblk
&&
586 newblk
->blkno
< state
->mp
->m_dirfreeblk
);
589 * We may need to make some room before we insert the new node.
592 btree
= &node
->btree
[ oldblk
->index
];
593 if (oldblk
->index
< be16_to_cpu(node
->hdr
.count
)) {
594 tmp
= (be16_to_cpu(node
->hdr
.count
) - oldblk
->index
) * (uint
)sizeof(*btree
);
595 memmove(btree
+ 1, btree
, tmp
);
597 btree
->hashval
= cpu_to_be32(newblk
->hashval
);
598 btree
->before
= cpu_to_be32(newblk
->blkno
);
599 xfs_da_log_buf(state
->args
->trans
, oldblk
->bp
,
600 XFS_DA_LOGRANGE(node
, btree
, tmp
+ sizeof(*btree
)));
601 be16_add_cpu(&node
->hdr
.count
, 1);
602 xfs_da_log_buf(state
->args
->trans
, oldblk
->bp
,
603 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
606 * Copy the last hash value from the oldblk to propagate upwards.
608 oldblk
->hashval
= be32_to_cpu(node
->btree
[be16_to_cpu(node
->hdr
.count
)-1 ].hashval
);
611 /*========================================================================
612 * Routines used for shrinking the Btree.
613 *========================================================================*/
616 * Deallocate an empty leaf node, remove it from its parent,
617 * possibly deallocating that block, etc...
620 xfs_da_join(xfs_da_state_t
*state
)
622 xfs_da_state_blk_t
*drop_blk
, *save_blk
;
626 drop_blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
627 save_blk
= &state
->altpath
.blk
[ state
->path
.active
-1 ];
628 ASSERT(state
->path
.blk
[0].magic
== XFS_DA_NODE_MAGIC
);
629 ASSERT(drop_blk
->magic
== XFS_ATTR_LEAF_MAGIC
||
630 drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
633 * Walk back up the tree joining/deallocating as necessary.
634 * When we stop dropping blocks, break out.
636 for ( ; state
->path
.active
>= 2; drop_blk
--, save_blk
--,
637 state
->path
.active
--) {
639 * See if we can combine the block with a neighbor.
640 * (action == 0) => no options, just leave
641 * (action == 1) => coalesce, then unlink
642 * (action == 2) => block empty, unlink it
644 switch (drop_blk
->magic
) {
645 case XFS_ATTR_LEAF_MAGIC
:
646 error
= xfs_attr_leaf_toosmall(state
, &action
);
651 xfs_attr_leaf_unbalance(state
, drop_blk
, save_blk
);
653 case XFS_DIR2_LEAFN_MAGIC
:
654 error
= xfs_dir2_leafn_toosmall(state
, &action
);
659 xfs_dir2_leafn_unbalance(state
, drop_blk
, save_blk
);
661 case XFS_DA_NODE_MAGIC
:
663 * Remove the offending node, fixup hashvals,
664 * check for a toosmall neighbor.
666 xfs_da_node_remove(state
, drop_blk
);
667 xfs_da_fixhashpath(state
, &state
->path
);
668 error
= xfs_da_node_toosmall(state
, &action
);
673 xfs_da_node_unbalance(state
, drop_blk
, save_blk
);
676 xfs_da_fixhashpath(state
, &state
->altpath
);
677 error
= xfs_da_blk_unlink(state
, drop_blk
, save_blk
);
678 xfs_da_state_kill_altpath(state
);
681 error
= xfs_da_shrink_inode(state
->args
, drop_blk
->blkno
,
688 * We joined all the way to the top. If it turns out that
689 * we only have one entry in the root, make the child block
692 xfs_da_node_remove(state
, drop_blk
);
693 xfs_da_fixhashpath(state
, &state
->path
);
694 error
= xfs_da_root_join(state
, &state
->path
.blk
[0]);
699 * We have only one entry in the root. Copy the only remaining child of
700 * the old root to block 0 as the new root node.
703 xfs_da_root_join(xfs_da_state_t
*state
, xfs_da_state_blk_t
*root_blk
)
705 xfs_da_intnode_t
*oldroot
;
707 xfs_da_blkinfo_t
*blkinfo
;
714 ASSERT(args
!= NULL
);
715 ASSERT(root_blk
->magic
== XFS_DA_NODE_MAGIC
);
716 oldroot
= root_blk
->bp
->data
;
717 ASSERT(be16_to_cpu(oldroot
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
718 ASSERT(!oldroot
->hdr
.info
.forw
);
719 ASSERT(!oldroot
->hdr
.info
.back
);
722 * If the root has more than one child, then don't do anything.
724 if (be16_to_cpu(oldroot
->hdr
.count
) > 1)
728 * Read in the (only) child block, then copy those bytes into
729 * the root block's buffer and free the original child block.
731 child
= be32_to_cpu(oldroot
->btree
[0].before
);
733 error
= xfs_da_read_buf(args
->trans
, args
->dp
, child
, -1, &bp
,
739 if (be16_to_cpu(oldroot
->hdr
.level
) == 1) {
740 ASSERT(be16_to_cpu(blkinfo
->magic
) == XFS_DIR2_LEAFN_MAGIC
||
741 be16_to_cpu(blkinfo
->magic
) == XFS_ATTR_LEAF_MAGIC
);
743 ASSERT(be16_to_cpu(blkinfo
->magic
) == XFS_DA_NODE_MAGIC
);
745 ASSERT(!blkinfo
->forw
);
746 ASSERT(!blkinfo
->back
);
747 memcpy(root_blk
->bp
->data
, bp
->data
, state
->blocksize
);
748 xfs_da_log_buf(args
->trans
, root_blk
->bp
, 0, state
->blocksize
- 1);
749 error
= xfs_da_shrink_inode(args
, child
, bp
);
754 * Check a node block and its neighbors to see if the block should be
755 * collapsed into one or the other neighbor. Always keep the block
756 * with the smaller block number.
757 * If the current block is over 50% full, don't try to join it, return 0.
758 * If the block is empty, fill in the state structure and return 2.
759 * If it can be collapsed, fill in the state structure and return 1.
760 * If nothing can be done, return 0.
763 xfs_da_node_toosmall(xfs_da_state_t
*state
, int *action
)
765 xfs_da_intnode_t
*node
;
766 xfs_da_state_blk_t
*blk
;
767 xfs_da_blkinfo_t
*info
;
768 int count
, forward
, error
, retval
, i
;
773 * Check for the degenerate case of the block being over 50% full.
774 * If so, it's not worth even looking to see if we might be able
775 * to coalesce with a sibling.
777 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
778 info
= blk
->bp
->data
;
779 ASSERT(be16_to_cpu(info
->magic
) == XFS_DA_NODE_MAGIC
);
780 node
= (xfs_da_intnode_t
*)info
;
781 count
= be16_to_cpu(node
->hdr
.count
);
782 if (count
> (state
->node_ents
>> 1)) {
783 *action
= 0; /* blk over 50%, don't try to join */
784 return(0); /* blk over 50%, don't try to join */
788 * Check for the degenerate case of the block being empty.
789 * If the block is empty, we'll simply delete it, no need to
790 * coalesce it with a sibling block. We choose (arbitrarily)
791 * to merge with the forward block unless it is NULL.
795 * Make altpath point to the block we want to keep and
796 * path point to the block we want to drop (this one).
798 forward
= (info
->forw
!= 0);
799 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
800 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
813 * Examine each sibling block to see if we can coalesce with
814 * at least 25% free space to spare. We need to figure out
815 * whether to merge with the forward or the backward block.
816 * We prefer coalescing with the lower numbered sibling so as
817 * to shrink a directory over time.
819 /* start with smaller blk num */
820 forward
= (be32_to_cpu(info
->forw
) < be32_to_cpu(info
->back
));
821 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
823 blkno
= be32_to_cpu(info
->forw
);
825 blkno
= be32_to_cpu(info
->back
);
828 error
= xfs_da_read_buf(state
->args
->trans
, state
->args
->dp
,
829 blkno
, -1, &bp
, state
->args
->whichfork
);
834 node
= (xfs_da_intnode_t
*)info
;
835 count
= state
->node_ents
;
836 count
-= state
->node_ents
>> 2;
837 count
-= be16_to_cpu(node
->hdr
.count
);
839 ASSERT(be16_to_cpu(node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
840 count
-= be16_to_cpu(node
->hdr
.count
);
841 xfs_da_brelse(state
->args
->trans
, bp
);
843 break; /* fits with at least 25% to spare */
851 * Make altpath point to the block we want to keep (the lower
852 * numbered block) and path point to the block we want to drop.
854 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
855 if (blkno
< blk
->blkno
) {
856 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
866 error
= xfs_da_path_shift(state
, &state
->path
, forward
,
881 * Walk back up the tree adjusting hash values as necessary,
882 * when we stop making changes, return.
885 xfs_da_fixhashpath(xfs_da_state_t
*state
, xfs_da_state_path_t
*path
)
887 xfs_da_state_blk_t
*blk
;
888 xfs_da_intnode_t
*node
;
889 xfs_da_node_entry_t
*btree
;
890 xfs_dahash_t lasthash
=0;
893 level
= path
->active
-1;
894 blk
= &path
->blk
[ level
];
895 switch (blk
->magic
) {
896 case XFS_ATTR_LEAF_MAGIC
:
897 lasthash
= xfs_attr_leaf_lasthash(blk
->bp
, &count
);
901 case XFS_DIR2_LEAFN_MAGIC
:
902 lasthash
= xfs_dir2_leafn_lasthash(blk
->bp
, &count
);
906 case XFS_DA_NODE_MAGIC
:
907 lasthash
= xfs_da_node_lasthash(blk
->bp
, &count
);
912 for (blk
--, level
--; level
>= 0; blk
--, level
--) {
913 node
= blk
->bp
->data
;
914 ASSERT(be16_to_cpu(node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
915 btree
= &node
->btree
[ blk
->index
];
916 if (be32_to_cpu(btree
->hashval
) == lasthash
)
918 blk
->hashval
= lasthash
;
919 btree
->hashval
= cpu_to_be32(lasthash
);
920 xfs_da_log_buf(state
->args
->trans
, blk
->bp
,
921 XFS_DA_LOGRANGE(node
, btree
, sizeof(*btree
)));
923 lasthash
= be32_to_cpu(node
->btree
[be16_to_cpu(node
->hdr
.count
)-1].hashval
);
928 * Remove an entry from an intermediate node.
931 xfs_da_node_remove(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
)
933 xfs_da_intnode_t
*node
;
934 xfs_da_node_entry_t
*btree
;
937 node
= drop_blk
->bp
->data
;
938 ASSERT(drop_blk
->index
< be16_to_cpu(node
->hdr
.count
));
939 ASSERT(drop_blk
->index
>= 0);
942 * Copy over the offending entry, or just zero it out.
944 btree
= &node
->btree
[drop_blk
->index
];
945 if (drop_blk
->index
< (be16_to_cpu(node
->hdr
.count
)-1)) {
946 tmp
= be16_to_cpu(node
->hdr
.count
) - drop_blk
->index
- 1;
947 tmp
*= (uint
)sizeof(xfs_da_node_entry_t
);
948 memmove(btree
, btree
+ 1, tmp
);
949 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
950 XFS_DA_LOGRANGE(node
, btree
, tmp
));
951 btree
= &node
->btree
[be16_to_cpu(node
->hdr
.count
)-1];
953 memset((char *)btree
, 0, sizeof(xfs_da_node_entry_t
));
954 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
955 XFS_DA_LOGRANGE(node
, btree
, sizeof(*btree
)));
956 be16_add_cpu(&node
->hdr
.count
, -1);
957 xfs_da_log_buf(state
->args
->trans
, drop_blk
->bp
,
958 XFS_DA_LOGRANGE(node
, &node
->hdr
, sizeof(node
->hdr
)));
961 * Copy the last hash value from the block to propagate upwards.
964 drop_blk
->hashval
= be32_to_cpu(btree
->hashval
);
968 * Unbalance the btree elements between two intermediate nodes,
969 * move all Btree elements from one node into another.
972 xfs_da_node_unbalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
,
973 xfs_da_state_blk_t
*save_blk
)
975 xfs_da_intnode_t
*drop_node
, *save_node
;
976 xfs_da_node_entry_t
*btree
;
980 drop_node
= drop_blk
->bp
->data
;
981 save_node
= save_blk
->bp
->data
;
982 ASSERT(be16_to_cpu(drop_node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
983 ASSERT(be16_to_cpu(save_node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
984 tp
= state
->args
->trans
;
987 * If the dying block has lower hashvals, then move all the
988 * elements in the remaining block up to make a hole.
990 if ((be32_to_cpu(drop_node
->btree
[0].hashval
) < be32_to_cpu(save_node
->btree
[ 0 ].hashval
)) ||
991 (be32_to_cpu(drop_node
->btree
[be16_to_cpu(drop_node
->hdr
.count
)-1].hashval
) <
992 be32_to_cpu(save_node
->btree
[be16_to_cpu(save_node
->hdr
.count
)-1].hashval
)))
994 btree
= &save_node
->btree
[be16_to_cpu(drop_node
->hdr
.count
)];
995 tmp
= be16_to_cpu(save_node
->hdr
.count
) * (uint
)sizeof(xfs_da_node_entry_t
);
996 memmove(btree
, &save_node
->btree
[0], tmp
);
997 btree
= &save_node
->btree
[0];
998 xfs_da_log_buf(tp
, save_blk
->bp
,
999 XFS_DA_LOGRANGE(save_node
, btree
,
1000 (be16_to_cpu(save_node
->hdr
.count
) + be16_to_cpu(drop_node
->hdr
.count
)) *
1001 sizeof(xfs_da_node_entry_t
)));
1003 btree
= &save_node
->btree
[be16_to_cpu(save_node
->hdr
.count
)];
1004 xfs_da_log_buf(tp
, save_blk
->bp
,
1005 XFS_DA_LOGRANGE(save_node
, btree
,
1006 be16_to_cpu(drop_node
->hdr
.count
) *
1007 sizeof(xfs_da_node_entry_t
)));
1011 * Move all the B-tree elements from drop_blk to save_blk.
1013 tmp
= be16_to_cpu(drop_node
->hdr
.count
) * (uint
)sizeof(xfs_da_node_entry_t
);
1014 memcpy(btree
, &drop_node
->btree
[0], tmp
);
1015 be16_add_cpu(&save_node
->hdr
.count
, be16_to_cpu(drop_node
->hdr
.count
));
1017 xfs_da_log_buf(tp
, save_blk
->bp
,
1018 XFS_DA_LOGRANGE(save_node
, &save_node
->hdr
,
1019 sizeof(save_node
->hdr
)));
1022 * Save the last hashval in the remaining block for upward propagation.
1024 save_blk
->hashval
= be32_to_cpu(save_node
->btree
[be16_to_cpu(save_node
->hdr
.count
)-1].hashval
);
1027 /*========================================================================
1028 * Routines used for finding things in the Btree.
1029 *========================================================================*/
1032 * Walk down the Btree looking for a particular filename, filling
1033 * in the state structure as we go.
1035 * We will set the state structure to point to each of the elements
1036 * in each of the nodes where either the hashval is or should be.
1038 * We support duplicate hashval's so for each entry in the current
1039 * node that could contain the desired hashval, descend. This is a
1040 * pruned depth-first tree search.
1043 xfs_da_node_lookup_int(xfs_da_state_t
*state
, int *result
)
1045 xfs_da_state_blk_t
*blk
;
1046 xfs_da_blkinfo_t
*curr
;
1047 xfs_da_intnode_t
*node
;
1048 xfs_da_node_entry_t
*btree
;
1050 int probe
, span
, max
, error
, retval
;
1051 xfs_dahash_t hashval
, btreehashval
;
1052 xfs_da_args_t
*args
;
1057 * Descend thru the B-tree searching each level for the right
1058 * node to use, until the right hashval is found.
1060 blkno
= (args
->whichfork
== XFS_DATA_FORK
)? state
->mp
->m_dirleafblk
: 0;
1061 for (blk
= &state
->path
.blk
[0], state
->path
.active
= 1;
1062 state
->path
.active
<= XFS_DA_NODE_MAXDEPTH
;
1063 blk
++, state
->path
.active
++) {
1065 * Read the next node down in the tree.
1068 error
= xfs_da_read_buf(args
->trans
, args
->dp
, blkno
,
1069 -1, &blk
->bp
, args
->whichfork
);
1072 state
->path
.active
--;
1075 curr
= blk
->bp
->data
;
1076 blk
->magic
= be16_to_cpu(curr
->magic
);
1077 ASSERT(blk
->magic
== XFS_DA_NODE_MAGIC
||
1078 blk
->magic
== XFS_DIR2_LEAFN_MAGIC
||
1079 blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1082 * Search an intermediate node for a match.
1084 if (blk
->magic
== XFS_DA_NODE_MAGIC
) {
1085 node
= blk
->bp
->data
;
1086 max
= be16_to_cpu(node
->hdr
.count
);
1087 blk
->hashval
= be32_to_cpu(node
->btree
[max
-1].hashval
);
1090 * Binary search. (note: small blocks will skip loop)
1092 probe
= span
= max
/ 2;
1093 hashval
= args
->hashval
;
1094 for (btree
= &node
->btree
[probe
]; span
> 4;
1095 btree
= &node
->btree
[probe
]) {
1097 btreehashval
= be32_to_cpu(btree
->hashval
);
1098 if (btreehashval
< hashval
)
1100 else if (btreehashval
> hashval
)
1105 ASSERT((probe
>= 0) && (probe
< max
));
1106 ASSERT((span
<= 4) || (be32_to_cpu(btree
->hashval
) == hashval
));
1109 * Since we may have duplicate hashval's, find the first
1110 * matching hashval in the node.
1112 while ((probe
> 0) && (be32_to_cpu(btree
->hashval
) >= hashval
)) {
1116 while ((probe
< max
) && (be32_to_cpu(btree
->hashval
) < hashval
)) {
1122 * Pick the right block to descend on.
1126 blkno
= be32_to_cpu(node
->btree
[max
-1].before
);
1129 blkno
= be32_to_cpu(btree
->before
);
1131 } else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1132 blk
->hashval
= xfs_attr_leaf_lasthash(blk
->bp
, NULL
);
1134 } else if (blk
->magic
== XFS_DIR2_LEAFN_MAGIC
) {
1135 blk
->hashval
= xfs_dir2_leafn_lasthash(blk
->bp
, NULL
);
1141 * A leaf block that ends in the hashval that we are interested in
1142 * (final hashval == search hashval) means that the next block may
1143 * contain more entries with the same hashval, shift upward to the
1144 * next leaf and keep searching.
1147 if (blk
->magic
== XFS_DIR2_LEAFN_MAGIC
) {
1148 retval
= xfs_dir2_leafn_lookup_int(blk
->bp
, args
,
1149 &blk
->index
, state
);
1150 } else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1151 retval
= xfs_attr_leaf_lookup_int(blk
->bp
, args
);
1152 blk
->index
= args
->index
;
1153 args
->blkno
= blk
->blkno
;
1156 return XFS_ERROR(EFSCORRUPTED
);
1158 if (((retval
== ENOENT
) || (retval
== ENOATTR
)) &&
1159 (blk
->hashval
== args
->hashval
)) {
1160 error
= xfs_da_path_shift(state
, &state
->path
, 1, 1,
1166 } else if (blk
->magic
== XFS_ATTR_LEAF_MAGIC
) {
1167 /* path_shift() gives ENOENT */
1168 retval
= XFS_ERROR(ENOATTR
);
1177 /*========================================================================
1179 *========================================================================*/
1182 * Link a new block into a doubly linked list of blocks (of whatever type).
1185 xfs_da_blk_link(xfs_da_state_t
*state
, xfs_da_state_blk_t
*old_blk
,
1186 xfs_da_state_blk_t
*new_blk
)
1188 xfs_da_blkinfo_t
*old_info
, *new_info
, *tmp_info
;
1189 xfs_da_args_t
*args
;
1190 int before
=0, error
;
1194 * Set up environment.
1197 ASSERT(args
!= NULL
);
1198 old_info
= old_blk
->bp
->data
;
1199 new_info
= new_blk
->bp
->data
;
1200 ASSERT(old_blk
->magic
== XFS_DA_NODE_MAGIC
||
1201 old_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
||
1202 old_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1203 ASSERT(old_blk
->magic
== be16_to_cpu(old_info
->magic
));
1204 ASSERT(new_blk
->magic
== be16_to_cpu(new_info
->magic
));
1205 ASSERT(old_blk
->magic
== new_blk
->magic
);
1207 switch (old_blk
->magic
) {
1208 case XFS_ATTR_LEAF_MAGIC
:
1209 before
= xfs_attr_leaf_order(old_blk
->bp
, new_blk
->bp
);
1211 case XFS_DIR2_LEAFN_MAGIC
:
1212 before
= xfs_dir2_leafn_order(old_blk
->bp
, new_blk
->bp
);
1214 case XFS_DA_NODE_MAGIC
:
1215 before
= xfs_da_node_order(old_blk
->bp
, new_blk
->bp
);
1220 * Link blocks in appropriate order.
1224 * Link new block in before existing block.
1226 new_info
->forw
= cpu_to_be32(old_blk
->blkno
);
1227 new_info
->back
= old_info
->back
;
1228 if (old_info
->back
) {
1229 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1230 be32_to_cpu(old_info
->back
),
1231 -1, &bp
, args
->whichfork
);
1235 tmp_info
= bp
->data
;
1236 ASSERT(be16_to_cpu(tmp_info
->magic
) == be16_to_cpu(old_info
->magic
));
1237 ASSERT(be32_to_cpu(tmp_info
->forw
) == old_blk
->blkno
);
1238 tmp_info
->forw
= cpu_to_be32(new_blk
->blkno
);
1239 xfs_da_log_buf(args
->trans
, bp
, 0, sizeof(*tmp_info
)-1);
1240 xfs_da_buf_done(bp
);
1242 old_info
->back
= cpu_to_be32(new_blk
->blkno
);
1245 * Link new block in after existing block.
1247 new_info
->forw
= old_info
->forw
;
1248 new_info
->back
= cpu_to_be32(old_blk
->blkno
);
1249 if (old_info
->forw
) {
1250 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1251 be32_to_cpu(old_info
->forw
),
1252 -1, &bp
, args
->whichfork
);
1256 tmp_info
= bp
->data
;
1257 ASSERT(tmp_info
->magic
== old_info
->magic
);
1258 ASSERT(be32_to_cpu(tmp_info
->back
) == old_blk
->blkno
);
1259 tmp_info
->back
= cpu_to_be32(new_blk
->blkno
);
1260 xfs_da_log_buf(args
->trans
, bp
, 0, sizeof(*tmp_info
)-1);
1261 xfs_da_buf_done(bp
);
1263 old_info
->forw
= cpu_to_be32(new_blk
->blkno
);
1266 xfs_da_log_buf(args
->trans
, old_blk
->bp
, 0, sizeof(*tmp_info
) - 1);
1267 xfs_da_log_buf(args
->trans
, new_blk
->bp
, 0, sizeof(*tmp_info
) - 1);
1272 * Compare two intermediate nodes for "order".
1275 xfs_da_node_order(xfs_dabuf_t
*node1_bp
, xfs_dabuf_t
*node2_bp
)
1277 xfs_da_intnode_t
*node1
, *node2
;
1279 node1
= node1_bp
->data
;
1280 node2
= node2_bp
->data
;
1281 ASSERT((be16_to_cpu(node1
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
) &&
1282 (be16_to_cpu(node2
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
));
1283 if ((be16_to_cpu(node1
->hdr
.count
) > 0) && (be16_to_cpu(node2
->hdr
.count
) > 0) &&
1284 ((be32_to_cpu(node2
->btree
[0].hashval
) <
1285 be32_to_cpu(node1
->btree
[0].hashval
)) ||
1286 (be32_to_cpu(node2
->btree
[be16_to_cpu(node2
->hdr
.count
)-1].hashval
) <
1287 be32_to_cpu(node1
->btree
[be16_to_cpu(node1
->hdr
.count
)-1].hashval
)))) {
1294 * Pick up the last hashvalue from an intermediate node.
1297 xfs_da_node_lasthash(xfs_dabuf_t
*bp
, int *count
)
1299 xfs_da_intnode_t
*node
;
1302 ASSERT(be16_to_cpu(node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
1304 *count
= be16_to_cpu(node
->hdr
.count
);
1305 if (!node
->hdr
.count
)
1307 return be32_to_cpu(node
->btree
[be16_to_cpu(node
->hdr
.count
)-1].hashval
);
1311 * Unlink a block from a doubly linked list of blocks.
1313 STATIC
int /* error */
1314 xfs_da_blk_unlink(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
,
1315 xfs_da_state_blk_t
*save_blk
)
1317 xfs_da_blkinfo_t
*drop_info
, *save_info
, *tmp_info
;
1318 xfs_da_args_t
*args
;
1323 * Set up environment.
1326 ASSERT(args
!= NULL
);
1327 save_info
= save_blk
->bp
->data
;
1328 drop_info
= drop_blk
->bp
->data
;
1329 ASSERT(save_blk
->magic
== XFS_DA_NODE_MAGIC
||
1330 save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
||
1331 save_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1332 ASSERT(save_blk
->magic
== be16_to_cpu(save_info
->magic
));
1333 ASSERT(drop_blk
->magic
== be16_to_cpu(drop_info
->magic
));
1334 ASSERT(save_blk
->magic
== drop_blk
->magic
);
1335 ASSERT((be32_to_cpu(save_info
->forw
) == drop_blk
->blkno
) ||
1336 (be32_to_cpu(save_info
->back
) == drop_blk
->blkno
));
1337 ASSERT((be32_to_cpu(drop_info
->forw
) == save_blk
->blkno
) ||
1338 (be32_to_cpu(drop_info
->back
) == save_blk
->blkno
));
1341 * Unlink the leaf block from the doubly linked chain of leaves.
1343 if (be32_to_cpu(save_info
->back
) == drop_blk
->blkno
) {
1344 save_info
->back
= drop_info
->back
;
1345 if (drop_info
->back
) {
1346 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1347 be32_to_cpu(drop_info
->back
),
1348 -1, &bp
, args
->whichfork
);
1352 tmp_info
= bp
->data
;
1353 ASSERT(tmp_info
->magic
== save_info
->magic
);
1354 ASSERT(be32_to_cpu(tmp_info
->forw
) == drop_blk
->blkno
);
1355 tmp_info
->forw
= cpu_to_be32(save_blk
->blkno
);
1356 xfs_da_log_buf(args
->trans
, bp
, 0,
1357 sizeof(*tmp_info
) - 1);
1358 xfs_da_buf_done(bp
);
1361 save_info
->forw
= drop_info
->forw
;
1362 if (drop_info
->forw
) {
1363 error
= xfs_da_read_buf(args
->trans
, args
->dp
,
1364 be32_to_cpu(drop_info
->forw
),
1365 -1, &bp
, args
->whichfork
);
1369 tmp_info
= bp
->data
;
1370 ASSERT(tmp_info
->magic
== save_info
->magic
);
1371 ASSERT(be32_to_cpu(tmp_info
->back
) == drop_blk
->blkno
);
1372 tmp_info
->back
= cpu_to_be32(save_blk
->blkno
);
1373 xfs_da_log_buf(args
->trans
, bp
, 0,
1374 sizeof(*tmp_info
) - 1);
1375 xfs_da_buf_done(bp
);
1379 xfs_da_log_buf(args
->trans
, save_blk
->bp
, 0, sizeof(*save_info
) - 1);
1384 * Move a path "forward" or "!forward" one block at the current level.
1386 * This routine will adjust a "path" to point to the next block
1387 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1388 * Btree, including updating pointers to the intermediate nodes between
1389 * the new bottom and the root.
1392 xfs_da_path_shift(xfs_da_state_t
*state
, xfs_da_state_path_t
*path
,
1393 int forward
, int release
, int *result
)
1395 xfs_da_state_blk_t
*blk
;
1396 xfs_da_blkinfo_t
*info
;
1397 xfs_da_intnode_t
*node
;
1398 xfs_da_args_t
*args
;
1399 xfs_dablk_t blkno
=0;
1403 * Roll up the Btree looking for the first block where our
1404 * current index is not at the edge of the block. Note that
1405 * we skip the bottom layer because we want the sibling block.
1408 ASSERT(args
!= NULL
);
1409 ASSERT(path
!= NULL
);
1410 ASSERT((path
->active
> 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1411 level
= (path
->active
-1) - 1; /* skip bottom layer in path */
1412 for (blk
= &path
->blk
[level
]; level
>= 0; blk
--, level
--) {
1413 ASSERT(blk
->bp
!= NULL
);
1414 node
= blk
->bp
->data
;
1415 ASSERT(be16_to_cpu(node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
1416 if (forward
&& (blk
->index
< be16_to_cpu(node
->hdr
.count
)-1)) {
1418 blkno
= be32_to_cpu(node
->btree
[blk
->index
].before
);
1420 } else if (!forward
&& (blk
->index
> 0)) {
1422 blkno
= be32_to_cpu(node
->btree
[blk
->index
].before
);
1427 *result
= XFS_ERROR(ENOENT
); /* we're out of our tree */
1428 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
1433 * Roll down the edge of the subtree until we reach the
1434 * same depth we were at originally.
1436 for (blk
++, level
++; level
< path
->active
; blk
++, level
++) {
1438 * Release the old block.
1439 * (if it's dirty, trans won't actually let go)
1442 xfs_da_brelse(args
->trans
, blk
->bp
);
1445 * Read the next child block.
1448 error
= xfs_da_read_buf(args
->trans
, args
->dp
, blkno
, -1,
1449 &blk
->bp
, args
->whichfork
);
1452 ASSERT(blk
->bp
!= NULL
);
1453 info
= blk
->bp
->data
;
1454 ASSERT(be16_to_cpu(info
->magic
) == XFS_DA_NODE_MAGIC
||
1455 be16_to_cpu(info
->magic
) == XFS_DIR2_LEAFN_MAGIC
||
1456 be16_to_cpu(info
->magic
) == XFS_ATTR_LEAF_MAGIC
);
1457 blk
->magic
= be16_to_cpu(info
->magic
);
1458 if (blk
->magic
== XFS_DA_NODE_MAGIC
) {
1459 node
= (xfs_da_intnode_t
*)info
;
1460 blk
->hashval
= be32_to_cpu(node
->btree
[be16_to_cpu(node
->hdr
.count
)-1].hashval
);
1464 blk
->index
= be16_to_cpu(node
->hdr
.count
)-1;
1465 blkno
= be32_to_cpu(node
->btree
[blk
->index
].before
);
1467 ASSERT(level
== path
->active
-1);
1469 switch(blk
->magic
) {
1470 case XFS_ATTR_LEAF_MAGIC
:
1471 blk
->hashval
= xfs_attr_leaf_lasthash(blk
->bp
,
1474 case XFS_DIR2_LEAFN_MAGIC
:
1475 blk
->hashval
= xfs_dir2_leafn_lasthash(blk
->bp
,
1479 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
||
1480 blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1490 /*========================================================================
1492 *========================================================================*/
1495 * Implement a simple hash on a character string.
1496 * Rotate the hash value by 7 bits, then XOR each character in.
1497 * This is implemented with some source-level loop unrolling.
1500 xfs_da_hashname(const __uint8_t
*name
, int namelen
)
1505 * Do four characters at a time as long as we can.
1507 for (hash
= 0; namelen
>= 4; namelen
-= 4, name
+= 4)
1508 hash
= (name
[0] << 21) ^ (name
[1] << 14) ^ (name
[2] << 7) ^
1509 (name
[3] << 0) ^ rol32(hash
, 7 * 4);
1512 * Now do the rest of the characters.
1516 return (name
[0] << 14) ^ (name
[1] << 7) ^ (name
[2] << 0) ^
1519 return (name
[0] << 7) ^ (name
[1] << 0) ^ rol32(hash
, 7 * 2);
1521 return (name
[0] << 0) ^ rol32(hash
, 7 * 1);
1522 default: /* case 0: */
1529 struct xfs_da_args
*args
,
1530 const unsigned char *name
,
1533 return (args
->namelen
== len
&& memcmp(args
->name
, name
, len
) == 0) ?
1534 XFS_CMP_EXACT
: XFS_CMP_DIFFERENT
;
1538 xfs_default_hashname(
1539 struct xfs_name
*name
)
1541 return xfs_da_hashname(name
->name
, name
->len
);
1544 const struct xfs_nameops xfs_default_nameops
= {
1545 .hashname
= xfs_default_hashname
,
1546 .compname
= xfs_da_compname
1550 * Add a block to the btree ahead of the file.
1551 * Return the new block number to the caller.
1554 xfs_da_grow_inode(xfs_da_args_t
*args
, xfs_dablk_t
*new_blkno
)
1556 xfs_fileoff_t bno
, b
;
1557 xfs_bmbt_irec_t map
;
1558 xfs_bmbt_irec_t
*mapp
;
1560 int nmap
, error
, w
, count
, c
, got
, i
, mapi
;
1563 xfs_drfsbno_t nblks
;
1567 w
= args
->whichfork
;
1569 nblks
= dp
->i_d
.di_nblocks
;
1572 * For new directories adjust the file offset and block count.
1574 if (w
== XFS_DATA_FORK
) {
1575 bno
= mp
->m_dirleafblk
;
1576 count
= mp
->m_dirblkfsbs
;
1582 * Find a spot in the file space to put the new block.
1584 if ((error
= xfs_bmap_first_unused(tp
, dp
, count
, &bno
, w
)))
1586 if (w
== XFS_DATA_FORK
)
1587 ASSERT(bno
>= mp
->m_dirleafblk
&& bno
< mp
->m_dirfreeblk
);
1589 * Try mapping it in one filesystem block.
1592 ASSERT(args
->firstblock
!= NULL
);
1593 if ((error
= xfs_bmapi(tp
, dp
, bno
, count
,
1594 xfs_bmapi_aflag(w
)|XFS_BMAPI_WRITE
|XFS_BMAPI_METADATA
|
1596 args
->firstblock
, args
->total
, &map
, &nmap
,
1606 * If we didn't get it and the block might work if fragmented,
1607 * try without the CONTIG flag. Loop until we get it all.
1609 else if (nmap
== 0 && count
> 1) {
1610 mapp
= kmem_alloc(sizeof(*mapp
) * count
, KM_SLEEP
);
1611 for (b
= bno
, mapi
= 0; b
< bno
+ count
; ) {
1612 nmap
= MIN(XFS_BMAP_MAX_NMAP
, count
);
1613 c
= (int)(bno
+ count
- b
);
1614 if ((error
= xfs_bmapi(tp
, dp
, b
, c
,
1615 xfs_bmapi_aflag(w
)|XFS_BMAPI_WRITE
|
1617 args
->firstblock
, args
->total
,
1618 &mapp
[mapi
], &nmap
, args
->flist
))) {
1625 b
= mapp
[mapi
- 1].br_startoff
+
1626 mapp
[mapi
- 1].br_blockcount
;
1633 * Count the blocks we got, make sure it matches the total.
1635 for (i
= 0, got
= 0; i
< mapi
; i
++)
1636 got
+= mapp
[i
].br_blockcount
;
1637 if (got
!= count
|| mapp
[0].br_startoff
!= bno
||
1638 mapp
[mapi
- 1].br_startoff
+ mapp
[mapi
- 1].br_blockcount
!=
1642 return XFS_ERROR(ENOSPC
);
1646 /* account for newly allocated blocks in reserved blocks total */
1647 args
->total
-= dp
->i_d
.di_nblocks
- nblks
;
1648 *new_blkno
= (xfs_dablk_t
)bno
;
1653 * Ick. We need to always be able to remove a btree block, even
1654 * if there's no space reservation because the filesystem is full.
1655 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
1656 * It swaps the target block with the last block in the file. The
1657 * last block in the file can always be removed since it can't cause
1658 * a bmap btree split to do that.
1661 xfs_da_swap_lastblock(xfs_da_args_t
*args
, xfs_dablk_t
*dead_blknop
,
1662 xfs_dabuf_t
**dead_bufp
)
1664 xfs_dablk_t dead_blkno
, last_blkno
, sib_blkno
, par_blkno
;
1665 xfs_dabuf_t
*dead_buf
, *last_buf
, *sib_buf
, *par_buf
;
1666 xfs_fileoff_t lastoff
;
1670 int error
, w
, entno
, level
, dead_level
;
1671 xfs_da_blkinfo_t
*dead_info
, *sib_info
;
1672 xfs_da_intnode_t
*par_node
, *dead_node
;
1673 xfs_dir2_leaf_t
*dead_leaf2
;
1674 xfs_dahash_t dead_hash
;
1676 dead_buf
= *dead_bufp
;
1677 dead_blkno
= *dead_blknop
;
1680 w
= args
->whichfork
;
1681 ASSERT(w
== XFS_DATA_FORK
);
1683 lastoff
= mp
->m_dirfreeblk
;
1684 error
= xfs_bmap_last_before(tp
, ip
, &lastoff
, w
);
1687 if (unlikely(lastoff
== 0)) {
1688 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW
,
1690 return XFS_ERROR(EFSCORRUPTED
);
1693 * Read the last block in the btree space.
1695 last_blkno
= (xfs_dablk_t
)lastoff
- mp
->m_dirblkfsbs
;
1696 if ((error
= xfs_da_read_buf(tp
, ip
, last_blkno
, -1, &last_buf
, w
)))
1699 * Copy the last block into the dead buffer and log it.
1701 memcpy(dead_buf
->data
, last_buf
->data
, mp
->m_dirblksize
);
1702 xfs_da_log_buf(tp
, dead_buf
, 0, mp
->m_dirblksize
- 1);
1703 dead_info
= dead_buf
->data
;
1705 * Get values from the moved block.
1707 if (be16_to_cpu(dead_info
->magic
) == XFS_DIR2_LEAFN_MAGIC
) {
1708 dead_leaf2
= (xfs_dir2_leaf_t
*)dead_info
;
1710 dead_hash
= be32_to_cpu(dead_leaf2
->ents
[be16_to_cpu(dead_leaf2
->hdr
.count
) - 1].hashval
);
1712 ASSERT(be16_to_cpu(dead_info
->magic
) == XFS_DA_NODE_MAGIC
);
1713 dead_node
= (xfs_da_intnode_t
*)dead_info
;
1714 dead_level
= be16_to_cpu(dead_node
->hdr
.level
);
1715 dead_hash
= be32_to_cpu(dead_node
->btree
[be16_to_cpu(dead_node
->hdr
.count
) - 1].hashval
);
1717 sib_buf
= par_buf
= NULL
;
1719 * If the moved block has a left sibling, fix up the pointers.
1721 if ((sib_blkno
= be32_to_cpu(dead_info
->back
))) {
1722 if ((error
= xfs_da_read_buf(tp
, ip
, sib_blkno
, -1, &sib_buf
, w
)))
1724 sib_info
= sib_buf
->data
;
1726 be32_to_cpu(sib_info
->forw
) != last_blkno
||
1727 sib_info
->magic
!= dead_info
->magic
)) {
1728 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
1729 XFS_ERRLEVEL_LOW
, mp
);
1730 error
= XFS_ERROR(EFSCORRUPTED
);
1733 sib_info
->forw
= cpu_to_be32(dead_blkno
);
1734 xfs_da_log_buf(tp
, sib_buf
,
1735 XFS_DA_LOGRANGE(sib_info
, &sib_info
->forw
,
1736 sizeof(sib_info
->forw
)));
1737 xfs_da_buf_done(sib_buf
);
1741 * If the moved block has a right sibling, fix up the pointers.
1743 if ((sib_blkno
= be32_to_cpu(dead_info
->forw
))) {
1744 if ((error
= xfs_da_read_buf(tp
, ip
, sib_blkno
, -1, &sib_buf
, w
)))
1746 sib_info
= sib_buf
->data
;
1748 be32_to_cpu(sib_info
->back
) != last_blkno
||
1749 sib_info
->magic
!= dead_info
->magic
)) {
1750 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
1751 XFS_ERRLEVEL_LOW
, mp
);
1752 error
= XFS_ERROR(EFSCORRUPTED
);
1755 sib_info
->back
= cpu_to_be32(dead_blkno
);
1756 xfs_da_log_buf(tp
, sib_buf
,
1757 XFS_DA_LOGRANGE(sib_info
, &sib_info
->back
,
1758 sizeof(sib_info
->back
)));
1759 xfs_da_buf_done(sib_buf
);
1762 par_blkno
= mp
->m_dirleafblk
;
1765 * Walk down the tree looking for the parent of the moved block.
1768 if ((error
= xfs_da_read_buf(tp
, ip
, par_blkno
, -1, &par_buf
, w
)))
1770 par_node
= par_buf
->data
;
1772 be16_to_cpu(par_node
->hdr
.info
.magic
) != XFS_DA_NODE_MAGIC
||
1773 (level
>= 0 && level
!= be16_to_cpu(par_node
->hdr
.level
) + 1))) {
1774 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
1775 XFS_ERRLEVEL_LOW
, mp
);
1776 error
= XFS_ERROR(EFSCORRUPTED
);
1779 level
= be16_to_cpu(par_node
->hdr
.level
);
1781 entno
< be16_to_cpu(par_node
->hdr
.count
) &&
1782 be32_to_cpu(par_node
->btree
[entno
].hashval
) < dead_hash
;
1785 if (unlikely(entno
== be16_to_cpu(par_node
->hdr
.count
))) {
1786 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
1787 XFS_ERRLEVEL_LOW
, mp
);
1788 error
= XFS_ERROR(EFSCORRUPTED
);
1791 par_blkno
= be32_to_cpu(par_node
->btree
[entno
].before
);
1792 if (level
== dead_level
+ 1)
1794 xfs_da_brelse(tp
, par_buf
);
1798 * We're in the right parent block.
1799 * Look for the right entry.
1803 entno
< be16_to_cpu(par_node
->hdr
.count
) &&
1804 be32_to_cpu(par_node
->btree
[entno
].before
) != last_blkno
;
1807 if (entno
< be16_to_cpu(par_node
->hdr
.count
))
1809 par_blkno
= be32_to_cpu(par_node
->hdr
.info
.forw
);
1810 xfs_da_brelse(tp
, par_buf
);
1812 if (unlikely(par_blkno
== 0)) {
1813 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
1814 XFS_ERRLEVEL_LOW
, mp
);
1815 error
= XFS_ERROR(EFSCORRUPTED
);
1818 if ((error
= xfs_da_read_buf(tp
, ip
, par_blkno
, -1, &par_buf
, w
)))
1820 par_node
= par_buf
->data
;
1822 be16_to_cpu(par_node
->hdr
.level
) != level
||
1823 be16_to_cpu(par_node
->hdr
.info
.magic
) != XFS_DA_NODE_MAGIC
)) {
1824 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
1825 XFS_ERRLEVEL_LOW
, mp
);
1826 error
= XFS_ERROR(EFSCORRUPTED
);
1832 * Update the parent entry pointing to the moved block.
1834 par_node
->btree
[entno
].before
= cpu_to_be32(dead_blkno
);
1835 xfs_da_log_buf(tp
, par_buf
,
1836 XFS_DA_LOGRANGE(par_node
, &par_node
->btree
[entno
].before
,
1837 sizeof(par_node
->btree
[entno
].before
)));
1838 xfs_da_buf_done(par_buf
);
1839 xfs_da_buf_done(dead_buf
);
1840 *dead_blknop
= last_blkno
;
1841 *dead_bufp
= last_buf
;
1845 xfs_da_brelse(tp
, par_buf
);
1847 xfs_da_brelse(tp
, sib_buf
);
1848 xfs_da_brelse(tp
, last_buf
);
1853 * Remove a btree block from a directory or attribute.
1856 xfs_da_shrink_inode(xfs_da_args_t
*args
, xfs_dablk_t dead_blkno
,
1857 xfs_dabuf_t
*dead_buf
)
1860 int done
, error
, w
, count
;
1865 w
= args
->whichfork
;
1868 if (w
== XFS_DATA_FORK
)
1869 count
= mp
->m_dirblkfsbs
;
1874 * Remove extents. If we get ENOSPC for a dir we have to move
1875 * the last block to the place we want to kill.
1877 if ((error
= xfs_bunmapi(tp
, dp
, dead_blkno
, count
,
1878 xfs_bmapi_aflag(w
)|XFS_BMAPI_METADATA
,
1879 0, args
->firstblock
, args
->flist
,
1880 &done
)) == ENOSPC
) {
1881 if (w
!= XFS_DATA_FORK
)
1883 if ((error
= xfs_da_swap_lastblock(args
, &dead_blkno
,
1890 xfs_da_binval(tp
, dead_buf
);
1895 * See if the mapping(s) for this btree block are valid, i.e.
1896 * don't contain holes, are logically contiguous, and cover the whole range.
1899 xfs_da_map_covers_blocks(
1901 xfs_bmbt_irec_t
*mapp
,
1908 for (i
= 0, off
= bno
; i
< nmap
; i
++) {
1909 if (mapp
[i
].br_startblock
== HOLESTARTBLOCK
||
1910 mapp
[i
].br_startblock
== DELAYSTARTBLOCK
) {
1913 if (off
!= mapp
[i
].br_startoff
) {
1916 off
+= mapp
[i
].br_blockcount
;
1918 return off
== bno
+ count
;
1923 * Used for get_buf, read_buf, read_bufr, and reada_buf.
1930 xfs_daddr_t
*mappedbnop
,
1936 xfs_buf_t
*bp
= NULL
;
1940 xfs_bmbt_irec_t map
;
1941 xfs_bmbt_irec_t
*mapp
;
1942 xfs_daddr_t mappedbno
;
1950 nfsb
= (whichfork
== XFS_DATA_FORK
) ? mp
->m_dirblkfsbs
: 1;
1951 mappedbno
= *mappedbnop
;
1953 * Caller doesn't have a mapping. -2 means don't complain
1954 * if we land in a hole.
1956 if (mappedbno
== -1 || mappedbno
== -2) {
1958 * Optimize the one-block case.
1964 xfs_bmapi_single(trans
, dp
, whichfork
, &fsb
,
1965 (xfs_fileoff_t
)bno
))) {
1969 if (fsb
== NULLFSBLOCK
) {
1972 map
.br_startblock
= fsb
;
1973 map
.br_startoff
= (xfs_fileoff_t
)bno
;
1974 map
.br_blockcount
= 1;
1978 mapp
= kmem_alloc(sizeof(*mapp
) * nfsb
, KM_SLEEP
);
1980 if ((error
= xfs_bmapi(trans
, dp
, (xfs_fileoff_t
)bno
,
1982 XFS_BMAPI_METADATA
|
1983 xfs_bmapi_aflag(whichfork
),
1984 NULL
, 0, mapp
, &nmap
, NULL
)))
1988 map
.br_startblock
= XFS_DADDR_TO_FSB(mp
, mappedbno
);
1989 map
.br_startoff
= (xfs_fileoff_t
)bno
;
1990 map
.br_blockcount
= nfsb
;
1994 if (!xfs_da_map_covers_blocks(nmap
, mapp
, bno
, nfsb
)) {
1995 error
= mappedbno
== -2 ? 0 : XFS_ERROR(EFSCORRUPTED
);
1996 if (unlikely(error
== EFSCORRUPTED
)) {
1997 if (xfs_error_level
>= XFS_ERRLEVEL_LOW
) {
1998 xfs_alert(mp
, "%s: bno %lld dir: inode %lld",
1999 __func__
, (long long)bno
,
2000 (long long)dp
->i_ino
);
2001 for (i
= 0; i
< nmap
; i
++) {
2003 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
2005 (long long)mapp
[i
].br_startoff
,
2006 (long long)mapp
[i
].br_startblock
,
2007 (long long)mapp
[i
].br_blockcount
,
2011 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2012 XFS_ERRLEVEL_LOW
, mp
);
2016 if (caller
!= 3 && nmap
> 1) {
2017 bplist
= kmem_alloc(sizeof(*bplist
) * nmap
, KM_SLEEP
);
2022 * Turn the mapping(s) into buffer(s).
2024 for (i
= 0; i
< nmap
; i
++) {
2027 mappedbno
= XFS_FSB_TO_DADDR(mp
, mapp
[i
].br_startblock
);
2029 *mappedbnop
= mappedbno
;
2030 nmapped
= (int)XFS_FSB_TO_BB(mp
, mapp
[i
].br_blockcount
);
2033 bp
= xfs_trans_get_buf(trans
, mp
->m_ddev_targp
,
2034 mappedbno
, nmapped
, 0);
2035 error
= bp
? XFS_BUF_GETERROR(bp
) : XFS_ERROR(EIO
);
2040 error
= xfs_trans_read_buf(mp
, trans
, mp
->m_ddev_targp
,
2041 mappedbno
, nmapped
, 0, &bp
);
2044 xfs_buf_readahead(mp
->m_ddev_targp
, mappedbno
, nmapped
);
2051 xfs_trans_brelse(trans
, bp
);
2057 if (whichfork
== XFS_ATTR_FORK
) {
2058 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_ATTR_BTREE
,
2059 XFS_ATTR_BTREE_REF
);
2061 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_DIR_BTREE
,
2066 bplist
[nbplist
++] = bp
;
2070 * Build a dabuf structure.
2073 rbp
= xfs_da_buf_make(nbplist
, bplist
, ra
);
2075 rbp
= xfs_da_buf_make(1, &bp
, ra
);
2079 * For read_buf, check the magic number.
2082 xfs_dir2_data_t
*data
;
2083 xfs_dir2_free_t
*free
;
2084 xfs_da_blkinfo_t
*info
;
2090 magic
= be16_to_cpu(info
->magic
);
2091 magic1
= be32_to_cpu(data
->hdr
.magic
);
2093 XFS_TEST_ERROR((magic
!= XFS_DA_NODE_MAGIC
) &&
2094 (magic
!= XFS_ATTR_LEAF_MAGIC
) &&
2095 (magic
!= XFS_DIR2_LEAF1_MAGIC
) &&
2096 (magic
!= XFS_DIR2_LEAFN_MAGIC
) &&
2097 (magic1
!= XFS_DIR2_BLOCK_MAGIC
) &&
2098 (magic1
!= XFS_DIR2_DATA_MAGIC
) &&
2099 (be32_to_cpu(free
->hdr
.magic
) != XFS_DIR2_FREE_MAGIC
),
2100 mp
, XFS_ERRTAG_DA_READ_BUF
,
2101 XFS_RANDOM_DA_READ_BUF
))) {
2102 trace_xfs_da_btree_corrupt(rbp
->bps
[0], _RET_IP_
);
2103 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2104 XFS_ERRLEVEL_LOW
, mp
, info
);
2105 error
= XFS_ERROR(EFSCORRUPTED
);
2106 xfs_da_brelse(trans
, rbp
);
2122 for (i
= 0; i
< nbplist
; i
++)
2123 xfs_trans_brelse(trans
, bplist
[i
]);
2135 * Get a buffer for the dir/attr block.
2142 xfs_daddr_t mappedbno
,
2146 return xfs_da_do_buf(trans
, dp
, bno
, &mappedbno
, bpp
, whichfork
, 0,
2147 (inst_t
*)__return_address
);
2151 * Get a buffer for the dir/attr block, fill in the contents.
2158 xfs_daddr_t mappedbno
,
2162 return xfs_da_do_buf(trans
, dp
, bno
, &mappedbno
, bpp
, whichfork
, 1,
2163 (inst_t
*)__return_address
);
2167 * Readahead the dir/attr block.
2179 if (xfs_da_do_buf(trans
, dp
, bno
, &rval
, NULL
, whichfork
, 3,
2180 (inst_t
*)__return_address
))
2186 kmem_zone_t
*xfs_da_state_zone
; /* anchor for state struct zone */
2187 kmem_zone_t
*xfs_dabuf_zone
; /* dabuf zone */
2190 * Allocate a dir-state structure.
2191 * We don't put them on the stack since they're large.
2194 xfs_da_state_alloc(void)
2196 return kmem_zone_zalloc(xfs_da_state_zone
, KM_NOFS
);
2200 * Kill the altpath contents of a da-state structure.
2203 xfs_da_state_kill_altpath(xfs_da_state_t
*state
)
2207 for (i
= 0; i
< state
->altpath
.active
; i
++) {
2208 if (state
->altpath
.blk
[i
].bp
) {
2209 if (state
->altpath
.blk
[i
].bp
!= state
->path
.blk
[i
].bp
)
2210 xfs_da_buf_done(state
->altpath
.blk
[i
].bp
);
2211 state
->altpath
.blk
[i
].bp
= NULL
;
2214 state
->altpath
.active
= 0;
2218 * Free a da-state structure.
2221 xfs_da_state_free(xfs_da_state_t
*state
)
2225 xfs_da_state_kill_altpath(state
);
2226 for (i
= 0; i
< state
->path
.active
; i
++) {
2227 if (state
->path
.blk
[i
].bp
)
2228 xfs_da_buf_done(state
->path
.blk
[i
].bp
);
2230 if (state
->extravalid
&& state
->extrablk
.bp
)
2231 xfs_da_buf_done(state
->extrablk
.bp
);
2233 memset((char *)state
, 0, sizeof(*state
));
2235 kmem_zone_free(xfs_da_state_zone
, state
);
2238 #ifdef XFS_DABUF_DEBUG
2239 xfs_dabuf_t
*xfs_dabuf_global_list
;
2240 static DEFINE_SPINLOCK(xfs_dabuf_global_lock
);
2247 STATIC xfs_dabuf_t
*
2248 xfs_da_buf_make(int nbuf
, xfs_buf_t
**bps
, inst_t
*ra
)
2256 dabuf
= kmem_zone_alloc(xfs_dabuf_zone
, KM_NOFS
);
2258 dabuf
= kmem_alloc(XFS_DA_BUF_SIZE(nbuf
), KM_NOFS
);
2260 #ifdef XFS_DABUF_DEBUG
2262 dabuf
->target
= XFS_BUF_TARGET(bps
[0]);
2263 dabuf
->blkno
= XFS_BUF_ADDR(bps
[0]);
2268 dabuf
->bbcount
= (short)BTOBB(XFS_BUF_COUNT(bp
));
2269 dabuf
->data
= XFS_BUF_PTR(bp
);
2273 for (i
= 0, dabuf
->bbcount
= 0; i
< nbuf
; i
++) {
2274 dabuf
->bps
[i
] = bp
= bps
[i
];
2275 dabuf
->bbcount
+= BTOBB(XFS_BUF_COUNT(bp
));
2277 dabuf
->data
= kmem_alloc(BBTOB(dabuf
->bbcount
), KM_SLEEP
);
2278 for (i
= off
= 0; i
< nbuf
; i
++, off
+= XFS_BUF_COUNT(bp
)) {
2280 memcpy((char *)dabuf
->data
+ off
, XFS_BUF_PTR(bp
),
2284 #ifdef XFS_DABUF_DEBUG
2288 spin_lock(&xfs_dabuf_global_lock
);
2289 for (p
= xfs_dabuf_global_list
; p
; p
= p
->next
) {
2290 ASSERT(p
->blkno
!= dabuf
->blkno
||
2291 p
->target
!= dabuf
->target
);
2294 if (xfs_dabuf_global_list
)
2295 xfs_dabuf_global_list
->prev
= dabuf
;
2296 dabuf
->next
= xfs_dabuf_global_list
;
2297 xfs_dabuf_global_list
= dabuf
;
2298 spin_unlock(&xfs_dabuf_global_lock
);
2308 xfs_da_buf_clean(xfs_dabuf_t
*dabuf
)
2315 ASSERT(dabuf
->nbuf
> 1);
2317 for (i
= off
= 0; i
< dabuf
->nbuf
;
2318 i
++, off
+= XFS_BUF_COUNT(bp
)) {
2320 memcpy(XFS_BUF_PTR(bp
), (char *)dabuf
->data
+ off
,
2330 xfs_da_buf_done(xfs_dabuf_t
*dabuf
)
2333 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2335 xfs_da_buf_clean(dabuf
);
2336 if (dabuf
->nbuf
> 1)
2337 kmem_free(dabuf
->data
);
2338 #ifdef XFS_DABUF_DEBUG
2340 spin_lock(&xfs_dabuf_global_lock
);
2342 dabuf
->prev
->next
= dabuf
->next
;
2344 xfs_dabuf_global_list
= dabuf
->next
;
2346 dabuf
->next
->prev
= dabuf
->prev
;
2347 spin_unlock(&xfs_dabuf_global_lock
);
2349 memset(dabuf
, 0, XFS_DA_BUF_SIZE(dabuf
->nbuf
));
2351 if (dabuf
->nbuf
== 1)
2352 kmem_zone_free(xfs_dabuf_zone
, dabuf
);
2358 * Log transaction from a dabuf.
2361 xfs_da_log_buf(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
, uint first
, uint last
)
2369 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2370 if (dabuf
->nbuf
== 1) {
2371 ASSERT(dabuf
->data
== (void *)XFS_BUF_PTR(dabuf
->bps
[0]));
2372 xfs_trans_log_buf(tp
, dabuf
->bps
[0], first
, last
);
2376 ASSERT(first
<= last
);
2377 for (i
= off
= 0; i
< dabuf
->nbuf
; i
++, off
+= XFS_BUF_COUNT(bp
)) {
2380 l
= f
+ XFS_BUF_COUNT(bp
) - 1;
2386 xfs_trans_log_buf(tp
, bp
, f
- off
, l
- off
);
2388 * B_DONE is set by xfs_trans_log buf.
2389 * If we don't set it on a new buffer (get not read)
2390 * then if we don't put anything in the buffer it won't
2391 * be set, and at commit it it released into the cache,
2392 * and then a read will fail.
2394 else if (!(XFS_BUF_ISDONE(bp
)))
2401 * Release dabuf from a transaction.
2402 * Have to free up the dabuf before the buffers are released,
2403 * since the synchronization on the dabuf is really the lock on the buffer.
2406 xfs_da_brelse(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
)
2413 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2414 if ((nbuf
= dabuf
->nbuf
) == 1) {
2418 bplist
= kmem_alloc(nbuf
* sizeof(*bplist
), KM_SLEEP
);
2419 memcpy(bplist
, dabuf
->bps
, nbuf
* sizeof(*bplist
));
2421 xfs_da_buf_done(dabuf
);
2422 for (i
= 0; i
< nbuf
; i
++)
2423 xfs_trans_brelse(tp
, bplist
[i
]);
2429 * Invalidate dabuf from a transaction.
2432 xfs_da_binval(xfs_trans_t
*tp
, xfs_dabuf_t
*dabuf
)
2439 ASSERT(dabuf
->nbuf
&& dabuf
->data
&& dabuf
->bbcount
&& dabuf
->bps
[0]);
2440 if ((nbuf
= dabuf
->nbuf
) == 1) {
2444 bplist
= kmem_alloc(nbuf
* sizeof(*bplist
), KM_SLEEP
);
2445 memcpy(bplist
, dabuf
->bps
, nbuf
* sizeof(*bplist
));
2447 xfs_da_buf_done(dabuf
);
2448 for (i
= 0; i
< nbuf
; i
++)
2449 xfs_trans_binval(tp
, bplist
[i
]);
2455 * Get the first daddr from a dabuf.
2458 xfs_da_blkno(xfs_dabuf_t
*dabuf
)
2460 ASSERT(dabuf
->nbuf
);
2461 ASSERT(dabuf
->data
);
2462 return XFS_BUF_ADDR(dabuf
->bps
[0]);