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