xfs_da_btree.c 69.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3
 * Copyright (c) 2013 Red Hat, Inc.
4
 * All Rights Reserved.
L
Linus Torvalds 已提交
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
L
Linus Torvalds 已提交
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.
L
Linus Torvalds 已提交
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
L
Linus Torvalds 已提交
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"
L
Linus Torvalds 已提交
26
#include "xfs_mount.h"
27
#include "xfs_da_format.h"
28
#include "xfs_da_btree.h"
29
#include "xfs_dir2.h"
C
Christoph Hellwig 已提交
30
#include "xfs_dir2_priv.h"
L
Linus Torvalds 已提交
31
#include "xfs_inode.h"
32
#include "xfs_trans.h"
33 34
#include "xfs_inode_item.h"
#include "xfs_alloc.h"
L
Linus Torvalds 已提交
35 36 37 38
#include "xfs_bmap.h"
#include "xfs_attr.h"
#include "xfs_attr_leaf.h"
#include "xfs_error.h"
C
Christoph Hellwig 已提交
39
#include "xfs_trace.h"
40 41
#include "xfs_cksum.h"
#include "xfs_buf_item.h"
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55

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

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

/*
 * Routines used for growing the Btree.
 */
56
STATIC int xfs_da3_root_split(xfs_da_state_t *state,
L
Linus Torvalds 已提交
57 58
					    xfs_da_state_blk_t *existing_root,
					    xfs_da_state_blk_t *new_child);
59
STATIC int xfs_da3_node_split(xfs_da_state_t *state,
L
Linus Torvalds 已提交
60 61 62 63 64
					    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);
65
STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
L
Linus Torvalds 已提交
66 67
					 xfs_da_state_blk_t *node_blk_1,
					 xfs_da_state_blk_t *node_blk_2);
68
STATIC void xfs_da3_node_add(xfs_da_state_t *state,
L
Linus Torvalds 已提交
69 70 71 72 73 74
				   xfs_da_state_blk_t *old_node_blk,
				   xfs_da_state_blk_t *new_node_blk);

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

/*
 * Utility routines.
 */
87
STATIC int	xfs_da3_blk_unlink(xfs_da_state_t *state,
88 89
				  xfs_da_state_blk_t *drop_blk,
				  xfs_da_state_blk_t *save_blk);
L
Linus Torvalds 已提交
90

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

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(
D
Dave Chinner 已提交
132 133 134
	struct xfs_buf		*bp)
{
	struct xfs_mount	*mp = bp->b_target->bt_mount;
135 136
	struct xfs_da_intnode	*hdr = bp->b_addr;
	struct xfs_da3_icnode_hdr ichdr;
137 138 139
	const struct xfs_dir_ops *ops;

	ops = xfs_dir_get_ops(mp, NULL);
140

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

	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;

149
		if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
150 151 152 153 154 155
			return false;
		if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
			return false;
	} else {
		if (ichdr.magic != XFS_DA_NODE_MAGIC)
			return false;
D
Dave Chinner 已提交
156
	}
157 158 159 160 161 162 163 164 165 166 167
	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
	 */
168 169
	if (ichdr.count > mp->m_dir_geo->node_ents &&
	    ichdr.count > mp->m_attr_geo->node_ents)
170 171 172
		return false;

	/* XXX: hash order check? */
D
Dave Chinner 已提交
173

174
	return true;
D
Dave Chinner 已提交
175 176 177
}

static void
178
xfs_da3_node_write_verify(
179 180
	struct xfs_buf	*bp)
{
181 182 183 184 185
	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)) {
D
Dave Chinner 已提交
186
		xfs_buf_ioerror(bp, -EFSCORRUPTED);
187
		xfs_verifier_error(bp);
188 189 190 191 192 193 194 195 196
		return;
	}

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

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

197
	xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
198 199
}

200 201 202 203 204 205
/*
 * 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.
 */
206
static void
207
xfs_da3_node_read_verify(
D
Dave Chinner 已提交
208 209 210 211 212
	struct xfs_buf		*bp)
{
	struct xfs_da_blkinfo	*info = bp->b_addr;

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

	/* corrupt block */
240
	xfs_verifier_error(bp);
D
Dave Chinner 已提交
241 242
}

243 244 245
const struct xfs_buf_ops xfs_da3_node_buf_ops = {
	.verify_read = xfs_da3_node_read_verify,
	.verify_write = xfs_da3_node_write_verify,
246 247
};

D
Dave Chinner 已提交
248
int
249
xfs_da3_node_read(
D
Dave Chinner 已提交
250 251 252 253 254 255 256
	struct xfs_trans	*tp,
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mappedbno,
	struct xfs_buf		**bpp,
	int			which_fork)
{
257 258 259
	int			err;

	err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
260
					which_fork, &xfs_da3_node_buf_ops);
261 262 263 264 265 266
	if (!err && tp) {
		struct xfs_da_blkinfo	*info = (*bpp)->b_addr;
		int			type;

		switch (be16_to_cpu(info->magic)) {
		case XFS_DA_NODE_MAGIC:
267
		case XFS_DA3_NODE_MAGIC:
268
			type = XFS_BLFT_DA_NODE_BUF;
269 270 271
			break;
		case XFS_ATTR_LEAF_MAGIC:
		case XFS_ATTR3_LEAF_MAGIC:
272
			type = XFS_BLFT_ATTR_LEAF_BUF;
273 274 275
			break;
		case XFS_DIR2_LEAFN_MAGIC:
		case XFS_DIR3_LEAFN_MAGIC:
276
			type = XFS_BLFT_DIR_LEAFN_BUF;
277 278 279 280 281 282 283 284 285
			break;
		default:
			type = 0;
			ASSERT(0);
			break;
		}
		xfs_trans_buf_set_type(tp, *bpp, type);
	}
	return err;
D
Dave Chinner 已提交
286 287
}

L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295
/*========================================================================
 * Routines used for growing the Btree.
 *========================================================================*/

/*
 * Create the initial contents of an intermediate node.
 */
int
296 297 298 299 300 301
xfs_da3_node_create(
	struct xfs_da_args	*args,
	xfs_dablk_t		blkno,
	int			level,
	struct xfs_buf		**bpp,
	int			whichfork)
L
Linus Torvalds 已提交
302
{
303 304 305 306 307 308
	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;
309
	struct xfs_inode	*dp = args->dp;
L
Linus Torvalds 已提交
310

311
	trace_xfs_da_node_create(args);
312
	ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
313

314
	error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, whichfork);
L
Linus Torvalds 已提交
315
	if (error)
E
Eric Sandeen 已提交
316
		return error;
317
	bp->b_ops = &xfs_da3_node_buf_ops;
318
	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
319
	node = bp->b_addr;
L
Linus Torvalds 已提交
320

321 322 323 324 325 326
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		struct xfs_da3_node_hdr *hdr3 = bp->b_addr;

		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);
327
		uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
328 329 330 331 332
	} else {
		ichdr.magic = XFS_DA_NODE_MAGIC;
	}
	ichdr.level = level;

333
	dp->d_ops->node_hdr_to_disk(node, &ichdr);
334
	xfs_trans_log_buf(tp, bp,
335
		XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
L
Linus Torvalds 已提交
336 337

	*bpp = bp;
E
Eric Sandeen 已提交
338
	return 0;
L
Linus Torvalds 已提交
339 340 341 342 343 344 345
}

/*
 * Split a leaf node, rebalance, then possibly split
 * intermediate nodes, rebalance, etc.
 */
int							/* error */
346 347
xfs_da3_split(
	struct xfs_da_state	*state)
L
Linus Torvalds 已提交
348
{
349 350 351 352 353 354
	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;
355
	int			action = 0;
356 357
	int			error;
	int			i;
L
Linus Torvalds 已提交
358

359 360
	trace_xfs_da_split(state->args);

L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369
	/*
	 * 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 ||
370
	       state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
L
Linus Torvalds 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384

	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:
D
Dave Chinner 已提交
385
			error = xfs_attr3_leaf_split(state, oldblk, newblk);
D
Dave Chinner 已提交
386
			if ((error != 0) && (error != -ENOSPC)) {
E
Eric Sandeen 已提交
387
				return error;	/* GROT: attr is inconsistent */
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398
			}
			if (!error) {
				addblk = newblk;
				break;
			}
			/*
			 * Entry wouldn't fit, split the leaf again.
			 */
			state->extravalid = 1;
			if (state->inleaf) {
				state->extraafter = 0;	/* before newblk */
399
				trace_xfs_attr_leaf_split_before(state->args);
D
Dave Chinner 已提交
400
				error = xfs_attr3_leaf_split(state, oldblk,
L
Linus Torvalds 已提交
401 402 403
							    &state->extrablk);
			} else {
				state->extraafter = 1;	/* after newblk */
404
				trace_xfs_attr_leaf_split_after(state->args);
D
Dave Chinner 已提交
405
				error = xfs_attr3_leaf_split(state, newblk,
L
Linus Torvalds 已提交
406 407 408
							    &state->extrablk);
			}
			if (error)
