xfs_ioctl32.c 17.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2
/*
3 4
 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7
 */
#include <linux/compat.h>
#include <linux/ioctl.h>
8
#include <linux/mount.h>
9
#include <linux/slab.h>
10
#include <linux/uaccess.h>
11
#include <linux/fsmap.h>
Linus Torvalds's avatar
Linus Torvalds committed
12 13
#include "xfs.h"
#include "xfs_fs.h"
14
#include "xfs_format.h"
15 16
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
17 18 19 20
#include "xfs_mount.h"
#include "xfs_inode.h"
#include "xfs_itable.h"
#include "xfs_error.h"
21
#include "xfs_fsops.h"
22 23
#include "xfs_alloc.h"
#include "xfs_rtalloc.h"
24
#include "xfs_attr.h"
25
#include "xfs_ioctl.h"
26
#include "xfs_ioctl32.h"
27
#include "xfs_trace.h"
28
#include "xfs_sb.h"
Linus Torvalds's avatar
Linus Torvalds committed
29

30 31 32
#define  _NATIVE_IOC(cmd, type) \
	  _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))

33
#ifdef BROKEN_X86_ALIGNMENT
34 35 36 37
STATIC int
xfs_compat_flock64_copyin(
	xfs_flock64_t		*bf,
	compat_xfs_flock64_t	__user *arg32)
38
{
39 40 41 42 43 44 45
	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)))
Eric Sandeen's avatar
Eric Sandeen committed
46
		return -EFAULT;
47
	return 0;
48 49
}

50 51 52 53
STATIC int
xfs_compat_ioc_fsgeometry_v1(
	struct xfs_mount	  *mp,
	compat_xfs_fsop_geom_v1_t __user *arg32)
54
{
55 56
	xfs_fsop_geom_t		  fsgeo;
	int			  error;
57

58
	error = xfs_fs_geometry(&mp->m_sb, &fsgeo, 3);
59
	if (error)
60
		return error;
61 62
	/* The 32-bit variant simply has some padding at the end */
	if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
Eric Sandeen's avatar
Eric Sandeen committed
63
		return -EFAULT;
64
	return 0;
65 66
}

67 68 69 70 71 72 73
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))
Eric Sandeen's avatar
Eric Sandeen committed
74
		return -EFAULT;
75 76 77 78 79 80 81 82 83 84
	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))
Eric Sandeen's avatar
Eric Sandeen committed
85
		return -EFAULT;
86 87 88
	return 0;
}

89 90 91
STATIC int
xfs_inumbers_fmt_compat(
	void			__user *ubuffer,
Jie Liu's avatar
Jie Liu committed
92
	const struct xfs_inogrp	*buffer,
93 94
	long			count,
	long			*written)
95
{
96 97
	compat_xfs_inogrp_t	__user *p32 = ubuffer;
	long			i;
98 99 100 101 102

	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))
Eric Sandeen's avatar
Eric Sandeen committed
103
			return -EFAULT;
104 105 106 107 108
	}
	*written = count * sizeof(*p32);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
109
#else
110
#define xfs_inumbers_fmt_compat xfs_inumbers_fmt
111
#endif	/* BROKEN_X86_ALIGNMENT */
112

113 114 115 116 117 118 119 120 121
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))
Eric Sandeen's avatar
Eric Sandeen committed
122
		return -EFAULT;
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	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)	||
149 150
	    get_user(bstat->bs_projid_lo, &bstat32->bs_projid_lo) ||
	    get_user(bstat->bs_projid_hi, &bstat32->bs_projid_hi) ||
151
	    get_user(bstat->bs_forkoff,	&bstat32->bs_forkoff)	||
152 153 154
	    get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask)	||
	    get_user(bstat->bs_dmstate,	&bstat32->bs_dmstate)	||
	    get_user(bstat->bs_aextents, &bstat32->bs_aextents))
Eric Sandeen's avatar
Eric Sandeen committed
155
		return -EFAULT;
156 157 158
	return 0;
}

159 160
/* XFS_IOC_FSBULKSTAT and friends */

161 162 163 164
STATIC int
xfs_bstime_store_compat(
	compat_xfs_bstime_t	__user *p32,
	const xfs_bstime_t	*p)
165
{
166
	__s32			sec32;
167 168 169 170

	sec32 = p->tv_sec;
	if (put_user(sec32, &p32->tv_sec) ||
	    put_user(p->tv_nsec, &p32->tv_nsec))
Eric Sandeen's avatar
Eric Sandeen committed
171
		return -EFAULT;
172 173 174
	return 0;
}

