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