blob: 6e827de1a4c439c6489b51de11a3b8f53a27fa58 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Randy.Dunlapc59ede72006-01-11 12:17:46 -08002#include <linux/capability.h>
Arnd Bergmannee6a1292019-11-28 15:48:10 +01003#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/blkdev.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -04005#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/blkpg.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08008#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/backing-dev.h>
Al Viroff01bb42011-09-16 02:31:11 -040010#include <linux/fs.h>
Jens Axboe2056a782006-03-23 20:00:26 +010011#include <linux/blktrace_api.h>
Christoph Hellwigbbd3e062015-10-15 14:10:48 +020012#include <linux/pr.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080013#include <linux/uaccess.h>
Christoph Hellwig581e2602020-03-25 16:48:41 +010014#include "blk.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010016static int blkpg_do_ioctl(struct block_device *bdev,
17 struct blkpg_partition __user *upart, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
19 struct block_device *bdevp;
20 struct gendisk *disk;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020021 struct hd_struct *part, *lpart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 struct blkpg_partition p;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020023 struct disk_part_iter piter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 long long start, length;
Tejun Heocf771cb2008-09-03 09:01:09 +020025 int partno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 if (!capable(CAP_SYS_ADMIN))
28 return -EACCES;
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010029 if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 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;
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010037 switch (op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 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
Arnd Bergmann5fb889f2019-11-28 11:28:41 +0100158static int blkpg_ioctl(struct block_device *bdev,
159 struct blkpg_ioctl_arg __user *arg)
160{
161 struct blkpg_partition __user *udata;
162 int op;
163
164 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
165 return -EFAULT;
166
167 return blkpg_do_ioctl(bdev, udata, op);
168}
169
170#ifdef CONFIG_COMPAT
171struct compat_blkpg_ioctl_arg {
172 compat_int_t op;
173 compat_int_t flags;
174 compat_int_t datalen;
175 compat_caddr_t data;
176};
177
178static int compat_blkpg_ioctl(struct block_device *bdev,
179 struct compat_blkpg_ioctl_arg __user *arg)
180{
181 compat_caddr_t udata;
182 int op;
183
184 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
185 return -EFAULT;
186
187 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
188}
189#endif
190
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100191static int blkdev_reread_part(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100193 int ret;
194
Christoph Hellwig142fe8f2019-11-14 15:34:35 +0100195 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EINVAL;
197 if (!capable(CAP_SYS_ADMIN))
198 return -EACCES;
Jarod Wilsonbe324172015-05-06 12:26:22 +0800199
Ming Leib04a56362015-05-06 12:26:27 +0800200 mutex_lock(&bdev->bd_mutex);
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100201 ret = bdev_disk_changed(bdev, false);
Arjan van de Venc039e312006-03-23 03:00:28 -0800202 mutex_unlock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800203
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100204 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200207static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
208 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100209{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200210 uint64_t range[2];
211 uint64_t start, len;
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600212 struct request_queue *q = bdev_get_queue(bdev);
213 struct address_space *mapping = bdev->bd_inode->i_mapping;
214
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200215
216 if (!(mode & FMODE_WRITE))
217 return -EBADF;
218
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600219 if (!blk_queue_discard(q))
220 return -EOPNOTSUPP;
221
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200222 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
223 return -EFAULT;
224
225 start = range[0];
226 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700227
David Woodhoused30a2602008-08-11 15:58:42 +0100228 if (start & 511)
229 return -EINVAL;
230 if (len & 511)
231 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100232
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600233 if (start + len > i_size_read(bdev->bd_inode))
David Woodhoused30a2602008-08-11 15:58:42 +0100234 return -EINVAL;
Ming Lei0bd1ed42018-02-10 08:46:17 +0800235 truncate_inode_pages_range(mapping, start, start + len - 1);
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600236 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
237 GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100238}
239
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200240static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
241 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400242{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200243 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700244 struct address_space *mapping;
245 uint64_t start, end, len;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200246
247 if (!(mode & FMODE_WRITE))
248 return -EBADF;
249
250 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
251 return -EFAULT;
252
253 start = range[0];
254 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700255 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200256
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400257 if (start & 511)
258 return -EINVAL;
259 if (len & 511)
260 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700261 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
262 return -EINVAL;
263 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400264 return -EINVAL;
265
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700266 /* Invalidate the page cache, including dirty pages */
267 mapping = bdev->bd_inode->i_mapping;
268 truncate_inode_pages_range(mapping, start, end);
269
270 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
Christoph Hellwigee472d82017-04-05 19:21:08 +0200271 BLKDEV_ZERO_NOUNMAP);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400272}
273
Arnd Bergmann9b816482019-11-29 11:48:31 +0100274static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100276 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Arnd Bergmann9b816482019-11-29 11:48:31 +0100279static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100281 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Arnd Bergmann9b816482019-11-29 11:48:31 +0100284static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200285{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100286 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200287}
288
Arnd Bergmann9b816482019-11-29 11:48:31 +0100289static int put_long(long __user *argp, long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100291 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Arnd Bergmann9b816482019-11-29 11:48:31 +0100294static int put_ulong(unsigned long __user *argp, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100296 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Arnd Bergmann9b816482019-11-29 11:48:31 +0100299static int put_u64(u64 __user *argp, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100301 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100304#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100305static int compat_put_long(compat_long_t *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100306{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100307 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100308}
309
Arnd Bergmann9b816482019-11-29 11:48:31 +0100310static int compat_put_ulong(compat_ulong_t *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100311{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100312 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100313}
314#endif
315
Al Viro633a08b2007-08-29 20:34:12 -0400316int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
317 unsigned cmd, unsigned long arg)
318{
319 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d62008-03-02 09:09:22 -0500320
321 if (disk->fops->ioctl)
322 return disk->fops->ioctl(bdev, mode, cmd, arg);
323
Al Viro633a08b2007-08-29 20:34:12 -0400324 return -ENOTTY;
325}
326/*
327 * For the record: _GPL here is only because somebody decided to slap it
328 * on the previous export. Sheer idiocy, since it wasn't copyrightable
329 * at all and could be open-coded without any exports by anybody who cares.
330 */
331EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
332
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100333#ifdef CONFIG_COMPAT
334/*
335 * This is the equivalent of compat_ptr_ioctl(), to be used by block
336 * drivers that implement only commands that are completely compatible
337 * between 32-bit and 64-bit user space
338 */
339int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
340 unsigned cmd, unsigned long arg)
341{
342 struct gendisk *disk = bdev->bd_disk;
343
344 if (disk->fops->ioctl)
345 return disk->fops->ioctl(bdev, mode, cmd,
346 (unsigned long)compat_ptr(arg));
347
348 return -ENOIOCTLCMD;
349}
350EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
351#endif
352
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200353static int blkdev_pr_register(struct block_device *bdev,
354 struct pr_registration __user *arg)
355{
356 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
357 struct pr_registration reg;
358
359 if (!capable(CAP_SYS_ADMIN))
360 return -EPERM;
361 if (!ops || !ops->pr_register)
362 return -EOPNOTSUPP;
363 if (copy_from_user(&reg, arg, sizeof(reg)))
364 return -EFAULT;
365
366 if (reg.flags & ~PR_FL_IGNORE_KEY)
367 return -EOPNOTSUPP;
368 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
369}
370
371static int blkdev_pr_reserve(struct block_device *bdev,
372 struct pr_reservation __user *arg)
373{
374 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
375 struct pr_reservation rsv;
376
377 if (!capable(CAP_SYS_ADMIN))
378 return -EPERM;
379 if (!ops || !ops->pr_reserve)
380 return -EOPNOTSUPP;
381 if (copy_from_user(&rsv, arg, sizeof(rsv)))
382 return -EFAULT;
383
384 if (rsv.flags & ~PR_FL_IGNORE_KEY)
385 return -EOPNOTSUPP;
386 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
387}
388
389static int blkdev_pr_release(struct block_device *bdev,
390 struct pr_reservation __user *arg)
391{
392 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
393 struct pr_reservation rsv;
394
395 if (!capable(CAP_SYS_ADMIN))
396 return -EPERM;
397 if (!ops || !ops->pr_release)
398 return -EOPNOTSUPP;
399 if (copy_from_user(&rsv, arg, sizeof(rsv)))
400 return -EFAULT;
401
402 if (rsv.flags)
403 return -EOPNOTSUPP;
404 return ops->pr_release(bdev, rsv.key, rsv.type);
405}
406
407static int blkdev_pr_preempt(struct block_device *bdev,
408 struct pr_preempt __user *arg, bool abort)
409{
410 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
411 struct pr_preempt p;
412
413 if (!capable(CAP_SYS_ADMIN))
414 return -EPERM;
415 if (!ops || !ops->pr_preempt)
416 return -EOPNOTSUPP;
417 if (copy_from_user(&p, arg, sizeof(p)))
418 return -EFAULT;
419
420 if (p.flags)
421 return -EOPNOTSUPP;
422 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
423}
424
425static int blkdev_pr_clear(struct block_device *bdev,
426 struct pr_clear __user *arg)
427{
428 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
429 struct pr_clear c;
430
431 if (!capable(CAP_SYS_ADMIN))
432 return -EPERM;
433 if (!ops || !ops->pr_clear)
434 return -EOPNOTSUPP;
435 if (copy_from_user(&c, arg, sizeof(c)))
436 return -EFAULT;
437
438 if (c.flags)
439 return -EOPNOTSUPP;
440 return ops->pr_clear(bdev, c.key);
441}
442
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200443/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800444 * Is it an unrecognized ioctl? The correct returns are either
445 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
446 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
447 * code before returning.
448 *
449 * Confused drivers sometimes return EINVAL, which is wrong. It
450 * means "I understood the ioctl command, but the parameters to
451 * it were wrong".
452 *
453 * We should aim to just fix the broken drivers, the EINVAL case
454 * should go away.
455 */
456static inline int is_unrecognized_ioctl(int ret)
457{
458 return ret == -EINVAL ||
459 ret == -ENOTTY ||
460 ret == -ENOIOCTLCMD;
461}
462
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200463static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
464 unsigned cmd, unsigned long arg)
465{
466 int ret;
467
468 if (!capable(CAP_SYS_ADMIN))
469 return -EACCES;
470
471 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
472 if (!is_unrecognized_ioctl(ret))
473 return ret;
474
475 fsync_bdev(bdev);
476 invalidate_bdev(bdev);
477 return 0;
478}
479
480static int blkdev_roset(struct block_device *bdev, fmode_t mode,
481 unsigned cmd, unsigned long arg)
482{
483 int ret, n;
484
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200485 if (!capable(CAP_SYS_ADMIN))
486 return -EACCES;
487
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200488 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
489 if (!is_unrecognized_ioctl(ret))
490 return ret;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200491 if (get_user(n, (int __user *)arg))
492 return -EFAULT;
493 set_device_ro(bdev, n);
494 return 0;
495}
496
497static int blkdev_getgeo(struct block_device *bdev,
498 struct hd_geometry __user *argp)
499{
500 struct gendisk *disk = bdev->bd_disk;
501 struct hd_geometry geo;
502 int ret;
503
504 if (!argp)
505 return -EINVAL;
506 if (!disk->fops->getgeo)
507 return -ENOTTY;
508
509 /*
510 * We need to set the startsect first, the driver may
511 * want to override it.
512 */
513 memset(&geo, 0, sizeof(geo));
514 geo.start = get_start_sect(bdev);
515 ret = disk->fops->getgeo(bdev, &geo);
516 if (ret)
517 return ret;
518 if (copy_to_user(argp, &geo, sizeof(geo)))
519 return -EFAULT;
520 return 0;
521}
522
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100523#ifdef CONFIG_COMPAT
524struct compat_hd_geometry {
525 unsigned char heads;
526 unsigned char sectors;
527 unsigned short cylinders;
528 u32 start;
529};
530
Arnd Bergmann9b816482019-11-29 11:48:31 +0100531static int compat_hdio_getgeo(struct block_device *bdev,
532 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100533{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100534 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100535 struct hd_geometry geo;
536 int ret;
537
538 if (!ugeo)
539 return -EINVAL;
540 if (!disk->fops->getgeo)
541 return -ENOTTY;
542
543 memset(&geo, 0, sizeof(geo));
544 /*
545 * We need to set the startsect first, the driver may
546 * want to override it.
547 */
548 geo.start = get_start_sect(bdev);
549 ret = disk->fops->getgeo(bdev, &geo);
550 if (ret)
551 return ret;
552
553 ret = copy_to_user(ugeo, &geo, 4);
554 ret |= put_user(geo.start, &ugeo->start);
555 if (ret)
556 ret = -EFAULT;
557
558 return ret;
559}
560#endif
561
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200562/* set the logical block size */
563static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
564 int __user *argp)
565{
566 int ret, n;
567
568 if (!capable(CAP_SYS_ADMIN))
569 return -EACCES;
570 if (!argp)
571 return -EINVAL;
572 if (get_user(n, argp))
573 return -EFAULT;
574
575 if (!(mode & FMODE_EXCL)) {
576 bdgrab(bdev);
577 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
578 return -EBUSY;
579 }
580
581 ret = set_blocksize(bdev, n);
582 if (!(mode & FMODE_EXCL))
583 blkdev_put(bdev, mode | FMODE_EXCL);
584 return ret;
585}
586
Linus Torvalds07d106d2012-01-05 15:40:12 -0800587/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100588 * Common commands that are handled the same way on native and compat
589 * user space. Note the separate arg/argp parameters that are needed
590 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200591 */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100592static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
593 unsigned cmd, unsigned long arg, void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700594{
Akinobu Mita63f26492014-05-25 21:43:33 +0900595 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700596
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200597 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200599 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200601 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700602 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200603 return blk_ioctl_discard(bdev, mode, arg, 0);
604 case BLKSECDISCARD:
605 return blk_ioctl_discard(bdev, mode, arg,
606 BLKDEV_DISCARD_SECURE);
607 case BLKZEROOUT:
608 return blk_ioctl_zeroout(bdev, mode, arg);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900609 case BLKREPORTZONE:
610 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
611 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900612 case BLKOPENZONE:
613 case BLKCLOSEZONE:
614 case BLKFINISHZONE:
615 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900616 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100617 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900618 case BLKGETNRZONES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100619 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
Al Viro45048d02008-09-18 15:53:24 -0400620 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100621 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200622 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100623 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200624 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100625 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200626 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100627 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200628 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100629 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200630 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100631 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100632 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100633 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400634 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900635 max_sectors = min_t(unsigned int, USHRT_MAX,
636 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100637 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100638 case BLKROTATIONAL:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100639 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400640 case BLKRASET:
641 case BLKFRASET:
642 if(!capable(CAP_SYS_ADMIN))
643 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100644 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400645 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400646 case BLKRRPART:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200647 return blkdev_reread_part(bdev);
Al Viro45048d02008-09-18 15:53:24 -0400648 case BLKTRACESTART:
649 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400650 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200651 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200652 case IOC_PR_REGISTER:
653 return blkdev_pr_register(bdev, argp);
654 case IOC_PR_RESERVE:
655 return blkdev_pr_reserve(bdev, argp);
656 case IOC_PR_RELEASE:
657 return blkdev_pr_release(bdev, argp);
658 case IOC_PR_PREEMPT:
659 return blkdev_pr_preempt(bdev, argp, false);
660 case IOC_PR_PREEMPT_ABORT:
661 return blkdev_pr_preempt(bdev, argp, true);
662 case IOC_PR_CLEAR:
663 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400664 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100665 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100668
669/*
670 * Always keep this in sync with compat_blkdev_ioctl()
671 * to handle all incompatible commands in both functions.
672 *
673 * New commands must be compatible and go into blkdev_common_ioctl
674 */
675int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
676 unsigned long arg)
677{
678 int ret;
679 loff_t size;
680 void __user *argp = (void __user *)arg;
681
682 switch (cmd) {
683 /* These need separate implementations for the data structure */
684 case HDIO_GETGEO:
685 return blkdev_getgeo(bdev, argp);
686 case BLKPG:
687 return blkpg_ioctl(bdev, argp);
688
689 /* Compat mode returns 32-bit data instead of 'long' */
690 case BLKRAGET:
691 case BLKFRAGET:
692 if (!argp)
693 return -EINVAL;
694 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
695 case BLKGETSIZE:
696 size = i_size_read(bdev->bd_inode);
697 if ((size >> 9) > ~0UL)
698 return -EFBIG;
699 return put_ulong(argp, size >> 9);
700
701 /* The data is compatible, but the command number is different */
702 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
703 return put_int(argp, block_size(bdev));
704 case BLKBSZSET:
705 return blkdev_bszset(bdev, mode, argp);
706 case BLKGETSIZE64:
707 return put_u64(argp, i_size_read(bdev->bd_inode));
708
709 /* Incompatible alignment on i386 */
710 case BLKTRACESETUP:
711 return blk_trace_ioctl(bdev, cmd, argp);
712 default:
713 break;
714 }
715
716 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
717 if (ret == -ENOIOCTLCMD)
718 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
719
720 return ret;
721}
722EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100723
724#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100725
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100726#define BLKBSZGET_32 _IOR(0x12, 112, int)
727#define BLKBSZSET_32 _IOW(0x12, 113, int)
728#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
729
730/* Most of the generic ioctls are handled in the normal fallback path.
731 This assumes the blkdev's low level compat_ioctl always returns
732 ENOIOCTLCMD for unknown ioctls. */
733long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
734{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100735 int ret;
736 void __user *argp = compat_ptr(arg);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100737 struct inode *inode = file->f_mapping->host;
738 struct block_device *bdev = inode->i_bdev;
739 struct gendisk *disk = bdev->bd_disk;
740 fmode_t mode = file->f_mode;
741 loff_t size;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100742
743 /*
744 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
745 * to updated it before every ioctl.
746 */
747 if (file->f_flags & O_NDELAY)
748 mode |= FMODE_NDELAY;
749 else
750 mode &= ~FMODE_NDELAY;
751
752 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100753 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100754 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100755 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100756 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100757 return compat_blkpg_ioctl(bdev, argp);
758
759 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100760 case BLKRAGET:
761 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100762 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100763 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100764 return compat_put_long(argp,
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100765 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100766 case BLKGETSIZE:
767 size = i_size_read(bdev->bd_inode);
768 if ((size >> 9) > ~0UL)
769 return -EFBIG;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100770 return compat_put_ulong(argp, size >> 9);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100771
Arnd Bergmann9b816482019-11-29 11:48:31 +0100772 /* The data is compatible, but the command number is different */
773 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
774 return put_int(argp, bdev_logical_block_size(bdev));
775 case BLKBSZSET_32:
776 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100777 case BLKGETSIZE64_32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100778 return put_u64(argp, i_size_read(bdev->bd_inode));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100779
Arnd Bergmann9b816482019-11-29 11:48:31 +0100780 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100781 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100782 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100783 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100784 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100785 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100786
787 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
788 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
789 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
790
791 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100792}
793#endif