blob: 0264c8719ffd9d2ecf52a6f92848c0fdc117a0ca [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.net743bb4652008-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.net743bb4652008-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.net743bb4652008-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.net743bb4652008-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.net743bb4652008-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{
255 int error;
256 int new_fd;
257 int permflag;
258 struct file *filp;
259 struct inode *inode;
260 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!capable(CAP_SYS_ADMIN))
263 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600265 error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (error)
267 return -error;
268
269 /* Restrict xfs_open_by_handle to directories & regular files. */
270 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
271 iput(inode);
272 return -XFS_ERROR(EINVAL);
273 }
274
275#if BITS_PER_LONG != 32
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600276 hreq->oflags |= O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#endif
278 /* Put open permission in namei format. */
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600279 permflag = hreq->oflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if ((permflag+1) & O_ACCMODE)
281 permflag++;
282 if (permflag & O_TRUNC)
283 permflag |= 2;
284
285 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
286 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
287 iput(inode);
288 return -XFS_ERROR(EPERM);
289 }
290
291 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
292 iput(inode);
293 return -XFS_ERROR(EACCES);
294 }
295
296 /* Can't write directories. */
297 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
298 iput(inode);
299 return -XFS_ERROR(EISDIR);
300 }
301
302 if ((new_fd = get_unused_fd()) < 0) {
303 iput(inode);
304 return new_fd;
305 }
306
Christoph Hellwig44003722008-08-11 15:49:04 +0200307 dentry = d_obtain_alias(inode);
308 if (IS_ERR(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 put_unused_fd(new_fd);
Christoph Hellwig44003722008-08-11 15:49:04 +0200310 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 /* Ensure umount returns EBUSY on umounts while this file is open. */
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800314 mntget(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Create file pointer. */
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600317 filp = dentry_open(dentry, parfilp->f_path.mnt, hreq->oflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (IS_ERR(filp)) {
319 put_unused_fd(new_fd);
320 return -XFS_ERROR(-PTR_ERR(filp));
321 }
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500322
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100323 if (inode->i_mode & S_IFREG) {
324 /* invisible operation should not change atime */
325 filp->f_flags |= O_NOATIME;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500326 filp->f_mode |= FMODE_NOCMTIME;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 fd_install(new_fd, filp);
330 return new_fd;
331}
332
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000333/*
334 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
335 * unused first argument.
336 */
337STATIC int
338do_readlink(
339 char __user *buffer,
340 int buflen,
341 const char *link)
342{
343 int len;
344
345 len = PTR_ERR(link);
346 if (IS_ERR(link))
347 goto out;
348
349 len = strlen(link);
350 if (len > (unsigned) buflen)
351 len = buflen;
352 if (copy_to_user(buffer, link, len))
353 len = -EFAULT;
354 out:
355 return len;
356}
357
358
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600359int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360xfs_readlink_by_handle(
361 xfs_mount_t *mp,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600362 xfs_fsop_handlereq_t *hreq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct inode *parinode)
364{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000367 void *link;
368 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (!capable(CAP_SYS_ADMIN))
371 return -XFS_ERROR(EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600373 error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (error)
375 return -error;
376
377 /* Restrict this handle operation to symlinks only. */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000378 if (!S_ISLNK(inode->i_mode)) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000379 error = -XFS_ERROR(EINVAL);
380 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600383 if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000384 error = -XFS_ERROR(EFAULT);
385 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000388 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
389 if (!link)
390 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000392 error = -xfs_readlink(XFS_I(inode), link);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000393 if (error)
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000394 goto out_kfree;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600395 error = do_readlink(hreq->ohandle, olen, link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000396 if (error)
397 goto out_kfree;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000398
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000399 out_kfree:
400 kfree(link);
401 out_iput:
402 iput(inode);
403 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406STATIC int
407xfs_fssetdm_by_handle(
408 xfs_mount_t *mp,
409 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct inode *parinode)
411{
412 int error;
413 struct fsdmidata fsd;
414 xfs_fsop_setdm_handlereq_t dmhreq;
415 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (!capable(CAP_MKNOD))
418 return -XFS_ERROR(EPERM);
419 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
420 return -XFS_ERROR(EFAULT);
421
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000422 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (error)
424 return -error;
425
426 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000427 error = -XFS_ERROR(EPERM);
428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000432 error = -XFS_ERROR(EFAULT);
433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000436 error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
437 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000439 out:
440 iput(inode);
441 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444STATIC int
445xfs_attrlist_by_handle(
446 xfs_mount_t *mp,
447 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct inode *parinode)
449{
450 int error;
451 attrlist_cursor_kern_t *cursor;
452 xfs_fsop_attrlist_handlereq_t al_hreq;
453 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 char *kbuf;
455
456 if (!capable(CAP_SYS_ADMIN))
457 return -XFS_ERROR(EPERM);
458 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
459 return -XFS_ERROR(EFAULT);
460 if (al_hreq.buflen > XATTR_LIST_MAX)
461 return -XFS_ERROR(EINVAL);
462
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000463 /*
464 * Reject flags, only allow namespaces.
465 */
466 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
467 return -XFS_ERROR(EINVAL);
468
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000469 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (error)
471 goto out;
472
473 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
474 if (!kbuf)
475 goto out_vn_rele;
476
477 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000478 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
479 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (error)
481 goto out_kfree;
482
483 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
484 error = -EFAULT;
485
486 out_kfree:
487 kfree(kbuf);
488 out_vn_rele:
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000489 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 out:
491 return -error;
492}
493
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600494int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000496 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 char *name,
498 char __user *ubuf,
499 __uint32_t *len,
500 __uint32_t flags)
501{
502 char *kbuf;
503 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (*len > XATTR_SIZE_MAX)
506 return EINVAL;
507 kbuf = kmalloc(*len, GFP_KERNEL);
508 if (!kbuf)
509 return ENOMEM;
510
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000511 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (error)
513 goto out_kfree;
514
515 if (copy_to_user(ubuf, kbuf, *len))
516 error = EFAULT;
517
518 out_kfree:
519 kfree(kbuf);
520 return error;
521}
522
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600523int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000525 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 char *name,
527 const char __user *ubuf,
528 __uint32_t len,
529 __uint32_t flags)
530{
531 char *kbuf;
532 int error = EFAULT;
533
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000534 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return EPERM;
536 if (len > XATTR_SIZE_MAX)
537 return EINVAL;
538
539 kbuf = kmalloc(len, GFP_KERNEL);
540 if (!kbuf)
541 return ENOMEM;
542
543 if (copy_from_user(kbuf, ubuf, len))
544 goto out_kfree;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000545
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000546 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 out_kfree:
549 kfree(kbuf);
550 return error;
551}
552
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600553int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000555 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 char *name,
557 __uint32_t flags)
558{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000559 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000561 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564STATIC int
565xfs_attrmulti_by_handle(
566 xfs_mount_t *mp,
567 void __user *arg,
Dave Hansen42a74f22008-02-15 14:37:46 -0800568 struct file *parfilp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct inode *parinode)
570{
571 int error;
572 xfs_attr_multiop_t *ops;
573 xfs_fsop_attrmulti_handlereq_t am_hreq;
574 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unsigned int i, size;
576 char *attr_name;
577
578 if (!capable(CAP_SYS_ADMIN))
579 return -XFS_ERROR(EPERM);
580 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
581 return -XFS_ERROR(EFAULT);
582
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000583 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (error)
585 goto out;
586
587 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000588 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!size || size > 16 * PAGE_SIZE)
590 goto out_vn_rele;
591
592 error = ENOMEM;
593 ops = kmalloc(size, GFP_KERNEL);
594 if (!ops)
595 goto out_vn_rele;
596
597 error = EFAULT;
598 if (copy_from_user(ops, am_hreq.ops, size))
599 goto out_kfree_ops;
600
601 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
602 if (!attr_name)
603 goto out_kfree_ops;
604
605
606 error = 0;
607 for (i = 0; i < am_hreq.opcount; i++) {
608 ops[i].am_error = strncpy_from_user(attr_name,
609 ops[i].am_attrname, MAXNAMELEN);
610 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
611 error = -ERANGE;
612 if (ops[i].am_error < 0)
613 break;
614
615 switch (ops[i].am_opcode) {
616 case ATTR_OP_GET:
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000617 ops[i].am_error = xfs_attrmulti_attr_get(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 attr_name, ops[i].am_attrvalue,
619 &ops[i].am_length, ops[i].am_flags);
620 break;
621 case ATTR_OP_SET:
Dave Hansen42a74f22008-02-15 14:37:46 -0800622 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
623 if (ops[i].am_error)
624 break;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000625 ops[i].am_error = xfs_attrmulti_attr_set(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 attr_name, ops[i].am_attrvalue,
627 ops[i].am_length, ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800628 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
630 case ATTR_OP_REMOVE:
Dave Hansen42a74f22008-02-15 14:37:46 -0800631 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
632 if (ops[i].am_error)
633 break;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000634 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 attr_name, ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800636 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
638 default:
639 ops[i].am_error = EINVAL;
640 }
641 }
642
643 if (copy_to_user(am_hreq.ops, ops, size))
644 error = XFS_ERROR(EFAULT);
645
646 kfree(attr_name);
647 out_kfree_ops:
648 kfree(ops);
649 out_vn_rele:
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000650 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 out:
652 return -error;
653}
654
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600655int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000657 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000658 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct file *filp,
660 int ioflags,
661 unsigned int cmd,
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600662 xfs_flock64_t *bf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int attr_flags = 0;
665 int error;
666
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600667 /*
668 * Only allow the sys admin to reserve space unless
669 * unwritten extents are enabled.
670 */
671 if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
672 !capable(CAP_SYS_ADMIN))
673 return -XFS_ERROR(EPERM);
674
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000675 if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -XFS_ERROR(EPERM);
677
Eric Sandeenad4a8ac2005-09-02 16:41:16 +1000678 if (!(filp->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return -XFS_ERROR(EBADF);
680
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000681 if (!S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -XFS_ERROR(EINVAL);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000685 attr_flags |= XFS_ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (ioflags & IO_INVIS)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000687 attr_flags |= XFS_ATTR_DMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600689 error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -error;
691}
692
693STATIC int
694xfs_ioc_bulkstat(
695 xfs_mount_t *mp,
696 unsigned int cmd,
697 void __user *arg)
698{
699 xfs_fsop_bulkreq_t bulkreq;
700 int count; /* # of records returned */
701 xfs_ino_t inlast; /* last inode number */
702 int done;
703 int error;
704
705 /* done = 1 if there are more stats to get and if bulkstat */
706 /* should be called again (unused here, but used in dmapi) */
707
708 if (!capable(CAP_SYS_ADMIN))
709 return -EPERM;
710
711 if (XFS_FORCED_SHUTDOWN(mp))
712 return -XFS_ERROR(EIO);
713
714 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
715 return -XFS_ERROR(EFAULT);
716
717 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
718 return -XFS_ERROR(EFAULT);
719
720 if ((count = bulkreq.icount) <= 0)
721 return -XFS_ERROR(EINVAL);
722
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100723 if (bulkreq.ubuffer == NULL)
724 return -XFS_ERROR(EINVAL);
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (cmd == XFS_IOC_FSINUMBERS)
727 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000728 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
730 error = xfs_bulkstat_single(mp, &inlast,
731 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100732 else /* XFS_IOC_FSBULKSTAT */
733 error = xfs_bulkstat(mp, &inlast, &count,
734 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
735 sizeof(xfs_bstat_t), bulkreq.ubuffer,
736 BULKSTAT_FG_QUICK, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (error)
739 return -error;
740
741 if (bulkreq.ocount != NULL) {
742 if (copy_to_user(bulkreq.lastip, &inlast,
743 sizeof(xfs_ino_t)))
744 return -XFS_ERROR(EFAULT);
745
746 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
747 return -XFS_ERROR(EFAULT);
748 }
749
750 return 0;
751}
752
753STATIC int
754xfs_ioc_fsgeometry_v1(
755 xfs_mount_t *mp,
756 void __user *arg)
757{
758 xfs_fsop_geom_v1_t fsgeo;
759 int error;
760
761 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
762 if (error)
763 return -error;
764
765 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
766 return -XFS_ERROR(EFAULT);
767 return 0;
768}
769
770STATIC int
771xfs_ioc_fsgeometry(
772 xfs_mount_t *mp,
773 void __user *arg)
774{
775 xfs_fsop_geom_t fsgeo;
776 int error;
777
778 error = xfs_fs_geometry(mp, &fsgeo, 4);
779 if (error)
780 return -error;
781
782 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
783 return -XFS_ERROR(EFAULT);
784 return 0;
785}
786
787/*
788 * Linux extended inode flags interface.
789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791STATIC unsigned int
792xfs_merge_ioc_xflags(
793 unsigned int flags,
794 unsigned int start)
795{
796 unsigned int xflags = start;
797
Eric Sandeen39058a02007-02-10 18:37:10 +1100798 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 xflags |= XFS_XFLAG_IMMUTABLE;
800 else
801 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100802 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 xflags |= XFS_XFLAG_APPEND;
804 else
805 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100806 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 xflags |= XFS_XFLAG_SYNC;
808 else
809 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100810 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 xflags |= XFS_XFLAG_NOATIME;
812 else
813 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100814 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 xflags |= XFS_XFLAG_NODUMP;
816 else
817 xflags &= ~XFS_XFLAG_NODUMP;
818
819 return xflags;
820}
821
822STATIC unsigned int
823xfs_di2lxflags(
824 __uint16_t di_flags)
825{
826 unsigned int flags = 0;
827
828 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100829 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100831 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100833 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100835 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100837 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return flags;
839}
840
841STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000842xfs_ioc_fsgetxattr(
843 xfs_inode_t *ip,
844 int attr,
845 void __user *arg)
846{
847 struct fsxattr fa;
848
849 xfs_ilock(ip, XFS_ILOCK_SHARED);
850 fa.fsx_xflags = xfs_ip2xflags(ip);
851 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
852 fa.fsx_projid = ip->i_d.di_projid;
853
854 if (attr) {
855 if (ip->i_afp) {
856 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
857 fa.fsx_nextents = ip->i_afp->if_bytes /
858 sizeof(xfs_bmbt_rec_t);
859 else
860 fa.fsx_nextents = ip->i_d.di_anextents;
861 } else
862 fa.fsx_nextents = 0;
863 } else {
864 if (ip->i_df.if_flags & XFS_IFEXTENTS)
865 fa.fsx_nextents = ip->i_df.if_bytes /
866 sizeof(xfs_bmbt_rec_t);
867 else
868 fa.fsx_nextents = ip->i_d.di_nextents;
869 }
870 xfs_iunlock(ip, XFS_ILOCK_SHARED);
871
872 if (copy_to_user(arg, &fa, sizeof(fa)))
873 return -EFAULT;
874 return 0;
875}
876
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000877STATIC void
878xfs_set_diflags(
879 struct xfs_inode *ip,
880 unsigned int xflags)
881{
882 unsigned int di_flags;
883
884 /* can't set PREALLOC this way, just preserve it */
885 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
886 if (xflags & XFS_XFLAG_IMMUTABLE)
887 di_flags |= XFS_DIFLAG_IMMUTABLE;
888 if (xflags & XFS_XFLAG_APPEND)
889 di_flags |= XFS_DIFLAG_APPEND;
890 if (xflags & XFS_XFLAG_SYNC)
891 di_flags |= XFS_DIFLAG_SYNC;
892 if (xflags & XFS_XFLAG_NOATIME)
893 di_flags |= XFS_DIFLAG_NOATIME;
894 if (xflags & XFS_XFLAG_NODUMP)
895 di_flags |= XFS_DIFLAG_NODUMP;
896 if (xflags & XFS_XFLAG_PROJINHERIT)
897 di_flags |= XFS_DIFLAG_PROJINHERIT;
898 if (xflags & XFS_XFLAG_NODEFRAG)
899 di_flags |= XFS_DIFLAG_NODEFRAG;
900 if (xflags & XFS_XFLAG_FILESTREAM)
901 di_flags |= XFS_DIFLAG_FILESTREAM;
902 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
903 if (xflags & XFS_XFLAG_RTINHERIT)
904 di_flags |= XFS_DIFLAG_RTINHERIT;
905 if (xflags & XFS_XFLAG_NOSYMLINKS)
906 di_flags |= XFS_DIFLAG_NOSYMLINKS;
907 if (xflags & XFS_XFLAG_EXTSZINHERIT)
908 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
909 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
910 if (xflags & XFS_XFLAG_REALTIME)
911 di_flags |= XFS_DIFLAG_REALTIME;
912 if (xflags & XFS_XFLAG_EXTSIZE)
913 di_flags |= XFS_DIFLAG_EXTSIZE;
914 }
915
916 ip->i_d.di_flags = di_flags;
917}
918
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000919STATIC void
920xfs_diflags_to_linux(
921 struct xfs_inode *ip)
922{
David Chinnere4f75292008-08-13 16:00:45 +1000923 struct inode *inode = VFS_I(ip);
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000924 unsigned int xflags = xfs_ip2xflags(ip);
925
926 if (xflags & XFS_XFLAG_IMMUTABLE)
927 inode->i_flags |= S_IMMUTABLE;
928 else
929 inode->i_flags &= ~S_IMMUTABLE;
930 if (xflags & XFS_XFLAG_APPEND)
931 inode->i_flags |= S_APPEND;
932 else
933 inode->i_flags &= ~S_APPEND;
934 if (xflags & XFS_XFLAG_SYNC)
935 inode->i_flags |= S_SYNC;
936 else
937 inode->i_flags &= ~S_SYNC;
938 if (xflags & XFS_XFLAG_NOATIME)
939 inode->i_flags |= S_NOATIME;
940 else
941 inode->i_flags &= ~S_NOATIME;
942}
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000943
944#define FSX_PROJID 1
945#define FSX_EXTSIZE 2
946#define FSX_XFLAGS 4
947#define FSX_NONBLOCK 8
948
949STATIC int
950xfs_ioctl_setattr(
951 xfs_inode_t *ip,
952 struct fsxattr *fa,
953 int mask)
954{
955 struct xfs_mount *mp = ip->i_mount;
956 struct xfs_trans *tp;
957 unsigned int lock_flags = 0;
958 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
959 struct xfs_dquot *olddquot = NULL;
960 int code;
961
962 xfs_itrace_entry(ip);
963
964 if (mp->m_flags & XFS_MOUNT_RDONLY)
965 return XFS_ERROR(EROFS);
966 if (XFS_FORCED_SHUTDOWN(mp))
967 return XFS_ERROR(EIO);
968
969 /*
970 * If disk quotas is on, we make sure that the dquots do exist on disk,
971 * before we start any other transactions. Trying to do this later
972 * is messy. We don't care to take a readlock to look at the ids
973 * in inode here, because we can't hold it across the trans_reserve.
974 * If the IDs do change before we take the ilock, we're covered
975 * because the i_*dquot fields will get updated anyway.
976 */
977 if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
978 code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid,
979 ip->i_d.di_gid, fa->fsx_projid,
980 XFS_QMOPT_PQUOTA, &udqp, &gdqp);
981 if (code)
982 return code;
983 }
984
985 /*
986 * For the other attributes, we acquire the inode lock and
987 * first do an error checking pass.
988 */
989 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
990 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
991 if (code)
992 goto error_return;
993
994 lock_flags = XFS_ILOCK_EXCL;
995 xfs_ilock(ip, lock_flags);
996
997 /*
998 * CAP_FOWNER overrides the following restrictions:
999 *
1000 * The user ID of the calling process must be equal
1001 * to the file owner ID, except in cases where the
1002 * CAP_FSETID capability is applicable.
1003 */
David Howells91b7771252008-10-31 15:50:04 +11001004 if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001005 code = XFS_ERROR(EPERM);
1006 goto error_return;
1007 }
1008
1009 /*
1010 * Do a quota reservation only if projid is actually going to change.
1011 */
1012 if (mask & FSX_PROJID) {
1013 if (XFS_IS_PQUOTA_ON(mp) &&
1014 ip->i_d.di_projid != fa->fsx_projid) {
1015 ASSERT(tp);
1016 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
1017 capable(CAP_FOWNER) ?
1018 XFS_QMOPT_FORCE_RES : 0);
1019 if (code) /* out of quota */
1020 goto error_return;
1021 }
1022 }
1023
1024 if (mask & FSX_EXTSIZE) {
1025 /*
1026 * Can't change extent size if any extents are allocated.
1027 */
1028 if (ip->i_d.di_nextents &&
1029 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
1030 fa->fsx_extsize)) {
1031 code = XFS_ERROR(EINVAL); /* EFBIG? */
1032 goto error_return;
1033 }
1034
1035 /*
1036 * Extent size must be a multiple of the appropriate block
1037 * size, if set at all.
1038 */
1039 if (fa->fsx_extsize != 0) {
1040 xfs_extlen_t size;
1041
1042 if (XFS_IS_REALTIME_INODE(ip) ||
1043 ((mask & FSX_XFLAGS) &&
1044 (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
1045 size = mp->m_sb.sb_rextsize <<
1046 mp->m_sb.sb_blocklog;
1047 } else {
1048 size = mp->m_sb.sb_blocksize;
1049 }
1050
1051 if (fa->fsx_extsize % size) {
1052 code = XFS_ERROR(EINVAL);
1053 goto error_return;
1054 }
1055 }
1056 }
1057
1058
1059 if (mask & FSX_XFLAGS) {
1060 /*
1061 * Can't change realtime flag if any extents are allocated.
1062 */
1063 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
1064 (XFS_IS_REALTIME_INODE(ip)) !=
1065 (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1066 code = XFS_ERROR(EINVAL); /* EFBIG? */
1067 goto error_return;
1068 }
1069
1070 /*
1071 * If realtime flag is set then must have realtime data.
1072 */
1073 if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
1074 if ((mp->m_sb.sb_rblocks == 0) ||
1075 (mp->m_sb.sb_rextsize == 0) ||
1076 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
1077 code = XFS_ERROR(EINVAL);
1078 goto error_return;
1079 }
1080 }
1081
1082 /*
1083 * Can't modify an immutable/append-only file unless
1084 * we have appropriate permission.
1085 */
1086 if ((ip->i_d.di_flags &
1087 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
1088 (fa->fsx_xflags &
1089 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
1090 !capable(CAP_LINUX_IMMUTABLE)) {
1091 code = XFS_ERROR(EPERM);
1092 goto error_return;
1093 }
1094 }
1095
1096 xfs_trans_ijoin(tp, ip, lock_flags);
1097 xfs_trans_ihold(tp, ip);
1098
1099 /*
1100 * Change file ownership. Must be the owner or privileged.
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001101 */
1102 if (mask & FSX_PROJID) {
1103 /*
1104 * CAP_FSETID overrides the following restrictions:
1105 *
1106 * The set-user-ID and set-group-ID bits of a file will be
1107 * cleared upon successful return from chown()
1108 */
1109 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1110 !capable(CAP_FSETID))
1111 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
1112
1113 /*
1114 * Change the ownerships and register quota modifications
1115 * in the transaction.
1116 */
1117 if (ip->i_d.di_projid != fa->fsx_projid) {
1118 if (XFS_IS_PQUOTA_ON(mp)) {
1119 olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip,
1120 &ip->i_gdquot, gdqp);
1121 }
1122 ip->i_d.di_projid = fa->fsx_projid;
1123
1124 /*
1125 * We may have to rev the inode as well as
1126 * the superblock version number since projids didn't
1127 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
1128 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001129 if (ip->i_d.di_version == 1)
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001130 xfs_bump_ino_vers2(tp, ip);
1131 }
1132
1133 }
1134
1135 if (mask & FSX_EXTSIZE)
1136 ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001137 if (mask & FSX_XFLAGS) {
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001138 xfs_set_diflags(ip, fa->fsx_xflags);
Christoph Hellwigf13fae22008-07-21 16:16:15 +10001139 xfs_diflags_to_linux(ip);
1140 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001141
1142 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1143 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1144
1145 XFS_STATS_INC(xs_ig_attrchg);
1146
1147 /*
1148 * If this is a synchronous mount, make sure that the
1149 * transaction goes to disk before returning to the user.
1150 * This is slightly sub-optimal in that truncates require
1151 * two sync transactions instead of one for wsync filesystems.
1152 * One for the truncate and one for the timestamps since we
1153 * don't want to change the timestamps unless we're sure the
1154 * truncate worked. Truncates are less than 1% of the laddis
1155 * mix so this probably isn't worth the trouble to optimize.
1156 */
1157 if (mp->m_flags & XFS_MOUNT_WSYNC)
1158 xfs_trans_set_sync(tp);
1159 code = xfs_trans_commit(tp, 0);
1160 xfs_iunlock(ip, lock_flags);
1161
1162 /*
1163 * Release any dquot(s) the inode had kept before chown.
1164 */
1165 XFS_QM_DQRELE(mp, olddquot);
1166 XFS_QM_DQRELE(mp, udqp);
1167 XFS_QM_DQRELE(mp, gdqp);
1168
1169 if (code)
1170 return code;
1171
1172 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
1173 XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1174 NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
1175 (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
1176 }
1177
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001178 return 0;
1179
1180 error_return:
1181 XFS_QM_DQRELE(mp, udqp);
1182 XFS_QM_DQRELE(mp, gdqp);
1183 xfs_trans_cancel(tp, 0);
1184 if (lock_flags)
1185 xfs_iunlock(ip, lock_flags);
1186 return code;
1187}
1188
Christoph Hellwigc83bfab2007-10-11 17:47:00 +10001189STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001190xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 xfs_inode_t *ip,
1192 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 void __user *arg)
1194{
1195 struct fsxattr fa;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001196 unsigned int mask;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001197
1198 if (copy_from_user(&fa, arg, sizeof(fa)))
1199 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001201 mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001202 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001203 mask |= FSX_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001205 return -xfs_ioctl_setattr(ip, &fa, mask);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001208STATIC int
1209xfs_ioc_getxflags(
1210 xfs_inode_t *ip,
1211 void __user *arg)
1212{
1213 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001215 flags = xfs_di2lxflags(ip->i_d.di_flags);
1216 if (copy_to_user(arg, &flags, sizeof(flags)))
1217 return -EFAULT;
1218 return 0;
1219}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001221STATIC int
1222xfs_ioc_setxflags(
1223 xfs_inode_t *ip,
1224 struct file *filp,
1225 void __user *arg)
1226{
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001227 struct fsxattr fa;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001228 unsigned int flags;
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001229 unsigned int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001231 if (copy_from_user(&flags, arg, sizeof(flags)))
1232 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001234 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
1235 FS_NOATIME_FL | FS_NODUMP_FL | \
1236 FS_SYNC_FL))
1237 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001239 mask = FSX_XFLAGS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001240 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001241 mask |= FSX_NONBLOCK;
1242 fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Christoph Hellwig25fe55e2008-07-18 17:13:20 +10001244 return -xfs_ioctl_setattr(ip, &fa, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
1247STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001248xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
1249{
1250 struct getbmap __user *base = *ap;
1251
1252 /* copy only getbmap portion (not getbmapx) */
1253 if (copy_to_user(base, bmv, sizeof(struct getbmap)))
1254 return XFS_ERROR(EFAULT);
1255
1256 *ap += sizeof(struct getbmap);
1257 return 0;
1258}
1259
1260STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001262 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int ioflags,
1264 unsigned int cmd,
1265 void __user *arg)
1266{
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001267 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int error;
1269
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001270 if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return -XFS_ERROR(EFAULT);
1272
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001273 if (bmx.bmv_count < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -XFS_ERROR(EINVAL);
1275
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001276 bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (ioflags & IO_INVIS)
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001278 bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001280 error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
1281 (struct getbmap *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (error)
1283 return -error;
1284
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001285 /* copy back header - only size of getbmap */
1286 if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -XFS_ERROR(EFAULT);
1288 return 0;
1289}
1290
1291STATIC int
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001292xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
1293{
1294 struct getbmapx __user *base = *ap;
1295
1296 if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
1297 return XFS_ERROR(EFAULT);
1298
1299 *ap += sizeof(struct getbmapx);
1300 return 0;
1301}
1302
1303STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001305 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 void __user *arg)
1307{
1308 struct getbmapx bmx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int error;
1310
1311 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1312 return -XFS_ERROR(EFAULT);
1313
1314 if (bmx.bmv_count < 2)
1315 return -XFS_ERROR(EINVAL);
1316
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001317 if (bmx.bmv_iflags & (~BMV_IF_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return -XFS_ERROR(EINVAL);
1319
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001320 error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
1321 (struct getbmapx *)arg+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (error)
1323 return -error;
1324
Eric Sandeen8a7141a2008-11-28 14:23:35 +11001325 /* copy back header */
1326 if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -XFS_ERROR(EFAULT);
1328
1329 return 0;
1330}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001331
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001332/*
1333 * Note: some of the ioctl's return positive numbers as a
1334 * byte count indicating success, such as readlink_by_handle.
1335 * So we don't "sign flip" like most other routines. This means
1336 * true errors need to be returned as a negative value.
1337 */
1338long
1339xfs_file_ioctl(
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001340 struct file *filp,
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001341 unsigned int cmd,
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001342 unsigned long p)
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001343{
1344 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001345 struct xfs_inode *ip = XFS_I(inode);
1346 struct xfs_mount *mp = ip->i_mount;
1347 void __user *arg = (void __user *)p;
1348 int ioflags = 0;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001349 int error;
1350
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001351 if (filp->f_mode & FMODE_NOCMTIME)
1352 ioflags |= IO_INVIS;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001353
Christoph Hellwig4d4be482008-12-09 04:47:33 -05001354 xfs_itrace_entry(ip);
1355
1356 switch (cmd) {
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001357 case XFS_IOC_ALLOCSP:
1358 case XFS_IOC_FREESP:
1359 case XFS_IOC_RESVSP:
1360 case XFS_IOC_UNRESVSP:
1361 case XFS_IOC_ALLOCSP64:
1362 case XFS_IOC_FREESP64:
1363 case XFS_IOC_RESVSP64:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001364 case XFS_IOC_UNRESVSP64: {
1365 xfs_flock64_t bf;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001366
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001367 if (copy_from_user(&bf, arg, sizeof(bf)))
1368 return -XFS_ERROR(EFAULT);
1369 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
1370 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001371 case XFS_IOC_DIOINFO: {
1372 struct dioattr da;
1373 xfs_buftarg_t *target =
1374 XFS_IS_REALTIME_INODE(ip) ?
1375 mp->m_rtdev_targp : mp->m_ddev_targp;
1376
1377 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1378 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1379
1380 if (copy_to_user(arg, &da, sizeof(da)))
1381 return -XFS_ERROR(EFAULT);
1382 return 0;
1383 }
1384
1385 case XFS_IOC_FSBULKSTAT_SINGLE:
1386 case XFS_IOC_FSBULKSTAT:
1387 case XFS_IOC_FSINUMBERS:
1388 return xfs_ioc_bulkstat(mp, cmd, arg);
1389
1390 case XFS_IOC_FSGEOMETRY_V1:
1391 return xfs_ioc_fsgeometry_v1(mp, arg);
1392
1393 case XFS_IOC_FSGEOMETRY:
1394 return xfs_ioc_fsgeometry(mp, arg);
1395
1396 case XFS_IOC_GETVERSION:
1397 return put_user(inode->i_generation, (int __user *)arg);
1398
1399 case XFS_IOC_FSGETXATTR:
1400 return xfs_ioc_fsgetxattr(ip, 0, arg);
1401 case XFS_IOC_FSGETXATTRA:
1402 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001403 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001404 return xfs_ioc_fssetxattr(ip, filp, arg);
1405 case XFS_IOC_GETXFLAGS:
1406 return xfs_ioc_getxflags(ip, arg);
1407 case XFS_IOC_SETXFLAGS:
1408 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001409
1410 case XFS_IOC_FSSETDM: {
1411 struct fsdmidata dmi;
1412
1413 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1414 return -XFS_ERROR(EFAULT);
1415
1416 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1417 dmi.fsd_dmstate);
1418 return -error;
1419 }
1420
1421 case XFS_IOC_GETBMAP:
1422 case XFS_IOC_GETBMAPA:
1423 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1424
1425 case XFS_IOC_GETBMAPX:
1426 return xfs_ioc_getbmapx(ip, arg);
1427
1428 case XFS_IOC_FD_TO_HANDLE:
1429 case XFS_IOC_PATH_TO_HANDLE:
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001430 case XFS_IOC_PATH_TO_FSHANDLE: {
1431 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001432
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001433 if (copy_from_user(&hreq, arg, sizeof(hreq)))
1434 return -XFS_ERROR(EFAULT);
1435 return xfs_find_handle(cmd, &hreq);
1436 }
1437 case XFS_IOC_OPEN_BY_HANDLE: {
1438 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001439
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001440 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1441 return -XFS_ERROR(EFAULT);
1442 return xfs_open_by_handle(mp, &hreq, filp, inode);
1443 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001444 case XFS_IOC_FSSETDM_BY_HANDLE:
1445 return xfs_fssetdm_by_handle(mp, arg, inode);
1446
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001447 case XFS_IOC_READLINK_BY_HANDLE: {
1448 xfs_fsop_handlereq_t hreq;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001449
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001450 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
1451 return -XFS_ERROR(EFAULT);
1452 return xfs_readlink_by_handle(mp, &hreq, inode);
1453 }
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001454 case XFS_IOC_ATTRLIST_BY_HANDLE:
1455 return xfs_attrlist_by_handle(mp, arg, inode);
1456
1457 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Dave Hansen42a74f22008-02-15 14:37:46 -08001458 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001459
1460 case XFS_IOC_SWAPEXT: {
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -06001461 struct xfs_swapext sxp;
1462
1463 if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
1464 return -XFS_ERROR(EFAULT);
1465 error = xfs_swapext(&sxp);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001466 return -error;
1467 }
1468
1469 case XFS_IOC_FSCOUNTS: {
1470 xfs_fsop_counts_t out;
1471
1472 error = xfs_fs_counts(mp, &out);
1473 if (error)
1474 return -error;
1475
1476 if (copy_to_user(arg, &out, sizeof(out)))
1477 return -XFS_ERROR(EFAULT);
1478 return 0;
1479 }
1480
1481 case XFS_IOC_SET_RESBLKS: {
1482 xfs_fsop_resblks_t inout;
1483 __uint64_t in;
1484
1485 if (!capable(CAP_SYS_ADMIN))
1486 return -EPERM;
1487
1488 if (copy_from_user(&inout, arg, sizeof(inout)))
1489 return -XFS_ERROR(EFAULT);
1490
1491 /* input parameter is passed in resblks field of structure */
1492 in = inout.resblks;
1493 error = xfs_reserve_blocks(mp, &in, &inout);
1494 if (error)
1495 return -error;
1496
1497 if (copy_to_user(arg, &inout, sizeof(inout)))
1498 return -XFS_ERROR(EFAULT);
1499 return 0;
1500 }
1501
1502 case XFS_IOC_GET_RESBLKS: {
1503 xfs_fsop_resblks_t out;
1504
1505 if (!capable(CAP_SYS_ADMIN))
1506 return -EPERM;
1507
1508 error = xfs_reserve_blocks(mp, NULL, &out);
1509 if (error)
1510 return -error;
1511
1512 if (copy_to_user(arg, &out, sizeof(out)))
1513 return -XFS_ERROR(EFAULT);
1514
1515 return 0;
1516 }
1517
1518 case XFS_IOC_FSGROWFSDATA: {
1519 xfs_growfs_data_t in;
1520
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001521 if (copy_from_user(&in, arg, sizeof(in)))
1522 return -XFS_ERROR(EFAULT);
1523
1524 error = xfs_growfs_data(mp, &in);
1525 return -error;
1526 }
1527
1528 case XFS_IOC_FSGROWFSLOG: {
1529 xfs_growfs_log_t in;
1530
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001531 if (copy_from_user(&in, arg, sizeof(in)))
1532 return -XFS_ERROR(EFAULT);
1533
1534 error = xfs_growfs_log(mp, &in);
1535 return -error;
1536 }
1537
1538 case XFS_IOC_FSGROWFSRT: {
1539 xfs_growfs_rt_t in;
1540
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001541 if (copy_from_user(&in, arg, sizeof(in)))
1542 return -XFS_ERROR(EFAULT);
1543
1544 error = xfs_growfs_rt(mp, &in);
1545 return -error;
1546 }
1547
1548 case XFS_IOC_FREEZE:
1549 if (!capable(CAP_SYS_ADMIN))
1550 return -EPERM;
1551
1552 if (inode->i_sb->s_frozen == SB_UNFROZEN)
1553 freeze_bdev(inode->i_sb->s_bdev);
1554 return 0;
1555
1556 case XFS_IOC_THAW:
1557 if (!capable(CAP_SYS_ADMIN))
1558 return -EPERM;
1559 if (inode->i_sb->s_frozen != SB_UNFROZEN)
1560 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
1561 return 0;
1562
1563 case XFS_IOC_GOINGDOWN: {
1564 __uint32_t in;
1565
1566 if (!capable(CAP_SYS_ADMIN))
1567 return -EPERM;
1568
1569 if (get_user(in, (__uint32_t __user *)arg))
1570 return -XFS_ERROR(EFAULT);
1571
1572 error = xfs_fs_goingdown(mp, in);
1573 return -error;
1574 }
1575
1576 case XFS_IOC_ERROR_INJECTION: {
1577 xfs_error_injection_t in;
1578
1579 if (!capable(CAP_SYS_ADMIN))
1580 return -EPERM;
1581
1582 if (copy_from_user(&in, arg, sizeof(in)))
1583 return -XFS_ERROR(EFAULT);
1584
1585 error = xfs_errortag_add(in.errtag, mp);
1586 return -error;
1587 }
1588
1589 case XFS_IOC_ERROR_CLEARALL:
1590 if (!capable(CAP_SYS_ADMIN))
1591 return -EPERM;
1592
1593 error = xfs_errortag_clearall(mp, 1);
1594 return -error;
1595
1596 default:
1597 return -ENOTTY;
1598 }
1599}