xfs_ioctl32.c 17.7 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
#include <linux/mount.h>
7
#include <linux/fsmap.h>
Linus Torvalds's avatar
Linus Torvalds committed
8 9
#include "xfs.h"
#include "xfs_fs.h"
10
#include "xfs_shared.h"
11
#include "xfs_format.h"
12 13
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
14 15
#include "xfs_mount.h"
#include "xfs_inode.h"
16
#include "xfs_iwalk.h"
17
#include "xfs_itable.h"
18
#include "xfs_fsops.h"
19
#include "xfs_rtalloc.h"
20
#include "xfs_attr.h"
21
#include "xfs_ioctl.h"
22
#include "xfs_ioctl32.h"
23
#include "xfs_trace.h"
24
#include "xfs_sb.h"
Linus Torvalds's avatar
Linus Torvalds committed
25

26 27 28
#define  _NATIVE_IOC(cmd, type) \
	  _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))

29
#ifdef BROKEN_X86_ALIGNMENT
30 31 32 33
STATIC int
xfs_compat_flock64_copyin(
	xfs_flock64_t		*bf,
	compat_xfs_flock64_t	__user *arg32)
34
{
35 36 37 38 39 40 41
	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
42
		return -EFAULT;
43
	return 0;
44 45
}

46 47 48 49
STATIC int
xfs_compat_ioc_fsgeometry_v1(
	struct xfs_mount	  *mp,
	compat_xfs_fsop_geom_v1_t __user *arg32)
50
{
51
	struct xfs_fsop_geom	  fsgeo;
52

53
	xfs_fs_geometry(&mp->m_sb, &fsgeo, 3);
54 55
	/* 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
56
		return -EFAULT;
57
	return 0;
58 59
}

60 61 62 63 64 65 66
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
67
		return -EFAULT;
68 69 70 71 72 73 74 75 76 77
	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
78
		return -EFAULT;
79 80 81
	return 0;
}

82
STATIC int
83
xfs_fsinumbers_fmt_compat(
84 85
	struct xfs_ibulk		*breq,
	const struct xfs_inumbers	*ig)
86
{
87 88 89 90 91
	struct compat_xfs_inogrp __user	*p32 = breq->ubuffer;
	struct xfs_inogrp		ig1;
	struct xfs_inogrp		*igrp = &ig1;

	xfs_inumbers_to_inogrp(&ig1, ig);
92

93 94 95 96 97 98
	if (put_user(igrp->xi_startino,   &p32->xi_startino) ||
	    put_user(igrp->xi_alloccount, &p32->xi_alloccount) ||
	    put_user(igrp->xi_allocmask,  &p32->xi_allocmask))
		return -EFAULT;

	return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_inogrp));
99 100
}

Linus Torvalds's avatar
Linus Torvalds committed
101
#else
102
#define xfs_fsinumbers_fmt_compat xfs_fsinumbers_fmt
103
#endif	/* BROKEN_X86_ALIGNMENT */
104

105 106 107 108 109 110 111 112 113
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
114
		return -EFAULT;
115 116 117 118
	bstime->tv_sec = sec32;
	return 0;
}

119 120 121 122
/*
 * struct xfs_bstat has differing alignment on intel, & bstime_t sizes
 * everywhere
 */
123 124
STATIC int
xfs_ioctl32_bstat_copyin(
125 126
	struct xfs_bstat		*bstat,
	struct compat_xfs_bstat	__user	*bstat32)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
	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)	||
144 145
	    get_user(bstat->bs_projid_lo, &bstat32->bs_projid_lo) ||
	    get_user(bstat->bs_projid_hi, &bstat32->bs_projid_hi) ||
146
	    get_user(bstat->bs_forkoff,	&bstat32->bs_forkoff)	||
147 148 149
	    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
150
		return -EFAULT;
151 152 153
	return 0;
}

154 155
/* XFS_IOC_FSBULKSTAT and friends */

156 157 158 159
STATIC int
xfs_bstime_store_compat(
	compat_xfs_bstime_t	__user *p32,
	const xfs_bstime_t	*p)
160
{
161
	__s32			sec32;
162 163 164 165

	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
166
		return -EFAULT;
167 168 169
	return 0;
}

170
/* Return 0 on success or positive error (to xfs_bulkstat()) */
171
STATIC int
172
xfs_fsbulkstat_one_fmt_compat(
173 174
	struct xfs_ibulk		*breq,
	const struct xfs_bulkstat	*bstat)