E
Eric Sandeen 已提交
409
				return error;	/* GROT: attr inconsistent */
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417 418
			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:
419
			error = xfs_da3_node_split(state, oldblk, newblk, addblk,
L
Linus Torvalds 已提交
420 421 422
							 max - i, &action);
			addblk->bp = NULL;
			if (error)
E
Eric Sandeen 已提交
423
				return error;	/* GROT: dir is inconsistent */
L
Linus Torvalds 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436
			/*
			 * 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.
		 */
437
		xfs_da3_fixhashpath(state, &state->path);
L
Linus Torvalds 已提交
438 439
	}
	if (!addblk)
E
Eric Sandeen 已提交
440
		return 0;
L
Linus Torvalds 已提交
441 442 443 444 445 446

	/*
	 * Split the root node.
	 */
	ASSERT(state->path.active == 0);
	oldblk = &state->path.blk[0];
447
	error = xfs_da3_root_split(state, oldblk, addblk);
L
Linus Torvalds 已提交
448 449
	if (error) {
		addblk->bp = NULL;
E
Eric Sandeen 已提交
450
		return error;	/* GROT: dir is inconsistent */
L
Linus Torvalds 已提交
451 452 453 454 455 456 457
	}

	/*
	 * 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.
458 459 460 461 462
	 *
	 * 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.
L
Linus Torvalds 已提交
463
	 */
464
	node = oldblk->bp->b_addr;
L
Linus Torvalds 已提交
465
	if (node->hdr.info.forw) {
466
		if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
L
Linus Torvalds 已提交
467 468 469 470 471
			bp = addblk->bp;
		} else {
			ASSERT(state->extravalid);
			bp = state->extrablk.bp;
		}
472
		node = bp->b_addr;
473
		node->hdr.info.back = cpu_to_be32(oldblk->blkno);
474
		xfs_trans_log_buf(state->args->trans, bp,
L
Linus Torvalds 已提交
475 476 477
		    XFS_DA_LOGRANGE(node, &node->hdr.info,
		    sizeof(node->hdr.info)));
	}
478
	node = oldblk->bp->b_addr;
479 480
	if (node->hdr.info.back) {
		if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
L
Linus Torvalds 已提交
481 482 483 484 485
			bp = addblk->bp;
		} else {
			ASSERT(state->extravalid);
			bp = state->extrablk.bp;
		}
486
		node = bp->b_addr;
487
		node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
488
		xfs_trans_log_buf(state->args->trans, bp,
L
Linus Torvalds 已提交
489 490 491 492
		    XFS_DA_LOGRANGE(node, &node->hdr.info,
		    sizeof(node->hdr.info)));
	}
	addblk->bp = NULL;
E
Eric Sandeen 已提交
493
	return 0;
L
Linus Torvalds 已提交
494 495 496 497 498 499 500 501
}

/*
 * 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 */
502 503 504 505
xfs_da3_root_split(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*blk1,
	struct xfs_da_state_blk	*blk2)
L
Linus Torvalds 已提交
506
{
507 508 509 510 511 512 513 514 515 516 517 518 519
	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;
L
Linus Torvalds 已提交
520

521 522
	trace_xfs_da_root_split(state->args);

L
Linus Torvalds 已提交
523 524 525 526 527 528 529
	/*
	 * 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)
530 531
		return error;

L
Linus Torvalds 已提交
532 533 534 535
	dp = args->dp;
	tp = args->trans;
	error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
	if (error)
536
		return error;
537 538
	node = bp->b_addr;
	oldroot = blk1->bp->b_addr;
539 540
	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
541
		struct xfs_da3_icnode_hdr icnodehdr;
542

543
		dp->d_ops->node_hdr_from_disk(&icnodehdr, oldroot);
D
Dave Chinner 已提交
544
		btree = dp->d_ops->node_tree_p(oldroot);
545 546
		size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
		level = icnodehdr.level;
547 548 549 550 551

		/*
		 * we are about to copy oldroot to bp, so set up the type
		 * of bp while we know exactly what it will be.
		 */
552
		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
L
Linus Torvalds 已提交
553
	} else {
554 555 556
		struct xfs_dir3_icleaf_hdr leafhdr;
		struct xfs_dir2_leaf_entry *ents;

L
Linus Torvalds 已提交
557
		leaf = (xfs_dir2_leaf_t *)oldroot;
558
		dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
559
		ents = dp->d_ops->leaf_ents_p(leaf);
560 561 562 563

		ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
		       leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
		size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
564
		level = 0;
565 566 567 568 569

		/*
		 * we are about to copy oldroot to bp, so set up the type
		 * of bp while we know exactly what it will be.
		 */
570
		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
L
Linus Torvalds 已提交
571
	}
572 573 574 575 576 577 578

	/*
	 * 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.
	 */
L
Linus Torvalds 已提交
579
	memcpy(node, oldroot, size);
580 581 582 583 584 585
	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);
	}
586
	xfs_trans_log_buf(tp, bp, 0, size - 1);
587

588
	bp->b_ops = blk1->bp->b_ops;
589
	xfs_trans_buf_copy_type(bp, blk1->bp);
L
Linus Torvalds 已提交
590 591 592 593 594 595
	blk1->bp = bp;
	blk1->blkno = blkno;

	/*
	 * Set up the new root node.
	 */
596
	error = xfs_da3_node_create(args,
597
		(args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
598
		level + 1, &bp, args->whichfork);
L
Linus Torvalds 已提交
599
	if (error)
600 601
		return error;

602
	node = bp->b_addr;
603
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
D
Dave Chinner 已提交
604
	btree = dp->d_ops->node_tree_p(node);
605 606 607 608 609
	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;
610
	dp->d_ops->node_hdr_to_disk(node, &nodehdr);
L
Linus Torvalds 已提交
611 612

#ifdef DEBUG
613 614
	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
615 616 617 618
		ASSERT(blk1->blkno >= args->geo->leafblk &&
		       blk1->blkno < args->geo->freeblk);
		ASSERT(blk2->blkno >= args->geo->leafblk &&
		       blk2->blkno < args->geo->freeblk);
L
Linus Torvalds 已提交
619 620 621 622
	}
#endif

	/* Header is already logged by xfs_da_node_create */
623
	xfs_trans_log_buf(tp, bp,
624
		XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
L
Linus Torvalds 已提交
625

626
	return 0;
L
Linus Torvalds 已提交
627 628 629 630 631 632
}

/*
 * Split the node, rebalance, then add the new entry.
 */
STATIC int						/* error */
633 634 635 636 637 638 639
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)
L
Linus Torvalds 已提交
640
{
641 642 643 644 645 646
	struct xfs_da_intnode	*node;
	struct xfs_da3_icnode_hdr nodehdr;
	xfs_dablk_t		blkno;
	int			newcount;
	int			error;
	int			useextra;
647
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
648

649 650
	trace_xfs_da_node_split(state->args);

651
	node = oldblk->bp->b_addr;
652
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
L
Linus Torvalds 已提交
653 654

	/*
655
	 * With V2 dirs the extra block is data or freespace.
L
Linus Torvalds 已提交
656
	 */
657
	useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
L
Linus Torvalds 已提交
658 659 660 661
	newcount = 1 + useextra;
	/*
	 * Do we have to split the node?
	 */
662
	if (nodehdr.count + newcount > state->args->geo->node_ents) {
L
Linus Torvalds 已提交
663 664 665 666 667 668
		/*
		 * 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)
E
Eric Sandeen 已提交
669
			return error;	/* GROT: dir is inconsistent */
L
Linus Torvalds 已提交
670

671
		error = xfs_da3_node_create(state->args, blkno, treelevel,
L
Linus Torvalds 已提交
672 673
					   &newblk->bp, state->args->whichfork);
		if (error)
E
Eric Sandeen 已提交
674
			return error;	/* GROT: dir is inconsistent */
L
Linus Torvalds 已提交
675 676
		newblk->blkno = blkno;
		newblk->magic = XFS_DA_NODE_MAGIC;
677 678
		xfs_da3_node_rebalance(state, oldblk, newblk);
		error = xfs_da3_blk_link(state, oldblk, newblk);
L
Linus Torvalds 已提交
679
		if (error)
E
Eric Sandeen 已提交
680
			return error;
L
Linus Torvalds 已提交
681 682 683 684 685 686 687 688 689
		*result = 1;
	} else {
		*result = 0;
	}

	/*
	 * Insert the new entry(s) into the correct block
	 * (updating last hashval in the process).
	 *
690
	 * xfs_da3_node_add() inserts BEFORE the given index,
L
Linus Torvalds 已提交
691 692 693 694 695 696 697
	 * 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.
	 */
698
	node = oldblk->bp->b_addr;
699
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
700
	if (oldblk->index <= nodehdr.count) {
L
Linus Torvalds 已提交
701
		oldblk->index++;
702
		xfs_da3_node_add(state, oldblk, addblk);
L
Linus Torvalds 已提交
703 704 705
		if (useextra) {
			if (state->extraafter)
				oldblk->index++;
706
			xfs_da3_node_add(state, oldblk, &state->extrablk);
L
Linus Torvalds 已提交
707 708 709 710
			state->extravalid = 0;
		}
	} else {
		newblk->index++;
711
		xfs_da3_node_add(state, newblk, addblk);
L
Linus Torvalds 已提交
712 713 714
		if (useextra) {
			if (state->extraafter)
				newblk->index++;
715
			xfs_da3_node_add(state, newblk, &state->extrablk);
L
Linus Torvalds 已提交
716 717 718 719
			state->extravalid = 0;
		}
	}

E
Eric Sandeen 已提交
720
	return 0;
L
Linus Torvalds 已提交
721 722 723 724 725 726 727 728 729
}