175
/* Return 0 on success or positive error (to xfs_bulkstat()) */
176 177
STATIC int
xfs_bulkstat_one_fmt_compat(
178
	void			__user *ubuffer,
179 180
	int			ubsize,
	int			*ubused,
181 182
	const xfs_bstat_t	*buffer)
{
183
	compat_xfs_bstat_t	__user *p32 = ubuffer;
184

185
	if (ubsize < sizeof(*p32))
186
		return -ENOMEM;
187 188 189 190 191 192 193 194 195

	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)	||
196 197 198
	    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) ||
199 200 201 202 203 204
	    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)	||
205
	    put_user(buffer->bs_projid_hi,	&p32->bs_projid_hi)	||
206
	    put_user(buffer->bs_forkoff,  &p32->bs_forkoff)	||
207 208
	    put_user(buffer->bs_dmevmask, &p32->bs_dmevmask)	||
	    put_user(buffer->bs_dmstate,  &p32->bs_dmstate)	||
209
	    put_user(buffer->bs_aextents, &p32->bs_aextents))
210
		return -EFAULT;
211 212 213
	if (ubused)
		*ubused = sizeof(*p32);
	return 0;
214 215
}

216 217 218 219 220 221 222 223 224 225
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,
226
				    xfs_bulkstat_one_fmt_compat,
227
				    ubused, stat);
228 229
}

230 231
/* copied from xfs_ioctl.c */
STATIC int
232 233 234 235
xfs_compat_ioc_bulkstat(
	xfs_mount_t		  *mp,
	unsigned int		  cmd,
	compat_xfs_fsop_bulkreq_t __user *p32)
Linus Torvalds's avatar
Linus Torvalds committed
236 237
{
	u32			addr;
238 239 240 241 242 243 244 245
	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) */
Linus Torvalds's avatar
Linus Torvalds committed
246

247
	if (!capable(CAP_SYS_ADMIN))
Eric Sandeen's avatar
Eric Sandeen committed
248
		return -EPERM;
249 250

	if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeen's avatar
Eric Sandeen committed
251
		return -EIO;
252 253

	if (get_user(addr, &p32->lastip))
Eric Sandeen's avatar
Eric Sandeen committed
254
		return -EFAULT;
255 256 257
	bulkreq.lastip = compat_ptr(addr);
	if (get_user(bulkreq.icount, &p32->icount) ||
	    get_user(addr, &p32->ubuffer))
Eric Sandeen's avatar
Eric Sandeen committed
258
		return -EFAULT;
259 260
	bulkreq.ubuffer = compat_ptr(addr);
	if (get_user(addr, &p32->ocount))
Eric Sandeen's avatar
Eric Sandeen committed
261
		return -EFAULT;
262
	bulkreq.ocount = compat_ptr(addr);
Linus Torvalds's avatar
Linus Torvalds committed
263

264
	if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
Eric Sandeen's avatar
Eric Sandeen committed
265
		return -EFAULT;
266 267

	if ((count = bulkreq.icount) <= 0)
Eric Sandeen's avatar
Eric Sandeen committed
268
		return -EINVAL;
269

270
	if (bulkreq.ubuffer == NULL)
Eric Sandeen's avatar
Eric Sandeen committed
271
		return -EINVAL;
272

273
	if (cmd == XFS_IOC_FSINUMBERS_32) {
274 275
		error = xfs_inumbers(mp, &inlast, &count,
				bulkreq.ubuffer, xfs_inumbers_fmt_compat);
276 277 278 279
	} else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE_32) {
		int res;

		error = xfs_bulkstat_one_compat(mp, inlast, bulkreq.ubuffer,
280
				sizeof(compat_xfs_bstat_t), NULL, &res);
281
	} else if (cmd == XFS_IOC_FSBULKSTAT_32) {
282
		error = xfs_bulkstat(mp, &inlast, &count,
283 284
			xfs_bulkstat_one_compat, sizeof(compat_xfs_bstat_t),
			bulkreq.ubuffer, &done);
285
	} else
286
		error = -EINVAL;
287
	if (error)
288
		return error;
289 290 291 292

	if (bulkreq.ocount != NULL) {
		if (copy_to_user(bulkreq.lastip, &inlast,
						sizeof(xfs_ino_t)))
Eric Sandeen's avatar
Eric Sandeen committed
293
			return -EFAULT;
294 295

		if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
Eric Sandeen's avatar
Eric Sandeen committed
296
			return -EFAULT;
297 298 299
	}

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
300
}
301

