blob: 4a86340133e46b2465ed0de12fbf81b836ee81c3 [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 Hellwigd8e4bb8102015-10-15 14:10:47 +020085static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
86 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +010087{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +020088 uint64_t range[2];
89 uint64_t start, len;
Dmitry Monakhov351499a12017-10-24 18:44:57 -060090 struct request_queue *q = bdev_get_queue(bdev);
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +090091 struct inode *inode = bdev->bd_inode;
Jan Kara384d87e2020-09-04 10:58:52 +020092 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +020093
94 if (!(mode & FMODE_WRITE))
95 return -EBADF;
96
Dmitry Monakhov351499a12017-10-24 18:44:57 -060097 if (!blk_queue_discard(q))
98 return -EOPNOTSUPP;
99
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200100 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
101 return -EFAULT;
102
103 start = range[0];
104 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700105
David Woodhoused30a2602008-08-11 15:58:42 +0100106 if (start & 511)
107 return -EINVAL;
108 if (len & 511)
109 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100110
Christoph Hellwig946e9932021-10-19 08:20:22 +0200111 if (start + len > bdev_nr_bytes(bdev))
David Woodhoused30a2602008-08-11 15:58:42 +0100112 return -EINVAL;
Jan Kara384d87e2020-09-04 10:58:52 +0200113
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900114 filemap_invalidate_lock(inode->i_mapping);
Jan Kara384d87e2020-09-04 10:58:52 +0200115 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
116 if (err)
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900117 goto fail;
Jan Kara384d87e2020-09-04 10:58:52 +0200118
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900119 err = blkdev_issue_discard(bdev, start >> 9, len >> 9,
120 GFP_KERNEL, flags);
121
122fail:
123 filemap_invalidate_unlock(inode->i_mapping);
124 return err;
David Woodhoused30a2602008-08-11 15:58:42 +0100125}
126
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200127static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
128 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400129{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200130 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700131 uint64_t start, end, len;
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900132 struct inode *inode = bdev->bd_inode;
Jan Kara384d87e2020-09-04 10:58:52 +0200133 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200134
135 if (!(mode & FMODE_WRITE))
136 return -EBADF;
137
138 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
139 return -EFAULT;
140
141 start = range[0];
142 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700143 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200144
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400145 if (start & 511)
146 return -EINVAL;
147 if (len & 511)
148 return -EINVAL;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200149 if (end >= (uint64_t)bdev_nr_bytes(bdev))
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700150 return -EINVAL;
151 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400152 return -EINVAL;
153
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700154 /* Invalidate the page cache, including dirty pages */
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900155 filemap_invalidate_lock(inode->i_mapping);
Jan Kara384d87e2020-09-04 10:58:52 +0200156 err = truncate_bdev_range(bdev, mode, start, end);
157 if (err)
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900158 goto fail;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700159
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900160 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
161 BLKDEV_ZERO_NOUNMAP);
162
163fail:
164 filemap_invalidate_unlock(inode->i_mapping);
165 return err;
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400166}
167
Arnd Bergmann9b816482019-11-29 11:48:31 +0100168static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100170 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Arnd Bergmann9b816482019-11-29 11:48:31 +0100173static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100175 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Arnd Bergmann9b816482019-11-29 11:48:31 +0100178static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200179{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100180 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200181}
182
Arnd Bergmann9b816482019-11-29 11:48:31 +0100183static int put_long(long __user *argp, long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100185 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Arnd Bergmann9b816482019-11-29 11:48:31 +0100188static int put_ulong(unsigned long __user *argp, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100190 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Arnd Bergmann9b816482019-11-29 11:48:31 +0100193static int put_u64(u64 __user *argp, u64 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 Bergmannbdc1dda2019-11-29 11:45:30 +0100198#ifdef CONFIG_COMPAT
Bart Van Asschec8210a52020-05-18 21:07:34 -0700199static int compat_put_long(compat_long_t __user *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100200{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100201 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100202}
203
Bart Van Asschec8210a52020-05-18 21:07:34 -0700204static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100205{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100206 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100207}
208#endif
209
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100210#ifdef CONFIG_COMPAT
211/*
212 * This is the equivalent of compat_ptr_ioctl(), to be used by block
213 * drivers that implement only commands that are completely compatible
214 * between 32-bit and 64-bit user space
215 */
216int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
217 unsigned cmd, unsigned long arg)
218{
219 struct gendisk *disk = bdev->bd_disk;
220
221 if (disk->fops->ioctl)
222 return disk->fops->ioctl(bdev, mode, cmd,
223 (unsigned long)compat_ptr(arg));
224
225 return -ENOIOCTLCMD;
226}
227EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
228#endif
229
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200230static int blkdev_pr_register(struct block_device *bdev,
231 struct pr_registration __user *arg)
232{
233 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
234 struct pr_registration reg;
235
236 if (!capable(CAP_SYS_ADMIN))
237 return -EPERM;
238 if (!ops || !ops->pr_register)
239 return -EOPNOTSUPP;
240 if (copy_from_user(&reg, arg, sizeof(reg)))
241 return -EFAULT;
242
243 if (reg.flags & ~PR_FL_IGNORE_KEY)
244 return -EOPNOTSUPP;
245 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
246}
247
248static int blkdev_pr_reserve(struct block_device *bdev,
249 struct pr_reservation __user *arg)
250{
251 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
252 struct pr_reservation rsv;
253
254 if (!capable(CAP_SYS_ADMIN))
255 return -EPERM;
256 if (!ops || !ops->pr_reserve)
257 return -EOPNOTSUPP;
258 if (copy_from_user(&rsv, arg, sizeof(rsv)))
259 return -EFAULT;
260
261 if (rsv.flags & ~PR_FL_IGNORE_KEY)
262 return -EOPNOTSUPP;
263 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
264}
265
266static int blkdev_pr_release(struct block_device *bdev,
267 struct pr_reservation __user *arg)
268{
269 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
270 struct pr_reservation rsv;
271
272 if (!capable(CAP_SYS_ADMIN))
273 return -EPERM;
274 if (!ops || !ops->pr_release)
275 return -EOPNOTSUPP;
276 if (copy_from_user(&rsv, arg, sizeof(rsv)))
277 return -EFAULT;
278
279 if (rsv.flags)
280 return -EOPNOTSUPP;
281 return ops->pr_release(bdev, rsv.key, rsv.type);
282}
283
284static int blkdev_pr_preempt(struct block_device *bdev,
285 struct pr_preempt __user *arg, bool abort)
286{
287 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
288 struct pr_preempt p;
289
290 if (!capable(CAP_SYS_ADMIN))
291 return -EPERM;
292 if (!ops || !ops->pr_preempt)
293 return -EOPNOTSUPP;
294 if (copy_from_user(&p, arg, sizeof(p)))
295 return -EFAULT;
296
297 if (p.flags)
298 return -EOPNOTSUPP;
299 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
300}
301
302static int blkdev_pr_clear(struct block_device *bdev,
303 struct pr_clear __user *arg)
304{
305 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
306 struct pr_clear c;
307
308 if (!capable(CAP_SYS_ADMIN))
309 return -EPERM;
310 if (!ops || !ops->pr_clear)
311 return -EOPNOTSUPP;
312 if (copy_from_user(&c, arg, sizeof(c)))
313 return -EFAULT;
314
315 if (c.flags)
316 return -EOPNOTSUPP;
317 return ops->pr_clear(bdev, c.key);
318}
319
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200320static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
321 unsigned cmd, unsigned long arg)
322{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200323 if (!capable(CAP_SYS_ADMIN))
324 return -EACCES;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200325 fsync_bdev(bdev);
326 invalidate_bdev(bdev);
327 return 0;
328}
329
330static int blkdev_roset(struct block_device *bdev, fmode_t mode,
331 unsigned cmd, unsigned long arg)
332{
333 int ret, n;
334
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200335 if (!capable(CAP_SYS_ADMIN))
336 return -EACCES;
337
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200338 if (get_user(n, (int __user *)arg))
339 return -EFAULT;
Christoph Hellwige00adca2020-11-03 11:00:11 +0100340 if (bdev->bd_disk->fops->set_read_only) {
341 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
342 if (ret)
343 return ret;
344 }
Christoph Hellwig83950d32020-11-23 16:36:02 +0100345 bdev->bd_read_only = n;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200346 return 0;
347}
348
349static int blkdev_getgeo(struct block_device *bdev,
350 struct hd_geometry __user *argp)
351{
352 struct gendisk *disk = bdev->bd_disk;
353 struct hd_geometry geo;
354 int ret;
355
356 if (!argp)
357 return -EINVAL;
358 if (!disk->fops->getgeo)
359 return -ENOTTY;
360
361 /*
362 * We need to set the startsect first, the driver may
363 * want to override it.
364 */
365 memset(&geo, 0, sizeof(geo));
366 geo.start = get_start_sect(bdev);
367 ret = disk->fops->getgeo(bdev, &geo);
368 if (ret)
369 return ret;
370 if (copy_to_user(argp, &geo, sizeof(geo)))
371 return -EFAULT;
372 return 0;
373}
374
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100375#ifdef CONFIG_COMPAT
376struct compat_hd_geometry {
377 unsigned char heads;
378 unsigned char sectors;
379 unsigned short cylinders;
380 u32 start;
381};
382
Arnd Bergmann9b816482019-11-29 11:48:31 +0100383static int compat_hdio_getgeo(struct block_device *bdev,
384 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100385{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100386 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100387 struct hd_geometry geo;
388 int ret;
389
390 if (!ugeo)
391 return -EINVAL;
392 if (!disk->fops->getgeo)
393 return -ENOTTY;
394
395 memset(&geo, 0, sizeof(geo));
396 /*
397 * We need to set the startsect first, the driver may
398 * want to override it.
399 */
400 geo.start = get_start_sect(bdev);
401 ret = disk->fops->getgeo(bdev, &geo);
402 if (ret)
403 return ret;
404
405 ret = copy_to_user(ugeo, &geo, 4);
406 ret |= put_user(geo.start, &ugeo->start);
407 if (ret)
408 ret = -EFAULT;
409
410 return ret;
411}
412#endif
413
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200414/* set the logical block size */
415static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
416 int __user *argp)
417{
418 int ret, n;
419
420 if (!capable(CAP_SYS_ADMIN))
421 return -EACCES;
422 if (!argp)
423 return -EINVAL;
424 if (get_user(n, argp))
425 return -EFAULT;
426
Christoph Hellwig47816282020-09-21 09:19:47 +0200427 if (mode & FMODE_EXCL)
428 return set_blocksize(bdev, n);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200429
Christoph Hellwig47816282020-09-21 09:19:47 +0200430 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
431 return -EBUSY;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200432 ret = set_blocksize(bdev, n);
Christoph Hellwig47816282020-09-21 09:19:47 +0200433 blkdev_put(bdev, mode | FMODE_EXCL);
434
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200435 return ret;
436}
437
Linus Torvalds07d106d2012-01-05 15:40:12 -0800438/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100439 * Common commands that are handled the same way on native and compat
440 * user space. Note the separate arg/argp parameters that are needed
441 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200442 */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100443static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
444 unsigned cmd, unsigned long arg, void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700445{
Akinobu Mita63f26492014-05-25 21:43:33 +0900446 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700447
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200448 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200450 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200452 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700453 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200454 return blk_ioctl_discard(bdev, mode, arg, 0);
455 case BLKSECDISCARD:
456 return blk_ioctl_discard(bdev, mode, arg,
457 BLKDEV_DISCARD_SECURE);
458 case BLKZEROOUT:
459 return blk_ioctl_zeroout(bdev, mode, arg);
Matteo Croce7957d932021-07-13 01:05:27 +0200460 case BLKGETDISKSEQ:
461 return put_u64(argp, bdev->bd_disk->diskseq);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900462 case BLKREPORTZONE:
463 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
464 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900465 case BLKOPENZONE:
466 case BLKCLOSEZONE:
467 case BLKFINISHZONE:
468 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900469 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100470 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900471 case BLKGETNRZONES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100472 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
Al Viro45048d02008-09-18 15:53:24 -0400473 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100474 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200475 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100476 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200477 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100478 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200479 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100480 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200481 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100482 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200483 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100484 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100485 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100486 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400487 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900488 max_sectors = min_t(unsigned int, USHRT_MAX,
489 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100490 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100491 case BLKROTATIONAL:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100492 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400493 case BLKRASET:
494 case BLKFRASET:
495 if(!capable(CAP_SYS_ADMIN))
496 return -EACCES;
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200497 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400498 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400499 case BLKRRPART:
Christoph Hellwige16e5062021-11-22 14:06:16 +0100500 if (!capable(CAP_SYS_ADMIN))
501 return -EACCES;
502 if (bdev_is_partition(bdev))
503 return -EINVAL;
504 return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL);
Al Viro45048d02008-09-18 15:53:24 -0400505 case BLKTRACESTART:
506 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400507 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200508 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200509 case IOC_PR_REGISTER:
510 return blkdev_pr_register(bdev, argp);
511 case IOC_PR_RESERVE:
512 return blkdev_pr_reserve(bdev, argp);
513 case IOC_PR_RELEASE:
514 return blkdev_pr_release(bdev, argp);
515 case IOC_PR_PREEMPT:
516 return blkdev_pr_preempt(bdev, argp, false);
517 case IOC_PR_PREEMPT_ABORT:
518 return blkdev_pr_preempt(bdev, argp, true);
519 case IOC_PR_CLEAR:
520 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400521 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100522 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100525
526/*
527 * Always keep this in sync with compat_blkdev_ioctl()
528 * to handle all incompatible commands in both functions.
529 *
530 * New commands must be compatible and go into blkdev_common_ioctl
531 */
Christoph Hellwig8a709512021-10-12 12:44:50 +0200532long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Arnd Bergmann9b816482019-11-29 11:48:31 +0100533{
Christoph Hellwig8a709512021-10-12 12:44:50 +0200534 struct block_device *bdev = I_BDEV(file->f_mapping->host);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100535 void __user *argp = (void __user *)arg;
Christoph Hellwig8a709512021-10-12 12:44:50 +0200536 fmode_t mode = file->f_mode;
Christoph Hellwig8a709512021-10-12 12:44:50 +0200537 int ret;
538
539 /*
540 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
541 * to updated it before every ioctl.
542 */
543 if (file->f_flags & O_NDELAY)
544 mode |= FMODE_NDELAY;
545 else
546 mode &= ~FMODE_NDELAY;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100547
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:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200563 if (bdev_nr_sectors(bdev) > ~0UL)
Arnd Bergmann9b816482019-11-29 11:48:31 +0100564 return -EFBIG;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200565 return put_ulong(argp, bdev_nr_sectors(bdev));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100566
567 /* The data is compatible, but the command number is different */
568 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
569 return put_int(argp, block_size(bdev));
570 case BLKBSZSET:
571 return blkdev_bszset(bdev, mode, argp);
572 case BLKGETSIZE64:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200573 return put_u64(argp, bdev_nr_bytes(bdev));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100574
575 /* Incompatible alignment on i386 */
576 case BLKTRACESETUP:
577 return blk_trace_ioctl(bdev, cmd, argp);
578 default:
579 break;
580 }
581
582 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
Christoph Hellwiga7cb3d22020-11-03 11:00:18 +0100583 if (ret != -ENOIOCTLCMD)
584 return ret;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100585
Christoph Hellwiga7cb3d22020-11-03 11:00:18 +0100586 if (!bdev->bd_disk->fops->ioctl)
587 return -ENOTTY;
588 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100589}
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100590
591#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100592
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100593#define BLKBSZGET_32 _IOR(0x12, 112, int)
594#define BLKBSZSET_32 _IOW(0x12, 113, int)
595#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
596
597/* Most of the generic ioctls are handled in the normal fallback path.
598 This assumes the blkdev's low level compat_ioctl always returns
599 ENOIOCTLCMD for unknown ioctls. */
600long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
601{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100602 int ret;
603 void __user *argp = compat_ptr(arg);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100604 struct block_device *bdev = I_BDEV(file->f_mapping->host);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100605 struct gendisk *disk = bdev->bd_disk;
606 fmode_t mode = file->f_mode;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100607
608 /*
609 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
610 * to updated it before every ioctl.
611 */
612 if (file->f_flags & O_NDELAY)
613 mode |= FMODE_NDELAY;
614 else
615 mode &= ~FMODE_NDELAY;
616
617 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100618 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100619 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100620 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100621 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100622 return compat_blkpg_ioctl(bdev, argp);
623
624 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100625 case BLKRAGET:
626 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100627 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100628 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100629 return compat_put_long(argp,
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200630 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100631 case BLKGETSIZE:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200632 if (bdev_nr_sectors(bdev) > ~0UL)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100633 return -EFBIG;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200634 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100635
Arnd Bergmann9b816482019-11-29 11:48:31 +0100636 /* The data is compatible, but the command number is different */
637 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
638 return put_int(argp, bdev_logical_block_size(bdev));
639 case BLKBSZSET_32:
640 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100641 case BLKGETSIZE64_32:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200642 return put_u64(argp, bdev_nr_bytes(bdev));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100643
Arnd Bergmann9b816482019-11-29 11:48:31 +0100644 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100645 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100646 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100647 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100648 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100649 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100650
651 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
652 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
653 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
654
655 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100656}
657#endif