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