xfs_da_btree.c 69.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3
 * Copyright (c) 2013 Red Hat, Inc.
4
 * All Rights Reserved.
Linus Torvalds's avatar
Linus Torvalds committed
5
 *
6 7
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
Linus Torvalds's avatar
Linus Torvalds committed
8 9
 * published by the Free Software Foundation.
 *
10 11 12 13
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Linus Torvalds's avatar
Linus Torvalds committed
14
 *
15 16 17
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Linus Torvalds's avatar
Linus Torvalds committed
18 19
 */
#include "xfs.h"
20
#include "xfs_fs.h"
21
#include "xfs_shared.h"
22 23 24
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
25
#include "xfs_bit.h"
Linus Torvalds's avatar
Linus Torvalds committed
26
#include "xfs_mount.h"
27
#include "xfs_da_format.h"
28
#include "xfs_da_btree.h"
29
#include "xfs_dir2.h"
30
#include "xfs_dir2_priv.h"
Linus Torvalds's avatar
Linus Torvalds committed
31
#include "xfs_inode.h"
32
#include "xfs_trans.h"
33 34
#include "xfs_inode_item.h"
#include "xfs_alloc.h"
Linus Torvalds's avatar
Linus Torvalds committed
35 36 37 38
#include "xfs_bmap.h"
#include "xfs_attr.h"
#include "xfs_attr_leaf.h"
#include "xfs_error.h"
39
#include "xfs_trace.h"
40 41
#include "xfs_cksum.h"
#include "xfs_buf_item.h"
42
#include "xfs_log.h"
Linus Torvalds's avatar
Linus Torvalds committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56

/*
 * xfs_da_btree.c
 *
 * Routines to implement directories as Btrees of hashed names.
 */

/*========================================================================
 * Function prototypes for the kernel.
 *========================================================================*/

/*
 * Routines used for growing the Btree.
 */
57
STATIC int xfs_da3_root_split(xfs_da_state_t *state,
Linus Torvalds's avatar
Linus Torvalds committed
58 59
					    xfs_da_state_blk_t *existing_root,
					    xfs_da_state_blk_t *new_child);
60
STATIC int xfs_da3_node_split(xfs_da_state_t *state,
Linus Torvalds's avatar
Linus Torvalds committed
61 62 63 64 65
					    xfs_da_state_blk_t *existing_blk,
					    xfs_da_state_blk_t *split_blk,
					    xfs_da_state_blk_t *blk_to_add,
					    int treelevel,
					    int *result);
66
STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
Linus Torvalds's avatar
Linus Torvalds committed
67 68
					 xfs_da_state_blk_t *node_blk_1,
					 xfs_da_state_blk_t *node_blk_2);
69
STATIC void xfs_da3_node_add(xfs_da_state_t *state,
Linus Torvalds's avatar
Linus Torvalds committed
70 71 72 73 74 75
				   xfs_da_state_blk_t *old_node_blk,
				   xfs_da_state_blk_t *new_node_blk);

/*
 * Routines used for shrinking the Btree.
 */
76
STATIC int xfs_da3_root_join(xfs_da_state_t *state,
Linus Torvalds's avatar
Linus Torvalds committed
77
					   xfs_da_state_blk_t *root_blk);
78 79
STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
Linus Torvalds's avatar
Linus Torvalds committed
80
					      xfs_da_state_blk_t *drop_blk);
81
STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84 85 86 87
					 xfs_da_state_blk_t *src_node_blk,
					 xfs_da_state_blk_t *dst_node_blk);

/*
 * Utility routines.
 */
88
STATIC int	xfs_da3_blk_unlink(xfs_da_state_t *state,
89 90
				  xfs_da_state_blk_t *drop_blk,
				  xfs_da_state_blk_t *save_blk);
Linus Torvalds's avatar
Linus Torvalds committed
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

kmem_zone_t *xfs_da_state_zone;	/* anchor for state struct zone */

/*
 * Allocate a dir-state structure.
 * We don't put them on the stack since they're large.
 */
xfs_da_state_t *
xfs_da_state_alloc(void)
{
	return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
}

/*
 * Kill the altpath contents of a da-state structure.
 */
STATIC void
xfs_da_state_kill_altpath(xfs_da_state_t *state)
{
	int	i;

	for (i = 0; i < state->altpath.active; i++)
		state->altpath.blk[i].bp = NULL;
	state->altpath.active = 0;
}

/*
 * Free a da-state structure.
 */
void
xfs_da_state_free(xfs_da_state_t *state)
{
	xfs_da_state_kill_altpath(state);
#ifdef DEBUG
	memset((char *)state, 0, sizeof(*state));
#endif /* DEBUG */
	kmem_zone_free(xfs_da_state_zone, state);
}

static bool
xfs_da3_node_verify(
133 134 135
	struct xfs_buf		*bp)
{
	struct xfs_mount	*mp = bp->b_target->bt_mount;
136 137
	struct xfs_da_intnode	*hdr = bp->b_addr;
	struct xfs_da3_icnode_hdr ichdr;
138 139 140
	const struct xfs_dir_ops *ops;

	ops = xfs_dir_get_ops(mp, NULL);
141

142
	ops->node_hdr_from_disk(&ichdr, hdr);
143 144 145 146 147 148 149

	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		struct xfs_da3_node_hdr *hdr3 = bp->b_addr;

		if (ichdr.magic != XFS_DA3_NODE_MAGIC)
			return false;

150
		if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
151 152 153
			return false;
		if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
			return false;
154 155
		if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
			return false;
156 157 158
	} else {
		if (ichdr.magic != XFS_DA_NODE_MAGIC)
			return false;
159
	}
160 161 162 163 164 165 166 167 168 169 170
	if (ichdr.level == 0)
		return false;
	if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
		return false;
	if (ichdr.count == 0)
		return false;

	/*
	 * we don't know if the node is for and attribute or directory tree,
	 * so only fail if the count is outside both bounds
	 */
171 172
	if (ichdr.count > mp->m_dir_geo->node_ents &&
	    ichdr.count > mp->m_attr_geo->node_ents)
173 174 175
		return false;

	/* XXX: hash order check? */
176

177
	return true;
178 179 180
}

static void
181
xfs_da3_node_write_verify(
182 183
	struct xfs_buf	*bp)
{
184 185 186 187 188
	struct xfs_mount	*mp = bp->b_target->bt_mount;
	struct xfs_buf_log_item	*bip = bp->b_fspriv;
	struct xfs_da3_node_hdr *hdr3 = bp->b_addr;

	if (!xfs_da3_node_verify(bp)) {
189
		xfs_buf_ioerror(bp, -EFSCORRUPTED);
190
		xfs_verifier_error(bp);
191 192 193 194 195 196 197 198 199
		return;
	}

	if (!xfs_sb_version_hascrc(&mp->m_sb))
		return;

	if (bip)
		hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);

200
	xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
201 202
}

203 204 205 206 207 208
/*
 * leaf/node format detection on trees is sketchy, so a node read can be done on
 * leaf level blocks when detection identifies the tree as a node format tree
 * incorrectly. In this case, we need to swap the verifier to match the correct
 * format of the block being read.
 */
209
static void
210
xfs_da3_node_read_verify(
211 212 213 214 215
	struct xfs_buf		*bp)
{
	struct xfs_da_blkinfo	*info = bp->b_addr;

	switch (be16_to_cpu(info->magic)) {
216
		case XFS_DA3_NODE_MAGIC:
217
			if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
218
				xfs_buf_ioerror(bp, -EFSBADCRC);
219
				break;
220
			}
221
			/* fall through */
222
		case XFS_DA_NODE_MAGIC:
223
			if (!xfs_da3_node_verify(bp)) {
224
				xfs_buf_ioerror(bp, -EFSCORRUPTED);
225
				break;
226
			}
227
			return;
228
		case XFS_ATTR_LEAF_MAGIC:
229
		case XFS_ATTR3_LEAF_MAGIC:
230
			bp->b_ops = &xfs_attr3_leaf_buf_ops;
231
			bp->b_ops->verify_read(bp);
232 233
			return;
		case XFS_DIR2_LEAFN_MAGIC:
234 235
		case XFS_DIR3_LEAFN_MAGIC:
			bp->b_ops = &xfs_dir3_leafn_buf_ops;
236
			bp->b_ops->verify_read(bp);
237 238
			return;
		default:
239
			xfs_buf_ioerror(bp, -EFSCORRUPTED);
240 241
			break;
	}
242 243

	/* corrupt block */
244
	xfs_verifier_error(bp);
245 246
}

247
const struct xfs_buf_ops xfs_da3_node_buf_ops = {
248
	.name = "xfs_da3_node",
249 250
	.verify_read = xfs_da3_node_read_verify,
	.verify_write = xfs_da3_node_write_verify,
251 252
};

253
int
254
xfs_da3_node_read(
255 256 257 258 259 260 261
	struct xfs_trans	*tp,
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mappedbno,
	struct xfs_buf		**bpp,
	int			which_fork)
{
262 263 264
	int			err;

	err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
265
					which_fork, &xfs_da3_node_buf_ops);
266 267 268 269 270 271
	if (!err && tp) {
		struct xfs_da_blkinfo	*info = (*bpp)->b_addr;
		int			type;

		switch (be16_to_cpu(info->magic)) {
		case XFS_DA_NODE_MAGIC:
272
		case XFS_DA3_NODE_MAGIC:
273
			type = XFS_BLFT_DA_NODE_BUF;
274 275 276
			break;
		case XFS_ATTR_LEAF_MAGIC:
		case XFS_ATTR3_LEAF_MAGIC:
277
			type = XFS_BLFT_ATTR_LEAF_BUF;
278 279 280
			break;
		case XFS_DIR2_LEAFN_MAGIC:
		case XFS_DIR3_LEAFN_MAGIC:
281
			type = XFS_BLFT_DIR_LEAFN_BUF;
282 283 284 285 286 287 288 289 290
			break;
		default:
			type = 0;
			ASSERT(0);
			break;
		}
		xfs_trans_buf_set_type(tp, *bpp, type);
	}
	return err;
291 292
}

Linus Torvalds's avatar
Linus Torvalds committed
293 294 295 296 297 298 299 300
/*========================================================================
 * Routines used for growing the Btree.
 *========================================================================*/

/*
 * Create the initial contents of an intermediate node.
 */
int
301 302 303 304 305 306
xfs_da3_node_create(
	struct xfs_da_args	*args,
	xfs_dablk_t		blkno,
	int			level,
	struct xfs_buf		**bpp,
	int			whichfork)
Linus Torvalds's avatar
Linus Torvalds committed
307
{
308 309 310 311 312 313
	struct xfs_da_intnode	*node;
	struct xfs_trans	*tp = args->trans;
	struct xfs_mount	*mp = tp->t_mountp;
	struct xfs_da3_icnode_hdr ichdr = {0};
	struct xfs_buf		*bp;
	int			error;
314
	struct xfs_inode	*dp = args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
315

316
	trace_xfs_da_node_create(args);
317
	ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
318

319
	error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
320
	if (error)
Eric Sandeen's avatar
Eric Sandeen committed
321
		return error;
322
	bp->b_ops = &xfs_da3_node_buf_ops;
323
	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
324
	node = bp->b_addr;
Linus Torvalds's avatar
Linus Torvalds committed
325

326 327 328
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		struct xfs_da3_node_hdr *hdr3 = bp->b_addr;

329
		memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
330 331 332
		ichdr.magic = XFS_DA3_NODE_MAGIC;
		hdr3->info.blkno = cpu_to_be64(bp->b_bn);
		hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
333
		uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
334 335 336 337 338
	} else {
		ichdr.magic = XFS_DA_NODE_MAGIC;
	}
	ichdr.level = level;

339
	dp->d_ops->node_hdr_to_disk(node, &ichdr);
340
	xfs_trans_log_buf(tp, bp,
341
		XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds's avatar
Linus Torvalds committed
342 343

	*bpp = bp;
Eric Sandeen's avatar
Eric Sandeen committed
344
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
345 346 347 348 349 350 351
}

/*
 * Split a leaf node, rebalance, then possibly split
 * intermediate nodes, rebalance, etc.
 */
int							/* error */
352 353
xfs_da3_split(
	struct xfs_da_state	*state)
