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