blob: 127194b9f9bda40ade08d36c89445aae32223cba [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010015static int blkpg_do_ioctl(struct block_device *bdev,
16 struct blkpg_partition __user *upart, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
18 struct block_device *bdevp;
19 struct gendisk *disk;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +020020 struct hd_struct *part, *lpart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 struct blkpg_partition p;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020022 struct disk_part_iter piter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 long long start, length;
Tejun Heocf771cb2008-09-03 09:01:09 +020024 int partno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26 if (!capable(CAP_SYS_ADMIN))
27 return -EACCES;
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010028 if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 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;
Arnd Bergmann5fb889f2019-11-28 11:28:41 +010036 switch (op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 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
Arnd Bergmann5fb889f2019-11-28 11:28:41 +0100157static int blkpg_ioctl(struct block_device *bdev,
158 struct blkpg_ioctl_arg __user *arg)
159{
160 struct blkpg_partition __user *udata;
161 int op;
162
163 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
164 return -EFAULT;
165
166 return blkpg_do_ioctl(bdev, udata, op);
167}
168
169#ifdef CONFIG_COMPAT
170struct compat_blkpg_ioctl_arg {
171 compat_int_t op;
172 compat_int_t flags;
173 compat_int_t datalen;
174 compat_caddr_t data;
175};
176
177static int compat_blkpg_ioctl(struct block_device *bdev,
178 struct compat_blkpg_ioctl_arg __user *arg)
179{
180 compat_caddr_t udata;
181 int op;
182
183 if (get_user(op, &arg->op) || get_user(udata, &arg->data))
184 return -EFAULT;
185
186 return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
187}
188#endif
189
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100190static int blkdev_reread_part(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100192 int ret;
193
Christoph Hellwig142fe8f2019-11-14 15:34:35 +0100194 if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EINVAL;
196 if (!capable(CAP_SYS_ADMIN))
197 return -EACCES;
Jarod Wilsonbe324172015-05-06 12:26:22 +0800198
Ming Leib04a56362015-05-06 12:26:27 +0800199 mutex_lock(&bdev->bd_mutex);
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100200 ret = bdev_disk_changed(bdev, false);
Arjan van de Venc039e312006-03-23 03:00:28 -0800201 mutex_unlock(&bdev->bd_mutex);
Jarod Wilsonbe324172015-05-06 12:26:22 +0800202
Christoph Hellwigf0b870d2019-11-14 15:34:36 +0100203 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200206static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
207 unsigned long arg, unsigned long flags)
David Woodhoused30a2602008-08-11 15:58:42 +0100208{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200209 uint64_t range[2];
210 uint64_t start, len;
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600211 struct request_queue *q = bdev_get_queue(bdev);
212 struct address_space *mapping = bdev->bd_inode->i_mapping;
213
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200214
215 if (!(mode & FMODE_WRITE))
216 return -EBADF;
217
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600218 if (!blk_queue_discard(q))
219 return -EOPNOTSUPP;
220
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200221 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
222 return -EFAULT;
223
224 start = range[0];
225 len = range[1];
Adrian Hunter8d57a982010-08-11 14:17:49 -0700226
David Woodhoused30a2602008-08-11 15:58:42 +0100227 if (start & 511)
228 return -EINVAL;
229 if (len & 511)
230 return -EINVAL;
David Woodhoused30a2602008-08-11 15:58:42 +0100231
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600232 if (start + len > i_size_read(bdev->bd_inode))
David Woodhoused30a2602008-08-11 15:58:42 +0100233 return -EINVAL;
Ming Lei0bd1ed42018-02-10 08:46:17 +0800234 truncate_inode_pages_range(mapping, start, start + len - 1);
Dmitry Monakhov351499a12017-10-24 18:44:57 -0600235 return blkdev_issue_discard(bdev, start >> 9, len >> 9,
236 GFP_KERNEL, flags);
David Woodhoused30a2602008-08-11 15:58:42 +0100237}
238
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200239static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
240 unsigned long arg)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400241{
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200242 uint64_t range[2];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700243 struct address_space *mapping;
244 uint64_t start, end, len;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200245
246 if (!(mode & FMODE_WRITE))
247 return -EBADF;
248
249 if (copy_from_user(range, (void __user *)arg, sizeof(range)))
250 return -EFAULT;
251
252 start = range[0];
253 len = range[1];
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700254 end = start + len - 1;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200255
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400256 if (start & 511)
257 return -EINVAL;
258 if (len & 511)
259 return -EINVAL;
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700260 if (end >= (uint64_t)i_size_read(bdev->bd_inode))
261 return -EINVAL;
262 if (end < start)
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400263 return -EINVAL;
264
Darrick J. Wong22dd6d352016-10-11 13:51:05 -0700265 /* Invalidate the page cache, including dirty pages */
266 mapping = bdev->bd_inode->i_mapping;
267 truncate_inode_pages_range(mapping, start, end);
268
269 return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
Christoph Hellwigee472d82017-04-05 19:21:08 +0200270 BLKDEV_ZERO_NOUNMAP);
Martin K. Petersen66ba32d2012-09-18 12:19:29 -0400271}
272
Arnd Bergmann9b816482019-11-29 11:48:31 +0100273static int put_ushort(unsigned short __user *argp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100275 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Arnd Bergmann9b816482019-11-29 11:48:31 +0100278static int put_int(int __user *argp, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100280 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Arnd Bergmann9b816482019-11-29 11:48:31 +0100283static int put_uint(unsigned int __user *argp, unsigned int val)
Martin K. Petersenac481c22009-10-03 20:52:01 +0200284{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100285 return put_user(val, argp);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200286}
287
Arnd Bergmann9b816482019-11-29 11:48:31 +0100288static int put_long(long __user *argp, long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100290 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Arnd Bergmann9b816482019-11-29 11:48:31 +0100293static int put_ulong(unsigned long __user *argp, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100295 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Arnd Bergmann9b816482019-11-29 11:48:31 +0100298static int put_u64(u64 __user *argp, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100300 return put_user(val, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100303#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100304static int compat_put_long(compat_long_t *argp, long val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100305{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100306 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100307}
308
Arnd Bergmann9b816482019-11-29 11:48:31 +0100309static int compat_put_ulong(compat_ulong_t *argp, compat_ulong_t val)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100310{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100311 return put_user(val, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100312}
313#endif
314
Al Viro633a08b2007-08-29 20:34:12 -0400315int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
316 unsigned cmd, unsigned long arg)
317{
318 struct gendisk *disk = bdev->bd_disk;
Al Virod4430d62008-03-02 09:09:22 -0500319
320 if (disk->fops->ioctl)
321 return disk->fops->ioctl(bdev, mode, cmd, arg);
322
Al Viro633a08b2007-08-29 20:34:12 -0400323 return -ENOTTY;
324}
325/*
326 * For the record: _GPL here is only because somebody decided to slap it
327 * on the previous export. Sheer idiocy, since it wasn't copyrightable
328 * at all and could be open-coded without any exports by anybody who cares.
329 */
330EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
331
Arnd Bergmannee6a1292019-11-28 15:48:10 +0100332#ifdef CONFIG_COMPAT
333/*
334 * This is the equivalent of compat_ptr_ioctl(), to be used by block
335 * drivers that implement only commands that are completely compatible
336 * between 32-bit and 64-bit user space
337 */
338int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
339 unsigned cmd, unsigned long arg)
340{
341 struct gendisk *disk = bdev->bd_disk;
342
343 if (disk->fops->ioctl)
344 return disk->fops->ioctl(bdev, mode, cmd,
345 (unsigned long)compat_ptr(arg));
346
347 return -ENOIOCTLCMD;
348}
349EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
350#endif
351
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200352static int blkdev_pr_register(struct block_device *bdev,
353 struct pr_registration __user *arg)
354{
355 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
356 struct pr_registration reg;
357
358 if (!capable(CAP_SYS_ADMIN))
359 return -EPERM;
360 if (!ops || !ops->pr_register)
361 return -EOPNOTSUPP;
362 if (copy_from_user(&reg, arg, sizeof(reg)))
363 return -EFAULT;
364
365 if (reg.flags & ~PR_FL_IGNORE_KEY)
366 return -EOPNOTSUPP;
367 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
368}
369
370static int blkdev_pr_reserve(struct block_device *bdev,
371 struct pr_reservation __user *arg)
372{
373 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
374 struct pr_reservation rsv;
375
376 if (!capable(CAP_SYS_ADMIN))
377 return -EPERM;
378 if (!ops || !ops->pr_reserve)
379 return -EOPNOTSUPP;
380 if (copy_from_user(&rsv, arg, sizeof(rsv)))
381 return -EFAULT;
382
383 if (rsv.flags & ~PR_FL_IGNORE_KEY)
384 return -EOPNOTSUPP;
385 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
386}
387
388static int blkdev_pr_release(struct block_device *bdev,
389 struct pr_reservation __user *arg)
390{
391 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
392 struct pr_reservation rsv;
393
394 if (!capable(CAP_SYS_ADMIN))
395 return -EPERM;
396 if (!ops || !ops->pr_release)
397 return -EOPNOTSUPP;
398 if (copy_from_user(&rsv, arg, sizeof(rsv)))
399 return -EFAULT;
400
401 if (rsv.flags)
402 return -EOPNOTSUPP;
403 return ops->pr_release(bdev, rsv.key, rsv.type);
404}
405
406static int blkdev_pr_preempt(struct block_device *bdev,
407 struct pr_preempt __user *arg, bool abort)
408{
409 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
410 struct pr_preempt p;
411
412 if (!capable(CAP_SYS_ADMIN))
413 return -EPERM;
414 if (!ops || !ops->pr_preempt)
415 return -EOPNOTSUPP;
416 if (copy_from_user(&p, arg, sizeof(p)))
417 return -EFAULT;
418
419 if (p.flags)
420 return -EOPNOTSUPP;
421 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
422}
423
424static int blkdev_pr_clear(struct block_device *bdev,
425 struct pr_clear __user *arg)
426{
427 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
428 struct pr_clear c;
429
430 if (!capable(CAP_SYS_ADMIN))
431 return -EPERM;
432 if (!ops || !ops->pr_clear)
433 return -EOPNOTSUPP;
434 if (copy_from_user(&c, arg, sizeof(c)))
435 return -EFAULT;
436
437 if (c.flags)
438 return -EOPNOTSUPP;
439 return ops->pr_clear(bdev, c.key);
440}
441
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200442/*
Linus Torvalds07d106d2012-01-05 15:40:12 -0800443 * Is it an unrecognized ioctl? The correct returns are either
444 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
445 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
446 * code before returning.
447 *
448 * Confused drivers sometimes return EINVAL, which is wrong. It
449 * means "I understood the ioctl command, but the parameters to
450 * it were wrong".
451 *
452 * We should aim to just fix the broken drivers, the EINVAL case
453 * should go away.
454 */
455static inline int is_unrecognized_ioctl(int ret)
456{
457 return ret == -EINVAL ||
458 ret == -ENOTTY ||
459 ret == -ENOIOCTLCMD;
460}
461
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200462static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
463 unsigned cmd, unsigned long arg)
464{
465 int ret;
466
467 if (!capable(CAP_SYS_ADMIN))
468 return -EACCES;
469
470 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
471 if (!is_unrecognized_ioctl(ret))
472 return ret;
473
474 fsync_bdev(bdev);
475 invalidate_bdev(bdev);
476 return 0;
477}
478
479static int blkdev_roset(struct block_device *bdev, fmode_t mode,
480 unsigned cmd, unsigned long arg)
481{
482 int ret, n;
483
Ilya Dryomovbb749b32017-10-18 14:38:38 +0200484 if (!capable(CAP_SYS_ADMIN))
485 return -EACCES;
486
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200487 ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
488 if (!is_unrecognized_ioctl(ret))
489 return ret;
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200490 if (get_user(n, (int __user *)arg))
491 return -EFAULT;
492 set_device_ro(bdev, n);
493 return 0;
494}
495
496static int blkdev_getgeo(struct block_device *bdev,
497 struct hd_geometry __user *argp)
498{
499 struct gendisk *disk = bdev->bd_disk;
500 struct hd_geometry geo;
501 int ret;
502
503 if (!argp)
504 return -EINVAL;
505 if (!disk->fops->getgeo)
506 return -ENOTTY;
507
508 /*
509 * We need to set the startsect first, the driver may
510 * want to override it.
511 */
512 memset(&geo, 0, sizeof(geo));
513 geo.start = get_start_sect(bdev);
514 ret = disk->fops->getgeo(bdev, &geo);
515 if (ret)
516 return ret;
517 if (copy_to_user(argp, &geo, sizeof(geo)))
518 return -EFAULT;
519 return 0;
520}
521
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100522#ifdef CONFIG_COMPAT
523struct compat_hd_geometry {
524 unsigned char heads;
525 unsigned char sectors;
526 unsigned short cylinders;
527 u32 start;
528};
529
Arnd Bergmann9b816482019-11-29 11:48:31 +0100530static int compat_hdio_getgeo(struct block_device *bdev,
531 struct compat_hd_geometry __user *ugeo)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100532{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100533 struct gendisk *disk = bdev->bd_disk;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100534 struct hd_geometry geo;
535 int ret;
536
537 if (!ugeo)
538 return -EINVAL;
539 if (!disk->fops->getgeo)
540 return -ENOTTY;
541
542 memset(&geo, 0, sizeof(geo));
543 /*
544 * We need to set the startsect first, the driver may
545 * want to override it.
546 */
547 geo.start = get_start_sect(bdev);
548 ret = disk->fops->getgeo(bdev, &geo);
549 if (ret)
550 return ret;
551
552 ret = copy_to_user(ugeo, &geo, 4);
553 ret |= put_user(geo.start, &ugeo->start);
554 if (ret)
555 ret = -EFAULT;
556
557 return ret;
558}
559#endif
560
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200561/* set the logical block size */
562static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
563 int __user *argp)
564{
565 int ret, n;
566
567 if (!capable(CAP_SYS_ADMIN))
568 return -EACCES;
569 if (!argp)
570 return -EINVAL;
571 if (get_user(n, argp))
572 return -EFAULT;
573
574 if (!(mode & FMODE_EXCL)) {
575 bdgrab(bdev);
576 if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
577 return -EBUSY;
578 }
579
580 ret = set_blocksize(bdev, n);
581 if (!(mode & FMODE_EXCL))
582 blkdev_put(bdev, mode | FMODE_EXCL);
583 return ret;
584}
585
Linus Torvalds07d106d2012-01-05 15:40:12 -0800586/*
Arnd Bergmann9b816482019-11-29 11:48:31 +0100587 * Common commands that are handled the same way on native and compat
588 * user space. Note the separate arg/argp parameters that are needed
589 * to deal with the compat_ptr() conversion.
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200590 */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100591static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
592 unsigned cmd, unsigned long arg, void __user *argp)
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700593{
Akinobu Mita63f26492014-05-25 21:43:33 +0900594 unsigned int max_sectors;
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700595
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200596 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 case BLKFLSBUF:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200598 return blkdev_flushbuf(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 case BLKROSET:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200600 return blkdev_roset(bdev, mode, cmd, arg);
Adrian Hunter8d57a982010-08-11 14:17:49 -0700601 case BLKDISCARD:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200602 return blk_ioctl_discard(bdev, mode, arg, 0);
603 case BLKSECDISCARD:
604 return blk_ioctl_discard(bdev, mode, arg,
605 BLKDEV_DISCARD_SECURE);
606 case BLKZEROOUT:
607 return blk_ioctl_zeroout(bdev, mode, arg);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900608 case BLKREPORTZONE:
609 return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
610 case BLKRESETZONE:
Ajay Joshie876df12019-10-27 23:05:46 +0900611 case BLKOPENZONE:
612 case BLKCLOSEZONE:
613 case BLKFINISHZONE:
614 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
Damien Le Moal72cd8752018-10-12 19:08:45 +0900615 case BLKGETZONESZ:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100616 return put_uint(argp, bdev_zone_sectors(bdev));
Damien Le Moal65e4e3e2018-10-12 19:08:46 +0900617 case BLKGETNRZONES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100618 return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
Al Viro45048d02008-09-18 15:53:24 -0400619 case BLKROGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100620 return put_int(argp, bdev_read_only(bdev) != 0);
Martin K. Petersenac481c22009-10-03 20:52:01 +0200621 case BLKSSZGET: /* get block device logical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100622 return put_int(argp, bdev_logical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200623 case BLKPBSZGET: /* get block device physical block size */
Arnd Bergmann9b816482019-11-29 11:48:31 +0100624 return put_uint(argp, bdev_physical_block_size(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200625 case BLKIOMIN:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100626 return put_uint(argp, bdev_io_min(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200627 case BLKIOOPT:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100628 return put_uint(argp, bdev_io_opt(bdev));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200629 case BLKALIGNOFF:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100630 return put_int(argp, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100631 case BLKDISCARDZEROES:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100632 return put_uint(argp, 0);
Al Viro45048d02008-09-18 15:53:24 -0400633 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900634 max_sectors = min_t(unsigned int, USHRT_MAX,
635 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmann9b816482019-11-29 11:48:31 +0100636 return put_ushort(argp, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100637 case BLKROTATIONAL:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100638 return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
Al Viro45048d02008-09-18 15:53:24 -0400639 case BLKRASET:
640 case BLKFRASET:
641 if(!capable(CAP_SYS_ADMIN))
642 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100643 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Al Viro45048d02008-09-18 15:53:24 -0400644 return 0;
Al Viro45048d02008-09-18 15:53:24 -0400645 case BLKRRPART:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200646 return blkdev_reread_part(bdev);
Al Viro45048d02008-09-18 15:53:24 -0400647 case BLKTRACESTART:
648 case BLKTRACESTOP:
Al Viro45048d02008-09-18 15:53:24 -0400649 case BLKTRACETEARDOWN:
Christoph Hellwigd8e4bb8102015-10-15 14:10:47 +0200650 return blk_trace_ioctl(bdev, cmd, argp);
Christoph Hellwigbbd3e062015-10-15 14:10:48 +0200651 case IOC_PR_REGISTER:
652 return blkdev_pr_register(bdev, argp);
653 case IOC_PR_RESERVE:
654 return blkdev_pr_reserve(bdev, argp);
655 case IOC_PR_RELEASE:
656 return blkdev_pr_release(bdev, argp);
657 case IOC_PR_PREEMPT:
658 return blkdev_pr_preempt(bdev, argp, false);
659 case IOC_PR_PREEMPT_ABORT:
660 return blkdev_pr_preempt(bdev, argp, true);
661 case IOC_PR_CLEAR:
662 return blkdev_pr_clear(bdev, argp);
Al Viro45048d02008-09-18 15:53:24 -0400663 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100664 return -ENOIOCTLCMD;
Al Viro45048d02008-09-18 15:53:24 -0400665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
Arnd Bergmann9b816482019-11-29 11:48:31 +0100667
668/*
669 * Always keep this in sync with compat_blkdev_ioctl()
670 * to handle all incompatible commands in both functions.
671 *
672 * New commands must be compatible and go into blkdev_common_ioctl
673 */
674int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
675 unsigned long arg)
676{
677 int ret;
678 loff_t size;
679 void __user *argp = (void __user *)arg;
680
681 switch (cmd) {
682 /* These need separate implementations for the data structure */
683 case HDIO_GETGEO:
684 return blkdev_getgeo(bdev, argp);
685 case BLKPG:
686 return blkpg_ioctl(bdev, argp);
687
688 /* Compat mode returns 32-bit data instead of 'long' */
689 case BLKRAGET:
690 case BLKFRAGET:
691 if (!argp)
692 return -EINVAL;
693 return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
694 case BLKGETSIZE:
695 size = i_size_read(bdev->bd_inode);
696 if ((size >> 9) > ~0UL)
697 return -EFBIG;
698 return put_ulong(argp, size >> 9);
699
700 /* The data is compatible, but the command number is different */
701 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
702 return put_int(argp, block_size(bdev));
703 case BLKBSZSET:
704 return blkdev_bszset(bdev, mode, argp);
705 case BLKGETSIZE64:
706 return put_u64(argp, i_size_read(bdev->bd_inode));
707
708 /* Incompatible alignment on i386 */
709 case BLKTRACESETUP:
710 return blk_trace_ioctl(bdev, cmd, argp);
711 default:
712 break;
713 }
714
715 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
716 if (ret == -ENOIOCTLCMD)
717 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
718
719 return ret;
720}
721EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100722
723#ifdef CONFIG_COMPAT
Arnd Bergmann9b816482019-11-29 11:48:31 +0100724
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100725#define BLKBSZGET_32 _IOR(0x12, 112, int)
726#define BLKBSZSET_32 _IOW(0x12, 113, int)
727#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
728
729/* Most of the generic ioctls are handled in the normal fallback path.
730 This assumes the blkdev's low level compat_ioctl always returns
731 ENOIOCTLCMD for unknown ioctls. */
732long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
733{
Arnd Bergmann9b816482019-11-29 11:48:31 +0100734 int ret;
735 void __user *argp = compat_ptr(arg);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100736 struct inode *inode = file->f_mapping->host;
737 struct block_device *bdev = inode->i_bdev;
738 struct gendisk *disk = bdev->bd_disk;
739 fmode_t mode = file->f_mode;
740 loff_t size;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100741
742 /*
743 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
744 * to updated it before every ioctl.
745 */
746 if (file->f_flags & O_NDELAY)
747 mode |= FMODE_NDELAY;
748 else
749 mode &= ~FMODE_NDELAY;
750
751 switch (cmd) {
Arnd Bergmann9b816482019-11-29 11:48:31 +0100752 /* These need separate implementations for the data structure */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100753 case HDIO_GETGEO:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100754 return compat_hdio_getgeo(bdev, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100755 case BLKPG:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100756 return compat_blkpg_ioctl(bdev, argp);
757
758 /* Compat mode returns 32-bit data instead of 'long' */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100759 case BLKRAGET:
760 case BLKFRAGET:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100761 if (!argp)
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100762 return -EINVAL;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100763 return compat_put_long(argp,
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100764 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100765 case BLKGETSIZE:
766 size = i_size_read(bdev->bd_inode);
767 if ((size >> 9) > ~0UL)
768 return -EFBIG;
Arnd Bergmann9b816482019-11-29 11:48:31 +0100769 return compat_put_ulong(argp, size >> 9);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100770
Arnd Bergmann9b816482019-11-29 11:48:31 +0100771 /* The data is compatible, but the command number is different */
772 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
773 return put_int(argp, bdev_logical_block_size(bdev));
774 case BLKBSZSET_32:
775 return blkdev_bszset(bdev, mode, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100776 case BLKGETSIZE64_32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100777 return put_u64(argp, i_size_read(bdev->bd_inode));
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100778
Arnd Bergmann9b816482019-11-29 11:48:31 +0100779 /* Incompatible alignment on i386 */
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100780 case BLKTRACESETUP32:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100781 return blk_trace_ioctl(bdev, cmd, argp);
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100782 default:
Arnd Bergmann9b816482019-11-29 11:48:31 +0100783 break;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100784 }
Arnd Bergmann9b816482019-11-29 11:48:31 +0100785
786 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
787 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
788 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
789
790 return ret;
Arnd Bergmannbdc1dda2019-11-29 11:45:30 +0100791}
792#endif