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