blob: bdb3bbb253d9f8772cce383105b5ac86f9f32419 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 if (bdev != bdev->bd_contains)
27 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 Hellwig142fe8f2019-11-14 15:34:35 +010097 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains)
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);
115 struct address_space *mapping = bdev->bd_inode->i_mapping;
116
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200117
118 if (!(mode & FMODE_WRITE))
119 return -EBADF;
120
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600121 if (!blk_queue_discard(q))
122 return -EOPNOTSUPP;
123
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200124 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
125 return -EFAULT;
126
127 start = range[0];
128 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700129
David Woodhoused30a2602008-08-11 15:58:42 +0100130 if (start & 511)
131 return -EINVAL;
132 if (len & 511)
133 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100134
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600135 if (start + len > i_size_read(bdev->bd_inode))
David Woodhoused30a2602008-08-11 15:58:42 +0100136 return -EINVAL;
Ming Lei0bd1ed42018-02-10 08:46:17 +0800137 truncate_inode_pages_range(mapping, start, start + len - 1);
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600138 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
139 GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100140}
141
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200142static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
143 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400144{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200145 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700146 struct address_space *mapping;
147 uint64_t start, end, len;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200148
149 if (!(mode & FMODE_WRITE))
150 return -EBADF;
151
152 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
153 return -EFAULT;
154
155 start = range[0];
156 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700157 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200158
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400159 if (start & 511)
160 return -EINVAL;
161 if (len & 511)
162 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700163 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
164 return -EINVAL;
165 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400166 return -EINVAL;
167
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700168 /* Invalidate the page cache, including dirty pages */
169 mapping = bdev->bd_inode->i_mapping;
170 truncate_inode_pages_range(mapping, start, end);
171
172 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
Christoph Hellwigee472d82017-04-05 19:21:08 +0200173 BLKDEV_ZERO_NOUNMAP);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400174}
175
Arnd Bergmann9b816482019-11-29 11:48:31 +0100176static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100178 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Arnd Bergmann9b816482019-11-29 11:48:31 +0100181static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100183 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Arnd Bergmann9b816482019-11-29 11:48:31 +0100186static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200187{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100188 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200189}
190
Arnd Bergmann9b816482019-11-29 11:48:31 +0100191static int put_long(long __user *argp, long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100193 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Arnd Bergmann9b816482019-11-29 11:48:31 +0100196static int put_ulong(unsigned long __user *argp, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100198 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Arnd Bergmann9b816482019-11-29 11:48:31 +0100201static int put_u64(u64 __user *argp, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100203 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100206#ifdef CONFIG_COMPAT
Bart Van Asschec8210a52020-05-18 21:07:34 -0700207static int compat_put_long(compat_long_t __user *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100208{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100209 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100210}
211
Bart Van Asschec8210a52020-05-18 21:07:34 -0700212static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100213{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100214 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100215}
216#endif
217
Al Viro633a08b2007-08-29 20:34:12 -0400218int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
219 unsigned cmd, unsigned long arg)
220{
221 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d62008-03-02 09:09:22 -0500222
223 if (disk->fops->ioctl)
224 return disk->fops->ioctl(bdev, mode, cmd, arg);
225
Al Viro633a08b2007-08-29 20:34:12 -0400226 return -ENOTTY;
227}
228/*
229 * For the record: _GPL here is only because somebody decided to slap it
230 * on the previous export. Sheer idiocy, since it wasn't copyrightable
231 * at all and could be open-coded without any exports by anybody who cares.
232 */
233EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
234
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100235#ifdef CONFIG_COMPAT
236/*
237 * This is the equivalent of compat_ptr_ioctl(), to be used by block
238 * drivers that implement only commands that are completely compatible
239 * between 32-bit and 64-bit user space
240 */
241int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
242 unsigned cmd, unsigned long arg)
243{
244 struct gendisk *disk = bdev->bd_disk;
245
246 if (disk->fops->ioctl)
247 return disk->fops->ioctl(bdev, mode, cmd,
248 (unsigned long)compat_ptr(arg));
249
250 return -ENOIOCTLCMD;
251}
252EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
253#endif
254
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200255static int blkdev_pr_register(struct block_device *bdev,
256 struct pr_registration __user *arg)
257{
258 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
259 struct pr_registration reg;
260
261 if (!capable(CAP_SYS_ADMIN))
262 return -EPERM;
263 if (!ops || !ops->pr_register)
264 return -EOPNOTSUPP;
265 if (copy_from_user(&reg, arg, sizeof(reg)))
266 return -EFAULT;
267
268 if (reg.flags & ~PR_FL_IGNORE_KEY)
269 return -EOPNOTSUPP;
270 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
271}
272
273static int blkdev_pr_reserve(struct block_device *bdev,
274 struct pr_reservation __user *arg)
275{
276 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
277 struct pr_reservation rsv;
278
279 if (!capable(CAP_SYS_ADMIN))
280 return -EPERM;
281 if (!ops || !ops->pr_reserve)
282 return -EOPNOTSUPP;
283 if (copy_from_user(&rsv, arg, sizeof(rsv)))
284 return -EFAULT;
285
286 if (rsv.flags & ~PR_FL_IGNORE_KEY)
287 return -EOPNOTSUPP;
288 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
289}
290
291static int blkdev_pr_release(struct block_device *bdev,
292 struct pr_reservation __user *arg)
293{
294 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
295 struct pr_reservation rsv;
296
297 if (!capable(CAP_SYS_ADMIN))
298 return -EPERM;
299 if (!ops || !ops->pr_release)
300 return -EOPNOTSUPP;
301 if (copy_from_user(&rsv, arg, sizeof(rsv)))
302 return -EFAULT;
303
304 if (rsv.flags)
305 return -EOPNOTSUPP;
306 return ops->pr_release(bdev, rsv.key, rsv.type);
307}
308
309static int blkdev_pr_preempt(struct block_device *bdev,
310 struct pr_preempt __user *arg, bool abort)
311{
312 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
313 struct pr_preempt p;
314
315 if (!capable(CAP_SYS_ADMIN))
316 return -EPERM;
317 if (!ops || !ops->pr_preempt)
318 return -EOPNOTSUPP;
319 if (copy_from_user(&p, arg, sizeof(p)))
320 return -EFAULT;
321
322 if (p.flags)
323 return -EOPNOTSUPP;
324 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
325}
326
327static int blkdev_pr_clear(struct block_device *bdev,
328 struct pr_clear __user *arg)
329{
330 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
331 struct pr_clear c;
332
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
335 if (!ops || !ops->pr_clear)
336 return -EOPNOTSUPP;
337 if (copy_from_user(&c, arg, sizeof(c)))
338 return -EFAULT;
339
340 if (c.flags)
341 return -EOPNOTSUPP;
342 return ops->pr_clear(bdev, c.key);
343}
344
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200345/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800346 * Is it an unrecognized ioctl? The correct returns are either
347 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
348 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
349 * code before returning.
350 *
351 * Confused drivers sometimes return EINVAL, which is wrong. It
352 * means "I understood the ioctl command, but the parameters to
353 * it were wrong".
354 *
355 * We should aim to just fix the broken drivers, the EINVAL case
356 * should go away.
357 */
358static inline int is_unrecognized_ioctl(int ret)
359{
360 return ret == -EINVAL ||
361 ret == -ENOTTY ||
362 ret == -ENOIOCTLCMD;
363}
364
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200365static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
366 unsigned cmd, unsigned long arg)
367{
368 int ret;
369
370 if (!capable(CAP_SYS_ADMIN))
371 return -EACCES;
372
373 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
374 if (!is_unrecognized_ioctl(ret))
375 return ret;
376
377 fsync_bdev(bdev);
378 invalidate_bdev(bdev);
379 return 0;
380}
381
382static int blkdev_roset(struct block_device *bdev, fmode_t mode,
383 unsigned cmd, unsigned long arg)
384{
385 int ret, n;
386
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200387 if (!capable(CAP_SYS_ADMIN))
388 return -EACCES;
389
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200390 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
391 if (!is_unrecognized_ioctl(ret))
392 return ret;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200393 if (get_user(n, (int __user *)arg))
394 return -EFAULT;
395 set_device_ro(bdev, n);
396 return 0;
397}
398
399static int blkdev_getgeo(struct block_device *bdev,
400 struct hd_geometry __user *argp)
401{
402 struct gendisk *disk = bdev->bd_disk;
403 struct hd_geometry geo;
404 int ret;
405
406 if (!argp)
407 return -EINVAL;
408 if (!disk->fops->getgeo)
409 return -ENOTTY;
410
411 /*
412 * We need to set the startsect first, the driver may
413 * want to override it.
414 */
415 memset(&geo, 0, sizeof(geo));
416 geo.start = get_start_sect(bdev);
417 ret = disk->fops->getgeo(bdev, &geo);
418 if (ret)
419 return ret;
420 if (copy_to_user(argp, &geo, sizeof(geo)))
421 return -EFAULT;
422 return 0;
423}
424
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100425#ifdef CONFIG_COMPAT
426struct compat_hd_geometry {
427 unsigned char heads;
428 unsigned char sectors;
429 unsigned short cylinders;
430 u32 start;
431};
432
Arnd Bergmann9b816482019-11-29 11:48:31 +0100433static int compat_hdio_getgeo(struct block_device *bdev,
434 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100435{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100436 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100437 struct hd_geometry geo;
438 int ret;
439
440 if (!ugeo)
441 return -EINVAL;
442 if (!disk->fops->getgeo)
443 return -ENOTTY;
444
445 memset(&geo, 0, sizeof(geo));
446 /*
447 * We need to set the startsect first, the driver may
448 * want to override it.
449 */
450 geo.start = get_start_sect(bdev);
451 ret = disk->fops->getgeo(bdev, &geo);
452 if (ret)
453 return ret;
454
455 ret = copy_to_user(ugeo, &geo, 4);
456 ret |= put_user(geo.start, &ugeo->start);
457 if (ret)
458 ret = -EFAULT;
459
460 return ret;
461}
462#endif
463
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200464/* set the logical block size */
465static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
466 int __user *argp)
467{
468 int ret, n;
469
470 if (!capable(CAP_SYS_ADMIN))
471 return -EACCES;
472 if (!argp)
473 return -EINVAL;
474 if (get_user(n, argp))
475 return -EFAULT;
476
477 if (!(mode & FMODE_EXCL)) {
478 bdgrab(bdev);
479 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
480 return -EBUSY;
481 }
482
483 ret = set_blocksize(bdev, n);
484 if (!(mode & FMODE_EXCL))
485 blkdev_put(bdev, mode | FMODE_EXCL);
486 return ret;
487}
488
Linus Torvalds07d106d2012-01-05 15:40:12 -0800489/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100490 * Common commands that are handled the same way on native and compat
491 * user space. Note the separate arg/argp parameters that are needed
492 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200493 */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100494static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
495 unsigned cmd, unsigned long arg, void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700496{
Akinobu Mita63f26492014-05-25 21:43:33 +0900497 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700498
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200499 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200501 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200503 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700504 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200505 return blk_ioctl_discard(bdev, mode, arg, 0);
506 case BLKSECDISCARD:
507 return blk_ioctl_discard(bdev, mode, arg,
508 BLKDEV_DISCARD_SECURE);
509 case BLKZEROOUT:
510 return blk_ioctl_zeroout(bdev, mode, arg);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900511 case BLKREPORTZONE:
512 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
513 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900514 case BLKOPENZONE:
515 case BLKCLOSEZONE:
516 case BLKFINISHZONE:
517 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900518 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100519 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900520 case BLKGETNRZONES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100521 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
Al Viro45048d02008-09-18 15:53:24 -0400522 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100523 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200524 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100525 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200526 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100527 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200528 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100529 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200530 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100531 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200532 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100533 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100534 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100535 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400536 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900537 max_sectors = min_t(unsigned int, USHRT_MAX,
538 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100539 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100540 case BLKROTATIONAL:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100541 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400542 case BLKRASET:
543 case BLKFRASET:
544 if(!capable(CAP_SYS_ADMIN))
545 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100546 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400547 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400548 case BLKRRPART:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200549 return blkdev_reread_part(bdev);
Al Viro45048d02008-09-18 15:53:24 -0400550 case BLKTRACESTART:
551 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400552 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200553 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200554 case IOC_PR_REGISTER:
555 return blkdev_pr_register(bdev, argp);
556 case IOC_PR_RESERVE:
557 return blkdev_pr_reserve(bdev, argp);
558 case IOC_PR_RELEASE:
559 return blkdev_pr_release(bdev, argp);
560 case IOC_PR_PREEMPT:
561 return blkdev_pr_preempt(bdev, argp, false);
562 case IOC_PR_PREEMPT_ABORT:
563 return blkdev_pr_preempt(bdev, argp, true);
564 case IOC_PR_CLEAR:
565 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400566 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100567 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100570
571/*
572 * Always keep this in sync with compat_blkdev_ioctl()
573 * to handle all incompatible commands in both functions.
574 *
575 * New commands must be compatible and go into blkdev_common_ioctl
576 */
577int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
578 unsigned long arg)
579{
580 int ret;
581 loff_t size;
582 void __user *argp = (void __user *)arg;
583
584 switch (cmd) {
585 /* These need separate implementations for the data structure */
586 case HDIO_GETGEO:
587 return blkdev_getgeo(bdev, argp);
588 case BLKPG:
589 return blkpg_ioctl(bdev, argp);
590
591 /* Compat mode returns 32-bit data instead of 'long' */
592 case BLKRAGET:
593 case BLKFRAGET:
594 if (!argp)
595 return -EINVAL;
596 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
597 case BLKGETSIZE:
598 size = i_size_read(bdev->bd_inode);
599 if ((size >> 9) > ~0UL)
600 return -EFBIG;
601 return put_ulong(argp, size >> 9);
602
603 /* The data is compatible, but the command number is different */
604 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
605 return put_int(argp, block_size(bdev));
606 case BLKBSZSET:
607 return blkdev_bszset(bdev, mode, argp);
608 case BLKGETSIZE64:
609 return put_u64(argp, i_size_read(bdev->bd_inode));
610
611 /* Incompatible alignment on i386 */
612 case BLKTRACESETUP:
613 return blk_trace_ioctl(bdev, cmd, argp);
614 default:
615 break;
616 }
617
618 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
619 if (ret == -ENOIOCTLCMD)
620 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
621
622 return ret;
623}
624EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100625
626#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100627
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100628#define BLKBSZGET_32 _IOR(0x12, 112, int)
629#define BLKBSZSET_32 _IOW(0x12, 113, int)
630#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
631
632/* Most of the generic ioctls are handled in the normal fallback path.
633 This assumes the blkdev's low level compat_ioctl always returns
634 ENOIOCTLCMD for unknown ioctls. */
635long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
636{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100637 int ret;
638 void __user *argp = compat_ptr(arg);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100639 struct inode *inode = file->f_mapping->host;
640 struct block_device *bdev = inode->i_bdev;
641 struct gendisk *disk = bdev->bd_disk;
642 fmode_t mode = file->f_mode;
643 loff_t size;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100644
645 /*
646 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
647 * to updated it before every ioctl.
648 */
649 if (file->f_flags & O_NDELAY)
650 mode |= FMODE_NDELAY;
651 else
652 mode &= ~FMODE_NDELAY;
653
654 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100655 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100656 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100657 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100658 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100659 return compat_blkpg_ioctl(bdev, argp);
660
661 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100662 case BLKRAGET:
663 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100664 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100665 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100666 return compat_put_long(argp,
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100667 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100668 case BLKGETSIZE:
669 size = i_size_read(bdev->bd_inode);
670 if ((size >> 9) > ~0UL)
671 return -EFBIG;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100672 return compat_put_ulong(argp, size >> 9);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100673
Arnd Bergmann9b816482019-11-29 11:48:31 +0100674 /* The data is compatible, but the command number is different */
675 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
676 return put_int(argp, bdev_logical_block_size(bdev));
677 case BLKBSZSET_32:
678 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100679 case BLKGETSIZE64_32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100680 return put_u64(argp, i_size_read(bdev->bd_inode));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100681
Arnd Bergmann9b816482019-11-29 11:48:31 +0100682 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100683 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100684 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100685 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100686 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100687 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100688
689 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
690 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
691 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
692
693 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100694}
695#endif