xfs_ioctl32.c 17.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
17 18 19
 */
#include <linux/compat.h>
#include <linux/ioctl.h>
20
#include <linux/mount.h>
21
#include <linux/slab.h>
L
Linus Torvalds 已提交
22 23 24
#include <asm/uaccess.h>
#include "xfs.h"
#include "xfs_fs.h"
25
#include "xfs_format.h"
26 27
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
28 29 30
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_mount.h"
L
Linus Torvalds 已提交
31
#include "xfs_vnode.h"
32 33 34
#include "xfs_inode.h"
#include "xfs_itable.h"
#include "xfs_error.h"
35
#include "xfs_fsops.h"
36 37
#include "xfs_alloc.h"
#include "xfs_rtalloc.h"
38
#include "xfs_attr.h"
39
#include "xfs_ioctl.h"
D
David Chinner 已提交
40
#include "xfs_ioctl32.h"
C
Christoph Hellwig 已提交
41
#include "xfs_trace.h"
L
Linus Torvalds 已提交
42

43 44 45
#define  _NATIVE_IOC(cmd, type) \
	  _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))

46
#ifdef BROKEN_X86_ALIGNMENT
47 48 49 50
STATIC int
xfs_compat_flock64_copyin(
	xfs_flock64_t		*bf,
	compat_xfs_flock64_t	__user *arg32)
51
{
52 53 54 55 56 57 58
	if (get_user(bf->l_type,	&arg32->l_type) ||
	    get_user(bf->l_whence,	&arg32->l_whence) ||
	    get_user(bf->l_start,	&arg32->l_start) ||
	    get_user(bf->l_len,		&arg32->l_len) ||
	    get_user(bf->l_sysid,	&arg32->l_sysid) ||
	    get_user(bf->l_pid,		&arg32->l_pid) ||
	    copy_from_user(bf->l_pad,	&arg32->l_pad,	4*sizeof(u32)))
E
Eric Sandeen 已提交
59
		return -EFAULT;
60
	return 0;
61 62
}

63 64 65 66
STATIC int
xfs_compat_ioc_fsgeometry_v1(
	struct xfs_mount	  *mp,
	compat_xfs_fsop_geom_v1_t __user *arg32)
67
{
68 69
	xfs_fsop_geom_t		  fsgeo;
	int			  error;
70

71 72
	error = xfs_fs_geometry(mp, &fsgeo, 3);
	if (error)
D
Dave Chinner 已提交
73
		return error;
74 75
	/* The 32-bit variant simply has some padding at the end */
	if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
E
Eric Sandeen 已提交
76
		return -EFAULT;
77
	return 0;
78 79
}

80 81 82 83 84 85 86
STATIC int
xfs_compat_growfs_data_copyin(
	struct xfs_growfs_data	 *in,
	compat_xfs_growfs_data_t __user *arg32)
{
	if (get_user(in->newblocks, &arg32->newblocks) ||
	    get_user(in->imaxpct,   &arg32->imaxpct))
E
Eric Sandeen 已提交
87
		return -EFAULT;
88 89 90 91 92 93 94 95 96 97
	return 0;
}

STATIC int
xfs_compat_growfs_rt_copyin(
	struct xfs_growfs_rt	 *in,
	compat_xfs_growfs_rt_t	__user *arg32)
{
	if (get_user(in->newblocks, &arg32->newblocks) ||
	    get_user(in->extsize,   &arg32->extsize))
E
Eric Sandeen 已提交
98
		return -EFAULT;
99 100 101
	return 0;
}

102 103 104 105 106 107
STATIC int
xfs_inumbers_fmt_compat(
	void			__user *ubuffer,
	const xfs_inogrp_t	*buffer,
	long			count,
	long			*written)
108
{
109 110
	compat_xfs_inogrp_t	__user *p32 = ubuffer;
	long			i;
111 112 113 114 115

	for (i = 0; i < count; i++) {
		if (put_user(buffer[i].xi_startino,   &p32[i].xi_startino) ||
		    put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) ||
		    put_user(buffer[i].xi_allocmask,  &p32[i].xi_allocmask))
E
Eric Sandeen 已提交
116
			return -EFAULT;
117 118 119 120 121
	}
	*written = count * sizeof(*p32);
	return 0;
}