302 303 304 305
STATIC int
xfs_compat_handlereq_copyin(
	xfs_fsop_handlereq_t		*hreq,
	compat_xfs_fsop_handlereq_t	__user *arg32)
306
{
307 308 309
	compat_xfs_fsop_handlereq_t	hreq32;

	if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
Eric Sandeen's avatar
Eric Sandeen committed
310
		return -EFAULT;
311 312 313 314 315 316 317 318 319 320

	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;
321 322
}

323 324 325 326
STATIC struct dentry *
xfs_compat_handlereq_to_dentry(
	struct file		*parfilp,
	compat_xfs_fsop_handlereq_t *hreq)
327
{
328 329
	return xfs_handle_to_dentry(parfilp,
			compat_ptr(hreq->ihandle), hreq->ihandlen);
330 331 332 333
}

STATIC int
xfs_compat_attrlist_by_handle(
334 335
	struct file		*parfilp,
	void			__user *arg)
336 337 338
{
	int			error;
	attrlist_cursor_kern_t	*cursor;
339
	compat_xfs_fsop_attrlist_handlereq_t __user *p = arg;
340
	compat_xfs_fsop_attrlist_handlereq_t al_hreq;
341
	struct dentry		*dentry;
342 343 344
	char			*kbuf;

	if (!capable(CAP_SYS_ADMIN))
Eric Sandeen's avatar
Eric Sandeen committed
345
		return -EPERM;
346 347
	if (copy_from_user(&al_hreq, arg,
			   sizeof(compat_xfs_fsop_attrlist_handlereq_t)))
Eric Sandeen's avatar
Eric Sandeen committed
348
		return -EFAULT;
349
	if (al_hreq.buflen < sizeof(struct attrlist) ||
350
	    al_hreq.buflen > XFS_XATTR_LIST_MAX)
Eric Sandeen's avatar
Eric Sandeen committed
351
		return -EINVAL;
352 353 354 355 356

	/*
	 * Reject flags, only allow namespaces.
	 */
	if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
Eric Sandeen's avatar
Eric Sandeen committed
357
		return -EINVAL;
358

359 360 361
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &al_hreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
362

363
	error = -ENOMEM;
364 365 366
	kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
	if (!kbuf)
		goto out_dput;
367 368

	cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
369
	error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
370 371 372 373
					al_hreq.flags, cursor);
	if (error)
		goto out_kfree;

374 375 376 377 378
	if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
		error = -EFAULT;
		goto out_kfree;
	}

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

382 383 384
out_kfree:
	kmem_free(kbuf);
out_dput:
385 386
	dput(dentry);
	return error;
387 388
}

389 390
STATIC int
xfs_compat_attrmulti_by_handle(
391 392
	struct file				*parfilp,
	void					__user *arg)
393 394 395 396
{
	int					error;
	compat_xfs_attr_multiop_t		*ops;
	compat_xfs_fsop_attrmulti_handlereq_t	am_hreq;
397
	struct dentry				*dentry;
398
	unsigned int				i, size;
399
	unsigned char				*attr_name;
400 401

	if (!capable(CAP_SYS_ADMIN))
Eric Sandeen's avatar
Eric Sandeen committed
402
		return -EPERM;
403 404
	if (copy_from_user(&am_hreq, arg,
			   sizeof(compat_xfs_fsop_attrmulti_handlereq_t)))
Eric Sandeen's avatar
Eric Sandeen committed
405
		return -EFAULT;
406

407 408 409 410
	/* overflow check */
	if (am_hreq.opcount >= INT_MAX / sizeof(compat_xfs_attr_multiop_t))
		return -E2BIG;

411 412 413
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &am_hreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
414

415
	error = -E2BIG;
416 417
	size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t);
	if (!size || size > 16 * PAGE_SIZE)
418
		goto out_dput;
419

Li Zefan's avatar
Li Zefan committed
420 421
	ops = memdup_user(compat_ptr(am_hreq.ops), size);
	if (IS_ERR(ops)) {
422
		error = PTR_ERR(ops);
423
		goto out_dput;
Li Zefan's avatar
Li Zefan committed
424
	}
425

426
	error = -ENOMEM;
