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