blob: 9c5f637ff153f8b0010a6812ab8565fdacfbc5fe [file] [log] [blame]
Christoph Hellwig3dcf60bc2019-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,
Christoph Hellwig44abff2c2022-04-15 06:52:57 +020086 unsigned long arg)
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;
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +090090 struct inode *inode = bdev->bd_inode;
Jan Kara384d87e2020-09-04 10:58:52 +020091 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +020092
93 if (!(mode & FMODE_WRITE))
94 return -EBADF;
95
Christoph Hellwig70200572022-04-15 06:52:55 +020096 if (!bdev_max_discard_sectors(bdev))
Dmitry Monakhov351499a12017-10-24 18:44:57 -060097 return -EOPNOTSUPP;
98
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +020099 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
100 return -EFAULT;
101
102 start = range[0];
103 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700104
David Woodhoused30a2602008-08-11 15:58:42 +0100105 if (start & 511)
106 return -EINVAL;
107 if (len & 511)
108 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100109
Christoph Hellwig946e9932021-10-19 08:20:22 +0200110 if (start + len > bdev_nr_bytes(bdev))
David Woodhoused30a2602008-08-11 15:58:42 +0100111 return -EINVAL;
Jan Kara384d87e2020-09-04 10:58:52 +0200112
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900113 filemap_invalidate_lock(inode->i_mapping);
Jan Kara384d87e2020-09-04 10:58:52 +0200114 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
115 if (err)
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900116 goto fail;
Christoph Hellwig44abff2c2022-04-15 06:52:57 +0200117 err = blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Shin'ichiro Kawasaki7607c442021-11-09 19:47:22 +0900118fail:
119 filemap_invalidate_unlock(inode->i_mapping);
120 return err;
David Woodhoused30a2602008-08-11 15:58:42 +0100121}
122
Christoph Hellwig44abff2c2022-04-15 06:52:57 +0200123static int blk_ioctl_secure_erase(struct block_device *bdev, fmode_t mode,
124 void __user *argp)
125{
126 uint64_t start, len;
127 uint64_t range[2];
128 int err;
129
130 if (!(mode & FMODE_WRITE))
131 return -EBADF;
132 if (!bdev_max_secure_erase_sectors(bdev))
133 return -EOPNOTSUPP;
134 if (copy_from_user(range, argp, sizeof(range)))
135 return -EFAULT;
136
137 start = range[0];
138 len = range[1];
139 if ((start & 511) || (len & 511))
140 return -EINVAL;
141 if (start + len > bdev_nr_bytes(bdev))
142 return -EINVAL;
143
144 filemap_invalidate_lock(bdev->bd_inode->i_mapping);
145 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
146 if (!err)
147 err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9,
148 GFP_KERNEL);
149 filemap_invalidate_unlock(bdev->bd_inode->i_mapping);
150 return err;
151}
152
153
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200154static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
155 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400156{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200157 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700158 uint64_t start, end, len;
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900159 struct inode *inode = bdev->bd_inode;
Jan Kara384d87e2020-09-04 10:58:52 +0200160 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200161
162 if (!(mode & FMODE_WRITE))
163 return -EBADF;
164
165 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
166 return -EFAULT;
167
168 start = range[0];
169 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700170 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200171
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400172 if (start & 511)
173 return -EINVAL;
174 if (len & 511)
175 return -EINVAL;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200176 if (end >= (uint64_t)bdev_nr_bytes(bdev))
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700177 return -EINVAL;
178 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400179 return -EINVAL;
180
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700181 /* Invalidate the page cache, including dirty pages */
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900182 filemap_invalidate_lock(inode->i_mapping);
Jan Kara384d87e2020-09-04 10:58:52 +0200183 err = truncate_bdev_range(bdev, mode, start, end);
184 if (err)
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900185 goto fail;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700186
Shin'ichiro Kawasaki35e4c6c2021-11-09 19:47:23 +0900187 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
188 BLKDEV_ZERO_NOUNMAP);
189
190fail:
191 filemap_invalidate_unlock(inode->i_mapping);
192 return err;
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400193}
194
Arnd Bergmann9b816482019-11-29 11:48:31 +0100195static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100197 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Arnd Bergmann9b816482019-11-29 11:48:31 +0100200static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100202 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Arnd Bergmann9b816482019-11-29 11:48:31 +0100205static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200206{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100207 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200208}
209
Arnd Bergmann9b816482019-11-29 11:48:31 +0100210static int put_long(long __user *argp, long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100212 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Arnd Bergmann9b816482019-11-29 11:48:31 +0100215static int put_ulong(unsigned long __user *argp, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100217 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Arnd Bergmann9b816482019-11-29 11:48:31 +0100220static int put_u64(u64 __user *argp, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100222 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100225#ifdef CONFIG_COMPAT
Bart Van Asschec8210a52020-05-18 21:07:34 -0700226static int compat_put_long(compat_long_t __user *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100227{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100228 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100229}
230
Bart Van Asschec8210a52020-05-18 21:07:34 -0700231static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100232{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100233 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100234}
235#endif
236
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100237#ifdef CONFIG_COMPAT
238/*
239 * This is the equivalent of compat_ptr_ioctl(), to be used by block
240 * drivers that implement only commands that are completely compatible
241 * between 32-bit and 64-bit user space
242 */
243int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
244 unsigned cmd, unsigned long arg)
245{
246 struct gendisk *disk = bdev->bd_disk;
247
248 if (disk->fops->ioctl)
249 return disk->fops->ioctl(bdev, mode, cmd,
250 (unsigned long)compat_ptr(arg));
251
252 return -ENOIOCTLCMD;
253}
254EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
255#endif
256
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200257static int blkdev_pr_register(struct block_device *bdev,
258 struct pr_registration __user *arg)
259{
260 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
261 struct pr_registration reg;
262
263 if (!capable(CAP_SYS_ADMIN))
264 return -EPERM;
265 if (!ops || !ops->pr_register)
266 return -EOPNOTSUPP;
267 if (copy_from_user(&reg, arg, sizeof(reg)))
268 return -EFAULT;
269
270 if (reg.flags & ~PR_FL_IGNORE_KEY)
271 return -EOPNOTSUPP;
272 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
273}
274
275static int blkdev_pr_reserve(struct block_device *bdev,
276 struct pr_reservation __user *arg)
277{
278 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
279 struct pr_reservation rsv;
280
281 if (!capable(CAP_SYS_ADMIN))
282 return -EPERM;
283 if (!ops || !ops->pr_reserve)
284 return -EOPNOTSUPP;
285 if (copy_from_user(&rsv, arg, sizeof(rsv)))
286 return -EFAULT;
287
288 if (rsv.flags & ~PR_FL_IGNORE_KEY)
289 return -EOPNOTSUPP;
290 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
291}
292
293static int blkdev_pr_release(struct block_device *bdev,
294 struct pr_reservation __user *arg)
295{
296 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
297 struct pr_reservation rsv;
298
299 if (!capable(CAP_SYS_ADMIN))
300 return -EPERM;
301 if (!ops || !ops->pr_release)
302 return -EOPNOTSUPP;
303 if (copy_from_user(&rsv, arg, sizeof(rsv)))
304 return -EFAULT;
305
306 if (rsv.flags)
307 return -EOPNOTSUPP;
308 return ops->pr_release(bdev, rsv.key, rsv.type);
309}
310
311static int blkdev_pr_preempt(struct block_device *bdev,
312 struct pr_preempt __user *arg, bool abort)
313{
314 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
315 struct pr_preempt p;
316
317 if (!capable(CAP_SYS_ADMIN))
318 return -EPERM;
319 if (!ops || !ops->pr_preempt)
320 return -EOPNOTSUPP;
321 if (copy_from_user(&p, arg, sizeof(p)))
322 return -EFAULT;
323
324 if (p.flags)
325 return -EOPNOTSUPP;
326 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
327}
328
329static int blkdev_pr_clear(struct block_device *bdev,
330 struct pr_clear __user *arg)
331{
332 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
333 struct pr_clear c;
334
335 if (!capable(CAP_SYS_ADMIN))
336 return -EPERM;
337 if (!ops || !ops->pr_clear)
338 return -EOPNOTSUPP;
339 if (copy_from_user(&c, arg, sizeof(c)))
340 return -EFAULT;
341
342 if (c.flags)
343 return -EOPNOTSUPP;
344 return ops->pr_clear(bdev, c.key);
345}
346
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200347static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
348 unsigned cmd, unsigned long arg)
349{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200350 if (!capable(CAP_SYS_ADMIN))
351 return -EACCES;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200352 fsync_bdev(bdev);
353 invalidate_bdev(bdev);
354 return 0;
355}
356
357static int blkdev_roset(struct block_device *bdev, fmode_t mode,
358 unsigned cmd, unsigned long arg)
359{
360 int ret, n;
361
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200362 if (!capable(CAP_SYS_ADMIN))
363 return -EACCES;
364
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200365 if (get_user(n, (int __user *)arg))
366 return -EFAULT;
Christoph Hellwige00adca2020-11-03 11:00:11 +0100367 if (bdev->bd_disk->fops->set_read_only) {
368 ret = bdev->bd_disk->fops->set_read_only(bdev, n);
369 if (ret)
370 return ret;
371 }
Christoph Hellwig83950d32020-11-23 16:36:02 +0100372 bdev->bd_read_only = n;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200373 return 0;
374}
375
376static int blkdev_getgeo(struct block_device *bdev,
377 struct hd_geometry __user *argp)
378{
379 struct gendisk *disk = bdev->bd_disk;
380 struct hd_geometry geo;
381 int ret;
382
383 if (!argp)
384 return -EINVAL;
385 if (!disk->fops->getgeo)
386 return -ENOTTY;
387
388 /*
389 * We need to set the startsect first, the driver may
390 * want to override it.
391 */
392 memset(&geo, 0, sizeof(geo));
393 geo.start = get_start_sect(bdev);
394 ret = disk->fops->getgeo(bdev, &geo);
395 if (ret)
396 return ret;
397 if (copy_to_user(argp, &geo, sizeof(geo)))
398 return -EFAULT;
399 return 0;
400}
401
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100402#ifdef CONFIG_COMPAT
403struct compat_hd_geometry {
404 unsigned char heads;
405 unsigned char sectors;
406 unsigned short cylinders;
407 u32 start;
408};
409
Arnd Bergmann9b816482019-11-29 11:48:31 +0100410static int compat_hdio_getgeo(struct block_device *bdev,
411 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100412{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100413 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100414 struct hd_geometry geo;
415 int ret;
416
417 if (!ugeo)
418 return -EINVAL;
419 if (!disk->fops->getgeo)
420 return -ENOTTY;
421
422 memset(&geo, 0, sizeof(geo));
423 /*
424 * We need to set the startsect first, the driver may
425 * want to override it.
426 */
427 geo.start = get_start_sect(bdev);
428 ret = disk->fops->getgeo(bdev, &geo);
429 if (ret)
430 return ret;
431
432 ret = copy_to_user(ugeo, &geo, 4);
433 ret |= put_user(geo.start, &ugeo->start);
434 if (ret)
435 ret = -EFAULT;
436
437 return ret;
438}
439#endif
440
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200441/* set the logical block size */
442static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
443 int __user *argp)
444{
445 int ret, n;
446
447 if (!capable(CAP_SYS_ADMIN))
448 return -EACCES;
449 if (!argp)
450 return -EINVAL;
451 if (get_user(n, argp))
452 return -EFAULT;
453
Christoph Hellwig47816282020-09-21 09:19:47 +0200454 if (mode & FMODE_EXCL)
455 return set_blocksize(bdev, n);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200456
Christoph Hellwig47816282020-09-21 09:19:47 +0200457 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
458 return -EBUSY;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200459 ret = set_blocksize(bdev, n);
Christoph Hellwig47816282020-09-21 09:19:47 +0200460 blkdev_put(bdev, mode | FMODE_EXCL);
461
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200462 return ret;
463}
464
Linus Torvalds07d106d2012-01-05 15:40:12 -0800465/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100466 * Common commands that are handled the same way on native and compat
467 * user space. Note the separate arg/argp parameters that are needed
468 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200469 */
Yu Kuai0f77b292023-02-17 10:21:59 +0800470static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
471 unsigned int cmd, unsigned long arg,
472 void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700473{
Akinobu Mita63f26492014-05-25 21:43:33 +0900474 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700475
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200476 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200478 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200480 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700481 case BLKDISCARD:
Christoph Hellwig44abff2c2022-04-15 06:52:57 +0200482 return blk_ioctl_discard(bdev, mode, arg);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200483 case BLKSECDISCARD:
Christoph Hellwig44abff2c2022-04-15 06:52:57 +0200484 return blk_ioctl_secure_erase(bdev, mode, argp);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200485 case BLKZEROOUT:
486 return blk_ioctl_zeroout(bdev, mode, arg);
Matteo Croce7957d93b2021-07-13 01:05:27 +0200487 case BLKGETDISKSEQ:
488 return put_u64(argp, bdev->bd_disk->diskseq);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900489 case BLKREPORTZONE:
490 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
491 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900492 case BLKOPENZONE:
493 case BLKCLOSEZONE:
494 case BLKFINISHZONE:
495 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900496 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100497 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900498 case BLKGETNRZONES:
Christoph Hellwigb623e342022-07-06 09:03:45 +0200499 return put_uint(argp, bdev_nr_zones(bdev));
Al Viro45048d02008-09-18 15:53:24 -0400500 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100501 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200502 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100503 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200504 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100505 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200506 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100507 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200508 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100509 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200510 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100511 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100512 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100513 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400514 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900515 max_sectors = min_t(unsigned int, USHRT_MAX,
516 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100517 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100518 case BLKROTATIONAL:
Christoph Hellwig10f0d2a52022-04-15 06:52:42 +0200519 return put_ushort(argp, !bdev_nonrot(bdev));
Al Viro45048d02008-09-18 15:53:24 -0400520 case BLKRASET:
521 case BLKFRASET:
522 if(!capable(CAP_SYS_ADMIN))
523 return -EACCES;
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200524 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400525 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400526 case BLKRRPART:
Christoph Hellwige16e5062021-11-22 14:06:16 +0100527 if (!capable(CAP_SYS_ADMIN))
528 return -EACCES;
529 if (bdev_is_partition(bdev))
530 return -EINVAL;
Yu Kuaie5cfefa2023-02-17 10:22:00 +0800531 return disk_scan_partitions(bdev->bd_disk, mode);
Al Viro45048d02008-09-18 15:53:24 -0400532 case BLKTRACESTART:
533 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400534 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200535 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200536 case IOC_PR_REGISTER:
537 return blkdev_pr_register(bdev, argp);
538 case IOC_PR_RESERVE:
539 return blkdev_pr_reserve(bdev, argp);
540 case IOC_PR_RELEASE:
541 return blkdev_pr_release(bdev, argp);
542 case IOC_PR_PREEMPT:
543 return blkdev_pr_preempt(bdev, argp, false);
544 case IOC_PR_PREEMPT_ABORT:
545 return blkdev_pr_preempt(bdev, argp, true);
546 case IOC_PR_CLEAR:
547 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400548 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100549 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100552
553/*
554 * Always keep this in sync with compat_blkdev_ioctl()
555 * to handle all incompatible commands in both functions.
556 *
557 * New commands must be compatible and go into blkdev_common_ioctl
558 */
Christoph Hellwig8a709512021-10-12 12:44:50 +0200559long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Arnd Bergmann9b816482019-11-29 11:48:31 +0100560{
Christoph Hellwig8a709512021-10-12 12:44:50 +0200561 struct block_device *bdev = I_BDEV(file->f_mapping->host);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100562 void __user *argp = (void __user *)arg;
Christoph Hellwig8a709512021-10-12 12:44:50 +0200563 fmode_t mode = file->f_mode;
Christoph Hellwig8a709512021-10-12 12:44:50 +0200564 int ret;
565
566 /*
567 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
568 * to updated it before every ioctl.
569 */
570 if (file->f_flags & O_NDELAY)
571 mode |= FMODE_NDELAY;
572 else
573 mode &= ~FMODE_NDELAY;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100574
575 switch (cmd) {
576 /* These need separate implementations for the data structure */
577 case HDIO_GETGEO:
578 return blkdev_getgeo(bdev, argp);
579 case BLKPG:
580 return blkpg_ioctl(bdev, argp);
581
582 /* Compat mode returns 32-bit data instead of 'long' */
583 case BLKRAGET:
584 case BLKFRAGET:
585 if (!argp)
586 return -EINVAL;
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200587 return put_long(argp,
588 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100589 case BLKGETSIZE:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200590 if (bdev_nr_sectors(bdev) > ~0UL)
Arnd Bergmann9b816482019-11-29 11:48:31 +0100591 return -EFBIG;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200592 return put_ulong(argp, bdev_nr_sectors(bdev));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100593
594 /* The data is compatible, but the command number is different */
595 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
596 return put_int(argp, block_size(bdev));
597 case BLKBSZSET:
598 return blkdev_bszset(bdev, mode, argp);
599 case BLKGETSIZE64:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200600 return put_u64(argp, bdev_nr_bytes(bdev));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100601
602 /* Incompatible alignment on i386 */
603 case BLKTRACESETUP:
604 return blk_trace_ioctl(bdev, cmd, argp);
605 default:
606 break;
607 }
608
Yu Kuai0f77b292023-02-17 10:21:59 +0800609 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
Christoph Hellwiga7cb3d2f2020-11-03 11:00:18 +0100610 if (ret != -ENOIOCTLCMD)
611 return ret;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100612
Christoph Hellwiga7cb3d2f2020-11-03 11:00:18 +0100613 if (!bdev->bd_disk->fops->ioctl)
614 return -ENOTTY;
615 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100616}
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100617
618#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100619
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100620#define BLKBSZGET_32 _IOR(0x12, 112, int)
621#define BLKBSZSET_32 _IOW(0x12, 113, int)
622#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
623
624/* Most of the generic ioctls are handled in the normal fallback path.
625 This assumes the blkdev's low level compat_ioctl always returns
626 ENOIOCTLCMD for unknown ioctls. */
627long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
628{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100629 int ret;
630 void __user *argp = compat_ptr(arg);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100631 struct block_device *bdev = I_BDEV(file->f_mapping->host);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100632 struct gendisk *disk = bdev->bd_disk;
633 fmode_t mode = file->f_mode;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100634
635 /*
636 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
637 * to updated it before every ioctl.
638 */
639 if (file->f_flags & O_NDELAY)
640 mode |= FMODE_NDELAY;
641 else
642 mode &= ~FMODE_NDELAY;
643
644 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100645 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100646 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100647 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100648 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100649 return compat_blkpg_ioctl(bdev, argp);
650
651 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100652 case BLKRAGET:
653 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100654 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100655 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100656 return compat_put_long(argp,
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200657 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100658 case BLKGETSIZE:
Khazhismel Kumykovccf16412022-04-14 15:40:56 -0700659 if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100660 return -EFBIG;
Christoph Hellwig946e9932021-10-19 08:20:22 +0200661 return compat_put_ulong(argp, bdev_nr_sectors(bdev));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100662
Arnd Bergmann9b816482019-11-29 11:48:31 +0100663 /* The data is compatible, but the command number is different */
664 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
665 return put_int(argp, bdev_logical_block_size(bdev));
666 case BLKBSZSET_32:
667 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100668 case BLKGETSIZE64_32:
Christoph Hellwig946e9932021-10-19 08:20:22 +0200669 return put_u64(argp, bdev_nr_bytes(bdev));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100670
Arnd Bergmann9b816482019-11-29 11:48:31 +0100671 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100672 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100673 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100674 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100675 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100676 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100677
Yu Kuai0f77b292023-02-17 10:21:59 +0800678 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
Arnd Bergmann9b816482019-11-29 11:48:31 +0100679 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
680 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
681
682 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100683}
684#endif