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