Linus Torvalds's avatar
Linus Torvalds committed
354
{
355 356 357 358 359 360
	struct xfs_da_state_blk	*oldblk;
	struct xfs_da_state_blk	*newblk;
	struct xfs_da_state_blk	*addblk;
	struct xfs_da_intnode	*node;
	struct xfs_buf		*bp;
	int			max;
361
	int			action = 0;
362 363
	int			error;
	int			i;
Linus Torvalds's avatar
Linus Torvalds committed
364

365 366
	trace_xfs_da_split(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
367 368 369 370 371 372 373 374 375
	/*
	 * Walk back up the tree splitting/inserting/adjusting as necessary.
	 * If we need to insert and there isn't room, split the node, then
	 * decide which fragment to insert the new block from below into.
	 * Note that we may split the root this way, but we need more fixup.
	 */
	max = state->path.active - 1;
	ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
	ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
376
	       state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds's avatar
Linus Torvalds committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390

	addblk = &state->path.blk[max];		/* initial dummy value */
	for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
		oldblk = &state->path.blk[i];
		newblk = &state->altpath.blk[i];

		/*
		 * If a leaf node then
		 *     Allocate a new leaf node, then rebalance across them.
		 * else if an intermediate node then
		 *     We split on the last layer, must we split the node?
		 */
		switch (oldblk->magic) {
		case XFS_ATTR_LEAF_MAGIC:
391
			error = xfs_attr3_leaf_split(state, oldblk, newblk);
392
			if ((error != 0) && (error != -ENOSPC)) {
Eric Sandeen's avatar
Eric Sandeen committed
393
				return error;	/* GROT: attr is inconsistent */
Linus Torvalds's avatar
Linus Torvalds committed
394 395 396 397 398 399 400 401 402 403 404
			}
			if (!error) {
				addblk = newblk;
				break;
			}
			/*
			 * Entry wouldn't fit, split the leaf again.
			 */
			state->extravalid = 1;
			if (state->inleaf) {
				state->extraafter = 0;	/* before newblk */
405
				trace_xfs_attr_leaf_split_before(state->args);
406
				error = xfs_attr3_leaf_split(state, oldblk,
Linus Torvalds's avatar
Linus Torvalds committed
407 408 409
							    &state->extrablk);
			} else {
				state->extraafter = 1;	/* after newblk */
410
				trace_xfs_attr_leaf_split_after(state->args);
411
				error = xfs_attr3_leaf_split(state, newblk,
Linus Torvalds's avatar
Linus Torvalds committed
412 413 414
							    &state->extrablk);
			}
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
415
				return error;	/* GROT: attr inconsistent */
Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419 420 421 422 423 424
			addblk = newblk;
			break;
		case XFS_DIR2_LEAFN_MAGIC:
			error = xfs_dir2_leafn_split(state, oldblk, newblk);
			if (error)
				return error;
			addblk = newblk;
			break;
		case XFS_DA_NODE_MAGIC:
425
			error = xfs_da3_node_split(state, oldblk, newblk, addblk,
Linus Torvalds's avatar
Linus Torvalds committed
426 427 428
							 max - i, &action);
			addblk->bp = NULL;
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
429
				return error;	/* GROT: dir is inconsistent */
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433 434 435 436 437 438 439 440 441 442
			/*
			 * Record the newly split block for the next time thru?
			 */
			if (action)
				addblk = newblk;
			else
				addblk = NULL;
			break;
		}

		/*
		 * Update the btree to show the new hashval for this child.
		 */
443
		xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds's avatar
Linus Torvalds committed
444 445
	}
	if (!addblk)
Eric Sandeen's avatar
Eric Sandeen committed
446
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
447 448 449 450 451 452

	/*
	 * Split the root node.
	 */
	ASSERT(state->path.active == 0);
	oldblk = &state->path.blk[0];
453
	error = xfs_da3_root_split(state, oldblk, addblk);
Linus Torvalds's avatar
Linus Torvalds committed
454 455
	if (error) {
		addblk->bp = NULL;
Eric Sandeen's avatar
Eric Sandeen committed
456
		return error;	/* GROT: dir is inconsistent */
Linus Torvalds's avatar
Linus Torvalds committed
457 458 459 460 461 462 463
	}

	/*
	 * Update pointers to the node which used to be block 0 and
	 * just got bumped because of the addition of a new root node.
	 * There might be three blocks involved if a double split occurred,
	 * and the original block 0 could be at any position in the list.
464 465 466 467 468
	 *
	 * Note: the magic numbers and sibling pointers are in the same
	 * physical place for both v2 and v3 headers (by design). Hence it
	 * doesn't matter which version of the xfs_da_intnode structure we use
	 * here as the result will be the same using either structure.
Linus Torvalds's avatar
Linus Torvalds committed
469
	 */
470
	node = oldblk->bp->b_addr;
Linus Torvalds's avatar
Linus Torvalds committed
471
	if (node->hdr.info.forw) {
472
		if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
Linus Torvalds's avatar
Linus Torvalds committed
473 474 475 476 477
			bp = addblk->bp;
		} else {
			ASSERT(state->extravalid);
			bp = state->extrablk.bp;
		}
478
		node = bp->b_addr;
479
		node->hdr.info.back = cpu_to_be32(oldblk->blkno);
480
		xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds's avatar
Linus Torvalds committed
481 482 483
		    XFS_DA_LOGRANGE(node, &node->hdr.info,
		    sizeof(node->hdr.info)));
	}
484
	node = oldblk->bp->b_addr;
485 486
	if (node->hdr.info.back) {
		if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
Linus Torvalds's avatar
Linus Torvalds committed
487 488 489 490 491
			bp = addblk->bp;
		} else {
			ASSERT(state->extravalid);
			bp = state->extrablk.bp;
		}
492
		node = bp->b_addr;
493
		node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
494
		xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds's avatar
Linus Torvalds committed
495 496 497 498
		    XFS_DA_LOGRANGE(node, &node->hdr.info,
		    sizeof(node->hdr.info)));
	}
	addblk->bp = NULL;
Eric Sandeen's avatar
Eric Sandeen committed
499
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
500 501 502 503 504 505 506 507
}

/*
 * Split the root.  We have to create a new root and point to the two
 * parts (the split old root) that we just created.  Copy block zero to
 * the EOF, extending the inode in process.
 */
STATIC int						/* error */
508 509 510 511
xfs_da3_root_split(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*blk1,
	struct xfs_da_state_blk	*blk2)
