block: don't depend on consecutive minor space
[safe/jmp/linux-2.6] / block / ioctl.c
index f6962b6..403f7d7 100644 (file)
@@ -15,8 +15,9 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
        struct blkpg_ioctl_arg a;
        struct blkpg_partition p;
        long long start, length;
-       int part;
+       int partno;
        int i;
+       int err;
 
        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
@@ -27,8 +28,8 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
        disk = bdev->bd_disk;
        if (bdev != bdev->bd_contains)
                return -EINVAL;
-       part = p.pno;
-       if (part <= 0 || part >= disk->minors)
+       partno = p.pno;
+       if (partno <= 0 || partno > disk_max_parts(disk))
                return -EINVAL;
        switch (a.op) {
                case BLKPG_ADD_PARTITION:
@@ -42,14 +43,11 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
                                    || pstart < 0 || plength < 0)
                                        return -EINVAL;
                        }
-                       /* partition number in use? */
+
                        mutex_lock(&bdev->bd_mutex);
-                       if (disk->part[part - 1]) {
-                               mutex_unlock(&bdev->bd_mutex);
-                               return -EBUSY;
-                       }
+
                        /* overlap? */
-                       for (i = 0; i < disk->minors - 1; i++) {
+                       for (i = 0; i < disk_max_parts(disk); i++) {
                                struct hd_struct *s = disk->part[i];
 
                                if (!s)
@@ -61,15 +59,14 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
                                }
                        }
                        /* all seems OK */
-                       add_partition(disk, part, start, length);
+                       err = add_partition(disk, partno, start, length,
+                                           ADDPART_FLAG_NONE);
                        mutex_unlock(&bdev->bd_mutex);
-                       return 0;
+                       return err;
                case BLKPG_DEL_PARTITION:
-                       if (!disk->part[part-1])
-                               return -ENXIO;
-                       if (disk->part[part - 1]->nr_sects == 0)
+                       if (!disk->part[partno - 1])
                                return -ENXIO;
-                       bdevp = bdget_disk(disk, part);
+                       bdevp = bdget_disk(disk, partno);
                        if (!bdevp)
                                return -ENOMEM;
                        mutex_lock(&bdevp->bd_mutex);
@@ -80,10 +77,10 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
                        }
                        /* all seems OK */
                        fsync_bdev(bdevp);
-                       invalidate_bdev(bdevp, 0);
+                       invalidate_bdev(bdevp);
 
-                       mutex_lock(&bdev->bd_mutex);
-                       delete_partition(disk, part);
+                       mutex_lock_nested(&bdev->bd_mutex, 1);
+                       delete_partition(disk, partno);
                        mutex_unlock(&bdev->bd_mutex);
                        mutex_unlock(&bdevp->bd_mutex);
                        bdput(bdevp);
@@ -99,7 +96,7 @@ static int blkdev_reread_part(struct block_device *bdev)
        struct gendisk *disk = bdev->bd_disk;
        int res;
 
-       if (disk->minors == 1 || bdev != bdev->bd_contains)
+       if (!disk_max_parts(disk) || bdev != bdev->bd_contains)
                return -EINVAL;
        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
@@ -110,6 +107,69 @@ static int blkdev_reread_part(struct block_device *bdev)
        return res;
 }
 
+static void blk_ioc_discard_endio(struct bio *bio, int err)
+{
+       if (err) {
+               if (err == -EOPNOTSUPP)
+                       set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
+               clear_bit(BIO_UPTODATE, &bio->bi_flags);
+       }
+       complete(bio->bi_private);
+}
+
+static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
+                            uint64_t len)
+{
+       struct request_queue *q = bdev_get_queue(bdev);
+       int ret = 0;
+
+       if (start & 511)
+               return -EINVAL;
+       if (len & 511)
+               return -EINVAL;
+       start >>= 9;
+       len >>= 9;
+
+       if (start + len > (bdev->bd_inode->i_size >> 9))
+               return -EINVAL;
+
+       if (!q->prepare_discard_fn)
+               return -EOPNOTSUPP;
+
+       while (len && !ret) {
+               DECLARE_COMPLETION_ONSTACK(wait);
+               struct bio *bio;
+
+               bio = bio_alloc(GFP_KERNEL, 0);
+               if (!bio)
+                       return -ENOMEM;
+
+               bio->bi_end_io = blk_ioc_discard_endio;
+               bio->bi_bdev = bdev;
+               bio->bi_private = &wait;
+               bio->bi_sector = start;
+
+               if (len > q->max_hw_sectors) {
+                       bio->bi_size = q->max_hw_sectors << 9;
+                       len -= q->max_hw_sectors;
+                       start += q->max_hw_sectors;
+               } else {
+                       bio->bi_size = len << 9;
+                       len = 0;
+               }
+               submit_bio(DISCARD_NOBARRIER, bio);
+
+               wait_for_completion(&wait);
+
+               if (bio_flagged(bio, BIO_EOPNOTSUPP))
+                       ret = -EOPNOTSUPP;
+               else if (!bio_flagged(bio, BIO_UPTODATE))
+                       ret = -EIO;
+               bio_put(bio);
+       }
+       return ret;
+}
+
 static int put_ushort(unsigned long arg, unsigned short val)
 {
        return put_user(val, (unsigned short __user *)arg);
@@ -217,6 +277,10 @@ int blkdev_driver_ioctl(struct inode *inode, struct file *file,
 }
 EXPORT_SYMBOL_GPL(blkdev_driver_ioctl);
 
+/*
+ * always keep this in sync with compat_blkdev_ioctl() and
+ * compat_blkdev_locked_ioctl()
+ */
 int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
                        unsigned long arg)
 {
@@ -236,7 +300,7 @@ int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
 
                lock_kernel();
                fsync_bdev(bdev);
-               invalidate_bdev(bdev, 0);
+               invalidate_bdev(bdev);
                unlock_kernel();
                return 0;
 
@@ -253,6 +317,19 @@ int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
                set_device_ro(bdev, n);
                unlock_kernel();
                return 0;
+
+       case BLKDISCARD: {
+               uint64_t range[2];
+
+               if (!(file->f_mode & FMODE_WRITE))
+                       return -EBADF;
+
+               if (copy_from_user(range, (void __user *)arg, sizeof(range)))
+                       return -EFAULT;
+
+               return blk_ioctl_discard(bdev, range[0], range[1]);
+       }
+
        case HDIO_GETGEO: {
                struct hd_geometry geo;
 
@@ -284,21 +361,4 @@ int blkdev_ioctl(struct inode *inode, struct file *file, unsigned cmd,
 
        return blkdev_driver_ioctl(inode, file, disk, cmd, arg);
 }
-
-/* Most of the generic ioctls are handled in the normal fallback path.
-   This assumes the blkdev's low level compat_ioctl always returns
-   ENOIOCTLCMD for unknown ioctls. */
-long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
-{
-       struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev;
-       struct gendisk *disk = bdev->bd_disk;
-       int ret = -ENOIOCTLCMD;
-       if (disk->fops->compat_ioctl) {
-               lock_kernel();
-               ret = disk->fops->compat_ioctl(file, cmd, arg);
-               unlock_kernel();
-       }
-       return ret;
-}
-
 EXPORT_SYMBOL_GPL(blkdev_ioctl);