xfs_inode_fork.c 19.6 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 27 28 29 30 31 32 33

kmem_zone_t *xfs_ifork_zone;

STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);

/*
C
Christoph Hellwig 已提交
34 35 36 37 38 39 40
 * Copy inode type and data and attr format specific information from the
 * on-disk inode to the in-core inode and fork structures.  For fifos, devices,
 * and sockets this means set i_rdev to the proper value.  For files,
 * directories, and symlinks this means to bring in the in-line data or extent
 * pointers as well as the attribute fork.  For a fork in B-tree format, only
 * the root is immediately brought in-core.  The rest will be read in later when
 * first referenced (see xfs_iread_extents()).
41 42 43
 */
int
xfs_iformat_fork(
C
Christoph Hellwig 已提交
44 45
	struct xfs_inode	*ip,
	struct xfs_dinode	*dip)
46
{
C
Christoph Hellwig 已提交
47 48
	struct inode		*inode = VFS_I(ip);
	struct xfs_attr_shortform *atp;
49 50 51 52
	int			size;
	int			error = 0;
	xfs_fsize_t             di_size;

C
Christoph Hellwig 已提交
53
	switch (inode->i_mode & S_IFMT) {
54 55 56 57 58
	case S_IFIFO:
	case S_IFCHR:
	case S_IFBLK:
	case S_IFSOCK:
		ip->i_d.di_size = 0;
C
Christoph Hellwig 已提交
59
		inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
		break;

	case S_IFREG:
	case S_IFLNK:
	case S_IFDIR:
		switch (dip->di_format) {
		case XFS_DINODE_FMT_LOCAL:
			di_size = be64_to_cpu(dip->di_size);
			size = (int)di_size;
			error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
			break;
		case XFS_DINODE_FMT_EXTENTS:
			error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
			break;
		case XFS_DINODE_FMT_BTREE:
			error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
			break;
		default:
78 79
			xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
					dip, sizeof(*dip), __this_address);
D
Dave Chinner 已提交
80
			return -EFSCORRUPTED;
81 82 83 84
		}
		break;

	default:
85 86
		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
				sizeof(*dip), __this_address);
D
Dave Chinner 已提交
87
		return -EFSCORRUPTED;
88
	}
D
Darrick J. Wong 已提交
89
	if (error)
90
		return error;
D
Darrick J. Wong 已提交
91 92 93 94

	if (xfs_is_reflink_inode(ip)) {
		ASSERT(ip->i_cowfp == NULL);
		xfs_ifork_init_cow(ip);
95
	}
D
Darrick J. Wong 已提交
96

97 98 99 100
	if (!XFS_DFORK_Q(dip))
		return 0;

	ASSERT(ip->i_afp == NULL);
101
	ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_NOFS);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

	switch (dip->di_aformat) {
	case XFS_DINODE_FMT_LOCAL:
		atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
		size = be16_to_cpu(atp->hdr.totsize);

		error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
		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:
117 118
		xfs_inode_verifier_error(ip, error, __func__, dip,
				sizeof(*dip), __this_address);
D
Dave Chinner 已提交
119
		error = -EFSCORRUPTED;
120 121 122
		break;
	}
	if (error) {
123
		kmem_cache_free(xfs_ifork_zone, ip->i_afp);
124
		ip->i_afp = NULL;
D
Darrick J. Wong 已提交
125
		if (ip->i_cowfp)
126
			kmem_cache_free(xfs_ifork_zone, ip->i_cowfp);
D
Darrick J. Wong 已提交
127
		ip->i_cowfp = NULL;
128 129 130 131 132
		xfs_idestroy_fork(ip, XFS_DATA_FORK);
	}
	return error;
}

133 134 135 136 137
void
xfs_init_local_fork(
	struct xfs_inode	*ip,
	int			whichfork,
	const void		*data,
138
	int64_t			size)