175
{
176 177 178 179 180
	struct compat_xfs_bstat	__user	*p32 = breq->ubuffer;
	struct xfs_bstat		bs1;
	struct xfs_bstat		*buffer = &bs1;

	xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
181 182 183 184 185 186 187 188 189

	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)	||
190 191 192
	    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) ||
193 194 195 196 197 198
	    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)	||
199
	    put_user(buffer->bs_projid_hi,	&p32->bs_projid_hi)	||
200
	    put_user(buffer->bs_forkoff,  &p32->bs_forkoff)	||
201 202
	    put_user(buffer->bs_dmevmask, &p32->bs_dmevmask)	||
	    put_user(buffer->bs_dmstate,  &p32->bs_dmstate)	||
203
	    put_user(buffer->bs_aextents, &p32->bs_aextents))
204
		return -EFAULT;
205

206
	return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_bstat));
207 208
}

209 210
/* copied from xfs_ioctl.c */
STATIC int
211
xfs_compat_ioc_fsbulkstat(
212 213
	xfs_mount_t		  *mp,
	unsigned int		  cmd,
214
	struct compat_xfs_fsop_bulkreq __user *p32)
Linus Torvalds's avatar
Linus Torvalds committed
215 216
{
	u32			addr;
217 218 219 220 221 222
	struct xfs_fsop_bulkreq	bulkreq;
	struct xfs_ibulk	breq = {
		.mp		= mp,
		.ocount		= 0,
	};
	xfs_ino_t		lastino;
223 224
	int			error;

225 226 227 228 229 230
	/*
	 * Output structure handling functions.  Depending on the command,
	 * either the xfs_bstat and xfs_inogrp structures are written out
	 * to userpace memory via bulkreq.ubuffer.  Normally the compat
	 * functions and structure size are the correct ones to use ...
	 */
231 232
	inumbers_fmt_pf		inumbers_func = xfs_fsinumbers_fmt_compat;
	bulkstat_one_fmt_pf	bs_one_func = xfs_fsbulkstat_one_fmt_compat;
233 234 235 236 237 238 239 240 241 242 243

#ifdef CONFIG_X86_X32
	if (in_x32_syscall()) {
		/*
		 * ... but on x32 the input xfs_fsop_bulkreq has pointers
		 * which must be handled in the "compat" (32-bit) way, while
		 * the xfs_bstat and xfs_inogrp structures follow native 64-
		 * bit layout convention.  So adjust accordingly, otherwise
		 * the data written out in compat layout will not match what
		 * x32 userspace expects.
		 */
244 245
		inumbers_func = xfs_fsinumbers_fmt;
		bs_one_func = xfs_fsbulkstat_one_fmt;
246 247 248
	}
#endif

249 250
	/* 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
251

252
	if (!capable(CAP_SYS_ADMIN))
Eric Sandeen's avatar
Eric Sandeen committed
253
		return -EPERM;
254 255

	if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeen's avatar
Eric Sandeen committed
256
		return -EIO;
257 258

	if (get_user(addr, &p32->lastip))
Eric Sandeen's avatar
Eric Sandeen committed
259
		return -EFAULT;
260 261 262
	bulkreq.lastip = compat_ptr(addr);
	if (get_user(bulkreq.icount, &p32->icount) ||
	    get_user(addr, &p32->ubuffer))
Eric Sandeen's avatar
Eric Sandeen committed
263
		return -EFAULT;
264 265
	bulkreq.ubuffer = compat_ptr(addr);
	if (get_user(addr, &p32->ocount))
Eric Sandeen's avatar
Eric Sandeen committed
266
		return -EFAULT;
267
	bulkreq.ocount = compat_ptr(addr);
Linus Torvalds's avatar
Linus Torvalds committed
268

269
	if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
Eric Sandeen's avatar
Eric Sandeen committed
270
		return -EFAULT;
271

272
	if (bulkreq.icount <= 0)
Eric Sandeen's avatar
Eric Sandeen committed
273
		return -EINVAL;
274

275
	if (bulkreq.ubuffer == NULL)
Eric Sandeen's avatar
Eric Sandeen committed
276
		return -EINVAL;
277

278 279 280 281 282 283 284 285 286 287 288 289 290 291
	breq.ubuffer = bulkreq.ubuffer;
	breq.icount = bulkreq.icount;

	/*
	 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
	 * that we want to stat.  However, FSINUMBERS and FSBULKSTAT expect
	 * that *lastip contains either zero or the number of the last inode to
	 * be examined by the previous call and return results starting with
	 * the next inode after that.  The new bulk request back end functions
	 * take the inode to start with, so we have to compute the startino
	 * parameter from lastino to maintain correct function.  lastino == 0
	 * is a special case because it has traditionally meant "first inode
	 * in filesystem".
	 */
292
	if (cmd == XFS_IOC_FSINUMBERS_32) {
293 294 295
		breq.startino = lastino ? lastino + 1 : 0;
		error = xfs_inumbers(&breq, inumbers_func);
		lastino = breq.startino - 1;
296
	} else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE_32) {
297 298 299 300
		breq.startino = lastino;
		breq.icount = 1;
		error = xfs_bulkstat_one(&breq, bs_one_func);
		lastino = breq.startino;
301
	} else if (cmd == XFS_IOC_FSBULKSTAT_32) {
302 303 304 305
		breq.startino = lastino ? lastino + 1 : 0;
		error = xfs_bulkstat(&breq, bs_one_func);
		lastino = breq.startino - 1;
	} else {
306
		error = -EINVAL;
307
	}
