Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2004-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
62e194ec | 6 | #include <linux/mount.h> |
e89c0413 | 7 | #include <linux/fsmap.h> |
1da177e4 | 8 | #include "xfs.h" |
1da177e4 | 9 | #include "xfs_fs.h" |
5467b34b | 10 | #include "xfs_shared.h" |
a4fbe6ab | 11 | #include "xfs_format.h" |
239880ef DC |
12 | #include "xfs_log_format.h" |
13 | #include "xfs_trans_resv.h" | |
faa63e95 | 14 | #include "xfs_mount.h" |
faa63e95 | 15 | #include "xfs_inode.h" |
2810bd68 | 16 | #include "xfs_iwalk.h" |
faa63e95 | 17 | #include "xfs_itable.h" |
d5547f9f | 18 | #include "xfs_fsops.h" |
471d5910 | 19 | #include "xfs_rtalloc.h" |
ebeecd2b | 20 | #include "xfs_attr.h" |
d5547f9f | 21 | #include "xfs_ioctl.h" |
a8272ce0 | 22 | #include "xfs_ioctl32.h" |
0b1b213f | 23 | #include "xfs_trace.h" |
c368ebcd | 24 | #include "xfs_sb.h" |
1da177e4 | 25 | |
526c420c ES |
26 | #define _NATIVE_IOC(cmd, type) \ |
27 | _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type)) | |
28 | ||
ffae263a | 29 | #ifdef BROKEN_X86_ALIGNMENT |
d5547f9f | 30 | STATIC int |
31 | xfs_compat_flock64_copyin( | |
32 | xfs_flock64_t *bf, | |
33 | compat_xfs_flock64_t __user *arg32) | |
526c420c | 34 | { |
d5547f9f | 35 | if (get_user(bf->l_type, &arg32->l_type) || |
36 | get_user(bf->l_whence, &arg32->l_whence) || | |
37 | get_user(bf->l_start, &arg32->l_start) || | |
38 | get_user(bf->l_len, &arg32->l_len) || | |
39 | get_user(bf->l_sysid, &arg32->l_sysid) || | |
40 | get_user(bf->l_pid, &arg32->l_pid) || | |
41 | copy_from_user(bf->l_pad, &arg32->l_pad, 4*sizeof(u32))) | |
b474c7ae | 42 | return -EFAULT; |
d5547f9f | 43 | return 0; |
526c420c ES |
44 | } |
45 | ||
d5547f9f | 46 | STATIC int |
47 | xfs_compat_ioc_fsgeometry_v1( | |
48 | struct xfs_mount *mp, | |
49 | compat_xfs_fsop_geom_v1_t __user *arg32) | |
547e00c3 | 50 | { |
1b6d968d | 51 | struct xfs_fsop_geom fsgeo; |
547e00c3 | 52 | |
91083269 | 53 | xfs_fs_geometry(&mp->m_sb, &fsgeo, 3); |
d5547f9f | 54 | /* The 32-bit variant simply has some padding at the end */ |
55 | if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1))) | |
b474c7ae | 56 | return -EFAULT; |
d5547f9f | 57 | return 0; |
547e00c3 MM |
58 | } |
59 | ||
471d5910 | 60 | STATIC int |
61 | xfs_compat_growfs_data_copyin( | |
62 | struct xfs_growfs_data *in, | |
63 | compat_xfs_growfs_data_t __user *arg32) | |
64 | { | |
65 | if (get_user(in->newblocks, &arg32->newblocks) || | |
66 | get_user(in->imaxpct, &arg32->imaxpct)) | |
b474c7ae | 67 | return -EFAULT; |
471d5910 | 68 | return 0; |
69 | } | |
70 | ||
71 | STATIC int | |
72 | xfs_compat_growfs_rt_copyin( | |
73 | struct xfs_growfs_rt *in, | |
74 | compat_xfs_growfs_rt_t __user *arg32) | |
75 | { | |
76 | if (get_user(in->newblocks, &arg32->newblocks) || | |
77 | get_user(in->extsize, &arg32->extsize)) | |
b474c7ae | 78 | return -EFAULT; |
471d5910 | 79 | return 0; |
80 | } | |
81 | ||
d5547f9f | 82 | STATIC int |
83 | xfs_inumbers_fmt_compat( | |
84 | void __user *ubuffer, | |
549fa006 | 85 | const struct xfs_inogrp *buffer, |
d5547f9f | 86 | long count, |
87 | long *written) | |
faa63e95 | 88 | { |
d5547f9f | 89 | compat_xfs_inogrp_t __user *p32 = ubuffer; |
90 | long i; | |
faa63e95 MM |
91 | |
92 | for (i = 0; i < count; i++) { | |
93 | if (put_user(buffer[i].xi_startino, &p32[i].xi_startino) || | |
94 | put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) || | |
95 | put_user(buffer[i].xi_allocmask, &p32[i].xi_allocmask)) | |
b474c7ae | 96 | return -EFAULT; |
faa63e95 MM |
97 | } |
98 | *written = count * sizeof(*p32); | |
99 | return 0; | |
100 | } | |
101 | ||
1da177e4 | 102 | #else |
faa63e95 | 103 | #define xfs_inumbers_fmt_compat xfs_inumbers_fmt |
e5d412f1 | 104 | #endif /* BROKEN_X86_ALIGNMENT */ |
faa63e95 | 105 | |
e94fc4a4 | 106 | STATIC int |
107 | xfs_ioctl32_bstime_copyin( | |
108 | xfs_bstime_t *bstime, | |
109 | compat_xfs_bstime_t __user *bstime32) | |
110 | { | |
111 | compat_time_t sec32; /* tv_sec differs on 64 vs. 32 */ | |
112 | ||
113 | if (get_user(sec32, &bstime32->tv_sec) || | |
114 | get_user(bstime->tv_nsec, &bstime32->tv_nsec)) | |
b474c7ae | 115 | return -EFAULT; |
e94fc4a4 | 116 | bstime->tv_sec = sec32; |
117 | return 0; | |
118 | } | |
119 | ||
120 | /* xfs_bstat_t has differing alignment on intel, & bstime_t sizes everywhere */ | |
121 | STATIC int | |
122 | xfs_ioctl32_bstat_copyin( | |
123 | xfs_bstat_t *bstat, | |
124 | compat_xfs_bstat_t __user *bstat32) | |
125 | { | |
126 | if (get_user(bstat->bs_ino, &bstat32->bs_ino) || | |
127 | get_user(bstat->bs_mode, &bstat32->bs_mode) || | |
128 | get_user(bstat->bs_nlink, &bstat32->bs_nlink) || | |
129 | get_user(bstat->bs_uid, &bstat32->bs_uid) || | |
130 | get_user(bstat->bs_gid, &bstat32->bs_gid) || | |
131 | get_user(bstat->bs_rdev, &bstat32->bs_rdev) || | |
132 | get_user(bstat->bs_blksize, &bstat32->bs_blksize) || | |
133 | get_user(bstat->bs_size, &bstat32->bs_size) || | |
134 | xfs_ioctl32_bstime_copyin(&bstat->bs_atime, &bstat32->bs_atime) || | |
135 | xfs_ioctl32_bstime_copyin(&bstat->bs_mtime, &bstat32->bs_mtime) || | |
136 | xfs_ioctl32_bstime_copyin(&bstat->bs_ctime, &bstat32->bs_ctime) || | |
137 | get_user(bstat->bs_blocks, &bstat32->bs_size) || | |
138 | get_user(bstat->bs_xflags, &bstat32->bs_size) || | |
139 | get_user(bstat->bs_extsize, &bstat32->bs_extsize) || | |
140 | get_user(bstat->bs_extents, &bstat32->bs_extents) || | |
141 | get_user(bstat->bs_gen, &bstat32->bs_gen) || | |
6743099c AM |
142 | get_user(bstat->bs_projid_lo, &bstat32->bs_projid_lo) || |
143 | get_user(bstat->bs_projid_hi, &bstat32->bs_projid_hi) || | |
b1d6cc02 | 144 | get_user(bstat->bs_forkoff, &bstat32->bs_forkoff) || |
e94fc4a4 | 145 | get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask) || |
146 | get_user(bstat->bs_dmstate, &bstat32->bs_dmstate) || | |
147 | get_user(bstat->bs_aextents, &bstat32->bs_aextents)) | |
b474c7ae | 148 | return -EFAULT; |
e94fc4a4 | 149 | return 0; |
150 | } | |
151 | ||
faa63e95 MM |
152 | /* XFS_IOC_FSBULKSTAT and friends */ |
153 | ||
d5547f9f | 154 | STATIC int |
155 | xfs_bstime_store_compat( | |
156 | compat_xfs_bstime_t __user *p32, | |
157 | const xfs_bstime_t *p) | |
faa63e95 | 158 | { |
d5547f9f | 159 | __s32 sec32; |
faa63e95 MM |
160 | |
161 | sec32 = p->tv_sec; | |
162 | if (put_user(sec32, &p32->tv_sec) || | |
163 | put_user(p->tv_nsec, &p32->tv_nsec)) | |
b474c7ae | 164 | return -EFAULT; |
faa63e95 MM |
165 | return 0; |
166 | } | |
167 | ||
65fbaf24 | 168 | /* Return 0 on success or positive error (to xfs_bulkstat()) */ |
d5547f9f | 169 | STATIC int |
170 | xfs_bulkstat_one_fmt_compat( | |
2810bd68 DW |
171 | struct xfs_ibulk *breq, |
172 | const struct xfs_bstat *buffer) | |
faa63e95 | 173 | { |
2810bd68 | 174 | struct compat_xfs_bstat __user *p32 = breq->ubuffer; |
65fbaf24 | 175 | |
176 | if (put_user(buffer->bs_ino, &p32->bs_ino) || | |
177 | put_user(buffer->bs_mode, &p32->bs_mode) || | |
178 | put_user(buffer->bs_nlink, &p32->bs_nlink) || | |
179 | put_user(buffer->bs_uid, &p32->bs_uid) || | |
180 | put_user(buffer->bs_gid, &p32->bs_gid) || | |
181 | put_user(buffer->bs_rdev, &p32->bs_rdev) || | |
182 | put_user(buffer->bs_blksize, &p32->bs_blksize) || | |
183 | put_user(buffer->bs_size, &p32->bs_size) || | |
faa63e95 MM |
184 | xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) || |
185 | xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) || | |
186 | xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) || | |
65fbaf24 | 187 | put_user(buffer->bs_blocks, &p32->bs_blocks) || |
188 | put_user(buffer->bs_xflags, &p32->bs_xflags) || | |
189 | put_user(buffer->bs_extsize, &p32->bs_extsize) || | |
190 | put_user(buffer->bs_extents, &p32->bs_extents) || | |
191 | put_user(buffer->bs_gen, &p32->bs_gen) || | |
192 | put_user(buffer->bs_projid, &p32->bs_projid) || | |
6743099c | 193 | put_user(buffer->bs_projid_hi, &p32->bs_projid_hi) || |
b1d6cc02 | 194 | put_user(buffer->bs_forkoff, &p32->bs_forkoff) || |
65fbaf24 | 195 | put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) || |
196 | put_user(buffer->bs_dmstate, &p32->bs_dmstate) || | |
faa63e95 | 197 | put_user(buffer->bs_aextents, &p32->bs_aextents)) |
2451337d | 198 | return -EFAULT; |
faa63e95 | 199 | |
2810bd68 | 200 | return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_bstat)); |
2ee4fa5c | 201 | } |
202 | ||
faa63e95 MM |
203 | /* copied from xfs_ioctl.c */ |
204 | STATIC int | |
2ee4fa5c | 205 | xfs_compat_ioc_bulkstat( |
206 | xfs_mount_t *mp, | |
207 | unsigned int cmd, | |
208 | compat_xfs_fsop_bulkreq_t __user *p32) | |
1da177e4 | 209 | { |
1da177e4 | 210 | u32 addr; |
2810bd68 DW |
211 | struct xfs_fsop_bulkreq bulkreq; |
212 | struct xfs_ibulk breq = { | |
213 | .mp = mp, | |
214 | .ocount = 0, | |
215 | }; | |
216 | xfs_ino_t lastino; | |
faa63e95 MM |
217 | int error; |
218 | ||
7ca860e3 NB |
219 | /* |
220 | * Output structure handling functions. Depending on the command, | |
221 | * either the xfs_bstat and xfs_inogrp structures are written out | |
222 | * to userpace memory via bulkreq.ubuffer. Normally the compat | |
223 | * functions and structure size are the correct ones to use ... | |
224 | */ | |
225 | inumbers_fmt_pf inumbers_func = xfs_inumbers_fmt_compat; | |
2810bd68 | 226 | bulkstat_one_fmt_pf bs_one_func = xfs_bulkstat_one_fmt_compat; |
7ca860e3 NB |
227 | |
228 | #ifdef CONFIG_X86_X32 | |
229 | if (in_x32_syscall()) { | |
230 | /* | |
231 | * ... but on x32 the input xfs_fsop_bulkreq has pointers | |
232 | * which must be handled in the "compat" (32-bit) way, while | |
233 | * the xfs_bstat and xfs_inogrp structures follow native 64- | |
234 | * bit layout convention. So adjust accordingly, otherwise | |
235 | * the data written out in compat layout will not match what | |
236 | * x32 userspace expects. | |
237 | */ | |
238 | inumbers_func = xfs_inumbers_fmt; | |
2810bd68 | 239 | bs_one_func = xfs_bulkstat_one_fmt; |
7ca860e3 NB |
240 | } |
241 | #endif | |
242 | ||
faa63e95 MM |
243 | /* done = 1 if there are more stats to get and if bulkstat */ |
244 | /* should be called again (unused here, but used in dmapi) */ | |
1da177e4 | 245 | |
faa63e95 | 246 | if (!capable(CAP_SYS_ADMIN)) |
b474c7ae | 247 | return -EPERM; |
faa63e95 MM |
248 | |
249 | if (XFS_FORCED_SHUTDOWN(mp)) | |
b474c7ae | 250 | return -EIO; |
faa63e95 MM |
251 | |
252 | if (get_user(addr, &p32->lastip)) | |
b474c7ae | 253 | return -EFAULT; |
faa63e95 MM |
254 | bulkreq.lastip = compat_ptr(addr); |
255 | if (get_user(bulkreq.icount, &p32->icount) || | |
256 | get_user(addr, &p32->ubuffer)) | |
b474c7ae | 257 | return -EFAULT; |
faa63e95 MM |
258 | bulkreq.ubuffer = compat_ptr(addr); |
259 | if (get_user(addr, &p32->ocount)) | |
b474c7ae | 260 | return -EFAULT; |
faa63e95 | 261 | bulkreq.ocount = compat_ptr(addr); |
1da177e4 | 262 | |
2810bd68 | 263 | if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64))) |
b474c7ae | 264 | return -EFAULT; |
faa63e95 | 265 | |
2810bd68 | 266 | if (bulkreq.icount <= 0) |
b474c7ae | 267 | return -EINVAL; |
faa63e95 | 268 | |
cd57e594 | 269 | if (bulkreq.ubuffer == NULL) |
b474c7ae | 270 | return -EINVAL; |
cd57e594 | 271 | |
2810bd68 DW |
272 | breq.ubuffer = bulkreq.ubuffer; |
273 | breq.icount = bulkreq.icount; | |
274 | ||
275 | /* | |
276 | * FSBULKSTAT_SINGLE expects that *lastip contains the inode number | |
277 | * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect | |
278 | * that *lastip contains either zero or the number of the last inode to | |
279 | * be examined by the previous call and return results starting with | |
280 | * the next inode after that. The new bulk request back end functions | |
281 | * take the inode to start with, so we have to compute the startino | |
282 | * parameter from lastino to maintain correct function. lastino == 0 | |
283 | * is a special case because it has traditionally meant "first inode | |
284 | * in filesystem". | |
285 | */ | |
af819d27 | 286 | if (cmd == XFS_IOC_FSINUMBERS_32) { |
2810bd68 DW |
287 | int count = breq.icount; |
288 | ||
289 | breq.startino = lastino; | |
290 | error = xfs_inumbers(mp, &breq.startino, &count, | |
7ca860e3 | 291 | bulkreq.ubuffer, inumbers_func); |
2810bd68 DW |
292 | breq.ocount = count; |
293 | lastino = breq.startino; | |
af819d27 | 294 | } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE_32) { |
2810bd68 DW |
295 | breq.startino = lastino; |
296 | breq.icount = 1; | |
297 | error = xfs_bulkstat_one(&breq, bs_one_func); | |
298 | lastino = breq.startino; | |
af819d27 | 299 | } else if (cmd == XFS_IOC_FSBULKSTAT_32) { |
2810bd68 DW |
300 | breq.startino = lastino ? lastino + 1 : 0; |
301 | error = xfs_bulkstat(&breq, bs_one_func); | |
302 | lastino = breq.startino - 1; | |
303 | } else { | |
2451337d | 304 | error = -EINVAL; |
2810bd68 | 305 | } |
faa63e95 | 306 | if (error) |
2451337d | 307 | return error; |
faa63e95 | 308 | |
f16fe3ec | 309 | if (bulkreq.lastip != NULL && |
2810bd68 | 310 | copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t))) |
f16fe3ec | 311 | return -EFAULT; |
faa63e95 | 312 | |
f16fe3ec | 313 | if (bulkreq.ocount != NULL && |
2810bd68 | 314 | copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32))) |
f16fe3ec | 315 | return -EFAULT; |
faa63e95 MM |
316 | |
317 | return 0; | |
1da177e4 | 318 | } |
faa63e95 | 319 | |
d5547f9f | 320 | STATIC int |
321 | xfs_compat_handlereq_copyin( | |
322 | xfs_fsop_handlereq_t *hreq, | |
323 | compat_xfs_fsop_handlereq_t __user *arg32) | |
1fa503df | 324 | { |
d5547f9f | 325 | compat_xfs_fsop_handlereq_t hreq32; |
326 | ||
327 | if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t))) | |
b474c7ae | 328 | return -EFAULT; |
d5547f9f | 329 | |
330 | hreq->fd = hreq32.fd; | |
331 | hreq->path = compat_ptr(hreq32.path); | |
332 | hreq->oflags = hreq32.oflags; | |
333 | hreq->ihandle = compat_ptr(hreq32.ihandle); | |
334 | hreq->ihandlen = hreq32.ihandlen; | |
335 | hreq->ohandle = compat_ptr(hreq32.ohandle); | |
336 | hreq->ohandlen = compat_ptr(hreq32.ohandlen); | |
337 | ||
338 | return 0; | |
1fa503df MM |
339 | } |
340 | ||
ab596ad8 CH |
341 | STATIC struct dentry * |
342 | xfs_compat_handlereq_to_dentry( | |
343 | struct file *parfilp, | |
344 | compat_xfs_fsop_handlereq_t *hreq) | |
ebeecd2b | 345 | { |
ab596ad8 CH |
346 | return xfs_handle_to_dentry(parfilp, |
347 | compat_ptr(hreq->ihandle), hreq->ihandlen); | |
ebeecd2b | 348 | } |
349 | ||
350 | STATIC int | |
351 | xfs_compat_attrlist_by_handle( | |
ab596ad8 CH |
352 | struct file *parfilp, |
353 | void __user *arg) | |
ebeecd2b | 354 | { |
355 | int error; | |
356 | attrlist_cursor_kern_t *cursor; | |
c456d644 | 357 | compat_xfs_fsop_attrlist_handlereq_t __user *p = arg; |
ebeecd2b | 358 | compat_xfs_fsop_attrlist_handlereq_t al_hreq; |
ab596ad8 | 359 | struct dentry *dentry; |
ebeecd2b | 360 | char *kbuf; |
361 | ||
362 | if (!capable(CAP_SYS_ADMIN)) | |
b474c7ae | 363 | return -EPERM; |
ebeecd2b | 364 | if (copy_from_user(&al_hreq, arg, |
365 | sizeof(compat_xfs_fsop_attrlist_handlereq_t))) | |
b474c7ae | 366 | return -EFAULT; |
31978b5c | 367 | if (al_hreq.buflen < sizeof(struct attrlist) || |
4e247614 | 368 | al_hreq.buflen > XFS_XATTR_LIST_MAX) |
b474c7ae | 369 | return -EINVAL; |
ebeecd2b | 370 | |
371 | /* | |
372 | * Reject flags, only allow namespaces. | |
373 | */ | |
374 | if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE)) | |
b474c7ae | 375 | return -EINVAL; |
ebeecd2b | 376 | |
ab596ad8 CH |
377 | dentry = xfs_compat_handlereq_to_dentry(parfilp, &al_hreq.hreq); |
378 | if (IS_ERR(dentry)) | |
379 | return PTR_ERR(dentry); | |
ebeecd2b | 380 | |
ab596ad8 | 381 | error = -ENOMEM; |
fdd3ccee DC |
382 | kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP); |
383 | if (!kbuf) | |
384 | goto out_dput; | |
ebeecd2b | 385 | |
386 | cursor = (attrlist_cursor_kern_t *)&al_hreq.pos; | |
2b0143b5 | 387 | error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen, |
ebeecd2b | 388 | al_hreq.flags, cursor); |
389 | if (error) | |
390 | goto out_kfree; | |
391 | ||
c456d644 NB |
392 | if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) { |
393 | error = -EFAULT; | |
394 | goto out_kfree; | |
395 | } | |
396 | ||
ebeecd2b | 397 | if (copy_to_user(compat_ptr(al_hreq.buffer), kbuf, al_hreq.buflen)) |
398 | error = -EFAULT; | |
399 | ||
fdd3ccee DC |
400 | out_kfree: |
401 | kmem_free(kbuf); | |
402 | out_dput: | |
ab596ad8 CH |
403 | dput(dentry); |
404 | return error; | |
ebeecd2b | 405 | } |
406 | ||
28750975 | 407 | STATIC int |
408 | xfs_compat_attrmulti_by_handle( | |
ab596ad8 CH |
409 | struct file *parfilp, |
410 | void __user *arg) | |
28750975 | 411 | { |
412 | int error; | |
413 | compat_xfs_attr_multiop_t *ops; | |
414 | compat_xfs_fsop_attrmulti_handlereq_t am_hreq; | |
ab596ad8 | 415 | struct dentry *dentry; |
28750975 | 416 | unsigned int i, size; |
a9273ca5 | 417 | unsigned char *attr_name; |
28750975 | 418 | |
419 | if (!capable(CAP_SYS_ADMIN)) | |
b474c7ae | 420 | return -EPERM; |
28750975 | 421 | if (copy_from_user(&am_hreq, arg, |
422 | sizeof(compat_xfs_fsop_attrmulti_handlereq_t))) | |
b474c7ae | 423 | return -EFAULT; |
28750975 | 424 | |
fda168c2 ZW |
425 | /* overflow check */ |
426 | if (am_hreq.opcount >= INT_MAX / sizeof(compat_xfs_attr_multiop_t)) | |
427 | return -E2BIG; | |
428 | ||
ab596ad8 CH |
429 | dentry = xfs_compat_handlereq_to_dentry(parfilp, &am_hreq.hreq); |
430 | if (IS_ERR(dentry)) | |
431 | return PTR_ERR(dentry); | |
28750975 | 432 | |
2451337d | 433 | error = -E2BIG; |
28750975 | 434 | size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t); |
435 | if (!size || size > 16 * PAGE_SIZE) | |
ab596ad8 | 436 | goto out_dput; |
28750975 | 437 | |
0e639bde LZ |
438 | ops = memdup_user(compat_ptr(am_hreq.ops), size); |
439 | if (IS_ERR(ops)) { | |
4d949021 | 440 | error = PTR_ERR(ops); |
ab596ad8 | 441 | goto out_dput; |
0e639bde | 442 | } |
28750975 | 443 | |
2451337d | 444 | error = -ENOMEM; |
28750975 | 445 | attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL); |
446 | if (!attr_name) | |
447 | goto out_kfree_ops; | |
448 | ||
28750975 | 449 | error = 0; |
450 | for (i = 0; i < am_hreq.opcount; i++) { | |
a9273ca5 | 451 | ops[i].am_error = strncpy_from_user((char *)attr_name, |
28750975 | 452 | compat_ptr(ops[i].am_attrname), |
453 | MAXNAMELEN); | |
454 | if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN) | |
2451337d | 455 | error = -ERANGE; |
28750975 | 456 | if (ops[i].am_error < 0) |
457 | break; | |
458 | ||
459 | switch (ops[i].am_opcode) { | |
460 | case ATTR_OP_GET: | |
ab596ad8 | 461 | ops[i].am_error = xfs_attrmulti_attr_get( |
2b0143b5 | 462 | d_inode(dentry), attr_name, |
28750975 | 463 | compat_ptr(ops[i].am_attrvalue), |
464 | &ops[i].am_length, ops[i].am_flags); | |
465 | break; | |
466 | case ATTR_OP_SET: | |
a561be71 | 467 | ops[i].am_error = mnt_want_write_file(parfilp); |
62e194ec CH |
468 | if (ops[i].am_error) |
469 | break; | |
ab596ad8 | 470 | ops[i].am_error = xfs_attrmulti_attr_set( |
2b0143b5 | 471 | d_inode(dentry), attr_name, |
28750975 | 472 | compat_ptr(ops[i].am_attrvalue), |
473 | ops[i].am_length, ops[i].am_flags); | |
2a79f17e | 474 | mnt_drop_write_file(parfilp); |
28750975 | 475 | break; |
476 | case ATTR_OP_REMOVE: | |
a561be71 | 477 | ops[i].am_error = mnt_want_write_file(parfilp); |
62e194ec CH |
478 | if (ops[i].am_error) |
479 | break; | |
ab596ad8 | 480 | ops[i].am_error = xfs_attrmulti_attr_remove( |
2b0143b5 | 481 | d_inode(dentry), attr_name, |
ab596ad8 | 482 | ops[i].am_flags); |
2a79f17e | 483 | mnt_drop_write_file(parfilp); |
28750975 | 484 | break; |
485 | default: | |
2451337d | 486 | ops[i].am_error = -EINVAL; |
28750975 | 487 | } |
488 | } | |
489 | ||
490 | if (copy_to_user(compat_ptr(am_hreq.ops), ops, size)) | |
2451337d | 491 | error = -EFAULT; |
28750975 | 492 | |
493 | kfree(attr_name); | |
494 | out_kfree_ops: | |
495 | kfree(ops); | |
ab596ad8 CH |
496 | out_dput: |
497 | dput(dentry); | |
2451337d | 498 | return error; |
28750975 | 499 | } |
500 | ||
710d62aa | 501 | STATIC int |
502 | xfs_compat_fssetdm_by_handle( | |
ab596ad8 CH |
503 | struct file *parfilp, |
504 | void __user *arg) | |
710d62aa | 505 | { |
506 | int error; | |
507 | struct fsdmidata fsd; | |
508 | compat_xfs_fsop_setdm_handlereq_t dmhreq; | |
ab596ad8 | 509 | struct dentry *dentry; |
710d62aa | 510 | |
511 | if (!capable(CAP_MKNOD)) | |
b474c7ae | 512 | return -EPERM; |
710d62aa | 513 | if (copy_from_user(&dmhreq, arg, |
514 | sizeof(compat_xfs_fsop_setdm_handlereq_t))) | |
b474c7ae | 515 | return -EFAULT; |
710d62aa | 516 | |
ab596ad8 CH |
517 | dentry = xfs_compat_handlereq_to_dentry(parfilp, &dmhreq.hreq); |
518 | if (IS_ERR(dentry)) | |
519 | return PTR_ERR(dentry); | |
710d62aa | 520 | |
2b0143b5 | 521 | if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) { |
b474c7ae | 522 | error = -EPERM; |
710d62aa | 523 | goto out; |
524 | } | |
525 | ||
526 | if (copy_from_user(&fsd, compat_ptr(dmhreq.data), sizeof(fsd))) { | |
b474c7ae | 527 | error = -EFAULT; |
710d62aa | 528 | goto out; |
529 | } | |
530 | ||
2b0143b5 | 531 | error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask, |
710d62aa | 532 | fsd.fsd_dmstate); |
533 | ||
534 | out: | |
ab596ad8 | 535 | dput(dentry); |
710d62aa | 536 | return error; |
537 | } | |
538 | ||
4d4be482 CH |
539 | long |
540 | xfs_file_compat_ioctl( | |
541 | struct file *filp, | |
542 | unsigned cmd, | |
543 | unsigned long p) | |
1da177e4 | 544 | { |
496ad9aa | 545 | struct inode *inode = file_inode(filp); |
4d4be482 CH |
546 | struct xfs_inode *ip = XFS_I(inode); |
547 | struct xfs_mount *mp = ip->i_mount; | |
548 | void __user *arg = (void __user *)p; | |
4d4be482 CH |
549 | int error; |
550 | ||
cca28fb8 | 551 | trace_xfs_file_compat_ioctl(ip); |
1da177e4 LT |
552 | |
553 | switch (cmd) { | |
e5d412f1 | 554 | /* No size or alignment issues on any arch */ |
1da177e4 | 555 | case XFS_IOC_DIOINFO: |
1b6d968d | 556 | case XFS_IOC_FSGEOMETRY_V4: |
1da177e4 | 557 | case XFS_IOC_FSGEOMETRY: |
7cd5006b | 558 | case XFS_IOC_AG_GEOMETRY: |
1da177e4 LT |
559 | case XFS_IOC_FSGETXATTR: |
560 | case XFS_IOC_FSSETXATTR: | |
561 | case XFS_IOC_FSGETXATTRA: | |
562 | case XFS_IOC_FSSETDM: | |
563 | case XFS_IOC_GETBMAP: | |
564 | case XFS_IOC_GETBMAPA: | |
565 | case XFS_IOC_GETBMAPX: | |
1da177e4 LT |
566 | case XFS_IOC_FSCOUNTS: |
567 | case XFS_IOC_SET_RESBLKS: | |
568 | case XFS_IOC_GET_RESBLKS: | |
1da177e4 | 569 | case XFS_IOC_FSGROWFSLOG: |
1da177e4 LT |
570 | case XFS_IOC_GOINGDOWN: |
571 | case XFS_IOC_ERROR_INJECTION: | |
572 | case XFS_IOC_ERROR_CLEARALL: | |
e89c0413 | 573 | case FS_IOC_GETFSMAP: |
36fd6e86 | 574 | case XFS_IOC_SCRUB_METADATA: |
4d4be482 | 575 | return xfs_file_ioctl(filp, cmd, p); |
a9d25bde NB |
576 | #if !defined(BROKEN_X86_ALIGNMENT) || defined(CONFIG_X86_X32) |
577 | /* | |
578 | * These are handled fine if no alignment issues. To support x32 | |
579 | * which uses native 64-bit alignment we must emit these cases in | |
580 | * addition to the ia-32 compat set below. | |
581 | */ | |
e5d412f1 | 582 | case XFS_IOC_ALLOCSP: |
583 | case XFS_IOC_FREESP: | |
584 | case XFS_IOC_RESVSP: | |
585 | case XFS_IOC_UNRESVSP: | |
586 | case XFS_IOC_ALLOCSP64: | |
587 | case XFS_IOC_FREESP64: | |
588 | case XFS_IOC_RESVSP64: | |
589 | case XFS_IOC_UNRESVSP64: | |
590 | case XFS_IOC_FSGEOMETRY_V1: | |
591 | case XFS_IOC_FSGROWFSDATA: | |
592 | case XFS_IOC_FSGROWFSRT: | |
44722352 | 593 | case XFS_IOC_ZERO_RANGE: |
a9d25bde NB |
594 | #ifdef CONFIG_X86_X32 |
595 | /* | |
596 | * x32 special: this gets a different cmd number from the ia-32 compat | |
597 | * case below; the associated data will match native 64-bit alignment. | |
598 | */ | |
599 | case XFS_IOC_SWAPEXT: | |
600 | #endif | |
4d4be482 | 601 | return xfs_file_ioctl(filp, cmd, p); |
a9d25bde NB |
602 | #endif |
603 | #if defined(BROKEN_X86_ALIGNMENT) | |
526c420c ES |
604 | case XFS_IOC_ALLOCSP_32: |
605 | case XFS_IOC_FREESP_32: | |
606 | case XFS_IOC_ALLOCSP64_32: | |
607 | case XFS_IOC_FREESP64_32: | |
608 | case XFS_IOC_RESVSP_32: | |
609 | case XFS_IOC_UNRESVSP_32: | |
610 | case XFS_IOC_RESVSP64_32: | |
8c1fdd0b CH |
611 | case XFS_IOC_UNRESVSP64_32: |
612 | case XFS_IOC_ZERO_RANGE_32: { | |
d5547f9f | 613 | struct xfs_flock64 bf; |
614 | ||
615 | if (xfs_compat_flock64_copyin(&bf, arg)) | |
b474c7ae | 616 | return -EFAULT; |
526c420c | 617 | cmd = _NATIVE_IOC(cmd, struct xfs_flock64); |
8f3e2058 | 618 | return xfs_ioc_space(filp, cmd, &bf); |
d5547f9f | 619 | } |
547e00c3 | 620 | case XFS_IOC_FSGEOMETRY_V1_32: |
d5547f9f | 621 | return xfs_compat_ioc_fsgeometry_v1(mp, arg); |
471d5910 | 622 | case XFS_IOC_FSGROWFSDATA_32: { |
623 | struct xfs_growfs_data in; | |
624 | ||
625 | if (xfs_compat_growfs_data_copyin(&in, arg)) | |
b474c7ae | 626 | return -EFAULT; |
d9457dc0 JK |
627 | error = mnt_want_write_file(filp); |
628 | if (error) | |
629 | return error; | |
471d5910 | 630 | error = xfs_growfs_data(mp, &in); |
d9457dc0 | 631 | mnt_drop_write_file(filp); |
2451337d | 632 | return error; |
471d5910 | 633 | } |
634 | case XFS_IOC_FSGROWFSRT_32: { | |
635 | struct xfs_growfs_rt in; | |
636 | ||
637 | if (xfs_compat_growfs_rt_copyin(&in, arg)) | |
b474c7ae | 638 | return -EFAULT; |
d9457dc0 JK |
639 | error = mnt_want_write_file(filp); |
640 | if (error) | |
641 | return error; | |
471d5910 | 642 | error = xfs_growfs_rt(mp, &in); |
d9457dc0 | 643 | mnt_drop_write_file(filp); |
2451337d | 644 | return error; |
471d5910 | 645 | } |
1da177e4 | 646 | #endif |
e5d412f1 | 647 | /* long changes size, but xfs only copiese out 32 bits */ |
648 | case XFS_IOC_GETXFLAGS_32: | |
649 | case XFS_IOC_SETXFLAGS_32: | |
650 | case XFS_IOC_GETVERSION_32: | |
651 | cmd = _NATIVE_IOC(cmd, long); | |
4d4be482 | 652 | return xfs_file_ioctl(filp, cmd, p); |
3725867d | 653 | case XFS_IOC_SWAPEXT_32: { |
e5d412f1 | 654 | struct xfs_swapext sxp; |
655 | struct compat_xfs_swapext __user *sxu = arg; | |
656 | ||
657 | /* Bulk copy in up to the sx_stat field, then copy bstat */ | |
658 | if (copy_from_user(&sxp, sxu, | |
659 | offsetof(struct xfs_swapext, sx_stat)) || | |
660 | xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat)) | |
b474c7ae | 661 | return -EFAULT; |
d9457dc0 JK |
662 | error = mnt_want_write_file(filp); |
663 | if (error) | |
664 | return error; | |
a133d952 | 665 | error = xfs_ioc_swapext(&sxp); |
d9457dc0 | 666 | mnt_drop_write_file(filp); |
2451337d | 667 | return error; |
e5d412f1 | 668 | } |
faa63e95 MM |
669 | case XFS_IOC_FSBULKSTAT_32: |
670 | case XFS_IOC_FSBULKSTAT_SINGLE_32: | |
671 | case XFS_IOC_FSINUMBERS_32: | |
2ee4fa5c | 672 | return xfs_compat_ioc_bulkstat(mp, cmd, arg); |
1fa503df MM |
673 | case XFS_IOC_FD_TO_HANDLE_32: |
674 | case XFS_IOC_PATH_TO_HANDLE_32: | |
d5547f9f | 675 | case XFS_IOC_PATH_TO_FSHANDLE_32: { |
676 | struct xfs_fsop_handlereq hreq; | |
677 | ||
678 | if (xfs_compat_handlereq_copyin(&hreq, arg)) | |
b474c7ae | 679 | return -EFAULT; |
1fa503df | 680 | cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq); |
d5547f9f | 681 | return xfs_find_handle(cmd, &hreq); |
682 | } | |
683 | case XFS_IOC_OPEN_BY_HANDLE_32: { | |
684 | struct xfs_fsop_handlereq hreq; | |
685 | ||
686 | if (xfs_compat_handlereq_copyin(&hreq, arg)) | |
b474c7ae | 687 | return -EFAULT; |
ab596ad8 | 688 | return xfs_open_by_handle(filp, &hreq); |
d5547f9f | 689 | } |
690 | case XFS_IOC_READLINK_BY_HANDLE_32: { | |
691 | struct xfs_fsop_handlereq hreq; | |
692 | ||
693 | if (xfs_compat_handlereq_copyin(&hreq, arg)) | |
b474c7ae | 694 | return -EFAULT; |
ab596ad8 | 695 | return xfs_readlink_by_handle(filp, &hreq); |
d5547f9f | 696 | } |
ebeecd2b | 697 | case XFS_IOC_ATTRLIST_BY_HANDLE_32: |
ab596ad8 | 698 | return xfs_compat_attrlist_by_handle(filp, arg); |
28750975 | 699 | case XFS_IOC_ATTRMULTI_BY_HANDLE_32: |
ab596ad8 | 700 | return xfs_compat_attrmulti_by_handle(filp, arg); |
710d62aa | 701 | case XFS_IOC_FSSETDM_BY_HANDLE_32: |
ab596ad8 | 702 | return xfs_compat_fssetdm_by_handle(filp, arg); |
1da177e4 | 703 | default: |
b474c7ae | 704 | return -ENOIOCTLCMD; |
1da177e4 | 705 | } |
1da177e4 | 706 | } |