Linus Torvalds's avatar
Linus Torvalds committed
512
{
513 514 515 516 517 518 519 520 521 522 523 524 525
	struct xfs_da_intnode	*node;
	struct xfs_da_intnode	*oldroot;
	struct xfs_da_node_entry *btree;
	struct xfs_da3_icnode_hdr nodehdr;
	struct xfs_da_args	*args;
	struct xfs_buf		*bp;
	struct xfs_inode	*dp;
	struct xfs_trans	*tp;
	struct xfs_dir2_leaf	*leaf;
	xfs_dablk_t		blkno;
	int			level;
	int			error;
	int			size;
Linus Torvalds's avatar
Linus Torvalds committed
526

527 528
	trace_xfs_da_root_split(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
529 530 531 532 533 534 535
	/*
	 * Copy the existing (incorrect) block from the root node position
	 * to a free space somewhere.
	 */
	args = state->args;
	error = xfs_da_grow_inode(args, &blkno);
	if (error)
536 537
		return error;

Linus Torvalds's avatar
Linus Torvalds committed
538 539 540 541
	dp = args->dp;
	tp = args->trans;
	error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
	if (error)
542
		return error;
543 544
	node = bp->b_addr;
	oldroot = blk1->bp->b_addr;
545 546
	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
547
		struct xfs_da3_icnode_hdr icnodehdr;
548

549
		dp->d_ops->node_hdr_from_disk(&icnodehdr, oldroot);
550
		btree = dp->d_ops->node_tree_p(oldroot);
551 552
		size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
		level = icnodehdr.level;
553 554 555 556 557

		/*
		 * we are about to copy oldroot to bp, so set up the type
		 * of bp while we know exactly what it will be.
		 */
558
		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Linus Torvalds's avatar
Linus Torvalds committed
559
	} else {
560 561 562
		struct xfs_dir3_icleaf_hdr leafhdr;
		struct xfs_dir2_leaf_entry *ents;

Linus Torvalds's avatar
Linus Torvalds committed
563
		leaf = (xfs_dir2_leaf_t *)oldroot;
564
		dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
565
		ents = dp->d_ops->leaf_ents_p(leaf);
566 567 568 569

		ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
		       leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
		size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
570
		level = 0;
571 572 573 574 575

		/*
		 * we are about to copy oldroot to bp, so set up the type
		 * of bp while we know exactly what it will be.
		 */
576
		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Linus Torvalds's avatar
Linus Torvalds committed
577
	}
578 579 580 581 582 583 584

	/*
	 * we can copy most of the information in the node from one block to
	 * another, but for CRC enabled headers we have to make sure that the
	 * block specific identifiers are kept intact. We update the buffer
	 * directly for this.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
585
	memcpy(node, oldroot, size);
586 587 588 589 590 591
	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
		struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;

		node3->hdr.info.blkno = cpu_to_be64(bp->b_bn);
	}
592
	xfs_trans_log_buf(tp, bp, 0, size - 1);
593

594
	bp->b_ops = blk1->bp->b_ops;
595
	xfs_trans_buf_copy_type(bp, blk1->bp);
Linus Torvalds's avatar
Linus Torvalds committed
596 597 598 599 600 601
	blk1->bp = bp;
	blk1->blkno = blkno;

	/*
	 * Set up the new root node.
	 */
602
	error = xfs_da3_node_create(args,
603
		(args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
604
		level + 1, &bp, args->whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
605
	if (error)
606 607
		return error;

608
	node = bp->b_addr;
609
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
610
	btree = dp->d_ops->node_tree_p(node);
611 612 613 614 615
	btree[0].hashval = cpu_to_be32(blk1->hashval);
	btree[0].before = cpu_to_be32(blk1->blkno);
	btree[1].hashval = cpu_to_be32(blk2->hashval);
	btree[1].before = cpu_to_be32(blk2->blkno);
	nodehdr.count = 2;
616
	dp->d_ops->node_hdr_to_disk(node, &nodehdr);
Linus Torvalds's avatar
Linus Torvalds committed
617 618

#ifdef DEBUG
619 620
	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
621 622 623 624
		ASSERT(blk1->blkno >= args->geo->leafblk &&
		       blk1->blkno < args->geo->freeblk);
		ASSERT(blk2->blkno >= args->geo->leafblk &&
		       blk2->blkno < args->geo->freeblk);
Linus Torvalds's avatar
Linus Torvalds committed
625 626 627 628
	}
#endif

	/* Header is already logged by xfs_da_node_create */
629
	xfs_trans_log_buf(tp, bp,
630
		XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
Linus Torvalds's avatar
Linus Torvalds committed
631

632
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
633 634 635 636 637 638
}

/*
 * Split the node, rebalance, then add the new entry.
 */
STATIC int						/* error */
639 640 641 642 643 644 645
xfs_da3_node_split(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*oldblk,
	struct xfs_da_state_blk	*newblk,
	struct xfs_da_state_blk	*addblk,
	int			treelevel,
	int			*result)
Linus Torvalds's avatar
Linus Torvalds committed
646
{
647 648 649 650 651 652
	struct xfs_da_intnode	*node;
	struct xfs_da3_icnode_hdr nodehdr;
	xfs_dablk_t		blkno;
	int			newcount;
	int			error;
	int			useextra;
653
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
654

655 656
	trace_xfs_da_node_split(state->args);

657
	node = oldblk->bp->b_addr;
658
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Linus Torvalds's avatar
Linus Torvalds committed
659 660

	/*
661
	 * With V2 dirs the extra block is data or freespace.
Linus Torvalds's avatar
Linus Torvalds committed
662
	 */
663
	useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
Linus Torvalds's avatar
Linus Torvalds committed
664 665 666 667
	newcount = 1 + useextra;
	/*
	 * Do we have to split the node?
	 */
668
	if (nodehdr.count + newcount > state->args->geo->node_ents) {
Linus Torvalds's avatar
Linus Torvalds committed
669 670 671 672 673 674
		/*
		 * Allocate a new node, add to the doubly linked chain of
		 * nodes, then move some of our excess entries into it.
		 */
		error = xfs_da_grow_inode(state->args, &blkno);
		if (error)
Eric Sandeen's avatar
Eric Sandeen committed
675
			return error;	/* GROT: dir is inconsistent */
Linus Torvalds's avatar
Linus Torvalds committed
676

677
		error = xfs_da3_node_create(state->args, blkno, treelevel,
Linus Torvalds's avatar
Linus Torvalds committed
678 679
					   &newblk->bp, state->args->whichfork);
		if (error)
Eric Sandeen's avatar
Eric Sandeen committed
680
			return error;	/* GROT: dir is inconsistent */
Linus Torvalds's avatar
Linus Torvalds committed
681 682
		newblk->blkno = blkno;
		newblk->magic = XFS_DA_NODE_MAGIC;
683 684
		xfs_da3_node_rebalance(state, oldblk, newblk);
		error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds's avatar
Linus Torvalds committed
685
		if (error)
Eric Sandeen's avatar
Eric Sandeen committed
686
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
687 688 689 690 691 692 693 694 695
		*result = 1;
	} else {
		*result = 0;
	}

	/*
	 * Insert the new entry(s) into the correct block
	 * (updating last hashval in the process).
	 *
696
	 * xfs_da3_node_add() inserts BEFORE the given index,
Linus Torvalds's avatar
Linus Torvalds committed
697 698 699 700 701 702 703
	 * and as a result of using node_lookup_int() we always
	 * point to a valid entry (not after one), but a split
	 * operation always results in a new block whose hashvals
	 * FOLLOW the current block.
	 *
	 * If we had double-split op below us, then add the extra block too.
	 */
704
	node = oldblk->bp->b_addr;
705
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
706
	if (oldblk->index <= nodehdr.count) {
Linus Torvalds's avatar
Linus Torvalds committed
707
		oldblk->index++;
708
		xfs_da3_node_add(state, oldblk, addblk);
Linus Torvalds's avatar
Linus Torvalds committed
709 710 711
		if (useextra) {
			if (state->extraafter)
				oldblk->index++;
712
			xfs_da3_node_add(state, oldblk, &state->extrablk);
Linus Torvalds's avatar
Linus Torvalds committed
713 714 715 716
			state->extravalid = 0;
		}
	} else {
		newblk->index++;
717
		xfs_da3_node_add(state, newblk, addblk);
Linus Torvalds's avatar
Linus Torvalds committed
718 719 720
		if (useextra) {
			if (state->extraafter)
				newblk->index++;
721
			xfs_da3_node_add(state, newblk, &state->extrablk);
Linus Torvalds's avatar
Linus Torvalds committed
722 723 724 725
			state->extravalid = 0;
		}
	}

Eric Sandeen's avatar
Eric Sandeen committed
726
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
727 728 729 730 731 732 733 734 735
}

/*
 * Balance the btree elements between two intermediate nodes,
 * usually one full and one empty.
 *
 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
 */
STATIC void
736 737 738 739
xfs_da3_node_rebalance(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*blk1,
	struct xfs_da_state_blk	*blk2)
Linus Torvalds's avatar
Linus Torvalds committed
740
{
741 742 743 744 745 746 747 748 749 750 751 752 753
	struct xfs_da_intnode	*node1;
	struct xfs_da_intnode	*node2;
	struct xfs_da_intnode	*tmpnode;
	struct xfs_da_node_entry *btree1;
	struct xfs_da_node_entry *btree2;
	struct xfs_da_node_entry *btree_s;
	struct xfs_da_node_entry *btree_d;
	struct xfs_da3_icnode_hdr nodehdr1;
	struct xfs_da3_icnode_hdr nodehdr2;
	struct xfs_trans	*tp;
	int			count;
	int			tmp;
	int			swap = 0;
754
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
755

756 757
	trace_xfs_da_node_rebalance(state->args);

758 759
	node1 = blk1->bp->b_addr;
	node2 = blk2->bp->b_addr;
760 761
	dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
	dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
762 763
	btree1 = dp->d_ops->node_tree_p(node1);
	btree2 = dp->d_ops->node_tree_p(node2);
764

Linus Torvalds's avatar
Linus Torvalds committed
765 766 767 768
	/*
	 * Figure out how many entries need to move, and in which direction.
	 * Swap the nodes around if that makes it simpler.
	 */
769 770 771 772
	if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
	    ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
	     (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
			be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
Linus Torvalds's avatar
Linus Torvalds committed
773 774 775
		tmpnode = node1;
		node1 = node2;
		node2 = tmpnode;
776 777
		dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
		dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
778 779
		btree1 = dp->d_ops->node_tree_p(node1);
		btree2 = dp->d_ops->node_tree_p(node2);
780
		swap = 1;
Linus Torvalds's avatar
Linus Torvalds committed
781
	}
782 783

	count = (nodehdr1.count - nodehdr2.count) / 2;
Linus Torvalds's avatar
Linus Torvalds committed
784 785 786 787 788 789 790 791 792 793
	if (count == 0)
		return;
	tp = state->args->trans;
	/*
	 * Two cases: high-to-low and low-to-high.
	 */
	if (count > 0) {
		/*
		 * Move elements in node2 up to make a hole.
		 */
794 795
		tmp = nodehdr2.count;
		if (tmp > 0) {
Linus Torvalds's avatar
Linus Torvalds committed
796
			tmp *= (uint)sizeof(xfs_da_node_entry_t);
797 798
			btree_s = &btree2[0];
			btree_d = &btree2[count];
Linus Torvalds's avatar
Linus Torvalds committed
799 800 801 802 803 804 805
			memmove(btree_d, btree_s, tmp);
		}

		/*
		 * Move the req'd B-tree elements from high in node1 to
		 * low in node2.
		 */
806
		nodehdr2.count += count;
Linus Torvalds's avatar
Linus Torvalds committed
807
		tmp = count * (uint)sizeof(xfs_da_node_entry_t);
808 809
		btree_s = &btree1[nodehdr1.count - count];
		btree_d = &btree2[0];
Linus Torvalds's avatar
Linus Torvalds committed
810
		memcpy(btree_d, btree_s, tmp);
811
		nodehdr1.count -= count;
Linus Torvalds's avatar
Linus Torvalds committed
812 813 814 815 816 817 818
	} else {
		/*
		 * Move the req'd B-tree elements from low in node2 to
		 * high in node1.
		 */
		count = -count;
		tmp = count * (uint)sizeof(xfs_da_node_entry_t);
819 820
		btree_s = &btree2[0];
		btree_d = &btree1[nodehdr1.count];
Linus Torvalds's avatar
Linus Torvalds committed
821
		memcpy(btree_d, btree_s, tmp);
822 823
		nodehdr1.count += count;

824
		xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds's avatar
Linus Torvalds committed
825 826 827 828 829
			XFS_DA_LOGRANGE(node1, btree_d, tmp));

		/*
		 * Move elements in node2 down to fill the hole.
		 */
830
		tmp  = nodehdr2.count - count;
Linus Torvalds's avatar
Linus Torvalds committed
831
		tmp *= (uint)sizeof(xfs_da_node_entry_t);
832 833
		btree_s = &btree2[count];
		btree_d = &btree2[0];
Linus Torvalds's avatar
Linus Torvalds committed
834
		memmove(btree_d, btree_s, tmp);
835
		nodehdr2.count -= count;
Linus Torvalds's avatar
Linus Torvalds committed
836 837 838 839 840
	}

	/*
	 * Log header of node 1 and all current bits of node 2.
	 */
841
	dp->d_ops->node_hdr_to_disk(node1, &nodehdr1);
842
	xfs_trans_log_buf(tp, blk1->bp,
843
		XFS_DA_LOGRANGE(node1, &node1->hdr, dp->d_ops->node_hdr_size));
844

845
	dp->d_ops->node_hdr_to_disk(node2, &nodehdr2);
846
	xfs_trans_log_buf(tp, blk2->bp,
Linus Torvalds's avatar
Linus Torvalds committed
847
		XFS_DA_LOGRANGE(node2, &node2->hdr,
848
				dp->d_ops->node_hdr_size +
849
				(sizeof(btree2[0]) * nodehdr2.count)));
Linus Torvalds's avatar
Linus Torvalds committed
850 851 852 853 854

	/*
	 * Record the last hashval from each block for upward propagation.
	 * (note: don't use the swapped node pointers)
	 */
855 856 857
	if (swap) {
		node1 = blk1->bp->b_addr;
		node2 = blk2->bp->b_addr;
858 859
		dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
		dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
860 861
		btree1 = dp->d_ops->node_tree_p(node1);
		btree2 = dp->d_ops->node_tree_p(node2);
862 863 864
	}
	blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
	blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
865 866 867 868

	/*
	 * Adjust the expected index for insertion.
	 */
869 870 871
	if (blk1->index >= nodehdr1.count) {
		blk2->index = blk1->index - nodehdr1.count;
		blk1->index = nodehdr1.count + 1;	/* make it invalid */
Linus Torvalds's avatar
Linus Torvalds committed
872 873 874 875 876 877 878
	}
}

/*
 * Add a new entry to an intermediate node.
 */
STATIC void
879 880 881 882
xfs_da3_node_add(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*oldblk,
	struct xfs_da_state_blk	*newblk)
Linus Torvalds's avatar
Linus Torvalds committed
883
{
884 885 886 887
	struct xfs_da_intnode	*node;
	struct xfs_da3_icnode_hdr nodehdr;
	struct xfs_da_node_entry *btree;
	int			tmp;
888
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
889

890 891
	trace_xfs_da_node_add(state->args);

892
	node = oldblk->bp->b_addr;
893
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
894
	btree = dp->d_ops->node_tree_p(node);
895 896

	ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
Linus Torvalds's avatar
Linus Torvalds committed
897
	ASSERT(newblk->blkno != 0);
898
	if (state->args->whichfork == XFS_DATA_FORK)
899 900
		ASSERT(newblk->blkno >= state->args->geo->leafblk &&
		       newblk->blkno < state->args->geo->freeblk);
Linus Torvalds's avatar
Linus Torvalds committed
901 902 903 904 905

	/*
	 * We may need to make some room before we insert the new node.
	 */
	tmp = 0;
906 907 908
	if (oldblk->index < nodehdr.count) {
		tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
		memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
Linus Torvalds's avatar
Linus Torvalds committed
909
	}
910 911
	btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
	btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
912
	xfs_trans_log_buf(state->args->trans, oldblk->bp,
913 914 915 916
		XFS_DA_LOGRANGE(node, &btree[oldblk->index],
				tmp + sizeof(*btree)));

	nodehdr.count += 1;
917
	dp->d_ops->node_hdr_to_disk(node, &nodehdr);
918
	xfs_trans_log_buf(state->args->trans, oldblk->bp,
919
		XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds's avatar
Linus Torvalds committed
920 921 922 923

	/*
	 * Copy the last hash value from the oldblk to propagate upwards.
	 */
924
	oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
925 926 927 928 929 930 931 932 933 934 935
}

/*========================================================================
 * Routines used for shrinking the Btree.
 *========================================================================*/

/*
 * Deallocate an empty leaf node, remove it from its parent,
 * possibly deallocating that block, etc...
 */
int
936 937
xfs_da3_join(
	struct xfs_da_state	*state)
Linus Torvalds's avatar
Linus Torvalds committed
938
{
939 940 941 942
	struct xfs_da_state_blk	*drop_blk;
	struct xfs_da_state_blk	*save_blk;
	int			action = 0;
	int			error;
Linus Torvalds's avatar
Linus Torvalds committed
943

944 945
	trace_xfs_da_join(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
946 947 948 949
	drop_blk = &state->path.blk[ state->path.active-1 ];
	save_blk = &state->altpath.blk[ state->path.active-1 ];
	ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
	ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
950
	       drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds's avatar
Linus Torvalds committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965

	/*
	 * Walk back up the tree joining/deallocating as necessary.
	 * When we stop dropping blocks, break out.
	 */
	for (  ; state->path.active >= 2; drop_blk--, save_blk--,
		 state->path.active--) {
		/*
		 * See if we can combine the block with a neighbor.
		 *   (action == 0) => no options, just leave
		 *   (action == 1) => coalesce, then unlink
		 *   (action == 2) => block empty, unlink it
		 */
		switch (drop_blk->magic) {
		case XFS_ATTR_LEAF_MAGIC:
966
			error = xfs_attr3_leaf_toosmall(state, &action);
Linus Torvalds's avatar
Linus Torvalds committed
967
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
968
				return error;
Linus Torvalds's avatar
Linus Torvalds committed
969
			if (action == 0)
Eric Sandeen's avatar
Eric Sandeen committed
970
				return 0;
971
			xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
Linus Torvalds's avatar
Linus Torvalds committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985
			break;
		case XFS_DIR2_LEAFN_MAGIC:
			error = xfs_dir2_leafn_toosmall(state, &action);
			if (error)
				return error;
			if (action == 0)
				return 0;
			xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
			break;
		case XFS_DA_NODE_MAGIC:
			/*
			 * Remove the offending node, fixup hashvals,
			 * check for a toosmall neighbor.
			 */
986 987 988
			xfs_da3_node_remove(state, drop_blk);
			xfs_da3_fixhashpath(state, &state->path);
			error = xfs_da3_node_toosmall(state, &action);
Linus Torvalds's avatar
Linus Torvalds committed
989
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
990
				return error;
Linus Torvalds's avatar
Linus Torvalds committed
991 992
			if (action == 0)
				return 0;
993
			xfs_da3_node_unbalance(state, drop_blk, save_blk);
Linus Torvalds's avatar
Linus Torvalds committed
994 995
			break;
		}
996 997
		xfs_da3_fixhashpath(state, &state->altpath);
		error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
Linus Torvalds's avatar
Linus Torvalds committed
998 999
		xfs_da_state_kill_altpath(state);
		if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1000
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
1001 1002 1003 1004
		error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
							 drop_blk->bp);
		drop_blk->bp = NULL;
		if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1005
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
1006 1007 1008 1009 1010 1011
	}
	/*
	 * We joined all the way to the top.  If it turns out that
	 * we only have one entry in the root, make the child block
	 * the new root.
	 */
1012 1013 1014
	xfs_da3_node_remove(state, drop_blk);
	xfs_da3_fixhashpath(state, &state->path);
	error = xfs_da3_root_join(state, &state->path.blk[0]);
Eric Sandeen's avatar
Eric Sandeen committed
1015
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
1016 1017
}

1018 1019 1020 1021 1022 1023 1024 1025
#ifdef	DEBUG
static void
xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
{
	__be16	magic = blkinfo->magic;

	if (level == 1) {
		ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1026
		       magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
1027 1028
		       magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
		       magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1029 1030 1031 1032
	} else {
		ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
		       magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
	}
1033 1034 1035 1036 1037 1038 1039
	ASSERT(!blkinfo->forw);
	ASSERT(!blkinfo->back);
}
#else	/* !DEBUG */
#define	xfs_da_blkinfo_onlychild_validate(blkinfo, level)
#endif	/* !DEBUG */

Linus Torvalds's avatar
Linus Torvalds committed
1040 1041 1042 1043 1044
/*
 * We have only one entry in the root.  Copy the only remaining child of
 * the old root to block 0 as the new root node.
 */
STATIC int
1045 1046 1047
xfs_da3_root_join(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*root_blk)
Linus Torvalds's avatar
Linus Torvalds committed
1048
{
1049 1050 1051 1052 1053 1054 1055
	struct xfs_da_intnode	*oldroot;
	struct xfs_da_args	*args;
	xfs_dablk_t		child;
	struct xfs_buf		*bp;
	struct xfs_da3_icnode_hdr oldroothdr;
	struct xfs_da_node_entry *btree;
	int			error;
1056
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
1057

1058 1059
	trace_xfs_da_root_join(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
1060
	ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
1061 1062

	args = state->args;
1063
	oldroot = root_blk->bp->b_addr;
1064
	dp->d_ops->node_hdr_from_disk(&oldroothdr, oldroot);
1065 1066
	ASSERT(oldroothdr.forw == 0);
	ASSERT(oldroothdr.back == 0);
Linus Torvalds's avatar
Linus Torvalds committed
1067 1068 1069 1070

	/*
	 * If the root has more than one child, then don't do anything.
	 */
1071 1072
	if (oldroothdr.count > 1)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1073 1074 1075 1076 1077

	/*
	 * Read in the (only) child block, then copy those bytes into
	 * the root block's buffer and free the original child block.
	 */
1078
	btree = dp->d_ops->node_tree_p(oldroot);
1079
	child = be32_to_cpu(btree[0].before);
Linus Torvalds's avatar
Linus Torvalds committed
1080
	ASSERT(child != 0);
1081
	error = xfs_da3_node_read(args->trans, dp, child, -1, &bp,
1082
					     args->whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
1083
	if (error)
1084 1085
		return error;
	xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
1086

1087 1088 1089
	/*
	 * This could be copying a leaf back into the root block in the case of
	 * there only being a single leaf block left in the tree. Hence we have
1090
	 * to update the b_ops pointer as well to match the buffer type change
1091 1092
	 * that could occur. For dir3 blocks we also need to update the block
	 * number in the buffer header.
1093
	 */
1094
	memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
1095
	root_blk->bp->b_ops = bp->b_ops;
1096
	xfs_trans_buf_copy_type(root_blk->bp, bp);
1097 1098 1099 1100
	if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
		struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
		da3->blkno = cpu_to_be64(root_blk->bp->b_bn);
	}
1101 1102
	xfs_trans_log_buf(args->trans, root_blk->bp, 0,
			  args->geo->blksize - 1);
Linus Torvalds's avatar
Linus Torvalds committed
1103
	error = xfs_da_shrink_inode(args, child, bp);
Eric Sandeen's avatar
Eric Sandeen committed
1104
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
}

/*
 * Check a node block and its neighbors to see if the block should be
 * collapsed into one or the other neighbor.  Always keep the block
 * with the smaller block number.
 * If the current block is over 50% full, don't try to join it, return 0.
 * If the block is empty, fill in the state structure and return 2.
 * If it can be collapsed, fill in the state structure and return 1.
 * If nothing can be done, return 0.
 */
STATIC int
1117 1118 1119
xfs_da3_node_toosmall(
	struct xfs_da_state	*state,
	int			*action)
Linus Torvalds's avatar
Linus Torvalds committed
1120
{
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
	struct xfs_da_intnode	*node;
	struct xfs_da_state_blk	*blk;
	struct xfs_da_blkinfo	*info;
	xfs_dablk_t		blkno;
	struct xfs_buf		*bp;
	struct xfs_da3_icnode_hdr nodehdr;
	int			count;
	int			forward;
	int			error;
	int			retval;
	int			i;
1132
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
1133

1134 1135
	trace_xfs_da_node_toosmall(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
1136 1137 1138 1139 1140 1141
	/*
	 * Check for the degenerate case of the block being over 50% full.
	 * If so, it's not worth even looking to see if we might be able
	 * to coalesce with a sibling.
	 */
	blk = &state->path.blk[ state->path.active-1 ];
1142
	info = blk->bp->b_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1143
	node = (xfs_da_intnode_t *)info;
1144
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1145
	if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
Linus Torvalds's avatar
Linus Torvalds committed
1146
		*action = 0;	/* blk over 50%, don't try to join */
Eric Sandeen's avatar
Eric Sandeen committed
1147
		return 0;	/* blk over 50%, don't try to join */
Linus Torvalds's avatar
Linus Torvalds committed
1148 1149 1150 1151 1152
	}

	/*
	 * Check for the degenerate case of the block being empty.
	 * If the block is empty, we'll simply delete it, no need to
1153
	 * coalesce it with a sibling block.  We choose (arbitrarily)
Linus Torvalds's avatar
Linus Torvalds committed
1154 1155
	 * to merge with the forward block unless it is NULL.
	 */
1156
	if (nodehdr.count == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158 1159 1160
		/*
		 * Make altpath point to the block we want to keep and
		 * path point to the block we want to drop (this one).
		 */
1161
		forward = (info->forw != 0);
Linus Torvalds's avatar
Linus Torvalds committed
1162
		memcpy(&state->altpath, &state->path, sizeof(state->path));
1163
		error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds's avatar
Linus Torvalds committed
1164 1165
						 0, &retval);
		if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1166
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
1167 1168 1169 1170 1171
		if (retval) {
			*action = 0;
		} else {
			*action = 2;
		}
Eric Sandeen's avatar
Eric Sandeen committed
1172
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1173 1174 1175 1176 1177 1178 1179 1180 1181
	}

	/*
	 * Examine each sibling block to see if we can coalesce with
	 * at least 25% free space to spare.  We need to figure out
	 * whether to merge with the forward or the backward block.
	 * We prefer coalescing with the lower numbered sibling so as
	 * to shrink a directory over time.
	 */
1182 1183
	count  = state->args->geo->node_ents;
	count -= state->args->geo->node_ents >> 2;
1184 1185
	count -= nodehdr.count;

Linus Torvalds's avatar
Linus Torvalds committed
1186
	/* start with smaller blk num */
1187
	forward = nodehdr.forw < nodehdr.back;
Linus Torvalds's avatar
Linus Torvalds committed
1188
	for (i = 0; i < 2; forward = !forward, i++) {
1189
		struct xfs_da3_icnode_hdr thdr;
Linus Torvalds's avatar
Linus Torvalds committed
1190
		if (forward)
1191
			blkno = nodehdr.forw;
Linus Torvalds's avatar
Linus Torvalds committed
1192
		else
1193
			blkno = nodehdr.back;
Linus Torvalds's avatar
Linus Torvalds committed
1194 1195
		if (blkno == 0)
			continue;
1196
		error = xfs_da3_node_read(state->args->trans, dp,
1197
					blkno, -1, &bp, state->args->whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
1198
		if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1199
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
1200

1201
		node = bp->b_addr;
1202
		dp->d_ops->node_hdr_from_disk(&thdr, node);
1203
		xfs_trans_brelse(state->args->trans, bp);
1204

1205
		if (count - thdr.count >= 0)
Linus Torvalds's avatar
Linus Torvalds committed
1206 1207 1208 1209
			break;	/* fits with at least 25% to spare */
	}
	if (i >= 2) {
		*action = 0;
1210
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1211 1212 1213 1214 1215 1216 1217 1218
	}

	/*
	 * Make altpath point to the block we want to keep (the lower
	 * numbered block) and path point to the block we want to drop.
	 */
	memcpy(&state->altpath, &state->path, sizeof(state->path));
	if (blkno < blk->blkno) {
1219
		error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds's avatar
Linus Torvalds committed
1220 1221
						 0, &retval);
	} else {
1222
		error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds's avatar
Linus Torvalds committed
1223
						 0, &retval);
1224 1225 1226 1227 1228 1229
	}
	if (error)
		return error;
	if (retval) {
		*action = 0;
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1230 1231
	}
	*action = 1;
1232 1233 1234 1235 1236 1237 1238 1239
	return 0;
}

/*
 * Pick up the last hashvalue from an intermediate node.
 */
STATIC uint
xfs_da3_node_lasthash(
1240
	struct xfs_inode	*dp,
1241 1242 1243 1244 1245 1246 1247 1248
	struct xfs_buf		*bp,
	int			*count)
{
	struct xfs_da_intnode	 *node;
	struct xfs_da_node_entry *btree;
	struct xfs_da3_icnode_hdr nodehdr;

	node = bp->b_addr;
1249
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1250 1251 1252 1253
	if (count)
		*count = nodehdr.count;
	if (!nodehdr.count)
		return 0;
1254
	btree = dp->d_ops->node_tree_p(node);
1255
	return be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
1256 1257 1258 1259 1260 1261 1262
}

/*
 * Walk back up the tree adjusting hash values as necessary,
 * when we stop making changes, return.
 */
void
1263 1264 1265
xfs_da3_fixhashpath(
	struct xfs_da_state	*state,
	struct xfs_da_state_path *path)
Linus Torvalds's avatar
Linus Torvalds committed
1266
{
1267 1268 1269 1270 1271 1272
	struct xfs_da_state_blk	*blk;
	struct xfs_da_intnode	*node;
	struct xfs_da_node_entry *btree;
	xfs_dahash_t		lasthash=0;
	int			level;
	int			count;
1273
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
1274

1275 1276
	trace_xfs_da_fixhashpath(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
1277 1278 1279 1280 1281 1282 1283 1284 1285
	level = path->active-1;
	blk = &path->blk[ level ];
	switch (blk->magic) {
	case XFS_ATTR_LEAF_MAGIC:
		lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
		if (count == 0)
			return;
		break;
	case XFS_DIR2_LEAFN_MAGIC:
1286
		lasthash = xfs_dir2_leafn_lasthash(dp, blk->bp, &count);
Linus Torvalds's avatar
Linus Torvalds committed
1287 1288 1289 1290
		if (count == 0)
			return;
		break;
	case XFS_DA_NODE_MAGIC:
1291
		lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
Linus Torvalds's avatar
Linus Torvalds committed
1292 1293 1294 1295 1296
		if (count == 0)
			return;
		break;
	}
	for (blk--, level--; level >= 0; blk--, level--) {
1297 1298
		struct xfs_da3_icnode_hdr nodehdr;

1299
		node = blk->bp->b_addr;
1300
		dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1301
		btree = dp->d_ops->node_tree_p(node);
1302
		if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
Linus Torvalds's avatar
Linus Torvalds committed
1303 1304
			break;
		blk->hashval = lasthash;
1305
		btree[blk->index].hashval = cpu_to_be32(lasthash);
1306
		xfs_trans_log_buf(state->args->trans, blk->bp,
1307 1308
				  XFS_DA_LOGRANGE(node, &btree[blk->index],
						  sizeof(*btree)));
Linus Torvalds's avatar
Linus Torvalds committed
1309

1310
		lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
1311 1312 1313 1314 1315 1316 1317
	}
}

/*
 * Remove an entry from an intermediate node.
 */
STATIC void
1318 1319 1320
xfs_da3_node_remove(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*drop_blk)
Linus Torvalds's avatar
Linus Torvalds committed
1321
{
1322 1323 1324 1325 1326
	struct xfs_da_intnode	*node;
	struct xfs_da3_icnode_hdr nodehdr;
	struct xfs_da_node_entry *btree;
	int			index;
	int			tmp;
1327
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
1328

1329 1330
	trace_xfs_da_node_remove(state->args);

1331
	node = drop_blk->bp->b_addr;
1332
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1333
	ASSERT(drop_blk->index < nodehdr.count);
Linus Torvalds's avatar
Linus Torvalds committed
1334 1335 1336 1337 1338
	ASSERT(drop_blk->index >= 0);

	/*
	 * Copy over the offending entry, or just zero it out.
	 */
1339
	index = drop_blk->index;
1340
	btree = dp->d_ops->node_tree_p(node);
1341 1342
	if (index < nodehdr.count - 1) {
		tmp  = nodehdr.count - index - 1;
Linus Torvalds's avatar
Linus Torvalds committed
1343
		tmp *= (uint)sizeof(xfs_da_node_entry_t);
1344
		memmove(&btree[index], &btree[index + 1], tmp);
1345
		xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1346 1347
		    XFS_DA_LOGRANGE(node, &btree[index], tmp));
		index = nodehdr.count - 1;
Linus Torvalds's avatar
Linus Torvalds committed
1348
	}
1349
	memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
1350
	xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1351 1352
	    XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
	nodehdr.count -= 1;
1353
	dp->d_ops->node_hdr_to_disk(node, &nodehdr);
1354
	xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1355
	    XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds's avatar
Linus Torvalds committed
1356 1357 1358 1359

	/*
	 * Copy the last hash value from the block to propagate upwards.
	 */
1360
	drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
1361 1362 1363
}

/*
1364
 * Unbalance the elements between two intermediate nodes,
Linus Torvalds's avatar
Linus Torvalds committed
1365 1366 1367
 * move all Btree elements from one node into another.
 */
STATIC void
1368 1369 1370 1371
xfs_da3_node_unbalance(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*drop_blk,
	struct xfs_da_state_blk	*save_blk)
Linus Torvalds's avatar
Linus Torvalds committed
1372
{
1373 1374 1375 1376 1377 1378 1379 1380 1381
	struct xfs_da_intnode	*drop_node;
	struct xfs_da_intnode	*save_node;
	struct xfs_da_node_entry *drop_btree;
	struct xfs_da_node_entry *save_btree;
	struct xfs_da3_icnode_hdr drop_hdr;
	struct xfs_da3_icnode_hdr save_hdr;
	struct xfs_trans	*tp;
	int			sindex;
	int			tmp;
1382
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
1383

1384 1385
	trace_xfs_da_node_unbalance(state->args);

1386 1387
	drop_node = drop_blk->bp->b_addr;
	save_node = save_blk->bp->b_addr;
1388 1389
	dp->d_ops->node_hdr_from_disk(&drop_hdr, drop_node);
	dp->d_ops->node_hdr_from_disk(&save_hdr, save_node);
1390 1391
	drop_btree = dp->d_ops->node_tree_p(drop_node);
	save_btree = dp->d_ops->node_tree_p(save_node);
Linus Torvalds's avatar
Linus Torvalds committed
1392 1393 1394 1395 1396 1397
	tp = state->args->trans;

	/*
	 * If the dying block has lower hashvals, then move all the
	 * elements in the remaining block up to make a hole.
	 */
1398 1399 1400 1401 1402 1403 1404 1405 1406
	if ((be32_to_cpu(drop_btree[0].hashval) <
			be32_to_cpu(save_btree[0].hashval)) ||
	    (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
			be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
		/* XXX: check this - is memmove dst correct? */
		tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
		memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);

		sindex = 0;
1407
		xfs_trans_log_buf(tp, save_blk->bp,
1408 1409 1410
			XFS_DA_LOGRANGE(save_node, &save_btree[0],
				(save_hdr.count + drop_hdr.count) *
						sizeof(xfs_da_node_entry_t)));
Linus Torvalds's avatar
Linus Torvalds committed
1411
	} else {
1412
		sindex = save_hdr.count;
1413
		xfs_trans_log_buf(tp, save_blk->bp,
1414 1415
			XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
				drop_hdr.count * sizeof(xfs_da_node_entry_t)));
Linus Torvalds's avatar
Linus Torvalds committed
1416 1417 1418 1419 1420
	}

	/*
	 * Move all the B-tree elements from drop_blk to save_blk.
	 */
1421 1422 1423
	tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
	memcpy(&save_btree[sindex], &drop_btree[0], tmp);
	save_hdr.count += drop_hdr.count;
Linus Torvalds's avatar
Linus Torvalds committed
1424

1425
	dp->d_ops->node_hdr_to_disk(save_node, &save_hdr);
1426
	xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds's avatar
Linus Torvalds committed
1427
		XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1428
				dp->d_ops->node_hdr_size));