/*
 * 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
730 731 732 733
xfs_da3_node_rebalance(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*blk1,
	struct xfs_da_state_blk	*blk2)
L
Linus Torvalds 已提交
734
{
735 736 737 738 739 740 741 742 743 744 745 746 747
	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;
D
Dave Chinner 已提交
748
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
749

750 751
	trace_xfs_da_node_rebalance(state->args);

752 753
	node1 = blk1->bp->b_addr;
	node2 = blk2->bp->b_addr;
754 755
	dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
	dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
D
Dave Chinner 已提交
756 757
	btree1 = dp->d_ops->node_tree_p(node1);
	btree2 = dp->d_ops->node_tree_p(node2);
758

L
Linus Torvalds 已提交
759 760 761 762
	/*
	 * Figure out how many entries need to move, and in which direction.
	 * Swap the nodes around if that makes it simpler.
	 */
763 764 765 766
	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)))) {
L
Linus Torvalds 已提交
767 768 769
		tmpnode = node1;
		node1 = node2;
		node2 = tmpnode;
770 771
		dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
		dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
D
Dave Chinner 已提交
772 773
		btree1 = dp->d_ops->node_tree_p(node1);
		btree2 = dp->d_ops->node_tree_p(node2);
774
		swap = 1;
L
Linus Torvalds 已提交
775
	}
776 777

	count = (nodehdr1.count - nodehdr2.count) / 2;
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786 787
	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.
		 */
788 789
		tmp = nodehdr2.count;
		if (tmp > 0) {
L
Linus Torvalds 已提交
790
			tmp *= (uint)sizeof(xfs_da_node_entry_t);
791 792
			btree_s = &btree2[0];
			btree_d = &btree2[count];
L
Linus Torvalds 已提交
793 794 795 796 797 798 799
			memmove(btree_d, btree_s, tmp);
		}

		/*
		 * Move the req'd B-tree elements from high in node1 to
		 * low in node2.
		 */
800
		nodehdr2.count += count;
L
Linus Torvalds 已提交
801
		tmp = count * (uint)sizeof(xfs_da_node_entry_t);
802 803
		btree_s = &btree1[nodehdr1.count - count];
		btree_d = &btree2[0];
L
Linus Torvalds 已提交
804
		memcpy(btree_d, btree_s, tmp);
805
		nodehdr1.count -= count;
L
Linus Torvalds 已提交
806 807 808 809 810 811 812
	} 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);
813 814
		btree_s = &btree2[0];
		btree_d = &btree1[nodehdr1.count];
L
Linus Torvalds 已提交
815
		memcpy(btree_d, btree_s, tmp);
816 817
		nodehdr1.count += count;

818
		xfs_trans_log_buf(tp, blk1->bp,
L
Linus Torvalds 已提交
819 820 821 822 823
			XFS_DA_LOGRANGE(node1, btree_d, tmp));

		/*
		 * Move elements in node2 down to fill the hole.
		 */
824
		tmp  = nodehdr2.count - count;
L
Linus Torvalds 已提交
825
		tmp *= (uint)sizeof(xfs_da_node_entry_t);
826 827
		btree_s = &btree2[count];
		btree_d = &btree2[0];
L
Linus Torvalds 已提交
828
		memmove(btree_d, btree_s, tmp);
829
		nodehdr2.count -= count;
L
Linus Torvalds 已提交
830 831 832 833 834
	}

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

839
	dp->d_ops->node_hdr_to_disk(node2, &nodehdr2);
840
	xfs_trans_log_buf(tp, blk2->bp,
L
Linus Torvalds 已提交
841
		XFS_DA_LOGRANGE(node2, &node2->hdr,
842
				dp->d_ops->node_hdr_size +
843
				(sizeof(btree2[0]) * nodehdr2.count)));
L
Linus Torvalds 已提交
844 845 846 847 848

	/*
	 * Record the last hashval from each block for upward propagation.
	 * (note: don't use the swapped node pointers)
	 */
849 850 851
	if (swap) {
		node1 = blk1->bp->b_addr;
		node2 = blk2->bp->b_addr;
852 853
		dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
		dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
D
Dave Chinner 已提交
854 855
		btree1 = dp->d_ops->node_tree_p(node1);
		btree2 = dp->d_ops->node_tree_p(node2);
856 857 858
	}
	blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
	blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
L
Linus Torvalds 已提交
859 860 861 862

	/*
	 * Adjust the expected index for insertion.
	 */
863 864 865
	if (blk1->index >= nodehdr1.count) {
		blk2->index = blk1->index - nodehdr1.count;
		blk1->index = nodehdr1.count + 1;	/* make it invalid */
L
Linus Torvalds 已提交
866 867 868 869 870 871 872
	}
}

/*
 * Add a new entry to an intermediate node.
 */
STATIC void
873 874 875 876
xfs_da3_node_add(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*oldblk,
	struct xfs_da_state_blk	*newblk)
L
Linus Torvalds 已提交
877
{
878 879 880 881
	struct xfs_da_intnode	*node;
	struct xfs_da3_icnode_hdr nodehdr;
	struct xfs_da_node_entry *btree;
	int			tmp;
D
Dave Chinner 已提交
882
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
883

884 885
	trace_xfs_da_node_add(state->args);

886
	node = oldblk->bp->b_addr;
887
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
D
Dave Chinner 已提交
888
	btree = dp->d_ops->node_tree_p(node);
889 890

	ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
L
Linus Torvalds 已提交
891
	ASSERT(newblk->blkno != 0);
892
	if (state->args->whichfork == XFS_DATA_FORK)
893 894
		ASSERT(newblk->blkno >= state->args->geo->leafblk &&
		       newblk->blkno < state->args->geo->freeblk);
L
Linus Torvalds 已提交
895 896 897 898 899

	/*
	 * We may need to make some room before we insert the new node.
	 */
	tmp = 0;
900 901 902
	if (oldblk->index < nodehdr.count) {
		tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
		memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
L
Linus Torvalds 已提交
903
	}
904 905
	btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
	btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
906
	xfs_trans_log_buf(state->args->trans, oldblk->bp,
907 908 909 910
		XFS_DA_LOGRANGE(node, &btree[oldblk->index],
				tmp + sizeof(*btree)));

	nodehdr.count += 1;
911
	dp->d_ops->node_hdr_to_disk(node, &nodehdr);
912
	xfs_trans_log_buf(state->args->trans, oldblk->bp,
913
		XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
L
Linus Torvalds 已提交
914 915 916 917

	/*
	 * Copy the last hash value from the oldblk to propagate upwards.
	 */
918
	oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
L
Linus Torvalds 已提交
919 920 921 922 923 924 925 926 927 928 929
}

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

/*
 * Deallocate an empty leaf node, remove it from its parent,
 * possibly deallocating that block, etc...
 */
int
930 931
xfs_da3_join(
	struct xfs_da_state	*state)
L
Linus Torvalds 已提交
932
{
933 934 935 936
	struct xfs_da_state_blk	*drop_blk;
	struct xfs_da_state_blk	*save_blk;
	int			action = 0;
	int			error;
L
Linus Torvalds 已提交
937

938 939
	trace_xfs_da_join(state->args);

L
Linus Torvalds 已提交
940 941 942 943
	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 ||
944
	       drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
L
Linus Torvalds 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

	/*
	 * 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:
D
Dave Chinner 已提交
960
			error = xfs_attr3_leaf_toosmall(state, &action);
L
Linus Torvalds 已提交
961
			if (error)
E
Eric Sandeen 已提交
962
				return error;
L
Linus Torvalds 已提交
963
			if (action == 0)
E
Eric Sandeen 已提交
964
				return 0;
D
Dave Chinner 已提交
965
			xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
L
Linus Torvalds 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979
			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.
			 */
980 981 982
			xfs_da3_node_remove(state, drop_blk);
			xfs_da3_fixhashpath(state, &state->path);
			error = xfs_da3_node_toosmall(state, &action);
L
Linus Torvalds 已提交
983
			if (error)
E
Eric Sandeen 已提交
984
				return error;
L
Linus Torvalds 已提交
985 986
			if (action == 0)
				return 0;
987
			xfs_da3_node_unbalance(state, drop_blk, save_blk);
L
Linus Torvalds 已提交
988 989
			break;
		}
990 991
		xfs_da3_fixhashpath(state, &state->altpath);
		error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
L
Linus Torvalds 已提交
992 993
		xfs_da_state_kill_altpath(state);
		if (error)
E
Eric Sandeen 已提交
994
			return error;
L
Linus Torvalds 已提交
995 996 997 998
		error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
							 drop_blk->bp);
		drop_blk->bp = NULL;
		if (error)