308
	if (error)
309
		return error;
310

311
	if (bulkreq.lastip != NULL &&
312
	    copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
313
		return -EFAULT;
314

315
	if (bulkreq.ocount != NULL &&
316
	    copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
317
		return -EFAULT;
318 319

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
320
}
321

322 323 324 325
STATIC int
xfs_compat_handlereq_copyin(
	xfs_fsop_handlereq_t		*hreq,
	compat_xfs_fsop_handlereq_t	__user *arg32)
326
{
327 328 329
	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
330
		return -EFAULT;
331 332 333 334 335 336 337 338 339 340

	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;
341 342
}

343 344 345 346
STATIC struct dentry *
xfs_compat_handlereq_to_dentry(
	struct file		*parfilp,
	compat_xfs_fsop_handlereq_t *hreq)
347
{
348 349
	return xfs_handle_to_dentry(parfilp,
			compat_ptr(hreq->ihandle), hreq->ihandlen);
350 351 352 353
}

STATIC int
xfs_compat_attrlist_by_handle(
354 355
	struct file		*parfilp,
	void			__user *arg)
356 357 358
{
	int			error;
	attrlist_cursor_kern_t	*cursor;
359
	compat_xfs_fsop_attrlist_handlereq_t __user *p = arg;
360
	compat_xfs_fsop_attrlist_handlereq_t al_hreq;
361
	struct dentry		*dentry;
362 363 364
	char			*kbuf;

	if (!capable(CAP_SYS_ADMIN))
Eric Sandeen's avatar
Eric Sandeen committed
365
		return -EPERM;
366 367
	if (copy_from_user(&al_hreq, arg,
			   sizeof(compat_xfs_fsop_attrlist_handlereq_t)))
Eric Sandeen's avatar
Eric Sandeen committed
368
		return -EFAULT;
369
	if (al_hreq.buflen < sizeof(struct attrlist) ||
370
	    al_hreq.buflen > XFS_XATTR_LIST_MAX)
Eric Sandeen's avatar
Eric Sandeen committed
371
		return -EINVAL;
372 373 374 375 376

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

379 380 381
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &al_hreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
382

383
	error = -ENOMEM;
384
	kbuf = kmem_zalloc_large(al_hreq.buflen, 0);
385 386
	if (!kbuf)
		goto out_dput;
387 388

	cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
389
	error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
390 391 392 393
					al_hreq.flags, cursor);
	if (error)
		goto out_kfree;

394 395 396 397 398
	if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
		error = -EFAULT;
		goto out_kfree;
	}

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

402 403 404
out_kfree:
	kmem_free(kbuf);
out_dput:
405 406
	dput(dentry);
	return error;
407 408
}

409 410
STATIC int
xfs_compat_attrmulti_by_handle(
411 412
	struct file				*parfilp,
	void					__user *arg)
