xfs_inode_fork.c 19.5 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8
/*
 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
 * All Rights Reserved.
 */

#include "xfs.h"
#include "xfs_fs.h"
9
#include "xfs_shared.h"
10
#include "xfs_format.h"
11 12
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
13 14
#include "xfs_mount.h"
#include "xfs_inode.h"
15
#include "xfs_trans.h"
16
#include "xfs_inode_item.h"
17
#include "xfs_btree.h"
18
#include "xfs_bmap_btree.h"
19 20 21
#include "xfs_bmap.h"
#include "xfs_error.h"
#include "xfs_trace.h"
22
#include "xfs_da_format.h"
23 24
#include "xfs_da_btree.h"
#include "xfs_dir2_priv.h"
25
#include "xfs_attr_leaf.h"
26
#include "xfs_types.h"
27
#include "xfs_errortag.h"
28

29
struct kmem_cache *xfs_ifork_cache;
30

31 32 33 34 35
void
xfs_init_local_fork(
	struct xfs_inode	*ip,
	int			whichfork,
	const void		*data,
36
	int64_t			size)
37
{
38
	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
39
	int			mem_size = size;
40 41 42 43 44 45 46 47 48 49 50
	bool			zero_terminate;

	/*
	 * If we are using the local fork to store a symlink body we need to
	 * zero-terminate it so that we can pass it back to the VFS directly.
	 * Overallocate the in-memory fork by one for that and add a zero
	 * to terminate it below.
	 */
	zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
	if (zero_terminate)
		mem_size++;
51

52
	if (size) {
53
		ifp->if_u1.if_data = kmem_alloc(mem_size, KM_NOFS);
54
		memcpy(ifp->if_u1.if_data, data, size);
55 56
		if (zero_terminate)
			ifp->if_u1.if_data[size] = '\0';
57 58
	} else {
		ifp->if_u1.if_data = NULL;
59
	}
60 61 62 63

	ifp->if_bytes = size;
}

64 65 66 67 68
/*
 * The file is in-lined in the on-disk inode.
 */
STATIC int
xfs_iformat_local(
69 70 71 72
	struct xfs_inode	*ip,
	struct xfs_dinode	*dip,
	int			whichfork,
	int			size)
73 74 75 76 77 78 79 80
{
	/*
	 * If the size is unreasonable, then something
	 * is wrong and we just bail out rather than crash in
	 * kmem_alloc() or memcpy() below.
	 */
	if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
		xfs_warn(ip->i_mount,
81
	"corrupt inode %Lu (bad size %d for local fork, size = %zd).",
82 83
			(unsigned long long) ip->i_ino, size,
			XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
84 85 86
		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
				"xfs_iformat_local", dip, sizeof(*dip),
				__this_address);
D
Dave Chinner 已提交
87
		return -EFSCORRUPTED;
88
	}
89 90

	xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
91 92 93 94
	return 0;
}

/*
95
 * The file consists of a set of extents all of which fit into the on-disk
96
 * inode.
97 98 99
 */
STATIC int
xfs_iformat_extents(
100 101 102
	struct xfs_inode	*ip,
	struct xfs_dinode	*dip,
	int			whichfork)
103
{
104
	struct xfs_mount	*mp = ip->i_mount;
105
	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
106
	int			state = xfs_bmap_fork_to_state(whichfork);
107
	xfs_extnum_t		nex = xfs_dfork_nextents(dip, whichfork);
108
	int			size = nex * sizeof(xfs_bmbt_rec_t);
109
	struct xfs_iext_cursor	icur;
110
	struct xfs_bmbt_rec	*dp;
111
	struct xfs_bmbt_irec	new;
112
	int			i;
113 114

	/*
115 116
	 * If the number of extents is unreasonable, then something is wrong and
	 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
117
	 */
118
	if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
119 120
		xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).",
			ip->i_ino, nex);
121 122 123
		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
				"xfs_iformat_extents(1)", dip, sizeof(*dip),
				__this_address);
D
Dave Chinner 已提交
124
		return -EFSCORRUPTED;
125 126
	}

127 128 129
	ifp->if_bytes = 0;
	ifp->if_u1.if_root = NULL;
	ifp->if_height = 0;
