]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/xfs_da_btree.c
[XFS] Remove old, broken nolog-mode code - noone plans to ever fix it.
[net-next-2.6.git] / fs / xfs / xfs_da_btree.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_dir.h"
28#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
a844f451 31#include "xfs_da_btree.h"
1da177e4 32#include "xfs_bmap_btree.h"
a844f451 33#include "xfs_alloc_btree.h"
1da177e4 34#include "xfs_ialloc_btree.h"
1da177e4
LT
35#include "xfs_dir_sf.h"
36#include "xfs_dir2_sf.h"
a844f451 37#include "xfs_attr_sf.h"
1da177e4 38#include "xfs_dinode.h"
1da177e4 39#include "xfs_inode.h"
a844f451
NS
40#include "xfs_inode_item.h"
41#include "xfs_alloc.h"
42#include "xfs_btree.h"
1da177e4 43#include "xfs_bmap.h"
1da177e4
LT
44#include "xfs_attr.h"
45#include "xfs_attr_leaf.h"
46#include "xfs_dir_leaf.h"
47#include "xfs_dir2_data.h"
48#include "xfs_dir2_leaf.h"
49#include "xfs_dir2_block.h"
50#include "xfs_dir2_node.h"
51#include "xfs_error.h"
1da177e4
LT
52
53/*
54 * xfs_da_btree.c
55 *
56 * Routines to implement directories as Btrees of hashed names.
57 */
58
59/*========================================================================
60 * Function prototypes for the kernel.
61 *========================================================================*/
62
63/*
64 * Routines used for growing the Btree.
65 */
66STATIC int xfs_da_root_split(xfs_da_state_t *state,
67 xfs_da_state_blk_t *existing_root,
68 xfs_da_state_blk_t *new_child);
69STATIC int xfs_da_node_split(xfs_da_state_t *state,
70 xfs_da_state_blk_t *existing_blk,
71 xfs_da_state_blk_t *split_blk,
72 xfs_da_state_blk_t *blk_to_add,
73 int treelevel,
74 int *result);
75STATIC void xfs_da_node_rebalance(xfs_da_state_t *state,
76 xfs_da_state_blk_t *node_blk_1,
77 xfs_da_state_blk_t *node_blk_2);
78STATIC void xfs_da_node_add(xfs_da_state_t *state,
79 xfs_da_state_blk_t *old_node_blk,
80 xfs_da_state_blk_t *new_node_blk);
81
82/*
83 * Routines used for shrinking the Btree.
84 */
85STATIC int xfs_da_root_join(xfs_da_state_t *state,
86 xfs_da_state_blk_t *root_blk);
87STATIC int xfs_da_node_toosmall(xfs_da_state_t *state, int *retval);
88STATIC void xfs_da_node_remove(xfs_da_state_t *state,
89 xfs_da_state_blk_t *drop_blk);
90STATIC void xfs_da_node_unbalance(xfs_da_state_t *state,
91 xfs_da_state_blk_t *src_node_blk,
92 xfs_da_state_blk_t *dst_node_blk);
93
94/*
95 * Utility routines.
96 */
97STATIC uint xfs_da_node_lasthash(xfs_dabuf_t *bp, int *count);
98STATIC int xfs_da_node_order(xfs_dabuf_t *node1_bp, xfs_dabuf_t *node2_bp);
99STATIC xfs_dabuf_t *xfs_da_buf_make(int nbuf, xfs_buf_t **bps, inst_t *ra);
ba0f32d4
CH
100STATIC int xfs_da_blk_unlink(xfs_da_state_t *state,
101 xfs_da_state_blk_t *drop_blk,
102 xfs_da_state_blk_t *save_blk);
103STATIC void xfs_da_state_kill_altpath(xfs_da_state_t *state);
1da177e4
LT
104
105/*========================================================================
106 * Routines used for growing the Btree.
107 *========================================================================*/
108
109/*
110 * Create the initial contents of an intermediate node.
111 */
112int
113xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
114 xfs_dabuf_t **bpp, int whichfork)
115{
116 xfs_da_intnode_t *node;
117 xfs_dabuf_t *bp;
118 int error;
119 xfs_trans_t *tp;
120
121 tp = args->trans;
122 error = xfs_da_get_buf(tp, args->dp, blkno, -1, &bp, whichfork);
123 if (error)
124 return(error);
125 ASSERT(bp != NULL);
126 node = bp->data;
127 node->hdr.info.forw = 0;
128 node->hdr.info.back = 0;
129 INT_SET(node->hdr.info.magic, ARCH_CONVERT, XFS_DA_NODE_MAGIC);
130 node->hdr.info.pad = 0;
131 node->hdr.count = 0;
132 INT_SET(node->hdr.level, ARCH_CONVERT, level);
133
134 xfs_da_log_buf(tp, bp,
135 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
136
137 *bpp = bp;
138 return(0);
139}
140
141/*
142 * Split a leaf node, rebalance, then possibly split
143 * intermediate nodes, rebalance, etc.
144 */
145int /* error */
146xfs_da_split(xfs_da_state_t *state)
147{
148 xfs_da_state_blk_t *oldblk, *newblk, *addblk;
149 xfs_da_intnode_t *node;
150 xfs_dabuf_t *bp;
151 int max, action, error, i;
152
153 /*
154 * Walk back up the tree splitting/inserting/adjusting as necessary.
155 * If we need to insert and there isn't room, split the node, then
156 * decide which fragment to insert the new block from below into.
157 * Note that we may split the root this way, but we need more fixup.
158 */
159 max = state->path.active - 1;
160 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
161 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
162 state->path.blk[max].magic == XFS_DIRX_LEAF_MAGIC(state->mp));
163
164 addblk = &state->path.blk[max]; /* initial dummy value */
165 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
166 oldblk = &state->path.blk[i];
167 newblk = &state->altpath.blk[i];
168
169 /*
170 * If a leaf node then
171 * Allocate a new leaf node, then rebalance across them.
172 * else if an intermediate node then
173 * We split on the last layer, must we split the node?
174 */
175 switch (oldblk->magic) {
176 case XFS_ATTR_LEAF_MAGIC:
1da177e4
LT
177 error = xfs_attr_leaf_split(state, oldblk, newblk);
178 if ((error != 0) && (error != ENOSPC)) {
179 return(error); /* GROT: attr is inconsistent */
180 }
181 if (!error) {
182 addblk = newblk;
183 break;
184 }
185 /*
186 * Entry wouldn't fit, split the leaf again.
187 */
188 state->extravalid = 1;
189 if (state->inleaf) {
190 state->extraafter = 0; /* before newblk */
191 error = xfs_attr_leaf_split(state, oldblk,
192 &state->extrablk);
193 } else {
194 state->extraafter = 1; /* after newblk */
195 error = xfs_attr_leaf_split(state, newblk,
196 &state->extrablk);
197 }
198 if (error)
199 return(error); /* GROT: attr inconsistent */
200 addblk = newblk;
201 break;
1da177e4
LT
202 case XFS_DIR_LEAF_MAGIC:
203 ASSERT(XFS_DIR_IS_V1(state->mp));
204 error = xfs_dir_leaf_split(state, oldblk, newblk);
205 if ((error != 0) && (error != ENOSPC)) {
206 return(error); /* GROT: dir is inconsistent */
207 }
208 if (!error) {
209 addblk = newblk;
210 break;
211 }
212 /*
213 * Entry wouldn't fit, split the leaf again.
214 */
215 state->extravalid = 1;
216 if (state->inleaf) {
217 state->extraafter = 0; /* before newblk */
218 error = xfs_dir_leaf_split(state, oldblk,
219 &state->extrablk);
220 if (error)
221 return(error); /* GROT: dir incon. */
222 addblk = newblk;
223 } else {
224 state->extraafter = 1; /* after newblk */
225 error = xfs_dir_leaf_split(state, newblk,
226 &state->extrablk);
227 if (error)
228 return(error); /* GROT: dir incon. */
229 addblk = newblk;
230 }
231 break;
232 case XFS_DIR2_LEAFN_MAGIC:
233 ASSERT(XFS_DIR_IS_V2(state->mp));
234 error = xfs_dir2_leafn_split(state, oldblk, newblk);
235 if (error)
236 return error;
237 addblk = newblk;
238 break;
239 case XFS_DA_NODE_MAGIC:
240 error = xfs_da_node_split(state, oldblk, newblk, addblk,
241 max - i, &action);
242 xfs_da_buf_done(addblk->bp);
243 addblk->bp = NULL;
244 if (error)
245 return(error); /* GROT: dir is inconsistent */
246 /*
247 * Record the newly split block for the next time thru?
248 */
249 if (action)
250 addblk = newblk;
251 else
252 addblk = NULL;
253 break;
254 }
255
256 /*
257 * Update the btree to show the new hashval for this child.
258 */
259 xfs_da_fixhashpath(state, &state->path);
260 /*
261 * If we won't need this block again, it's getting dropped
262 * from the active path by the loop control, so we need
263 * to mark it done now.
264 */
265 if (i > 0 || !addblk)
266 xfs_da_buf_done(oldblk->bp);
267 }
268 if (!addblk)
269 return(0);
270
271 /*
272 * Split the root node.
273 */
274 ASSERT(state->path.active == 0);
275 oldblk = &state->path.blk[0];
276 error = xfs_da_root_split(state, oldblk, addblk);
277 if (error) {
278 xfs_da_buf_done(oldblk->bp);
279 xfs_da_buf_done(addblk->bp);
280 addblk->bp = NULL;
281 return(error); /* GROT: dir is inconsistent */
282 }
283
284 /*
285 * Update pointers to the node which used to be block 0 and
286 * just got bumped because of the addition of a new root node.
287 * There might be three blocks involved if a double split occurred,
288 * and the original block 0 could be at any position in the list.
289 */
290
291 node = oldblk->bp->data;
292 if (node->hdr.info.forw) {
293 if (INT_GET(node->hdr.info.forw, ARCH_CONVERT) == addblk->blkno) {
294 bp = addblk->bp;
295 } else {
296 ASSERT(state->extravalid);
297 bp = state->extrablk.bp;
298 }
299 node = bp->data;
300 INT_SET(node->hdr.info.back, ARCH_CONVERT, oldblk->blkno);
301 xfs_da_log_buf(state->args->trans, bp,
302 XFS_DA_LOGRANGE(node, &node->hdr.info,
303 sizeof(node->hdr.info)));
304 }
305 node = oldblk->bp->data;
306 if (INT_GET(node->hdr.info.back, ARCH_CONVERT)) {
307 if (INT_GET(node->hdr.info.back, ARCH_CONVERT) == addblk->blkno) {
308 bp = addblk->bp;
309 } else {
310 ASSERT(state->extravalid);
311 bp = state->extrablk.bp;
312 }
313 node = bp->data;
314 INT_SET(node->hdr.info.forw, ARCH_CONVERT, oldblk->blkno);
315 xfs_da_log_buf(state->args->trans, bp,
316 XFS_DA_LOGRANGE(node, &node->hdr.info,
317 sizeof(node->hdr.info)));
318 }
319 xfs_da_buf_done(oldblk->bp);
320 xfs_da_buf_done(addblk->bp);
321 addblk->bp = NULL;
322 return(0);
323}
324
325/*
326 * Split the root. We have to create a new root and point to the two
327 * parts (the split old root) that we just created. Copy block zero to
328 * the EOF, extending the inode in process.
329 */
330STATIC int /* error */
331xfs_da_root_split(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
332 xfs_da_state_blk_t *blk2)
333{
334 xfs_da_intnode_t *node, *oldroot;
335 xfs_da_args_t *args;
336 xfs_dablk_t blkno;
337 xfs_dabuf_t *bp;
338 int error, size;
339 xfs_inode_t *dp;
340 xfs_trans_t *tp;
341 xfs_mount_t *mp;
342 xfs_dir2_leaf_t *leaf;
343
344 /*
345 * Copy the existing (incorrect) block from the root node position
346 * to a free space somewhere.
347 */
348 args = state->args;
349 ASSERT(args != NULL);
350 error = xfs_da_grow_inode(args, &blkno);
351 if (error)
352 return(error);
353 dp = args->dp;
354 tp = args->trans;
355 mp = state->mp;
356 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
357 if (error)
358 return(error);
359 ASSERT(bp != NULL);
360 node = bp->data;
361 oldroot = blk1->bp->data;
362 if (INT_GET(oldroot->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
363 size = (int)((char *)&oldroot->btree[INT_GET(oldroot->hdr.count, ARCH_CONVERT)] -
364 (char *)oldroot);
365 } else {
366 ASSERT(XFS_DIR_IS_V2(mp));
367 ASSERT(INT_GET(oldroot->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
368 leaf = (xfs_dir2_leaf_t *)oldroot;
369 size = (int)((char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] -
370 (char *)leaf);
371 }
372 memcpy(node, oldroot, size);
373 xfs_da_log_buf(tp, bp, 0, size - 1);
374 xfs_da_buf_done(blk1->bp);
375 blk1->bp = bp;
376 blk1->blkno = blkno;
377
378 /*
379 * Set up the new root node.
380 */
381 error = xfs_da_node_create(args,
382 args->whichfork == XFS_DATA_FORK &&
383 XFS_DIR_IS_V2(mp) ? mp->m_dirleafblk : 0,
384 INT_GET(node->hdr.level, ARCH_CONVERT) + 1, &bp, args->whichfork);
385 if (error)
386 return(error);
387 node = bp->data;
388 INT_SET(node->btree[0].hashval, ARCH_CONVERT, blk1->hashval);
389 INT_SET(node->btree[0].before, ARCH_CONVERT, blk1->blkno);
390 INT_SET(node->btree[1].hashval, ARCH_CONVERT, blk2->hashval);
391 INT_SET(node->btree[1].before, ARCH_CONVERT, blk2->blkno);
392 INT_SET(node->hdr.count, ARCH_CONVERT, 2);
393
394#ifdef DEBUG
395 if (INT_GET(oldroot->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC) {
396 ASSERT(blk1->blkno >= mp->m_dirleafblk &&
397 blk1->blkno < mp->m_dirfreeblk);
398 ASSERT(blk2->blkno >= mp->m_dirleafblk &&
399 blk2->blkno < mp->m_dirfreeblk);
400 }
401#endif
402
403 /* Header is already logged by xfs_da_node_create */
404 xfs_da_log_buf(tp, bp,
405 XFS_DA_LOGRANGE(node, node->btree,
406 sizeof(xfs_da_node_entry_t) * 2));
407 xfs_da_buf_done(bp);
408
409 return(0);
410}
411
412/*
413 * Split the node, rebalance, then add the new entry.
414 */
415STATIC int /* error */
416xfs_da_node_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
417 xfs_da_state_blk_t *newblk,
418 xfs_da_state_blk_t *addblk,
419 int treelevel, int *result)
420{
421 xfs_da_intnode_t *node;
422 xfs_dablk_t blkno;
423 int newcount, error;
424 int useextra;
425
426 node = oldblk->bp->data;
427 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
428
429 /*
430 * With V2 the extra block is data or freespace.
431 */
432 useextra = state->extravalid && XFS_DIR_IS_V1(state->mp);
433 newcount = 1 + useextra;
434 /*
435 * Do we have to split the node?
436 */
437 if ((INT_GET(node->hdr.count, ARCH_CONVERT) + newcount) > state->node_ents) {
438 /*
439 * Allocate a new node, add to the doubly linked chain of
440 * nodes, then move some of our excess entries into it.
441 */
442 error = xfs_da_grow_inode(state->args, &blkno);
443 if (error)
444 return(error); /* GROT: dir is inconsistent */
445
446 error = xfs_da_node_create(state->args, blkno, treelevel,
447 &newblk->bp, state->args->whichfork);
448 if (error)
449 return(error); /* GROT: dir is inconsistent */
450 newblk->blkno = blkno;
451 newblk->magic = XFS_DA_NODE_MAGIC;
452 xfs_da_node_rebalance(state, oldblk, newblk);
453 error = xfs_da_blk_link(state, oldblk, newblk);
454 if (error)
455 return(error);
456 *result = 1;
457 } else {
458 *result = 0;
459 }
460
461 /*
462 * Insert the new entry(s) into the correct block
463 * (updating last hashval in the process).
464 *
465 * xfs_da_node_add() inserts BEFORE the given index,
466 * and as a result of using node_lookup_int() we always
467 * point to a valid entry (not after one), but a split
468 * operation always results in a new block whose hashvals
469 * FOLLOW the current block.
470 *
471 * If we had double-split op below us, then add the extra block too.
472 */
473 node = oldblk->bp->data;
474 if (oldblk->index <= INT_GET(node->hdr.count, ARCH_CONVERT)) {
475 oldblk->index++;
476 xfs_da_node_add(state, oldblk, addblk);
477 if (useextra) {
478 if (state->extraafter)
479 oldblk->index++;
480 xfs_da_node_add(state, oldblk, &state->extrablk);
481 state->extravalid = 0;
482 }
483 } else {
484 newblk->index++;
485 xfs_da_node_add(state, newblk, addblk);
486 if (useextra) {
487 if (state->extraafter)
488 newblk->index++;
489 xfs_da_node_add(state, newblk, &state->extrablk);
490 state->extravalid = 0;
491 }
492 }
493
494 return(0);
495}
496
497/*
498 * Balance the btree elements between two intermediate nodes,
499 * usually one full and one empty.
500 *
501 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
502 */
503STATIC void
504xfs_da_node_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
505 xfs_da_state_blk_t *blk2)
506{
507 xfs_da_intnode_t *node1, *node2, *tmpnode;
508 xfs_da_node_entry_t *btree_s, *btree_d;
509 int count, tmp;
510 xfs_trans_t *tp;
511
512 node1 = blk1->bp->data;
513 node2 = blk2->bp->data;
514 /*
515 * Figure out how many entries need to move, and in which direction.
516 * Swap the nodes around if that makes it simpler.
517 */
518 if ((INT_GET(node1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(node2->hdr.count, ARCH_CONVERT) > 0) &&
519 ((INT_GET(node2->btree[ 0 ].hashval, ARCH_CONVERT) < INT_GET(node1->btree[ 0 ].hashval, ARCH_CONVERT)) ||
520 (INT_GET(node2->btree[ INT_GET(node2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
521 INT_GET(node1->btree[ INT_GET(node1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
522 tmpnode = node1;
523 node1 = node2;
524 node2 = tmpnode;
525 }
526 ASSERT(INT_GET(node1->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
527 ASSERT(INT_GET(node2->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
528 count = (INT_GET(node1->hdr.count, ARCH_CONVERT) - INT_GET(node2->hdr.count, ARCH_CONVERT)) / 2;
529 if (count == 0)
530 return;
531 tp = state->args->trans;
532 /*
533 * Two cases: high-to-low and low-to-high.
534 */
535 if (count > 0) {
536 /*
537 * Move elements in node2 up to make a hole.
538 */
539 if ((tmp = INT_GET(node2->hdr.count, ARCH_CONVERT)) > 0) {
540 tmp *= (uint)sizeof(xfs_da_node_entry_t);
541 btree_s = &node2->btree[0];
542 btree_d = &node2->btree[count];
543 memmove(btree_d, btree_s, tmp);
544 }
545
546 /*
547 * Move the req'd B-tree elements from high in node1 to
548 * low in node2.
549 */
550 INT_MOD(node2->hdr.count, ARCH_CONVERT, count);
551 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
552 btree_s = &node1->btree[INT_GET(node1->hdr.count, ARCH_CONVERT) - count];
553 btree_d = &node2->btree[0];
554 memcpy(btree_d, btree_s, tmp);
555 INT_MOD(node1->hdr.count, ARCH_CONVERT, -(count));
556
557 } else {
558 /*
559 * Move the req'd B-tree elements from low in node2 to
560 * high in node1.
561 */
562 count = -count;
563 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
564 btree_s = &node2->btree[0];
565 btree_d = &node1->btree[INT_GET(node1->hdr.count, ARCH_CONVERT)];
566 memcpy(btree_d, btree_s, tmp);
567 INT_MOD(node1->hdr.count, ARCH_CONVERT, count);
568 xfs_da_log_buf(tp, blk1->bp,
569 XFS_DA_LOGRANGE(node1, btree_d, tmp));
570
571 /*
572 * Move elements in node2 down to fill the hole.
573 */
574 tmp = INT_GET(node2->hdr.count, ARCH_CONVERT) - count;
575 tmp *= (uint)sizeof(xfs_da_node_entry_t);
576 btree_s = &node2->btree[count];
577 btree_d = &node2->btree[0];
578 memmove(btree_d, btree_s, tmp);
579 INT_MOD(node2->hdr.count, ARCH_CONVERT, -(count));
580 }
581
582 /*
583 * Log header of node 1 and all current bits of node 2.
584 */
585 xfs_da_log_buf(tp, blk1->bp,
586 XFS_DA_LOGRANGE(node1, &node1->hdr, sizeof(node1->hdr)));
587 xfs_da_log_buf(tp, blk2->bp,
588 XFS_DA_LOGRANGE(node2, &node2->hdr,
589 sizeof(node2->hdr) +
590 sizeof(node2->btree[0]) * INT_GET(node2->hdr.count, ARCH_CONVERT)));
591
592 /*
593 * Record the last hashval from each block for upward propagation.
594 * (note: don't use the swapped node pointers)
595 */
596 node1 = blk1->bp->data;
597 node2 = blk2->bp->data;
598 blk1->hashval = INT_GET(node1->btree[ INT_GET(node1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
599 blk2->hashval = INT_GET(node2->btree[ INT_GET(node2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
600
601 /*
602 * Adjust the expected index for insertion.
603 */
604 if (blk1->index >= INT_GET(node1->hdr.count, ARCH_CONVERT)) {
605 blk2->index = blk1->index - INT_GET(node1->hdr.count, ARCH_CONVERT);
606 blk1->index = INT_GET(node1->hdr.count, ARCH_CONVERT) + 1; /* make it invalid */
607 }
608}
609
610/*
611 * Add a new entry to an intermediate node.
612 */
613STATIC void
614xfs_da_node_add(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
615 xfs_da_state_blk_t *newblk)
616{
617 xfs_da_intnode_t *node;
618 xfs_da_node_entry_t *btree;
619 int tmp;
620 xfs_mount_t *mp;
621
622 node = oldblk->bp->data;
623 mp = state->mp;
624 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
625 ASSERT((oldblk->index >= 0) && (oldblk->index <= INT_GET(node->hdr.count, ARCH_CONVERT)));
626 ASSERT(newblk->blkno != 0);
627 if (state->args->whichfork == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
628 ASSERT(newblk->blkno >= mp->m_dirleafblk &&
629 newblk->blkno < mp->m_dirfreeblk);
630
631 /*
632 * We may need to make some room before we insert the new node.
633 */
634 tmp = 0;
635 btree = &node->btree[ oldblk->index ];
636 if (oldblk->index < INT_GET(node->hdr.count, ARCH_CONVERT)) {
637 tmp = (INT_GET(node->hdr.count, ARCH_CONVERT) - oldblk->index) * (uint)sizeof(*btree);
638 memmove(btree + 1, btree, tmp);
639 }
640 INT_SET(btree->hashval, ARCH_CONVERT, newblk->hashval);
641 INT_SET(btree->before, ARCH_CONVERT, newblk->blkno);
642 xfs_da_log_buf(state->args->trans, oldblk->bp,
643 XFS_DA_LOGRANGE(node, btree, tmp + sizeof(*btree)));
644 INT_MOD(node->hdr.count, ARCH_CONVERT, +1);
645 xfs_da_log_buf(state->args->trans, oldblk->bp,
646 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
647
648 /*
649 * Copy the last hash value from the oldblk to propagate upwards.
650 */
651 oldblk->hashval = INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
652}
653
654/*========================================================================
655 * Routines used for shrinking the Btree.
656 *========================================================================*/
657
658/*
659 * Deallocate an empty leaf node, remove it from its parent,
660 * possibly deallocating that block, etc...
661 */
662int
663xfs_da_join(xfs_da_state_t *state)
664{
665 xfs_da_state_blk_t *drop_blk, *save_blk;
666 int action, error;
667
668 action = 0;
669 drop_blk = &state->path.blk[ state->path.active-1 ];
670 save_blk = &state->altpath.blk[ state->path.active-1 ];
671 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
672 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
673 drop_blk->magic == XFS_DIRX_LEAF_MAGIC(state->mp));
674
675 /*
676 * Walk back up the tree joining/deallocating as necessary.
677 * When we stop dropping blocks, break out.
678 */
679 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
680 state->path.active--) {
681 /*
682 * See if we can combine the block with a neighbor.
683 * (action == 0) => no options, just leave
684 * (action == 1) => coalesce, then unlink
685 * (action == 2) => block empty, unlink it
686 */
687 switch (drop_blk->magic) {
688 case XFS_ATTR_LEAF_MAGIC:
1da177e4 689 error = xfs_attr_leaf_toosmall(state, &action);
1da177e4
LT
690 if (error)
691 return(error);
692 if (action == 0)
693 return(0);
1da177e4 694 xfs_attr_leaf_unbalance(state, drop_blk, save_blk);
1da177e4
LT
695 break;
696 case XFS_DIR_LEAF_MAGIC:
697 ASSERT(XFS_DIR_IS_V1(state->mp));
698 error = xfs_dir_leaf_toosmall(state, &action);
699 if (error)
700 return(error);
701 if (action == 0)
702 return(0);
703 xfs_dir_leaf_unbalance(state, drop_blk, save_blk);
704 break;
705 case XFS_DIR2_LEAFN_MAGIC:
706 ASSERT(XFS_DIR_IS_V2(state->mp));
707 error = xfs_dir2_leafn_toosmall(state, &action);
708 if (error)
709 return error;
710 if (action == 0)
711 return 0;
712 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
713 break;
714 case XFS_DA_NODE_MAGIC:
715 /*
716 * Remove the offending node, fixup hashvals,
717 * check for a toosmall neighbor.
718 */
719 xfs_da_node_remove(state, drop_blk);
720 xfs_da_fixhashpath(state, &state->path);
721 error = xfs_da_node_toosmall(state, &action);
722 if (error)
723 return(error);
724 if (action == 0)
725 return 0;
726 xfs_da_node_unbalance(state, drop_blk, save_blk);
727 break;
728 }
729 xfs_da_fixhashpath(state, &state->altpath);
730 error = xfs_da_blk_unlink(state, drop_blk, save_blk);
731 xfs_da_state_kill_altpath(state);
732 if (error)
733 return(error);
734 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
735 drop_blk->bp);
736 drop_blk->bp = NULL;
737 if (error)
738 return(error);
739 }
740 /*
741 * We joined all the way to the top. If it turns out that
742 * we only have one entry in the root, make the child block
743 * the new root.
744 */
745 xfs_da_node_remove(state, drop_blk);
746 xfs_da_fixhashpath(state, &state->path);
747 error = xfs_da_root_join(state, &state->path.blk[0]);
748 return(error);
749}
750
751/*
752 * We have only one entry in the root. Copy the only remaining child of
753 * the old root to block 0 as the new root node.
754 */
755STATIC int
756xfs_da_root_join(xfs_da_state_t *state, xfs_da_state_blk_t *root_blk)
757{
758 xfs_da_intnode_t *oldroot;
759 /* REFERENCED */
760 xfs_da_blkinfo_t *blkinfo;
761 xfs_da_args_t *args;
762 xfs_dablk_t child;
763 xfs_dabuf_t *bp;
764 int error;
765
766 args = state->args;
767 ASSERT(args != NULL);
768 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
769 oldroot = root_blk->bp->data;
770 ASSERT(INT_GET(oldroot->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
771 ASSERT(!oldroot->hdr.info.forw);
772 ASSERT(!oldroot->hdr.info.back);
773
774 /*
775 * If the root has more than one child, then don't do anything.
776 */
777 if (INT_GET(oldroot->hdr.count, ARCH_CONVERT) > 1)
778 return(0);
779
780 /*
781 * Read in the (only) child block, then copy those bytes into
782 * the root block's buffer and free the original child block.
783 */
784 child = INT_GET(oldroot->btree[ 0 ].before, ARCH_CONVERT);
785 ASSERT(child != 0);
786 error = xfs_da_read_buf(args->trans, args->dp, child, -1, &bp,
787 args->whichfork);
788 if (error)
789 return(error);
790 ASSERT(bp != NULL);
791 blkinfo = bp->data;
792 if (INT_GET(oldroot->hdr.level, ARCH_CONVERT) == 1) {
793 ASSERT(INT_GET(blkinfo->magic, ARCH_CONVERT) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
794 INT_GET(blkinfo->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
795 } else {
796 ASSERT(INT_GET(blkinfo->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
797 }
798 ASSERT(!blkinfo->forw);
799 ASSERT(!blkinfo->back);
800 memcpy(root_blk->bp->data, bp->data, state->blocksize);
801 xfs_da_log_buf(args->trans, root_blk->bp, 0, state->blocksize - 1);
802 error = xfs_da_shrink_inode(args, child, bp);
803 return(error);
804}
805
806/*
807 * Check a node block and its neighbors to see if the block should be
808 * collapsed into one or the other neighbor. Always keep the block
809 * with the smaller block number.
810 * If the current block is over 50% full, don't try to join it, return 0.
811 * If the block is empty, fill in the state structure and return 2.
812 * If it can be collapsed, fill in the state structure and return 1.
813 * If nothing can be done, return 0.
814 */
815STATIC int
816xfs_da_node_toosmall(xfs_da_state_t *state, int *action)
817{
818 xfs_da_intnode_t *node;
819 xfs_da_state_blk_t *blk;
820 xfs_da_blkinfo_t *info;
821 int count, forward, error, retval, i;
822 xfs_dablk_t blkno;
823 xfs_dabuf_t *bp;
824
825 /*
826 * Check for the degenerate case of the block being over 50% full.
827 * If so, it's not worth even looking to see if we might be able
828 * to coalesce with a sibling.
829 */
830 blk = &state->path.blk[ state->path.active-1 ];
831 info = blk->bp->data;
832 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
833 node = (xfs_da_intnode_t *)info;
834 count = INT_GET(node->hdr.count, ARCH_CONVERT);
835 if (count > (state->node_ents >> 1)) {
836 *action = 0; /* blk over 50%, don't try to join */
837 return(0); /* blk over 50%, don't try to join */
838 }
839
840 /*
841 * Check for the degenerate case of the block being empty.
842 * If the block is empty, we'll simply delete it, no need to
843 * coalesce it with a sibling block. We choose (aribtrarily)
844 * to merge with the forward block unless it is NULL.
845 */
846 if (count == 0) {
847 /*
848 * Make altpath point to the block we want to keep and
849 * path point to the block we want to drop (this one).
850 */
851 forward = info->forw;
852 memcpy(&state->altpath, &state->path, sizeof(state->path));
853 error = xfs_da_path_shift(state, &state->altpath, forward,
854 0, &retval);
855 if (error)
856 return(error);
857 if (retval) {
858 *action = 0;
859 } else {
860 *action = 2;
861 }
862 return(0);
863 }
864
865 /*
866 * Examine each sibling block to see if we can coalesce with
867 * at least 25% free space to spare. We need to figure out
868 * whether to merge with the forward or the backward block.
869 * We prefer coalescing with the lower numbered sibling so as
870 * to shrink a directory over time.
871 */
872 /* start with smaller blk num */
873 forward = (INT_GET(info->forw, ARCH_CONVERT)
874 < INT_GET(info->back, ARCH_CONVERT));
875 for (i = 0; i < 2; forward = !forward, i++) {
876 if (forward)
877 blkno = INT_GET(info->forw, ARCH_CONVERT);
878 else
879 blkno = INT_GET(info->back, ARCH_CONVERT);
880 if (blkno == 0)
881 continue;
882 error = xfs_da_read_buf(state->args->trans, state->args->dp,
883 blkno, -1, &bp, state->args->whichfork);
884 if (error)
885 return(error);
886 ASSERT(bp != NULL);
887
888 node = (xfs_da_intnode_t *)info;
889 count = state->node_ents;
890 count -= state->node_ents >> 2;
891 count -= INT_GET(node->hdr.count, ARCH_CONVERT);
892 node = bp->data;
893 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
894 count -= INT_GET(node->hdr.count, ARCH_CONVERT);
895 xfs_da_brelse(state->args->trans, bp);
896 if (count >= 0)
897 break; /* fits with at least 25% to spare */
898 }
899 if (i >= 2) {
900 *action = 0;
901 return(0);
902 }
903
904 /*
905 * Make altpath point to the block we want to keep (the lower
906 * numbered block) and path point to the block we want to drop.
907 */
908 memcpy(&state->altpath, &state->path, sizeof(state->path));
909 if (blkno < blk->blkno) {
910 error = xfs_da_path_shift(state, &state->altpath, forward,
911 0, &retval);
912 if (error) {
913 return(error);
914 }
915 if (retval) {
916 *action = 0;
917 return(0);
918 }
919 } else {
920 error = xfs_da_path_shift(state, &state->path, forward,
921 0, &retval);
922 if (error) {
923 return(error);
924 }
925 if (retval) {
926 *action = 0;
927 return(0);
928 }
929 }
930 *action = 1;
931 return(0);
932}
933
934/*
935 * Walk back up the tree adjusting hash values as necessary,
936 * when we stop making changes, return.
937 */
938void
939xfs_da_fixhashpath(xfs_da_state_t *state, xfs_da_state_path_t *path)
940{
941 xfs_da_state_blk_t *blk;
942 xfs_da_intnode_t *node;
943 xfs_da_node_entry_t *btree;
944 xfs_dahash_t lasthash=0;
945 int level, count;
946
947 level = path->active-1;
948 blk = &path->blk[ level ];
949 switch (blk->magic) {
1da177e4
LT
950 case XFS_ATTR_LEAF_MAGIC:
951 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
952 if (count == 0)
953 return;
954 break;
1da177e4
LT
955 case XFS_DIR_LEAF_MAGIC:
956 ASSERT(XFS_DIR_IS_V1(state->mp));
957 lasthash = xfs_dir_leaf_lasthash(blk->bp, &count);
958 if (count == 0)
959 return;
960 break;
961 case XFS_DIR2_LEAFN_MAGIC:
962 ASSERT(XFS_DIR_IS_V2(state->mp));
963 lasthash = xfs_dir2_leafn_lasthash(blk->bp, &count);
964 if (count == 0)
965 return;
966 break;
967 case XFS_DA_NODE_MAGIC:
968 lasthash = xfs_da_node_lasthash(blk->bp, &count);
969 if (count == 0)
970 return;
971 break;
972 }
973 for (blk--, level--; level >= 0; blk--, level--) {
974 node = blk->bp->data;
975 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
976 btree = &node->btree[ blk->index ];
977 if (INT_GET(btree->hashval, ARCH_CONVERT) == lasthash)
978 break;
979 blk->hashval = lasthash;
980 INT_SET(btree->hashval, ARCH_CONVERT, lasthash);
981 xfs_da_log_buf(state->args->trans, blk->bp,
982 XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
983
984 lasthash = INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
985 }
986}
987
988/*
989 * Remove an entry from an intermediate node.
990 */
991STATIC void
992xfs_da_node_remove(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk)
993{
994 xfs_da_intnode_t *node;
995 xfs_da_node_entry_t *btree;
996 int tmp;
997
998 node = drop_blk->bp->data;
999 ASSERT(drop_blk->index < INT_GET(node->hdr.count, ARCH_CONVERT));
1000 ASSERT(drop_blk->index >= 0);
1001
1002 /*
1003 * Copy over the offending entry, or just zero it out.
1004 */
1005 btree = &node->btree[drop_blk->index];
1006 if (drop_blk->index < (INT_GET(node->hdr.count, ARCH_CONVERT)-1)) {
1007 tmp = INT_GET(node->hdr.count, ARCH_CONVERT) - drop_blk->index - 1;
1008 tmp *= (uint)sizeof(xfs_da_node_entry_t);
1009 memmove(btree, btree + 1, tmp);
1010 xfs_da_log_buf(state->args->trans, drop_blk->bp,
1011 XFS_DA_LOGRANGE(node, btree, tmp));
1012 btree = &node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ];
1013 }
1014 memset((char *)btree, 0, sizeof(xfs_da_node_entry_t));
1015 xfs_da_log_buf(state->args->trans, drop_blk->bp,
1016 XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
1017 INT_MOD(node->hdr.count, ARCH_CONVERT, -1);
1018 xfs_da_log_buf(state->args->trans, drop_blk->bp,
1019 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
1020
1021 /*
1022 * Copy the last hash value from the block to propagate upwards.
1023 */
1024 btree--;
1025 drop_blk->hashval = INT_GET(btree->hashval, ARCH_CONVERT);
1026}
1027
1028/*
1029 * Unbalance the btree elements between two intermediate nodes,
1030 * move all Btree elements from one node into another.
1031 */
1032STATIC void
1033xfs_da_node_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1034 xfs_da_state_blk_t *save_blk)
1035{
1036 xfs_da_intnode_t *drop_node, *save_node;
1037 xfs_da_node_entry_t *btree;
1038 int tmp;
1039 xfs_trans_t *tp;
1040
1041 drop_node = drop_blk->bp->data;
1042 save_node = save_blk->bp->data;
1043 ASSERT(INT_GET(drop_node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
1044 ASSERT(INT_GET(save_node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
1045 tp = state->args->trans;
1046
1047 /*
1048 * If the dying block has lower hashvals, then move all the
1049 * elements in the remaining block up to make a hole.
1050 */
1051 if ((INT_GET(drop_node->btree[ 0 ].hashval, ARCH_CONVERT) < INT_GET(save_node->btree[ 0 ].hashval, ARCH_CONVERT)) ||
1052 (INT_GET(drop_node->btree[ INT_GET(drop_node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
1053 INT_GET(save_node->btree[ INT_GET(save_node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))
1054 {
1055 btree = &save_node->btree[ INT_GET(drop_node->hdr.count, ARCH_CONVERT) ];
1056 tmp = INT_GET(save_node->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_da_node_entry_t);
1057 memmove(btree, &save_node->btree[0], tmp);
1058 btree = &save_node->btree[0];
1059 xfs_da_log_buf(tp, save_blk->bp,
1060 XFS_DA_LOGRANGE(save_node, btree,
1061 (INT_GET(save_node->hdr.count, ARCH_CONVERT) + INT_GET(drop_node->hdr.count, ARCH_CONVERT)) *
1062 sizeof(xfs_da_node_entry_t)));
1063 } else {
1064 btree = &save_node->btree[ INT_GET(save_node->hdr.count, ARCH_CONVERT) ];
1065 xfs_da_log_buf(tp, save_blk->bp,
1066 XFS_DA_LOGRANGE(save_node, btree,
1067 INT_GET(drop_node->hdr.count, ARCH_CONVERT) *
1068 sizeof(xfs_da_node_entry_t)));
1069 }
1070
1071 /*
1072 * Move all the B-tree elements from drop_blk to save_blk.
1073 */
1074 tmp = INT_GET(drop_node->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_da_node_entry_t);
1075 memcpy(btree, &drop_node->btree[0], tmp);
1076 INT_MOD(save_node->hdr.count, ARCH_CONVERT, INT_GET(drop_node->hdr.count, ARCH_CONVERT));
1077
1078 xfs_da_log_buf(tp, save_blk->bp,
1079 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1080 sizeof(save_node->hdr)));
1081
1082 /*
1083 * Save the last hashval in the remaining block for upward propagation.
1084 */
1085 save_blk->hashval = INT_GET(save_node->btree[ INT_GET(save_node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1086}
1087
1088/*========================================================================
1089 * Routines used for finding things in the Btree.
1090 *========================================================================*/
1091
1092/*
1093 * Walk down the Btree looking for a particular filename, filling
1094 * in the state structure as we go.
1095 *
1096 * We will set the state structure to point to each of the elements
1097 * in each of the nodes where either the hashval is or should be.
1098 *
1099 * We support duplicate hashval's so for each entry in the current
1100 * node that could contain the desired hashval, descend. This is a
1101 * pruned depth-first tree search.
1102 */
1103int /* error */
1104xfs_da_node_lookup_int(xfs_da_state_t *state, int *result)
1105{
1106 xfs_da_state_blk_t *blk;
1107 xfs_da_blkinfo_t *curr;
1108 xfs_da_intnode_t *node;
1109 xfs_da_node_entry_t *btree;
1110 xfs_dablk_t blkno;
1111 int probe, span, max, error, retval;
1112 xfs_dahash_t hashval;
1113 xfs_da_args_t *args;
1114
1115 args = state->args;
1116
1117 /*
1118 * Descend thru the B-tree searching each level for the right
1119 * node to use, until the right hashval is found.
1120 */
1121 if (args->whichfork == XFS_DATA_FORK && XFS_DIR_IS_V2(state->mp))
1122 blkno = state->mp->m_dirleafblk;
1123 else
1124 blkno = 0;
1125 for (blk = &state->path.blk[0], state->path.active = 1;
1126 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1127 blk++, state->path.active++) {
1128 /*
1129 * Read the next node down in the tree.
1130 */
1131 blk->blkno = blkno;
1132 error = xfs_da_read_buf(args->trans, args->dp, blkno,
1133 -1, &blk->bp, args->whichfork);
1134 if (error) {
1135 blk->blkno = 0;
1136 state->path.active--;
1137 return(error);
1138 }
1139 curr = blk->bp->data;
1140 ASSERT(INT_GET(curr->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC ||
1141 INT_GET(curr->magic, ARCH_CONVERT) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
1142 INT_GET(curr->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
1143
1144 /*
1145 * Search an intermediate node for a match.
1146 */
1147 blk->magic = INT_GET(curr->magic, ARCH_CONVERT);
1148 if (INT_GET(curr->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
1149 node = blk->bp->data;
1150 blk->hashval = INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1151
1152 /*
1153 * Binary search. (note: small blocks will skip loop)
1154 */
1155 max = INT_GET(node->hdr.count, ARCH_CONVERT);
1156 probe = span = max / 2;
1157 hashval = args->hashval;
1158 for (btree = &node->btree[probe]; span > 4;
1159 btree = &node->btree[probe]) {
1160 span /= 2;
1161 if (INT_GET(btree->hashval, ARCH_CONVERT) < hashval)
1162 probe += span;
1163 else if (INT_GET(btree->hashval, ARCH_CONVERT) > hashval)
1164 probe -= span;
1165 else
1166 break;
1167 }
1168 ASSERT((probe >= 0) && (probe < max));
1169 ASSERT((span <= 4) || (INT_GET(btree->hashval, ARCH_CONVERT) == hashval));
1170
1171 /*
1172 * Since we may have duplicate hashval's, find the first
1173 * matching hashval in the node.
1174 */
1175 while ((probe > 0) && (INT_GET(btree->hashval, ARCH_CONVERT) >= hashval)) {
1176 btree--;
1177 probe--;
1178 }
1179 while ((probe < max) && (INT_GET(btree->hashval, ARCH_CONVERT) < hashval)) {
1180 btree++;
1181 probe++;
1182 }
1183
1184 /*
1185 * Pick the right block to descend on.
1186 */
1187 if (probe == max) {
1188 blk->index = max-1;
1189 blkno = INT_GET(node->btree[ max-1 ].before, ARCH_CONVERT);
1190 } else {
1191 blk->index = probe;
1192 blkno = INT_GET(btree->before, ARCH_CONVERT);
1193 }
1194 }
1da177e4
LT
1195 else if (INT_GET(curr->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC) {
1196 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1197 break;
1198 }
1da177e4
LT
1199 else if (INT_GET(curr->magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC) {
1200 blk->hashval = xfs_dir_leaf_lasthash(blk->bp, NULL);
1201 break;
1202 }
1203 else if (INT_GET(curr->magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC) {
1204 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp, NULL);
1205 break;
1206 }
1207 }
1208
1209 /*
1210 * A leaf block that ends in the hashval that we are interested in
1211 * (final hashval == search hashval) means that the next block may
1212 * contain more entries with the same hashval, shift upward to the
1213 * next leaf and keep searching.
1214 */
1215 for (;;) {
1216 if (blk->magic == XFS_DIR_LEAF_MAGIC) {
1217 ASSERT(XFS_DIR_IS_V1(state->mp));
1218 retval = xfs_dir_leaf_lookup_int(blk->bp, args,
1219 &blk->index);
1220 } else if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
1221 ASSERT(XFS_DIR_IS_V2(state->mp));
1222 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1223 &blk->index, state);
1224 }
1da177e4
LT
1225 else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1226 retval = xfs_attr_leaf_lookup_int(blk->bp, args);
1227 blk->index = args->index;
1228 args->blkno = blk->blkno;
1229 }
1da177e4
LT
1230 if (((retval == ENOENT) || (retval == ENOATTR)) &&
1231 (blk->hashval == args->hashval)) {
1232 error = xfs_da_path_shift(state, &state->path, 1, 1,
1233 &retval);
1234 if (error)
1235 return(error);
1236 if (retval == 0) {
1237 continue;
1238 }
1da177e4
LT
1239 else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1240 /* path_shift() gives ENOENT */
1241 retval = XFS_ERROR(ENOATTR);
1242 }
1da177e4
LT
1243 }
1244 break;
1245 }
1246 *result = retval;
1247 return(0);
1248}
1249
1250/*========================================================================
1251 * Utility routines.
1252 *========================================================================*/
1253
1254/*
1255 * Link a new block into a doubly linked list of blocks (of whatever type).
1256 */
1257int /* error */
1258xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
1259 xfs_da_state_blk_t *new_blk)
1260{
1261 xfs_da_blkinfo_t *old_info, *new_info, *tmp_info;
1262 xfs_da_args_t *args;
1263 int before=0, error;
1264 xfs_dabuf_t *bp;
1265
1266 /*
1267 * Set up environment.
1268 */
1269 args = state->args;
1270 ASSERT(args != NULL);
1271 old_info = old_blk->bp->data;
1272 new_info = new_blk->bp->data;
1273 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
1274 old_blk->magic == XFS_DIRX_LEAF_MAGIC(state->mp) ||
1275 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
1276 ASSERT(old_blk->magic == INT_GET(old_info->magic, ARCH_CONVERT));
1277 ASSERT(new_blk->magic == INT_GET(new_info->magic, ARCH_CONVERT));
1278 ASSERT(old_blk->magic == new_blk->magic);
1279
1280 switch (old_blk->magic) {
1da177e4
LT
1281 case XFS_ATTR_LEAF_MAGIC:
1282 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1283 break;
1da177e4
LT
1284 case XFS_DIR_LEAF_MAGIC:
1285 ASSERT(XFS_DIR_IS_V1(state->mp));
1286 before = xfs_dir_leaf_order(old_blk->bp, new_blk->bp);
1287 break;
1288 case XFS_DIR2_LEAFN_MAGIC:
1289 ASSERT(XFS_DIR_IS_V2(state->mp));
1290 before = xfs_dir2_leafn_order(old_blk->bp, new_blk->bp);
1291 break;
1292 case XFS_DA_NODE_MAGIC:
1293 before = xfs_da_node_order(old_blk->bp, new_blk->bp);
1294 break;
1295 }
1296
1297 /*
1298 * Link blocks in appropriate order.
1299 */
1300 if (before) {
1301 /*
1302 * Link new block in before existing block.
1303 */
1304 INT_SET(new_info->forw, ARCH_CONVERT, old_blk->blkno);
1305 new_info->back = old_info->back; /* INT_: direct copy */
1306 if (INT_GET(old_info->back, ARCH_CONVERT)) {
1307 error = xfs_da_read_buf(args->trans, args->dp,
1308 INT_GET(old_info->back,
1309 ARCH_CONVERT), -1, &bp,
1310 args->whichfork);
1311 if (error)
1312 return(error);
1313 ASSERT(bp != NULL);
1314 tmp_info = bp->data;
1315 ASSERT(INT_GET(tmp_info->magic, ARCH_CONVERT) == INT_GET(old_info->magic, ARCH_CONVERT));
1316 ASSERT(INT_GET(tmp_info->forw, ARCH_CONVERT) == old_blk->blkno);
1317 INT_SET(tmp_info->forw, ARCH_CONVERT, new_blk->blkno);
1318 xfs_da_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1319 xfs_da_buf_done(bp);
1320 }
1321 INT_SET(old_info->back, ARCH_CONVERT, new_blk->blkno);
1322 } else {
1323 /*
1324 * Link new block in after existing block.
1325 */
1326 new_info->forw = old_info->forw; /* INT_: direct copy */
1327 INT_SET(new_info->back, ARCH_CONVERT, old_blk->blkno);
1328 if (INT_GET(old_info->forw, ARCH_CONVERT)) {
1329 error = xfs_da_read_buf(args->trans, args->dp,
1330 INT_GET(old_info->forw, ARCH_CONVERT), -1, &bp,
1331 args->whichfork);
1332 if (error)
1333 return(error);
1334 ASSERT(bp != NULL);
1335 tmp_info = bp->data;
1336 ASSERT(INT_GET(tmp_info->magic, ARCH_CONVERT)
1337 == INT_GET(old_info->magic, ARCH_CONVERT));
1338 ASSERT(INT_GET(tmp_info->back, ARCH_CONVERT)
1339 == old_blk->blkno);
1340 INT_SET(tmp_info->back, ARCH_CONVERT, new_blk->blkno);
1341 xfs_da_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1342 xfs_da_buf_done(bp);
1343 }
1344 INT_SET(old_info->forw, ARCH_CONVERT, new_blk->blkno);
1345 }
1346
1347 xfs_da_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1348 xfs_da_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
1349 return(0);
1350}
1351
1352/*
1353 * Compare two intermediate nodes for "order".
1354 */
1355STATIC int
1356xfs_da_node_order(xfs_dabuf_t *node1_bp, xfs_dabuf_t *node2_bp)
1357{
1358 xfs_da_intnode_t *node1, *node2;
1359
1360 node1 = node1_bp->data;
1361 node2 = node2_bp->data;
1362 ASSERT((INT_GET(node1->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) &&
1363 (INT_GET(node2->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC));
1364 if ((INT_GET(node1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(node2->hdr.count, ARCH_CONVERT) > 0) &&
1365 ((INT_GET(node2->btree[ 0 ].hashval, ARCH_CONVERT) <
1366 INT_GET(node1->btree[ 0 ].hashval, ARCH_CONVERT)) ||
1367 (INT_GET(node2->btree[ INT_GET(node2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
1368 INT_GET(node1->btree[ INT_GET(node1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
1369 return(1);
1370 }
1371 return(0);
1372}
1373
1374/*
1375 * Pick up the last hashvalue from an intermediate node.
1376 */
1377STATIC uint
1378xfs_da_node_lasthash(xfs_dabuf_t *bp, int *count)
1379{
1380 xfs_da_intnode_t *node;
1381
1382 node = bp->data;
1383 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
1384 if (count)
1385 *count = INT_GET(node->hdr.count, ARCH_CONVERT);
1386 if (!node->hdr.count)
1387 return(0);
1388 return(INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
1389}
1390
1391/*
1392 * Unlink a block from a doubly linked list of blocks.
1393 */
ba0f32d4 1394STATIC int /* error */
1da177e4
LT
1395xfs_da_blk_unlink(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1396 xfs_da_state_blk_t *save_blk)
1397{
1398 xfs_da_blkinfo_t *drop_info, *save_info, *tmp_info;
1399 xfs_da_args_t *args;
1400 xfs_dabuf_t *bp;
1401 int error;
1402
1403 /*
1404 * Set up environment.
1405 */
1406 args = state->args;
1407 ASSERT(args != NULL);
1408 save_info = save_blk->bp->data;
1409 drop_info = drop_blk->bp->data;
1410 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
1411 save_blk->magic == XFS_DIRX_LEAF_MAGIC(state->mp) ||
1412 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1413 ASSERT(save_blk->magic == INT_GET(save_info->magic, ARCH_CONVERT));
1414 ASSERT(drop_blk->magic == INT_GET(drop_info->magic, ARCH_CONVERT));
1415 ASSERT(save_blk->magic == drop_blk->magic);
1416 ASSERT((INT_GET(save_info->forw, ARCH_CONVERT) == drop_blk->blkno) ||
1417 (INT_GET(save_info->back, ARCH_CONVERT) == drop_blk->blkno));
1418 ASSERT((INT_GET(drop_info->forw, ARCH_CONVERT) == save_blk->blkno) ||
1419 (INT_GET(drop_info->back, ARCH_CONVERT) == save_blk->blkno));
1420
1421 /*
1422 * Unlink the leaf block from the doubly linked chain of leaves.
1423 */
1424 if (INT_GET(save_info->back, ARCH_CONVERT) == drop_blk->blkno) {
1425 save_info->back = drop_info->back; /* INT_: direct copy */
1426 if (INT_GET(drop_info->back, ARCH_CONVERT)) {
1427 error = xfs_da_read_buf(args->trans, args->dp,
1428 INT_GET(drop_info->back,
1429 ARCH_CONVERT), -1, &bp,
1430 args->whichfork);
1431 if (error)
1432 return(error);
1433 ASSERT(bp != NULL);
1434 tmp_info = bp->data;
1435 ASSERT(INT_GET(tmp_info->magic, ARCH_CONVERT) == INT_GET(save_info->magic, ARCH_CONVERT));
1436 ASSERT(INT_GET(tmp_info->forw, ARCH_CONVERT) == drop_blk->blkno);
1437 INT_SET(tmp_info->forw, ARCH_CONVERT, save_blk->blkno);
1438 xfs_da_log_buf(args->trans, bp, 0,
1439 sizeof(*tmp_info) - 1);
1440 xfs_da_buf_done(bp);
1441 }
1442 } else {
1443 save_info->forw = drop_info->forw; /* INT_: direct copy */
1444 if (INT_GET(drop_info->forw, ARCH_CONVERT)) {
1445 error = xfs_da_read_buf(args->trans, args->dp,
1446 INT_GET(drop_info->forw, ARCH_CONVERT), -1, &bp,
1447 args->whichfork);
1448 if (error)
1449 return(error);
1450 ASSERT(bp != NULL);
1451 tmp_info = bp->data;
1452 ASSERT(INT_GET(tmp_info->magic, ARCH_CONVERT)
1453 == INT_GET(save_info->magic, ARCH_CONVERT));
1454 ASSERT(INT_GET(tmp_info->back, ARCH_CONVERT)
1455 == drop_blk->blkno);
1456 INT_SET(tmp_info->back, ARCH_CONVERT, save_blk->blkno);
1457 xfs_da_log_buf(args->trans, bp, 0,
1458 sizeof(*tmp_info) - 1);
1459 xfs_da_buf_done(bp);
1460 }
1461 }
1462
1463 xfs_da_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
1464 return(0);
1465}
1466
1467/*
1468 * Move a path "forward" or "!forward" one block at the current level.
1469 *
1470 * This routine will adjust a "path" to point to the next block
1471 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1472 * Btree, including updating pointers to the intermediate nodes between
1473 * the new bottom and the root.
1474 */
1475int /* error */
1476xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
1477 int forward, int release, int *result)
1478{
1479 xfs_da_state_blk_t *blk;
1480 xfs_da_blkinfo_t *info;
1481 xfs_da_intnode_t *node;
1482 xfs_da_args_t *args;
1483 xfs_dablk_t blkno=0;
1484 int level, error;
1485
1486 /*
1487 * Roll up the Btree looking for the first block where our
1488 * current index is not at the edge of the block. Note that
1489 * we skip the bottom layer because we want the sibling block.
1490 */
1491 args = state->args;
1492 ASSERT(args != NULL);
1493 ASSERT(path != NULL);
1494 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1495 level = (path->active-1) - 1; /* skip bottom layer in path */
1496 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
1497 ASSERT(blk->bp != NULL);
1498 node = blk->bp->data;
1499 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
1500 if (forward && (blk->index < INT_GET(node->hdr.count, ARCH_CONVERT)-1)) {
1501 blk->index++;
1502 blkno = INT_GET(node->btree[ blk->index ].before, ARCH_CONVERT);
1503 break;
1504 } else if (!forward && (blk->index > 0)) {
1505 blk->index--;
1506 blkno = INT_GET(node->btree[ blk->index ].before, ARCH_CONVERT);
1507 break;
1508 }
1509 }
1510 if (level < 0) {
1511 *result = XFS_ERROR(ENOENT); /* we're out of our tree */
1512 ASSERT(args->oknoent);
1513 return(0);
1514 }
1515
1516 /*
1517 * Roll down the edge of the subtree until we reach the
1518 * same depth we were at originally.
1519 */
1520 for (blk++, level++; level < path->active; blk++, level++) {
1521 /*
1522 * Release the old block.
1523 * (if it's dirty, trans won't actually let go)
1524 */
1525 if (release)
1526 xfs_da_brelse(args->trans, blk->bp);
1527
1528 /*
1529 * Read the next child block.
1530 */
1531 blk->blkno = blkno;
1532 error = xfs_da_read_buf(args->trans, args->dp, blkno, -1,
1533 &blk->bp, args->whichfork);
1534 if (error)
1535 return(error);
1536 ASSERT(blk->bp != NULL);
1537 info = blk->bp->data;
1538 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC ||
1539 INT_GET(info->magic, ARCH_CONVERT) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
1540 INT_GET(info->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
1541 blk->magic = INT_GET(info->magic, ARCH_CONVERT);
1542 if (INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
1543 node = (xfs_da_intnode_t *)info;
1544 blk->hashval = INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1545 if (forward)
1546 blk->index = 0;
1547 else
1548 blk->index = INT_GET(node->hdr.count, ARCH_CONVERT)-1;
1549 blkno = INT_GET(node->btree[ blk->index ].before, ARCH_CONVERT);
1550 } else {
1551 ASSERT(level == path->active-1);
1552 blk->index = 0;
1553 switch(blk->magic) {
1da177e4
LT
1554 case XFS_ATTR_LEAF_MAGIC:
1555 blk->hashval = xfs_attr_leaf_lasthash(blk->bp,
1556 NULL);
1557 break;
1da177e4
LT
1558 case XFS_DIR_LEAF_MAGIC:
1559 ASSERT(XFS_DIR_IS_V1(state->mp));
1560 blk->hashval = xfs_dir_leaf_lasthash(blk->bp,
1561 NULL);
1562 break;
1563 case XFS_DIR2_LEAFN_MAGIC:
1564 ASSERT(XFS_DIR_IS_V2(state->mp));
1565 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp,
1566 NULL);
1567 break;
1568 default:
1569 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC ||
1570 blk->magic ==
1571 XFS_DIRX_LEAF_MAGIC(state->mp));
1572 break;
1573 }
1574 }
1575 }
1576 *result = 0;
1577 return(0);
1578}
1579
1580
1581/*========================================================================
1582 * Utility routines.
1583 *========================================================================*/
1584
1585/*
1586 * Implement a simple hash on a character string.
1587 * Rotate the hash value by 7 bits, then XOR each character in.
1588 * This is implemented with some source-level loop unrolling.
1589 */
1590xfs_dahash_t
80cce779 1591xfs_da_hashname(const uchar_t *name, int namelen)
1da177e4
LT
1592{
1593 xfs_dahash_t hash;
1594
1595#ifdef SLOWVERSION
1596 /*
1597 * This is the old one-byte-at-a-time version.
1598 */
1599 for (hash = 0; namelen > 0; namelen--)
1600 hash = *name++ ^ rol32(hash, 7);
1601
1602 return(hash);
1603#else
1604 /*
1605 * Do four characters at a time as long as we can.
1606 */
1607 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
1608 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
1609 (name[3] << 0) ^ rol32(hash, 7 * 4);
1610
1611 /*
1612 * Now do the rest of the characters.
1613 */
1614 switch (namelen) {
1615 case 3:
1616 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
1617 rol32(hash, 7 * 3);
1618 case 2:
1619 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
1620 case 1:
1621 return (name[0] << 0) ^ rol32(hash, 7 * 1);
1622 case 0:
1623 return hash;
1624 }
1625 /* NOTREACHED */
1626#endif
1627 return 0; /* keep gcc happy */
1628}
1629
1630/*
1631 * Add a block to the btree ahead of the file.
1632 * Return the new block number to the caller.
1633 */
1634int
1635xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno)
1636{
1637 xfs_fileoff_t bno, b;
1638 xfs_bmbt_irec_t map;
1639 xfs_bmbt_irec_t *mapp;
1640 xfs_inode_t *dp;
1641 int nmap, error, w, count, c, got, i, mapi;
1642 xfs_fsize_t size;
1643 xfs_trans_t *tp;
1644 xfs_mount_t *mp;
1645
1646 dp = args->dp;
1647 mp = dp->i_mount;
1648 w = args->whichfork;
1649 tp = args->trans;
1650 /*
1651 * For new directories adjust the file offset and block count.
1652 */
1653 if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp)) {
1654 bno = mp->m_dirleafblk;
1655 count = mp->m_dirblkfsbs;
1656 } else {
1657 bno = 0;
1658 count = 1;
1659 }
1660 /*
1661 * Find a spot in the file space to put the new block.
1662 */
1663 if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, w))) {
1664 return error;
1665 }
1666 if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
1667 ASSERT(bno >= mp->m_dirleafblk && bno < mp->m_dirfreeblk);
1668 /*
1669 * Try mapping it in one filesystem block.
1670 */
1671 nmap = 1;
1672 ASSERT(args->firstblock != NULL);
1673 if ((error = xfs_bmapi(tp, dp, bno, count,
1674 XFS_BMAPI_AFLAG(w)|XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|
1675 XFS_BMAPI_CONTIG,
1676 args->firstblock, args->total, &map, &nmap,
1677 args->flist))) {
1678 return error;
1679 }
1680 ASSERT(nmap <= 1);
1681 if (nmap == 1) {
1682 mapp = &map;
1683 mapi = 1;
1684 }
1685 /*
1686 * If we didn't get it and the block might work if fragmented,
1687 * try without the CONTIG flag. Loop until we get it all.
1688 */
1689 else if (nmap == 0 && count > 1) {
1690 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
1691 for (b = bno, mapi = 0; b < bno + count; ) {
1692 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
1693 c = (int)(bno + count - b);
1694 if ((error = xfs_bmapi(tp, dp, b, c,
1695 XFS_BMAPI_AFLAG(w)|XFS_BMAPI_WRITE|
1696 XFS_BMAPI_METADATA,
1697 args->firstblock, args->total,
1698 &mapp[mapi], &nmap, args->flist))) {
1699 kmem_free(mapp, sizeof(*mapp) * count);
1700 return error;
1701 }
1702 if (nmap < 1)
1703 break;
1704 mapi += nmap;
1705 b = mapp[mapi - 1].br_startoff +
1706 mapp[mapi - 1].br_blockcount;
1707 }
1708 } else {
1709 mapi = 0;
1710 mapp = NULL;
1711 }
1712 /*
1713 * Count the blocks we got, make sure it matches the total.
1714 */
1715 for (i = 0, got = 0; i < mapi; i++)
1716 got += mapp[i].br_blockcount;
1717 if (got != count || mapp[0].br_startoff != bno ||
1718 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
1719 bno + count) {
1720 if (mapp != &map)
1721 kmem_free(mapp, sizeof(*mapp) * count);
1722 return XFS_ERROR(ENOSPC);
1723 }
1724 if (mapp != &map)
1725 kmem_free(mapp, sizeof(*mapp) * count);
1726 *new_blkno = (xfs_dablk_t)bno;
1727 /*
1728 * For version 1 directories, adjust the file size if it changed.
1729 */
1730 if (w == XFS_DATA_FORK && XFS_DIR_IS_V1(mp)) {
1731 ASSERT(mapi == 1);
1732 if ((error = xfs_bmap_last_offset(tp, dp, &bno, w)))
1733 return error;
1734 size = XFS_FSB_TO_B(mp, bno);
1735 if (size != dp->i_d.di_size) {
1736 dp->i_d.di_size = size;
1737 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1738 }
1739 }
1740 return 0;
1741}
1742
1743/*
1744 * Ick. We need to always be able to remove a btree block, even
1745 * if there's no space reservation because the filesystem is full.
1746 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
1747 * It swaps the target block with the last block in the file. The
1748 * last block in the file can always be removed since it can't cause
1749 * a bmap btree split to do that.
1750 */
1751STATIC int
1752xfs_da_swap_lastblock(xfs_da_args_t *args, xfs_dablk_t *dead_blknop,
1753 xfs_dabuf_t **dead_bufp)
1754{
1755 xfs_dablk_t dead_blkno, last_blkno, sib_blkno, par_blkno;
1756 xfs_dabuf_t *dead_buf, *last_buf, *sib_buf, *par_buf;
1757 xfs_fileoff_t lastoff;
1758 xfs_inode_t *ip;
1759 xfs_trans_t *tp;
1760 xfs_mount_t *mp;
1761 int error, w, entno, level, dead_level;
1762 xfs_da_blkinfo_t *dead_info, *sib_info;
1763 xfs_da_intnode_t *par_node, *dead_node;
1764 xfs_dir_leafblock_t *dead_leaf;
1765 xfs_dir2_leaf_t *dead_leaf2;
1766 xfs_dahash_t dead_hash;
1767
1768 dead_buf = *dead_bufp;
1769 dead_blkno = *dead_blknop;
1770 tp = args->trans;
1771 ip = args->dp;
1772 w = args->whichfork;
1773 ASSERT(w == XFS_DATA_FORK);
1774 mp = ip->i_mount;
1775 if (XFS_DIR_IS_V2(mp)) {
1776 lastoff = mp->m_dirfreeblk;
1777 error = xfs_bmap_last_before(tp, ip, &lastoff, w);
1778 } else
1779 error = xfs_bmap_last_offset(tp, ip, &lastoff, w);
1780 if (error)
1781 return error;
1782 if (unlikely(lastoff == 0)) {
1783 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
1784 mp);
1785 return XFS_ERROR(EFSCORRUPTED);
1786 }
1787 /*
1788 * Read the last block in the btree space.
1789 */
1790 last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
1791 if ((error = xfs_da_read_buf(tp, ip, last_blkno, -1, &last_buf, w)))
1792 return error;
1793 /*
1794 * Copy the last block into the dead buffer and log it.
1795 */
1796 memcpy(dead_buf->data, last_buf->data, mp->m_dirblksize);
1797 xfs_da_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
1798 dead_info = dead_buf->data;
1799 /*
1800 * Get values from the moved block.
1801 */
1802 if (INT_GET(dead_info->magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC) {
1803 ASSERT(XFS_DIR_IS_V1(mp));
1804 dead_leaf = (xfs_dir_leafblock_t *)dead_info;
1805 dead_level = 0;
1806 dead_hash =
1807 INT_GET(dead_leaf->entries[INT_GET(dead_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1808 } else if (INT_GET(dead_info->magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC) {
1809 ASSERT(XFS_DIR_IS_V2(mp));
1810 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
1811 dead_level = 0;
1812 dead_hash = INT_GET(dead_leaf2->ents[INT_GET(dead_leaf2->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1813 } else {
1814 ASSERT(INT_GET(dead_info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
1815 dead_node = (xfs_da_intnode_t *)dead_info;
1816 dead_level = INT_GET(dead_node->hdr.level, ARCH_CONVERT);
1817 dead_hash = INT_GET(dead_node->btree[INT_GET(dead_node->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1818 }
1819 sib_buf = par_buf = NULL;
1820 /*
1821 * If the moved block has a left sibling, fix up the pointers.
1822 */
1823 if ((sib_blkno = INT_GET(dead_info->back, ARCH_CONVERT))) {
1824 if ((error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w)))
1825 goto done;
1826 sib_info = sib_buf->data;
1827 if (unlikely(
1828 INT_GET(sib_info->forw, ARCH_CONVERT) != last_blkno ||
1829 INT_GET(sib_info->magic, ARCH_CONVERT) != INT_GET(dead_info->magic, ARCH_CONVERT))) {
1830 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
1831 XFS_ERRLEVEL_LOW, mp);
1832 error = XFS_ERROR(EFSCORRUPTED);
1833 goto done;
1834 }
1835 INT_SET(sib_info->forw, ARCH_CONVERT, dead_blkno);
1836 xfs_da_log_buf(tp, sib_buf,
1837 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
1838 sizeof(sib_info->forw)));
1839 xfs_da_buf_done(sib_buf);
1840 sib_buf = NULL;
1841 }
1842 /*
1843 * If the moved block has a right sibling, fix up the pointers.
1844 */
1845 if ((sib_blkno = INT_GET(dead_info->forw, ARCH_CONVERT))) {
1846 if ((error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w)))
1847 goto done;
1848 sib_info = sib_buf->data;
1849 if (unlikely(
1850 INT_GET(sib_info->back, ARCH_CONVERT) != last_blkno
1851 || INT_GET(sib_info->magic, ARCH_CONVERT)
1852 != INT_GET(dead_info->magic, ARCH_CONVERT))) {
1853 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
1854 XFS_ERRLEVEL_LOW, mp);
1855 error = XFS_ERROR(EFSCORRUPTED);
1856 goto done;
1857 }
1858 INT_SET(sib_info->back, ARCH_CONVERT, dead_blkno);
1859 xfs_da_log_buf(tp, sib_buf,
1860 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
1861 sizeof(sib_info->back)));
1862 xfs_da_buf_done(sib_buf);
1863 sib_buf = NULL;
1864 }
1865 par_blkno = XFS_DIR_IS_V1(mp) ? 0 : mp->m_dirleafblk;
1866 level = -1;
1867 /*
1868 * Walk down the tree looking for the parent of the moved block.
1869 */
1870 for (;;) {
1871 if ((error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w)))
1872 goto done;
1873 par_node = par_buf->data;
1874 if (unlikely(
1875 INT_GET(par_node->hdr.info.magic, ARCH_CONVERT) != XFS_DA_NODE_MAGIC ||
1876 (level >= 0 && level != INT_GET(par_node->hdr.level, ARCH_CONVERT) + 1))) {
1877 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
1878 XFS_ERRLEVEL_LOW, mp);
1879 error = XFS_ERROR(EFSCORRUPTED);
1880 goto done;
1881 }
1882 level = INT_GET(par_node->hdr.level, ARCH_CONVERT);
1883 for (entno = 0;
1884 entno < INT_GET(par_node->hdr.count, ARCH_CONVERT) &&
1885 INT_GET(par_node->btree[entno].hashval, ARCH_CONVERT) < dead_hash;
1886 entno++)
1887 continue;
1888 if (unlikely(entno == INT_GET(par_node->hdr.count, ARCH_CONVERT))) {
1889 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
1890 XFS_ERRLEVEL_LOW, mp);
1891 error = XFS_ERROR(EFSCORRUPTED);
1892 goto done;
1893 }
1894 par_blkno = INT_GET(par_node->btree[entno].before, ARCH_CONVERT);
1895 if (level == dead_level + 1)
1896 break;
1897 xfs_da_brelse(tp, par_buf);
1898 par_buf = NULL;
1899 }
1900 /*
1901 * We're in the right parent block.
1902 * Look for the right entry.
1903 */
1904 for (;;) {
1905 for (;
1906 entno < INT_GET(par_node->hdr.count, ARCH_CONVERT) &&
1907 INT_GET(par_node->btree[entno].before, ARCH_CONVERT) != last_blkno;
1908 entno++)
1909 continue;
1910 if (entno < INT_GET(par_node->hdr.count, ARCH_CONVERT))
1911 break;
1912 par_blkno = INT_GET(par_node->hdr.info.forw, ARCH_CONVERT);
1913 xfs_da_brelse(tp, par_buf);
1914 par_buf = NULL;
1915 if (unlikely(par_blkno == 0)) {
1916 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
1917 XFS_ERRLEVEL_LOW, mp);
1918 error = XFS_ERROR(EFSCORRUPTED);
1919 goto done;
1920 }
1921 if ((error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w)))
1922 goto done;
1923 par_node = par_buf->data;
1924 if (unlikely(
1925 INT_GET(par_node->hdr.level, ARCH_CONVERT) != level ||
1926 INT_GET(par_node->hdr.info.magic, ARCH_CONVERT) != XFS_DA_NODE_MAGIC)) {
1927 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
1928 XFS_ERRLEVEL_LOW, mp);
1929 error = XFS_ERROR(EFSCORRUPTED);
1930 goto done;
1931 }
1932 entno = 0;
1933 }
1934 /*
1935 * Update the parent entry pointing to the moved block.
1936 */
1937 INT_SET(par_node->btree[entno].before, ARCH_CONVERT, dead_blkno);
1938 xfs_da_log_buf(tp, par_buf,
1939 XFS_DA_LOGRANGE(par_node, &par_node->btree[entno].before,
1940 sizeof(par_node->btree[entno].before)));
1941 xfs_da_buf_done(par_buf);
1942 xfs_da_buf_done(dead_buf);
1943 *dead_blknop = last_blkno;
1944 *dead_bufp = last_buf;
1945 return 0;
1946done:
1947 if (par_buf)
1948 xfs_da_brelse(tp, par_buf);
1949 if (sib_buf)
1950 xfs_da_brelse(tp, sib_buf);
1951 xfs_da_brelse(tp, last_buf);
1952 return error;
1953}
1954
1955/*
1956 * Remove a btree block from a directory or attribute.
1957 */
1958int
1959xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
1960 xfs_dabuf_t *dead_buf)
1961{
1962 xfs_inode_t *dp;
1963 int done, error, w, count;
1964 xfs_fileoff_t bno;
1965 xfs_fsize_t size;
1966 xfs_trans_t *tp;
1967 xfs_mount_t *mp;
1968
1969 dp = args->dp;
1970 w = args->whichfork;
1971 tp = args->trans;
1972 mp = dp->i_mount;
1973 if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
1974 count = mp->m_dirblkfsbs;
1975 else
1976 count = 1;
1977 for (;;) {
1978 /*
1979 * Remove extents. If we get ENOSPC for a dir we have to move
1980 * the last block to the place we want to kill.
1981 */
1982 if ((error = xfs_bunmapi(tp, dp, dead_blkno, count,
1983 XFS_BMAPI_AFLAG(w)|XFS_BMAPI_METADATA,
1984 0, args->firstblock, args->flist,
1985 &done)) == ENOSPC) {
1986 if (w != XFS_DATA_FORK)
1987 goto done;
1988 if ((error = xfs_da_swap_lastblock(args, &dead_blkno,
1989 &dead_buf)))
1990 goto done;
1991 } else if (error)
1992 goto done;
1993 else
1994 break;
1995 }
1996 ASSERT(done);
1997 xfs_da_binval(tp, dead_buf);
1998 /*
1999 * Adjust the directory size for version 1.
2000 */
2001 if (w == XFS_DATA_FORK && XFS_DIR_IS_V1(mp)) {
2002 if ((error = xfs_bmap_last_offset(tp, dp, &bno, w)))
2003 return error;
2004 size = XFS_FSB_TO_B(dp->i_mount, bno);
2005 if (size != dp->i_d.di_size) {
2006 dp->i_d.di_size = size;
2007 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2008 }
2009 }
2010 return 0;
2011done:
2012 xfs_da_binval(tp, dead_buf);
2013 return error;
2014}
2015
2016/*
2017 * See if the mapping(s) for this btree block are valid, i.e.
2018 * don't contain holes, are logically contiguous, and cover the whole range.
2019 */
2020STATIC int
2021xfs_da_map_covers_blocks(
2022 int nmap,
2023 xfs_bmbt_irec_t *mapp,
2024 xfs_dablk_t bno,
2025 int count)
2026{
2027 int i;
2028 xfs_fileoff_t off;
2029
2030 for (i = 0, off = bno; i < nmap; i++) {
2031 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
2032 mapp[i].br_startblock == DELAYSTARTBLOCK) {
2033 return 0;
2034 }
2035 if (off != mapp[i].br_startoff) {
2036 return 0;
2037 }
2038 off += mapp[i].br_blockcount;
2039 }
2040 return off == bno + count;
2041}
2042
2043/*
2044 * Make a dabuf.
2045 * Used for get_buf, read_buf, read_bufr, and reada_buf.
2046 */
2047STATIC int
2048xfs_da_do_buf(
2049 xfs_trans_t *trans,
2050 xfs_inode_t *dp,
2051 xfs_dablk_t bno,
2052 xfs_daddr_t *mappedbnop,
2053 xfs_dabuf_t **bpp,
2054 int whichfork,
2055 int caller,
2056 inst_t *ra)
2057{
2058 xfs_buf_t *bp = NULL;
2059 xfs_buf_t **bplist;
2060 int error=0;
2061 int i;
2062 xfs_bmbt_irec_t map;
2063 xfs_bmbt_irec_t *mapp;
2064 xfs_daddr_t mappedbno;
2065 xfs_mount_t *mp;
2066 int nbplist=0;
2067 int nfsb;
2068 int nmap;
2069 xfs_dabuf_t *rbp;
2070
2071 mp = dp->i_mount;
2072 if (whichfork == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
2073 nfsb = mp->m_dirblkfsbs;
2074 else
2075 nfsb = 1;
2076 mappedbno = *mappedbnop;
2077 /*
2078 * Caller doesn't have a mapping. -2 means don't complain
2079 * if we land in a hole.
2080 */
2081 if (mappedbno == -1 || mappedbno == -2) {
2082 /*
2083 * Optimize the one-block case.
2084 */
2085 if (nfsb == 1) {
2086 xfs_fsblock_t fsb;
2087
2088 if ((error =
2089 xfs_bmapi_single(trans, dp, whichfork, &fsb,
2090 (xfs_fileoff_t)bno))) {
2091 return error;
2092 }
2093 mapp = &map;
2094 if (fsb == NULLFSBLOCK) {
2095 nmap = 0;
2096 } else {
2097 map.br_startblock = fsb;
2098 map.br_startoff = (xfs_fileoff_t)bno;
2099 map.br_blockcount = 1;
2100 nmap = 1;
2101 }
2102 } else {
2103 mapp = kmem_alloc(sizeof(*mapp) * nfsb, KM_SLEEP);
2104 nmap = nfsb;
2105 if ((error = xfs_bmapi(trans, dp, (xfs_fileoff_t)bno,
2106 nfsb,
2107 XFS_BMAPI_METADATA |
2108 XFS_BMAPI_AFLAG(whichfork),
2109 NULL, 0, mapp, &nmap, NULL)))
2110 goto exit0;
2111 }
2112 } else {
2113 map.br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2114 map.br_startoff = (xfs_fileoff_t)bno;
2115 map.br_blockcount = nfsb;
2116 mapp = &map;
2117 nmap = 1;
2118 }
2119 if (!xfs_da_map_covers_blocks(nmap, mapp, bno, nfsb)) {
2120 error = mappedbno == -2 ? 0 : XFS_ERROR(EFSCORRUPTED);
2121 if (unlikely(error == EFSCORRUPTED)) {
2122 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2123 int i;
2124 cmn_err(CE_ALERT, "xfs_da_do_buf: bno %lld\n",
2125 (long long)bno);
2126 cmn_err(CE_ALERT, "dir: inode %lld\n",
2127 (long long)dp->i_ino);
2128 for (i = 0; i < nmap; i++) {
2129 cmn_err(CE_ALERT,
2130 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d\n",
2131 i,
2132 (long long)mapp[i].br_startoff,
2133 (long long)mapp[i].br_startblock,
2134 (long long)mapp[i].br_blockcount,
2135 mapp[i].br_state);
2136 }
2137 }
2138 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2139 XFS_ERRLEVEL_LOW, mp);
2140 }
2141 goto exit0;
2142 }
2143 if (caller != 3 && nmap > 1) {
2144 bplist = kmem_alloc(sizeof(*bplist) * nmap, KM_SLEEP);
2145 nbplist = 0;
2146 } else
2147 bplist = NULL;
2148 /*
2149 * Turn the mapping(s) into buffer(s).
2150 */
2151 for (i = 0; i < nmap; i++) {
2152 int nmapped;
2153
2154 mappedbno = XFS_FSB_TO_DADDR(mp, mapp[i].br_startblock);
2155 if (i == 0)
2156 *mappedbnop = mappedbno;
2157 nmapped = (int)XFS_FSB_TO_BB(mp, mapp[i].br_blockcount);
2158 switch (caller) {
2159 case 0:
2160 bp = xfs_trans_get_buf(trans, mp->m_ddev_targp,
2161 mappedbno, nmapped, 0);
2162 error = bp ? XFS_BUF_GETERROR(bp) : XFS_ERROR(EIO);
2163 break;
2164 case 1:
1da177e4 2165 case 2:
1da177e4
LT
2166 bp = NULL;
2167 error = xfs_trans_read_buf(mp, trans, mp->m_ddev_targp,
2168 mappedbno, nmapped, 0, &bp);
2169 break;
1da177e4
LT
2170 case 3:
2171 xfs_baread(mp->m_ddev_targp, mappedbno, nmapped);
2172 error = 0;
2173 bp = NULL;
2174 break;
1da177e4
LT
2175 }
2176 if (error) {
2177 if (bp)
2178 xfs_trans_brelse(trans, bp);
2179 goto exit1;
2180 }
2181 if (!bp)
2182 continue;
2183 if (caller == 1) {
2184 if (whichfork == XFS_ATTR_FORK) {
2185 XFS_BUF_SET_VTYPE_REF(bp, B_FS_ATTR_BTREE,
2186 XFS_ATTR_BTREE_REF);
2187 } else {
2188 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DIR_BTREE,
2189 XFS_DIR_BTREE_REF);
2190 }
2191 }
2192 if (bplist) {
2193 bplist[nbplist++] = bp;
2194 }
2195 }
2196 /*
2197 * Build a dabuf structure.
2198 */
2199 if (bplist) {
2200 rbp = xfs_da_buf_make(nbplist, bplist, ra);
2201 } else if (bp)
2202 rbp = xfs_da_buf_make(1, &bp, ra);
2203 else
2204 rbp = NULL;
2205 /*
2206 * For read_buf, check the magic number.
2207 */
2208 if (caller == 1) {
2209 xfs_dir2_data_t *data;
2210 xfs_dir2_free_t *free;
2211 xfs_da_blkinfo_t *info;
2212 uint magic, magic1;
2213
2214 info = rbp->data;
2215 data = rbp->data;
2216 free = rbp->data;
2217 magic = INT_GET(info->magic, ARCH_CONVERT);
2218 magic1 = INT_GET(data->hdr.magic, ARCH_CONVERT);
2219 if (unlikely(
2220 XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
2221 (magic != XFS_DIR_LEAF_MAGIC) &&
2222 (magic != XFS_ATTR_LEAF_MAGIC) &&
2223 (magic != XFS_DIR2_LEAF1_MAGIC) &&
2224 (magic != XFS_DIR2_LEAFN_MAGIC) &&
2225 (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
2226 (magic1 != XFS_DIR2_DATA_MAGIC) &&
2227 (INT_GET(free->hdr.magic, ARCH_CONVERT) != XFS_DIR2_FREE_MAGIC),
2228 mp, XFS_ERRTAG_DA_READ_BUF,
2229 XFS_RANDOM_DA_READ_BUF))) {
2230 xfs_buftrace("DA READ ERROR", rbp->bps[0]);
2231 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2232 XFS_ERRLEVEL_LOW, mp, info);
2233 error = XFS_ERROR(EFSCORRUPTED);
2234 xfs_da_brelse(trans, rbp);
2235 nbplist = 0;
2236 goto exit1;
2237 }
2238 }
2239 if (bplist) {
2240 kmem_free(bplist, sizeof(*bplist) * nmap);
2241 }
2242 if (mapp != &map) {
2243 kmem_free(mapp, sizeof(*mapp) * nfsb);
2244 }
2245 if (bpp)
2246 *bpp = rbp;
2247 return 0;
2248exit1:
2249 if (bplist) {
2250 for (i = 0; i < nbplist; i++)
2251 xfs_trans_brelse(trans, bplist[i]);
2252 kmem_free(bplist, sizeof(*bplist) * nmap);
2253 }
2254exit0:
2255 if (mapp != &map)
2256 kmem_free(mapp, sizeof(*mapp) * nfsb);
2257 if (bpp)
2258 *bpp = NULL;
2259 return error;
2260}
2261
2262/*
2263 * Get a buffer for the dir/attr block.
2264 */
2265int
2266xfs_da_get_buf(
2267 xfs_trans_t *trans,
2268 xfs_inode_t *dp,
2269 xfs_dablk_t bno,
2270 xfs_daddr_t mappedbno,
2271 xfs_dabuf_t **bpp,
2272 int whichfork)
2273{
2274 return xfs_da_do_buf(trans, dp, bno, &mappedbno, bpp, whichfork, 0,
2275 (inst_t *)__return_address);
2276}
2277
2278/*
2279 * Get a buffer for the dir/attr block, fill in the contents.
2280 */
2281int
2282xfs_da_read_buf(
2283 xfs_trans_t *trans,
2284 xfs_inode_t *dp,
2285 xfs_dablk_t bno,
2286 xfs_daddr_t mappedbno,
2287 xfs_dabuf_t **bpp,
2288 int whichfork)
2289{
2290 return xfs_da_do_buf(trans, dp, bno, &mappedbno, bpp, whichfork, 1,
2291 (inst_t *)__return_address);
2292}
2293
2294/*
2295 * Readahead the dir/attr block.
2296 */
2297xfs_daddr_t
2298xfs_da_reada_buf(
2299 xfs_trans_t *trans,
2300 xfs_inode_t *dp,
2301 xfs_dablk_t bno,
2302 int whichfork)
2303{
2304 xfs_daddr_t rval;
2305
2306 rval = -1;
2307 if (xfs_da_do_buf(trans, dp, bno, &rval, NULL, whichfork, 3,
2308 (inst_t *)__return_address))
2309 return -1;
2310 else
2311 return rval;
2312}
2313
2314/*
2315 * Calculate the number of bits needed to hold i different values.
2316 */
2317uint
2318xfs_da_log2_roundup(uint i)
2319{
2320 uint rval;
2321
2322 for (rval = 0; rval < NBBY * sizeof(i); rval++) {
2323 if ((1 << rval) >= i)
2324 break;
2325 }
2326 return(rval);
2327}
2328
2329kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
2330kmem_zone_t *xfs_dabuf_zone; /* dabuf zone */
2331
2332/*
2333 * Allocate a dir-state structure.
2334 * We don't put them on the stack since they're large.
2335 */
2336xfs_da_state_t *
2337xfs_da_state_alloc(void)
2338{
2339 return kmem_zone_zalloc(xfs_da_state_zone, KM_SLEEP);
2340}
2341
2342/*
2343 * Kill the altpath contents of a da-state structure.
2344 */
ba0f32d4 2345STATIC void
1da177e4
LT
2346xfs_da_state_kill_altpath(xfs_da_state_t *state)
2347{
2348 int i;
2349
2350 for (i = 0; i < state->altpath.active; i++) {
2351 if (state->altpath.blk[i].bp) {
2352 if (state->altpath.blk[i].bp != state->path.blk[i].bp)
2353 xfs_da_buf_done(state->altpath.blk[i].bp);
2354 state->altpath.blk[i].bp = NULL;
2355 }
2356 }
2357 state->altpath.active = 0;
2358}
2359
2360/*
2361 * Free a da-state structure.
2362 */
2363void
2364xfs_da_state_free(xfs_da_state_t *state)
2365{
2366 int i;
2367
2368 xfs_da_state_kill_altpath(state);
2369 for (i = 0; i < state->path.active; i++) {
2370 if (state->path.blk[i].bp)
2371 xfs_da_buf_done(state->path.blk[i].bp);
2372 }
2373 if (state->extravalid && state->extrablk.bp)
2374 xfs_da_buf_done(state->extrablk.bp);
2375#ifdef DEBUG
2376 memset((char *)state, 0, sizeof(*state));
2377#endif /* DEBUG */
2378 kmem_zone_free(xfs_da_state_zone, state);
2379}
2380
2381#ifdef XFS_DABUF_DEBUG
2382xfs_dabuf_t *xfs_dabuf_global_list;
2383lock_t xfs_dabuf_global_lock;
2384#endif
2385
2386/*
2387 * Create a dabuf.
2388 */
2389/* ARGSUSED */
2390STATIC xfs_dabuf_t *
2391xfs_da_buf_make(int nbuf, xfs_buf_t **bps, inst_t *ra)
2392{
2393 xfs_buf_t *bp;
2394 xfs_dabuf_t *dabuf;
2395 int i;
2396 int off;
2397
2398 if (nbuf == 1)
2399 dabuf = kmem_zone_alloc(xfs_dabuf_zone, KM_SLEEP);
2400 else
2401 dabuf = kmem_alloc(XFS_DA_BUF_SIZE(nbuf), KM_SLEEP);
2402 dabuf->dirty = 0;
2403#ifdef XFS_DABUF_DEBUG
2404 dabuf->ra = ra;
2405 dabuf->target = XFS_BUF_TARGET(bps[0]);
2406 dabuf->blkno = XFS_BUF_ADDR(bps[0]);
2407#endif
2408 if (nbuf == 1) {
2409 dabuf->nbuf = 1;
2410 bp = bps[0];
2411 dabuf->bbcount = (short)BTOBB(XFS_BUF_COUNT(bp));
2412 dabuf->data = XFS_BUF_PTR(bp);
2413 dabuf->bps[0] = bp;
2414 } else {
2415 dabuf->nbuf = nbuf;
2416 for (i = 0, dabuf->bbcount = 0; i < nbuf; i++) {
2417 dabuf->bps[i] = bp = bps[i];
2418 dabuf->bbcount += BTOBB(XFS_BUF_COUNT(bp));
2419 }
2420 dabuf->data = kmem_alloc(BBTOB(dabuf->bbcount), KM_SLEEP);
2421 for (i = off = 0; i < nbuf; i++, off += XFS_BUF_COUNT(bp)) {
2422 bp = bps[i];
2423 memcpy((char *)dabuf->data + off, XFS_BUF_PTR(bp),
2424 XFS_BUF_COUNT(bp));
2425 }
2426 }
2427#ifdef XFS_DABUF_DEBUG
2428 {
2429 SPLDECL(s);
2430 xfs_dabuf_t *p;
2431
2432 s = mutex_spinlock(&xfs_dabuf_global_lock);
2433 for (p = xfs_dabuf_global_list; p; p = p->next) {
2434 ASSERT(p->blkno != dabuf->blkno ||
2435 p->target != dabuf->target);
2436 }
2437 dabuf->prev = NULL;
2438 if (xfs_dabuf_global_list)
2439 xfs_dabuf_global_list->prev = dabuf;
2440 dabuf->next = xfs_dabuf_global_list;
2441 xfs_dabuf_global_list = dabuf;
2442 mutex_spinunlock(&xfs_dabuf_global_lock, s);
2443 }
2444#endif
2445 return dabuf;
2446}
2447
2448/*
2449 * Un-dirty a dabuf.
2450 */
2451STATIC void
2452xfs_da_buf_clean(xfs_dabuf_t *dabuf)
2453{
2454 xfs_buf_t *bp;
2455 int i;
2456 int off;
2457
2458 if (dabuf->dirty) {
2459 ASSERT(dabuf->nbuf > 1);
2460 dabuf->dirty = 0;
2461 for (i = off = 0; i < dabuf->nbuf;
2462 i++, off += XFS_BUF_COUNT(bp)) {
2463 bp = dabuf->bps[i];
2464 memcpy(XFS_BUF_PTR(bp), (char *)dabuf->data + off,
2465 XFS_BUF_COUNT(bp));
2466 }
2467 }
2468}
2469
2470/*
2471 * Release a dabuf.
2472 */
2473void
2474xfs_da_buf_done(xfs_dabuf_t *dabuf)
2475{
2476 ASSERT(dabuf);
2477 ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
2478 if (dabuf->dirty)
2479 xfs_da_buf_clean(dabuf);
2480 if (dabuf->nbuf > 1)
2481 kmem_free(dabuf->data, BBTOB(dabuf->bbcount));
2482#ifdef XFS_DABUF_DEBUG
2483 {
2484 SPLDECL(s);
2485
2486 s = mutex_spinlock(&xfs_dabuf_global_lock);
2487 if (dabuf->prev)
2488 dabuf->prev->next = dabuf->next;
2489 else
2490 xfs_dabuf_global_list = dabuf->next;
2491 if (dabuf->next)
2492 dabuf->next->prev = dabuf->prev;
2493 mutex_spinunlock(&xfs_dabuf_global_lock, s);
2494 }
2495 memset(dabuf, 0, XFS_DA_BUF_SIZE(dabuf->nbuf));
2496#endif
2497 if (dabuf->nbuf == 1)
2498 kmem_zone_free(xfs_dabuf_zone, dabuf);
2499 else
2500 kmem_free(dabuf, XFS_DA_BUF_SIZE(dabuf->nbuf));
2501}
2502
2503/*
2504 * Log transaction from a dabuf.
2505 */
2506void
2507xfs_da_log_buf(xfs_trans_t *tp, xfs_dabuf_t *dabuf, uint first, uint last)
2508{
2509 xfs_buf_t *bp;
2510 uint f;
2511 int i;
2512 uint l;
2513 int off;
2514
2515 ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
2516 if (dabuf->nbuf == 1) {
2517 ASSERT(dabuf->data == (void *)XFS_BUF_PTR(dabuf->bps[0]));
2518 xfs_trans_log_buf(tp, dabuf->bps[0], first, last);
2519 return;
2520 }
2521 dabuf->dirty = 1;
2522 ASSERT(first <= last);
2523 for (i = off = 0; i < dabuf->nbuf; i++, off += XFS_BUF_COUNT(bp)) {
2524 bp = dabuf->bps[i];
2525 f = off;
2526 l = f + XFS_BUF_COUNT(bp) - 1;
2527 if (f < first)
2528 f = first;
2529 if (l > last)
2530 l = last;
2531 if (f <= l)
2532 xfs_trans_log_buf(tp, bp, f - off, l - off);
2533 /*
2534 * B_DONE is set by xfs_trans_log buf.
2535 * If we don't set it on a new buffer (get not read)
2536 * then if we don't put anything in the buffer it won't
2537 * be set, and at commit it it released into the cache,
2538 * and then a read will fail.
2539 */
2540 else if (!(XFS_BUF_ISDONE(bp)))
2541 XFS_BUF_DONE(bp);
2542 }
2543 ASSERT(last < off);
2544}
2545
2546/*
2547 * Release dabuf from a transaction.
2548 * Have to free up the dabuf before the buffers are released,
2549 * since the synchronization on the dabuf is really the lock on the buffer.
2550 */
2551void
2552xfs_da_brelse(xfs_trans_t *tp, xfs_dabuf_t *dabuf)
2553{
2554 xfs_buf_t *bp;
2555 xfs_buf_t **bplist;
2556 int i;
2557 int nbuf;
2558
2559 ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
2560 if ((nbuf = dabuf->nbuf) == 1) {
2561 bplist = &bp;
2562 bp = dabuf->bps[0];
2563 } else {
2564 bplist = kmem_alloc(nbuf * sizeof(*bplist), KM_SLEEP);
2565 memcpy(bplist, dabuf->bps, nbuf * sizeof(*bplist));
2566 }
2567 xfs_da_buf_done(dabuf);
2568 for (i = 0; i < nbuf; i++)
2569 xfs_trans_brelse(tp, bplist[i]);
2570 if (bplist != &bp)
2571 kmem_free(bplist, nbuf * sizeof(*bplist));
2572}
2573
2574/*
2575 * Invalidate dabuf from a transaction.
2576 */
2577void
2578xfs_da_binval(xfs_trans_t *tp, xfs_dabuf_t *dabuf)
2579{
2580 xfs_buf_t *bp;
2581 xfs_buf_t **bplist;
2582 int i;
2583 int nbuf;
2584
2585 ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
2586 if ((nbuf = dabuf->nbuf) == 1) {
2587 bplist = &bp;
2588 bp = dabuf->bps[0];
2589 } else {
2590 bplist = kmem_alloc(nbuf * sizeof(*bplist), KM_SLEEP);
2591 memcpy(bplist, dabuf->bps, nbuf * sizeof(*bplist));
2592 }
2593 xfs_da_buf_done(dabuf);
2594 for (i = 0; i < nbuf; i++)
2595 xfs_trans_binval(tp, bplist[i]);
2596 if (bplist != &bp)
2597 kmem_free(bplist, nbuf * sizeof(*bplist));
2598}
2599
2600/*
2601 * Get the first daddr from a dabuf.
2602 */
2603xfs_daddr_t
2604xfs_da_blkno(xfs_dabuf_t *dabuf)
2605{
2606 ASSERT(dabuf->nbuf);
2607 ASSERT(dabuf->data);
2608 return XFS_BUF_ADDR(dabuf->bps[0]);
2609}