blob: 3fbc382eb926d418564ae514ef71cdccd4df0483 [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 struct blkpg_partition p;
20 long long start, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22 if (!capable(CAP_SYS_ADMIN))
23 return -EACCES;
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010024 if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 return -EFAULT;
Christoph Hellwigfa01b1e2020-09-03 07:40:57 +020026 if (bdev_is_partition(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 return -EINVAL;
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020028
29 if (p.pno <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return -EINVAL;
Tejun Heo88e34122008-08-25 19:30:16 +090031
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020032 if (op == BLKPG_DEL_PARTITION)
33 return bdev_del_partition(bdev, p.pno);
Tejun Heo88e34122008-08-25 19:30:16 +090034
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020035 start = p.start >> SECTOR_SHIFT;
36 length = p.length >> SECTOR_SHIFT;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020037
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020038 /* check for fit in a hd_struct */
39 if (sizeof(sector_t) < sizeof(long long)) {
40 long pstart = start, plength = length;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020041
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020042 if (pstart != start || plength != length || pstart < 0 ||
43 plength < 0 || p.pno > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return -EINVAL;
45 }
Christoph Hellwigfa9156a2020-04-14 09:28:53 +020046
47 switch (op) {
48 case BLKPG_ADD_PARTITION:
49 /* check if partition is aligned to blocksize */
50 if (p.start & (bdev_logical_block_size(bdev) - 1))
51 return -EINVAL;
52 return bdev_add_partition(bdev, p.pno, start, length);
53 case BLKPG_RESIZE_PARTITION:
54 return bdev_resize_partition(bdev, p.pno, start, length);
55 default:
56 return -EINVAL;
57 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010060static int blkpg_ioctl(struct block_device *bdev,
61 struct blkpg_ioctl_arg __user *arg)
62{
63 struct blkpg_partition __user *udata;
64 int op;
65
66 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
67 return -EFAULT;
68
69 return blkpg_do_ioctl(bdev, udata, op);
70}
71
72#ifdef CONFIG_COMPAT
73struct compat_blkpg_ioctl_arg {
74 compat_int_t op;
75 compat_int_t flags;
76 compat_int_t datalen;
77 compat_caddr_t data;
78};
79
80static int compat_blkpg_ioctl(struct block_device *bdev,
81 struct compat_blkpg_ioctl_arg __user *arg)
82{
83 compat_caddr_t udata;
84 int op;
85
86 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
87 return -EFAULT;
88
89 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
90}
91#endif
92
Christoph Hellwigf0b870d2019-11-14 15:34:36 +010093static int blkdev_reread_part(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Christoph Hellwigf0b870d2019-11-14 15:34:36 +010095 int ret;
96
Christoph Hellwigfa01b1e2020-09-03 07:40:57 +020097 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EINVAL;
99 if (!capable(CAP_SYS_ADMIN))
100 return -EACCES;
Jarod Wilsonbe324172015-05-06 12:26:22 +0800101
Ming Leib04a56362015-05-06 12:26:27 +0800102 mutex_lock(&bdev->bd_mutex);
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100103 ret = bdev_disk_changed(bdev, false);
Arjan van de Venc039e312006-03-23 03:00:28 -0800104 mutex_unlock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800105
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100106 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200109static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
110 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100111{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200112 uint64_t range[2];
113 uint64_t start, len;
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600114 struct request_queue *q = bdev_get_queue(bdev);
Jan Kara384d87e2020-09-04 10:58:52 +0200115 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200116
117 if (!(mode & FMODE_WRITE))
118 return -EBADF;
119
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600120 if (!blk_queue_discard(q))
121 return -EOPNOTSUPP;
122
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200123 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
124 return -EFAULT;
125
126 start = range[0];
127 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700128
David Woodhoused30a2602008-08-11 15:58:42 +0100129 if (start & 511)
130 return -EINVAL;
131 if (len & 511)
132 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100133
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600134 if (start + len > i_size_read(bdev->bd_inode))
David Woodhoused30a2602008-08-11 15:58:42 +0100135 return -EINVAL;
Jan Kara384d87e2020-09-04 10:58:52 +0200136
137 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
138 if (err)
139 return err;
140
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600141 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
142 GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100143}
144
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200145static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
146 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400147{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200148 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700149 uint64_t start, end, len;
Jan Kara384d87e2020-09-04 10:58:52 +0200150 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200151
152 if (!(mode & FMODE_WRITE))
153 return -EBADF;
154
155 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
156 return -EFAULT;
157
158 start = range[0];
159 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700160 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200161
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400162 if (start & 511)
163 return -EINVAL;
164 if (len & 511)
165 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700166 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
167 return -EINVAL;
168 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400169 return -EINVAL;
170
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700171 /* Invalidate the page cache, including dirty pages */
Jan Kara384d87e2020-09-04 10:58:52 +0200172 err = truncate_bdev_range(bdev, mode, start, end);
173 if (err)
174 return err;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700175
176 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
Christoph Hellwigee472d82017-04-05 19:21:08 +0200177 BLKDEV_ZERO_NOUNMAP);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400178}
179
Arnd Bergmann9b816482019-11-29 11:48:31 +0100180static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100182 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Arnd Bergmann9b816482019-11-29 11:48:31 +0100185static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100187 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Arnd Bergmann9b816482019-11-29 11:48:31 +0100190static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200191{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100192 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200193}
194
Arnd Bergmann9b816482019-11-29 11:48:31 +0100195static int put_long(long __user *argp, long 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_ulong(unsigned long __user *argp, unsigned long 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_u64(u64 __user *argp, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100207 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100210#ifdef CONFIG_COMPAT
Bart Van Asschec8210a52020-05-18 21:07:34 -0700211static int compat_put_long(compat_long_t __user *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100212{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100213 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100214}
215
Bart Van Asschec8210a52020-05-18 21:07:34 -0700216static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100217{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100218 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100219}
220#endif
221
Al Viro633a08b2007-08-29 20:34:12 -0400222int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
223 unsigned cmd, unsigned long arg)
224{
225 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d62008-03-02 09:09:22 -0500226
227 if (disk->fops->ioctl)
228 return disk->fops->ioctl(bdev, mode, cmd, arg);
229
Al Viro633a08b2007-08-29 20:34:12 -0400230 return -ENOTTY;
231}
232/*
233 * For the record: _GPL here is only because somebody decided to slap it
234 * on the previous export. Sheer idiocy, since it wasn't copyrightable
235 * at all and could be open-coded without any exports by anybody who cares.
236 */
237EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
238
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100239#ifdef CONFIG_COMPAT
240/*
241 * This is the equivalent of compat_ptr_ioctl(), to be used by block
242 * drivers that implement only commands that are completely compatible
243 * between 32-bit and 64-bit user space
244 */
245int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
246 unsigned cmd, unsigned long arg)
247{
248 struct gendisk *disk = bdev->bd_disk;
249
250 if (disk->fops->ioctl)
251 return disk->fops->ioctl(bdev, mode, cmd,
252 (unsigned long)compat_ptr(arg));
253
254 return -ENOIOCTLCMD;
255}
256EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
257#endif
258
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200259static int blkdev_pr_register(struct block_device *bdev,
260 struct pr_registration __user *arg)
261{
262 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
263 struct pr_registration reg;
264
265 if (!capable(CAP_SYS_ADMIN))
266 return -EPERM;
267 if (!ops || !ops->pr_register)
268 return -EOPNOTSUPP;
269 if (copy_from_user(&reg, arg, sizeof(reg)))
270 return -EFAULT;
271
272 if (reg.flags & ~PR_FL_IGNORE_KEY)
273 return -EOPNOTSUPP;
274 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
275}
276
277static int blkdev_pr_reserve(struct block_device *bdev,
278 struct pr_reservation __user *arg)
279{
280 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
281 struct pr_reservation rsv;
282
283 if (!capable(CAP_SYS_ADMIN))
284 return -EPERM;
285 if (!ops || !ops->pr_reserve)
286 return -EOPNOTSUPP;
287 if (copy_from_user(&rsv, arg, sizeof(rsv)))
288 return -EFAULT;
289
290 if (rsv.flags & ~PR_FL_IGNORE_KEY)
291 return -EOPNOTSUPP;
292 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
293}
294
295static int blkdev_pr_release(struct block_device *bdev,
296 struct pr_reservation __user *arg)
297{
298 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
299 struct pr_reservation rsv;
300
301 if (!capable(CAP_SYS_ADMIN))
302 return -EPERM;
303 if (!ops || !ops->pr_release)
304 return -EOPNOTSUPP;
305 if (copy_from_user(&rsv, arg, sizeof(rsv)))
306 return -EFAULT;
307
308 if (rsv.flags)
309 return -EOPNOTSUPP;
310 return ops->pr_release(bdev, rsv.key, rsv.type);
311}
312
313static int blkdev_pr_preempt(struct block_device *bdev,
314 struct pr_preempt __user *arg, bool abort)
315{
316 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
317 struct pr_preempt p;
318
319 if (!capable(CAP_SYS_ADMIN))
320 return -EPERM;
321 if (!ops || !ops->pr_preempt)
322 return -EOPNOTSUPP;
323 if (copy_from_user(&p, arg, sizeof(p)))
324 return -EFAULT;
325
326 if (p.flags)
327 return -EOPNOTSUPP;
328 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
329}
330
331static int blkdev_pr_clear(struct block_device *bdev,
332 struct pr_clear __user *arg)
333{
334 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
335 struct pr_clear c;
336
337 if (!capable(CAP_SYS_ADMIN))
338 return -EPERM;
339 if (!ops || !ops->pr_clear)
340 return -EOPNOTSUPP;
341 if (copy_from_user(&c, arg, sizeof(c)))
342 return -EFAULT;
343
344 if (c.flags)
345 return -EOPNOTSUPP;
346 return ops->pr_clear(bdev, c.key);
347}
348
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200349/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800350 * Is it an unrecognized ioctl? The correct returns are either
351 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
352 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
353 * code before returning.
354 *
355 * Confused drivers sometimes return EINVAL, which is wrong. It
356 * means "I understood the ioctl command, but the parameters to
357 * it were wrong".
358 *
359 * We should aim to just fix the broken drivers, the EINVAL case
360 * should go away.
361 */
362static inline int is_unrecognized_ioctl(int ret)
363{
364 return ret == -EINVAL ||
365 ret == -ENOTTY ||
366 ret == -ENOIOCTLCMD;
367}
368
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200369static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
370 unsigned cmd, unsigned long arg)
371{
372 int ret;
373
374 if (!capable(CAP_SYS_ADMIN))
375 return -EACCES;
376
377 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
378 if (!is_unrecognized_ioctl(ret))
379 return ret;
380
381 fsync_bdev(bdev);
382 invalidate_bdev(bdev);
383 return 0;
384}
385
386static int blkdev_roset(struct block_device *bdev, fmode_t mode,
387 unsigned cmd, unsigned long arg)
388{
389 int ret, n;
390
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200391 if (!capable(CAP_SYS_ADMIN))
392 return -EACCES;
393
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200394 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
395 if (!is_unrecognized_ioctl(ret))
396 return ret;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200397 if (get_user(n, (int __user *)arg))
398 return -EFAULT;
399 set_device_ro(bdev, n);
400 return 0;
401}
402
403static int blkdev_getgeo(struct block_device *bdev,
404 struct hd_geometry __user *argp)
405{
406 struct gendisk *disk = bdev->bd_disk;
407 struct hd_geometry geo;
408 int ret;
409
410 if (!argp)
411 return -EINVAL;
412 if (!disk->fops->getgeo)
413 return -ENOTTY;
414
415 /*
416 * We need to set the startsect first, the driver may
417 * want to override it.
418 */
419 memset(&geo, 0, sizeof(geo));
420 geo.start = get_start_sect(bdev);
421 ret = disk->fops->getgeo(bdev, &geo);
422 if (ret)
423 return ret;
424 if (copy_to_user(argp, &geo, sizeof(geo)))
425 return -EFAULT;
426 return 0;
427}
428
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100429#ifdef CONFIG_COMPAT
430struct compat_hd_geometry {
431 unsigned char heads;
432 unsigned char sectors;
433 unsigned short cylinders;
434 u32 start;
435};
436
Arnd Bergmann9b816482019-11-29 11:48:31 +0100437static int compat_hdio_getgeo(struct block_device *bdev,
438 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100439{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100440 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100441 struct hd_geometry geo;
442 int ret;
443
444 if (!ugeo)
445 return -EINVAL;
446 if (!disk->fops->getgeo)
447 return -ENOTTY;
448
449 memset(&geo, 0, sizeof(geo));
450 /*
451 * We need to set the startsect first, the driver may
452 * want to override it.
453 */
454 geo.start = get_start_sect(bdev);
455 ret = disk->fops->getgeo(bdev, &geo);
456 if (ret)
457 return ret;
458
459 ret = copy_to_user(ugeo, &geo, 4);
460 ret |= put_user(geo.start, &ugeo->start);
461 if (ret)
462 ret = -EFAULT;
463
464 return ret;
465}
466#endif
467
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200468/* set the logical block size */
469static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
470 int __user *argp)
471{
472 int ret, n;
473
474 if (!capable(CAP_SYS_ADMIN))
475 return -EACCES;
476 if (!argp)
477 return -EINVAL;
478 if (get_user(n, argp))
479 return -EFAULT;
480
Christoph Hellwig47816282020-09-21 09:19:47 +0200481 if (mode & FMODE_EXCL)
482 return set_blocksize(bdev, n);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200483
Christoph Hellwig47816282020-09-21 09:19:47 +0200484 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
485 return -EBUSY;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200486 ret = set_blocksize(bdev, n);
Christoph Hellwig47816282020-09-21 09:19:47 +0200487 blkdev_put(bdev, mode | FMODE_EXCL);
488
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200489 return ret;
490}
491
Linus Torvalds07d106d2012-01-05 15:40:12 -0800492/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100493 * Common commands that are handled the same way on native and compat
494 * user space. Note the separate arg/argp parameters that are needed
495 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200496 */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100497static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
498 unsigned cmd, unsigned long arg, void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700499{
Akinobu Mita63f26492014-05-25 21:43:33 +0900500 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700501
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200502 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200504 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200506 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700507 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200508 return blk_ioctl_discard(bdev, mode, arg, 0);
509 case BLKSECDISCARD:
510 return blk_ioctl_discard(bdev, mode, arg,
511 BLKDEV_DISCARD_SECURE);
512 case BLKZEROOUT:
513 return blk_ioctl_zeroout(bdev, mode, arg);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900514 case BLKREPORTZONE:
515 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
516 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900517 case BLKOPENZONE:
518 case BLKCLOSEZONE:
519 case BLKFINISHZONE:
520 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900521 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100522 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900523 case BLKGETNRZONES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100524 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
Al Viro45048d02008-09-18 15:53:24 -0400525 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100526 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200527 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100528 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200529 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100530 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200531 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100532 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200533 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100534 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200535 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100536 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100537 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100538 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400539 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900540 max_sectors = min_t(unsigned int, USHRT_MAX,
541 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100542 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100543 case BLKROTATIONAL:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100544 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400545 case BLKRASET:
546 case BLKFRASET:
547 if(!capable(CAP_SYS_ADMIN))
548 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100549 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400550 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400551 case BLKRRPART:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200552 return blkdev_reread_part(bdev);
Al Viro45048d02008-09-18 15:53:24 -0400553 case BLKTRACESTART:
554 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400555 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200556 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200557 case IOC_PR_REGISTER:
558 return blkdev_pr_register(bdev, argp);
559 case IOC_PR_RESERVE:
560 return blkdev_pr_reserve(bdev, argp);
561 case IOC_PR_RELEASE:
562 return blkdev_pr_release(bdev, argp);
563 case IOC_PR_PREEMPT:
564 return blkdev_pr_preempt(bdev, argp, false);
565 case IOC_PR_PREEMPT_ABORT:
566 return blkdev_pr_preempt(bdev, argp, true);
567 case IOC_PR_CLEAR:
568 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400569 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100570 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100573
574/*
575 * Always keep this in sync with compat_blkdev_ioctl()
576 * to handle all incompatible commands in both functions.
577 *
578 * New commands must be compatible and go into blkdev_common_ioctl
579 */
580int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
581 unsigned long arg)
582{
583 int ret;
584 loff_t size;
585 void __user *argp = (void __user *)arg;
586
587 switch (cmd) {
588 /* These need separate implementations for the data structure */
589 case HDIO_GETGEO:
590 return blkdev_getgeo(bdev, argp);
591 case BLKPG:
592 return blkpg_ioctl(bdev, argp);
593
594 /* Compat mode returns 32-bit data instead of 'long' */
595 case BLKRAGET:
596 case BLKFRAGET:
597 if (!argp)
598 return -EINVAL;
599 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
600 case BLKGETSIZE:
601 size = i_size_read(bdev->bd_inode);
602 if ((size >> 9) > ~0UL)
603 return -EFBIG;
604 return put_ulong(argp, size >> 9);
605
606 /* The data is compatible, but the command number is different */
607 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
608 return put_int(argp, block_size(bdev));
609 case BLKBSZSET:
610 return blkdev_bszset(bdev, mode, argp);
611 case BLKGETSIZE64:
612 return put_u64(argp, i_size_read(bdev->bd_inode));
613
614 /* Incompatible alignment on i386 */
615 case BLKTRACESETUP:
616 return blk_trace_ioctl(bdev, cmd, argp);
617 default:
618 break;
619 }
620
621 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
622 if (ret == -ENOIOCTLCMD)
623 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
624
625 return ret;
626}
627EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100628
629#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100630
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100631#define BLKBSZGET_32 _IOR(0x12, 112, int)
632#define BLKBSZSET_32 _IOW(0x12, 113, int)
633#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
634
635/* Most of the generic ioctls are handled in the normal fallback path.
636 This assumes the blkdev's low level compat_ioctl always returns
637 ENOIOCTLCMD for unknown ioctls. */
638long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
639{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100640 int ret;
641 void __user *argp = compat_ptr(arg);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100642 struct inode *inode = file->f_mapping->host;
643 struct block_device *bdev = inode->i_bdev;
644 struct gendisk *disk = bdev->bd_disk;
645 fmode_t mode = file->f_mode;
646 loff_t size;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100647
648 /*
649 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
650 * to updated it before every ioctl.
651 */
652 if (file->f_flags & O_NDELAY)
653 mode |= FMODE_NDELAY;
654 else
655 mode &= ~FMODE_NDELAY;
656
657 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100658 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100659 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100660 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100661 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100662 return compat_blkpg_ioctl(bdev, argp);
663
664 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100665 case BLKRAGET:
666 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100667 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100668 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100669 return compat_put_long(argp,
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100670 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100671 case BLKGETSIZE:
672 size = i_size_read(bdev->bd_inode);
673 if ((size >> 9) > ~0UL)
674 return -EFBIG;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100675 return compat_put_ulong(argp, size >> 9);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100676
Arnd Bergmann9b816482019-11-29 11:48:31 +0100677 /* The data is compatible, but the command number is different */
678 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
679 return put_int(argp, bdev_logical_block_size(bdev));
680 case BLKBSZSET_32:
681 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100682 case BLKGETSIZE64_32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100683 return put_u64(argp, i_size_read(bdev->bd_inode));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100684
Arnd Bergmann9b816482019-11-29 11:48:31 +0100685 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100686 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100687 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100688 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100689 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100690 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100691
692 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
693 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
694 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
695
696 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100697}
698#endif