427 428 429 430 431 432
	attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
	if (!attr_name)
		goto out_kfree_ops;

	error = 0;
	for (i = 0; i < am_hreq.opcount; i++) {
433
		ops[i].am_error = strncpy_from_user((char *)attr_name,
434 435 436
				compat_ptr(ops[i].am_attrname),
				MAXNAMELEN);
		if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
437
			error = -ERANGE;
438 439 440 441 442
		if (ops[i].am_error < 0)
			break;

		switch (ops[i].am_opcode) {
		case ATTR_OP_GET:
443
			ops[i].am_error = xfs_attrmulti_attr_get(
444
					d_inode(dentry), attr_name,
445 446 447 448
					compat_ptr(ops[i].am_attrvalue),
					&ops[i].am_length, ops[i].am_flags);
			break;
		case ATTR_OP_SET:
449
			ops[i].am_error = mnt_want_write_file(parfilp);
450 451
			if (ops[i].am_error)
				break;
452
			ops[i].am_error = xfs_attrmulti_attr_set(
453
					d_inode(dentry), attr_name,
454 455
					compat_ptr(ops[i].am_attrvalue),
					ops[i].am_length, ops[i].am_flags);
Al Viro's avatar
Al Viro committed
456
			mnt_drop_write_file(parfilp);
457 458
			break;
		case ATTR_OP_REMOVE:
459
			ops[i].am_error = mnt_want_write_file(parfilp);
460 461
			if (ops[i].am_error)
				break;
462
			ops[i].am_error = xfs_attrmulti_attr_remove(
463
					d_inode(dentry), attr_name,
464
					ops[i].am_flags);
Al Viro's avatar
Al Viro committed
465
			mnt_drop_write_file(parfilp);
466 467
			break;
		default:
468
			ops[i].am_error = -EINVAL;
469 470 471 472
		}
	}

	if (copy_to_user(compat_ptr(am_hreq.ops), ops, size))
473
		error = -EFAULT;
474 475 476 477

	kfree(attr_name);
 out_kfree_ops:
	kfree(ops);
478 479
 out_dput:
	dput(dentry);
480
	return error;
481 482
}

483 484
STATIC int
xfs_compat_fssetdm_by_handle(
485 486
	struct file		*parfilp,
	void			__user *arg)
487 488 489 490
{
	int			error;
	struct fsdmidata	fsd;
	compat_xfs_fsop_setdm_handlereq_t dmhreq;
491
	struct dentry		*dentry;
492 493

	if (!capable(CAP_MKNOD))
Eric Sandeen's avatar
Eric Sandeen committed
494
		return -EPERM;
495 496
	if (copy_from_user(&dmhreq, arg,
			   sizeof(compat_xfs_fsop_setdm_handlereq_t)))
Eric Sandeen's avatar
Eric Sandeen committed
497
		return -EFAULT;
498

499 500 501
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &dmhreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
502

503
	if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
Eric Sandeen's avatar
Eric Sandeen committed
504
		error = -EPERM;
505 506 507 508
		goto out;
	}

	if (copy_from_user(&fsd, compat_ptr(dmhreq.data), sizeof(fsd))) {
Eric Sandeen's avatar
Eric Sandeen committed
509
		error = -EFAULT;
510 511 512
		goto out;
	}

513
	error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
514 515 516
				 fsd.fsd_dmstate);

out:
517
	dput(dentry);
518 519 520
	return error;
}

521 522 523 524 525
long
xfs_file_compat_ioctl(
	struct file		*filp,
	unsigned		cmd,
	unsigned long		p)