Linus Torvalds's avatar
Linus Torvalds committed
1429 1430 1431 1432

	/*
	 * Save the last hashval in the remaining block for upward propagation.
	 */
1433
	save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
}

/*========================================================================
 * Routines used for finding things in the Btree.
 *========================================================================*/

/*
 * Walk down the Btree looking for a particular filename, filling
 * in the state structure as we go.
 *
 * We will set the state structure to point to each of the elements
 * in each of the nodes where either the hashval is or should be.
 *
 * We support duplicate hashval's so for each entry in the current
 * node that could contain the desired hashval, descend.  This is a
 * pruned depth-first tree search.
 */
int							/* error */
1452 1453 1454
xfs_da3_node_lookup_int(
	struct xfs_da_state	*state,
	int			*result)
Linus Torvalds's avatar
Linus Torvalds committed
1455
{
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
	struct xfs_da_state_blk	*blk;
	struct xfs_da_blkinfo	*curr;
	struct xfs_da_intnode	*node;
	struct xfs_da_node_entry *btree;
	struct xfs_da3_icnode_hdr nodehdr;
	struct xfs_da_args	*args;
	xfs_dablk_t		blkno;
	xfs_dahash_t		hashval;
	xfs_dahash_t		btreehashval;
	int			probe;
	int			span;
	int			max;
	int			error;
	int			retval;
1470
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
1471 1472 1473 1474 1475 1476 1477

	args = state->args;

	/*
	 * Descend thru the B-tree searching each level for the right
	 * node to use, until the right hashval is found.
	 */
1478
	blkno = (args->whichfork == XFS_DATA_FORK)? args->geo->leafblk : 0;
Linus Torvalds's avatar
Linus Torvalds committed
1479 1480 1481 1482 1483 1484 1485
	for (blk = &state->path.blk[0], state->path.active = 1;
			 state->path.active <= XFS_DA_NODE_MAXDEPTH;
			 blk++, state->path.active++) {
		/*
		 * Read the next node down in the tree.
		 */
		blk->blkno = blkno;
1486
		error = xfs_da3_node_read(args->trans, args->dp, blkno,
1487
					-1, &blk->bp, args->whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
1488 1489 1490
		if (error) {
			blk->blkno = 0;
			state->path.active--;
Eric Sandeen's avatar
Eric Sandeen committed
1491
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
1492
		}
1493
		curr = blk->bp->b_addr;
1494
		blk->magic = be16_to_cpu(curr->magic);
1495

1496 1497 1498
		if (blk->magic == XFS_ATTR_LEAF_MAGIC ||
		    blk->magic == XFS_ATTR3_LEAF_MAGIC) {
			blk->magic = XFS_ATTR_LEAF_MAGIC;
1499 1500 1501 1502 1503 1504 1505
			blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
			break;
		}

		if (blk->magic == XFS_DIR2_LEAFN_MAGIC ||
		    blk->magic == XFS_DIR3_LEAFN_MAGIC) {
			blk->magic = XFS_DIR2_LEAFN_MAGIC;
1506 1507
			blk->hashval = xfs_dir2_leafn_lasthash(args->dp,
							       blk->bp, NULL);
1508 1509 1510 1511 1512
			break;
		}

		blk->magic = XFS_DA_NODE_MAGIC;

Linus Torvalds's avatar
Linus Torvalds committed
1513 1514 1515 1516

		/*
		 * Search an intermediate node for a match.
		 */
1517
		node = blk->bp->b_addr;
1518
		dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1519
		btree = dp->d_ops->node_tree_p(node);
Linus Torvalds's avatar
Linus Torvalds committed
1520

1521 1522
		max = nodehdr.count;
		blk->hashval = be32_to_cpu(btree[max - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
1523

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
		/*
		 * Binary search.  (note: small blocks will skip loop)
		 */
		probe = span = max / 2;
		hashval = args->hashval;
		while (span > 4) {
			span /= 2;
			btreehashval = be32_to_cpu(btree[probe].hashval);
			if (btreehashval < hashval)
				probe += span;
			else if (btreehashval > hashval)
				probe -= span;
			else
				break;
		}
		ASSERT((probe >= 0) && (probe < max));
		ASSERT((span <= 4) ||
			(be32_to_cpu(btree[probe].hashval) == hashval));
Linus Torvalds's avatar
Linus Torvalds committed
1542

1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
		/*
		 * Since we may have duplicate hashval's, find the first
		 * matching hashval in the node.
		 */
		while (probe > 0 &&
		       be32_to_cpu(btree[probe].hashval) >= hashval) {
			probe--;
		}
		while (probe < max &&
		       be32_to_cpu(btree[probe].hashval) < hashval) {
			probe++;
		}

		/*
		 * Pick the right block to descend on.
		 */
		if (probe == max) {
			blk->index = max - 1;
			blkno = be32_to_cpu(btree[max - 1].before);
		} else {
			blk->index = probe;
			blkno = be32_to_cpu(btree[probe].before);
Linus Torvalds's avatar
Linus Torvalds committed
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
		}
	}

	/*
	 * A leaf block that ends in the hashval that we are interested in
	 * (final hashval == search hashval) means that the next block may
	 * contain more entries with the same hashval, shift upward to the
	 * next leaf and keep searching.
	 */
	for (;;) {
1575
		if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds's avatar
Linus Torvalds committed
1576 1577
			retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
							&blk->index, state);
1578
		} else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1579
			retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
Linus Torvalds's avatar
Linus Torvalds committed
1580 1581
			blk->index = args->index;
			args->blkno = blk->blkno;
1582 1583
		} else {
			ASSERT(0);
1584
			return -EFSCORRUPTED;
Linus Torvalds's avatar
Linus Torvalds committed
1585
		}
1586
		if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
Linus Torvalds's avatar
Linus Torvalds committed
1587
		    (blk->hashval == args->hashval)) {
1588
			error = xfs_da3_path_shift(state, &state->path, 1, 1,
Linus Torvalds's avatar
Linus Torvalds committed
1589 1590
							 &retval);
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1591
				return error;
Linus Torvalds's avatar
Linus Torvalds committed
1592 1593
			if (retval == 0) {
				continue;
1594
			} else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds's avatar
Linus Torvalds committed
1595
				/* path_shift() gives ENOENT */
1596
				retval = -ENOATTR;
Linus Torvalds's avatar
Linus Torvalds committed
1597 1598 1599 1600 1601
			}
		}
		break;
	}
	*result = retval;