139 140
{
	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
141 142 143 144 145 146 147 148 149 150 151 152
	int			mem_size = size, real_size = 0;
	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++;
153

154
	if (size) {
155
		real_size = roundup(mem_size, 4);
156
		ifp->if_u1.if_data = kmem_alloc(real_size, KM_NOFS);
157
		memcpy(ifp->if_u1.if_data, data, size);
158 159
		if (zero_terminate)
			ifp->if_u1.if_data[size] = '\0';
160 161
	} else {
		ifp->if_u1.if_data = NULL;
162
	}
163 164 165 166 167 168

	ifp->if_bytes = size;
	ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
	ifp->if_flags |= XFS_IFINLINE;
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
 * The file is in-lined in the on-disk inode.
 */
STATIC int
xfs_iformat_local(
	xfs_inode_t	*ip,
	xfs_dinode_t	*dip,
	int		whichfork,
	int		size)
{
	/*
	 * 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,
186
	"corrupt inode %Lu (bad size %d for local fork, size = %zd).",
187 188
			(unsigned long long) ip->i_ino, size,
			XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
189 190 191
		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
				"xfs_iformat_local", dip, sizeof(*dip),
				__this_address);
D
Dave Chinner 已提交
192
		return -EFSCORRUPTED;
193
	}
194 195

	xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
196 197 198 199
	return 0;
}

/*
200
 * The file consists of a set of extents all of which fit into the on-disk
201
 * inode.
202 203 204
 */
STATIC int
xfs_iformat_extents(
205 206 207
	struct xfs_inode	*ip,
	struct xfs_dinode	*dip,
	int			whichfork)
208
{
209 210
	struct xfs_mount	*mp = ip->i_mount;
	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
211
	int			state = xfs_bmap_fork_to_state(whichfork);
212 213
	int			nex = XFS_DFORK_NEXTENTS(dip, whichfork);
	int			size = nex * sizeof(xfs_bmbt_rec_t);
214
	struct xfs_iext_cursor	icur;
215
	struct xfs_bmbt_rec	*dp;
216
	struct xfs_bmbt_irec	new;
217
	int			i;
218 219

	/*
220 221
	 * 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.
222
	 */
223
	if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
224 225
		xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
			(unsigned long long) ip->i_ino, nex);
226 227 228
		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
				"xfs_iformat_extents(1)", dip, sizeof(*dip),
				__this_address);
D
Dave Chinner 已提交
229
		return -EFSCORRUPTED;
230 231
	}

232 233 234
	ifp->if_bytes = 0;
	ifp->if_u1.if_root = NULL;
	ifp->if_height = 0;
235 236
	if (size) {
		dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
237 238

		xfs_iext_first(ifp, &icur);
239
		for (i = 0; i < nex; i++, dp++) {
240 241
			xfs_failaddr_t	fa;

242
			xfs_bmbt_disk_get_all(dp, &new);
243 244 245 246 247
			fa = xfs_bmap_validate_extent(ip, whichfork, &new);
			if (fa) {
				xfs_inode_verifier_error(ip, -EFSCORRUPTED,
						"xfs_iformat_extents(2)",
						dp, sizeof(*dp), fa);
248 249
				return -EFSCORRUPTED;
			}
250

251
			xfs_iext_insert(ip, &icur, &new, state);
252 253
			trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
			xfs_iext_next(ifp, &icur);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
		}
	}
	ifp->if_flags |= XFS_IFEXTENTS;
	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(
	xfs_inode_t		*ip,
	xfs_dinode_t		*dip,
	int			whichfork)
{
	struct xfs_mount	*mp = ip->i_mount;
	xfs_bmdr_block_t	*dfp;
276
	struct xfs_ifork	*ifp;
277 278 279
	/* REFERENCED */
	int			nrecs;
	int			size;
280
	int			level;
281 282 283 284 285

	ifp = XFS_IFORK_PTR(ip, whichfork);
	dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
	size = XFS_BMAP_BROOT_SPACE(mp, dfp);
	nrecs = be16_to_cpu(dfp->bb_numrecs);
286
	level = be16_to_cpu(dfp->bb_level);
287 288 289 290 291 292 293 294 295 296

	/*
	 * 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.
	 */
	if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
					XFS_IFORK_MAXEXT(ip, whichfork) ||
297
		     nrecs == 0 ||
298 299
		     XFS_BMDR_SPACE_CALC(nrecs) >
					XFS_DFORK_SIZE(dip, mp, whichfork) ||
300 301
		     XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
		     level == 0 || level > XFS_BTREE_MAXLEVELS) {
302 303
		xfs_warn(mp, "corrupt inode %Lu (btree).",
					(unsigned long long) ip->i_ino);
304 305 306
		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
				"xfs_iformat_btree", dfp, size,
				__this_address);
