1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/capability.h> 3 #include <linux/compat.h> 4 #include <linux/blkdev.h> 5 #include <linux/export.h> 6 #include <linux/gfp.h> 7 #include <linux/blkpg.h> 8 #include <linux/hdreg.h> 9 #include <linux/backing-dev.h> 10 #include <linux/fs.h> 11 #include <linux/blktrace_api.h> 12 #include <linux/pr.h> 13 #include <linux/uaccess.h> 14 #include "blk.h" 15 16 static int blkpg_do_ioctl(struct block_device *bdev, 17 struct blkpg_partition __user *upart, int op) 18 { 19 struct gendisk *disk = bdev->bd_disk; 20 struct blkpg_partition p; 21 sector_t start, length; 22 23 if (disk->flags & GENHD_FL_NO_PART) 24 return -EINVAL; 25 if (!capable(CAP_SYS_ADMIN)) 26 return -EACCES; 27 if (copy_from_user(&p, upart, sizeof(struct blkpg_partition))) 28 return -EFAULT; 29 if (bdev_is_partition(bdev)) 30 return -EINVAL; 31 32 if (p.pno <= 0) 33 return -EINVAL; 34 35 if (op == BLKPG_DEL_PARTITION) 36 return bdev_del_partition(disk, p.pno); 37 38 if (p.start < 0 || p.length <= 0 || p.start + p.length < 0) 39 return -EINVAL; 40 /* Check that the partition is aligned to the block size */ 41 if (!IS_ALIGNED(p.start | p.length, bdev_logical_block_size(bdev))) 42 return -EINVAL; 43 44 start = p.start >> SECTOR_SHIFT; 45 length = p.length >> SECTOR_SHIFT; 46 47 switch (op) { 48 case BLKPG_ADD_PARTITION: 49 return bdev_add_partition(disk, p.pno, start, length); 50 case BLKPG_RESIZE_PARTITION: 51 return bdev_resize_partition(disk, p.pno, start, length); 52 default: 53 return -EINVAL; 54 } 55 } 56 57 static int blkpg_ioctl(struct block_device *bdev, 58 struct blkpg_ioctl_arg __user *arg) 59 { 60 struct blkpg_partition __user *udata; 61 int op; 62 63 if (get_user(op, &arg->op) || get_user(udata, &arg->data)) 64 return -EFAULT; 65 66 return blkpg_do_ioctl(bdev, udata, op); 67 } 68 69 #ifdef CONFIG_COMPAT 70 struct compat_blkpg_ioctl_arg { 71 compat_int_t op; 72 compat_int_t flags; 73 compat_int_t datalen; 74 compat_caddr_t data; 75 }; 76 77 static int compat_blkpg_ioctl(struct block_device *bdev, 78 struct compat_blkpg_ioctl_arg __user *arg) 79 { 80 compat_caddr_t udata; 81 int op; 82 83 if (get_user(op, &arg->op) || get_user(udata, &arg->data)) 84 return -EFAULT; 85 86 return blkpg_do_ioctl(bdev, compat_ptr(udata), op); 87 } 88 #endif 89 90 static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, 91 unsigned long arg) 92 { 93 uint64_t range[2]; 94 uint64_t start, len; 95 struct inode *inode = bdev->bd_inode; 96 int err; 97 98 if (!(mode & BLK_OPEN_WRITE)) 99 return -EBADF; 100 101 if (!bdev_max_discard_sectors(bdev)) 102 return -EOPNOTSUPP; 103 104 if (copy_from_user(range, (void __user *)arg, sizeof(range))) 105 return -EFAULT; 106 107 start = range[0]; 108 len = range[1]; 109 110 if (start & 511) 111 return -EINVAL; 112 if (len & 511) 113 return -EINVAL; 114 115 if (start + len > bdev_nr_bytes(bdev)) 116 return -EINVAL; 117 118 filemap_invalidate_lock(inode->i_mapping); 119 err = truncate_bdev_range(bdev, mode, start, start + len - 1); 120 if (err) 121 goto fail; 122 err = blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); 123 fail: 124 filemap_invalidate_unlock(inode->i_mapping); 125 return err; 126 } 127 128 static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode, 129 void __user *argp) 130 { 131 uint64_t start, len; 132 uint64_t range[2]; 133 int err; 134 135 if (!(mode & BLK_OPEN_WRITE)) 136 return -EBADF; 137 if (!bdev_max_secure_erase_sectors(bdev)) 138 return -EOPNOTSUPP; 139 if (copy_from_user(range, argp, sizeof(range))) 140 return -EFAULT; 141 142 start = range[0]; 143 len = range[1]; 144 if ((start & 511) || (len & 511)) 145 return -EINVAL; 146 if (start + len > bdev_nr_bytes(bdev)) 147 return -EINVAL; 148 149 filemap_invalidate_lock(bdev->bd_inode->i_mapping); 150 err = truncate_bdev_range(bdev, mode, start, start + len - 1); 151 if (!err) 152 err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9, 153 GFP_KERNEL); 154 filemap_invalidate_unlock(bdev->bd_inode->i_mapping); 155 return err; 156 } 157 158 159 static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode, 160 unsigned long arg) 161 { 162 uint64_t range[2]; 163 uint64_t start, end, len; 164 struct inode *inode = bdev->bd_inode; 165 int err; 166 167 if (!(mode & BLK_OPEN_WRITE)) 168 return -EBADF; 169 170 if (copy_from_user(range, (void __user *)arg, sizeof(range))) 171 return -EFAULT; 172 173 start = range[0]; 174 len = range[1]; 175 end = start + len - 1; 176 177 if (start & 511) 178 return -EINVAL; 179 if (len & 511) 180 return -EINVAL; 181 if (end >= (uint64_t)bdev_nr_bytes(bdev)) 182 return -EINVAL; 183 if (end < start) 184 return -EINVAL; 185 186 /* Invalidate the page cache, including dirty pages */ 187 filemap_invalidate_lock(inode->i_mapping); 188 err = truncate_bdev_range(bdev, mode, start, end); 189 if (err) 190 goto fail; 191 192 err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL, 193 BLKDEV_ZERO_NOUNMAP); 194 195 fail: 196 filemap_invalidate_unlock(inode->i_mapping); 197 return err; 198 } 199 200 static int put_ushort(unsigned short __user *argp, unsigned short val) 201 { 202 return put_user(val, argp); 203 } 204 205 static int put_int(int __user *argp, int val) 206 { 207 return put_user(val, argp); 208 } 209 210 static int put_uint(unsigned int __user *argp, unsigned int val) 211 { 212 return put_user(val, argp); 213 } 214 215 static int put_long(long __user *argp, long val) 216 { 217 return put_user(val, argp); 218 } 219 220 static int put_ulong(unsigned long __user *argp, unsigned long val) 221 { 222 return put_user(val, argp); 223 } 224 225 static int put_u64(u64 __user *argp, u64 val) 226 { 227 return put_user(val, argp); 228 } 229 230 #ifdef CONFIG_COMPAT 231 static int compat_put_long(compat_long_t __user *argp, long val) 232 { 233 return put_user(val, argp); 234 } 235 236 static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val) 237 { 238 return put_user(val, argp); 239 } 240 #endif 241 242 #ifdef CONFIG_COMPAT 243 /* 244 * This is the equivalent of compat_ptr_ioctl(), to be used by block 245 * drivers that implement only commands that are completely compatible 246 * between 32-bit and 64-bit user space 247 */ 248 int blkdev_compat_ptr_ioctl(struct block_device *bdev, blk_mode_t mode, 249 unsigned cmd, unsigned long arg) 250 { 251 struct gendisk *disk = bdev->bd_disk; 252 253 if (disk->fops->ioctl) 254 return disk->fops->ioctl(bdev, mode, cmd, 255 (unsigned long)compat_ptr(arg)); 256 257 return -ENOIOCTLCMD; 258 } 259 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl); 260 #endif 261 262 static bool blkdev_pr_allowed(struct block_device *bdev, blk_mode_t mode) 263 { 264 /* no sense to make reservations for partitions */ 265 if (bdev_is_partition(bdev)) 266 return false; 267 268 if (capable(CAP_SYS_ADMIN)) 269 return true; 270 /* 271 * Only allow unprivileged reservations if the file descriptor is open 272 * for writing. 273 */ 274 return mode & BLK_OPEN_WRITE; 275 } 276 277 static int blkdev_pr_register(struct block_device *bdev, blk_mode_t mode, 278 struct pr_registration __user *arg) 279 { 280 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; 281 struct pr_registration reg; 282 283 if (!blkdev_pr_allowed(bdev, mode)) 284 return -EPERM; 285 if (!ops || !ops->pr_register) 286 return -EOPNOTSUPP; 287 if (copy_from_user(®, arg, sizeof(reg))) 288 return -EFAULT; 289 290 if (reg.flags & ~PR_FL_IGNORE_KEY) 291 return -EOPNOTSUPP; 292 return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags); 293 } 294 295 static int blkdev_pr_reserve(struct block_device *bdev, blk_mode_t mode, 296 struct pr_reservation __user *arg) 297 { 298 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; 299 struct pr_reservation rsv; 300 301 if (!blkdev_pr_allowed(bdev, mode)) 302 return -EPERM; 303 if (!ops || !ops->pr_reserve) 304 return -EOPNOTSUPP; 305 if (copy_from_user(&rsv, arg, sizeof(rsv))) 306 return -EFAULT; 307 308 if (rsv.flags & ~PR_FL_IGNORE_KEY) 309 return -EOPNOTSUPP; 310 return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags); 311 } 312 313 static int blkdev_pr_release(struct block_device *bdev, blk_mode_t mode, 314 struct pr_reservation __user *arg) 315 { 316 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; 317 struct pr_reservation rsv; 318 319 if (!blkdev_pr_allowed(bdev, mode)) 320 return -EPERM; 321 if (!ops || !ops->pr_release) 322 return -EOPNOTSUPP; 323 if (copy_from_user(&rsv, arg, sizeof(rsv))) 324 return -EFAULT; 325 326 if (rsv.flags) 327 return -EOPNOTSUPP; 328 return ops->pr_release(bdev, rsv.key, rsv.type); 329 } 330 331 static int blkdev_pr_preempt(struct block_device *bdev, blk_mode_t mode, 332 struct pr_preempt __user *arg, bool abort) 333 { 334 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; 335 struct pr_preempt p; 336 337 if (!blkdev_pr_allowed(bdev, mode)) 338 return -EPERM; 339 if (!ops || !ops->pr_preempt) 340 return -EOPNOTSUPP; 341 if (copy_from_user(&p, arg, sizeof(p))) 342 return -EFAULT; 343 344 if (p.flags) 345 return -EOPNOTSUPP; 346 return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort); 347 } 348 349 static int blkdev_pr_clear(struct block_device *bdev, blk_mode_t mode, 350 struct pr_clear __user *arg) 351 { 352 const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; 353 struct pr_clear c; 354 355 if (!blkdev_pr_allowed(bdev, mode)) 356 return -EPERM; 357 if (!ops || !ops->pr_clear) 358 return -EOPNOTSUPP; 359 if (copy_from_user(&c, arg, sizeof(c))) 360 return -EFAULT; 361 362 if (c.flags) 363 return -EOPNOTSUPP; 364 return ops->pr_clear(bdev, c.key); 365 } 366 367 static int blkdev_flushbuf(struct block_device *bdev, unsigned cmd, 368 unsigned long arg) 369 { 370 if (!capable(CAP_SYS_ADMIN)) 371 return -EACCES; 372 373 mutex_lock(&bdev->bd_holder_lock); 374 if (bdev->bd_holder_ops && bdev->bd_holder_ops->sync) 375 bdev->bd_holder_ops->sync(bdev); 376 else 377 sync_blockdev(bdev); 378 mutex_unlock(&bdev->bd_holder_lock); 379 380 invalidate_bdev(bdev); 381 return 0; 382 } 383 384 static int blkdev_roset(struct block_device *bdev, unsigned cmd, 385 unsigned long arg) 386 { 387 int ret, n; 388 389 if (!capable(CAP_SYS_ADMIN)) 390 return -EACCES; 391 392 if (get_user(n, (int __user *)arg)) 393 return -EFAULT; 394 if (bdev->bd_disk->fops->set_read_only) { 395 ret = bdev->bd_disk->fops->set_read_only(bdev, n); 396 if (ret) 397 return ret; 398 } 399 bdev->bd_read_only = n; 400 return 0; 401 } 402 403 static int blkdev_getgeo(struct block_device *bdev, 404 struct hd_geometry __user *argp) 405 { 406 struct gendisk *disk = bdev->bd_disk; 407 struct hd_geometry geo; 408 int ret; 409 410 if (!argp) 411 return -EINVAL; 412 if (!disk->fops->getgeo) 413 return -ENOTTY; 414 415 /* 416 * We need to set the startsect first, the driver may 417 * want to override it. 418 */ 419 memset(&geo, 0, sizeof(geo)); 420 geo.start = get_start_sect(bdev); 421 ret = disk->fops->getgeo(bdev, &geo); 422 if (ret) 423 return ret; 424 if (copy_to_user(argp, &geo, sizeof(geo))) 425 return -EFAULT; 426 return 0; 427 } 428 429 #ifdef CONFIG_COMPAT 430 struct compat_hd_geometry { 431 unsigned char heads; 432 unsigned char sectors; 433 unsigned short cylinders; 434 u32 start; 435 }; 436 437 static int compat_hdio_getgeo(struct block_device *bdev, 438 struct compat_hd_geometry __user *ugeo) 439 { 440 struct gendisk *disk = bdev->bd_disk; 441 struct hd_geometry geo; 442 int ret; 443 444 if (!ugeo) 445 return -EINVAL; 446 if (!disk->fops->getgeo) 447 return -ENOTTY; 448 449 memset(&geo, 0, sizeof(geo)); 450 /* 451 * We need to set the startsect first, the driver may 452 * want to override it. 453 */ 454 geo.start = get_start_sect(bdev); 455 ret = disk->fops->getgeo(bdev, &geo); 456 if (ret) 457 return ret; 458 459 ret = copy_to_user(ugeo, &geo, 4); 460 ret |= put_user(geo.start, &ugeo->start); 461 if (ret) 462 ret = -EFAULT; 463 464 return ret; 465 } 466 #endif 467 468 /* set the logical block size */ 469 static int blkdev_bszset(struct block_device *bdev, blk_mode_t mode, 470 int __user *argp) 471 { 472 int ret, n; 473 474 if (!capable(CAP_SYS_ADMIN)) 475 return -EACCES; 476 if (!argp) 477 return -EINVAL; 478 if (get_user(n, argp)) 479 return -EFAULT; 480 481 if (mode & BLK_OPEN_EXCL) 482 return set_blocksize(bdev, n); 483 484 if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode, &bdev, NULL))) 485 return -EBUSY; 486 ret = set_blocksize(bdev, n); 487 blkdev_put(bdev, &bdev); 488 489 return ret; 490 } 491 492 /* 493 * Common commands that are handled the same way on native and compat 494 * user space. Note the separate arg/argp parameters that are needed 495 * to deal with the compat_ptr() conversion. 496 */ 497 static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, 498 unsigned int cmd, unsigned long arg, 499 void __user *argp) 500 { 501 unsigned int max_sectors; 502 503 switch (cmd) { 504 case BLKFLSBUF: 505 return blkdev_flushbuf(bdev, cmd, arg); 506 case BLKROSET: 507 return blkdev_roset(bdev, cmd, arg); 508 case BLKDISCARD: 509 return blk_ioctl_discard(bdev, mode, arg); 510 case BLKSECDISCARD: 511 return blk_ioctl_secure_erase(bdev, mode, argp); 512 case BLKZEROOUT: 513 return blk_ioctl_zeroout(bdev, mode, arg); 514 case BLKGETDISKSEQ: 515 return put_u64(argp, bdev->bd_disk->diskseq); 516 case BLKREPORTZONE: 517 return blkdev_report_zones_ioctl(bdev, cmd, arg); 518 case BLKRESETZONE: 519 case BLKOPENZONE: 520 case BLKCLOSEZONE: 521 case BLKFINISHZONE: 522 return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg); 523 case BLKGETZONESZ: 524 return put_uint(argp, bdev_zone_sectors(bdev)); 525 case BLKGETNRZONES: 526 return put_uint(argp, bdev_nr_zones(bdev)); 527 case BLKROGET: 528 return put_int(argp, bdev_read_only(bdev) != 0); 529 case BLKSSZGET: /* get block device logical block size */ 530 return put_int(argp, bdev_logical_block_size(bdev)); 531 case BLKPBSZGET: /* get block device physical block size */ 532 return put_uint(argp, bdev_physical_block_size(bdev)); 533 case BLKIOMIN: 534 return put_uint(argp, bdev_io_min(bdev)); 535 case BLKIOOPT: 536 return put_uint(argp, bdev_io_opt(bdev)); 537 case BLKALIGNOFF: 538 return put_int(argp, bdev_alignment_offset(bdev)); 539 case BLKDISCARDZEROES: 540 return put_uint(argp, 0); 541 case BLKSECTGET: 542 max_sectors = min_t(unsigned int, USHRT_MAX, 543 queue_max_sectors(bdev_get_queue(bdev))); 544 return put_ushort(argp, max_sectors); 545 case BLKROTATIONAL: 546 return put_ushort(argp, !bdev_nonrot(bdev)); 547 case BLKRASET: 548 case BLKFRASET: 549 if(!capable(CAP_SYS_ADMIN)) 550 return -EACCES; 551 bdev->bd_disk->bdi->ra_pages = (arg * 512) / PAGE_SIZE; 552 return 0; 553 case BLKRRPART: 554 if (!capable(CAP_SYS_ADMIN)) 555 return -EACCES; 556 if (bdev_is_partition(bdev)) 557 return -EINVAL; 558 return disk_scan_partitions(bdev->bd_disk, mode); 559 case BLKTRACESTART: 560 case BLKTRACESTOP: 561 case BLKTRACETEARDOWN: 562 return blk_trace_ioctl(bdev, cmd, argp); 563 case IOC_PR_REGISTER: 564 return blkdev_pr_register(bdev, mode, argp); 565 case IOC_PR_RESERVE: 566 return blkdev_pr_reserve(bdev, mode, argp); 567 case IOC_PR_RELEASE: 568 return blkdev_pr_release(bdev, mode, argp); 569 case IOC_PR_PREEMPT: 570 return blkdev_pr_preempt(bdev, mode, argp, false); 571 case IOC_PR_PREEMPT_ABORT: 572 return blkdev_pr_preempt(bdev, mode, argp, true); 573 case IOC_PR_CLEAR: 574 return blkdev_pr_clear(bdev, mode, argp); 575 default: 576 return -ENOIOCTLCMD; 577 } 578 } 579 580 /* 581 * Always keep this in sync with compat_blkdev_ioctl() 582 * to handle all incompatible commands in both functions. 583 * 584 * New commands must be compatible and go into blkdev_common_ioctl 585 */ 586 long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) 587 { 588 struct block_device *bdev = I_BDEV(file->f_mapping->host); 589 void __user *argp = (void __user *)arg; 590 blk_mode_t mode = file_to_blk_mode(file); 591 int ret; 592 593 switch (cmd) { 594 /* These need separate implementations for the data structure */ 595 case HDIO_GETGEO: 596 return blkdev_getgeo(bdev, argp); 597 case BLKPG: 598 return blkpg_ioctl(bdev, argp); 599 600 /* Compat mode returns 32-bit data instead of 'long' */ 601 case BLKRAGET: 602 case BLKFRAGET: 603 if (!argp) 604 return -EINVAL; 605 return put_long(argp, 606 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512); 607 case BLKGETSIZE: 608 if (bdev_nr_sectors(bdev) > ~0UL) 609 return -EFBIG; 610 return put_ulong(argp, bdev_nr_sectors(bdev)); 611 612 /* The data is compatible, but the command number is different */ 613 case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */ 614 return put_int(argp, block_size(bdev)); 615 case BLKBSZSET: 616 return blkdev_bszset(bdev, mode, argp); 617 case BLKGETSIZE64: 618 return put_u64(argp, bdev_nr_bytes(bdev)); 619 620 /* Incompatible alignment on i386 */ 621 case BLKTRACESETUP: 622 return blk_trace_ioctl(bdev, cmd, argp); 623 default: 624 break; 625 } 626 627 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); 628 if (ret != -ENOIOCTLCMD) 629 return ret; 630 631 if (!bdev->bd_disk->fops->ioctl) 632 return -ENOTTY; 633 return bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); 634 } 635 636 #ifdef CONFIG_COMPAT 637 638 #define BLKBSZGET_32 _IOR(0x12, 112, int) 639 #define BLKBSZSET_32 _IOW(0x12, 113, int) 640 #define BLKGETSIZE64_32 _IOR(0x12, 114, int) 641 642 /* Most of the generic ioctls are handled in the normal fallback path. 643 This assumes the blkdev's low level compat_ioctl always returns 644 ENOIOCTLCMD for unknown ioctls. */ 645 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) 646 { 647 int ret; 648 void __user *argp = compat_ptr(arg); 649 struct block_device *bdev = I_BDEV(file->f_mapping->host); 650 struct gendisk *disk = bdev->bd_disk; 651 blk_mode_t mode = file_to_blk_mode(file); 652 653 switch (cmd) { 654 /* These need separate implementations for the data structure */ 655 case HDIO_GETGEO: 656 return compat_hdio_getgeo(bdev, argp); 657 case BLKPG: 658 return compat_blkpg_ioctl(bdev, argp); 659 660 /* Compat mode returns 32-bit data instead of 'long' */ 661 case BLKRAGET: 662 case BLKFRAGET: 663 if (!argp) 664 return -EINVAL; 665 return compat_put_long(argp, 666 (bdev->bd_disk->bdi->ra_pages * PAGE_SIZE) / 512); 667 case BLKGETSIZE: 668 if (bdev_nr_sectors(bdev) > ~(compat_ulong_t)0) 669 return -EFBIG; 670 return compat_put_ulong(argp, bdev_nr_sectors(bdev)); 671 672 /* The data is compatible, but the command number is different */ 673 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */ 674 return put_int(argp, bdev_logical_block_size(bdev)); 675 case BLKBSZSET_32: 676 return blkdev_bszset(bdev, mode, argp); 677 case BLKGETSIZE64_32: 678 return put_u64(argp, bdev_nr_bytes(bdev)); 679 680 /* Incompatible alignment on i386 */ 681 case BLKTRACESETUP32: 682 return blk_trace_ioctl(bdev, cmd, argp); 683 default: 684 break; 685 } 686 687 ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp); 688 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl) 689 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg); 690 691 return ret; 692 } 693 #endif 694