L
Linus Torvalds 已提交
122
#else
123
#define xfs_inumbers_fmt_compat xfs_inumbers_fmt
124
#endif	/* BROKEN_X86_ALIGNMENT */
125

126 127 128 129 130 131 132 133 134
STATIC int
xfs_ioctl32_bstime_copyin(
	xfs_bstime_t		*bstime,
	compat_xfs_bstime_t	__user *bstime32)
{
	compat_time_t		sec32;	/* tv_sec differs on 64 vs. 32 */

	if (get_user(sec32,		&bstime32->tv_sec)	||
	    get_user(bstime->tv_nsec,	&bstime32->tv_nsec))
E
Eric Sandeen 已提交
135
		return -EFAULT;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	bstime->tv_sec = sec32;
	return 0;
}

/* xfs_bstat_t has differing alignment on intel, & bstime_t sizes everywhere */
STATIC int
xfs_ioctl32_bstat_copyin(
	xfs_bstat_t		*bstat,
	compat_xfs_bstat_t	__user *bstat32)
{
	if (get_user(bstat->bs_ino,	&bstat32->bs_ino)	||
	    get_user(bstat->bs_mode,	&bstat32->bs_mode)	||
	    get_user(bstat->bs_nlink,	&bstat32->bs_nlink)	||
	    get_user(bstat->bs_uid,	&bstat32->bs_uid)	||
	    get_user(bstat->bs_gid,	&bstat32->bs_gid)	||
	    get_user(bstat->bs_rdev,	&bstat32->bs_rdev)	||
	    get_user(bstat->bs_blksize,	&bstat32->bs_blksize)	||
	    get_user(bstat->bs_size,	&bstat32->bs_size)	||
	    xfs_ioctl32_bstime_copyin(&bstat->bs_atime, &bstat32->bs_atime) ||
	    xfs_ioctl32_bstime_copyin(&bstat->bs_mtime, &bstat32->bs_mtime) ||
	    xfs_ioctl32_bstime_copyin(&bstat->bs_ctime, &bstat32->bs_ctime) ||
	    get_user(bstat->bs_blocks,	&bstat32->bs_size)	||
	    get_user(bstat->bs_xflags,	&bstat32->bs_size)	||
	    get_user(bstat->bs_extsize,	&bstat32->bs_extsize)	||
	    get_user(bstat->bs_extents,	&bstat32->bs_extents)	||
	    get_user(bstat->bs_gen,	&bstat32->bs_gen)	||
162 163
	    get_user(bstat->bs_projid_lo, &bstat32->bs_projid_lo) ||
	    get_user(bstat->bs_projid_hi, &bstat32->bs_projid_hi) ||
164 165 166
	    get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask)	||
	    get_user(bstat->bs_dmstate,	&bstat32->bs_dmstate)	||
	    get_user(bstat->bs_aextents, &bstat32->bs_aextents))
E
Eric Sandeen 已提交
167
		return -EFAULT;
168 169 170
	return 0;
}

171 172
/* XFS_IOC_FSBULKSTAT and friends */

173 174 175 176
STATIC int
xfs_bstime_store_compat(
	compat_xfs_bstime_t	__user *p32,
	const xfs_bstime_t	*p)
177
{
178
	__s32			sec32;
179 180 181 182

	sec32 = p->tv_sec;
	if (put_user(sec32, &p32->tv_sec) ||
	    put_user(p->tv_nsec, &p32->tv_nsec))
E
Eric Sandeen 已提交
183
		return -EFAULT;
184 185 186
	return 0;
}