130 131
	if (size) {
		dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
132 133

		xfs_iext_first(ifp, &icur);
134
		for (i = 0; i < nex; i++, dp++) {
135 136
			xfs_failaddr_t	fa;

137
			xfs_bmbt_disk_get_all(dp, &new);
138 139 140 141 142
			fa = xfs_bmap_validate_extent(ip, whichfork, &new);
			if (fa) {
				xfs_inode_verifier_error(ip, -EFSCORRUPTED,
						"xfs_iformat_extents(2)",
						dp, sizeof(*dp), fa);
143 144
				return -EFSCORRUPTED;
			}
145

146
			xfs_iext_insert(ip, &icur, &new, state);
147 148
			trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
			xfs_iext_next(ifp, &icur);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		}
	}
	return 0;
}

/*
 * The file has too many extents to fit into
 * the inode, so they are in B-tree format.
 * Allocate a buffer for the root of the B-tree
 * and copy the root into it.  The i_extents
 * field will remain NULL until all of the
 * extents are read in (when they are needed).
 */
STATIC int
xfs_iformat_btree(
164 165
	struct xfs_inode	*ip,
	struct xfs_dinode	*dip,
166 167 168 169
	int			whichfork)
{
	struct xfs_mount	*mp = ip->i_mount;
	xfs_bmdr_block_t	*dfp;
170
	struct xfs_ifork	*ifp;
171 172 173
	/* REFERENCED */
	int			nrecs;
	int			size;
174
	int			level;
175

176
	ifp = xfs_ifork_ptr(ip, whichfork);
177 178 179
	dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
	size = XFS_BMAP_BROOT_SPACE(mp, dfp);
	nrecs = be16_to_cpu(dfp->bb_numrecs);
180
	level = be16_to_cpu(dfp->bb_level);
181 182 183 184 185 186 187 188

	/*
	 * blow out if -- fork has less extents than can fit in
	 * fork (fork shouldn't be a btree format), root btree
	 * block has more records than can fit into the fork,
	 * or the number of extents is greater than the number of
	 * blocks.
	 */
189
	if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
190
		     nrecs == 0 ||
191 192
		     XFS_BMDR_SPACE_CALC(nrecs) >
					XFS_DFORK_SIZE(dip, mp, whichfork) ||
193
		     ifp->if_nextents > ip->i_nblocks) ||
194
		     level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
195 196
		xfs_warn(mp, "corrupt inode %Lu (btree).",
					(unsigned long long) ip->i_ino);
197 198 199
		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
				"xfs_iformat_btree", dfp, size,
				__this_address);
D
Dave Chinner 已提交
200
		return -EFSCORRUPTED;
201 202 203
	}

	ifp->if_broot_bytes = size;
204
	ifp->if_broot = kmem_alloc(size, KM_NOFS);
205 206 207 208 209 210 211 212
	ASSERT(ifp->if_broot != NULL);
	/*
	 * Copy and convert from the on-disk structure
	 * to the in-memory structure.
	 */
	xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
			 ifp->if_broot, size);

213 214 215
	ifp->if_bytes = 0;
	ifp->if_u1.if_root = NULL;
	ifp->if_height = 0;
216 217 218
	return 0;
}

C
Christoph Hellwig 已提交
219 220 221 222 223 224
int
xfs_iformat_data_fork(
	struct xfs_inode	*ip,
	struct xfs_dinode	*dip)
{
	struct inode		*inode = VFS_I(ip);
225
	int			error;
C
Christoph Hellwig 已提交
226

227 228 229 230
	/*
	 * Initialize the extent count early, as the per-format routines may
	 * depend on it.
	 */
231
	ip->i_df.if_format = dip->di_format;
232
	ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
233

C
Christoph Hellwig 已提交
234 235 236 237 238
	switch (inode->i_mode & S_IFMT) {
	case S_IFIFO:
	case S_IFCHR:
	case S_IFBLK:
	case S_IFSOCK:
239
		ip->i_disk_size = 0;
C
Christoph Hellwig 已提交
240 241 242 243 244
		inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
		return 0;
	case S_IFREG:
	case S_IFLNK:
	case S_IFDIR:
245
		switch (ip->i_df.if_format) {
C
Christoph Hellwig 已提交
246
		case XFS_DINODE_FMT_LOCAL:
247
			error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
C
Christoph Hellwig 已提交
248
					be64_to_cpu(dip->di_size));
249 250 251
			if (!error)
				error = xfs_ifork_verify_local_data(ip);
			return error;
C
Christoph Hellwig 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
		case XFS_DINODE_FMT_EXTENTS:
			return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
		case XFS_DINODE_FMT_BTREE:
			return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
		default:
			xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
					dip, sizeof(*dip), __this_address);
			return -EFSCORRUPTED;
		}
		break;
	default:
		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
				sizeof(*dip), __this_address);
		return -EFSCORRUPTED;
	}
}

