xref: /openbmc/linux/fs/xfs/libxfs/xfs_inode_fork.c (revision ba61bb17)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include <linux/log2.h>
7 
8 #include "xfs.h"
9 #include "xfs_fs.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #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 #include "xfs_bmap.h"
20 #include "xfs_error.h"
21 #include "xfs_trace.h"
22 #include "xfs_attr_sf.h"
23 #include "xfs_da_format.h"
24 #include "xfs_da_btree.h"
25 #include "xfs_dir2_priv.h"
26 #include "xfs_attr_leaf.h"
27 #include "xfs_shared.h"
28 
29 kmem_zone_t *xfs_ifork_zone;
30 
31 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
32 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
33 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
34 
35 /*
36  * Copy inode type and data and attr format specific information from the
37  * on-disk inode to the in-core inode and fork structures.  For fifos, devices,
38  * and sockets this means set i_rdev to the proper value.  For files,
39  * directories, and symlinks this means to bring in the in-line data or extent
40  * pointers as well as the attribute fork.  For a fork in B-tree format, only
41  * the root is immediately brought in-core.  The rest will be read in later when
42  * first referenced (see xfs_iread_extents()).
43  */
44 int
45 xfs_iformat_fork(
46 	struct xfs_inode	*ip,
47 	struct xfs_dinode	*dip)
48 {
49 	struct inode		*inode = VFS_I(ip);
50 	struct xfs_attr_shortform *atp;
51 	int			size;
52 	int			error = 0;
53 	xfs_fsize_t             di_size;
54 
55 	switch (inode->i_mode & S_IFMT) {
56 	case S_IFIFO:
57 	case S_IFCHR:
58 	case S_IFBLK:
59 	case S_IFSOCK:
60 		ip->i_d.di_size = 0;
61 		inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
62 		break;
63 
64 	case S_IFREG:
65 	case S_IFLNK:
66 	case S_IFDIR:
67 		switch (dip->di_format) {
68 		case XFS_DINODE_FMT_LOCAL:
69 			di_size = be64_to_cpu(dip->di_size);
70 			size = (int)di_size;
71 			error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
72 			break;
73 		case XFS_DINODE_FMT_EXTENTS:
74 			error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
75 			break;
76 		case XFS_DINODE_FMT_BTREE:
77 			error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
78 			break;
79 		default:
80 			return -EFSCORRUPTED;
81 		}
82 		break;
83 
84 	default:
85 		return -EFSCORRUPTED;
86 	}
87 	if (error)
88 		return error;
89 
90 	if (xfs_is_reflink_inode(ip)) {
91 		ASSERT(ip->i_cowfp == NULL);
92 		xfs_ifork_init_cow(ip);
93 	}
94 
95 	if (!XFS_DFORK_Q(dip))
96 		return 0;
97 
98 	ASSERT(ip->i_afp == NULL);
99 	ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
100 
101 	switch (dip->di_aformat) {
102 	case XFS_DINODE_FMT_LOCAL:
103 		atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
104 		size = be16_to_cpu(atp->hdr.totsize);
105 
106 		error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
107 		break;
108 	case XFS_DINODE_FMT_EXTENTS:
109 		error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
110 		break;
111 	case XFS_DINODE_FMT_BTREE:
112 		error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
113 		break;
114 	default:
115 		error = -EFSCORRUPTED;
116 		break;
117 	}
118 	if (error) {
119 		kmem_zone_free(xfs_ifork_zone, ip->i_afp);
120 		ip->i_afp = NULL;
121 		if (ip->i_cowfp)
122 			kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
123 		ip->i_cowfp = NULL;
124 		xfs_idestroy_fork(ip, XFS_DATA_FORK);
125 	}
126 	return error;
127 }
128 
129 void
130 xfs_init_local_fork(
131 	struct xfs_inode	*ip,
132 	int			whichfork,
133 	const void		*data,
134 	int			size)
135 {
136 	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
137 	int			mem_size = size, real_size = 0;
138 	bool			zero_terminate;
139 
140 	/*
141 	 * If we are using the local fork to store a symlink body we need to
142 	 * zero-terminate it so that we can pass it back to the VFS directly.
143 	 * Overallocate the in-memory fork by one for that and add a zero
144 	 * to terminate it below.
145 	 */
146 	zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
147 	if (zero_terminate)
148 		mem_size++;
149 
150 	if (size) {
151 		real_size = roundup(mem_size, 4);
152 		ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
153 		memcpy(ifp->if_u1.if_data, data, size);
154 		if (zero_terminate)
155 			ifp->if_u1.if_data[size] = '\0';
156 	} else {
157 		ifp->if_u1.if_data = NULL;
158 	}
159 
160 	ifp->if_bytes = size;
161 	ifp->if_real_bytes = real_size;
162 	ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
163 	ifp->if_flags |= XFS_IFINLINE;
164 }
165 
166 /*
167  * The file is in-lined in the on-disk inode.
168  */
169 STATIC int
170 xfs_iformat_local(
171 	xfs_inode_t	*ip,
172 	xfs_dinode_t	*dip,
173 	int		whichfork,
174 	int		size)
175 {
176 	/*
177 	 * If the size is unreasonable, then something
178 	 * is wrong and we just bail out rather than crash in
179 	 * kmem_alloc() or memcpy() below.
180 	 */
181 	if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
182 		xfs_warn(ip->i_mount,
183 	"corrupt inode %Lu (bad size %d for local fork, size = %d).",
184 			(unsigned long long) ip->i_ino, size,
185 			XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
186 		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
187 				"xfs_iformat_local", dip, sizeof(*dip),
188 				__this_address);
189 		return -EFSCORRUPTED;
190 	}
191 
192 	xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
193 	return 0;
194 }
195 
196 /*
197  * The file consists of a set of extents all of which fit into the on-disk
198  * inode.
199  */
200 STATIC int
201 xfs_iformat_extents(
202 	struct xfs_inode	*ip,
203 	struct xfs_dinode	*dip,
204 	int			whichfork)
205 {
206 	struct xfs_mount	*mp = ip->i_mount;
207 	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
208 	int			state = xfs_bmap_fork_to_state(whichfork);
209 	int			nex = XFS_DFORK_NEXTENTS(dip, whichfork);
210 	int			size = nex * sizeof(xfs_bmbt_rec_t);
211 	struct xfs_iext_cursor	icur;
212 	struct xfs_bmbt_rec	*dp;
213 	struct xfs_bmbt_irec	new;
214 	int			i;
215 
216 	/*
217 	 * If the number of extents is unreasonable, then something is wrong and
218 	 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
219 	 */
220 	if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
221 		xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
222 			(unsigned long long) ip->i_ino, nex);
223 		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
224 				"xfs_iformat_extents(1)", dip, sizeof(*dip),
225 				__this_address);
226 		return -EFSCORRUPTED;
227 	}
228 
229 	ifp->if_real_bytes = 0;
230 	ifp->if_bytes = 0;
231 	ifp->if_u1.if_root = NULL;
232 	ifp->if_height = 0;
233 	if (size) {
234 		dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
235 
236 		xfs_iext_first(ifp, &icur);
237 		for (i = 0; i < nex; i++, dp++) {
238 			xfs_failaddr_t	fa;
239 
240 			xfs_bmbt_disk_get_all(dp, &new);
241 			fa = xfs_bmap_validate_extent(ip, whichfork, &new);
242 			if (fa) {
243 				xfs_inode_verifier_error(ip, -EFSCORRUPTED,
244 						"xfs_iformat_extents(2)",
245 						dp, sizeof(*dp), fa);
246 				return -EFSCORRUPTED;
247 			}
248 
249 			xfs_iext_insert(ip, &icur, &new, state);
250 			trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
251 			xfs_iext_next(ifp, &icur);
252 		}
253 	}
254 	ifp->if_flags |= XFS_IFEXTENTS;
255 	return 0;
256 }
257 
258 /*
259  * The file has too many extents to fit into
260  * the inode, so they are in B-tree format.
261  * Allocate a buffer for the root of the B-tree
262  * and copy the root into it.  The i_extents
263  * field will remain NULL until all of the
264  * extents are read in (when they are needed).
265  */
266 STATIC int
267 xfs_iformat_btree(
268 	xfs_inode_t		*ip,
269 	xfs_dinode_t		*dip,
270 	int			whichfork)
271 {
272 	struct xfs_mount	*mp = ip->i_mount;
273 	xfs_bmdr_block_t	*dfp;
274 	xfs_ifork_t		*ifp;
275 	/* REFERENCED */
276 	int			nrecs;
277 	int			size;
278 	int			level;
279 
280 	ifp = XFS_IFORK_PTR(ip, whichfork);
281 	dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
282 	size = XFS_BMAP_BROOT_SPACE(mp, dfp);
283 	nrecs = be16_to_cpu(dfp->bb_numrecs);
284 	level = be16_to_cpu(dfp->bb_level);
285 
286 	/*
287 	 * blow out if -- fork has less extents than can fit in
288 	 * fork (fork shouldn't be a btree format), root btree
289 	 * block has more records than can fit into the fork,
290 	 * or the number of extents is greater than the number of
291 	 * blocks.
292 	 */
293 	if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
294 					XFS_IFORK_MAXEXT(ip, whichfork) ||
295 		     nrecs == 0 ||
296 		     XFS_BMDR_SPACE_CALC(nrecs) >
297 					XFS_DFORK_SIZE(dip, mp, whichfork) ||
298 		     XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
299 		     level == 0 || level > XFS_BTREE_MAXLEVELS) {
300 		xfs_warn(mp, "corrupt inode %Lu (btree).",
301 					(unsigned long long) ip->i_ino);
302 		xfs_inode_verifier_error(ip, -EFSCORRUPTED,
303 				"xfs_iformat_btree", dfp, size,
304 				__this_address);
305 		return -EFSCORRUPTED;
306 	}
307 
308 	ifp->if_broot_bytes = size;
309 	ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
310 	ASSERT(ifp->if_broot != NULL);
311 	/*
312 	 * Copy and convert from the on-disk structure
313 	 * to the in-memory structure.
314 	 */
315 	xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
316 			 ifp->if_broot, size);
317 	ifp->if_flags &= ~XFS_IFEXTENTS;
318 	ifp->if_flags |= XFS_IFBROOT;
319 
320 	ifp->if_real_bytes = 0;
321 	ifp->if_bytes = 0;
322 	ifp->if_u1.if_root = NULL;
323 	ifp->if_height = 0;
324 	return 0;
325 }
326 
327 /*
328  * Reallocate the space for if_broot based on the number of records
329  * being added or deleted as indicated in rec_diff.  Move the records
330  * and pointers in if_broot to fit the new size.  When shrinking this
331  * will eliminate holes between the records and pointers created by
332  * the caller.  When growing this will create holes to be filled in
333  * by the caller.
334  *
335  * The caller must not request to add more records than would fit in
336  * the on-disk inode root.  If the if_broot is currently NULL, then
337  * if we are adding records, one will be allocated.  The caller must also
338  * not request that the number of records go below zero, although
339  * it can go to zero.
340  *
341  * ip -- the inode whose if_broot area is changing
342  * ext_diff -- the change in the number of records, positive or negative,
343  *	 requested for the if_broot array.
344  */
345 void
346 xfs_iroot_realloc(
347 	xfs_inode_t		*ip,
348 	int			rec_diff,
349 	int			whichfork)
350 {
351 	struct xfs_mount	*mp = ip->i_mount;
352 	int			cur_max;
353 	xfs_ifork_t		*ifp;
354 	struct xfs_btree_block	*new_broot;
355 	int			new_max;
356 	size_t			new_size;
357 	char			*np;
358 	char			*op;
359 
360 	/*
361 	 * Handle the degenerate case quietly.
362 	 */
363 	if (rec_diff == 0) {
364 		return;
365 	}
366 
367 	ifp = XFS_IFORK_PTR(ip, whichfork);
368 	if (rec_diff > 0) {
369 		/*
370 		 * If there wasn't any memory allocated before, just
371 		 * allocate it now and get out.
372 		 */
373 		if (ifp->if_broot_bytes == 0) {
374 			new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
375 			ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
376 			ifp->if_broot_bytes = (int)new_size;
377 			return;
378 		}
379 
380 		/*
381 		 * If there is already an existing if_broot, then we need
382 		 * to realloc() it and shift the pointers to their new
383 		 * location.  The records don't change location because
384 		 * they are kept butted up against the btree block header.
385 		 */
386 		cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
387 		new_max = cur_max + rec_diff;
388 		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
389 		ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
390 				KM_SLEEP | KM_NOFS);
391 		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
392 						     ifp->if_broot_bytes);
393 		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
394 						     (int)new_size);
395 		ifp->if_broot_bytes = (int)new_size;
396 		ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
397 			XFS_IFORK_SIZE(ip, whichfork));
398 		memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
399 		return;
400 	}
401 
402 	/*
403 	 * rec_diff is less than 0.  In this case, we are shrinking the
404 	 * if_broot buffer.  It must already exist.  If we go to zero
405 	 * records, just get rid of the root and clear the status bit.
406 	 */
407 	ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
408 	cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
409 	new_max = cur_max + rec_diff;
410 	ASSERT(new_max >= 0);
411 	if (new_max > 0)
412 		new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
413 	else
414 		new_size = 0;
415 	if (new_size > 0) {
416 		new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
417 		/*
418 		 * First copy over the btree block header.
419 		 */
420 		memcpy(new_broot, ifp->if_broot,
421 			XFS_BMBT_BLOCK_LEN(ip->i_mount));
422 	} else {
423 		new_broot = NULL;
424 		ifp->if_flags &= ~XFS_IFBROOT;
425 	}
426 
427 	/*
428 	 * Only copy the records and pointers if there are any.
429 	 */
430 	if (new_max > 0) {
431 		/*
432 		 * First copy the records.
433 		 */
434 		op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
435 		np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
436 		memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
437 
438 		/*
439 		 * Then copy the pointers.
440 		 */
441 		op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
442 						     ifp->if_broot_bytes);
443 		np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
444 						     (int)new_size);
445 		memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
446 	}
447 	kmem_free(ifp->if_broot);
448 	ifp->if_broot = new_broot;
449 	ifp->if_broot_bytes = (int)new_size;
450 	if (ifp->if_broot)
451 		ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
452 			XFS_IFORK_SIZE(ip, whichfork));
453 	return;
454 }
455 
456 
457 /*
458  * This is called when the amount of space needed for if_data
459  * is increased or decreased.  The change in size is indicated by
460  * the number of bytes that need to be added or deleted in the
461  * byte_diff parameter.
462  *
463  * If the amount of space needed has decreased below the size of the
464  * inline buffer, then switch to using the inline buffer.  Otherwise,
465  * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
466  * to what is needed.
467  *
468  * ip -- the inode whose if_data area is changing
469  * byte_diff -- the change in the number of bytes, positive or negative,
470  *	 requested for the if_data array.
471  */
472 void
473 xfs_idata_realloc(
474 	xfs_inode_t	*ip,
475 	int		byte_diff,
476 	int		whichfork)
477 {
478 	xfs_ifork_t	*ifp;
479 	int		new_size;
480 	int		real_size;
481 
482 	if (byte_diff == 0) {
483 		return;
484 	}
485 
486 	ifp = XFS_IFORK_PTR(ip, whichfork);
487 	new_size = (int)ifp->if_bytes + byte_diff;
488 	ASSERT(new_size >= 0);
489 
490 	if (new_size == 0) {
491 		kmem_free(ifp->if_u1.if_data);
492 		ifp->if_u1.if_data = NULL;
493 		real_size = 0;
494 	} else {
495 		/*
496 		 * Stuck with malloc/realloc.
497 		 * For inline data, the underlying buffer must be
498 		 * a multiple of 4 bytes in size so that it can be
499 		 * logged and stay on word boundaries.  We enforce
500 		 * that here.
501 		 */
502 		real_size = roundup(new_size, 4);
503 		if (ifp->if_u1.if_data == NULL) {
504 			ASSERT(ifp->if_real_bytes == 0);
505 			ifp->if_u1.if_data = kmem_alloc(real_size,
506 							KM_SLEEP | KM_NOFS);
507 		} else {
508 			/*
509 			 * Only do the realloc if the underlying size
510 			 * is really changing.
511 			 */
512 			if (ifp->if_real_bytes != real_size) {
513 				ifp->if_u1.if_data =
514 					kmem_realloc(ifp->if_u1.if_data,
515 							real_size,
516 							KM_SLEEP | KM_NOFS);
517 			}
518 		}
519 	}
520 	ifp->if_real_bytes = real_size;
521 	ifp->if_bytes = new_size;
522 	ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
523 }
524 
525 void
526 xfs_idestroy_fork(
527 	xfs_inode_t	*ip,
528 	int		whichfork)
529 {
530 	xfs_ifork_t	*ifp;
531 
532 	ifp = XFS_IFORK_PTR(ip, whichfork);
533 	if (ifp->if_broot != NULL) {
534 		kmem_free(ifp->if_broot);
535 		ifp->if_broot = NULL;
536 	}
537 
538 	/*
539 	 * If the format is local, then we can't have an extents
540 	 * array so just look for an inline data array.  If we're
541 	 * not local then we may or may not have an extents list,
542 	 * so check and free it up if we do.
543 	 */
544 	if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
545 		if (ifp->if_u1.if_data != NULL) {
546 			ASSERT(ifp->if_real_bytes != 0);
547 			kmem_free(ifp->if_u1.if_data);
548 			ifp->if_u1.if_data = NULL;
549 			ifp->if_real_bytes = 0;
550 		}
551 	} else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
552 		xfs_iext_destroy(ifp);
553 	}
554 
555 	ASSERT(ifp->if_real_bytes == 0);
556 
557 	if (whichfork == XFS_ATTR_FORK) {
558 		kmem_zone_free(xfs_ifork_zone, ip->i_afp);
559 		ip->i_afp = NULL;
560 	} else if (whichfork == XFS_COW_FORK) {
561 		kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
562 		ip->i_cowfp = NULL;
563 	}
564 }
565 
566 /*
567  * Convert in-core extents to on-disk form
568  *
569  * In the case of the data fork, the in-core and on-disk fork sizes can be
570  * different due to delayed allocation extents. We only copy on-disk extents
571  * here, so callers must always use the physical fork size to determine the
572  * size of the buffer passed to this routine.  We will return the size actually
573  * used.
574  */
575 int
576 xfs_iextents_copy(
577 	struct xfs_inode	*ip,
578 	struct xfs_bmbt_rec	*dp,
579 	int			whichfork)
580 {
581 	int			state = xfs_bmap_fork_to_state(whichfork);
582 	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
583 	struct xfs_iext_cursor	icur;
584 	struct xfs_bmbt_irec	rec;
585 	int			copied = 0;
586 
587 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
588 	ASSERT(ifp->if_bytes > 0);
589 
590 	for_each_xfs_iext(ifp, &icur, &rec) {
591 		if (isnullstartblock(rec.br_startblock))
592 			continue;
593 		ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
594 		xfs_bmbt_disk_set_all(dp, &rec);
595 		trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
596 		copied += sizeof(struct xfs_bmbt_rec);
597 		dp++;
598 	}
599 
600 	ASSERT(copied > 0);
601 	ASSERT(copied <= ifp->if_bytes);
602 	return copied;
603 }
604 
605 /*
606  * Each of the following cases stores data into the same region
607  * of the on-disk inode, so only one of them can be valid at
608  * any given time. While it is possible to have conflicting formats
609  * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
610  * in EXTENTS format, this can only happen when the fork has
611  * changed formats after being modified but before being flushed.
612  * In these cases, the format always takes precedence, because the
613  * format indicates the current state of the fork.
614  */
615 void
616 xfs_iflush_fork(
617 	xfs_inode_t		*ip,
618 	xfs_dinode_t		*dip,
619 	xfs_inode_log_item_t	*iip,
620 	int			whichfork)
621 {
622 	char			*cp;
623 	xfs_ifork_t		*ifp;
624 	xfs_mount_t		*mp;
625 	static const short	brootflag[2] =
626 		{ XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
627 	static const short	dataflag[2] =
628 		{ XFS_ILOG_DDATA, XFS_ILOG_ADATA };
629 	static const short	extflag[2] =
630 		{ XFS_ILOG_DEXT, XFS_ILOG_AEXT };
631 
632 	if (!iip)
633 		return;
634 	ifp = XFS_IFORK_PTR(ip, whichfork);
635 	/*
636 	 * This can happen if we gave up in iformat in an error path,
637 	 * for the attribute fork.
638 	 */
639 	if (!ifp) {
640 		ASSERT(whichfork == XFS_ATTR_FORK);
641 		return;
642 	}
643 	cp = XFS_DFORK_PTR(dip, whichfork);
644 	mp = ip->i_mount;
645 	switch (XFS_IFORK_FORMAT(ip, whichfork)) {
646 	case XFS_DINODE_FMT_LOCAL:
647 		if ((iip->ili_fields & dataflag[whichfork]) &&
648 		    (ifp->if_bytes > 0)) {
649 			ASSERT(ifp->if_u1.if_data != NULL);
650 			ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
651 			memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
652 		}
653 		break;
654 
655 	case XFS_DINODE_FMT_EXTENTS:
656 		ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
657 		       !(iip->ili_fields & extflag[whichfork]));
658 		if ((iip->ili_fields & extflag[whichfork]) &&
659 		    (ifp->if_bytes > 0)) {
660 			ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
661 			(void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
662 				whichfork);
663 		}
664 		break;
665 
666 	case XFS_DINODE_FMT_BTREE:
667 		if ((iip->ili_fields & brootflag[whichfork]) &&
668 		    (ifp->if_broot_bytes > 0)) {
669 			ASSERT(ifp->if_broot != NULL);
670 			ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
671 			        XFS_IFORK_SIZE(ip, whichfork));
672 			xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
673 				(xfs_bmdr_block_t *)cp,
674 				XFS_DFORK_SIZE(dip, mp, whichfork));
675 		}
676 		break;
677 
678 	case XFS_DINODE_FMT_DEV:
679 		if (iip->ili_fields & XFS_ILOG_DEV) {
680 			ASSERT(whichfork == XFS_DATA_FORK);
681 			xfs_dinode_put_rdev(dip,
682 					linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
683 		}
684 		break;
685 
686 	default:
687 		ASSERT(0);
688 		break;
689 	}
690 }
691 
692 /* Convert bmap state flags to an inode fork. */
693 struct xfs_ifork *
694 xfs_iext_state_to_fork(
695 	struct xfs_inode	*ip,
696 	int			state)
697 {
698 	if (state & BMAP_COWFORK)
699 		return ip->i_cowfp;
700 	else if (state & BMAP_ATTRFORK)
701 		return ip->i_afp;
702 	return &ip->i_df;
703 }
704 
705 /*
706  * Initialize an inode's copy-on-write fork.
707  */
708 void
709 xfs_ifork_init_cow(
710 	struct xfs_inode	*ip)
711 {
712 	if (ip->i_cowfp)
713 		return;
714 
715 	ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
716 				       KM_SLEEP | KM_NOFS);
717 	ip->i_cowfp->if_flags = XFS_IFEXTENTS;
718 	ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
719 	ip->i_cnextents = 0;
720 }
721 
722 /* Default fork content verifiers. */
723 struct xfs_ifork_ops xfs_default_ifork_ops = {
724 	.verify_attr	= xfs_attr_shortform_verify,
725 	.verify_dir	= xfs_dir2_sf_verify,
726 	.verify_symlink	= xfs_symlink_shortform_verify,
727 };
728 
729 /* Verify the inline contents of the data fork of an inode. */
730 xfs_failaddr_t
731 xfs_ifork_verify_data(
732 	struct xfs_inode	*ip,
733 	struct xfs_ifork_ops	*ops)
734 {
735 	/* Non-local data fork, we're done. */
736 	if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
737 		return NULL;
738 
739 	/* Check the inline data fork if there is one. */
740 	switch (VFS_I(ip)->i_mode & S_IFMT) {
741 	case S_IFDIR:
742 		return ops->verify_dir(ip);
743 	case S_IFLNK:
744 		return ops->verify_symlink(ip);
745 	default:
746 		return NULL;
747 	}
748 }
749 
750 /* Verify the inline contents of the attr fork of an inode. */
751 xfs_failaddr_t
752 xfs_ifork_verify_attr(
753 	struct xfs_inode	*ip,
754 	struct xfs_ifork_ops	*ops)
755 {
756 	/* There has to be an attr fork allocated if aformat is local. */
757 	if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
758 		return NULL;
759 	if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
760 		return __this_address;
761 	return ops->verify_attr(ip);
762 }
763