187
/* Return 0 on success or positive error (to xfs_bulkstat()) */
188 189
STATIC int
xfs_bulkstat_one_fmt_compat(
190
	void			__user *ubuffer,
191 192
	int			ubsize,
	int			*ubused,
193 194
	const xfs_bstat_t	*buffer)
{
195
	compat_xfs_bstat_t	__user *p32 = ubuffer;
196

197
	if (ubsize < sizeof(*p32))
D
Dave Chinner 已提交
198
		return -ENOMEM;
199 200 201 202 203 204 205 206 207

	if (put_user(buffer->bs_ino,	  &p32->bs_ino)		||
	    put_user(buffer->bs_mode,	  &p32->bs_mode)	||
	    put_user(buffer->bs_nlink,	  &p32->bs_nlink)	||
	    put_user(buffer->bs_uid,	  &p32->bs_uid)		||
	    put_user(buffer->bs_gid,	  &p32->bs_gid)		||
	    put_user(buffer->bs_rdev,	  &p32->bs_rdev)	||
	    put_user(buffer->bs_blksize,  &p32->bs_blksize)	||
	    put_user(buffer->bs_size,	  &p32->bs_size)	||
208 209 210
	    xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
	    xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
	    xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
211 212 213 214 215 216
	    put_user(buffer->bs_blocks,	  &p32->bs_blocks)	||
	    put_user(buffer->bs_xflags,	  &p32->bs_xflags)	||
	    put_user(buffer->bs_extsize,  &p32->bs_extsize)	||
	    put_user(buffer->bs_extents,  &p32->bs_extents)	||
	    put_user(buffer->bs_gen,	  &p32->bs_gen)		||
	    put_user(buffer->bs_projid,	  &p32->bs_projid)	||
217
	    put_user(buffer->bs_projid_hi,	&p32->bs_projid_hi)	||
218 219
	    put_user(buffer->bs_dmevmask, &p32->bs_dmevmask)	||
	    put_user(buffer->bs_dmstate,  &p32->bs_dmstate)	||
220
	    put_user(buffer->bs_aextents, &p32->bs_aextents))
D
Dave Chinner 已提交
221
		return -EFAULT;
222 223 224
	if (ubused)
		*ubused = sizeof(*p32);
	return 0;
225 226
}

227 228 229 230 231 232 233 234 235 236
STATIC int
xfs_bulkstat_one_compat(
	xfs_mount_t	*mp,		/* mount point for filesystem */
	xfs_ino_t	ino,		/* inode number to get data for */
	void		__user *buffer,	/* buffer to place output in */
	int		ubsize,		/* size of buffer */
	int		*ubused,	/* bytes used by me */
	int		*stat)		/* BULKSTAT_RV_... */
{
	return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
237
				    xfs_bulkstat_one_fmt_compat,
238
				    ubused, stat);
239 240
}

241 242
/* copied from xfs_ioctl.c */
STATIC int
243 244 245 246
xfs_compat_ioc_bulkstat(
	xfs_mount_t		  *mp,
	unsigned int		  cmd,
	compat_xfs_fsop_bulkreq_t __user *p32)
L
Linus Torvalds 已提交
247 248
{
	u32			addr;
249 250 251 252 253 254 255 256
	xfs_fsop_bulkreq_t	bulkreq;
	int			count;	/* # of records returned */
	xfs_ino_t		inlast;	/* last inode number */
	int			done;
	int			error;

	/* done = 1 if there are more stats to get and if bulkstat */
	/* should be called again (unused here, but used in dmapi) */
L
Linus Torvalds 已提交
257

258
	if (!capable(CAP_SYS_ADMIN))
E
Eric Sandeen 已提交
259
		return -EPERM;
260 261

	if (XFS_FORCED_SHUTDOWN(mp))
E
Eric Sandeen 已提交
262
		return -EIO;
263 264

	if (get_user(addr, &p32->lastip))
E
Eric Sandeen 已提交
265
		return -EFAULT;
266 267 268
	bulkreq.lastip = compat_ptr(addr);
	if (get_user(bulkreq.icount, &p32->icount) ||
	    get_user(addr, &p32->ubuffer))
E
Eric Sandeen 已提交
269
		return -EFAULT;
270 271
	bulkreq.ubuffer = compat_ptr(addr);
	if (get_user(addr, &p32->ocount))
E
Eric Sandeen 已提交
272
		return -EFAULT;
273
	bulkreq.ocount = compat_ptr(addr);
L
Linus Torvalds 已提交
274

275
	if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
E
Eric Sandeen 已提交
276
		return -EFAULT;
277 278

	if ((count = bulkreq.icount) <= 0)
E
Eric Sandeen 已提交
279
		return -EINVAL;
280

281
	if (bulkreq.ubuffer == NULL)
E
Eric Sandeen 已提交
282
		return -EINVAL;
283

284
	if (cmd == XFS_IOC_FSINUMBERS_32) {
285 286
		error = xfs_inumbers(mp, &inlast, &count,
				bulkreq.ubuffer, xfs_inumbers_fmt_compat);
287 288 289 290
	} else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE_32) {
		int res;

		error = xfs_bulkstat_one_compat(mp, inlast, bulkreq.ubuffer,
291
				sizeof(compat_xfs_bstat_t), NULL, &res);
292
	} else if (cmd == XFS_IOC_FSBULKSTAT_32) {
293
		error = xfs_bulkstat(mp, &inlast, &count,
294 295
			xfs_bulkstat_one_compat, sizeof(compat_xfs_bstat_t),
			bulkreq.ubuffer, &done);
296
	} else