static uint16_t
xfs_dfork_attr_shortform_size(
	struct xfs_dinode		*dip)
{
	struct xfs_attr_shortform	*atp =
		(struct xfs_attr_shortform *)XFS_DFORK_APTR(dip);

	return be16_to_cpu(atp->hdr.totsize);
}

279 280 281
void
xfs_ifork_init_attr(
	struct xfs_inode	*ip,
282 283 284
	enum xfs_dinode_fmt	format,
	xfs_extnum_t		nextents)
{
285 286 287 288 289 290 291 292 293 294 295 296 297 298
	ip->i_af.if_format = format;
	ip->i_af.if_nextents = nextents;
}

void
xfs_ifork_zap_attr(
	struct xfs_inode	*ip)
{
	ASSERT(ip->i_af.if_broot == NULL);
	ASSERT(ip->i_af.if_u1.if_data == NULL);
	ASSERT(ip->i_af.if_height == 0);

	memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
	ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
299 300
}

C
Christoph Hellwig 已提交
301 302 303 304 305
int
xfs_iformat_attr_fork(
	struct xfs_inode	*ip,
	struct xfs_dinode	*dip)
{
306
	xfs_extnum_t		naextents = xfs_dfork_attr_extents(dip);
C
Christoph Hellwig 已提交
307 308
	int			error = 0;

309 310 311 312
	/*
	 * Initialize the extent count early, as the per-format routines may
	 * depend on it.
	 */
313
	xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
314

315
	switch (ip->i_af.if_format) {
C
Christoph Hellwig 已提交
316 317 318
	case XFS_DINODE_FMT_LOCAL:
		error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
				xfs_dfork_attr_shortform_size(dip));
319 320
		if (!error)
			error = xfs_ifork_verify_local_attr(ip);
C
Christoph Hellwig 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334
		break;
	case XFS_DINODE_FMT_EXTENTS:
		error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
		break;
	case XFS_DINODE_FMT_BTREE:
		error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
		break;
	default:
		xfs_inode_verifier_error(ip, error, __func__, dip,
				sizeof(*dip), __this_address);
		error = -EFSCORRUPTED;
		break;
	}

335 336
	if (error)
		xfs_ifork_zap_attr(ip);
C
Christoph Hellwig 已提交
337 338 339
	return error;
}

340 341 342 343 344 345 346 347 348 349
/*
 * Reallocate the space for if_broot based on the number of records
 * being added or deleted as indicated in rec_diff.  Move the records
 * and pointers in if_broot to fit the new size.  When shrinking this
 * will eliminate holes between the records and pointers created by
 * the caller.  When growing this will create holes to be filled in
 * by the caller.
 *
 * The caller must not request to add more records than would fit in
 * the on-disk inode root.  If the if_broot is currently NULL, then
350
 * if we are adding records, one will be allocated.  The caller must also
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
 * not request that the number of records go below zero, although
 * it can go to zero.
 *
 * ip -- the inode whose if_broot area is changing
 * ext_diff -- the change in the number of records, positive or negative,
 *	 requested for the if_broot array.
 */