Eric Sandeen's avatar
Eric Sandeen committed
1602
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1603 1604 1605 1606 1607 1608
}

/*========================================================================
 * Utility routines.
 *========================================================================*/

1609 1610 1611 1612 1613
/*
 * Compare two intermediate nodes for "order".
 */
STATIC int
xfs_da3_node_order(
1614
	struct xfs_inode *dp,
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
	struct xfs_buf	*node1_bp,
	struct xfs_buf	*node2_bp)
{
	struct xfs_da_intnode	*node1;
	struct xfs_da_intnode	*node2;
	struct xfs_da_node_entry *btree1;
	struct xfs_da_node_entry *btree2;
	struct xfs_da3_icnode_hdr node1hdr;
	struct xfs_da3_icnode_hdr node2hdr;

	node1 = node1_bp->b_addr;
	node2 = node2_bp->b_addr;
1627 1628
	dp->d_ops->node_hdr_from_disk(&node1hdr, node1);
	dp->d_ops->node_hdr_from_disk(&node2hdr, node2);
1629 1630
	btree1 = dp->d_ops->node_tree_p(node1);
	btree2 = dp->d_ops->node_tree_p(node2);
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640

	if (node1hdr.count > 0 && node2hdr.count > 0 &&
	    ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
	     (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
	      be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
		return 1;
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1641 1642 1643 1644
/*
 * Link a new block into a doubly linked list of blocks (of whatever type).
 */
int							/* error */
1645 1646 1647 1648
xfs_da3_blk_link(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*old_blk,
	struct xfs_da_state_blk	*new_blk)
Linus Torvalds's avatar
Linus Torvalds committed
1649
{
1650 1651 1652 1653 1654 1655 1656
	struct xfs_da_blkinfo	*old_info;
	struct xfs_da_blkinfo	*new_info;
	struct xfs_da_blkinfo	*tmp_info;
	struct xfs_da_args	*args;
	struct xfs_buf		*bp;
	int			before = 0;
	int			error;
1657
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
1658 1659 1660 1661 1662 1663

	/*
	 * Set up environment.
	 */
	args = state->args;
	ASSERT(args != NULL);
1664 1665
	old_info = old_blk->bp->b_addr;
	new_info = new_blk->bp->b_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1666
	ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
1667
	       old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds's avatar
Linus Torvalds committed
1668 1669 1670 1671 1672 1673 1674
	       old_blk->magic == XFS_ATTR_LEAF_MAGIC);

	switch (old_blk->magic) {
	case XFS_ATTR_LEAF_MAGIC:
		before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
		break;
	case XFS_DIR2_LEAFN_MAGIC:
1675
		before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds's avatar
Linus Torvalds committed
1676 1677
		break;
	case XFS_DA_NODE_MAGIC:
1678
		before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds's avatar
Linus Torvalds committed
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
		break;
	}

	/*
	 * Link blocks in appropriate order.
	 */
	if (before) {
		/*
		 * Link new block in before existing block.
		 */
1689
		trace_xfs_da_link_before(args);
1690 1691 1692
		new_info->forw = cpu_to_be32(old_blk->blkno);
		new_info->back = old_info->back;
		if (old_info->back) {
1693
			error = xfs_da3_node_read(args->trans, dp,
1694
						be32_to_cpu(old_info->back),
1695
						-1, &bp, args->whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
1696
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1697
				return error;
Linus Torvalds's avatar
Linus Torvalds committed
1698
			ASSERT(bp != NULL);
1699
			tmp_info = bp->b_addr;
1700
			ASSERT(tmp_info->magic == old_info->magic);
1701 1702
			ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
			tmp_info->forw = cpu_to_be32(new_blk->blkno);
1703
			xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds's avatar
Linus Torvalds committed
1704
		}
1705
		old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds's avatar
Linus Torvalds committed
1706 1707 1708 1709
	} else {
		/*
		 * Link new block in after existing block.
		 */
1710
		trace_xfs_da_link_after(args);
1711 1712 1713
		new_info->forw = old_info->forw;
		new_info->back = cpu_to_be32(old_blk->blkno);
		if (old_info->forw) {
1714
			error = xfs_da3_node_read(args->trans, dp,
1715
						be32_to_cpu(old_info->forw),
1716
						-1, &bp, args->whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
1717
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1718
				return error;
Linus Torvalds's avatar
Linus Torvalds committed
1719
			ASSERT(bp != NULL);
1720
			tmp_info = bp->b_addr;
1721 1722 1723
			ASSERT(tmp_info->magic == old_info->magic);
			ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
			tmp_info->back = cpu_to_be32(new_blk->blkno);
1724
			xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds's avatar
Linus Torvalds committed
1725
		}
1726
		old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds's avatar
Linus Torvalds committed
1727 1728
	}

1729 1730
	xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
	xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Eric Sandeen's avatar
Eric Sandeen committed
1731
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1732 1733 1734 1735 1736
}

/*
 * Unlink a block from a doubly linked list of blocks.
 */
1737
STATIC int						/* error */
1738 1739 1740 1741
xfs_da3_blk_unlink(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*drop_blk,
	struct xfs_da_state_blk	*save_blk)
Linus Torvalds's avatar
Linus Torvalds committed
1742
{
1743 1744 1745 1746 1747 1748
	struct xfs_da_blkinfo	*drop_info;
	struct xfs_da_blkinfo	*save_info;
	struct xfs_da_blkinfo	*tmp_info;
	struct xfs_da_args	*args;
	struct xfs_buf		*bp;
	int			error;
Linus Torvalds's avatar
Linus Torvalds committed
1749 1750 1751 1752 1753 1754

	/*
	 * Set up environment.
	 */
	args = state->args;
	ASSERT(args != NULL);
1755 1756
	save_info = save_blk->bp->b_addr;
	drop_info = drop_blk->bp->b_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1757
	ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
1758
	       save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds's avatar
Linus Torvalds committed
1759 1760
	       save_blk->magic == XFS_ATTR_LEAF_MAGIC);
	ASSERT(save_blk->magic == drop_blk->magic);
1761 1762 1763 1764
	ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
	       (be32_to_cpu(save_info->back) == drop_blk->blkno));
	ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
	       (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds's avatar
Linus Torvalds committed
1765 1766 1767 1768

	/*
	 * Unlink the leaf block from the doubly linked chain of leaves.
	 */
1769
	if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
1770
		trace_xfs_da_unlink_back(args);
1771 1772
		save_info->back = drop_info->back;
		if (drop_info->back) {
1773
			error = xfs_da3_node_read(args->trans, args->dp,
1774
						be32_to_cpu(drop_info->back),
1775
						-1, &bp, args->whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
1776
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1777
				return error;
Linus Torvalds's avatar
Linus Torvalds committed
1778
			ASSERT(bp != NULL);
1779
			tmp_info = bp->b_addr;
1780 1781 1782
			ASSERT(tmp_info->magic == save_info->magic);
			ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
			tmp_info->forw = cpu_to_be32(save_blk->blkno);
1783
			xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds's avatar
Linus Torvalds committed
1784 1785 1786
						    sizeof(*tmp_info) - 1);
		}
	} else {
1787
		trace_xfs_da_unlink_forward(args);
1788 1789
		save_info->forw = drop_info->forw;
		if (drop_info->forw) {
1790
			error = xfs_da3_node_read(args->trans, args->dp,
1791
						be32_to_cpu(drop_info->forw),
1792
						-1, &bp, args->whichfork);
Linus Torvalds's avatar
Linus Torvalds committed
1793
			if (error)
Eric Sandeen's avatar
Eric Sandeen committed
1794
				return error;
Linus Torvalds's avatar
Linus Torvalds committed
1795
			ASSERT(bp != NULL);
1796
			tmp_info = bp->b_addr;
1797 1798 1799
			ASSERT(tmp_info->magic == save_info->magic);
			ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
			tmp_info->back = cpu_to_be32(save_blk->blkno);
1800
			xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds's avatar
Linus Torvalds committed
1801 1802 1803 1804
						    sizeof(*tmp_info) - 1);
		}
	}

1805
	xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Eric Sandeen's avatar
Eric Sandeen committed
1806
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
}

/*
 * Move a path "forward" or "!forward" one block at the current level.
 *
 * This routine will adjust a "path" to point to the next block
 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
 * Btree, including updating pointers to the intermediate nodes between
 * the new bottom and the root.
 */
int							/* error */
1818 1819 1820 1821 1822 1823
xfs_da3_path_shift(
	struct xfs_da_state	*state,
	struct xfs_da_state_path *path,
	int			forward,
	int			release,
	int			*result)
Linus Torvalds's avatar
Linus Torvalds committed
1824
{
1825 1826 1827 1828 1829 1830
	struct xfs_da_state_blk	*blk;
	struct xfs_da_blkinfo	*info;
	struct xfs_da_intnode	*node;
	struct xfs_da_args	*args;
	struct xfs_da_node_entry *btree;
	struct xfs_da3_icnode_hdr nodehdr;
1831
	struct xfs_buf		*bp;
1832 1833 1834
	xfs_dablk_t		blkno = 0;
	int			level;
	int			error;
1835
	struct xfs_inode	*dp = state->args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
1836

1837 1838
	trace_xfs_da_path_shift(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
	/*
	 * Roll up the Btree looking for the first block where our
	 * current index is not at the edge of the block.  Note that
	 * we skip the bottom layer because we want the sibling block.
	 */
	args = state->args;
	ASSERT(args != NULL);
	ASSERT(path != NULL);
	ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
	level = (path->active-1) - 1;	/* skip bottom layer in path */
	for (blk = &path->blk[level]; level >= 0; blk--, level--) {
1850
		node = blk->bp->b_addr;
1851
		dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1852
		btree = dp->d_ops->node_tree_p(node);
1853 1854

		if (forward && (blk->index < nodehdr.count - 1)) {
Linus Torvalds's avatar
Linus Torvalds committed
1855
			blk->index++;
1856
			blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds's avatar
Linus Torvalds committed
1857 1858 1859
			break;
		} else if (!forward && (blk->index > 0)) {
			blk->index--;
1860
			blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds's avatar
Linus Torvalds committed
1861 1862 1863 1864
			break;
		}
	}
	if (level < 0) {
1865
		*result = -ENOENT;	/* we're out of our tree */
1866
		ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Eric Sandeen's avatar
Eric Sandeen committed
1867
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1868 1869 1870 1871 1872 1873 1874 1875
	}

	/*
	 * Roll down the edge of the subtree until we reach the
	 * same depth we were at originally.
	 */
	for (blk++, level++; level < path->active; blk++, level++) {
		/*
1876
		 * Read the next child block into a local buffer.
Linus Torvalds's avatar
Linus Torvalds committed
1877
		 */
1878 1879 1880 1881
		error = xfs_da3_node_read(args->trans, dp, blkno, -1, &bp,
					  args->whichfork);
		if (error)
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
1882 1883

		/*
1884 1885 1886 1887
		 * Release the old block (if it's dirty, the trans doesn't
		 * actually let go) and swap the local buffer into the path
		 * structure. This ensures failure of the above read doesn't set
		 * a NULL buffer in an active slot in the path.
Linus Torvalds's avatar
Linus Torvalds committed
1888
		 */
1889 1890
		if (release)
			xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds's avatar
Linus Torvalds committed
1891
		blk->blkno = blkno;
1892 1893
		blk->bp = bp;

1894
		info = blk->bp->b_addr;
1895
		ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1896
		       info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
1897
		       info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1898
		       info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
1899 1900
		       info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
		       info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910


		/*
		 * Note: we flatten the magic number to a single type so we
		 * don't have to compare against crc/non-crc types elsewhere.
		 */
		switch (be16_to_cpu(info->magic)) {
		case XFS_DA_NODE_MAGIC:
		case XFS_DA3_NODE_MAGIC:
			blk->magic = XFS_DA_NODE_MAGIC;
Linus Torvalds's avatar
Linus Torvalds committed
1911
			node = (xfs_da_intnode_t *)info;
1912
			dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1913
			btree = dp->d_ops->node_tree_p(node);
1914
			blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
1915 1916 1917
			if (forward)
				blk->index = 0;
			else
1918 1919 1920 1921
				blk->index = nodehdr.count - 1;
			blkno = be32_to_cpu(btree[blk->index].before);
			break;
		case XFS_ATTR_LEAF_MAGIC:
1922
		case XFS_ATTR3_LEAF_MAGIC:
1923
			blk->magic = XFS_ATTR_LEAF_MAGIC;
Linus Torvalds's avatar
Linus Torvalds committed
1924 1925
			ASSERT(level == path->active-1);
			blk->index = 0;
1926
			blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1927 1928 1929 1930 1931 1932
			break;
		case XFS_DIR2_LEAFN_MAGIC:
		case XFS_DIR3_LEAFN_MAGIC:
			blk->magic = XFS_DIR2_LEAFN_MAGIC;
			ASSERT(level == path->active-1);
			blk->index = 0;
1933 1934
			blk->hashval = xfs_dir2_leafn_lasthash(args->dp,
							       blk->bp, NULL);
1935 1936 1937 1938
			break;
		default:
			ASSERT(0);
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1939 1940 1941
		}
	}
	*result = 0;
1942
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
}


/*========================================================================
 * Utility routines.
 *========================================================================*/

/*
 * Implement a simple hash on a character string.
 * Rotate the hash value by 7 bits, then XOR each character in.
 * This is implemented with some source-level loop unrolling.
 */
xfs_dahash_t
1956
xfs_da_hashname(const __uint8_t *name, int namelen)
Linus Torvalds's avatar
Linus Torvalds committed
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
{
	xfs_dahash_t hash;

	/*
	 * Do four characters at a time as long as we can.
	 */
	for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
		hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
		       (name[3] << 0) ^ rol32(hash, 7 * 4);

	/*
	 * Now do the rest of the characters.
	 */
	switch (namelen) {
	case 3:
		return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
		       rol32(hash, 7 * 3);
	case 2:
		return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
	case 1:
		return (name[0] << 0) ^ rol32(hash, 7 * 1);
1978
	default: /* case 0: */
Linus Torvalds's avatar
Linus Torvalds committed
1979 1980 1981 1982
		return hash;
	}
}

1983 1984 1985
enum xfs_dacmp
xfs_da_compname(
	struct xfs_da_args *args,
1986 1987
	const unsigned char *name,
	int		len)
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
{
	return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
					XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
}

static xfs_dahash_t
xfs_default_hashname(
	struct xfs_name	*name)
{
	return xfs_da_hashname(name->name, name->len);
}

const struct xfs_nameops xfs_default_nameops = {
	.hashname	= xfs_default_hashname,
	.compname	= xfs_da_compname
};

Linus Torvalds's avatar
Linus Torvalds committed
2005
int
2006 2007 2008 2009
xfs_da_grow_inode_int(
	struct xfs_da_args	*args,
	xfs_fileoff_t		*bno,
	int			count)
Linus Torvalds's avatar
Linus Torvalds committed
2010
{
2011 2012 2013
	struct xfs_trans	*tp = args->trans;
	struct xfs_inode	*dp = args->dp;
	int			w = args->whichfork;
2014
	xfs_rfsblock_t		nblks = dp->i_d.di_nblocks;
2015 2016
	struct xfs_bmbt_irec	map, *mapp;
	int			nmap, error, got, i, mapi;
Linus Torvalds's avatar
Linus Torvalds committed
2017 2018 2019 2020

	/*
	 * Find a spot in the file space to put the new block.
	 */
2021 2022
	error = xfs_bmap_first_unused(tp, dp, count, bno, w);
	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
2023
		return error;
2024

Linus Torvalds's avatar
Linus Torvalds committed
2025 2026 2027 2028 2029
	/*
	 * Try mapping it in one filesystem block.
	 */
	nmap = 1;
	ASSERT(args->firstblock != NULL);
2030 2031
	error = xfs_bmapi_write(tp, dp, *bno, count,
			xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Linus Torvalds's avatar
Linus Torvalds committed
2032
			args->firstblock, args->total, &map, &nmap,
2033 2034
			args->flist);
	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
2035
		return error;
2036

Linus Torvalds's avatar
Linus Torvalds committed
2037 2038 2039 2040
	ASSERT(nmap <= 1);
	if (nmap == 1) {
		mapp = &map;
		mapi = 1;
2041 2042 2043 2044 2045 2046 2047 2048
	} else if (nmap == 0 && count > 1) {
		xfs_fileoff_t		b;
		int			c;

		/*
		 * If we didn't get it and the block might work if fragmented,
		 * try without the CONTIG flag.  Loop until we get it all.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
2049
		mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
2050
		for (b = *bno, mapi = 0; b < *bno + count; ) {
Linus Torvalds's avatar
Linus Torvalds committed
2051
			nmap = MIN(XFS_BMAP_MAX_NMAP, count);
2052
			c = (int)(*bno + count - b);
2053 2054
			error = xfs_bmapi_write(tp, dp, b, c,
					xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Linus Torvalds's avatar
Linus Torvalds committed
2055
					args->firstblock, args->total,
2056 2057 2058
					&mapp[mapi], &nmap, args->flist);
			if (error)
				goto out_free_map;
Linus Torvalds's avatar
Linus Torvalds committed
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
			if (nmap < 1)
				break;
			mapi += nmap;
			b = mapp[mapi - 1].br_startoff +
			    mapp[mapi - 1].br_blockcount;
		}
	} else {
		mapi = 0;
		mapp = NULL;
	}
2069

Linus Torvalds's avatar
Linus Torvalds committed
2070 2071 2072 2073 2074
	/*
	 * Count the blocks we got, make sure it matches the total.
	 */
	for (i = 0, got = 0; i < mapi; i++)
		got += mapp[i].br_blockcount;
2075
	if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds's avatar
Linus Torvalds committed
2076
	    mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
2077
	    *bno + count) {
2078
		error = -ENOSPC;
2079
		goto out_free_map;
Linus Torvalds's avatar
Linus Torvalds committed
2080
	}
2081

2082 2083
	/* account for newly allocated blocks in reserved blocks total */
	args->total -= dp->i_d.di_nblocks - nblks;
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102

out_free_map:
	if (mapp != &map)
		kmem_free(mapp);
	return error;
}

/*
 * Add a block to the btree ahead of the file.
 * Return the new block number to the caller.
 */
int
xfs_da_grow_inode(
	struct xfs_da_args	*args,
	xfs_dablk_t		*new_blkno)
{
	xfs_fileoff_t		bno;
	int			error;

2103 2104
	trace_xfs_da_grow_inode(args);

2105 2106
	bno = args->geo->leafblk;
	error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
2107 2108 2109
	if (!error)
		*new_blkno = (xfs_dablk_t)bno;
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
}

/*
 * Ick.  We need to always be able to remove a btree block, even
 * if there's no space reservation because the filesystem is full.
 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
 * It swaps the target block with the last block in the file.  The
 * last block in the file can always be removed since it can't cause
 * a bmap btree split to do that.
 */
STATIC int
2121 2122 2123 2124
xfs_da3_swap_lastblock(
	struct xfs_da_args	*args,
	xfs_dablk_t		*dead_blknop,
	struct xfs_buf		**dead_bufp)
Linus Torvalds's avatar
Linus Torvalds committed
2125
{
2126 2127 2128 2129 2130 2131 2132
	struct xfs_da_blkinfo	*dead_info;
	struct xfs_da_blkinfo	*sib_info;
	struct xfs_da_intnode	*par_node;
	struct xfs_da_intnode	*dead_node;
	struct xfs_dir2_leaf	*dead_leaf2;
	struct xfs_da_node_entry *btree;
	struct xfs_da3_icnode_hdr par_hdr;
2133
	struct xfs_inode	*dp;
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
	struct xfs_trans	*tp;
	struct xfs_mount	*mp;
	struct xfs_buf		*dead_buf;
	struct xfs_buf		*last_buf;
	struct xfs_buf		*sib_buf;
	struct xfs_buf		*par_buf;
	xfs_dahash_t		dead_hash;
	xfs_fileoff_t		lastoff;
	xfs_dablk_t		dead_blkno;
	xfs_dablk_t		last_blkno;
	xfs_dablk_t		sib_blkno;
	xfs_dablk_t		par_blkno;
	int			error;
	int			w;
	int			entno;
	int			level;
	int			dead_level;
Linus Torvalds's avatar
Linus Torvalds committed
2151

2152 2153
	trace_xfs_da_swap_lastblock(args);

Linus Torvalds's avatar
Linus Torvalds committed
2154 2155 2156
	dead_buf = *dead_bufp;
	dead_blkno = *dead_blknop;
	tp = args->trans;
2157
	dp = args->dp;
Linus Torvalds's avatar
Linus Torvalds committed
2158 2159
	w = args->whichfork;
	ASSERT(w == XFS_DATA_FORK);
2160
	mp = dp->i_mount;
2161
	lastoff = args->geo->freeblk;
2162
	error = xfs_bmap_last_before(tp, dp, &lastoff, w);
Linus Torvalds's avatar
Linus Torvalds committed
2163 2164 2165 2166 2167
	if (error)
		return error;
	if (unlikely(lastoff == 0)) {
		XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
				 mp);
2168
		return -EFSCORRUPTED;
Linus Torvalds's avatar
Linus Torvalds committed
2169 2170 2171 2172
	}
	/*
	 * Read the last block in the btree space.
	 */
2173
	last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
2174
	error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w);
2175
	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
2176 2177 2178 2179
		return error;
	/*
	 * Copy the last block into the dead buffer and log it.
	 */
2180 2181
	memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
	xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
2182
	dead_info = dead_buf->b_addr;
Linus Torvalds's avatar
Linus Torvalds committed
2183 2184 2185
	/*
	 * Get values from the moved block.
	 */
2186 2187 2188 2189 2190
	if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
	    dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
		struct xfs_dir3_icleaf_hdr leafhdr;
		struct xfs_dir2_leaf_entry *ents;

Linus Torvalds's avatar
Linus Torvalds committed
2191
		dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
2192
		dp->d_ops->leaf_hdr_from_disk(&leafhdr, dead_leaf2);
2193
		ents = dp->d_ops->leaf_ents_p(dead_leaf2);
Linus Torvalds's avatar
Linus Torvalds committed
2194
		dead_level = 0;
2195
		dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
2196
	} else {
2197 2198
		struct xfs_da3_icnode_hdr deadhdr;

Linus Torvalds's avatar
Linus Torvalds committed
2199
		dead_node = (xfs_da_intnode_t *)dead_info;
2200
		dp->d_ops->node_hdr_from_disk(&deadhdr, dead_node);
2201
		btree = dp->d_ops->node_tree_p(dead_node);
2202 2203
		dead_level = deadhdr.level;
		dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
Linus Torvalds's avatar
Linus Torvalds committed
2204 2205 2206 2207 2208
	}
	sib_buf = par_buf = NULL;
	/*
	 * If the moved block has a left sibling, fix up the pointers.
	 */