D
Dave Chinner 已提交
307
		return -EFSCORRUPTED;
308 309 310
	}

	ifp->if_broot_bytes = size;
311
	ifp->if_broot = kmem_alloc(size, KM_NOFS);
312 313 314 315 316 317 318 319 320 321
	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);
	ifp->if_flags &= ~XFS_IFEXTENTS;
	ifp->if_flags |= XFS_IFBROOT;

322 323 324
	ifp->if_bytes = 0;
	ifp->if_u1.if_root = NULL;
	ifp->if_height = 0;
325 326 327 328 329 330 331 332 333 334 335 336 337
	return 0;
}

/*
 * 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
338
 * if we are adding records, one will be allocated.  The caller must also
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
 * 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;
354
	struct xfs_ifork	*ifp;
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	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;
	}

	ifp = XFS_IFORK_PTR(ip, whichfork);
	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);
376
			ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
377 378 379 380 381 382 383 384 385 386 387 388 389 390
			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);
		ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
391
				KM_NOFS);
392 393 394 395 396 397 398
		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) <=
			XFS_IFORK_SIZE(ip, whichfork));
C
Christoph Hellwig 已提交
399
		memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
		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) {
417
		new_broot = kmem_alloc(new_size, KM_NOFS);
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
		/*
		 * First copy over the btree block header.
		 */
		memcpy(new_broot, ifp->if_broot,
			XFS_BMBT_BLOCK_LEN(ip->i_mount));
	} else {
		new_broot = NULL;
		ifp->if_flags &= ~XFS_IFBROOT;
	}

	/*
	 * 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 已提交
446
		memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	}
	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) <=
			XFS_IFORK_SIZE(ip, whichfork));
	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(
475
	struct xfs_inode	*ip,
476
	int64_t			byte_diff,
477
	int			whichfork)
478
{
479
	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
480
	int64_t			new_size = ifp->if_bytes + byte_diff;
481 482

	ASSERT(new_size >= 0);
483 484 485 486
	ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));

	if (byte_diff == 0)
		return;
487 488

	if (new_size == 0) {
489
		kmem_free(ifp->if_u1.if_data);
490
		ifp->if_u1.if_data = NULL;
491 492
		ifp->if_bytes = 0;
		return;
493
	}
494 495 496 497 498 499 500

	/*
	 * For inline data, the underlying buffer must be a multiple of 4 bytes
	 * in size so that it can be logged and stay on word boundaries.
	 * We enforce that here.
	 */
	ifp->if_u1.if_data = kmem_realloc(ifp->if_u1.if_data,
501
			roundup(new_size, 4), KM_NOFS);
502 503 504 505 506 507 508 509
	ifp->if_bytes = new_size;
}

void
xfs_idestroy_fork(
	xfs_inode_t	*ip,
	int		whichfork)
{
510
	struct xfs_ifork	*ifp;
511 512 513 514 515 516 517 518 519 520 521 522 523 524

	ifp = XFS_IFORK_PTR(ip, whichfork);
	if (ifp->if_broot != NULL) {
		kmem_free(ifp->if_broot);
		ifp->if_broot = NULL;
	}

	/*
	 * If the format is local, then we can't have an extents
	 * array so just look for an inline data array.  If we're
	 * not local then we may or may not have an extents list,
	 * so check and free it up if we do.
	 */
	if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
525
		if (ifp->if_u1.if_data != NULL) {
526 527 528
			kmem_free(ifp->if_u1.if_data);
			ifp->if_u1.if_data = NULL;
		}
529
	} else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
530 531
		xfs_iext_destroy(ifp);
	}
532

533
	if (whichfork == XFS_ATTR_FORK) {
534
		kmem_cache_free(xfs_ifork_zone, ip->i_afp);
535
		ip->i_afp = NULL;
D
Darrick J. Wong 已提交
536
	} else if (whichfork == XFS_COW_FORK) {
537
		kmem_cache_free(xfs_ifork_zone, ip->i_cowfp);
D
Darrick J. Wong 已提交
538
		ip->i_cowfp = NULL;
539 540 541 542
	}
}

/*
543
 * Convert in-core extents to on-disk form
544
 *
545 546 547 548 549
 * 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.
550 551 552
 */
