Back to home page

LXR

 
 

    


0001 #include <linux/capability.h>
0002 #include <linux/blkdev.h>
0003 #include <linux/export.h>
0004 #include <linux/gfp.h>
0005 #include <linux/blkpg.h>
0006 #include <linux/hdreg.h>
0007 #include <linux/backing-dev.h>
0008 #include <linux/fs.h>
0009 #include <linux/blktrace_api.h>
0010 #include <linux/pr.h>
0011 #include <linux/uaccess.h>
0012 
0013 static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg)
0014 {
0015     struct block_device *bdevp;
0016     struct gendisk *disk;
0017     struct hd_struct *part, *lpart;
0018     struct blkpg_ioctl_arg a;
0019     struct blkpg_partition p;
0020     struct disk_part_iter piter;
0021     long long start, length;
0022     int partno;
0023 
0024     if (!capable(CAP_SYS_ADMIN))
0025         return -EACCES;
0026     if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
0027         return -EFAULT;
0028     if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
0029         return -EFAULT;
0030     disk = bdev->bd_disk;
0031     if (bdev != bdev->bd_contains)
0032         return -EINVAL;
0033     partno = p.pno;
0034     if (partno <= 0)
0035         return -EINVAL;
0036     switch (a.op) {
0037         case BLKPG_ADD_PARTITION:
0038             start = p.start >> 9;
0039             length = p.length >> 9;
0040             /* check for fit in a hd_struct */
0041             if (sizeof(sector_t) == sizeof(long) &&
0042                 sizeof(long long) > sizeof(long)) {
0043                 long pstart = start, plength = length;
0044                 if (pstart != start || plength != length
0045                     || pstart < 0 || plength < 0 || partno > 65535)
0046                     return -EINVAL;
0047             }
0048             /* check if partition is aligned to blocksize */
0049             if (p.start & (bdev_logical_block_size(bdev) - 1))
0050                 return -EINVAL;
0051 
0052             mutex_lock(&bdev->bd_mutex);
0053 
0054             /* overlap? */
0055             disk_part_iter_init(&piter, disk,
0056                         DISK_PITER_INCL_EMPTY);
0057             while ((part = disk_part_iter_next(&piter))) {
0058                 if (!(start + length <= part->start_sect ||
0059                       start >= part->start_sect + part->nr_sects)) {
0060                     disk_part_iter_exit(&piter);
0061                     mutex_unlock(&bdev->bd_mutex);
0062                     return -EBUSY;
0063                 }
0064             }
0065             disk_part_iter_exit(&piter);
0066 
0067             /* all seems OK */
0068             part = add_partition(disk, partno, start, length,
0069                          ADDPART_FLAG_NONE, NULL);
0070             mutex_unlock(&bdev->bd_mutex);
0071             return PTR_ERR_OR_ZERO(part);
0072         case BLKPG_DEL_PARTITION:
0073             part = disk_get_part(disk, partno);
0074             if (!part)
0075                 return -ENXIO;
0076 
0077             bdevp = bdget(part_devt(part));
0078             disk_put_part(part);
0079             if (!bdevp)
0080                 return -ENOMEM;
0081 
0082             mutex_lock(&bdevp->bd_mutex);
0083             if (bdevp->bd_openers) {
0084                 mutex_unlock(&bdevp->bd_mutex);
0085                 bdput(bdevp);
0086                 return -EBUSY;
0087             }
0088             /* all seems OK */
0089             fsync_bdev(bdevp);
0090             invalidate_bdev(bdevp);
0091 
0092             mutex_lock_nested(&bdev->bd_mutex, 1);
0093             delete_partition(disk, partno);
0094             mutex_unlock(&bdev->bd_mutex);
0095             mutex_unlock(&bdevp->bd_mutex);
0096             bdput(bdevp);
0097 
0098             return 0;
0099         case BLKPG_RESIZE_PARTITION:
0100             start = p.start >> 9;
0101             /* new length of partition in bytes */
0102             length = p.length >> 9;
0103             /* check for fit in a hd_struct */
0104             if (sizeof(sector_t) == sizeof(long) &&
0105                 sizeof(long long) > sizeof(long)) {
0106                 long pstart = start, plength = length;
0107                 if (pstart != start || plength != length
0108                     || pstart < 0 || plength < 0)
0109                     return -EINVAL;
0110             }
0111             part = disk_get_part(disk, partno);
0112             if (!part)
0113                 return -ENXIO;
0114             bdevp = bdget(part_devt(part));
0115             if (!bdevp) {
0116                 disk_put_part(part);
0117                 return -ENOMEM;
0118             }
0119             mutex_lock(&bdevp->bd_mutex);
0120             mutex_lock_nested(&bdev->bd_mutex, 1);
0121             if (start != part->start_sect) {
0122                 mutex_unlock(&bdevp->bd_mutex);
0123                 mutex_unlock(&bdev->bd_mutex);
0124                 bdput(bdevp);
0125                 disk_put_part(part);
0126                 return -EINVAL;
0127             }
0128             /* overlap? */
0129             disk_part_iter_init(&piter, disk,
0130                         DISK_PITER_INCL_EMPTY);
0131             while ((lpart = disk_part_iter_next(&piter))) {
0132                 if (lpart->partno != partno &&
0133                    !(start + length <= lpart->start_sect ||
0134                    start >= lpart->start_sect + lpart->nr_sects)
0135                    ) {
0136                     disk_part_iter_exit(&piter);
0137                     mutex_unlock(&bdevp->bd_mutex);
0138                     mutex_unlock(&bdev->bd_mutex);
0139                     bdput(bdevp);
0140                     disk_put_part(part);
0141                     return -EBUSY;
0142                 }
0143             }
0144             disk_part_iter_exit(&piter);
0145             part_nr_sects_write(part, (sector_t)length);
0146             i_size_write(bdevp->bd_inode, p.length);
0147             mutex_unlock(&bdevp->bd_mutex);
0148             mutex_unlock(&bdev->bd_mutex);
0149             bdput(bdevp);
0150             disk_put_part(part);
0151             return 0;
0152         default:
0153             return -EINVAL;
0154     }
0155 }
0156 
0157 /*
0158  * This is an exported API for the block driver, and will not
0159  * acquire bd_mutex. This API should be used in case that
0160  * caller has held bd_mutex already.
0161  */
0162 int __blkdev_reread_part(struct block_device *bdev)
0163 {
0164     struct gendisk *disk = bdev->bd_disk;
0165 
0166     if (!disk_part_scan_enabled(disk) || bdev != bdev->bd_contains)
0167         return -EINVAL;
0168     if (!capable(CAP_SYS_ADMIN))
0169         return -EACCES;
0170 
0171     lockdep_assert_held(&bdev->bd_mutex);
0172 
0173     return rescan_partitions(disk, bdev);
0174 }
0175 EXPORT_SYMBOL(__blkdev_reread_part);
0176 
0177 /*
0178  * This is an exported API for the block driver, and will
0179  * try to acquire bd_mutex. If bd_mutex has been held already
0180  * in current context, please call __blkdev_reread_part().
0181  *
0182  * Make sure the held locks in current context aren't required
0183  * in open()/close() handler and I/O path for avoiding ABBA deadlock:
0184  * - bd_mutex is held before calling block driver's open/close
0185  *   handler
0186  * - reading partition table may submit I/O to the block device
0187  */
0188 int blkdev_reread_part(struct block_device *bdev)
0189 {
0190     int res;
0191 
0192     mutex_lock(&bdev->bd_mutex);
0193     res = __blkdev_reread_part(bdev);
0194     mutex_unlock(&bdev->bd_mutex);
0195 
0196     return res;
0197 }
0198 EXPORT_SYMBOL(blkdev_reread_part);
0199 
0200 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
0201         unsigned long arg, unsigned long flags)
0202 {
0203     uint64_t range[2];
0204     uint64_t start, len;
0205 
0206     if (!(mode & FMODE_WRITE))
0207         return -EBADF;
0208 
0209     if (copy_from_user(range, (void __user *)arg, sizeof(range)))
0210         return -EFAULT;
0211 
0212     start = range[0];
0213     len = range[1];
0214 
0215     if (start & 511)
0216         return -EINVAL;
0217     if (len & 511)
0218         return -EINVAL;
0219     start >>= 9;
0220     len >>= 9;
0221 
0222     if (start + len > (i_size_read(bdev->bd_inode) >> 9))
0223         return -EINVAL;
0224     return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
0225 }
0226 
0227 static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
0228         unsigned long arg)
0229 {
0230     uint64_t range[2];
0231     struct address_space *mapping;
0232     uint64_t start, end, len;
0233 
0234     if (!(mode & FMODE_WRITE))
0235         return -EBADF;
0236 
0237     if (copy_from_user(range, (void __user *)arg, sizeof(range)))
0238         return -EFAULT;
0239 
0240     start = range[0];
0241     len = range[1];
0242     end = start + len - 1;
0243 
0244     if (start & 511)
0245         return -EINVAL;
0246     if (len & 511)
0247         return -EINVAL;
0248     if (end >= (uint64_t)i_size_read(bdev->bd_inode))
0249         return -EINVAL;
0250     if (end < start)
0251         return -EINVAL;
0252 
0253     /* Invalidate the page cache, including dirty pages */
0254     mapping = bdev->bd_inode->i_mapping;
0255     truncate_inode_pages_range(mapping, start, end);
0256 
0257     return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
0258                     false);
0259 }
0260 
0261 static int put_ushort(unsigned long arg, unsigned short val)
0262 {
0263     return put_user(val, (unsigned short __user *)arg);
0264 }
0265 
0266 static int put_int(unsigned long arg, int val)
0267 {
0268     return put_user(val, (int __user *)arg);
0269 }
0270 
0271 static int put_uint(unsigned long arg, unsigned int val)
0272 {
0273     return put_user(val, (unsigned int __user *)arg);
0274 }
0275 
0276 static int put_long(unsigned long arg, long val)
0277 {
0278     return put_user(val, (long __user *)arg);
0279 }
0280 
0281 static int put_ulong(unsigned long arg, unsigned long val)
0282 {
0283     return put_user(val, (unsigned long __user *)arg);
0284 }
0285 
0286 static int put_u64(unsigned long arg, u64 val)
0287 {
0288     return put_user(val, (u64 __user *)arg);
0289 }
0290 
0291 int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
0292             unsigned cmd, unsigned long arg)
0293 {
0294     struct gendisk *disk = bdev->bd_disk;
0295 
0296     if (disk->fops->ioctl)
0297         return disk->fops->ioctl(bdev, mode, cmd, arg);
0298 
0299     return -ENOTTY;
0300 }
0301 /*
0302  * For the record: _GPL here is only because somebody decided to slap it
0303  * on the previous export.  Sheer idiocy, since it wasn't copyrightable
0304  * at all and could be open-coded without any exports by anybody who cares.
0305  */
0306 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
0307 
0308 static int blkdev_pr_register(struct block_device *bdev,
0309         struct pr_registration __user *arg)
0310 {
0311     const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
0312     struct pr_registration reg;
0313 
0314     if (!capable(CAP_SYS_ADMIN))
0315         return -EPERM;
0316     if (!ops || !ops->pr_register)
0317         return -EOPNOTSUPP;
0318     if (copy_from_user(&reg, arg, sizeof(reg)))
0319         return -EFAULT;
0320 
0321     if (reg.flags & ~PR_FL_IGNORE_KEY)
0322         return -EOPNOTSUPP;
0323     return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
0324 }
0325 
0326 static int blkdev_pr_reserve(struct block_device *bdev,
0327         struct pr_reservation __user *arg)
0328 {
0329     const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
0330     struct pr_reservation rsv;
0331 
0332     if (!capable(CAP_SYS_ADMIN))
0333         return -EPERM;
0334     if (!ops || !ops->pr_reserve)
0335         return -EOPNOTSUPP;
0336     if (copy_from_user(&rsv, arg, sizeof(rsv)))
0337         return -EFAULT;
0338 
0339     if (rsv.flags & ~PR_FL_IGNORE_KEY)
0340         return -EOPNOTSUPP;
0341     return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
0342 }
0343 
0344 static int blkdev_pr_release(struct block_device *bdev,
0345         struct pr_reservation __user *arg)
0346 {
0347     const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
0348     struct pr_reservation rsv;
0349 
0350     if (!capable(CAP_SYS_ADMIN))
0351         return -EPERM;
0352     if (!ops || !ops->pr_release)
0353         return -EOPNOTSUPP;
0354     if (copy_from_user(&rsv, arg, sizeof(rsv)))
0355         return -EFAULT;
0356 
0357     if (rsv.flags)
0358         return -EOPNOTSUPP;
0359     return ops->pr_release(bdev, rsv.key, rsv.type);
0360 }
0361 
0362 static int blkdev_pr_preempt(struct block_device *bdev,
0363         struct pr_preempt __user *arg, bool abort)
0364 {
0365     const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
0366     struct pr_preempt p;
0367 
0368     if (!capable(CAP_SYS_ADMIN))
0369         return -EPERM;
0370     if (!ops || !ops->pr_preempt)
0371         return -EOPNOTSUPP;
0372     if (copy_from_user(&p, arg, sizeof(p)))
0373         return -EFAULT;
0374 
0375     if (p.flags)
0376         return -EOPNOTSUPP;
0377     return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
0378 }
0379 
0380 static int blkdev_pr_clear(struct block_device *bdev,
0381         struct pr_clear __user *arg)
0382 {
0383     const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
0384     struct pr_clear c;
0385 
0386     if (!capable(CAP_SYS_ADMIN))
0387         return -EPERM;
0388     if (!ops || !ops->pr_clear)
0389         return -EOPNOTSUPP;
0390     if (copy_from_user(&c, arg, sizeof(c)))
0391         return -EFAULT;
0392 
0393     if (c.flags)
0394         return -EOPNOTSUPP;
0395     return ops->pr_clear(bdev, c.key);
0396 }
0397 
0398 /*
0399  * Is it an unrecognized ioctl? The correct returns are either
0400  * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
0401  * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
0402  * code before returning.
0403  *
0404  * Confused drivers sometimes return EINVAL, which is wrong. It
0405  * means "I understood the ioctl command, but the parameters to
0406  * it were wrong".
0407  *
0408  * We should aim to just fix the broken drivers, the EINVAL case
0409  * should go away.
0410  */
0411 static inline int is_unrecognized_ioctl(int ret)
0412 {
0413     return  ret == -EINVAL ||
0414         ret == -ENOTTY ||
0415         ret == -ENOIOCTLCMD;
0416 }
0417 
0418 static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
0419         unsigned cmd, unsigned long arg)
0420 {
0421     int ret;
0422 
0423     if (!capable(CAP_SYS_ADMIN))
0424         return -EACCES;
0425 
0426     ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
0427     if (!is_unrecognized_ioctl(ret))
0428         return ret;
0429 
0430     fsync_bdev(bdev);
0431     invalidate_bdev(bdev);
0432     return 0;
0433 }
0434 
0435 static int blkdev_roset(struct block_device *bdev, fmode_t mode,
0436         unsigned cmd, unsigned long arg)
0437 {
0438     int ret, n;
0439 
0440     ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
0441     if (!is_unrecognized_ioctl(ret))
0442         return ret;
0443     if (!capable(CAP_SYS_ADMIN))
0444         return -EACCES;
0445     if (get_user(n, (int __user *)arg))
0446         return -EFAULT;
0447     set_device_ro(bdev, n);
0448     return 0;
0449 }
0450 
0451 static int blkdev_getgeo(struct block_device *bdev,
0452         struct hd_geometry __user *argp)
0453 {
0454     struct gendisk *disk = bdev->bd_disk;
0455     struct hd_geometry geo;
0456     int ret;
0457 
0458     if (!argp)
0459         return -EINVAL;
0460     if (!disk->fops->getgeo)
0461         return -ENOTTY;
0462 
0463     /*
0464      * We need to set the startsect first, the driver may
0465      * want to override it.
0466      */
0467     memset(&geo, 0, sizeof(geo));
0468     geo.start = get_start_sect(bdev);
0469     ret = disk->fops->getgeo(bdev, &geo);
0470     if (ret)
0471         return ret;
0472     if (copy_to_user(argp, &geo, sizeof(geo)))
0473         return -EFAULT;
0474     return 0;
0475 }
0476 
0477 /* set the logical block size */
0478 static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
0479         int __user *argp)
0480 {
0481     int ret, n;
0482 
0483     if (!capable(CAP_SYS_ADMIN))
0484         return -EACCES;
0485     if (!argp)
0486         return -EINVAL;
0487     if (get_user(n, argp))
0488         return -EFAULT;
0489 
0490     if (!(mode & FMODE_EXCL)) {
0491         bdgrab(bdev);
0492         if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
0493             return -EBUSY;
0494     }
0495 
0496     ret = set_blocksize(bdev, n);
0497     if (!(mode & FMODE_EXCL))
0498         blkdev_put(bdev, mode | FMODE_EXCL);
0499     return ret;
0500 }
0501 
0502 /*
0503  * always keep this in sync with compat_blkdev_ioctl()
0504  */
0505 int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
0506             unsigned long arg)
0507 {
0508     struct backing_dev_info *bdi;
0509     void __user *argp = (void __user *)arg;
0510     loff_t size;
0511     unsigned int max_sectors;
0512 
0513     switch (cmd) {
0514     case BLKFLSBUF:
0515         return blkdev_flushbuf(bdev, mode, cmd, arg);
0516     case BLKROSET:
0517         return blkdev_roset(bdev, mode, cmd, arg);
0518     case BLKDISCARD:
0519         return blk_ioctl_discard(bdev, mode, arg, 0);
0520     case BLKSECDISCARD:
0521         return blk_ioctl_discard(bdev, mode, arg,
0522                 BLKDEV_DISCARD_SECURE);
0523     case BLKZEROOUT:
0524         return blk_ioctl_zeroout(bdev, mode, arg);
0525     case BLKREPORTZONE:
0526         return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
0527     case BLKRESETZONE:
0528         return blkdev_reset_zones_ioctl(bdev, mode, cmd, arg);
0529     case HDIO_GETGEO:
0530         return blkdev_getgeo(bdev, argp);
0531     case BLKRAGET:
0532     case BLKFRAGET:
0533         if (!arg)
0534             return -EINVAL;
0535         bdi = blk_get_backing_dev_info(bdev);
0536         return put_long(arg, (bdi->ra_pages * PAGE_SIZE) / 512);
0537     case BLKROGET:
0538         return put_int(arg, bdev_read_only(bdev) != 0);
0539     case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
0540         return put_int(arg, block_size(bdev));
0541     case BLKSSZGET: /* get block device logical block size */
0542         return put_int(arg, bdev_logical_block_size(bdev));
0543     case BLKPBSZGET: /* get block device physical block size */
0544         return put_uint(arg, bdev_physical_block_size(bdev));
0545     case BLKIOMIN:
0546         return put_uint(arg, bdev_io_min(bdev));
0547     case BLKIOOPT:
0548         return put_uint(arg, bdev_io_opt(bdev));
0549     case BLKALIGNOFF:
0550         return put_int(arg, bdev_alignment_offset(bdev));
0551     case BLKDISCARDZEROES:
0552         return put_uint(arg, bdev_discard_zeroes_data(bdev));
0553     case BLKSECTGET:
0554         max_sectors = min_t(unsigned int, USHRT_MAX,
0555                     queue_max_sectors(bdev_get_queue(bdev)));
0556         return put_ushort(arg, max_sectors);
0557     case BLKROTATIONAL:
0558         return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
0559     case BLKRASET:
0560     case BLKFRASET:
0561         if(!capable(CAP_SYS_ADMIN))
0562             return -EACCES;
0563         bdi = blk_get_backing_dev_info(bdev);
0564         bdi->ra_pages = (arg * 512) / PAGE_SIZE;
0565         return 0;
0566     case BLKBSZSET:
0567         return blkdev_bszset(bdev, mode, argp);
0568     case BLKPG:
0569         return blkpg_ioctl(bdev, argp);
0570     case BLKRRPART:
0571         return blkdev_reread_part(bdev);
0572     case BLKGETSIZE:
0573         size = i_size_read(bdev->bd_inode);
0574         if ((size >> 9) > ~0UL)
0575             return -EFBIG;
0576         return put_ulong(arg, size >> 9);
0577     case BLKGETSIZE64:
0578         return put_u64(arg, i_size_read(bdev->bd_inode));
0579     case BLKTRACESTART:
0580     case BLKTRACESTOP:
0581     case BLKTRACESETUP:
0582     case BLKTRACETEARDOWN:
0583         return blk_trace_ioctl(bdev, cmd, argp);
0584     case IOC_PR_REGISTER:
0585         return blkdev_pr_register(bdev, argp);
0586     case IOC_PR_RESERVE:
0587         return blkdev_pr_reserve(bdev, argp);
0588     case IOC_PR_RELEASE:
0589         return blkdev_pr_release(bdev, argp);
0590     case IOC_PR_PREEMPT:
0591         return blkdev_pr_preempt(bdev, argp, false);
0592     case IOC_PR_PREEMPT_ABORT:
0593         return blkdev_pr_preempt(bdev, argp, true);
0594     case IOC_PR_CLEAR:
0595         return blkdev_pr_clear(bdev, argp);
0596     default:
0597         return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
0598     }
0599 }
0600 EXPORT_SYMBOL_GPL(blkdev_ioctl);