blob: d91f6fac398e2717de691455771b160bc1b5e40e [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 Viroff01bb42011-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 Torvalds7c0f6ba2016-12-24 11:46:01 -080011#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
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 }
Stefan Haberland633395b2016-12-19 17:15:50 +010048 /* check if partition is aligned to blocksize */
49 if (p.start & (bdev_logical_block_size(bdev) - 1))
50 return -EINVAL;
Tejun Heo88e34122008-08-25 19:30:16 +090051
Arjan van de Venc039e312006-03-23 03:00:28 -080052 mutex_lock(&bdev->bd_mutex);
Tejun Heo88e34122008-08-25 19:30:16 +090053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /* overlap? */
Tejun Heoe71bf0d2008-09-03 09:03:02 +020055 disk_part_iter_init(&piter, disk,
56 DISK_PITER_INCL_EMPTY);
57 while ((part = disk_part_iter_next(&piter))) {
58 if (!(start + length <= part->start_sect ||
59 start >= part->start_sect + part->nr_sects)) {
60 disk_part_iter_exit(&piter);
Arjan van de Venc039e312006-03-23 03:00:28 -080061 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return -EBUSY;
63 }
64 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020065 disk_part_iter_exit(&piter);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /* all seems OK */
Tejun Heoba329292008-11-10 15:29:58 +090068 part = add_partition(disk, partno, start, length,
Will Drewry6d1d8052010-08-31 15:47:05 -050069 ADDPART_FLAG_NONE, NULL);
Arjan van de Venc039e312006-03-23 03:00:28 -080070 mutex_unlock(&bdev->bd_mutex);
Duan Jiongc7d1ba42013-11-06 15:56:39 +080071 return PTR_ERR_OR_ZERO(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 case BLKPG_DEL_PARTITION:
Tejun Heoe71bf0d2008-09-03 09:03:02 +020073 part = disk_get_part(disk, partno);
74 if (!part)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -ENXIO;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020076
77 bdevp = bdget(part_devt(part));
78 disk_put_part(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (!bdevp)
80 return -ENOMEM;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020081
Peter Zijlstra2e7b6512006-12-08 02:36:13 -080082 mutex_lock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (bdevp->bd_openers) {
Arjan van de Venc039e312006-03-23 03:00:28 -080084 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 bdput(bdevp);
86 return -EBUSY;
87 }
88 /* all seems OK */
89 fsync_bdev(bdevp);
Peter Zijlstraf98393a2007-05-06 14:49:54 -070090 invalidate_bdev(bdevp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Peter Zijlstra6d740cd2007-02-20 13:58:18 -080092 mutex_lock_nested(&bdev->bd_mutex, 1);
Tejun Heocf771cb2008-09-03 09:01:09 +020093 delete_partition(disk, partno);
Arjan van de Venc039e312006-03-23 03:00:28 -080094 mutex_unlock(&bdev->bd_mutex);
95 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 bdput(bdevp);
97
98 return 0;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020099 case BLKPG_RESIZE_PARTITION:
100 start = p.start >> 9;
101 /* new length of partition in bytes */
102 length = p.length >> 9;
103 /* check for fit in a hd_struct */
104 if (sizeof(sector_t) == sizeof(long) &&
105 sizeof(long long) > sizeof(long)) {
106 long pstart = start, plength = length;
107 if (pstart != start || plength != length
108 || pstart < 0 || plength < 0)
109 return -EINVAL;
110 }
111 part = disk_get_part(disk, partno);
112 if (!part)
113 return -ENXIO;
114 bdevp = bdget(part_devt(part));
115 if (!bdevp) {
116 disk_put_part(part);
117 return -ENOMEM;
118 }
119 mutex_lock(&bdevp->bd_mutex);
120 mutex_lock_nested(&bdev->bd_mutex, 1);
121 if (start != part->start_sect) {
122 mutex_unlock(&bdevp->bd_mutex);
123 mutex_unlock(&bdev->bd_mutex);
124 bdput(bdevp);
125 disk_put_part(part);
126 return -EINVAL;
127 }
128 /* overlap? */
129 disk_part_iter_init(&piter, disk,
130 DISK_PITER_INCL_EMPTY);
131 while ((lpart = disk_part_iter_next(&piter))) {
132 if (lpart->partno != partno &&
133 !(start + length <= lpart->start_sect ||
134 start >= lpart->start_sect + lpart->nr_sects)
135 ) {
136 disk_part_iter_exit(&piter);
137 mutex_unlock(&bdevp->bd_mutex);
138 mutex_unlock(&bdev->bd_mutex);
139 bdput(bdevp);
140 disk_put_part(part);
141 return -EBUSY;
142 }
143 }
144 disk_part_iter_exit(&piter);
145 part_nr_sects_write(part, (sector_t)length);
146 i_size_write(bdevp->bd_inode, p.length);
147 mutex_unlock(&bdevp->bd_mutex);
148 mutex_unlock(&bdev->bd_mutex);
149 bdput(bdevp);
150 disk_put_part(part);
151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 default:
153 return -EINVAL;
154 }
155}
156
Jarod Wilsonbe324172015-05-06 12:26:22 +0800157/*
158 * This is an exported API for the block driver, and will not
159 * acquire bd_mutex. This API should be used in case that
160 * caller has held bd_mutex already.
161 */
162int __blkdev_reread_part(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Tejun Heod27769e2011-08-23 20:01:04 +0200166 if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -EINVAL;
168 if (!capable(CAP_SYS_ADMIN))
169 return -EACCES;
Jarod Wilsonbe324172015-05-06 12:26:22 +0800170
171 lockdep_assert_held(&bdev->bd_mutex);
172
173 return rescan_partitions(disk, bdev);
174}
175EXPORT_SYMBOL(__blkdev_reread_part);
176
177/*
178 * This is an exported API for the block driver, and will
179 * try to acquire bd_mutex. If bd_mutex has been held already
180 * in current context, please call __blkdev_reread_part().
Ming Leib04a56362015-05-06 12:26:27 +0800181 *
182 * Make sure the held locks in current context aren't required
183 * in open()/close() handler and I/O path for avoiding ABBA deadlock:
184 * - bd_mutex is held before calling block driver's open/close
185 * handler
186 * - reading partition table may submit I/O to the block device
Jarod Wilsonbe324172015-05-06 12:26:22 +0800187 */
188int blkdev_reread_part(struct block_device *bdev)
189{
190 int res;
191
Ming Leib04a56362015-05-06 12:26:27 +0800192 mutex_lock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800193 res = __blkdev_reread_part(bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800194 mutex_unlock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return res;
197}
Jarod Wilsonbe324172015-05-06 12:26:22 +0800198EXPORT_SYMBOL(blkdev_reread_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200200static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
201 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100202{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200203 uint64_t range[2];
204 uint64_t start, len;
205
206 if (!(mode & FMODE_WRITE))
207 return -EBADF;
208
209 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
210 return -EFAULT;
211
212 start = range[0];
213 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700214
David Woodhoused30a2602008-08-11 15:58:42 +0100215 if (start & 511)
216 return -EINVAL;
217 if (len & 511)
218 return -EINVAL;
219 start >>= 9;
220 len >>= 9;
221
Mike Snitzer77304d22010-11-08 14:39:12 +0100222 if (start + len > (i_size_read(bdev->bd_inode) >> 9))
David Woodhoused30a2602008-08-11 15:58:42 +0100223 return -EINVAL;
Tim Zimmermann6a834b72020-09-03 09:11:59 +0200224 printk("%s %d:%d %llu %llu",
225 (flags & BLKDEV_DISCARD_SECURE) ? "SECDIS" : "DIS",
226 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev),
227 (unsigned long long)start, (unsigned long long)len);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700228 return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100229}
230
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200231static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
232 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400233{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200234 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700235 struct address_space *mapping;
236 uint64_t start, end, len;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200237
238 if (!(mode & FMODE_WRITE))
239 return -EBADF;
240
241 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
242 return -EFAULT;
243
244 start = range[0];
245 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700246 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200247
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400248 if (start & 511)
249 return -EINVAL;
250 if (len & 511)
251 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700252 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
253 return -EINVAL;
254 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400255 return -EINVAL;
256
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700257 /* Invalidate the page cache, including dirty pages */
258 mapping = bdev->bd_inode->i_mapping;
259 truncate_inode_pages_range(mapping, start, end);
260
261 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
Christoph Hellwigee472d82017-04-05 19:21:08 +0200262 BLKDEV_ZERO_NOUNMAP);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static int put_ushort(unsigned long arg, unsigned short val)
266{
267 return put_user(val, (unsigned short __user *)arg);
268}
269
270static int put_int(unsigned long arg, int val)
271{
272 return put_user(val, (int __user *)arg);
273}
274
Martin K. Petersenac481c22009-10-03 20:52:01 +0200275static int put_uint(unsigned long arg, unsigned int val)
276{
277 return put_user(val, (unsigned int __user *)arg);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int put_long(unsigned long arg, long val)
281{
282 return put_user(val, (long __user *)arg);
283}
284
285static int put_ulong(unsigned long arg, unsigned long val)
286{
287 return put_user(val, (unsigned long __user *)arg);
288}
289
290static int put_u64(unsigned long arg, u64 val)
291{
292 return put_user(val, (u64 __user *)arg);
293}
294
Al Viro633a08b2007-08-29 20:34:12 -0400295int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
296 unsigned cmd, unsigned long arg)
297{
298 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d622008-03-02 09:09:22 -0500299
300 if (disk->fops->ioctl)
301 return disk->fops->ioctl(bdev, mode, cmd, arg);
302
Al Viro633a08b2007-08-29 20:34:12 -0400303 return -ENOTTY;
304}
305/*
306 * For the record: _GPL here is only because somebody decided to slap it
307 * on the previous export. Sheer idiocy, since it wasn't copyrightable
308 * at all and could be open-coded without any exports by anybody who cares.
309 */
310EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
311
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200312static int blkdev_pr_register(struct block_device *bdev,
313 struct pr_registration __user *arg)
314{
315 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
316 struct pr_registration reg;
317
318 if (!capable(CAP_SYS_ADMIN))
319 return -EPERM;
320 if (!ops || !ops->pr_register)
321 return -EOPNOTSUPP;
322 if (copy_from_user(&reg, arg, sizeof(reg)))
323 return -EFAULT;
324
325 if (reg.flags & ~PR_FL_IGNORE_KEY)
326 return -EOPNOTSUPP;
327 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
328}
329
330static int blkdev_pr_reserve(struct block_device *bdev,
331 struct pr_reservation __user *arg)
332{
333 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
334 struct pr_reservation rsv;
335
336 if (!capable(CAP_SYS_ADMIN))
337 return -EPERM;
338 if (!ops || !ops->pr_reserve)
339 return -EOPNOTSUPP;
340 if (copy_from_user(&rsv, arg, sizeof(rsv)))
341 return -EFAULT;
342
343 if (rsv.flags & ~PR_FL_IGNORE_KEY)
344 return -EOPNOTSUPP;
345 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
346}
347
348static int blkdev_pr_release(struct block_device *bdev,
349 struct pr_reservation __user *arg)
350{
351 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
352 struct pr_reservation rsv;
353
354 if (!capable(CAP_SYS_ADMIN))
355 return -EPERM;
356 if (!ops || !ops->pr_release)
357 return -EOPNOTSUPP;
358 if (copy_from_user(&rsv, arg, sizeof(rsv)))
359 return -EFAULT;
360
361 if (rsv.flags)
362 return -EOPNOTSUPP;
363 return ops->pr_release(bdev, rsv.key, rsv.type);
364}
365
366static int blkdev_pr_preempt(struct block_device *bdev,
367 struct pr_preempt __user *arg, bool abort)
368{
369 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
370 struct pr_preempt p;
371
372 if (!capable(CAP_SYS_ADMIN))
373 return -EPERM;
374 if (!ops || !ops->pr_preempt)
375 return -EOPNOTSUPP;
376 if (copy_from_user(&p, arg, sizeof(p)))
377 return -EFAULT;
378
379 if (p.flags)
380 return -EOPNOTSUPP;
381 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
382}
383
384static int blkdev_pr_clear(struct block_device *bdev,
385 struct pr_clear __user *arg)
386{
387 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
388 struct pr_clear c;
389
390 if (!capable(CAP_SYS_ADMIN))
391 return -EPERM;
392 if (!ops || !ops->pr_clear)
393 return -EOPNOTSUPP;
394 if (copy_from_user(&c, arg, sizeof(c)))
395 return -EFAULT;
396
397 if (c.flags)
398 return -EOPNOTSUPP;
399 return ops->pr_clear(bdev, c.key);
400}
401
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200402/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800403 * Is it an unrecognized ioctl? The correct returns are either
404 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
405 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
406 * code before returning.
407 *
408 * Confused drivers sometimes return EINVAL, which is wrong. It
409 * means "I understood the ioctl command, but the parameters to
410 * it were wrong".
411 *
412 * We should aim to just fix the broken drivers, the EINVAL case
413 * should go away.
414 */
415static inline int is_unrecognized_ioctl(int ret)
416{
417 return ret == -EINVAL ||
418 ret == -ENOTTY ||
419 ret == -ENOIOCTLCMD;
420}
421
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200422static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
423 unsigned cmd, unsigned long arg)
424{
425 int ret;
426
427 if (!capable(CAP_SYS_ADMIN))
428 return -EACCES;
429
430 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
431 if (!is_unrecognized_ioctl(ret))
432 return ret;
433
434 fsync_bdev(bdev);
435 invalidate_bdev(bdev);
436 return 0;
437}
438
439static int blkdev_roset(struct block_device *bdev, fmode_t mode,
440 unsigned cmd, unsigned long arg)
441{
442 int ret, n;
443
444 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
445 if (!is_unrecognized_ioctl(ret))
446 return ret;
447 if (!capable(CAP_SYS_ADMIN))
448 return -EACCES;
449 if (get_user(n, (int __user *)arg))
450 return -EFAULT;
451 set_device_ro(bdev, n);
452 return 0;
453}
454
455static int blkdev_getgeo(struct block_device *bdev,
456 struct hd_geometry __user *argp)
457{
458 struct gendisk *disk = bdev->bd_disk;
459 struct hd_geometry geo;
460 int ret;
461
462 if (!argp)
463 return -EINVAL;
464 if (!disk->fops->getgeo)
465 return -ENOTTY;
466
467 /*
468 * We need to set the startsect first, the driver may
469 * want to override it.
470 */
471 memset(&geo, 0, sizeof(geo));
472 geo.start = get_start_sect(bdev);
473 ret = disk->fops->getgeo(bdev, &geo);
474 if (ret)
475 return ret;
476 if (copy_to_user(argp, &geo, sizeof(geo)))
477 return -EFAULT;
478 return 0;
479}
480
481/* set the logical block size */
482static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
483 int __user *argp)
484{
485 int ret, n;
486
487 if (!capable(CAP_SYS_ADMIN))
488 return -EACCES;
489 if (!argp)
490 return -EINVAL;
491 if (get_user(n, argp))
492 return -EFAULT;
493
494 if (!(mode & FMODE_EXCL)) {
495 bdgrab(bdev);
496 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
497 return -EBUSY;
498 }
499
500 ret = set_blocksize(bdev, n);
501 if (!(mode & FMODE_EXCL))
502 blkdev_put(bdev, mode | FMODE_EXCL);
503 return ret;
504}
505
Linus Torvalds07d106d2012-01-05 15:40:12 -0800506/*
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200507 * always keep this in sync with compat_blkdev_ioctl()
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200508 */
Al Viro56b26ad2008-09-19 03:17:36 -0400509int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700510 unsigned long arg)
511{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200512 void __user *argp = (void __user *)arg;
Al Viro45048d02008-09-18 15:53:24 -0400513 loff_t size;
Akinobu Mita63f26492014-05-25 21:43:33 +0900514 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700515
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200516 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200518 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200520 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700521 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200522 return blk_ioctl_discard(bdev, mode, arg, 0);
523 case BLKSECDISCARD:
524 return blk_ioctl_discard(bdev, mode, arg,
525 BLKDEV_DISCARD_SECURE);
526 case BLKZEROOUT:
527 return blk_ioctl_zeroout(bdev, mode, arg);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900528 case BLKREPORTZONE:
529 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
530 case BLKRESETZONE:
531 return blkdev_reset_zones_ioctl(bdev, mode, cmd, arg);
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200532 case HDIO_GETGEO:
533 return blkdev_getgeo(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400534 case BLKRAGET:
535 case BLKFRAGET:
536 if (!arg)
537 return -EINVAL;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100538 return put_long(arg, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
Al Viro45048d02008-09-18 15:53:24 -0400539 case BLKROGET:
540 return put_int(arg, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200541 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
Al Viro45048d02008-09-18 15:53:24 -0400542 return put_int(arg, block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200543 case BLKSSZGET: /* get block device logical block size */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400544 return put_int(arg, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200545 case BLKPBSZGET: /* get block device physical block size */
546 return put_uint(arg, bdev_physical_block_size(bdev));
547 case BLKIOMIN:
548 return put_uint(arg, bdev_io_min(bdev));
549 case BLKIOOPT:
550 return put_uint(arg, bdev_io_opt(bdev));
551 case BLKALIGNOFF:
552 return put_int(arg, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100553 case BLKDISCARDZEROES:
Christoph Hellwig48920ff2017-04-05 19:21:23 +0200554 return put_uint(arg, 0);
Al Viro45048d02008-09-18 15:53:24 -0400555 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900556 max_sectors = min_t(unsigned int, USHRT_MAX,
557 queue_max_sectors(bdev_get_queue(bdev)));
558 return put_ushort(arg, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100559 case BLKROTATIONAL:
560 return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400561 case BLKRASET:
562 case BLKFRASET:
563 if(!capable(CAP_SYS_ADMIN))
564 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100565 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400566 return 0;
567 case BLKBSZSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200568 return blkdev_bszset(bdev, mode, argp);
Al Viro45048d02008-09-18 15:53:24 -0400569 case BLKPG:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200570 return blkpg_ioctl(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400571 case BLKRRPART:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200572 return blkdev_reread_part(bdev);
Al Viro45048d02008-09-18 15:53:24 -0400573 case BLKGETSIZE:
Mike Snitzer77304d22010-11-08 14:39:12 +0100574 size = i_size_read(bdev->bd_inode);
Al Viro45048d02008-09-18 15:53:24 -0400575 if ((size >> 9) > ~0UL)
576 return -EFBIG;
577 return put_ulong(arg, size >> 9);
578 case BLKGETSIZE64:
Mike Snitzer77304d22010-11-08 14:39:12 +0100579 return put_u64(arg, i_size_read(bdev->bd_inode));
Al Viro45048d02008-09-18 15:53:24 -0400580 case BLKTRACESTART:
581 case BLKTRACESTOP:
582 case BLKTRACESETUP:
583 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200584 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200585 case IOC_PR_REGISTER:
586 return blkdev_pr_register(bdev, argp);
587 case IOC_PR_RESERVE:
588 return blkdev_pr_reserve(bdev, argp);
589 case IOC_PR_RELEASE:
590 return blkdev_pr_release(bdev, argp);
591 case IOC_PR_PREEMPT:
592 return blkdev_pr_preempt(bdev, argp, false);
593 case IOC_PR_PREEMPT_ABORT:
594 return blkdev_pr_preempt(bdev, argp, true);
595 case IOC_PR_CLEAR:
596 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400597 default:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200598 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Al Viro45048d02008-09-18 15:53:24 -0400599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Stephen Tweedie68f66fe2005-05-13 23:31:19 -0400601EXPORT_SYMBOL_GPL(blkdev_ioctl);