E
Eric Sandeen 已提交
999
			return error;
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005
	}
	/*
	 * 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.
	 */
1006 1007 1008
	xfs_da3_node_remove(state, drop_blk);
	xfs_da3_fixhashpath(state, &state->path);
	error = xfs_da3_root_join(state, &state->path.blk[0]);
E
Eric Sandeen 已提交
1009
	return error;
L
Linus Torvalds 已提交
1010 1011
}

1012 1013 1014 1015 1016 1017 1018 1019
#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) ||
1020
		       magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
D
Dave Chinner 已提交
1021 1022
		       magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
		       magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1023 1024 1025 1026
	} else {
		ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
		       magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
	}
1027 1028 1029 1030 1031 1032 1033
	ASSERT(!blkinfo->forw);
	ASSERT(!blkinfo->back);
}
#else	/* !DEBUG */
#define	xfs_da_blkinfo_onlychild_validate(blkinfo, level)
#endif	/* !DEBUG */

L
Linus Torvalds 已提交
1034 1035 1036 1037 1038
/*
 * 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
1039 1040 1041
xfs_da3_root_join(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*root_blk)
L
Linus Torvalds 已提交
1042
{
1043 1044 1045 1046 1047 1048 1049
	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;
1050
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
1051

1052 1053
	trace_xfs_da_root_join(state->args);

L
Linus Torvalds 已提交
1054
	ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
1055 1056

	args = state->args;
1057
	oldroot = root_blk->bp->b_addr;
1058
	dp->d_ops->node_hdr_from_disk(&oldroothdr, oldroot);
1059 1060
	ASSERT(oldroothdr.forw == 0);
	ASSERT(oldroothdr.back == 0);
L
Linus Torvalds 已提交
1061 1062 1063 1064

	/*
	 * If the root has more than one child, then don't do anything.
	 */
1065 1066
	if (oldroothdr.count > 1)
		return 0;
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071

	/*
	 * Read in the (only) child block, then copy those bytes into
	 * the root block's buffer and free the original child block.
	 */
1072
	btree = dp->d_ops->node_tree_p(oldroot);
1073
	child = be32_to_cpu(btree[0].before);
L
Linus Torvalds 已提交
1074
	ASSERT(child != 0);
1075
	error = xfs_da3_node_read(args->trans, dp, child, -1, &bp,
D
Dave Chinner 已提交
1076
					     args->whichfork);
L
Linus Torvalds 已提交
1077
	if (error)
1078 1079
		return error;
	xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
1080

1081 1082 1083
	/*
	 * 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
1084
	 * to update the b_ops pointer as well to match the buffer type change
1085 1086
	 * that could occur. For dir3 blocks we also need to update the block
	 * number in the buffer header.
1087
	 */
1088
	memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
1089
	root_blk->bp->b_ops = bp->b_ops;
1090
	xfs_trans_buf_copy_type(root_blk->bp, bp);
1091 1092 1093 1094
	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);
	}
1095 1096
	xfs_trans_log_buf(args->trans, root_blk->bp, 0,
			  args->geo->blksize - 1);
L
Linus Torvalds 已提交
1097
	error = xfs_da_shrink_inode(args, child, bp);
E
Eric Sandeen 已提交
1098
	return error;
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
}

/*
 * 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
1111 1112 1113
xfs_da3_node_toosmall(
	struct xfs_da_state	*state,
	int			*action)
L
Linus Torvalds 已提交
1114
{
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	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;
1126
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
1127

1128 1129
	trace_xfs_da_node_toosmall(state->args);

L
Linus Torvalds 已提交
1130 1131 1132 1133 1134 1135
	/*
	 * 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 ];
1136
	info = blk->bp->b_addr;
L
Linus Torvalds 已提交
1137
	node = (xfs_da_intnode_t *)info;
1138
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1139
	if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
L
Linus Torvalds 已提交
1140
		*action = 0;	/* blk over 50%, don't try to join */
E
Eric Sandeen 已提交
1141
		return 0;	/* blk over 50%, don't try to join */
L
Linus Torvalds 已提交
1142 1143 1144 1145 1146
	}

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

	/*
	 * 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.
	 */
1176 1177
	count  = state->args->geo->node_ents;
	count -= state->args->geo->node_ents >> 2;
1178 1179
	count -= nodehdr.count;

L
Linus Torvalds 已提交
1180
	/* start with smaller blk num */
1181
	forward = nodehdr.forw < nodehdr.back;
L
Linus Torvalds 已提交
1182
	for (i = 0; i < 2; forward = !forward, i++) {
1183
		struct xfs_da3_icnode_hdr thdr;
L
Linus Torvalds 已提交
1184
		if (forward)
1185
			blkno = nodehdr.forw;
L
Linus Torvalds 已提交
1186
		else
1187
			blkno = nodehdr.back;
L
Linus Torvalds 已提交
1188 1189
		if (blkno == 0)
			continue;
1190
		error = xfs_da3_node_read(state->args->trans, dp,
D
Dave Chinner 已提交
1191
					blkno, -1, &bp, state->args->whichfork);
L
Linus Torvalds 已提交
1192
		if (error)
E
Eric Sandeen 已提交
1193
			return error;
L
Linus Torvalds 已提交
1194

1195
		node = bp->b_addr;
1196
		dp->d_ops->node_hdr_from_disk(&thdr, node);
1197
		xfs_trans_brelse(state->args->trans, bp);
1198

1199
		if (count - thdr.count >= 0)
L
Linus Torvalds 已提交
1200 1201 1202 1203
			break;	/* fits with at least 25% to spare */
	}
	if (i >= 2) {
		*action = 0;
1204
		return 0;
L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211 1212
	}

	/*
	 * 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) {
1213
		error = xfs_da3_path_shift(state, &state->altpath, forward,
L
Linus Torvalds 已提交
1214 1215
						 0, &retval);
	} else {
1216
		error = xfs_da3_path_shift(state, &state->path, forward,
L
Linus Torvalds 已提交
1217
						 0, &retval);
1218 1219 1220 1221 1222 1223
	}
	if (error)
		return error;
	if (retval) {
		*action = 0;
		return 0;
L
Linus Torvalds 已提交
1224 1225
	}
	*action = 1;
1226 1227 1228 1229 1230 1231 1232 1233
	return 0;
}

/*
 * Pick up the last hashvalue from an intermediate node.
 */
STATIC uint
xfs_da3_node_lasthash(
D
Dave Chinner 已提交
1234
	struct xfs_inode	*dp,
1235 1236 1237 1238 1239 1240 1241 1242
	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;
1243
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1244 1245 1246 1247
	if (count)
		*count = nodehdr.count;
	if (!nodehdr.count)
		return 0;
D
Dave Chinner 已提交
1248
	btree = dp->d_ops->node_tree_p(node);
1249
	return be32_to_cpu(btree[nodehdr.count - 1].hashval);
L
Linus Torvalds 已提交
1250 1251 1252 1253 1254 1255 1256
}

/*
 * Walk back up the tree adjusting hash values as necessary,
 * when we stop making changes, return.
 */
void
1257 1258 1259
xfs_da3_fixhashpath(
	struct xfs_da_state	*state,
	struct xfs_da_state_path *path)
