blob: e7eed7dadb5cf5867b21efc59a125dc9dc43ca87 [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 Hellwigcc88a812021-02-23 16:18:22 +010093static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Christoph Hellwigcc88a812021-02-23 16:18:22 +010095 struct block_device *tmp;
Christoph Hellwigf0b870d2019-11-14 15:34:36 +010096
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;
Christoph Hellwigfc2454c2021-04-21 18:05:02 +0200101 if (bdev->bd_part_count)
102 return -EBUSY;
Jarod Wilsonbe324172015-05-06 12:26:22 +0800103
Christoph Hellwigcc88a812021-02-23 16:18:22 +0100104 /*
105 * Reopen the device to revalidate the driver state and force a
106 * partition rescan.
107 */
108 mode &= ~FMODE_EXCL;
109 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800110
Christoph Hellwigcc88a812021-02-23 16:18:22 +0100111 tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
112 if (IS_ERR(tmp))
113 return PTR_ERR(tmp);
114 blkdev_put(tmp, mode);
115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200118static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
119 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100120{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200121 uint64_t range[2];
122 uint64_t start, len;
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600123 struct request_queue *q = bdev_get_queue(bdev);
Jan Kara384d87e2020-09-04 10:58:52 +0200124 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200125
126 if (!(mode & FMODE_WRITE))
127 return -EBADF;
128
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600129 if (!blk_queue_discard(q))
130 return -EOPNOTSUPP;
131
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200132 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
133 return -EFAULT;
134
135 start = range[0];
136 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700137
David Woodhoused30a2602008-08-11 15:58:42 +0100138 if (start & 511)
139 return -EINVAL;
140 if (len & 511)
141 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100142
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600143 if (start + len > i_size_read(bdev->bd_inode))
David Woodhoused30a2602008-08-11 15:58:42 +0100144 return -EINVAL;
Jan Kara384d87e2020-09-04 10:58:52 +0200145
146 err = truncate_bdev_range(bdev, mode, start, start + len - 1);
147 if (err)
148 return err;
149
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600150 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
151 GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100152}
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;
Jan Kara384d87e2020-09-04 10:58:52 +0200159 int err;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200160
161 if (!(mode & FMODE_WRITE))
162 return -EBADF;
163
164 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
165 return -EFAULT;
166
167 start = range[0];
168 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700169 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200170
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400171 if (start & 511)
172 return -EINVAL;
173 if (len & 511)
174 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700175 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
176 return -EINVAL;
177 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400178 return -EINVAL;
179
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700180 /* Invalidate the page cache, including dirty pages */
Jan Kara384d87e2020-09-04 10:58:52 +0200181 err = truncate_bdev_range(bdev, mode, start, end);
182 if (err)
183 return err;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700184
185 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
Christoph Hellwigee472d82017-04-05 19:21:08 +0200186 BLKDEV_ZERO_NOUNMAP);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400187}
188
Arnd Bergmann9b816482019-11-29 11:48:31 +0100189static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100191 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Arnd Bergmann9b816482019-11-29 11:48:31 +0100194static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100196 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Arnd Bergmann9b816482019-11-29 11:48:31 +0100199static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200200{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100201 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200202}
203
Arnd Bergmann9b816482019-11-29 11:48:31 +0100204static int put_long(long __user *argp, long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100206 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Arnd Bergmann9b816482019-11-29 11:48:31 +0100209static int put_ulong(unsigned long __user *argp, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100211 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Arnd Bergmann9b816482019-11-29 11:48:31 +0100214static int put_u64(u64 __user *argp, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100216 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100219#ifdef CONFIG_COMPAT
Bart Van Asschec8210a52020-05-18 21:07:34 -0700220static int compat_put_long(compat_long_t __user *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100221{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100222 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100223}
224
Bart Van Asschec8210a52020-05-18 21:07:34 -0700225static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100226{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100227 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100228}
229#endif
230
Al Viro633a08b2007-08-29 20:34:12 -0400231int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
232 unsigned cmd, unsigned long arg)
233{
234 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d62008-03-02 09:09:22 -0500235
236 if (disk->fops->ioctl)
237 return disk->fops->ioctl(bdev, mode, cmd, arg);
238
Al Viro633a08b2007-08-29 20:34:12 -0400239 return -ENOTTY;
240}
241/*
242 * For the record: _GPL here is only because somebody decided to slap it
243 * on the previous export. Sheer idiocy, since it wasn't copyrightable
244 * at all and could be open-coded without any exports by anybody who cares.
245 */
246EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
247
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100248#ifdef CONFIG_COMPAT
249/*
250 * This is the equivalent of compat_ptr_ioctl(), to be used by block
251 * drivers that implement only commands that are completely compatible
252 * between 32-bit and 64-bit user space
253 */
254int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
255 unsigned cmd, unsigned long arg)
256{
257 struct gendisk *disk = bdev->bd_disk;
258
259 if (disk->fops->ioctl)
260 return disk->fops->ioctl(bdev, mode, cmd,
261 (unsigned long)compat_ptr(arg));
262
263 return -ENOIOCTLCMD;
264}
265EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
266#endif
267
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200268static int blkdev_pr_register(struct block_device *bdev,
269 struct pr_registration __user *arg)
270{
271 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
272 struct pr_registration reg;
273
274 if (!capable(CAP_SYS_ADMIN))
275 return -EPERM;
276 if (!ops || !ops->pr_register)
277 return -EOPNOTSUPP;
278 if (copy_from_user(&reg, arg, sizeof(reg)))
279 return -EFAULT;
280
281 if (reg.flags & ~PR_FL_IGNORE_KEY)
282 return -EOPNOTSUPP;
283 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
284}
285
286static int blkdev_pr_reserve(struct block_device *bdev,
287 struct pr_reservation __user *arg)
288{
289 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
290 struct pr_reservation rsv;
291
292 if (!capable(CAP_SYS_ADMIN))
293 return -EPERM;
294 if (!ops || !ops->pr_reserve)
295 return -EOPNOTSUPP;
296 if (copy_from_user(&rsv, arg, sizeof(rsv)))
297 return -EFAULT;
298
299 if (rsv.flags & ~PR_FL_IGNORE_KEY)
300 return -EOPNOTSUPP;
301 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
302}
303
304static int blkdev_pr_release(struct block_device *bdev,
305 struct pr_reservation __user *arg)
306{
307 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
308 struct pr_reservation rsv;
309
310 if (!capable(CAP_SYS_ADMIN))
311 return -EPERM;
312 if (!ops || !ops->pr_release)
313 return -EOPNOTSUPP;
314 if (copy_from_user(&rsv, arg, sizeof(rsv)))
315 return -EFAULT;
316
317 if (rsv.flags)
318 return -EOPNOTSUPP;
319 return ops->pr_release(bdev, rsv.key, rsv.type);
320}
321
322static int blkdev_pr_preempt(struct block_device *bdev,
323 struct pr_preempt __user *arg, bool abort)
324{
325 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
326 struct pr_preempt p;
327
328 if (!capable(CAP_SYS_ADMIN))
329 return -EPERM;
330 if (!ops || !ops->pr_preempt)
331 return -EOPNOTSUPP;
332 if (copy_from_user(&p, arg, sizeof(p)))
333 return -EFAULT;
334
335 if (p.flags)
336 return -EOPNOTSUPP;
337 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
338}
339
340static int blkdev_pr_clear(struct block_device *bdev,
341 struct pr_clear __user *arg)
342{
343 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
344 struct pr_clear c;
345
346 if (!capable(CAP_SYS_ADMIN))
347 return -EPERM;
348 if (!ops || !ops->pr_clear)
349 return -EOPNOTSUPP;
350 if (copy_from_user(&c, arg, sizeof(c)))
351 return -EFAULT;
352
353 if (c.flags)
354 return -EOPNOTSUPP;
355 return ops->pr_clear(bdev, c.key);
356}
357
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200358/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800359 * Is it an unrecognized ioctl? The correct returns are either
360 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
361 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
362 * code before returning.
363 *
364 * Confused drivers sometimes return EINVAL, which is wrong. It
365 * means "I understood the ioctl command, but the parameters to
366 * it were wrong".
367 *
368 * We should aim to just fix the broken drivers, the EINVAL case
369 * should go away.
370 */
371static inline int is_unrecognized_ioctl(int ret)
372{
373 return ret == -EINVAL ||
374 ret == -ENOTTY ||
375 ret == -ENOIOCTLCMD;
376}
377
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200378static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
379 unsigned cmd, unsigned long arg)
380{
381 int ret;
382
383 if (!capable(CAP_SYS_ADMIN))
384 return -EACCES;
385
386 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
387 if (!is_unrecognized_ioctl(ret))
388 return ret;
389
390 fsync_bdev(bdev);
391 invalidate_bdev(bdev);
392 return 0;
393}
394
395static int blkdev_roset(struct block_device *bdev, fmode_t mode,
396 unsigned cmd, unsigned long arg)
397{
398 int ret, n;
399
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200400 if (!capable(CAP_SYS_ADMIN))
401 return -EACCES;
402
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200403 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
404 if (!is_unrecognized_ioctl(ret))
405 return ret;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200406 if (get_user(n, (int __user *)arg))
407 return -EFAULT;
408 set_device_ro(bdev, n);
409 return 0;
410}
411
412static int blkdev_getgeo(struct block_device *bdev,
413 struct hd_geometry __user *argp)
414{
415 struct gendisk *disk = bdev->bd_disk;
416 struct hd_geometry geo;
417 int ret;
418
419 if (!argp)
420 return -EINVAL;
421 if (!disk->fops->getgeo)
422 return -ENOTTY;
423
424 /*
425 * We need to set the startsect first, the driver may
426 * want to override it.
427 */
428 memset(&geo, 0, sizeof(geo));
429 geo.start = get_start_sect(bdev);
430 ret = disk->fops->getgeo(bdev, &geo);
431 if (ret)
432 return ret;
433 if (copy_to_user(argp, &geo, sizeof(geo)))
434 return -EFAULT;
435 return 0;
436}
437
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100438#ifdef CONFIG_COMPAT
439struct compat_hd_geometry {
440 unsigned char heads;
441 unsigned char sectors;
442 unsigned short cylinders;
443 u32 start;
444};
445
Arnd Bergmann9b816482019-11-29 11:48:31 +0100446static int compat_hdio_getgeo(struct block_device *bdev,
447 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100448{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100449 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100450 struct hd_geometry geo;
451 int ret;
452
453 if (!ugeo)
454 return -EINVAL;
455 if (!disk->fops->getgeo)
456 return -ENOTTY;
457
458 memset(&geo, 0, sizeof(geo));
459 /*
460 * We need to set the startsect first, the driver may
461 * want to override it.
462 */
463 geo.start = get_start_sect(bdev);
464 ret = disk->fops->getgeo(bdev, &geo);
465 if (ret)
466 return ret;
467
468 ret = copy_to_user(ugeo, &geo, 4);
469 ret |= put_user(geo.start, &ugeo->start);
470 if (ret)
471 ret = -EFAULT;
472
473 return ret;
474}
475#endif
476
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200477/* set the logical block size */
478static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
479 int __user *argp)
480{
481 int ret, n;
482
483 if (!capable(CAP_SYS_ADMIN))
484 return -EACCES;
485 if (!argp)
486 return -EINVAL;
487 if (get_user(n, argp))
488 return -EFAULT;
489
Christoph Hellwig47816282020-09-21 09:19:47 +0200490 if (mode & FMODE_EXCL)
491 return set_blocksize(bdev, n);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200492
Christoph Hellwig47816282020-09-21 09:19:47 +0200493 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
494 return -EBUSY;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200495 ret = set_blocksize(bdev, n);
Christoph Hellwig47816282020-09-21 09:19:47 +0200496 blkdev_put(bdev, mode | FMODE_EXCL);
497
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200498 return ret;
499}
500
Linus Torvalds07d106d2012-01-05 15:40:12 -0800501/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100502 * Common commands that are handled the same way on native and compat
503 * user space. Note the separate arg/argp parameters that are needed
504 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200505 */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100506static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
507 unsigned cmd, unsigned long arg, void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700508{
Akinobu Mita63f26492014-05-25 21:43:33 +0900509 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700510
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200511 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200513 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200515 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700516 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200517 return blk_ioctl_discard(bdev, mode, arg, 0);
518 case BLKSECDISCARD:
519 return blk_ioctl_discard(bdev, mode, arg,
520 BLKDEV_DISCARD_SECURE);
521 case BLKZEROOUT:
522 return blk_ioctl_zeroout(bdev, mode, arg);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900523 case BLKREPORTZONE:
524 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
525 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900526 case BLKOPENZONE:
527 case BLKCLOSEZONE:
528 case BLKFINISHZONE:
529 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900530 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100531 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900532 case BLKGETNRZONES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100533 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
Al Viro45048d02008-09-18 15:53:24 -0400534 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100535 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200536 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100537 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200538 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100539 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200540 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100541 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200542 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100543 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200544 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100545 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100546 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100547 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400548 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900549 max_sectors = min_t(unsigned int, USHRT_MAX,
550 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100551 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100552 case BLKROTATIONAL:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100553 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400554 case BLKRASET:
555 case BLKFRASET:
556 if(!capable(CAP_SYS_ADMIN))
557 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100558 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400559 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400560 case BLKRRPART:
Christoph Hellwigcc88a812021-02-23 16:18:22 +0100561 return blkdev_reread_part(bdev, mode);
Al Viro45048d02008-09-18 15:53:24 -0400562 case BLKTRACESTART:
563 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400564 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200565 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200566 case IOC_PR_REGISTER:
567 return blkdev_pr_register(bdev, argp);
568 case IOC_PR_RESERVE:
569 return blkdev_pr_reserve(bdev, argp);
570 case IOC_PR_RELEASE:
571 return blkdev_pr_release(bdev, argp);
572 case IOC_PR_PREEMPT:
573 return blkdev_pr_preempt(bdev, argp, false);
574 case IOC_PR_PREEMPT_ABORT:
575 return blkdev_pr_preempt(bdev, argp, true);
576 case IOC_PR_CLEAR:
577 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400578 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100579 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100582
583/*
584 * Always keep this in sync with compat_blkdev_ioctl()
585 * to handle all incompatible commands in both functions.
586 *
587 * New commands must be compatible and go into blkdev_common_ioctl
588 */
589int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
590 unsigned long arg)
591{
592 int ret;
593 loff_t size;
594 void __user *argp = (void __user *)arg;
595
596 switch (cmd) {
597 /* These need separate implementations for the data structure */
598 case HDIO_GETGEO:
599 return blkdev_getgeo(bdev, argp);
600 case BLKPG:
601 return blkpg_ioctl(bdev, argp);
602
603 /* Compat mode returns 32-bit data instead of 'long' */
604 case BLKRAGET:
605 case BLKFRAGET:
606 if (!argp)
607 return -EINVAL;
608 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
609 case BLKGETSIZE:
610 size = i_size_read(bdev->bd_inode);
611 if ((size >> 9) > ~0UL)
612 return -EFBIG;
613 return put_ulong(argp, size >> 9);
614
615 /* The data is compatible, but the command number is different */
616 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
617 return put_int(argp, block_size(bdev));
618 case BLKBSZSET:
619 return blkdev_bszset(bdev, mode, argp);
620 case BLKGETSIZE64:
621 return put_u64(argp, i_size_read(bdev->bd_inode));
622
623 /* Incompatible alignment on i386 */
624 case BLKTRACESETUP:
625 return blk_trace_ioctl(bdev, cmd, argp);
626 default:
627 break;
628 }
629
630 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
631 if (ret == -ENOIOCTLCMD)
632 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
633
634 return ret;
635}
636EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100637
638#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100639
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100640#define BLKBSZGET_32 _IOR(0x12, 112, int)
641#define BLKBSZSET_32 _IOW(0x12, 113, int)
642#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
643
644/* Most of the generic ioctls are handled in the normal fallback path.
645 This assumes the blkdev's low level compat_ioctl always returns
646 ENOIOCTLCMD for unknown ioctls. */
647long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
648{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100649 int ret;
650 void __user *argp = compat_ptr(arg);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100651 struct inode *inode = file->f_mapping->host;
652 struct block_device *bdev = inode->i_bdev;
653 struct gendisk *disk = bdev->bd_disk;
654 fmode_t mode = file->f_mode;
655 loff_t size;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100656
657 /*
658 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
659 * to updated it before every ioctl.
660 */
661 if (file->f_flags & O_NDELAY)
662 mode |= FMODE_NDELAY;
663 else
664 mode &= ~FMODE_NDELAY;
665
666 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100667 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100668 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100669 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100670 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100671 return compat_blkpg_ioctl(bdev, argp);
672
673 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100674 case BLKRAGET:
675 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100676 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100677 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100678 return compat_put_long(argp,
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100679 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100680 case BLKGETSIZE:
681 size = i_size_read(bdev->bd_inode);
Khazhismel Kumykov8bedbc82022-04-14 15:40:56 -0700682 if ((size >> 9) > ~(compat_ulong_t)0)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100683 return -EFBIG;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100684 return compat_put_ulong(argp, size >> 9);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100685
Arnd Bergmann9b816482019-11-29 11:48:31 +0100686 /* The data is compatible, but the command number is different */
687 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
688 return put_int(argp, bdev_logical_block_size(bdev));
689 case BLKBSZSET_32:
690 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100691 case BLKGETSIZE64_32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100692 return put_u64(argp, i_size_read(bdev->bd_inode));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100693
Arnd Bergmann9b816482019-11-29 11:48:31 +0100694 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100695 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100696 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100697 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100698 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100699 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100700
701 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
702 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
703 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
704
705 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100706}
707#endif