blob: 8eddaff368434fc4231e974a9e2dfad0aa7fcb21 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080052#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/dcache.h>
54#include <linux/mount.h>
55#include <linux/namei.h>
56#include <linux/pagemap.h>
57
58/*
59 * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
60 * a file or fs handle.
61 *
62 * XFS_IOC_PATH_TO_FSHANDLE
63 * returns fs handle for a mount point or path within that mount point
64 * XFS_IOC_FD_TO_HANDLE
65 * returns full handle for a FD opened in user space
66 * XFS_IOC_PATH_TO_HANDLE
67 * returns full handle for a path
68 */
69STATIC int
70xfs_find_handle(
71 unsigned int cmd,
72 void __user *arg)
73{
74 int hsize;
75 xfs_handle_t handle;
76 xfs_fsop_handlereq_t hreq;
77 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (copy_from_user(&hreq, arg, sizeof(hreq)))
80 return -XFS_ERROR(EFAULT);
81
82 memset((char *)&handle, 0, sizeof(handle));
83
84 switch (cmd) {
85 case XFS_IOC_PATH_TO_FSHANDLE:
86 case XFS_IOC_PATH_TO_HANDLE: {
Al Viro2d8f3032008-07-22 09:59:21 -040087 struct path path;
88 int error = user_lpath((const char __user *)hreq.path, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (error)
90 return error;
91
Al Viro2d8f3032008-07-22 09:59:21 -040092 ASSERT(path.dentry);
93 ASSERT(path.dentry->d_inode);
94 inode = igrab(path.dentry->d_inode);
95 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 break;
97 }
98
99 case XFS_IOC_FD_TO_HANDLE: {
100 struct file *file;
101
102 file = fget(hreq.fd);
103 if (!file)
104 return -EBADF;
105
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800106 ASSERT(file->f_path.dentry);
107 ASSERT(file->f_path.dentry->d_inode);
108 inode = igrab(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 fput(file);
110 break;
111 }
112
113 default:
114 ASSERT(0);
115 return -XFS_ERROR(EINVAL);
116 }
117
118 if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
119 /* we're not in XFS anymore, Toto */
120 iput(inode);
121 return -XFS_ERROR(EINVAL);
122 }
123
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000124 switch (inode->i_mode & S_IFMT) {
125 case S_IFREG:
126 case S_IFDIR:
127 case S_IFLNK:
128 break;
129 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 iput(inode);
131 return -XFS_ERROR(EBADF);
132 }
133
134 /* now we can grab the fsid */
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +1000135 memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000136 sizeof(xfs_fsid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 hsize = sizeof(xfs_fsid_t);
138
139 if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000140 xfs_inode_t *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int lock_mode;
142
143 /* need to get access to the xfs_inode to read the generation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 lock_mode = xfs_ilock_map_shared(ip);
145
146 /* fill in fid section of handle from inode */
Christoph Hellwigc6143912007-09-14 15:22:37 +1000147 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
148 sizeof(handle.ha_fid.fid_len);
149 handle.ha_fid.fid_pad = 0;
150 handle.ha_fid.fid_gen = ip->i_d.di_gen;
151 handle.ha_fid.fid_ino = ip->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 xfs_iunlock_map_shared(ip, lock_mode);
154
155 hsize = XFS_HSIZE(handle);
156 }
157
158 /* now copy our handle into the user buffer & write out the size */
159 if (copy_to_user(hreq.ohandle, &handle, hsize) ||
160 copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
161 iput(inode);
162 return -XFS_ERROR(EFAULT);
163 }
164
165 iput(inode);
166 return 0;
167}
168
169
170/*
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000171 * Convert userspace handle data into inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000173 * We use the fact that all the fsop_handlereq ioctl calls have a data
174 * structure argument whose first component is always a xfs_fsop_handlereq_t,
175 * so we can pass that sub structure into this handy, shared routine.
176 *
177 * If no error, caller must always iput the returned inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
179STATIC int
180xfs_vget_fsop_handlereq(
181 xfs_mount_t *mp,
182 struct inode *parinode, /* parent inode pointer */
183 xfs_fsop_handlereq_t *hreq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct inode **inode)
185{
186 void __user *hanp;
187 size_t hlen;
188 xfs_fid_t *xfid;
189 xfs_handle_t *handlep;
190 xfs_handle_t handle;
191 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 xfs_ino_t ino;
193 __u32 igen;
194 int error;
195
196 /*
197 * Only allow handle opens under a directory.
198 */
199 if (!S_ISDIR(parinode->i_mode))
200 return XFS_ERROR(ENOTDIR);
201
202 hanp = hreq->ihandle;
203 hlen = hreq->ihandlen;
204 handlep = &handle;
205
206 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
207 return XFS_ERROR(EINVAL);
208 if (copy_from_user(handlep, hanp, hlen))
209 return XFS_ERROR(EFAULT);
210 if (hlen < sizeof(*handlep))
211 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
212 if (hlen > sizeof(handlep->ha_fsid)) {
Christoph Hellwigc6143912007-09-14 15:22:37 +1000213 if (handlep->ha_fid.fid_len !=
214 (hlen - sizeof(handlep->ha_fsid) -
215 sizeof(handlep->ha_fid.fid_len)) ||
216 handlep->ha_fid.fid_pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return XFS_ERROR(EINVAL);
218 }
219
220 /*
221 * Crack the handle, obtain the inode # & generation #
222 */
223 xfid = (struct xfs_fid *)&handlep->ha_fid;
Christoph Hellwigc6143912007-09-14 15:22:37 +1000224 if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
225 ino = xfid->fid_ino;
226 igen = xfid->fid_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } else {
228 return XFS_ERROR(EINVAL);
229 }
230
231 /*
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000232 * Get the XFS inode, building a Linux inode to go with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
235 if (error)
236 return error;
237 if (ip == NULL)
238 return XFS_ERROR(EIO);
Christoph Hellwig6a7f4222008-04-22 17:33:40 +1000239 if (ip->i_d.di_gen != igen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 xfs_iput_new(ip, XFS_ILOCK_SHARED);
241 return XFS_ERROR(ENOENT);
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 xfs_iunlock(ip, XFS_ILOCK_SHARED);
245
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000246 *inode = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248}
249
250STATIC int
251xfs_open_by_handle(
252 xfs_mount_t *mp,
253 void __user *arg,
254 struct file *parfilp,
255 struct inode *parinode)
256{
257 int error;
258 int new_fd;
259 int permflag;
260 struct file *filp;
261 struct inode *inode;
262 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 xfs_fsop_handlereq_t hreq;
264
265 if (!capable(CAP_SYS_ADMIN))
266 return -XFS_ERROR(EPERM);
267 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
268 return -XFS_ERROR(EFAULT);
269
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000270 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (error)
272 return -error;
273
274 /* Restrict xfs_open_by_handle to directories & regular files. */
275 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
276 iput(inode);
277 return -XFS_ERROR(EINVAL);
278 }
279
280#if BITS_PER_LONG != 32
281 hreq.oflags |= O_LARGEFILE;
282#endif
283 /* Put open permission in namei format. */
284 permflag = hreq.oflags;
285 if ((permflag+1) & O_ACCMODE)
286 permflag++;
287 if (permflag & O_TRUNC)
288 permflag |= 2;
289
290 if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
291 (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
292 iput(inode);
293 return -XFS_ERROR(EPERM);
294 }
295
296 if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
297 iput(inode);
298 return -XFS_ERROR(EACCES);
299 }
300
301 /* Can't write directories. */
302 if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
303 iput(inode);
304 return -XFS_ERROR(EISDIR);
305 }
306
307 if ((new_fd = get_unused_fd()) < 0) {
308 iput(inode);
309 return new_fd;
310 }
311
312 dentry = d_alloc_anon(inode);
313 if (dentry == NULL) {
314 iput(inode);
315 put_unused_fd(new_fd);
316 return -XFS_ERROR(ENOMEM);
317 }
318
319 /* Ensure umount returns EBUSY on umounts while this file is open. */
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800320 mntget(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 /* Create file pointer. */
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800323 filp = dentry_open(dentry, parfilp->f_path.mnt, hreq.oflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (IS_ERR(filp)) {
325 put_unused_fd(new_fd);
326 return -XFS_ERROR(-PTR_ERR(filp));
327 }
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100328 if (inode->i_mode & S_IFREG) {
329 /* invisible operation should not change atime */
330 filp->f_flags |= O_NOATIME;
Nathan Scott3562fd42006-03-14 14:00:35 +1100331 filp->f_op = &xfs_invis_file_operations;
Vlad Apostolov2e2e7bb2006-11-11 18:04:47 +1100332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 fd_install(new_fd, filp);
335 return new_fd;
336}
337
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000338/*
339 * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
340 * unused first argument.
341 */
342STATIC int
343do_readlink(
344 char __user *buffer,
345 int buflen,
346 const char *link)
347{
348 int len;
349
350 len = PTR_ERR(link);
351 if (IS_ERR(link))
352 goto out;
353
354 len = strlen(link);
355 if (len > (unsigned) buflen)
356 len = buflen;
357 if (copy_to_user(buffer, link, len))
358 len = -EFAULT;
359 out:
360 return len;
361}
362
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364STATIC int
365xfs_readlink_by_handle(
366 xfs_mount_t *mp,
367 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct inode *parinode)
369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct inode *inode;
371 xfs_fsop_handlereq_t hreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 __u32 olen;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000373 void *link;
374 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (!capable(CAP_SYS_ADMIN))
377 return -XFS_ERROR(EPERM);
378 if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
379 return -XFS_ERROR(EFAULT);
380
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000381 error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (error)
383 return -error;
384
385 /* Restrict this handle operation to symlinks only. */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000386 if (!S_ISLNK(inode->i_mode)) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000387 error = -XFS_ERROR(EINVAL);
388 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391 if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000392 error = -XFS_ERROR(EFAULT);
393 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000396 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
397 if (!link)
398 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000400 error = -xfs_readlink(XFS_I(inode), link);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000401 if (error)
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000402 goto out_kfree;
403 error = do_readlink(hreq.ohandle, olen, link);
404 if (error)
405 goto out_kfree;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000406
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000407 out_kfree:
408 kfree(link);
409 out_iput:
410 iput(inode);
411 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414STATIC int
415xfs_fssetdm_by_handle(
416 xfs_mount_t *mp,
417 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct inode *parinode)
419{
420 int error;
421 struct fsdmidata fsd;
422 xfs_fsop_setdm_handlereq_t dmhreq;
423 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (!capable(CAP_MKNOD))
426 return -XFS_ERROR(EPERM);
427 if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
428 return -XFS_ERROR(EFAULT);
429
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000430 error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (error)
432 return -error;
433
434 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000435 error = -XFS_ERROR(EPERM);
436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
439 if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000440 error = -XFS_ERROR(EFAULT);
441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000444 error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
445 fsd.fsd_dmstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000447 out:
448 iput(inode);
449 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452STATIC int
453xfs_attrlist_by_handle(
454 xfs_mount_t *mp,
455 void __user *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct inode *parinode)
457{
458 int error;
459 attrlist_cursor_kern_t *cursor;
460 xfs_fsop_attrlist_handlereq_t al_hreq;
461 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 char *kbuf;
463
464 if (!capable(CAP_SYS_ADMIN))
465 return -XFS_ERROR(EPERM);
466 if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
467 return -XFS_ERROR(EFAULT);
468 if (al_hreq.buflen > XATTR_LIST_MAX)
469 return -XFS_ERROR(EINVAL);
470
Christoph Hellwig90ad58a2008-06-27 13:32:19 +1000471 /*
472 * Reject flags, only allow namespaces.
473 */
474 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
475 return -XFS_ERROR(EINVAL);
476
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000477 error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (error)
479 goto out;
480
481 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
482 if (!kbuf)
483 goto out_vn_rele;
484
485 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000486 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
487 al_hreq.flags, cursor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (error)
489 goto out_kfree;
490
491 if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
492 error = -EFAULT;
493
494 out_kfree:
495 kfree(kbuf);
496 out_vn_rele:
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000497 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 out:
499 return -error;
500}
501
502STATIC int
503xfs_attrmulti_attr_get(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000504 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 char *name,
506 char __user *ubuf,
507 __uint32_t *len,
508 __uint32_t flags)
509{
510 char *kbuf;
511 int error = EFAULT;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (*len > XATTR_SIZE_MAX)
514 return EINVAL;
515 kbuf = kmalloc(*len, GFP_KERNEL);
516 if (!kbuf)
517 return ENOMEM;
518
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000519 error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (error)
521 goto out_kfree;
522
523 if (copy_to_user(ubuf, kbuf, *len))
524 error = EFAULT;
525
526 out_kfree:
527 kfree(kbuf);
528 return error;
529}
530
531STATIC int
532xfs_attrmulti_attr_set(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000533 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 char *name,
535 const char __user *ubuf,
536 __uint32_t len,
537 __uint32_t flags)
538{
539 char *kbuf;
540 int error = EFAULT;
541
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000542 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return EPERM;
544 if (len > XATTR_SIZE_MAX)
545 return EINVAL;
546
547 kbuf = kmalloc(len, GFP_KERNEL);
548 if (!kbuf)
549 return ENOMEM;
550
551 if (copy_from_user(kbuf, ubuf, len))
552 goto out_kfree;
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000553
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000554 error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 out_kfree:
557 kfree(kbuf);
558 return error;
559}
560
561STATIC int
562xfs_attrmulti_attr_remove(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000563 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 char *name,
565 __uint32_t flags)
566{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000567 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return EPERM;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000569 return xfs_attr_remove(XFS_I(inode), name, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572STATIC int
573xfs_attrmulti_by_handle(
574 xfs_mount_t *mp,
575 void __user *arg,
Dave Hansen42a74f22008-02-15 14:37:46 -0800576 struct file *parfilp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct inode *parinode)
578{
579 int error;
580 xfs_attr_multiop_t *ops;
581 xfs_fsop_attrmulti_handlereq_t am_hreq;
582 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned int i, size;
584 char *attr_name;
585
586 if (!capable(CAP_SYS_ADMIN))
587 return -XFS_ERROR(EPERM);
588 if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
589 return -XFS_ERROR(EFAULT);
590
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000591 error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (error)
593 goto out;
594
595 error = E2BIG;
Christoph Hellwige182f572008-06-27 13:32:31 +1000596 size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!size || size > 16 * PAGE_SIZE)
598 goto out_vn_rele;
599
600 error = ENOMEM;
601 ops = kmalloc(size, GFP_KERNEL);
602 if (!ops)
603 goto out_vn_rele;
604
605 error = EFAULT;
606 if (copy_from_user(ops, am_hreq.ops, size))
607 goto out_kfree_ops;
608
609 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
610 if (!attr_name)
611 goto out_kfree_ops;
612
613
614 error = 0;
615 for (i = 0; i < am_hreq.opcount; i++) {
616 ops[i].am_error = strncpy_from_user(attr_name,
617 ops[i].am_attrname, MAXNAMELEN);
618 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
619 error = -ERANGE;
620 if (ops[i].am_error < 0)
621 break;
622
623 switch (ops[i].am_opcode) {
624 case ATTR_OP_GET:
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000625 ops[i].am_error = xfs_attrmulti_attr_get(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 attr_name, ops[i].am_attrvalue,
627 &ops[i].am_length, ops[i].am_flags);
628 break;
629 case ATTR_OP_SET:
Dave Hansen42a74f22008-02-15 14:37:46 -0800630 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
631 if (ops[i].am_error)
632 break;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000633 ops[i].am_error = xfs_attrmulti_attr_set(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 attr_name, ops[i].am_attrvalue,
635 ops[i].am_length, 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 case ATTR_OP_REMOVE:
Dave Hansen42a74f22008-02-15 14:37:46 -0800639 ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
640 if (ops[i].am_error)
641 break;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000642 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 attr_name, ops[i].am_flags);
Dave Hansen42a74f22008-02-15 14:37:46 -0800644 mnt_drop_write(parfilp->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 default:
647 ops[i].am_error = EINVAL;
648 }
649 }
650
651 if (copy_to_user(am_hreq.ops, ops, size))
652 error = XFS_ERROR(EFAULT);
653
654 kfree(attr_name);
655 out_kfree_ops:
656 kfree(ops);
657 out_vn_rele:
Christoph Hellwig6e7f75e2007-10-11 18:09:50 +1000658 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 out:
660 return -error;
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663STATIC int
664xfs_ioc_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000665 struct xfs_inode *ip,
Alexey Dobriyanf37ea142006-09-28 10:52:04 +1000666 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct file *filp,
668 int ioflags,
669 unsigned int cmd,
670 void __user *arg)
671{
672 xfs_flock64_t bf;
673 int attr_flags = 0;
674 int error;
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
685 if (copy_from_user(&bf, arg, sizeof(bf)))
686 return -XFS_ERROR(EFAULT);
687
688 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
689 attr_flags |= ATTR_NONBLOCK;
690 if (ioflags & IO_INVIS)
691 attr_flags |= ATTR_DMI;
692
Christoph Hellwig993386c2007-08-28 16:12:30 +1000693 error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 NULL, attr_flags);
695 return -error;
696}
697
698STATIC int
699xfs_ioc_bulkstat(
700 xfs_mount_t *mp,
701 unsigned int cmd,
702 void __user *arg)
703{
704 xfs_fsop_bulkreq_t bulkreq;
705 int count; /* # of records returned */
706 xfs_ino_t inlast; /* last inode number */
707 int done;
708 int error;
709
710 /* done = 1 if there are more stats to get and if bulkstat */
711 /* should be called again (unused here, but used in dmapi) */
712
713 if (!capable(CAP_SYS_ADMIN))
714 return -EPERM;
715
716 if (XFS_FORCED_SHUTDOWN(mp))
717 return -XFS_ERROR(EIO);
718
719 if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
720 return -XFS_ERROR(EFAULT);
721
722 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
723 return -XFS_ERROR(EFAULT);
724
725 if ((count = bulkreq.icount) <= 0)
726 return -XFS_ERROR(EINVAL);
727
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100728 if (bulkreq.ubuffer == NULL)
729 return -XFS_ERROR(EINVAL);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (cmd == XFS_IOC_FSINUMBERS)
732 error = xfs_inumbers(mp, &inlast, &count,
Michal Marekfaa63e92007-07-11 11:10:19 +1000733 bulkreq.ubuffer, xfs_inumbers_fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
735 error = xfs_bulkstat_single(mp, &inlast,
736 bulkreq.ubuffer, &done);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100737 else /* XFS_IOC_FSBULKSTAT */
738 error = xfs_bulkstat(mp, &inlast, &count,
739 (bulkstat_one_pf)xfs_bulkstat_one, NULL,
740 sizeof(xfs_bstat_t), bulkreq.ubuffer,
741 BULKSTAT_FG_QUICK, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (error)
744 return -error;
745
746 if (bulkreq.ocount != NULL) {
747 if (copy_to_user(bulkreq.lastip, &inlast,
748 sizeof(xfs_ino_t)))
749 return -XFS_ERROR(EFAULT);
750
751 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
752 return -XFS_ERROR(EFAULT);
753 }
754
755 return 0;
756}
757
758STATIC int
759xfs_ioc_fsgeometry_v1(
760 xfs_mount_t *mp,
761 void __user *arg)
762{
763 xfs_fsop_geom_v1_t fsgeo;
764 int error;
765
766 error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
767 if (error)
768 return -error;
769
770 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
771 return -XFS_ERROR(EFAULT);
772 return 0;
773}
774
775STATIC int
776xfs_ioc_fsgeometry(
777 xfs_mount_t *mp,
778 void __user *arg)
779{
780 xfs_fsop_geom_t fsgeo;
781 int error;
782
783 error = xfs_fs_geometry(mp, &fsgeo, 4);
784 if (error)
785 return -error;
786
787 if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
788 return -XFS_ERROR(EFAULT);
789 return 0;
790}
791
792/*
793 * Linux extended inode flags interface.
794 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796STATIC unsigned int
797xfs_merge_ioc_xflags(
798 unsigned int flags,
799 unsigned int start)
800{
801 unsigned int xflags = start;
802
Eric Sandeen39058a02007-02-10 18:37:10 +1100803 if (flags & FS_IMMUTABLE_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 xflags |= XFS_XFLAG_IMMUTABLE;
805 else
806 xflags &= ~XFS_XFLAG_IMMUTABLE;
Eric Sandeen39058a02007-02-10 18:37:10 +1100807 if (flags & FS_APPEND_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xflags |= XFS_XFLAG_APPEND;
809 else
810 xflags &= ~XFS_XFLAG_APPEND;
Eric Sandeen39058a02007-02-10 18:37:10 +1100811 if (flags & FS_SYNC_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 xflags |= XFS_XFLAG_SYNC;
813 else
814 xflags &= ~XFS_XFLAG_SYNC;
Eric Sandeen39058a02007-02-10 18:37:10 +1100815 if (flags & FS_NOATIME_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 xflags |= XFS_XFLAG_NOATIME;
817 else
818 xflags &= ~XFS_XFLAG_NOATIME;
Eric Sandeen39058a02007-02-10 18:37:10 +1100819 if (flags & FS_NODUMP_FL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 xflags |= XFS_XFLAG_NODUMP;
821 else
822 xflags &= ~XFS_XFLAG_NODUMP;
823
824 return xflags;
825}
826
827STATIC unsigned int
828xfs_di2lxflags(
829 __uint16_t di_flags)
830{
831 unsigned int flags = 0;
832
833 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Eric Sandeen39058a02007-02-10 18:37:10 +1100834 flags |= FS_IMMUTABLE_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (di_flags & XFS_DIFLAG_APPEND)
Eric Sandeen39058a02007-02-10 18:37:10 +1100836 flags |= FS_APPEND_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (di_flags & XFS_DIFLAG_SYNC)
Eric Sandeen39058a02007-02-10 18:37:10 +1100838 flags |= FS_SYNC_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (di_flags & XFS_DIFLAG_NOATIME)
Eric Sandeen39058a02007-02-10 18:37:10 +1100840 flags |= FS_NOATIME_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (di_flags & XFS_DIFLAG_NODUMP)
Eric Sandeen39058a02007-02-10 18:37:10 +1100842 flags |= FS_NODUMP_FL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return flags;
844}
845
846STATIC int
Christoph Hellwigc83bfab2007-10-11 17:47:00 +1000847xfs_ioc_fsgetxattr(
848 xfs_inode_t *ip,
849 int attr,
850 void __user *arg)
851{
852 struct fsxattr fa;
853
854 xfs_ilock(ip, XFS_ILOCK_SHARED);
855 fa.fsx_xflags = xfs_ip2xflags(ip);
856 fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
857 fa.fsx_projid = ip->i_d.di_projid;
858
859 if (attr) {
860 if (ip->i_afp) {
861 if (ip->i_afp->if_flags & XFS_IFEXTENTS)
862 fa.fsx_nextents = ip->i_afp->if_bytes /
863 sizeof(xfs_bmbt_rec_t);
864 else
865 fa.fsx_nextents = ip->i_d.di_anextents;
866 } else
867 fa.fsx_nextents = 0;
868 } else {
869 if (ip->i_df.if_flags & XFS_IFEXTENTS)
870 fa.fsx_nextents = ip->i_df.if_bytes /
871 sizeof(xfs_bmbt_rec_t);
872 else
873 fa.fsx_nextents = ip->i_d.di_nextents;
874 }
875 xfs_iunlock(ip, XFS_ILOCK_SHARED);
876
877 if (copy_to_user(arg, &fa, sizeof(fa)))
878 return -EFAULT;
879 return 0;
880}
881
882STATIC int
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000883xfs_ioc_fssetxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 xfs_inode_t *ip,
885 struct file *filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 void __user *arg)
887{
888 struct fsxattr fa;
Nathan Scott8285fb52006-06-09 17:07:12 +1000889 struct bhv_vattr *vattr;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000890 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int attr_flags;
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000892
893 if (copy_from_user(&fa, arg, sizeof(fa)))
894 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Nathan Scott220b5282006-03-14 13:33:36 +1100896 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
897 if (unlikely(!vattr))
898 return -ENOMEM;
899
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000900 attr_flags = 0;
901 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
902 attr_flags |= ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000904 vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
905 vattr->va_xflags = fa.fsx_xflags;
906 vattr->va_extsize = fa.fsx_extsize;
907 vattr->va_projid = fa.fsx_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000909 error = -xfs_setattr(ip, vattr, attr_flags, NULL);
910 if (!error)
911 vn_revalidate(XFS_ITOV(ip)); /* update flags */
912 kfree(vattr);
913 return 0;
914}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000916STATIC int
917xfs_ioc_getxflags(
918 xfs_inode_t *ip,
919 void __user *arg)
920{
921 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000923 flags = xfs_di2lxflags(ip->i_d.di_flags);
924 if (copy_to_user(arg, &flags, sizeof(flags)))
925 return -EFAULT;
926 return 0;
927}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000929STATIC int
930xfs_ioc_setxflags(
931 xfs_inode_t *ip,
932 struct file *filp,
933 void __user *arg)
934{
935 struct bhv_vattr *vattr;
936 unsigned int flags;
937 int attr_flags;
938 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000940 if (copy_from_user(&flags, arg, sizeof(flags)))
941 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000943 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
944 FS_NOATIME_FL | FS_NODUMP_FL | \
945 FS_SYNC_FL))
946 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000948 vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
949 if (unlikely(!vattr))
950 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000952 attr_flags = 0;
953 if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
954 attr_flags |= ATTR_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000956 vattr->va_mask = XFS_AT_XFLAGS;
957 vattr->va_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
Nathan Scott220b5282006-03-14 13:33:36 +1100958
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +1000959 error = -xfs_setattr(ip, vattr, attr_flags, NULL);
960 if (likely(!error))
961 vn_revalidate(XFS_ITOV(ip)); /* update flags */
Nathan Scott220b5282006-03-14 13:33:36 +1100962 kfree(vattr);
963 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966STATIC int
967xfs_ioc_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000968 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int ioflags,
970 unsigned int cmd,
971 void __user *arg)
972{
973 struct getbmap bm;
974 int iflags;
975 int error;
976
977 if (copy_from_user(&bm, arg, sizeof(bm)))
978 return -XFS_ERROR(EFAULT);
979
980 if (bm.bmv_count < 2)
981 return -XFS_ERROR(EINVAL);
982
983 iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
984 if (ioflags & IO_INVIS)
985 iflags |= BMV_IF_NO_DMAPI_READ;
986
Christoph Hellwig993386c2007-08-28 16:12:30 +1000987 error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (error)
989 return -error;
990
991 if (copy_to_user(arg, &bm, sizeof(bm)))
992 return -XFS_ERROR(EFAULT);
993 return 0;
994}
995
996STATIC int
997xfs_ioc_getbmapx(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000998 struct xfs_inode *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 void __user *arg)
1000{
1001 struct getbmapx bmx;
1002 struct getbmap bm;
1003 int iflags;
1004 int error;
1005
1006 if (copy_from_user(&bmx, arg, sizeof(bmx)))
1007 return -XFS_ERROR(EFAULT);
1008
1009 if (bmx.bmv_count < 2)
1010 return -XFS_ERROR(EINVAL);
1011
1012 /*
1013 * Map input getbmapx structure to a getbmap
1014 * structure for xfs_getbmap.
1015 */
1016 GETBMAP_CONVERT(bmx, bm);
1017
1018 iflags = bmx.bmv_iflags;
1019
1020 if (iflags & (~BMV_IF_VALID))
1021 return -XFS_ERROR(EINVAL);
1022
1023 iflags |= BMV_IF_EXTENDED;
1024
Christoph Hellwig993386c2007-08-28 16:12:30 +10001025 error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (error)
1027 return -error;
1028
1029 GETBMAP_CONVERT(bm, bmx);
1030
1031 if (copy_to_user(arg, &bmx, sizeof(bmx)))
1032 return -XFS_ERROR(EFAULT);
1033
1034 return 0;
1035}
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001036
1037int
1038xfs_ioctl(
1039 xfs_inode_t *ip,
1040 struct file *filp,
1041 int ioflags,
1042 unsigned int cmd,
1043 void __user *arg)
1044{
1045 struct inode *inode = filp->f_path.dentry->d_inode;
1046 xfs_mount_t *mp = ip->i_mount;
1047 int error;
1048
1049 xfs_itrace_entry(XFS_I(inode));
1050 switch (cmd) {
1051
1052 case XFS_IOC_ALLOCSP:
1053 case XFS_IOC_FREESP:
1054 case XFS_IOC_RESVSP:
1055 case XFS_IOC_UNRESVSP:
1056 case XFS_IOC_ALLOCSP64:
1057 case XFS_IOC_FREESP64:
1058 case XFS_IOC_RESVSP64:
1059 case XFS_IOC_UNRESVSP64:
1060 /*
1061 * Only allow the sys admin to reserve space unless
1062 * unwritten extents are enabled.
1063 */
1064 if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
1065 !capable(CAP_SYS_ADMIN))
1066 return -EPERM;
1067
1068 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
1069
1070 case XFS_IOC_DIOINFO: {
1071 struct dioattr da;
1072 xfs_buftarg_t *target =
1073 XFS_IS_REALTIME_INODE(ip) ?
1074 mp->m_rtdev_targp : mp->m_ddev_targp;
1075
1076 da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
1077 da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
1078
1079 if (copy_to_user(arg, &da, sizeof(da)))
1080 return -XFS_ERROR(EFAULT);
1081 return 0;
1082 }
1083
1084 case XFS_IOC_FSBULKSTAT_SINGLE:
1085 case XFS_IOC_FSBULKSTAT:
1086 case XFS_IOC_FSINUMBERS:
1087 return xfs_ioc_bulkstat(mp, cmd, arg);
1088
1089 case XFS_IOC_FSGEOMETRY_V1:
1090 return xfs_ioc_fsgeometry_v1(mp, arg);
1091
1092 case XFS_IOC_FSGEOMETRY:
1093 return xfs_ioc_fsgeometry(mp, arg);
1094
1095 case XFS_IOC_GETVERSION:
1096 return put_user(inode->i_generation, (int __user *)arg);
1097
1098 case XFS_IOC_FSGETXATTR:
1099 return xfs_ioc_fsgetxattr(ip, 0, arg);
1100 case XFS_IOC_FSGETXATTRA:
1101 return xfs_ioc_fsgetxattr(ip, 1, arg);
Lachlan McIlroy3b2816b2008-04-18 12:43:35 +10001102 case XFS_IOC_FSSETXATTR:
Lachlan McIlroy65e67f52008-04-18 12:59:45 +10001103 return xfs_ioc_fssetxattr(ip, filp, arg);
1104 case XFS_IOC_GETXFLAGS:
1105 return xfs_ioc_getxflags(ip, arg);
1106 case XFS_IOC_SETXFLAGS:
1107 return xfs_ioc_setxflags(ip, filp, arg);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001108
1109 case XFS_IOC_FSSETDM: {
1110 struct fsdmidata dmi;
1111
1112 if (copy_from_user(&dmi, arg, sizeof(dmi)))
1113 return -XFS_ERROR(EFAULT);
1114
1115 error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
1116 dmi.fsd_dmstate);
1117 return -error;
1118 }
1119
1120 case XFS_IOC_GETBMAP:
1121 case XFS_IOC_GETBMAPA:
1122 return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
1123
1124 case XFS_IOC_GETBMAPX:
1125 return xfs_ioc_getbmapx(ip, arg);
1126
1127 case XFS_IOC_FD_TO_HANDLE:
1128 case XFS_IOC_PATH_TO_HANDLE:
1129 case XFS_IOC_PATH_TO_FSHANDLE:
1130 return xfs_find_handle(cmd, arg);
1131
1132 case XFS_IOC_OPEN_BY_HANDLE:
1133 return xfs_open_by_handle(mp, arg, filp, inode);
1134
1135 case XFS_IOC_FSSETDM_BY_HANDLE:
1136 return xfs_fssetdm_by_handle(mp, arg, inode);
1137
1138 case XFS_IOC_READLINK_BY_HANDLE:
1139 return xfs_readlink_by_handle(mp, arg, inode);
1140
1141 case XFS_IOC_ATTRLIST_BY_HANDLE:
1142 return xfs_attrlist_by_handle(mp, arg, inode);
1143
1144 case XFS_IOC_ATTRMULTI_BY_HANDLE:
Dave Hansen42a74f22008-02-15 14:37:46 -08001145 return xfs_attrmulti_by_handle(mp, arg, filp, inode);
Lachlan McIlroydf26cfe2008-04-18 11:44:03 +10001146
1147 case XFS_IOC_SWAPEXT: {
1148 error = xfs_swapext((struct xfs_swapext __user *)arg);
1149 return -error;
1150 }
1151
1152 case XFS_IOC_FSCOUNTS: {
1153 xfs_fsop_counts_t out;
1154
1155 error = xfs_fs_counts(mp, &out);
1156 if (error)
1157 return -error;
1158
1159 if (copy_to_user(arg, &out, sizeof(out)))
1160 return -XFS_ERROR(EFAULT);
1161 return 0;
1162 }
1163
1164 case XFS_IOC_SET_RESBLKS: {
1165 xfs_fsop_resblks_t inout;
1166 __uint64_t in;
1167
1168 if (!capable(CAP_SYS_ADMIN))
1169 return -EPERM;
1170
1171 if (copy_from_user(&inout, arg, sizeof(inout)))
1172 return -XFS_ERROR(EFAULT);
1173
1174 /* input parameter is passed in resblks field of structure */
1175 in = inout.resblks;
1176 error = xfs_reserve_blocks(mp, &in, &inout);
1177 if (error)
1178 return -error;
1179
1180 if (copy_to_user(arg, &inout, sizeof(inout)))
1181 return -XFS_ERROR(EFAULT);
1182 return 0;
1183 }
1184
1185 case XFS_IOC_GET_RESBLKS: {
1186 xfs_fsop_resblks_t out;
1187
1188 if (!capable(CAP_SYS_ADMIN))
1189 return -EPERM;
1190
1191 error = xfs_reserve_blocks(mp, NULL, &out);
1192 if (error)
1193 return -error;
1194
1195 if (copy_to_user(arg, &out, sizeof(out)))
1196 return -XFS_ERROR(EFAULT);
1197
1198 return 0;
1199 }
1200
1201 case XFS_IOC_FSGROWFSDATA: {
1202 xfs_growfs_data_t in;
1203
1204 if (!capable(CAP_SYS_ADMIN))
1205 return -EPERM;
1206
1207 if (copy_from_user(&in, arg, sizeof(in)))
1208 return -XFS_ERROR(EFAULT);
1209
1210 error = xfs_growfs_data(mp, &in);
1211 return -error;
1212 }
1213
1214 case XFS_IOC_FSGROWFSLOG: {
1215 xfs_growfs_log_t in;
1216
1217 if (!capable(CAP_SYS_ADMIN))
1218 return -EPERM;
1219
1220 if (copy_from_user(&in, arg, sizeof(in)))
1221 return -XFS_ERROR(EFAULT);
1222
1223 error = xfs_growfs_log(mp, &in);
1224 return -error;
1225 }
1226
1227 case XFS_IOC_FSGROWFSRT: {
1228 xfs_growfs_rt_t in;
1229
1230 if (!capable(CAP_SYS_ADMIN))
1231 return -EPERM;
1232
1233 if (copy_from_user(&in, arg, sizeof(in)))
1234 return -XFS_ERROR(EFAULT);
1235
1236 error = xfs_growfs_rt(mp, &in);
1237 return -error;
1238 }
1239
1240 case XFS_IOC_FREEZE:
1241 if (!capable(CAP_SYS_ADMIN))
1242 return -EPERM;
1243
1244 if (inode->i_sb->s_frozen == SB_UNFROZEN)
1245 freeze_bdev(inode->i_sb->s_bdev);
1246 return 0;
1247
1248 case XFS_IOC_THAW:
1249 if (!capable(CAP_SYS_ADMIN))
1250 return -EPERM;
1251 if (inode->i_sb->s_frozen != SB_UNFROZEN)
1252 thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
1253 return 0;
1254
1255 case XFS_IOC_GOINGDOWN: {
1256 __uint32_t in;
1257
1258 if (!capable(CAP_SYS_ADMIN))
1259 return -EPERM;
1260
1261 if (get_user(in, (__uint32_t __user *)arg))
1262 return -XFS_ERROR(EFAULT);
1263
1264 error = xfs_fs_goingdown(mp, in);
1265 return -error;
1266 }
1267
1268 case XFS_IOC_ERROR_INJECTION: {
1269 xfs_error_injection_t in;
1270
1271 if (!capable(CAP_SYS_ADMIN))
1272 return -EPERM;
1273
1274 if (copy_from_user(&in, arg, sizeof(in)))
1275 return -XFS_ERROR(EFAULT);
1276
1277 error = xfs_errortag_add(in.errtag, mp);
1278 return -error;
1279 }
1280
1281 case XFS_IOC_ERROR_CLEARALL:
1282 if (!capable(CAP_SYS_ADMIN))
1283 return -EPERM;
1284
1285 error = xfs_errortag_clearall(mp, 1);
1286 return -error;
1287
1288 default:
1289 return -ENOTTY;
1290 }
1291}