blob: 8aa54f0eec15467b58db3f8d0d17d6159364d9b2 [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_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
Hannes Eder7bf446f2009-03-05 15:20:25 +010031#include "xfs_ioctl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_buf_item.h"
38#include "xfs_utils.h"
39#include "xfs_dfrag.h"
40#include "xfs_fsops.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100041#include "xfs_vnodeops.h"
Christoph Hellwig25fe55e2008-07-18 17:13:20 +100042#include "xfs_quota.h"
43#include "xfs_inode_item.h"
Christoph Hellwigd296d302009-01-19 02:02:57 +010044#include "xfs_export.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080047#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/dcache.h>
49#include <linux/mount.h>
50#include <linux/namei.h>
51#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Christoph Hellwigd296d302009-01-19 02:02:57 +010053#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
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 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060066int
Linus Torvalds1da177e2005-04-16 15:20:36 -070067xfs_find_handle(
68 unsigned int cmd,
sandeen@sandeen.net743bb462008-11-25 21:20:06 -060069 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 int hsize;
72 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct inode *inode;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010074 struct file *file = NULL;
75 struct path path;
76 int error;
77 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010079 if (cmd == XFS_IOC_FD_TO_HANDLE) {
sandeen@sandeen.net743bb462008-11-25 21:20:06 -060080 file = fget(hreq->fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (!file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010082 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010090 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010092 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100100 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100107 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100109 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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int lock_mode;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 lock_mode = xfs_ilock_map_shared(ip);
Christoph Hellwigc6143912007-09-14 15:22:37 +1000119 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 xfs_iunlock_map_shared(ip, lock_mode);
125
126 hsize = XFS_HSIZE(handle);
127 }
128
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100129 error = -EFAULT;
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600130 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100131 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
132 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100134 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100145 * No need to do permission checks on the various pathname components
146 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
148STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100149xfs_handle_acceptable(
150 void *context,
151 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100153 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)
164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100166 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
169 * Only allow handle opens under a directory.
170 */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100171 if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
172 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Christoph Hellwigd296d302009-01-19 02:02:57 +0100174 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Christoph Hellwigd296d302009-01-19 02:02:57 +0100182 memset(&fid, 0, sizeof(struct fid));
183 fid.ino = handle.ha_fid.fid_ino;
184 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Christoph Hellwigd296d302009-01-19 02:02:57 +0100186 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Christoph Hellwigd296d302009-01-19 02:02:57 +0100191STATIC 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600199int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100202 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
David Howells745ca242008-11-14 10:39:22 +1100204 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100206 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int permflag;
208 struct file *filp;
209 struct inode *inode;
210 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if (!capable(CAP_SYS_ADMIN))
213 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Christoph Hellwigd296d302009-01-19 02:02:57 +0100215 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
216 if (IS_ERR(dentry))
217 return PTR_ERR(dentry);
218 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Restrict xfs_open_by_handle to directories & regular files. */
221 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100222 error = -XFS_ERROR(EPERM);
223 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600227 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Put open permission in namei format. */
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600231 permflag = hreq->oflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 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)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100239 error = -XFS_ERROR(EPERM);
240 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100244 error = -XFS_ERROR(EACCES);
245 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
248 /* Can't write directories. */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100249 if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
250 error = -XFS_ERROR(EISDIR);
251 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Christoph Hellwigd296d302009-01-19 02:02:57 +0100254 fd = get_unused_fd();
255 if (fd < 0) {
256 error = fd;
257 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Christoph Hellwigd296d302009-01-19 02:02:57 +0100260 filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
261 hreq->oflags, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100263 put_unused_fd(fd);
264 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500266
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100267 if (inode->i_mode & S_IFREG) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100268 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500269 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Hellwigd296d302009-01-19 02:02:57 +0100272 fd_install(fd, filp);
273 return fd;
274
275 out_dput:
276 dput(dentry);
277 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000280/*
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
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600306int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100308 struct file *parfilp,
309 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100311 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000313 void *link;
314 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (!capable(CAP_SYS_ADMIN))
317 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Christoph Hellwigd296d302009-01-19 02:02:57 +0100319 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
320 if (IS_ERR(dentry))
321 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Restrict this handle operation to symlinks only. */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100324 if (!S_ISLNK(dentry->d_inode->i_mode)) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000325 error = -XFS_ERROR(EINVAL);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100326 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600329 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000330 error = -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100331 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000334 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100335 if (!link) {
336 error = -XFS_ERROR(ENOMEM);
337 goto out_dput;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Christoph Hellwigd296d302009-01-19 02:02:57 +0100340 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000341 if (error)
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000342 goto out_kfree;
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600343 error = do_readlink(hreq->ohandle, olen, link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000344 if (error)
345 goto out_kfree;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000346
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000347 out_kfree:
348 kfree(link);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100349 out_dput:
350 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000351 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354STATIC int
355xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100356 struct file *parfilp,
357 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int error;
360 struct fsdmidata fsd;
361 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100362 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
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
Christoph Hellwigd296d302009-01-19 02:02:57 +0100369 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
370 if (IS_ERR(dentry))
371 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Christoph Hellwigd296d302009-01-19 02:02:57 +0100373 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000374 error = -XFS_ERROR(EPERM);
375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000379 error = -XFS_ERROR(EFAULT);
380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Christoph Hellwigd296d302009-01-19 02:02:57 +0100383 error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000384 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000386 out:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100387 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000388 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391STATIC int
392xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100393 struct file *parfilp,
394 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100396 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 attrlist_cursor_kern_t *cursor;
398 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100399 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 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
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000409 /*
410 * Reject flags, only allow namespaces.
411 */
412 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
413 return -XFS_ERROR(EINVAL);
414
Christoph Hellwigd296d302009-01-19 02:02:57 +0100415 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
416 if (IS_ERR(dentry))
417 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
420 if (!kbuf)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100421 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100424 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000425 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 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);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100434 out_dput:
435 dput(dentry);
436 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600439int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000441 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100442 unsigned char *name,
443 unsigned char __user *ubuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 __uint32_t *len,
445 __uint32_t flags)
446{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100447 unsigned char *kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (*len > XATTR_SIZE_MAX)
451 return EINVAL;
452 kbuf = kmalloc(*len, GFP_KERNEL);
453 if (!kbuf)
454 return ENOMEM;
455
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000456 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 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
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600468int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000470 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100471 unsigned char *name,
472 const unsigned char __user *ubuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 __uint32_t len,
474 __uint32_t flags)
475{
Dave Chinnera9273ca2010-01-20 10:47:48 +1100476 unsigned char *kbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int error = EFAULT;
478
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000479 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return EPERM;
481 if (len > XATTR_SIZE_MAX)
482 return EINVAL;
483
Li Zefan0e639bd2009-04-08 15:08:04 +0800484 kbuf = memdup_user(ubuf, len);
485 if (IS_ERR(kbuf))
486 return PTR_ERR(kbuf);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000487
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000488 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return error;
491}
492
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600493int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000495 struct inode *inode,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100496 unsigned char *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 __uint32_t flags)
498{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000499 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000501 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504STATIC int
505xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800506 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100507 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int error;
510 xfs_attr_multiop_t *ops;
511 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100512 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unsigned int i, size;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100514 unsigned char *attr_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
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
Zhitong Wangfda168c2010-03-23 09:51:22 +1100521 /* overflow check */
522 if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
523 return -E2BIG;
524
Christoph Hellwigd296d302009-01-19 02:02:57 +0100525 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
526 if (IS_ERR(dentry))
527 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000530 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100532 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Li Zefan0e639bd2009-04-08 15:08:04 +0800534 ops = memdup_user(am_hreq.ops, size);
535 if (IS_ERR(ops)) {
536 error = PTR_ERR(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100537 goto out_dput;
Li Zefan0e639bd2009-04-08 15:08:04 +0800538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
541 if (!attr_name)
542 goto out_kfree_ops;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 error = 0;
545 for (i = 0; i < am_hreq.opcount; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100546 ops[i].am_error = strncpy_from_user((char *)attr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 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:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100555 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560 case ATTR_OP_SET:
Dave Hansen42a74f22008-02-15 14:37:46 -0800561 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
562 if (ops[i].am_error)
563 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100564 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);
Dave Hansen42a74f22008-02-15 14:37:46 -0800568 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
570 case ATTR_OP_REMOVE:
Dave Hansen42a74f22008-02-15 14:37:46 -0800571 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
572 if (ops[i].am_error)
573 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100574 ops[i].am_error = xfs_attrmulti_attr_remove(
575 dentry->d_inode, attr_name,
576 ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800577 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 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);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100590 out_dput:
591 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return -error;
593}
594
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600595int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000597 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000598 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct file *filp,
600 int ioflags,
601 unsigned int cmd,
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600602 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int attr_flags = 0;
605 int error;
606
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600607 /*
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
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000615 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -XFS_ERROR(EPERM);
617
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000618 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return -XFS_ERROR(EBADF);
620
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000621 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -XFS_ERROR(EINVAL);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000625 attr_flags |= XFS_ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (ioflags & IO_INVIS)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000627 attr_flags |= XFS_ATTR_DMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600629 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 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
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100663 if (bulkreq.ubuffer == NULL)
664 return -XFS_ERROR(EINVAL);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (cmd == XFS_IOC_FSINUMBERS)
667 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000668 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
670 error = xfs_bulkstat_single(mp, &inlast,
671 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100672 else /* XFS_IOC_FSBULKSTAT */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000673 error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
674 sizeof(xfs_bstat_t), bulkreq.ubuffer,
675 &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730STATIC unsigned int
731xfs_merge_ioc_xflags(
732 unsigned int flags,
733 unsigned int start)
734{
735 unsigned int xflags = start;
736
Eric Sandeen39058a02007-02-10 18:37:10 +1100737 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 xflags |= XFS_XFLAG_IMMUTABLE;
739 else
740 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100741 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 xflags |= XFS_XFLAG_APPEND;
743 else
744 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100745 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 xflags |= XFS_XFLAG_SYNC;
747 else
748 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100749 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 xflags |= XFS_XFLAG_NOATIME;
751 else
752 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100753 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 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)
Eric Sandeen39058a02007-02-10 18:37:10 +1100768 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100770 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100772 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100774 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100776 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return flags;
778}
779
780STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000781xfs_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
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000816STATIC 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
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000858STATIC void
859xfs_diflags_to_linux(
860 struct xfs_inode *ip)
861{
David Chinnere4f75292008-08-13 16:00:45 +1000862 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000863 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}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000882
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;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200897 struct xfs_dquot *udqp = NULL;
898 struct xfs_dquot *gdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000899 struct xfs_dquot *olddquot = NULL;
900 int code;
901
902 xfs_itrace_entry(ip);
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
909 /*
910 * If disk quotas is on, we make sure that the dquots do exist on disk,
911 * before we start any other transactions. Trying to do this later
912 * is messy. We don't care to take a readlock to look at the ids
913 * in inode here, because we can't hold it across the trans_reserve.
914 * If the IDs do change before we take the ilock, we're covered
915 * because the i_*dquot fields will get updated anyway.
916 */
917 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200918 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000919 ip->i_d.di_gid, fa->fsx_projid,
920 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
921 if (code)
922 return code;
923 }
924
925 /*
926 * For the other attributes, we acquire the inode lock and
927 * first do an error checking pass.
928 */
929 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
930 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
931 if (code)
932 goto error_return;
933
934 lock_flags = XFS_ILOCK_EXCL;
935 xfs_ilock(ip, lock_flags);
936
937 /*
938 * CAP_FOWNER overrides the following restrictions:
939 *
940 * The user ID of the calling process must be equal
941 * to the file owner ID, except in cases where the
942 * CAP_FSETID capability is applicable.
943 */
David Howells91b77712008-10-31 15:50:04 +1100944 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000945 code = XFS_ERROR(EPERM);
946 goto error_return;
947 }
948
949 /*
950 * Do a quota reservation only if projid is actually going to change.
951 */
952 if (mask & FSX_PROJID) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200953 if (XFS_IS_QUOTA_RUNNING(mp) &&
954 XFS_IS_PQUOTA_ON(mp) &&
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000955 ip->i_d.di_projid != fa->fsx_projid) {
956 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200957 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000958 capable(CAP_FOWNER) ?
959 XFS_QMOPT_FORCE_RES : 0);
960 if (code) /* out of quota */
961 goto error_return;
962 }
963 }
964
965 if (mask & FSX_EXTSIZE) {
966 /*
967 * Can't change extent size if any extents are allocated.
968 */
969 if (ip->i_d.di_nextents &&
970 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
971 fa->fsx_extsize)) {
972 code = XFS_ERROR(EINVAL); /* EFBIG? */
973 goto error_return;
974 }
975
976 /*
977 * Extent size must be a multiple of the appropriate block
978 * size, if set at all.
979 */
980 if (fa->fsx_extsize != 0) {
981 xfs_extlen_t size;
982
983 if (XFS_IS_REALTIME_INODE(ip) ||
984 ((mask & FSX_XFLAGS) &&
985 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
986 size = mp->m_sb.sb_rextsize <<
987 mp->m_sb.sb_blocklog;
988 } else {
989 size = mp->m_sb.sb_blocksize;
990 }
991
992 if (fa->fsx_extsize % size) {
993 code = XFS_ERROR(EINVAL);
994 goto error_return;
995 }
996 }
997 }
998
999
1000 if (mask & FSX_XFLAGS) {
1001 /*
1002 * Can't change realtime flag if any extents are allocated.
1003 */
1004 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1005 (XFS_IS_REALTIME_INODE(ip)) !=
1006 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1007 code = XFS_ERROR(EINVAL); /* EFBIG? */
1008 goto error_return;
1009 }
1010
1011 /*
1012 * If realtime flag is set then must have realtime data.
1013 */
1014 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1015 if ((mp->m_sb.sb_rblocks == 0) ||
1016 (mp->m_sb.sb_rextsize == 0) ||
1017 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1018 code = XFS_ERROR(EINVAL);
1019 goto error_return;
1020 }
1021 }
1022
1023 /*
1024 * Can't modify an immutable/append-only file unless
1025 * we have appropriate permission.
1026 */
1027 if ((ip->i_d.di_flags &
1028 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1029 (fa->fsx_xflags &
1030 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1031 !capable(CAP_LINUX_IMMUTABLE)) {
1032 code = XFS_ERROR(EPERM);
1033 goto error_return;
1034 }
1035 }
1036
1037 xfs_trans_ijoin(tp, ip, lock_flags);
1038 xfs_trans_ihold(tp, ip);
1039
1040 /*
1041 * Change file ownership. Must be the owner or privileged.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001042 */
1043 if (mask & FSX_PROJID) {
1044 /*
1045 * CAP_FSETID overrides the following restrictions:
1046 *
1047 * The set-user-ID and set-group-ID bits of a file will be
1048 * cleared upon successful return from chown()
1049 */
1050 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1051 !capable(CAP_FSETID))
1052 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1053
1054 /*
1055 * Change the ownerships and register quota modifications
1056 * in the transaction.
1057 */
1058 if (ip->i_d.di_projid != fa->fsx_projid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001059 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1060 olddquot = xfs_qm_vop_chown(tp, ip,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001061 &ip->i_gdquot, gdqp);
1062 }
1063 ip->i_d.di_projid = fa->fsx_projid;
1064
1065 /*
1066 * We may have to rev the inode as well as
1067 * the superblock version number since projids didn't
1068 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1069 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001070 if (ip->i_d.di_version == 1)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001071 xfs_bump_ino_vers2(tp, ip);
1072 }
1073
1074 }
1075
1076 if (mask & FSX_EXTSIZE)
1077 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001078 if (mask & FSX_XFLAGS) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001079 xfs_set_diflags(ip, fa->fsx_xflags);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001080 xfs_diflags_to_linux(ip);
1081 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001082
1083 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1084 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1085
1086 XFS_STATS_INC(xs_ig_attrchg);
1087
1088 /*
1089 * If this is a synchronous mount, make sure that the
1090 * transaction goes to disk before returning to the user.
1091 * This is slightly sub-optimal in that truncates require
1092 * two sync transactions instead of one for wsync filesystems.
1093 * One for the truncate and one for the timestamps since we
1094 * don't want to change the timestamps unless we're sure the
1095 * truncate worked. Truncates are less than 1% of the laddis
1096 * mix so this probably isn't worth the trouble to optimize.
1097 */
1098 if (mp->m_flags & XFS_MOUNT_WSYNC)
1099 xfs_trans_set_sync(tp);
1100 code = xfs_trans_commit(tp, 0);
1101 xfs_iunlock(ip, lock_flags);
1102
1103 /*
1104 * Release any dquot(s) the inode had kept before chown.
1105 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001106 xfs_qm_dqrele(olddquot);
1107 xfs_qm_dqrele(udqp);
1108 xfs_qm_dqrele(gdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001109
Christoph Hellwig288699f2010-06-23 18:11:15 +10001110 return code;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001111
1112 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001113 xfs_qm_dqrele(udqp);
1114 xfs_qm_dqrele(gdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001115 xfs_trans_cancel(tp, 0);
1116 if (lock_flags)
1117 xfs_iunlock(ip, lock_flags);
1118 return code;
1119}
1120
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001121STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001122xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 xfs_inode_t *ip,
1124 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 void __user *arg)
1126{
1127 struct fsxattr fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001128 unsigned int mask;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001129
1130 if (copy_from_user(&fa, arg, sizeof(fa)))
1131 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001133 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001134 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001135 mask |= FSX_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001137 return -xfs_ioctl_setattr(ip, &fa, mask);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001140STATIC int
1141xfs_ioc_getxflags(
1142 xfs_inode_t *ip,
1143 void __user *arg)
1144{
1145 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001147 flags = xfs_di2lxflags(ip->i_d.di_flags);
1148 if (copy_to_user(arg, &flags, sizeof(flags)))
1149 return -EFAULT;
1150 return 0;
1151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001153STATIC int
1154xfs_ioc_setxflags(
1155 xfs_inode_t *ip,
1156 struct file *filp,
1157 void __user *arg)
1158{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001159 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001160 unsigned int flags;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001161 unsigned int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001163 if (copy_from_user(&flags, arg, sizeof(flags)))
1164 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001166 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1167 FS_NOATIME_FL | FS_NODUMP_FL | \
1168 FS_SYNC_FL))
1169 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001171 mask = FSX_XFLAGS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001172 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001173 mask |= FSX_NONBLOCK;
1174 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001176 return -xfs_ioctl_setattr(ip, &fa, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001180xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1181{
1182 struct getbmap __user *base = *ap;
1183
1184 /* copy only getbmap portion (not getbmapx) */
1185 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1186 return XFS_ERROR(EFAULT);
1187
1188 *ap += sizeof(struct getbmap);
1189 return 0;
1190}
1191
1192STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001194 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int ioflags,
1196 unsigned int cmd,
1197 void __user *arg)
1198{
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001199 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int error;
1201
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001202 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -XFS_ERROR(EFAULT);
1204
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001205 if (bmx.bmv_count < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return -XFS_ERROR(EINVAL);
1207
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001208 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (ioflags & IO_INVIS)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001210 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001212 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1213 (struct getbmap *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (error)
1215 return -error;
1216
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001217 /* copy back header - only size of getbmap */
1218 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return -XFS_ERROR(EFAULT);
1220 return 0;
1221}
1222
1223STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001224xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1225{
1226 struct getbmapx __user *base = *ap;
1227
1228 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1229 return XFS_ERROR(EFAULT);
1230
1231 *ap += sizeof(struct getbmapx);
1232 return 0;
1233}
1234
1235STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001237 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 void __user *arg)
1239{
1240 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int error;
1242
1243 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1244 return -XFS_ERROR(EFAULT);
1245
1246 if (bmx.bmv_count < 2)
1247 return -XFS_ERROR(EINVAL);
1248
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001249 if (bmx.bmv_iflags & (~BMV_IF_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return -XFS_ERROR(EINVAL);
1251
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001252 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1253 (struct getbmapx *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (error)
1255 return -error;
1256
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001257 /* copy back header */
1258 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return -XFS_ERROR(EFAULT);
1260
1261 return 0;
1262}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001263
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001264/*
1265 * Note: some of the ioctl's return positive numbers as a
1266 * byte count indicating success, such as readlink_by_handle.
1267 * So we don't "sign flip" like most other routines. This means
1268 * true errors need to be returned as a negative value.
1269 */
1270long
1271xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001272 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001273 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001274 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001275{
1276 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001277 struct xfs_inode *ip = XFS_I(inode);
1278 struct xfs_mount *mp = ip->i_mount;
1279 void __user *arg = (void __user *)p;
1280 int ioflags = 0;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001281 int error;
1282
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001283 if (filp->f_mode & FMODE_NOCMTIME)
1284 ioflags |= IO_INVIS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001285
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001286 xfs_itrace_entry(ip);
1287
1288 switch (cmd) {
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001289 case XFS_IOC_ALLOCSP:
1290 case XFS_IOC_FREESP:
1291 case XFS_IOC_RESVSP:
1292 case XFS_IOC_UNRESVSP:
1293 case XFS_IOC_ALLOCSP64:
1294 case XFS_IOC_FREESP64:
1295 case XFS_IOC_RESVSP64:
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001296 case XFS_IOC_UNRESVSP64: {
1297 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001298
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001299 if (copy_from_user(&bf, arg, sizeof(bf)))
1300 return -XFS_ERROR(EFAULT);
1301 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1302 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001303 case XFS_IOC_DIOINFO: {
1304 struct dioattr da;
1305 xfs_buftarg_t *target =
1306 XFS_IS_REALTIME_INODE(ip) ?
1307 mp->m_rtdev_targp : mp->m_ddev_targp;
1308
1309 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1310 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1311
1312 if (copy_to_user(arg, &da, sizeof(da)))
1313 return -XFS_ERROR(EFAULT);
1314 return 0;
1315 }
1316
1317 case XFS_IOC_FSBULKSTAT_SINGLE:
1318 case XFS_IOC_FSBULKSTAT:
1319 case XFS_IOC_FSINUMBERS:
1320 return xfs_ioc_bulkstat(mp, cmd, arg);
1321
1322 case XFS_IOC_FSGEOMETRY_V1:
1323 return xfs_ioc_fsgeometry_v1(mp, arg);
1324
1325 case XFS_IOC_FSGEOMETRY:
1326 return xfs_ioc_fsgeometry(mp, arg);
1327
1328 case XFS_IOC_GETVERSION:
1329 return put_user(inode->i_generation, (int __user *)arg);
1330
1331 case XFS_IOC_FSGETXATTR:
1332 return xfs_ioc_fsgetxattr(ip, 0, arg);
1333 case XFS_IOC_FSGETXATTRA:
1334 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001335 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001336 return xfs_ioc_fssetxattr(ip, filp, arg);
1337 case XFS_IOC_GETXFLAGS:
1338 return xfs_ioc_getxflags(ip, arg);
1339 case XFS_IOC_SETXFLAGS:
1340 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001341
1342 case XFS_IOC_FSSETDM: {
1343 struct fsdmidata dmi;
1344
1345 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1346 return -XFS_ERROR(EFAULT);
1347
1348 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1349 dmi.fsd_dmstate);
1350 return -error;
1351 }
1352
1353 case XFS_IOC_GETBMAP:
1354 case XFS_IOC_GETBMAPA:
1355 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1356
1357 case XFS_IOC_GETBMAPX:
1358 return xfs_ioc_getbmapx(ip, arg);
1359
1360 case XFS_IOC_FD_TO_HANDLE:
1361 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001362 case XFS_IOC_PATH_TO_FSHANDLE: {
1363 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001364
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001365 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1366 return -XFS_ERROR(EFAULT);
1367 return xfs_find_handle(cmd, &hreq);
1368 }
1369 case XFS_IOC_OPEN_BY_HANDLE: {
1370 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001371
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001372 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1373 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001374 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001375 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001376 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001377 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001378
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001379 case XFS_IOC_READLINK_BY_HANDLE: {
1380 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001381
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001382 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1383 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001384 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001385 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001386 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001387 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001388
1389 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001390 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001391
1392 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001393 struct xfs_swapext sxp;
1394
1395 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1396 return -XFS_ERROR(EFAULT);
1397 error = xfs_swapext(&sxp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001398 return -error;
1399 }
1400
1401 case XFS_IOC_FSCOUNTS: {
1402 xfs_fsop_counts_t out;
1403
1404 error = xfs_fs_counts(mp, &out);
1405 if (error)
1406 return -error;
1407
1408 if (copy_to_user(arg, &out, sizeof(out)))
1409 return -XFS_ERROR(EFAULT);
1410 return 0;
1411 }
1412
1413 case XFS_IOC_SET_RESBLKS: {
1414 xfs_fsop_resblks_t inout;
1415 __uint64_t in;
1416
1417 if (!capable(CAP_SYS_ADMIN))
1418 return -EPERM;
1419
Eric Sandeend5db0f92010-02-05 22:59:53 +00001420 if (mp->m_flags & XFS_MOUNT_RDONLY)
1421 return -XFS_ERROR(EROFS);
1422
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001423 if (copy_from_user(&inout, arg, sizeof(inout)))
1424 return -XFS_ERROR(EFAULT);
1425
1426 /* input parameter is passed in resblks field of structure */
1427 in = inout.resblks;
1428 error = xfs_reserve_blocks(mp, &in, &inout);
1429 if (error)
1430 return -error;
1431
1432 if (copy_to_user(arg, &inout, sizeof(inout)))
1433 return -XFS_ERROR(EFAULT);
1434 return 0;
1435 }
1436
1437 case XFS_IOC_GET_RESBLKS: {
1438 xfs_fsop_resblks_t out;
1439
1440 if (!capable(CAP_SYS_ADMIN))
1441 return -EPERM;
1442
1443 error = xfs_reserve_blocks(mp, NULL, &out);
1444 if (error)
1445 return -error;
1446
1447 if (copy_to_user(arg, &out, sizeof(out)))
1448 return -XFS_ERROR(EFAULT);
1449
1450 return 0;
1451 }
1452
1453 case XFS_IOC_FSGROWFSDATA: {
1454 xfs_growfs_data_t in;
1455
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001456 if (copy_from_user(&in, arg, sizeof(in)))
1457 return -XFS_ERROR(EFAULT);
1458
1459 error = xfs_growfs_data(mp, &in);
1460 return -error;
1461 }
1462
1463 case XFS_IOC_FSGROWFSLOG: {
1464 xfs_growfs_log_t in;
1465
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001466 if (copy_from_user(&in, arg, sizeof(in)))
1467 return -XFS_ERROR(EFAULT);
1468
1469 error = xfs_growfs_log(mp, &in);
1470 return -error;
1471 }
1472
1473 case XFS_IOC_FSGROWFSRT: {
1474 xfs_growfs_rt_t in;
1475
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001476 if (copy_from_user(&in, arg, sizeof(in)))
1477 return -XFS_ERROR(EFAULT);
1478
1479 error = xfs_growfs_rt(mp, &in);
1480 return -error;
1481 }
1482
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001483 case XFS_IOC_GOINGDOWN: {
1484 __uint32_t in;
1485
1486 if (!capable(CAP_SYS_ADMIN))
1487 return -EPERM;
1488
1489 if (get_user(in, (__uint32_t __user *)arg))
1490 return -XFS_ERROR(EFAULT);
1491
1492 error = xfs_fs_goingdown(mp, in);
1493 return -error;
1494 }
1495
1496 case XFS_IOC_ERROR_INJECTION: {
1497 xfs_error_injection_t in;
1498
1499 if (!capable(CAP_SYS_ADMIN))
1500 return -EPERM;
1501
1502 if (copy_from_user(&in, arg, sizeof(in)))
1503 return -XFS_ERROR(EFAULT);
1504
1505 error = xfs_errortag_add(in.errtag, mp);
1506 return -error;
1507 }
1508
1509 case XFS_IOC_ERROR_CLEARALL:
1510 if (!capable(CAP_SYS_ADMIN))
1511 return -EPERM;
1512
1513 error = xfs_errortag_clearall(mp, 1);
1514 return -error;
1515
1516 default:
1517 return -ENOTTY;
1518 }
1519}