2209
	if ((sib_blkno = be32_to_cpu(dead_info->back))) {
2210
		error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
2211
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
2212
			goto done;
2213
		sib_info = sib_buf->b_addr;
Linus Torvalds's avatar
Linus Torvalds committed
2214
		if (unlikely(
2215 2216
		    be32_to_cpu(sib_info->forw) != last_blkno ||
		    sib_info->magic != dead_info->magic)) {
Linus Torvalds's avatar
Linus Torvalds committed
2217 2218
			XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
					 XFS_ERRLEVEL_LOW, mp);
2219
			error = -EFSCORRUPTED;
Linus Torvalds's avatar
Linus Torvalds committed
2220 2221
			goto done;
		}
2222
		sib_info->forw = cpu_to_be32(dead_blkno);
2223
		xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds's avatar
Linus Torvalds committed
2224 2225 2226 2227 2228 2229 2230
			XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
					sizeof(sib_info->forw)));
		sib_buf = NULL;
	}
	/*
	 * If the moved block has a right sibling, fix up the pointers.
	 */
2231
	if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
2232
		error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
2233
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
2234
			goto done;
2235
		sib_info = sib_buf->b_addr;
Linus Torvalds's avatar
Linus Torvalds committed
2236
		if (unlikely(
2237 2238
		       be32_to_cpu(sib_info->back) != last_blkno ||
		       sib_info->magic != dead_info->magic)) {
Linus Torvalds's avatar
Linus Torvalds committed
2239 2240
			XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
					 XFS_ERRLEVEL_LOW, mp);