Linus Torvalds's avatar
Linus Torvalds committed
526
{
Al Viro's avatar
Al Viro committed
527
	struct inode		*inode = file_inode(filp);
528 529 530 531 532
	struct xfs_inode	*ip = XFS_I(inode);
	struct xfs_mount	*mp = ip->i_mount;
	void			__user *arg = (void __user *)p;
	int			error;

533
	trace_xfs_file_compat_ioctl(ip);
Linus Torvalds's avatar
Linus Torvalds committed
534 535

	switch (cmd) {
536
	/* No size or alignment issues on any arch */
Linus Torvalds's avatar
Linus Torvalds committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	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:
553
	case FS_IOC_GETFSMAP:
554
	case XFS_IOC_SCRUB_METADATA:
555
		return xfs_file_ioctl(filp, cmd, p);
556 557 558 559 560 561 562 563 564 565 566 567 568
#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:
569
	case XFS_IOC_ZERO_RANGE:
570
		return xfs_file_ioctl(filp, cmd, p);
571
#else
572 573 574 575 576 577 578
	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:
579 580
	case XFS_IOC_UNRESVSP64_32:
	case XFS_IOC_ZERO_RANGE_32: {
581 582 583
		struct xfs_flock64	bf;

		if (xfs_compat_flock64_copyin(&bf, arg))
Eric Sandeen's avatar
Eric Sandeen committed
584
			return -EFAULT;
585
		cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
586
		return xfs_ioc_space(filp, cmd, &bf);
587
	}
588
	case XFS_IOC_FSGEOMETRY_V1_32:
589
		return xfs_compat_ioc_fsgeometry_v1(mp, arg);
590 591 592 593
	case XFS_IOC_FSGROWFSDATA_32: {
		struct xfs_growfs_data	in;

		if (xfs_compat_growfs_data_copyin(&in, arg))
Eric Sandeen's avatar
Eric Sandeen committed
594
			return -EFAULT;
Jan Kara's avatar
Jan Kara committed
595 596 597
		error = mnt_want_write_file(filp);
		if (error)
			return error;
598
		error = xfs_growfs_data(mp, &in);
Jan Kara's avatar
Jan Kara committed
599
		mnt_drop_write_file(filp);
600
		return error;
601 602 603 604 605
	}
	case XFS_IOC_FSGROWFSRT_32: {
		struct xfs_growfs_rt	in;

		if (xfs_compat_growfs_rt_copyin(&in, arg))
Eric Sandeen's avatar
Eric Sandeen committed
606
			return -EFAULT;
Jan Kara's avatar
Jan Kara committed
607 608 609
		error = mnt_want_write_file(filp);
		if (error)
			return error;
610
		error = xfs_growfs_rt(mp, &in);
Jan Kara's avatar
Jan Kara committed
611
		mnt_drop_write_file(filp);
612
		return error;
613
	}
Linus Torvalds's avatar
Linus Torvalds committed
614
#endif
615 616 617 618 619
	/* 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);
620
		return xfs_file_ioctl(filp, cmd, p);
621
	case XFS_IOC_SWAPEXT_32: {
622 623 624 625 626 627 628
		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))
Eric Sandeen's avatar
Eric Sandeen committed
629
			return -EFAULT;
Jan Kara's avatar
Jan Kara committed
630 631 632
		error = mnt_want_write_file(filp);
		if (error)
			return error;
633
		error = xfs_ioc_swapext(&sxp);
Jan Kara's avatar
Jan Kara committed
634
		mnt_drop_write_file(filp);
635
		return error;
636
	}
637 638 639
	case XFS_IOC_FSBULKSTAT_32:
	case XFS_IOC_FSBULKSTAT_SINGLE_32:
	case XFS_IOC_FSINUMBERS_32:
640
		return xfs_compat_ioc_bulkstat(mp, cmd, arg);
641 642
	case XFS_IOC_FD_TO_HANDLE_32:
	case XFS_IOC_PATH_TO_HANDLE_32:
643 644 645 646
	case XFS_IOC_PATH_TO_FSHANDLE_32: {
		struct xfs_fsop_handlereq	hreq;

		if (xfs_compat_handlereq_copyin(&hreq, arg))
Eric Sandeen's avatar
Eric Sandeen committed
647
			return -EFAULT;
648
		cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
649 650 651 652 653 654
		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))
Eric Sandeen's avatar
Eric Sandeen committed
655
			return -EFAULT;
656
		return xfs_open_by_handle(filp, &hreq);
657 658 659 660 661
	}
	case XFS_IOC_READLINK_BY_HANDLE_32: {
		struct xfs_fsop_handlereq	hreq;

		if (xfs_compat_handlereq_copyin(&hreq, arg))
Eric Sandeen's avatar
Eric Sandeen committed
662
			return -EFAULT;
663
		return xfs_readlink_by_handle(filp, &hreq);
664
	}
665
	case XFS_IOC_ATTRLIST_BY_HANDLE_32:
666
		return xfs_compat_attrlist_by_handle(filp, arg);
667
	case XFS_IOC_ATTRMULTI_BY_HANDLE_32:
668
		return xfs_compat_attrmulti_by_handle(filp, arg);
669
	case XFS_IOC_FSSETDM_BY_HANDLE_32:
670
		return xfs_compat_fssetdm_by_handle(filp, arg);
Linus Torvalds's avatar
Linus Torvalds committed
671
	default:
Eric Sandeen's avatar
Eric Sandeen committed
672
		return -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
673 674
	}
}