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