2241
			error = -EFSCORRUPTED;
Linus Torvalds's avatar
Linus Torvalds committed
2242 2243
			goto done;
		}
2244
		sib_info->back = cpu_to_be32(dead_blkno);
2245
		xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds's avatar
Linus Torvalds committed
2246 2247 2248 2249
			XFS_DA_LOGRANGE(sib_info, &sib_info->back,
					sizeof(sib_info->back)));
		sib_buf = NULL;
	}
2250
	par_blkno = args->geo->leafblk;
Linus Torvalds's avatar
Linus Torvalds committed
2251 2252 2253 2254 2255
	level = -1;
	/*
	 * Walk down the tree looking for the parent of the moved block.
	 */
	for (;;) {
2256
		error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
2257
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
2258
			goto done;
2259
		par_node = par_buf->b_addr;
2260
		dp->d_ops->node_hdr_from_disk(&par_hdr, par_node);
2261
		if (level >= 0 && level != par_hdr.level + 1) {
Linus Torvalds's avatar
Linus Torvalds committed
2262 2263
			XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
					 XFS_ERRLEVEL_LOW, mp);
2264
			error = -EFSCORRUPTED;
Linus Torvalds's avatar
Linus Torvalds committed
2265 2266
			goto done;
		}
2267
		level = par_hdr.level;
2268
		btree = dp->d_ops->node_tree_p(par_node);
Linus Torvalds's avatar
Linus Torvalds committed
2269
		for (entno = 0;
2270 2271
		     entno < par_hdr.count &&
		     be32_to_cpu(btree[entno].hashval) < dead_hash;
Linus Torvalds's avatar
Linus Torvalds committed
2272 2273
		     entno++)
			continue;
2274
		if (entno == par_hdr.count) {
Linus Torvalds's avatar
Linus Torvalds committed
2275 2276
			XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
					 XFS_ERRLEVEL_LOW, mp);
2277
			error = -EFSCORRUPTED;
Linus Torvalds's avatar
Linus Torvalds committed
2278 2279
			goto done;
		}
2280
		par_blkno = be32_to_cpu(btree[entno].before);
Linus Torvalds's avatar
Linus Torvalds committed
2281 2282
		if (level == dead_level + 1)
			break;
2283
		xfs_trans_brelse(tp, par_buf);
Linus Torvalds's avatar
Linus Torvalds committed
2284 2285 2286 2287 2288 2289 2290 2291
		par_buf = NULL;
	}
	/*
	 * We're in the right parent block.
	 * Look for the right entry.
	 */
	for (;;) {
		for (;
2292 2293
		     entno < par_hdr.count &&
		     be32_to_cpu(btree[entno].before) != last_blkno;
Linus Torvalds's avatar
Linus Torvalds committed
2294 2295
		     entno++)
			continue;
2296
		if (entno < par_hdr.count)
Linus Torvalds's avatar
Linus Torvalds committed
2297
			break;
2298
		par_blkno = par_hdr.forw;
2299
		xfs_trans_brelse(tp, par_buf);
Linus Torvalds's avatar
Linus Torvalds committed
2300 2301 2302 2303
		par_buf = NULL;
		if (unlikely(par_blkno == 0)) {
			XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
					 XFS_ERRLEVEL_LOW, mp);
2304
			error = -EFSCORRUPTED;
Linus Torvalds's avatar
Linus Torvalds committed
2305 2306
			goto done;
		}