void
xfs_iroot_realloc(
	xfs_inode_t		*ip,
	int			rec_diff,
	int			whichfork)
{
	struct xfs_mount	*mp = ip->i_mount;
	int			cur_max;
366
	struct xfs_ifork	*ifp;
367 368 369 370 371 372 373 374 375 376 377 378 379
	struct xfs_btree_block	*new_broot;
	int			new_max;
	size_t			new_size;
	char			*np;
	char			*op;

	/*
	 * Handle the degenerate case quietly.
	 */
	if (rec_diff == 0) {
		return;
	}

380
	ifp = xfs_ifork_ptr(ip, whichfork);
381 382 383 384 385 386 387
	if (rec_diff > 0) {
		/*
		 * If there wasn't any memory allocated before, just
		 * allocate it now and get out.
		 */
		if (ifp->if_broot_bytes == 0) {
			new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
388
			ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
389 390 391 392 393 394 395 396 397 398 399 400 401
			ifp->if_broot_bytes = (int)new_size;
			return;
		}

		/*
		 * If there is already an existing if_broot, then we need
		 * to realloc() it and shift the pointers to their new
		 * location.  The records don't change location because
		 * they are kept butted up against the btree block header.
		 */
		cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
		new_max = cur_max + rec_diff;
		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
C
Carlos Maiolino 已提交
402 403
		ifp->if_broot = krealloc(ifp->if_broot, new_size,
					 GFP_NOFS | __GFP_NOFAIL);
404 405 406 407 408 409
		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
						     ifp->if_broot_bytes);
		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
						     (int)new_size);
		ifp->if_broot_bytes = (int)new_size;
		ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
410
			xfs_inode_fork_size(ip, whichfork));
C
Christoph Hellwig 已提交
411
		memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
		return;
	}

	/*
	 * rec_diff is less than 0.  In this case, we are shrinking the
	 * if_broot buffer.  It must already exist.  If we go to zero
	 * records, just get rid of the root and clear the status bit.
	 */
	ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
	cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
	new_max = cur_max + rec_diff;
	ASSERT(new_max >= 0);
	if (new_max > 0)
		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
	else
		new_size = 0;
	if (new_size > 0) {
429
		new_broot = kmem_alloc(new_size, KM_NOFS);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
		/*
		 * First copy over the btree block header.
		 */
		memcpy(new_broot, ifp->if_broot,
			XFS_BMBT_BLOCK_LEN(ip->i_mount));
	} else {
		new_broot = NULL;
	}

	/*
	 * Only copy the records and pointers if there are any.
	 */
	if (new_max > 0) {
		/*
		 * First copy the records.
		 */
		op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
		np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
		memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));

		/*
		 * Then copy the pointers.
		 */
		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
						     ifp->if_broot_bytes);
		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
						     (int)new_size);
C
Christoph Hellwig 已提交
457
		memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
458 459 460 461 462 463
	}
	kmem_free(ifp->if_broot);
	ifp->if_broot = new_broot;
	ifp->if_broot_bytes = (int)new_size;
	if (ifp->if_broot)
		ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
464
			xfs_inode_fork_size(ip, whichfork));
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	return;
}


/*
 * This is called when the amount of space needed for if_data
 * is increased or decreased.  The change in size is indicated by
 * the number of bytes that need to be added or deleted in the
 * byte_diff parameter.
 *
 * If the amount of space needed has decreased below the size of the
 * inline buffer, then switch to using the inline buffer.  Otherwise,
 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
 * to what is needed.
 *
 * ip -- the inode whose if_data area is changing
 * byte_diff -- the change in the number of bytes, positive or negative,
 *	 requested for the if_data array.
 */
void
xfs_idata_realloc(
486
	struct xfs_inode	*ip,
487
	int64_t			byte_diff,
488
	int			whichfork)
489
{
490
	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
491
	int64_t			new_size = ifp->if_bytes + byte_diff;
492 493

	ASSERT(new_size >= 0);
494
	ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
495 496 497

	if (byte_diff == 0)
		return;
498 499

	if (new_size == 0) {
500
		kmem_free(ifp->if_u1.if_data);
501
		ifp->if_u1.if_data = NULL;
502 503
		ifp->if_bytes = 0;
		return;
504
	}
505

506 507
	ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, new_size,
				      GFP_NOFS | __GFP_NOFAIL);
508 509 510 511 512
	ifp->if_bytes = new_size;
}

void
xfs_idestroy_fork(
513
	struct xfs_ifork	*ifp)
