xref: /OK3568_Linux_fs/kernel/fs/xfs/xfs_ioctl32.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2004-2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include <linux/mount.h>
7*4882a593Smuzhiyun #include <linux/fsmap.h>
8*4882a593Smuzhiyun #include "xfs.h"
9*4882a593Smuzhiyun #include "xfs_fs.h"
10*4882a593Smuzhiyun #include "xfs_shared.h"
11*4882a593Smuzhiyun #include "xfs_format.h"
12*4882a593Smuzhiyun #include "xfs_log_format.h"
13*4882a593Smuzhiyun #include "xfs_trans_resv.h"
14*4882a593Smuzhiyun #include "xfs_mount.h"
15*4882a593Smuzhiyun #include "xfs_inode.h"
16*4882a593Smuzhiyun #include "xfs_iwalk.h"
17*4882a593Smuzhiyun #include "xfs_itable.h"
18*4882a593Smuzhiyun #include "xfs_fsops.h"
19*4882a593Smuzhiyun #include "xfs_rtalloc.h"
20*4882a593Smuzhiyun #include "xfs_attr.h"
21*4882a593Smuzhiyun #include "xfs_ioctl.h"
22*4882a593Smuzhiyun #include "xfs_ioctl32.h"
23*4882a593Smuzhiyun #include "xfs_trace.h"
24*4882a593Smuzhiyun #include "xfs_sb.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define  _NATIVE_IOC(cmd, type) \
27*4882a593Smuzhiyun 	  _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #ifdef BROKEN_X86_ALIGNMENT
30*4882a593Smuzhiyun STATIC int
xfs_compat_flock64_copyin(xfs_flock64_t * bf,compat_xfs_flock64_t __user * arg32)31*4882a593Smuzhiyun xfs_compat_flock64_copyin(
32*4882a593Smuzhiyun 	xfs_flock64_t		*bf,
33*4882a593Smuzhiyun 	compat_xfs_flock64_t	__user *arg32)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	if (get_user(bf->l_type,	&arg32->l_type) ||
36*4882a593Smuzhiyun 	    get_user(bf->l_whence,	&arg32->l_whence) ||
37*4882a593Smuzhiyun 	    get_user(bf->l_start,	&arg32->l_start) ||
38*4882a593Smuzhiyun 	    get_user(bf->l_len,		&arg32->l_len) ||
39*4882a593Smuzhiyun 	    get_user(bf->l_sysid,	&arg32->l_sysid) ||
40*4882a593Smuzhiyun 	    get_user(bf->l_pid,		&arg32->l_pid) ||
41*4882a593Smuzhiyun 	    copy_from_user(bf->l_pad,	&arg32->l_pad,	4*sizeof(u32)))
42*4882a593Smuzhiyun 		return -EFAULT;
43*4882a593Smuzhiyun 	return 0;
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun STATIC int
xfs_compat_ioc_fsgeometry_v1(struct xfs_mount * mp,compat_xfs_fsop_geom_v1_t __user * arg32)47*4882a593Smuzhiyun xfs_compat_ioc_fsgeometry_v1(
48*4882a593Smuzhiyun 	struct xfs_mount	  *mp,
49*4882a593Smuzhiyun 	compat_xfs_fsop_geom_v1_t __user *arg32)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	struct xfs_fsop_geom	  fsgeo;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	xfs_fs_geometry(&mp->m_sb, &fsgeo, 3);
54*4882a593Smuzhiyun 	/* The 32-bit variant simply has some padding at the end */
55*4882a593Smuzhiyun 	if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
56*4882a593Smuzhiyun 		return -EFAULT;
57*4882a593Smuzhiyun 	return 0;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun STATIC int
xfs_compat_growfs_data_copyin(struct xfs_growfs_data * in,compat_xfs_growfs_data_t __user * arg32)61*4882a593Smuzhiyun xfs_compat_growfs_data_copyin(
62*4882a593Smuzhiyun 	struct xfs_growfs_data	 *in,
63*4882a593Smuzhiyun 	compat_xfs_growfs_data_t __user *arg32)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	if (get_user(in->newblocks, &arg32->newblocks) ||
66*4882a593Smuzhiyun 	    get_user(in->imaxpct,   &arg32->imaxpct))
67*4882a593Smuzhiyun 		return -EFAULT;
68*4882a593Smuzhiyun 	return 0;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun STATIC int
xfs_compat_growfs_rt_copyin(struct xfs_growfs_rt * in,compat_xfs_growfs_rt_t __user * arg32)72*4882a593Smuzhiyun xfs_compat_growfs_rt_copyin(
73*4882a593Smuzhiyun 	struct xfs_growfs_rt	 *in,
74*4882a593Smuzhiyun 	compat_xfs_growfs_rt_t	__user *arg32)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	if (get_user(in->newblocks, &arg32->newblocks) ||
77*4882a593Smuzhiyun 	    get_user(in->extsize,   &arg32->extsize))
78*4882a593Smuzhiyun 		return -EFAULT;
79*4882a593Smuzhiyun 	return 0;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun STATIC int
xfs_fsinumbers_fmt_compat(struct xfs_ibulk * breq,const struct xfs_inumbers * ig)83*4882a593Smuzhiyun xfs_fsinumbers_fmt_compat(
84*4882a593Smuzhiyun 	struct xfs_ibulk		*breq,
85*4882a593Smuzhiyun 	const struct xfs_inumbers	*ig)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	struct compat_xfs_inogrp __user	*p32 = breq->ubuffer;
88*4882a593Smuzhiyun 	struct xfs_inogrp		ig1;
89*4882a593Smuzhiyun 	struct xfs_inogrp		*igrp = &ig1;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	xfs_inumbers_to_inogrp(&ig1, ig);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	if (put_user(igrp->xi_startino,   &p32->xi_startino) ||
94*4882a593Smuzhiyun 	    put_user(igrp->xi_alloccount, &p32->xi_alloccount) ||
95*4882a593Smuzhiyun 	    put_user(igrp->xi_allocmask,  &p32->xi_allocmask))
96*4882a593Smuzhiyun 		return -EFAULT;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_inogrp));
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #else
102*4882a593Smuzhiyun #define xfs_fsinumbers_fmt_compat xfs_fsinumbers_fmt
103*4882a593Smuzhiyun #endif	/* BROKEN_X86_ALIGNMENT */
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun STATIC int
xfs_ioctl32_bstime_copyin(xfs_bstime_t * bstime,compat_xfs_bstime_t __user * bstime32)106*4882a593Smuzhiyun xfs_ioctl32_bstime_copyin(
107*4882a593Smuzhiyun 	xfs_bstime_t		*bstime,
108*4882a593Smuzhiyun 	compat_xfs_bstime_t	__user *bstime32)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	old_time32_t		sec32;	/* tv_sec differs on 64 vs. 32 */
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	if (get_user(sec32,		&bstime32->tv_sec)	||
113*4882a593Smuzhiyun 	    get_user(bstime->tv_nsec,	&bstime32->tv_nsec))
114*4882a593Smuzhiyun 		return -EFAULT;
115*4882a593Smuzhiyun 	bstime->tv_sec = sec32;
116*4882a593Smuzhiyun 	return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun  * struct xfs_bstat has differing alignment on intel, & bstime_t sizes
121*4882a593Smuzhiyun  * everywhere
122*4882a593Smuzhiyun  */
123*4882a593Smuzhiyun STATIC int
xfs_ioctl32_bstat_copyin(struct xfs_bstat * bstat,struct compat_xfs_bstat __user * bstat32)124*4882a593Smuzhiyun xfs_ioctl32_bstat_copyin(
125*4882a593Smuzhiyun 	struct xfs_bstat		*bstat,
126*4882a593Smuzhiyun 	struct compat_xfs_bstat	__user	*bstat32)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	if (get_user(bstat->bs_ino,	&bstat32->bs_ino)	||
129*4882a593Smuzhiyun 	    get_user(bstat->bs_mode,	&bstat32->bs_mode)	||
130*4882a593Smuzhiyun 	    get_user(bstat->bs_nlink,	&bstat32->bs_nlink)	||
131*4882a593Smuzhiyun 	    get_user(bstat->bs_uid,	&bstat32->bs_uid)	||
132*4882a593Smuzhiyun 	    get_user(bstat->bs_gid,	&bstat32->bs_gid)	||
133*4882a593Smuzhiyun 	    get_user(bstat->bs_rdev,	&bstat32->bs_rdev)	||
134*4882a593Smuzhiyun 	    get_user(bstat->bs_blksize,	&bstat32->bs_blksize)	||
135*4882a593Smuzhiyun 	    get_user(bstat->bs_size,	&bstat32->bs_size)	||
136*4882a593Smuzhiyun 	    xfs_ioctl32_bstime_copyin(&bstat->bs_atime, &bstat32->bs_atime) ||
137*4882a593Smuzhiyun 	    xfs_ioctl32_bstime_copyin(&bstat->bs_mtime, &bstat32->bs_mtime) ||
138*4882a593Smuzhiyun 	    xfs_ioctl32_bstime_copyin(&bstat->bs_ctime, &bstat32->bs_ctime) ||
139*4882a593Smuzhiyun 	    get_user(bstat->bs_blocks,	&bstat32->bs_size)	||
140*4882a593Smuzhiyun 	    get_user(bstat->bs_xflags,	&bstat32->bs_size)	||
141*4882a593Smuzhiyun 	    get_user(bstat->bs_extsize,	&bstat32->bs_extsize)	||
142*4882a593Smuzhiyun 	    get_user(bstat->bs_extents,	&bstat32->bs_extents)	||
143*4882a593Smuzhiyun 	    get_user(bstat->bs_gen,	&bstat32->bs_gen)	||
144*4882a593Smuzhiyun 	    get_user(bstat->bs_projid_lo, &bstat32->bs_projid_lo) ||
145*4882a593Smuzhiyun 	    get_user(bstat->bs_projid_hi, &bstat32->bs_projid_hi) ||
146*4882a593Smuzhiyun 	    get_user(bstat->bs_forkoff,	&bstat32->bs_forkoff)	||
147*4882a593Smuzhiyun 	    get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask)	||
148*4882a593Smuzhiyun 	    get_user(bstat->bs_dmstate,	&bstat32->bs_dmstate)	||
149*4882a593Smuzhiyun 	    get_user(bstat->bs_aextents, &bstat32->bs_aextents))
150*4882a593Smuzhiyun 		return -EFAULT;
151*4882a593Smuzhiyun 	return 0;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /* XFS_IOC_FSBULKSTAT and friends */
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun STATIC int
xfs_bstime_store_compat(compat_xfs_bstime_t __user * p32,const xfs_bstime_t * p)157*4882a593Smuzhiyun xfs_bstime_store_compat(
158*4882a593Smuzhiyun 	compat_xfs_bstime_t	__user *p32,
159*4882a593Smuzhiyun 	const xfs_bstime_t	*p)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	__s32			sec32;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	sec32 = p->tv_sec;
164*4882a593Smuzhiyun 	if (put_user(sec32, &p32->tv_sec) ||
165*4882a593Smuzhiyun 	    put_user(p->tv_nsec, &p32->tv_nsec))
166*4882a593Smuzhiyun 		return -EFAULT;
167*4882a593Smuzhiyun 	return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /* Return 0 on success or positive error (to xfs_bulkstat()) */
171*4882a593Smuzhiyun STATIC int
xfs_fsbulkstat_one_fmt_compat(struct xfs_ibulk * breq,const struct xfs_bulkstat * bstat)172*4882a593Smuzhiyun xfs_fsbulkstat_one_fmt_compat(
173*4882a593Smuzhiyun 	struct xfs_ibulk		*breq,
174*4882a593Smuzhiyun 	const struct xfs_bulkstat	*bstat)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct compat_xfs_bstat	__user	*p32 = breq->ubuffer;
177*4882a593Smuzhiyun 	struct xfs_bstat		bs1;
178*4882a593Smuzhiyun 	struct xfs_bstat		*buffer = &bs1;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (put_user(buffer->bs_ino,	  &p32->bs_ino)		||
183*4882a593Smuzhiyun 	    put_user(buffer->bs_mode,	  &p32->bs_mode)	||
184*4882a593Smuzhiyun 	    put_user(buffer->bs_nlink,	  &p32->bs_nlink)	||
185*4882a593Smuzhiyun 	    put_user(buffer->bs_uid,	  &p32->bs_uid)		||
186*4882a593Smuzhiyun 	    put_user(buffer->bs_gid,	  &p32->bs_gid)		||
187*4882a593Smuzhiyun 	    put_user(buffer->bs_rdev,	  &p32->bs_rdev)	||
188*4882a593Smuzhiyun 	    put_user(buffer->bs_blksize,  &p32->bs_blksize)	||
189*4882a593Smuzhiyun 	    put_user(buffer->bs_size,	  &p32->bs_size)	||
190*4882a593Smuzhiyun 	    xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
191*4882a593Smuzhiyun 	    xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
192*4882a593Smuzhiyun 	    xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
193*4882a593Smuzhiyun 	    put_user(buffer->bs_blocks,	  &p32->bs_blocks)	||
194*4882a593Smuzhiyun 	    put_user(buffer->bs_xflags,	  &p32->bs_xflags)	||
195*4882a593Smuzhiyun 	    put_user(buffer->bs_extsize,  &p32->bs_extsize)	||
196*4882a593Smuzhiyun 	    put_user(buffer->bs_extents,  &p32->bs_extents)	||
197*4882a593Smuzhiyun 	    put_user(buffer->bs_gen,	  &p32->bs_gen)		||
198*4882a593Smuzhiyun 	    put_user(buffer->bs_projid,	  &p32->bs_projid)	||
199*4882a593Smuzhiyun 	    put_user(buffer->bs_projid_hi,	&p32->bs_projid_hi)	||
200*4882a593Smuzhiyun 	    put_user(buffer->bs_forkoff,  &p32->bs_forkoff)	||
201*4882a593Smuzhiyun 	    put_user(buffer->bs_dmevmask, &p32->bs_dmevmask)	||
202*4882a593Smuzhiyun 	    put_user(buffer->bs_dmstate,  &p32->bs_dmstate)	||
203*4882a593Smuzhiyun 	    put_user(buffer->bs_aextents, &p32->bs_aextents))
204*4882a593Smuzhiyun 		return -EFAULT;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_bstat));
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun /* copied from xfs_ioctl.c */
210*4882a593Smuzhiyun STATIC int
xfs_compat_ioc_fsbulkstat(xfs_mount_t * mp,unsigned int cmd,struct compat_xfs_fsop_bulkreq __user * p32)211*4882a593Smuzhiyun xfs_compat_ioc_fsbulkstat(
212*4882a593Smuzhiyun 	xfs_mount_t		  *mp,
213*4882a593Smuzhiyun 	unsigned int		  cmd,
214*4882a593Smuzhiyun 	struct compat_xfs_fsop_bulkreq __user *p32)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	u32			addr;
217*4882a593Smuzhiyun 	struct xfs_fsop_bulkreq	bulkreq;
218*4882a593Smuzhiyun 	struct xfs_ibulk	breq = {
219*4882a593Smuzhiyun 		.mp		= mp,
220*4882a593Smuzhiyun 		.ocount		= 0,
221*4882a593Smuzhiyun 	};
222*4882a593Smuzhiyun 	xfs_ino_t		lastino;
223*4882a593Smuzhiyun 	int			error;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/*
226*4882a593Smuzhiyun 	 * Output structure handling functions.  Depending on the command,
227*4882a593Smuzhiyun 	 * either the xfs_bstat and xfs_inogrp structures are written out
228*4882a593Smuzhiyun 	 * to userpace memory via bulkreq.ubuffer.  Normally the compat
229*4882a593Smuzhiyun 	 * functions and structure size are the correct ones to use ...
230*4882a593Smuzhiyun 	 */
231*4882a593Smuzhiyun 	inumbers_fmt_pf		inumbers_func = xfs_fsinumbers_fmt_compat;
232*4882a593Smuzhiyun 	bulkstat_one_fmt_pf	bs_one_func = xfs_fsbulkstat_one_fmt_compat;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun #ifdef CONFIG_X86_X32
235*4882a593Smuzhiyun 	if (in_x32_syscall()) {
236*4882a593Smuzhiyun 		/*
237*4882a593Smuzhiyun 		 * ... but on x32 the input xfs_fsop_bulkreq has pointers
238*4882a593Smuzhiyun 		 * which must be handled in the "compat" (32-bit) way, while
239*4882a593Smuzhiyun 		 * the xfs_bstat and xfs_inogrp structures follow native 64-
240*4882a593Smuzhiyun 		 * bit layout convention.  So adjust accordingly, otherwise
241*4882a593Smuzhiyun 		 * the data written out in compat layout will not match what
242*4882a593Smuzhiyun 		 * x32 userspace expects.
243*4882a593Smuzhiyun 		 */
244*4882a593Smuzhiyun 		inumbers_func = xfs_fsinumbers_fmt;
245*4882a593Smuzhiyun 		bs_one_func = xfs_fsbulkstat_one_fmt;
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun #endif
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/* done = 1 if there are more stats to get and if bulkstat */
250*4882a593Smuzhiyun 	/* should be called again (unused here, but used in dmapi) */
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
253*4882a593Smuzhiyun 		return -EPERM;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (XFS_FORCED_SHUTDOWN(mp))
256*4882a593Smuzhiyun 		return -EIO;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	if (get_user(addr, &p32->lastip))
259*4882a593Smuzhiyun 		return -EFAULT;
260*4882a593Smuzhiyun 	bulkreq.lastip = compat_ptr(addr);
261*4882a593Smuzhiyun 	if (get_user(bulkreq.icount, &p32->icount) ||
262*4882a593Smuzhiyun 	    get_user(addr, &p32->ubuffer))
263*4882a593Smuzhiyun 		return -EFAULT;
264*4882a593Smuzhiyun 	bulkreq.ubuffer = compat_ptr(addr);
265*4882a593Smuzhiyun 	if (get_user(addr, &p32->ocount))
266*4882a593Smuzhiyun 		return -EFAULT;
267*4882a593Smuzhiyun 	bulkreq.ocount = compat_ptr(addr);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
270*4882a593Smuzhiyun 		return -EFAULT;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	if (bulkreq.icount <= 0)
273*4882a593Smuzhiyun 		return -EINVAL;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (bulkreq.ubuffer == NULL)
276*4882a593Smuzhiyun 		return -EINVAL;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	breq.ubuffer = bulkreq.ubuffer;
279*4882a593Smuzhiyun 	breq.icount = bulkreq.icount;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/*
282*4882a593Smuzhiyun 	 * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
283*4882a593Smuzhiyun 	 * that we want to stat.  However, FSINUMBERS and FSBULKSTAT expect
284*4882a593Smuzhiyun 	 * that *lastip contains either zero or the number of the last inode to
285*4882a593Smuzhiyun 	 * be examined by the previous call and return results starting with
286*4882a593Smuzhiyun 	 * the next inode after that.  The new bulk request back end functions
287*4882a593Smuzhiyun 	 * take the inode to start with, so we have to compute the startino
288*4882a593Smuzhiyun 	 * parameter from lastino to maintain correct function.  lastino == 0
289*4882a593Smuzhiyun 	 * is a special case because it has traditionally meant "first inode
290*4882a593Smuzhiyun 	 * in filesystem".
291*4882a593Smuzhiyun 	 */
292*4882a593Smuzhiyun 	if (cmd == XFS_IOC_FSINUMBERS_32) {
293*4882a593Smuzhiyun 		breq.startino = lastino ? lastino + 1 : 0;
294*4882a593Smuzhiyun 		error = xfs_inumbers(&breq, inumbers_func);
295*4882a593Smuzhiyun 		lastino = breq.startino - 1;
296*4882a593Smuzhiyun 	} else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE_32) {
297*4882a593Smuzhiyun 		breq.startino = lastino;
298*4882a593Smuzhiyun 		breq.icount = 1;
299*4882a593Smuzhiyun 		error = xfs_bulkstat_one(&breq, bs_one_func);
300*4882a593Smuzhiyun 		lastino = breq.startino;
301*4882a593Smuzhiyun 	} else if (cmd == XFS_IOC_FSBULKSTAT_32) {
302*4882a593Smuzhiyun 		breq.startino = lastino ? lastino + 1 : 0;
303*4882a593Smuzhiyun 		error = xfs_bulkstat(&breq, bs_one_func);
304*4882a593Smuzhiyun 		lastino = breq.startino - 1;
305*4882a593Smuzhiyun 	} else {
306*4882a593Smuzhiyun 		error = -EINVAL;
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 	if (error)
309*4882a593Smuzhiyun 		return error;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (bulkreq.lastip != NULL &&
312*4882a593Smuzhiyun 	    copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
313*4882a593Smuzhiyun 		return -EFAULT;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	if (bulkreq.ocount != NULL &&
316*4882a593Smuzhiyun 	    copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
317*4882a593Smuzhiyun 		return -EFAULT;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	return 0;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun STATIC int
xfs_compat_handlereq_copyin(xfs_fsop_handlereq_t * hreq,compat_xfs_fsop_handlereq_t __user * arg32)323*4882a593Smuzhiyun xfs_compat_handlereq_copyin(
324*4882a593Smuzhiyun 	xfs_fsop_handlereq_t		*hreq,
325*4882a593Smuzhiyun 	compat_xfs_fsop_handlereq_t	__user *arg32)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	compat_xfs_fsop_handlereq_t	hreq32;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
330*4882a593Smuzhiyun 		return -EFAULT;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	hreq->fd = hreq32.fd;
333*4882a593Smuzhiyun 	hreq->path = compat_ptr(hreq32.path);
334*4882a593Smuzhiyun 	hreq->oflags = hreq32.oflags;
335*4882a593Smuzhiyun 	hreq->ihandle = compat_ptr(hreq32.ihandle);
336*4882a593Smuzhiyun 	hreq->ihandlen = hreq32.ihandlen;
337*4882a593Smuzhiyun 	hreq->ohandle = compat_ptr(hreq32.ohandle);
338*4882a593Smuzhiyun 	hreq->ohandlen = compat_ptr(hreq32.ohandlen);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	return 0;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun STATIC struct dentry *
xfs_compat_handlereq_to_dentry(struct file * parfilp,compat_xfs_fsop_handlereq_t * hreq)344*4882a593Smuzhiyun xfs_compat_handlereq_to_dentry(
345*4882a593Smuzhiyun 	struct file		*parfilp,
346*4882a593Smuzhiyun 	compat_xfs_fsop_handlereq_t *hreq)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	return xfs_handle_to_dentry(parfilp,
349*4882a593Smuzhiyun 			compat_ptr(hreq->ihandle), hreq->ihandlen);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun STATIC int
xfs_compat_attrlist_by_handle(struct file * parfilp,compat_xfs_fsop_attrlist_handlereq_t __user * p)353*4882a593Smuzhiyun xfs_compat_attrlist_by_handle(
354*4882a593Smuzhiyun 	struct file		*parfilp,
355*4882a593Smuzhiyun 	compat_xfs_fsop_attrlist_handlereq_t __user *p)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	compat_xfs_fsop_attrlist_handlereq_t al_hreq;
358*4882a593Smuzhiyun 	struct dentry		*dentry;
359*4882a593Smuzhiyun 	int			error;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
362*4882a593Smuzhiyun 		return -EPERM;
363*4882a593Smuzhiyun 	if (copy_from_user(&al_hreq, p, sizeof(al_hreq)))
364*4882a593Smuzhiyun 		return -EFAULT;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	dentry = xfs_compat_handlereq_to_dentry(parfilp, &al_hreq.hreq);
367*4882a593Smuzhiyun 	if (IS_ERR(dentry))
368*4882a593Smuzhiyun 		return PTR_ERR(dentry);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	error = xfs_ioc_attr_list(XFS_I(d_inode(dentry)),
371*4882a593Smuzhiyun 			compat_ptr(al_hreq.buffer), al_hreq.buflen,
372*4882a593Smuzhiyun 			al_hreq.flags, &p->pos);
373*4882a593Smuzhiyun 	dput(dentry);
374*4882a593Smuzhiyun 	return error;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun STATIC int
xfs_compat_attrmulti_by_handle(struct file * parfilp,void __user * arg)378*4882a593Smuzhiyun xfs_compat_attrmulti_by_handle(
379*4882a593Smuzhiyun 	struct file				*parfilp,
380*4882a593Smuzhiyun 	void					__user *arg)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	int					error;
383*4882a593Smuzhiyun 	compat_xfs_attr_multiop_t		*ops;
384*4882a593Smuzhiyun 	compat_xfs_fsop_attrmulti_handlereq_t	am_hreq;
385*4882a593Smuzhiyun 	struct dentry				*dentry;
386*4882a593Smuzhiyun 	unsigned int				i, size;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
389*4882a593Smuzhiyun 		return -EPERM;
390*4882a593Smuzhiyun 	if (copy_from_user(&am_hreq, arg,
391*4882a593Smuzhiyun 			   sizeof(compat_xfs_fsop_attrmulti_handlereq_t)))
392*4882a593Smuzhiyun 		return -EFAULT;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* overflow check */
395*4882a593Smuzhiyun 	if (am_hreq.opcount >= INT_MAX / sizeof(compat_xfs_attr_multiop_t))
396*4882a593Smuzhiyun 		return -E2BIG;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	dentry = xfs_compat_handlereq_to_dentry(parfilp, &am_hreq.hreq);
399*4882a593Smuzhiyun 	if (IS_ERR(dentry))
400*4882a593Smuzhiyun 		return PTR_ERR(dentry);
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	error = -E2BIG;
403*4882a593Smuzhiyun 	size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t);
404*4882a593Smuzhiyun 	if (!size || size > 16 * PAGE_SIZE)
405*4882a593Smuzhiyun 		goto out_dput;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	ops = memdup_user(compat_ptr(am_hreq.ops), size);
408*4882a593Smuzhiyun 	if (IS_ERR(ops)) {
409*4882a593Smuzhiyun 		error = PTR_ERR(ops);
410*4882a593Smuzhiyun 		goto out_dput;
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	error = 0;
414*4882a593Smuzhiyun 	for (i = 0; i < am_hreq.opcount; i++) {
415*4882a593Smuzhiyun 		ops[i].am_error = xfs_ioc_attrmulti_one(parfilp,
416*4882a593Smuzhiyun 				d_inode(dentry), ops[i].am_opcode,
417*4882a593Smuzhiyun 				compat_ptr(ops[i].am_attrname),
418*4882a593Smuzhiyun 				compat_ptr(ops[i].am_attrvalue),
419*4882a593Smuzhiyun 				&ops[i].am_length, ops[i].am_flags);
420*4882a593Smuzhiyun 	}
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	if (copy_to_user(compat_ptr(am_hreq.ops), ops, size))
423*4882a593Smuzhiyun 		error = -EFAULT;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	kfree(ops);
426*4882a593Smuzhiyun  out_dput:
427*4882a593Smuzhiyun 	dput(dentry);
428*4882a593Smuzhiyun 	return error;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun long
xfs_file_compat_ioctl(struct file * filp,unsigned cmd,unsigned long p)432*4882a593Smuzhiyun xfs_file_compat_ioctl(
433*4882a593Smuzhiyun 	struct file		*filp,
434*4882a593Smuzhiyun 	unsigned		cmd,
435*4882a593Smuzhiyun 	unsigned long		p)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	struct inode		*inode = file_inode(filp);
438*4882a593Smuzhiyun 	struct xfs_inode	*ip = XFS_I(inode);
439*4882a593Smuzhiyun 	struct xfs_mount	*mp = ip->i_mount;
440*4882a593Smuzhiyun 	void			__user *arg = compat_ptr(p);
441*4882a593Smuzhiyun 	int			error;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	trace_xfs_file_compat_ioctl(ip);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	switch (cmd) {
446*4882a593Smuzhiyun #if defined(BROKEN_X86_ALIGNMENT)
447*4882a593Smuzhiyun 	case XFS_IOC_ALLOCSP_32:
448*4882a593Smuzhiyun 	case XFS_IOC_FREESP_32:
449*4882a593Smuzhiyun 	case XFS_IOC_ALLOCSP64_32:
450*4882a593Smuzhiyun 	case XFS_IOC_FREESP64_32: {
451*4882a593Smuzhiyun 		struct xfs_flock64	bf;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 		if (xfs_compat_flock64_copyin(&bf, arg))
454*4882a593Smuzhiyun 			return -EFAULT;
455*4882a593Smuzhiyun 		cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
456*4882a593Smuzhiyun 		return xfs_ioc_space(filp, &bf);
457*4882a593Smuzhiyun 	}
458*4882a593Smuzhiyun 	case XFS_IOC_FSGEOMETRY_V1_32:
459*4882a593Smuzhiyun 		return xfs_compat_ioc_fsgeometry_v1(mp, arg);
460*4882a593Smuzhiyun 	case XFS_IOC_FSGROWFSDATA_32: {
461*4882a593Smuzhiyun 		struct xfs_growfs_data	in;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 		if (xfs_compat_growfs_data_copyin(&in, arg))
464*4882a593Smuzhiyun 			return -EFAULT;
465*4882a593Smuzhiyun 		error = mnt_want_write_file(filp);
466*4882a593Smuzhiyun 		if (error)
467*4882a593Smuzhiyun 			return error;
468*4882a593Smuzhiyun 		error = xfs_growfs_data(mp, &in);
469*4882a593Smuzhiyun 		mnt_drop_write_file(filp);
470*4882a593Smuzhiyun 		return error;
471*4882a593Smuzhiyun 	}
472*4882a593Smuzhiyun 	case XFS_IOC_FSGROWFSRT_32: {
473*4882a593Smuzhiyun 		struct xfs_growfs_rt	in;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 		if (xfs_compat_growfs_rt_copyin(&in, arg))
476*4882a593Smuzhiyun 			return -EFAULT;
477*4882a593Smuzhiyun 		error = mnt_want_write_file(filp);
478*4882a593Smuzhiyun 		if (error)
479*4882a593Smuzhiyun 			return error;
480*4882a593Smuzhiyun 		error = xfs_growfs_rt(mp, &in);
481*4882a593Smuzhiyun 		mnt_drop_write_file(filp);
482*4882a593Smuzhiyun 		return error;
483*4882a593Smuzhiyun 	}
484*4882a593Smuzhiyun #endif
485*4882a593Smuzhiyun 	/* long changes size, but xfs only copiese out 32 bits */
486*4882a593Smuzhiyun 	case XFS_IOC_GETXFLAGS_32:
487*4882a593Smuzhiyun 	case XFS_IOC_SETXFLAGS_32:
488*4882a593Smuzhiyun 	case XFS_IOC_GETVERSION_32:
489*4882a593Smuzhiyun 		cmd = _NATIVE_IOC(cmd, long);
490*4882a593Smuzhiyun 		return xfs_file_ioctl(filp, cmd, p);
491*4882a593Smuzhiyun 	case XFS_IOC_SWAPEXT_32: {
492*4882a593Smuzhiyun 		struct xfs_swapext	  sxp;
493*4882a593Smuzhiyun 		struct compat_xfs_swapext __user *sxu = arg;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		/* Bulk copy in up to the sx_stat field, then copy bstat */
496*4882a593Smuzhiyun 		if (copy_from_user(&sxp, sxu,
497*4882a593Smuzhiyun 				   offsetof(struct xfs_swapext, sx_stat)) ||
498*4882a593Smuzhiyun 		    xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat))
499*4882a593Smuzhiyun 			return -EFAULT;
500*4882a593Smuzhiyun 		error = mnt_want_write_file(filp);
501*4882a593Smuzhiyun 		if (error)
502*4882a593Smuzhiyun 			return error;
503*4882a593Smuzhiyun 		error = xfs_ioc_swapext(&sxp);
504*4882a593Smuzhiyun 		mnt_drop_write_file(filp);
505*4882a593Smuzhiyun 		return error;
506*4882a593Smuzhiyun 	}
507*4882a593Smuzhiyun 	case XFS_IOC_FSBULKSTAT_32:
508*4882a593Smuzhiyun 	case XFS_IOC_FSBULKSTAT_SINGLE_32:
509*4882a593Smuzhiyun 	case XFS_IOC_FSINUMBERS_32:
510*4882a593Smuzhiyun 		return xfs_compat_ioc_fsbulkstat(mp, cmd, arg);
511*4882a593Smuzhiyun 	case XFS_IOC_FD_TO_HANDLE_32:
512*4882a593Smuzhiyun 	case XFS_IOC_PATH_TO_HANDLE_32:
513*4882a593Smuzhiyun 	case XFS_IOC_PATH_TO_FSHANDLE_32: {
514*4882a593Smuzhiyun 		struct xfs_fsop_handlereq	hreq;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 		if (xfs_compat_handlereq_copyin(&hreq, arg))
517*4882a593Smuzhiyun 			return -EFAULT;
518*4882a593Smuzhiyun 		cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
519*4882a593Smuzhiyun 		return xfs_find_handle(cmd, &hreq);
520*4882a593Smuzhiyun 	}
521*4882a593Smuzhiyun 	case XFS_IOC_OPEN_BY_HANDLE_32: {
522*4882a593Smuzhiyun 		struct xfs_fsop_handlereq	hreq;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 		if (xfs_compat_handlereq_copyin(&hreq, arg))
525*4882a593Smuzhiyun 			return -EFAULT;
526*4882a593Smuzhiyun 		return xfs_open_by_handle(filp, &hreq);
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun 	case XFS_IOC_READLINK_BY_HANDLE_32: {
529*4882a593Smuzhiyun 		struct xfs_fsop_handlereq	hreq;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 		if (xfs_compat_handlereq_copyin(&hreq, arg))
532*4882a593Smuzhiyun 			return -EFAULT;
533*4882a593Smuzhiyun 		return xfs_readlink_by_handle(filp, &hreq);
534*4882a593Smuzhiyun 	}
535*4882a593Smuzhiyun 	case XFS_IOC_ATTRLIST_BY_HANDLE_32:
536*4882a593Smuzhiyun 		return xfs_compat_attrlist_by_handle(filp, arg);
537*4882a593Smuzhiyun 	case XFS_IOC_ATTRMULTI_BY_HANDLE_32:
538*4882a593Smuzhiyun 		return xfs_compat_attrmulti_by_handle(filp, arg);
539*4882a593Smuzhiyun 	default:
540*4882a593Smuzhiyun 		/* try the native version */
541*4882a593Smuzhiyun 		return xfs_file_ioctl(filp, cmd, (unsigned long)arg);
542*4882a593Smuzhiyun 	}
543*4882a593Smuzhiyun }
544