1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/syscalls.h>
10 #include <linux/capability.h>
11 #include <linux/compat.h>
12 #include <linux/file.h>
14 #include <linux/security.h>
15 #include <linux/export.h>
16 #include <linux/uaccess.h>
17 #include <linux/writeback.h>
18 #include <linux/buffer_head.h>
19 #include <linux/falloc.h>
20 #include <linux/sched/signal.h>
24 #include <asm/ioctls.h>
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))
30 * vfs_ioctl - call filesystem specific ioctl methods
31 * @filp: open file to invoke ioctl method on
32 * @cmd: ioctl command to execute
33 * @arg: command-specific argument for ioctl
35 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
38 * Returns 0 on success, -errno on error.
40 long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
44 if (!filp->f_op->unlocked_ioctl)
47 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
48 if (error == -ENOIOCTLCMD)
53 EXPORT_SYMBOL(vfs_ioctl);
55 static int ioctl_fibmap(struct file *filp, int __user *p)
57 struct inode *inode = file_inode(filp);
61 if (!capable(CAP_SYS_RAWIO))
64 error = get_user(ur_block, p);
72 error = bmap(inode, &block);
79 if (put_user(ur_block, p))
86 * fiemap_fill_next_extent - Fiemap helper function
87 * @fieinfo: Fiemap context passed into ->fiemap
88 * @logical: Extent logical start offset, in bytes
89 * @phys: Extent physical start offset, in bytes
90 * @len: Extent length, in bytes
91 * @flags: FIEMAP_EXTENT flags that describe this extent
93 * Called from file system ->fiemap callback. Will populate extent
94 * info as passed in via arguments and copy to user memory. On
95 * success, extent count on fieinfo is incremented.
97 * Returns 0 on success, -errno on error, 1 if this was the last
98 * extent that will fit in user array.
100 #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
101 #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED)
102 #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
103 int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
104 u64 phys, u64 len, u32 flags)
106 struct fiemap_extent extent;
107 struct fiemap_extent __user *dest = fieinfo->fi_extents_start;
109 /* only count the extents */
110 if (fieinfo->fi_extents_max == 0) {
111 fieinfo->fi_extents_mapped++;
112 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
115 if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
118 if (flags & SET_UNKNOWN_FLAGS)
119 flags |= FIEMAP_EXTENT_UNKNOWN;
120 if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
121 flags |= FIEMAP_EXTENT_ENCODED;
122 if (flags & SET_NOT_ALIGNED_FLAGS)
123 flags |= FIEMAP_EXTENT_NOT_ALIGNED;
125 memset(&extent, 0, sizeof(extent));
126 extent.fe_logical = logical;
127 extent.fe_physical = phys;
128 extent.fe_length = len;
129 extent.fe_flags = flags;
131 dest += fieinfo->fi_extents_mapped;
132 if (copy_to_user(dest, &extent, sizeof(extent)))
135 fieinfo->fi_extents_mapped++;
136 if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
138 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
140 EXPORT_SYMBOL(fiemap_fill_next_extent);
143 * fiemap_check_flags - check validity of requested flags for fiemap
144 * @fieinfo: Fiemap context passed into ->fiemap
145 * @fs_flags: Set of fiemap flags that the file system understands
147 * Called from file system ->fiemap callback. This will compute the
148 * intersection of valid fiemap flags and those that the fs supports. That
149 * value is then compared against the user supplied flags. In case of bad user
150 * flags, the invalid values will be written into the fieinfo structure, and
151 * -EBADR is returned, which tells ioctl_fiemap() to return those values to
152 * userspace. For this reason, a return code of -EBADR should be preserved.
154 * Returns 0 on success, -EBADR on bad flags.
156 int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
160 incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
161 if (incompat_flags) {
162 fieinfo->fi_flags = incompat_flags;
167 EXPORT_SYMBOL(fiemap_check_flags);
169 static int fiemap_check_ranges(struct super_block *sb,
170 u64 start, u64 len, u64 *new_len)
172 u64 maxbytes = (u64) sb->s_maxbytes;
179 if (start > maxbytes)
183 * Shrink request scope to what the fs can actually handle.
185 if (len > maxbytes || (maxbytes - len) < start)
186 *new_len = maxbytes - start;
191 static int ioctl_fiemap(struct file *filp, struct fiemap __user *ufiemap)
193 struct fiemap fiemap;
194 struct fiemap_extent_info fieinfo = { 0, };
195 struct inode *inode = file_inode(filp);
196 struct super_block *sb = inode->i_sb;
200 if (!inode->i_op->fiemap)
203 if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
206 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
209 error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
214 fieinfo.fi_flags = fiemap.fm_flags;
215 fieinfo.fi_extents_max = fiemap.fm_extent_count;
216 fieinfo.fi_extents_start = ufiemap->fm_extents;
218 if (fiemap.fm_extent_count != 0 &&
219 !access_ok(fieinfo.fi_extents_start,
220 fieinfo.fi_extents_max * sizeof(struct fiemap_extent)))
223 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
224 filemap_write_and_wait(inode->i_mapping);
226 error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len);
227 fiemap.fm_flags = fieinfo.fi_flags;
228 fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
229 if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
235 static long ioctl_file_clone(struct file *dst_file, unsigned long srcfd,
236 u64 off, u64 olen, u64 destoff)
238 struct fd src_file = fdget(srcfd);
245 if (src_file.file->f_path.mnt != dst_file->f_path.mnt)
247 cloned = vfs_clone_file_range(src_file.file, off, dst_file, destoff,
251 else if (olen && cloned != olen)
260 static long ioctl_file_clone_range(struct file *file,
261 struct file_clone_range __user *argp)
263 struct file_clone_range args;
265 if (copy_from_user(&args, argp, sizeof(args)))
267 return ioctl_file_clone(file, args.src_fd, args.src_offset,
268 args.src_length, args.dest_offset);
273 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
275 return (offset >> inode->i_blkbits);
278 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
280 return (blk << inode->i_blkbits);
284 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
285 * @inode: the inode to map
286 * @fieinfo: the fiemap info struct that will be passed back to userspace
287 * @start: where to start mapping in the inode
288 * @len: how much space to map
289 * @get_block: the fs's get_block function
291 * This does FIEMAP for block based inodes. Basically it will just loop
292 * through get_block until we hit the number of extents we want to map, or we
293 * go past the end of the file and hit a hole.
295 * If it is possible to have data blocks beyond a hole past @inode->i_size, then
296 * please do not use this function, it will stop at the first unmapped block
299 * If you use this function directly, you need to do your own locking. Use
300 * generic_block_fiemap if you want the locking done for you.
303 int __generic_block_fiemap(struct inode *inode,
304 struct fiemap_extent_info *fieinfo, loff_t start,
305 loff_t len, get_block_t *get_block)
307 struct buffer_head map_bh;
308 sector_t start_blk, last_blk;
309 loff_t isize = i_size_read(inode);
310 u64 logical = 0, phys = 0, size = 0;
311 u32 flags = FIEMAP_EXTENT_MERGED;
312 bool past_eof = false, whole_file = false;
315 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
320 * Either the i_mutex or other appropriate locking needs to be held
321 * since we expect isize to not change at all through the duration of
330 * Some filesystems can't deal with being asked to map less than
331 * blocksize, so make sure our len is at least block length.
333 if (logical_to_blk(inode, len) == 0)
334 len = blk_to_logical(inode, 1);
336 start_blk = logical_to_blk(inode, start);
337 last_blk = logical_to_blk(inode, start + len - 1);
341 * we set b_size to the total size we want so it will map as
342 * many contiguous blocks as possible at once
344 memset(&map_bh, 0, sizeof(struct buffer_head));
347 ret = get_block(inode, start_blk, &map_bh, 0);
352 if (!buffer_mapped(&map_bh)) {
356 * We want to handle the case where there is an
357 * allocated block at the front of the file, and then
358 * nothing but holes up to the end of the file properly,
359 * to make sure that extent at the front gets properly
360 * marked with FIEMAP_EXTENT_LAST
363 blk_to_logical(inode, start_blk) >= isize)
367 * First hole after going past the EOF, this is our
370 if (past_eof && size) {
371 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
372 ret = fiemap_fill_next_extent(fieinfo, logical,
376 ret = fiemap_fill_next_extent(fieinfo, logical,
381 /* if we have holes up to/past EOF then we're done */
382 if (start_blk > last_blk || past_eof || ret)
386 * We have gone over the length of what we wanted to
387 * map, and it wasn't the entire file, so add the extent
388 * we got last time and exit.
390 * This is for the case where say we want to map all the
391 * way up to the second to the last block in a file, but
392 * the last block is a hole, making the second to last
393 * block FIEMAP_EXTENT_LAST. In this case we want to
394 * see if there is a hole after the second to last block
395 * so we can mark it properly. If we found data after
396 * we exceeded the length we were requesting, then we
397 * are good to go, just add the extent to the fieinfo
400 if (start_blk > last_blk && !whole_file) {
401 ret = fiemap_fill_next_extent(fieinfo, logical,
408 * if size != 0 then we know we already have an extent
412 ret = fiemap_fill_next_extent(fieinfo, logical,
419 logical = blk_to_logical(inode, start_blk);
420 phys = blk_to_logical(inode, map_bh.b_blocknr);
421 size = map_bh.b_size;
422 flags = FIEMAP_EXTENT_MERGED;
424 start_blk += logical_to_blk(inode, size);
427 * If we are past the EOF, then we need to make sure as
428 * soon as we find a hole that the last extent we found
429 * is marked with FIEMAP_EXTENT_LAST
431 if (!past_eof && logical + size >= isize)
435 if (fatal_signal_pending(current)) {
442 /* If ret is 1 then we just hit the end of the extent array */
448 EXPORT_SYMBOL(__generic_block_fiemap);
451 * generic_block_fiemap - FIEMAP for block based inodes
452 * @inode: The inode to map
453 * @fieinfo: The mapping information
454 * @start: The initial block to map
455 * @len: The length of the extect to attempt to map
456 * @get_block: The block mapping function for the fs
458 * Calls __generic_block_fiemap to map the inode, after taking
459 * the inode's mutex lock.
462 int generic_block_fiemap(struct inode *inode,
463 struct fiemap_extent_info *fieinfo, u64 start,
464 u64 len, get_block_t *get_block)
468 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
472 EXPORT_SYMBOL(generic_block_fiemap);
474 #endif /* CONFIG_BLOCK */
477 * This provides compatibility with legacy XFS pre-allocation ioctls
478 * which predate the fallocate syscall.
480 * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
481 * are used here, rest are ignored.
483 static int ioctl_preallocate(struct file *filp, int mode, void __user *argp)
485 struct inode *inode = file_inode(filp);
486 struct space_resv sr;
488 if (copy_from_user(&sr, argp, sizeof(sr)))
491 switch (sr.l_whence) {
495 sr.l_start += filp->f_pos;
498 sr.l_start += i_size_read(inode);
504 return vfs_fallocate(filp, mode | FALLOC_FL_KEEP_SIZE, sr.l_start,
508 /* on ia32 l_start is on a 32-bit boundary */
509 #if defined CONFIG_COMPAT && defined(CONFIG_X86_64)
510 /* just account for different alignment */
511 static int compat_ioctl_preallocate(struct file *file, int mode,
512 struct space_resv_32 __user *argp)
514 struct inode *inode = file_inode(file);
515 struct space_resv_32 sr;
517 if (copy_from_user(&sr, argp, sizeof(sr)))
520 switch (sr.l_whence) {
524 sr.l_start += file->f_pos;
527 sr.l_start += i_size_read(inode);
533 return vfs_fallocate(file, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
537 static int file_ioctl(struct file *filp, unsigned int cmd, int __user *p)
539 struct inode *inode = file_inode(filp);
543 return ioctl_fibmap(filp, p);
545 return put_user(i_size_read(inode) - filp->f_pos, p);
547 case FS_IOC_RESVSP64:
548 return ioctl_preallocate(filp, 0, p);
549 case FS_IOC_UNRESVSP:
550 case FS_IOC_UNRESVSP64:
551 return ioctl_preallocate(filp, FALLOC_FL_PUNCH_HOLE, p);
552 case FS_IOC_ZERO_RANGE:
553 return ioctl_preallocate(filp, FALLOC_FL_ZERO_RANGE, p);
559 static int ioctl_fionbio(struct file *filp, int __user *argp)
564 error = get_user(on, argp);
569 /* SunOS compatibility item. */
570 if (O_NONBLOCK != O_NDELAY)
573 spin_lock(&filp->f_lock);
575 filp->f_flags |= flag;
577 filp->f_flags &= ~flag;
578 spin_unlock(&filp->f_lock);
582 static int ioctl_fioasync(unsigned int fd, struct file *filp,
588 error = get_user(on, argp);
591 flag = on ? FASYNC : 0;
593 /* Did FASYNC state change ? */
594 if ((flag ^ filp->f_flags) & FASYNC) {
595 if (filp->f_op->fasync)
596 /* fasync() adjusts filp->f_flags */
597 error = filp->f_op->fasync(fd, filp, on);
601 return error < 0 ? error : 0;
604 static int ioctl_fsfreeze(struct file *filp)
606 struct super_block *sb = file_inode(filp)->i_sb;
608 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
611 /* If filesystem doesn't support freeze feature, return. */
612 if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL)
616 if (sb->s_op->freeze_super)
617 return sb->s_op->freeze_super(sb);
618 return freeze_super(sb);
621 static int ioctl_fsthaw(struct file *filp)
623 struct super_block *sb = file_inode(filp)->i_sb;
625 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
629 if (sb->s_op->thaw_super)
630 return sb->s_op->thaw_super(sb);
631 return thaw_super(sb);
634 static int ioctl_file_dedupe_range(struct file *file,
635 struct file_dedupe_range __user *argp)
637 struct file_dedupe_range *same = NULL;
642 if (get_user(count, &argp->dest_count)) {
647 size = offsetof(struct file_dedupe_range __user, info[count]);
648 if (size > PAGE_SIZE) {
653 same = memdup_user(argp, size);
660 same->dest_count = count;
661 ret = vfs_dedupe_file_range(file, same);
665 ret = copy_to_user(argp, same, size);
675 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
676 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
678 * When you add any new common ioctls to the switches above and below,
679 * please ensure they have compatible arguments in compat mode.
681 static int do_vfs_ioctl(struct file *filp, unsigned int fd,
682 unsigned int cmd, unsigned long arg)
684 void __user *argp = (void __user *)arg;
685 struct inode *inode = file_inode(filp);
689 set_close_on_exec(fd, 1);
693 set_close_on_exec(fd, 0);
697 return ioctl_fionbio(filp, argp);
700 return ioctl_fioasync(fd, filp, argp);
703 if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) ||
704 S_ISLNK(inode->i_mode)) {
705 loff_t res = inode_get_bytes(inode);
706 return copy_to_user(argp, &res, sizeof(res)) ?
713 return ioctl_fsfreeze(filp);
716 return ioctl_fsthaw(filp);
719 return ioctl_fiemap(filp, argp);
722 /* anon_bdev filesystems may not have a block size */
723 if (!inode->i_sb->s_blocksize)
726 return put_user(inode->i_sb->s_blocksize, (int __user *)argp);
729 return ioctl_file_clone(filp, arg, 0, 0, 0);
732 return ioctl_file_clone_range(filp, argp);
735 return ioctl_file_dedupe_range(filp, argp);
738 if (S_ISREG(inode->i_mode))
739 return file_ioctl(filp, cmd, argp);
746 int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
748 struct fd f = fdget(fd);
754 error = security_file_ioctl(f.file, cmd, arg);
758 error = do_vfs_ioctl(f.file, fd, cmd, arg);
759 if (error == -ENOIOCTLCMD)
760 error = vfs_ioctl(f.file, cmd, arg);
767 SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
769 return ksys_ioctl(fd, cmd, arg);
774 * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation
776 * This is not normally called as a function, but instead set in struct
779 * .compat_ioctl = compat_ptr_ioctl,
781 * On most architectures, the compat_ptr_ioctl() just passes all arguments
782 * to the corresponding ->ioctl handler. The exception is arch/s390, where
783 * compat_ptr() clears the top bit of a 32-bit pointer value, so user space
784 * pointers to the second 2GB alias the first 2GB, as is the case for
785 * native 32-bit s390 user space.
787 * The compat_ptr_ioctl() function must therefore be used only with ioctl
788 * functions that either ignore the argument or pass a pointer to a
789 * compatible data type.
791 * If any ioctl command handled by fops->unlocked_ioctl passes a plain
792 * integer instead of a pointer, or any of the passed data types
793 * is incompatible between 32-bit and 64-bit architectures, a proper
794 * handler is required instead of compat_ptr_ioctl.
796 long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
798 if (!file->f_op->unlocked_ioctl)
801 return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
803 EXPORT_SYMBOL(compat_ptr_ioctl);
805 COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
808 struct fd f = fdget(fd);
814 /* RED-PEN how should LSM module know it's handling 32bit? */
815 error = security_file_ioctl(f.file, cmd, arg);
820 /* FICLONE takes an int argument, so don't use compat_ptr() */
822 error = ioctl_file_clone(f.file, arg, 0, 0, 0);
825 #if defined(CONFIG_X86_64)
826 /* these get messy on amd64 due to alignment differences */
827 case FS_IOC_RESVSP_32:
828 case FS_IOC_RESVSP64_32:
829 error = compat_ioctl_preallocate(f.file, 0, compat_ptr(arg));
831 case FS_IOC_UNRESVSP_32:
832 case FS_IOC_UNRESVSP64_32:
833 error = compat_ioctl_preallocate(f.file, FALLOC_FL_PUNCH_HOLE,
836 case FS_IOC_ZERO_RANGE_32:
837 error = compat_ioctl_preallocate(f.file, FALLOC_FL_ZERO_RANGE,
843 * everything else in do_vfs_ioctl() takes either a compatible
844 * pointer argument or no argument -- call it with a modified
848 error = do_vfs_ioctl(f.file, fd, cmd,
849 (unsigned long)compat_ptr(arg));
850 if (error != -ENOIOCTLCMD)
853 if (f.file->f_op->compat_ioctl)
854 error = f.file->f_op->compat_ioctl(f.file, cmd, arg);
855 if (error == -ENOIOCTLCMD)