413 414 415 416
{
	int					error;
	compat_xfs_attr_multiop_t		*ops;
	compat_xfs_fsop_attrmulti_handlereq_t	am_hreq;
417
	struct dentry				*dentry;
418
	unsigned int				i, size;
419
	unsigned char				*attr_name;
420 421

	if (!capable(CAP_SYS_ADMIN))
Eric Sandeen's avatar
Eric Sandeen committed
422
		return -EPERM;
423 424
	if (copy_from_user(&am_hreq, arg,
			   sizeof(compat_xfs_fsop_attrmulti_handlereq_t)))
Eric Sandeen's avatar
Eric Sandeen committed
425
		return -EFAULT;
426

427 428 429 430
	/* overflow check */
	if (am_hreq.opcount >= INT_MAX / sizeof(compat_xfs_attr_multiop_t))
		return -E2BIG;

431 432 433
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &am_hreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
434

435
	error = -E2BIG;
436 437
	size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t);
	if (!size || size > 16 * PAGE_SIZE)
438
		goto out_dput;
439

Li Zefan's avatar
Li Zefan committed
440 441
	ops = memdup_user(compat_ptr(am_hreq.ops), size);
	if (IS_ERR(ops)) {
442
		error = PTR_ERR(ops);
443
		goto out_dput;
Li Zefan's avatar
Li Zefan committed
444
	}
445

446
	error = -ENOMEM;
447 448 449 450 451 452
	attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
	if (!attr_name)
		goto out_kfree_ops;

	error = 0;
	for (i = 0; i < am_hreq.opcount; i++) {
453
		ops[i].am_error = strncpy_from_user((char *)attr_name,
454 455 456
				compat_ptr(ops[i].am_attrname),
				MAXNAMELEN);
		if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
457
			error = -ERANGE;
458 459 460 461 462
		if (ops[i].am_error < 0)
			break;

		switch (ops[i].am_opcode) {
		case ATTR_OP_GET:
463
			ops[i].am_error = xfs_attrmulti_attr_get(
464
					d_inode(dentry), attr_name,
465 466 467 468
					compat_ptr(ops[i].am_attrvalue),
					&ops[i].am_length, ops[i].am_flags);
			break;
		case ATTR_OP_SET:
469
			ops[i].am_error = mnt_want_write_file(parfilp);
470 471
			if (ops[i].am_error)
				break;
472
			ops[i].am_error = xfs_attrmulti_attr_set(
473
					d_inode(dentry), attr_name,
474 475
					compat_ptr(ops[i].am_attrvalue),
					ops[i].am_length, ops[i].am_flags);
Al Viro's avatar
Al Viro committed
476
			mnt_drop_write_file(parfilp);
477 478
			break;
		case ATTR_OP_REMOVE:
479
			ops[i].am_error = mnt_want_write_file(parfilp);
480 481
			if (ops[i].am_error)
				break;
482
			ops[i].am_error = xfs_attrmulti_attr_remove(
483
					d_inode(dentry), attr_name,
484
					ops[i].am_flags);
Al Viro's avatar
Al Viro committed
485
			mnt_drop_write_file(parfilp);
486 487
			break;
		default:
488
			ops[i].am_error = -EINVAL;
489 490 491 492
		}
	}

	if (copy_to_user(compat_ptr(am_hreq.ops), ops, size))
493
		error = -EFAULT;
494 495 496 497

	kfree(attr_name);
 out_kfree_ops:
	kfree(ops);
498 499
 out_dput:
	dput(dentry);
500
	return error;
501 502
}

503 504
STATIC int
xfs_compat_fssetdm_by_handle(
505 506
	struct file		*parfilp,
	void			__user *arg)
507 508 509 510
{
	int			error;
	struct fsdmidata	fsd;
	compat_xfs_fsop_setdm_handlereq_t dmhreq;
511
	struct dentry		*dentry;
512 513

	if (!capable(CAP_MKNOD))
Eric Sandeen's avatar
Eric Sandeen committed
514
		return -EPERM;
515 516
	if (copy_from_user(&dmhreq, arg,
			   sizeof(compat_xfs_fsop_setdm_handlereq_t)))
Eric Sandeen's avatar
Eric Sandeen committed
517
		return -EFAULT;
518

519 520 521
	dentry = xfs_compat_handlereq_to_dentry(parfilp, &dmhreq.hreq);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);
522

523
	if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
Eric Sandeen's avatar
Eric Sandeen committed
524
		error = -EPERM;
525 526 527 528
		goto out;
	}

	if (copy_from_user(&fsd, compat_ptr(dmhreq.data), sizeof(fsd))) {
Eric Sandeen's avatar
Eric Sandeen committed
529
		error = -EFAULT;
530 531 532
		goto out;
	}