514 515 516 517 518 519
{
	if (ifp->if_broot != NULL) {
		kmem_free(ifp->if_broot);
		ifp->if_broot = NULL;
	}

520 521
	switch (ifp->if_format) {
	case XFS_DINODE_FMT_LOCAL:
522 523
		kmem_free(ifp->if_u1.if_data);
		ifp->if_u1.if_data = NULL;
524 525 526
		break;
	case XFS_DINODE_FMT_EXTENTS:
	case XFS_DINODE_FMT_BTREE:
527 528
		if (ifp->if_height)
			xfs_iext_destroy(ifp);
529
		break;
530 531 532 533
	}
}

/*
534
 * Convert in-core extents to on-disk form
535
 *
536 537 538 539 540
 * In the case of the data fork, the in-core and on-disk fork sizes can be
 * different due to delayed allocation extents. We only copy on-disk extents
 * here, so callers must always use the physical fork size to determine the
 * size of the buffer passed to this routine.  We will return the size actually
 * used.
541 542 543
 */
int
xfs_iextents_copy(
544 545
	struct xfs_inode	*ip,
	struct xfs_bmbt_rec	*dp,
546 547
	int			whichfork)
{
548
	int			state = xfs_bmap_fork_to_state(whichfork);
549
	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
550
	struct xfs_iext_cursor	icur;
551
	struct xfs_bmbt_irec	rec;
552
	int64_t			copied = 0;
553

554
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
555 556
	ASSERT(ifp->if_bytes > 0);

557
	for_each_xfs_iext(ifp, &icur, &rec) {
558
		if (isnullstartblock(rec.br_startblock))
559
			continue;
560
		ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
561
		xfs_bmbt_disk_set_all(dp, &rec);
562
		trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
563
		copied += sizeof(struct xfs_bmbt_rec);
564 565 566
		dp++;
	}

567 568 569
	ASSERT(copied > 0);
	ASSERT(copied <= ifp->if_bytes);
	return copied;
570 571 572 573 574 575 576 577 578 579 580 581
}

/*
 * Each of the following cases stores data into the same region
 * of the on-disk inode, so only one of them can be valid at
 * any given time. While it is possible to have conflicting formats
 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
 * in EXTENTS format, this can only happen when the fork has
 * changed formats after being modified but before being flushed.
 * In these cases, the format always takes precedence, because the
 * format indicates the current state of the fork.
 */
582
void
583
xfs_iflush_fork(
584 585
	struct xfs_inode	*ip,
	struct xfs_dinode	*dip,
586
	struct xfs_inode_log_item *iip,
587
	int			whichfork)
588 589
{
	char			*cp;
590
	struct xfs_ifork	*ifp;
591 592 593 594 595 596 597 598 599
	xfs_mount_t		*mp;
	static const short	brootflag[2] =
		{ XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
	static const short	dataflag[2] =
		{ XFS_ILOG_DDATA, XFS_ILOG_ADATA };
	static const short	extflag[2] =
		{ XFS_ILOG_DEXT, XFS_ILOG_AEXT };

	if (!iip)
600
		return;
601
	ifp = xfs_ifork_ptr(ip, whichfork);
602 603 604 605 606 607
	/*
	 * This can happen if we gave up in iformat in an error path,
	 * for the attribute fork.
	 */
	if (!ifp) {
		ASSERT(whichfork == XFS_ATTR_FORK);
608
		return;
609 610 611
	}
	cp = XFS_DFORK_PTR(dip, whichfork);
	mp = ip->i_mount;
612
	switch (ifp->if_format) {
613 614 615 616
	case XFS_DINODE_FMT_LOCAL:
		if ((iip->ili_fields & dataflag[whichfork]) &&
		    (ifp->if_bytes > 0)) {
			ASSERT(ifp->if_u1.if_data != NULL);
617
			ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
618 619 620 621 622 623 624
			memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
		}
		break;

	case XFS_DINODE_FMT_EXTENTS:
		if ((iip->ili_fields & extflag[whichfork]) &&
		    (ifp->if_bytes > 0)) {
625
			ASSERT(ifp->if_nextents > 0);
626 627 628 629 630 631 632 633 634 635
			(void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
				whichfork);
		}
		break;

	case XFS_DINODE_FMT_BTREE:
		if ((iip->ili_fields & brootflag[whichfork]) &&
		    (ifp->if_broot_bytes > 0)) {
			ASSERT(ifp->if_broot != NULL);
			ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
636
			        xfs_inode_fork_size(ip, whichfork));
637 638 639 640 641 642 643 644 645
			xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
				(xfs_bmdr_block_t *)cp,
				XFS_DFORK_SIZE(dip, mp, whichfork));
		}
		break;

	case XFS_DINODE_FMT_DEV:
		if (iip->ili_fields & XFS_ILOG_DEV) {
			ASSERT(whichfork == XFS_DATA_FORK);
646 647
			xfs_dinode_put_rdev(dip,
					linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
648 649 650 651 652 653 654 655 656
		}
		break;

	default:
		ASSERT(0);
		break;
	}
}