D
Dave Chinner 已提交
297
		error = -EINVAL;
298
	if (error)
D
Dave Chinner 已提交
299
		return error;
300 301 302 303

	if (bulkreq.ocount != NULL) {
		if (copy_to_user(bulkreq.lastip, &inlast,
						sizeof(xfs_ino_t)))
E
Eric Sandeen 已提交
304
			return -EFAULT;
305 306

		if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
E
Eric Sandeen 已提交
307
			return -EFAULT;
308 309 310
	}

	return 0;
L
Linus Torvalds 已提交
311
}
312

313 314 315 316
STATIC int
xfs_compat_handlereq_copyin(
	xfs_fsop_handlereq_t		*hreq,
	compat_xfs_fsop_handlereq_t	__user *arg32)
317
{
318 319 320
	compat_xfs_fsop_handlereq_t	hreq32;

	if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
E
Eric Sandeen 已提交
321
		return -EFAULT;
322 323 324 325 326 327 328 329 330 331

	hreq->fd = hreq32.fd;
	hreq->path = compat_ptr(hreq32.path);
	hreq->oflags = hreq32.oflags;
	hreq->ihandle = compat_ptr(hreq32.ihandle);
	hreq->ihandlen = hreq32.ihandlen;
	hreq->ohandle = compat_ptr(hreq32.ohandle);
	hreq->ohandlen = compat_ptr(hreq32.ohandlen);

	return 0;
332 333
}

334 335 336 337
STATIC struct dentry *
xfs_compat_handlereq_to_dentry(
	struct file		*parfilp,
	compat_xfs_fsop_handlereq_t *hreq)
338
{
339 340
	return xfs_handle_to_dentry(parfilp,
			compat_ptr(hreq->ihandle), hreq->ihandlen);
341 342 343 344
}

STATIC int
xfs_compat_attrlist_by_handle(
345 346
	struct file		*parfilp,
	void			__user *arg)
347 348 349 350
{
	int			error;
	attrlist_cursor_kern_t	*cursor;
	compat_xfs_fsop_attrlist_handlereq_t al_hreq;
351
	struct dentry		*dentry;
352 353 354
	char			*kbuf;

	if (!capable(CAP_SYS_ADMIN))
E
Eric Sandeen 已提交
355
		return -EPERM;
356 357
	if (copy_from_user(&al_hreq, arg,
			   sizeof(compat_xfs_fsop_attrlist_handlereq_t)))
E
Eric Sandeen 已提交
358
		return -EFAULT;
359 360
	if (al_hreq.buflen < sizeof(struct attrlist) ||
	    al_hreq.buflen > XATTR_LIST_MAX)
E
Eric Sandeen 已提交
361
		return -EINVAL;
362 363 364 365 366

	/*
	 * Reject flags, only allow namespaces.
	 */
	if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
E
Eric Sandeen 已提交
367
		return -EINVAL;
368

369 370 371
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &al_hreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
372

373
	error = -ENOMEM;
374 375 376
	kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
	if (!kbuf)
		goto out_dput;
377 378

	cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
D
Dave Chinner 已提交
379
	error = xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
380 381 382 383 384 385 386
					al_hreq.flags, cursor);
	if (error)
		goto out_kfree;

	if (copy_to_user(compat_ptr(al_hreq.buffer), kbuf, al_hreq.buflen))
		error = -EFAULT;

387 388 389
out_kfree:
	kmem_free(kbuf);
out_dput:
390 391
	dput(dentry);
	return error;
392 393
}

394 395
STATIC int
xfs_compat_attrmulti_by_handle(
396 397
	struct file				*parfilp,
	void					__user *arg)
