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