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