L
Linus Torvalds 已提交
1260
{
1261 1262 1263 1264 1265 1266
	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;
D
Dave Chinner 已提交
1267
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
1268

1269 1270
	trace_xfs_da_fixhashpath(state->args);

L
Linus Torvalds 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279
	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:
D
Dave Chinner 已提交
1280
		lasthash = xfs_dir2_leafn_lasthash(dp, blk->bp, &count);
L
Linus Torvalds 已提交
1281 1282 1283 1284
		if (count == 0)
			return;
		break;
	case XFS_DA_NODE_MAGIC:
D
Dave Chinner 已提交
1285
		lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
L
Linus Torvalds 已提交
1286 1287 1288 1289 1290
		if (count == 0)
			return;
		break;
	}
	for (blk--, level--; level >= 0; blk--, level--) {
1291 1292
		struct xfs_da3_icnode_hdr nodehdr;

1293
		node = blk->bp->b_addr;
1294
		dp->d_ops->node_hdr_from_disk(&nodehdr, node);
D
Dave Chinner 已提交
1295
		btree = dp->d_ops->node_tree_p(node);
1296
		if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
L
Linus Torvalds 已提交
1297 1298
			break;
		blk->hashval = lasthash;
1299
		btree[blk->index].hashval = cpu_to_be32(lasthash);
1300
		xfs_trans_log_buf(state->args->trans, blk->bp,
1301 1302
				  XFS_DA_LOGRANGE(node, &btree[blk->index],
						  sizeof(*btree)));
L
Linus Torvalds 已提交
1303

1304
		lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
L
Linus Torvalds 已提交
1305 1306 1307 1308 1309 1310 1311
	}
}

/*
 * Remove an entry from an intermediate node.
 */
STATIC void
1312 1313 1314
xfs_da3_node_remove(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*drop_blk)
L
Linus Torvalds 已提交
1315
{
1316 1317 1318 1319 1320
	struct xfs_da_intnode	*node;
	struct xfs_da3_icnode_hdr nodehdr;
	struct xfs_da_node_entry *btree;
	int			index;
	int			tmp;
D
Dave Chinner 已提交
1321
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
1322

1323 1324
	trace_xfs_da_node_remove(state->args);

1325
	node = drop_blk->bp->b_addr;
1326
	dp->d_ops->node_hdr_from_disk(&nodehdr, node);
1327
	ASSERT(drop_blk->index < nodehdr.count);
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332
	ASSERT(drop_blk->index >= 0);

	/*
	 * Copy over the offending entry, or just zero it out.
	 */
1333
	index = drop_blk->index;
D
Dave Chinner 已提交
1334
	btree = dp->d_ops->node_tree_p(node);
1335 1336
	if (index < nodehdr.count - 1) {
		tmp  = nodehdr.count - index - 1;
L
Linus Torvalds 已提交
1337
		tmp *= (uint)sizeof(xfs_da_node_entry_t);
1338
		memmove(&btree[index], &btree[index + 1], tmp);
1339
		xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1340 1341
		    XFS_DA_LOGRANGE(node, &btree[index], tmp));
		index = nodehdr.count - 1;
L
Linus Torvalds 已提交
1342
	}
1343
	memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
1344
	xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1345 1346
	    XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
	nodehdr.count -= 1;
1347
	dp->d_ops->node_hdr_to_disk(node, &nodehdr);
1348
	xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1349
	    XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
L
Linus Torvalds 已提交
1350 1351 1352 1353

	/*
	 * Copy the last hash value from the block to propagate upwards.
	 */
1354
	drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
L
Linus Torvalds 已提交
1355 1356 1357
}

/*
1358
 * Unbalance the elements between two intermediate nodes,
L
Linus Torvalds 已提交
1359 1360 1361
 * move all Btree elements from one node into another.
 */
STATIC void
1362 1363 1364 1365
xfs_da3_node_unbalance(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*drop_blk,
	struct xfs_da_state_blk	*save_blk)
L
Linus Torvalds 已提交
1366
{
1367 1368 1369 1370 1371 1372 1373 1374 1375
	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;
D
Dave Chinner 已提交
1376
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
1377

1378 1379
	trace_xfs_da_node_unbalance(state->args);

1380 1381
	drop_node = drop_blk->bp->b_addr;
	save_node = save_blk->bp->b_addr;
1382 1383
	dp->d_ops->node_hdr_from_disk(&drop_hdr, drop_node);
	dp->d_ops->node_hdr_from_disk(&save_hdr, save_node);
D
Dave Chinner 已提交
1384 1385
	drop_btree = dp->d_ops->node_tree_p(drop_node);
	save_btree = dp->d_ops->node_tree_p(save_node);
L
Linus Torvalds 已提交
1386 1387 1388 1389 1390 1391
	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.
	 */
1392 1393 1394 1395 1396 1397 1398 1399 1400
	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;
1401
		xfs_trans_log_buf(tp, save_blk->bp,
1402 1403 1404
			XFS_DA_LOGRANGE(save_node, &save_btree[0],
				(save_hdr.count + drop_hdr.count) *
						sizeof(xfs_da_node_entry_t)));
L
Linus Torvalds 已提交
1405
	} else {
1406
		sindex = save_hdr.count;
1407
		xfs_trans_log_buf(tp, save_blk->bp,
1408 1409
			XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
				drop_hdr.count * sizeof(xfs_da_node_entry_t)));
L
Linus Torvalds 已提交
1410 1411 1412 1413 1414
	}

	/*
	 * Move all the B-tree elements from drop_blk to save_blk.
	 */
1415 1416 1417
	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;
L
Linus Torvalds 已提交
1418

1419
	dp->d_ops->node_hdr_to_disk(save_node, &save_hdr);
1420
	xfs_trans_log_buf(tp, save_blk->bp,
L
Linus Torvalds 已提交
1421
		XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1422
				dp->d_ops->node_hdr_size));
L
Linus Torvalds 已提交
1423 1424 1425 1426

	/*
	 * Save the last hashval in the remaining block for upward propagation.
	 */
1427
	save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
L
Linus Torvalds 已提交
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
}

/*========================================================================
 * 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 */
1446 1447 1448
xfs_da3_node_lookup_int(
	struct xfs_da_state	*state,
	int			*result)
L
Linus Torvalds 已提交
1449
{
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
	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;
D
Dave Chinner 已提交
1464
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
1465 1466 1467 1468 1469 1470 1471

	args = state->args;

	/*
	 * Descend thru the B-tree searching each level for the right
	 * node to use, until the right hashval is found.
	 */
1472
	blkno = (args->whichfork == XFS_DATA_FORK)? args->geo->leafblk : 0;
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477 1478 1479
	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;
1480
		error = xfs_da3_node_read(args->trans, args->dp, blkno,
D
Dave Chinner 已提交
1481
					-1, &blk->bp, args->whichfork);
L
Linus Torvalds 已提交
1482 1483 1484
		if (error) {
			blk->blkno = 0;
			state->path.active--;
E
Eric Sandeen 已提交
1485
			return error;
L
Linus Torvalds 已提交
1486
		}
1487
		curr = blk->bp->b_addr;
1488
		blk->magic = be16_to_cpu(curr->magic);
1489

D
Dave Chinner 已提交
1490 1491 1492
		if (blk->magic == XFS_ATTR_LEAF_MAGIC ||
		    blk->magic == XFS_ATTR3_LEAF_MAGIC) {
			blk->magic = XFS_ATTR_LEAF_MAGIC;
1493 1494 1495 1496 1497 1498 1499
			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;
1500 1501
			blk->hashval = xfs_dir2_leafn_lasthash(args->dp,
							       blk->bp, NULL);
1502 1503 1504 1505 1506
			break;
		}

		blk->magic = XFS_DA_NODE_MAGIC;

L
Linus Torvalds 已提交
1507 1508 1509 1510

		/*
		 * Search an intermediate node for a match.
		 */
1511
		node = blk->bp->b_addr;
1512
		dp->d_ops->node_hdr_from_disk(&nodehdr, node);
D
Dave Chinner 已提交
1513
		btree = dp->d_ops->node_tree_p(node);
L
Linus Torvalds 已提交
1514

1515 1516
		max = nodehdr.count;
		blk->hashval = be32_to_cpu(btree[max - 1].hashval);
L
Linus Torvalds 已提交
1517

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
		/*
		 * 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));
L
Linus Torvalds 已提交
1536

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
		/*
		 * 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);
L
Linus Torvalds 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
		}
	}

	/*
	 * 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 (;;) {
1569
		if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
L
Linus Torvalds 已提交
1570 1571
			retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
							&blk->index, state);
1572
		} else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
D
Dave Chinner 已提交
1573
			retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
L
Linus Torvalds 已提交
1574 1575
			blk->index = args->index;
			args->blkno = blk->blkno;
1576 1577
		} else {
			ASSERT(0);
D
Dave Chinner 已提交
1578
			return -EFSCORRUPTED;
L
Linus Torvalds 已提交
1579
		}
D
Dave Chinner 已提交
1580
		if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
L
Linus Torvalds 已提交
1581
		    (blk->hashval == args->hashval)) {
1582
			error = xfs_da3_path_shift(state, &state->path, 1, 1,
L
Linus Torvalds 已提交
1583 1584
							 &retval);
			if (error)
E
Eric Sandeen 已提交
1585
				return error;
L
Linus Torvalds 已提交
1586 1587
			if (retval == 0) {
				continue;
1588
			} else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
L
Linus Torvalds 已提交
1589
				/* path_shift() gives ENOENT */
D
Dave Chinner 已提交
1590
				retval = -ENOATTR;
L
Linus Torvalds 已提交
1591 1592 1593 1594 1595
			}
		}
		break;
	}
	*result = retval;
