Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/ioctl.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992 Linus Torvalds | |
6 | */ | |
7 | ||
1da177e4 LT |
8 | #include <linux/syscalls.h> |
9 | #include <linux/mm.h> | |
16f7e0fe | 10 | #include <linux/capability.h> |
2952db0f | 11 | #include <linux/compat.h> |
1da177e4 LT |
12 | #include <linux/file.h> |
13 | #include <linux/fs.h> | |
14 | #include <linux/security.h> | |
630d9c47 | 15 | #include <linux/export.h> |
c9845ff1 | 16 | #include <linux/uaccess.h> |
68c9d702 JB |
17 | #include <linux/writeback.h> |
18 | #include <linux/buffer_head.h> | |
3e63cbb1 | 19 | #include <linux/falloc.h> |
f361bf4a IM |
20 | #include <linux/sched/signal.h> |
21 | ||
66cf191f | 22 | #include "internal.h" |
1da177e4 | 23 | |
1da177e4 LT |
24 | #include <asm/ioctls.h> |
25 | ||
c4b929b8 MF |
26 | /* So that the fiemap access checks can't overflow on 32 bit machines. */ |
27 | #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent)) | |
28 | ||
deb21db7 EZ |
29 | /** |
30 | * vfs_ioctl - call filesystem specific ioctl methods | |
f6a4c8bd CH |
31 | * @filp: open file to invoke ioctl method on |
32 | * @cmd: ioctl command to execute | |
33 | * @arg: command-specific argument for ioctl | |
deb21db7 EZ |
34 | * |
35 | * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise | |
deb21db7 EZ |
36 | * returns -ENOTTY. |
37 | * | |
38 | * Returns 0 on success, -errno on error. | |
39 | */ | |
66cf191f | 40 | long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
1da177e4 LT |
41 | { |
42 | int error = -ENOTTY; | |
43 | ||
72c2d531 | 44 | if (!filp->f_op->unlocked_ioctl) |
1da177e4 LT |
45 | goto out; |
46 | ||
b19dd42f AB |
47 | error = filp->f_op->unlocked_ioctl(filp, cmd, arg); |
48 | if (error == -ENOIOCTLCMD) | |
07d106d0 | 49 | error = -ENOTTY; |
1da177e4 LT |
50 | out: |
51 | return error; | |
52 | } | |
9df6702a | 53 | EXPORT_SYMBOL(vfs_ioctl); |
1da177e4 | 54 | |
aa81a7c7 EZ |
55 | static int ioctl_fibmap(struct file *filp, int __user *p) |
56 | { | |
57 | struct address_space *mapping = filp->f_mapping; | |
58 | int res, block; | |
59 | ||
60 | /* do we support this mess? */ | |
61 | if (!mapping->a_ops->bmap) | |
62 | return -EINVAL; | |
63 | if (!capable(CAP_SYS_RAWIO)) | |
64 | return -EPERM; | |
65 | res = get_user(block, p); | |
66 | if (res) | |
67 | return res; | |
aa81a7c7 | 68 | res = mapping->a_ops->bmap(mapping, block); |
aa81a7c7 EZ |
69 | return put_user(res, p); |
70 | } | |
71 | ||
c4b929b8 MF |
72 | /** |
73 | * fiemap_fill_next_extent - Fiemap helper function | |
74 | * @fieinfo: Fiemap context passed into ->fiemap | |
75 | * @logical: Extent logical start offset, in bytes | |
76 | * @phys: Extent physical start offset, in bytes | |
77 | * @len: Extent length, in bytes | |
78 | * @flags: FIEMAP_EXTENT flags that describe this extent | |
79 | * | |
80 | * Called from file system ->fiemap callback. Will populate extent | |
81 | * info as passed in via arguments and copy to user memory. On | |
82 | * success, extent count on fieinfo is incremented. | |
83 | * | |
84 | * Returns 0 on success, -errno on error, 1 if this was the last | |
85 | * extent that will fit in user array. | |
86 | */ | |
87 | #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC) | |
88 | #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED) | |
89 | #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE) | |
90 | int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical, | |
91 | u64 phys, u64 len, u32 flags) | |
92 | { | |
93 | struct fiemap_extent extent; | |
ecf5632d | 94 | struct fiemap_extent __user *dest = fieinfo->fi_extents_start; |
c4b929b8 MF |
95 | |
96 | /* only count the extents */ | |
97 | if (fieinfo->fi_extents_max == 0) { | |
98 | fieinfo->fi_extents_mapped++; | |
99 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; | |
100 | } | |
101 | ||
102 | if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max) | |
103 | return 1; | |
104 | ||
105 | if (flags & SET_UNKNOWN_FLAGS) | |
106 | flags |= FIEMAP_EXTENT_UNKNOWN; | |
107 | if (flags & SET_NO_UNMOUNTED_IO_FLAGS) | |
108 | flags |= FIEMAP_EXTENT_ENCODED; | |
109 | if (flags & SET_NOT_ALIGNED_FLAGS) | |
110 | flags |= FIEMAP_EXTENT_NOT_ALIGNED; | |
111 | ||
112 | memset(&extent, 0, sizeof(extent)); | |
113 | extent.fe_logical = logical; | |
114 | extent.fe_physical = phys; | |
115 | extent.fe_length = len; | |
116 | extent.fe_flags = flags; | |
117 | ||
118 | dest += fieinfo->fi_extents_mapped; | |
119 | if (copy_to_user(dest, &extent, sizeof(extent))) | |
120 | return -EFAULT; | |
121 | ||
122 | fieinfo->fi_extents_mapped++; | |
123 | if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max) | |
124 | return 1; | |
125 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; | |
126 | } | |
127 | EXPORT_SYMBOL(fiemap_fill_next_extent); | |
128 | ||
129 | /** | |
130 | * fiemap_check_flags - check validity of requested flags for fiemap | |
131 | * @fieinfo: Fiemap context passed into ->fiemap | |
132 | * @fs_flags: Set of fiemap flags that the file system understands | |
133 | * | |
134 | * Called from file system ->fiemap callback. This will compute the | |
135 | * intersection of valid fiemap flags and those that the fs supports. That | |
136 | * value is then compared against the user supplied flags. In case of bad user | |
137 | * flags, the invalid values will be written into the fieinfo structure, and | |
138 | * -EBADR is returned, which tells ioctl_fiemap() to return those values to | |
139 | * userspace. For this reason, a return code of -EBADR should be preserved. | |
140 | * | |
141 | * Returns 0 on success, -EBADR on bad flags. | |
142 | */ | |
143 | int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags) | |
144 | { | |
145 | u32 incompat_flags; | |
146 | ||
147 | incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags); | |
148 | if (incompat_flags) { | |
149 | fieinfo->fi_flags = incompat_flags; | |
150 | return -EBADR; | |
151 | } | |
152 | return 0; | |
153 | } | |
154 | EXPORT_SYMBOL(fiemap_check_flags); | |
155 | ||
156 | static int fiemap_check_ranges(struct super_block *sb, | |
157 | u64 start, u64 len, u64 *new_len) | |
158 | { | |
5aa98b70 JL |
159 | u64 maxbytes = (u64) sb->s_maxbytes; |
160 | ||
c4b929b8 MF |
161 | *new_len = len; |
162 | ||
163 | if (len == 0) | |
164 | return -EINVAL; | |
165 | ||
5aa98b70 | 166 | if (start > maxbytes) |
c4b929b8 MF |
167 | return -EFBIG; |
168 | ||
169 | /* | |
170 | * Shrink request scope to what the fs can actually handle. | |
171 | */ | |
5aa98b70 JL |
172 | if (len > maxbytes || (maxbytes - len) < start) |
173 | *new_len = maxbytes - start; | |
c4b929b8 MF |
174 | |
175 | return 0; | |
176 | } | |
177 | ||
34d3d0e6 | 178 | static int ioctl_fiemap(struct file *filp, struct fiemap __user *ufiemap) |
c4b929b8 MF |
179 | { |
180 | struct fiemap fiemap; | |
181 | struct fiemap_extent_info fieinfo = { 0, }; | |
496ad9aa | 182 | struct inode *inode = file_inode(filp); |
c4b929b8 MF |
183 | struct super_block *sb = inode->i_sb; |
184 | u64 len; | |
185 | int error; | |
186 | ||
187 | if (!inode->i_op->fiemap) | |
188 | return -EOPNOTSUPP; | |
189 | ||
ecf5632d | 190 | if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap))) |
c4b929b8 MF |
191 | return -EFAULT; |
192 | ||
193 | if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS) | |
194 | return -EINVAL; | |
195 | ||
196 | error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length, | |
197 | &len); | |
198 | if (error) | |
199 | return error; | |
200 | ||
201 | fieinfo.fi_flags = fiemap.fm_flags; | |
202 | fieinfo.fi_extents_max = fiemap.fm_extent_count; | |
ecf5632d | 203 | fieinfo.fi_extents_start = ufiemap->fm_extents; |
c4b929b8 MF |
204 | |
205 | if (fiemap.fm_extent_count != 0 && | |
96d4f267 | 206 | !access_ok(fieinfo.fi_extents_start, |
c4b929b8 MF |
207 | fieinfo.fi_extents_max * sizeof(struct fiemap_extent))) |
208 | return -EFAULT; | |
209 | ||
210 | if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC) | |
211 | filemap_write_and_wait(inode->i_mapping); | |
212 | ||
213 | error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len); | |
214 | fiemap.fm_flags = fieinfo.fi_flags; | |
215 | fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped; | |
ecf5632d | 216 | if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap))) |
c4b929b8 MF |
217 | error = -EFAULT; |
218 | ||
219 | return error; | |
220 | } | |
221 | ||
04b38d60 CH |
222 | static long ioctl_file_clone(struct file *dst_file, unsigned long srcfd, |
223 | u64 off, u64 olen, u64 destoff) | |
224 | { | |
225 | struct fd src_file = fdget(srcfd); | |
42ec3d4c | 226 | loff_t cloned; |
04b38d60 CH |
227 | int ret; |
228 | ||
229 | if (!src_file.file) | |
230 | return -EBADF; | |
913b86e9 AG |
231 | ret = -EXDEV; |
232 | if (src_file.file->f_path.mnt != dst_file->f_path.mnt) | |
233 | goto fdput; | |
42ec3d4c | 234 | cloned = vfs_clone_file_range(src_file.file, off, dst_file, destoff, |
452ce659 | 235 | olen, 0); |
42ec3d4c DW |
236 | if (cloned < 0) |
237 | ret = cloned; | |
238 | else if (olen && cloned != olen) | |
239 | ret = -EINVAL; | |
240 | else | |
241 | ret = 0; | |
913b86e9 | 242 | fdput: |
04b38d60 CH |
243 | fdput(src_file); |
244 | return ret; | |
245 | } | |
246 | ||
34d3d0e6 AV |
247 | static long ioctl_file_clone_range(struct file *file, |
248 | struct file_clone_range __user *argp) | |
04b38d60 CH |
249 | { |
250 | struct file_clone_range args; | |
251 | ||
252 | if (copy_from_user(&args, argp, sizeof(args))) | |
253 | return -EFAULT; | |
254 | return ioctl_file_clone(file, args.src_fd, args.src_offset, | |
255 | args.src_length, args.dest_offset); | |
256 | } | |
257 | ||
06270d5d AB |
258 | #ifdef CONFIG_BLOCK |
259 | ||
3a3076f4 JB |
260 | static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) |
261 | { | |
262 | return (offset >> inode->i_blkbits); | |
263 | } | |
264 | ||
265 | static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) | |
266 | { | |
267 | return (blk << inode->i_blkbits); | |
268 | } | |
68c9d702 | 269 | |
e9079cce SW |
270 | /** |
271 | * __generic_block_fiemap - FIEMAP for block based inodes (no locking) | |
3a3076f4 JB |
272 | * @inode: the inode to map |
273 | * @fieinfo: the fiemap info struct that will be passed back to userspace | |
274 | * @start: where to start mapping in the inode | |
275 | * @len: how much space to map | |
276 | * @get_block: the fs's get_block function | |
68c9d702 JB |
277 | * |
278 | * This does FIEMAP for block based inodes. Basically it will just loop | |
279 | * through get_block until we hit the number of extents we want to map, or we | |
280 | * go past the end of the file and hit a hole. | |
281 | * | |
282 | * If it is possible to have data blocks beyond a hole past @inode->i_size, then | |
283 | * please do not use this function, it will stop at the first unmapped block | |
e9079cce SW |
284 | * beyond i_size. |
285 | * | |
286 | * If you use this function directly, you need to do your own locking. Use | |
287 | * generic_block_fiemap if you want the locking done for you. | |
68c9d702 | 288 | */ |
e9079cce SW |
289 | |
290 | int __generic_block_fiemap(struct inode *inode, | |
3a3076f4 JB |
291 | struct fiemap_extent_info *fieinfo, loff_t start, |
292 | loff_t len, get_block_t *get_block) | |
68c9d702 | 293 | { |
3a3076f4 JB |
294 | struct buffer_head map_bh; |
295 | sector_t start_blk, last_blk; | |
296 | loff_t isize = i_size_read(inode); | |
68c9d702 JB |
297 | u64 logical = 0, phys = 0, size = 0; |
298 | u32 flags = FIEMAP_EXTENT_MERGED; | |
3a3076f4 JB |
299 | bool past_eof = false, whole_file = false; |
300 | int ret = 0; | |
68c9d702 | 301 | |
3a3076f4 JB |
302 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); |
303 | if (ret) | |
68c9d702 JB |
304 | return ret; |
305 | ||
3a3076f4 JB |
306 | /* |
307 | * Either the i_mutex or other appropriate locking needs to be held | |
308 | * since we expect isize to not change at all through the duration of | |
309 | * this call. | |
310 | */ | |
311 | if (len >= isize) { | |
312 | whole_file = true; | |
313 | len = isize; | |
314 | } | |
df3935ff | 315 | |
d54cdc8c JB |
316 | /* |
317 | * Some filesystems can't deal with being asked to map less than | |
318 | * blocksize, so make sure our len is at least block length. | |
319 | */ | |
320 | if (logical_to_blk(inode, len) == 0) | |
321 | len = blk_to_logical(inode, 1); | |
322 | ||
3a3076f4 JB |
323 | start_blk = logical_to_blk(inode, start); |
324 | last_blk = logical_to_blk(inode, start + len - 1); | |
68c9d702 JB |
325 | |
326 | do { | |
327 | /* | |
328 | * we set b_size to the total size we want so it will map as | |
329 | * many contiguous blocks as possible at once | |
330 | */ | |
3a3076f4 JB |
331 | memset(&map_bh, 0, sizeof(struct buffer_head)); |
332 | map_bh.b_size = len; | |
68c9d702 | 333 | |
3a3076f4 | 334 | ret = get_block(inode, start_blk, &map_bh, 0); |
68c9d702 JB |
335 | if (ret) |
336 | break; | |
337 | ||
338 | /* HOLE */ | |
3a3076f4 | 339 | if (!buffer_mapped(&map_bh)) { |
df3935ff JB |
340 | start_blk++; |
341 | ||
342 | /* | |
3a3076f4 | 343 | * We want to handle the case where there is an |
df3935ff JB |
344 | * allocated block at the front of the file, and then |
345 | * nothing but holes up to the end of the file properly, | |
346 | * to make sure that extent at the front gets properly | |
347 | * marked with FIEMAP_EXTENT_LAST | |
348 | */ | |
349 | if (!past_eof && | |
3a3076f4 | 350 | blk_to_logical(inode, start_blk) >= isize) |
df3935ff JB |
351 | past_eof = 1; |
352 | ||
68c9d702 | 353 | /* |
3a3076f4 | 354 | * First hole after going past the EOF, this is our |
68c9d702 JB |
355 | * last extent |
356 | */ | |
df3935ff | 357 | if (past_eof && size) { |
68c9d702 JB |
358 | flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST; |
359 | ret = fiemap_fill_next_extent(fieinfo, logical, | |
360 | phys, size, | |
361 | flags); | |
3a3076f4 JB |
362 | } else if (size) { |
363 | ret = fiemap_fill_next_extent(fieinfo, logical, | |
364 | phys, size, flags); | |
365 | size = 0; | |
68c9d702 JB |
366 | } |
367 | ||
68c9d702 | 368 | /* if we have holes up to/past EOF then we're done */ |
3a3076f4 | 369 | if (start_blk > last_blk || past_eof || ret) |
68c9d702 | 370 | break; |
68c9d702 | 371 | } else { |
df3935ff | 372 | /* |
3a3076f4 | 373 | * We have gone over the length of what we wanted to |
df3935ff JB |
374 | * map, and it wasn't the entire file, so add the extent |
375 | * we got last time and exit. | |
376 | * | |
377 | * This is for the case where say we want to map all the | |
378 | * way up to the second to the last block in a file, but | |
379 | * the last block is a hole, making the second to last | |
380 | * block FIEMAP_EXTENT_LAST. In this case we want to | |
381 | * see if there is a hole after the second to last block | |
382 | * so we can mark it properly. If we found data after | |
383 | * we exceeded the length we were requesting, then we | |
384 | * are good to go, just add the extent to the fieinfo | |
385 | * and break | |
386 | */ | |
3a3076f4 | 387 | if (start_blk > last_blk && !whole_file) { |
df3935ff JB |
388 | ret = fiemap_fill_next_extent(fieinfo, logical, |
389 | phys, size, | |
390 | flags); | |
391 | break; | |
392 | } | |
393 | ||
394 | /* | |
395 | * if size != 0 then we know we already have an extent | |
396 | * to add, so add it. | |
397 | */ | |
398 | if (size) { | |
68c9d702 JB |
399 | ret = fiemap_fill_next_extent(fieinfo, logical, |
400 | phys, size, | |
401 | flags); | |
402 | if (ret) | |
403 | break; | |
404 | } | |
405 | ||
406 | logical = blk_to_logical(inode, start_blk); | |
3a3076f4 JB |
407 | phys = blk_to_logical(inode, map_bh.b_blocknr); |
408 | size = map_bh.b_size; | |
68c9d702 JB |
409 | flags = FIEMAP_EXTENT_MERGED; |
410 | ||
68c9d702 JB |
411 | start_blk += logical_to_blk(inode, size); |
412 | ||
413 | /* | |
df3935ff JB |
414 | * If we are past the EOF, then we need to make sure as |
415 | * soon as we find a hole that the last extent we found | |
416 | * is marked with FIEMAP_EXTENT_LAST | |
68c9d702 | 417 | */ |
3a3076f4 JB |
418 | if (!past_eof && logical + size >= isize) |
419 | past_eof = true; | |
68c9d702 JB |
420 | } |
421 | cond_resched(); | |
913e027c DM |
422 | if (fatal_signal_pending(current)) { |
423 | ret = -EINTR; | |
424 | break; | |
425 | } | |
426 | ||
68c9d702 JB |
427 | } while (1); |
428 | ||
3a3076f4 | 429 | /* If ret is 1 then we just hit the end of the extent array */ |
68c9d702 JB |
430 | if (ret == 1) |
431 | ret = 0; | |
432 | ||
433 | return ret; | |
434 | } | |
e9079cce SW |
435 | EXPORT_SYMBOL(__generic_block_fiemap); |
436 | ||
437 | /** | |
438 | * generic_block_fiemap - FIEMAP for block based inodes | |
439 | * @inode: The inode to map | |
440 | * @fieinfo: The mapping information | |
441 | * @start: The initial block to map | |
442 | * @len: The length of the extect to attempt to map | |
443 | * @get_block: The block mapping function for the fs | |
444 | * | |
445 | * Calls __generic_block_fiemap to map the inode, after taking | |
446 | * the inode's mutex lock. | |
447 | */ | |
448 | ||
449 | int generic_block_fiemap(struct inode *inode, | |
450 | struct fiemap_extent_info *fieinfo, u64 start, | |
451 | u64 len, get_block_t *get_block) | |
452 | { | |
453 | int ret; | |
5955102c | 454 | inode_lock(inode); |
e9079cce | 455 | ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block); |
5955102c | 456 | inode_unlock(inode); |
e9079cce SW |
457 | return ret; |
458 | } | |
68c9d702 JB |
459 | EXPORT_SYMBOL(generic_block_fiemap); |
460 | ||
06270d5d AB |
461 | #endif /* CONFIG_BLOCK */ |
462 | ||
3e63cbb1 AJ |
463 | /* |
464 | * This provides compatibility with legacy XFS pre-allocation ioctls | |
465 | * which predate the fallocate syscall. | |
466 | * | |
467 | * Only the l_start, l_len and l_whence fields of the 'struct space_resv' | |
468 | * are used here, rest are ignored. | |
469 | */ | |
77b90401 | 470 | static int ioctl_preallocate(struct file *filp, int mode, void __user *argp) |
3e63cbb1 | 471 | { |
496ad9aa | 472 | struct inode *inode = file_inode(filp); |
3e63cbb1 AJ |
473 | struct space_resv sr; |
474 | ||
475 | if (copy_from_user(&sr, argp, sizeof(sr))) | |
476 | return -EFAULT; | |
477 | ||
478 | switch (sr.l_whence) { | |
479 | case SEEK_SET: | |
480 | break; | |
481 | case SEEK_CUR: | |
482 | sr.l_start += filp->f_pos; | |
483 | break; | |
484 | case SEEK_END: | |
485 | sr.l_start += i_size_read(inode); | |
486 | break; | |
487 | default: | |
488 | return -EINVAL; | |
489 | } | |
490 | ||
837a6e7f CH |
491 | return vfs_fallocate(filp, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, |
492 | sr.l_len); | |
3e63cbb1 AJ |
493 | } |
494 | ||
011da44b AV |
495 | /* on ia32 l_start is on a 32-bit boundary */ |
496 | #if defined CONFIG_COMPAT && defined(CONFIG_X86_64) | |
497 | /* just account for different alignment */ | |
77b90401 AB |
498 | static int compat_ioctl_preallocate(struct file *file, int mode, |
499 | struct space_resv_32 __user *argp) | |
011da44b AV |
500 | { |
501 | struct inode *inode = file_inode(file); | |
502 | struct space_resv_32 sr; | |
503 | ||
504 | if (copy_from_user(&sr, argp, sizeof(sr))) | |
505 | return -EFAULT; | |
506 | ||
507 | switch (sr.l_whence) { | |
508 | case SEEK_SET: | |
509 | break; | |
510 | case SEEK_CUR: | |
511 | sr.l_start += file->f_pos; | |
512 | break; | |
513 | case SEEK_END: | |
514 | sr.l_start += i_size_read(inode); | |
515 | break; | |
516 | default: | |
517 | return -EINVAL; | |
518 | } | |
519 | ||
97eeb4d9 | 520 | return vfs_fallocate(file, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len); |
011da44b AV |
521 | } |
522 | #endif | |
523 | ||
77b90401 | 524 | static int file_ioctl(struct file *filp, unsigned int cmd, int __user *p) |
1da177e4 | 525 | { |
496ad9aa | 526 | struct inode *inode = file_inode(filp); |
1da177e4 LT |
527 | |
528 | switch (cmd) { | |
c9845ff1 | 529 | case FIBMAP: |
aa81a7c7 | 530 | return ioctl_fibmap(filp, p); |
c9845ff1 EZ |
531 | case FIONREAD: |
532 | return put_user(i_size_read(inode) - filp->f_pos, p); | |
3e63cbb1 AJ |
533 | case FS_IOC_RESVSP: |
534 | case FS_IOC_RESVSP64: | |
837a6e7f CH |
535 | return ioctl_preallocate(filp, 0, p); |
536 | case FS_IOC_UNRESVSP: | |
537 | case FS_IOC_UNRESVSP64: | |
538 | return ioctl_preallocate(filp, FALLOC_FL_PUNCH_HOLE, p); | |
539 | case FS_IOC_ZERO_RANGE: | |
540 | return ioctl_preallocate(filp, FALLOC_FL_ZERO_RANGE, p); | |
1da177e4 LT |
541 | } |
542 | ||
77b90401 | 543 | return -ENOIOCTLCMD; |
1da177e4 LT |
544 | } |
545 | ||
aa81a7c7 EZ |
546 | static int ioctl_fionbio(struct file *filp, int __user *argp) |
547 | { | |
548 | unsigned int flag; | |
549 | int on, error; | |
550 | ||
551 | error = get_user(on, argp); | |
552 | if (error) | |
553 | return error; | |
554 | flag = O_NONBLOCK; | |
555 | #ifdef __sparc__ | |
556 | /* SunOS compatibility item. */ | |
557 | if (O_NONBLOCK != O_NDELAY) | |
558 | flag |= O_NDELAY; | |
559 | #endif | |
db1dd4d3 | 560 | spin_lock(&filp->f_lock); |
aa81a7c7 EZ |
561 | if (on) |
562 | filp->f_flags |= flag; | |
563 | else | |
564 | filp->f_flags &= ~flag; | |
db1dd4d3 | 565 | spin_unlock(&filp->f_lock); |
aa81a7c7 EZ |
566 | return error; |
567 | } | |
568 | ||
569 | static int ioctl_fioasync(unsigned int fd, struct file *filp, | |
570 | int __user *argp) | |
571 | { | |
572 | unsigned int flag; | |
573 | int on, error; | |
574 | ||
575 | error = get_user(on, argp); | |
576 | if (error) | |
577 | return error; | |
578 | flag = on ? FASYNC : 0; | |
579 | ||
580 | /* Did FASYNC state change ? */ | |
581 | if ((flag ^ filp->f_flags) & FASYNC) { | |
72c2d531 | 582 | if (filp->f_op->fasync) |
76398425 | 583 | /* fasync() adjusts filp->f_flags */ |
aa81a7c7 | 584 | error = filp->f_op->fasync(fd, filp, on); |
218d11a8 | 585 | else |
aa81a7c7 EZ |
586 | error = -ENOTTY; |
587 | } | |
60aa4924 | 588 | return error < 0 ? error : 0; |
aa81a7c7 EZ |
589 | } |
590 | ||
fcccf502 TS |
591 | static int ioctl_fsfreeze(struct file *filp) |
592 | { | |
496ad9aa | 593 | struct super_block *sb = file_inode(filp)->i_sb; |
fcccf502 | 594 | |
f3f1a183 | 595 | if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) |
fcccf502 TS |
596 | return -EPERM; |
597 | ||
598 | /* If filesystem doesn't support freeze feature, return. */ | |
48b6bca6 | 599 | if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL) |
fcccf502 TS |
600 | return -EOPNOTSUPP; |
601 | ||
fcccf502 | 602 | /* Freeze */ |
48b6bca6 BM |
603 | if (sb->s_op->freeze_super) |
604 | return sb->s_op->freeze_super(sb); | |
18e9e510 | 605 | return freeze_super(sb); |
fcccf502 TS |
606 | } |
607 | ||
608 | static int ioctl_fsthaw(struct file *filp) | |
609 | { | |
496ad9aa | 610 | struct super_block *sb = file_inode(filp)->i_sb; |
fcccf502 | 611 | |
f3f1a183 | 612 | if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) |
fcccf502 TS |
613 | return -EPERM; |
614 | ||
fcccf502 | 615 | /* Thaw */ |
48b6bca6 BM |
616 | if (sb->s_op->thaw_super) |
617 | return sb->s_op->thaw_super(sb); | |
18e9e510 | 618 | return thaw_super(sb); |
fcccf502 TS |
619 | } |
620 | ||
34d3d0e6 AV |
621 | static int ioctl_file_dedupe_range(struct file *file, |
622 | struct file_dedupe_range __user *argp) | |
54dbc151 | 623 | { |
54dbc151 DW |
624 | struct file_dedupe_range *same = NULL; |
625 | int ret; | |
626 | unsigned long size; | |
627 | u16 count; | |
628 | ||
629 | if (get_user(count, &argp->dest_count)) { | |
630 | ret = -EFAULT; | |
631 | goto out; | |
632 | } | |
633 | ||
634 | size = offsetof(struct file_dedupe_range __user, info[count]); | |
b71dbf10 DW |
635 | if (size > PAGE_SIZE) { |
636 | ret = -ENOMEM; | |
637 | goto out; | |
638 | } | |
54dbc151 DW |
639 | |
640 | same = memdup_user(argp, size); | |
641 | if (IS_ERR(same)) { | |
642 | ret = PTR_ERR(same); | |
643 | same = NULL; | |
644 | goto out; | |
645 | } | |
646 | ||
10eec60c | 647 | same->dest_count = count; |
54dbc151 DW |
648 | ret = vfs_dedupe_file_range(file, same); |
649 | if (ret) | |
650 | goto out; | |
651 | ||
652 | ret = copy_to_user(argp, same, size); | |
653 | if (ret) | |
654 | ret = -EFAULT; | |
655 | ||
656 | out: | |
657 | kfree(same); | |
658 | return ret; | |
659 | } | |
660 | ||
1da177e4 | 661 | /* |
deb21db7 | 662 | * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d. |
1da177e4 | 663 | * It's just a simple helper for sys_ioctl and compat_sys_ioctl. |
77b90401 AB |
664 | * |
665 | * When you add any new common ioctls to the switches above and below, | |
666 | * please ensure they have compatible arguments in compat mode. | |
1da177e4 | 667 | */ |
77b90401 AB |
668 | static int do_vfs_ioctl(struct file *filp, unsigned int fd, |
669 | unsigned int cmd, unsigned long arg) | |
1da177e4 | 670 | { |
34d3d0e6 | 671 | void __user *argp = (void __user *)arg; |
496ad9aa | 672 | struct inode *inode = file_inode(filp); |
1da177e4 LT |
673 | |
674 | switch (cmd) { | |
c9845ff1 EZ |
675 | case FIOCLEX: |
676 | set_close_on_exec(fd, 1); | |
77b90401 | 677 | return 0; |
1da177e4 | 678 | |
c9845ff1 EZ |
679 | case FIONCLEX: |
680 | set_close_on_exec(fd, 0); | |
77b90401 | 681 | return 0; |
1da177e4 | 682 | |
c9845ff1 | 683 | case FIONBIO: |
77b90401 | 684 | return ioctl_fionbio(filp, argp); |
c9845ff1 EZ |
685 | |
686 | case FIOASYNC: | |
77b90401 | 687 | return ioctl_fioasync(fd, filp, argp); |
c9845ff1 EZ |
688 | |
689 | case FIOQSIZE: | |
27a4f7e6 NK |
690 | if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) || |
691 | S_ISLNK(inode->i_mode)) { | |
692 | loff_t res = inode_get_bytes(inode); | |
77b90401 AB |
693 | return copy_to_user(argp, &res, sizeof(res)) ? |
694 | -EFAULT : 0; | |
695 | } | |
696 | ||
697 | return -ENOTTY; | |
fcccf502 TS |
698 | |
699 | case FIFREEZE: | |
77b90401 | 700 | return ioctl_fsfreeze(filp); |
fcccf502 TS |
701 | |
702 | case FITHAW: | |
77b90401 | 703 | return ioctl_fsthaw(filp); |
fcccf502 | 704 | |
19ba0559 | 705 | case FS_IOC_FIEMAP: |
34d3d0e6 | 706 | return ioctl_fiemap(filp, argp); |
19ba0559 AK |
707 | |
708 | case FIGETBSZ: | |
8f97d1e9 AG |
709 | /* anon_bdev filesystems may not have a block size */ |
710 | if (!inode->i_sb->s_blocksize) | |
711 | return -EINVAL; | |
77b90401 | 712 | |
34d3d0e6 | 713 | return put_user(inode->i_sb->s_blocksize, (int __user *)argp); |
19ba0559 | 714 | |
04b38d60 CH |
715 | case FICLONE: |
716 | return ioctl_file_clone(filp, arg, 0, 0, 0); | |
717 | ||
718 | case FICLONERANGE: | |
719 | return ioctl_file_clone_range(filp, argp); | |
720 | ||
54dbc151 DW |
721 | case FIDEDUPERANGE: |
722 | return ioctl_file_dedupe_range(filp, argp); | |
723 | ||
c9845ff1 | 724 | default: |
27a4f7e6 | 725 | if (S_ISREG(inode->i_mode)) |
77b90401 | 726 | return file_ioctl(filp, cmd, argp); |
c9845ff1 | 727 | break; |
1da177e4 | 728 | } |
77b90401 AB |
729 | |
730 | return -ENOIOCTLCMD; | |
1da177e4 LT |
731 | } |
732 | ||
cbb60b92 | 733 | int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg) |
1da177e4 | 734 | { |
2903ff01 | 735 | struct fd f = fdget(fd); |
77b90401 | 736 | int error; |
2903ff01 AV |
737 | |
738 | if (!f.file) | |
739 | return -EBADF; | |
77b90401 | 740 | |
2903ff01 | 741 | error = security_file_ioctl(f.file, cmd, arg); |
77b90401 AB |
742 | if (error) |
743 | goto out; | |
744 | ||
745 | error = do_vfs_ioctl(f.file, fd, cmd, arg); | |
746 | if (error == -ENOIOCTLCMD) | |
747 | error = vfs_ioctl(f.file, cmd, arg); | |
748 | ||
749 | out: | |
2903ff01 | 750 | fdput(f); |
1da177e4 LT |
751 | return error; |
752 | } | |
cbb60b92 DB |
753 | |
754 | SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) | |
755 | { | |
756 | return ksys_ioctl(fd, cmd, arg); | |
757 | } | |
2952db0f AB |
758 | |
759 | #ifdef CONFIG_COMPAT | |
760 | /** | |
761 | * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation | |
762 | * | |
763 | * This is not normally called as a function, but instead set in struct | |
764 | * file_operations as | |
765 | * | |
766 | * .compat_ioctl = compat_ptr_ioctl, | |
767 | * | |
768 | * On most architectures, the compat_ptr_ioctl() just passes all arguments | |
769 | * to the corresponding ->ioctl handler. The exception is arch/s390, where | |
770 | * compat_ptr() clears the top bit of a 32-bit pointer value, so user space | |
771 | * pointers to the second 2GB alias the first 2GB, as is the case for | |
772 | * native 32-bit s390 user space. | |
773 | * | |
774 | * The compat_ptr_ioctl() function must therefore be used only with ioctl | |
775 | * functions that either ignore the argument or pass a pointer to a | |
776 | * compatible data type. | |
777 | * | |
778 | * If any ioctl command handled by fops->unlocked_ioctl passes a plain | |
779 | * integer instead of a pointer, or any of the passed data types | |
780 | * is incompatible between 32-bit and 64-bit architectures, a proper | |
781 | * handler is required instead of compat_ptr_ioctl. | |
782 | */ | |
783 | long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
784 | { | |
785 | if (!file->f_op->unlocked_ioctl) | |
786 | return -ENOIOCTLCMD; | |
787 | ||
788 | return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); | |
789 | } | |
790 | EXPORT_SYMBOL(compat_ptr_ioctl); | |
2af563d0 AB |
791 | |
792 | COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, | |
77b90401 | 793 | compat_ulong_t, arg) |
2af563d0 | 794 | { |
2af563d0 | 795 | struct fd f = fdget(fd); |
77b90401 AB |
796 | int error; |
797 | ||
2af563d0 | 798 | if (!f.file) |
77b90401 | 799 | return -EBADF; |
2af563d0 AB |
800 | |
801 | /* RED-PEN how should LSM module know it's handling 32bit? */ | |
802 | error = security_file_ioctl(f.file, cmd, arg); | |
803 | if (error) | |
77b90401 | 804 | goto out; |
2af563d0 AB |
805 | |
806 | switch (cmd) { | |
77b90401 | 807 | /* FICLONE takes an int argument, so don't use compat_ptr() */ |
2af563d0 | 808 | case FICLONE: |
77b90401 AB |
809 | error = ioctl_file_clone(f.file, arg, 0, 0, 0); |
810 | break; | |
811 | ||
2af563d0 | 812 | #if defined(CONFIG_X86_64) |
77b90401 | 813 | /* these get messy on amd64 due to alignment differences */ |
2af563d0 AB |
814 | case FS_IOC_RESVSP_32: |
815 | case FS_IOC_RESVSP64_32: | |
816 | error = compat_ioctl_preallocate(f.file, 0, compat_ptr(arg)); | |
77b90401 | 817 | break; |
2af563d0 AB |
818 | case FS_IOC_UNRESVSP_32: |
819 | case FS_IOC_UNRESVSP64_32: | |
820 | error = compat_ioctl_preallocate(f.file, FALLOC_FL_PUNCH_HOLE, | |
821 | compat_ptr(arg)); | |
77b90401 | 822 | break; |
2af563d0 AB |
823 | case FS_IOC_ZERO_RANGE_32: |
824 | error = compat_ioctl_preallocate(f.file, FALLOC_FL_ZERO_RANGE, | |
825 | compat_ptr(arg)); | |
77b90401 | 826 | break; |
2af563d0 AB |
827 | #endif |
828 | ||
77b90401 AB |
829 | /* |
830 | * everything else in do_vfs_ioctl() takes either a compatible | |
831 | * pointer argument or no argument -- call it with a modified | |
832 | * argument. | |
833 | */ | |
2af563d0 | 834 | default: |
77b90401 AB |
835 | error = do_vfs_ioctl(f.file, fd, cmd, |
836 | (unsigned long)compat_ptr(arg)); | |
837 | if (error != -ENOIOCTLCMD) | |
838 | break; | |
839 | ||
840 | if (f.file->f_op->compat_ioctl) | |
2af563d0 | 841 | error = f.file->f_op->compat_ioctl(f.file, cmd, arg); |
77b90401 AB |
842 | if (error == -ENOIOCTLCMD) |
843 | error = -ENOTTY; | |
844 | break; | |
2af563d0 AB |
845 | } |
846 | ||
2af563d0 | 847 | out: |
77b90401 AB |
848 | fdput(f); |
849 | ||
2af563d0 AB |
850 | return error; |
851 | } | |
2952db0f | 852 | #endif |