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 */
38static void
39xfs_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
86static void
87xfs_inode_buf_read_verify(
88	struct xfs_buf	*bp)
89{
90	xfs_inode_buf_verify(bp, false);
91}
92
93static void
94xfs_inode_buf_readahead_verify(
95	struct xfs_buf	*bp)
96{
97	xfs_inode_buf_verify(bp, true);
98}
99
100static void
101xfs_inode_buf_write_verify(
102	struct xfs_buf	*bp)
103{
104	xfs_inode_buf_verify(bp, false);
105}
106
107const 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
115const 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 */
133int
134xfs_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
160static 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. */
172struct timespec64
173xfs_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
190int
191xfs_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
276out_destroy_data_fork:
277	xfs_idestroy_fork(&ip->i_df);
278	return error;
279}
280
281/* Convert an incore timestamp to an ondisk timestamp. */
282static inline xfs_timestamp_t
283xfs_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
300void
301xfs_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
354static xfs_failaddr_t
355xfs_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	mode_t			mode = be16_to_cpu(dip->di_mode);
362	uint32_t		fork_size = XFS_DFORK_SIZE(dip, mp, whichfork);
363	uint32_t		fork_format = XFS_DFORK_FORMAT(dip, whichfork);
364
365	/*
366	 * For fork types that can contain local data, check that the fork
367	 * format matches the size of local data contained within the fork.
368	 *
369	 * For all types, check that when the size says the should be in extent
370	 * or btree format, the inode isn't claiming it is in local format.
371	 */
372	if (whichfork == XFS_DATA_FORK) {
373		if (S_ISDIR(mode) || S_ISLNK(mode)) {
374			if (be64_to_cpu(dip->di_size) <= fork_size &&
375			    fork_format != XFS_DINODE_FMT_LOCAL)
376				return __this_address;
377		}
378
379		if (be64_to_cpu(dip->di_size) > fork_size &&
380		    fork_format == XFS_DINODE_FMT_LOCAL)
381			return __this_address;
382	}
383
384	switch (fork_format) {
385	case XFS_DINODE_FMT_LOCAL:
386		/*
387		 * No local regular files yet.
388		 */
389		if (S_ISREG(mode) && whichfork == XFS_DATA_FORK)
390			return __this_address;
391		if (di_nextents)
392			return __this_address;
393		break;
394	case XFS_DINODE_FMT_EXTENTS:
395		if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
396			return __this_address;
397		break;
398	case XFS_DINODE_FMT_BTREE:
399		if (whichfork == XFS_ATTR_FORK) {
400			if (di_nextents > MAXAEXTNUM)
401				return __this_address;
402		} else if (di_nextents > MAXEXTNUM) {
403			return __this_address;
404		}
405		break;
406	default:
407		return __this_address;
408	}
409	return NULL;
410}
411
412static xfs_failaddr_t
413xfs_dinode_verify_forkoff(
414	struct xfs_dinode	*dip,
415	struct xfs_mount	*mp)
416{
417	if (!dip->di_forkoff)
418		return NULL;
419
420	switch (dip->di_format)  {
421	case XFS_DINODE_FMT_DEV:
422		if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
423			return __this_address;
424		break;
425	case XFS_DINODE_FMT_LOCAL:	/* fall through ... */
426	case XFS_DINODE_FMT_EXTENTS:    /* fall through ... */
427	case XFS_DINODE_FMT_BTREE:
428		if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
429			return __this_address;
430		break;
431	default:
432		return __this_address;
433	}
434	return NULL;
435}
436
437xfs_failaddr_t
438xfs_dinode_verify(
439	struct xfs_mount	*mp,
440	xfs_ino_t		ino,
441	struct xfs_dinode	*dip)
442{
443	xfs_failaddr_t		fa;
444	uint16_t		mode;
445	uint16_t		flags;
446	uint64_t		flags2;
447	uint64_t		di_size;
448
449	if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
450		return __this_address;
451
452	/* Verify v3 integrity information first */
453	if (dip->di_version >= 3) {
454		if (!xfs_sb_version_has_v3inode(&mp->m_sb))
455			return __this_address;
456		if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
457				      XFS_DINODE_CRC_OFF))
458			return __this_address;
459		if (be64_to_cpu(dip->di_ino) != ino)
460			return __this_address;
461		if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
462			return __this_address;
463	}
464
465	/* don't allow invalid i_size */
466	di_size = be64_to_cpu(dip->di_size);
467	if (di_size & (1ULL << 63))
468		return __this_address;
469
470	mode = be16_to_cpu(dip->di_mode);
471	if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
472		return __this_address;
473
474	/* No zero-length symlinks/dirs. */
475	if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0)
476		return __this_address;
477
478	/* Fork checks carried over from xfs_iformat_fork */
479	if (mode &&
480	    be32_to_cpu(dip->di_nextents) + be16_to_cpu(dip->di_anextents) >
481			be64_to_cpu(dip->di_nblocks))
482		return __this_address;
483
484	if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
485		return __this_address;
486
487	flags = be16_to_cpu(dip->di_flags);
488
489	if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
490		return __this_address;
491
492	/* check for illegal values of forkoff */
493	fa = xfs_dinode_verify_forkoff(dip, mp);
494	if (fa)
495		return fa;
496
497	/* Do we have appropriate data fork formats for the mode? */
498	switch (mode & S_IFMT) {
499	case S_IFIFO:
500	case S_IFCHR:
501	case S_IFBLK:
502	case S_IFSOCK:
503		if (dip->di_format != XFS_DINODE_FMT_DEV)
504			return __this_address;
505		break;
506	case S_IFREG:
507	case S_IFLNK:
508	case S_IFDIR:
509		fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
510		if (fa)
511			return fa;
512		break;
513	case 0:
514		/* Uninitialized inode ok. */
515		break;
516	default:
517		return __this_address;
518	}
519
520	if (dip->di_forkoff) {
521		fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
522		if (fa)
523			return fa;
524	} else {
525		/*
526		 * If there is no fork offset, this may be a freshly-made inode
527		 * in a new disk cluster, in which case di_aformat is zeroed.
528		 * Otherwise, such an inode must be in EXTENTS format; this goes
529		 * for freed inodes as well.
530		 */
531		switch (dip->di_aformat) {
532		case 0:
533		case XFS_DINODE_FMT_EXTENTS:
534			break;
535		default:
536			return __this_address;
537		}
538		if (dip->di_anextents)
539			return __this_address;
540	}
541
542	/* extent size hint validation */
543	fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
544			mode, flags);
545	if (fa)
546		return fa;
547
548	/* only version 3 or greater inodes are extensively verified here */
549	if (dip->di_version < 3)
550		return NULL;
551
552	flags2 = be64_to_cpu(dip->di_flags2);
553
554	/* don't allow reflink/cowextsize if we don't have reflink */
555	if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
556	     !xfs_sb_version_hasreflink(&mp->m_sb))
557		return __this_address;
558
559	/* only regular files get reflink */
560	if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
561		return __this_address;
562
563	/* don't let reflink and realtime mix */
564	if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
565		return __this_address;
566
567	/* don't let reflink and dax mix */
568	if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags2 & XFS_DIFLAG2_DAX))
569		return __this_address;
570
571	/* COW extent size hint validation */
572	fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
573			mode, flags, flags2);
574	if (fa)
575		return fa;
576
577	/* bigtime iflag can only happen on bigtime filesystems */
578	if (xfs_dinode_has_bigtime(dip) &&
579	    !xfs_sb_version_hasbigtime(&mp->m_sb))
580		return __this_address;
581
582	return NULL;
583}
584
585void
586xfs_dinode_calc_crc(
587	struct xfs_mount	*mp,
588	struct xfs_dinode	*dip)
589{
590	uint32_t		crc;
591
592	if (dip->di_version < 3)
593		return;
594
595	ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
596	crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
597			      XFS_DINODE_CRC_OFF);
598	dip->di_crc = xfs_end_cksum(crc);
599}
600
601/*
602 * Validate di_extsize hint.
603 *
604 * The rules are documented at xfs_ioctl_setattr_check_extsize().
605 * These functions must be kept in sync with each other.
606 */
607xfs_failaddr_t
608xfs_inode_validate_extsize(
609	struct xfs_mount		*mp,
610	uint32_t			extsize,
611	uint16_t			mode,
612	uint16_t			flags)
613{
614	bool				rt_flag;
615	bool				hint_flag;
616	bool				inherit_flag;
617	uint32_t			extsize_bytes;
618	uint32_t			blocksize_bytes;
619
620	rt_flag = (flags & XFS_DIFLAG_REALTIME);
621	hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
622	inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
623	extsize_bytes = XFS_FSB_TO_B(mp, extsize);
624
625	if (rt_flag)
626		blocksize_bytes = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
627	else
628		blocksize_bytes = mp->m_sb.sb_blocksize;
629
630	if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
631		return __this_address;
632
633	if (hint_flag && !S_ISREG(mode))
634		return __this_address;
635
636	if (inherit_flag && !S_ISDIR(mode))
637		return __this_address;
638
639	if ((hint_flag || inherit_flag) && extsize == 0)
640		return __this_address;
641
642	/* free inodes get flags set to zero but extsize remains */
643	if (mode && !(hint_flag || inherit_flag) && extsize != 0)
644		return __this_address;
645
646	if (extsize_bytes % blocksize_bytes)
647		return __this_address;
648
649	if (extsize > MAXEXTLEN)
650		return __this_address;
651
652	if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
653		return __this_address;
654
655	return NULL;
656}
657
658/*
659 * Validate di_cowextsize hint.
660 *
661 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
662 * These functions must be kept in sync with each other.
663 */
664xfs_failaddr_t
665xfs_inode_validate_cowextsize(
666	struct xfs_mount		*mp,
667	uint32_t			cowextsize,
668	uint16_t			mode,
669	uint16_t			flags,
670	uint64_t			flags2)
671{
672	bool				rt_flag;
673	bool				hint_flag;
674	uint32_t			cowextsize_bytes;
675
676	rt_flag = (flags & XFS_DIFLAG_REALTIME);
677	hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
678	cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
679
680	if (hint_flag && !xfs_sb_version_hasreflink(&mp->m_sb))
681		return __this_address;
682
683	if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
684		return __this_address;
685
686	if (hint_flag && cowextsize == 0)
687		return __this_address;
688
689	/* free inodes get flags set to zero but cowextsize remains */
690	if (mode && !hint_flag && cowextsize != 0)
691		return __this_address;
692
693	if (hint_flag && rt_flag)
694		return __this_address;
695
696	if (cowextsize_bytes % mp->m_sb.sb_blocksize)
697		return __this_address;
698
699	if (cowextsize > MAXEXTLEN)
700		return __this_address;
701
702	if (cowextsize > mp->m_sb.sb_agblocks / 2)
703		return __this_address;
704
705	return NULL;
706}
707