2307
		error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
2308
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
2309
			goto done;
2310
		par_node = par_buf->b_addr;
2311
		dp->d_ops->node_hdr_from_disk(&par_hdr, par_node);
2312
		if (par_hdr.level != level) {
Linus Torvalds's avatar
Linus Torvalds committed
2313 2314
			XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
					 XFS_ERRLEVEL_LOW, mp);
2315
			error = -EFSCORRUPTED;
Linus Torvalds's avatar
Linus Torvalds committed
2316 2317
			goto done;
		}
2318
		btree = dp->d_ops->node_tree_p(par_node);
Linus Torvalds's avatar
Linus Torvalds committed
2319 2320 2321 2322 2323
		entno = 0;
	}
	/*
	 * Update the parent entry pointing to the moved block.
	 */
2324
	btree[entno].before = cpu_to_be32(dead_blkno);
2325
	xfs_trans_log_buf(tp, par_buf,
2326 2327
		XFS_DA_LOGRANGE(par_node, &btree[entno].before,
				sizeof(btree[entno].before)));
Linus Torvalds's avatar
Linus Torvalds committed
2328 2329 2330 2331 2332
	*dead_blknop = last_blkno;
	*dead_bufp = last_buf;
	return 0;
done:
	if (par_buf)
2333
		xfs_trans_brelse(tp, par_buf);
Linus Torvalds's avatar
Linus Torvalds committed
2334
	if (sib_buf)
2335 2336
		xfs_trans_brelse(tp, sib_buf);
	xfs_trans_brelse(tp, last_buf);
Linus Torvalds's avatar
Linus Torvalds committed
2337 2338 2339 2340 2341 2342 2343
	return error;
}

/*
 * Remove a btree block from a directory or attribute.
 */
int
2344 2345 2346 2347
xfs_da_shrink_inode(
	xfs_da_args_t	*args,
	xfs_dablk_t	dead_blkno,
	struct xfs_buf	*dead_buf)
Linus Torvalds's avatar
Linus Torvalds committed
2348 2349 2350 2351 2352
{
	xfs_inode_t *dp;
	int done, error, w, count;
	xfs_trans_t *tp;

2353 2354
	trace_xfs_da_shrink_inode(args);

Linus Torvalds's avatar
Linus Torvalds committed
2355 2356 2357
	dp = args->dp;
	w = args->whichfork;
	tp = args->trans;
2358
	count = args->geo->fsbcount;
Linus Torvalds's avatar
Linus Torvalds committed
2359 2360 2361 2362 2363
	for (;;) {
		/*
		 * Remove extents.  If we get ENOSPC for a dir we have to move
		 * the last block to the place we want to kill.
		 */
2364
		error = xfs_bunmapi(tp, dp, dead_blkno, count,
2365 2366
				    xfs_bmapi_aflag(w), 0, args->firstblock,
				    args->flist, &done);
2367
		if (error == -ENOSPC) {
Linus Torvalds's avatar
Linus Torvalds committed
2368
			if (w != XFS_DATA_FORK)
2369
				break;
2370 2371 2372
			error = xfs_da3_swap_lastblock(args, &dead_blkno,
						      &dead_buf);
			if (error)
2373 2374
				break;
		} else {
Linus Torvalds's avatar
Linus Torvalds committed
2375 2376 2377
			break;
		}
	}
2378
	xfs_trans_binval(tp, dead_buf);
Linus Torvalds's avatar
Linus Torvalds committed
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
	return error;
}

/*
 * See if the mapping(s) for this btree block are valid, i.e.
 * don't contain holes, are logically contiguous, and cover the whole range.
 */
STATIC int
xfs_da_map_covers_blocks(
	int		nmap,
	xfs_bmbt_irec_t	*mapp,
	xfs_dablk_t	bno,
	int		count)
{
	int		i;
	xfs_fileoff_t	off;

	for (i = 0, off = bno; i < nmap; i++) {
		if (mapp[i].br_startblock == HOLESTARTBLOCK ||
		    mapp[i].br_startblock == DELAYSTARTBLOCK) {
			return 0;
		}
		if (off != mapp[i].br_startoff) {
			return 0;
		}
		off += mapp[i].br_blockcount;
	}
	return off == bno + count;
}

/*
2410 2411 2412 2413 2414 2415
 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
 *
 * For the single map case, it is assumed that the caller has provided a pointer
 * to a valid xfs_buf_map.  For the multiple map case, this function will
 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
 * map pointer with the allocated map.
Linus Torvalds's avatar
Linus Torvalds committed
2416
 */
2417 2418 2419 2420
static int
xfs_buf_map_from_irec(
	struct xfs_mount	*mp,
	struct xfs_buf_map	**mapp,
2421
	int			*nmaps,
2422
	struct xfs_bmbt_irec	*irecs,
2423
	int			nirecs)
Linus Torvalds's avatar
Linus Torvalds committed
2424
{
2425 2426 2427 2428 2429 2430 2431
	struct xfs_buf_map	*map;
	int			i;

	ASSERT(*nmaps == 1);
	ASSERT(nirecs >= 1);

	if (nirecs > 1) {
2432 2433
		map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
				  KM_SLEEP | KM_NOFS);
2434
		if (!map)
2435
			return -ENOMEM;
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
		*mapp = map;
	}

	*nmaps = nirecs;
	map = *mapp;
	for (i = 0; i < *nmaps; i++) {
		ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
		       irecs[i].br_startblock != HOLESTARTBLOCK);
		map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
		map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
	}
	return 0;
}

/*
 * Map the block we are given ready for reading. There are three possible return
 * values:
 *	-1 - will be returned if we land in a hole and mappedbno == -2 so the
 *	     caller knows not to execute a subsequent read.
 *	 0 - if we mapped the block successfully
 *	>0 - positive error number if there was an error.
 */
static int
xfs_dabuf_map(
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mappedbno,
	int			whichfork,
	struct xfs_buf_map	**map,
	int			*nmaps)
{
	struct xfs_mount	*mp = dp->i_mount;
	int			nfsb;
	int			error = 0;
	struct xfs_bmbt_irec	irec;
	struct xfs_bmbt_irec	*irecs = &irec;
	int			nirecs;

	ASSERT(map && *map);
	ASSERT(*nmaps == 1);
Linus Torvalds's avatar
Linus Torvalds committed
2476

2477 2478 2479 2480
	if (whichfork == XFS_DATA_FORK)
		nfsb = mp->m_dir_geo->fsbcount;
	else
		nfsb = mp->m_attr_geo->fsbcount;
2481

Linus Torvalds's avatar
Linus Torvalds committed
2482 2483 2484 2485 2486 2487 2488 2489
	/*
	 * Caller doesn't have a mapping.  -2 means don't complain
	 * if we land in a hole.
	 */
	if (mappedbno == -1 || mappedbno == -2) {
		/*
		 * Optimize the one-block case.
		 */
2490
		if (nfsb != 1)
2491 2492
			irecs = kmem_zalloc(sizeof(irec) * nfsb,
					    KM_SLEEP | KM_NOFS);
2493

2494 2495 2496
		nirecs = nfsb;
		error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
				       &nirecs, xfs_bmapi_aflag(whichfork));
2497
		if (error)
2498
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2499
	} else {
2500 2501 2502 2503 2504
		irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
		irecs->br_startoff = (xfs_fileoff_t)bno;
		irecs->br_blockcount = nfsb;
		irecs->br_state = 0;
		nirecs = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2505
	}
2506 2507

	if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
2508 2509
		error = mappedbno == -2 ? -1 : -EFSCORRUPTED;
		if (unlikely(error == -EFSCORRUPTED)) {
Linus Torvalds's avatar
Linus Torvalds committed
2510
			if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2511
				int i;
2512 2513
				xfs_alert(mp, "%s: bno %lld dir: inode %lld",
					__func__, (long long)bno,
Linus Torvalds's avatar
Linus Torvalds committed
2514
					(long long)dp->i_ino);
2515
				for (i = 0; i < *nmaps; i++) {
2516 2517
					xfs_alert(mp,
"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
Linus Torvalds's avatar
Linus Torvalds committed
2518
						i,
2519 2520 2521 2522
						(long long)irecs[i].br_startoff,
						(long long)irecs[i].br_startblock,
						(long long)irecs[i].br_blockcount,
						irecs[i].br_state);
Linus Torvalds's avatar
Linus Torvalds committed
2523 2524 2525 2526 2527
				}
			}
			XFS_ERROR_REPORT("xfs_da_do_buf(1)",
					 XFS_ERRLEVEL_LOW, mp);
		}
2528
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2529
	}
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
	error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
out:
	if (irecs != &irec)
		kmem_free(irecs);
	return error;
}

/*
 * Get a buffer for the dir/attr block.
 */
int
xfs_da_get_buf(
	struct xfs_trans	*trans,
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mappedbno,
2546
	struct xfs_buf		**bpp,
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
	int			whichfork)
{
	struct xfs_buf		*bp;
	struct xfs_buf_map	map;
	struct xfs_buf_map	*mapp;
	int			nmap;
	int			error;

	*bpp = NULL;
	mapp = &map;
	nmap = 1;
2558
	error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2559 2560 2561 2562
				&mapp, &nmap);
	if (error) {
		/* mapping a hole is not an error, but we don't continue */
		if (error == -1)
Linus Torvalds's avatar
Linus Torvalds committed
2563
			error = 0;
2564
		goto out_free;
Linus Torvalds's avatar
Linus Torvalds committed
2565
	}
2566 2567 2568

	bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
				    mapp, nmap, 0);
2569
	error = bp ? bp->b_error : -EIO;
2570
	if (error) {
2571 2572
		if (bp)
			xfs_trans_brelse(trans, bp);
2573 2574 2575
		goto out_free;
	}

2576
	*bpp = bp;
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593

out_free:
	if (mapp != &map)
		kmem_free(mapp);

	return error;
}

/*
 * Get a buffer for the dir/attr block, fill in the contents.
 */
int
xfs_da_read_buf(
	struct xfs_trans	*trans,
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mappedbno,
2594
	struct xfs_buf		**bpp,
2595
	int			whichfork,
2596
	const struct xfs_buf_ops *ops)
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
{
	struct xfs_buf		*bp;
	struct xfs_buf_map	map;
	struct xfs_buf_map	*mapp;
	int			nmap;
	int			error;

	*bpp = NULL;
	mapp = &map;
	nmap = 1;
2607
	error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
				&mapp, &nmap);
	if (error) {
		/* mapping a hole is not an error, but we don't continue */
		if (error == -1)
			error = 0;
		goto out_free;
	}

	error = xfs_trans_read_buf_map(dp->i_mount, trans,
					dp->i_mount->m_ddev_targp,
2618
					mapp, nmap, 0, &bp, ops);
2619 2620 2621 2622 2623
	if (error)
		goto out_free;

	if (whichfork == XFS_ATTR_FORK)
		xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds's avatar
Linus Torvalds committed
2624
	else
2625
		xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2626
	*bpp = bp;
2627
out_free:
Linus Torvalds's avatar
Linus Torvalds committed
2628
	if (mapp != &map)
2629
		kmem_free(mapp);
Linus Torvalds's avatar
Linus Torvalds committed
2630

2631
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
2632 2633 2634 2635 2636 2637 2638
}

/*
 * Readahead the dir/attr block.
 */
xfs_daddr_t
xfs_da_reada_buf(
2639 2640
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
2641
	xfs_daddr_t		mappedbno,
2642
	int			whichfork,
2643
	const struct xfs_buf_ops *ops)
Linus Torvalds's avatar
Linus Torvalds committed
2644
{
2645 2646 2647 2648 2649 2650 2651
	struct xfs_buf_map	map;
	struct xfs_buf_map	*mapp;
	int			nmap;
	int			error;

	mapp = &map;
	nmap = 1;
2652
	error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2653 2654 2655 2656 2657 2658 2659
				&mapp, &nmap);
	if (error) {
		/* mapping a hole is not an error, but we don't continue */
		if (error == -1)
			error = 0;
		goto out_free;
	}
Linus Torvalds's avatar
Linus Torvalds committed
2660

2661
	mappedbno = mapp[0].bm_bn;
2662
	xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
2663 2664 2665 2666 2667 2668

out_free:
	if (mapp != &map)
		kmem_free(mapp);

	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
2669
		return -1;
2670
	return mappedbno;
Linus Torvalds's avatar
Linus Torvalds committed
2671
}