blob: 5e80b40bc1b5cab63aa4472a2616aa06d6169a45 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/ioctl.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/syscalls.h>
9#include <linux/mm.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080010#include <linux/capability.h>
Arnd Bergmann2952db02018-09-11 16:55:03 +020011#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/file.h>
13#include <linux/fs.h>
14#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050015#include <linux/export.h>
Erez Zadokc9845ff2008-02-07 00:13:23 -080016#include <linux/uaccess.h>
Josef Bacik68c9d702008-10-03 17:32:43 -040017#include <linux/writeback.h>
18#include <linux/buffer_head.h>
Ankit Jain3e63cbb2009-06-19 14:28:07 -040019#include <linux/falloc.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010020#include <linux/sched/signal.h>
21
Al Viro66cf1912016-01-07 09:53:30 -050022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/ioctls.h>
25
Mark Fashehc4b929b2008-10-08 19:44:18 -040026/* 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
Erez Zadokdeb21db2008-02-07 00:13:25 -080029/**
30 * vfs_ioctl - call filesystem specific ioctl methods
Christoph Hellwigf6a4c8b2008-02-09 00:10:16 -080031 * @filp: open file to invoke ioctl method on
32 * @cmd: ioctl command to execute
33 * @arg: command-specific argument for ioctl
Erez Zadokdeb21db2008-02-07 00:13:25 -080034 *
35 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
Erez Zadokdeb21db2008-02-07 00:13:25 -080036 * returns -ENOTTY.
37 *
38 * Returns 0 on success, -errno on error.
39 */
Al Viro66cf1912016-01-07 09:53:30 -050040long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 int error = -ENOTTY;
43
Al Viro72c2d532013-09-22 16:27:52 -040044 if (!filp->f_op->unlocked_ioctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 goto out;
46
Arnd Bergmannb19dd422010-07-04 00:15:10 +020047 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
48 if (error == -ENOIOCTLCMD)
Linus Torvalds07d106d2012-01-05 15:40:12 -080049 error = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 out:
51 return error;
52}
Miklos Szeredi9df67022018-07-18 15:44:40 +020053EXPORT_SYMBOL(vfs_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Erez Zadokaa81a7c2008-02-07 00:13:25 -080055static int ioctl_fibmap(struct file *filp, int __user *p)
56{
Carlos Maiolino0d89fda2020-01-09 14:30:44 +010057 struct inode *inode = file_inode(filp);
Ritesh Harjanib75dfde2020-04-30 07:57:46 -070058 struct super_block *sb = inode->i_sb;
Carlos Maiolino0d89fda2020-01-09 14:30:44 +010059 int error, ur_block;
60 sector_t block;
Erez Zadokaa81a7c2008-02-07 00:13:25 -080061
Erez Zadokaa81a7c2008-02-07 00:13:25 -080062 if (!capable(CAP_SYS_RAWIO))
63 return -EPERM;
Carlos Maiolino0d89fda2020-01-09 14:30:44 +010064
65 error = get_user(ur_block, p);
66 if (error)
67 return error;
68
Carlos Maiolino324282c2020-01-09 14:30:45 +010069 if (ur_block < 0)
70 return -EINVAL;
71
Carlos Maiolino0d89fda2020-01-09 14:30:44 +010072 block = ur_block;
73 error = bmap(inode, &block);
74
Ritesh Harjanib75dfde2020-04-30 07:57:46 -070075 if (block > INT_MAX) {
76 error = -ERANGE;
77 pr_warn_ratelimited("[%s/%d] FS: %s File: %pD4 would truncate fibmap result\n",
78 current->comm, task_pid_nr(current),
79 sb->s_id, filp);
80 }
81
Carlos Maiolino0d89fda2020-01-09 14:30:44 +010082 if (error)
83 ur_block = 0;
84 else
85 ur_block = block;
86
87 if (put_user(ur_block, p))
88 error = -EFAULT;
89
90 return error;
Erez Zadokaa81a7c2008-02-07 00:13:25 -080091}
92
Mark Fashehc4b929b2008-10-08 19:44:18 -040093/**
94 * fiemap_fill_next_extent - Fiemap helper function
95 * @fieinfo: Fiemap context passed into ->fiemap
96 * @logical: Extent logical start offset, in bytes
97 * @phys: Extent physical start offset, in bytes
98 * @len: Extent length, in bytes
99 * @flags: FIEMAP_EXTENT flags that describe this extent
100 *
101 * Called from file system ->fiemap callback. Will populate extent
102 * info as passed in via arguments and copy to user memory. On
103 * success, extent count on fieinfo is incremented.
104 *
105 * Returns 0 on success, -errno on error, 1 if this was the last
106 * extent that will fit in user array.
107 */
108#define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
109#define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED)
110#define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
111int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
112 u64 phys, u64 len, u32 flags)
113{
114 struct fiemap_extent extent;
Namhyung Kimecf56322011-01-16 23:28:17 +0900115 struct fiemap_extent __user *dest = fieinfo->fi_extents_start;
Mark Fashehc4b929b2008-10-08 19:44:18 -0400116
117 /* only count the extents */
118 if (fieinfo->fi_extents_max == 0) {
119 fieinfo->fi_extents_mapped++;
120 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
121 }
122
123 if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
124 return 1;
125
126 if (flags & SET_UNKNOWN_FLAGS)
127 flags |= FIEMAP_EXTENT_UNKNOWN;
128 if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
129 flags |= FIEMAP_EXTENT_ENCODED;
130 if (flags & SET_NOT_ALIGNED_FLAGS)
131 flags |= FIEMAP_EXTENT_NOT_ALIGNED;
132
133 memset(&extent, 0, sizeof(extent));
134 extent.fe_logical = logical;
135 extent.fe_physical = phys;
136 extent.fe_length = len;
137 extent.fe_flags = flags;
138
139 dest += fieinfo->fi_extents_mapped;
140 if (copy_to_user(dest, &extent, sizeof(extent)))
141 return -EFAULT;
142
143 fieinfo->fi_extents_mapped++;
144 if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
145 return 1;
146 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
147}
148EXPORT_SYMBOL(fiemap_fill_next_extent);
149
150/**
151 * fiemap_check_flags - check validity of requested flags for fiemap
152 * @fieinfo: Fiemap context passed into ->fiemap
153 * @fs_flags: Set of fiemap flags that the file system understands
154 *
155 * Called from file system ->fiemap callback. This will compute the
156 * intersection of valid fiemap flags and those that the fs supports. That
157 * value is then compared against the user supplied flags. In case of bad user
158 * flags, the invalid values will be written into the fieinfo structure, and
159 * -EBADR is returned, which tells ioctl_fiemap() to return those values to
160 * userspace. For this reason, a return code of -EBADR should be preserved.
161 *
162 * Returns 0 on success, -EBADR on bad flags.
163 */
164int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
165{
166 u32 incompat_flags;
167
168 incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
169 if (incompat_flags) {
170 fieinfo->fi_flags = incompat_flags;
171 return -EBADR;
172 }
173 return 0;
174}
175EXPORT_SYMBOL(fiemap_check_flags);
176
177static int fiemap_check_ranges(struct super_block *sb,
178 u64 start, u64 len, u64 *new_len)
179{
Jeff Layton5aa98b72009-09-18 13:05:50 -0700180 u64 maxbytes = (u64) sb->s_maxbytes;
181
Mark Fashehc4b929b2008-10-08 19:44:18 -0400182 *new_len = len;
183
184 if (len == 0)
185 return -EINVAL;
186
Jeff Layton5aa98b72009-09-18 13:05:50 -0700187 if (start > maxbytes)
Mark Fashehc4b929b2008-10-08 19:44:18 -0400188 return -EFBIG;
189
190 /*
191 * Shrink request scope to what the fs can actually handle.
192 */
Jeff Layton5aa98b72009-09-18 13:05:50 -0700193 if (len > maxbytes || (maxbytes - len) < start)
194 *new_len = maxbytes - start;
Mark Fashehc4b929b2008-10-08 19:44:18 -0400195
196 return 0;
197}
198
Al Viro34d3d0e2019-04-21 18:45:28 -0400199static int ioctl_fiemap(struct file *filp, struct fiemap __user *ufiemap)
Mark Fashehc4b929b2008-10-08 19:44:18 -0400200{
201 struct fiemap fiemap;
202 struct fiemap_extent_info fieinfo = { 0, };
Al Viro496ad9a2013-01-23 17:07:38 -0500203 struct inode *inode = file_inode(filp);
Mark Fashehc4b929b2008-10-08 19:44:18 -0400204 struct super_block *sb = inode->i_sb;
205 u64 len;
206 int error;
207
208 if (!inode->i_op->fiemap)
209 return -EOPNOTSUPP;
210
Namhyung Kimecf56322011-01-16 23:28:17 +0900211 if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
Mark Fashehc4b929b2008-10-08 19:44:18 -0400212 return -EFAULT;
213
214 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
215 return -EINVAL;
216
217 error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
218 &len);
219 if (error)
220 return error;
221
222 fieinfo.fi_flags = fiemap.fm_flags;
223 fieinfo.fi_extents_max = fiemap.fm_extent_count;
Namhyung Kimecf56322011-01-16 23:28:17 +0900224 fieinfo.fi_extents_start = ufiemap->fm_extents;
Mark Fashehc4b929b2008-10-08 19:44:18 -0400225
226 if (fiemap.fm_extent_count != 0 &&
Linus Torvalds96d4f262019-01-03 18:57:57 -0800227 !access_ok(fieinfo.fi_extents_start,
Mark Fashehc4b929b2008-10-08 19:44:18 -0400228 fieinfo.fi_extents_max * sizeof(struct fiemap_extent)))
229 return -EFAULT;
230
231 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
232 filemap_write_and_wait(inode->i_mapping);
233
234 error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len);
235 fiemap.fm_flags = fieinfo.fi_flags;
236 fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
Namhyung Kimecf56322011-01-16 23:28:17 +0900237 if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
Mark Fashehc4b929b2008-10-08 19:44:18 -0400238 error = -EFAULT;
239
240 return error;
241}
242
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100243static long ioctl_file_clone(struct file *dst_file, unsigned long srcfd,
244 u64 off, u64 olen, u64 destoff)
245{
246 struct fd src_file = fdget(srcfd);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100247 loff_t cloned;
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100248 int ret;
249
250 if (!src_file.file)
251 return -EBADF;
Amir Goldstein913b86e2016-09-23 11:38:10 +0300252 ret = -EXDEV;
253 if (src_file.file->f_path.mnt != dst_file->f_path.mnt)
254 goto fdput;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100255 cloned = vfs_clone_file_range(src_file.file, off, dst_file, destoff,
Darrick J. Wong452ce652018-10-30 10:41:56 +1100256 olen, 0);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100257 if (cloned < 0)
258 ret = cloned;
259 else if (olen && cloned != olen)
260 ret = -EINVAL;
261 else
262 ret = 0;
Amir Goldstein913b86e2016-09-23 11:38:10 +0300263fdput:
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100264 fdput(src_file);
265 return ret;
266}
267
Al Viro34d3d0e2019-04-21 18:45:28 -0400268static long ioctl_file_clone_range(struct file *file,
269 struct file_clone_range __user *argp)
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100270{
271 struct file_clone_range args;
272
273 if (copy_from_user(&args, argp, sizeof(args)))
274 return -EFAULT;
275 return ioctl_file_clone(file, args.src_fd, args.src_offset,
276 args.src_length, args.dest_offset);
277}
278
Adrian Bunk06270d52008-10-12 07:15:19 +0300279#ifdef CONFIG_BLOCK
280
Josef Bacik3a3076f2010-04-23 12:17:17 -0400281static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
282{
283 return (offset >> inode->i_blkbits);
284}
285
286static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
287{
288 return (blk << inode->i_blkbits);
289}
Josef Bacik68c9d702008-10-03 17:32:43 -0400290
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100291/**
292 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
Josef Bacik3a3076f2010-04-23 12:17:17 -0400293 * @inode: the inode to map
294 * @fieinfo: the fiemap info struct that will be passed back to userspace
295 * @start: where to start mapping in the inode
296 * @len: how much space to map
297 * @get_block: the fs's get_block function
Josef Bacik68c9d702008-10-03 17:32:43 -0400298 *
299 * This does FIEMAP for block based inodes. Basically it will just loop
300 * through get_block until we hit the number of extents we want to map, or we
301 * go past the end of the file and hit a hole.
302 *
303 * If it is possible to have data blocks beyond a hole past @inode->i_size, then
304 * please do not use this function, it will stop at the first unmapped block
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100305 * beyond i_size.
306 *
307 * If you use this function directly, you need to do your own locking. Use
308 * generic_block_fiemap if you want the locking done for you.
Josef Bacik68c9d702008-10-03 17:32:43 -0400309 */
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100310
311int __generic_block_fiemap(struct inode *inode,
Josef Bacik3a3076f2010-04-23 12:17:17 -0400312 struct fiemap_extent_info *fieinfo, loff_t start,
313 loff_t len, get_block_t *get_block)
Josef Bacik68c9d702008-10-03 17:32:43 -0400314{
Josef Bacik3a3076f2010-04-23 12:17:17 -0400315 struct buffer_head map_bh;
316 sector_t start_blk, last_blk;
317 loff_t isize = i_size_read(inode);
Josef Bacik68c9d702008-10-03 17:32:43 -0400318 u64 logical = 0, phys = 0, size = 0;
319 u32 flags = FIEMAP_EXTENT_MERGED;
Josef Bacik3a3076f2010-04-23 12:17:17 -0400320 bool past_eof = false, whole_file = false;
321 int ret = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400322
Josef Bacik3a3076f2010-04-23 12:17:17 -0400323 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
324 if (ret)
Josef Bacik68c9d702008-10-03 17:32:43 -0400325 return ret;
326
Josef Bacik3a3076f2010-04-23 12:17:17 -0400327 /*
328 * Either the i_mutex or other appropriate locking needs to be held
329 * since we expect isize to not change at all through the duration of
330 * this call.
331 */
332 if (len >= isize) {
333 whole_file = true;
334 len = isize;
335 }
336
Josef Bacikd54cdc82011-02-01 15:52:47 -0800337 /*
338 * Some filesystems can't deal with being asked to map less than
339 * blocksize, so make sure our len is at least block length.
340 */
341 if (logical_to_blk(inode, len) == 0)
342 len = blk_to_logical(inode, 1);
343
Josef Bacik68c9d702008-10-03 17:32:43 -0400344 start_blk = logical_to_blk(inode, start);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400345 last_blk = logical_to_blk(inode, start + len - 1);
Josef Bacik68c9d702008-10-03 17:32:43 -0400346
347 do {
348 /*
349 * we set b_size to the total size we want so it will map as
350 * many contiguous blocks as possible at once
351 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400352 memset(&map_bh, 0, sizeof(struct buffer_head));
353 map_bh.b_size = len;
Josef Bacik68c9d702008-10-03 17:32:43 -0400354
Josef Bacik3a3076f2010-04-23 12:17:17 -0400355 ret = get_block(inode, start_blk, &map_bh, 0);
Josef Bacik68c9d702008-10-03 17:32:43 -0400356 if (ret)
357 break;
358
359 /* HOLE */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400360 if (!buffer_mapped(&map_bh)) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700361 start_blk++;
362
363 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400364 * We want to handle the case where there is an
Josef Bacikdf3935f2009-05-06 16:02:53 -0700365 * allocated block at the front of the file, and then
366 * nothing but holes up to the end of the file properly,
367 * to make sure that extent at the front gets properly
368 * marked with FIEMAP_EXTENT_LAST
369 */
370 if (!past_eof &&
Josef Bacik3a3076f2010-04-23 12:17:17 -0400371 blk_to_logical(inode, start_blk) >= isize)
Josef Bacikdf3935f2009-05-06 16:02:53 -0700372 past_eof = 1;
373
Josef Bacik68c9d702008-10-03 17:32:43 -0400374 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400375 * First hole after going past the EOF, this is our
Josef Bacik68c9d702008-10-03 17:32:43 -0400376 * last extent
377 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700378 if (past_eof && size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400379 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
380 ret = fiemap_fill_next_extent(fieinfo, logical,
381 phys, size,
382 flags);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400383 } else if (size) {
384 ret = fiemap_fill_next_extent(fieinfo, logical,
385 phys, size, flags);
386 size = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400387 }
388
Josef Bacik68c9d702008-10-03 17:32:43 -0400389 /* if we have holes up to/past EOF then we're done */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400390 if (start_blk > last_blk || past_eof || ret)
Josef Bacik68c9d702008-10-03 17:32:43 -0400391 break;
Josef Bacik68c9d702008-10-03 17:32:43 -0400392 } else {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700393 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400394 * We have gone over the length of what we wanted to
Josef Bacikdf3935f2009-05-06 16:02:53 -0700395 * map, and it wasn't the entire file, so add the extent
396 * we got last time and exit.
397 *
398 * This is for the case where say we want to map all the
399 * way up to the second to the last block in a file, but
400 * the last block is a hole, making the second to last
401 * block FIEMAP_EXTENT_LAST. In this case we want to
402 * see if there is a hole after the second to last block
403 * so we can mark it properly. If we found data after
404 * we exceeded the length we were requesting, then we
405 * are good to go, just add the extent to the fieinfo
406 * and break
407 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400408 if (start_blk > last_blk && !whole_file) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700409 ret = fiemap_fill_next_extent(fieinfo, logical,
410 phys, size,
411 flags);
412 break;
413 }
414
415 /*
416 * if size != 0 then we know we already have an extent
417 * to add, so add it.
418 */
419 if (size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400420 ret = fiemap_fill_next_extent(fieinfo, logical,
421 phys, size,
422 flags);
423 if (ret)
424 break;
425 }
426
427 logical = blk_to_logical(inode, start_blk);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400428 phys = blk_to_logical(inode, map_bh.b_blocknr);
429 size = map_bh.b_size;
Josef Bacik68c9d702008-10-03 17:32:43 -0400430 flags = FIEMAP_EXTENT_MERGED;
431
Josef Bacik68c9d702008-10-03 17:32:43 -0400432 start_blk += logical_to_blk(inode, size);
433
434 /*
Josef Bacikdf3935f2009-05-06 16:02:53 -0700435 * If we are past the EOF, then we need to make sure as
436 * soon as we find a hole that the last extent we found
437 * is marked with FIEMAP_EXTENT_LAST
Josef Bacik68c9d702008-10-03 17:32:43 -0400438 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400439 if (!past_eof && logical + size >= isize)
440 past_eof = true;
Josef Bacik68c9d702008-10-03 17:32:43 -0400441 }
442 cond_resched();
Dmitry Monakhov913e0272015-02-10 14:09:29 -0800443 if (fatal_signal_pending(current)) {
444 ret = -EINTR;
445 break;
446 }
447
Josef Bacik68c9d702008-10-03 17:32:43 -0400448 } while (1);
449
Josef Bacik3a3076f2010-04-23 12:17:17 -0400450 /* If ret is 1 then we just hit the end of the extent array */
Josef Bacik68c9d702008-10-03 17:32:43 -0400451 if (ret == 1)
452 ret = 0;
453
454 return ret;
455}
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100456EXPORT_SYMBOL(__generic_block_fiemap);
457
458/**
459 * generic_block_fiemap - FIEMAP for block based inodes
460 * @inode: The inode to map
461 * @fieinfo: The mapping information
462 * @start: The initial block to map
463 * @len: The length of the extect to attempt to map
464 * @get_block: The block mapping function for the fs
465 *
466 * Calls __generic_block_fiemap to map the inode, after taking
467 * the inode's mutex lock.
468 */
469
470int generic_block_fiemap(struct inode *inode,
471 struct fiemap_extent_info *fieinfo, u64 start,
472 u64 len, get_block_t *get_block)
473{
474 int ret;
Al Viro59551022016-01-22 15:40:57 -0500475 inode_lock(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100476 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
Al Viro59551022016-01-22 15:40:57 -0500477 inode_unlock(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100478 return ret;
479}
Josef Bacik68c9d702008-10-03 17:32:43 -0400480EXPORT_SYMBOL(generic_block_fiemap);
481
Adrian Bunk06270d52008-10-12 07:15:19 +0300482#endif /* CONFIG_BLOCK */
483
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400484/*
485 * This provides compatibility with legacy XFS pre-allocation ioctls
486 * which predate the fallocate syscall.
487 *
488 * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
489 * are used here, rest are ignored.
490 */
Arnd Bergmann77b90402019-11-27 21:25:36 +0100491static int ioctl_preallocate(struct file *filp, int mode, void __user *argp)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400492{
Al Viro496ad9a2013-01-23 17:07:38 -0500493 struct inode *inode = file_inode(filp);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400494 struct space_resv sr;
495
496 if (copy_from_user(&sr, argp, sizeof(sr)))
497 return -EFAULT;
498
499 switch (sr.l_whence) {
500 case SEEK_SET:
501 break;
502 case SEEK_CUR:
503 sr.l_start += filp->f_pos;
504 break;
505 case SEEK_END:
506 sr.l_start += i_size_read(inode);
507 break;
508 default:
509 return -EINVAL;
510 }
511
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700512 return vfs_fallocate(filp, mode | FALLOC_FL_KEEP_SIZE, sr.l_start,
513 sr.l_len);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400514}
515
Al Viro011da442019-04-21 19:24:03 -0400516/* on ia32 l_start is on a 32-bit boundary */
517#if defined CONFIG_COMPAT && defined(CONFIG_X86_64)
518/* just account for different alignment */
Arnd Bergmann77b90402019-11-27 21:25:36 +0100519static int compat_ioctl_preallocate(struct file *file, int mode,
520 struct space_resv_32 __user *argp)
Al Viro011da442019-04-21 19:24:03 -0400521{
522 struct inode *inode = file_inode(file);
523 struct space_resv_32 sr;
524
525 if (copy_from_user(&sr, argp, sizeof(sr)))
526 return -EFAULT;
527
528 switch (sr.l_whence) {
529 case SEEK_SET:
530 break;
531 case SEEK_CUR:
532 sr.l_start += file->f_pos;
533 break;
534 case SEEK_END:
535 sr.l_start += i_size_read(inode);
536 break;
537 default:
538 return -EINVAL;
539 }
540
Linus Torvalds97eeb4d2019-12-02 14:46:22 -0800541 return vfs_fallocate(file, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
Al Viro011da442019-04-21 19:24:03 -0400542}
543#endif
544
Arnd Bergmann77b90402019-11-27 21:25:36 +0100545static int file_ioctl(struct file *filp, unsigned int cmd, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800548 case FIBMAP:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800549 return ioctl_fibmap(filp, p);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400550 case FS_IOC_RESVSP:
551 case FS_IOC_RESVSP64:
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700552 return ioctl_preallocate(filp, 0, p);
553 case FS_IOC_UNRESVSP:
554 case FS_IOC_UNRESVSP64:
555 return ioctl_preallocate(filp, FALLOC_FL_PUNCH_HOLE, p);
556 case FS_IOC_ZERO_RANGE:
557 return ioctl_preallocate(filp, FALLOC_FL_ZERO_RANGE, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
Arnd Bergmann77b90402019-11-27 21:25:36 +0100560 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800563static int ioctl_fionbio(struct file *filp, int __user *argp)
564{
565 unsigned int flag;
566 int on, error;
567
568 error = get_user(on, argp);
569 if (error)
570 return error;
571 flag = O_NONBLOCK;
572#ifdef __sparc__
573 /* SunOS compatibility item. */
574 if (O_NONBLOCK != O_NDELAY)
575 flag |= O_NDELAY;
576#endif
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700577 spin_lock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800578 if (on)
579 filp->f_flags |= flag;
580 else
581 filp->f_flags &= ~flag;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700582 spin_unlock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800583 return error;
584}
585
586static int ioctl_fioasync(unsigned int fd, struct file *filp,
587 int __user *argp)
588{
589 unsigned int flag;
590 int on, error;
591
592 error = get_user(on, argp);
593 if (error)
594 return error;
595 flag = on ? FASYNC : 0;
596
597 /* Did FASYNC state change ? */
598 if ((flag ^ filp->f_flags) & FASYNC) {
Al Viro72c2d532013-09-22 16:27:52 -0400599 if (filp->f_op->fasync)
Jonathan Corbet76398422009-02-01 14:26:59 -0700600 /* fasync() adjusts filp->f_flags */
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800601 error = filp->f_op->fasync(fd, filp, on);
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700602 else
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800603 error = -ENOTTY;
604 }
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700605 return error < 0 ? error : 0;
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800606}
607
Takashi Satofcccf502009-01-09 16:40:59 -0800608static int ioctl_fsfreeze(struct file *filp)
609{
Al Viro496ad9a2013-01-23 17:07:38 -0500610 struct super_block *sb = file_inode(filp)->i_sb;
Takashi Satofcccf502009-01-09 16:40:59 -0800611
Seth Forsheef3f1a182015-02-15 14:35:35 -0600612 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
Takashi Satofcccf502009-01-09 16:40:59 -0800613 return -EPERM;
614
615 /* If filesystem doesn't support freeze feature, return. */
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600616 if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL)
Takashi Satofcccf502009-01-09 16:40:59 -0800617 return -EOPNOTSUPP;
618
Takashi Satofcccf502009-01-09 16:40:59 -0800619 /* Freeze */
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600620 if (sb->s_op->freeze_super)
621 return sb->s_op->freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400622 return freeze_super(sb);
Takashi Satofcccf502009-01-09 16:40:59 -0800623}
624
625static int ioctl_fsthaw(struct file *filp)
626{
Al Viro496ad9a2013-01-23 17:07:38 -0500627 struct super_block *sb = file_inode(filp)->i_sb;
Takashi Satofcccf502009-01-09 16:40:59 -0800628
Seth Forsheef3f1a182015-02-15 14:35:35 -0600629 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
Takashi Satofcccf502009-01-09 16:40:59 -0800630 return -EPERM;
631
Takashi Satofcccf502009-01-09 16:40:59 -0800632 /* Thaw */
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600633 if (sb->s_op->thaw_super)
634 return sb->s_op->thaw_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400635 return thaw_super(sb);
Takashi Satofcccf502009-01-09 16:40:59 -0800636}
637
Al Viro34d3d0e2019-04-21 18:45:28 -0400638static int ioctl_file_dedupe_range(struct file *file,
639 struct file_dedupe_range __user *argp)
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800640{
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800641 struct file_dedupe_range *same = NULL;
642 int ret;
643 unsigned long size;
644 u16 count;
645
646 if (get_user(count, &argp->dest_count)) {
647 ret = -EFAULT;
648 goto out;
649 }
650
651 size = offsetof(struct file_dedupe_range __user, info[count]);
Darrick J. Wongb71dbf12016-09-14 20:20:44 -0700652 if (size > PAGE_SIZE) {
653 ret = -ENOMEM;
654 goto out;
655 }
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800656
657 same = memdup_user(argp, size);
658 if (IS_ERR(same)) {
659 ret = PTR_ERR(same);
660 same = NULL;
661 goto out;
662 }
663
Scott Bauer10eec602016-07-27 19:11:29 -0600664 same->dest_count = count;
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800665 ret = vfs_dedupe_file_range(file, same);
666 if (ret)
667 goto out;
668
669 ret = copy_to_user(argp, same, size);
670 if (ret)
671 ret = -EFAULT;
672
673out:
674 kfree(same);
675 return ret;
676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/*
Erez Zadokdeb21db2008-02-07 00:13:25 -0800679 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
Arnd Bergmann77b90402019-11-27 21:25:36 +0100681 *
682 * When you add any new common ioctls to the switches above and below,
683 * please ensure they have compatible arguments in compat mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Arnd Bergmann77b90402019-11-27 21:25:36 +0100685static int do_vfs_ioctl(struct file *filp, unsigned int fd,
686 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Al Viro34d3d0e2019-04-21 18:45:28 -0400688 void __user *argp = (void __user *)arg;
Al Viro496ad9a2013-01-23 17:07:38 -0500689 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800692 case FIOCLEX:
693 set_close_on_exec(fd, 1);
Arnd Bergmann77b90402019-11-27 21:25:36 +0100694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Erez Zadokc9845ff2008-02-07 00:13:23 -0800696 case FIONCLEX:
697 set_close_on_exec(fd, 0);
Arnd Bergmann77b90402019-11-27 21:25:36 +0100698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Erez Zadokc9845ff2008-02-07 00:13:23 -0800700 case FIONBIO:
Arnd Bergmann77b90402019-11-27 21:25:36 +0100701 return ioctl_fionbio(filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800702
703 case FIOASYNC:
Arnd Bergmann77b90402019-11-27 21:25:36 +0100704 return ioctl_fioasync(fd, filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800705
706 case FIOQSIZE:
Namhyung Kim27a4f7e62011-01-17 00:48:17 +0900707 if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) ||
708 S_ISLNK(inode->i_mode)) {
709 loff_t res = inode_get_bytes(inode);
Arnd Bergmann77b90402019-11-27 21:25:36 +0100710 return copy_to_user(argp, &res, sizeof(res)) ?
711 -EFAULT : 0;
712 }
713
714 return -ENOTTY;
Takashi Satofcccf502009-01-09 16:40:59 -0800715
716 case FIFREEZE:
Arnd Bergmann77b90402019-11-27 21:25:36 +0100717 return ioctl_fsfreeze(filp);
Takashi Satofcccf502009-01-09 16:40:59 -0800718
719 case FITHAW:
Arnd Bergmann77b90402019-11-27 21:25:36 +0100720 return ioctl_fsthaw(filp);
Takashi Satofcccf502009-01-09 16:40:59 -0800721
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400722 case FS_IOC_FIEMAP:
Al Viro34d3d0e2019-04-21 18:45:28 -0400723 return ioctl_fiemap(filp, argp);
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400724
725 case FIGETBSZ:
Amir Goldstein8f97d1e2018-10-11 17:38:14 +0300726 /* anon_bdev filesystems may not have a block size */
727 if (!inode->i_sb->s_blocksize)
728 return -EINVAL;
Arnd Bergmann77b90402019-11-27 21:25:36 +0100729
Al Viro34d3d0e2019-04-21 18:45:28 -0400730 return put_user(inode->i_sb->s_blocksize, (int __user *)argp);
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400731
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100732 case FICLONE:
733 return ioctl_file_clone(filp, arg, 0, 0, 0);
734
735 case FICLONERANGE:
736 return ioctl_file_clone_range(filp, argp);
737
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800738 case FIDEDUPERANGE:
739 return ioctl_file_dedupe_range(filp, argp);
740
Arnd Bergmann0a061742020-02-07 17:55:48 +0100741 case FIONREAD:
742 if (!S_ISREG(inode->i_mode))
743 return vfs_ioctl(filp, cmd, arg);
744
745 return put_user(i_size_read(inode) - filp->f_pos,
746 (int __user *)argp);
747
Erez Zadokc9845ff2008-02-07 00:13:23 -0800748 default:
Namhyung Kim27a4f7e62011-01-17 00:48:17 +0900749 if (S_ISREG(inode->i_mode))
Arnd Bergmann77b90402019-11-27 21:25:36 +0100750 return file_ioctl(filp, cmd, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Arnd Bergmann77b90402019-11-27 21:25:36 +0100753
754 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Dominik Brodowskicbb60b92018-03-13 21:43:59 +0100757int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Al Viro2903ff02012-08-28 12:52:22 -0400759 struct fd f = fdget(fd);
Arnd Bergmann77b90402019-11-27 21:25:36 +0100760 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Al Viro2903ff02012-08-28 12:52:22 -0400762 if (!f.file)
763 return -EBADF;
Arnd Bergmann77b90402019-11-27 21:25:36 +0100764
Al Viro2903ff02012-08-28 12:52:22 -0400765 error = security_file_ioctl(f.file, cmd, arg);
Arnd Bergmann77b90402019-11-27 21:25:36 +0100766 if (error)
767 goto out;
768
769 error = do_vfs_ioctl(f.file, fd, cmd, arg);
770 if (error == -ENOIOCTLCMD)
771 error = vfs_ioctl(f.file, cmd, arg);
772
773out:
Al Viro2903ff02012-08-28 12:52:22 -0400774 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return error;
776}
Dominik Brodowskicbb60b92018-03-13 21:43:59 +0100777
778SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
779{
780 return ksys_ioctl(fd, cmd, arg);
781}
Arnd Bergmann2952db02018-09-11 16:55:03 +0200782
783#ifdef CONFIG_COMPAT
784/**
785 * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation
786 *
787 * This is not normally called as a function, but instead set in struct
788 * file_operations as
789 *
790 * .compat_ioctl = compat_ptr_ioctl,
791 *
792 * On most architectures, the compat_ptr_ioctl() just passes all arguments
793 * to the corresponding ->ioctl handler. The exception is arch/s390, where
794 * compat_ptr() clears the top bit of a 32-bit pointer value, so user space
795 * pointers to the second 2GB alias the first 2GB, as is the case for
796 * native 32-bit s390 user space.
797 *
798 * The compat_ptr_ioctl() function must therefore be used only with ioctl
799 * functions that either ignore the argument or pass a pointer to a
800 * compatible data type.
801 *
802 * If any ioctl command handled by fops->unlocked_ioctl passes a plain
803 * integer instead of a pointer, or any of the passed data types
804 * is incompatible between 32-bit and 64-bit architectures, a proper
805 * handler is required instead of compat_ptr_ioctl.
806 */
807long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
808{
809 if (!file->f_op->unlocked_ioctl)
810 return -ENOIOCTLCMD;
811
812 return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
813}
814EXPORT_SYMBOL(compat_ptr_ioctl);
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200815
816COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
Arnd Bergmann77b90402019-11-27 21:25:36 +0100817 compat_ulong_t, arg)
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200818{
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200819 struct fd f = fdget(fd);
Arnd Bergmann77b90402019-11-27 21:25:36 +0100820 int error;
821
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200822 if (!f.file)
Arnd Bergmann77b90402019-11-27 21:25:36 +0100823 return -EBADF;
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200824
825 /* RED-PEN how should LSM module know it's handling 32bit? */
826 error = security_file_ioctl(f.file, cmd, arg);
827 if (error)
Arnd Bergmann77b90402019-11-27 21:25:36 +0100828 goto out;
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200829
830 switch (cmd) {
Arnd Bergmann77b90402019-11-27 21:25:36 +0100831 /* FICLONE takes an int argument, so don't use compat_ptr() */
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200832 case FICLONE:
Arnd Bergmann77b90402019-11-27 21:25:36 +0100833 error = ioctl_file_clone(f.file, arg, 0, 0, 0);
834 break;
835
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200836#if defined(CONFIG_X86_64)
Arnd Bergmann77b90402019-11-27 21:25:36 +0100837 /* these get messy on amd64 due to alignment differences */
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200838 case FS_IOC_RESVSP_32:
839 case FS_IOC_RESVSP64_32:
840 error = compat_ioctl_preallocate(f.file, 0, compat_ptr(arg));
Arnd Bergmann77b90402019-11-27 21:25:36 +0100841 break;
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200842 case FS_IOC_UNRESVSP_32:
843 case FS_IOC_UNRESVSP64_32:
844 error = compat_ioctl_preallocate(f.file, FALLOC_FL_PUNCH_HOLE,
845 compat_ptr(arg));
Arnd Bergmann77b90402019-11-27 21:25:36 +0100846 break;
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200847 case FS_IOC_ZERO_RANGE_32:
848 error = compat_ioctl_preallocate(f.file, FALLOC_FL_ZERO_RANGE,
849 compat_ptr(arg));
Arnd Bergmann77b90402019-11-27 21:25:36 +0100850 break;
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200851#endif
852
Arnd Bergmann77b90402019-11-27 21:25:36 +0100853 /*
854 * everything else in do_vfs_ioctl() takes either a compatible
855 * pointer argument or no argument -- call it with a modified
856 * argument.
857 */
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200858 default:
Arnd Bergmann77b90402019-11-27 21:25:36 +0100859 error = do_vfs_ioctl(f.file, fd, cmd,
860 (unsigned long)compat_ptr(arg));
861 if (error != -ENOIOCTLCMD)
862 break;
863
864 if (f.file->f_op->compat_ioctl)
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200865 error = f.file->f_op->compat_ioctl(f.file, cmd, arg);
Arnd Bergmann77b90402019-11-27 21:25:36 +0100866 if (error == -ENOIOCTLCMD)
867 error = -ENOTTY;
868 break;
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200869 }
870
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200871 out:
Arnd Bergmann77b90402019-11-27 21:25:36 +0100872 fdput(f);
873
Arnd Bergmann2af563d2019-06-07 10:06:42 +0200874 return error;
875}
Arnd Bergmann2952db02018-09-11 16:55:03 +0200876#endif