E
Eric Sandeen 已提交
1596
	return 0;
L
Linus Torvalds 已提交
1597 1598 1599 1600 1601 1602
}

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

1603 1604 1605 1606 1607
/*
 * Compare two intermediate nodes for "order".
 */
STATIC int
xfs_da3_node_order(
D
Dave Chinner 已提交
1608
	struct xfs_inode *dp,
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
	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;
1621 1622
	dp->d_ops->node_hdr_from_disk(&node1hdr, node1);
	dp->d_ops->node_hdr_from_disk(&node2hdr, node2);
D
Dave Chinner 已提交
1623 1624
	btree1 = dp->d_ops->node_tree_p(node1);
	btree2 = dp->d_ops->node_tree_p(node2);
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634

	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;
}

L
Linus Torvalds 已提交
1635 1636 1637 1638
/*
 * Link a new block into a doubly linked list of blocks (of whatever type).
 */
int							/* error */
1639 1640 1641 1642
xfs_da3_blk_link(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*old_blk,
	struct xfs_da_state_blk	*new_blk)
L
Linus Torvalds 已提交
1643
{
1644 1645 1646 1647 1648 1649 1650
	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;
D
Dave Chinner 已提交
1651
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
1652 1653 1654 1655 1656 1657

	/*
	 * Set up environment.
	 */
	args = state->args;
	ASSERT(args != NULL);
1658 1659
	old_info = old_blk->bp->b_addr;
	new_info = new_blk->bp->b_addr;
L
Linus Torvalds 已提交
1660
	ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
1661
	       old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
L
Linus Torvalds 已提交
1662 1663 1664 1665 1666 1667 1668
	       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:
D
Dave Chinner 已提交
1669
		before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
L
Linus Torvalds 已提交
1670 1671
		break;
	case XFS_DA_NODE_MAGIC:
D
Dave Chinner 已提交
1672
		before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
L
Linus Torvalds 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
		break;
	}

	/*
	 * Link blocks in appropriate order.
	 */
	if (before) {
		/*
		 * Link new block in before existing block.
		 */
1683
		trace_xfs_da_link_before(args);
1684 1685 1686
		new_info->forw = cpu_to_be32(old_blk->blkno);
		new_info->back = old_info->back;
		if (old_info->back) {
D
Dave Chinner 已提交
1687
			error = xfs_da3_node_read(args->trans, dp,
1688
						be32_to_cpu(old_info->back),
D
Dave Chinner 已提交
1689
						-1, &bp, args->whichfork);
L
Linus Torvalds 已提交
1690
			if (error)
E
Eric Sandeen 已提交
1691
				return error;
L
Linus Torvalds 已提交
1692
			ASSERT(bp != NULL);
1693
			tmp_info = bp->b_addr;
1694
			ASSERT(tmp_info->magic == old_info->magic);
1695 1696
			ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
			tmp_info->forw = cpu_to_be32(new_blk->blkno);
1697
			xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
L
Linus Torvalds 已提交
1698
		}
1699
		old_info->back = cpu_to_be32(new_blk->blkno);
L
Linus Torvalds 已提交
1700 1701 1702 1703
	} else {
		/*
		 * Link new block in after existing block.
		 */
1704
		trace_xfs_da_link_after(args);
1705 1706 1707
		new_info->forw = old_info->forw;
		new_info->back = cpu_to_be32(old_blk->blkno);
		if (old_info->forw) {
D
Dave Chinner 已提交
1708
			error = xfs_da3_node_read(args->trans, dp,
1709
						be32_to_cpu(old_info->forw),
D
Dave Chinner 已提交
1710
						-1, &bp, args->whichfork);
L
Linus Torvalds 已提交
1711
			if (error)
E
Eric Sandeen 已提交
1712
				return error;
L
Linus Torvalds 已提交
1713
			ASSERT(bp != NULL);
1714
			tmp_info = bp->b_addr;
1715 1716 1717
			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);
1718
			xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
L
Linus Torvalds 已提交
1719
		}
1720
		old_info->forw = cpu_to_be32(new_blk->blkno);
L
Linus Torvalds 已提交
1721 1722
	}

1723 1724
	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);
E
Eric Sandeen 已提交
1725
	return 0;
L
Linus Torvalds 已提交
1726 1727 1728 1729 1730
}

/*
 * Unlink a block from a doubly linked list of blocks.
 */
1731
STATIC int						/* error */
1732 1733 1734 1735
xfs_da3_blk_unlink(
	struct xfs_da_state	*state,
	struct xfs_da_state_blk	*drop_blk,
	struct xfs_da_state_blk	*save_blk)
L
Linus Torvalds 已提交
1736
{
1737 1738 1739 1740 1741 1742
	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;
L
Linus Torvalds 已提交
1743 1744 1745 1746 1747 1748

	/*
	 * Set up environment.
	 */
	args = state->args;
	ASSERT(args != NULL);
1749 1750
	save_info = save_blk->bp->b_addr;
	drop_info = drop_blk->bp->b_addr;
L
Linus Torvalds 已提交
1751
	ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
1752
	       save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
L
Linus Torvalds 已提交
1753 1754
	       save_blk->magic == XFS_ATTR_LEAF_MAGIC);
	ASSERT(save_blk->magic == drop_blk->magic);
1755 1756 1757 1758
	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));
L
Linus Torvalds 已提交
1759 1760 1761 1762

	/*
	 * Unlink the leaf block from the doubly linked chain of leaves.
	 */
1763
	if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
1764
		trace_xfs_da_unlink_back(args);
1765 1766
		save_info->back = drop_info->back;
		if (drop_info->back) {
1767
			error = xfs_da3_node_read(args->trans, args->dp,
1768
						be32_to_cpu(drop_info->back),
D
Dave Chinner 已提交
1769
						-1, &bp, args->whichfork);
L
Linus Torvalds 已提交
1770
			if (error)
E
Eric Sandeen 已提交
1771
				return error;
L
Linus Torvalds 已提交
1772
			ASSERT(bp != NULL);
1773
			tmp_info = bp->b_addr;
1774 1775 1776
			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);
1777
			xfs_trans_log_buf(args->trans, bp, 0,
L
Linus Torvalds 已提交
1778 1779 1780
						    sizeof(*tmp_info) - 1);
		}
	} else {
1781
		trace_xfs_da_unlink_forward(args);
1782 1783
		save_info->forw = drop_info->forw;
		if (drop_info->forw) {
1784
			error = xfs_da3_node_read(args->trans, args->dp,
1785
						be32_to_cpu(drop_info->forw),
D
Dave Chinner 已提交
1786
						-1, &bp, args->whichfork);
L
Linus Torvalds 已提交
1787
			if (error)
E
Eric Sandeen 已提交
1788
				return error;
L
Linus Torvalds 已提交
1789
			ASSERT(bp != NULL);
1790
			tmp_info = bp->b_addr;
1791 1792 1793
			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);
1794
			xfs_trans_log_buf(args->trans, bp, 0,
L
Linus Torvalds 已提交
1795 1796 1797 1798
						    sizeof(*tmp_info) - 1);
		}
	}

1799
	xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
E
Eric Sandeen 已提交
1800
	return 0;
L
Linus Torvalds 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
}

/*
 * 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 */
1812 1813 1814 1815 1816 1817
xfs_da3_path_shift(
	struct xfs_da_state	*state,
	struct xfs_da_state_path *path,
	int			forward,
	int			release,
	int			*result)
