blob: 620bb2d6d26c44ab8b1e22d800075d3013f08e59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2004-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 <linux/compat.h>
19#include <linux/init.h>
20#include <linux/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/syscalls.h>
22#include <linux/types.h>
23#include <linux/fs.h>
24#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_fs.h"
Michal Marekfaa63e92007-07-11 11:10:19 +100027#include "xfs_bit.h"
28#include "xfs_log.h"
29#include "xfs_inum.h"
30#include "xfs_trans.h"
31#include "xfs_sb.h"
32#include "xfs_ag.h"
33#include "xfs_dir2.h"
34#include "xfs_dmapi.h"
35#include "xfs_mount.h"
36#include "xfs_bmap_btree.h"
37#include "xfs_attr_sf.h"
38#include "xfs_dir2_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_vnode.h"
Michal Marekfaa63e92007-07-11 11:10:19 +100040#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_itable.h"
43#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_dfrag.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100045#include "xfs_vnodeops.h"
David Chinnera8272ce2007-11-23 16:28:09 +110046#include "xfs_ioctl32.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Eric Sandeen526c4202005-09-05 08:25:06 +100048#define _NATIVE_IOC(cmd, type) \
49 _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
50
sandeen@sandeen.netffae2632008-11-25 21:20:07 -060051#ifdef BROKEN_X86_ALIGNMENT
Eric Sandeen526c4202005-09-05 08:25:06 +100052STATIC unsigned long
53xfs_ioctl32_flock(
54 unsigned long arg)
55{
sandeen@sandeen.netffae2632008-11-25 21:20:07 -060056 compat_xfs_flock64_t __user *p32 = (void __user *)arg;
Eric Sandeen526c4202005-09-05 08:25:06 +100057 xfs_flock64_t __user *p = compat_alloc_user_space(sizeof(*p));
58
59 if (copy_in_user(&p->l_type, &p32->l_type, sizeof(s16)) ||
60 copy_in_user(&p->l_whence, &p32->l_whence, sizeof(s16)) ||
61 copy_in_user(&p->l_start, &p32->l_start, sizeof(s64)) ||
62 copy_in_user(&p->l_len, &p32->l_len, sizeof(s64)) ||
63 copy_in_user(&p->l_sysid, &p32->l_sysid, sizeof(s32)) ||
64 copy_in_user(&p->l_pid, &p32->l_pid, sizeof(u32)) ||
65 copy_in_user(&p->l_pad, &p32->l_pad, 4*sizeof(u32)))
66 return -EFAULT;
Nathan Scotte0edd5962006-03-29 08:55:47 +100067
Eric Sandeen526c4202005-09-05 08:25:06 +100068 return (unsigned long)p;
69}
70
Michal Marek547e00c2007-07-11 11:09:57 +100071STATIC unsigned long xfs_ioctl32_geom_v1(unsigned long arg)
72{
73 compat_xfs_fsop_geom_v1_t __user *p32 = (void __user *)arg;
74 xfs_fsop_geom_v1_t __user *p = compat_alloc_user_space(sizeof(*p));
75
76 if (copy_in_user(p, p32, sizeof(*p32)))
77 return -EFAULT;
78 return (unsigned long)p;
79}
80
Michal Marekfaa63e92007-07-11 11:10:19 +100081STATIC int xfs_inumbers_fmt_compat(
82 void __user *ubuffer,
83 const xfs_inogrp_t *buffer,
84 long count,
85 long *written)
86{
Al Viroad690ef2007-07-26 17:35:59 +010087 compat_xfs_inogrp_t __user *p32 = ubuffer;
Michal Marekfaa63e92007-07-11 11:10:19 +100088 long i;
89
90 for (i = 0; i < count; i++) {
91 if (put_user(buffer[i].xi_startino, &p32[i].xi_startino) ||
92 put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) ||
93 put_user(buffer[i].xi_allocmask, &p32[i].xi_allocmask))
94 return -EFAULT;
95 }
96 *written = count * sizeof(*p32);
97 return 0;
98}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#else
Michal Marekfaa63e92007-07-11 11:10:19 +1000101#define xfs_inumbers_fmt_compat xfs_inumbers_fmt
Michal Marekfaa63e92007-07-11 11:10:19 +1000102#endif
103
104/* XFS_IOC_FSBULKSTAT and friends */
105
Michal Marekfaa63e92007-07-11 11:10:19 +1000106STATIC int xfs_bstime_store_compat(
107 compat_xfs_bstime_t __user *p32,
108 const xfs_bstime_t *p)
109{
110 __s32 sec32;
111
112 sec32 = p->tv_sec;
113 if (put_user(sec32, &p32->tv_sec) ||
114 put_user(p->tv_nsec, &p32->tv_nsec))
115 return -EFAULT;
116 return 0;
117}
118
Michal Marekfaa63e92007-07-11 11:10:19 +1000119STATIC int xfs_bulkstat_one_fmt_compat(
120 void __user *ubuffer,
121 const xfs_bstat_t *buffer)
122{
123 compat_xfs_bstat_t __user *p32 = ubuffer;
124
125 if (put_user(buffer->bs_ino, &p32->bs_ino) ||
126 put_user(buffer->bs_mode, &p32->bs_mode) ||
127 put_user(buffer->bs_nlink, &p32->bs_nlink) ||
128 put_user(buffer->bs_uid, &p32->bs_uid) ||
129 put_user(buffer->bs_gid, &p32->bs_gid) ||
130 put_user(buffer->bs_rdev, &p32->bs_rdev) ||
131 put_user(buffer->bs_blksize, &p32->bs_blksize) ||
132 put_user(buffer->bs_size, &p32->bs_size) ||
133 xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
134 xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
135 xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
136 put_user(buffer->bs_blocks, &p32->bs_blocks) ||
137 put_user(buffer->bs_xflags, &p32->bs_xflags) ||
138 put_user(buffer->bs_extsize, &p32->bs_extsize) ||
139 put_user(buffer->bs_extents, &p32->bs_extents) ||
140 put_user(buffer->bs_gen, &p32->bs_gen) ||
141 put_user(buffer->bs_projid, &p32->bs_projid) ||
142 put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) ||
143 put_user(buffer->bs_dmstate, &p32->bs_dmstate) ||
144 put_user(buffer->bs_aextents, &p32->bs_aextents))
145 return -EFAULT;
146 return sizeof(*p32);
147}
148
Michal Marekfaa63e92007-07-11 11:10:19 +1000149/* copied from xfs_ioctl.c */
150STATIC int
151xfs_ioc_bulkstat_compat(
152 xfs_mount_t *mp,
153 unsigned int cmd,
154 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Michal Marekfaa63e92007-07-11 11:10:19 +1000156 compat_xfs_fsop_bulkreq_t __user *p32 = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 u32 addr;
Michal Marekfaa63e92007-07-11 11:10:19 +1000158 xfs_fsop_bulkreq_t bulkreq;
159 int count; /* # of records returned */
160 xfs_ino_t inlast; /* last inode number */
161 int done;
162 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Michal Marekfaa63e92007-07-11 11:10:19 +1000164 /* done = 1 if there are more stats to get and if bulkstat */
165 /* should be called again (unused here, but used in dmapi) */
166
167 if (!capable(CAP_SYS_ADMIN))
168 return -EPERM;
169
170 if (XFS_FORCED_SHUTDOWN(mp))
171 return -XFS_ERROR(EIO);
172
173 if (get_user(addr, &p32->lastip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -EFAULT;
Michal Marekfaa63e92007-07-11 11:10:19 +1000175 bulkreq.lastip = compat_ptr(addr);
176 if (get_user(bulkreq.icount, &p32->icount) ||
177 get_user(addr, &p32->ubuffer))
178 return -EFAULT;
179 bulkreq.ubuffer = compat_ptr(addr);
180 if (get_user(addr, &p32->ocount))
181 return -EFAULT;
182 bulkreq.ocount = compat_ptr(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Michal Marekfaa63e92007-07-11 11:10:19 +1000184 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
185 return -XFS_ERROR(EFAULT);
186
187 if ((count = bulkreq.icount) <= 0)
188 return -XFS_ERROR(EINVAL);
189
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100190 if (bulkreq.ubuffer == NULL)
191 return -XFS_ERROR(EINVAL);
192
Michal Marekfaa63e92007-07-11 11:10:19 +1000193 if (cmd == XFS_IOC_FSINUMBERS)
194 error = xfs_inumbers(mp, &inlast, &count,
195 bulkreq.ubuffer, xfs_inumbers_fmt_compat);
196 else {
197 /* declare a var to get a warning in case the type changes */
198 bulkstat_one_fmt_pf formatter = xfs_bulkstat_one_fmt_compat;
199 error = xfs_bulkstat(mp, &inlast, &count,
200 xfs_bulkstat_one, formatter,
201 sizeof(compat_xfs_bstat_t), bulkreq.ubuffer,
202 BULKSTAT_FG_QUICK, &done);
203 }
204 if (error)
205 return -error;
206
207 if (bulkreq.ocount != NULL) {
208 if (copy_to_user(bulkreq.lastip, &inlast,
209 sizeof(xfs_ino_t)))
210 return -XFS_ERROR(EFAULT);
211
212 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
213 return -XFS_ERROR(EFAULT);
214 }
215
216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
Michal Marekfaa63e92007-07-11 11:10:19 +1000218
Michal Marek1fa503d2007-07-11 11:10:09 +1000219STATIC unsigned long xfs_ioctl32_fshandle(unsigned long arg)
220{
221 compat_xfs_fsop_handlereq_t __user *p32 = (void __user *)arg;
222 xfs_fsop_handlereq_t __user *p = compat_alloc_user_space(sizeof(*p));
223 u32 addr;
224
225 if (copy_in_user(&p->fd, &p32->fd, sizeof(__u32)) ||
226 get_user(addr, &p32->path) ||
227 put_user(compat_ptr(addr), &p->path) ||
228 copy_in_user(&p->oflags, &p32->oflags, sizeof(__u32)) ||
229 get_user(addr, &p32->ihandle) ||
230 put_user(compat_ptr(addr), &p->ihandle) ||
231 copy_in_user(&p->ihandlen, &p32->ihandlen, sizeof(__u32)) ||
232 get_user(addr, &p32->ohandle) ||
233 put_user(compat_ptr(addr), &p->ohandle) ||
234 get_user(addr, &p32->ohandlen) ||
235 put_user(compat_ptr(addr), &p->ohandlen))
236 return -EFAULT;
237
238 return (unsigned long)p;
239}
240
Nathan Scottd3870392005-05-06 06:44:46 -0700241STATIC long
Nathan Scotte0edd5962006-03-29 08:55:47 +1000242xfs_compat_ioctl(
243 int mode,
244 struct file *file,
245 unsigned cmd,
246 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800248 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 switch (cmd) {
252 case XFS_IOC_DIOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 case XFS_IOC_FSGEOMETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 case XFS_IOC_FSGETXATTR:
255 case XFS_IOC_FSSETXATTR:
256 case XFS_IOC_FSGETXATTRA:
257 case XFS_IOC_FSSETDM:
258 case XFS_IOC_GETBMAP:
259 case XFS_IOC_GETBMAPA:
260 case XFS_IOC_GETBMAPX:
261/* not handled
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 case XFS_IOC_FSSETDM_BY_HANDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 case XFS_IOC_ATTRLIST_BY_HANDLE:
264 case XFS_IOC_ATTRMULTI_BY_HANDLE:
265*/
266 case XFS_IOC_FSCOUNTS:
267 case XFS_IOC_SET_RESBLKS:
268 case XFS_IOC_GET_RESBLKS:
269 case XFS_IOC_FSGROWFSDATA:
270 case XFS_IOC_FSGROWFSLOG:
271 case XFS_IOC_FSGROWFSRT:
272 case XFS_IOC_FREEZE:
273 case XFS_IOC_THAW:
274 case XFS_IOC_GOINGDOWN:
275 case XFS_IOC_ERROR_INJECTION:
276 case XFS_IOC_ERROR_CLEARALL:
277 break;
278
sandeen@sandeen.netffae2632008-11-25 21:20:07 -0600279 case XFS_IOC_GETXFLAGS_32:
280 case XFS_IOC_SETXFLAGS_32:
281 case XFS_IOC_GETVERSION_32:
Eric Sandeenbc58f9b2007-10-12 11:13:22 +1000282 cmd = _NATIVE_IOC(cmd, long);
283 break;
Eric Sandeen526c4202005-09-05 08:25:06 +1000284#ifdef BROKEN_X86_ALIGNMENT
285 /* xfs_flock_t has wrong u32 vs u64 alignment */
286 case XFS_IOC_ALLOCSP_32:
287 case XFS_IOC_FREESP_32:
288 case XFS_IOC_ALLOCSP64_32:
289 case XFS_IOC_FREESP64_32:
290 case XFS_IOC_RESVSP_32:
291 case XFS_IOC_UNRESVSP_32:
292 case XFS_IOC_RESVSP64_32:
293 case XFS_IOC_UNRESVSP64_32:
294 arg = xfs_ioctl32_flock(arg);
295 cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
296 break;
Michal Marek547e00c2007-07-11 11:09:57 +1000297 case XFS_IOC_FSGEOMETRY_V1_32:
298 arg = xfs_ioctl32_geom_v1(arg);
299 cmd = _NATIVE_IOC(cmd, struct xfs_fsop_geom_v1);
300 break;
Eric Sandeen526c4202005-09-05 08:25:06 +1000301#else /* These are handled fine if no alignment issues */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 case XFS_IOC_ALLOCSP:
303 case XFS_IOC_FREESP:
304 case XFS_IOC_RESVSP:
305 case XFS_IOC_UNRESVSP:
306 case XFS_IOC_ALLOCSP64:
307 case XFS_IOC_FREESP64:
308 case XFS_IOC_RESVSP64:
309 case XFS_IOC_UNRESVSP64:
Michal Marek547e00c2007-07-11 11:09:57 +1000310 case XFS_IOC_FSGEOMETRY_V1:
Eric Sandeen526c4202005-09-05 08:25:06 +1000311 break;
312
313 /* xfs_bstat_t still has wrong u32 vs u64 alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case XFS_IOC_SWAPEXT:
315 break;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#endif
Michal Marekfaa63e92007-07-11 11:10:19 +1000318 case XFS_IOC_FSBULKSTAT_32:
319 case XFS_IOC_FSBULKSTAT_SINGLE_32:
320 case XFS_IOC_FSINUMBERS_32:
321 cmd = _NATIVE_IOC(cmd, struct xfs_fsop_bulkreq);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000322 return xfs_ioc_bulkstat_compat(XFS_I(inode)->i_mount,
Al Viroad690ef2007-07-26 17:35:59 +0100323 cmd, (void __user*)arg);
Michal Marek1fa503d2007-07-11 11:10:09 +1000324 case XFS_IOC_FD_TO_HANDLE_32:
325 case XFS_IOC_PATH_TO_HANDLE_32:
326 case XFS_IOC_PATH_TO_FSHANDLE_32:
327 case XFS_IOC_OPEN_BY_HANDLE_32:
328 case XFS_IOC_READLINK_BY_HANDLE_32:
329 arg = xfs_ioctl32_fshandle(arg);
330 cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 default:
333 return -ENOIOCTLCMD;
334 }
335
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000336 error = xfs_ioctl(XFS_I(inode), file, mode, cmd, (void __user *)arg);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000337 xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return error;
339}
340
Nathan Scottd3870392005-05-06 06:44:46 -0700341long
Nathan Scotta50cd262006-03-14 14:06:18 +1100342xfs_file_compat_ioctl(
Nathan Scotte0edd5962006-03-29 08:55:47 +1000343 struct file *file,
Nathan Scottd3870392005-05-06 06:44:46 -0700344 unsigned cmd,
345 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Nathan Scotte0edd5962006-03-29 08:55:47 +1000347 return xfs_compat_ioctl(0, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Nathan Scottd3870392005-05-06 06:44:46 -0700350long
Nathan Scotta50cd262006-03-14 14:06:18 +1100351xfs_file_compat_invis_ioctl(
Nathan Scotte0edd5962006-03-29 08:55:47 +1000352 struct file *file,
Nathan Scottd3870392005-05-06 06:44:46 -0700353 unsigned cmd,
354 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Nathan Scotte0edd5962006-03-29 08:55:47 +1000356 return xfs_compat_ioctl(IO_INVIS, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}