]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/xfs/linux-2.6/xfs_ioctl.c
[XFS] Hook up compat XFS_IOC_ATTRLIST_BY_HANDLE ioctl handler
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / linux-2.6 / xfs_ioctl.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
1da177e4 19#include "xfs_fs.h"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
a844f451 25#include "xfs_ag.h"
1da177e4
LT
26#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_alloc_btree.h"
1da177e4 32#include "xfs_ialloc_btree.h"
a844f451 33#include "xfs_attr_sf.h"
1da177e4
LT
34#include "xfs_dir2_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
a844f451
NS
37#include "xfs_btree.h"
38#include "xfs_ialloc.h"
1da177e4 39#include "xfs_rtalloc.h"
1da177e4 40#include "xfs_itable.h"
a844f451 41#include "xfs_error.h"
1da177e4
LT
42#include "xfs_rw.h"
43#include "xfs_acl.h"
1da177e4 44#include "xfs_attr.h"
a844f451 45#include "xfs_bmap.h"
1da177e4
LT
46#include "xfs_buf_item.h"
47#include "xfs_utils.h"
48#include "xfs_dfrag.h"
49#include "xfs_fsops.h"
993386c1 50#include "xfs_vnodeops.h"
25fe55e8
CH
51#include "xfs_quota.h"
52#include "xfs_inode_item.h"
1da177e4 53
16f7e0fe 54#include <linux/capability.h>
1da177e4
LT
55#include <linux/dcache.h>
56#include <linux/mount.h>
57#include <linux/namei.h>
58#include <linux/pagemap.h>
59
60/*
61 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
62 * a file or fs handle.
63 *
64 * XFS_IOC_PATH_TO_FSHANDLE
65 * returns fs handle for a mount point or path within that mount point
66 * XFS_IOC_FD_TO_HANDLE
67 * returns full handle for a FD opened in user space
68 * XFS_IOC_PATH_TO_HANDLE
69 * returns full handle for a path
70 */
d5547f9f 71int
1da177e4
LT
72xfs_find_handle(
73 unsigned int cmd,
743bb465 74 xfs_fsop_handlereq_t *hreq)
1da177e4
LT
75{
76 int hsize;
77 xfs_handle_t handle;
1da177e4 78 struct inode *inode;
1da177e4 79
1da177e4
LT
80 memset((char *)&handle, 0, sizeof(handle));
81
82 switch (cmd) {
83 case XFS_IOC_PATH_TO_FSHANDLE:
84 case XFS_IOC_PATH_TO_HANDLE: {
2d8f3038 85 struct path path;
743bb465 86 int error = user_lpath((const char __user *)hreq->path, &path);
1da177e4
LT
87 if (error)
88 return error;
89
2d8f3038
AV
90 ASSERT(path.dentry);
91 ASSERT(path.dentry->d_inode);
92 inode = igrab(path.dentry->d_inode);
93 path_put(&path);
1da177e4
LT
94 break;
95 }
96
97 case XFS_IOC_FD_TO_HANDLE: {
98 struct file *file;
99
743bb465 100 file = fget(hreq->fd);
1da177e4
LT
101 if (!file)
102 return -EBADF;
103
e678fb0d
JJS
104 ASSERT(file->f_path.dentry);
105 ASSERT(file->f_path.dentry->d_inode);
106 inode = igrab(file->f_path.dentry->d_inode);
1da177e4
LT
107 fput(file);
108 break;
109 }
110
111 default:
112 ASSERT(0);
113 return -XFS_ERROR(EINVAL);
114 }
115
116 if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
117 /* we're not in XFS anymore, Toto */
118 iput(inode);
119 return -XFS_ERROR(EINVAL);
120 }
121
0432dab2
CH
122 switch (inode->i_mode & S_IFMT) {
123 case S_IFREG:
124 case S_IFDIR:
125 case S_IFLNK:
126 break;
127 default:
1da177e4
LT
128 iput(inode);
129 return -XFS_ERROR(EBADF);
130 }
131
132 /* now we can grab the fsid */
0ce4cfd4 133 memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
2f6f7b3d 134 sizeof(xfs_fsid_t));
1da177e4
LT
135 hsize = sizeof(xfs_fsid_t);
136
137 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
6e7f75ea 138 xfs_inode_t *ip = XFS_I(inode);
1da177e4
LT
139 int lock_mode;
140
141 /* need to get access to the xfs_inode to read the generation */
1da177e4
LT
142 lock_mode = xfs_ilock_map_shared(ip);
143
144 /* fill in fid section of handle from inode */
c6143911
CH
145 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
146 sizeof(handle.ha_fid.fid_len);
147 handle.ha_fid.fid_pad = 0;
148 handle.ha_fid.fid_gen = ip->i_d.di_gen;
149 handle.ha_fid.fid_ino = ip->i_ino;
1da177e4
LT
150
151 xfs_iunlock_map_shared(ip, lock_mode);
152
153 hsize = XFS_HSIZE(handle);
154 }
155
156 /* now copy our handle into the user buffer & write out the size */
743bb465 157 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
158 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32))) {
1da177e4
LT
159 iput(inode);
160 return -XFS_ERROR(EFAULT);
161 }
162
163 iput(inode);
164 return 0;
165}
166
167
168/*
6e7f75ea
CH
169 * Convert userspace handle data into inode.
170 *
171 * We use the fact that all the fsop_handlereq ioctl calls have a data
172 * structure argument whose first component is always a xfs_fsop_handlereq_t,
173 * so we can pass that sub structure into this handy, shared routine.
1da177e4 174 *
6e7f75ea 175 * If no error, caller must always iput the returned inode.
1da177e4
LT
176 */
177STATIC int
178xfs_vget_fsop_handlereq(
179 xfs_mount_t *mp,
180 struct inode *parinode, /* parent inode pointer */
181 xfs_fsop_handlereq_t *hreq,
1da177e4
LT
182 struct inode **inode)
183{
184 void __user *hanp;
185 size_t hlen;
186 xfs_fid_t *xfid;
187 xfs_handle_t *handlep;
188 xfs_handle_t handle;
189 xfs_inode_t *ip;
1da177e4
LT
190 xfs_ino_t ino;
191 __u32 igen;
192 int error;
193
194 /*
195 * Only allow handle opens under a directory.
196 */
197 if (!S_ISDIR(parinode->i_mode))
198 return XFS_ERROR(ENOTDIR);
199
200 hanp = hreq->ihandle;
201 hlen = hreq->ihandlen;
202 handlep = &handle;
203
204 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
205 return XFS_ERROR(EINVAL);
206 if (copy_from_user(handlep, hanp, hlen))
207 return XFS_ERROR(EFAULT);
208 if (hlen < sizeof(*handlep))
209 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
210 if (hlen > sizeof(handlep->ha_fsid)) {
c6143911
CH
211 if (handlep->ha_fid.fid_len !=
212 (hlen - sizeof(handlep->ha_fsid) -
213 sizeof(handlep->ha_fid.fid_len)) ||
214 handlep->ha_fid.fid_pad)
1da177e4
LT
215 return XFS_ERROR(EINVAL);
216 }
217
218 /*
219 * Crack the handle, obtain the inode # & generation #
220 */
221 xfid = (struct xfs_fid *)&handlep->ha_fid;
c6143911
CH
222 if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
223 ino = xfid->fid_ino;
224 igen = xfid->fid_gen;
1da177e4
LT
225 } else {
226 return XFS_ERROR(EINVAL);
227 }
228
229 /*
6e7f75ea 230 * Get the XFS inode, building a Linux inode to go with it.
1da177e4
LT
231 */
232 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
233 if (error)
234 return error;
235 if (ip == NULL)
236 return XFS_ERROR(EIO);
6a7f422d 237 if (ip->i_d.di_gen != igen) {
1da177e4
LT
238 xfs_iput_new(ip, XFS_ILOCK_SHARED);
239 return XFS_ERROR(ENOENT);
240 }
241
1da177e4
LT
242 xfs_iunlock(ip, XFS_ILOCK_SHARED);
243
e4f75291 244 *inode = VFS_I(ip);
1da177e4
LT
245 return 0;
246}
247
d5547f9f 248int
1da177e4
LT
249xfs_open_by_handle(
250 xfs_mount_t *mp,
743bb465 251 xfs_fsop_handlereq_t *hreq,
1da177e4
LT
252 struct file *parfilp,
253 struct inode *parinode)
254{
255 int error;
256 int new_fd;
257 int permflag;
258 struct file *filp;
259 struct inode *inode;
260 struct dentry *dentry;
1da177e4
LT
261
262 if (!capable(CAP_SYS_ADMIN))
263 return -XFS_ERROR(EPERM);
1da177e4 264
743bb465 265 error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode);
1da177e4
LT
266 if (error)
267 return -error;
268
269 /* Restrict xfs_open_by_handle to directories & regular files. */
270 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
271 iput(inode);
272 return -XFS_ERROR(EINVAL);
273 }
274
275#if BITS_PER_LONG != 32
743bb465 276 hreq->oflags |= O_LARGEFILE;
1da177e4
LT
277#endif
278 /* Put open permission in namei format. */
743bb465 279 permflag = hreq->oflags;
1da177e4
LT
280 if ((permflag+1) & O_ACCMODE)
281 permflag++;
282 if (permflag & O_TRUNC)
283 permflag |= 2;
284
285 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
286 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
287 iput(inode);
288 return -XFS_ERROR(EPERM);
289 }
290
291 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
292 iput(inode);
293 return -XFS_ERROR(EACCES);
294 }
295
296 /* Can't write directories. */
297 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
298 iput(inode);
299 return -XFS_ERROR(EISDIR);
300 }
301
302 if ((new_fd = get_unused_fd()) < 0) {
303 iput(inode);
304 return new_fd;
305 }
306
44003728
CH
307 dentry = d_obtain_alias(inode);
308 if (IS_ERR(dentry)) {
1da177e4 309 put_unused_fd(new_fd);
44003728 310 return PTR_ERR(dentry);
1da177e4
LT
311 }
312
313 /* Ensure umount returns EBUSY on umounts while this file is open. */
e678fb0d 314 mntget(parfilp->f_path.mnt);
1da177e4
LT
315
316 /* Create file pointer. */
743bb465 317 filp = dentry_open(dentry, parfilp->f_path.mnt, hreq->oflags);
1da177e4
LT
318 if (IS_ERR(filp)) {
319 put_unused_fd(new_fd);
320 return -XFS_ERROR(-PTR_ERR(filp));
321 }
2e2e7bb1
VA
322 if (inode->i_mode & S_IFREG) {
323 /* invisible operation should not change atime */
324 filp->f_flags |= O_NOATIME;
3562fd45 325 filp->f_op = &xfs_invis_file_operations;
2e2e7bb1 326 }
1da177e4
LT
327
328 fd_install(new_fd, filp);
329 return new_fd;
330}
331
804c83c3
CH
332/*
333 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
334 * unused first argument.
335 */
336STATIC int
337do_readlink(
338 char __user *buffer,
339 int buflen,
340 const char *link)
341{
342 int len;
343
344 len = PTR_ERR(link);
345 if (IS_ERR(link))
346 goto out;
347
348 len = strlen(link);
349 if (len > (unsigned) buflen)
350 len = buflen;
351 if (copy_to_user(buffer, link, len))
352 len = -EFAULT;
353 out:
354 return len;
355}
356
357
d5547f9f 358int
1da177e4
LT
359xfs_readlink_by_handle(
360 xfs_mount_t *mp,
743bb465 361 xfs_fsop_handlereq_t *hreq,
1da177e4
LT
362 struct inode *parinode)
363{
1da177e4 364 struct inode *inode;
1da177e4 365 __u32 olen;
804c83c3
CH
366 void *link;
367 int error;
1da177e4
LT
368
369 if (!capable(CAP_SYS_ADMIN))
370 return -XFS_ERROR(EPERM);
1da177e4 371
743bb465 372 error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode);
1da177e4
LT
373 if (error)
374 return -error;
375
376 /* Restrict this handle operation to symlinks only. */
0432dab2 377 if (!S_ISLNK(inode->i_mode)) {
804c83c3
CH
378 error = -XFS_ERROR(EINVAL);
379 goto out_iput;
1da177e4
LT
380 }
381
743bb465 382 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
804c83c3
CH
383 error = -XFS_ERROR(EFAULT);
384 goto out_iput;
1da177e4 385 }
1da177e4 386
804c83c3
CH
387 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
388 if (!link)
389 goto out_iput;
1da177e4 390
739bfb2a 391 error = -xfs_readlink(XFS_I(inode), link);
67fcaa73 392 if (error)
804c83c3 393 goto out_kfree;
743bb465 394 error = do_readlink(hreq->ohandle, olen, link);
804c83c3
CH
395 if (error)
396 goto out_kfree;
67fcaa73 397
804c83c3
CH
398 out_kfree:
399 kfree(link);
400 out_iput:
401 iput(inode);
402 return error;
1da177e4
LT
403}
404
405STATIC int
406xfs_fssetdm_by_handle(
407 xfs_mount_t *mp,
408 void __user *arg,
1da177e4
LT
409 struct inode *parinode)
410{
411 int error;
412 struct fsdmidata fsd;
413 xfs_fsop_setdm_handlereq_t dmhreq;
414 struct inode *inode;
1da177e4
LT
415
416 if (!capable(CAP_MKNOD))
417 return -XFS_ERROR(EPERM);
418 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
419 return -XFS_ERROR(EFAULT);
420
6e7f75ea 421 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
1da177e4
LT
422 if (error)
423 return -error;
424
425 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
6e7f75ea
CH
426 error = -XFS_ERROR(EPERM);
427 goto out;
1da177e4
LT
428 }
429
430 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
6e7f75ea
CH
431 error = -XFS_ERROR(EFAULT);
432 goto out;
1da177e4
LT
433 }
434
6e7f75ea
CH
435 error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
436 fsd.fsd_dmstate);
1da177e4 437
6e7f75ea
CH
438 out:
439 iput(inode);
440 return error;
1da177e4
LT
441}
442
443STATIC int
444xfs_attrlist_by_handle(
445 xfs_mount_t *mp,
446 void __user *arg,
1da177e4
LT
447 struct inode *parinode)
448{
449 int error;
450 attrlist_cursor_kern_t *cursor;
451 xfs_fsop_attrlist_handlereq_t al_hreq;
452 struct inode *inode;
1da177e4
LT
453 char *kbuf;
454
455 if (!capable(CAP_SYS_ADMIN))
456 return -XFS_ERROR(EPERM);
457 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
458 return -XFS_ERROR(EFAULT);
459 if (al_hreq.buflen > XATTR_LIST_MAX)
460 return -XFS_ERROR(EINVAL);
461
90ad58a8
CH
462 /*
463 * Reject flags, only allow namespaces.
464 */
465 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
466 return -XFS_ERROR(EINVAL);
467
6e7f75ea 468 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
1da177e4
LT
469 if (error)
470 goto out;
471
472 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
473 if (!kbuf)
474 goto out_vn_rele;
475
476 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
739bfb2a
CH
477 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
478 al_hreq.flags, cursor);
1da177e4
LT
479 if (error)
480 goto out_kfree;
481
482 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
483 error = -EFAULT;
484
485 out_kfree:
486 kfree(kbuf);
487 out_vn_rele:
6e7f75ea 488 iput(inode);
1da177e4
LT
489 out:
490 return -error;
491}
492
493STATIC int
494xfs_attrmulti_attr_get(
739bfb2a 495 struct inode *inode,
1da177e4
LT
496 char *name,
497 char __user *ubuf,
498 __uint32_t *len,
499 __uint32_t flags)
500{
501 char *kbuf;
502 int error = EFAULT;
e8b0ebaa 503
1da177e4
LT
504 if (*len > XATTR_SIZE_MAX)
505 return EINVAL;
506 kbuf = kmalloc(*len, GFP_KERNEL);
507 if (!kbuf)
508 return ENOMEM;
509
e8b0ebaa 510 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
1da177e4
LT
511 if (error)
512 goto out_kfree;
513
514 if (copy_to_user(ubuf, kbuf, *len))
515 error = EFAULT;
516
517 out_kfree:
518 kfree(kbuf);
519 return error;
520}
521
522STATIC int
523xfs_attrmulti_attr_set(
739bfb2a 524 struct inode *inode,
1da177e4
LT
525 char *name,
526 const char __user *ubuf,
527 __uint32_t len,
528 __uint32_t flags)
529{
530 char *kbuf;
531 int error = EFAULT;
532
739bfb2a 533 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1da177e4
LT
534 return EPERM;
535 if (len > XATTR_SIZE_MAX)
536 return EINVAL;
537
538 kbuf = kmalloc(len, GFP_KERNEL);
539 if (!kbuf)
540 return ENOMEM;
541
542 if (copy_from_user(kbuf, ubuf, len))
543 goto out_kfree;
e8b0ebaa 544
739bfb2a 545 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
1da177e4
LT
546
547 out_kfree:
548 kfree(kbuf);
549 return error;
550}
551
552STATIC int
553xfs_attrmulti_attr_remove(
739bfb2a 554 struct inode *inode,
1da177e4
LT
555 char *name,
556 __uint32_t flags)
557{
739bfb2a 558 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1da177e4 559 return EPERM;
739bfb2a 560 return xfs_attr_remove(XFS_I(inode), name, flags);
1da177e4
LT
561}
562
563STATIC int
564xfs_attrmulti_by_handle(
565 xfs_mount_t *mp,
566 void __user *arg,
42a74f20 567 struct file *parfilp,
1da177e4
LT
568 struct inode *parinode)
569{
570 int error;
571 xfs_attr_multiop_t *ops;
572 xfs_fsop_attrmulti_handlereq_t am_hreq;
573 struct inode *inode;
1da177e4
LT
574 unsigned int i, size;
575 char *attr_name;
576
577 if (!capable(CAP_SYS_ADMIN))
578 return -XFS_ERROR(EPERM);
579 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
580 return -XFS_ERROR(EFAULT);
581
6e7f75ea 582 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
1da177e4
LT
583 if (error)
584 goto out;
585
586 error = E2BIG;
e182f57a 587 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
1da177e4
LT
588 if (!size || size > 16 * PAGE_SIZE)
589 goto out_vn_rele;
590
591 error = ENOMEM;
592 ops = kmalloc(size, GFP_KERNEL);
593 if (!ops)
594 goto out_vn_rele;
595
596 error = EFAULT;
597 if (copy_from_user(ops, am_hreq.ops, size))
598 goto out_kfree_ops;
599
600 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
601 if (!attr_name)
602 goto out_kfree_ops;
603
604
605 error = 0;
606 for (i = 0; i < am_hreq.opcount; i++) {
607 ops[i].am_error = strncpy_from_user(attr_name,
608 ops[i].am_attrname, MAXNAMELEN);
609 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
610 error = -ERANGE;
611 if (ops[i].am_error < 0)
612 break;
613
614 switch (ops[i].am_opcode) {
615 case ATTR_OP_GET:
739bfb2a 616 ops[i].am_error = xfs_attrmulti_attr_get(inode,
1da177e4
LT
617 attr_name, ops[i].am_attrvalue,
618 &ops[i].am_length, ops[i].am_flags);
619 break;
620 case ATTR_OP_SET:
42a74f20
DH
621 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
622 if (ops[i].am_error)
623 break;
739bfb2a 624 ops[i].am_error = xfs_attrmulti_attr_set(inode,
1da177e4
LT
625 attr_name, ops[i].am_attrvalue,
626 ops[i].am_length, ops[i].am_flags);
42a74f20 627 mnt_drop_write(parfilp->f_path.mnt);
1da177e4
LT
628 break;
629 case ATTR_OP_REMOVE:
42a74f20
DH
630 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
631 if (ops[i].am_error)
632 break;
739bfb2a 633 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
1da177e4 634 attr_name, ops[i].am_flags);
42a74f20 635 mnt_drop_write(parfilp->f_path.mnt);
1da177e4
LT
636 break;
637 default:
638 ops[i].am_error = EINVAL;
639 }
640 }
641
642 if (copy_to_user(am_hreq.ops, ops, size))
643 error = XFS_ERROR(EFAULT);
644
645 kfree(attr_name);
646 out_kfree_ops:
647 kfree(ops);
648 out_vn_rele:
6e7f75ea 649 iput(inode);
1da177e4
LT
650 out:
651 return -error;
652}
653
d5547f9f 654int
1da177e4 655xfs_ioc_space(
993386c1 656 struct xfs_inode *ip,
f37ea149 657 struct inode *inode,
1da177e4
LT
658 struct file *filp,
659 int ioflags,
660 unsigned int cmd,
743bb465 661 xfs_flock64_t *bf)
1da177e4 662{
1da177e4
LT
663 int attr_flags = 0;
664 int error;
665
743bb465 666 /*
667 * Only allow the sys admin to reserve space unless
668 * unwritten extents are enabled.
669 */
670 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
671 !capable(CAP_SYS_ADMIN))
672 return -XFS_ERROR(EPERM);
673
f37ea149 674 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
1da177e4
LT
675 return -XFS_ERROR(EPERM);
676
ad4a8ac4 677 if (!(filp->f_mode & FMODE_WRITE))
1da177e4
LT
678 return -XFS_ERROR(EBADF);
679
f37ea149 680 if (!S_ISREG(inode->i_mode))
1da177e4
LT
681 return -XFS_ERROR(EINVAL);
682
1da177e4 683 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
0f285c8a 684 attr_flags |= XFS_ATTR_NONBLOCK;
1da177e4 685 if (ioflags & IO_INVIS)
0f285c8a 686 attr_flags |= XFS_ATTR_DMI;
1da177e4 687
743bb465 688 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
1da177e4
LT
689 return -error;
690}
691
692STATIC int
693xfs_ioc_bulkstat(
694 xfs_mount_t *mp,
695 unsigned int cmd,
696 void __user *arg)
697{
698 xfs_fsop_bulkreq_t bulkreq;
699 int count; /* # of records returned */
700 xfs_ino_t inlast; /* last inode number */
701 int done;
702 int error;
703
704 /* done = 1 if there are more stats to get and if bulkstat */
705 /* should be called again (unused here, but used in dmapi) */
706
707 if (!capable(CAP_SYS_ADMIN))
708 return -EPERM;
709
710 if (XFS_FORCED_SHUTDOWN(mp))
711 return -XFS_ERROR(EIO);
712
713 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
714 return -XFS_ERROR(EFAULT);
715
716 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
717 return -XFS_ERROR(EFAULT);
718
719 if ((count = bulkreq.icount) <= 0)
720 return -XFS_ERROR(EINVAL);
721
cd57e594
LM
722 if (bulkreq.ubuffer == NULL)
723 return -XFS_ERROR(EINVAL);
724
1da177e4
LT
725 if (cmd == XFS_IOC_FSINUMBERS)
726 error = xfs_inumbers(mp, &inlast, &count,
faa63e95 727 bulkreq.ubuffer, xfs_inumbers_fmt);
1da177e4
LT
728 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
729 error = xfs_bulkstat_single(mp, &inlast,
730 bulkreq.ubuffer, &done);
cd57e594
LM
731 else /* XFS_IOC_FSBULKSTAT */
732 error = xfs_bulkstat(mp, &inlast, &count,
733 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
734 sizeof(xfs_bstat_t), bulkreq.ubuffer,
735 BULKSTAT_FG_QUICK, &done);
1da177e4
LT
736
737 if (error)
738 return -error;
739
740 if (bulkreq.ocount != NULL) {
741 if (copy_to_user(bulkreq.lastip, &inlast,
742 sizeof(xfs_ino_t)))
743 return -XFS_ERROR(EFAULT);
744
745 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
746 return -XFS_ERROR(EFAULT);
747 }
748
749 return 0;
750}
751
752STATIC int
753xfs_ioc_fsgeometry_v1(
754 xfs_mount_t *mp,
755 void __user *arg)
756{
757 xfs_fsop_geom_v1_t fsgeo;
758 int error;
759
760 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
761 if (error)
762 return -error;
763
764 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
765 return -XFS_ERROR(EFAULT);
766 return 0;
767}
768
769STATIC int
770xfs_ioc_fsgeometry(
771 xfs_mount_t *mp,
772 void __user *arg)
773{
774 xfs_fsop_geom_t fsgeo;
775 int error;
776
777 error = xfs_fs_geometry(mp, &fsgeo, 4);
778 if (error)
779 return -error;
780
781 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
782 return -XFS_ERROR(EFAULT);
783 return 0;
784}
785
786/*
787 * Linux extended inode flags interface.
788 */
1da177e4
LT
789
790STATIC unsigned int
791xfs_merge_ioc_xflags(
792 unsigned int flags,
793 unsigned int start)
794{
795 unsigned int xflags = start;
796
39058a0e 797 if (flags & FS_IMMUTABLE_FL)
1da177e4
LT
798 xflags |= XFS_XFLAG_IMMUTABLE;
799 else
800 xflags &= ~XFS_XFLAG_IMMUTABLE;
39058a0e 801 if (flags & FS_APPEND_FL)
1da177e4
LT
802 xflags |= XFS_XFLAG_APPEND;
803 else
804 xflags &= ~XFS_XFLAG_APPEND;
39058a0e 805 if (flags & FS_SYNC_FL)
1da177e4
LT
806 xflags |= XFS_XFLAG_SYNC;
807 else
808 xflags &= ~XFS_XFLAG_SYNC;
39058a0e 809 if (flags & FS_NOATIME_FL)
1da177e4
LT
810 xflags |= XFS_XFLAG_NOATIME;
811 else
812 xflags &= ~XFS_XFLAG_NOATIME;
39058a0e 813 if (flags & FS_NODUMP_FL)
1da177e4
LT
814 xflags |= XFS_XFLAG_NODUMP;
815 else
816 xflags &= ~XFS_XFLAG_NODUMP;
817
818 return xflags;
819}
820
821STATIC unsigned int
822xfs_di2lxflags(
823 __uint16_t di_flags)
824{
825 unsigned int flags = 0;
826
827 if (di_flags & XFS_DIFLAG_IMMUTABLE)
39058a0e 828 flags |= FS_IMMUTABLE_FL;
1da177e4 829 if (di_flags & XFS_DIFLAG_APPEND)
39058a0e 830 flags |= FS_APPEND_FL;
1da177e4 831 if (di_flags & XFS_DIFLAG_SYNC)
39058a0e 832 flags |= FS_SYNC_FL;
1da177e4 833 if (di_flags & XFS_DIFLAG_NOATIME)
39058a0e 834 flags |= FS_NOATIME_FL;
1da177e4 835 if (di_flags & XFS_DIFLAG_NODUMP)
39058a0e 836 flags |= FS_NODUMP_FL;
1da177e4
LT
837 return flags;
838}
839
c83bfab1
CH
840STATIC int
841xfs_ioc_fsgetxattr(
842 xfs_inode_t *ip,
843 int attr,
844 void __user *arg)
845{
846 struct fsxattr fa;
847
848 xfs_ilock(ip, XFS_ILOCK_SHARED);
849 fa.fsx_xflags = xfs_ip2xflags(ip);
850 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
851 fa.fsx_projid = ip->i_d.di_projid;
852
853 if (attr) {
854 if (ip->i_afp) {
855 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
856 fa.fsx_nextents = ip->i_afp->if_bytes /
857 sizeof(xfs_bmbt_rec_t);
858 else
859 fa.fsx_nextents = ip->i_d.di_anextents;
860 } else
861 fa.fsx_nextents = 0;
862 } else {
863 if (ip->i_df.if_flags & XFS_IFEXTENTS)
864 fa.fsx_nextents = ip->i_df.if_bytes /
865 sizeof(xfs_bmbt_rec_t);
866 else
867 fa.fsx_nextents = ip->i_d.di_nextents;
868 }
869 xfs_iunlock(ip, XFS_ILOCK_SHARED);
870
871 if (copy_to_user(arg, &fa, sizeof(fa)))
872 return -EFAULT;
873 return 0;
874}
875
25fe55e8
CH
876STATIC void
877xfs_set_diflags(
878 struct xfs_inode *ip,
879 unsigned int xflags)
880{
881 unsigned int di_flags;
882
883 /* can't set PREALLOC this way, just preserve it */
884 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
885 if (xflags & XFS_XFLAG_IMMUTABLE)
886 di_flags |= XFS_DIFLAG_IMMUTABLE;
887 if (xflags & XFS_XFLAG_APPEND)
888 di_flags |= XFS_DIFLAG_APPEND;
889 if (xflags & XFS_XFLAG_SYNC)
890 di_flags |= XFS_DIFLAG_SYNC;
891 if (xflags & XFS_XFLAG_NOATIME)
892 di_flags |= XFS_DIFLAG_NOATIME;
893 if (xflags & XFS_XFLAG_NODUMP)
894 di_flags |= XFS_DIFLAG_NODUMP;
895 if (xflags & XFS_XFLAG_PROJINHERIT)
896 di_flags |= XFS_DIFLAG_PROJINHERIT;
897 if (xflags & XFS_XFLAG_NODEFRAG)
898 di_flags |= XFS_DIFLAG_NODEFRAG;
899 if (xflags & XFS_XFLAG_FILESTREAM)
900 di_flags |= XFS_DIFLAG_FILESTREAM;
901 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
902 if (xflags & XFS_XFLAG_RTINHERIT)
903 di_flags |= XFS_DIFLAG_RTINHERIT;
904 if (xflags & XFS_XFLAG_NOSYMLINKS)
905 di_flags |= XFS_DIFLAG_NOSYMLINKS;
906 if (xflags & XFS_XFLAG_EXTSZINHERIT)
907 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
908 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
909 if (xflags & XFS_XFLAG_REALTIME)
910 di_flags |= XFS_DIFLAG_REALTIME;
911 if (xflags & XFS_XFLAG_EXTSIZE)
912 di_flags |= XFS_DIFLAG_EXTSIZE;
913 }
914
915 ip->i_d.di_flags = di_flags;
916}
917
f13fae2d
CH
918STATIC void
919xfs_diflags_to_linux(
920 struct xfs_inode *ip)
921{
e4f75291 922 struct inode *inode = VFS_I(ip);
f13fae2d
CH
923 unsigned int xflags = xfs_ip2xflags(ip);
924
925 if (xflags & XFS_XFLAG_IMMUTABLE)
926 inode->i_flags |= S_IMMUTABLE;
927 else
928 inode->i_flags &= ~S_IMMUTABLE;
929 if (xflags & XFS_XFLAG_APPEND)
930 inode->i_flags |= S_APPEND;
931 else
932 inode->i_flags &= ~S_APPEND;
933 if (xflags & XFS_XFLAG_SYNC)
934 inode->i_flags |= S_SYNC;
935 else
936 inode->i_flags &= ~S_SYNC;
937 if (xflags & XFS_XFLAG_NOATIME)
938 inode->i_flags |= S_NOATIME;
939 else
940 inode->i_flags &= ~S_NOATIME;
941}
25fe55e8
CH
942
943#define FSX_PROJID 1
944#define FSX_EXTSIZE 2
945#define FSX_XFLAGS 4
946#define FSX_NONBLOCK 8
947
948STATIC int
949xfs_ioctl_setattr(
950 xfs_inode_t *ip,
951 struct fsxattr *fa,
952 int mask)
953{
954 struct xfs_mount *mp = ip->i_mount;
955 struct xfs_trans *tp;
956 unsigned int lock_flags = 0;
957 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
958 struct xfs_dquot *olddquot = NULL;
959 int code;
960
961 xfs_itrace_entry(ip);
962
963 if (mp->m_flags & XFS_MOUNT_RDONLY)
964 return XFS_ERROR(EROFS);
965 if (XFS_FORCED_SHUTDOWN(mp))
966 return XFS_ERROR(EIO);
967
968 /*
969 * If disk quotas is on, we make sure that the dquots do exist on disk,
970 * before we start any other transactions. Trying to do this later
971 * is messy. We don't care to take a readlock to look at the ids
972 * in inode here, because we can't hold it across the trans_reserve.
973 * If the IDs do change before we take the ilock, we're covered
974 * because the i_*dquot fields will get updated anyway.
975 */
976 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
977 code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid,
978 ip->i_d.di_gid, fa->fsx_projid,
979 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
980 if (code)
981 return code;
982 }
983
984 /*
985 * For the other attributes, we acquire the inode lock and
986 * first do an error checking pass.
987 */
988 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
989 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
990 if (code)
991 goto error_return;
992
993 lock_flags = XFS_ILOCK_EXCL;
994 xfs_ilock(ip, lock_flags);
995
996 /*
997 * CAP_FOWNER overrides the following restrictions:
998 *
999 * The user ID of the calling process must be equal
1000 * to the file owner ID, except in cases where the
1001 * CAP_FSETID capability is applicable.
1002 */
91b77712 1003 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
25fe55e8
CH
1004 code = XFS_ERROR(EPERM);
1005 goto error_return;
1006 }
1007
1008 /*
1009 * Do a quota reservation only if projid is actually going to change.
1010 */
1011 if (mask & FSX_PROJID) {
1012 if (XFS_IS_PQUOTA_ON(mp) &&
1013 ip->i_d.di_projid != fa->fsx_projid) {
1014 ASSERT(tp);
1015 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
1016 capable(CAP_FOWNER) ?
1017 XFS_QMOPT_FORCE_RES : 0);
1018 if (code) /* out of quota */
1019 goto error_return;
1020 }
1021 }
1022
1023 if (mask & FSX_EXTSIZE) {
1024 /*
1025 * Can't change extent size if any extents are allocated.
1026 */
1027 if (ip->i_d.di_nextents &&
1028 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1029 fa->fsx_extsize)) {
1030 code = XFS_ERROR(EINVAL); /* EFBIG? */
1031 goto error_return;
1032 }
1033
1034 /*
1035 * Extent size must be a multiple of the appropriate block
1036 * size, if set at all.
1037 */
1038 if (fa->fsx_extsize != 0) {
1039 xfs_extlen_t size;
1040
1041 if (XFS_IS_REALTIME_INODE(ip) ||
1042 ((mask & FSX_XFLAGS) &&
1043 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1044 size = mp->m_sb.sb_rextsize <<
1045 mp->m_sb.sb_blocklog;
1046 } else {
1047 size = mp->m_sb.sb_blocksize;
1048 }
1049
1050 if (fa->fsx_extsize % size) {
1051 code = XFS_ERROR(EINVAL);
1052 goto error_return;
1053 }
1054 }
1055 }
1056
1057
1058 if (mask & FSX_XFLAGS) {
1059 /*
1060 * Can't change realtime flag if any extents are allocated.
1061 */
1062 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1063 (XFS_IS_REALTIME_INODE(ip)) !=
1064 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1065 code = XFS_ERROR(EINVAL); /* EFBIG? */
1066 goto error_return;
1067 }
1068
1069 /*
1070 * If realtime flag is set then must have realtime data.
1071 */
1072 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1073 if ((mp->m_sb.sb_rblocks == 0) ||
1074 (mp->m_sb.sb_rextsize == 0) ||
1075 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1076 code = XFS_ERROR(EINVAL);
1077 goto error_return;
1078 }
1079 }
1080
1081 /*
1082 * Can't modify an immutable/append-only file unless
1083 * we have appropriate permission.
1084 */
1085 if ((ip->i_d.di_flags &
1086 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1087 (fa->fsx_xflags &
1088 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1089 !capable(CAP_LINUX_IMMUTABLE)) {
1090 code = XFS_ERROR(EPERM);
1091 goto error_return;
1092 }
1093 }
1094
1095 xfs_trans_ijoin(tp, ip, lock_flags);
1096 xfs_trans_ihold(tp, ip);
1097
1098 /*
1099 * Change file ownership. Must be the owner or privileged.
25fe55e8
CH
1100 */
1101 if (mask & FSX_PROJID) {
1102 /*
1103 * CAP_FSETID overrides the following restrictions:
1104 *
1105 * The set-user-ID and set-group-ID bits of a file will be
1106 * cleared upon successful return from chown()
1107 */
1108 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1109 !capable(CAP_FSETID))
1110 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1111
1112 /*
1113 * Change the ownerships and register quota modifications
1114 * in the transaction.
1115 */
1116 if (ip->i_d.di_projid != fa->fsx_projid) {
1117 if (XFS_IS_PQUOTA_ON(mp)) {
1118 olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip,
1119 &ip->i_gdquot, gdqp);
1120 }
1121 ip->i_d.di_projid = fa->fsx_projid;
1122
1123 /*
1124 * We may have to rev the inode as well as
1125 * the superblock version number since projids didn't
1126 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1127 */
51ce16d5 1128 if (ip->i_d.di_version == 1)
25fe55e8
CH
1129 xfs_bump_ino_vers2(tp, ip);
1130 }
1131
1132 }
1133
1134 if (mask & FSX_EXTSIZE)
1135 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
f13fae2d 1136 if (mask & FSX_XFLAGS) {
25fe55e8 1137 xfs_set_diflags(ip, fa->fsx_xflags);
f13fae2d
CH
1138 xfs_diflags_to_linux(ip);
1139 }
25fe55e8
CH
1140
1141 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1142 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1143
1144 XFS_STATS_INC(xs_ig_attrchg);
1145
1146 /*
1147 * If this is a synchronous mount, make sure that the
1148 * transaction goes to disk before returning to the user.
1149 * This is slightly sub-optimal in that truncates require
1150 * two sync transactions instead of one for wsync filesystems.
1151 * One for the truncate and one for the timestamps since we
1152 * don't want to change the timestamps unless we're sure the
1153 * truncate worked. Truncates are less than 1% of the laddis
1154 * mix so this probably isn't worth the trouble to optimize.
1155 */
1156 if (mp->m_flags & XFS_MOUNT_WSYNC)
1157 xfs_trans_set_sync(tp);
1158 code = xfs_trans_commit(tp, 0);
1159 xfs_iunlock(ip, lock_flags);
1160
1161 /*
1162 * Release any dquot(s) the inode had kept before chown.
1163 */
1164 XFS_QM_DQRELE(mp, olddquot);
1165 XFS_QM_DQRELE(mp, udqp);
1166 XFS_QM_DQRELE(mp, gdqp);
1167
1168 if (code)
1169 return code;
1170
1171 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1172 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1173 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1174 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1175 }
1176
25fe55e8
CH
1177 return 0;
1178
1179 error_return:
1180 XFS_QM_DQRELE(mp, udqp);
1181 XFS_QM_DQRELE(mp, gdqp);
1182 xfs_trans_cancel(tp, 0);
1183 if (lock_flags)
1184 xfs_iunlock(ip, lock_flags);
1185 return code;
1186}
1187
1da177e4 1188STATIC int
df26cfe8 1189xfs_ioc_fssetxattr(
1da177e4
LT
1190 xfs_inode_t *ip,
1191 struct file *filp,
1da177e4
LT
1192 void __user *arg)
1193{
1194 struct fsxattr fa;
25fe55e8 1195 unsigned int mask;
df26cfe8
LM
1196
1197 if (copy_from_user(&fa, arg, sizeof(fa)))
1198 return -EFAULT;
1da177e4 1199
25fe55e8 1200 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
df26cfe8 1201 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
25fe55e8 1202 mask |= FSX_NONBLOCK;
1da177e4 1203
25fe55e8 1204 return -xfs_ioctl_setattr(ip, &fa, mask);
df26cfe8 1205}
1da177e4 1206
df26cfe8
LM
1207STATIC int
1208xfs_ioc_getxflags(
1209 xfs_inode_t *ip,
1210 void __user *arg)
1211{
1212 unsigned int flags;
1da177e4 1213
df26cfe8
LM
1214 flags = xfs_di2lxflags(ip->i_d.di_flags);
1215 if (copy_to_user(arg, &flags, sizeof(flags)))
1216 return -EFAULT;
1217 return 0;
1218}
1da177e4 1219
df26cfe8
LM
1220STATIC int
1221xfs_ioc_setxflags(
1222 xfs_inode_t *ip,
1223 struct file *filp,
1224 void __user *arg)
1225{
25fe55e8 1226 struct fsxattr fa;
df26cfe8 1227 unsigned int flags;
25fe55e8 1228 unsigned int mask;
1da177e4 1229
df26cfe8
LM
1230 if (copy_from_user(&flags, arg, sizeof(flags)))
1231 return -EFAULT;
1da177e4 1232
df26cfe8
LM
1233 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1234 FS_NOATIME_FL | FS_NODUMP_FL | \
1235 FS_SYNC_FL))
1236 return -EOPNOTSUPP;
1da177e4 1237
25fe55e8 1238 mask = FSX_XFLAGS;
df26cfe8 1239 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
25fe55e8
CH
1240 mask |= FSX_NONBLOCK;
1241 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
1da177e4 1242
25fe55e8 1243 return -xfs_ioctl_setattr(ip, &fa, mask);
1da177e4
LT
1244}
1245
8a7141a8
ES
1246STATIC int
1247xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1248{
1249 struct getbmap __user *base = *ap;
1250
1251 /* copy only getbmap portion (not getbmapx) */
1252 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1253 return XFS_ERROR(EFAULT);
1254
1255 *ap += sizeof(struct getbmap);
1256 return 0;
1257}
1258
1da177e4
LT
1259STATIC int
1260xfs_ioc_getbmap(
993386c1 1261 struct xfs_inode *ip,
1da177e4
LT
1262 int ioflags,
1263 unsigned int cmd,
1264 void __user *arg)
1265{
8a7141a8 1266 struct getbmapx bmx;
1da177e4
LT
1267 int error;
1268
8a7141a8 1269 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
1da177e4
LT
1270 return -XFS_ERROR(EFAULT);
1271
8a7141a8 1272 if (bmx.bmv_count < 2)
1da177e4
LT
1273 return -XFS_ERROR(EINVAL);
1274
8a7141a8 1275 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
1da177e4 1276 if (ioflags & IO_INVIS)
8a7141a8 1277 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
1da177e4 1278
8a7141a8
ES
1279 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1280 (struct getbmap *)arg+1);
1da177e4
LT
1281 if (error)
1282 return -error;
1283
8a7141a8
ES
1284 /* copy back header - only size of getbmap */
1285 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
1da177e4
LT
1286 return -XFS_ERROR(EFAULT);
1287 return 0;
1288}
1289
8a7141a8
ES
1290STATIC int
1291xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1292{
1293 struct getbmapx __user *base = *ap;
1294
1295 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1296 return XFS_ERROR(EFAULT);
1297
1298 *ap += sizeof(struct getbmapx);
1299 return 0;
1300}
1301
1da177e4
LT
1302STATIC int
1303xfs_ioc_getbmapx(
993386c1 1304 struct xfs_inode *ip,
1da177e4
LT
1305 void __user *arg)
1306{
1307 struct getbmapx bmx;
1da177e4
LT
1308 int error;
1309
1310 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1311 return -XFS_ERROR(EFAULT);
1312
1313 if (bmx.bmv_count < 2)
1314 return -XFS_ERROR(EINVAL);
1315
8a7141a8 1316 if (bmx.bmv_iflags & (~BMV_IF_VALID))
1da177e4
LT
1317 return -XFS_ERROR(EINVAL);
1318
8a7141a8
ES
1319 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1320 (struct getbmapx *)arg+1);
1da177e4
LT
1321 if (error)
1322 return -error;
1323
8a7141a8
ES
1324 /* copy back header */
1325 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
1da177e4
LT
1326 return -XFS_ERROR(EFAULT);
1327
1328 return 0;
1329}
df26cfe8
LM
1330
1331int
1332xfs_ioctl(
1333 xfs_inode_t *ip,
1334 struct file *filp,
1335 int ioflags,
1336 unsigned int cmd,
1337 void __user *arg)
1338{
1339 struct inode *inode = filp->f_path.dentry->d_inode;
1340 xfs_mount_t *mp = ip->i_mount;
1341 int error;
1342
1343 xfs_itrace_entry(XFS_I(inode));
1344 switch (cmd) {
1345
1346 case XFS_IOC_ALLOCSP:
1347 case XFS_IOC_FREESP:
1348 case XFS_IOC_RESVSP:
1349 case XFS_IOC_UNRESVSP:
1350 case XFS_IOC_ALLOCSP64:
1351 case XFS_IOC_FREESP64:
1352 case XFS_IOC_RESVSP64:
743bb465 1353 case XFS_IOC_UNRESVSP64: {
1354 xfs_flock64_t bf;
df26cfe8 1355
743bb465 1356 if (copy_from_user(&bf, arg, sizeof(bf)))
1357 return -XFS_ERROR(EFAULT);
1358 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1359 }
df26cfe8
LM
1360 case XFS_IOC_DIOINFO: {
1361 struct dioattr da;
1362 xfs_buftarg_t *target =
1363 XFS_IS_REALTIME_INODE(ip) ?
1364 mp->m_rtdev_targp : mp->m_ddev_targp;
1365
1366 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1367 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1368
1369 if (copy_to_user(arg, &da, sizeof(da)))
1370 return -XFS_ERROR(EFAULT);
1371 return 0;
1372 }
1373
1374 case XFS_IOC_FSBULKSTAT_SINGLE:
1375 case XFS_IOC_FSBULKSTAT:
1376 case XFS_IOC_FSINUMBERS:
1377 return xfs_ioc_bulkstat(mp, cmd, arg);
1378
1379 case XFS_IOC_FSGEOMETRY_V1:
1380 return xfs_ioc_fsgeometry_v1(mp, arg);
1381
1382 case XFS_IOC_FSGEOMETRY:
1383 return xfs_ioc_fsgeometry(mp, arg);
1384
1385 case XFS_IOC_GETVERSION:
1386 return put_user(inode->i_generation, (int __user *)arg);
1387
1388 case XFS_IOC_FSGETXATTR:
1389 return xfs_ioc_fsgetxattr(ip, 0, arg);
1390 case XFS_IOC_FSGETXATTRA:
1391 return xfs_ioc_fsgetxattr(ip, 1, arg);
65e67f51
LM
1392 case XFS_IOC_FSSETXATTR:
1393 return xfs_ioc_fssetxattr(ip, filp, arg);
df26cfe8 1394 case XFS_IOC_GETXFLAGS:
65e67f51 1395 return xfs_ioc_getxflags(ip, arg);
df26cfe8 1396 case XFS_IOC_SETXFLAGS:
65e67f51 1397 return xfs_ioc_setxflags(ip, filp, arg);
df26cfe8
LM
1398
1399 case XFS_IOC_FSSETDM: {
1400 struct fsdmidata dmi;
1401
1402 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1403 return -XFS_ERROR(EFAULT);
1404
1405 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1406 dmi.fsd_dmstate);
1407 return -error;
1408 }
1409
1410 case XFS_IOC_GETBMAP:
1411 case XFS_IOC_GETBMAPA:
1412 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1413
1414 case XFS_IOC_GETBMAPX:
1415 return xfs_ioc_getbmapx(ip, arg);
1416
1417 case XFS_IOC_FD_TO_HANDLE:
1418 case XFS_IOC_PATH_TO_HANDLE:
743bb465 1419 case XFS_IOC_PATH_TO_FSHANDLE: {
1420 xfs_fsop_handlereq_t hreq;
df26cfe8 1421
743bb465 1422 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1423 return -XFS_ERROR(EFAULT);
1424 return xfs_find_handle(cmd, &hreq);
1425 }
1426 case XFS_IOC_OPEN_BY_HANDLE: {
1427 xfs_fsop_handlereq_t hreq;
df26cfe8 1428
743bb465 1429 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1430 return -XFS_ERROR(EFAULT);
1431 return xfs_open_by_handle(mp, &hreq, filp, inode);
1432 }
df26cfe8
LM
1433 case XFS_IOC_FSSETDM_BY_HANDLE:
1434 return xfs_fssetdm_by_handle(mp, arg, inode);
1435
743bb465 1436 case XFS_IOC_READLINK_BY_HANDLE: {
1437 xfs_fsop_handlereq_t hreq;
df26cfe8 1438
743bb465 1439 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1440 return -XFS_ERROR(EFAULT);
1441 return xfs_readlink_by_handle(mp, &hreq, inode);
1442 }
df26cfe8
LM
1443 case XFS_IOC_ATTRLIST_BY_HANDLE:
1444 return xfs_attrlist_by_handle(mp, arg, inode);
1445
1446 case XFS_IOC_ATTRMULTI_BY_HANDLE:
42a74f20 1447 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
df26cfe8
LM
1448
1449 case XFS_IOC_SWAPEXT: {
743bb465 1450 struct xfs_swapext sxp;
1451
1452 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1453 return -XFS_ERROR(EFAULT);
1454 error = xfs_swapext(&sxp);
df26cfe8
LM
1455 return -error;
1456 }
1457
1458 case XFS_IOC_FSCOUNTS: {
1459 xfs_fsop_counts_t out;
1460
1461 error = xfs_fs_counts(mp, &out);
1462 if (error)
1463 return -error;
1464
1465 if (copy_to_user(arg, &out, sizeof(out)))
1466 return -XFS_ERROR(EFAULT);
1467 return 0;
1468 }
1469
1470 case XFS_IOC_SET_RESBLKS: {
1471 xfs_fsop_resblks_t inout;
1472 __uint64_t in;
1473
1474 if (!capable(CAP_SYS_ADMIN))
1475 return -EPERM;
1476
1477 if (copy_from_user(&inout, arg, sizeof(inout)))
1478 return -XFS_ERROR(EFAULT);
1479
1480 /* input parameter is passed in resblks field of structure */
1481 in = inout.resblks;
1482 error = xfs_reserve_blocks(mp, &in, &inout);
1483 if (error)
1484 return -error;
1485
1486 if (copy_to_user(arg, &inout, sizeof(inout)))
1487 return -XFS_ERROR(EFAULT);
1488 return 0;
1489 }
1490
1491 case XFS_IOC_GET_RESBLKS: {
1492 xfs_fsop_resblks_t out;
1493
1494 if (!capable(CAP_SYS_ADMIN))
1495 return -EPERM;
1496
1497 error = xfs_reserve_blocks(mp, NULL, &out);
1498 if (error)
1499 return -error;
1500
1501 if (copy_to_user(arg, &out, sizeof(out)))
1502 return -XFS_ERROR(EFAULT);
1503
1504 return 0;
1505 }
1506
1507 case XFS_IOC_FSGROWFSDATA: {
1508 xfs_growfs_data_t in;
1509
df26cfe8
LM
1510 if (copy_from_user(&in, arg, sizeof(in)))
1511 return -XFS_ERROR(EFAULT);
1512
1513 error = xfs_growfs_data(mp, &in);
1514 return -error;
1515 }
1516
1517 case XFS_IOC_FSGROWFSLOG: {
1518 xfs_growfs_log_t in;
1519
df26cfe8
LM
1520 if (copy_from_user(&in, arg, sizeof(in)))
1521 return -XFS_ERROR(EFAULT);
1522
1523 error = xfs_growfs_log(mp, &in);
1524 return -error;
1525 }
1526
1527 case XFS_IOC_FSGROWFSRT: {
1528 xfs_growfs_rt_t in;
1529
df26cfe8
LM
1530 if (copy_from_user(&in, arg, sizeof(in)))
1531 return -XFS_ERROR(EFAULT);
1532
1533 error = xfs_growfs_rt(mp, &in);
1534 return -error;
1535 }
1536
1537 case XFS_IOC_FREEZE:
1538 if (!capable(CAP_SYS_ADMIN))
1539 return -EPERM;
1540
1541 if (inode->i_sb->s_frozen == SB_UNFROZEN)
1542 freeze_bdev(inode->i_sb->s_bdev);
1543 return 0;
1544
1545 case XFS_IOC_THAW:
1546 if (!capable(CAP_SYS_ADMIN))
1547 return -EPERM;
1548 if (inode->i_sb->s_frozen != SB_UNFROZEN)
1549 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
1550 return 0;
1551
1552 case XFS_IOC_GOINGDOWN: {
1553 __uint32_t in;
1554
1555 if (!capable(CAP_SYS_ADMIN))
1556 return -EPERM;
1557
1558 if (get_user(in, (__uint32_t __user *)arg))
1559 return -XFS_ERROR(EFAULT);
1560
1561 error = xfs_fs_goingdown(mp, in);
1562 return -error;
1563 }
1564
1565 case XFS_IOC_ERROR_INJECTION: {
1566 xfs_error_injection_t in;
1567
1568 if (!capable(CAP_SYS_ADMIN))
1569 return -EPERM;
1570
1571 if (copy_from_user(&in, arg, sizeof(in)))
1572 return -XFS_ERROR(EFAULT);
1573
1574 error = xfs_errortag_add(in.errtag, mp);
1575 return -error;
1576 }
1577
1578 case XFS_IOC_ERROR_CLEARALL:
1579 if (!capable(CAP_SYS_ADMIN))
1580 return -EPERM;
1581
1582 error = xfs_errortag_clearall(mp, 1);
1583 return -error;
1584
1585 default:
1586 return -ENOTTY;
1587 }
1588}