blob: 8756efb1419e42027aa2ba0fe187956e5828c13e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/blkdev.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -04004#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/blkpg.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/backing-dev.h>
Al Viroff01bb42011-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 Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
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 }
Stefan Haberland633395b2016-12-19 17:15:50 +010049 /* check if partition is aligned to blocksize */
50 if (p.start & (bdev_logical_block_size(bdev) - 1))
51 return -EINVAL;
Tejun Heo88e34122008-08-25 19:30:16 +090052
Arjan van de Venc039e312006-03-23 03:00:28 -080053 mutex_lock(&bdev->bd_mutex);
Tejun Heo88e34122008-08-25 19:30:16 +090054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /* overlap? */
Tejun Heoe71bf0d2008-09-03 09:03:02 +020056 disk_part_iter_init(&piter, disk,
57 DISK_PITER_INCL_EMPTY);
58 while ((part = disk_part_iter_next(&piter))) {
59 if (!(start + length <= part->start_sect ||
60 start >= part->start_sect + part->nr_sects)) {
61 disk_part_iter_exit(&piter);
Arjan van de Venc039e312006-03-23 03:00:28 -080062 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return -EBUSY;
64 }
65 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020066 disk_part_iter_exit(&piter);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* all seems OK */
Tejun Heoba329292008-11-10 15:29:58 +090069 part = add_partition(disk, partno, start, length,
Will Drewry6d1d8052010-08-31 15:47:05 -050070 ADDPART_FLAG_NONE, NULL);
Arjan van de Venc039e312006-03-23 03:00:28 -080071 mutex_unlock(&bdev->bd_mutex);
Duan Jiongc7d1ba42013-11-06 15:56:39 +080072 return PTR_ERR_OR_ZERO(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 case BLKPG_DEL_PARTITION:
Tejun Heoe71bf0d2008-09-03 09:03:02 +020074 part = disk_get_part(disk, partno);
75 if (!part)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return -ENXIO;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020077
78 bdevp = bdget(part_devt(part));
79 disk_put_part(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (!bdevp)
81 return -ENOMEM;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020082
Peter Zijlstra2e7b6512006-12-08 02:36:13 -080083 mutex_lock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (bdevp->bd_openers) {
Arjan van de Venc039e312006-03-23 03:00:28 -080085 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 bdput(bdevp);
87 return -EBUSY;
88 }
89 /* all seems OK */
90 fsync_bdev(bdevp);
Peter Zijlstraf98393a2007-05-06 14:49:54 -070091 invalidate_bdev(bdevp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Peter Zijlstra6d740cd2007-02-20 13:58:18 -080093 mutex_lock_nested(&bdev->bd_mutex, 1);
Tejun Heocf771cb2008-09-03 09:01:09 +020094 delete_partition(disk, partno);
Arjan van de Venc039e312006-03-23 03:00:28 -080095 mutex_unlock(&bdev->bd_mutex);
96 mutex_unlock(&bdevp->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 bdput(bdevp);
98
99 return 0;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200100 case BLKPG_RESIZE_PARTITION:
101 start = p.start >> 9;
102 /* new length of partition in bytes */
103 length = p.length >> 9;
104 /* check for fit in a hd_struct */
105 if (sizeof(sector_t) == sizeof(long) &&
106 sizeof(long long) > sizeof(long)) {
107 long pstart = start, plength = length;
108 if (pstart != start || plength != length
109 || pstart < 0 || plength < 0)
110 return -EINVAL;
111 }
112 part = disk_get_part(disk, partno);
113 if (!part)
114 return -ENXIO;
115 bdevp = bdget(part_devt(part));
116 if (!bdevp) {
117 disk_put_part(part);
118 return -ENOMEM;
119 }
120 mutex_lock(&bdevp->bd_mutex);
121 mutex_lock_nested(&bdev->bd_mutex, 1);
122 if (start != part->start_sect) {
123 mutex_unlock(&bdevp->bd_mutex);
124 mutex_unlock(&bdev->bd_mutex);
125 bdput(bdevp);
126 disk_put_part(part);
127 return -EINVAL;
128 }
129 /* overlap? */
130 disk_part_iter_init(&piter, disk,
131 DISK_PITER_INCL_EMPTY);
132 while ((lpart = disk_part_iter_next(&piter))) {
133 if (lpart->partno != partno &&
134 !(start + length <= lpart->start_sect ||
135 start >= lpart->start_sect + lpart->nr_sects)
136 ) {
137 disk_part_iter_exit(&piter);
138 mutex_unlock(&bdevp->bd_mutex);
139 mutex_unlock(&bdev->bd_mutex);
140 bdput(bdevp);
141 disk_put_part(part);
142 return -EBUSY;
143 }
144 }
145 disk_part_iter_exit(&piter);
146 part_nr_sects_write(part, (sector_t)length);
147 i_size_write(bdevp->bd_inode, p.length);
148 mutex_unlock(&bdevp->bd_mutex);
149 mutex_unlock(&bdev->bd_mutex);
150 bdput(bdevp);
151 disk_put_part(part);
152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 default:
154 return -EINVAL;
155 }
156}
157
Jarod Wilsonbe324172015-05-06 12:26:22 +0800158/*
159 * This is an exported API for the block driver, and will not
160 * acquire bd_mutex. This API should be used in case that
161 * caller has held bd_mutex already.
162 */
163int __blkdev_reread_part(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Tejun Heod27769e2011-08-23 20:01:04 +0200167 if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -EINVAL;
169 if (!capable(CAP_SYS_ADMIN))
170 return -EACCES;
Jarod Wilsonbe324172015-05-06 12:26:22 +0800171
172 lockdep_assert_held(&bdev->bd_mutex);
173
174 return rescan_partitions(disk, bdev);
175}
176EXPORT_SYMBOL(__blkdev_reread_part);
177
178/*
179 * This is an exported API for the block driver, and will
180 * try to acquire bd_mutex. If bd_mutex has been held already
181 * in current context, please call __blkdev_reread_part().
Ming Leib04a56362015-05-06 12:26:27 +0800182 *
183 * Make sure the held locks in current context aren't required
184 * in open()/close() handler and I/O path for avoiding ABBA deadlock:
185 * - bd_mutex is held before calling block driver's open/close
186 * handler
187 * - reading partition table may submit I/O to the block device
Jarod Wilsonbe324172015-05-06 12:26:22 +0800188 */
189int blkdev_reread_part(struct block_device *bdev)
190{
191 int res;
192
Ming Leib04a56362015-05-06 12:26:27 +0800193 mutex_lock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800194 res = __blkdev_reread_part(bdev);
Arjan van de Venc039e312006-03-23 03:00:28 -0800195 mutex_unlock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return res;
198}
Jarod Wilsonbe324172015-05-06 12:26:22 +0800199EXPORT_SYMBOL(blkdev_reread_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200201static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
202 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100203{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200204 uint64_t range[2];
205 uint64_t start, len;
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600206 struct request_queue *q = bdev_get_queue(bdev);
207 struct address_space *mapping = bdev->bd_inode->i_mapping;
208
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200209
210 if (!(mode & FMODE_WRITE))
211 return -EBADF;
212
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600213 if (!blk_queue_discard(q))
214 return -EOPNOTSUPP;
215
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200216 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
217 return -EFAULT;
218
219 start = range[0];
220 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700221
David Woodhoused30a2602008-08-11 15:58:42 +0100222 if (start & 511)
223 return -EINVAL;
224 if (len & 511)
225 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100226
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600227 if (start + len > i_size_read(bdev->bd_inode))
David Woodhoused30a2602008-08-11 15:58:42 +0100228 return -EINVAL;
Ming Lei0bd1ed42018-02-10 08:46:17 +0800229 truncate_inode_pages_range(mapping, start, start + len - 1);
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600230 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
231 GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100232}
233
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200234static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
235 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400236{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200237 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700238 struct address_space *mapping;
239 uint64_t start, end, len;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200240
241 if (!(mode & FMODE_WRITE))
242 return -EBADF;
243
244 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
245 return -EFAULT;
246
247 start = range[0];
248 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700249 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200250
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400251 if (start & 511)
252 return -EINVAL;
253 if (len & 511)
254 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700255 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
256 return -EINVAL;
257 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400258 return -EINVAL;
259
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700260 /* Invalidate the page cache, including dirty pages */
261 mapping = bdev->bd_inode->i_mapping;
262 truncate_inode_pages_range(mapping, start, end);
263
264 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
Christoph Hellwigee472d82017-04-05 19:21:08 +0200265 BLKDEV_ZERO_NOUNMAP);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static int put_ushort(unsigned long arg, unsigned short val)
269{
270 return put_user(val, (unsigned short __user *)arg);
271}
272
273static int put_int(unsigned long arg, int val)
274{
275 return put_user(val, (int __user *)arg);
276}
277
Martin K. Petersenac481c22009-10-03 20:52:01 +0200278static int put_uint(unsigned long arg, unsigned int val)
279{
280 return put_user(val, (unsigned int __user *)arg);
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static int put_long(unsigned long arg, long val)
284{
285 return put_user(val, (long __user *)arg);
286}
287
288static int put_ulong(unsigned long arg, unsigned long val)
289{
290 return put_user(val, (unsigned long __user *)arg);
291}
292
293static int put_u64(unsigned long arg, u64 val)
294{
295 return put_user(val, (u64 __user *)arg);
296}
297
Al Viro633a08b2007-08-29 20:34:12 -0400298int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
299 unsigned cmd, unsigned long arg)
300{
301 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d62008-03-02 09:09:22 -0500302
303 if (disk->fops->ioctl)
304 return disk->fops->ioctl(bdev, mode, cmd, arg);
305
Al Viro633a08b2007-08-29 20:34:12 -0400306 return -ENOTTY;
307}
308/*
309 * For the record: _GPL here is only because somebody decided to slap it
310 * on the previous export. Sheer idiocy, since it wasn't copyrightable
311 * at all and could be open-coded without any exports by anybody who cares.
312 */
313EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
314
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200315static int blkdev_pr_register(struct block_device *bdev,
316 struct pr_registration __user *arg)
317{
318 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
319 struct pr_registration reg;
320
321 if (!capable(CAP_SYS_ADMIN))
322 return -EPERM;
323 if (!ops || !ops->pr_register)
324 return -EOPNOTSUPP;
325 if (copy_from_user(&reg, arg, sizeof(reg)))
326 return -EFAULT;
327
328 if (reg.flags & ~PR_FL_IGNORE_KEY)
329 return -EOPNOTSUPP;
330 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
331}
332
333static int blkdev_pr_reserve(struct block_device *bdev,
334 struct pr_reservation __user *arg)
335{
336 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
337 struct pr_reservation rsv;
338
339 if (!capable(CAP_SYS_ADMIN))
340 return -EPERM;
341 if (!ops || !ops->pr_reserve)
342 return -EOPNOTSUPP;
343 if (copy_from_user(&rsv, arg, sizeof(rsv)))
344 return -EFAULT;
345
346 if (rsv.flags & ~PR_FL_IGNORE_KEY)
347 return -EOPNOTSUPP;
348 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
349}
350
351static int blkdev_pr_release(struct block_device *bdev,
352 struct pr_reservation __user *arg)
353{
354 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
355 struct pr_reservation rsv;
356
357 if (!capable(CAP_SYS_ADMIN))
358 return -EPERM;
359 if (!ops || !ops->pr_release)
360 return -EOPNOTSUPP;
361 if (copy_from_user(&rsv, arg, sizeof(rsv)))
362 return -EFAULT;
363
364 if (rsv.flags)
365 return -EOPNOTSUPP;
366 return ops->pr_release(bdev, rsv.key, rsv.type);
367}
368
369static int blkdev_pr_preempt(struct block_device *bdev,
370 struct pr_preempt __user *arg, bool abort)
371{
372 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
373 struct pr_preempt p;
374
375 if (!capable(CAP_SYS_ADMIN))
376 return -EPERM;
377 if (!ops || !ops->pr_preempt)
378 return -EOPNOTSUPP;
379 if (copy_from_user(&p, arg, sizeof(p)))
380 return -EFAULT;
381
382 if (p.flags)
383 return -EOPNOTSUPP;
384 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
385}
386
387static int blkdev_pr_clear(struct block_device *bdev,
388 struct pr_clear __user *arg)
389{
390 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
391 struct pr_clear c;
392
393 if (!capable(CAP_SYS_ADMIN))
394 return -EPERM;
395 if (!ops || !ops->pr_clear)
396 return -EOPNOTSUPP;
397 if (copy_from_user(&c, arg, sizeof(c)))
398 return -EFAULT;
399
400 if (c.flags)
401 return -EOPNOTSUPP;
402 return ops->pr_clear(bdev, c.key);
403}
404
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200405/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800406 * Is it an unrecognized ioctl? The correct returns are either
407 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
408 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
409 * code before returning.
410 *
411 * Confused drivers sometimes return EINVAL, which is wrong. It
412 * means "I understood the ioctl command, but the parameters to
413 * it were wrong".
414 *
415 * We should aim to just fix the broken drivers, the EINVAL case
416 * should go away.
417 */
418static inline int is_unrecognized_ioctl(int ret)
419{
420 return ret == -EINVAL ||
421 ret == -ENOTTY ||
422 ret == -ENOIOCTLCMD;
423}
424
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200425static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
426 unsigned cmd, unsigned long arg)
427{
428 int ret;
429
430 if (!capable(CAP_SYS_ADMIN))
431 return -EACCES;
432
433 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
434 if (!is_unrecognized_ioctl(ret))
435 return ret;
436
437 fsync_bdev(bdev);
438 invalidate_bdev(bdev);
439 return 0;
440}
441
442static int blkdev_roset(struct block_device *bdev, fmode_t mode,
443 unsigned cmd, unsigned long arg)
444{
445 int ret, n;
446
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200447 if (!capable(CAP_SYS_ADMIN))
448 return -EACCES;
449
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200450 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
451 if (!is_unrecognized_ioctl(ret))
452 return ret;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200453 if (get_user(n, (int __user *)arg))
454 return -EFAULT;
455 set_device_ro(bdev, n);
456 return 0;
457}
458
459static int blkdev_getgeo(struct block_device *bdev,
460 struct hd_geometry __user *argp)
461{
462 struct gendisk *disk = bdev->bd_disk;
463 struct hd_geometry geo;
464 int ret;
465
466 if (!argp)
467 return -EINVAL;
468 if (!disk->fops->getgeo)
469 return -ENOTTY;
470
471 /*
472 * We need to set the startsect first, the driver may
473 * want to override it.
474 */
475 memset(&geo, 0, sizeof(geo));
476 geo.start = get_start_sect(bdev);
477 ret = disk->fops->getgeo(bdev, &geo);
478 if (ret)
479 return ret;
480 if (copy_to_user(argp, &geo, sizeof(geo)))
481 return -EFAULT;
482 return 0;
483}
484
485/* set the logical block size */
486static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
487 int __user *argp)
488{
489 int ret, n;
490
491 if (!capable(CAP_SYS_ADMIN))
492 return -EACCES;
493 if (!argp)
494 return -EINVAL;
495 if (get_user(n, argp))
496 return -EFAULT;
497
498 if (!(mode & FMODE_EXCL)) {
499 bdgrab(bdev);
500 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
501 return -EBUSY;
502 }
503
504 ret = set_blocksize(bdev, n);
505 if (!(mode & FMODE_EXCL))
506 blkdev_put(bdev, mode | FMODE_EXCL);
507 return ret;
508}
509
Linus Torvalds07d106d2012-01-05 15:40:12 -0800510/*
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200511 * always keep this in sync with compat_blkdev_ioctl()
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200512 */
Al Viro56b26ad2008-09-19 03:17:36 -0400513int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700514 unsigned long arg)
515{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200516 void __user *argp = (void __user *)arg;
Al Viro45048d02008-09-18 15:53:24 -0400517 loff_t size;
Akinobu Mita63f26492014-05-25 21:43:33 +0900518 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700519
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200520 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200522 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200524 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700525 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200526 return blk_ioctl_discard(bdev, mode, arg, 0);
527 case BLKSECDISCARD:
528 return blk_ioctl_discard(bdev, mode, arg,
529 BLKDEV_DISCARD_SECURE);
530 case BLKZEROOUT:
531 return blk_ioctl_zeroout(bdev, mode, arg);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900532 case BLKREPORTZONE:
533 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
534 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900535 case BLKOPENZONE:
536 case BLKCLOSEZONE:
537 case BLKFINISHZONE:
538 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900539 case BLKGETZONESZ:
540 return put_uint(arg, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900541 case BLKGETNRZONES:
542 return put_uint(arg, blkdev_nr_zones(bdev));
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200543 case HDIO_GETGEO:
544 return blkdev_getgeo(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400545 case BLKRAGET:
546 case BLKFRAGET:
547 if (!arg)
548 return -EINVAL;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100549 return put_long(arg, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
Al Viro45048d02008-09-18 15:53:24 -0400550 case BLKROGET:
551 return put_int(arg, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200552 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
Al Viro45048d02008-09-18 15:53:24 -0400553 return put_int(arg, block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200554 case BLKSSZGET: /* get block device logical block size */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400555 return put_int(arg, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200556 case BLKPBSZGET: /* get block device physical block size */
557 return put_uint(arg, bdev_physical_block_size(bdev));
558 case BLKIOMIN:
559 return put_uint(arg, bdev_io_min(bdev));
560 case BLKIOOPT:
561 return put_uint(arg, bdev_io_opt(bdev));
562 case BLKALIGNOFF:
563 return put_int(arg, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100564 case BLKDISCARDZEROES:
Christoph Hellwig48920ff2017-04-05 19:21:23 +0200565 return put_uint(arg, 0);
Al Viro45048d02008-09-18 15:53:24 -0400566 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900567 max_sectors = min_t(unsigned int, USHRT_MAX,
568 queue_max_sectors(bdev_get_queue(bdev)));
569 return put_ushort(arg, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100570 case BLKROTATIONAL:
571 return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400572 case BLKRASET:
573 case BLKFRASET:
574 if(!capable(CAP_SYS_ADMIN))
575 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100576 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400577 return 0;
578 case BLKBSZSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200579 return blkdev_bszset(bdev, mode, argp);
Al Viro45048d02008-09-18 15:53:24 -0400580 case BLKPG:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200581 return blkpg_ioctl(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400582 case BLKRRPART:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200583 return blkdev_reread_part(bdev);
Al Viro45048d02008-09-18 15:53:24 -0400584 case BLKGETSIZE:
Mike Snitzer77304d22010-11-08 14:39:12 +0100585 size = i_size_read(bdev->bd_inode);
Al Viro45048d02008-09-18 15:53:24 -0400586 if ((size >> 9) > ~0UL)
587 return -EFBIG;
588 return put_ulong(arg, size >> 9);
589 case BLKGETSIZE64:
Mike Snitzer77304d22010-11-08 14:39:12 +0100590 return put_u64(arg, i_size_read(bdev->bd_inode));
Al Viro45048d02008-09-18 15:53:24 -0400591 case BLKTRACESTART:
592 case BLKTRACESTOP:
593 case BLKTRACESETUP:
594 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200595 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200596 case IOC_PR_REGISTER:
597 return blkdev_pr_register(bdev, argp);
598 case IOC_PR_RESERVE:
599 return blkdev_pr_reserve(bdev, argp);
600 case IOC_PR_RELEASE:
601 return blkdev_pr_release(bdev, argp);
602 case IOC_PR_PREEMPT:
603 return blkdev_pr_preempt(bdev, argp, false);
604 case IOC_PR_PREEMPT_ABORT:
605 return blkdev_pr_preempt(bdev, argp, true);
606 case IOC_PR_CLEAR:
607 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400608 default:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200609 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Al Viro45048d02008-09-18 15:53:24 -0400610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Stephen Tweedie68f66fe2005-05-13 23:31:19 -0400612EXPORT_SYMBOL_GPL(blkdev_ioctl);