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