398 399 400 401
{
	int					error;
	compat_xfs_attr_multiop_t		*ops;
	compat_xfs_fsop_attrmulti_handlereq_t	am_hreq;
402
	struct dentry				*dentry;
403
	unsigned int				i, size;
404
	unsigned char				*attr_name;
405 406

	if (!capable(CAP_SYS_ADMIN))
E
Eric Sandeen 已提交
407
		return -EPERM;
408 409
	if (copy_from_user(&am_hreq, arg,
			   sizeof(compat_xfs_fsop_attrmulti_handlereq_t)))
E
Eric Sandeen 已提交
410
		return -EFAULT;
411

412 413 414 415
	/* overflow check */
	if (am_hreq.opcount >= INT_MAX / sizeof(compat_xfs_attr_multiop_t))
		return -E2BIG;

416 417 418
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &am_hreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
419

D
Dave Chinner 已提交
420
	error = -E2BIG;
421 422
	size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t);
	if (!size || size > 16 * PAGE_SIZE)
423
		goto out_dput;
424

L
Li Zefan 已提交
425 426
	ops = memdup_user(compat_ptr(am_hreq.ops), size);
	if (IS_ERR(ops)) {
427
		error = -PTR_ERR(ops);
428
		goto out_dput;
L
Li Zefan 已提交
429
	}
430

D
Dave Chinner 已提交
431
	error = -ENOMEM;
432 433 434 435 436 437
	attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
	if (!attr_name)
		goto out_kfree_ops;

	error = 0;
	for (i = 0; i < am_hreq.opcount; i++) {
438
		ops[i].am_error = strncpy_from_user((char *)attr_name,
439 440 441
				compat_ptr(ops[i].am_attrname),
				MAXNAMELEN);
		if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
D
Dave Chinner 已提交
442
			error = -ERANGE;
443 444 445 446 447
		if (ops[i].am_error < 0)
			break;

		switch (ops[i].am_opcode) {
		case ATTR_OP_GET:
448 449
			ops[i].am_error = xfs_attrmulti_attr_get(
					dentry->d_inode, attr_name,
450 451 452 453
					compat_ptr(ops[i].am_attrvalue),
					&ops[i].am_length, ops[i].am_flags);
			break;
		case ATTR_OP_SET:
454
			ops[i].am_error = mnt_want_write_file(parfilp);
455 456
			if (ops[i].am_error)
				break;
457 458
			ops[i].am_error = xfs_attrmulti_attr_set(
					dentry->d_inode, attr_name,
459 460
					compat_ptr(ops[i].am_attrvalue),
					ops[i].am_length, ops[i].am_flags);
A
Al Viro 已提交
461
			mnt_drop_write_file(parfilp);
462 463
			break;
		case ATTR_OP_REMOVE:
464
			ops[i].am_error = mnt_want_write_file(parfilp);
465 466
			if (ops[i].am_error)
				break;
467 468 469
			ops[i].am_error = xfs_attrmulti_attr_remove(
					dentry->d_inode, attr_name,
					ops[i].am_flags);
A
Al Viro 已提交
470
			mnt_drop_write_file(parfilp);
471 472
			break;
		default:
D
Dave Chinner 已提交
473
			ops[i].am_error = -EINVAL;
474 475 476 477
		}
	}

	if (copy_to_user(compat_ptr(am_hreq.ops), ops, size))
D
Dave Chinner 已提交
478
		error = -EFAULT;
479 480 481 482

	kfree(attr_name);
 out_kfree_ops:
	kfree(ops);
483 484
 out_dput:
	dput(dentry);
D
Dave Chinner 已提交
485
	return error;
486 487
}

488 489
STATIC int
xfs_compat_fssetdm_by_handle(
490 491
	struct file		*parfilp,
	void			__user *arg)
492 493 494 495
{
	int			error;
	struct fsdmidata	fsd;
	compat_xfs_fsop_setdm_handlereq_t dmhreq;
496
	struct dentry		*dentry;
497 498

	if (!capable(CAP_MKNOD))
E
Eric Sandeen 已提交
499
		return -EPERM;
500 501
	if (copy_from_user(&dmhreq, arg,
			   sizeof(compat_xfs_fsop_setdm_handlereq_t)))
E
Eric Sandeen 已提交
502
		return -EFAULT;
503

504 505 506
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &dmhreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
507

508
	if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
E
Eric Sandeen 已提交
509
		error = -EPERM;
510 511 512 513
		goto out;
	}

	if (copy_from_user(&fsd, compat_ptr(dmhreq.data), sizeof(fsd))) {
E
Eric Sandeen 已提交
514
		error = -EFAULT;
515 516 517
		goto out;
	}

