blob: 755119c3c1b9129890eedc6abbe670ac4b88ecdf [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/backing-dev.h>
Al Viroff01bb482011-09-16 02:31:11 -04008#include <linux/fs.h>
Jens Axboe2056a782006-03-23 20:00:26 +01009#include <linux/blktrace_api.h>
Christoph Hellwigbbd3e062015-10-15 14:10:48 +020010#include <linux/pr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/uaccess.h>
12
13static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
14{
15 struct block_device *bdevp;
16 struct gendisk *disk;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020017 struct hd_struct *part, *lpart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 struct blkpg_ioctl_arg a;
19 struct blkpg_partition p;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020020 struct disk_part_iter piter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 long long start, length;
Tejun Heocf771cb2008-09-03 09:01:09 +020022 int partno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24 if (!capable(CAP_SYS_ADMIN))
25 return -EACCES;
26 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
27 return -EFAULT;
28 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
29 return -EFAULT;
30 disk = bdev->bd_disk;
31 if (bdev != bdev->bd_contains)
32 return -EINVAL;
Tejun Heocf771cb2008-09-03 09:01:09 +020033 partno = p.pno;
Tejun Heo540eed52008-08-25 19:56:15 +090034 if (partno <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 return -EINVAL;
36 switch (a.op) {
37 case BLKPG_ADD_PARTITION:
38 start = p.start >> 9;
39 length = p.length >> 9;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020040 /* check for fit in a hd_struct */
41 if (sizeof(sector_t) == sizeof(long) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 sizeof(long long) > sizeof(long)) {
43 long pstart = start, plength = length;
44 if (pstart != start || plength != length
Alan Cox2bd6efa2012-09-17 11:47:13 +010045 || pstart < 0 || plength < 0 || partno > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return -EINVAL;
47 }
Tejun Heo88e34122008-08-25 19:30:16 +090048
Arjan van de Venc039e312006-03-23 03:00:28 -080049 mutex_lock(&bdev->bd_mutex);
Tejun Heo88e34122008-08-25 19:30:16 +090050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 /* overlap? */
Tejun Heoe71bf0d2008-09-03 09:03:02 +020052 disk_part_iter_init(&piter, disk,
53 DISK_PITER_INCL_EMPTY);
54 while ((part = disk_part_iter_next(&piter))) {
55 if (!(start + length <= part->start_sect ||
56 start >= part->start_sect + part->nr_sects)) {
57 disk_part_iter_exit(&piter);
Arjan van de Venc039e312006-03-23 03:00:28 -080058 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return -EBUSY;
60 }
61 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020062 disk_part_iter_exit(&piter);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 /* all seems OK */
Tejun Heoba329292008-11-10 15:29:58 +090065 part = add_partition(disk, partno, start, length,
Will Drewry6d1d8052010-08-31 15:47:05 -050066 ADDPART_FLAG_NONE, NULL);
Arjan van de Venc039e312006-03-23 03:00:28 -080067 mutex_unlock(&bdev->bd_mutex);
Duan Jiongc7d1ba42013-11-06 15:56:39 +080068 return PTR_ERR_OR_ZERO(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 case BLKPG_DEL_PARTITION:
Tejun Heoe71bf0d2008-09-03 09:03:02 +020070 part = disk_get_part(disk, partno);
71 if (!part)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -ENXIO;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020073
74 bdevp = bdget(part_devt(part));
75 disk_put_part(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!bdevp)
77 return -ENOMEM;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020078
Peter Zijlstra2e7b6512006-12-08 02:36:13 -080079 mutex_lock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (bdevp->bd_openers) {
Arjan van de Venc039e312006-03-23 03:00:28 -080081 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 bdput(bdevp);
83 return -EBUSY;
84 }
85 /* all seems OK */
86 fsync_bdev(bdevp);
Peter Zijlstraf98393a2007-05-06 14:49:54 -070087 invalidate_bdev(bdevp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Peter Zijlstra6d740cd2007-02-20 13:58:18 -080089 mutex_lock_nested(&bdev->bd_mutex, 1);
Tejun Heocf771cb2008-09-03 09:01:09 +020090 delete_partition(disk, partno);
Arjan van de Venc039e312006-03-23 03:00:28 -080091 mutex_unlock(&bdev->bd_mutex);
92 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 bdput(bdevp);
94
95 return 0;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020096 case BLKPG_RESIZE_PARTITION:
97 start = p.start >> 9;
98 /* new length of partition in bytes */
99 length = p.length >> 9;
100 /* check for fit in a hd_struct */
101 if (sizeof(sector_t) == sizeof(long) &&
102 sizeof(long long) > sizeof(long)) {
103 long pstart = start, plength = length;
104 if (pstart != start || plength != length
105 || pstart < 0 || plength < 0)
106 return -EINVAL;
107 }
108 part = disk_get_part(disk, partno);
109 if (!part)
110 return -ENXIO;
111 bdevp = bdget(part_devt(part));
112 if (!bdevp) {
113 disk_put_part(part);
114 return -ENOMEM;
115 }
116 mutex_lock(&bdevp->bd_mutex);
117 mutex_lock_nested(&bdev->bd_mutex, 1);
118 if (start != part->start_sect) {
119 mutex_unlock(&bdevp->bd_mutex);
120 mutex_unlock(&bdev->bd_mutex);
121 bdput(bdevp);
122 disk_put_part(part);
123 return -EINVAL;
124 }
125 /* overlap? */
126 disk_part_iter_init(&piter, disk,
127 DISK_PITER_INCL_EMPTY);
128 while ((lpart = disk_part_iter_next(&piter))) {
129 if (lpart->partno != partno &&
130 !(start + length <= lpart->start_sect ||
131 start >= lpart->start_sect + lpart->nr_sects)
132 ) {
133 disk_part_iter_exit(&piter);
134 mutex_unlock(&bdevp->bd_mutex);
135 mutex_unlock(&bdev->bd_mutex);
136 bdput(bdevp);
137 disk_put_part(part);
138 return -EBUSY;
139 }
140 }
141 disk_part_iter_exit(&piter);
142 part_nr_sects_write(part, (sector_t)length);
143 i_size_write(bdevp->bd_inode, p.length);
144 mutex_unlock(&bdevp->bd_mutex);
145 mutex_unlock(&bdev->bd_mutex);
146 bdput(bdevp);
147 disk_put_part(part);
148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 default:
150 return -EINVAL;
151 }
152}
153
Jarod Wilsonbe324172015-05-06 12:26:22 +0800154/*
155 * This is an exported API for the block driver, and will not
156 * acquire bd_mutex. This API should be used in case that
157 * caller has held bd_mutex already.
158 */
159int __blkdev_reread_part(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Tejun Heod27769e2011-08-23 20:01:04 +0200163 if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -EINVAL;
165 if (!capable(CAP_SYS_ADMIN))
166 return -EACCES;
Jarod Wilsonbe324172015-05-06 12:26:22 +0800167
168 lockdep_assert_held(&bdev->bd_mutex);
169
170 return rescan_partitions(disk, bdev);
171}
172EXPORT_SYMBOL(__blkdev_reread_part);
173
174/*
175 * This is an exported API for the block driver, and will
176 * try to acquire bd_mutex. If bd_mutex has been held already
177 * in current context, please call __blkdev_reread_part().
Ming Leib04a56362015-05-06 12:26:27 +0800178 *
179 * Make sure the held locks in current context aren't required
180 * in open()/close() handler and I/O path for avoiding ABBA deadlock:
181 * - bd_mutex is held before calling block driver's open/close
182 * handler
183 * - reading partition table may submit I/O to the block device
Jarod Wilsonbe324172015-05-06 12:26:22 +0800184 */
185int blkdev_reread_part(struct block_device *bdev)
186{
187 int res;
188
Ming Leib04a56362015-05-06 12:26:27 +0800189 mutex_lock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800190 res = __blkdev_reread_part(bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800191 mutex_unlock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return res;
194}
Jarod Wilsonbe324172015-05-06 12:26:22 +0800195EXPORT_SYMBOL(blkdev_reread_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200197static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
198 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100199{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200200 uint64_t range[2];
201 uint64_t start, len;
202
203 if (!(mode & FMODE_WRITE))
204 return -EBADF;
205
206 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
207 return -EFAULT;
208
209 start = range[0];
210 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700211
David Woodhoused30a2602008-08-11 15:58:42 +0100212 if (start & 511)
213 return -EINVAL;
214 if (len & 511)
215 return -EINVAL;
216 start >>= 9;
217 len >>= 9;
218
Mike Snitzer77304d22010-11-08 14:39:12 +0100219 if (start + len > (i_size_read(bdev->bd_inode) >> 9))
David Woodhoused30a2602008-08-11 15:58:42 +0100220 return -EINVAL;
Adrian Hunter8d57a982010-08-11 14:17:49 -0700221 return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100222}
223
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200224static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
225 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400226{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200227 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700228 struct address_space *mapping;
229 uint64_t start, end, len;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200230
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];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700239 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200240
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400241 if (start & 511)
242 return -EINVAL;
243 if (len & 511)
244 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700245 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
246 return -EINVAL;
247 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400248 return -EINVAL;
249
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700250 /* Invalidate the page cache, including dirty pages */
251 mapping = bdev->bd_inode->i_mapping;
252 truncate_inode_pages_range(mapping, start, end);
253
254 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
255 false);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static int put_ushort(unsigned long arg, unsigned short val)
259{
260 return put_user(val, (unsigned short __user *)arg);
261}
262
263static int put_int(unsigned long arg, int val)
264{
265 return put_user(val, (int __user *)arg);
266}
267
Martin K. Petersenac481c22009-10-03 20:52:01 +0200268static int put_uint(unsigned long arg, unsigned int val)
269{
270 return put_user(val, (unsigned int __user *)arg);
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static int put_long(unsigned long arg, long val)
274{
275 return put_user(val, (long __user *)arg);
276}
277
278static int put_ulong(unsigned long arg, unsigned long val)
279{
280 return put_user(val, (unsigned long __user *)arg);
281}
282
283static int put_u64(unsigned long arg, u64 val)
284{
285 return put_user(val, (u64 __user *)arg);
286}
287
Al Viro633a08b2007-08-29 20:34:12 -0400288int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
289 unsigned cmd, unsigned long arg)
290{
291 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d62008-03-02 09:09:22 -0500292
293 if (disk->fops->ioctl)
294 return disk->fops->ioctl(bdev, mode, cmd, arg);
295
Al Viro633a08b2007-08-29 20:34:12 -0400296 return -ENOTTY;
297}
298/*
299 * For the record: _GPL here is only because somebody decided to slap it
300 * on the previous export. Sheer idiocy, since it wasn't copyrightable
301 * at all and could be open-coded without any exports by anybody who cares.
302 */
303EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
304
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200305static int blkdev_pr_register(struct block_device *bdev,
306 struct pr_registration __user *arg)
307{
308 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
309 struct pr_registration reg;
310
311 if (!capable(CAP_SYS_ADMIN))
312 return -EPERM;
313 if (!ops || !ops->pr_register)
314 return -EOPNOTSUPP;
315 if (copy_from_user(&reg, arg, sizeof(reg)))
316 return -EFAULT;
317
318 if (reg.flags & ~PR_FL_IGNORE_KEY)
319 return -EOPNOTSUPP;
320 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
321}
322
323static int blkdev_pr_reserve(struct block_device *bdev,
324 struct pr_reservation __user *arg)
325{
326 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
327 struct pr_reservation rsv;
328
329 if (!capable(CAP_SYS_ADMIN))
330 return -EPERM;
331 if (!ops || !ops->pr_reserve)
332 return -EOPNOTSUPP;
333 if (copy_from_user(&rsv, arg, sizeof(rsv)))
334 return -EFAULT;
335
336 if (rsv.flags & ~PR_FL_IGNORE_KEY)
337 return -EOPNOTSUPP;
338 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
339}
340
341static int blkdev_pr_release(struct block_device *bdev,
342 struct pr_reservation __user *arg)
343{
344 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
345 struct pr_reservation rsv;
346
347 if (!capable(CAP_SYS_ADMIN))
348 return -EPERM;
349 if (!ops || !ops->pr_release)
350 return -EOPNOTSUPP;
351 if (copy_from_user(&rsv, arg, sizeof(rsv)))
352 return -EFAULT;
353
354 if (rsv.flags)
355 return -EOPNOTSUPP;
356 return ops->pr_release(bdev, rsv.key, rsv.type);
357}
358
359static int blkdev_pr_preempt(struct block_device *bdev,
360 struct pr_preempt __user *arg, bool abort)
361{
362 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
363 struct pr_preempt p;
364
365 if (!capable(CAP_SYS_ADMIN))
366 return -EPERM;
367 if (!ops || !ops->pr_preempt)
368 return -EOPNOTSUPP;
369 if (copy_from_user(&p, arg, sizeof(p)))
370 return -EFAULT;
371
372 if (p.flags)
373 return -EOPNOTSUPP;
374 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
375}
376
377static int blkdev_pr_clear(struct block_device *bdev,
378 struct pr_clear __user *arg)
379{
380 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
381 struct pr_clear c;
382
383 if (!capable(CAP_SYS_ADMIN))
384 return -EPERM;
385 if (!ops || !ops->pr_clear)
386 return -EOPNOTSUPP;
387 if (copy_from_user(&c, arg, sizeof(c)))
388 return -EFAULT;
389
390 if (c.flags)
391 return -EOPNOTSUPP;
392 return ops->pr_clear(bdev, c.key);
393}
394
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200395/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800396 * Is it an unrecognized ioctl? The correct returns are either
397 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
398 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
399 * code before returning.
400 *
401 * Confused drivers sometimes return EINVAL, which is wrong. It
402 * means "I understood the ioctl command, but the parameters to
403 * it were wrong".
404 *
405 * We should aim to just fix the broken drivers, the EINVAL case
406 * should go away.
407 */
408static inline int is_unrecognized_ioctl(int ret)
409{
410 return ret == -EINVAL ||
411 ret == -ENOTTY ||
412 ret == -ENOIOCTLCMD;
413}
414
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200415static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
416 unsigned cmd, unsigned long arg)
417{
418 int ret;
419
420 if (!capable(CAP_SYS_ADMIN))
421 return -EACCES;
422
423 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
424 if (!is_unrecognized_ioctl(ret))
425 return ret;
426
427 fsync_bdev(bdev);
428 invalidate_bdev(bdev);
429 return 0;
430}
431
432static int blkdev_roset(struct block_device *bdev, fmode_t mode,
433 unsigned cmd, unsigned long arg)
434{
435 int ret, n;
436
437 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
438 if (!is_unrecognized_ioctl(ret))
439 return ret;
440 if (!capable(CAP_SYS_ADMIN))
441 return -EACCES;
442 if (get_user(n, (int __user *)arg))
443 return -EFAULT;
444 set_device_ro(bdev, n);
445 return 0;
446}
447
448static int blkdev_getgeo(struct block_device *bdev,
449 struct hd_geometry __user *argp)
450{
451 struct gendisk *disk = bdev->bd_disk;
452 struct hd_geometry geo;
453 int ret;
454
455 if (!argp)
456 return -EINVAL;
457 if (!disk->fops->getgeo)
458 return -ENOTTY;
459
460 /*
461 * We need to set the startsect first, the driver may
462 * want to override it.
463 */
464 memset(&geo, 0, sizeof(geo));
465 geo.start = get_start_sect(bdev);
466 ret = disk->fops->getgeo(bdev, &geo);
467 if (ret)
468 return ret;
469 if (copy_to_user(argp, &geo, sizeof(geo)))
470 return -EFAULT;
471 return 0;
472}
473
474/* set the logical block size */
475static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
476 int __user *argp)
477{
478 int ret, n;
479
480 if (!capable(CAP_SYS_ADMIN))
481 return -EACCES;
482 if (!argp)
483 return -EINVAL;
484 if (get_user(n, argp))
485 return -EFAULT;
486
487 if (!(mode & FMODE_EXCL)) {
488 bdgrab(bdev);
489 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
490 return -EBUSY;
491 }
492
493 ret = set_blocksize(bdev, n);
494 if (!(mode & FMODE_EXCL))
495 blkdev_put(bdev, mode | FMODE_EXCL);
496 return ret;
497}
498
Linus Torvalds07d106d2012-01-05 15:40:12 -0800499/*
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200500 * always keep this in sync with compat_blkdev_ioctl()
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200501 */
Al Viro56b26ad2008-09-19 03:17:36 -0400502int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700503 unsigned long arg)
504{
Al Viro45048d02008-09-18 15:53:24 -0400505 struct backing_dev_info *bdi;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200506 void __user *argp = (void __user *)arg;
Al Viro45048d02008-09-18 15:53:24 -0400507 loff_t size;
Akinobu Mita63f26492014-05-25 21:43:33 +0900508 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700509
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200510 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200512 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200514 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700515 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200516 return blk_ioctl_discard(bdev, mode, arg, 0);
517 case BLKSECDISCARD:
518 return blk_ioctl_discard(bdev, mode, arg,
519 BLKDEV_DISCARD_SECURE);
520 case BLKZEROOUT:
521 return blk_ioctl_zeroout(bdev, mode, arg);
522 case HDIO_GETGEO:
523 return blkdev_getgeo(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400524 case BLKRAGET:
525 case BLKFRAGET:
526 if (!arg)
527 return -EINVAL;
528 bdi = blk_get_backing_dev_info(bdev);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300529 return put_long(arg, (bdi->ra_pages * PAGE_SIZE) / 512);
Al Viro45048d02008-09-18 15:53:24 -0400530 case BLKROGET:
531 return put_int(arg, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200532 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
Al Viro45048d02008-09-18 15:53:24 -0400533 return put_int(arg, block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200534 case BLKSSZGET: /* get block device logical block size */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400535 return put_int(arg, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200536 case BLKPBSZGET: /* get block device physical block size */
537 return put_uint(arg, bdev_physical_block_size(bdev));
538 case BLKIOMIN:
539 return put_uint(arg, bdev_io_min(bdev));
540 case BLKIOOPT:
541 return put_uint(arg, bdev_io_opt(bdev));
542 case BLKALIGNOFF:
543 return put_int(arg, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100544 case BLKDISCARDZEROES:
545 return put_uint(arg, bdev_discard_zeroes_data(bdev));
Al Viro45048d02008-09-18 15:53:24 -0400546 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900547 max_sectors = min_t(unsigned int, USHRT_MAX,
548 queue_max_sectors(bdev_get_queue(bdev)));
549 return put_ushort(arg, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100550 case BLKROTATIONAL:
551 return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400552 case BLKRASET:
553 case BLKFRASET:
554 if(!capable(CAP_SYS_ADMIN))
555 return -EACCES;
556 bdi = blk_get_backing_dev_info(bdev);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300557 bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400558 return 0;
559 case BLKBSZSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200560 return blkdev_bszset(bdev, mode, argp);
Al Viro45048d02008-09-18 15:53:24 -0400561 case BLKPG:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200562 return blkpg_ioctl(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400563 case BLKRRPART:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200564 return blkdev_reread_part(bdev);
Al Viro45048d02008-09-18 15:53:24 -0400565 case BLKGETSIZE:
Mike Snitzer77304d22010-11-08 14:39:12 +0100566 size = i_size_read(bdev->bd_inode);
Al Viro45048d02008-09-18 15:53:24 -0400567 if ((size >> 9) > ~0UL)
568 return -EFBIG;
569 return put_ulong(arg, size >> 9);
570 case BLKGETSIZE64:
Mike Snitzer77304d22010-11-08 14:39:12 +0100571 return put_u64(arg, i_size_read(bdev->bd_inode));
Al Viro45048d02008-09-18 15:53:24 -0400572 case BLKTRACESTART:
573 case BLKTRACESTOP:
574 case BLKTRACESETUP:
575 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200576 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200577 case IOC_PR_REGISTER:
578 return blkdev_pr_register(bdev, argp);
579 case IOC_PR_RESERVE:
580 return blkdev_pr_reserve(bdev, argp);
581 case IOC_PR_RELEASE:
582 return blkdev_pr_release(bdev, argp);
583 case IOC_PR_PREEMPT:
584 return blkdev_pr_preempt(bdev, argp, false);
585 case IOC_PR_PREEMPT_ABORT:
586 return blkdev_pr_preempt(bdev, argp, true);
587 case IOC_PR_CLEAR:
588 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400589 default:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200590 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Al Viro45048d02008-09-18 15:53:24 -0400591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
Stephen Tweedie68f66fe2005-05-13 23:31:19 -0400593EXPORT_SYMBOL_GPL(blkdev_ioctl);