L
Linus Torvalds 已提交
1818
{
1819 1820 1821 1822 1823 1824 1825 1826 1827
	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;
	xfs_dablk_t		blkno = 0;
	int			level;
	int			error;
D
Dave Chinner 已提交
1828
	struct xfs_inode	*dp = state->args->dp;
L
Linus Torvalds 已提交
1829

1830 1831
	trace_xfs_da_path_shift(state->args);

L
Linus Torvalds 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
	/*
	 * 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--) {
1843
		node = blk->bp->b_addr;
1844
		dp->d_ops->node_hdr_from_disk(&nodehdr, node);
D
Dave Chinner 已提交
1845
		btree = dp->d_ops->node_tree_p(node);
1846 1847

		if (forward && (blk->index < nodehdr.count - 1)) {
L
Linus Torvalds 已提交
1848
			blk->index++;
1849
			blkno = be32_to_cpu(btree[blk->index].before);
L
Linus Torvalds 已提交
1850 1851 1852
			break;
		} else if (!forward && (blk->index > 0)) {
			blk->index--;
1853
			blkno = be32_to_cpu(btree[blk->index].before);
L
Linus Torvalds 已提交
1854 1855 1856 1857
			break;
		}
	}
	if (level < 0) {
D
Dave Chinner 已提交
1858
		*result = -ENOENT;	/* we're out of our tree */
1859
		ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
E
Eric Sandeen 已提交
1860
		return 0;
L
Linus Torvalds 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
	}

	/*
	 * 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++) {
		/*
		 * Release the old block.
		 * (if it's dirty, trans won't actually let go)
		 */
		if (release)
1873
			xfs_trans_brelse(args->trans, blk->bp);
L
Linus Torvalds 已提交
1874 1875 1876 1877 1878

		/*
		 * Read the next child block.
		 */
		blk->blkno = blkno;
D
Dave Chinner 已提交
1879
		error = xfs_da3_node_read(args->trans, dp, blkno, -1,
D
Dave Chinner 已提交
1880
					&blk->bp, args->whichfork);
L
Linus Torvalds 已提交
1881
		if (error)
E
Eric Sandeen 已提交
1882
			return error;
1883
		info = blk->bp->b_addr;
1884
		ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1885
		       info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
1886
		       info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1887
		       info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
D
Dave Chinner 已提交
1888 1889
		       info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
		       info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899


		/*
		 * 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;
L
Linus Torvalds 已提交
1900
			node = (xfs_da_intnode_t *)info;
1901
			dp->d_ops->node_hdr_from_disk(&nodehdr, node);
D
Dave Chinner 已提交
1902
			btree = dp->d_ops->node_tree_p(node);
1903
			blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
L
Linus Torvalds 已提交
1904 1905 1906
			if (forward)
				blk->index = 0;
			else
1907 1908 1909 1910
				blk->index = nodehdr.count - 1;
			blkno = be32_to_cpu(btree[blk->index].before);
			break;
		case XFS_ATTR_LEAF_MAGIC:
D
Dave Chinner 已提交
1911
		case XFS_ATTR3_LEAF_MAGIC:
1912
			blk->magic = XFS_ATTR_LEAF_MAGIC;
L
Linus Torvalds 已提交
1913 1914
			ASSERT(level == path->active-1);
			blk->index = 0;
1915
			blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1916 1917 1918 1919 1920 1921
			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;
1922 1923
			blk->hashval = xfs_dir2_leafn_lasthash(args->dp,
							       blk->bp, NULL);
1924 1925 1926 1927
			break;
		default:
			ASSERT(0);
			break;
L
Linus Torvalds 已提交
1928 1929 1930
		}
	}
	*result = 0;
1931
	return 0;
L
Linus Torvalds 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
}


/*========================================================================
 * 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
1945
xfs_da_hashname(const __uint8_t *name, int namelen)
L
Linus Torvalds 已提交
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
{
	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);
1967
	default: /* case 0: */
L
Linus Torvalds 已提交
1968 1969 1970 1971
		return hash;
	}
}

1972 1973 1974
enum xfs_dacmp
xfs_da_compname(
	struct xfs_da_args *args,
1975 1976
	const unsigned char *name,
	int		len)
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
{
	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
};

L
Linus Torvalds 已提交
1994
int
1995 1996 1997 1998
xfs_da_grow_inode_int(
	struct xfs_da_args	*args,
	xfs_fileoff_t		*bno,
	int			count)
L
Linus Torvalds 已提交
1999
{
2000 2001 2002
	struct xfs_trans	*tp = args->trans;
	struct xfs_inode	*dp = args->dp;
	int			w = args->whichfork;
C
Christoph Hellwig 已提交
2003
	xfs_rfsblock_t		nblks = dp->i_d.di_nblocks;
2004 2005
	struct xfs_bmbt_irec	map, *mapp;
	int			nmap, error, got, i, mapi;
L
Linus Torvalds 已提交
2006 2007 2008 2009

	/*
	 * Find a spot in the file space to put the new block.
	 */
2010 2011
	error = xfs_bmap_first_unused(tp, dp, count, bno, w);
	if (error)
L
Linus Torvalds 已提交
2012
		return error;
2013

L
Linus Torvalds 已提交
2014 2015 2016 2017 2018
	/*
	 * Try mapping it in one filesystem block.
	 */
	nmap = 1;
	ASSERT(args->firstblock != NULL);
2019 2020
	error = xfs_bmapi_write(tp, dp, *bno, count,
			xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
L
Linus Torvalds 已提交
2021
			args->firstblock, args->total, &map, &nmap,
2022 2023
			args->flist);
	if (error)
L
Linus Torvalds 已提交
2024
		return error;
2025

L
Linus Torvalds 已提交
2026 2027 2028 2029
	ASSERT(nmap <= 1);
	if (nmap == 1) {
		mapp = &map;
		mapi = 1;
2030 2031 2032 2033 2034 2035 2036 2037
	} 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.
		 */
L
Linus Torvalds 已提交
2038
		mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
2039
		for (b = *bno, mapi = 0; b < *bno + count; ) {
L
Linus Torvalds 已提交
2040
			nmap = MIN(XFS_BMAP_MAX_NMAP, count);
2041
			c = (int)(*bno + count - b);
2042 2043
			error = xfs_bmapi_write(tp, dp, b, c,
					xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
L
Linus Torvalds 已提交
2044
					args->firstblock, args->total,
2045 2046 2047
					&mapp[mapi], &nmap, args->flist);
			if (error)
				goto out_free_map;
L
Linus Torvalds 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
			if (nmap < 1)
				break;
			mapi += nmap;
			b = mapp[mapi - 1].br_startoff +
			    mapp[mapi - 1].br_blockcount;
		}
	} else {
		mapi = 0;
		mapp = NULL;
	}
2058

L
Linus Torvalds 已提交
2059 2060 2061 2062 2063
	/*
	 * Count the blocks we got, make sure it matches the total.
	 */
	for (i = 0, got = 0; i < mapi; i++)
		got += mapp[i].br_blockcount;
2064
	if (got != count || mapp[0].br_startoff != *bno ||
L
Linus Torvalds 已提交
2065
	    mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
2066
	    *bno + count) {
D
Dave Chinner 已提交
2067
		error = -ENOSPC;
2068
		goto out_free_map;
L
Linus Torvalds 已提交
2069
	}
2070

2071 2072
	/* account for newly allocated blocks in reserved blocks total */
	args->total -= dp->i_d.di_nblocks - nblks;
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091

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;

2092 2093
	trace_xfs_da_grow_inode(args);

2094 2095
	bno = args->geo->leafblk;
	error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
2096 2097 2098
	if (!error)
		*new_blkno = (xfs_dablk_t)bno;
	return error;
L
Linus Torvalds 已提交
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
}

/*
 * 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
2110 2111 2112 2113
xfs_da3_swap_lastblock(
	struct xfs_da_args	*args,
	xfs_dablk_t		*dead_blknop,
	struct xfs_buf		**dead_bufp)
L
Linus Torvalds 已提交
2114
{
2115 2116 2117 2118 2119 2120 2121
	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;
D
Dave Chinner 已提交
2122
	struct xfs_inode	*dp;
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
	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;
L
Linus Torvalds 已提交
2140

2141 2142
	trace_xfs_da_swap_lastblock(args);

L
Linus Torvalds 已提交
2143 2144 2145
	dead_buf = *dead_bufp;
	dead_blkno = *dead_blknop;
	tp = args->trans;
D
Dave Chinner 已提交
2146
	dp = args->dp;
L
Linus Torvalds 已提交
2147 2148
	w = args->whichfork;
	ASSERT(w == XFS_DATA_FORK);
D
Dave Chinner 已提交
2149
	mp = dp->i_mount;
2150
	lastoff = args->geo->freeblk;
D
Dave Chinner 已提交
2151
	error = xfs_bmap_last_before(tp, dp, &lastoff, w);
L
Linus Torvalds 已提交
2152 2153 2154 2155 2156
	if (error)
		return error;
	if (unlikely(lastoff == 0)) {
		XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
				 mp);
D
Dave Chinner 已提交
2157
		return -EFSCORRUPTED;
L
Linus Torvalds 已提交
2158 2159 2160 2161
	}
	/*
	 * Read the last block in the btree space.
	 */
