1c59ede7bSRandy.Dunlap #include <linux/capability.h> 23a65dfe8SJens Axboe #include <linux/blkdev.h> 35a0e3ad6STejun Heo #include <linux/gfp.h> 43a65dfe8SJens Axboe #include <linux/blkpg.h> 5a885c8c4SChristoph Hellwig #include <linux/hdreg.h> 63a65dfe8SJens Axboe #include <linux/backing-dev.h> 73a65dfe8SJens Axboe #include <linux/buffer_head.h> 83a65dfe8SJens Axboe #include <linux/smp_lock.h> 92056a782SJens Axboe #include <linux/blktrace_api.h> 103a65dfe8SJens Axboe #include <asm/uaccess.h> 113a65dfe8SJens Axboe 123a65dfe8SJens Axboe static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user *arg) 133a65dfe8SJens Axboe { 143a65dfe8SJens Axboe struct block_device *bdevp; 153a65dfe8SJens Axboe struct gendisk *disk; 16e71bf0d0STejun Heo struct hd_struct *part; 173a65dfe8SJens Axboe struct blkpg_ioctl_arg a; 183a65dfe8SJens Axboe struct blkpg_partition p; 19e71bf0d0STejun Heo struct disk_part_iter piter; 203a65dfe8SJens Axboe long long start, length; 21cf771cb5STejun Heo int partno; 223a65dfe8SJens Axboe 233a65dfe8SJens Axboe if (!capable(CAP_SYS_ADMIN)) 243a65dfe8SJens Axboe return -EACCES; 253a65dfe8SJens Axboe if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg))) 263a65dfe8SJens Axboe return -EFAULT; 273a65dfe8SJens Axboe if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition))) 283a65dfe8SJens Axboe return -EFAULT; 293a65dfe8SJens Axboe disk = bdev->bd_disk; 303a65dfe8SJens Axboe if (bdev != bdev->bd_contains) 313a65dfe8SJens Axboe return -EINVAL; 32cf771cb5STejun Heo partno = p.pno; 33540eed56STejun Heo if (partno <= 0) 343a65dfe8SJens Axboe return -EINVAL; 353a65dfe8SJens Axboe switch (a.op) { 363a65dfe8SJens Axboe case BLKPG_ADD_PARTITION: 373a65dfe8SJens Axboe start = p.start >> 9; 383a65dfe8SJens Axboe length = p.length >> 9; 393a65dfe8SJens Axboe /* check for fit in a hd_struct */ 403a65dfe8SJens Axboe if (sizeof(sector_t) == sizeof(long) && 413a65dfe8SJens Axboe sizeof(long long) > sizeof(long)) { 423a65dfe8SJens Axboe long pstart = start, plength = length; 433a65dfe8SJens Axboe if (pstart != start || plength != length 443a65dfe8SJens Axboe || pstart < 0 || plength < 0) 453a65dfe8SJens Axboe return -EINVAL; 463a65dfe8SJens Axboe } 4788e34126STejun Heo 48c039e313SArjan van de Ven mutex_lock(&bdev->bd_mutex); 4988e34126STejun Heo 503a65dfe8SJens Axboe /* overlap? */ 51e71bf0d0STejun Heo disk_part_iter_init(&piter, disk, 52e71bf0d0STejun Heo DISK_PITER_INCL_EMPTY); 53e71bf0d0STejun Heo while ((part = disk_part_iter_next(&piter))) { 54e71bf0d0STejun Heo if (!(start + length <= part->start_sect || 55e71bf0d0STejun Heo start >= part->start_sect + part->nr_sects)) { 56e71bf0d0STejun Heo disk_part_iter_exit(&piter); 57c039e313SArjan van de Ven mutex_unlock(&bdev->bd_mutex); 583a65dfe8SJens Axboe return -EBUSY; 593a65dfe8SJens Axboe } 603a65dfe8SJens Axboe } 61e71bf0d0STejun Heo disk_part_iter_exit(&piter); 62e71bf0d0STejun Heo 633a65dfe8SJens Axboe /* all seems OK */ 64ba32929aSTejun Heo part = add_partition(disk, partno, start, length, 65cf771cb5STejun Heo ADDPART_FLAG_NONE); 66c039e313SArjan van de Ven mutex_unlock(&bdev->bd_mutex); 67ba32929aSTejun Heo return IS_ERR(part) ? PTR_ERR(part) : 0; 683a65dfe8SJens Axboe case BLKPG_DEL_PARTITION: 69e71bf0d0STejun Heo part = disk_get_part(disk, partno); 70e71bf0d0STejun Heo if (!part) 713a65dfe8SJens Axboe return -ENXIO; 72e71bf0d0STejun Heo 73e71bf0d0STejun Heo bdevp = bdget(part_devt(part)); 74e71bf0d0STejun Heo disk_put_part(part); 753a65dfe8SJens Axboe if (!bdevp) 763a65dfe8SJens Axboe return -ENOMEM; 77e71bf0d0STejun Heo 782e7b651dSPeter Zijlstra mutex_lock(&bdevp->bd_mutex); 793a65dfe8SJens Axboe if (bdevp->bd_openers) { 80c039e313SArjan van de Ven mutex_unlock(&bdevp->bd_mutex); 813a65dfe8SJens Axboe bdput(bdevp); 823a65dfe8SJens Axboe return -EBUSY; 833a65dfe8SJens Axboe } 843a65dfe8SJens Axboe /* all seems OK */ 853a65dfe8SJens Axboe fsync_bdev(bdevp); 86f98393a6SPeter Zijlstra invalidate_bdev(bdevp); 873a65dfe8SJens Axboe 886d740cd5SPeter Zijlstra mutex_lock_nested(&bdev->bd_mutex, 1); 89cf771cb5STejun Heo delete_partition(disk, partno); 90c039e313SArjan van de Ven mutex_unlock(&bdev->bd_mutex); 91c039e313SArjan van de Ven mutex_unlock(&bdevp->bd_mutex); 923a65dfe8SJens Axboe bdput(bdevp); 933a65dfe8SJens Axboe 943a65dfe8SJens Axboe return 0; 953a65dfe8SJens Axboe default: 963a65dfe8SJens Axboe return -EINVAL; 973a65dfe8SJens Axboe } 983a65dfe8SJens Axboe } 993a65dfe8SJens Axboe 1003a65dfe8SJens Axboe static int blkdev_reread_part(struct block_device *bdev) 1013a65dfe8SJens Axboe { 1023a65dfe8SJens Axboe struct gendisk *disk = bdev->bd_disk; 1033a65dfe8SJens Axboe int res; 1043a65dfe8SJens Axboe 105b5d0b9dfSTejun Heo if (!disk_partitionable(disk) || bdev != bdev->bd_contains) 1063a65dfe8SJens Axboe return -EINVAL; 1073a65dfe8SJens Axboe if (!capable(CAP_SYS_ADMIN)) 1083a65dfe8SJens Axboe return -EACCES; 109c039e313SArjan van de Ven if (!mutex_trylock(&bdev->bd_mutex)) 1103a65dfe8SJens Axboe return -EBUSY; 1113a65dfe8SJens Axboe res = rescan_partitions(disk, bdev); 112c039e313SArjan van de Ven mutex_unlock(&bdev->bd_mutex); 1133a65dfe8SJens Axboe return res; 1143a65dfe8SJens Axboe } 1153a65dfe8SJens Axboe 116d30a2605SDavid Woodhouse static int blk_ioctl_discard(struct block_device *bdev, uint64_t start, 117*8d57a98cSAdrian Hunter uint64_t len, int secure) 118d30a2605SDavid Woodhouse { 119*8d57a98cSAdrian Hunter unsigned long flags = BLKDEV_IFL_WAIT; 120*8d57a98cSAdrian Hunter 121d30a2605SDavid Woodhouse if (start & 511) 122d30a2605SDavid Woodhouse return -EINVAL; 123d30a2605SDavid Woodhouse if (len & 511) 124d30a2605SDavid Woodhouse return -EINVAL; 125d30a2605SDavid Woodhouse start >>= 9; 126d30a2605SDavid Woodhouse len >>= 9; 127d30a2605SDavid Woodhouse 128d30a2605SDavid Woodhouse if (start + len > (bdev->bd_inode->i_size >> 9)) 129d30a2605SDavid Woodhouse return -EINVAL; 130*8d57a98cSAdrian Hunter if (secure) 131*8d57a98cSAdrian Hunter flags |= BLKDEV_IFL_SECURE; 132*8d57a98cSAdrian Hunter return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags); 133d30a2605SDavid Woodhouse } 134d30a2605SDavid Woodhouse 1353a65dfe8SJens Axboe static int put_ushort(unsigned long arg, unsigned short val) 1363a65dfe8SJens Axboe { 1373a65dfe8SJens Axboe return put_user(val, (unsigned short __user *)arg); 1383a65dfe8SJens Axboe } 1393a65dfe8SJens Axboe 1403a65dfe8SJens Axboe static int put_int(unsigned long arg, int val) 1413a65dfe8SJens Axboe { 1423a65dfe8SJens Axboe return put_user(val, (int __user *)arg); 1433a65dfe8SJens Axboe } 1443a65dfe8SJens Axboe 145ac481c20SMartin K. Petersen static int put_uint(unsigned long arg, unsigned int val) 146ac481c20SMartin K. Petersen { 147ac481c20SMartin K. Petersen return put_user(val, (unsigned int __user *)arg); 148ac481c20SMartin K. Petersen } 149ac481c20SMartin K. Petersen 1503a65dfe8SJens Axboe static int put_long(unsigned long arg, long val) 1513a65dfe8SJens Axboe { 1523a65dfe8SJens Axboe return put_user(val, (long __user *)arg); 1533a65dfe8SJens Axboe } 1543a65dfe8SJens Axboe 1553a65dfe8SJens Axboe static int put_ulong(unsigned long arg, unsigned long val) 1563a65dfe8SJens Axboe { 1573a65dfe8SJens Axboe return put_user(val, (unsigned long __user *)arg); 1583a65dfe8SJens Axboe } 1593a65dfe8SJens Axboe 1603a65dfe8SJens Axboe static int put_u64(unsigned long arg, u64 val) 1613a65dfe8SJens Axboe { 1623a65dfe8SJens Axboe return put_user(val, (u64 __user *)arg); 1633a65dfe8SJens Axboe } 1643a65dfe8SJens Axboe 165633a08b8SAl Viro int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode, 166633a08b8SAl Viro unsigned cmd, unsigned long arg) 167633a08b8SAl Viro { 168633a08b8SAl Viro struct gendisk *disk = bdev->bd_disk; 169d4430d62SAl Viro 170d4430d62SAl Viro if (disk->fops->ioctl) 171d4430d62SAl Viro return disk->fops->ioctl(bdev, mode, cmd, arg); 172d4430d62SAl Viro 173633a08b8SAl Viro return -ENOTTY; 174633a08b8SAl Viro } 175633a08b8SAl Viro /* 176633a08b8SAl Viro * For the record: _GPL here is only because somebody decided to slap it 177633a08b8SAl Viro * on the previous export. Sheer idiocy, since it wasn't copyrightable 178633a08b8SAl Viro * at all and could be open-coded without any exports by anybody who cares. 179633a08b8SAl Viro */ 180633a08b8SAl Viro EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl); 181633a08b8SAl Viro 182f58c4c0aSArnd Bergmann /* 1838a6cfeb6SArnd Bergmann * always keep this in sync with compat_blkdev_ioctl() 184f58c4c0aSArnd Bergmann */ 18556b26addSAl Viro int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, 1863a65dfe8SJens Axboe unsigned long arg) 1873a65dfe8SJens Axboe { 1883a65dfe8SJens Axboe struct gendisk *disk = bdev->bd_disk; 18945048d09SAl Viro struct backing_dev_info *bdi; 19045048d09SAl Viro loff_t size; 1913a65dfe8SJens Axboe int ret, n; 1923a65dfe8SJens Axboe 1933a65dfe8SJens Axboe switch(cmd) { 1943a65dfe8SJens Axboe case BLKFLSBUF: 1953a65dfe8SJens Axboe if (!capable(CAP_SYS_ADMIN)) 1963a65dfe8SJens Axboe return -EACCES; 1973a65dfe8SJens Axboe 198e436fdaeSAl Viro ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); 1993a65dfe8SJens Axboe /* -EINVAL to handle old uncorrected drivers */ 2003a65dfe8SJens Axboe if (ret != -EINVAL && ret != -ENOTTY) 2013a65dfe8SJens Axboe return ret; 2023a65dfe8SJens Axboe 2033a65dfe8SJens Axboe fsync_bdev(bdev); 204f98393a6SPeter Zijlstra invalidate_bdev(bdev); 2053a65dfe8SJens Axboe return 0; 2063a65dfe8SJens Axboe 2073a65dfe8SJens Axboe case BLKROSET: 208e436fdaeSAl Viro ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); 2093a65dfe8SJens Axboe /* -EINVAL to handle old uncorrected drivers */ 2103a65dfe8SJens Axboe if (ret != -EINVAL && ret != -ENOTTY) 2113a65dfe8SJens Axboe return ret; 2123a65dfe8SJens Axboe if (!capable(CAP_SYS_ADMIN)) 2133a65dfe8SJens Axboe return -EACCES; 2143a65dfe8SJens Axboe if (get_user(n, (int __user *)(arg))) 2153a65dfe8SJens Axboe return -EFAULT; 2163a65dfe8SJens Axboe set_device_ro(bdev, n); 2173a65dfe8SJens Axboe return 0; 218d30a2605SDavid Woodhouse 219*8d57a98cSAdrian Hunter case BLKDISCARD: 220*8d57a98cSAdrian Hunter case BLKSECDISCARD: { 221d30a2605SDavid Woodhouse uint64_t range[2]; 222d30a2605SDavid Woodhouse 223e436fdaeSAl Viro if (!(mode & FMODE_WRITE)) 224d30a2605SDavid Woodhouse return -EBADF; 225d30a2605SDavid Woodhouse 226d30a2605SDavid Woodhouse if (copy_from_user(range, (void __user *)arg, sizeof(range))) 227d30a2605SDavid Woodhouse return -EFAULT; 228d30a2605SDavid Woodhouse 229*8d57a98cSAdrian Hunter return blk_ioctl_discard(bdev, range[0], range[1], 230*8d57a98cSAdrian Hunter cmd == BLKSECDISCARD); 231d30a2605SDavid Woodhouse } 232d30a2605SDavid Woodhouse 233a885c8c4SChristoph Hellwig case HDIO_GETGEO: { 234a885c8c4SChristoph Hellwig struct hd_geometry geo; 235a885c8c4SChristoph Hellwig 236a885c8c4SChristoph Hellwig if (!arg) 237a885c8c4SChristoph Hellwig return -EINVAL; 238a885c8c4SChristoph Hellwig if (!disk->fops->getgeo) 239a885c8c4SChristoph Hellwig return -ENOTTY; 240a885c8c4SChristoph Hellwig 241a885c8c4SChristoph Hellwig /* 242a885c8c4SChristoph Hellwig * We need to set the startsect first, the driver may 243a885c8c4SChristoph Hellwig * want to override it. 244a885c8c4SChristoph Hellwig */ 245a885c8c4SChristoph Hellwig geo.start = get_start_sect(bdev); 246a885c8c4SChristoph Hellwig ret = disk->fops->getgeo(bdev, &geo); 247a885c8c4SChristoph Hellwig if (ret) 248a885c8c4SChristoph Hellwig return ret; 249a885c8c4SChristoph Hellwig if (copy_to_user((struct hd_geometry __user *)arg, &geo, 250a885c8c4SChristoph Hellwig sizeof(geo))) 251a885c8c4SChristoph Hellwig return -EFAULT; 252a885c8c4SChristoph Hellwig return 0; 253a885c8c4SChristoph Hellwig } 25445048d09SAl Viro case BLKRAGET: 25545048d09SAl Viro case BLKFRAGET: 25645048d09SAl Viro if (!arg) 25745048d09SAl Viro return -EINVAL; 25845048d09SAl Viro bdi = blk_get_backing_dev_info(bdev); 25945048d09SAl Viro if (bdi == NULL) 26045048d09SAl Viro return -ENOTTY; 26145048d09SAl Viro return put_long(arg, (bdi->ra_pages * PAGE_CACHE_SIZE) / 512); 26245048d09SAl Viro case BLKROGET: 26345048d09SAl Viro return put_int(arg, bdev_read_only(bdev) != 0); 264ac481c20SMartin K. Petersen case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */ 26545048d09SAl Viro return put_int(arg, block_size(bdev)); 266ac481c20SMartin K. Petersen case BLKSSZGET: /* get block device logical block size */ 267e1defc4fSMartin K. Petersen return put_int(arg, bdev_logical_block_size(bdev)); 268ac481c20SMartin K. Petersen case BLKPBSZGET: /* get block device physical block size */ 269ac481c20SMartin K. Petersen return put_uint(arg, bdev_physical_block_size(bdev)); 270ac481c20SMartin K. Petersen case BLKIOMIN: 271ac481c20SMartin K. Petersen return put_uint(arg, bdev_io_min(bdev)); 272ac481c20SMartin K. Petersen case BLKIOOPT: 273ac481c20SMartin K. Petersen return put_uint(arg, bdev_io_opt(bdev)); 274ac481c20SMartin K. Petersen case BLKALIGNOFF: 275ac481c20SMartin K. Petersen return put_int(arg, bdev_alignment_offset(bdev)); 27698262f27SMartin K. Petersen case BLKDISCARDZEROES: 27798262f27SMartin K. Petersen return put_uint(arg, bdev_discard_zeroes_data(bdev)); 27845048d09SAl Viro case BLKSECTGET: 279ae03bf63SMartin K. Petersen return put_ushort(arg, queue_max_sectors(bdev_get_queue(bdev))); 28045048d09SAl Viro case BLKRASET: 28145048d09SAl Viro case BLKFRASET: 28245048d09SAl Viro if(!capable(CAP_SYS_ADMIN)) 28345048d09SAl Viro return -EACCES; 28445048d09SAl Viro bdi = blk_get_backing_dev_info(bdev); 28545048d09SAl Viro if (bdi == NULL) 28645048d09SAl Viro return -ENOTTY; 28745048d09SAl Viro bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE; 28845048d09SAl Viro return 0; 28945048d09SAl Viro case BLKBSZSET: 29045048d09SAl Viro /* set the logical block size */ 29145048d09SAl Viro if (!capable(CAP_SYS_ADMIN)) 29245048d09SAl Viro return -EACCES; 29345048d09SAl Viro if (!arg) 29445048d09SAl Viro return -EINVAL; 29545048d09SAl Viro if (get_user(n, (int __user *) arg)) 29645048d09SAl Viro return -EFAULT; 2976af3a56eSAl Viro if (!(mode & FMODE_EXCL) && bd_claim(bdev, &bdev) < 0) 29845048d09SAl Viro return -EBUSY; 29945048d09SAl Viro ret = set_blocksize(bdev, n); 3006af3a56eSAl Viro if (!(mode & FMODE_EXCL)) 30145048d09SAl Viro bd_release(bdev); 3023a65dfe8SJens Axboe return ret; 30345048d09SAl Viro case BLKPG: 30445048d09SAl Viro ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg); 30545048d09SAl Viro break; 30645048d09SAl Viro case BLKRRPART: 30745048d09SAl Viro ret = blkdev_reread_part(bdev); 30845048d09SAl Viro break; 30945048d09SAl Viro case BLKGETSIZE: 31045048d09SAl Viro size = bdev->bd_inode->i_size; 31145048d09SAl Viro if ((size >> 9) > ~0UL) 31245048d09SAl Viro return -EFBIG; 31345048d09SAl Viro return put_ulong(arg, size >> 9); 31445048d09SAl Viro case BLKGETSIZE64: 31545048d09SAl Viro return put_u64(arg, bdev->bd_inode->i_size); 31645048d09SAl Viro case BLKTRACESTART: 31745048d09SAl Viro case BLKTRACESTOP: 31845048d09SAl Viro case BLKTRACESETUP: 31945048d09SAl Viro case BLKTRACETEARDOWN: 32045048d09SAl Viro ret = blk_trace_ioctl(bdev, cmd, (char __user *) arg); 32145048d09SAl Viro break; 32245048d09SAl Viro default: 323e436fdaeSAl Viro ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg); 3243a65dfe8SJens Axboe } 32545048d09SAl Viro return ret; 32645048d09SAl Viro } 3273a65dfe8SJens Axboe EXPORT_SYMBOL_GPL(blkdev_ioctl); 328