D
Darrick J. Wong 已提交
657 658 659 660 661 662 663 664 665
/* Convert bmap state flags to an inode fork. */
struct xfs_ifork *
xfs_iext_state_to_fork(
	struct xfs_inode	*ip,
	int			state)
{
	if (state & BMAP_COWFORK)
		return ip->i_cowfp;
	else if (state & BMAP_ATTRFORK)
666
		return &ip->i_af;
D
Darrick J. Wong 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679
	return &ip->i_df;
}

/*
 * Initialize an inode's copy-on-write fork.
 */
void
xfs_ifork_init_cow(
	struct xfs_inode	*ip)
{
	if (ip->i_cowfp)
		return;

680
	ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
681
				       GFP_NOFS | __GFP_NOFAIL);
682
	ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
D
Darrick J. Wong 已提交
683
}
684 685

/* Verify the inline contents of the data fork of an inode. */
686 687
int
xfs_ifork_verify_local_data(
C
Christoph Hellwig 已提交
688
	struct xfs_inode	*ip)
689
{
690
	xfs_failaddr_t		fa = NULL;
691 692 693

	switch (VFS_I(ip)->i_mode & S_IFMT) {
	case S_IFDIR:
694 695
		fa = xfs_dir2_sf_verify(ip);
		break;
696
	case S_IFLNK:
697 698
		fa = xfs_symlink_shortform_verify(ip);
		break;
699
	default:
700
		break;
701
	}
702 703 704 705 706 707 708 709

	if (fa) {
		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
				ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa);
		return -EFSCORRUPTED;
	}

	return 0;
710 711 712
}

/* Verify the inline contents of the attr fork of an inode. */
713 714
int
xfs_ifork_verify_local_attr(
C
Christoph Hellwig 已提交
715
	struct xfs_inode	*ip)
716
{
717
	struct xfs_ifork	*ifp = &ip->i_af;
718 719
	xfs_failaddr_t		fa;

720
	if (!xfs_inode_has_attr_fork(ip))
721 722 723 724 725 726 727 728 729 730 731 732
		fa = __this_address;
	else
		fa = xfs_attr_shortform_verify(ip);

	if (fa) {
		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
				ifp ? ifp->if_u1.if_data : NULL,
				ifp ? ifp->if_bytes : 0, fa);
		return -EFSCORRUPTED;
	}

	return 0;
733
}
734 735 736 737 738 739 740

int
xfs_iext_count_may_overflow(
	struct xfs_inode	*ip,
	int			whichfork,
	int			nr_to_add)
{
741
	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
742 743 744 745 746 747
	uint64_t		max_exts;
	uint64_t		nr_exts;

	if (whichfork == XFS_COW_FORK)
		return 0;

748 749
	max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip),
				whichfork);
750

751 752 753
	if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
		max_exts = 10;

754 755 756 757 758 759
	nr_exts = ifp->if_nextents + nr_to_add;
	if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
		return -EFBIG;

	return 0;
}
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

/*
 * Upgrade this inode's extent counter fields to be able to handle a potential
 * increase in the extent count by nr_to_add.  Normally this is the same
 * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
 */
int
xfs_iext_count_upgrade(
	struct xfs_trans	*tp,
	struct xfs_inode	*ip,
	uint			nr_to_add)
{
	ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);

	if (!xfs_has_large_extent_counts(ip->i_mount) ||
	    xfs_inode_has_large_extent_counts(ip) ||
	    XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
		return -EFBIG;

	ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);

	return 0;
}