blob: 67205f6198ba58019eb549ffc6440e56b84af8b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_rw.h"
43#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_attr.h"
Nathan Scotta844f452005-11-02 14:38:42 +110045#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_buf_item.h"
47#include "xfs_utils.h"
48#include "xfs_dfrag.h"
49#include "xfs_fsops.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100050#include "xfs_vnodeops.h"
Christoph Hellwig25fe55e2008-07-18 17:13:20 +100051#include "xfs_quota.h"
52#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080054#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/dcache.h>
56#include <linux/mount.h>
57#include <linux/namei.h>
58#include <linux/pagemap.h>
59
60/*
61 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
62 * a file or fs handle.
63 *
64 * XFS_IOC_PATH_TO_FSHANDLE
65 * returns fs handle for a mount point or path within that mount point
66 * XFS_IOC_FD_TO_HANDLE
67 * returns full handle for a FD opened in user space
68 * XFS_IOC_PATH_TO_HANDLE
69 * returns full handle for a path
70 */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060071int
Linus Torvalds1da177e2005-04-16 15:20:36 -070072xfs_find_handle(
73 unsigned int cmd,
sandeen@sandeen.net743bb462008-11-25 21:20:06 -060074 xfs_fsop_handlereq_t *hreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 int hsize;
77 xfs_handle_t handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 memset((char *)&handle, 0, sizeof(handle));
81
82 switch (cmd) {
83 case XFS_IOC_PATH_TO_FSHANDLE:
84 case XFS_IOC_PATH_TO_HANDLE: {
Al Viro2d8f3032008-07-22 09:59:21 -040085 struct path path;
sandeen@sandeen.net743bb462008-11-25 21:20:06 -060086 int error = user_lpath((const char __user *)hreq->path, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (error)
88 return error;
89
Al Viro2d8f3032008-07-22 09:59:21 -040090 ASSERT(path.dentry);
91 ASSERT(path.dentry->d_inode);
92 inode = igrab(path.dentry->d_inode);
93 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 break;
95 }
96
97 case XFS_IOC_FD_TO_HANDLE: {
98 struct file *file;
99
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600100 file = fget(hreq->fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!file)
102 return -EBADF;
103
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800104 ASSERT(file->f_path.dentry);
105 ASSERT(file->f_path.dentry->d_inode);
106 inode = igrab(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 fput(file);
108 break;
109 }
110
111 default:
112 ASSERT(0);
113 return -XFS_ERROR(EINVAL);
114 }
115
116 if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
117 /* we're not in XFS anymore, Toto */
118 iput(inode);
119 return -XFS_ERROR(EINVAL);
120 }
121
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000122 switch (inode->i_mode & S_IFMT) {
123 case S_IFREG:
124 case S_IFDIR:
125 case S_IFLNK:
126 break;
127 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 iput(inode);
129 return -XFS_ERROR(EBADF);
130 }
131
132 /* now we can grab the fsid */
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +1000133 memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000134 sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 hsize = sizeof(xfs_fsid_t);
136
137 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000138 xfs_inode_t *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int lock_mode;
140
141 /* need to get access to the xfs_inode to read the generation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 lock_mode = xfs_ilock_map_shared(ip);
143
144 /* fill in fid section of handle from inode */
Christoph Hellwigc6143912007-09-14 15:22:37 +1000145 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
146 sizeof(handle.ha_fid.fid_len);
147 handle.ha_fid.fid_pad = 0;
148 handle.ha_fid.fid_gen = ip->i_d.di_gen;
149 handle.ha_fid.fid_ino = ip->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 xfs_iunlock_map_shared(ip, lock_mode);
152
153 hsize = XFS_HSIZE(handle);
154 }
155
156 /* now copy our handle into the user buffer & write out the size */
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600157 if (copy_to_user(hreq->ohandle, &handle, hsize) ||
158 copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 iput(inode);
160 return -XFS_ERROR(EFAULT);
161 }
162
163 iput(inode);
164 return 0;
165}
166
167
168/*
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000169 * Convert userspace handle data into inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000171 * We use the fact that all the fsop_handlereq ioctl calls have a data
172 * structure argument whose first component is always a xfs_fsop_handlereq_t,
173 * so we can pass that sub structure into this handy, shared routine.
174 *
175 * If no error, caller must always iput the returned inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177STATIC int
178xfs_vget_fsop_handlereq(
179 xfs_mount_t *mp,
180 struct inode *parinode, /* parent inode pointer */
181 xfs_fsop_handlereq_t *hreq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct inode **inode)
183{
184 void __user *hanp;
185 size_t hlen;
186 xfs_fid_t *xfid;
187 xfs_handle_t *handlep;
188 xfs_handle_t handle;
189 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 xfs_ino_t ino;
191 __u32 igen;
192 int error;
193
194 /*
195 * Only allow handle opens under a directory.
196 */
197 if (!S_ISDIR(parinode->i_mode))
198 return XFS_ERROR(ENOTDIR);
199
200 hanp = hreq->ihandle;
201 hlen = hreq->ihandlen;
202 handlep = &handle;
203
204 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
205 return XFS_ERROR(EINVAL);
206 if (copy_from_user(handlep, hanp, hlen))
207 return XFS_ERROR(EFAULT);
208 if (hlen < sizeof(*handlep))
209 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
210 if (hlen > sizeof(handlep->ha_fsid)) {
Christoph Hellwigc6143912007-09-14 15:22:37 +1000211 if (handlep->ha_fid.fid_len !=
212 (hlen - sizeof(handlep->ha_fsid) -
213 sizeof(handlep->ha_fid.fid_len)) ||
214 handlep->ha_fid.fid_pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return XFS_ERROR(EINVAL);
216 }
217
218 /*
219 * Crack the handle, obtain the inode # & generation #
220 */
221 xfid = (struct xfs_fid *)&handlep->ha_fid;
Christoph Hellwigc6143912007-09-14 15:22:37 +1000222 if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
223 ino = xfid->fid_ino;
224 igen = xfid->fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 } else {
226 return XFS_ERROR(EINVAL);
227 }
228
229 /*
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000230 * Get the XFS inode, building a Linux inode to go with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
232 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
233 if (error)
234 return error;
235 if (ip == NULL)
236 return XFS_ERROR(EIO);
Christoph Hellwig6a7f4222008-04-22 17:33:40 +1000237 if (ip->i_d.di_gen != igen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 xfs_iput_new(ip, XFS_ILOCK_SHARED);
239 return XFS_ERROR(ENOENT);
240 }
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfs_iunlock(ip, XFS_ILOCK_SHARED);
243
David Chinnere4f75292008-08-13 16:00:45 +1000244 *inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600248int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249xfs_open_by_handle(
250 xfs_mount_t *mp,
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600251 xfs_fsop_handlereq_t *hreq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct file *parfilp,
253 struct inode *parinode)
254{
David Howells745ca242008-11-14 10:39:22 +1100255 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int error;
257 int new_fd;
258 int permflag;
259 struct file *filp;
260 struct inode *inode;
261 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (!capable(CAP_SYS_ADMIN))
264 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600266 error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (error)
268 return -error;
269
270 /* Restrict xfs_open_by_handle to directories & regular files. */
271 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
272 iput(inode);
273 return -XFS_ERROR(EINVAL);
274 }
275
276#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600277 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#endif
279 /* Put open permission in namei format. */
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600280 permflag = hreq->oflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if ((permflag+1) & O_ACCMODE)
282 permflag++;
283 if (permflag & O_TRUNC)
284 permflag |= 2;
285
286 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
287 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
288 iput(inode);
289 return -XFS_ERROR(EPERM);
290 }
291
292 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
293 iput(inode);
294 return -XFS_ERROR(EACCES);
295 }
296
297 /* Can't write directories. */
298 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
299 iput(inode);
300 return -XFS_ERROR(EISDIR);
301 }
302
303 if ((new_fd = get_unused_fd()) < 0) {
304 iput(inode);
305 return new_fd;
306 }
307
Christoph Hellwig44003722008-08-11 15:49:04 +0200308 dentry = d_obtain_alias(inode);
309 if (IS_ERR(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 put_unused_fd(new_fd);
Christoph Hellwig44003722008-08-11 15:49:04 +0200311 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 /* Ensure umount returns EBUSY on umounts while this file is open. */
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800315 mntget(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Create file pointer. */
Lachlan McIlroy0a8c5392008-12-29 16:47:18 +1100318 filp = dentry_open(dentry, parfilp->f_path.mnt, hreq->oflags, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (IS_ERR(filp)) {
320 put_unused_fd(new_fd);
321 return -XFS_ERROR(-PTR_ERR(filp));
322 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500323
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100324 if (inode->i_mode & S_IFREG) {
325 /* invisible operation should not change atime */
326 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500327 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 fd_install(new_fd, filp);
331 return new_fd;
332}
333
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000334/*
335 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
336 * unused first argument.
337 */
338STATIC int
339do_readlink(
340 char __user *buffer,
341 int buflen,
342 const char *link)
343{
344 int len;
345
346 len = PTR_ERR(link);
347 if (IS_ERR(link))
348 goto out;
349
350 len = strlen(link);
351 if (len > (unsigned) buflen)
352 len = buflen;
353 if (copy_to_user(buffer, link, len))
354 len = -EFAULT;
355 out:
356 return len;
357}
358
359
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600360int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361xfs_readlink_by_handle(
362 xfs_mount_t *mp,
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600363 xfs_fsop_handlereq_t *hreq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct inode *parinode)
365{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000368 void *link;
369 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (!capable(CAP_SYS_ADMIN))
372 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600374 error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (error)
376 return -error;
377
378 /* Restrict this handle operation to symlinks only. */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000379 if (!S_ISLNK(inode->i_mode)) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000380 error = -XFS_ERROR(EINVAL);
381 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600384 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000385 error = -XFS_ERROR(EFAULT);
386 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000389 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
390 if (!link)
391 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000393 error = -xfs_readlink(XFS_I(inode), link);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000394 if (error)
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000395 goto out_kfree;
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600396 error = do_readlink(hreq->ohandle, olen, link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000397 if (error)
398 goto out_kfree;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000399
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000400 out_kfree:
401 kfree(link);
402 out_iput:
403 iput(inode);
404 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407STATIC int
408xfs_fssetdm_by_handle(
409 xfs_mount_t *mp,
410 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct inode *parinode)
412{
413 int error;
414 struct fsdmidata fsd;
415 xfs_fsop_setdm_handlereq_t dmhreq;
416 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (!capable(CAP_MKNOD))
419 return -XFS_ERROR(EPERM);
420 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
421 return -XFS_ERROR(EFAULT);
422
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000423 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (error)
425 return -error;
426
427 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000428 error = -XFS_ERROR(EPERM);
429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000433 error = -XFS_ERROR(EFAULT);
434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000437 error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
438 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000440 out:
441 iput(inode);
442 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445STATIC int
446xfs_attrlist_by_handle(
447 xfs_mount_t *mp,
448 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct inode *parinode)
450{
451 int error;
452 attrlist_cursor_kern_t *cursor;
453 xfs_fsop_attrlist_handlereq_t al_hreq;
454 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 char *kbuf;
456
457 if (!capable(CAP_SYS_ADMIN))
458 return -XFS_ERROR(EPERM);
459 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
460 return -XFS_ERROR(EFAULT);
461 if (al_hreq.buflen > XATTR_LIST_MAX)
462 return -XFS_ERROR(EINVAL);
463
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000464 /*
465 * Reject flags, only allow namespaces.
466 */
467 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
468 return -XFS_ERROR(EINVAL);
469
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000470 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (error)
472 goto out;
473
474 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
475 if (!kbuf)
476 goto out_vn_rele;
477
478 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000479 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
480 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (error)
482 goto out_kfree;
483
484 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
485 error = -EFAULT;
486
487 out_kfree:
488 kfree(kbuf);
489 out_vn_rele:
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000490 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 out:
492 return -error;
493}
494
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600495int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000497 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 char *name,
499 char __user *ubuf,
500 __uint32_t *len,
501 __uint32_t flags)
502{
503 char *kbuf;
504 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (*len > XATTR_SIZE_MAX)
507 return EINVAL;
508 kbuf = kmalloc(*len, GFP_KERNEL);
509 if (!kbuf)
510 return ENOMEM;
511
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000512 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (error)
514 goto out_kfree;
515
516 if (copy_to_user(ubuf, kbuf, *len))
517 error = EFAULT;
518
519 out_kfree:
520 kfree(kbuf);
521 return error;
522}
523
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600524int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000526 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 char *name,
528 const char __user *ubuf,
529 __uint32_t len,
530 __uint32_t flags)
531{
532 char *kbuf;
533 int error = EFAULT;
534
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000535 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return EPERM;
537 if (len > XATTR_SIZE_MAX)
538 return EINVAL;
539
540 kbuf = kmalloc(len, GFP_KERNEL);
541 if (!kbuf)
542 return ENOMEM;
543
544 if (copy_from_user(kbuf, ubuf, len))
545 goto out_kfree;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000546
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000547 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 out_kfree:
550 kfree(kbuf);
551 return error;
552}
553
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600554int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000556 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 char *name,
558 __uint32_t flags)
559{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000560 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000562 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565STATIC int
566xfs_attrmulti_by_handle(
567 xfs_mount_t *mp,
568 void __user *arg,
Dave Hansen42a74f22008-02-15 14:37:46 -0800569 struct file *parfilp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct inode *parinode)
571{
572 int error;
573 xfs_attr_multiop_t *ops;
574 xfs_fsop_attrmulti_handlereq_t am_hreq;
575 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 unsigned int i, size;
577 char *attr_name;
578
579 if (!capable(CAP_SYS_ADMIN))
580 return -XFS_ERROR(EPERM);
581 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
582 return -XFS_ERROR(EFAULT);
583
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000584 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (error)
586 goto out;
587
588 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000589 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!size || size > 16 * PAGE_SIZE)
591 goto out_vn_rele;
592
593 error = ENOMEM;
594 ops = kmalloc(size, GFP_KERNEL);
595 if (!ops)
596 goto out_vn_rele;
597
598 error = EFAULT;
599 if (copy_from_user(ops, am_hreq.ops, size))
600 goto out_kfree_ops;
601
602 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
603 if (!attr_name)
604 goto out_kfree_ops;
605
606
607 error = 0;
608 for (i = 0; i < am_hreq.opcount; i++) {
609 ops[i].am_error = strncpy_from_user(attr_name,
610 ops[i].am_attrname, MAXNAMELEN);
611 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
612 error = -ERANGE;
613 if (ops[i].am_error < 0)
614 break;
615
616 switch (ops[i].am_opcode) {
617 case ATTR_OP_GET:
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000618 ops[i].am_error = xfs_attrmulti_attr_get(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 attr_name, ops[i].am_attrvalue,
620 &ops[i].am_length, ops[i].am_flags);
621 break;
622 case ATTR_OP_SET:
Dave Hansen42a74f22008-02-15 14:37:46 -0800623 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
624 if (ops[i].am_error)
625 break;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000626 ops[i].am_error = xfs_attrmulti_attr_set(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 attr_name, ops[i].am_attrvalue,
628 ops[i].am_length, ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800629 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 break;
631 case ATTR_OP_REMOVE:
Dave Hansen42a74f22008-02-15 14:37:46 -0800632 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
633 if (ops[i].am_error)
634 break;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000635 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 attr_name, ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800637 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639 default:
640 ops[i].am_error = EINVAL;
641 }
642 }
643
644 if (copy_to_user(am_hreq.ops, ops, size))
645 error = XFS_ERROR(EFAULT);
646
647 kfree(attr_name);
648 out_kfree_ops:
649 kfree(ops);
650 out_vn_rele:
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000651 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 out:
653 return -error;
654}
655
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600656int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000658 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000659 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct file *filp,
661 int ioflags,
662 unsigned int cmd,
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600663 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int attr_flags = 0;
666 int error;
667
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600668 /*
669 * Only allow the sys admin to reserve space unless
670 * unwritten extents are enabled.
671 */
672 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
673 !capable(CAP_SYS_ADMIN))
674 return -XFS_ERROR(EPERM);
675
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000676 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -XFS_ERROR(EPERM);
678
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000679 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return -XFS_ERROR(EBADF);
681
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000682 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -XFS_ERROR(EINVAL);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000686 attr_flags |= XFS_ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (ioflags & IO_INVIS)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000688 attr_flags |= XFS_ATTR_DMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
sandeen@sandeen.net743bb462008-11-25 21:20:06 -0600690 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -error;
692}
693
694STATIC int
695xfs_ioc_bulkstat(
696 xfs_mount_t *mp,
697 unsigned int cmd,
698 void __user *arg)
699{
700 xfs_fsop_bulkreq_t bulkreq;
701 int count; /* # of records returned */
702 xfs_ino_t inlast; /* last inode number */
703 int done;
704 int error;
705
706 /* done = 1 if there are more stats to get and if bulkstat */
707 /* should be called again (unused here, but used in dmapi) */
708
709 if (!capable(CAP_SYS_ADMIN))
710 return -EPERM;
711
712 if (XFS_FORCED_SHUTDOWN(mp))
713 return -XFS_ERROR(EIO);
714
715 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
716 return -XFS_ERROR(EFAULT);
717
718 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
719 return -XFS_ERROR(EFAULT);
720
721 if ((count = bulkreq.icount) <= 0)
722 return -XFS_ERROR(EINVAL);
723
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100724 if (bulkreq.ubuffer == NULL)
725 return -XFS_ERROR(EINVAL);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (cmd == XFS_IOC_FSINUMBERS)
728 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000729 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
731 error = xfs_bulkstat_single(mp, &inlast,
732 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100733 else /* XFS_IOC_FSBULKSTAT */
734 error = xfs_bulkstat(mp, &inlast, &count,
735 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
736 sizeof(xfs_bstat_t), bulkreq.ubuffer,
737 BULKSTAT_FG_QUICK, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (error)
740 return -error;
741
742 if (bulkreq.ocount != NULL) {
743 if (copy_to_user(bulkreq.lastip, &inlast,
744 sizeof(xfs_ino_t)))
745 return -XFS_ERROR(EFAULT);
746
747 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
748 return -XFS_ERROR(EFAULT);
749 }
750
751 return 0;
752}
753
754STATIC int
755xfs_ioc_fsgeometry_v1(
756 xfs_mount_t *mp,
757 void __user *arg)
758{
759 xfs_fsop_geom_v1_t fsgeo;
760 int error;
761
762 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
763 if (error)
764 return -error;
765
766 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
767 return -XFS_ERROR(EFAULT);
768 return 0;
769}
770
771STATIC int
772xfs_ioc_fsgeometry(
773 xfs_mount_t *mp,
774 void __user *arg)
775{
776 xfs_fsop_geom_t fsgeo;
777 int error;
778
779 error = xfs_fs_geometry(mp, &fsgeo, 4);
780 if (error)
781 return -error;
782
783 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
784 return -XFS_ERROR(EFAULT);
785 return 0;
786}
787
788/*
789 * Linux extended inode flags interface.
790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792STATIC unsigned int
793xfs_merge_ioc_xflags(
794 unsigned int flags,
795 unsigned int start)
796{
797 unsigned int xflags = start;
798
Eric Sandeen39058a02007-02-10 18:37:10 +1100799 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 xflags |= XFS_XFLAG_IMMUTABLE;
801 else
802 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100803 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 xflags |= XFS_XFLAG_APPEND;
805 else
806 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100807 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xflags |= XFS_XFLAG_SYNC;
809 else
810 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100811 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 xflags |= XFS_XFLAG_NOATIME;
813 else
814 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100815 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 xflags |= XFS_XFLAG_NODUMP;
817 else
818 xflags &= ~XFS_XFLAG_NODUMP;
819
820 return xflags;
821}
822
823STATIC unsigned int
824xfs_di2lxflags(
825 __uint16_t di_flags)
826{
827 unsigned int flags = 0;
828
829 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100830 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100832 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100834 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100836 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100838 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return flags;
840}
841
842STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000843xfs_ioc_fsgetxattr(
844 xfs_inode_t *ip,
845 int attr,
846 void __user *arg)
847{
848 struct fsxattr fa;
849
850 xfs_ilock(ip, XFS_ILOCK_SHARED);
851 fa.fsx_xflags = xfs_ip2xflags(ip);
852 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
853 fa.fsx_projid = ip->i_d.di_projid;
854
855 if (attr) {
856 if (ip->i_afp) {
857 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
858 fa.fsx_nextents = ip->i_afp->if_bytes /
859 sizeof(xfs_bmbt_rec_t);
860 else
861 fa.fsx_nextents = ip->i_d.di_anextents;
862 } else
863 fa.fsx_nextents = 0;
864 } else {
865 if (ip->i_df.if_flags & XFS_IFEXTENTS)
866 fa.fsx_nextents = ip->i_df.if_bytes /
867 sizeof(xfs_bmbt_rec_t);
868 else
869 fa.fsx_nextents = ip->i_d.di_nextents;
870 }
871 xfs_iunlock(ip, XFS_ILOCK_SHARED);
872
873 if (copy_to_user(arg, &fa, sizeof(fa)))
874 return -EFAULT;
875 return 0;
876}
877
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000878STATIC void
879xfs_set_diflags(
880 struct xfs_inode *ip,
881 unsigned int xflags)
882{
883 unsigned int di_flags;
884
885 /* can't set PREALLOC this way, just preserve it */
886 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
887 if (xflags & XFS_XFLAG_IMMUTABLE)
888 di_flags |= XFS_DIFLAG_IMMUTABLE;
889 if (xflags & XFS_XFLAG_APPEND)
890 di_flags |= XFS_DIFLAG_APPEND;
891 if (xflags & XFS_XFLAG_SYNC)
892 di_flags |= XFS_DIFLAG_SYNC;
893 if (xflags & XFS_XFLAG_NOATIME)
894 di_flags |= XFS_DIFLAG_NOATIME;
895 if (xflags & XFS_XFLAG_NODUMP)
896 di_flags |= XFS_DIFLAG_NODUMP;
897 if (xflags & XFS_XFLAG_PROJINHERIT)
898 di_flags |= XFS_DIFLAG_PROJINHERIT;
899 if (xflags & XFS_XFLAG_NODEFRAG)
900 di_flags |= XFS_DIFLAG_NODEFRAG;
901 if (xflags & XFS_XFLAG_FILESTREAM)
902 di_flags |= XFS_DIFLAG_FILESTREAM;
903 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
904 if (xflags & XFS_XFLAG_RTINHERIT)
905 di_flags |= XFS_DIFLAG_RTINHERIT;
906 if (xflags & XFS_XFLAG_NOSYMLINKS)
907 di_flags |= XFS_DIFLAG_NOSYMLINKS;
908 if (xflags & XFS_XFLAG_EXTSZINHERIT)
909 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
910 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
911 if (xflags & XFS_XFLAG_REALTIME)
912 di_flags |= XFS_DIFLAG_REALTIME;
913 if (xflags & XFS_XFLAG_EXTSIZE)
914 di_flags |= XFS_DIFLAG_EXTSIZE;
915 }
916
917 ip->i_d.di_flags = di_flags;
918}
919
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000920STATIC void
921xfs_diflags_to_linux(
922 struct xfs_inode *ip)
923{
David Chinnere4f75292008-08-13 16:00:45 +1000924 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000925 unsigned int xflags = xfs_ip2xflags(ip);
926
927 if (xflags & XFS_XFLAG_IMMUTABLE)
928 inode->i_flags |= S_IMMUTABLE;
929 else
930 inode->i_flags &= ~S_IMMUTABLE;
931 if (xflags & XFS_XFLAG_APPEND)
932 inode->i_flags |= S_APPEND;
933 else
934 inode->i_flags &= ~S_APPEND;
935 if (xflags & XFS_XFLAG_SYNC)
936 inode->i_flags |= S_SYNC;
937 else
938 inode->i_flags &= ~S_SYNC;
939 if (xflags & XFS_XFLAG_NOATIME)
940 inode->i_flags |= S_NOATIME;
941 else
942 inode->i_flags &= ~S_NOATIME;
943}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000944
945#define FSX_PROJID 1
946#define FSX_EXTSIZE 2
947#define FSX_XFLAGS 4
948#define FSX_NONBLOCK 8
949
950STATIC int
951xfs_ioctl_setattr(
952 xfs_inode_t *ip,
953 struct fsxattr *fa,
954 int mask)
955{
956 struct xfs_mount *mp = ip->i_mount;
957 struct xfs_trans *tp;
958 unsigned int lock_flags = 0;
959 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
960 struct xfs_dquot *olddquot = NULL;
961 int code;
962
963 xfs_itrace_entry(ip);
964
965 if (mp->m_flags & XFS_MOUNT_RDONLY)
966 return XFS_ERROR(EROFS);
967 if (XFS_FORCED_SHUTDOWN(mp))
968 return XFS_ERROR(EIO);
969
970 /*
971 * If disk quotas is on, we make sure that the dquots do exist on disk,
972 * before we start any other transactions. Trying to do this later
973 * is messy. We don't care to take a readlock to look at the ids
974 * in inode here, because we can't hold it across the trans_reserve.
975 * If the IDs do change before we take the ilock, we're covered
976 * because the i_*dquot fields will get updated anyway.
977 */
978 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
979 code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid,
980 ip->i_d.di_gid, fa->fsx_projid,
981 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
982 if (code)
983 return code;
984 }
985
986 /*
987 * For the other attributes, we acquire the inode lock and
988 * first do an error checking pass.
989 */
990 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
991 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
992 if (code)
993 goto error_return;
994
995 lock_flags = XFS_ILOCK_EXCL;
996 xfs_ilock(ip, lock_flags);
997
998 /*
999 * CAP_FOWNER overrides the following restrictions:
1000 *
1001 * The user ID of the calling process must be equal
1002 * to the file owner ID, except in cases where the
1003 * CAP_FSETID capability is applicable.
1004 */
David Howells91b77712008-10-31 15:50:04 +11001005 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001006 code = XFS_ERROR(EPERM);
1007 goto error_return;
1008 }
1009
1010 /*
1011 * Do a quota reservation only if projid is actually going to change.
1012 */
1013 if (mask & FSX_PROJID) {
1014 if (XFS_IS_PQUOTA_ON(mp) &&
1015 ip->i_d.di_projid != fa->fsx_projid) {
1016 ASSERT(tp);
1017 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
1018 capable(CAP_FOWNER) ?
1019 XFS_QMOPT_FORCE_RES : 0);
1020 if (code) /* out of quota */
1021 goto error_return;
1022 }
1023 }
1024
1025 if (mask & FSX_EXTSIZE) {
1026 /*
1027 * Can't change extent size if any extents are allocated.
1028 */
1029 if (ip->i_d.di_nextents &&
1030 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1031 fa->fsx_extsize)) {
1032 code = XFS_ERROR(EINVAL); /* EFBIG? */
1033 goto error_return;
1034 }
1035
1036 /*
1037 * Extent size must be a multiple of the appropriate block
1038 * size, if set at all.
1039 */
1040 if (fa->fsx_extsize != 0) {
1041 xfs_extlen_t size;
1042
1043 if (XFS_IS_REALTIME_INODE(ip) ||
1044 ((mask & FSX_XFLAGS) &&
1045 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1046 size = mp->m_sb.sb_rextsize <<
1047 mp->m_sb.sb_blocklog;
1048 } else {
1049 size = mp->m_sb.sb_blocksize;
1050 }
1051
1052 if (fa->fsx_extsize % size) {
1053 code = XFS_ERROR(EINVAL);
1054 goto error_return;
1055 }
1056 }
1057 }
1058
1059
1060 if (mask & FSX_XFLAGS) {
1061 /*
1062 * Can't change realtime flag if any extents are allocated.
1063 */
1064 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1065 (XFS_IS_REALTIME_INODE(ip)) !=
1066 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1067 code = XFS_ERROR(EINVAL); /* EFBIG? */
1068 goto error_return;
1069 }
1070
1071 /*
1072 * If realtime flag is set then must have realtime data.
1073 */
1074 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1075 if ((mp->m_sb.sb_rblocks == 0) ||
1076 (mp->m_sb.sb_rextsize == 0) ||
1077 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1078 code = XFS_ERROR(EINVAL);
1079 goto error_return;
1080 }
1081 }
1082
1083 /*
1084 * Can't modify an immutable/append-only file unless
1085 * we have appropriate permission.
1086 */
1087 if ((ip->i_d.di_flags &
1088 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1089 (fa->fsx_xflags &
1090 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1091 !capable(CAP_LINUX_IMMUTABLE)) {
1092 code = XFS_ERROR(EPERM);
1093 goto error_return;
1094 }
1095 }
1096
1097 xfs_trans_ijoin(tp, ip, lock_flags);
1098 xfs_trans_ihold(tp, ip);
1099
1100 /*
1101 * Change file ownership. Must be the owner or privileged.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001102 */
1103 if (mask & FSX_PROJID) {
1104 /*
1105 * CAP_FSETID overrides the following restrictions:
1106 *
1107 * The set-user-ID and set-group-ID bits of a file will be
1108 * cleared upon successful return from chown()
1109 */
1110 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1111 !capable(CAP_FSETID))
1112 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1113
1114 /*
1115 * Change the ownerships and register quota modifications
1116 * in the transaction.
1117 */
1118 if (ip->i_d.di_projid != fa->fsx_projid) {
1119 if (XFS_IS_PQUOTA_ON(mp)) {
1120 olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip,
1121 &ip->i_gdquot, gdqp);
1122 }
1123 ip->i_d.di_projid = fa->fsx_projid;
1124
1125 /*
1126 * We may have to rev the inode as well as
1127 * the superblock version number since projids didn't
1128 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1129 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001130 if (ip->i_d.di_version == 1)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001131 xfs_bump_ino_vers2(tp, ip);
1132 }
1133
1134 }
1135
1136 if (mask & FSX_EXTSIZE)
1137 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001138 if (mask & FSX_XFLAGS) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001139 xfs_set_diflags(ip, fa->fsx_xflags);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001140 xfs_diflags_to_linux(ip);
1141 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001142
1143 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1144 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1145
1146 XFS_STATS_INC(xs_ig_attrchg);
1147
1148 /*
1149 * If this is a synchronous mount, make sure that the
1150 * transaction goes to disk before returning to the user.
1151 * This is slightly sub-optimal in that truncates require
1152 * two sync transactions instead of one for wsync filesystems.
1153 * One for the truncate and one for the timestamps since we
1154 * don't want to change the timestamps unless we're sure the
1155 * truncate worked. Truncates are less than 1% of the laddis
1156 * mix so this probably isn't worth the trouble to optimize.
1157 */
1158 if (mp->m_flags & XFS_MOUNT_WSYNC)
1159 xfs_trans_set_sync(tp);
1160 code = xfs_trans_commit(tp, 0);
1161 xfs_iunlock(ip, lock_flags);
1162
1163 /*
1164 * Release any dquot(s) the inode had kept before chown.
1165 */
1166 XFS_QM_DQRELE(mp, olddquot);
1167 XFS_QM_DQRELE(mp, udqp);
1168 XFS_QM_DQRELE(mp, gdqp);
1169
1170 if (code)
1171 return code;
1172
1173 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1174 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1175 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1176 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1177 }
1178
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001179 return 0;
1180
1181 error_return:
1182 XFS_QM_DQRELE(mp, udqp);
1183 XFS_QM_DQRELE(mp, gdqp);
1184 xfs_trans_cancel(tp, 0);
1185 if (lock_flags)
1186 xfs_iunlock(ip, lock_flags);
1187 return code;
1188}
1189
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001190STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001191xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 xfs_inode_t *ip,
1193 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 void __user *arg)
1195{
1196 struct fsxattr fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001197 unsigned int mask;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001198
1199 if (copy_from_user(&fa, arg, sizeof(fa)))
1200 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001202 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001203 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001204 mask |= FSX_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001206 return -xfs_ioctl_setattr(ip, &fa, mask);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001207}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001209STATIC int
1210xfs_ioc_getxflags(
1211 xfs_inode_t *ip,
1212 void __user *arg)
1213{
1214 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001216 flags = xfs_di2lxflags(ip->i_d.di_flags);
1217 if (copy_to_user(arg, &flags, sizeof(flags)))
1218 return -EFAULT;
1219 return 0;
1220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001222STATIC int
1223xfs_ioc_setxflags(
1224 xfs_inode_t *ip,
1225 struct file *filp,
1226 void __user *arg)
1227{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001228 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001229 unsigned int flags;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001230 unsigned int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001232 if (copy_from_user(&flags, arg, sizeof(flags)))
1233 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001235 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1236 FS_NOATIME_FL | FS_NODUMP_FL | \
1237 FS_SYNC_FL))
1238 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001240 mask = FSX_XFLAGS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001241 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001242 mask |= FSX_NONBLOCK;
1243 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001245 return -xfs_ioctl_setattr(ip, &fa, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001249xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1250{
1251 struct getbmap __user *base = *ap;
1252
1253 /* copy only getbmap portion (not getbmapx) */
1254 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1255 return XFS_ERROR(EFAULT);
1256
1257 *ap += sizeof(struct getbmap);
1258 return 0;
1259}
1260
1261STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001263 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int ioflags,
1265 unsigned int cmd,
1266 void __user *arg)
1267{
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001268 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int error;
1270
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001271 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -XFS_ERROR(EFAULT);
1273
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001274 if (bmx.bmv_count < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -XFS_ERROR(EINVAL);
1276
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001277 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (ioflags & IO_INVIS)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001279 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001281 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1282 (struct getbmap *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (error)
1284 return -error;
1285
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001286 /* copy back header - only size of getbmap */
1287 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -XFS_ERROR(EFAULT);
1289 return 0;
1290}
1291
1292STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001293xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1294{
1295 struct getbmapx __user *base = *ap;
1296
1297 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1298 return XFS_ERROR(EFAULT);
1299
1300 *ap += sizeof(struct getbmapx);
1301 return 0;
1302}
1303
1304STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001306 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 void __user *arg)
1308{
1309 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int error;
1311
1312 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1313 return -XFS_ERROR(EFAULT);
1314
1315 if (bmx.bmv_count < 2)
1316 return -XFS_ERROR(EINVAL);
1317
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001318 if (bmx.bmv_iflags & (~BMV_IF_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -XFS_ERROR(EINVAL);
1320
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001321 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1322 (struct getbmapx *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (error)
1324 return -error;
1325
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001326 /* copy back header */
1327 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -XFS_ERROR(EFAULT);
1329
1330 return 0;
1331}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001332
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001333/*
1334 * Note: some of the ioctl's return positive numbers as a
1335 * byte count indicating success, such as readlink_by_handle.
1336 * So we don't "sign flip" like most other routines. This means
1337 * true errors need to be returned as a negative value.
1338 */
1339long
1340xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001341 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001342 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001343 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001344{
1345 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001346 struct xfs_inode *ip = XFS_I(inode);
1347 struct xfs_mount *mp = ip->i_mount;
1348 void __user *arg = (void __user *)p;
1349 int ioflags = 0;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001350 int error;
1351
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001352 if (filp->f_mode & FMODE_NOCMTIME)
1353 ioflags |= IO_INVIS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001354
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001355 xfs_itrace_entry(ip);
1356
1357 switch (cmd) {
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001358 case XFS_IOC_ALLOCSP:
1359 case XFS_IOC_FREESP:
1360 case XFS_IOC_RESVSP:
1361 case XFS_IOC_UNRESVSP:
1362 case XFS_IOC_ALLOCSP64:
1363 case XFS_IOC_FREESP64:
1364 case XFS_IOC_RESVSP64:
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001365 case XFS_IOC_UNRESVSP64: {
1366 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001367
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001368 if (copy_from_user(&bf, arg, sizeof(bf)))
1369 return -XFS_ERROR(EFAULT);
1370 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1371 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001372 case XFS_IOC_DIOINFO: {
1373 struct dioattr da;
1374 xfs_buftarg_t *target =
1375 XFS_IS_REALTIME_INODE(ip) ?
1376 mp->m_rtdev_targp : mp->m_ddev_targp;
1377
1378 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1379 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1380
1381 if (copy_to_user(arg, &da, sizeof(da)))
1382 return -XFS_ERROR(EFAULT);
1383 return 0;
1384 }
1385
1386 case XFS_IOC_FSBULKSTAT_SINGLE:
1387 case XFS_IOC_FSBULKSTAT:
1388 case XFS_IOC_FSINUMBERS:
1389 return xfs_ioc_bulkstat(mp, cmd, arg);
1390
1391 case XFS_IOC_FSGEOMETRY_V1:
1392 return xfs_ioc_fsgeometry_v1(mp, arg);
1393
1394 case XFS_IOC_FSGEOMETRY:
1395 return xfs_ioc_fsgeometry(mp, arg);
1396
1397 case XFS_IOC_GETVERSION:
1398 return put_user(inode->i_generation, (int __user *)arg);
1399
1400 case XFS_IOC_FSGETXATTR:
1401 return xfs_ioc_fsgetxattr(ip, 0, arg);
1402 case XFS_IOC_FSGETXATTRA:
1403 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001404 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001405 return xfs_ioc_fssetxattr(ip, filp, arg);
1406 case XFS_IOC_GETXFLAGS:
1407 return xfs_ioc_getxflags(ip, arg);
1408 case XFS_IOC_SETXFLAGS:
1409 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001410
1411 case XFS_IOC_FSSETDM: {
1412 struct fsdmidata dmi;
1413
1414 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1415 return -XFS_ERROR(EFAULT);
1416
1417 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1418 dmi.fsd_dmstate);
1419 return -error;
1420 }
1421
1422 case XFS_IOC_GETBMAP:
1423 case XFS_IOC_GETBMAPA:
1424 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1425
1426 case XFS_IOC_GETBMAPX:
1427 return xfs_ioc_getbmapx(ip, arg);
1428
1429 case XFS_IOC_FD_TO_HANDLE:
1430 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001431 case XFS_IOC_PATH_TO_FSHANDLE: {
1432 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001433
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001434 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1435 return -XFS_ERROR(EFAULT);
1436 return xfs_find_handle(cmd, &hreq);
1437 }
1438 case XFS_IOC_OPEN_BY_HANDLE: {
1439 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001440
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001441 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1442 return -XFS_ERROR(EFAULT);
1443 return xfs_open_by_handle(mp, &hreq, filp, inode);
1444 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001445 case XFS_IOC_FSSETDM_BY_HANDLE:
1446 return xfs_fssetdm_by_handle(mp, arg, inode);
1447
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001448 case XFS_IOC_READLINK_BY_HANDLE: {
1449 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001450
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001451 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1452 return -XFS_ERROR(EFAULT);
1453 return xfs_readlink_by_handle(mp, &hreq, inode);
1454 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001455 case XFS_IOC_ATTRLIST_BY_HANDLE:
1456 return xfs_attrlist_by_handle(mp, arg, inode);
1457
1458 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Dave Hansen42a74f22008-02-15 14:37:46 -08001459 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001460
1461 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb462008-11-25 21:20:06 -06001462 struct xfs_swapext sxp;
1463
1464 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1465 return -XFS_ERROR(EFAULT);
1466 error = xfs_swapext(&sxp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001467 return -error;
1468 }
1469
1470 case XFS_IOC_FSCOUNTS: {
1471 xfs_fsop_counts_t out;
1472
1473 error = xfs_fs_counts(mp, &out);
1474 if (error)
1475 return -error;
1476
1477 if (copy_to_user(arg, &out, sizeof(out)))
1478 return -XFS_ERROR(EFAULT);
1479 return 0;
1480 }
1481
1482 case XFS_IOC_SET_RESBLKS: {
1483 xfs_fsop_resblks_t inout;
1484 __uint64_t in;
1485
1486 if (!capable(CAP_SYS_ADMIN))
1487 return -EPERM;
1488
1489 if (copy_from_user(&inout, arg, sizeof(inout)))
1490 return -XFS_ERROR(EFAULT);
1491
1492 /* input parameter is passed in resblks field of structure */
1493 in = inout.resblks;
1494 error = xfs_reserve_blocks(mp, &in, &inout);
1495 if (error)
1496 return -error;
1497
1498 if (copy_to_user(arg, &inout, sizeof(inout)))
1499 return -XFS_ERROR(EFAULT);
1500 return 0;
1501 }
1502
1503 case XFS_IOC_GET_RESBLKS: {
1504 xfs_fsop_resblks_t out;
1505
1506 if (!capable(CAP_SYS_ADMIN))
1507 return -EPERM;
1508
1509 error = xfs_reserve_blocks(mp, NULL, &out);
1510 if (error)
1511 return -error;
1512
1513 if (copy_to_user(arg, &out, sizeof(out)))
1514 return -XFS_ERROR(EFAULT);
1515
1516 return 0;
1517 }
1518
1519 case XFS_IOC_FSGROWFSDATA: {
1520 xfs_growfs_data_t in;
1521
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001522 if (copy_from_user(&in, arg, sizeof(in)))
1523 return -XFS_ERROR(EFAULT);
1524
1525 error = xfs_growfs_data(mp, &in);
1526 return -error;
1527 }
1528
1529 case XFS_IOC_FSGROWFSLOG: {
1530 xfs_growfs_log_t in;
1531
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001532 if (copy_from_user(&in, arg, sizeof(in)))
1533 return -XFS_ERROR(EFAULT);
1534
1535 error = xfs_growfs_log(mp, &in);
1536 return -error;
1537 }
1538
1539 case XFS_IOC_FSGROWFSRT: {
1540 xfs_growfs_rt_t in;
1541
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001542 if (copy_from_user(&in, arg, sizeof(in)))
1543 return -XFS_ERROR(EFAULT);
1544
1545 error = xfs_growfs_rt(mp, &in);
1546 return -error;
1547 }
1548
1549 case XFS_IOC_FREEZE:
1550 if (!capable(CAP_SYS_ADMIN))
1551 return -EPERM;
1552
1553 if (inode->i_sb->s_frozen == SB_UNFROZEN)
1554 freeze_bdev(inode->i_sb->s_bdev);
1555 return 0;
1556
1557 case XFS_IOC_THAW:
1558 if (!capable(CAP_SYS_ADMIN))
1559 return -EPERM;
1560 if (inode->i_sb->s_frozen != SB_UNFROZEN)
1561 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
1562 return 0;
1563
1564 case XFS_IOC_GOINGDOWN: {
1565 __uint32_t in;
1566
1567 if (!capable(CAP_SYS_ADMIN))
1568 return -EPERM;
1569
1570 if (get_user(in, (__uint32_t __user *)arg))
1571 return -XFS_ERROR(EFAULT);
1572
1573 error = xfs_fs_goingdown(mp, in);
1574 return -error;
1575 }
1576
1577 case XFS_IOC_ERROR_INJECTION: {
1578 xfs_error_injection_t in;
1579
1580 if (!capable(CAP_SYS_ADMIN))
1581 return -EPERM;
1582
1583 if (copy_from_user(&in, arg, sizeof(in)))
1584 return -XFS_ERROR(EFAULT);
1585
1586 error = xfs_errortag_add(in.errtag, mp);
1587 return -error;
1588 }
1589
1590 case XFS_IOC_ERROR_CLEARALL:
1591 if (!capable(CAP_SYS_ADMIN))
1592 return -EPERM;
1593
1594 error = xfs_errortag_clearall(mp, 1);
1595 return -error;
1596
1597 default:
1598 return -ENOTTY;
1599 }
1600}