blob: eb0491e90b9a0c07a552049a183537b189289005 [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);
Jan Kara384d87e2020-09-04 10:58:52 +0200116 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200117
118 if (!(mode & FMODE_WRITE))
119 return -EBADF;
120
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600121 if (!blk_queue_discard(q))
122 return -EOPNOTSUPP;
123
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200124 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
125 return -EFAULT;
126
127 start = range[0];
128 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700129
David Woodhoused30a2602008-08-11 15:58:42 +0100130 if (start & 511)
131 return -EINVAL;
132 if (len & 511)
133 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100134
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600135 if (start + len > i_size_read(bdev->bd_inode))
David Woodhoused30a2602008-08-11 15:58:42 +0100136 return -EINVAL;
Jan Kara384d87e2020-09-04 10:58:52 +0200137
138 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
139 if (err)
140 return err;
141
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600142 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
143 GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100144}
145
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200146static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
147 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400148{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200149 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700150 uint64_t start, end, len;
Jan Kara384d87e2020-09-04 10:58:52 +0200151 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200152
153 if (!(mode & FMODE_WRITE))
154 return -EBADF;
155
156 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
157 return -EFAULT;
158
159 start = range[0];
160 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700161 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200162
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400163 if (start & 511)
164 return -EINVAL;
165 if (len & 511)
166 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700167 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
168 return -EINVAL;
169 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400170 return -EINVAL;
171
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700172 /* Invalidate the page cache, including dirty pages */
Jan Kara384d87e2020-09-04 10:58:52 +0200173 err = truncate_bdev_range(bdev, mode, start, end);
174 if (err)
175 return err;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700176
177 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
Christoph Hellwigee472d82017-04-05 19:21:08 +0200178 BLKDEV_ZERO_NOUNMAP);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400179}
180
Arnd Bergmann9b816482019-11-29 11:48:31 +0100181static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100183 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Arnd Bergmann9b816482019-11-29 11:48:31 +0100186static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100188 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Arnd Bergmann9b816482019-11-29 11:48:31 +0100191static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200192{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100193 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200194}
195
Arnd Bergmann9b816482019-11-29 11:48:31 +0100196static int put_long(long __user *argp, long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100198 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Arnd Bergmann9b816482019-11-29 11:48:31 +0100201static int put_ulong(unsigned long __user *argp, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100203 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Arnd Bergmann9b816482019-11-29 11:48:31 +0100206static int put_u64(u64 __user *argp, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100208 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100211#ifdef CONFIG_COMPAT
Bart Van Asschec8210a52020-05-18 21:07:34 -0700212static int compat_put_long(compat_long_t __user *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100213{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100214 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100215}
216
Bart Van Asschec8210a52020-05-18 21:07:34 -0700217static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100218{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100219 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100220}
221#endif
222
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100223#ifdef CONFIG_COMPAT
224/*
225 * This is the equivalent of compat_ptr_ioctl(), to be used by block
226 * drivers that implement only commands that are completely compatible
227 * between 32-bit and 64-bit user space
228 */
229int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
230 unsigned cmd, unsigned long arg)
231{
232 struct gendisk *disk = bdev->bd_disk;
233
234 if (disk->fops->ioctl)
235 return disk->fops->ioctl(bdev, mode, cmd,
236 (unsigned long)compat_ptr(arg));
237
238 return -ENOIOCTLCMD;
239}
240EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
241#endif
242
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200243static int blkdev_pr_register(struct block_device *bdev,
244 struct pr_registration __user *arg)
245{
246 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
247 struct pr_registration reg;
248
249 if (!capable(CAP_SYS_ADMIN))
250 return -EPERM;
251 if (!ops || !ops->pr_register)
252 return -EOPNOTSUPP;
253 if (copy_from_user(&reg, arg, sizeof(reg)))
254 return -EFAULT;
255
256 if (reg.flags & ~PR_FL_IGNORE_KEY)
257 return -EOPNOTSUPP;
258 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
259}
260
261static int blkdev_pr_reserve(struct block_device *bdev,
262 struct pr_reservation __user *arg)
263{
264 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
265 struct pr_reservation rsv;
266
267 if (!capable(CAP_SYS_ADMIN))
268 return -EPERM;
269 if (!ops || !ops->pr_reserve)
270 return -EOPNOTSUPP;
271 if (copy_from_user(&rsv, arg, sizeof(rsv)))
272 return -EFAULT;
273
274 if (rsv.flags & ~PR_FL_IGNORE_KEY)
275 return -EOPNOTSUPP;
276 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
277}
278
279static int blkdev_pr_release(struct block_device *bdev,
280 struct pr_reservation __user *arg)
281{
282 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
283 struct pr_reservation rsv;
284
285 if (!capable(CAP_SYS_ADMIN))
286 return -EPERM;
287 if (!ops || !ops->pr_release)
288 return -EOPNOTSUPP;
289 if (copy_from_user(&rsv, arg, sizeof(rsv)))
290 return -EFAULT;
291
292 if (rsv.flags)
293 return -EOPNOTSUPP;
294 return ops->pr_release(bdev, rsv.key, rsv.type);
295}
296
297static int blkdev_pr_preempt(struct block_device *bdev,
298 struct pr_preempt __user *arg, bool abort)
299{
300 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
301 struct pr_preempt p;
302
303 if (!capable(CAP_SYS_ADMIN))
304 return -EPERM;
305 if (!ops || !ops->pr_preempt)
306 return -EOPNOTSUPP;
307 if (copy_from_user(&p, arg, sizeof(p)))
308 return -EFAULT;
309
310 if (p.flags)
311 return -EOPNOTSUPP;
312 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
313}
314
315static int blkdev_pr_clear(struct block_device *bdev,
316 struct pr_clear __user *arg)
317{
318 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
319 struct pr_clear c;
320
321 if (!capable(CAP_SYS_ADMIN))
322 return -EPERM;
323 if (!ops || !ops->pr_clear)
324 return -EOPNOTSUPP;
325 if (copy_from_user(&c, arg, sizeof(c)))
326 return -EFAULT;
327
328 if (c.flags)
329 return -EOPNOTSUPP;
330 return ops->pr_clear(bdev, c.key);
331}
332
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200333static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
334 unsigned cmd, unsigned long arg)
335{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200336 if (!capable(CAP_SYS_ADMIN))
337 return -EACCES;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200338 fsync_bdev(bdev);
339 invalidate_bdev(bdev);
340 return 0;
341}
342
343static int blkdev_roset(struct block_device *bdev, fmode_t mode,
344 unsigned cmd, unsigned long arg)
345{
346 int ret, n;
347
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200348 if (!capable(CAP_SYS_ADMIN))
349 return -EACCES;
350
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200351 if (get_user(n, (int __user *)arg))
352 return -EFAULT;
Christoph Hellwige00adca2020-11-03 11:00:11 +0100353 if (bdev->bd_disk->fops->set_read_only) {
354 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
355 if (ret)
356 return ret;
357 }
Christoph Hellwig83950d32020-11-23 16:36:02 +0100358 bdev->bd_read_only = n;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200359 return 0;
360}
361
362static int blkdev_getgeo(struct block_device *bdev,
363 struct hd_geometry __user *argp)
364{
365 struct gendisk *disk = bdev->bd_disk;
366 struct hd_geometry geo;
367 int ret;
368
369 if (!argp)
370 return -EINVAL;
371 if (!disk->fops->getgeo)
372 return -ENOTTY;
373
374 /*
375 * We need to set the startsect first, the driver may
376 * want to override it.
377 */
378 memset(&geo, 0, sizeof(geo));
379 geo.start = get_start_sect(bdev);
380 ret = disk->fops->getgeo(bdev, &geo);
381 if (ret)
382 return ret;
383 if (copy_to_user(argp, &geo, sizeof(geo)))
384 return -EFAULT;
385 return 0;
386}
387
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100388#ifdef CONFIG_COMPAT
389struct compat_hd_geometry {
390 unsigned char heads;
391 unsigned char sectors;
392 unsigned short cylinders;
393 u32 start;
394};
395
Arnd Bergmann9b816482019-11-29 11:48:31 +0100396static int compat_hdio_getgeo(struct block_device *bdev,
397 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100398{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100399 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100400 struct hd_geometry geo;
401 int ret;
402
403 if (!ugeo)
404 return -EINVAL;
405 if (!disk->fops->getgeo)
406 return -ENOTTY;
407
408 memset(&geo, 0, sizeof(geo));
409 /*
410 * We need to set the startsect first, the driver may
411 * want to override it.
412 */
413 geo.start = get_start_sect(bdev);
414 ret = disk->fops->getgeo(bdev, &geo);
415 if (ret)
416 return ret;
417
418 ret = copy_to_user(ugeo, &geo, 4);
419 ret |= put_user(geo.start, &ugeo->start);
420 if (ret)
421 ret = -EFAULT;
422
423 return ret;
424}
425#endif
426
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200427/* set the logical block size */
428static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
429 int __user *argp)
430{
431 int ret, n;
432
433 if (!capable(CAP_SYS_ADMIN))
434 return -EACCES;
435 if (!argp)
436 return -EINVAL;
437 if (get_user(n, argp))
438 return -EFAULT;
439
Christoph Hellwig47816282020-09-21 09:19:47 +0200440 if (mode & FMODE_EXCL)
441 return set_blocksize(bdev, n);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200442
Christoph Hellwig47816282020-09-21 09:19:47 +0200443 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
444 return -EBUSY;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200445 ret = set_blocksize(bdev, n);
Christoph Hellwig47816282020-09-21 09:19:47 +0200446 blkdev_put(bdev, mode | FMODE_EXCL);
447
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200448 return ret;
449}
450
Linus Torvalds07d106d2012-01-05 15:40:12 -0800451/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100452 * Common commands that are handled the same way on native and compat
453 * user space. Note the separate arg/argp parameters that are needed
454 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200455 */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100456static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
457 unsigned cmd, unsigned long arg, void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700458{
Akinobu Mita63f26492014-05-25 21:43:33 +0900459 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700460
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200461 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200463 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200465 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700466 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200467 return blk_ioctl_discard(bdev, mode, arg, 0);
468 case BLKSECDISCARD:
469 return blk_ioctl_discard(bdev, mode, arg,
470 BLKDEV_DISCARD_SECURE);
471 case BLKZEROOUT:
472 return blk_ioctl_zeroout(bdev, mode, arg);
Matteo Croce7957d932021-07-13 01:05:27 +0200473 case BLKGETDISKSEQ:
474 return put_u64(argp, bdev->bd_disk->diskseq);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900475 case BLKREPORTZONE:
476 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
477 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900478 case BLKOPENZONE:
479 case BLKCLOSEZONE:
480 case BLKFINISHZONE:
481 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900482 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100483 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900484 case BLKGETNRZONES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100485 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
Al Viro45048d02008-09-18 15:53:24 -0400486 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100487 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200488 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100489 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200490 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100491 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200492 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100493 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200494 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100495 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200496 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100497 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100498 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100499 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400500 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900501 max_sectors = min_t(unsigned int, USHRT_MAX,
502 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100503 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100504 case BLKROTATIONAL:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100505 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400506 case BLKRASET:
507 case BLKFRASET:
508 if(!capable(CAP_SYS_ADMIN))
509 return -EACCES;
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200510 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400511 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400512 case BLKRRPART:
Christoph Hellwig4601b4b2021-02-23 16:18:22 +0100513 return blkdev_reread_part(bdev, mode);
Al Viro45048d02008-09-18 15:53:24 -0400514 case BLKTRACESTART:
515 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400516 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200517 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200518 case IOC_PR_REGISTER:
519 return blkdev_pr_register(bdev, argp);
520 case IOC_PR_RESERVE:
521 return blkdev_pr_reserve(bdev, argp);
522 case IOC_PR_RELEASE:
523 return blkdev_pr_release(bdev, argp);
524 case IOC_PR_PREEMPT:
525 return blkdev_pr_preempt(bdev, argp, false);
526 case IOC_PR_PREEMPT_ABORT:
527 return blkdev_pr_preempt(bdev, argp, true);
528 case IOC_PR_CLEAR:
529 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400530 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100531 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100534
535/*
536 * Always keep this in sync with compat_blkdev_ioctl()
537 * to handle all incompatible commands in both functions.
538 *
539 * New commands must be compatible and go into blkdev_common_ioctl
540 */
541int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
542 unsigned long arg)
543{
544 int ret;
545 loff_t size;
546 void __user *argp = (void __user *)arg;
547
548 switch (cmd) {
549 /* These need separate implementations for the data structure */
550 case HDIO_GETGEO:
551 return blkdev_getgeo(bdev, argp);
552 case BLKPG:
553 return blkpg_ioctl(bdev, argp);
554
555 /* Compat mode returns 32-bit data instead of 'long' */
556 case BLKRAGET:
557 case BLKFRAGET:
558 if (!argp)
559 return -EINVAL;
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200560 return put_long(argp,
561 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100562 case BLKGETSIZE:
563 size = i_size_read(bdev->bd_inode);
564 if ((size >> 9) > ~0UL)
565 return -EFBIG;
566 return put_ulong(argp, size >> 9);
567
568 /* The data is compatible, but the command number is different */
569 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
570 return put_int(argp, block_size(bdev));
571 case BLKBSZSET:
572 return blkdev_bszset(bdev, mode, argp);
573 case BLKGETSIZE64:
574 return put_u64(argp, i_size_read(bdev->bd_inode));
575
576 /* Incompatible alignment on i386 */
577 case BLKTRACESETUP:
578 return blk_trace_ioctl(bdev, cmd, argp);
579 default:
580 break;
581 }
582
583 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
Christoph Hellwiga7cb3d22020-11-03 11:00:18 +0100584 if (ret != -ENOIOCTLCMD)
585 return ret;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100586
Christoph Hellwiga7cb3d22020-11-03 11:00:18 +0100587 if (!bdev->bd_disk->fops->ioctl)
588 return -ENOTTY;
589 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100590}
591EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100592
593#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100594
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100595#define BLKBSZGET_32 _IOR(0x12, 112, int)
596#define BLKBSZSET_32 _IOW(0x12, 113, int)
597#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
598
599/* Most of the generic ioctls are handled in the normal fallback path.
600 This assumes the blkdev's low level compat_ioctl always returns
601 ENOIOCTLCMD for unknown ioctls. */
602long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
603{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100604 int ret;
605 void __user *argp = compat_ptr(arg);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100606 struct block_device *bdev = I_BDEV(file->f_mapping->host);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100607 struct gendisk *disk = bdev->bd_disk;
608 fmode_t mode = file->f_mode;
609 loff_t size;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100610
611 /*
612 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
613 * to updated it before every ioctl.
614 */
615 if (file->f_flags & O_NDELAY)
616 mode |= FMODE_NDELAY;
617 else
618 mode &= ~FMODE_NDELAY;
619
620 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100621 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100622 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100623 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100624 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100625 return compat_blkpg_ioctl(bdev, argp);
626
627 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100628 case BLKRAGET:
629 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100630 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100631 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100632 return compat_put_long(argp,
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200633 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100634 case BLKGETSIZE:
635 size = i_size_read(bdev->bd_inode);
636 if ((size >> 9) > ~0UL)
637 return -EFBIG;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100638 return compat_put_ulong(argp, size >> 9);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100639
Arnd Bergmann9b816482019-11-29 11:48:31 +0100640 /* The data is compatible, but the command number is different */
641 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
642 return put_int(argp, bdev_logical_block_size(bdev));
643 case BLKBSZSET_32:
644 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100645 case BLKGETSIZE64_32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100646 return put_u64(argp, i_size_read(bdev->bd_inode));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100647
Arnd Bergmann9b816482019-11-29 11:48:31 +0100648 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100649 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100650 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100651 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100652 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100653 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100654
655 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
656 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
657 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
658
659 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100660}
661#endif