blob: 17993352aa647196fd7b5f0b93ecf0cda9787e52 [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"
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060046#include "xfs_fsops.h"
sandeen@sandeen.net471d5912008-11-25 21:20:10 -060047#include "xfs_alloc.h"
48#include "xfs_rtalloc.h"
sandeen@sandeen.netebeecd22008-11-25 21:20:14 -060049#include "xfs_attr.h"
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060050#include "xfs_ioctl.h"
David Chinnera8272ce2007-11-23 16:28:09 +110051#include "xfs_ioctl32.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Eric Sandeen526c4202005-09-05 08:25:06 +100053#define _NATIVE_IOC(cmd, type) \
54 _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
55
sandeen@sandeen.netffae2632008-11-25 21:20:07 -060056#ifdef BROKEN_X86_ALIGNMENT
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060057STATIC int
58xfs_compat_flock64_copyin(
59 xfs_flock64_t *bf,
60 compat_xfs_flock64_t __user *arg32)
Eric Sandeen526c4202005-09-05 08:25:06 +100061{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060062 if (get_user(bf->l_type, &arg32->l_type) ||
63 get_user(bf->l_whence, &arg32->l_whence) ||
64 get_user(bf->l_start, &arg32->l_start) ||
65 get_user(bf->l_len, &arg32->l_len) ||
66 get_user(bf->l_sysid, &arg32->l_sysid) ||
67 get_user(bf->l_pid, &arg32->l_pid) ||
68 copy_from_user(bf->l_pad, &arg32->l_pad, 4*sizeof(u32)))
69 return -XFS_ERROR(EFAULT);
70 return 0;
Eric Sandeen526c4202005-09-05 08:25:06 +100071}
72
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060073STATIC int
74xfs_compat_ioc_fsgeometry_v1(
75 struct xfs_mount *mp,
76 compat_xfs_fsop_geom_v1_t __user *arg32)
Michal Marek547e00c2007-07-11 11:09:57 +100077{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060078 xfs_fsop_geom_t fsgeo;
79 int error;
Michal Marek547e00c2007-07-11 11:09:57 +100080
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060081 error = xfs_fs_geometry(mp, &fsgeo, 3);
82 if (error)
83 return -error;
84 /* The 32-bit variant simply has some padding at the end */
85 if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
86 return -XFS_ERROR(EFAULT);
87 return 0;
Michal Marek547e00c2007-07-11 11:09:57 +100088}
89
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -060090STATIC int
sandeen@sandeen.net471d5912008-11-25 21:20:10 -060091xfs_compat_growfs_data_copyin(
92 struct xfs_growfs_data *in,
93 compat_xfs_growfs_data_t __user *arg32)
94{
95 if (get_user(in->newblocks, &arg32->newblocks) ||
96 get_user(in->imaxpct, &arg32->imaxpct))
97 return -XFS_ERROR(EFAULT);
98 return 0;
99}
100
101STATIC int
102xfs_compat_growfs_rt_copyin(
103 struct xfs_growfs_rt *in,
104 compat_xfs_growfs_rt_t __user *arg32)
105{
106 if (get_user(in->newblocks, &arg32->newblocks) ||
107 get_user(in->extsize, &arg32->extsize))
108 return -XFS_ERROR(EFAULT);
109 return 0;
110}
111
112STATIC int
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600113xfs_inumbers_fmt_compat(
114 void __user *ubuffer,
115 const xfs_inogrp_t *buffer,
116 long count,
117 long *written)
Michal Marekfaa63e92007-07-11 11:10:19 +1000118{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600119 compat_xfs_inogrp_t __user *p32 = ubuffer;
120 long i;
Michal Marekfaa63e92007-07-11 11:10:19 +1000121
122 for (i = 0; i < count; i++) {
123 if (put_user(buffer[i].xi_startino, &p32[i].xi_startino) ||
124 put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) ||
125 put_user(buffer[i].xi_allocmask, &p32[i].xi_allocmask))
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600126 return -XFS_ERROR(EFAULT);
Michal Marekfaa63e92007-07-11 11:10:19 +1000127 }
128 *written = count * sizeof(*p32);
129 return 0;
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#else
Michal Marekfaa63e92007-07-11 11:10:19 +1000133#define xfs_inumbers_fmt_compat xfs_inumbers_fmt
Michal Marekfaa63e92007-07-11 11:10:19 +1000134#endif
135
sandeen@sandeen.nete94fc4a2008-11-25 21:20:09 -0600136STATIC int
137xfs_ioctl32_bstime_copyin(
138 xfs_bstime_t *bstime,
139 compat_xfs_bstime_t __user *bstime32)
140{
141 compat_time_t sec32; /* tv_sec differs on 64 vs. 32 */
142
143 if (get_user(sec32, &bstime32->tv_sec) ||
144 get_user(bstime->tv_nsec, &bstime32->tv_nsec))
145 return -XFS_ERROR(EFAULT);
146 bstime->tv_sec = sec32;
147 return 0;
148}
149
150/* xfs_bstat_t has differing alignment on intel, & bstime_t sizes everywhere */
151STATIC int
152xfs_ioctl32_bstat_copyin(
153 xfs_bstat_t *bstat,
154 compat_xfs_bstat_t __user *bstat32)
155{
156 if (get_user(bstat->bs_ino, &bstat32->bs_ino) ||
157 get_user(bstat->bs_mode, &bstat32->bs_mode) ||
158 get_user(bstat->bs_nlink, &bstat32->bs_nlink) ||
159 get_user(bstat->bs_uid, &bstat32->bs_uid) ||
160 get_user(bstat->bs_gid, &bstat32->bs_gid) ||
161 get_user(bstat->bs_rdev, &bstat32->bs_rdev) ||
162 get_user(bstat->bs_blksize, &bstat32->bs_blksize) ||
163 get_user(bstat->bs_size, &bstat32->bs_size) ||
164 xfs_ioctl32_bstime_copyin(&bstat->bs_atime, &bstat32->bs_atime) ||
165 xfs_ioctl32_bstime_copyin(&bstat->bs_mtime, &bstat32->bs_mtime) ||
166 xfs_ioctl32_bstime_copyin(&bstat->bs_ctime, &bstat32->bs_ctime) ||
167 get_user(bstat->bs_blocks, &bstat32->bs_size) ||
168 get_user(bstat->bs_xflags, &bstat32->bs_size) ||
169 get_user(bstat->bs_extsize, &bstat32->bs_extsize) ||
170 get_user(bstat->bs_extents, &bstat32->bs_extents) ||
171 get_user(bstat->bs_gen, &bstat32->bs_gen) ||
172 get_user(bstat->bs_projid, &bstat32->bs_projid) ||
173 get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask) ||
174 get_user(bstat->bs_dmstate, &bstat32->bs_dmstate) ||
175 get_user(bstat->bs_aextents, &bstat32->bs_aextents))
176 return -XFS_ERROR(EFAULT);
177 return 0;
178}
179
Michal Marekfaa63e92007-07-11 11:10:19 +1000180/* XFS_IOC_FSBULKSTAT and friends */
181
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600182STATIC int
183xfs_bstime_store_compat(
184 compat_xfs_bstime_t __user *p32,
185 const xfs_bstime_t *p)
Michal Marekfaa63e92007-07-11 11:10:19 +1000186{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600187 __s32 sec32;
Michal Marekfaa63e92007-07-11 11:10:19 +1000188
189 sec32 = p->tv_sec;
190 if (put_user(sec32, &p32->tv_sec) ||
191 put_user(p->tv_nsec, &p32->tv_nsec))
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600192 return -XFS_ERROR(EFAULT);
Michal Marekfaa63e92007-07-11 11:10:19 +1000193 return 0;
194}
195
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600196/* Return 0 on success or positive error (to xfs_bulkstat()) */
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600197STATIC int
198xfs_bulkstat_one_fmt_compat(
Michal Marekfaa63e92007-07-11 11:10:19 +1000199 void __user *ubuffer,
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600200 int ubsize,
201 int *ubused,
Michal Marekfaa63e92007-07-11 11:10:19 +1000202 const xfs_bstat_t *buffer)
203{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600204 compat_xfs_bstat_t __user *p32 = ubuffer;
Michal Marekfaa63e92007-07-11 11:10:19 +1000205
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600206 if (ubsize < sizeof(*p32))
207 return XFS_ERROR(ENOMEM);
208
209 if (put_user(buffer->bs_ino, &p32->bs_ino) ||
210 put_user(buffer->bs_mode, &p32->bs_mode) ||
211 put_user(buffer->bs_nlink, &p32->bs_nlink) ||
212 put_user(buffer->bs_uid, &p32->bs_uid) ||
213 put_user(buffer->bs_gid, &p32->bs_gid) ||
214 put_user(buffer->bs_rdev, &p32->bs_rdev) ||
215 put_user(buffer->bs_blksize, &p32->bs_blksize) ||
216 put_user(buffer->bs_size, &p32->bs_size) ||
Michal Marekfaa63e92007-07-11 11:10:19 +1000217 xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
218 xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
219 xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600220 put_user(buffer->bs_blocks, &p32->bs_blocks) ||
221 put_user(buffer->bs_xflags, &p32->bs_xflags) ||
222 put_user(buffer->bs_extsize, &p32->bs_extsize) ||
223 put_user(buffer->bs_extents, &p32->bs_extents) ||
224 put_user(buffer->bs_gen, &p32->bs_gen) ||
225 put_user(buffer->bs_projid, &p32->bs_projid) ||
226 put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) ||
227 put_user(buffer->bs_dmstate, &p32->bs_dmstate) ||
Michal Marekfaa63e92007-07-11 11:10:19 +1000228 put_user(buffer->bs_aextents, &p32->bs_aextents))
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600229 return XFS_ERROR(EFAULT);
230 if (ubused)
231 *ubused = sizeof(*p32);
232 return 0;
Michal Marekfaa63e92007-07-11 11:10:19 +1000233}
234
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600235STATIC int
236xfs_bulkstat_one_compat(
237 xfs_mount_t *mp, /* mount point for filesystem */
238 xfs_ino_t ino, /* inode number to get data for */
239 void __user *buffer, /* buffer to place output in */
240 int ubsize, /* size of buffer */
241 void *private_data, /* my private data */
242 xfs_daddr_t bno, /* starting bno of inode cluster */
243 int *ubused, /* bytes used by me */
244 void *dibuff, /* on-disk inode buffer */
245 int *stat) /* BULKSTAT_RV_... */
246{
247 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
248 xfs_bulkstat_one_fmt_compat, bno,
249 ubused, dibuff, stat);
250}
251
Michal Marekfaa63e92007-07-11 11:10:19 +1000252/* copied from xfs_ioctl.c */
253STATIC int
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600254xfs_compat_ioc_bulkstat(
255 xfs_mount_t *mp,
256 unsigned int cmd,
257 compat_xfs_fsop_bulkreq_t __user *p32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 u32 addr;
Michal Marekfaa63e92007-07-11 11:10:19 +1000260 xfs_fsop_bulkreq_t bulkreq;
261 int count; /* # of records returned */
262 xfs_ino_t inlast; /* last inode number */
263 int done;
264 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Michal Marekfaa63e92007-07-11 11:10:19 +1000266 /* done = 1 if there are more stats to get and if bulkstat */
267 /* should be called again (unused here, but used in dmapi) */
268
269 if (!capable(CAP_SYS_ADMIN))
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600270 return -XFS_ERROR(EPERM);
Michal Marekfaa63e92007-07-11 11:10:19 +1000271
272 if (XFS_FORCED_SHUTDOWN(mp))
273 return -XFS_ERROR(EIO);
274
275 if (get_user(addr, &p32->lastip))
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600276 return -XFS_ERROR(EFAULT);
Michal Marekfaa63e92007-07-11 11:10:19 +1000277 bulkreq.lastip = compat_ptr(addr);
278 if (get_user(bulkreq.icount, &p32->icount) ||
279 get_user(addr, &p32->ubuffer))
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600280 return -XFS_ERROR(EFAULT);
Michal Marekfaa63e92007-07-11 11:10:19 +1000281 bulkreq.ubuffer = compat_ptr(addr);
282 if (get_user(addr, &p32->ocount))
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600283 return -XFS_ERROR(EFAULT);
Michal Marekfaa63e92007-07-11 11:10:19 +1000284 bulkreq.ocount = compat_ptr(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Michal Marekfaa63e92007-07-11 11:10:19 +1000286 if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
287 return -XFS_ERROR(EFAULT);
288
289 if ((count = bulkreq.icount) <= 0)
290 return -XFS_ERROR(EINVAL);
291
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100292 if (bulkreq.ubuffer == NULL)
293 return -XFS_ERROR(EINVAL);
294
sandeen@sandeen.netaf819d22008-11-25 21:20:13 -0600295 if (cmd == XFS_IOC_FSINUMBERS_32) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000296 error = xfs_inumbers(mp, &inlast, &count,
297 bulkreq.ubuffer, xfs_inumbers_fmt_compat);
sandeen@sandeen.netaf819d22008-11-25 21:20:13 -0600298 } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE_32) {
299 int res;
300
301 error = xfs_bulkstat_one_compat(mp, inlast, bulkreq.ubuffer,
302 sizeof(compat_xfs_bstat_t),
303 NULL, 0, NULL, NULL, &res);
304 } else if (cmd == XFS_IOC_FSBULKSTAT_32) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000305 error = xfs_bulkstat(mp, &inlast, &count,
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600306 xfs_bulkstat_one_compat, NULL,
Michal Marekfaa63e92007-07-11 11:10:19 +1000307 sizeof(compat_xfs_bstat_t), bulkreq.ubuffer,
308 BULKSTAT_FG_QUICK, &done);
sandeen@sandeen.netaf819d22008-11-25 21:20:13 -0600309 } else
310 error = XFS_ERROR(EINVAL);
Michal Marekfaa63e92007-07-11 11:10:19 +1000311 if (error)
312 return -error;
313
314 if (bulkreq.ocount != NULL) {
315 if (copy_to_user(bulkreq.lastip, &inlast,
316 sizeof(xfs_ino_t)))
317 return -XFS_ERROR(EFAULT);
318
319 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
320 return -XFS_ERROR(EFAULT);
321 }
322
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
Michal Marekfaa63e92007-07-11 11:10:19 +1000325
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600326STATIC int
327xfs_compat_handlereq_copyin(
328 xfs_fsop_handlereq_t *hreq,
329 compat_xfs_fsop_handlereq_t __user *arg32)
Michal Marek1fa503d2007-07-11 11:10:09 +1000330{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600331 compat_xfs_fsop_handlereq_t hreq32;
Michal Marek1fa503d2007-07-11 11:10:09 +1000332
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600333 if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
334 return -XFS_ERROR(EFAULT);
Michal Marek1fa503d2007-07-11 11:10:09 +1000335
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600336 hreq->fd = hreq32.fd;
337 hreq->path = compat_ptr(hreq32.path);
338 hreq->oflags = hreq32.oflags;
339 hreq->ihandle = compat_ptr(hreq32.ihandle);
340 hreq->ihandlen = hreq32.ihandlen;
341 hreq->ohandle = compat_ptr(hreq32.ohandle);
342 hreq->ohandlen = compat_ptr(hreq32.ohandlen);
343
344 return 0;
Michal Marek1fa503d2007-07-11 11:10:09 +1000345}
346
sandeen@sandeen.netebeecd22008-11-25 21:20:14 -0600347/*
348 * Convert userspace handle data into inode.
349 *
350 * We use the fact that all the fsop_handlereq ioctl calls have a data
351 * structure argument whose first component is always a xfs_fsop_handlereq_t,
352 * so we can pass that sub structure into this handy, shared routine.
353 *
354 * If no error, caller must always iput the returned inode.
355 */
356STATIC int
357xfs_vget_fsop_handlereq_compat(
358 xfs_mount_t *mp,
359 struct inode *parinode, /* parent inode pointer */
360 compat_xfs_fsop_handlereq_t *hreq,
361 struct inode **inode)
362{
363 void __user *hanp;
364 size_t hlen;
365 xfs_fid_t *xfid;
366 xfs_handle_t *handlep;
367 xfs_handle_t handle;
368 xfs_inode_t *ip;
369 xfs_ino_t ino;
370 __u32 igen;
371 int error;
372
373 /*
374 * Only allow handle opens under a directory.
375 */
376 if (!S_ISDIR(parinode->i_mode))
377 return XFS_ERROR(ENOTDIR);
378
379 hanp = compat_ptr(hreq->ihandle);
380 hlen = hreq->ihandlen;
381 handlep = &handle;
382
383 if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
384 return XFS_ERROR(EINVAL);
385 if (copy_from_user(handlep, hanp, hlen))
386 return XFS_ERROR(EFAULT);
387 if (hlen < sizeof(*handlep))
388 memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
389 if (hlen > sizeof(handlep->ha_fsid)) {
390 if (handlep->ha_fid.fid_len !=
391 (hlen - sizeof(handlep->ha_fsid) -
392 sizeof(handlep->ha_fid.fid_len)) ||
393 handlep->ha_fid.fid_pad)
394 return XFS_ERROR(EINVAL);
395 }
396
397 /*
398 * Crack the handle, obtain the inode # & generation #
399 */
400 xfid = (struct xfs_fid *)&handlep->ha_fid;
401 if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
402 ino = xfid->fid_ino;
403 igen = xfid->fid_gen;
404 } else {
405 return XFS_ERROR(EINVAL);
406 }
407
408 /*
409 * Get the XFS inode, building a Linux inode to go with it.
410 */
411 error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
412 if (error)
413 return error;
414 if (ip == NULL)
415 return XFS_ERROR(EIO);
416 if (ip->i_d.di_gen != igen) {
417 xfs_iput_new(ip, XFS_ILOCK_SHARED);
418 return XFS_ERROR(ENOENT);
419 }
420
421 xfs_iunlock(ip, XFS_ILOCK_SHARED);
422
423 *inode = VFS_I(ip);
424 return 0;
425}
426
427STATIC int
428xfs_compat_attrlist_by_handle(
429 xfs_mount_t *mp,
430 void __user *arg,
431 struct inode *parinode)
432{
433 int error;
434 attrlist_cursor_kern_t *cursor;
435 compat_xfs_fsop_attrlist_handlereq_t al_hreq;
436 struct inode *inode;
437 char *kbuf;
438
439 if (!capable(CAP_SYS_ADMIN))
440 return -XFS_ERROR(EPERM);
441 if (copy_from_user(&al_hreq, arg,
442 sizeof(compat_xfs_fsop_attrlist_handlereq_t)))
443 return -XFS_ERROR(EFAULT);
444 if (al_hreq.buflen > XATTR_LIST_MAX)
445 return -XFS_ERROR(EINVAL);
446
447 /*
448 * Reject flags, only allow namespaces.
449 */
450 if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
451 return -XFS_ERROR(EINVAL);
452
453 error = xfs_vget_fsop_handlereq_compat(mp, parinode, &al_hreq.hreq,
454 &inode);
455 if (error)
456 goto out;
457
458 kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
459 if (!kbuf)
460 goto out_vn_rele;
461
462 cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
463 error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
464 al_hreq.flags, cursor);
465 if (error)
466 goto out_kfree;
467
468 if (copy_to_user(compat_ptr(al_hreq.buffer), kbuf, al_hreq.buflen))
469 error = -EFAULT;
470
471 out_kfree:
472 kfree(kbuf);
473 out_vn_rele:
474 iput(inode);
475 out:
476 return -error;
477}
478
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600479STATIC int
480xfs_compat_attrmulti_by_handle(
481 xfs_mount_t *mp,
482 void __user *arg,
483 struct inode *parinode)
484{
485 int error;
486 compat_xfs_attr_multiop_t *ops;
487 compat_xfs_fsop_attrmulti_handlereq_t am_hreq;
488 struct inode *inode;
489 unsigned int i, size;
490 char *attr_name;
491
492 if (!capable(CAP_SYS_ADMIN))
493 return -XFS_ERROR(EPERM);
494 if (copy_from_user(&am_hreq, arg,
495 sizeof(compat_xfs_fsop_attrmulti_handlereq_t)))
496 return -XFS_ERROR(EFAULT);
497
498 error = xfs_vget_fsop_handlereq_compat(mp, parinode, &am_hreq.hreq,
499 &inode);
500 if (error)
501 goto out;
502
503 error = E2BIG;
504 size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t);
505 if (!size || size > 16 * PAGE_SIZE)
506 goto out_vn_rele;
507
508 error = ENOMEM;
509 ops = kmalloc(size, GFP_KERNEL);
510 if (!ops)
511 goto out_vn_rele;
512
513 error = EFAULT;
514 if (copy_from_user(ops, compat_ptr(am_hreq.ops), size))
515 goto out_kfree_ops;
516
517 attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
518 if (!attr_name)
519 goto out_kfree_ops;
520
521
522 error = 0;
523 for (i = 0; i < am_hreq.opcount; i++) {
524 ops[i].am_error = strncpy_from_user(attr_name,
525 compat_ptr(ops[i].am_attrname),
526 MAXNAMELEN);
527 if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
528 error = -ERANGE;
529 if (ops[i].am_error < 0)
530 break;
531
532 switch (ops[i].am_opcode) {
533 case ATTR_OP_GET:
534 ops[i].am_error = xfs_attrmulti_attr_get(inode,
535 attr_name,
536 compat_ptr(ops[i].am_attrvalue),
537 &ops[i].am_length, ops[i].am_flags);
538 break;
539 case ATTR_OP_SET:
540 ops[i].am_error = xfs_attrmulti_attr_set(inode,
541 attr_name,
542 compat_ptr(ops[i].am_attrvalue),
543 ops[i].am_length, ops[i].am_flags);
544 break;
545 case ATTR_OP_REMOVE:
546 ops[i].am_error = xfs_attrmulti_attr_remove(inode,
547 attr_name, ops[i].am_flags);
548 break;
549 default:
550 ops[i].am_error = EINVAL;
551 }
552 }
553
554 if (copy_to_user(compat_ptr(am_hreq.ops), ops, size))
555 error = XFS_ERROR(EFAULT);
556
557 kfree(attr_name);
558 out_kfree_ops:
559 kfree(ops);
560 out_vn_rele:
561 iput(inode);
562 out:
563 return -error;
564}
565
Nathan Scottd3870392005-05-06 06:44:46 -0700566STATIC long
Nathan Scotte0edd592006-03-29 08:55:47 +1000567xfs_compat_ioctl(
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600568 xfs_inode_t *ip,
569 struct file *filp,
570 int ioflags,
Nathan Scotte0edd592006-03-29 08:55:47 +1000571 unsigned cmd,
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600572 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600574 struct inode *inode = filp->f_path.dentry->d_inode;
575 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600578 xfs_itrace_entry(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 switch (cmd) {
580 case XFS_IOC_DIOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 case XFS_IOC_FSGEOMETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 case XFS_IOC_FSGETXATTR:
583 case XFS_IOC_FSSETXATTR:
584 case XFS_IOC_FSGETXATTRA:
585 case XFS_IOC_FSSETDM:
586 case XFS_IOC_GETBMAP:
587 case XFS_IOC_GETBMAPA:
588 case XFS_IOC_GETBMAPX:
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600589/* case XFS_IOC_FSSETDM_BY_HANDLE: not handled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 case XFS_IOC_FSCOUNTS:
591 case XFS_IOC_SET_RESBLKS:
592 case XFS_IOC_GET_RESBLKS:
593 case XFS_IOC_FSGROWFSDATA:
594 case XFS_IOC_FSGROWFSLOG:
595 case XFS_IOC_FSGROWFSRT:
596 case XFS_IOC_FREEZE:
597 case XFS_IOC_THAW:
598 case XFS_IOC_GOINGDOWN:
599 case XFS_IOC_ERROR_INJECTION:
600 case XFS_IOC_ERROR_CLEARALL:
601 break;
602
sandeen@sandeen.netffae2632008-11-25 21:20:07 -0600603 case XFS_IOC_GETXFLAGS_32:
604 case XFS_IOC_SETXFLAGS_32:
605 case XFS_IOC_GETVERSION_32:
Eric Sandeenbc58f9b2007-10-12 11:13:22 +1000606 cmd = _NATIVE_IOC(cmd, long);
607 break;
sandeen@sandeen.nete94fc4a2008-11-25 21:20:09 -0600608 case XFS_IOC_SWAPEXT: {
609 struct xfs_swapext sxp;
610 struct compat_xfs_swapext __user *sxu = arg;
611
612 /* Bulk copy in up to the sx_stat field, then grab bstat */
613 if (copy_from_user(&sxp, sxu,
614 offsetof(xfs_swapext_t, sx_stat)) ||
615 xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat))
616 return -XFS_ERROR(EFAULT);
617 error = xfs_swapext(&sxp);
618 return -error;
619 }
Eric Sandeen526c4202005-09-05 08:25:06 +1000620#ifdef BROKEN_X86_ALIGNMENT
621 /* xfs_flock_t has wrong u32 vs u64 alignment */
622 case XFS_IOC_ALLOCSP_32:
623 case XFS_IOC_FREESP_32:
624 case XFS_IOC_ALLOCSP64_32:
625 case XFS_IOC_FREESP64_32:
626 case XFS_IOC_RESVSP_32:
627 case XFS_IOC_UNRESVSP_32:
628 case XFS_IOC_RESVSP64_32:
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600629 case XFS_IOC_UNRESVSP64_32: {
630 struct xfs_flock64 bf;
631
632 if (xfs_compat_flock64_copyin(&bf, arg))
633 return -XFS_ERROR(EFAULT);
Eric Sandeen526c4202005-09-05 08:25:06 +1000634 cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600635 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
636 }
Michal Marek547e00c2007-07-11 11:09:57 +1000637 case XFS_IOC_FSGEOMETRY_V1_32:
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600638 return xfs_compat_ioc_fsgeometry_v1(mp, arg);
sandeen@sandeen.net471d5912008-11-25 21:20:10 -0600639 case XFS_IOC_FSGROWFSDATA_32: {
640 struct xfs_growfs_data in;
641
642 if (xfs_compat_growfs_data_copyin(&in, arg))
643 return -XFS_ERROR(EFAULT);
644 error = xfs_growfs_data(mp, &in);
645 return -error;
646 }
647 case XFS_IOC_FSGROWFSRT_32: {
648 struct xfs_growfs_rt in;
649
650 if (xfs_compat_growfs_rt_copyin(&in, arg))
651 return -XFS_ERROR(EFAULT);
652 error = xfs_growfs_rt(mp, &in);
653 return -error;
654 }
Eric Sandeen526c4202005-09-05 08:25:06 +1000655#else /* These are handled fine if no alignment issues */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 case XFS_IOC_ALLOCSP:
657 case XFS_IOC_FREESP:
658 case XFS_IOC_RESVSP:
659 case XFS_IOC_UNRESVSP:
660 case XFS_IOC_ALLOCSP64:
661 case XFS_IOC_FREESP64:
662 case XFS_IOC_RESVSP64:
663 case XFS_IOC_UNRESVSP64:
Michal Marek547e00c2007-07-11 11:09:57 +1000664 case XFS_IOC_FSGEOMETRY_V1:
Eric Sandeen526c4202005-09-05 08:25:06 +1000665 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666#endif
Michal Marekfaa63e92007-07-11 11:10:19 +1000667 case XFS_IOC_FSBULKSTAT_32:
668 case XFS_IOC_FSBULKSTAT_SINGLE_32:
669 case XFS_IOC_FSINUMBERS_32:
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600670 return xfs_compat_ioc_bulkstat(mp, cmd, arg);
Michal Marek1fa503d2007-07-11 11:10:09 +1000671 case XFS_IOC_FD_TO_HANDLE_32:
672 case XFS_IOC_PATH_TO_HANDLE_32:
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600673 case XFS_IOC_PATH_TO_FSHANDLE_32: {
674 struct xfs_fsop_handlereq hreq;
675
676 if (xfs_compat_handlereq_copyin(&hreq, arg))
677 return -XFS_ERROR(EFAULT);
Michal Marek1fa503d2007-07-11 11:10:09 +1000678 cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600679 return xfs_find_handle(cmd, &hreq);
680 }
681 case XFS_IOC_OPEN_BY_HANDLE_32: {
682 struct xfs_fsop_handlereq hreq;
683
684 if (xfs_compat_handlereq_copyin(&hreq, arg))
685 return -XFS_ERROR(EFAULT);
686 return xfs_open_by_handle(mp, &hreq, filp, inode);
687 }
688 case XFS_IOC_READLINK_BY_HANDLE_32: {
689 struct xfs_fsop_handlereq hreq;
690
691 if (xfs_compat_handlereq_copyin(&hreq, arg))
692 return -XFS_ERROR(EFAULT);
693 return xfs_readlink_by_handle(mp, &hreq, inode);
694 }
sandeen@sandeen.netebeecd22008-11-25 21:20:14 -0600695 case XFS_IOC_ATTRLIST_BY_HANDLE_32:
696 return xfs_compat_attrlist_by_handle(mp, arg, inode);
sandeen@sandeen.net28750972008-11-25 21:20:15 -0600697 case XFS_IOC_ATTRMULTI_BY_HANDLE_32:
698 return xfs_compat_attrmulti_by_handle(mp, arg, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 default:
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600700 return -XFS_ERROR(ENOIOCTLCMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600703 error = xfs_ioctl(ip, filp, ioflags, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return error;
705}
706
Nathan Scottd3870392005-05-06 06:44:46 -0700707long
Nathan Scotta50cd262006-03-14 14:06:18 +1100708xfs_file_compat_ioctl(
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600709 struct file *filp,
710 unsigned int cmd,
711 unsigned long p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600713 struct inode *inode = filp->f_path.dentry->d_inode;
714
715 return xfs_compat_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Nathan Scottd3870392005-05-06 06:44:46 -0700718long
Nathan Scotta50cd262006-03-14 14:06:18 +1100719xfs_file_compat_invis_ioctl(
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600720 struct file *filp,
721 unsigned int cmd,
722 unsigned long p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
sandeen@sandeen.netd5547f92008-11-25 21:20:08 -0600724 struct inode *inode = filp->f_path.dentry->d_inode;
725
726 return xfs_compat_ioctl(XFS_I(inode), filp, IO_INVIS, cmd,
727 (void __user *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}