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