blob: 698c7933d5826d82377264661d06b0884d865f24 [file] [log] [blame]
Randy.Dunlapc59ede72006-01-11 12:17:46 -08001#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/blkdev.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -04003#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/blkpg.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006#include <linux/hdreg.h>
Dan Williams57f7f312016-01-06 12:03:42 -08007#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/backing-dev.h>
Al Viroff01bb482011-09-16 02:31:11 -04009#include <linux/fs.h>
Jens Axboe2056a782006-03-23 20:00:26 +010010#include <linux/blktrace_api.h>
Christoph Hellwigbbd3e062015-10-15 14:10:48 +020011#include <linux/pr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/uaccess.h>
13
14static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
15{
16 struct block_device *bdevp;
17 struct gendisk *disk;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020018 struct hd_struct *part, *lpart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 struct blkpg_ioctl_arg a;
20 struct blkpg_partition p;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020021 struct disk_part_iter piter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 long long start, length;
Tejun Heocf771cb2008-09-03 09:01:09 +020023 int partno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25 if (!capable(CAP_SYS_ADMIN))
26 return -EACCES;
27 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
28 return -EFAULT;
29 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
30 return -EFAULT;
31 disk = bdev->bd_disk;
32 if (bdev != bdev->bd_contains)
33 return -EINVAL;
Tejun Heocf771cb2008-09-03 09:01:09 +020034 partno = p.pno;
Tejun Heo540eed52008-08-25 19:56:15 +090035 if (partno <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return -EINVAL;
37 switch (a.op) {
38 case BLKPG_ADD_PARTITION:
39 start = p.start >> 9;
40 length = p.length >> 9;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020041 /* check for fit in a hd_struct */
42 if (sizeof(sector_t) == sizeof(long) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 sizeof(long long) > sizeof(long)) {
44 long pstart = start, plength = length;
45 if (pstart != start || plength != length
Alan Cox2bd6efa2012-09-17 11:47:13 +010046 || pstart < 0 || plength < 0 || partno > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return -EINVAL;
48 }
Tejun Heo88e34122008-08-25 19:30:16 +090049
Arjan van de Venc039e312006-03-23 03:00:28 -080050 mutex_lock(&bdev->bd_mutex);
Tejun Heo88e34122008-08-25 19:30:16 +090051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 /* overlap? */
Tejun Heoe71bf0d2008-09-03 09:03:02 +020053 disk_part_iter_init(&piter, disk,
54 DISK_PITER_INCL_EMPTY);
55 while ((part = disk_part_iter_next(&piter))) {
56 if (!(start + length <= part->start_sect ||
57 start >= part->start_sect + part->nr_sects)) {
58 disk_part_iter_exit(&piter);
Arjan van de Venc039e312006-03-23 03:00:28 -080059 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -EBUSY;
61 }
62 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020063 disk_part_iter_exit(&piter);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 /* all seems OK */
Tejun Heoba329292008-11-10 15:29:58 +090066 part = add_partition(disk, partno, start, length,
Will Drewry6d1d8052010-08-31 15:47:05 -050067 ADDPART_FLAG_NONE, NULL);
Arjan van de Venc039e312006-03-23 03:00:28 -080068 mutex_unlock(&bdev->bd_mutex);
Duan Jiongc7d1ba42013-11-06 15:56:39 +080069 return PTR_ERR_OR_ZERO(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 case BLKPG_DEL_PARTITION:
Tejun Heoe71bf0d2008-09-03 09:03:02 +020071 part = disk_get_part(disk, partno);
72 if (!part)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -ENXIO;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020074
75 bdevp = bdget(part_devt(part));
76 disk_put_part(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!bdevp)
78 return -ENOMEM;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020079
Peter Zijlstra2e7b6512006-12-08 02:36:13 -080080 mutex_lock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (bdevp->bd_openers) {
Arjan van de Venc039e312006-03-23 03:00:28 -080082 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 bdput(bdevp);
84 return -EBUSY;
85 }
86 /* all seems OK */
87 fsync_bdev(bdevp);
Peter Zijlstraf98393a2007-05-06 14:49:54 -070088 invalidate_bdev(bdevp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Peter Zijlstra6d740cd2007-02-20 13:58:18 -080090 mutex_lock_nested(&bdev->bd_mutex, 1);
Tejun Heocf771cb2008-09-03 09:01:09 +020091 delete_partition(disk, partno);
Arjan van de Venc039e312006-03-23 03:00:28 -080092 mutex_unlock(&bdev->bd_mutex);
93 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 bdput(bdevp);
95
96 return 0;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020097 case BLKPG_RESIZE_PARTITION:
98 start = p.start >> 9;
99 /* new length of partition in bytes */
100 length = p.length >> 9;
101 /* check for fit in a hd_struct */
102 if (sizeof(sector_t) == sizeof(long) &&
103 sizeof(long long) > sizeof(long)) {
104 long pstart = start, plength = length;
105 if (pstart != start || plength != length
106 || pstart < 0 || plength < 0)
107 return -EINVAL;
108 }
109 part = disk_get_part(disk, partno);
110 if (!part)
111 return -ENXIO;
112 bdevp = bdget(part_devt(part));
113 if (!bdevp) {
114 disk_put_part(part);
115 return -ENOMEM;
116 }
117 mutex_lock(&bdevp->bd_mutex);
118 mutex_lock_nested(&bdev->bd_mutex, 1);
119 if (start != part->start_sect) {
120 mutex_unlock(&bdevp->bd_mutex);
121 mutex_unlock(&bdev->bd_mutex);
122 bdput(bdevp);
123 disk_put_part(part);
124 return -EINVAL;
125 }
126 /* overlap? */
127 disk_part_iter_init(&piter, disk,
128 DISK_PITER_INCL_EMPTY);
129 while ((lpart = disk_part_iter_next(&piter))) {
130 if (lpart->partno != partno &&
131 !(start + length <= lpart->start_sect ||
132 start >= lpart->start_sect + lpart->nr_sects)
133 ) {
134 disk_part_iter_exit(&piter);
135 mutex_unlock(&bdevp->bd_mutex);
136 mutex_unlock(&bdev->bd_mutex);
137 bdput(bdevp);
138 disk_put_part(part);
139 return -EBUSY;
140 }
141 }
142 disk_part_iter_exit(&piter);
143 part_nr_sects_write(part, (sector_t)length);
144 i_size_write(bdevp->bd_inode, p.length);
145 mutex_unlock(&bdevp->bd_mutex);
146 mutex_unlock(&bdev->bd_mutex);
147 bdput(bdevp);
148 disk_put_part(part);
149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 default:
151 return -EINVAL;
152 }
153}
154
Jarod Wilsonbe324172015-05-06 12:26:22 +0800155/*
156 * This is an exported API for the block driver, and will not
157 * acquire bd_mutex. This API should be used in case that
158 * caller has held bd_mutex already.
159 */
160int __blkdev_reread_part(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Tejun Heod27769e2011-08-23 20:01:04 +0200164 if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -EINVAL;
166 if (!capable(CAP_SYS_ADMIN))
167 return -EACCES;
Jarod Wilsonbe324172015-05-06 12:26:22 +0800168
169 lockdep_assert_held(&bdev->bd_mutex);
170
171 return rescan_partitions(disk, bdev);
172}
173EXPORT_SYMBOL(__blkdev_reread_part);
174
175/*
176 * This is an exported API for the block driver, and will
177 * try to acquire bd_mutex. If bd_mutex has been held already
178 * in current context, please call __blkdev_reread_part().
Ming Leib04a56362015-05-06 12:26:27 +0800179 *
180 * Make sure the held locks in current context aren't required
181 * in open()/close() handler and I/O path for avoiding ABBA deadlock:
182 * - bd_mutex is held before calling block driver's open/close
183 * handler
184 * - reading partition table may submit I/O to the block device
Jarod Wilsonbe324172015-05-06 12:26:22 +0800185 */
186int blkdev_reread_part(struct block_device *bdev)
187{
188 int res;
189
Ming Leib04a56362015-05-06 12:26:27 +0800190 mutex_lock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800191 res = __blkdev_reread_part(bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800192 mutex_unlock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return res;
195}
Jarod Wilsonbe324172015-05-06 12:26:22 +0800196EXPORT_SYMBOL(blkdev_reread_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200198static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
199 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100200{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200201 uint64_t range[2];
202 uint64_t start, len;
203
204 if (!(mode & FMODE_WRITE))
205 return -EBADF;
206
207 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
208 return -EFAULT;
209
210 start = range[0];
211 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700212
David Woodhoused30a2602008-08-11 15:58:42 +0100213 if (start & 511)
214 return -EINVAL;
215 if (len & 511)
216 return -EINVAL;
217 start >>= 9;
218 len >>= 9;
219
Mike Snitzer77304d22010-11-08 14:39:12 +0100220 if (start + len > (i_size_read(bdev->bd_inode) >> 9))
David Woodhoused30a2602008-08-11 15:58:42 +0100221 return -EINVAL;
Adrian Hunter8d57a982010-08-11 14:17:49 -0700222 return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100223}
224
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200225static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
226 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400227{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200228 uint64_t range[2];
229 uint64_t start, len;
230
231 if (!(mode & FMODE_WRITE))
232 return -EBADF;
233
234 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
235 return -EFAULT;
236
237 start = range[0];
238 len = range[1];
239
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400240 if (start & 511)
241 return -EINVAL;
242 if (len & 511)
243 return -EINVAL;
244 start >>= 9;
245 len >>= 9;
246
247 if (start + len > (i_size_read(bdev->bd_inode) >> 9))
248 return -EINVAL;
249
Martin K. Petersend93ba7a2015-01-20 20:06:30 -0500250 return blkdev_issue_zeroout(bdev, start, len, GFP_KERNEL, false);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int put_ushort(unsigned long arg, unsigned short val)
254{
255 return put_user(val, (unsigned short __user *)arg);
256}
257
258static int put_int(unsigned long arg, int val)
259{
260 return put_user(val, (int __user *)arg);
261}
262
Martin K. Petersenac481c22009-10-03 20:52:01 +0200263static int put_uint(unsigned long arg, unsigned int val)
264{
265 return put_user(val, (unsigned int __user *)arg);
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static int put_long(unsigned long arg, long val)
269{
270 return put_user(val, (long __user *)arg);
271}
272
273static int put_ulong(unsigned long arg, unsigned long val)
274{
275 return put_user(val, (unsigned long __user *)arg);
276}
277
278static int put_u64(unsigned long arg, u64 val)
279{
280 return put_user(val, (u64 __user *)arg);
281}
282
Al Viro633a08b2007-08-29 20:34:12 -0400283int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
284 unsigned cmd, unsigned long arg)
285{
286 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d622008-03-02 09:09:22 -0500287
288 if (disk->fops->ioctl)
289 return disk->fops->ioctl(bdev, mode, cmd, arg);
290
Al Viro633a08b2007-08-29 20:34:12 -0400291 return -ENOTTY;
292}
293/*
294 * For the record: _GPL here is only because somebody decided to slap it
295 * on the previous export. Sheer idiocy, since it wasn't copyrightable
296 * at all and could be open-coded without any exports by anybody who cares.
297 */
298EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
299
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200300static int blkdev_pr_register(struct block_device *bdev,
301 struct pr_registration __user *arg)
302{
303 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
304 struct pr_registration reg;
305
306 if (!capable(CAP_SYS_ADMIN))
307 return -EPERM;
308 if (!ops || !ops->pr_register)
309 return -EOPNOTSUPP;
310 if (copy_from_user(&reg, arg, sizeof(reg)))
311 return -EFAULT;
312
313 if (reg.flags & ~PR_FL_IGNORE_KEY)
314 return -EOPNOTSUPP;
315 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
316}
317
318static int blkdev_pr_reserve(struct block_device *bdev,
319 struct pr_reservation __user *arg)
320{
321 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
322 struct pr_reservation rsv;
323
324 if (!capable(CAP_SYS_ADMIN))
325 return -EPERM;
326 if (!ops || !ops->pr_reserve)
327 return -EOPNOTSUPP;
328 if (copy_from_user(&rsv, arg, sizeof(rsv)))
329 return -EFAULT;
330
331 if (rsv.flags & ~PR_FL_IGNORE_KEY)
332 return -EOPNOTSUPP;
333 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
334}
335
336static int blkdev_pr_release(struct block_device *bdev,
337 struct pr_reservation __user *arg)
338{
339 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
340 struct pr_reservation rsv;
341
342 if (!capable(CAP_SYS_ADMIN))
343 return -EPERM;
344 if (!ops || !ops->pr_release)
345 return -EOPNOTSUPP;
346 if (copy_from_user(&rsv, arg, sizeof(rsv)))
347 return -EFAULT;
348
349 if (rsv.flags)
350 return -EOPNOTSUPP;
351 return ops->pr_release(bdev, rsv.key, rsv.type);
352}
353
354static int blkdev_pr_preempt(struct block_device *bdev,
355 struct pr_preempt __user *arg, bool abort)
356{
357 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
358 struct pr_preempt p;
359
360 if (!capable(CAP_SYS_ADMIN))
361 return -EPERM;
362 if (!ops || !ops->pr_preempt)
363 return -EOPNOTSUPP;
364 if (copy_from_user(&p, arg, sizeof(p)))
365 return -EFAULT;
366
367 if (p.flags)
368 return -EOPNOTSUPP;
369 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
370}
371
372static int blkdev_pr_clear(struct block_device *bdev,
373 struct pr_clear __user *arg)
374{
375 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
376 struct pr_clear c;
377
378 if (!capable(CAP_SYS_ADMIN))
379 return -EPERM;
380 if (!ops || !ops->pr_clear)
381 return -EOPNOTSUPP;
382 if (copy_from_user(&c, arg, sizeof(c)))
383 return -EFAULT;
384
385 if (c.flags)
386 return -EOPNOTSUPP;
387 return ops->pr_clear(bdev, c.key);
388}
389
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200390/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800391 * Is it an unrecognized ioctl? The correct returns are either
392 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
393 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
394 * code before returning.
395 *
396 * Confused drivers sometimes return EINVAL, which is wrong. It
397 * means "I understood the ioctl command, but the parameters to
398 * it were wrong".
399 *
400 * We should aim to just fix the broken drivers, the EINVAL case
401 * should go away.
402 */
403static inline int is_unrecognized_ioctl(int ret)
404{
405 return ret == -EINVAL ||
406 ret == -ENOTTY ||
407 ret == -ENOIOCTLCMD;
408}
409
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200410static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
411 unsigned cmd, unsigned long arg)
412{
413 int ret;
414
415 if (!capable(CAP_SYS_ADMIN))
416 return -EACCES;
417
418 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
419 if (!is_unrecognized_ioctl(ret))
420 return ret;
421
422 fsync_bdev(bdev);
423 invalidate_bdev(bdev);
424 return 0;
425}
426
427static int blkdev_roset(struct block_device *bdev, fmode_t mode,
428 unsigned cmd, unsigned long arg)
429{
430 int ret, n;
431
432 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
433 if (!is_unrecognized_ioctl(ret))
434 return ret;
435 if (!capable(CAP_SYS_ADMIN))
436 return -EACCES;
437 if (get_user(n, (int __user *)arg))
438 return -EFAULT;
439 set_device_ro(bdev, n);
440 return 0;
441}
442
443static int blkdev_getgeo(struct block_device *bdev,
444 struct hd_geometry __user *argp)
445{
446 struct gendisk *disk = bdev->bd_disk;
447 struct hd_geometry geo;
448 int ret;
449
450 if (!argp)
451 return -EINVAL;
452 if (!disk->fops->getgeo)
453 return -ENOTTY;
454
455 /*
456 * We need to set the startsect first, the driver may
457 * want to override it.
458 */
459 memset(&geo, 0, sizeof(geo));
460 geo.start = get_start_sect(bdev);
461 ret = disk->fops->getgeo(bdev, &geo);
462 if (ret)
463 return ret;
464 if (copy_to_user(argp, &geo, sizeof(geo)))
465 return -EFAULT;
466 return 0;
467}
468
469/* set the logical block size */
470static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
471 int __user *argp)
472{
473 int ret, n;
474
475 if (!capable(CAP_SYS_ADMIN))
476 return -EACCES;
477 if (!argp)
478 return -EINVAL;
479 if (get_user(n, argp))
480 return -EFAULT;
481
482 if (!(mode & FMODE_EXCL)) {
483 bdgrab(bdev);
484 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
485 return -EBUSY;
486 }
487
488 ret = set_blocksize(bdev, n);
489 if (!(mode & FMODE_EXCL))
490 blkdev_put(bdev, mode | FMODE_EXCL);
491 return ret;
492}
493
Linus Torvalds07d106d2012-01-05 15:40:12 -0800494/*
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200495 * always keep this in sync with compat_blkdev_ioctl()
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200496 */
Al Viro56b26ad2008-09-19 03:17:36 -0400497int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700498 unsigned long arg)
499{
Al Viro45048d02008-09-18 15:53:24 -0400500 struct backing_dev_info *bdi;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200501 void __user *argp = (void __user *)arg;
Al Viro45048d02008-09-18 15:53:24 -0400502 loff_t size;
Akinobu Mita63f26492014-05-25 21:43:33 +0900503 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700504
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200505 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200507 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200509 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700510 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200511 return blk_ioctl_discard(bdev, mode, arg, 0);
512 case BLKSECDISCARD:
513 return blk_ioctl_discard(bdev, mode, arg,
514 BLKDEV_DISCARD_SECURE);
515 case BLKZEROOUT:
516 return blk_ioctl_zeroout(bdev, mode, arg);
517 case HDIO_GETGEO:
518 return blkdev_getgeo(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400519 case BLKRAGET:
520 case BLKFRAGET:
521 if (!arg)
522 return -EINVAL;
523 bdi = blk_get_backing_dev_info(bdev);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300524 return put_long(arg, (bdi->ra_pages * PAGE_SIZE) / 512);
Al Viro45048d02008-09-18 15:53:24 -0400525 case BLKROGET:
526 return put_int(arg, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200527 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
Al Viro45048d02008-09-18 15:53:24 -0400528 return put_int(arg, block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200529 case BLKSSZGET: /* get block device logical block size */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400530 return put_int(arg, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200531 case BLKPBSZGET: /* get block device physical block size */
532 return put_uint(arg, bdev_physical_block_size(bdev));
533 case BLKIOMIN:
534 return put_uint(arg, bdev_io_min(bdev));
535 case BLKIOOPT:
536 return put_uint(arg, bdev_io_opt(bdev));
537 case BLKALIGNOFF:
538 return put_int(arg, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100539 case BLKDISCARDZEROES:
540 return put_uint(arg, bdev_discard_zeroes_data(bdev));
Al Viro45048d02008-09-18 15:53:24 -0400541 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900542 max_sectors = min_t(unsigned int, USHRT_MAX,
543 queue_max_sectors(bdev_get_queue(bdev)));
544 return put_ushort(arg, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100545 case BLKROTATIONAL:
546 return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400547 case BLKRASET:
548 case BLKFRASET:
549 if(!capable(CAP_SYS_ADMIN))
550 return -EACCES;
551 bdi = blk_get_backing_dev_info(bdev);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300552 bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400553 return 0;
554 case BLKBSZSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200555 return blkdev_bszset(bdev, mode, argp);
Al Viro45048d02008-09-18 15:53:24 -0400556 case BLKPG:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200557 return blkpg_ioctl(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400558 case BLKRRPART:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200559 return blkdev_reread_part(bdev);
Al Viro45048d02008-09-18 15:53:24 -0400560 case BLKGETSIZE:
Mike Snitzer77304d22010-11-08 14:39:12 +0100561 size = i_size_read(bdev->bd_inode);
Al Viro45048d02008-09-18 15:53:24 -0400562 if ((size >> 9) > ~0UL)
563 return -EFBIG;
564 return put_ulong(arg, size >> 9);
565 case BLKGETSIZE64:
Mike Snitzer77304d22010-11-08 14:39:12 +0100566 return put_u64(arg, i_size_read(bdev->bd_inode));
Al Viro45048d02008-09-18 15:53:24 -0400567 case BLKTRACESTART:
568 case BLKTRACESTOP:
569 case BLKTRACESETUP:
570 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200571 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200572 case IOC_PR_REGISTER:
573 return blkdev_pr_register(bdev, argp);
574 case IOC_PR_RESERVE:
575 return blkdev_pr_reserve(bdev, argp);
576 case IOC_PR_RELEASE:
577 return blkdev_pr_release(bdev, argp);
578 case IOC_PR_PREEMPT:
579 return blkdev_pr_preempt(bdev, argp, false);
580 case IOC_PR_PREEMPT_ABORT:
581 return blkdev_pr_preempt(bdev, argp, true);
582 case IOC_PR_CLEAR:
583 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400584 default:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200585 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Al Viro45048d02008-09-18 15:53:24 -0400586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
Stephen Tweedie68f66fe2005-05-13 23:31:19 -0400588EXPORT_SYMBOL_GPL(blkdev_ioctl);