2162
	last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
D
Dave Chinner 已提交
2163
	error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w);
2164
	if (error)
L
Linus Torvalds 已提交
2165 2166 2167 2168
		return error;
	/*
	 * Copy the last block into the dead buffer and log it.
	 */
2169 2170
	memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
	xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
2171
	dead_info = dead_buf->b_addr;
L
Linus Torvalds 已提交
2172 2173 2174
	/*
	 * Get values from the moved block.
	 */
2175 2176 2177 2178 2179
	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;

L
Linus Torvalds 已提交
2180
		dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
2181
		dp->d_ops->leaf_hdr_from_disk(&leafhdr, dead_leaf2);
D
Dave Chinner 已提交
2182
		ents = dp->d_ops->leaf_ents_p(dead_leaf2);
L
Linus Torvalds 已提交
2183
		dead_level = 0;
2184
		dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
L
Linus Torvalds 已提交
2185
	} else {
2186 2187
		struct xfs_da3_icnode_hdr deadhdr;

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

/*
 * Remove a btree block from a directory or attribute.
 */
int
2333 2334 2335 2336
xfs_da_shrink_inode(
	xfs_da_args_t	*args,
	xfs_dablk_t	dead_blkno,
	struct xfs_buf	*dead_buf)
L
Linus Torvalds 已提交
2337 2338 2339 2340 2341
{
	xfs_inode_t *dp;
	int done, error, w, count;
	xfs_trans_t *tp;

2342 2343
	trace_xfs_da_shrink_inode(args);

L
Linus Torvalds 已提交
2344 2345 2346
	dp = args->dp;
	w = args->whichfork;
	tp = args->trans;
2347
	count = args->geo->fsbcount;
L
Linus Torvalds 已提交
2348 2349 2350 2351 2352
	for (;;) {
		/*
		 * Remove extents.  If we get ENOSPC for a dir we have to move
		 * the last block to the place we want to kill.
		 */
2353 2354 2355
		error = xfs_bunmapi(tp, dp, dead_blkno, count,
				    xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
				    0, args->firstblock, args->flist, &done);
D
Dave Chinner 已提交
2356
		if (error == -ENOSPC) {
L
Linus Torvalds 已提交
2357
			if (w != XFS_DATA_FORK)
2358
				break;
2359 2360 2361
			error = xfs_da3_swap_lastblock(args, &dead_blkno,
						      &dead_buf);
			if (error)
2362 2363
				break;
		} else {
L
Linus Torvalds 已提交
2364 2365 2366
			break;
		}
	}
2367
	xfs_trans_binval(tp, dead_buf);
L
Linus Torvalds 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
	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;
}

/*
2399 2400 2401 2402 2403 2404
 * 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.
L
Linus Torvalds 已提交
2405
 */
2406 2407 2408 2409
static int
xfs_buf_map_from_irec(
	struct xfs_mount	*mp,
	struct xfs_buf_map	**mapp,
2410
	int			*nmaps,
2411
	struct xfs_bmbt_irec	*irecs,
2412
	int			nirecs)
L
Linus Torvalds 已提交
2413
{
2414 2415 2416 2417 2418 2419 2420
	struct xfs_buf_map	*map;
	int			i;

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

	if (nirecs > 1) {
2421 2422
		map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
				  KM_SLEEP | KM_NOFS);
2423
		if (!map)
D
Dave Chinner 已提交
2424
			return -ENOMEM;
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 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
		*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);
L
Linus Torvalds 已提交
2465

2466 2467 2468 2469
	if (whichfork == XFS_DATA_FORK)
		nfsb = mp->m_dir_geo->fsbcount;
	else
		nfsb = mp->m_attr_geo->fsbcount;
2470

L
Linus Torvalds 已提交
2471 2472 2473 2474 2475 2476 2477 2478
	/*
	 * 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.
		 */
2479
		if (nfsb != 1)
2480 2481
			irecs = kmem_zalloc(sizeof(irec) * nfsb,
					    KM_SLEEP | KM_NOFS);
D
Dave Chinner 已提交
2482

2483 2484 2485
		nirecs = nfsb;
		error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
				       &nirecs, xfs_bmapi_aflag(whichfork));
D
Dave Chinner 已提交
2486
		if (error)
2487
			goto out;
L
Linus Torvalds 已提交
2488
	} else {
2489 2490 2491 2492 2493
		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;
L
Linus Torvalds 已提交
2494
	}
2495 2496

	if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
D
Dave Chinner 已提交
2497 2498
		error = mappedbno == -2 ? -1 : -EFSCORRUPTED;
		if (unlikely(error == -EFSCORRUPTED)) {
L
Linus Torvalds 已提交
2499
			if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2500
				int i;
2501 2502
				xfs_alert(mp, "%s: bno %lld dir: inode %lld",
					__func__, (long long)bno,
L
Linus Torvalds 已提交
2503
					(long long)dp->i_ino);
2504
				for (i = 0; i < *nmaps; i++) {
2505 2506
					xfs_alert(mp,
"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
L
Linus Torvalds 已提交
2507
						i,
2508 2509 2510 2511
						(long long)irecs[i].br_startoff,
						(long long)irecs[i].br_startblock,
						(long long)irecs[i].br_blockcount,
						irecs[i].br_state);
L
Linus Torvalds 已提交
2512 2513 2514 2515 2516
				}
			}
			XFS_ERROR_REPORT("xfs_da_do_buf(1)",
					 XFS_ERRLEVEL_LOW, mp);
		}
2517
		goto out;
L
Linus Torvalds 已提交
2518
	}
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
	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,
2535
	struct xfs_buf		**bpp,
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
	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;
2547
	error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2548 2549 2550 2551
				&mapp, &nmap);
	if (error) {
		/* mapping a hole is not an error, but we don't continue */
		if (error == -1)
L
Linus Torvalds 已提交
2552
			error = 0;
2553
		goto out_free;
L
Linus Torvalds 已提交
2554
	}
2555 2556 2557

	bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
				    mapp, nmap, 0);
D
Dave Chinner 已提交
2558
	error = bp ? bp->b_error : -EIO;
2559
	if (error) {
2560 2561
		if (bp)
			xfs_trans_brelse(trans, bp);
2562 2563 2564
		goto out_free;
	}

2565
	*bpp = bp;
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582

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,
2583
	struct xfs_buf		**bpp,
2584
	int			whichfork,
2585
	const struct xfs_buf_ops *ops)
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
{
	struct xfs_buf		*bp;
	struct xfs_buf_map	map;
	struct xfs_buf_map	*mapp;
	int			nmap;
	int			error;

	*bpp = NULL;
	mapp = &map;
	nmap = 1;
2596
	error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
				&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,
2607
					mapp, nmap, 0, &bp, ops);
2608 2609 2610 2611 2612
	if (error)
		goto out_free;

	if (whichfork == XFS_ATTR_FORK)
		xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
L
Linus Torvalds 已提交
2613
	else
2614
		xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2615
	*bpp = bp;
2616
out_free:
L
Linus Torvalds 已提交
2617
	if (mapp != &map)
2618
		kmem_free(mapp);
L
Linus Torvalds 已提交
2619

2620
	return error;
L
Linus Torvalds 已提交
2621 2622 2623 2624 2625 2626 2627
}

/*
 * Readahead the dir/attr block.
 */
xfs_daddr_t
xfs_da_reada_buf(
2628 2629
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
2630
	xfs_daddr_t		mappedbno,
2631
	int			whichfork,
2632
	const struct xfs_buf_ops *ops)
L
Linus Torvalds 已提交
2633
{
2634 2635 2636 2637 2638 2639 2640
	struct xfs_buf_map	map;
	struct xfs_buf_map	*mapp;
	int			nmap;
	int			error;

	mapp = &map;
	nmap = 1;
2641
	error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2642 2643 2644 2645 2646 2647 2648
				&mapp, &nmap);
	if (error) {
		/* mapping a hole is not an error, but we don't continue */
		if (error == -1)
			error = 0;
		goto out_free;
	}
L
Linus Torvalds 已提交
2649

2650
	mappedbno = mapp[0].bm_bn;
2651
	xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
2652 2653 2654 2655 2656 2657

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

	if (error)
L
Linus Torvalds 已提交
2658
		return -1;
2659
	return mappedbno;
L
Linus Torvalds 已提交
2660
}