xref: /openbmc/linux/fs/xfs/xfs_ioctl.c (revision f736d93d76d3e97d6986c6d26c8eaa32536ccc5c)
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_attr.h"
19 #include "xfs_bmap.h"
20 #include "xfs_bmap_util.h"
21 #include "xfs_fsops.h"
22 #include "xfs_discard.h"
23 #include "xfs_quota.h"
24 #include "xfs_export.h"
25 #include "xfs_trace.h"
26 #include "xfs_icache.h"
27 #include "xfs_trans.h"
28 #include "xfs_acl.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_da_format.h"
39 #include "xfs_da_btree.h"
40 
41 #include <linux/mount.h>
42 #include <linux/namei.h>
43 
44 /*
45  * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
46  * a file or fs handle.
47  *
48  * XFS_IOC_PATH_TO_FSHANDLE
49  *    returns fs handle for a mount point or path within that mount point
50  * XFS_IOC_FD_TO_HANDLE
51  *    returns full handle for a FD opened in user space
52  * XFS_IOC_PATH_TO_HANDLE
53  *    returns full handle for a path
54  */
55 int
56 xfs_find_handle(
57 	unsigned int		cmd,
58 	xfs_fsop_handlereq_t	*hreq)
59 {
60 	int			hsize;
61 	xfs_handle_t		handle;
62 	struct inode		*inode;
63 	struct fd		f = {NULL};
64 	struct path		path;
65 	int			error;
66 	struct xfs_inode	*ip;
67 
68 	if (cmd == XFS_IOC_FD_TO_HANDLE) {
69 		f = fdget(hreq->fd);
70 		if (!f.file)
71 			return -EBADF;
72 		inode = file_inode(f.file);
73 	} else {
74 		error = user_path_at(AT_FDCWD, hreq->path, 0, &path);
75 		if (error)
76 			return error;
77 		inode = d_inode(path.dentry);
78 	}
79 	ip = XFS_I(inode);
80 
81 	/*
82 	 * We can only generate handles for inodes residing on a XFS filesystem,
83 	 * and only for regular files, directories or symbolic links.
84 	 */
85 	error = -EINVAL;
86 	if (inode->i_sb->s_magic != XFS_SB_MAGIC)
87 		goto out_put;
88 
89 	error = -EBADF;
90 	if (!S_ISREG(inode->i_mode) &&
91 	    !S_ISDIR(inode->i_mode) &&
92 	    !S_ISLNK(inode->i_mode))
93 		goto out_put;
94 
95 
96 	memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
97 
98 	if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
99 		/*
100 		 * This handle only contains an fsid, zero the rest.
101 		 */
102 		memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
103 		hsize = sizeof(xfs_fsid_t);
104 	} else {
105 		handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
106 					sizeof(handle.ha_fid.fid_len);
107 		handle.ha_fid.fid_pad = 0;
108 		handle.ha_fid.fid_gen = inode->i_generation;
109 		handle.ha_fid.fid_ino = ip->i_ino;
110 		hsize = sizeof(xfs_handle_t);
111 	}
112 
113 	error = -EFAULT;
114 	if (copy_to_user(hreq->ohandle, &handle, hsize) ||
115 	    copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
116 		goto out_put;
117 
118 	error = 0;
119 
120  out_put:
121 	if (cmd == XFS_IOC_FD_TO_HANDLE)
122 		fdput(f);
123 	else
124 		path_put(&path);
125 	return error;
126 }
127 
128 /*
129  * No need to do permission checks on the various pathname components
130  * as the handle operations are privileged.
131  */
132 STATIC int
133 xfs_handle_acceptable(
134 	void			*context,
135 	struct dentry		*dentry)
136 {
137 	return 1;
138 }
139 
140 /*
141  * Convert userspace handle data into a dentry.
142  */
143 struct dentry *
144 xfs_handle_to_dentry(
145 	struct file		*parfilp,
146 	void __user		*uhandle,
147 	u32			hlen)
148 {
149 	xfs_handle_t		handle;
150 	struct xfs_fid64	fid;
151 
152 	/*
153 	 * Only allow handle opens under a directory.
154 	 */
155 	if (!S_ISDIR(file_inode(parfilp)->i_mode))
156 		return ERR_PTR(-ENOTDIR);
157 
158 	if (hlen != sizeof(xfs_handle_t))
159 		return ERR_PTR(-EINVAL);
160 	if (copy_from_user(&handle, uhandle, hlen))
161 		return ERR_PTR(-EFAULT);
162 	if (handle.ha_fid.fid_len !=
163 	    sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
164 		return ERR_PTR(-EINVAL);
165 
166 	memset(&fid, 0, sizeof(struct fid));
167 	fid.ino = handle.ha_fid.fid_ino;
168 	fid.gen = handle.ha_fid.fid_gen;
169 
170 	return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
171 			FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
172 			xfs_handle_acceptable, NULL);
173 }
174 
175 STATIC struct dentry *
176 xfs_handlereq_to_dentry(
177 	struct file		*parfilp,
178 	xfs_fsop_handlereq_t	*hreq)
179 {
180 	return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
181 }
182 
183 int
184 xfs_open_by_handle(
185 	struct file		*parfilp,
186 	xfs_fsop_handlereq_t	*hreq)
187 {
188 	const struct cred	*cred = current_cred();
189 	int			error;
190 	int			fd;
191 	int			permflag;
192 	struct file		*filp;
193 	struct inode		*inode;
194 	struct dentry		*dentry;
195 	fmode_t			fmode;
196 	struct path		path;
197 
198 	if (!capable(CAP_SYS_ADMIN))
199 		return -EPERM;
200 
201 	dentry = xfs_handlereq_to_dentry(parfilp, hreq);
202 	if (IS_ERR(dentry))
203 		return PTR_ERR(dentry);
204 	inode = d_inode(dentry);
205 
206 	/* Restrict xfs_open_by_handle to directories & regular files. */
207 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
208 		error = -EPERM;
209 		goto out_dput;
210 	}
211 
212 #if BITS_PER_LONG != 32
213 	hreq->oflags |= O_LARGEFILE;
214 #endif
215 
216 	permflag = hreq->oflags;
217 	fmode = OPEN_FMODE(permflag);
218 	if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
219 	    (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
220 		error = -EPERM;
221 		goto out_dput;
222 	}
223 
224 	if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
225 		error = -EPERM;
226 		goto out_dput;
227 	}
228 
229 	/* Can't write directories. */
230 	if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
231 		error = -EISDIR;
232 		goto out_dput;
233 	}
234 
235 	fd = get_unused_fd_flags(0);
236 	if (fd < 0) {
237 		error = fd;
238 		goto out_dput;
239 	}
240 
241 	path.mnt = parfilp->f_path.mnt;
242 	path.dentry = dentry;
243 	filp = dentry_open(&path, hreq->oflags, cred);
244 	dput(dentry);
245 	if (IS_ERR(filp)) {
246 		put_unused_fd(fd);
247 		return PTR_ERR(filp);
248 	}
249 
250 	if (S_ISREG(inode->i_mode)) {
251 		filp->f_flags |= O_NOATIME;
252 		filp->f_mode |= FMODE_NOCMTIME;
253 	}
254 
255 	fd_install(fd, filp);
256 	return fd;
257 
258  out_dput:
259 	dput(dentry);
260 	return error;
261 }
262 
263 int
264 xfs_readlink_by_handle(
265 	struct file		*parfilp,
266 	xfs_fsop_handlereq_t	*hreq)
267 {
268 	struct dentry		*dentry;
269 	__u32			olen;
270 	int			error;
271 
272 	if (!capable(CAP_SYS_ADMIN))
273 		return -EPERM;
274 
275 	dentry = xfs_handlereq_to_dentry(parfilp, hreq);
276 	if (IS_ERR(dentry))
277 		return PTR_ERR(dentry);
278 
279 	/* Restrict this handle operation to symlinks only. */
280 	if (!d_is_symlink(dentry)) {
281 		error = -EINVAL;
282 		goto out_dput;
283 	}
284 
285 	if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
286 		error = -EFAULT;
287 		goto out_dput;
288 	}
289 
290 	error = vfs_readlink(dentry, hreq->ohandle, olen);
291 
292  out_dput:
293 	dput(dentry);
294 	return error;
295 }
296 
297 /*
298  * Format an attribute and copy it out to the user's buffer.
299  * Take care to check values and protect against them changing later,
300  * we may be reading them directly out of a user buffer.
301  */
302 static void
303 xfs_ioc_attr_put_listent(
304 	struct xfs_attr_list_context *context,
305 	int			flags,
306 	unsigned char		*name,
307 	int			namelen,
308 	int			valuelen)
309 {
310 	struct xfs_attrlist	*alist = context->buffer;
311 	struct xfs_attrlist_ent	*aep;
312 	int			arraytop;
313 
314 	ASSERT(!context->seen_enough);
315 	ASSERT(context->count >= 0);
316 	ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
317 	ASSERT(context->firstu >= sizeof(*alist));
318 	ASSERT(context->firstu <= context->bufsize);
319 
320 	/*
321 	 * Only list entries in the right namespace.
322 	 */
323 	if (context->attr_filter != (flags & XFS_ATTR_NSP_ONDISK_MASK))
324 		return;
325 
326 	arraytop = sizeof(*alist) +
327 			context->count * sizeof(alist->al_offset[0]);
328 
329 	/* decrement by the actual bytes used by the attr */
330 	context->firstu -= round_up(offsetof(struct xfs_attrlist_ent, a_name) +
331 			namelen + 1, sizeof(uint32_t));
332 	if (context->firstu < arraytop) {
333 		trace_xfs_attr_list_full(context);
334 		alist->al_more = 1;
335 		context->seen_enough = 1;
336 		return;
337 	}
338 
339 	aep = context->buffer + context->firstu;
340 	aep->a_valuelen = valuelen;
341 	memcpy(aep->a_name, name, namelen);
342 	aep->a_name[namelen] = 0;
343 	alist->al_offset[context->count++] = context->firstu;
344 	alist->al_count = context->count;
345 	trace_xfs_attr_list_add(context);
346 }
347 
348 static unsigned int
349 xfs_attr_filter(
350 	u32			ioc_flags)
351 {
352 	if (ioc_flags & XFS_IOC_ATTR_ROOT)
353 		return XFS_ATTR_ROOT;
354 	if (ioc_flags & XFS_IOC_ATTR_SECURE)
355 		return XFS_ATTR_SECURE;
356 	return 0;
357 }
358 
359 static unsigned int
360 xfs_attr_flags(
361 	u32			ioc_flags)
362 {
363 	if (ioc_flags & XFS_IOC_ATTR_CREATE)
364 		return XATTR_CREATE;
365 	if (ioc_flags & XFS_IOC_ATTR_REPLACE)
366 		return XATTR_REPLACE;
367 	return 0;
368 }
369 
370 int
371 xfs_ioc_attr_list(
372 	struct xfs_inode		*dp,
373 	void __user			*ubuf,
374 	int				bufsize,
375 	int				flags,
376 	struct xfs_attrlist_cursor __user *ucursor)
377 {
378 	struct xfs_attr_list_context	context = { };
379 	struct xfs_attrlist		*alist;
380 	void				*buffer;
381 	int				error;
382 
383 	if (bufsize < sizeof(struct xfs_attrlist) ||
384 	    bufsize > XFS_XATTR_LIST_MAX)
385 		return -EINVAL;
386 
387 	/*
388 	 * Reject flags, only allow namespaces.
389 	 */
390 	if (flags & ~(XFS_IOC_ATTR_ROOT | XFS_IOC_ATTR_SECURE))
391 		return -EINVAL;
392 	if (flags == (XFS_IOC_ATTR_ROOT | XFS_IOC_ATTR_SECURE))
393 		return -EINVAL;
394 
395 	/*
396 	 * Validate the cursor.
397 	 */
398 	if (copy_from_user(&context.cursor, ucursor, sizeof(context.cursor)))
399 		return -EFAULT;
400 	if (context.cursor.pad1 || context.cursor.pad2)
401 		return -EINVAL;
402 	if (!context.cursor.initted &&
403 	    (context.cursor.hashval || context.cursor.blkno ||
404 	     context.cursor.offset))
405 		return -EINVAL;
406 
407 	buffer = kvzalloc(bufsize, GFP_KERNEL);
408 	if (!buffer)
409 		return -ENOMEM;
410 
411 	/*
412 	 * Initialize the output buffer.
413 	 */
414 	context.dp = dp;
415 	context.resynch = 1;
416 	context.attr_filter = xfs_attr_filter(flags);
417 	context.buffer = buffer;
418 	context.bufsize = round_down(bufsize, sizeof(uint32_t));
419 	context.firstu = context.bufsize;
420 	context.put_listent = xfs_ioc_attr_put_listent;
421 
422 	alist = context.buffer;
423 	alist->al_count = 0;
424 	alist->al_more = 0;
425 	alist->al_offset[0] = context.bufsize;
426 
427 	error = xfs_attr_list(&context);
428 	if (error)
429 		goto out_free;
430 
431 	if (copy_to_user(ubuf, buffer, bufsize) ||
432 	    copy_to_user(ucursor, &context.cursor, sizeof(context.cursor)))
433 		error = -EFAULT;
434 out_free:
435 	kmem_free(buffer);
436 	return error;
437 }
438 
439 STATIC int
440 xfs_attrlist_by_handle(
441 	struct file		*parfilp,
442 	struct xfs_fsop_attrlist_handlereq __user *p)
443 {
444 	struct xfs_fsop_attrlist_handlereq al_hreq;
445 	struct dentry		*dentry;
446 	int			error = -ENOMEM;
447 
448 	if (!capable(CAP_SYS_ADMIN))
449 		return -EPERM;
450 	if (copy_from_user(&al_hreq, p, sizeof(al_hreq)))
451 		return -EFAULT;
452 
453 	dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
454 	if (IS_ERR(dentry))
455 		return PTR_ERR(dentry);
456 
457 	error = xfs_ioc_attr_list(XFS_I(d_inode(dentry)), al_hreq.buffer,
458 				  al_hreq.buflen, al_hreq.flags, &p->pos);
459 	dput(dentry);
460 	return error;
461 }
462 
463 static int
464 xfs_attrmulti_attr_get(
465 	struct inode		*inode,
466 	unsigned char		*name,
467 	unsigned char		__user *ubuf,
468 	uint32_t		*len,
469 	uint32_t		flags)
470 {
471 	struct xfs_da_args	args = {
472 		.dp		= XFS_I(inode),
473 		.attr_filter	= xfs_attr_filter(flags),
474 		.attr_flags	= xfs_attr_flags(flags),
475 		.name		= name,
476 		.namelen	= strlen(name),
477 		.valuelen	= *len,
478 	};
479 	int			error;
480 
481 	if (*len > XFS_XATTR_SIZE_MAX)
482 		return -EINVAL;
483 
484 	error = xfs_attr_get(&args);
485 	if (error)
486 		goto out_kfree;
487 
488 	*len = args.valuelen;
489 	if (copy_to_user(ubuf, args.value, args.valuelen))
490 		error = -EFAULT;
491 
492 out_kfree:
493 	kmem_free(args.value);
494 	return error;
495 }
496 
497 static int
498 xfs_attrmulti_attr_set(
499 	struct inode		*inode,
500 	unsigned char		*name,
501 	const unsigned char	__user *ubuf,
502 	uint32_t		len,
503 	uint32_t		flags)
504 {
505 	struct xfs_da_args	args = {
506 		.dp		= XFS_I(inode),
507 		.attr_filter	= xfs_attr_filter(flags),
508 		.attr_flags	= xfs_attr_flags(flags),
509 		.name		= name,
510 		.namelen	= strlen(name),
511 	};
512 	int			error;
513 
514 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
515 		return -EPERM;
516 
517 	if (ubuf) {
518 		if (len > XFS_XATTR_SIZE_MAX)
519 			return -EINVAL;
520 		args.value = memdup_user(ubuf, len);
521 		if (IS_ERR(args.value))
522 			return PTR_ERR(args.value);
523 		args.valuelen = len;
524 	}
525 
526 	error = xfs_attr_set(&args);
527 	if (!error && (flags & XFS_IOC_ATTR_ROOT))
528 		xfs_forget_acl(inode, name);
529 	kfree(args.value);
530 	return error;
531 }
532 
533 int
534 xfs_ioc_attrmulti_one(
535 	struct file		*parfilp,
536 	struct inode		*inode,
537 	uint32_t		opcode,
538 	void __user		*uname,
539 	void __user		*value,
540 	uint32_t		*len,
541 	uint32_t		flags)
542 {
543 	unsigned char		*name;
544 	int			error;
545 
546 	if ((flags & XFS_IOC_ATTR_ROOT) && (flags & XFS_IOC_ATTR_SECURE))
547 		return -EINVAL;
548 
549 	name = strndup_user(uname, MAXNAMELEN);
550 	if (IS_ERR(name))
551 		return PTR_ERR(name);
552 
553 	switch (opcode) {
554 	case ATTR_OP_GET:
555 		error = xfs_attrmulti_attr_get(inode, name, value, len, flags);
556 		break;
557 	case ATTR_OP_REMOVE:
558 		value = NULL;
559 		*len = 0;
560 		/* fall through */
561 	case ATTR_OP_SET:
562 		error = mnt_want_write_file(parfilp);
563 		if (error)
564 			break;
565 		error = xfs_attrmulti_attr_set(inode, name, value, *len, flags);
566 		mnt_drop_write_file(parfilp);
567 		break;
568 	default:
569 		error = -EINVAL;
570 		break;
571 	}
572 
573 	kfree(name);
574 	return error;
575 }
576 
577 STATIC int
578 xfs_attrmulti_by_handle(
579 	struct file		*parfilp,
580 	void			__user *arg)
581 {
582 	int			error;
583 	xfs_attr_multiop_t	*ops;
584 	xfs_fsop_attrmulti_handlereq_t am_hreq;
585 	struct dentry		*dentry;
586 	unsigned int		i, size;
587 
588 	if (!capable(CAP_SYS_ADMIN))
589 		return -EPERM;
590 	if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
591 		return -EFAULT;
592 
593 	/* overflow check */
594 	if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
595 		return -E2BIG;
596 
597 	dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
598 	if (IS_ERR(dentry))
599 		return PTR_ERR(dentry);
600 
601 	error = -E2BIG;
602 	size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
603 	if (!size || size > 16 * PAGE_SIZE)
604 		goto out_dput;
605 
606 	ops = memdup_user(am_hreq.ops, size);
607 	if (IS_ERR(ops)) {
608 		error = PTR_ERR(ops);
609 		goto out_dput;
610 	}
611 
612 	error = 0;
613 	for (i = 0; i < am_hreq.opcount; i++) {
614 		ops[i].am_error = xfs_ioc_attrmulti_one(parfilp,
615 				d_inode(dentry), ops[i].am_opcode,
616 				ops[i].am_attrname, ops[i].am_attrvalue,
617 				&ops[i].am_length, ops[i].am_flags);
618 	}
619 
620 	if (copy_to_user(am_hreq.ops, ops, size))
621 		error = -EFAULT;
622 
623 	kfree(ops);
624  out_dput:
625 	dput(dentry);
626 	return error;
627 }
628 
629 int
630 xfs_ioc_space(
631 	struct file		*filp,
632 	xfs_flock64_t		*bf)
633 {
634 	struct inode		*inode = file_inode(filp);
635 	struct xfs_inode	*ip = XFS_I(inode);
636 	struct iattr		iattr;
637 	enum xfs_prealloc_flags	flags = XFS_PREALLOC_CLEAR;
638 	uint			iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
639 	int			error;
640 
641 	if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
642 		return -EPERM;
643 
644 	if (!(filp->f_mode & FMODE_WRITE))
645 		return -EBADF;
646 
647 	if (!S_ISREG(inode->i_mode))
648 		return -EINVAL;
649 
650 	if (xfs_is_always_cow_inode(ip))
651 		return -EOPNOTSUPP;
652 
653 	if (filp->f_flags & O_DSYNC)
654 		flags |= XFS_PREALLOC_SYNC;
655 	if (filp->f_mode & FMODE_NOCMTIME)
656 		flags |= XFS_PREALLOC_INVISIBLE;
657 
658 	error = mnt_want_write_file(filp);
659 	if (error)
660 		return error;
661 
662 	xfs_ilock(ip, iolock);
663 	error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
664 	if (error)
665 		goto out_unlock;
666 	inode_dio_wait(inode);
667 
668 	switch (bf->l_whence) {
669 	case 0: /*SEEK_SET*/
670 		break;
671 	case 1: /*SEEK_CUR*/
672 		bf->l_start += filp->f_pos;
673 		break;
674 	case 2: /*SEEK_END*/
675 		bf->l_start += XFS_ISIZE(ip);
676 		break;
677 	default:
678 		error = -EINVAL;
679 		goto out_unlock;
680 	}
681 
682 	if (bf->l_start < 0 || bf->l_start > inode->i_sb->s_maxbytes) {
683 		error = -EINVAL;
684 		goto out_unlock;
685 	}
686 
687 	if (bf->l_start > XFS_ISIZE(ip)) {
688 		error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
689 				bf->l_start - XFS_ISIZE(ip), 0);
690 		if (error)
691 			goto out_unlock;
692 	}
693 
694 	iattr.ia_valid = ATTR_SIZE;
695 	iattr.ia_size = bf->l_start;
696 	error = xfs_vn_setattr_size(file_mnt_user_ns(filp), file_dentry(filp),
697 				    &iattr);
698 	if (error)
699 		goto out_unlock;
700 
701 	error = xfs_update_prealloc_flags(ip, flags);
702 
703 out_unlock:
704 	xfs_iunlock(ip, iolock);
705 	mnt_drop_write_file(filp);
706 	return error;
707 }
708 
709 /* Return 0 on success or positive error */
710 int
711 xfs_fsbulkstat_one_fmt(
712 	struct xfs_ibulk		*breq,
713 	const struct xfs_bulkstat	*bstat)
714 {
715 	struct xfs_bstat		bs1;
716 
717 	xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
718 	if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
719 		return -EFAULT;
720 	return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
721 }
722 
723 int
724 xfs_fsinumbers_fmt(
725 	struct xfs_ibulk		*breq,
726 	const struct xfs_inumbers	*igrp)
727 {
728 	struct xfs_inogrp		ig1;
729 
730 	xfs_inumbers_to_inogrp(&ig1, igrp);
731 	if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
732 		return -EFAULT;
733 	return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
734 }
735 
736 STATIC int
737 xfs_ioc_fsbulkstat(
738 	struct file		*file,
739 	unsigned int		cmd,
740 	void			__user *arg)
741 {
742 	struct xfs_mount	*mp = XFS_I(file_inode(file))->i_mount;
743 	struct xfs_fsop_bulkreq	bulkreq;
744 	struct xfs_ibulk	breq = {
745 		.mp		= mp,
746 		.mnt_userns	= file_mnt_user_ns(file),
747 		.ocount		= 0,
748 	};
749 	xfs_ino_t		lastino;
750 	int			error;
751 
752 	/* done = 1 if there are more stats to get and if bulkstat */
753 	/* should be called again (unused here, but used in dmapi) */
754 
755 	if (!capable(CAP_SYS_ADMIN))
756 		return -EPERM;
757 
758 	if (XFS_FORCED_SHUTDOWN(mp))
759 		return -EIO;
760 
761 	if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
762 		return -EFAULT;
763 
764 	if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
765 		return -EFAULT;
766 
767 	if (bulkreq.icount <= 0)
768 		return -EINVAL;
769 
770 	if (bulkreq.ubuffer == NULL)
771 		return -EINVAL;
772 
773 	breq.ubuffer = bulkreq.ubuffer;
774 	breq.icount = bulkreq.icount;
775 
776 	/*
777 	 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
778 	 * that we want to stat.  However, FSINUMBERS and FSBULKSTAT expect
779 	 * that *lastip contains either zero or the number of the last inode to
780 	 * be examined by the previous call and return results starting with
781 	 * the next inode after that.  The new bulk request back end functions
782 	 * take the inode to start with, so we have to compute the startino
783 	 * parameter from lastino to maintain correct function.  lastino == 0
784 	 * is a special case because it has traditionally meant "first inode
785 	 * in filesystem".
786 	 */
787 	if (cmd == XFS_IOC_FSINUMBERS) {
788 		breq.startino = lastino ? lastino + 1 : 0;
789 		error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
790 		lastino = breq.startino - 1;
791 	} else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
792 		breq.startino = lastino;
793 		breq.icount = 1;
794 		error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
795 	} else {	/* XFS_IOC_FSBULKSTAT */
796 		breq.startino = lastino ? lastino + 1 : 0;
797 		error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
798 		lastino = breq.startino - 1;
799 	}
800 
801 	if (error)
802 		return error;
803 
804 	if (bulkreq.lastip != NULL &&
805 	    copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
806 		return -EFAULT;
807 
808 	if (bulkreq.ocount != NULL &&
809 	    copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
810 		return -EFAULT;
811 
812 	return 0;
813 }
814 
815 /* Return 0 on success or positive error */
816 static int
817 xfs_bulkstat_fmt(
818 	struct xfs_ibulk		*breq,
819 	const struct xfs_bulkstat	*bstat)
820 {
821 	if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
822 		return -EFAULT;
823 	return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
824 }
825 
826 /*
827  * Check the incoming bulk request @hdr from userspace and initialize the
828  * internal @breq bulk request appropriately.  Returns 0 if the bulk request
829  * should proceed; -ECANCELED if there's nothing to do; or the usual
830  * negative error code.
831  */
832 static int
833 xfs_bulk_ireq_setup(
834 	struct xfs_mount	*mp,
835 	struct xfs_bulk_ireq	*hdr,
836 	struct xfs_ibulk	*breq,
837 	void __user		*ubuffer)
838 {
839 	if (hdr->icount == 0 ||
840 	    (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
841 	    memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
842 		return -EINVAL;
843 
844 	breq->startino = hdr->ino;
845 	breq->ubuffer = ubuffer;
846 	breq->icount = hdr->icount;
847 	breq->ocount = 0;
848 	breq->flags = 0;
849 
850 	/*
851 	 * The @ino parameter is a special value, so we must look it up here.
852 	 * We're not allowed to have IREQ_AGNO, and we only return one inode
853 	 * worth of data.
854 	 */
855 	if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
856 		if (hdr->flags & XFS_BULK_IREQ_AGNO)
857 			return -EINVAL;
858 
859 		switch (hdr->ino) {
860 		case XFS_BULK_IREQ_SPECIAL_ROOT:
861 			hdr->ino = mp->m_sb.sb_rootino;
862 			break;
863 		default:
864 			return -EINVAL;
865 		}
866 		breq->icount = 1;
867 	}
868 
869 	/*
870 	 * The IREQ_AGNO flag means that we only want results from a given AG.
871 	 * If @hdr->ino is zero, we start iterating in that AG.  If @hdr->ino is
872 	 * beyond the specified AG then we return no results.
873 	 */
874 	if (hdr->flags & XFS_BULK_IREQ_AGNO) {
875 		if (hdr->agno >= mp->m_sb.sb_agcount)
876 			return -EINVAL;
877 
878 		if (breq->startino == 0)
879 			breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
880 		else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
881 			return -EINVAL;
882 
883 		breq->flags |= XFS_IBULK_SAME_AG;
884 
885 		/* Asking for an inode past the end of the AG?  We're done! */
886 		if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
887 			return -ECANCELED;
888 	} else if (hdr->agno)
889 		return -EINVAL;
890 
891 	/* Asking for an inode past the end of the FS?  We're done! */
892 	if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
893 		return -ECANCELED;
894 
895 	return 0;
896 }
897 
898 /*
899  * Update the userspace bulk request @hdr to reflect the end state of the
900  * internal bulk request @breq.
901  */
902 static void
903 xfs_bulk_ireq_teardown(
904 	struct xfs_bulk_ireq	*hdr,
905 	struct xfs_ibulk	*breq)
906 {
907 	hdr->ino = breq->startino;
908 	hdr->ocount = breq->ocount;
909 }
910 
911 /* Handle the v5 bulkstat ioctl. */
912 STATIC int
913 xfs_ioc_bulkstat(
914 	struct file			*file,
915 	unsigned int			cmd,
916 	struct xfs_bulkstat_req __user	*arg)
917 {
918 	struct xfs_mount		*mp = XFS_I(file_inode(file))->i_mount;
919 	struct xfs_bulk_ireq		hdr;
920 	struct xfs_ibulk		breq = {
921 		.mp			= mp,
922 		.mnt_userns		= file_mnt_user_ns(file),
923 	};
924 	int				error;
925 
926 	if (!capable(CAP_SYS_ADMIN))
927 		return -EPERM;
928 
929 	if (XFS_FORCED_SHUTDOWN(mp))
930 		return -EIO;
931 
932 	if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
933 		return -EFAULT;
934 
935 	error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
936 	if (error == -ECANCELED)
937 		goto out_teardown;
938 	if (error < 0)
939 		return error;
940 
941 	error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
942 	if (error)
943 		return error;
944 
945 out_teardown:
946 	xfs_bulk_ireq_teardown(&hdr, &breq);
947 	if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
948 		return -EFAULT;
949 
950 	return 0;
951 }
952 
953 STATIC int
954 xfs_inumbers_fmt(
955 	struct xfs_ibulk		*breq,
956 	const struct xfs_inumbers	*igrp)
957 {
958 	if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
959 		return -EFAULT;
960 	return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
961 }
962 
963 /* Handle the v5 inumbers ioctl. */
964 STATIC int
965 xfs_ioc_inumbers(
966 	struct xfs_mount		*mp,
967 	unsigned int			cmd,
968 	struct xfs_inumbers_req __user	*arg)
969 {
970 	struct xfs_bulk_ireq		hdr;
971 	struct xfs_ibulk		breq = {
972 		.mp			= mp,
973 	};
974 	int				error;
975 
976 	if (!capable(CAP_SYS_ADMIN))
977 		return -EPERM;
978 
979 	if (XFS_FORCED_SHUTDOWN(mp))
980 		return -EIO;
981 
982 	if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
983 		return -EFAULT;
984 
985 	error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
986 	if (error == -ECANCELED)
987 		goto out_teardown;
988 	if (error < 0)
989 		return error;
990 
991 	error = xfs_inumbers(&breq, xfs_inumbers_fmt);
992 	if (error)
993 		return error;
994 
995 out_teardown:
996 	xfs_bulk_ireq_teardown(&hdr, &breq);
997 	if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
998 		return -EFAULT;
999 
1000 	return 0;
1001 }
1002 
1003 STATIC int
1004 xfs_ioc_fsgeometry(
1005 	struct xfs_mount	*mp,
1006 	void			__user *arg,
1007 	int			struct_version)
1008 {
1009 	struct xfs_fsop_geom	fsgeo;
1010 	size_t			len;
1011 
1012 	xfs_fs_geometry(&mp->m_sb, &fsgeo, struct_version);
1013 
1014 	if (struct_version <= 3)
1015 		len = sizeof(struct xfs_fsop_geom_v1);
1016 	else if (struct_version == 4)
1017 		len = sizeof(struct xfs_fsop_geom_v4);
1018 	else {
1019 		xfs_fsop_geom_health(mp, &fsgeo);
1020 		len = sizeof(fsgeo);
1021 	}
1022 
1023 	if (copy_to_user(arg, &fsgeo, len))
1024 		return -EFAULT;
1025 	return 0;
1026 }
1027 
1028 STATIC int
1029 xfs_ioc_ag_geometry(
1030 	struct xfs_mount	*mp,
1031 	void			__user *arg)
1032 {
1033 	struct xfs_ag_geometry	ageo;
1034 	int			error;
1035 
1036 	if (copy_from_user(&ageo, arg, sizeof(ageo)))
1037 		return -EFAULT;
1038 	if (ageo.ag_flags)
1039 		return -EINVAL;
1040 	if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
1041 		return -EINVAL;
1042 
1043 	error = xfs_ag_get_geometry(mp, ageo.ag_number, &ageo);
1044 	if (error)
1045 		return error;
1046 
1047 	if (copy_to_user(arg, &ageo, sizeof(ageo)))
1048 		return -EFAULT;
1049 	return 0;
1050 }
1051 
1052 /*
1053  * Linux extended inode flags interface.
1054  */
1055 
1056 STATIC unsigned int
1057 xfs_merge_ioc_xflags(
1058 	unsigned int	flags,
1059 	unsigned int	start)
1060 {
1061 	unsigned int	xflags = start;
1062 
1063 	if (flags & FS_IMMUTABLE_FL)
1064 		xflags |= FS_XFLAG_IMMUTABLE;
1065 	else
1066 		xflags &= ~FS_XFLAG_IMMUTABLE;
1067 	if (flags & FS_APPEND_FL)
1068 		xflags |= FS_XFLAG_APPEND;
1069 	else
1070 		xflags &= ~FS_XFLAG_APPEND;
1071 	if (flags & FS_SYNC_FL)
1072 		xflags |= FS_XFLAG_SYNC;
1073 	else
1074 		xflags &= ~FS_XFLAG_SYNC;
1075 	if (flags & FS_NOATIME_FL)
1076 		xflags |= FS_XFLAG_NOATIME;
1077 	else
1078 		xflags &= ~FS_XFLAG_NOATIME;
1079 	if (flags & FS_NODUMP_FL)
1080 		xflags |= FS_XFLAG_NODUMP;
1081 	else
1082 		xflags &= ~FS_XFLAG_NODUMP;
1083 	if (flags & FS_DAX_FL)
1084 		xflags |= FS_XFLAG_DAX;
1085 	else
1086 		xflags &= ~FS_XFLAG_DAX;
1087 
1088 	return xflags;
1089 }
1090 
1091 STATIC unsigned int
1092 xfs_di2lxflags(
1093 	uint16_t	di_flags,
1094 	uint64_t	di_flags2)
1095 {
1096 	unsigned int	flags = 0;
1097 
1098 	if (di_flags & XFS_DIFLAG_IMMUTABLE)
1099 		flags |= FS_IMMUTABLE_FL;
1100 	if (di_flags & XFS_DIFLAG_APPEND)
1101 		flags |= FS_APPEND_FL;
1102 	if (di_flags & XFS_DIFLAG_SYNC)
1103 		flags |= FS_SYNC_FL;
1104 	if (di_flags & XFS_DIFLAG_NOATIME)
1105 		flags |= FS_NOATIME_FL;
1106 	if (di_flags & XFS_DIFLAG_NODUMP)
1107 		flags |= FS_NODUMP_FL;
1108 	if (di_flags2 & XFS_DIFLAG2_DAX) {
1109 		flags |= FS_DAX_FL;
1110 	}
1111 	return flags;
1112 }
1113 
1114 static void
1115 xfs_fill_fsxattr(
1116 	struct xfs_inode	*ip,
1117 	bool			attr,
1118 	struct fsxattr		*fa)
1119 {
1120 	struct xfs_ifork	*ifp = attr ? ip->i_afp : &ip->i_df;
1121 
1122 	simple_fill_fsxattr(fa, xfs_ip2xflags(ip));
1123 	fa->fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
1124 	fa->fsx_cowextsize = ip->i_d.di_cowextsize <<
1125 			ip->i_mount->m_sb.sb_blocklog;
1126 	fa->fsx_projid = ip->i_d.di_projid;
1127 	if (ifp && (ifp->if_flags & XFS_IFEXTENTS))
1128 		fa->fsx_nextents = xfs_iext_count(ifp);
1129 	else
1130 		fa->fsx_nextents = xfs_ifork_nextents(ifp);
1131 }
1132 
1133 STATIC int
1134 xfs_ioc_fsgetxattr(
1135 	xfs_inode_t		*ip,
1136 	int			attr,
1137 	void			__user *arg)
1138 {
1139 	struct fsxattr		fa;
1140 
1141 	xfs_ilock(ip, XFS_ILOCK_SHARED);
1142 	xfs_fill_fsxattr(ip, attr, &fa);
1143 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
1144 
1145 	if (copy_to_user(arg, &fa, sizeof(fa)))
1146 		return -EFAULT;
1147 	return 0;
1148 }
1149 
1150 STATIC uint16_t
1151 xfs_flags2diflags(
1152 	struct xfs_inode	*ip,
1153 	unsigned int		xflags)
1154 {
1155 	/* can't set PREALLOC this way, just preserve it */
1156 	uint16_t		di_flags =
1157 		(ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
1158 
1159 	if (xflags & FS_XFLAG_IMMUTABLE)
1160 		di_flags |= XFS_DIFLAG_IMMUTABLE;
1161 	if (xflags & FS_XFLAG_APPEND)
1162 		di_flags |= XFS_DIFLAG_APPEND;
1163 	if (xflags & FS_XFLAG_SYNC)
1164 		di_flags |= XFS_DIFLAG_SYNC;
1165 	if (xflags & FS_XFLAG_NOATIME)
1166 		di_flags |= XFS_DIFLAG_NOATIME;
1167 	if (xflags & FS_XFLAG_NODUMP)
1168 		di_flags |= XFS_DIFLAG_NODUMP;
1169 	if (xflags & FS_XFLAG_NODEFRAG)
1170 		di_flags |= XFS_DIFLAG_NODEFRAG;
1171 	if (xflags & FS_XFLAG_FILESTREAM)
1172 		di_flags |= XFS_DIFLAG_FILESTREAM;
1173 	if (S_ISDIR(VFS_I(ip)->i_mode)) {
1174 		if (xflags & FS_XFLAG_RTINHERIT)
1175 			di_flags |= XFS_DIFLAG_RTINHERIT;
1176 		if (xflags & FS_XFLAG_NOSYMLINKS)
1177 			di_flags |= XFS_DIFLAG_NOSYMLINKS;
1178 		if (xflags & FS_XFLAG_EXTSZINHERIT)
1179 			di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1180 		if (xflags & FS_XFLAG_PROJINHERIT)
1181 			di_flags |= XFS_DIFLAG_PROJINHERIT;
1182 	} else if (S_ISREG(VFS_I(ip)->i_mode)) {
1183 		if (xflags & FS_XFLAG_REALTIME)
1184 			di_flags |= XFS_DIFLAG_REALTIME;
1185 		if (xflags & FS_XFLAG_EXTSIZE)
1186 			di_flags |= XFS_DIFLAG_EXTSIZE;
1187 	}
1188 
1189 	return di_flags;
1190 }
1191 
1192 STATIC uint64_t
1193 xfs_flags2diflags2(
1194 	struct xfs_inode	*ip,
1195 	unsigned int		xflags)
1196 {
1197 	uint64_t		di_flags2 =
1198 		(ip->i_d.di_flags2 & (XFS_DIFLAG2_REFLINK |
1199 				      XFS_DIFLAG2_BIGTIME));
1200 
1201 	if (xflags & FS_XFLAG_DAX)
1202 		di_flags2 |= XFS_DIFLAG2_DAX;
1203 	if (xflags & FS_XFLAG_COWEXTSIZE)
1204 		di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1205 
1206 	return di_flags2;
1207 }
1208 
1209 static int
1210 xfs_ioctl_setattr_xflags(
1211 	struct xfs_trans	*tp,
1212 	struct xfs_inode	*ip,
1213 	struct fsxattr		*fa)
1214 {
1215 	struct xfs_mount	*mp = ip->i_mount;
1216 	uint64_t		di_flags2;
1217 
1218 	/* Can't change realtime flag if any extents are allocated. */
1219 	if ((ip->i_df.if_nextents || ip->i_delayed_blks) &&
1220 	    XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
1221 		return -EINVAL;
1222 
1223 	/* If realtime flag is set then must have realtime device */
1224 	if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
1225 		if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
1226 		    (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
1227 			return -EINVAL;
1228 	}
1229 
1230 	/* Clear reflink if we are actually able to set the rt flag. */
1231 	if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
1232 		ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1233 
1234 	/* Don't allow us to set DAX mode for a reflinked file for now. */
1235 	if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
1236 		return -EINVAL;
1237 
1238 	/* diflags2 only valid for v3 inodes. */
1239 	di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
1240 	if (di_flags2 && !xfs_sb_version_has_v3inode(&mp->m_sb))
1241 		return -EINVAL;
1242 
1243 	ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
1244 	ip->i_d.di_flags2 = di_flags2;
1245 
1246 	xfs_diflags_to_iflags(ip, false);
1247 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1248 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1249 	XFS_STATS_INC(mp, xs_ig_attrchg);
1250 	return 0;
1251 }
1252 
1253 static void
1254 xfs_ioctl_setattr_prepare_dax(
1255 	struct xfs_inode	*ip,
1256 	struct fsxattr		*fa)
1257 {
1258 	struct xfs_mount	*mp = ip->i_mount;
1259 	struct inode            *inode = VFS_I(ip);
1260 
1261 	if (S_ISDIR(inode->i_mode))
1262 		return;
1263 
1264 	if ((mp->m_flags & XFS_MOUNT_DAX_ALWAYS) ||
1265 	    (mp->m_flags & XFS_MOUNT_DAX_NEVER))
1266 		return;
1267 
1268 	if (((fa->fsx_xflags & FS_XFLAG_DAX) &&
1269 	    !(ip->i_d.di_flags2 & XFS_DIFLAG2_DAX)) ||
1270 	    (!(fa->fsx_xflags & FS_XFLAG_DAX) &&
1271 	     (ip->i_d.di_flags2 & XFS_DIFLAG2_DAX)))
1272 		d_mark_dontcache(inode);
1273 }
1274 
1275 /*
1276  * Set up the transaction structure for the setattr operation, checking that we
1277  * have permission to do so. On success, return a clean transaction and the
1278  * inode locked exclusively ready for further operation specific checks. On
1279  * failure, return an error without modifying or locking the inode.
1280  */
1281 static struct xfs_trans *
1282 xfs_ioctl_setattr_get_trans(
1283 	struct file		*file)
1284 {
1285 	struct xfs_inode	*ip = XFS_I(file_inode(file));
1286 	struct xfs_mount	*mp = ip->i_mount;
1287 	struct xfs_trans	*tp;
1288 	int			error = -EROFS;
1289 
1290 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1291 		goto out_unlock;
1292 	error = -EIO;
1293 	if (XFS_FORCED_SHUTDOWN(mp))
1294 		goto out_unlock;
1295 
1296 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1297 	if (error)
1298 		goto out_unlock;
1299 
1300 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1301 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1302 
1303 	/*
1304 	 * CAP_FOWNER overrides the following restrictions:
1305 	 *
1306 	 * The user ID of the calling process must be equal to the file owner
1307 	 * ID, except in cases where the CAP_FSETID capability is applicable.
1308 	 */
1309 	if (!inode_owner_or_capable(file_mnt_user_ns(file), VFS_I(ip))) {
1310 		error = -EPERM;
1311 		goto out_cancel;
1312 	}
1313 
1314 	if (mp->m_flags & XFS_MOUNT_WSYNC)
1315 		xfs_trans_set_sync(tp);
1316 
1317 	return tp;
1318 
1319 out_cancel:
1320 	xfs_trans_cancel(tp);
1321 out_unlock:
1322 	return ERR_PTR(error);
1323 }
1324 
1325 /*
1326  * extent size hint validation is somewhat cumbersome. Rules are:
1327  *
1328  * 1. extent size hint is only valid for directories and regular files
1329  * 2. FS_XFLAG_EXTSIZE is only valid for regular files
1330  * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
1331  * 4. can only be changed on regular files if no extents are allocated
1332  * 5. can be changed on directories at any time
1333  * 6. extsize hint of 0 turns off hints, clears inode flags.
1334  * 7. Extent size must be a multiple of the appropriate block size.
1335  * 8. for non-realtime files, the extent size hint must be limited
1336  *    to half the AG size to avoid alignment extending the extent beyond the
1337  *    limits of the AG.
1338  *
1339  * Please keep this function in sync with xfs_scrub_inode_extsize.
1340  */
1341 static int
1342 xfs_ioctl_setattr_check_extsize(
1343 	struct xfs_inode	*ip,
1344 	struct fsxattr		*fa)
1345 {
1346 	struct xfs_mount	*mp = ip->i_mount;
1347 	xfs_extlen_t		size;
1348 	xfs_fsblock_t		extsize_fsb;
1349 
1350 	if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_df.if_nextents &&
1351 	    ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
1352 		return -EINVAL;
1353 
1354 	if (fa->fsx_extsize == 0)
1355 		return 0;
1356 
1357 	extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
1358 	if (extsize_fsb > MAXEXTLEN)
1359 		return -EINVAL;
1360 
1361 	if (XFS_IS_REALTIME_INODE(ip) ||
1362 	    (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
1363 		size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
1364 	} else {
1365 		size = mp->m_sb.sb_blocksize;
1366 		if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
1367 			return -EINVAL;
1368 	}
1369 
1370 	if (fa->fsx_extsize % size)
1371 		return -EINVAL;
1372 
1373 	return 0;
1374 }
1375 
1376 /*
1377  * CoW extent size hint validation rules are:
1378  *
1379  * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
1380  *    The inode does not have to have any shared blocks, but it must be a v3.
1381  * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
1382  *    for a directory, the hint is propagated to new files.
1383  * 3. Can be changed on files & directories at any time.
1384  * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
1385  * 5. Extent size must be a multiple of the appropriate block size.
1386  * 6. The extent size hint must be limited to half the AG size to avoid
1387  *    alignment extending the extent beyond the limits of the AG.
1388  *
1389  * Please keep this function in sync with xfs_scrub_inode_cowextsize.
1390  */
1391 static int
1392 xfs_ioctl_setattr_check_cowextsize(
1393 	struct xfs_inode	*ip,
1394 	struct fsxattr		*fa)
1395 {
1396 	struct xfs_mount	*mp = ip->i_mount;
1397 	xfs_extlen_t		size;
1398 	xfs_fsblock_t		cowextsize_fsb;
1399 
1400 	if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
1401 		return 0;
1402 
1403 	if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb))
1404 		return -EINVAL;
1405 
1406 	if (fa->fsx_cowextsize == 0)
1407 		return 0;
1408 
1409 	cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
1410 	if (cowextsize_fsb > MAXEXTLEN)
1411 		return -EINVAL;
1412 
1413 	size = mp->m_sb.sb_blocksize;
1414 	if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
1415 		return -EINVAL;
1416 
1417 	if (fa->fsx_cowextsize % size)
1418 		return -EINVAL;
1419 
1420 	return 0;
1421 }
1422 
1423 static int
1424 xfs_ioctl_setattr_check_projid(
1425 	struct xfs_inode	*ip,
1426 	struct fsxattr		*fa)
1427 {
1428 	/* Disallow 32bit project ids if projid32bit feature is not enabled. */
1429 	if (fa->fsx_projid > (uint16_t)-1 &&
1430 	    !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
1431 		return -EINVAL;
1432 	return 0;
1433 }
1434 
1435 STATIC int
1436 xfs_ioctl_setattr(
1437 	struct file		*file,
1438 	struct fsxattr		*fa)
1439 {
1440 	struct user_namespace	*mnt_userns = file_mnt_user_ns(file);
1441 	struct xfs_inode	*ip = XFS_I(file_inode(file));
1442 	struct fsxattr		old_fa;
1443 	struct xfs_mount	*mp = ip->i_mount;
1444 	struct xfs_trans	*tp;
1445 	struct xfs_dquot	*pdqp = NULL;
1446 	struct xfs_dquot	*olddquot = NULL;
1447 	int			code;
1448 
1449 	trace_xfs_ioctl_setattr(ip);
1450 
1451 	code = xfs_ioctl_setattr_check_projid(ip, fa);
1452 	if (code)
1453 		return code;
1454 
1455 	/*
1456 	 * If disk quotas is on, we make sure that the dquots do exist on disk,
1457 	 * before we start any other transactions. Trying to do this later
1458 	 * is messy. We don't care to take a readlock to look at the ids
1459 	 * in inode here, because we can't hold it across the trans_reserve.
1460 	 * If the IDs do change before we take the ilock, we're covered
1461 	 * because the i_*dquot fields will get updated anyway.
1462 	 */
1463 	if (XFS_IS_QUOTA_ON(mp)) {
1464 		code = xfs_qm_vop_dqalloc(ip, VFS_I(ip)->i_uid,
1465 				VFS_I(ip)->i_gid, fa->fsx_projid,
1466 				XFS_QMOPT_PQUOTA, NULL, NULL, &pdqp);
1467 		if (code)
1468 			return code;
1469 	}
1470 
1471 	xfs_ioctl_setattr_prepare_dax(ip, fa);
1472 
1473 	tp = xfs_ioctl_setattr_get_trans(file);
1474 	if (IS_ERR(tp)) {
1475 		code = PTR_ERR(tp);
1476 		goto error_free_dquots;
1477 	}
1478 
1479 	if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
1480 	    ip->i_d.di_projid != fa->fsx_projid) {
1481 		code = xfs_qm_vop_chown_reserve(tp, ip, NULL, NULL, pdqp,
1482 				capable(CAP_FOWNER) ?  XFS_QMOPT_FORCE_RES : 0);
1483 		if (code)	/* out of quota */
1484 			goto error_trans_cancel;
1485 	}
1486 
1487 	xfs_fill_fsxattr(ip, false, &old_fa);
1488 	code = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, fa);
1489 	if (code)
1490 		goto error_trans_cancel;
1491 
1492 	code = xfs_ioctl_setattr_check_extsize(ip, fa);
1493 	if (code)
1494 		goto error_trans_cancel;
1495 
1496 	code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
1497 	if (code)
1498 		goto error_trans_cancel;
1499 
1500 	code = xfs_ioctl_setattr_xflags(tp, ip, fa);
1501 	if (code)
1502 		goto error_trans_cancel;
1503 
1504 	/*
1505 	 * Change file ownership.  Must be the owner or privileged.  CAP_FSETID
1506 	 * overrides the following restrictions:
1507 	 *
1508 	 * The set-user-ID and set-group-ID bits of a file will be cleared upon
1509 	 * successful return from chown()
1510 	 */
1511 
1512 	if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
1513 	    !capable_wrt_inode_uidgid(mnt_userns, VFS_I(ip), CAP_FSETID))
1514 		VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
1515 
1516 	/* Change the ownerships and register project quota modifications */
1517 	if (ip->i_d.di_projid != fa->fsx_projid) {
1518 		if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1519 			olddquot = xfs_qm_vop_chown(tp, ip,
1520 						&ip->i_pdquot, pdqp);
1521 		}
1522 		ip->i_d.di_projid = fa->fsx_projid;
1523 	}
1524 
1525 	/*
1526 	 * Only set the extent size hint if we've already determined that the
1527 	 * extent size hint should be set on the inode. If no extent size flags
1528 	 * are set on the inode then unconditionally clear the extent size hint.
1529 	 */
1530 	if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
1531 		ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
1532 	else
1533 		ip->i_d.di_extsize = 0;
1534 	if (xfs_sb_version_has_v3inode(&mp->m_sb) &&
1535 	    (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1536 		ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
1537 				mp->m_sb.sb_blocklog;
1538 	else
1539 		ip->i_d.di_cowextsize = 0;
1540 
1541 	code = xfs_trans_commit(tp);
1542 
1543 	/*
1544 	 * Release any dquot(s) the inode had kept before chown.
1545 	 */
1546 	xfs_qm_dqrele(olddquot);
1547 	xfs_qm_dqrele(pdqp);
1548 
1549 	return code;
1550 
1551 error_trans_cancel:
1552 	xfs_trans_cancel(tp);
1553 error_free_dquots:
1554 	xfs_qm_dqrele(pdqp);
1555 	return code;
1556 }
1557 
1558 STATIC int
1559 xfs_ioc_fssetxattr(
1560 	struct file		*filp,
1561 	void			__user *arg)
1562 {
1563 	struct fsxattr		fa;
1564 	int error;
1565 
1566 	if (copy_from_user(&fa, arg, sizeof(fa)))
1567 		return -EFAULT;
1568 
1569 	error = mnt_want_write_file(filp);
1570 	if (error)
1571 		return error;
1572 	error = xfs_ioctl_setattr(filp, &fa);
1573 	mnt_drop_write_file(filp);
1574 	return error;
1575 }
1576 
1577 STATIC int
1578 xfs_ioc_getxflags(
1579 	xfs_inode_t		*ip,
1580 	void			__user *arg)
1581 {
1582 	unsigned int		flags;
1583 
1584 	flags = xfs_di2lxflags(ip->i_d.di_flags, ip->i_d.di_flags2);
1585 	if (copy_to_user(arg, &flags, sizeof(flags)))
1586 		return -EFAULT;
1587 	return 0;
1588 }
1589 
1590 STATIC int
1591 xfs_ioc_setxflags(
1592 	struct xfs_inode	*ip,
1593 	struct file		*filp,
1594 	void			__user *arg)
1595 {
1596 	struct xfs_trans	*tp;
1597 	struct fsxattr		fa;
1598 	struct fsxattr		old_fa;
1599 	unsigned int		flags;
1600 	int			error;
1601 
1602 	if (copy_from_user(&flags, arg, sizeof(flags)))
1603 		return -EFAULT;
1604 
1605 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1606 		      FS_NOATIME_FL | FS_NODUMP_FL | \
1607 		      FS_SYNC_FL | FS_DAX_FL))
1608 		return -EOPNOTSUPP;
1609 
1610 	fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1611 
1612 	error = mnt_want_write_file(filp);
1613 	if (error)
1614 		return error;
1615 
1616 	xfs_ioctl_setattr_prepare_dax(ip, &fa);
1617 
1618 	tp = xfs_ioctl_setattr_get_trans(filp);
1619 	if (IS_ERR(tp)) {
1620 		error = PTR_ERR(tp);
1621 		goto out_drop_write;
1622 	}
1623 
1624 	xfs_fill_fsxattr(ip, false, &old_fa);
1625 	error = vfs_ioc_fssetxattr_check(VFS_I(ip), &old_fa, &fa);
1626 	if (error) {
1627 		xfs_trans_cancel(tp);
1628 		goto out_drop_write;
1629 	}
1630 
1631 	error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
1632 	if (error) {
1633 		xfs_trans_cancel(tp);
1634 		goto out_drop_write;
1635 	}
1636 
1637 	error = xfs_trans_commit(tp);
1638 out_drop_write:
1639 	mnt_drop_write_file(filp);
1640 	return error;
1641 }
1642 
1643 static bool
1644 xfs_getbmap_format(
1645 	struct kgetbmap		*p,
1646 	struct getbmapx __user	*u,
1647 	size_t			recsize)
1648 {
1649 	if (put_user(p->bmv_offset, &u->bmv_offset) ||
1650 	    put_user(p->bmv_block, &u->bmv_block) ||
1651 	    put_user(p->bmv_length, &u->bmv_length) ||
1652 	    put_user(0, &u->bmv_count) ||
1653 	    put_user(0, &u->bmv_entries))
1654 		return false;
1655 	if (recsize < sizeof(struct getbmapx))
1656 		return true;
1657 	if (put_user(0, &u->bmv_iflags) ||
1658 	    put_user(p->bmv_oflags, &u->bmv_oflags) ||
1659 	    put_user(0, &u->bmv_unused1) ||
1660 	    put_user(0, &u->bmv_unused2))
1661 		return false;
1662 	return true;
1663 }
1664 
1665 STATIC int
1666 xfs_ioc_getbmap(
1667 	struct file		*file,
1668 	unsigned int		cmd,
1669 	void			__user *arg)
1670 {
1671 	struct getbmapx		bmx = { 0 };
1672 	struct kgetbmap		*buf;
1673 	size_t			recsize;
1674 	int			error, i;
1675 
1676 	switch (cmd) {
1677 	case XFS_IOC_GETBMAPA:
1678 		bmx.bmv_iflags = BMV_IF_ATTRFORK;
1679 		/*FALLTHRU*/
1680 	case XFS_IOC_GETBMAP:
1681 		if (file->f_mode & FMODE_NOCMTIME)
1682 			bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1683 		/* struct getbmap is a strict subset of struct getbmapx. */
1684 		recsize = sizeof(struct getbmap);
1685 		break;
1686 	case XFS_IOC_GETBMAPX:
1687 		recsize = sizeof(struct getbmapx);
1688 		break;
1689 	default:
1690 		return -EINVAL;
1691 	}
1692 
1693 	if (copy_from_user(&bmx, arg, recsize))
1694 		return -EFAULT;
1695 
1696 	if (bmx.bmv_count < 2)
1697 		return -EINVAL;
1698 	if (bmx.bmv_count > ULONG_MAX / recsize)
1699 		return -ENOMEM;
1700 
1701 	buf = kvzalloc(bmx.bmv_count * sizeof(*buf), GFP_KERNEL);
1702 	if (!buf)
1703 		return -ENOMEM;
1704 
1705 	error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
1706 	if (error)
1707 		goto out_free_buf;
1708 
1709 	error = -EFAULT;
1710 	if (copy_to_user(arg, &bmx, recsize))
1711 		goto out_free_buf;
1712 	arg += recsize;
1713 
1714 	for (i = 0; i < bmx.bmv_entries; i++) {
1715 		if (!xfs_getbmap_format(buf + i, arg, recsize))
1716 			goto out_free_buf;
1717 		arg += recsize;
1718 	}
1719 
1720 	error = 0;
1721 out_free_buf:
1722 	kmem_free(buf);
1723 	return error;
1724 }
1725 
1726 STATIC int
1727 xfs_ioc_getfsmap(
1728 	struct xfs_inode	*ip,
1729 	struct fsmap_head	__user *arg)
1730 {
1731 	struct xfs_fsmap_head	xhead = {0};
1732 	struct fsmap_head	head;
1733 	struct fsmap		*recs;
1734 	unsigned int		count;
1735 	__u32			last_flags = 0;
1736 	bool			done = false;
1737 	int			error;
1738 
1739 	if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1740 		return -EFAULT;
1741 	if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1742 	    memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1743 		       sizeof(head.fmh_keys[0].fmr_reserved)) ||
1744 	    memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1745 		       sizeof(head.fmh_keys[1].fmr_reserved)))
1746 		return -EINVAL;
1747 
1748 	/*
1749 	 * Use an internal memory buffer so that we don't have to copy fsmap
1750 	 * data to userspace while holding locks.  Start by trying to allocate
1751 	 * up to 128k for the buffer, but fall back to a single page if needed.
1752 	 */
1753 	count = min_t(unsigned int, head.fmh_count,
1754 			131072 / sizeof(struct fsmap));
1755 	recs = kvzalloc(count * sizeof(struct fsmap), GFP_KERNEL);
1756 	if (!recs) {
1757 		count = min_t(unsigned int, head.fmh_count,
1758 				PAGE_SIZE / sizeof(struct fsmap));
1759 		recs = kvzalloc(count * sizeof(struct fsmap), GFP_KERNEL);
1760 		if (!recs)
1761 			return -ENOMEM;
1762 	}
1763 
1764 	xhead.fmh_iflags = head.fmh_iflags;
1765 	xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1766 	xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1767 
1768 	trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1769 	trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1770 
1771 	head.fmh_entries = 0;
1772 	do {
1773 		struct fsmap __user	*user_recs;
1774 		struct fsmap		*last_rec;
1775 
1776 		user_recs = &arg->fmh_recs[head.fmh_entries];
1777 		xhead.fmh_entries = 0;
1778 		xhead.fmh_count = min_t(unsigned int, count,
1779 					head.fmh_count - head.fmh_entries);
1780 
1781 		/* Run query, record how many entries we got. */
1782 		error = xfs_getfsmap(ip->i_mount, &xhead, recs);
1783 		switch (error) {
1784 		case 0:
1785 			/*
1786 			 * There are no more records in the result set.  Copy
1787 			 * whatever we got to userspace and break out.
1788 			 */
1789 			done = true;
1790 			break;
1791 		case -ECANCELED:
1792 			/*
1793 			 * The internal memory buffer is full.  Copy whatever
1794 			 * records we got to userspace and go again if we have
1795 			 * not yet filled the userspace buffer.
1796 			 */
1797 			error = 0;
1798 			break;
1799 		default:
1800 			goto out_free;
1801 		}
1802 		head.fmh_entries += xhead.fmh_entries;
1803 		head.fmh_oflags = xhead.fmh_oflags;
1804 
1805 		/*
1806 		 * If the caller wanted a record count or there aren't any
1807 		 * new records to return, we're done.
1808 		 */
1809 		if (head.fmh_count == 0 || xhead.fmh_entries == 0)
1810 			break;
1811 
1812 		/* Copy all the records we got out to userspace. */
1813 		if (copy_to_user(user_recs, recs,
1814 				 xhead.fmh_entries * sizeof(struct fsmap))) {
1815 			error = -EFAULT;
1816 			goto out_free;
1817 		}
1818 
1819 		/* Remember the last record flags we copied to userspace. */
1820 		last_rec = &recs[xhead.fmh_entries - 1];
1821 		last_flags = last_rec->fmr_flags;
1822 
1823 		/* Set up the low key for the next iteration. */
1824 		xfs_fsmap_to_internal(&xhead.fmh_keys[0], last_rec);
1825 		trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1826 	} while (!done && head.fmh_entries < head.fmh_count);
1827 
1828 	/*
1829 	 * If there are no more records in the query result set and we're not
1830 	 * in counting mode, mark the last record returned with the LAST flag.
1831 	 */
1832 	if (done && head.fmh_count > 0 && head.fmh_entries > 0) {
1833 		struct fsmap __user	*user_rec;
1834 
1835 		last_flags |= FMR_OF_LAST;
1836 		user_rec = &arg->fmh_recs[head.fmh_entries - 1];
1837 
1838 		if (copy_to_user(&user_rec->fmr_flags, &last_flags,
1839 					sizeof(last_flags))) {
1840 			error = -EFAULT;
1841 			goto out_free;
1842 		}
1843 	}
1844 
1845 	/* copy back header */
1846 	if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) {
1847 		error = -EFAULT;
1848 		goto out_free;
1849 	}
1850 
1851 out_free:
1852 	kmem_free(recs);
1853 	return error;
1854 }
1855 
1856 STATIC int
1857 xfs_ioc_scrub_metadata(
1858 	struct xfs_inode		*ip,
1859 	void				__user *arg)
1860 {
1861 	struct xfs_scrub_metadata	scrub;
1862 	int				error;
1863 
1864 	if (!capable(CAP_SYS_ADMIN))
1865 		return -EPERM;
1866 
1867 	if (copy_from_user(&scrub, arg, sizeof(scrub)))
1868 		return -EFAULT;
1869 
1870 	error = xfs_scrub_metadata(ip, &scrub);
1871 	if (error)
1872 		return error;
1873 
1874 	if (copy_to_user(arg, &scrub, sizeof(scrub)))
1875 		return -EFAULT;
1876 
1877 	return 0;
1878 }
1879 
1880 int
1881 xfs_ioc_swapext(
1882 	xfs_swapext_t	*sxp)
1883 {
1884 	xfs_inode_t     *ip, *tip;
1885 	struct fd	f, tmp;
1886 	int		error = 0;
1887 
1888 	/* Pull information for the target fd */
1889 	f = fdget((int)sxp->sx_fdtarget);
1890 	if (!f.file) {
1891 		error = -EINVAL;
1892 		goto out;
1893 	}
1894 
1895 	if (!(f.file->f_mode & FMODE_WRITE) ||
1896 	    !(f.file->f_mode & FMODE_READ) ||
1897 	    (f.file->f_flags & O_APPEND)) {
1898 		error = -EBADF;
1899 		goto out_put_file;
1900 	}
1901 
1902 	tmp = fdget((int)sxp->sx_fdtmp);
1903 	if (!tmp.file) {
1904 		error = -EINVAL;
1905 		goto out_put_file;
1906 	}
1907 
1908 	if (!(tmp.file->f_mode & FMODE_WRITE) ||
1909 	    !(tmp.file->f_mode & FMODE_READ) ||
1910 	    (tmp.file->f_flags & O_APPEND)) {
1911 		error = -EBADF;
1912 		goto out_put_tmp_file;
1913 	}
1914 
1915 	if (IS_SWAPFILE(file_inode(f.file)) ||
1916 	    IS_SWAPFILE(file_inode(tmp.file))) {
1917 		error = -EINVAL;
1918 		goto out_put_tmp_file;
1919 	}
1920 
1921 	/*
1922 	 * We need to ensure that the fds passed in point to XFS inodes
1923 	 * before we cast and access them as XFS structures as we have no
1924 	 * control over what the user passes us here.
1925 	 */
1926 	if (f.file->f_op != &xfs_file_operations ||
1927 	    tmp.file->f_op != &xfs_file_operations) {
1928 		error = -EINVAL;
1929 		goto out_put_tmp_file;
1930 	}
1931 
1932 	ip = XFS_I(file_inode(f.file));
1933 	tip = XFS_I(file_inode(tmp.file));
1934 
1935 	if (ip->i_mount != tip->i_mount) {
1936 		error = -EINVAL;
1937 		goto out_put_tmp_file;
1938 	}
1939 
1940 	if (ip->i_ino == tip->i_ino) {
1941 		error = -EINVAL;
1942 		goto out_put_tmp_file;
1943 	}
1944 
1945 	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1946 		error = -EIO;
1947 		goto out_put_tmp_file;
1948 	}
1949 
1950 	error = xfs_swap_extents(ip, tip, sxp);
1951 
1952  out_put_tmp_file:
1953 	fdput(tmp);
1954  out_put_file:
1955 	fdput(f);
1956  out:
1957 	return error;
1958 }
1959 
1960 static int
1961 xfs_ioc_getlabel(
1962 	struct xfs_mount	*mp,
1963 	char			__user *user_label)
1964 {
1965 	struct xfs_sb		*sbp = &mp->m_sb;
1966 	char			label[XFSLABEL_MAX + 1];
1967 
1968 	/* Paranoia */
1969 	BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
1970 
1971 	/* 1 larger than sb_fname, so this ensures a trailing NUL char */
1972 	memset(label, 0, sizeof(label));
1973 	spin_lock(&mp->m_sb_lock);
1974 	strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
1975 	spin_unlock(&mp->m_sb_lock);
1976 
1977 	if (copy_to_user(user_label, label, sizeof(label)))
1978 		return -EFAULT;
1979 	return 0;
1980 }
1981 
1982 static int
1983 xfs_ioc_setlabel(
1984 	struct file		*filp,
1985 	struct xfs_mount	*mp,
1986 	char			__user *newlabel)
1987 {
1988 	struct xfs_sb		*sbp = &mp->m_sb;
1989 	char			label[XFSLABEL_MAX + 1];
1990 	size_t			len;
1991 	int			error;
1992 
1993 	if (!capable(CAP_SYS_ADMIN))
1994 		return -EPERM;
1995 	/*
1996 	 * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
1997 	 * smaller, at 12 bytes.  We copy one more to be sure we find the
1998 	 * (required) NULL character to test the incoming label length.
1999 	 * NB: The on disk label doesn't need to be null terminated.
2000 	 */
2001 	if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
2002 		return -EFAULT;
2003 	len = strnlen(label, XFSLABEL_MAX + 1);
2004 	if (len > sizeof(sbp->sb_fname))
2005 		return -EINVAL;
2006 
2007 	error = mnt_want_write_file(filp);
2008 	if (error)
2009 		return error;
2010 
2011 	spin_lock(&mp->m_sb_lock);
2012 	memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
2013 	memcpy(sbp->sb_fname, label, len);
2014 	spin_unlock(&mp->m_sb_lock);
2015 
2016 	/*
2017 	 * Now we do several things to satisfy userspace.
2018 	 * In addition to normal logging of the primary superblock, we also
2019 	 * immediately write these changes to sector zero for the primary, then
2020 	 * update all backup supers (as xfs_db does for a label change), then
2021 	 * invalidate the block device page cache.  This is so that any prior
2022 	 * buffered reads from userspace (i.e. from blkid) are invalidated,
2023 	 * and userspace will see the newly-written label.
2024 	 */
2025 	error = xfs_sync_sb_buf(mp);
2026 	if (error)
2027 		goto out;
2028 	/*
2029 	 * growfs also updates backup supers so lock against that.
2030 	 */
2031 	mutex_lock(&mp->m_growlock);
2032 	error = xfs_update_secondary_sbs(mp);
2033 	mutex_unlock(&mp->m_growlock);
2034 
2035 	invalidate_bdev(mp->m_ddev_targp->bt_bdev);
2036 
2037 out:
2038 	mnt_drop_write_file(filp);
2039 	return error;
2040 }
2041 
2042 static inline int
2043 xfs_fs_eofblocks_from_user(
2044 	struct xfs_fs_eofblocks		*src,
2045 	struct xfs_eofblocks		*dst)
2046 {
2047 	if (src->eof_version != XFS_EOFBLOCKS_VERSION)
2048 		return -EINVAL;
2049 
2050 	if (src->eof_flags & ~XFS_EOF_FLAGS_VALID)
2051 		return -EINVAL;
2052 
2053 	if (memchr_inv(&src->pad32, 0, sizeof(src->pad32)) ||
2054 	    memchr_inv(src->pad64, 0, sizeof(src->pad64)))
2055 		return -EINVAL;
2056 
2057 	dst->eof_flags = src->eof_flags;
2058 	dst->eof_prid = src->eof_prid;
2059 	dst->eof_min_file_size = src->eof_min_file_size;
2060 
2061 	dst->eof_uid = INVALID_UID;
2062 	if (src->eof_flags & XFS_EOF_FLAGS_UID) {
2063 		dst->eof_uid = make_kuid(current_user_ns(), src->eof_uid);
2064 		if (!uid_valid(dst->eof_uid))
2065 			return -EINVAL;
2066 	}
2067 
2068 	dst->eof_gid = INVALID_GID;
2069 	if (src->eof_flags & XFS_EOF_FLAGS_GID) {
2070 		dst->eof_gid = make_kgid(current_user_ns(), src->eof_gid);
2071 		if (!gid_valid(dst->eof_gid))
2072 			return -EINVAL;
2073 	}
2074 	return 0;
2075 }
2076 
2077 /*
2078  * Note: some of the ioctl's return positive numbers as a
2079  * byte count indicating success, such as readlink_by_handle.
2080  * So we don't "sign flip" like most other routines.  This means
2081  * true errors need to be returned as a negative value.
2082  */
2083 long
2084 xfs_file_ioctl(
2085 	struct file		*filp,
2086 	unsigned int		cmd,
2087 	unsigned long		p)
2088 {
2089 	struct inode		*inode = file_inode(filp);
2090 	struct xfs_inode	*ip = XFS_I(inode);
2091 	struct xfs_mount	*mp = ip->i_mount;
2092 	void			__user *arg = (void __user *)p;
2093 	int			error;
2094 
2095 	trace_xfs_file_ioctl(ip);
2096 
2097 	switch (cmd) {
2098 	case FITRIM:
2099 		return xfs_ioc_trim(mp, arg);
2100 	case FS_IOC_GETFSLABEL:
2101 		return xfs_ioc_getlabel(mp, arg);
2102 	case FS_IOC_SETFSLABEL:
2103 		return xfs_ioc_setlabel(filp, mp, arg);
2104 	case XFS_IOC_ALLOCSP:
2105 	case XFS_IOC_FREESP:
2106 	case XFS_IOC_ALLOCSP64:
2107 	case XFS_IOC_FREESP64: {
2108 		xfs_flock64_t		bf;
2109 
2110 		if (copy_from_user(&bf, arg, sizeof(bf)))
2111 			return -EFAULT;
2112 		return xfs_ioc_space(filp, &bf);
2113 	}
2114 	case XFS_IOC_DIOINFO: {
2115 		struct xfs_buftarg	*target = xfs_inode_buftarg(ip);
2116 		struct dioattr		da;
2117 
2118 		da.d_mem =  da.d_miniosz = target->bt_logical_sectorsize;
2119 		da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
2120 
2121 		if (copy_to_user(arg, &da, sizeof(da)))
2122 			return -EFAULT;
2123 		return 0;
2124 	}
2125 
2126 	case XFS_IOC_FSBULKSTAT_SINGLE:
2127 	case XFS_IOC_FSBULKSTAT:
2128 	case XFS_IOC_FSINUMBERS:
2129 		return xfs_ioc_fsbulkstat(filp, cmd, arg);
2130 
2131 	case XFS_IOC_BULKSTAT:
2132 		return xfs_ioc_bulkstat(filp, cmd, arg);
2133 	case XFS_IOC_INUMBERS:
2134 		return xfs_ioc_inumbers(mp, cmd, arg);
2135 
2136 	case XFS_IOC_FSGEOMETRY_V1:
2137 		return xfs_ioc_fsgeometry(mp, arg, 3);
2138 	case XFS_IOC_FSGEOMETRY_V4:
2139 		return xfs_ioc_fsgeometry(mp, arg, 4);
2140 	case XFS_IOC_FSGEOMETRY:
2141 		return xfs_ioc_fsgeometry(mp, arg, 5);
2142 
2143 	case XFS_IOC_AG_GEOMETRY:
2144 		return xfs_ioc_ag_geometry(mp, arg);
2145 
2146 	case XFS_IOC_GETVERSION:
2147 		return put_user(inode->i_generation, (int __user *)arg);
2148 
2149 	case XFS_IOC_FSGETXATTR:
2150 		return xfs_ioc_fsgetxattr(ip, 0, arg);
2151 	case XFS_IOC_FSGETXATTRA:
2152 		return xfs_ioc_fsgetxattr(ip, 1, arg);
2153 	case XFS_IOC_FSSETXATTR:
2154 		return xfs_ioc_fssetxattr(filp, arg);
2155 	case XFS_IOC_GETXFLAGS:
2156 		return xfs_ioc_getxflags(ip, arg);
2157 	case XFS_IOC_SETXFLAGS:
2158 		return xfs_ioc_setxflags(ip, filp, arg);
2159 
2160 	case XFS_IOC_GETBMAP:
2161 	case XFS_IOC_GETBMAPA:
2162 	case XFS_IOC_GETBMAPX:
2163 		return xfs_ioc_getbmap(filp, cmd, arg);
2164 
2165 	case FS_IOC_GETFSMAP:
2166 		return xfs_ioc_getfsmap(ip, arg);
2167 
2168 	case XFS_IOC_SCRUB_METADATA:
2169 		return xfs_ioc_scrub_metadata(ip, arg);
2170 
2171 	case XFS_IOC_FD_TO_HANDLE:
2172 	case XFS_IOC_PATH_TO_HANDLE:
2173 	case XFS_IOC_PATH_TO_FSHANDLE: {
2174 		xfs_fsop_handlereq_t	hreq;
2175 
2176 		if (copy_from_user(&hreq, arg, sizeof(hreq)))
2177 			return -EFAULT;
2178 		return xfs_find_handle(cmd, &hreq);
2179 	}
2180 	case XFS_IOC_OPEN_BY_HANDLE: {
2181 		xfs_fsop_handlereq_t	hreq;
2182 
2183 		if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2184 			return -EFAULT;
2185 		return xfs_open_by_handle(filp, &hreq);
2186 	}
2187 
2188 	case XFS_IOC_READLINK_BY_HANDLE: {
2189 		xfs_fsop_handlereq_t	hreq;
2190 
2191 		if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
2192 			return -EFAULT;
2193 		return xfs_readlink_by_handle(filp, &hreq);
2194 	}
2195 	case XFS_IOC_ATTRLIST_BY_HANDLE:
2196 		return xfs_attrlist_by_handle(filp, arg);
2197 
2198 	case XFS_IOC_ATTRMULTI_BY_HANDLE:
2199 		return xfs_attrmulti_by_handle(filp, arg);
2200 
2201 	case XFS_IOC_SWAPEXT: {
2202 		struct xfs_swapext	sxp;
2203 
2204 		if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
2205 			return -EFAULT;
2206 		error = mnt_want_write_file(filp);
2207 		if (error)
2208 			return error;
2209 		error = xfs_ioc_swapext(&sxp);
2210 		mnt_drop_write_file(filp);
2211 		return error;
2212 	}
2213 
2214 	case XFS_IOC_FSCOUNTS: {
2215 		xfs_fsop_counts_t out;
2216 
2217 		xfs_fs_counts(mp, &out);
2218 
2219 		if (copy_to_user(arg, &out, sizeof(out)))
2220 			return -EFAULT;
2221 		return 0;
2222 	}
2223 
2224 	case XFS_IOC_SET_RESBLKS: {
2225 		xfs_fsop_resblks_t inout;
2226 		uint64_t	   in;
2227 
2228 		if (!capable(CAP_SYS_ADMIN))
2229 			return -EPERM;
2230 
2231 		if (mp->m_flags & XFS_MOUNT_RDONLY)
2232 			return -EROFS;
2233 
2234 		if (copy_from_user(&inout, arg, sizeof(inout)))
2235 			return -EFAULT;
2236 
2237 		error = mnt_want_write_file(filp);
2238 		if (error)
2239 			return error;
2240 
2241 		/* input parameter is passed in resblks field of structure */
2242 		in = inout.resblks;
2243 		error = xfs_reserve_blocks(mp, &in, &inout);
2244 		mnt_drop_write_file(filp);
2245 		if (error)
2246 			return error;
2247 
2248 		if (copy_to_user(arg, &inout, sizeof(inout)))
2249 			return -EFAULT;
2250 		return 0;
2251 	}
2252 
2253 	case XFS_IOC_GET_RESBLKS: {
2254 		xfs_fsop_resblks_t out;
2255 
2256 		if (!capable(CAP_SYS_ADMIN))
2257 			return -EPERM;
2258 
2259 		error = xfs_reserve_blocks(mp, NULL, &out);
2260 		if (error)
2261 			return error;
2262 
2263 		if (copy_to_user(arg, &out, sizeof(out)))
2264 			return -EFAULT;
2265 
2266 		return 0;
2267 	}
2268 
2269 	case XFS_IOC_FSGROWFSDATA: {
2270 		xfs_growfs_data_t in;
2271 
2272 		if (copy_from_user(&in, arg, sizeof(in)))
2273 			return -EFAULT;
2274 
2275 		error = mnt_want_write_file(filp);
2276 		if (error)
2277 			return error;
2278 		error = xfs_growfs_data(mp, &in);
2279 		mnt_drop_write_file(filp);
2280 		return error;
2281 	}
2282 
2283 	case XFS_IOC_FSGROWFSLOG: {
2284 		xfs_growfs_log_t in;
2285 
2286 		if (copy_from_user(&in, arg, sizeof(in)))
2287 			return -EFAULT;
2288 
2289 		error = mnt_want_write_file(filp);
2290 		if (error)
2291 			return error;
2292 		error = xfs_growfs_log(mp, &in);
2293 		mnt_drop_write_file(filp);
2294 		return error;
2295 	}
2296 
2297 	case XFS_IOC_FSGROWFSRT: {
2298 		xfs_growfs_rt_t in;
2299 
2300 		if (copy_from_user(&in, arg, sizeof(in)))
2301 			return -EFAULT;
2302 
2303 		error = mnt_want_write_file(filp);
2304 		if (error)
2305 			return error;
2306 		error = xfs_growfs_rt(mp, &in);
2307 		mnt_drop_write_file(filp);
2308 		return error;
2309 	}
2310 
2311 	case XFS_IOC_GOINGDOWN: {
2312 		uint32_t in;
2313 
2314 		if (!capable(CAP_SYS_ADMIN))
2315 			return -EPERM;
2316 
2317 		if (get_user(in, (uint32_t __user *)arg))
2318 			return -EFAULT;
2319 
2320 		return xfs_fs_goingdown(mp, in);
2321 	}
2322 
2323 	case XFS_IOC_ERROR_INJECTION: {
2324 		xfs_error_injection_t in;
2325 
2326 		if (!capable(CAP_SYS_ADMIN))
2327 			return -EPERM;
2328 
2329 		if (copy_from_user(&in, arg, sizeof(in)))
2330 			return -EFAULT;
2331 
2332 		return xfs_errortag_add(mp, in.errtag);
2333 	}
2334 
2335 	case XFS_IOC_ERROR_CLEARALL:
2336 		if (!capable(CAP_SYS_ADMIN))
2337 			return -EPERM;
2338 
2339 		return xfs_errortag_clearall(mp);
2340 
2341 	case XFS_IOC_FREE_EOFBLOCKS: {
2342 		struct xfs_fs_eofblocks eofb;
2343 		struct xfs_eofblocks keofb;
2344 
2345 		if (!capable(CAP_SYS_ADMIN))
2346 			return -EPERM;
2347 
2348 		if (mp->m_flags & XFS_MOUNT_RDONLY)
2349 			return -EROFS;
2350 
2351 		if (copy_from_user(&eofb, arg, sizeof(eofb)))
2352 			return -EFAULT;
2353 
2354 		error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
2355 		if (error)
2356 			return error;
2357 
2358 		sb_start_write(mp->m_super);
2359 		error = xfs_icache_free_eofblocks(mp, &keofb);
2360 		sb_end_write(mp->m_super);
2361 		return error;
2362 	}
2363 
2364 	default:
2365 		return -ENOTTY;
2366 	}
2367 }
2368