File Index
Files and directories
Sign in
FILE

xfs_ioctl.c

File preview with syntax highlighting.

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