blob: 2f5e4e5b97e111725a99b6414562d8e6c91b3b61 [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{
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;
Erez Zadokaa81a7c2008-02-07 00:13:25 -080068 res = mapping->a_ops->bmap(mapping, block);
Erez Zadokaa81a7c2008-02-07 00:13:25 -080069 return put_user(res, p);
70}
71
Mark Fashehc4b929b2008-10-08 19:44:18 -040072/**
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)
90int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
91 u64 phys, u64 len, u32 flags)
92{
93 struct fiemap_extent extent;
Namhyung Kimecf56322011-01-16 23:28:17 +090094 struct fiemap_extent __user *dest = fieinfo->fi_extents_start;
Mark Fashehc4b929b2008-10-08 19:44:18 -040095
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}
127EXPORT_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 */
143int 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}
154EXPORT_SYMBOL(fiemap_check_flags);
155
156static int fiemap_check_ranges(struct super_block *sb,
157 u64 start, u64 len, u64 *new_len)
158{
Jeff Layton5aa98b72009-09-18 13:05:50 -0700159 u64 maxbytes = (u64) sb->s_maxbytes;
160
Mark Fashehc4b929b2008-10-08 19:44:18 -0400161 *new_len = len;
162
163 if (len == 0)
164 return -EINVAL;
165
Jeff Layton5aa98b72009-09-18 13:05:50 -0700166 if (start > maxbytes)
Mark Fashehc4b929b2008-10-08 19:44:18 -0400167 return -EFBIG;
168
169 /*
170 * Shrink request scope to what the fs can actually handle.
171 */
Jeff Layton5aa98b72009-09-18 13:05:50 -0700172 if (len > maxbytes || (maxbytes - len) < start)
173 *new_len = maxbytes - start;
Mark Fashehc4b929b2008-10-08 19:44:18 -0400174
175 return 0;
176}
177
Al Viro34d3d0e2019-04-21 18:45:28 -0400178static int ioctl_fiemap(struct file *filp, struct fiemap __user *ufiemap)
Mark Fashehc4b929b2008-10-08 19:44:18 -0400179{
180 struct fiemap fiemap;
181 struct fiemap_extent_info fieinfo = { 0, };
Al Viro496ad9a2013-01-23 17:07:38 -0500182 struct inode *inode = file_inode(filp);
Mark Fashehc4b929b2008-10-08 19:44:18 -0400183 struct super_block *sb = inode->i_sb;
184 u64 len;
185 int error;
186
187 if (!inode->i_op->fiemap)
188 return -EOPNOTSUPP;
189
Namhyung Kimecf56322011-01-16 23:28:17 +0900190 if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
Mark Fashehc4b929b2008-10-08 19:44:18 -0400191 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;
Namhyung Kimecf56322011-01-16 23:28:17 +0900203 fieinfo.fi_extents_start = ufiemap->fm_extents;
Mark Fashehc4b929b2008-10-08 19:44:18 -0400204
205 if (fiemap.fm_extent_count != 0 &&
Linus Torvalds96d4f262019-01-03 18:57:57 -0800206 !access_ok(fieinfo.fi_extents_start,
Mark Fashehc4b929b2008-10-08 19:44:18 -0400207 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;
Namhyung Kimecf56322011-01-16 23:28:17 +0900216 if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
Mark Fashehc4b929b2008-10-08 19:44:18 -0400217 error = -EFAULT;
218
219 return error;
220}
221
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100222static 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);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100226 loff_t cloned;
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100227 int ret;
228
229 if (!src_file.file)
230 return -EBADF;
Amir Goldstein913b86e2016-09-23 11:38:10 +0300231 ret = -EXDEV;
232 if (src_file.file->f_path.mnt != dst_file->f_path.mnt)
233 goto fdput;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100234 cloned = vfs_clone_file_range(src_file.file, off, dst_file, destoff,
Darrick J. Wong452ce652018-10-30 10:41:56 +1100235 olen, 0);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100236 if (cloned < 0)
237 ret = cloned;
238 else if (olen && cloned != olen)
239 ret = -EINVAL;
240 else
241 ret = 0;
Amir Goldstein913b86e2016-09-23 11:38:10 +0300242fdput:
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100243 fdput(src_file);
244 return ret;
245}
246
Al Viro34d3d0e2019-04-21 18:45:28 -0400247static long ioctl_file_clone_range(struct file *file,
248 struct file_clone_range __user *argp)
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100249{
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
Adrian Bunk06270d52008-10-12 07:15:19 +0300258#ifdef CONFIG_BLOCK
259
Josef Bacik3a3076f2010-04-23 12:17:17 -0400260static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
261{
262 return (offset >> inode->i_blkbits);
263}
264
265static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
266{
267 return (blk << inode->i_blkbits);
268}
Josef Bacik68c9d702008-10-03 17:32:43 -0400269
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100270/**
271 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
Josef Bacik3a3076f2010-04-23 12:17:17 -0400272 * @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
Josef Bacik68c9d702008-10-03 17:32:43 -0400277 *
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
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100284 * 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.
Josef Bacik68c9d702008-10-03 17:32:43 -0400288 */
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100289
290int __generic_block_fiemap(struct inode *inode,
Josef Bacik3a3076f2010-04-23 12:17:17 -0400291 struct fiemap_extent_info *fieinfo, loff_t start,
292 loff_t len, get_block_t *get_block)
Josef Bacik68c9d702008-10-03 17:32:43 -0400293{
Josef Bacik3a3076f2010-04-23 12:17:17 -0400294 struct buffer_head map_bh;
295 sector_t start_blk, last_blk;
296 loff_t isize = i_size_read(inode);
Josef Bacik68c9d702008-10-03 17:32:43 -0400297 u64 logical = 0, phys = 0, size = 0;
298 u32 flags = FIEMAP_EXTENT_MERGED;
Josef Bacik3a3076f2010-04-23 12:17:17 -0400299 bool past_eof = false, whole_file = false;
300 int ret = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400301
Josef Bacik3a3076f2010-04-23 12:17:17 -0400302 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
303 if (ret)
Josef Bacik68c9d702008-10-03 17:32:43 -0400304 return ret;
305
Josef Bacik3a3076f2010-04-23 12:17:17 -0400306 /*
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 }
315
Josef Bacikd54cdc82011-02-01 15:52:47 -0800316 /*
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
Josef Bacik68c9d702008-10-03 17:32:43 -0400323 start_blk = logical_to_blk(inode, start);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400324 last_blk = logical_to_blk(inode, start + len - 1);
Josef Bacik68c9d702008-10-03 17:32:43 -0400325
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 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400331 memset(&map_bh, 0, sizeof(struct buffer_head));
332 map_bh.b_size = len;
Josef Bacik68c9d702008-10-03 17:32:43 -0400333
Josef Bacik3a3076f2010-04-23 12:17:17 -0400334 ret = get_block(inode, start_blk, &map_bh, 0);
Josef Bacik68c9d702008-10-03 17:32:43 -0400335 if (ret)
336 break;
337
338 /* HOLE */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400339 if (!buffer_mapped(&map_bh)) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700340 start_blk++;
341
342 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400343 * We want to handle the case where there is an
Josef Bacikdf3935f2009-05-06 16:02:53 -0700344 * 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 &&
Josef Bacik3a3076f2010-04-23 12:17:17 -0400350 blk_to_logical(inode, start_blk) >= isize)
Josef Bacikdf3935f2009-05-06 16:02:53 -0700351 past_eof = 1;
352
Josef Bacik68c9d702008-10-03 17:32:43 -0400353 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400354 * First hole after going past the EOF, this is our
Josef Bacik68c9d702008-10-03 17:32:43 -0400355 * last extent
356 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700357 if (past_eof && size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400358 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
359 ret = fiemap_fill_next_extent(fieinfo, logical,
360 phys, size,
361 flags);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400362 } else if (size) {
363 ret = fiemap_fill_next_extent(fieinfo, logical,
364 phys, size, flags);
365 size = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400366 }
367
Josef Bacik68c9d702008-10-03 17:32:43 -0400368 /* if we have holes up to/past EOF then we're done */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400369 if (start_blk > last_blk || past_eof || ret)
Josef Bacik68c9d702008-10-03 17:32:43 -0400370 break;
Josef Bacik68c9d702008-10-03 17:32:43 -0400371 } else {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700372 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400373 * We have gone over the length of what we wanted to
Josef Bacikdf3935f2009-05-06 16:02:53 -0700374 * 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 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400387 if (start_blk > last_blk && !whole_file) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700388 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) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400399 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);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400407 phys = blk_to_logical(inode, map_bh.b_blocknr);
408 size = map_bh.b_size;
Josef Bacik68c9d702008-10-03 17:32:43 -0400409 flags = FIEMAP_EXTENT_MERGED;
410
Josef Bacik68c9d702008-10-03 17:32:43 -0400411 start_blk += logical_to_blk(inode, size);
412
413 /*
Josef Bacikdf3935f2009-05-06 16:02:53 -0700414 * 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
Josef Bacik68c9d702008-10-03 17:32:43 -0400417 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400418 if (!past_eof && logical + size >= isize)
419 past_eof = true;
Josef Bacik68c9d702008-10-03 17:32:43 -0400420 }
421 cond_resched();
Dmitry Monakhov913e0272015-02-10 14:09:29 -0800422 if (fatal_signal_pending(current)) {
423 ret = -EINTR;
424 break;
425 }
426
Josef Bacik68c9d702008-10-03 17:32:43 -0400427 } while (1);
428
Josef Bacik3a3076f2010-04-23 12:17:17 -0400429 /* If ret is 1 then we just hit the end of the extent array */
Josef Bacik68c9d702008-10-03 17:32:43 -0400430 if (ret == 1)
431 ret = 0;
432
433 return ret;
434}
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100435EXPORT_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
449int 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;
Al Viro59551022016-01-22 15:40:57 -0500454 inode_lock(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100455 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
Al Viro59551022016-01-22 15:40:57 -0500456 inode_unlock(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100457 return ret;
458}
Josef Bacik68c9d702008-10-03 17:32:43 -0400459EXPORT_SYMBOL(generic_block_fiemap);
460
Adrian Bunk06270d52008-10-12 07:15:19 +0300461#endif /* CONFIG_BLOCK */
462
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400463/*
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 */
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700470int ioctl_preallocate(struct file *filp, int mode, void __user *argp)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400471{
Al Viro496ad9a2013-01-23 17:07:38 -0500472 struct inode *inode = file_inode(filp);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400473 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
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700491 return vfs_fallocate(filp, mode | FALLOC_FL_KEEP_SIZE, sr.l_start,
492 sr.l_len);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400493}
494
Al Viro011da442019-04-21 19:24:03 -0400495/* 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 */
Linus Torvalds97eeb4d2019-12-02 14:46:22 -0800498int compat_ioctl_preallocate(struct file *file, int mode,
Al Viro011da442019-04-21 19:24:03 -0400499 struct space_resv_32 __user *argp)
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
Linus Torvalds97eeb4d2019-12-02 14:46:22 -0800520 return vfs_fallocate(file, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
Al Viro011da442019-04-21 19:24:03 -0400521}
522#endif
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524static int file_ioctl(struct file *filp, unsigned int cmd,
525 unsigned long arg)
526{
Al Viro496ad9a2013-01-23 17:07:38 -0500527 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int __user *p = (int __user *)arg;
529
530 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800531 case FIBMAP:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800532 return ioctl_fibmap(filp, p);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800533 case FIONREAD:
534 return put_user(i_size_read(inode) - filp->f_pos, p);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400535 case FS_IOC_RESVSP:
536 case FS_IOC_RESVSP64:
Christoph Hellwig837a6e72019-10-24 22:26:02 -0700537 return ioctl_preallocate(filp, 0, p);
538 case FS_IOC_UNRESVSP:
539 case FS_IOC_UNRESVSP64:
540 return ioctl_preallocate(filp, FALLOC_FL_PUNCH_HOLE, p);
541 case FS_IOC_ZERO_RANGE:
542 return ioctl_preallocate(filp, FALLOC_FL_ZERO_RANGE, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
Erez Zadokdeb21db2008-02-07 00:13:25 -0800545 return vfs_ioctl(filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800548static int ioctl_fionbio(struct file *filp, int __user *argp)
549{
550 unsigned int flag;
551 int on, error;
552
553 error = get_user(on, argp);
554 if (error)
555 return error;
556 flag = O_NONBLOCK;
557#ifdef __sparc__
558 /* SunOS compatibility item. */
559 if (O_NONBLOCK != O_NDELAY)
560 flag |= O_NDELAY;
561#endif
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700562 spin_lock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800563 if (on)
564 filp->f_flags |= flag;
565 else
566 filp->f_flags &= ~flag;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700567 spin_unlock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800568 return error;
569}
570
571static int ioctl_fioasync(unsigned int fd, struct file *filp,
572 int __user *argp)
573{
574 unsigned int flag;
575 int on, error;
576
577 error = get_user(on, argp);
578 if (error)
579 return error;
580 flag = on ? FASYNC : 0;
581
582 /* Did FASYNC state change ? */
583 if ((flag ^ filp->f_flags) & FASYNC) {
Al Viro72c2d532013-09-22 16:27:52 -0400584 if (filp->f_op->fasync)
Jonathan Corbet76398422009-02-01 14:26:59 -0700585 /* fasync() adjusts filp->f_flags */
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800586 error = filp->f_op->fasync(fd, filp, on);
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700587 else
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800588 error = -ENOTTY;
589 }
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700590 return error < 0 ? error : 0;
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800591}
592
Takashi Satofcccf502009-01-09 16:40:59 -0800593static int ioctl_fsfreeze(struct file *filp)
594{
Al Viro496ad9a2013-01-23 17:07:38 -0500595 struct super_block *sb = file_inode(filp)->i_sb;
Takashi Satofcccf502009-01-09 16:40:59 -0800596
Seth Forsheef3f1a182015-02-15 14:35:35 -0600597 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
Takashi Satofcccf502009-01-09 16:40:59 -0800598 return -EPERM;
599
600 /* If filesystem doesn't support freeze feature, return. */
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600601 if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL)
Takashi Satofcccf502009-01-09 16:40:59 -0800602 return -EOPNOTSUPP;
603
Takashi Satofcccf502009-01-09 16:40:59 -0800604 /* Freeze */
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600605 if (sb->s_op->freeze_super)
606 return sb->s_op->freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400607 return freeze_super(sb);
Takashi Satofcccf502009-01-09 16:40:59 -0800608}
609
610static int ioctl_fsthaw(struct file *filp)
611{
Al Viro496ad9a2013-01-23 17:07:38 -0500612 struct super_block *sb = file_inode(filp)->i_sb;
Takashi Satofcccf502009-01-09 16:40:59 -0800613
Seth Forsheef3f1a182015-02-15 14:35:35 -0600614 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
Takashi Satofcccf502009-01-09 16:40:59 -0800615 return -EPERM;
616
Takashi Satofcccf502009-01-09 16:40:59 -0800617 /* Thaw */
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600618 if (sb->s_op->thaw_super)
619 return sb->s_op->thaw_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400620 return thaw_super(sb);
Takashi Satofcccf502009-01-09 16:40:59 -0800621}
622
Al Viro34d3d0e2019-04-21 18:45:28 -0400623static int ioctl_file_dedupe_range(struct file *file,
624 struct file_dedupe_range __user *argp)
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800625{
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800626 struct file_dedupe_range *same = NULL;
627 int ret;
628 unsigned long size;
629 u16 count;
630
631 if (get_user(count, &argp->dest_count)) {
632 ret = -EFAULT;
633 goto out;
634 }
635
636 size = offsetof(struct file_dedupe_range __user, info[count]);
Darrick J. Wongb71dbf12016-09-14 20:20:44 -0700637 if (size > PAGE_SIZE) {
638 ret = -ENOMEM;
639 goto out;
640 }
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800641
642 same = memdup_user(argp, size);
643 if (IS_ERR(same)) {
644 ret = PTR_ERR(same);
645 same = NULL;
646 goto out;
647 }
648
Scott Bauer10eec602016-07-27 19:11:29 -0600649 same->dest_count = count;
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800650 ret = vfs_dedupe_file_range(file, same);
651 if (ret)
652 goto out;
653
654 ret = copy_to_user(argp, same, size);
655 if (ret)
656 ret = -EFAULT;
657
658out:
659 kfree(same);
660 return ret;
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/*
664 * When you add any new common ioctls to the switches above and below
665 * please update compat_sys_ioctl() too.
666 *
Erez Zadokdeb21db2008-02-07 00:13:25 -0800667 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
669 */
Erez Zadokdeb21db2008-02-07 00:13:25 -0800670int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
671 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800673 int error = 0;
Al Viro34d3d0e2019-04-21 18:45:28 -0400674 void __user *argp = (void __user *)arg;
Al Viro496ad9a2013-01-23 17:07:38 -0500675 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800678 case FIOCLEX:
679 set_close_on_exec(fd, 1);
680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Erez Zadokc9845ff2008-02-07 00:13:23 -0800682 case FIONCLEX:
683 set_close_on_exec(fd, 0);
684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Erez Zadokc9845ff2008-02-07 00:13:23 -0800686 case FIONBIO:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800687 error = ioctl_fionbio(filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800688 break;
689
690 case FIOASYNC:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800691 error = ioctl_fioasync(fd, filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800692 break;
693
694 case FIOQSIZE:
Namhyung Kim27a4f7e62011-01-17 00:48:17 +0900695 if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) ||
696 S_ISLNK(inode->i_mode)) {
697 loff_t res = inode_get_bytes(inode);
698 error = copy_to_user(argp, &res, sizeof(res)) ?
699 -EFAULT : 0;
Erez Zadokc9845ff2008-02-07 00:13:23 -0800700 } else
701 error = -ENOTTY;
702 break;
Takashi Satofcccf502009-01-09 16:40:59 -0800703
704 case FIFREEZE:
705 error = ioctl_fsfreeze(filp);
706 break;
707
708 case FITHAW:
709 error = ioctl_fsthaw(filp);
710 break;
711
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400712 case FS_IOC_FIEMAP:
Al Viro34d3d0e2019-04-21 18:45:28 -0400713 return ioctl_fiemap(filp, argp);
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400714
715 case FIGETBSZ:
Amir Goldstein8f97d1e2018-10-11 17:38:14 +0300716 /* anon_bdev filesystems may not have a block size */
717 if (!inode->i_sb->s_blocksize)
718 return -EINVAL;
Al Viro34d3d0e2019-04-21 18:45:28 -0400719 return put_user(inode->i_sb->s_blocksize, (int __user *)argp);
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400720
Christoph Hellwig04b38d62015-12-03 12:59:50 +0100721 case FICLONE:
722 return ioctl_file_clone(filp, arg, 0, 0, 0);
723
724 case FICLONERANGE:
725 return ioctl_file_clone_range(filp, argp);
726
Darrick J. Wong54dbc152015-12-19 00:55:59 -0800727 case FIDEDUPERANGE:
728 return ioctl_file_dedupe_range(filp, argp);
729
Erez Zadokc9845ff2008-02-07 00:13:23 -0800730 default:
Namhyung Kim27a4f7e62011-01-17 00:48:17 +0900731 if (S_ISREG(inode->i_mode))
Erez Zadokc9845ff2008-02-07 00:13:23 -0800732 error = file_ioctl(filp, cmd, arg);
733 else
Erez Zadokdeb21db2008-02-07 00:13:25 -0800734 error = vfs_ioctl(filp, cmd, arg);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 return error;
738}
739
Dominik Brodowskicbb60b92018-03-13 21:43:59 +0100740int ksys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Al Viro2903ff02012-08-28 12:52:22 -0400742 int error;
743 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Al Viro2903ff02012-08-28 12:52:22 -0400745 if (!f.file)
746 return -EBADF;
747 error = security_file_ioctl(f.file, cmd, arg);
748 if (!error)
749 error = do_vfs_ioctl(f.file, fd, cmd, arg);
750 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return error;
752}
Dominik Brodowskicbb60b92018-03-13 21:43:59 +0100753
754SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
755{
756 return ksys_ioctl(fd, cmd, arg);
757}
Arnd Bergmann2952db02018-09-11 16:55:03 +0200758
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 */
783long 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}
790EXPORT_SYMBOL(compat_ptr_ioctl);
791#endif