xref: /openbmc/linux/fs/xfs/libxfs/xfs_inode_buf.c (revision 1d27a0be)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_errortag.h"
15 #include "xfs_error.h"
16 #include "xfs_icache.h"
17 #include "xfs_trans.h"
18 #include "xfs_ialloc.h"
19 #include "xfs_dir2.h"
20 
21 #include <linux/iversion.h>
22 
23 /*
24  * Check that none of the inode's in the buffer have a next
25  * unlinked field of 0.
26  */
27 #if defined(DEBUG)
28 void
29 xfs_inobp_check(
30 	xfs_mount_t	*mp,
31 	xfs_buf_t	*bp)
32 {
33 	int		i;
34 	xfs_dinode_t	*dip;
35 
36 	for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) {
37 		dip = xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize);
38 		if (!dip->di_next_unlinked)  {
39 			xfs_alert(mp,
40 	"Detected bogus zero next_unlinked field in inode %d buffer 0x%llx.",
41 				i, (long long)bp->b_bn);
42 		}
43 	}
44 }
45 #endif
46 
47 /*
48  * If we are doing readahead on an inode buffer, we might be in log recovery
49  * reading an inode allocation buffer that hasn't yet been replayed, and hence
50  * has not had the inode cores stamped into it. Hence for readahead, the buffer
51  * may be potentially invalid.
52  *
53  * If the readahead buffer is invalid, we need to mark it with an error and
54  * clear the DONE status of the buffer so that a followup read will re-read it
55  * from disk. We don't report the error otherwise to avoid warnings during log
56  * recovery and we don't get unnecssary panics on debug kernels. We use EIO here
57  * because all we want to do is say readahead failed; there is no-one to report
58  * the error to, so this will distinguish it from a non-ra verifier failure.
59  * Changes to this readahead error behavour also need to be reflected in
60  * xfs_dquot_buf_readahead_verify().
61  */
62 static void
63 xfs_inode_buf_verify(
64 	struct xfs_buf	*bp,
65 	bool		readahead)
66 {
67 	struct xfs_mount *mp = bp->b_mount;
68 	xfs_agnumber_t	agno;
69 	int		i;
70 	int		ni;
71 
72 	/*
73 	 * Validate the magic number and version of every inode in the buffer
74 	 */
75 	agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
76 	ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
77 	for (i = 0; i < ni; i++) {
78 		int		di_ok;
79 		xfs_dinode_t	*dip;
80 		xfs_agino_t	unlinked_ino;
81 
82 		dip = xfs_buf_offset(bp, (i << mp->m_sb.sb_inodelog));
83 		unlinked_ino = be32_to_cpu(dip->di_next_unlinked);
84 		di_ok = xfs_verify_magic16(bp, dip->di_magic) &&
85 			xfs_dinode_good_version(&mp->m_sb, dip->di_version) &&
86 			xfs_verify_agino_or_null(mp, agno, unlinked_ino);
87 		if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
88 						XFS_ERRTAG_ITOBP_INOTOBP))) {
89 			if (readahead) {
90 				bp->b_flags &= ~XBF_DONE;
91 				xfs_buf_ioerror(bp, -EIO);
92 				return;
93 			}
94 
95 #ifdef DEBUG
96 			xfs_alert(mp,
97 				"bad inode magic/vsn daddr %lld #%d (magic=%x)",
98 				(unsigned long long)bp->b_bn, i,
99 				be16_to_cpu(dip->di_magic));
100 #endif
101 			xfs_buf_verifier_error(bp, -EFSCORRUPTED,
102 					__func__, dip, sizeof(*dip),
103 					NULL);
104 			return;
105 		}
106 	}
107 }
108 
109 
110 static void
111 xfs_inode_buf_read_verify(
112 	struct xfs_buf	*bp)
113 {
114 	xfs_inode_buf_verify(bp, false);
115 }
116 
117 static void
118 xfs_inode_buf_readahead_verify(
119 	struct xfs_buf	*bp)
120 {
121 	xfs_inode_buf_verify(bp, true);
122 }
123 
124 static void
125 xfs_inode_buf_write_verify(
126 	struct xfs_buf	*bp)
127 {
128 	xfs_inode_buf_verify(bp, false);
129 }
130 
131 const struct xfs_buf_ops xfs_inode_buf_ops = {
132 	.name = "xfs_inode",
133 	.magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
134 		     cpu_to_be16(XFS_DINODE_MAGIC) },
135 	.verify_read = xfs_inode_buf_read_verify,
136 	.verify_write = xfs_inode_buf_write_verify,
137 };
138 
139 const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
140 	.name = "xfs_inode_ra",
141 	.magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
142 		     cpu_to_be16(XFS_DINODE_MAGIC) },
143 	.verify_read = xfs_inode_buf_readahead_verify,
144 	.verify_write = xfs_inode_buf_write_verify,
145 };
146 
147 
148 /*
149  * This routine is called to map an inode to the buffer containing the on-disk
150  * version of the inode.  It returns a pointer to the buffer containing the
151  * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
152  * pointer to the on-disk inode within that buffer.
153  *
154  * If a non-zero error is returned, then the contents of bpp and dipp are
155  * undefined.
156  */
157 int
158 xfs_imap_to_bp(
159 	struct xfs_mount	*mp,
160 	struct xfs_trans	*tp,
161 	struct xfs_imap		*imap,
162 	struct xfs_dinode       **dipp,
163 	struct xfs_buf		**bpp,
164 	uint			buf_flags)
165 {
166 	struct xfs_buf		*bp;
167 	int			error;
168 
169 	buf_flags |= XBF_UNMAPPED;
170 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
171 				   (int)imap->im_len, buf_flags, &bp,
172 				   &xfs_inode_buf_ops);
173 	if (error) {
174 		ASSERT(error != -EAGAIN || (buf_flags & XBF_TRYLOCK));
175 		return error;
176 	}
177 
178 	*bpp = bp;
179 	*dipp = xfs_buf_offset(bp, imap->im_boffset);
180 	return 0;
181 }
182 
183 int
184 xfs_inode_from_disk(
185 	struct xfs_inode	*ip,
186 	struct xfs_dinode	*from)
187 {
188 	struct xfs_icdinode	*to = &ip->i_d;
189 	struct inode		*inode = VFS_I(ip);
190 	int			error;
191 	xfs_failaddr_t		fa;
192 
193 	ASSERT(ip->i_cowfp == NULL);
194 	ASSERT(ip->i_afp == NULL);
195 
196 	fa = xfs_dinode_verify(ip->i_mount, ip->i_ino, from);
197 	if (fa) {
198 		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "dinode", from,
199 				sizeof(*from), fa);
200 		return -EFSCORRUPTED;
201 	}
202 
203 	/*
204 	 * First get the permanent information that is needed to allocate an
205 	 * inode. If the inode is unused, mode is zero and we shouldn't mess
206 	 * with the unitialized part of it.
207 	 */
208 	to->di_flushiter = be16_to_cpu(from->di_flushiter);
209 	inode->i_generation = be32_to_cpu(from->di_gen);
210 	inode->i_mode = be16_to_cpu(from->di_mode);
211 	if (!inode->i_mode)
212 		return 0;
213 
214 	/*
215 	 * Convert v1 inodes immediately to v2 inode format as this is the
216 	 * minimum inode version format we support in the rest of the code.
217 	 * They will also be unconditionally written back to disk as v2 inodes.
218 	 */
219 	if (unlikely(from->di_version == 1)) {
220 		set_nlink(inode, be16_to_cpu(from->di_onlink));
221 		to->di_projid = 0;
222 	} else {
223 		set_nlink(inode, be32_to_cpu(from->di_nlink));
224 		to->di_projid = (prid_t)be16_to_cpu(from->di_projid_hi) << 16 |
225 					be16_to_cpu(from->di_projid_lo);
226 	}
227 
228 	i_uid_write(inode, be32_to_cpu(from->di_uid));
229 	i_gid_write(inode, be32_to_cpu(from->di_gid));
230 
231 	/*
232 	 * Time is signed, so need to convert to signed 32 bit before
233 	 * storing in inode timestamp which may be 64 bit. Otherwise
234 	 * a time before epoch is converted to a time long after epoch
235 	 * on 64 bit systems.
236 	 */
237 	inode->i_atime.tv_sec = (int)be32_to_cpu(from->di_atime.t_sec);
238 	inode->i_atime.tv_nsec = (int)be32_to_cpu(from->di_atime.t_nsec);
239 	inode->i_mtime.tv_sec = (int)be32_to_cpu(from->di_mtime.t_sec);
240 	inode->i_mtime.tv_nsec = (int)be32_to_cpu(from->di_mtime.t_nsec);
241 	inode->i_ctime.tv_sec = (int)be32_to_cpu(from->di_ctime.t_sec);
242 	inode->i_ctime.tv_nsec = (int)be32_to_cpu(from->di_ctime.t_nsec);
243 
244 	to->di_size = be64_to_cpu(from->di_size);
245 	to->di_nblocks = be64_to_cpu(from->di_nblocks);
246 	to->di_extsize = be32_to_cpu(from->di_extsize);
247 	to->di_forkoff = from->di_forkoff;
248 	to->di_dmevmask	= be32_to_cpu(from->di_dmevmask);
249 	to->di_dmstate	= be16_to_cpu(from->di_dmstate);
250 	to->di_flags	= be16_to_cpu(from->di_flags);
251 
252 	if (xfs_sb_version_has_v3inode(&ip->i_mount->m_sb)) {
253 		inode_set_iversion_queried(inode,
254 					   be64_to_cpu(from->di_changecount));
255 		to->di_crtime.tv_sec = be32_to_cpu(from->di_crtime.t_sec);
256 		to->di_crtime.tv_nsec = be32_to_cpu(from->di_crtime.t_nsec);
257 		to->di_flags2 = be64_to_cpu(from->di_flags2);
258 		to->di_cowextsize = be32_to_cpu(from->di_cowextsize);
259 	}
260 
261 	error = xfs_iformat_data_fork(ip, from);
262 	if (error)
263 		return error;
264 	if (from->di_forkoff) {
265 		error = xfs_iformat_attr_fork(ip, from);
266 		if (error)
267 			goto out_destroy_data_fork;
268 	}
269 	if (xfs_is_reflink_inode(ip))
270 		xfs_ifork_init_cow(ip);
271 	return 0;
272 
273 out_destroy_data_fork:
274 	xfs_idestroy_fork(&ip->i_df);
275 	return error;
276 }
277 
278 void
279 xfs_inode_to_disk(
280 	struct xfs_inode	*ip,
281 	struct xfs_dinode	*to,
282 	xfs_lsn_t		lsn)
283 {
284 	struct xfs_icdinode	*from = &ip->i_d;
285 	struct inode		*inode = VFS_I(ip);
286 
287 	to->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
288 	to->di_onlink = 0;
289 
290 	to->di_format = xfs_ifork_format(&ip->i_df);
291 	to->di_uid = cpu_to_be32(i_uid_read(inode));
292 	to->di_gid = cpu_to_be32(i_gid_read(inode));
293 	to->di_projid_lo = cpu_to_be16(from->di_projid & 0xffff);
294 	to->di_projid_hi = cpu_to_be16(from->di_projid >> 16);
295 
296 	memset(to->di_pad, 0, sizeof(to->di_pad));
297 	to->di_atime.t_sec = cpu_to_be32(inode->i_atime.tv_sec);
298 	to->di_atime.t_nsec = cpu_to_be32(inode->i_atime.tv_nsec);
299 	to->di_mtime.t_sec = cpu_to_be32(inode->i_mtime.tv_sec);
300 	to->di_mtime.t_nsec = cpu_to_be32(inode->i_mtime.tv_nsec);
301 	to->di_ctime.t_sec = cpu_to_be32(inode->i_ctime.tv_sec);
302 	to->di_ctime.t_nsec = cpu_to_be32(inode->i_ctime.tv_nsec);
303 	to->di_nlink = cpu_to_be32(inode->i_nlink);
304 	to->di_gen = cpu_to_be32(inode->i_generation);
305 	to->di_mode = cpu_to_be16(inode->i_mode);
306 
307 	to->di_size = cpu_to_be64(from->di_size);
308 	to->di_nblocks = cpu_to_be64(from->di_nblocks);
309 	to->di_extsize = cpu_to_be32(from->di_extsize);
310 	to->di_nextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_df));
311 	to->di_anextents = cpu_to_be16(xfs_ifork_nextents(ip->i_afp));
312 	to->di_forkoff = from->di_forkoff;
313 	to->di_aformat = xfs_ifork_format(ip->i_afp);
314 	to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
315 	to->di_dmstate = cpu_to_be16(from->di_dmstate);
316 	to->di_flags = cpu_to_be16(from->di_flags);
317 
318 	if (xfs_sb_version_has_v3inode(&ip->i_mount->m_sb)) {
319 		to->di_version = 3;
320 		to->di_changecount = cpu_to_be64(inode_peek_iversion(inode));
321 		to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.tv_sec);
322 		to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.tv_nsec);
323 		to->di_flags2 = cpu_to_be64(from->di_flags2);
324 		to->di_cowextsize = cpu_to_be32(from->di_cowextsize);
325 		to->di_ino = cpu_to_be64(ip->i_ino);
326 		to->di_lsn = cpu_to_be64(lsn);
327 		memset(to->di_pad2, 0, sizeof(to->di_pad2));
328 		uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
329 		to->di_flushiter = 0;
330 	} else {
331 		to->di_version = 2;
332 		to->di_flushiter = cpu_to_be16(from->di_flushiter);
333 	}
334 }
335 
336 void
337 xfs_log_dinode_to_disk(
338 	struct xfs_log_dinode	*from,
339 	struct xfs_dinode	*to)
340 {
341 	to->di_magic = cpu_to_be16(from->di_magic);
342 	to->di_mode = cpu_to_be16(from->di_mode);
343 	to->di_version = from->di_version;
344 	to->di_format = from->di_format;
345 	to->di_onlink = 0;
346 	to->di_uid = cpu_to_be32(from->di_uid);
347 	to->di_gid = cpu_to_be32(from->di_gid);
348 	to->di_nlink = cpu_to_be32(from->di_nlink);
349 	to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
350 	to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
351 	memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
352 
353 	to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
354 	to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
355 	to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
356 	to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
357 	to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
358 	to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
359 
360 	to->di_size = cpu_to_be64(from->di_size);
361 	to->di_nblocks = cpu_to_be64(from->di_nblocks);
362 	to->di_extsize = cpu_to_be32(from->di_extsize);
363 	to->di_nextents = cpu_to_be32(from->di_nextents);
364 	to->di_anextents = cpu_to_be16(from->di_anextents);
365 	to->di_forkoff = from->di_forkoff;
366 	to->di_aformat = from->di_aformat;
367 	to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
368 	to->di_dmstate = cpu_to_be16(from->di_dmstate);
369 	to->di_flags = cpu_to_be16(from->di_flags);
370 	to->di_gen = cpu_to_be32(from->di_gen);
371 
372 	if (from->di_version == 3) {
373 		to->di_changecount = cpu_to_be64(from->di_changecount);
374 		to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
375 		to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
376 		to->di_flags2 = cpu_to_be64(from->di_flags2);
377 		to->di_cowextsize = cpu_to_be32(from->di_cowextsize);
378 		to->di_ino = cpu_to_be64(from->di_ino);
379 		to->di_lsn = cpu_to_be64(from->di_lsn);
380 		memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
381 		uuid_copy(&to->di_uuid, &from->di_uuid);
382 		to->di_flushiter = 0;
383 	} else {
384 		to->di_flushiter = cpu_to_be16(from->di_flushiter);
385 	}
386 }
387 
388 static xfs_failaddr_t
389 xfs_dinode_verify_fork(
390 	struct xfs_dinode	*dip,
391 	struct xfs_mount	*mp,
392 	int			whichfork)
393 {
394 	uint32_t		di_nextents = XFS_DFORK_NEXTENTS(dip, whichfork);
395 
396 	switch (XFS_DFORK_FORMAT(dip, whichfork)) {
397 	case XFS_DINODE_FMT_LOCAL:
398 		/*
399 		 * no local regular files yet
400 		 */
401 		if (whichfork == XFS_DATA_FORK) {
402 			if (S_ISREG(be16_to_cpu(dip->di_mode)))
403 				return __this_address;
404 			if (be64_to_cpu(dip->di_size) >
405 					XFS_DFORK_SIZE(dip, mp, whichfork))
406 				return __this_address;
407 		}
408 		if (di_nextents)
409 			return __this_address;
410 		break;
411 	case XFS_DINODE_FMT_EXTENTS:
412 		if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
413 			return __this_address;
414 		break;
415 	case XFS_DINODE_FMT_BTREE:
416 		if (whichfork == XFS_ATTR_FORK) {
417 			if (di_nextents > MAXAEXTNUM)
418 				return __this_address;
419 		} else if (di_nextents > MAXEXTNUM) {
420 			return __this_address;
421 		}
422 		break;
423 	default:
424 		return __this_address;
425 	}
426 	return NULL;
427 }
428 
429 static xfs_failaddr_t
430 xfs_dinode_verify_forkoff(
431 	struct xfs_dinode	*dip,
432 	struct xfs_mount	*mp)
433 {
434 	if (!dip->di_forkoff)
435 		return NULL;
436 
437 	switch (dip->di_format)  {
438 	case XFS_DINODE_FMT_DEV:
439 		if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
440 			return __this_address;
441 		break;
442 	case XFS_DINODE_FMT_LOCAL:	/* fall through ... */
443 	case XFS_DINODE_FMT_EXTENTS:    /* fall through ... */
444 	case XFS_DINODE_FMT_BTREE:
445 		if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
446 			return __this_address;
447 		break;
448 	default:
449 		return __this_address;
450 	}
451 	return NULL;
452 }
453 
454 xfs_failaddr_t
455 xfs_dinode_verify(
456 	struct xfs_mount	*mp,
457 	xfs_ino_t		ino,
458 	struct xfs_dinode	*dip)
459 {
460 	xfs_failaddr_t		fa;
461 	uint16_t		mode;
462 	uint16_t		flags;
463 	uint64_t		flags2;
464 	uint64_t		di_size;
465 
466 	if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
467 		return __this_address;
468 
469 	/* Verify v3 integrity information first */
470 	if (dip->di_version >= 3) {
471 		if (!xfs_sb_version_has_v3inode(&mp->m_sb))
472 			return __this_address;
473 		if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
474 				      XFS_DINODE_CRC_OFF))
475 			return __this_address;
476 		if (be64_to_cpu(dip->di_ino) != ino)
477 			return __this_address;
478 		if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
479 			return __this_address;
480 	}
481 
482 	/* don't allow invalid i_size */
483 	di_size = be64_to_cpu(dip->di_size);
484 	if (di_size & (1ULL << 63))
485 		return __this_address;
486 
487 	mode = be16_to_cpu(dip->di_mode);
488 	if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
489 		return __this_address;
490 
491 	/* No zero-length symlinks/dirs. */
492 	if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0)
493 		return __this_address;
494 
495 	/* Fork checks carried over from xfs_iformat_fork */
496 	if (mode &&
497 	    be32_to_cpu(dip->di_nextents) + be16_to_cpu(dip->di_anextents) >
498 			be64_to_cpu(dip->di_nblocks))
499 		return __this_address;
500 
501 	if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
502 		return __this_address;
503 
504 	flags = be16_to_cpu(dip->di_flags);
505 
506 	if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
507 		return __this_address;
508 
509 	/* check for illegal values of forkoff */
510 	fa = xfs_dinode_verify_forkoff(dip, mp);
511 	if (fa)
512 		return fa;
513 
514 	/* Do we have appropriate data fork formats for the mode? */
515 	switch (mode & S_IFMT) {
516 	case S_IFIFO:
517 	case S_IFCHR:
518 	case S_IFBLK:
519 	case S_IFSOCK:
520 		if (dip->di_format != XFS_DINODE_FMT_DEV)
521 			return __this_address;
522 		break;
523 	case S_IFREG:
524 	case S_IFLNK:
525 	case S_IFDIR:
526 		fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
527 		if (fa)
528 			return fa;
529 		break;
530 	case 0:
531 		/* Uninitialized inode ok. */
532 		break;
533 	default:
534 		return __this_address;
535 	}
536 
537 	if (dip->di_forkoff) {
538 		fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
539 		if (fa)
540 			return fa;
541 	} else {
542 		/*
543 		 * If there is no fork offset, this may be a freshly-made inode
544 		 * in a new disk cluster, in which case di_aformat is zeroed.
545 		 * Otherwise, such an inode must be in EXTENTS format; this goes
546 		 * for freed inodes as well.
547 		 */
548 		switch (dip->di_aformat) {
549 		case 0:
550 		case XFS_DINODE_FMT_EXTENTS:
551 			break;
552 		default:
553 			return __this_address;
554 		}
555 		if (dip->di_anextents)
556 			return __this_address;
557 	}
558 
559 	/* extent size hint validation */
560 	fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
561 			mode, flags);
562 	if (fa)
563 		return fa;
564 
565 	/* only version 3 or greater inodes are extensively verified here */
566 	if (dip->di_version < 3)
567 		return NULL;
568 
569 	flags2 = be64_to_cpu(dip->di_flags2);
570 
571 	/* don't allow reflink/cowextsize if we don't have reflink */
572 	if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
573 	     !xfs_sb_version_hasreflink(&mp->m_sb))
574 		return __this_address;
575 
576 	/* only regular files get reflink */
577 	if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
578 		return __this_address;
579 
580 	/* don't let reflink and realtime mix */
581 	if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
582 		return __this_address;
583 
584 	/* don't let reflink and dax mix */
585 	if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags2 & XFS_DIFLAG2_DAX))
586 		return __this_address;
587 
588 	/* COW extent size hint validation */
589 	fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
590 			mode, flags, flags2);
591 	if (fa)
592 		return fa;
593 
594 	return NULL;
595 }
596 
597 void
598 xfs_dinode_calc_crc(
599 	struct xfs_mount	*mp,
600 	struct xfs_dinode	*dip)
601 {
602 	uint32_t		crc;
603 
604 	if (dip->di_version < 3)
605 		return;
606 
607 	ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
608 	crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
609 			      XFS_DINODE_CRC_OFF);
610 	dip->di_crc = xfs_end_cksum(crc);
611 }
612 
613 /*
614  * Validate di_extsize hint.
615  *
616  * The rules are documented at xfs_ioctl_setattr_check_extsize().
617  * These functions must be kept in sync with each other.
618  */
619 xfs_failaddr_t
620 xfs_inode_validate_extsize(
621 	struct xfs_mount		*mp,
622 	uint32_t			extsize,
623 	uint16_t			mode,
624 	uint16_t			flags)
625 {
626 	bool				rt_flag;
627 	bool				hint_flag;
628 	bool				inherit_flag;
629 	uint32_t			extsize_bytes;
630 	uint32_t			blocksize_bytes;
631 
632 	rt_flag = (flags & XFS_DIFLAG_REALTIME);
633 	hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
634 	inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
635 	extsize_bytes = XFS_FSB_TO_B(mp, extsize);
636 
637 	if (rt_flag)
638 		blocksize_bytes = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
639 	else
640 		blocksize_bytes = mp->m_sb.sb_blocksize;
641 
642 	if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
643 		return __this_address;
644 
645 	if (hint_flag && !S_ISREG(mode))
646 		return __this_address;
647 
648 	if (inherit_flag && !S_ISDIR(mode))
649 		return __this_address;
650 
651 	if ((hint_flag || inherit_flag) && extsize == 0)
652 		return __this_address;
653 
654 	/* free inodes get flags set to zero but extsize remains */
655 	if (mode && !(hint_flag || inherit_flag) && extsize != 0)
656 		return __this_address;
657 
658 	if (extsize_bytes % blocksize_bytes)
659 		return __this_address;
660 
661 	if (extsize > MAXEXTLEN)
662 		return __this_address;
663 
664 	if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
665 		return __this_address;
666 
667 	return NULL;
668 }
669 
670 /*
671  * Validate di_cowextsize hint.
672  *
673  * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
674  * These functions must be kept in sync with each other.
675  */
676 xfs_failaddr_t
677 xfs_inode_validate_cowextsize(
678 	struct xfs_mount		*mp,
679 	uint32_t			cowextsize,
680 	uint16_t			mode,
681 	uint16_t			flags,
682 	uint64_t			flags2)
683 {
684 	bool				rt_flag;
685 	bool				hint_flag;
686 	uint32_t			cowextsize_bytes;
687 
688 	rt_flag = (flags & XFS_DIFLAG_REALTIME);
689 	hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
690 	cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
691 
692 	if (hint_flag && !xfs_sb_version_hasreflink(&mp->m_sb))
693 		return __this_address;
694 
695 	if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
696 		return __this_address;
697 
698 	if (hint_flag && cowextsize == 0)
699 		return __this_address;
700 
701 	/* free inodes get flags set to zero but cowextsize remains */
702 	if (mode && !hint_flag && cowextsize != 0)
703 		return __this_address;
704 
705 	if (hint_flag && rt_flag)
706 		return __this_address;
707 
708 	if (cowextsize_bytes % mp->m_sb.sb_blocksize)
709 		return __this_address;
710 
711 	if (cowextsize > MAXEXTLEN)
712 		return __this_address;
713 
714 	if (cowextsize > mp->m_sb.sb_agblocks / 2)
715 		return __this_address;
716 
717 	return NULL;
718 }
719