xref: /linux/fs/xfs/xfs_ioctl.c (revision 6271f6ea7f65191762efe17c1a7d33f8922e1eeb)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs_platform.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_rtalloc.h"
15 #include "xfs_iwalk.h"
16 #include "xfs_itable.h"
17 #include "xfs_error.h"
18 #include "xfs_da_format.h"
19 #include "xfs_da_btree.h"
20 #include "xfs_attr.h"
21 #include "xfs_bmap.h"
22 #include "xfs_bmap_util.h"
23 #include "xfs_fsops.h"
24 #include "xfs_discard.h"
25 #include "xfs_quota.h"
26 #include "xfs_trace.h"
27 #include "xfs_icache.h"
28 #include "xfs_trans.h"
29 #include "xfs_btree.h"
30 #include <linux/fsmap.h>
31 #include "xfs_fsmap.h"
32 #include "scrub/xfs_scrub.h"
33 #include "xfs_sb.h"
34 #include "xfs_ag.h"
35 #include "xfs_health.h"
36 #include "xfs_reflink.h"
37 #include "xfs_ioctl.h"
38 #include "xfs_xattr.h"
39 #include "xfs_rtbitmap.h"
40 #include "xfs_rtrmap_btree.h"
41 #include "xfs_file.h"
42 #include "xfs_exchrange.h"
43 #include "xfs_handle.h"
44 #include "xfs_rtgroup.h"
45 #include "xfs_healthmon.h"
46 #include "xfs_verify_media.h"
47 #include "xfs_zone_priv.h"
48 #include "xfs_zone_alloc.h"
49 
50 #include <linux/mount.h>
51 #include <linux/fileattr.h>
52 
53 /* Return 0 on success or positive error */
54 int
55 xfs_fsbulkstat_one_fmt(
56 	struct xfs_ibulk		*breq,
57 	const struct xfs_bulkstat	*bstat)
58 {
59 	struct xfs_bstat		bs1;
60 
61 	xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
62 	if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
63 		return -EFAULT;
64 	return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
65 }
66 
67 int
68 xfs_fsinumbers_fmt(
69 	struct xfs_ibulk		*breq,
70 	const struct xfs_inumbers	*igrp)
71 {
72 	struct xfs_inogrp		ig1;
73 
74 	xfs_inumbers_to_inogrp(&ig1, igrp);
75 	if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
76 		return -EFAULT;
77 	return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
78 }
79 
80 STATIC int
81 xfs_ioc_fsbulkstat(
82 	struct file		*file,
83 	unsigned int		cmd,
84 	void			__user *arg)
85 {
86 	struct xfs_mount	*mp = XFS_I(file_inode(file))->i_mount;
87 	struct xfs_fsop_bulkreq	bulkreq;
88 	struct xfs_ibulk	breq = {
89 		.mp		= mp,
90 		.idmap		= file_mnt_idmap(file),
91 		.ocount		= 0,
92 	};
93 	xfs_ino_t		lastino;
94 	int			error;
95 
96 	/* done = 1 if there are more stats to get and if bulkstat */
97 	/* should be called again (unused here, but used in dmapi) */
98 
99 	if (!capable(CAP_SYS_ADMIN))
100 		return -EPERM;
101 
102 	if (xfs_is_shutdown(mp))
103 		return -EIO;
104 
105 	if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
106 		return -EFAULT;
107 
108 	if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
109 		return -EFAULT;
110 
111 	if (bulkreq.icount <= 0)
112 		return -EINVAL;
113 
114 	if (bulkreq.ubuffer == NULL)
115 		return -EINVAL;
116 
117 	breq.ubuffer = bulkreq.ubuffer;
118 	breq.icount = bulkreq.icount;
119 
120 	/*
121 	 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
122 	 * that we want to stat.  However, FSINUMBERS and FSBULKSTAT expect
123 	 * that *lastip contains either zero or the number of the last inode to
124 	 * be examined by the previous call and return results starting with
125 	 * the next inode after that.  The new bulk request back end functions
126 	 * take the inode to start with, so we have to compute the startino
127 	 * parameter from lastino to maintain correct function.  lastino == 0
128 	 * is a special case because it has traditionally meant "first inode
129 	 * in filesystem".
130 	 */
131 	if (cmd == XFS_IOC_FSINUMBERS) {
132 		breq.startino = lastino ? lastino + 1 : 0;
133 		error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
134 		lastino = breq.startino - 1;
135 	} else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
136 		breq.startino = lastino;
137 		breq.icount = 1;
138 		error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
139 	} else {	/* XFS_IOC_FSBULKSTAT */
140 		breq.startino = lastino ? lastino + 1 : 0;
141 		error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
142 		lastino = breq.startino - 1;
143 	}
144 
145 	if (error)
146 		return error;
147 
148 	if (bulkreq.lastip != NULL &&
149 	    copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
150 		return -EFAULT;
151 
152 	if (bulkreq.ocount != NULL &&
153 	    copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
154 		return -EFAULT;
155 
156 	return 0;
157 }
158 
159 /* Return 0 on success or positive error */
160 static int
161 xfs_bulkstat_fmt(
162 	struct xfs_ibulk		*breq,
163 	const struct xfs_bulkstat	*bstat)
164 {
165 	if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
166 		return -EFAULT;
167 	return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
168 }
169 
170 /*
171  * Check the incoming bulk request @hdr from userspace and initialize the
172  * internal @breq bulk request appropriately.  Returns 0 if the bulk request
173  * should proceed; -ECANCELED if there's nothing to do; or the usual
174  * negative error code.
175  */
176 static int
177 xfs_bulk_ireq_setup(
178 	struct xfs_mount	*mp,
179 	const struct xfs_bulk_ireq *hdr,
180 	struct xfs_ibulk	*breq,
181 	void __user		*ubuffer)
182 {
183 	if (hdr->icount == 0 ||
184 	    (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
185 	    memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
186 		return -EINVAL;
187 
188 	breq->startino = hdr->ino;
189 	breq->ubuffer = ubuffer;
190 	breq->icount = hdr->icount;
191 	breq->ocount = 0;
192 	breq->flags = 0;
193 
194 	/*
195 	 * The @ino parameter is a special value, so we must look it up here.
196 	 * We're not allowed to have IREQ_AGNO, and we only return one inode
197 	 * worth of data.
198 	 */
199 	if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
200 		if (hdr->flags & XFS_BULK_IREQ_AGNO)
201 			return -EINVAL;
202 
203 		switch (hdr->ino) {
204 		case XFS_BULK_IREQ_SPECIAL_ROOT:
205 			breq->startino = mp->m_sb.sb_rootino;
206 			break;
207 		default:
208 			return -EINVAL;
209 		}
210 		breq->icount = 1;
211 	}
212 
213 	/*
214 	 * The IREQ_AGNO flag means that we only want results from a given AG.
215 	 * If @hdr->ino is zero, we start iterating in that AG.  If @hdr->ino is
216 	 * beyond the specified AG then we return no results.
217 	 */
218 	if (hdr->flags & XFS_BULK_IREQ_AGNO) {
219 		if (hdr->agno >= mp->m_sb.sb_agcount)
220 			return -EINVAL;
221 
222 		if (breq->startino == 0)
223 			breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
224 		else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
225 			return -EINVAL;
226 
227 		breq->iwalk_flags |= XFS_IWALK_SAME_AG;
228 
229 		/* Asking for an inode past the end of the AG?  We're done! */
230 		if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
231 			return -ECANCELED;
232 	} else if (hdr->agno)
233 		return -EINVAL;
234 
235 	/* Asking for an inode past the end of the FS?  We're done! */
236 	if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
237 		return -ECANCELED;
238 
239 	if (hdr->flags & XFS_BULK_IREQ_NREXT64)
240 		breq->flags |= XFS_IBULK_NREXT64;
241 
242 	/* Caller wants to see metadata directories in bulkstat output. */
243 	if (hdr->flags & XFS_BULK_IREQ_METADIR)
244 		breq->flags |= XFS_IBULK_METADIR;
245 
246 	return 0;
247 }
248 
249 /*
250  * Update the userspace bulk request @hdr to reflect the end state of the
251  * internal bulk request @breq.
252  */
253 static void
254 xfs_bulk_ireq_teardown(
255 	struct xfs_bulk_ireq	*hdr,
256 	struct xfs_ibulk	*breq)
257 {
258 	hdr->ino = breq->startino;
259 	hdr->ocount = breq->ocount;
260 }
261 
262 /* Handle the v5 bulkstat ioctl. */
263 STATIC int
264 xfs_ioc_bulkstat(
265 	struct file			*file,
266 	unsigned int			cmd,
267 	struct xfs_bulkstat_req __user	*arg)
268 {
269 	struct xfs_mount		*mp = XFS_I(file_inode(file))->i_mount;
270 	struct xfs_bulk_ireq		hdr;
271 	struct xfs_ibulk		breq = {
272 		.mp			= mp,
273 		.idmap			= file_mnt_idmap(file),
274 	};
275 	int				error;
276 
277 	if (!capable(CAP_SYS_ADMIN))
278 		return -EPERM;
279 
280 	if (xfs_is_shutdown(mp))
281 		return -EIO;
282 
283 	if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
284 		return -EFAULT;
285 
286 	error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
287 	if (error == -ECANCELED)
288 		goto out_teardown;
289 	if (error < 0)
290 		return error;
291 
292 	error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
293 	if (error)
294 		return error;
295 
296 out_teardown:
297 	xfs_bulk_ireq_teardown(&hdr, &breq);
298 	if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
299 		return -EFAULT;
300 
301 	return 0;
302 }
303 
304 STATIC int
305 xfs_inumbers_fmt(
306 	struct xfs_ibulk		*breq,
307 	const struct xfs_inumbers	*igrp)
308 {
309 	if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
310 		return -EFAULT;
311 	return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
312 }
313 
314 /* Handle the v5 inumbers ioctl. */
315 STATIC int
316 xfs_ioc_inumbers(
317 	struct xfs_mount		*mp,
318 	unsigned int			cmd,
319 	struct xfs_inumbers_req __user	*arg)
320 {
321 	struct xfs_bulk_ireq		hdr;
322 	struct xfs_ibulk		breq = {
323 		.mp			= mp,
324 	};
325 	int				error;
326 
327 	if (!capable(CAP_SYS_ADMIN))
328 		return -EPERM;
329 
330 	if (xfs_is_shutdown(mp))
331 		return -EIO;
332 
333 	if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
334 		return -EFAULT;
335 
336 	if (hdr.flags & XFS_BULK_IREQ_METADIR)
337 		return -EINVAL;
338 
339 	error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
340 	if (error == -ECANCELED)
341 		goto out_teardown;
342 	if (error < 0)
343 		return error;
344 
345 	error = xfs_inumbers(&breq, xfs_inumbers_fmt);
346 	if (error)
347 		return error;
348 
349 out_teardown:
350 	xfs_bulk_ireq_teardown(&hdr, &breq);
351 	if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
352 		return -EFAULT;
353 
354 	return 0;
355 }
356 
357 STATIC int
358 xfs_ioc_fsgeometry(
359 	struct xfs_mount	*mp,
360 	void			__user *arg,
361 	int			struct_version)
362 {
363 	struct xfs_fsop_geom	fsgeo;
364 	size_t			len;
365 
366 	xfs_fs_geometry(mp, &fsgeo, struct_version);
367 
368 	if (struct_version <= 3)
369 		len = sizeof(struct xfs_fsop_geom_v1);
370 	else if (struct_version == 4)
371 		len = sizeof(struct xfs_fsop_geom_v4);
372 	else {
373 		xfs_fsop_geom_health(mp, &fsgeo);
374 		len = sizeof(fsgeo);
375 	}
376 
377 	if (copy_to_user(arg, &fsgeo, len))
378 		return -EFAULT;
379 	return 0;
380 }
381 
382 STATIC int
383 xfs_ioc_ag_geometry(
384 	struct xfs_mount	*mp,
385 	void			__user *arg)
386 {
387 	struct xfs_perag	*pag;
388 	struct xfs_ag_geometry	ageo;
389 	int			error;
390 
391 	if (copy_from_user(&ageo, arg, sizeof(ageo)))
392 		return -EFAULT;
393 	if (ageo.ag_flags)
394 		return -EINVAL;
395 	if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
396 		return -EINVAL;
397 
398 	pag = xfs_perag_get(mp, ageo.ag_number);
399 	if (!pag)
400 		return -EINVAL;
401 
402 	error = xfs_ag_get_geometry(pag, &ageo);
403 	xfs_perag_put(pag);
404 	if (error)
405 		return error;
406 
407 	if (copy_to_user(arg, &ageo, sizeof(ageo)))
408 		return -EFAULT;
409 	return 0;
410 }
411 
412 static void
413 xfs_rtgroup_report_write_pointer(
414 	struct xfs_rtgroup	*rtg,
415 	struct xfs_rtgroup_geometry *rgeo)
416 {
417 	xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
418 	if (rtg->rtg_open_zone) {
419 		rgeo->rg_writepointer = rtg->rtg_open_zone->oz_allocated;
420 	} else {
421 		xfs_rgblock_t	highest_rgbno = xfs_rtrmap_highest_rgbno(rtg);
422 
423 		if (highest_rgbno == NULLRGBLOCK)
424 			rgeo->rg_writepointer = 0;
425 		else
426 			rgeo->rg_writepointer = highest_rgbno + 1;
427 	}
428 	xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP);
429 	rgeo->rg_flags |= XFS_RTGROUP_GEOM_WRITEPOINTER;
430 }
431 
432 STATIC int
433 xfs_ioc_rtgroup_geometry(
434 	struct xfs_mount	*mp,
435 	void			__user *arg)
436 {
437 	struct xfs_rtgroup	*rtg;
438 	struct xfs_rtgroup_geometry rgeo;
439 	int			error;
440 
441 	if (copy_from_user(&rgeo, arg, sizeof(rgeo)))
442 		return -EFAULT;
443 	if (rgeo.rg_flags)
444 		return -EINVAL;
445 	if (memchr_inv(&rgeo.rg_reserved, 0, sizeof(rgeo.rg_reserved)))
446 		return -EINVAL;
447 	if (!xfs_has_rtgroups(mp))
448 		return -EINVAL;
449 
450 	rtg = xfs_rtgroup_get(mp, rgeo.rg_number);
451 	if (!rtg)
452 		return -EINVAL;
453 
454 	error = xfs_rtgroup_get_geometry(rtg, &rgeo);
455 	if (error)
456 		goto out_put_rtg;
457 	if (xfs_has_zoned(mp))
458 		xfs_rtgroup_report_write_pointer(rtg, &rgeo);
459 
460 	if (copy_to_user(arg, &rgeo, sizeof(rgeo)))
461 		error = -EFAULT;
462 out_put_rtg:
463 	xfs_rtgroup_put(rtg);
464 	return error;
465 }
466 
467 /*
468  * Linux extended inode flags interface.
469  */
470 
471 static void
472 xfs_fill_fsxattr(
473 	struct xfs_inode	*ip,
474 	int			whichfork,
475 	struct file_kattr	*fa)
476 {
477 	struct xfs_mount	*mp = ip->i_mount;
478 	struct xfs_ifork	*ifp = xfs_ifork_ptr(ip, whichfork);
479 
480 	fileattr_fill_xflags(fa, xfs_ip2xflags(ip));
481 
482 	if (ip->i_diflags & XFS_DIFLAG_EXTSIZE) {
483 		fa->fsx_extsize = XFS_FSB_TO_B(mp, ip->i_extsize);
484 	} else if (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
485 		/*
486 		 * Don't let a misaligned extent size hint on a directory
487 		 * escape to userspace if it won't pass the setattr checks
488 		 * later.
489 		 */
490 		if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
491 		    xfs_extlen_to_rtxmod(mp, ip->i_extsize) > 0) {
492 			fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE |
493 					    FS_XFLAG_EXTSZINHERIT);
494 			fa->fsx_extsize = 0;
495 		} else {
496 			fa->fsx_extsize = XFS_FSB_TO_B(mp, ip->i_extsize);
497 		}
498 	}
499 
500 	if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) {
501 		/*
502 		 * Don't let a misaligned CoW extent size hint on a directory
503 		 * escape to userspace if it won't pass the setattr checks
504 		 * later.
505 		 */
506 		if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
507 		    ip->i_cowextsize % mp->m_sb.sb_rextsize > 0) {
508 			fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
509 			fa->fsx_cowextsize = 0;
510 		} else {
511 			fa->fsx_cowextsize = XFS_FSB_TO_B(mp, ip->i_cowextsize);
512 		}
513 	}
514 
515 	fa->fsx_projid = ip->i_projid;
516 	if (ifp && !xfs_need_iread_extents(ifp))
517 		fa->fsx_nextents = xfs_iext_count(ifp);
518 	else
519 		fa->fsx_nextents = xfs_ifork_nextents(ifp);
520 }
521 
522 STATIC int
523 xfs_ioc_fsgetxattra(
524 	xfs_inode_t		*ip,
525 	void			__user *arg)
526 {
527 	struct file_kattr	fa = {};
528 
529 	xfs_ilock(ip, XFS_ILOCK_SHARED);
530 	xfs_fill_fsxattr(ip, XFS_ATTR_FORK, &fa);
531 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
532 
533 	return copy_fsxattr_to_user(&fa, arg);
534 }
535 
536 int
537 xfs_fileattr_get(
538 	struct dentry		*dentry,
539 	struct file_kattr	*fa)
540 {
541 	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
542 
543 	xfs_ilock(ip, XFS_ILOCK_SHARED);
544 	xfs_fill_fsxattr(ip, XFS_DATA_FORK, fa);
545 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
546 
547 	return 0;
548 }
549 
550 static int
551 xfs_ioctl_setattr_xflags(
552 	struct xfs_trans	*tp,
553 	struct xfs_inode	*ip,
554 	struct file_kattr	*fa)
555 {
556 	struct xfs_mount	*mp = ip->i_mount;
557 	bool			rtflag = (fa->fsx_xflags & FS_XFLAG_REALTIME);
558 	uint64_t		i_flags2;
559 
560 	if (rtflag != XFS_IS_REALTIME_INODE(ip)) {
561 		/* Can't change realtime flag if any extents are allocated. */
562 		if (xfs_inode_has_filedata(ip))
563 			return -EINVAL;
564 
565 		/*
566 		 * If S_DAX is enabled on this file, we can only switch the
567 		 * device if both support fsdax.  We can't update S_DAX because
568 		 * there might be other threads walking down the access paths.
569 		 */
570 		if (IS_DAX(VFS_I(ip)) &&
571 		    (mp->m_ddev_targp->bt_daxdev == NULL ||
572 		     (mp->m_rtdev_targp &&
573 		      mp->m_rtdev_targp->bt_daxdev == NULL)))
574 			return -EINVAL;
575 	}
576 
577 	if (rtflag) {
578 		/* If realtime flag is set then must have realtime device */
579 		if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
580 		    xfs_extlen_to_rtxmod(mp, ip->i_extsize))
581 			return -EINVAL;
582 	}
583 
584 	/* diflags2 only valid for v3 inodes. */
585 	i_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
586 	if (i_flags2 && !xfs_has_v3inodes(mp))
587 		return -EINVAL;
588 
589 	ip->i_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
590 	ip->i_diflags2 = i_flags2;
591 
592 	xfs_diflags_to_iflags(ip, false);
593 
594 	/*
595 	 * Make the stable writes flag match that of the device the inode
596 	 * resides on when flipping the RT flag.
597 	 */
598 	if (rtflag != XFS_IS_REALTIME_INODE(ip) && S_ISREG(VFS_I(ip)->i_mode))
599 		xfs_update_stable_writes(ip);
600 
601 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
602 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
603 	XFS_STATS_INC(mp, xs_ig_attrchg);
604 	return 0;
605 }
606 
607 static void
608 xfs_ioctl_setattr_prepare_dax(
609 	struct xfs_inode	*ip,
610 	struct file_kattr	*fa)
611 {
612 	struct xfs_mount	*mp = ip->i_mount;
613 	struct inode            *inode = VFS_I(ip);
614 
615 	if (S_ISDIR(inode->i_mode))
616 		return;
617 
618 	if (xfs_has_dax_always(mp) || xfs_has_dax_never(mp))
619 		return;
620 
621 	if (((fa->fsx_xflags & FS_XFLAG_DAX) &&
622 	    !(ip->i_diflags2 & XFS_DIFLAG2_DAX)) ||
623 	    (!(fa->fsx_xflags & FS_XFLAG_DAX) &&
624 	     (ip->i_diflags2 & XFS_DIFLAG2_DAX)))
625 		d_mark_dontcache(inode);
626 }
627 
628 /*
629  * Set up the transaction structure for the setattr operation, checking that we
630  * have permission to do so. On success, return a clean transaction and the
631  * inode locked exclusively ready for further operation specific checks. On
632  * failure, return an error without modifying or locking the inode.
633  */
634 static struct xfs_trans *
635 xfs_ioctl_setattr_get_trans(
636 	struct xfs_inode	*ip,
637 	struct xfs_dquot	*pdqp)
638 {
639 	struct xfs_mount	*mp = ip->i_mount;
640 	struct xfs_trans	*tp;
641 	int			error = -EROFS;
642 
643 	if (xfs_is_readonly(mp))
644 		goto out_error;
645 	error = -EIO;
646 	if (xfs_is_shutdown(mp))
647 		goto out_error;
648 
649 	error = xfs_trans_alloc_ichange(ip, NULL, NULL, pdqp,
650 			has_capability_noaudit(current, CAP_FOWNER), &tp);
651 	if (error)
652 		goto out_error;
653 
654 	if (xfs_has_wsync(mp))
655 		xfs_trans_set_sync(tp);
656 
657 	return tp;
658 
659 out_error:
660 	return ERR_PTR(error);
661 }
662 
663 /*
664  * Validate a proposed extent size hint.  For regular files, the hint can only
665  * be changed if no extents are allocated.
666  */
667 static int
668 xfs_ioctl_setattr_check_extsize(
669 	struct xfs_inode	*ip,
670 	struct file_kattr	*fa)
671 {
672 	struct xfs_mount	*mp = ip->i_mount;
673 	xfs_failaddr_t		failaddr;
674 	uint16_t		new_diflags;
675 
676 	if (!fa->fsx_valid)
677 		return 0;
678 
679 	if (S_ISREG(VFS_I(ip)->i_mode) && xfs_inode_has_filedata(ip) &&
680 	    XFS_FSB_TO_B(mp, ip->i_extsize) != fa->fsx_extsize)
681 		return -EINVAL;
682 
683 	if (fa->fsx_extsize & mp->m_blockmask)
684 		return -EINVAL;
685 
686 	new_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
687 
688 	/*
689 	 * Inode verifiers do not check that the extent size hint is an integer
690 	 * multiple of the rt extent size on a directory with both rtinherit
691 	 * and extszinherit flags set.  Don't let sysadmins misconfigure
692 	 * directories.
693 	 */
694 	if ((new_diflags & XFS_DIFLAG_RTINHERIT) &&
695 	    (new_diflags & XFS_DIFLAG_EXTSZINHERIT)) {
696 		unsigned int	rtextsize_bytes;
697 
698 		rtextsize_bytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
699 		if (fa->fsx_extsize % rtextsize_bytes)
700 			return -EINVAL;
701 	}
702 
703 	failaddr = xfs_inode_validate_extsize(ip->i_mount,
704 			XFS_B_TO_FSB(mp, fa->fsx_extsize),
705 			VFS_I(ip)->i_mode, new_diflags);
706 	return failaddr != NULL ? -EINVAL : 0;
707 }
708 
709 static int
710 xfs_ioctl_setattr_check_cowextsize(
711 	struct xfs_inode	*ip,
712 	struct file_kattr	*fa)
713 {
714 	struct xfs_mount	*mp = ip->i_mount;
715 	xfs_failaddr_t		failaddr;
716 	uint64_t		new_diflags2;
717 	uint16_t		new_diflags;
718 
719 	if (!fa->fsx_valid)
720 		return 0;
721 
722 	if (fa->fsx_cowextsize & mp->m_blockmask)
723 		return -EINVAL;
724 
725 	new_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
726 	new_diflags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
727 
728 	failaddr = xfs_inode_validate_cowextsize(ip->i_mount,
729 			XFS_B_TO_FSB(mp, fa->fsx_cowextsize),
730 			VFS_I(ip)->i_mode, new_diflags, new_diflags2);
731 	return failaddr != NULL ? -EINVAL : 0;
732 }
733 
734 static int
735 xfs_ioctl_setattr_check_projid(
736 	struct xfs_inode	*ip,
737 	struct file_kattr	*fa)
738 {
739 	if (!fa->fsx_valid)
740 		return 0;
741 
742 	/* Disallow 32bit project ids if 32bit IDs are not enabled. */
743 	if (fa->fsx_projid > (uint16_t)-1 &&
744 	    !xfs_has_projid32(ip->i_mount))
745 		return -EINVAL;
746 	return 0;
747 }
748 
749 int
750 xfs_fileattr_set(
751 	struct mnt_idmap	*idmap,
752 	struct dentry		*dentry,
753 	struct file_kattr	*fa)
754 {
755 	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
756 	struct xfs_mount	*mp = ip->i_mount;
757 	struct xfs_trans	*tp;
758 	struct xfs_dquot	*pdqp = NULL;
759 	struct xfs_dquot	*olddquot = NULL;
760 	int			error;
761 
762 	trace_xfs_ioctl_setattr(ip);
763 
764 	if (!fa->fsx_valid) {
765 		unsigned int allowed = FS_IMMUTABLE_FL | FS_APPEND_FL |
766 				       FS_NOATIME_FL | FS_NODUMP_FL |
767 				       FS_SYNC_FL | FS_DAX_FL |
768 				       FS_PROJINHERIT_FL;
769 
770 		/*
771 		 * FS_CASEFOLD_FL reflects the ASCIICI superblock feature,
772 		 * a read-only property. Accept it as a no-op so chattr's
773 		 * RMW round-trip succeeds; reject any attempt to enable
774 		 * it on a non-ASCIICI filesystem. xfs_flags2diflags()
775 		 * has no clause for CASEFOLD, so the bit is dropped from
776 		 * the on-disk diflags regardless.
777 		 */
778 		if (xfs_has_asciici(mp))
779 			allowed |= FS_CASEFOLD_FL;
780 
781 		if (fa->flags & ~allowed)
782 			return -EOPNOTSUPP;
783 	}
784 
785 	error = xfs_ioctl_setattr_check_projid(ip, fa);
786 	if (error)
787 		return error;
788 
789 	/*
790 	 * If disk quotas is on, we make sure that the dquots do exist on disk,
791 	 * before we start any other transactions. Trying to do this later
792 	 * is messy. We don't care to take a readlock to look at the ids
793 	 * in inode here, because we can't hold it across the trans_reserve.
794 	 * If the IDs do change before we take the ilock, we're covered
795 	 * because the i_*dquot fields will get updated anyway.
796 	 */
797 	if (fa->fsx_valid && XFS_IS_QUOTA_ON(mp)) {
798 		error = xfs_qm_vop_dqalloc(ip, VFS_I(ip)->i_uid,
799 				VFS_I(ip)->i_gid, fa->fsx_projid,
800 				XFS_QMOPT_PQUOTA, NULL, NULL, &pdqp);
801 		if (error)
802 			return error;
803 	}
804 
805 	xfs_ioctl_setattr_prepare_dax(ip, fa);
806 
807 	tp = xfs_ioctl_setattr_get_trans(ip, pdqp);
808 	if (IS_ERR(tp)) {
809 		error = PTR_ERR(tp);
810 		goto error_free_dquots;
811 	}
812 
813 	error = xfs_ioctl_setattr_check_extsize(ip, fa);
814 	if (error)
815 		goto error_trans_cancel;
816 
817 	error = xfs_ioctl_setattr_check_cowextsize(ip, fa);
818 	if (error)
819 		goto error_trans_cancel;
820 
821 	error = xfs_ioctl_setattr_xflags(tp, ip, fa);
822 	if (error)
823 		goto error_trans_cancel;
824 
825 	if (!fa->fsx_valid)
826 		goto skip_xattr;
827 	/*
828 	 * Change file ownership.  Must be the owner or privileged.  CAP_FSETID
829 	 * overrides the following restrictions:
830 	 *
831 	 * The set-user-ID and set-group-ID bits of a file will be cleared upon
832 	 * successful return from chown()
833 	 */
834 
835 	if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
836 	    !capable_wrt_inode_uidgid(idmap, VFS_I(ip), CAP_FSETID))
837 		VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
838 
839 	/* Change the ownerships and register project quota modifications */
840 	if (ip->i_projid != fa->fsx_projid) {
841 		if (XFS_IS_PQUOTA_ON(mp)) {
842 			olddquot = xfs_qm_vop_chown(tp, ip,
843 						&ip->i_pdquot, pdqp);
844 		}
845 		ip->i_projid = fa->fsx_projid;
846 	}
847 
848 	/*
849 	 * Only set the extent size hint if we've already determined that the
850 	 * extent size hint should be set on the inode. If no extent size flags
851 	 * are set on the inode then unconditionally clear the extent size hint.
852 	 */
853 	if (ip->i_diflags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
854 		ip->i_extsize = XFS_B_TO_FSB(mp, fa->fsx_extsize);
855 	else
856 		ip->i_extsize = 0;
857 
858 	if (xfs_has_v3inodes(mp)) {
859 		if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
860 			ip->i_cowextsize = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
861 		else
862 			ip->i_cowextsize = 0;
863 	}
864 
865 skip_xattr:
866 	error = xfs_trans_commit(tp);
867 
868 	/*
869 	 * Release any dquot(s) the inode had kept before chown.
870 	 */
871 	xfs_qm_dqrele(olddquot);
872 	xfs_qm_dqrele(pdqp);
873 
874 	return error;
875 
876 error_trans_cancel:
877 	xfs_trans_cancel(tp);
878 error_free_dquots:
879 	xfs_qm_dqrele(pdqp);
880 	return error;
881 }
882 
883 static bool
884 xfs_getbmap_format(
885 	struct kgetbmap		*p,
886 	struct getbmapx __user	*u,
887 	size_t			recsize)
888 {
889 	if (put_user(p->bmv_offset, &u->bmv_offset) ||
890 	    put_user(p->bmv_block, &u->bmv_block) ||
891 	    put_user(p->bmv_length, &u->bmv_length) ||
892 	    put_user(0, &u->bmv_count) ||
893 	    put_user(0, &u->bmv_entries))
894 		return false;
895 	if (recsize < sizeof(struct getbmapx))
896 		return true;
897 	if (put_user(0, &u->bmv_iflags) ||
898 	    put_user(p->bmv_oflags, &u->bmv_oflags) ||
899 	    put_user(0, &u->bmv_unused1) ||
900 	    put_user(0, &u->bmv_unused2))
901 		return false;
902 	return true;
903 }
904 
905 STATIC int
906 xfs_ioc_getbmap(
907 	struct file		*file,
908 	unsigned int		cmd,
909 	void			__user *arg)
910 {
911 	struct getbmapx		bmx = { 0 };
912 	struct kgetbmap		*buf;
913 	size_t			recsize;
914 	int			error, i;
915 
916 	switch (cmd) {
917 	case XFS_IOC_GETBMAPA:
918 		bmx.bmv_iflags = BMV_IF_ATTRFORK;
919 		fallthrough;
920 	case XFS_IOC_GETBMAP:
921 		/* struct getbmap is a strict subset of struct getbmapx. */
922 		recsize = sizeof(struct getbmap);
923 		break;
924 	case XFS_IOC_GETBMAPX:
925 		recsize = sizeof(struct getbmapx);
926 		break;
927 	default:
928 		return -EINVAL;
929 	}
930 
931 	if (copy_from_user(&bmx, arg, recsize))
932 		return -EFAULT;
933 
934 	if (bmx.bmv_count < 2)
935 		return -EINVAL;
936 	if (bmx.bmv_count >= INT_MAX / recsize)
937 		return -ENOMEM;
938 
939 	buf = kvzalloc_objs(*buf, bmx.bmv_count);
940 	if (!buf)
941 		return -ENOMEM;
942 
943 	error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
944 	if (error)
945 		goto out_free_buf;
946 
947 	error = -EFAULT;
948 	if (copy_to_user(arg, &bmx, recsize))
949 		goto out_free_buf;
950 	arg += recsize;
951 
952 	for (i = 0; i < bmx.bmv_entries; i++) {
953 		if (!xfs_getbmap_format(buf + i, arg, recsize))
954 			goto out_free_buf;
955 		arg += recsize;
956 	}
957 
958 	error = 0;
959 out_free_buf:
960 	kvfree(buf);
961 	return error;
962 }
963 
964 int
965 xfs_ioc_swapext(
966 	xfs_swapext_t	*sxp)
967 {
968 	xfs_inode_t     *ip, *tip;
969 
970 	/* Pull information for the target fd */
971 	CLASS(fd, f)((int)sxp->sx_fdtarget);
972 	if (fd_empty(f))
973 		return -EINVAL;
974 
975 	if (!(fd_file(f)->f_mode & FMODE_WRITE) ||
976 	    !(fd_file(f)->f_mode & FMODE_READ) ||
977 	    (fd_file(f)->f_flags & O_APPEND))
978 		return -EBADF;
979 
980 	CLASS(fd, tmp)((int)sxp->sx_fdtmp);
981 	if (fd_empty(tmp))
982 		return -EINVAL;
983 
984 	if (!(fd_file(tmp)->f_mode & FMODE_WRITE) ||
985 	    !(fd_file(tmp)->f_mode & FMODE_READ) ||
986 	    (fd_file(tmp)->f_flags & O_APPEND))
987 		return -EBADF;
988 
989 	if (IS_SWAPFILE(file_inode(fd_file(f))) ||
990 	    IS_SWAPFILE(file_inode(fd_file(tmp))))
991 		return -EINVAL;
992 
993 	/*
994 	 * We need to ensure that the fds passed in point to XFS inodes
995 	 * before we cast and access them as XFS structures as we have no
996 	 * control over what the user passes us here.
997 	 */
998 	if (fd_file(f)->f_op != &xfs_file_operations ||
999 	    fd_file(tmp)->f_op != &xfs_file_operations)
1000 		return -EINVAL;
1001 
1002 	ip = XFS_I(file_inode(fd_file(f)));
1003 	tip = XFS_I(file_inode(fd_file(tmp)));
1004 
1005 	if (ip->i_mount != tip->i_mount)
1006 		return -EINVAL;
1007 
1008 	if (I_INO(ip) == I_INO(tip))
1009 		return -EINVAL;
1010 
1011 	if (xfs_is_shutdown(ip->i_mount))
1012 		return -EIO;
1013 
1014 	return xfs_swap_extents(ip, tip, sxp);
1015 }
1016 
1017 static int
1018 xfs_ioc_getlabel(
1019 	struct xfs_mount	*mp,
1020 	char			__user *user_label)
1021 {
1022 	struct xfs_sb		*sbp = &mp->m_sb;
1023 	char			label[XFSLABEL_MAX + 1];
1024 
1025 	/* Paranoia */
1026 	BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1027 
1028 	/* 1 larger than sb_fname, so this ensures a trailing NUL char */
1029 	spin_lock(&mp->m_sb_lock);
1030 	memtostr_pad(label, sbp->sb_fname);
1031 	spin_unlock(&mp->m_sb_lock);
1032 
1033 	if (copy_to_user(user_label, label, sizeof(label)))
1034 		return -EFAULT;
1035 	return 0;
1036 }
1037 
1038 static int
1039 xfs_ioc_setlabel(
1040 	struct file		*filp,
1041 	struct xfs_mount	*mp,
1042 	char			__user *newlabel)
1043 {
1044 	struct xfs_sb		*sbp = &mp->m_sb;
1045 	char			label[XFSLABEL_MAX + 1];
1046 	size_t			len;
1047 	int			error;
1048 
1049 	if (!capable(CAP_SYS_ADMIN))
1050 		return -EPERM;
1051 	/*
1052 	 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
1053 	 * smaller, at 12 bytes.  We copy one more to be sure we find the
1054 	 * (required) NULL character to test the incoming label length.
1055 	 * NB: The on disk label doesn't need to be null terminated.
1056 	 */
1057 	if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
1058 		return -EFAULT;
1059 	len = strnlen(label, XFSLABEL_MAX + 1);
1060 	if (len > sizeof(sbp->sb_fname))
1061 		return -EINVAL;
1062 
1063 	error = mnt_want_write_file(filp);
1064 	if (error)
1065 		return error;
1066 
1067 	spin_lock(&mp->m_sb_lock);
1068 	memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
1069 	memcpy(sbp->sb_fname, label, len);
1070 	spin_unlock(&mp->m_sb_lock);
1071 
1072 	/*
1073 	 * Now we do several things to satisfy userspace.
1074 	 * In addition to normal logging of the primary superblock, we also
1075 	 * immediately write these changes to sector zero for the primary, then
1076 	 * update all backup supers (as xfs_db does for a label change), then
1077 	 * invalidate the block device page cache.  This is so that any prior
1078 	 * buffered reads from userspace (i.e. from blkid) are invalidated,
1079 	 * and userspace will see the newly-written label.
1080 	 */
1081 	error = xfs_sync_sb_buf(mp, true);
1082 	if (error)
1083 		goto out;
1084 	/*
1085 	 * growfs also updates backup supers so lock against that.
1086 	 */
1087 	mutex_lock(&mp->m_growlock);
1088 	error = xfs_update_secondary_sbs(mp);
1089 	mutex_unlock(&mp->m_growlock);
1090 
1091 	invalidate_bdev(mp->m_ddev_targp->bt_bdev);
1092 	if (xfs_has_rtsb(mp) && mp->m_rtdev_targp)
1093 		invalidate_bdev(mp->m_rtdev_targp->bt_bdev);
1094 
1095 out:
1096 	mnt_drop_write_file(filp);
1097 	return error;
1098 }
1099 
1100 static inline int
1101 xfs_fs_eofblocks_from_user(
1102 	struct xfs_fs_eofblocks		*src,
1103 	struct xfs_icwalk		*dst)
1104 {
1105 	if (src->eof_version != XFS_EOFBLOCKS_VERSION)
1106 		return -EINVAL;
1107 
1108 	if (src->eof_flags & ~XFS_EOF_FLAGS_VALID)
1109 		return -EINVAL;
1110 
1111 	if (memchr_inv(&src->pad32, 0, sizeof(src->pad32)) ||
1112 	    memchr_inv(src->pad64, 0, sizeof(src->pad64)))
1113 		return -EINVAL;
1114 
1115 	dst->icw_flags = 0;
1116 	if (src->eof_flags & XFS_EOF_FLAGS_SYNC)
1117 		dst->icw_flags |= XFS_ICWALK_FLAG_SYNC;
1118 	if (src->eof_flags & XFS_EOF_FLAGS_UID)
1119 		dst->icw_flags |= XFS_ICWALK_FLAG_UID;
1120 	if (src->eof_flags & XFS_EOF_FLAGS_GID)
1121 		dst->icw_flags |= XFS_ICWALK_FLAG_GID;
1122 	if (src->eof_flags & XFS_EOF_FLAGS_PRID)
1123 		dst->icw_flags |= XFS_ICWALK_FLAG_PRID;
1124 	if (src->eof_flags & XFS_EOF_FLAGS_MINFILESIZE)
1125 		dst->icw_flags |= XFS_ICWALK_FLAG_MINFILESIZE;
1126 
1127 	dst->icw_prid = src->eof_prid;
1128 	dst->icw_min_file_size = src->eof_min_file_size;
1129 
1130 	dst->icw_uid = INVALID_UID;
1131 	if (src->eof_flags & XFS_EOF_FLAGS_UID) {
1132 		dst->icw_uid = make_kuid(current_user_ns(), src->eof_uid);
1133 		if (!uid_valid(dst->icw_uid))
1134 			return -EINVAL;
1135 	}
1136 
1137 	dst->icw_gid = INVALID_GID;
1138 	if (src->eof_flags & XFS_EOF_FLAGS_GID) {
1139 		dst->icw_gid = make_kgid(current_user_ns(), src->eof_gid);
1140 		if (!gid_valid(dst->icw_gid))
1141 			return -EINVAL;
1142 	}
1143 	return 0;
1144 }
1145 
1146 static int
1147 xfs_ioctl_getset_resblocks(
1148 	struct file		*filp,
1149 	unsigned int		cmd,
1150 	void __user		*arg)
1151 {
1152 	struct xfs_mount	*mp = XFS_I(file_inode(filp))->i_mount;
1153 	struct xfs_fsop_resblks	fsop = { };
1154 	int			error;
1155 
1156 	if (!capable(CAP_SYS_ADMIN))
1157 		return -EPERM;
1158 
1159 	if (cmd == XFS_IOC_SET_RESBLKS) {
1160 		if (xfs_is_readonly(mp))
1161 			return -EROFS;
1162 
1163 		if (copy_from_user(&fsop, arg, sizeof(fsop)))
1164 			return -EFAULT;
1165 
1166 		error = mnt_want_write_file(filp);
1167 		if (error)
1168 			return error;
1169 		error = xfs_reserve_blocks(mp, XC_FREE_BLOCKS, fsop.resblks);
1170 		mnt_drop_write_file(filp);
1171 		if (error)
1172 			return error;
1173 	}
1174 
1175 	spin_lock(&mp->m_sb_lock);
1176 	fsop.resblks = mp->m_free[XC_FREE_BLOCKS].res_total;
1177 	fsop.resblks_avail = mp->m_free[XC_FREE_BLOCKS].res_avail;
1178 	spin_unlock(&mp->m_sb_lock);
1179 
1180 	if (copy_to_user(arg, &fsop, sizeof(fsop)))
1181 		return -EFAULT;
1182 	return 0;
1183 }
1184 
1185 static int
1186 xfs_ioctl_fs_counts(
1187 	struct xfs_mount	*mp,
1188 	struct xfs_fsop_counts __user	*uarg)
1189 {
1190 	struct xfs_fsop_counts	out = {
1191 		.allocino = percpu_counter_read_positive(&mp->m_icount),
1192 		.freeino  = percpu_counter_read_positive(&mp->m_ifree),
1193 		.freedata = xfs_estimate_freecounter(mp, XC_FREE_BLOCKS) -
1194 				xfs_freecounter_unavailable(mp, XC_FREE_BLOCKS),
1195 		.freertx  = xfs_estimate_freecounter(mp, XC_FREE_RTEXTENTS),
1196 	};
1197 
1198 	if (copy_to_user(uarg, &out, sizeof(out)))
1199 		return -EFAULT;
1200 	return 0;
1201 }
1202 
1203 /*
1204  * These long-unused ioctls were removed from the official ioctl API in 5.17,
1205  * but retain these definitions so that we can log warnings about them.
1206  */
1207 #define XFS_IOC_ALLOCSP		_IOW ('X', 10, struct xfs_flock64)
1208 #define XFS_IOC_FREESP		_IOW ('X', 11, struct xfs_flock64)
1209 #define XFS_IOC_ALLOCSP64	_IOW ('X', 36, struct xfs_flock64)
1210 #define XFS_IOC_FREESP64	_IOW ('X', 37, struct xfs_flock64)
1211 
1212 /*
1213  * Note: some of the ioctl's return positive numbers as a
1214  * byte count indicating success, such as readlink_by_handle.
1215  * So we don't "sign flip" like most other routines.  This means
1216  * true errors need to be returned as a negative value.
1217  */
1218 long
1219 xfs_file_ioctl(
1220 	struct file		*filp,
1221 	unsigned int		cmd,
1222 	unsigned long		p)
1223 {
1224 	struct inode		*inode = file_inode(filp);
1225 	struct xfs_inode	*ip = XFS_I(inode);
1226 	struct xfs_mount	*mp = ip->i_mount;
1227 	void			__user *arg = (void __user *)p;
1228 	int			error;
1229 
1230 	trace_xfs_file_ioctl(ip);
1231 
1232 	switch (cmd) {
1233 	case FITRIM:
1234 		return xfs_ioc_trim(mp, arg);
1235 	case FS_IOC_GETFSLABEL:
1236 		return xfs_ioc_getlabel(mp, arg);
1237 	case FS_IOC_SETFSLABEL:
1238 		return xfs_ioc_setlabel(filp, mp, arg);
1239 	case XFS_IOC_ALLOCSP:
1240 	case XFS_IOC_FREESP:
1241 	case XFS_IOC_ALLOCSP64:
1242 	case XFS_IOC_FREESP64:
1243 		xfs_warn_once(mp,
1244 	"%s should use fallocate; XFS_IOC_{ALLOC,FREE}SP ioctl unsupported",
1245 				current->comm);
1246 		return -ENOTTY;
1247 	case XFS_IOC_DIOINFO: {
1248 		struct kstat		st;
1249 		struct dioattr		da;
1250 
1251 		error = vfs_getattr(&filp->f_path, &st, STATX_DIOALIGN, 0);
1252 		if (error)
1253 			return error;
1254 
1255 		/*
1256 		 * Some userspace directly feeds the return value to
1257 		 * posix_memalign, which fails for values that are smaller than
1258 		 * the pointer size.  Round up the value to not break userspace.
1259 		 */
1260 		da.d_mem = roundup(st.dio_mem_align, sizeof(void *));
1261 		da.d_miniosz = st.dio_offset_align;
1262 		da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1263 		if (copy_to_user(arg, &da, sizeof(da)))
1264 			return -EFAULT;
1265 		return 0;
1266 	}
1267 
1268 	case XFS_IOC_FSBULKSTAT_SINGLE:
1269 	case XFS_IOC_FSBULKSTAT:
1270 	case XFS_IOC_FSINUMBERS:
1271 		return xfs_ioc_fsbulkstat(filp, cmd, arg);
1272 
1273 	case XFS_IOC_BULKSTAT:
1274 		return xfs_ioc_bulkstat(filp, cmd, arg);
1275 	case XFS_IOC_INUMBERS:
1276 		return xfs_ioc_inumbers(mp, cmd, arg);
1277 
1278 	case XFS_IOC_FSGEOMETRY_V1:
1279 		return xfs_ioc_fsgeometry(mp, arg, 3);
1280 	case XFS_IOC_FSGEOMETRY_V4:
1281 		return xfs_ioc_fsgeometry(mp, arg, 4);
1282 	case XFS_IOC_FSGEOMETRY:
1283 		return xfs_ioc_fsgeometry(mp, arg, 5);
1284 
1285 	case XFS_IOC_AG_GEOMETRY:
1286 		return xfs_ioc_ag_geometry(mp, arg);
1287 	case XFS_IOC_RTGROUP_GEOMETRY:
1288 		return xfs_ioc_rtgroup_geometry(mp, arg);
1289 
1290 	case XFS_IOC_GETVERSION:
1291 		return put_user(inode->i_generation, (int __user *)arg);
1292 
1293 	case XFS_IOC_FSGETXATTRA:
1294 		return xfs_ioc_fsgetxattra(ip, arg);
1295 	case XFS_IOC_GETPARENTS:
1296 		return xfs_ioc_getparents(filp, arg);
1297 	case XFS_IOC_GETPARENTS_BY_HANDLE:
1298 		return xfs_ioc_getparents_by_handle(filp, arg);
1299 	case XFS_IOC_GETBMAP:
1300 	case XFS_IOC_GETBMAPA:
1301 	case XFS_IOC_GETBMAPX:
1302 		return xfs_ioc_getbmap(filp, cmd, arg);
1303 
1304 	case FS_IOC_GETFSMAP:
1305 		return xfs_ioc_getfsmap(ip, arg);
1306 
1307 	case XFS_IOC_SCRUBV_METADATA:
1308 		return xfs_ioc_scrubv_metadata(filp, arg);
1309 	case XFS_IOC_SCRUB_METADATA:
1310 		return xfs_ioc_scrub_metadata(filp, arg);
1311 
1312 	case XFS_IOC_FD_TO_HANDLE:
1313 	case XFS_IOC_PATH_TO_HANDLE:
1314 	case XFS_IOC_PATH_TO_FSHANDLE: {
1315 		xfs_fsop_handlereq_t	hreq;
1316 
1317 		if (copy_from_user(&hreq, arg, sizeof(hreq)))
1318 			return -EFAULT;
1319 		return xfs_find_handle(cmd, &hreq);
1320 	}
1321 	case XFS_IOC_OPEN_BY_HANDLE: {
1322 		xfs_fsop_handlereq_t	hreq;
1323 
1324 		if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1325 			return -EFAULT;
1326 		return xfs_open_by_handle(filp, &hreq);
1327 	}
1328 
1329 	case XFS_IOC_READLINK_BY_HANDLE: {
1330 		xfs_fsop_handlereq_t	hreq;
1331 
1332 		if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1333 			return -EFAULT;
1334 		return xfs_readlink_by_handle(filp, &hreq);
1335 	}
1336 	case XFS_IOC_ATTRLIST_BY_HANDLE:
1337 		return xfs_attrlist_by_handle(filp, arg);
1338 
1339 	case XFS_IOC_ATTRMULTI_BY_HANDLE:
1340 		return xfs_attrmulti_by_handle(filp, arg);
1341 
1342 	case XFS_IOC_SWAPEXT: {
1343 		struct xfs_swapext	sxp;
1344 
1345 		if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1346 			return -EFAULT;
1347 		error = mnt_want_write_file(filp);
1348 		if (error)
1349 			return error;
1350 		error = xfs_ioc_swapext(&sxp);
1351 		mnt_drop_write_file(filp);
1352 		return error;
1353 	}
1354 
1355 	case XFS_IOC_FSCOUNTS:
1356 		return xfs_ioctl_fs_counts(mp, arg);
1357 
1358 	case XFS_IOC_SET_RESBLKS:
1359 	case XFS_IOC_GET_RESBLKS:
1360 		return xfs_ioctl_getset_resblocks(filp, cmd, arg);
1361 
1362 	case XFS_IOC_FSGROWFSDATA: {
1363 		struct xfs_growfs_data in;
1364 
1365 		if (copy_from_user(&in, arg, sizeof(in)))
1366 			return -EFAULT;
1367 
1368 		error = mnt_want_write_file(filp);
1369 		if (error)
1370 			return error;
1371 		error = xfs_growfs_data(mp, &in);
1372 		mnt_drop_write_file(filp);
1373 		return error;
1374 	}
1375 
1376 	case XFS_IOC_FSGROWFSLOG: {
1377 		struct xfs_growfs_log in;
1378 
1379 		if (copy_from_user(&in, arg, sizeof(in)))
1380 			return -EFAULT;
1381 
1382 		error = mnt_want_write_file(filp);
1383 		if (error)
1384 			return error;
1385 		error = xfs_growfs_log(mp, &in);
1386 		mnt_drop_write_file(filp);
1387 		return error;
1388 	}
1389 
1390 	case XFS_IOC_FSGROWFSRT: {
1391 		xfs_growfs_rt_t in;
1392 
1393 		if (copy_from_user(&in, arg, sizeof(in)))
1394 			return -EFAULT;
1395 
1396 		error = mnt_want_write_file(filp);
1397 		if (error)
1398 			return error;
1399 		error = xfs_growfs_rt(mp, &in);
1400 		mnt_drop_write_file(filp);
1401 		return error;
1402 	}
1403 
1404 	case XFS_IOC_GOINGDOWN: {
1405 		uint32_t in;
1406 
1407 		if (!capable(CAP_SYS_ADMIN))
1408 			return -EPERM;
1409 
1410 		if (get_user(in, (uint32_t __user *)arg))
1411 			return -EFAULT;
1412 
1413 		return xfs_fs_goingdown(mp, in);
1414 	}
1415 
1416 	case XFS_IOC_ERROR_INJECTION: {
1417 		xfs_error_injection_t in;
1418 
1419 		if (!capable(CAP_SYS_ADMIN))
1420 			return -EPERM;
1421 
1422 		if (copy_from_user(&in, arg, sizeof(in)))
1423 			return -EFAULT;
1424 
1425 		return xfs_errortag_add(mp, in.errtag);
1426 	}
1427 
1428 	case XFS_IOC_ERROR_CLEARALL:
1429 		if (!capable(CAP_SYS_ADMIN))
1430 			return -EPERM;
1431 
1432 		return xfs_errortag_clearall(mp);
1433 
1434 	case XFS_IOC_FREE_EOFBLOCKS: {
1435 		struct xfs_fs_eofblocks	eofb;
1436 		struct xfs_icwalk	icw;
1437 
1438 		if (!capable(CAP_SYS_ADMIN))
1439 			return -EPERM;
1440 
1441 		if (xfs_is_readonly(mp))
1442 			return -EROFS;
1443 
1444 		if (copy_from_user(&eofb, arg, sizeof(eofb)))
1445 			return -EFAULT;
1446 
1447 		error = xfs_fs_eofblocks_from_user(&eofb, &icw);
1448 		if (error)
1449 			return error;
1450 
1451 		trace_xfs_ioc_free_eofblocks(mp, &icw, _RET_IP_);
1452 
1453 		guard(super_write)(mp->m_super);
1454 		return xfs_blockgc_free_space(mp, &icw);
1455 	}
1456 
1457 	case XFS_IOC_EXCHANGE_RANGE:
1458 		return xfs_ioc_exchange_range(filp, arg);
1459 	case XFS_IOC_START_COMMIT:
1460 		return xfs_ioc_start_commit(filp, arg);
1461 	case XFS_IOC_COMMIT_RANGE:
1462 		return xfs_ioc_commit_range(filp, arg);
1463 
1464 	case XFS_IOC_HEALTH_MONITOR:
1465 		return xfs_ioc_health_monitor(filp, arg);
1466 	case XFS_IOC_VERIFY_MEDIA:
1467 		return xfs_ioc_verify_media(filp, arg);
1468 
1469 	default:
1470 		return -ENOTTY;
1471 	}
1472 }
1473