blob: 6f04493b8aba1a1aabcf06751e18192d234b62cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_rw.h"
43#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110045#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_buf_item.h"
47#include "xfs_utils.h"
48#include "xfs_dfrag.h"
49#include "xfs_fsops.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100050#include "xfs_vnodeops.h"
Christoph Hellwig25fe55e2008-07-18 17:13:20 +100051#include "xfs_quota.h"
52#include "xfs_inode_item.h"
Christoph Hellwigd296d302009-01-19 02:02:57 +010053#include "xfs_export.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080055#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/dcache.h>
57#include <linux/mount.h>
58#include <linux/namei.h>
59#include <linux/pagemap.h>
Christoph Hellwigd296d302009-01-19 02:02:57 +010060#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
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 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060073int
Linus Torvalds1da177e2005-04-16 15:20:36 -070074xfs_find_handle(
75 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060076 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 int hsize;
79 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct inode *inode;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010081 struct file *file = NULL;
82 struct path path;
83 int error;
84 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010086 if (cmd == XFS_IOC_FD_TO_HANDLE) {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060087 file = fget(hreq->fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (!file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010089 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010097 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010099 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100107 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100114 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100116 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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int lock_mode;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 lock_mode = xfs_ilock_map_shared(ip);
Christoph Hellwigc6143912007-09-14 15:22:37 +1000126 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 xfs_iunlock_map_shared(ip, lock_mode);
132
133 hsize = XFS_HSIZE(handle);
134 }
135
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100136 error = -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600137 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100138 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
139 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100141 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100152 * No need to do permission checks on the various pathname components
153 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
155STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100156xfs_handle_acceptable(
157 void *context,
158 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100160 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)
171{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100173 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /*
176 * Only allow handle opens under a directory.
177 */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100178 if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
179 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Christoph Hellwigd296d302009-01-19 02:02:57 +0100181 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Christoph Hellwigd296d302009-01-19 02:02:57 +0100189 memset(&fid, 0, sizeof(struct fid));
190 fid.ino = handle.ha_fid.fid_ino;
191 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Christoph Hellwigd296d302009-01-19 02:02:57 +0100193 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Christoph Hellwigd296d302009-01-19 02:02:57 +0100198STATIC 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600206int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100209 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
David Howells745ca242008-11-14 10:39:22 +1100211 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100213 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int permflag;
215 struct file *filp;
216 struct inode *inode;
217 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (!capable(CAP_SYS_ADMIN))
220 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Christoph Hellwigd296d302009-01-19 02:02:57 +0100222 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
223 if (IS_ERR(dentry))
224 return PTR_ERR(dentry);
225 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* Restrict xfs_open_by_handle to directories & regular files. */
228 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100229 error = -XFS_ERROR(EPERM);
230 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
233#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600234 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* Put open permission in namei format. */
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600238 permflag = hreq->oflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 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)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100246 error = -XFS_ERROR(EPERM);
247 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
250 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100251 error = -XFS_ERROR(EACCES);
252 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 /* Can't write directories. */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100256 if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
257 error = -XFS_ERROR(EISDIR);
258 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Christoph Hellwigd296d302009-01-19 02:02:57 +0100261 fd = get_unused_fd();
262 if (fd < 0) {
263 error = fd;
264 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
Christoph Hellwigd296d302009-01-19 02:02:57 +0100267 filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
268 hreq->oflags, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100270 put_unused_fd(fd);
271 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500273
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100274 if (inode->i_mode & S_IFREG) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100275 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500276 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Christoph Hellwigd296d302009-01-19 02:02:57 +0100279 fd_install(fd, filp);
280 return fd;
281
282 out_dput:
283 dput(dentry);
284 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000287/*
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
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600313int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100315 struct file *parfilp,
316 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100318 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000320 void *link;
321 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (!capable(CAP_SYS_ADMIN))
324 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Christoph Hellwigd296d302009-01-19 02:02:57 +0100326 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
327 if (IS_ERR(dentry))
328 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Restrict this handle operation to symlinks only. */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100331 if (!S_ISLNK(dentry->d_inode->i_mode)) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000332 error = -XFS_ERROR(EINVAL);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100333 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600336 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000337 error = -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100338 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000341 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100342 if (!link) {
343 error = -XFS_ERROR(ENOMEM);
344 goto out_dput;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Christoph Hellwigd296d302009-01-19 02:02:57 +0100347 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000348 if (error)
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000349 goto out_kfree;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600350 error = do_readlink(hreq->ohandle, olen, link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000351 if (error)
352 goto out_kfree;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000353
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000354 out_kfree:
355 kfree(link);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100356 out_dput:
357 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000358 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361STATIC int
362xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100363 struct file *parfilp,
364 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 int error;
367 struct fsdmidata fsd;
368 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100369 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
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
Christoph Hellwigd296d302009-01-19 02:02:57 +0100376 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
377 if (IS_ERR(dentry))
378 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Christoph Hellwigd296d302009-01-19 02:02:57 +0100380 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000381 error = -XFS_ERROR(EPERM);
382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
385 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000386 error = -XFS_ERROR(EFAULT);
387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Christoph Hellwigd296d302009-01-19 02:02:57 +0100390 error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000391 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000393 out:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100394 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398STATIC int
399xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100400 struct file *parfilp,
401 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100403 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 attrlist_cursor_kern_t *cursor;
405 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100406 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 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
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000416 /*
417 * Reject flags, only allow namespaces.
418 */
419 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
420 return -XFS_ERROR(EINVAL);
421
Christoph Hellwigd296d302009-01-19 02:02:57 +0100422 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
423 if (IS_ERR(dentry))
424 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
427 if (!kbuf)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100428 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100431 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000432 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 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);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100441 out_dput:
442 dput(dentry);
443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600446int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000448 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 char *name,
450 char __user *ubuf,
451 __uint32_t *len,
452 __uint32_t flags)
453{
454 char *kbuf;
455 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (*len > XATTR_SIZE_MAX)
458 return EINVAL;
459 kbuf = kmalloc(*len, GFP_KERNEL);
460 if (!kbuf)
461 return ENOMEM;
462
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000463 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 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
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600475int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000477 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 char *name,
479 const char __user *ubuf,
480 __uint32_t len,
481 __uint32_t flags)
482{
483 char *kbuf;
484 int error = EFAULT;
485
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000486 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 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;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000497
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000498 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 out_kfree:
501 kfree(kbuf);
502 return error;
503}
504
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600505int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000507 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 char *name,
509 __uint32_t flags)
510{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000511 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000513 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516STATIC int
517xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800518 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100519 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 int error;
522 xfs_attr_multiop_t *ops;
523 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100524 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 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
Christoph Hellwigd296d302009-01-19 02:02:57 +0100533 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
534 if (IS_ERR(dentry))
535 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000538 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100540 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 error = ENOMEM;
543 ops = kmalloc(size, GFP_KERNEL);
544 if (!ops)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100545 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
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:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100567 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572 case ATTR_OP_SET:
Dave Hansen42a74f22008-02-15 14:37:46 -0800573 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
574 if (ops[i].am_error)
575 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100576 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);
Dave Hansen42a74f22008-02-15 14:37:46 -0800580 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 case ATTR_OP_REMOVE:
Dave Hansen42a74f22008-02-15 14:37:46 -0800583 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
584 if (ops[i].am_error)
585 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100586 ops[i].am_error = xfs_attrmulti_attr_remove(
587 dentry->d_inode, attr_name,
588 ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800589 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 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);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100602 out_dput:
603 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -error;
605}
606
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600607int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000609 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000610 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct file *filp,
612 int ioflags,
613 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600614 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int attr_flags = 0;
617 int error;
618
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600619 /*
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
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000627 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -XFS_ERROR(EPERM);
629
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000630 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -XFS_ERROR(EBADF);
632
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000633 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -XFS_ERROR(EINVAL);
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000637 attr_flags |= XFS_ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (ioflags & IO_INVIS)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000639 attr_flags |= XFS_ATTR_DMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600641 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 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
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100675 if (bulkreq.ubuffer == NULL)
676 return -XFS_ERROR(EINVAL);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (cmd == XFS_IOC_FSINUMBERS)
679 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000680 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
682 error = xfs_bulkstat_single(mp, &inlast,
683 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100684 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743STATIC unsigned int
744xfs_merge_ioc_xflags(
745 unsigned int flags,
746 unsigned int start)
747{
748 unsigned int xflags = start;
749
Eric Sandeen39058a02007-02-10 18:37:10 +1100750 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 xflags |= XFS_XFLAG_IMMUTABLE;
752 else
753 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100754 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 xflags |= XFS_XFLAG_APPEND;
756 else
757 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100758 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 xflags |= XFS_XFLAG_SYNC;
760 else
761 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100762 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 xflags |= XFS_XFLAG_NOATIME;
764 else
765 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100766 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 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)
Eric Sandeen39058a02007-02-10 18:37:10 +1100781 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100783 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100785 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100787 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100789 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return flags;
791}
792
793STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000794xfs_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
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000829STATIC 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
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000871STATIC void
872xfs_diflags_to_linux(
873 struct xfs_inode *ip)
874{
David Chinnere4f75292008-08-13 16:00:45 +1000875 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000876 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}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000895
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;
910 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
911 struct xfs_dquot *olddquot = NULL;
912 int code;
913
914 xfs_itrace_entry(ip);
915
916 if (mp->m_flags & XFS_MOUNT_RDONLY)
917 return XFS_ERROR(EROFS);
918 if (XFS_FORCED_SHUTDOWN(mp))
919 return XFS_ERROR(EIO);
920
921 /*
922 * If disk quotas is on, we make sure that the dquots do exist on disk,
923 * before we start any other transactions. Trying to do this later
924 * is messy. We don't care to take a readlock to look at the ids
925 * in inode here, because we can't hold it across the trans_reserve.
926 * If the IDs do change before we take the ilock, we're covered
927 * because the i_*dquot fields will get updated anyway.
928 */
929 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
930 code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid,
931 ip->i_d.di_gid, fa->fsx_projid,
932 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
933 if (code)
934 return code;
935 }
936
937 /*
938 * For the other attributes, we acquire the inode lock and
939 * first do an error checking pass.
940 */
941 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
942 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
943 if (code)
944 goto error_return;
945
946 lock_flags = XFS_ILOCK_EXCL;
947 xfs_ilock(ip, lock_flags);
948
949 /*
950 * CAP_FOWNER overrides the following restrictions:
951 *
952 * The user ID of the calling process must be equal
953 * to the file owner ID, except in cases where the
954 * CAP_FSETID capability is applicable.
955 */
David Howells91b7771252008-10-31 15:50:04 +1100956 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000957 code = XFS_ERROR(EPERM);
958 goto error_return;
959 }
960
961 /*
962 * Do a quota reservation only if projid is actually going to change.
963 */
964 if (mask & FSX_PROJID) {
965 if (XFS_IS_PQUOTA_ON(mp) &&
966 ip->i_d.di_projid != fa->fsx_projid) {
967 ASSERT(tp);
968 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
969 capable(CAP_FOWNER) ?
970 XFS_QMOPT_FORCE_RES : 0);
971 if (code) /* out of quota */
972 goto error_return;
973 }
974 }
975
976 if (mask & FSX_EXTSIZE) {
977 /*
978 * Can't change extent size if any extents are allocated.
979 */
980 if (ip->i_d.di_nextents &&
981 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
982 fa->fsx_extsize)) {
983 code = XFS_ERROR(EINVAL); /* EFBIG? */
984 goto error_return;
985 }
986
987 /*
988 * Extent size must be a multiple of the appropriate block
989 * size, if set at all.
990 */
991 if (fa->fsx_extsize != 0) {
992 xfs_extlen_t size;
993
994 if (XFS_IS_REALTIME_INODE(ip) ||
995 ((mask & FSX_XFLAGS) &&
996 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
997 size = mp->m_sb.sb_rextsize <<
998 mp->m_sb.sb_blocklog;
999 } else {
1000 size = mp->m_sb.sb_blocksize;
1001 }
1002
1003 if (fa->fsx_extsize % size) {
1004 code = XFS_ERROR(EINVAL);
1005 goto error_return;
1006 }
1007 }
1008 }
1009
1010
1011 if (mask & FSX_XFLAGS) {
1012 /*
1013 * Can't change realtime flag if any extents are allocated.
1014 */
1015 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1016 (XFS_IS_REALTIME_INODE(ip)) !=
1017 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1018 code = XFS_ERROR(EINVAL); /* EFBIG? */
1019 goto error_return;
1020 }
1021
1022 /*
1023 * If realtime flag is set then must have realtime data.
1024 */
1025 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1026 if ((mp->m_sb.sb_rblocks == 0) ||
1027 (mp->m_sb.sb_rextsize == 0) ||
1028 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1029 code = XFS_ERROR(EINVAL);
1030 goto error_return;
1031 }
1032 }
1033
1034 /*
1035 * Can't modify an immutable/append-only file unless
1036 * we have appropriate permission.
1037 */
1038 if ((ip->i_d.di_flags &
1039 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1040 (fa->fsx_xflags &
1041 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1042 !capable(CAP_LINUX_IMMUTABLE)) {
1043 code = XFS_ERROR(EPERM);
1044 goto error_return;
1045 }
1046 }
1047
1048 xfs_trans_ijoin(tp, ip, lock_flags);
1049 xfs_trans_ihold(tp, ip);
1050
1051 /*
1052 * Change file ownership. Must be the owner or privileged.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001053 */
1054 if (mask & FSX_PROJID) {
1055 /*
1056 * CAP_FSETID overrides the following restrictions:
1057 *
1058 * The set-user-ID and set-group-ID bits of a file will be
1059 * cleared upon successful return from chown()
1060 */
1061 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1062 !capable(CAP_FSETID))
1063 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1064
1065 /*
1066 * Change the ownerships and register quota modifications
1067 * in the transaction.
1068 */
1069 if (ip->i_d.di_projid != fa->fsx_projid) {
1070 if (XFS_IS_PQUOTA_ON(mp)) {
1071 olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip,
1072 &ip->i_gdquot, gdqp);
1073 }
1074 ip->i_d.di_projid = fa->fsx_projid;
1075
1076 /*
1077 * We may have to rev the inode as well as
1078 * the superblock version number since projids didn't
1079 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1080 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001081 if (ip->i_d.di_version == 1)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001082 xfs_bump_ino_vers2(tp, ip);
1083 }
1084
1085 }
1086
1087 if (mask & FSX_EXTSIZE)
1088 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001089 if (mask & FSX_XFLAGS) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001090 xfs_set_diflags(ip, fa->fsx_xflags);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001091 xfs_diflags_to_linux(ip);
1092 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001093
1094 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1095 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1096
1097 XFS_STATS_INC(xs_ig_attrchg);
1098
1099 /*
1100 * If this is a synchronous mount, make sure that the
1101 * transaction goes to disk before returning to the user.
1102 * This is slightly sub-optimal in that truncates require
1103 * two sync transactions instead of one for wsync filesystems.
1104 * One for the truncate and one for the timestamps since we
1105 * don't want to change the timestamps unless we're sure the
1106 * truncate worked. Truncates are less than 1% of the laddis
1107 * mix so this probably isn't worth the trouble to optimize.
1108 */
1109 if (mp->m_flags & XFS_MOUNT_WSYNC)
1110 xfs_trans_set_sync(tp);
1111 code = xfs_trans_commit(tp, 0);
1112 xfs_iunlock(ip, lock_flags);
1113
1114 /*
1115 * Release any dquot(s) the inode had kept before chown.
1116 */
1117 XFS_QM_DQRELE(mp, olddquot);
1118 XFS_QM_DQRELE(mp, udqp);
1119 XFS_QM_DQRELE(mp, gdqp);
1120
1121 if (code)
1122 return code;
1123
1124 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1125 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1126 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1127 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1128 }
1129
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001130 return 0;
1131
1132 error_return:
1133 XFS_QM_DQRELE(mp, udqp);
1134 XFS_QM_DQRELE(mp, gdqp);
1135 xfs_trans_cancel(tp, 0);
1136 if (lock_flags)
1137 xfs_iunlock(ip, lock_flags);
1138 return code;
1139}
1140
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001141STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001142xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 xfs_inode_t *ip,
1144 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 void __user *arg)
1146{
1147 struct fsxattr fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001148 unsigned int mask;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001149
1150 if (copy_from_user(&fa, arg, sizeof(fa)))
1151 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001153 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001154 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001155 mask |= FSX_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001157 return -xfs_ioctl_setattr(ip, &fa, mask);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001158}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001160STATIC int
1161xfs_ioc_getxflags(
1162 xfs_inode_t *ip,
1163 void __user *arg)
1164{
1165 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001167 flags = xfs_di2lxflags(ip->i_d.di_flags);
1168 if (copy_to_user(arg, &flags, sizeof(flags)))
1169 return -EFAULT;
1170 return 0;
1171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001173STATIC int
1174xfs_ioc_setxflags(
1175 xfs_inode_t *ip,
1176 struct file *filp,
1177 void __user *arg)
1178{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001179 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001180 unsigned int flags;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001181 unsigned int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001183 if (copy_from_user(&flags, arg, sizeof(flags)))
1184 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001186 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1187 FS_NOATIME_FL | FS_NODUMP_FL | \
1188 FS_SYNC_FL))
1189 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001191 mask = FSX_XFLAGS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001192 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001193 mask |= FSX_NONBLOCK;
1194 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001196 return -xfs_ioctl_setattr(ip, &fa, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001200xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1201{
1202 struct getbmap __user *base = *ap;
1203
1204 /* copy only getbmap portion (not getbmapx) */
1205 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1206 return XFS_ERROR(EFAULT);
1207
1208 *ap += sizeof(struct getbmap);
1209 return 0;
1210}
1211
1212STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001214 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int ioflags,
1216 unsigned int cmd,
1217 void __user *arg)
1218{
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001219 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int error;
1221
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001222 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return -XFS_ERROR(EFAULT);
1224
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001225 if (bmx.bmv_count < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return -XFS_ERROR(EINVAL);
1227
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001228 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (ioflags & IO_INVIS)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001230 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001232 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1233 (struct getbmap *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (error)
1235 return -error;
1236
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001237 /* copy back header - only size of getbmap */
1238 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -XFS_ERROR(EFAULT);
1240 return 0;
1241}
1242
1243STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001244xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1245{
1246 struct getbmapx __user *base = *ap;
1247
1248 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1249 return XFS_ERROR(EFAULT);
1250
1251 *ap += sizeof(struct getbmapx);
1252 return 0;
1253}
1254
1255STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001257 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 void __user *arg)
1259{
1260 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int error;
1262
1263 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1264 return -XFS_ERROR(EFAULT);
1265
1266 if (bmx.bmv_count < 2)
1267 return -XFS_ERROR(EINVAL);
1268
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001269 if (bmx.bmv_iflags & (~BMV_IF_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return -XFS_ERROR(EINVAL);
1271
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001272 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1273 (struct getbmapx *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (error)
1275 return -error;
1276
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001277 /* copy back header */
1278 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return -XFS_ERROR(EFAULT);
1280
1281 return 0;
1282}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001283
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001284/*
1285 * Note: some of the ioctl's return positive numbers as a
1286 * byte count indicating success, such as readlink_by_handle.
1287 * So we don't "sign flip" like most other routines. This means
1288 * true errors need to be returned as a negative value.
1289 */
1290long
1291xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001292 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001293 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001294 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001295{
1296 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001297 struct xfs_inode *ip = XFS_I(inode);
1298 struct xfs_mount *mp = ip->i_mount;
1299 void __user *arg = (void __user *)p;
1300 int ioflags = 0;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001301 int error;
1302
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001303 if (filp->f_mode & FMODE_NOCMTIME)
1304 ioflags |= IO_INVIS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001305
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001306 xfs_itrace_entry(ip);
1307
1308 switch (cmd) {
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001309 case XFS_IOC_ALLOCSP:
1310 case XFS_IOC_FREESP:
1311 case XFS_IOC_RESVSP:
1312 case XFS_IOC_UNRESVSP:
1313 case XFS_IOC_ALLOCSP64:
1314 case XFS_IOC_FREESP64:
1315 case XFS_IOC_RESVSP64:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001316 case XFS_IOC_UNRESVSP64: {
1317 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001318
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001319 if (copy_from_user(&bf, arg, sizeof(bf)))
1320 return -XFS_ERROR(EFAULT);
1321 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1322 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001323 case XFS_IOC_DIOINFO: {
1324 struct dioattr da;
1325 xfs_buftarg_t *target =
1326 XFS_IS_REALTIME_INODE(ip) ?
1327 mp->m_rtdev_targp : mp->m_ddev_targp;
1328
1329 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1330 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1331
1332 if (copy_to_user(arg, &da, sizeof(da)))
1333 return -XFS_ERROR(EFAULT);
1334 return 0;
1335 }
1336
1337 case XFS_IOC_FSBULKSTAT_SINGLE:
1338 case XFS_IOC_FSBULKSTAT:
1339 case XFS_IOC_FSINUMBERS:
1340 return xfs_ioc_bulkstat(mp, cmd, arg);
1341
1342 case XFS_IOC_FSGEOMETRY_V1:
1343 return xfs_ioc_fsgeometry_v1(mp, arg);
1344
1345 case XFS_IOC_FSGEOMETRY:
1346 return xfs_ioc_fsgeometry(mp, arg);
1347
1348 case XFS_IOC_GETVERSION:
1349 return put_user(inode->i_generation, (int __user *)arg);
1350
1351 case XFS_IOC_FSGETXATTR:
1352 return xfs_ioc_fsgetxattr(ip, 0, arg);
1353 case XFS_IOC_FSGETXATTRA:
1354 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001355 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001356 return xfs_ioc_fssetxattr(ip, filp, arg);
1357 case XFS_IOC_GETXFLAGS:
1358 return xfs_ioc_getxflags(ip, arg);
1359 case XFS_IOC_SETXFLAGS:
1360 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001361
1362 case XFS_IOC_FSSETDM: {
1363 struct fsdmidata dmi;
1364
1365 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1366 return -XFS_ERROR(EFAULT);
1367
1368 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1369 dmi.fsd_dmstate);
1370 return -error;
1371 }
1372
1373 case XFS_IOC_GETBMAP:
1374 case XFS_IOC_GETBMAPA:
1375 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1376
1377 case XFS_IOC_GETBMAPX:
1378 return xfs_ioc_getbmapx(ip, arg);
1379
1380 case XFS_IOC_FD_TO_HANDLE:
1381 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001382 case XFS_IOC_PATH_TO_FSHANDLE: {
1383 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001384
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001385 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1386 return -XFS_ERROR(EFAULT);
1387 return xfs_find_handle(cmd, &hreq);
1388 }
1389 case XFS_IOC_OPEN_BY_HANDLE: {
1390 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001391
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001392 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1393 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001394 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001395 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001396 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001397 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001398
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001399 case XFS_IOC_READLINK_BY_HANDLE: {
1400 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001401
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001402 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1403 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001404 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001405 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001406 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001407 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001408
1409 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001410 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001411
1412 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001413 struct xfs_swapext sxp;
1414
1415 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1416 return -XFS_ERROR(EFAULT);
1417 error = xfs_swapext(&sxp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001418 return -error;
1419 }
1420
1421 case XFS_IOC_FSCOUNTS: {
1422 xfs_fsop_counts_t out;
1423
1424 error = xfs_fs_counts(mp, &out);
1425 if (error)
1426 return -error;
1427
1428 if (copy_to_user(arg, &out, sizeof(out)))
1429 return -XFS_ERROR(EFAULT);
1430 return 0;
1431 }
1432
1433 case XFS_IOC_SET_RESBLKS: {
1434 xfs_fsop_resblks_t inout;
1435 __uint64_t in;
1436
1437 if (!capable(CAP_SYS_ADMIN))
1438 return -EPERM;
1439
1440 if (copy_from_user(&inout, arg, sizeof(inout)))
1441 return -XFS_ERROR(EFAULT);
1442
1443 /* input parameter is passed in resblks field of structure */
1444 in = inout.resblks;
1445 error = xfs_reserve_blocks(mp, &in, &inout);
1446 if (error)
1447 return -error;
1448
1449 if (copy_to_user(arg, &inout, sizeof(inout)))
1450 return -XFS_ERROR(EFAULT);
1451 return 0;
1452 }
1453
1454 case XFS_IOC_GET_RESBLKS: {
1455 xfs_fsop_resblks_t out;
1456
1457 if (!capable(CAP_SYS_ADMIN))
1458 return -EPERM;
1459
1460 error = xfs_reserve_blocks(mp, NULL, &out);
1461 if (error)
1462 return -error;
1463
1464 if (copy_to_user(arg, &out, sizeof(out)))
1465 return -XFS_ERROR(EFAULT);
1466
1467 return 0;
1468 }
1469
1470 case XFS_IOC_FSGROWFSDATA: {
1471 xfs_growfs_data_t in;
1472
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001473 if (copy_from_user(&in, arg, sizeof(in)))
1474 return -XFS_ERROR(EFAULT);
1475
1476 error = xfs_growfs_data(mp, &in);
1477 return -error;
1478 }
1479
1480 case XFS_IOC_FSGROWFSLOG: {
1481 xfs_growfs_log_t in;
1482
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001483 if (copy_from_user(&in, arg, sizeof(in)))
1484 return -XFS_ERROR(EFAULT);
1485
1486 error = xfs_growfs_log(mp, &in);
1487 return -error;
1488 }
1489
1490 case XFS_IOC_FSGROWFSRT: {
1491 xfs_growfs_rt_t in;
1492
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001493 if (copy_from_user(&in, arg, sizeof(in)))
1494 return -XFS_ERROR(EFAULT);
1495
1496 error = xfs_growfs_rt(mp, &in);
1497 return -error;
1498 }
1499
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001500 case XFS_IOC_GOINGDOWN: {
1501 __uint32_t in;
1502
1503 if (!capable(CAP_SYS_ADMIN))
1504 return -EPERM;
1505
1506 if (get_user(in, (__uint32_t __user *)arg))
1507 return -XFS_ERROR(EFAULT);
1508
1509 error = xfs_fs_goingdown(mp, in);
1510 return -error;
1511 }
1512
1513 case XFS_IOC_ERROR_INJECTION: {
1514 xfs_error_injection_t in;
1515
1516 if (!capable(CAP_SYS_ADMIN))
1517 return -EPERM;
1518
1519 if (copy_from_user(&in, arg, sizeof(in)))
1520 return -XFS_ERROR(EFAULT);
1521
1522 error = xfs_errortag_add(in.errtag, mp);
1523 return -error;
1524 }
1525
1526 case XFS_IOC_ERROR_CLEARALL:
1527 if (!capable(CAP_SYS_ADMIN))
1528 return -EPERM;
1529
1530 error = xfs_errortag_clearall(mp, 1);
1531 return -error;
1532
1533 default:
1534 return -ENOTTY;
1535 }
1536}