blob: c7d684f02f8978aab3b34e54cafd306db4202208 [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"
Hannes Eder7bf446f2009-03-05 15:20:25 +010037#include "xfs_ioctl.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_btree.h"
39#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110042#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_rw.h"
44#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110046#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_buf_item.h"
48#include "xfs_utils.h"
49#include "xfs_dfrag.h"
50#include "xfs_fsops.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100051#include "xfs_vnodeops.h"
Christoph Hellwig25fe55e2008-07-18 17:13:20 +100052#include "xfs_quota.h"
53#include "xfs_inode_item.h"
Christoph Hellwigd296d302009-01-19 02:02:57 +010054#include "xfs_export.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080056#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/dcache.h>
58#include <linux/mount.h>
59#include <linux/namei.h>
60#include <linux/pagemap.h>
Christoph Hellwigd296d302009-01-19 02:02:57 +010061#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
65 * a file or fs handle.
66 *
67 * XFS_IOC_PATH_TO_FSHANDLE
68 * returns fs handle for a mount point or path within that mount point
69 * XFS_IOC_FD_TO_HANDLE
70 * returns full handle for a FD opened in user space
71 * XFS_IOC_PATH_TO_HANDLE
72 * returns full handle for a path
73 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060074int
Linus Torvalds1da177e2005-04-16 15:20:36 -070075xfs_find_handle(
76 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060077 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int hsize;
80 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct inode *inode;
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010082 struct file *file = NULL;
83 struct path path;
84 int error;
85 struct xfs_inode *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010087 if (cmd == XFS_IOC_FD_TO_HANDLE) {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060088 file = fget(hreq->fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (!file)
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010090 return -EBADF;
91 inode = file->f_path.dentry->d_inode;
92 } else {
93 error = user_lpath((const char __user *)hreq->path, &path);
94 if (error)
95 return error;
96 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Christoph Hellwig4346cdd2009-02-08 21:51:14 +010098 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100100 /*
101 * We can only generate handles for inodes residing on a XFS filesystem,
102 * and only for regular files, directories or symbolic links.
103 */
104 error = -EINVAL;
105 if (inode->i_sb->s_magic != XFS_SB_MAGIC)
106 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100108 error = -EBADF;
109 if (!S_ISREG(inode->i_mode) &&
110 !S_ISDIR(inode->i_mode) &&
111 !S_ISLNK(inode->i_mode))
112 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100115 memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100117 if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
118 /*
119 * This handle only contains an fsid, zero the rest.
120 */
121 memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
122 hsize = sizeof(xfs_fsid_t);
123 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int lock_mode;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 lock_mode = xfs_ilock_map_shared(ip);
Christoph Hellwigc6143912007-09-14 15:22:37 +1000127 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
128 sizeof(handle.ha_fid.fid_len);
129 handle.ha_fid.fid_pad = 0;
130 handle.ha_fid.fid_gen = ip->i_d.di_gen;
131 handle.ha_fid.fid_ino = ip->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 xfs_iunlock_map_shared(ip, lock_mode);
133
134 hsize = XFS_HSIZE(handle);
135 }
136
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100137 error = -EFAULT;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600138 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100139 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
140 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Christoph Hellwig4346cdd2009-02-08 21:51:14 +0100142 error = 0;
143
144 out_put:
145 if (cmd == XFS_IOC_FD_TO_HANDLE)
146 fput(file);
147 else
148 path_put(&path);
149 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Christoph Hellwigd296d302009-01-19 02:02:57 +0100153 * No need to do permission checks on the various pathname components
154 * as the handle operations are privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
156STATIC int
Christoph Hellwigd296d302009-01-19 02:02:57 +0100157xfs_handle_acceptable(
158 void *context,
159 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100161 return 1;
162}
163
164/*
165 * Convert userspace handle data into a dentry.
166 */
167struct dentry *
168xfs_handle_to_dentry(
169 struct file *parfilp,
170 void __user *uhandle,
171 u32 hlen)
172{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xfs_handle_t handle;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100174 struct xfs_fid64 fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /*
177 * Only allow handle opens under a directory.
178 */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100179 if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
180 return ERR_PTR(-ENOTDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Christoph Hellwigd296d302009-01-19 02:02:57 +0100182 if (hlen != sizeof(xfs_handle_t))
183 return ERR_PTR(-EINVAL);
184 if (copy_from_user(&handle, uhandle, hlen))
185 return ERR_PTR(-EFAULT);
186 if (handle.ha_fid.fid_len !=
187 sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
188 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Christoph Hellwigd296d302009-01-19 02:02:57 +0100190 memset(&fid, 0, sizeof(struct fid));
191 fid.ino = handle.ha_fid.fid_ino;
192 fid.gen = handle.ha_fid.fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Christoph Hellwigd296d302009-01-19 02:02:57 +0100194 return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
195 FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
196 xfs_handle_acceptable, NULL);
197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Christoph Hellwigd296d302009-01-19 02:02:57 +0100199STATIC struct dentry *
200xfs_handlereq_to_dentry(
201 struct file *parfilp,
202 xfs_fsop_handlereq_t *hreq)
203{
204 return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600207int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208xfs_open_by_handle(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100210 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
David Howells745ca242008-11-14 10:39:22 +1100212 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int error;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100214 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int permflag;
216 struct file *filp;
217 struct inode *inode;
218 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (!capable(CAP_SYS_ADMIN))
221 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Christoph Hellwigd296d302009-01-19 02:02:57 +0100223 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
224 if (IS_ERR(dentry))
225 return PTR_ERR(dentry);
226 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* Restrict xfs_open_by_handle to directories & regular files. */
229 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100230 error = -XFS_ERROR(EPERM);
231 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
234#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600235 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#endif
Christoph Hellwigd296d302009-01-19 02:02:57 +0100237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* Put open permission in namei format. */
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600239 permflag = hreq->oflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if ((permflag+1) & O_ACCMODE)
241 permflag++;
242 if (permflag & O_TRUNC)
243 permflag |= 2;
244
245 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
246 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100247 error = -XFS_ERROR(EPERM);
248 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
251 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100252 error = -XFS_ERROR(EACCES);
253 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
256 /* Can't write directories. */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100257 if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
258 error = -XFS_ERROR(EISDIR);
259 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Christoph Hellwigd296d302009-01-19 02:02:57 +0100262 fd = get_unused_fd();
263 if (fd < 0) {
264 error = fd;
265 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Christoph Hellwigd296d302009-01-19 02:02:57 +0100268 filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
269 hreq->oflags, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (IS_ERR(filp)) {
Christoph Hellwigd296d302009-01-19 02:02:57 +0100271 put_unused_fd(fd);
272 return PTR_ERR(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500274
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100275 if (inode->i_mode & S_IFREG) {
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100276 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500277 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Christoph Hellwigd296d302009-01-19 02:02:57 +0100280 fd_install(fd, filp);
281 return fd;
282
283 out_dput:
284 dput(dentry);
285 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000288/*
289 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
290 * unused first argument.
291 */
292STATIC int
293do_readlink(
294 char __user *buffer,
295 int buflen,
296 const char *link)
297{
298 int len;
299
300 len = PTR_ERR(link);
301 if (IS_ERR(link))
302 goto out;
303
304 len = strlen(link);
305 if (len > (unsigned) buflen)
306 len = buflen;
307 if (copy_to_user(buffer, link, len))
308 len = -EFAULT;
309 out:
310 return len;
311}
312
313
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600314int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315xfs_readlink_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100316 struct file *parfilp,
317 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100319 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000321 void *link;
322 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (!capable(CAP_SYS_ADMIN))
325 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Christoph Hellwigd296d302009-01-19 02:02:57 +0100327 dentry = xfs_handlereq_to_dentry(parfilp, hreq);
328 if (IS_ERR(dentry))
329 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* Restrict this handle operation to symlinks only. */
Christoph Hellwigd296d302009-01-19 02:02:57 +0100332 if (!S_ISLNK(dentry->d_inode->i_mode)) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000333 error = -XFS_ERROR(EINVAL);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100334 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600337 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000338 error = -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100339 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000342 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100343 if (!link) {
344 error = -XFS_ERROR(ENOMEM);
345 goto out_dput;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Christoph Hellwigd296d302009-01-19 02:02:57 +0100348 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000349 if (error)
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000350 goto out_kfree;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600351 error = do_readlink(hreq->ohandle, olen, link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000352 if (error)
353 goto out_kfree;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000354
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000355 out_kfree:
356 kfree(link);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100357 out_dput:
358 dput(dentry);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000359 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362STATIC int
363xfs_fssetdm_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100364 struct file *parfilp,
365 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 int error;
368 struct fsdmidata fsd;
369 xfs_fsop_setdm_handlereq_t dmhreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100370 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 if (!capable(CAP_MKNOD))
373 return -XFS_ERROR(EPERM);
374 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
375 return -XFS_ERROR(EFAULT);
376
Christoph Hellwigd296d302009-01-19 02:02:57 +0100377 dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
378 if (IS_ERR(dentry))
379 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Christoph Hellwigd296d302009-01-19 02:02:57 +0100381 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000382 error = -XFS_ERROR(EPERM);
383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
386 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000387 error = -XFS_ERROR(EFAULT);
388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Christoph Hellwigd296d302009-01-19 02:02:57 +0100391 error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000392 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000394 out:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100395 dput(dentry);
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000396 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399STATIC int
400xfs_attrlist_by_handle(
Christoph Hellwigd296d302009-01-19 02:02:57 +0100401 struct file *parfilp,
402 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Christoph Hellwigd296d302009-01-19 02:02:57 +0100404 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 attrlist_cursor_kern_t *cursor;
406 xfs_fsop_attrlist_handlereq_t al_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100407 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 char *kbuf;
409
410 if (!capable(CAP_SYS_ADMIN))
411 return -XFS_ERROR(EPERM);
412 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
413 return -XFS_ERROR(EFAULT);
414 if (al_hreq.buflen > XATTR_LIST_MAX)
415 return -XFS_ERROR(EINVAL);
416
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000417 /*
418 * Reject flags, only allow namespaces.
419 */
420 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
421 return -XFS_ERROR(EINVAL);
422
Christoph Hellwigd296d302009-01-19 02:02:57 +0100423 dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
424 if (IS_ERR(dentry))
425 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
428 if (!kbuf)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100429 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100432 error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000433 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (error)
435 goto out_kfree;
436
437 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
438 error = -EFAULT;
439
440 out_kfree:
441 kfree(kbuf);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100442 out_dput:
443 dput(dentry);
444 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600447int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000449 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 char *name,
451 char __user *ubuf,
452 __uint32_t *len,
453 __uint32_t flags)
454{
455 char *kbuf;
456 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (*len > XATTR_SIZE_MAX)
459 return EINVAL;
460 kbuf = kmalloc(*len, GFP_KERNEL);
461 if (!kbuf)
462 return ENOMEM;
463
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000464 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (error)
466 goto out_kfree;
467
468 if (copy_to_user(ubuf, kbuf, *len))
469 error = EFAULT;
470
471 out_kfree:
472 kfree(kbuf);
473 return error;
474}
475
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600476int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000478 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 char *name,
480 const char __user *ubuf,
481 __uint32_t len,
482 __uint32_t flags)
483{
484 char *kbuf;
485 int error = EFAULT;
486
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000487 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return EPERM;
489 if (len > XATTR_SIZE_MAX)
490 return EINVAL;
491
492 kbuf = kmalloc(len, GFP_KERNEL);
493 if (!kbuf)
494 return ENOMEM;
495
496 if (copy_from_user(kbuf, ubuf, len))
497 goto out_kfree;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000498
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000499 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 out_kfree:
502 kfree(kbuf);
503 return error;
504}
505
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600506int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000508 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 char *name,
510 __uint32_t flags)
511{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000512 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000514 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517STATIC int
518xfs_attrmulti_by_handle(
Dave Hansen42a74f22008-02-15 14:37:46 -0800519 struct file *parfilp,
Christoph Hellwigd296d302009-01-19 02:02:57 +0100520 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 int error;
523 xfs_attr_multiop_t *ops;
524 xfs_fsop_attrmulti_handlereq_t am_hreq;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100525 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned int i, size;
527 char *attr_name;
528
529 if (!capable(CAP_SYS_ADMIN))
530 return -XFS_ERROR(EPERM);
531 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
532 return -XFS_ERROR(EFAULT);
533
Christoph Hellwigd296d302009-01-19 02:02:57 +0100534 dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
535 if (IS_ERR(dentry))
536 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000539 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (!size || size > 16 * PAGE_SIZE)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100541 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 error = ENOMEM;
544 ops = kmalloc(size, GFP_KERNEL);
545 if (!ops)
Christoph Hellwigd296d302009-01-19 02:02:57 +0100546 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 error = EFAULT;
549 if (copy_from_user(ops, am_hreq.ops, size))
550 goto out_kfree_ops;
551
552 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
553 if (!attr_name)
554 goto out_kfree_ops;
555
556
557 error = 0;
558 for (i = 0; i < am_hreq.opcount; i++) {
559 ops[i].am_error = strncpy_from_user(attr_name,
560 ops[i].am_attrname, MAXNAMELEN);
561 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
562 error = -ERANGE;
563 if (ops[i].am_error < 0)
564 break;
565
566 switch (ops[i].am_opcode) {
567 case ATTR_OP_GET:
Christoph Hellwigd296d302009-01-19 02:02:57 +0100568 ops[i].am_error = xfs_attrmulti_attr_get(
569 dentry->d_inode, attr_name,
570 ops[i].am_attrvalue, &ops[i].am_length,
571 ops[i].am_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573 case ATTR_OP_SET:
Dave Hansen42a74f22008-02-15 14:37:46 -0800574 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
575 if (ops[i].am_error)
576 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100577 ops[i].am_error = xfs_attrmulti_attr_set(
578 dentry->d_inode, attr_name,
579 ops[i].am_attrvalue, ops[i].am_length,
580 ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800581 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583 case ATTR_OP_REMOVE:
Dave Hansen42a74f22008-02-15 14:37:46 -0800584 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
585 if (ops[i].am_error)
586 break;
Christoph Hellwigd296d302009-01-19 02:02:57 +0100587 ops[i].am_error = xfs_attrmulti_attr_remove(
588 dentry->d_inode, attr_name,
589 ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800590 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592 default:
593 ops[i].am_error = EINVAL;
594 }
595 }
596
597 if (copy_to_user(am_hreq.ops, ops, size))
598 error = XFS_ERROR(EFAULT);
599
600 kfree(attr_name);
601 out_kfree_ops:
602 kfree(ops);
Christoph Hellwigd296d302009-01-19 02:02:57 +0100603 out_dput:
604 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -error;
606}
607
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600608int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000610 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000611 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct file *filp,
613 int ioflags,
614 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600615 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int attr_flags = 0;
618 int error;
619
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600620 /*
621 * Only allow the sys admin to reserve space unless
622 * unwritten extents are enabled.
623 */
624 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
625 !capable(CAP_SYS_ADMIN))
626 return -XFS_ERROR(EPERM);
627
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000628 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -XFS_ERROR(EPERM);
630
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000631 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -XFS_ERROR(EBADF);
633
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000634 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -XFS_ERROR(EINVAL);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000638 attr_flags |= XFS_ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (ioflags & IO_INVIS)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000640 attr_flags |= XFS_ATTR_DMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600642 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -error;
644}
645
646STATIC int
647xfs_ioc_bulkstat(
648 xfs_mount_t *mp,
649 unsigned int cmd,
650 void __user *arg)
651{
652 xfs_fsop_bulkreq_t bulkreq;
653 int count; /* # of records returned */
654 xfs_ino_t inlast; /* last inode number */
655 int done;
656 int error;
657
658 /* done = 1 if there are more stats to get and if bulkstat */
659 /* should be called again (unused here, but used in dmapi) */
660
661 if (!capable(CAP_SYS_ADMIN))
662 return -EPERM;
663
664 if (XFS_FORCED_SHUTDOWN(mp))
665 return -XFS_ERROR(EIO);
666
667 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
668 return -XFS_ERROR(EFAULT);
669
670 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
671 return -XFS_ERROR(EFAULT);
672
673 if ((count = bulkreq.icount) <= 0)
674 return -XFS_ERROR(EINVAL);
675
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100676 if (bulkreq.ubuffer == NULL)
677 return -XFS_ERROR(EINVAL);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (cmd == XFS_IOC_FSINUMBERS)
680 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000681 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
683 error = xfs_bulkstat_single(mp, &inlast,
684 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100685 else /* XFS_IOC_FSBULKSTAT */
686 error = xfs_bulkstat(mp, &inlast, &count,
687 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
688 sizeof(xfs_bstat_t), bulkreq.ubuffer,
689 BULKSTAT_FG_QUICK, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (error)
692 return -error;
693
694 if (bulkreq.ocount != NULL) {
695 if (copy_to_user(bulkreq.lastip, &inlast,
696 sizeof(xfs_ino_t)))
697 return -XFS_ERROR(EFAULT);
698
699 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
700 return -XFS_ERROR(EFAULT);
701 }
702
703 return 0;
704}
705
706STATIC int
707xfs_ioc_fsgeometry_v1(
708 xfs_mount_t *mp,
709 void __user *arg)
710{
711 xfs_fsop_geom_v1_t fsgeo;
712 int error;
713
714 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
715 if (error)
716 return -error;
717
718 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
719 return -XFS_ERROR(EFAULT);
720 return 0;
721}
722
723STATIC int
724xfs_ioc_fsgeometry(
725 xfs_mount_t *mp,
726 void __user *arg)
727{
728 xfs_fsop_geom_t fsgeo;
729 int error;
730
731 error = xfs_fs_geometry(mp, &fsgeo, 4);
732 if (error)
733 return -error;
734
735 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
736 return -XFS_ERROR(EFAULT);
737 return 0;
738}
739
740/*
741 * Linux extended inode flags interface.
742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744STATIC unsigned int
745xfs_merge_ioc_xflags(
746 unsigned int flags,
747 unsigned int start)
748{
749 unsigned int xflags = start;
750
Eric Sandeen39058a02007-02-10 18:37:10 +1100751 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 xflags |= XFS_XFLAG_IMMUTABLE;
753 else
754 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100755 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xflags |= XFS_XFLAG_APPEND;
757 else
758 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100759 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 xflags |= XFS_XFLAG_SYNC;
761 else
762 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100763 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 xflags |= XFS_XFLAG_NOATIME;
765 else
766 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100767 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 xflags |= XFS_XFLAG_NODUMP;
769 else
770 xflags &= ~XFS_XFLAG_NODUMP;
771
772 return xflags;
773}
774
775STATIC unsigned int
776xfs_di2lxflags(
777 __uint16_t di_flags)
778{
779 unsigned int flags = 0;
780
781 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100782 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100784 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100786 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100788 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100790 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return flags;
792}
793
794STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000795xfs_ioc_fsgetxattr(
796 xfs_inode_t *ip,
797 int attr,
798 void __user *arg)
799{
800 struct fsxattr fa;
801
802 xfs_ilock(ip, XFS_ILOCK_SHARED);
803 fa.fsx_xflags = xfs_ip2xflags(ip);
804 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
805 fa.fsx_projid = ip->i_d.di_projid;
806
807 if (attr) {
808 if (ip->i_afp) {
809 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
810 fa.fsx_nextents = ip->i_afp->if_bytes /
811 sizeof(xfs_bmbt_rec_t);
812 else
813 fa.fsx_nextents = ip->i_d.di_anextents;
814 } else
815 fa.fsx_nextents = 0;
816 } else {
817 if (ip->i_df.if_flags & XFS_IFEXTENTS)
818 fa.fsx_nextents = ip->i_df.if_bytes /
819 sizeof(xfs_bmbt_rec_t);
820 else
821 fa.fsx_nextents = ip->i_d.di_nextents;
822 }
823 xfs_iunlock(ip, XFS_ILOCK_SHARED);
824
825 if (copy_to_user(arg, &fa, sizeof(fa)))
826 return -EFAULT;
827 return 0;
828}
829
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000830STATIC void
831xfs_set_diflags(
832 struct xfs_inode *ip,
833 unsigned int xflags)
834{
835 unsigned int di_flags;
836
837 /* can't set PREALLOC this way, just preserve it */
838 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
839 if (xflags & XFS_XFLAG_IMMUTABLE)
840 di_flags |= XFS_DIFLAG_IMMUTABLE;
841 if (xflags & XFS_XFLAG_APPEND)
842 di_flags |= XFS_DIFLAG_APPEND;
843 if (xflags & XFS_XFLAG_SYNC)
844 di_flags |= XFS_DIFLAG_SYNC;
845 if (xflags & XFS_XFLAG_NOATIME)
846 di_flags |= XFS_DIFLAG_NOATIME;
847 if (xflags & XFS_XFLAG_NODUMP)
848 di_flags |= XFS_DIFLAG_NODUMP;
849 if (xflags & XFS_XFLAG_PROJINHERIT)
850 di_flags |= XFS_DIFLAG_PROJINHERIT;
851 if (xflags & XFS_XFLAG_NODEFRAG)
852 di_flags |= XFS_DIFLAG_NODEFRAG;
853 if (xflags & XFS_XFLAG_FILESTREAM)
854 di_flags |= XFS_DIFLAG_FILESTREAM;
855 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
856 if (xflags & XFS_XFLAG_RTINHERIT)
857 di_flags |= XFS_DIFLAG_RTINHERIT;
858 if (xflags & XFS_XFLAG_NOSYMLINKS)
859 di_flags |= XFS_DIFLAG_NOSYMLINKS;
860 if (xflags & XFS_XFLAG_EXTSZINHERIT)
861 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
862 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
863 if (xflags & XFS_XFLAG_REALTIME)
864 di_flags |= XFS_DIFLAG_REALTIME;
865 if (xflags & XFS_XFLAG_EXTSIZE)
866 di_flags |= XFS_DIFLAG_EXTSIZE;
867 }
868
869 ip->i_d.di_flags = di_flags;
870}
871
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000872STATIC void
873xfs_diflags_to_linux(
874 struct xfs_inode *ip)
875{
David Chinnere4f75292008-08-13 16:00:45 +1000876 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000877 unsigned int xflags = xfs_ip2xflags(ip);
878
879 if (xflags & XFS_XFLAG_IMMUTABLE)
880 inode->i_flags |= S_IMMUTABLE;
881 else
882 inode->i_flags &= ~S_IMMUTABLE;
883 if (xflags & XFS_XFLAG_APPEND)
884 inode->i_flags |= S_APPEND;
885 else
886 inode->i_flags &= ~S_APPEND;
887 if (xflags & XFS_XFLAG_SYNC)
888 inode->i_flags |= S_SYNC;
889 else
890 inode->i_flags &= ~S_SYNC;
891 if (xflags & XFS_XFLAG_NOATIME)
892 inode->i_flags |= S_NOATIME;
893 else
894 inode->i_flags &= ~S_NOATIME;
895}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000896
897#define FSX_PROJID 1
898#define FSX_EXTSIZE 2
899#define FSX_XFLAGS 4
900#define FSX_NONBLOCK 8
901
902STATIC int
903xfs_ioctl_setattr(
904 xfs_inode_t *ip,
905 struct fsxattr *fa,
906 int mask)
907{
908 struct xfs_mount *mp = ip->i_mount;
909 struct xfs_trans *tp;
910 unsigned int lock_flags = 0;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200911 struct xfs_dquot *udqp = NULL;
912 struct xfs_dquot *gdqp = NULL;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000913 struct xfs_dquot *olddquot = NULL;
914 int code;
915
916 xfs_itrace_entry(ip);
917
918 if (mp->m_flags & XFS_MOUNT_RDONLY)
919 return XFS_ERROR(EROFS);
920 if (XFS_FORCED_SHUTDOWN(mp))
921 return XFS_ERROR(EIO);
922
923 /*
924 * If disk quotas is on, we make sure that the dquots do exist on disk,
925 * before we start any other transactions. Trying to do this later
926 * is messy. We don't care to take a readlock to look at the ids
927 * in inode here, because we can't hold it across the trans_reserve.
928 * If the IDs do change before we take the ilock, we're covered
929 * because the i_*dquot fields will get updated anyway.
930 */
931 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200932 code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000933 ip->i_d.di_gid, fa->fsx_projid,
934 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
935 if (code)
936 return code;
937 }
938
939 /*
940 * For the other attributes, we acquire the inode lock and
941 * first do an error checking pass.
942 */
943 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
944 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
945 if (code)
946 goto error_return;
947
948 lock_flags = XFS_ILOCK_EXCL;
949 xfs_ilock(ip, lock_flags);
950
951 /*
952 * CAP_FOWNER overrides the following restrictions:
953 *
954 * The user ID of the calling process must be equal
955 * to the file owner ID, except in cases where the
956 * CAP_FSETID capability is applicable.
957 */
David Howells91b7771252008-10-31 15:50:04 +1100958 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000959 code = XFS_ERROR(EPERM);
960 goto error_return;
961 }
962
963 /*
964 * Do a quota reservation only if projid is actually going to change.
965 */
966 if (mask & FSX_PROJID) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200967 if (XFS_IS_QUOTA_RUNNING(mp) &&
968 XFS_IS_PQUOTA_ON(mp) &&
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000969 ip->i_d.di_projid != fa->fsx_projid) {
970 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200971 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000972 capable(CAP_FOWNER) ?
973 XFS_QMOPT_FORCE_RES : 0);
974 if (code) /* out of quota */
975 goto error_return;
976 }
977 }
978
979 if (mask & FSX_EXTSIZE) {
980 /*
981 * Can't change extent size if any extents are allocated.
982 */
983 if (ip->i_d.di_nextents &&
984 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
985 fa->fsx_extsize)) {
986 code = XFS_ERROR(EINVAL); /* EFBIG? */
987 goto error_return;
988 }
989
990 /*
991 * Extent size must be a multiple of the appropriate block
992 * size, if set at all.
993 */
994 if (fa->fsx_extsize != 0) {
995 xfs_extlen_t size;
996
997 if (XFS_IS_REALTIME_INODE(ip) ||
998 ((mask & FSX_XFLAGS) &&
999 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1000 size = mp->m_sb.sb_rextsize <<
1001 mp->m_sb.sb_blocklog;
1002 } else {
1003 size = mp->m_sb.sb_blocksize;
1004 }
1005
1006 if (fa->fsx_extsize % size) {
1007 code = XFS_ERROR(EINVAL);
1008 goto error_return;
1009 }
1010 }
1011 }
1012
1013
1014 if (mask & FSX_XFLAGS) {
1015 /*
1016 * Can't change realtime flag if any extents are allocated.
1017 */
1018 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1019 (XFS_IS_REALTIME_INODE(ip)) !=
1020 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1021 code = XFS_ERROR(EINVAL); /* EFBIG? */
1022 goto error_return;
1023 }
1024
1025 /*
1026 * If realtime flag is set then must have realtime data.
1027 */
1028 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1029 if ((mp->m_sb.sb_rblocks == 0) ||
1030 (mp->m_sb.sb_rextsize == 0) ||
1031 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1032 code = XFS_ERROR(EINVAL);
1033 goto error_return;
1034 }
1035 }
1036
1037 /*
1038 * Can't modify an immutable/append-only file unless
1039 * we have appropriate permission.
1040 */
1041 if ((ip->i_d.di_flags &
1042 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1043 (fa->fsx_xflags &
1044 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1045 !capable(CAP_LINUX_IMMUTABLE)) {
1046 code = XFS_ERROR(EPERM);
1047 goto error_return;
1048 }
1049 }
1050
1051 xfs_trans_ijoin(tp, ip, lock_flags);
1052 xfs_trans_ihold(tp, ip);
1053
1054 /*
1055 * Change file ownership. Must be the owner or privileged.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001056 */
1057 if (mask & FSX_PROJID) {
1058 /*
1059 * CAP_FSETID overrides the following restrictions:
1060 *
1061 * The set-user-ID and set-group-ID bits of a file will be
1062 * cleared upon successful return from chown()
1063 */
1064 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1065 !capable(CAP_FSETID))
1066 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1067
1068 /*
1069 * Change the ownerships and register quota modifications
1070 * in the transaction.
1071 */
1072 if (ip->i_d.di_projid != fa->fsx_projid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +02001073 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
1074 olddquot = xfs_qm_vop_chown(tp, ip,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001075 &ip->i_gdquot, gdqp);
1076 }
1077 ip->i_d.di_projid = fa->fsx_projid;
1078
1079 /*
1080 * We may have to rev the inode as well as
1081 * the superblock version number since projids didn't
1082 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1083 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001084 if (ip->i_d.di_version == 1)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001085 xfs_bump_ino_vers2(tp, ip);
1086 }
1087
1088 }
1089
1090 if (mask & FSX_EXTSIZE)
1091 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001092 if (mask & FSX_XFLAGS) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001093 xfs_set_diflags(ip, fa->fsx_xflags);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001094 xfs_diflags_to_linux(ip);
1095 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001096
1097 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1098 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1099
1100 XFS_STATS_INC(xs_ig_attrchg);
1101
1102 /*
1103 * If this is a synchronous mount, make sure that the
1104 * transaction goes to disk before returning to the user.
1105 * This is slightly sub-optimal in that truncates require
1106 * two sync transactions instead of one for wsync filesystems.
1107 * One for the truncate and one for the timestamps since we
1108 * don't want to change the timestamps unless we're sure the
1109 * truncate worked. Truncates are less than 1% of the laddis
1110 * mix so this probably isn't worth the trouble to optimize.
1111 */
1112 if (mp->m_flags & XFS_MOUNT_WSYNC)
1113 xfs_trans_set_sync(tp);
1114 code = xfs_trans_commit(tp, 0);
1115 xfs_iunlock(ip, lock_flags);
1116
1117 /*
1118 * Release any dquot(s) the inode had kept before chown.
1119 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001120 xfs_qm_dqrele(olddquot);
1121 xfs_qm_dqrele(udqp);
1122 xfs_qm_dqrele(gdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001123
1124 if (code)
1125 return code;
1126
1127 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1128 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1129 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1130 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1131 }
1132
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001133 return 0;
1134
1135 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001136 xfs_qm_dqrele(udqp);
1137 xfs_qm_dqrele(gdqp);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001138 xfs_trans_cancel(tp, 0);
1139 if (lock_flags)
1140 xfs_iunlock(ip, lock_flags);
1141 return code;
1142}
1143
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001144STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001145xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 xfs_inode_t *ip,
1147 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 void __user *arg)
1149{
1150 struct fsxattr fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001151 unsigned int mask;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001152
1153 if (copy_from_user(&fa, arg, sizeof(fa)))
1154 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001156 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001157 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001158 mask |= FSX_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001160 return -xfs_ioctl_setattr(ip, &fa, mask);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001163STATIC int
1164xfs_ioc_getxflags(
1165 xfs_inode_t *ip,
1166 void __user *arg)
1167{
1168 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001170 flags = xfs_di2lxflags(ip->i_d.di_flags);
1171 if (copy_to_user(arg, &flags, sizeof(flags)))
1172 return -EFAULT;
1173 return 0;
1174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001176STATIC int
1177xfs_ioc_setxflags(
1178 xfs_inode_t *ip,
1179 struct file *filp,
1180 void __user *arg)
1181{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001182 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001183 unsigned int flags;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001184 unsigned int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001186 if (copy_from_user(&flags, arg, sizeof(flags)))
1187 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001189 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1190 FS_NOATIME_FL | FS_NODUMP_FL | \
1191 FS_SYNC_FL))
1192 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001194 mask = FSX_XFLAGS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001195 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001196 mask |= FSX_NONBLOCK;
1197 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001199 return -xfs_ioctl_setattr(ip, &fa, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001203xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1204{
1205 struct getbmap __user *base = *ap;
1206
1207 /* copy only getbmap portion (not getbmapx) */
1208 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1209 return XFS_ERROR(EFAULT);
1210
1211 *ap += sizeof(struct getbmap);
1212 return 0;
1213}
1214
1215STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001217 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int ioflags,
1219 unsigned int cmd,
1220 void __user *arg)
1221{
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001222 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 int error;
1224
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001225 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return -XFS_ERROR(EFAULT);
1227
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001228 if (bmx.bmv_count < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return -XFS_ERROR(EINVAL);
1230
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001231 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (ioflags & IO_INVIS)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001233 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001235 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1236 (struct getbmap *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (error)
1238 return -error;
1239
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001240 /* copy back header - only size of getbmap */
1241 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -XFS_ERROR(EFAULT);
1243 return 0;
1244}
1245
1246STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001247xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1248{
1249 struct getbmapx __user *base = *ap;
1250
1251 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1252 return XFS_ERROR(EFAULT);
1253
1254 *ap += sizeof(struct getbmapx);
1255 return 0;
1256}
1257
1258STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001260 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 void __user *arg)
1262{
1263 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int error;
1265
1266 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1267 return -XFS_ERROR(EFAULT);
1268
1269 if (bmx.bmv_count < 2)
1270 return -XFS_ERROR(EINVAL);
1271
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001272 if (bmx.bmv_iflags & (~BMV_IF_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -XFS_ERROR(EINVAL);
1274
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001275 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1276 (struct getbmapx *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (error)
1278 return -error;
1279
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001280 /* copy back header */
1281 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -XFS_ERROR(EFAULT);
1283
1284 return 0;
1285}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001286
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001287/*
1288 * Note: some of the ioctl's return positive numbers as a
1289 * byte count indicating success, such as readlink_by_handle.
1290 * So we don't "sign flip" like most other routines. This means
1291 * true errors need to be returned as a negative value.
1292 */
1293long
1294xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001295 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001296 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001297 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001298{
1299 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001300 struct xfs_inode *ip = XFS_I(inode);
1301 struct xfs_mount *mp = ip->i_mount;
1302 void __user *arg = (void __user *)p;
1303 int ioflags = 0;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001304 int error;
1305
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001306 if (filp->f_mode & FMODE_NOCMTIME)
1307 ioflags |= IO_INVIS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001308
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001309 xfs_itrace_entry(ip);
1310
1311 switch (cmd) {
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001312 case XFS_IOC_ALLOCSP:
1313 case XFS_IOC_FREESP:
1314 case XFS_IOC_RESVSP:
1315 case XFS_IOC_UNRESVSP:
1316 case XFS_IOC_ALLOCSP64:
1317 case XFS_IOC_FREESP64:
1318 case XFS_IOC_RESVSP64:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001319 case XFS_IOC_UNRESVSP64: {
1320 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001321
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001322 if (copy_from_user(&bf, arg, sizeof(bf)))
1323 return -XFS_ERROR(EFAULT);
1324 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1325 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001326 case XFS_IOC_DIOINFO: {
1327 struct dioattr da;
1328 xfs_buftarg_t *target =
1329 XFS_IS_REALTIME_INODE(ip) ?
1330 mp->m_rtdev_targp : mp->m_ddev_targp;
1331
1332 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1333 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1334
1335 if (copy_to_user(arg, &da, sizeof(da)))
1336 return -XFS_ERROR(EFAULT);
1337 return 0;
1338 }
1339
1340 case XFS_IOC_FSBULKSTAT_SINGLE:
1341 case XFS_IOC_FSBULKSTAT:
1342 case XFS_IOC_FSINUMBERS:
1343 return xfs_ioc_bulkstat(mp, cmd, arg);
1344
1345 case XFS_IOC_FSGEOMETRY_V1:
1346 return xfs_ioc_fsgeometry_v1(mp, arg);
1347
1348 case XFS_IOC_FSGEOMETRY:
1349 return xfs_ioc_fsgeometry(mp, arg);
1350
1351 case XFS_IOC_GETVERSION:
1352 return put_user(inode->i_generation, (int __user *)arg);
1353
1354 case XFS_IOC_FSGETXATTR:
1355 return xfs_ioc_fsgetxattr(ip, 0, arg);
1356 case XFS_IOC_FSGETXATTRA:
1357 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001358 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001359 return xfs_ioc_fssetxattr(ip, filp, arg);
1360 case XFS_IOC_GETXFLAGS:
1361 return xfs_ioc_getxflags(ip, arg);
1362 case XFS_IOC_SETXFLAGS:
1363 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001364
1365 case XFS_IOC_FSSETDM: {
1366 struct fsdmidata dmi;
1367
1368 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1369 return -XFS_ERROR(EFAULT);
1370
1371 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1372 dmi.fsd_dmstate);
1373 return -error;
1374 }
1375
1376 case XFS_IOC_GETBMAP:
1377 case XFS_IOC_GETBMAPA:
1378 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1379
1380 case XFS_IOC_GETBMAPX:
1381 return xfs_ioc_getbmapx(ip, arg);
1382
1383 case XFS_IOC_FD_TO_HANDLE:
1384 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001385 case XFS_IOC_PATH_TO_FSHANDLE: {
1386 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001387
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001388 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1389 return -XFS_ERROR(EFAULT);
1390 return xfs_find_handle(cmd, &hreq);
1391 }
1392 case XFS_IOC_OPEN_BY_HANDLE: {
1393 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001394
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001395 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1396 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001397 return xfs_open_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001398 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001399 case XFS_IOC_FSSETDM_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001400 return xfs_fssetdm_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001401
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001402 case XFS_IOC_READLINK_BY_HANDLE: {
1403 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001404
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001405 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1406 return -XFS_ERROR(EFAULT);
Christoph Hellwigd296d302009-01-19 02:02:57 +01001407 return xfs_readlink_by_handle(filp, &hreq);
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001408 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001409 case XFS_IOC_ATTRLIST_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001410 return xfs_attrlist_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001411
1412 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Christoph Hellwigd296d302009-01-19 02:02:57 +01001413 return xfs_attrmulti_by_handle(filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001414
1415 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001416 struct xfs_swapext sxp;
1417
1418 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1419 return -XFS_ERROR(EFAULT);
1420 error = xfs_swapext(&sxp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001421 return -error;
1422 }
1423
1424 case XFS_IOC_FSCOUNTS: {
1425 xfs_fsop_counts_t out;
1426
1427 error = xfs_fs_counts(mp, &out);
1428 if (error)
1429 return -error;
1430
1431 if (copy_to_user(arg, &out, sizeof(out)))
1432 return -XFS_ERROR(EFAULT);
1433 return 0;
1434 }
1435
1436 case XFS_IOC_SET_RESBLKS: {
1437 xfs_fsop_resblks_t inout;
1438 __uint64_t in;
1439
1440 if (!capable(CAP_SYS_ADMIN))
1441 return -EPERM;
1442
1443 if (copy_from_user(&inout, arg, sizeof(inout)))
1444 return -XFS_ERROR(EFAULT);
1445
1446 /* input parameter is passed in resblks field of structure */
1447 in = inout.resblks;
1448 error = xfs_reserve_blocks(mp, &in, &inout);
1449 if (error)
1450 return -error;
1451
1452 if (copy_to_user(arg, &inout, sizeof(inout)))
1453 return -XFS_ERROR(EFAULT);
1454 return 0;
1455 }
1456
1457 case XFS_IOC_GET_RESBLKS: {
1458 xfs_fsop_resblks_t out;
1459
1460 if (!capable(CAP_SYS_ADMIN))
1461 return -EPERM;
1462
1463 error = xfs_reserve_blocks(mp, NULL, &out);
1464 if (error)
1465 return -error;
1466
1467 if (copy_to_user(arg, &out, sizeof(out)))
1468 return -XFS_ERROR(EFAULT);
1469
1470 return 0;
1471 }
1472
1473 case XFS_IOC_FSGROWFSDATA: {
1474 xfs_growfs_data_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_data(mp, &in);
1480 return -error;
1481 }
1482
1483 case XFS_IOC_FSGROWFSLOG: {
1484 xfs_growfs_log_t in;
1485
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001486 if (copy_from_user(&in, arg, sizeof(in)))
1487 return -XFS_ERROR(EFAULT);
1488
1489 error = xfs_growfs_log(mp, &in);
1490 return -error;
1491 }
1492
1493 case XFS_IOC_FSGROWFSRT: {
1494 xfs_growfs_rt_t in;
1495
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001496 if (copy_from_user(&in, arg, sizeof(in)))
1497 return -XFS_ERROR(EFAULT);
1498
1499 error = xfs_growfs_rt(mp, &in);
1500 return -error;
1501 }
1502
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001503 case XFS_IOC_GOINGDOWN: {
1504 __uint32_t in;
1505
1506 if (!capable(CAP_SYS_ADMIN))
1507 return -EPERM;
1508
1509 if (get_user(in, (__uint32_t __user *)arg))
1510 return -XFS_ERROR(EFAULT);
1511
1512 error = xfs_fs_goingdown(mp, in);
1513 return -error;
1514 }
1515
1516 case XFS_IOC_ERROR_INJECTION: {
1517 xfs_error_injection_t in;
1518
1519 if (!capable(CAP_SYS_ADMIN))
1520 return -EPERM;
1521
1522 if (copy_from_user(&in, arg, sizeof(in)))
1523 return -XFS_ERROR(EFAULT);
1524
1525 error = xfs_errortag_add(in.errtag, mp);
1526 return -error;
1527 }
1528
1529 case XFS_IOC_ERROR_CLEARALL:
1530 if (!capable(CAP_SYS_ADMIN))
1531 return -EPERM;
1532
1533 error = xfs_errortag_clearall(mp, 1);
1534 return -error;
1535
1536 default:
1537 return -ENOTTY;
1538 }
1539}