D
Dave Chinner 已提交
518
	error = xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
519 520 521
				 fsd.fsd_dmstate);

out:
522
	dput(dentry);
523 524 525
	return error;
}

526 527 528 529 530
long
xfs_file_compat_ioctl(
	struct file		*filp,
	unsigned		cmd,
	unsigned long		p)
L
Linus Torvalds 已提交
531
{
A
Al Viro 已提交
532
	struct inode		*inode = file_inode(filp);
533 534 535 536 537 538 539 540 541
	struct xfs_inode	*ip = XFS_I(inode);
	struct xfs_mount	*mp = ip->i_mount;
	void			__user *arg = (void __user *)p;
	int			ioflags = 0;
	int			error;

	if (filp->f_mode & FMODE_NOCMTIME)
		ioflags |= IO_INVIS;

C
Christoph Hellwig 已提交
542
	trace_xfs_file_compat_ioctl(ip);
L
Linus Torvalds 已提交
543 544

	switch (cmd) {
545
	/* No size or alignment issues on any arch */
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	case XFS_IOC_DIOINFO:
	case XFS_IOC_FSGEOMETRY:
	case XFS_IOC_FSGETXATTR:
	case XFS_IOC_FSSETXATTR:
	case XFS_IOC_FSGETXATTRA:
	case XFS_IOC_FSSETDM:
	case XFS_IOC_GETBMAP:
	case XFS_IOC_GETBMAPA:
	case XFS_IOC_GETBMAPX:
	case XFS_IOC_FSCOUNTS:
	case XFS_IOC_SET_RESBLKS:
	case XFS_IOC_GET_RESBLKS:
	case XFS_IOC_FSGROWFSLOG:
	case XFS_IOC_GOINGDOWN:
	case XFS_IOC_ERROR_INJECTION:
	case XFS_IOC_ERROR_CLEARALL:
562
		return xfs_file_ioctl(filp, cmd, p);
563 564 565 566 567 568 569 570 571 572 573 574 575
#ifndef BROKEN_X86_ALIGNMENT
	/* These are handled fine if no alignment issues */
	case XFS_IOC_ALLOCSP:
	case XFS_IOC_FREESP:
	case XFS_IOC_RESVSP:
	case XFS_IOC_UNRESVSP:
	case XFS_IOC_ALLOCSP64:
	case XFS_IOC_FREESP64:
	case XFS_IOC_RESVSP64:
	case XFS_IOC_UNRESVSP64:
	case XFS_IOC_FSGEOMETRY_V1:
	case XFS_IOC_FSGROWFSDATA:
	case XFS_IOC_FSGROWFSRT:
D
Dave Chinner 已提交
576
	case XFS_IOC_ZERO_RANGE:
577
		return xfs_file_ioctl(filp, cmd, p);
578
#else
579 580 581 582 583 584 585
	case XFS_IOC_ALLOCSP_32:
	case XFS_IOC_FREESP_32:
	case XFS_IOC_ALLOCSP64_32:
	case XFS_IOC_FREESP64_32:
	case XFS_IOC_RESVSP_32:
	case XFS_IOC_UNRESVSP_32:
	case XFS_IOC_RESVSP64_32:
586 587
	case XFS_IOC_UNRESVSP64_32:
	case XFS_IOC_ZERO_RANGE_32: {
588 589 590
		struct xfs_flock64	bf;

		if (xfs_compat_flock64_copyin(&bf, arg))
E
Eric Sandeen 已提交
591
			return -EFAULT;
592
		cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
593 594
		return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
	}
595
	case XFS_IOC_FSGEOMETRY_V1_32:
596
		return xfs_compat_ioc_fsgeometry_v1(mp, arg);
597 598 599 600
	case XFS_IOC_FSGROWFSDATA_32: {
		struct xfs_growfs_data	in;

		if (xfs_compat_growfs_data_copyin(&in, arg))
E
Eric Sandeen 已提交
601
			return -EFAULT;
J
Jan Kara 已提交
602 603 604
		error = mnt_want_write_file(filp);
		if (error)
			return error;
605
		error = xfs_growfs_data(mp, &in);
J
Jan Kara 已提交
606
		mnt_drop_write_file(filp);
D
Dave Chinner 已提交
607
		return error;
608 609 610 611 612
	}
	case XFS_IOC_FSGROWFSRT_32: {
		struct xfs_growfs_rt	in;

		if (xfs_compat_growfs_rt_copyin(&in, arg))
E
Eric Sandeen 已提交
613
			return -EFAULT;
J
Jan Kara 已提交
614 615 616
		error = mnt_want_write_file(filp);
		if (error)
			return error;
617
		error = xfs_growfs_rt(mp, &in);
J
Jan Kara 已提交
618
		mnt_drop_write_file(filp);
D
Dave Chinner 已提交
619
		return error;
620
	}
L
Linus Torvalds 已提交
621
#endif
622 623 624 625 626
	/* long changes size, but xfs only copiese out 32 bits */
	case XFS_IOC_GETXFLAGS_32:
	case XFS_IOC_SETXFLAGS_32:
	case XFS_IOC_GETVERSION_32:
		cmd = _NATIVE_IOC(cmd, long);
627
		return xfs_file_ioctl(filp, cmd, p);
628
	case XFS_IOC_SWAPEXT_32: {
629 630 631 632 633 634 635
		struct xfs_swapext	  sxp;
		struct compat_xfs_swapext __user *sxu = arg;

		/* Bulk copy in up to the sx_stat field, then copy bstat */
		if (copy_from_user(&sxp, sxu,
				   offsetof(struct xfs_swapext, sx_stat)) ||
		    xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat))
E
Eric Sandeen 已提交
636
			return -EFAULT;
J
Jan Kara 已提交
637 638 639
		error = mnt_want_write_file(filp);
		if (error)
			return error;
D
Dave Chinner 已提交
640
		error = xfs_ioc_swapext(&sxp);
J
Jan Kara 已提交
641
		mnt_drop_write_file(filp);
D
Dave Chinner 已提交
642
		return error;
643
	}
644 645 646
	case XFS_IOC_FSBULKSTAT_32:
	case XFS_IOC_FSBULKSTAT_SINGLE_32:
	case XFS_IOC_FSINUMBERS_32:
647
		return xfs_compat_ioc_bulkstat(mp, cmd, arg);
648 649
	case XFS_IOC_FD_TO_HANDLE_32:
	case XFS_IOC_PATH_TO_HANDLE_32:
650 651 652 653
	case XFS_IOC_PATH_TO_FSHANDLE_32: {
		struct xfs_fsop_handlereq	hreq;

		if (xfs_compat_handlereq_copyin(&hreq, arg))
E
Eric Sandeen 已提交
654
			return -EFAULT;
655
		cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
656 657 658 659 660 661
		return xfs_find_handle(cmd, &hreq);
	}
	case XFS_IOC_OPEN_BY_HANDLE_32: {
		struct xfs_fsop_handlereq	hreq;

		if (xfs_compat_handlereq_copyin(&hreq, arg))
E
Eric Sandeen 已提交
662
			return -EFAULT;
663
		return xfs_open_by_handle(filp, &hreq);
664 665 666 667 668
	}
	case XFS_IOC_READLINK_BY_HANDLE_32: {
		struct xfs_fsop_handlereq	hreq;

		if (xfs_compat_handlereq_copyin(&hreq, arg))
E
Eric Sandeen 已提交
669
			return -EFAULT;
670
		return xfs_readlink_by_handle(filp, &hreq);
671
	}
672
	case XFS_IOC_ATTRLIST_BY_HANDLE_32:
673
		return xfs_compat_attrlist_by_handle(filp, arg);
674
	case XFS_IOC_ATTRMULTI_BY_HANDLE_32:
675
		return xfs_compat_attrmulti_by_handle(filp, arg);
676
	case XFS_IOC_FSSETDM_BY_HANDLE_32:
677
		return xfs_compat_fssetdm_by_handle(filp, arg);
L
Linus Torvalds 已提交
678
	default:
E
Eric Sandeen 已提交
679
		return -ENOIOCTLCMD;
L
Linus Torvalds 已提交
680 681
	}
}