blob: 0a1d10ac2e1a59d929ac9ae0946c84484cfeabd0 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Randy.Dunlapc59ede72006-01-11 12:17:46 -08002#include <linux/capability.h>
Arnd Bergmannee6a1292019-11-28 15:48:10 +01003#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/blkdev.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -04005#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/blkpg.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08008#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/backing-dev.h>
Al Viroff01bb42011-09-16 02:31:11 -040010#include <linux/fs.h>
Jens Axboe2056a782006-03-23 20:00:26 +010011#include <linux/blktrace_api.h>
Christoph Hellwigbbd3e062015-10-15 14:10:48 +020012#include <linux/pr.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080013#include <linux/uaccess.h>
Christoph Hellwig581e2602020-03-25 16:48:41 +010014#include "blk.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010016static int blkpg_do_ioctl(struct block_device *bdev,
17 struct blkpg_partition __user *upart, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Christoph Hellwig7f6be372021-08-10 17:45:10 +020019 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 struct blkpg_partition p;
21 long long start, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 if (!capable(CAP_SYS_ADMIN))
24 return -EACCES;
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010025 if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 return -EFAULT;
Christoph Hellwigfa01b1e2020-09-03 07:40:57 +020027 if (bdev_is_partition(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return -EINVAL;
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020029
30 if (p.pno <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return -EINVAL;
Tejun Heo88e34122008-08-25 19:30:16 +090032
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020033 if (op == BLKPG_DEL_PARTITION)
Christoph Hellwig926fbb12021-08-10 17:45:11 +020034 return bdev_del_partition(disk, p.pno);
Tejun Heo88e34122008-08-25 19:30:16 +090035
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020036 start = p.start >> SECTOR_SHIFT;
37 length = p.length >> SECTOR_SHIFT;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020038
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020039 switch (op) {
40 case BLKPG_ADD_PARTITION:
41 /* check if partition is aligned to blocksize */
42 if (p.start & (bdev_logical_block_size(bdev) - 1))
43 return -EINVAL;
Christoph Hellwig7f6be372021-08-10 17:45:10 +020044 return bdev_add_partition(disk, p.pno, start, length);
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020045 case BLKPG_RESIZE_PARTITION:
Christoph Hellwig3d2e7982021-08-10 17:45:12 +020046 return bdev_resize_partition(disk, p.pno, start, length);
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020047 default:
48 return -EINVAL;
49 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010052static int blkpg_ioctl(struct block_device *bdev,
53 struct blkpg_ioctl_arg __user *arg)
54{
55 struct blkpg_partition __user *udata;
56 int op;
57
58 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
59 return -EFAULT;
60
61 return blkpg_do_ioctl(bdev, udata, op);
62}
63
64#ifdef CONFIG_COMPAT
65struct compat_blkpg_ioctl_arg {
66 compat_int_t op;
67 compat_int_t flags;
68 compat_int_t datalen;
69 compat_caddr_t data;
70};
71
72static int compat_blkpg_ioctl(struct block_device *bdev,
73 struct compat_blkpg_ioctl_arg __user *arg)
74{
75 compat_caddr_t udata;
76 int op;
77
78 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
79 return -EFAULT;
80
81 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
82}
83#endif
84
Christoph Hellwig4601b4b2021-02-23 16:18:22 +010085static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Christoph Hellwig4601b4b2021-02-23 16:18:22 +010087 struct block_device *tmp;
Christoph Hellwigf0b870d2019-11-14 15:34:36 +010088
Christoph Hellwigfa01b1e2020-09-03 07:40:57 +020089 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EINVAL;
91 if (!capable(CAP_SYS_ADMIN))
92 return -EACCES;
Christoph Hellwigab4b5702021-05-25 08:12:59 +020093 if (bdev->bd_disk->open_partitions)
Christoph Hellwig68e65822021-04-21 18:05:02 +020094 return -EBUSY;
Jarod Wilsonbe324172015-05-06 12:26:22 +080095
Christoph Hellwig4601b4b2021-02-23 16:18:22 +010096 /*
97 * Reopen the device to revalidate the driver state and force a
98 * partition rescan.
99 */
100 mode &= ~FMODE_EXCL;
101 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800102
Christoph Hellwig4601b4b2021-02-23 16:18:22 +0100103 tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
104 if (IS_ERR(tmp))
105 return PTR_ERR(tmp);
106 blkdev_put(tmp, mode);
107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200110static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
111 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100112{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200113 uint64_t range[2];
114 uint64_t start, len;
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600115 struct request_queue *q = bdev_get_queue(bdev);
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900116 struct inode *inode = bdev->bd_inode;
Jan Kara384d87e2020-09-04 10:58:52 +0200117 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200118
119 if (!(mode & FMODE_WRITE))
120 return -EBADF;
121
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600122 if (!blk_queue_discard(q))
123 return -EOPNOTSUPP;
124
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200125 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
126 return -EFAULT;
127
128 start = range[0];
129 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700130
David Woodhoused30a2602008-08-11 15:58:42 +0100131 if (start & 511)
132 return -EINVAL;
133 if (len & 511)
134 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100135
Christoph Hellwig946e9932021-10-19 08:20:22 +0200136 if (start + len > bdev_nr_bytes(bdev))
David Woodhoused30a2602008-08-11 15:58:42 +0100137 return -EINVAL;
Jan Kara384d87e2020-09-04 10:58:52 +0200138
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900139 filemap_invalidate_lock(inode->i_mapping);
Jan Kara384d87e2020-09-04 10:58:52 +0200140 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
141 if (err)
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900142 goto fail;
Jan Kara384d87e2020-09-04 10:58:52 +0200143
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900144 err = blkdev_issue_discard(bdev, start >> 9, len >> 9,
145 GFP_KERNEL, flags);
146
147fail:
148 filemap_invalidate_unlock(inode->i_mapping);
149 return err;
David Woodhoused30a2602008-08-11 15:58:42 +0100150}
151
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200152static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
153 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400154{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200155 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700156 uint64_t start, end, len;
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900157 struct inode *inode = bdev->bd_inode;
Jan Kara384d87e2020-09-04 10:58:52 +0200158 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200159
160 if (!(mode & FMODE_WRITE))
161 return -EBADF;
162
163 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
164 return -EFAULT;
165
166 start = range[0];
167 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700168 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200169
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400170 if (start & 511)
171 return -EINVAL;
172 if (len & 511)
173 return -EINVAL;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200174 if (end >= (uint64_t)bdev_nr_bytes(bdev))
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700175 return -EINVAL;
176 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400177 return -EINVAL;
178
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700179 /* Invalidate the page cache, including dirty pages */
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900180 filemap_invalidate_lock(inode->i_mapping);
Jan Kara384d87e2020-09-04 10:58:52 +0200181 err = truncate_bdev_range(bdev, mode, start, end);
182 if (err)
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900183 goto fail;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700184
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900185 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
186 BLKDEV_ZERO_NOUNMAP);
187
188fail:
189 filemap_invalidate_unlock(inode->i_mapping);
190 return err;
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400191}
192
Arnd Bergmann9b816482019-11-29 11:48:31 +0100193static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100195 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Arnd Bergmann9b816482019-11-29 11:48:31 +0100198static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100200 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Arnd Bergmann9b816482019-11-29 11:48:31 +0100203static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200204{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100205 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200206}
207
Arnd Bergmann9b816482019-11-29 11:48:31 +0100208static int put_long(long __user *argp, long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100210 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Arnd Bergmann9b816482019-11-29 11:48:31 +0100213static int put_ulong(unsigned long __user *argp, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100215 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Arnd Bergmann9b816482019-11-29 11:48:31 +0100218static int put_u64(u64 __user *argp, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100220 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100223#ifdef CONFIG_COMPAT
Bart Van Asschec8210a52020-05-18 21:07:34 -0700224static int compat_put_long(compat_long_t __user *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100225{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100226 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100227}
228
Bart Van Asschec8210a52020-05-18 21:07:34 -0700229static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100230{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100231 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100232}
233#endif
234
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100235#ifdef CONFIG_COMPAT
236/*
237 * This is the equivalent of compat_ptr_ioctl(), to be used by block
238 * drivers that implement only commands that are completely compatible
239 * between 32-bit and 64-bit user space
240 */
241int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
242 unsigned cmd, unsigned long arg)
243{
244 struct gendisk *disk = bdev->bd_disk;
245
246 if (disk->fops->ioctl)
247 return disk->fops->ioctl(bdev, mode, cmd,
248 (unsigned long)compat_ptr(arg));
249
250 return -ENOIOCTLCMD;
251}
252EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
253#endif
254
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200255static int blkdev_pr_register(struct block_device *bdev,
256 struct pr_registration __user *arg)
257{
258 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
259 struct pr_registration reg;
260
261 if (!capable(CAP_SYS_ADMIN))
262 return -EPERM;
263 if (!ops || !ops->pr_register)
264 return -EOPNOTSUPP;
265 if (copy_from_user(&reg, arg, sizeof(reg)))
266 return -EFAULT;
267
268 if (reg.flags & ~PR_FL_IGNORE_KEY)
269 return -EOPNOTSUPP;
270 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
271}
272
273static int blkdev_pr_reserve(struct block_device *bdev,
274 struct pr_reservation __user *arg)
275{
276 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
277 struct pr_reservation rsv;
278
279 if (!capable(CAP_SYS_ADMIN))
280 return -EPERM;
281 if (!ops || !ops->pr_reserve)
282 return -EOPNOTSUPP;
283 if (copy_from_user(&rsv, arg, sizeof(rsv)))
284 return -EFAULT;
285
286 if (rsv.flags & ~PR_FL_IGNORE_KEY)
287 return -EOPNOTSUPP;
288 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
289}
290
291static int blkdev_pr_release(struct block_device *bdev,
292 struct pr_reservation __user *arg)
293{
294 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
295 struct pr_reservation rsv;
296
297 if (!capable(CAP_SYS_ADMIN))
298 return -EPERM;
299 if (!ops || !ops->pr_release)
300 return -EOPNOTSUPP;
301 if (copy_from_user(&rsv, arg, sizeof(rsv)))
302 return -EFAULT;
303
304 if (rsv.flags)
305 return -EOPNOTSUPP;
306 return ops->pr_release(bdev, rsv.key, rsv.type);
307}
308
309static int blkdev_pr_preempt(struct block_device *bdev,
310 struct pr_preempt __user *arg, bool abort)
311{
312 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
313 struct pr_preempt p;
314
315 if (!capable(CAP_SYS_ADMIN))
316 return -EPERM;
317 if (!ops || !ops->pr_preempt)
318 return -EOPNOTSUPP;
319 if (copy_from_user(&p, arg, sizeof(p)))
320 return -EFAULT;
321
322 if (p.flags)
323 return -EOPNOTSUPP;
324 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
325}
326
327static int blkdev_pr_clear(struct block_device *bdev,
328 struct pr_clear __user *arg)
329{
330 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
331 struct pr_clear c;
332
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
335 if (!ops || !ops->pr_clear)
336 return -EOPNOTSUPP;
337 if (copy_from_user(&c, arg, sizeof(c)))
338 return -EFAULT;
339
340 if (c.flags)
341 return -EOPNOTSUPP;
342 return ops->pr_clear(bdev, c.key);
343}
344
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200345static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
346 unsigned cmd, unsigned long arg)
347{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200348 if (!capable(CAP_SYS_ADMIN))
349 return -EACCES;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200350 fsync_bdev(bdev);
351 invalidate_bdev(bdev);
352 return 0;
353}
354
355static int blkdev_roset(struct block_device *bdev, fmode_t mode,
356 unsigned cmd, unsigned long arg)
357{
358 int ret, n;
359
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200360 if (!capable(CAP_SYS_ADMIN))
361 return -EACCES;
362
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200363 if (get_user(n, (int __user *)arg))
364 return -EFAULT;
Christoph Hellwige00adca2020-11-03 11:00:11 +0100365 if (bdev->bd_disk->fops->set_read_only) {
366 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
367 if (ret)
368 return ret;
369 }
Christoph Hellwig83950d32020-11-23 16:36:02 +0100370 bdev->bd_read_only = n;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200371 return 0;
372}
373
374static int blkdev_getgeo(struct block_device *bdev,
375 struct hd_geometry __user *argp)
376{
377 struct gendisk *disk = bdev->bd_disk;
378 struct hd_geometry geo;
379 int ret;
380
381 if (!argp)
382 return -EINVAL;
383 if (!disk->fops->getgeo)
384 return -ENOTTY;
385
386 /*
387 * We need to set the startsect first, the driver may
388 * want to override it.
389 */
390 memset(&geo, 0, sizeof(geo));
391 geo.start = get_start_sect(bdev);
392 ret = disk->fops->getgeo(bdev, &geo);
393 if (ret)
394 return ret;
395 if (copy_to_user(argp, &geo, sizeof(geo)))
396 return -EFAULT;
397 return 0;
398}
399
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100400#ifdef CONFIG_COMPAT
401struct compat_hd_geometry {
402 unsigned char heads;
403 unsigned char sectors;
404 unsigned short cylinders;
405 u32 start;
406};
407
Arnd Bergmann9b816482019-11-29 11:48:31 +0100408static int compat_hdio_getgeo(struct block_device *bdev,
409 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100410{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100411 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100412 struct hd_geometry geo;
413 int ret;
414
415 if (!ugeo)
416 return -EINVAL;
417 if (!disk->fops->getgeo)
418 return -ENOTTY;
419
420 memset(&geo, 0, sizeof(geo));
421 /*
422 * We need to set the startsect first, the driver may
423 * want to override it.
424 */
425 geo.start = get_start_sect(bdev);
426 ret = disk->fops->getgeo(bdev, &geo);
427 if (ret)
428 return ret;
429
430 ret = copy_to_user(ugeo, &geo, 4);
431 ret |= put_user(geo.start, &ugeo->start);
432 if (ret)
433 ret = -EFAULT;
434
435 return ret;
436}
437#endif
438
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200439/* set the logical block size */
440static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
441 int __user *argp)
442{
443 int ret, n;
444
445 if (!capable(CAP_SYS_ADMIN))
446 return -EACCES;
447 if (!argp)
448 return -EINVAL;
449 if (get_user(n, argp))
450 return -EFAULT;
451
Christoph Hellwig47816282020-09-21 09:19:47 +0200452 if (mode & FMODE_EXCL)
453 return set_blocksize(bdev, n);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200454
Christoph Hellwig47816282020-09-21 09:19:47 +0200455 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
456 return -EBUSY;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200457 ret = set_blocksize(bdev, n);
Christoph Hellwig47816282020-09-21 09:19:47 +0200458 blkdev_put(bdev, mode | FMODE_EXCL);
459
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200460 return ret;
461}
462
Linus Torvalds07d106d2012-01-05 15:40:12 -0800463/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100464 * Common commands that are handled the same way on native and compat
465 * user space. Note the separate arg/argp parameters that are needed
466 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200467 */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100468static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
469 unsigned cmd, unsigned long arg, void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700470{
Akinobu Mita63f26492014-05-25 21:43:33 +0900471 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700472
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200473 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200475 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200477 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700478 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200479 return blk_ioctl_discard(bdev, mode, arg, 0);
480 case BLKSECDISCARD:
481 return blk_ioctl_discard(bdev, mode, arg,
482 BLKDEV_DISCARD_SECURE);
483 case BLKZEROOUT:
484 return blk_ioctl_zeroout(bdev, mode, arg);
Matteo Croce7957d932021-07-13 01:05:27 +0200485 case BLKGETDISKSEQ:
486 return put_u64(argp, bdev->bd_disk->diskseq);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900487 case BLKREPORTZONE:
488 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
489 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900490 case BLKOPENZONE:
491 case BLKCLOSEZONE:
492 case BLKFINISHZONE:
493 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900494 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100495 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900496 case BLKGETNRZONES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100497 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
Al Viro45048d02008-09-18 15:53:24 -0400498 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100499 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200500 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100501 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200502 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100503 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200504 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100505 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200506 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100507 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200508 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100509 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100510 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100511 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400512 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900513 max_sectors = min_t(unsigned int, USHRT_MAX,
514 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100515 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100516 case BLKROTATIONAL:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100517 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400518 case BLKRASET:
519 case BLKFRASET:
520 if(!capable(CAP_SYS_ADMIN))
521 return -EACCES;
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200522 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400523 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400524 case BLKRRPART:
Christoph Hellwig4601b4b2021-02-23 16:18:22 +0100525 return blkdev_reread_part(bdev, mode);
Al Viro45048d02008-09-18 15:53:24 -0400526 case BLKTRACESTART:
527 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400528 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200529 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200530 case IOC_PR_REGISTER:
531 return blkdev_pr_register(bdev, argp);
532 case IOC_PR_RESERVE:
533 return blkdev_pr_reserve(bdev, argp);
534 case IOC_PR_RELEASE:
535 return blkdev_pr_release(bdev, argp);
536 case IOC_PR_PREEMPT:
537 return blkdev_pr_preempt(bdev, argp, false);
538 case IOC_PR_PREEMPT_ABORT:
539 return blkdev_pr_preempt(bdev, argp, true);
540 case IOC_PR_CLEAR:
541 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400542 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100543 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100546
547/*
548 * Always keep this in sync with compat_blkdev_ioctl()
549 * to handle all incompatible commands in both functions.
550 *
551 * New commands must be compatible and go into blkdev_common_ioctl
552 */
Christoph Hellwig8a709512021-10-12 12:44:50 +0200553long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Arnd Bergmann9b816482019-11-29 11:48:31 +0100554{
Christoph Hellwig8a709512021-10-12 12:44:50 +0200555 struct block_device *bdev = I_BDEV(file->f_mapping->host);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100556 void __user *argp = (void __user *)arg;
Christoph Hellwig8a709512021-10-12 12:44:50 +0200557 fmode_t mode = file->f_mode;
Christoph Hellwig8a709512021-10-12 12:44:50 +0200558 int ret;
559
560 /*
561 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
562 * to updated it before every ioctl.
563 */
564 if (file->f_flags & O_NDELAY)
565 mode |= FMODE_NDELAY;
566 else
567 mode &= ~FMODE_NDELAY;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100568
569 switch (cmd) {
570 /* These need separate implementations for the data structure */
571 case HDIO_GETGEO:
572 return blkdev_getgeo(bdev, argp);
573 case BLKPG:
574 return blkpg_ioctl(bdev, argp);
575
576 /* Compat mode returns 32-bit data instead of 'long' */
577 case BLKRAGET:
578 case BLKFRAGET:
579 if (!argp)
580 return -EINVAL;
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200581 return put_long(argp,
582 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100583 case BLKGETSIZE:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200584 if (bdev_nr_sectors(bdev) > ~0UL)
Arnd Bergmann9b816482019-11-29 11:48:31 +0100585 return -EFBIG;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200586 return put_ulong(argp, bdev_nr_sectors(bdev));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100587
588 /* The data is compatible, but the command number is different */
589 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
590 return put_int(argp, block_size(bdev));
591 case BLKBSZSET:
592 return blkdev_bszset(bdev, mode, argp);
593 case BLKGETSIZE64:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200594 return put_u64(argp, bdev_nr_bytes(bdev));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100595
596 /* Incompatible alignment on i386 */
597 case BLKTRACESETUP:
598 return blk_trace_ioctl(bdev, cmd, argp);
599 default:
600 break;
601 }
602
603 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
Christoph Hellwiga7cb3d22020-11-03 11:00:18 +0100604 if (ret != -ENOIOCTLCMD)
605 return ret;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100606
Christoph Hellwiga7cb3d22020-11-03 11:00:18 +0100607 if (!bdev->bd_disk->fops->ioctl)
608 return -ENOTTY;
609 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100610}
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100611
612#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100613
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100614#define BLKBSZGET_32 _IOR(0x12, 112, int)
615#define BLKBSZSET_32 _IOW(0x12, 113, int)
616#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
617
618/* Most of the generic ioctls are handled in the normal fallback path.
619 This assumes the blkdev's low level compat_ioctl always returns
620 ENOIOCTLCMD for unknown ioctls. */
621long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
622{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100623 int ret;
624 void __user *argp = compat_ptr(arg);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100625 struct block_device *bdev = I_BDEV(file->f_mapping->host);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100626 struct gendisk *disk = bdev->bd_disk;
627 fmode_t mode = file->f_mode;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100628
629 /*
630 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
631 * to updated it before every ioctl.
632 */
633 if (file->f_flags & O_NDELAY)
634 mode |= FMODE_NDELAY;
635 else
636 mode &= ~FMODE_NDELAY;
637
638 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100639 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100640 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100641 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100642 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100643 return compat_blkpg_ioctl(bdev, argp);
644
645 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100646 case BLKRAGET:
647 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100648 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100649 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100650 return compat_put_long(argp,
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200651 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100652 case BLKGETSIZE:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200653 if (bdev_nr_sectors(bdev) > ~0UL)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100654 return -EFBIG;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200655 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100656
Arnd Bergmann9b816482019-11-29 11:48:31 +0100657 /* The data is compatible, but the command number is different */
658 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
659 return put_int(argp, bdev_logical_block_size(bdev));
660 case BLKBSZSET_32:
661 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100662 case BLKGETSIZE64_32:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200663 return put_u64(argp, bdev_nr_bytes(bdev));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100664
Arnd Bergmann9b816482019-11-29 11:48:31 +0100665 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100666 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100667 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100668 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100669 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100670 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100671
672 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
673 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
674 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
675
676 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100677}
678#endif