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