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