533
	error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
534 535 536
				 fsd.fsd_dmstate);

out:
537
	dput(dentry);
538 539 540
	return error;
}

541 542 543 544 545
long
xfs_file_compat_ioctl(
	struct file		*filp,
	unsigned		cmd,
	unsigned long		p)
Linus Torvalds's avatar
Linus Torvalds committed
546
{
Al Viro's avatar
Al Viro committed
547
	struct inode		*inode = file_inode(filp);
548 549
	struct xfs_inode	*ip = XFS_I(inode);
	struct xfs_mount	*mp = ip->i_mount;
550
	void			__user *arg = compat_ptr(p);
551 552
	int			error;

553
	trace_xfs_file_compat_ioctl(ip);
Linus Torvalds's avatar
Linus Torvalds committed
554 555

	switch (cmd) {
556
#if defined(BROKEN_X86_ALIGNMENT)
557 558 559 560 561 562 563
	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:
564 565
	case XFS_IOC_UNRESVSP64_32:
	case XFS_IOC_ZERO_RANGE_32: {
566 567 568
		struct xfs_flock64	bf;

		if (xfs_compat_flock64_copyin(&bf, arg))
Eric Sandeen's avatar
Eric Sandeen committed
569
			return -EFAULT;
570
		cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
571
		return xfs_ioc_space(filp, cmd, &bf);
572
	}
573
	case XFS_IOC_FSGEOMETRY_V1_32:
574
		return xfs_compat_ioc_fsgeometry_v1(mp, arg);
575 576 577 578
	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
579
			return -EFAULT;
Jan Kara's avatar
Jan Kara committed
580 581 582
		error = mnt_want_write_file(filp);
		if (error)
			return error;
583
		error = xfs_growfs_data(mp, &in);
Jan Kara's avatar
Jan Kara committed
584
		mnt_drop_write_file(filp);
585
		return error;
586 587 588 589 590
	}
	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
591
			return -EFAULT;
Jan Kara's avatar
Jan Kara committed
592 593 594
		error = mnt_want_write_file(filp);
		if (error)
			return error;
595
		error = xfs_growfs_rt(mp, &in);
Jan Kara's avatar
Jan Kara committed
596
		mnt_drop_write_file(filp);
597
		return error;
598
	}
Linus Torvalds's avatar
Linus Torvalds committed
599
#endif
600 601 602 603 604
	/* 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);
605
		return xfs_file_ioctl(filp, cmd, p);
606
	case XFS_IOC_SWAPEXT_32: {
607 608 609 610 611 612 613
		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
614
			return -EFAULT;
Jan Kara's avatar
Jan Kara committed
615 616 617
		error = mnt_want_write_file(filp);
		if (error)
			return error;
618
		error = xfs_ioc_swapext(&sxp);
Jan Kara's avatar
Jan Kara committed
619
		mnt_drop_write_file(filp);
620
		return error;
621
	}
622 623 624
	case XFS_IOC_FSBULKSTAT_32:
	case XFS_IOC_FSBULKSTAT_SINGLE_32:
	case XFS_IOC_FSINUMBERS_32:
625
		return xfs_compat_ioc_fsbulkstat(mp, cmd, arg);
626 627
	case XFS_IOC_FD_TO_HANDLE_32:
	case XFS_IOC_PATH_TO_HANDLE_32:
628 629 630 631
	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
632
			return -EFAULT;
633
		cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
634 635 636 637 638 639
		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
640
			return -EFAULT;
641
		return xfs_open_by_handle(filp, &hreq);
642 643 644 645 646
	}
	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
647
			return -EFAULT;
648
		return xfs_readlink_by_handle(filp, &hreq);
649
	}
650
	case XFS_IOC_ATTRLIST_BY_HANDLE_32:
651
		return xfs_compat_attrlist_by_handle(filp, arg);
652
	case XFS_IOC_ATTRMULTI_BY_HANDLE_32:
653
		return xfs_compat_attrmulti_by_handle(filp, arg);
654
	case XFS_IOC_FSSETDM_BY_HANDLE_32:
655
		return xfs_compat_fssetdm_by_handle(filp, arg);
Linus Torvalds's avatar
Linus Torvalds committed
656
	default:
657
		/* try the native version */
658
		return xfs_file_ioctl(filp, cmd, (unsigned long)arg);
Linus Torvalds's avatar
Linus Torvalds committed
659 660
	}
}