int
xfs_iextents_copy(
553 554
	struct xfs_inode	*ip,
	struct xfs_bmbt_rec	*dp,
555 556
	int			whichfork)
{
557
	int			state = xfs_bmap_fork_to_state(whichfork);
558
	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
559
	struct xfs_iext_cursor	icur;
560
	struct xfs_bmbt_irec	rec;
561
	int64_t			copied = 0;
562

563
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
564 565
	ASSERT(ifp->if_bytes > 0);

566
	for_each_xfs_iext(ifp, &icur, &rec) {
567
		if (isnullstartblock(rec.br_startblock))
568
			continue;
569
		ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
570
		xfs_bmbt_disk_set_all(dp, &rec);
571
		trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
572
		copied += sizeof(struct xfs_bmbt_rec);
573 574 575
		dp++;
	}

576 577 578
	ASSERT(copied > 0);
	ASSERT(copied <= ifp->if_bytes);
	return copied;
579 580 581 582 583 584 585 586 587 588 589 590
}

/*
 * 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.
 */
591
void
592 593 594
xfs_iflush_fork(
	xfs_inode_t		*ip,
	xfs_dinode_t		*dip,
595
	struct xfs_inode_log_item *iip,
596
	int			whichfork)
597 598
{
	char			*cp;
599
	struct xfs_ifork	*ifp;
600 601 602 603 604 605 606 607 608
	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)
609
		return;
610 611 612 613 614 615 616
	ifp = XFS_IFORK_PTR(ip, whichfork);
	/*
	 * This can happen if we gave up in iformat in an error path,
	 * for the attribute fork.
	 */
	if (!ifp) {
		ASSERT(whichfork == XFS_ATTR_FORK);
617
		return;
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	}
	cp = XFS_DFORK_PTR(dip, whichfork);
	mp = ip->i_mount;
	switch (XFS_IFORK_FORMAT(ip, whichfork)) {
	case XFS_DINODE_FMT_LOCAL:
		if ((iip->ili_fields & dataflag[whichfork]) &&
		    (ifp->if_bytes > 0)) {
			ASSERT(ifp->if_u1.if_data != NULL);
			ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
			memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
		}
		break;

	case XFS_DINODE_FMT_EXTENTS:
		ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
		       !(iip->ili_fields & extflag[whichfork]));
		if ((iip->ili_fields & extflag[whichfork]) &&
		    (ifp->if_bytes > 0)) {
			ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
			(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) <=
			        XFS_IFORK_SIZE(ip, whichfork));
			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);
657 658
			xfs_dinode_put_rdev(dip,
					linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
659 660 661 662 663 664 665 666 667
		}
		break;

	default:
		ASSERT(0);
		break;
	}
}

D
Darrick J. Wong 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
/* 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)
		return ip->i_afp;
	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;

	ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
692
				       KM_NOFS);
D
Darrick J. Wong 已提交
693 694 695 696
	ip->i_cowfp->if_flags = XFS_IFEXTENTS;
	ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
	ip->i_cnextents = 0;
}
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

/* Default fork content verifiers. */
struct xfs_ifork_ops xfs_default_ifork_ops = {
	.verify_attr	= xfs_attr_shortform_verify,
	.verify_dir	= xfs_dir2_sf_verify,
	.verify_symlink	= xfs_symlink_shortform_verify,
};

/* Verify the inline contents of the data fork of an inode. */
xfs_failaddr_t
xfs_ifork_verify_data(
	struct xfs_inode	*ip,
	struct xfs_ifork_ops	*ops)
{
	/* Non-local data fork, we're done. */
	if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
		return NULL;

	/* Check the inline data fork if there is one. */
	switch (VFS_I(ip)->i_mode & S_IFMT) {
	case S_IFDIR:
		return ops->verify_dir(ip);
	case S_IFLNK:
		return ops->verify_symlink(ip);
	default:
		return NULL;
	}
}

/* Verify the inline contents of the attr fork of an inode. */
xfs_failaddr_t
xfs_ifork_verify_attr(
	struct xfs_inode	*ip,
	struct xfs_ifork_ops	*ops)
{
	/* There has to be an attr fork allocated if aformat is local. */
	if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
		return NULL;
	if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
		return __this_address;
	return ops->verify_attr(ip);
}