1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com> 4 * Horst Hummel <Horst.Hummel@de.ibm.com> 5 * Carsten Otte <Cotte@de.ibm.com> 6 * Martin Schwidefsky <schwidefsky@de.ibm.com> 7 * Bugreports.to..: <Linux390@de.ibm.com> 8 * Copyright IBM Corp. 1999, 2001 9 * 10 * i/o controls for the dasd driver. 11 */ 12 13 #define KMSG_COMPONENT "dasd" 14 15 #include <linux/interrupt.h> 16 #include <linux/compat.h> 17 #include <linux/major.h> 18 #include <linux/fs.h> 19 #include <linux/blkpg.h> 20 #include <linux/slab.h> 21 #include <asm/ccwdev.h> 22 #include <asm/schid.h> 23 #include <asm/cmb.h> 24 #include <linux/uaccess.h> 25 #include <linux/dasd_mod.h> 26 27 /* This is ugly... */ 28 #define PRINTK_HEADER "dasd_ioctl:" 29 30 #include "dasd_int.h" 31 32 33 static int 34 dasd_ioctl_api_version(void __user *argp) 35 { 36 int ver = DASD_API_VERSION; 37 return put_user(ver, (int __user *)argp); 38 } 39 40 /* 41 * Enable device. 42 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection 43 */ 44 static int 45 dasd_ioctl_enable(struct block_device *bdev) 46 { 47 struct dasd_device *base; 48 49 if (!capable(CAP_SYS_ADMIN)) 50 return -EACCES; 51 52 base = dasd_device_from_gendisk(bdev->bd_disk); 53 if (!base) 54 return -ENODEV; 55 56 dasd_enable_device(base); 57 dasd_put_device(base); 58 return 0; 59 } 60 61 /* 62 * Disable device. 63 * Used by dasdfmt. Disable I/O operations but allow ioctls. 64 */ 65 static int 66 dasd_ioctl_disable(struct block_device *bdev) 67 { 68 struct dasd_device *base; 69 70 if (!capable(CAP_SYS_ADMIN)) 71 return -EACCES; 72 73 base = dasd_device_from_gendisk(bdev->bd_disk); 74 if (!base) 75 return -ENODEV; 76 /* 77 * Man this is sick. We don't do a real disable but only downgrade 78 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses 79 * BIODASDDISABLE to disable accesses to the device via the block 80 * device layer but it still wants to do i/o on the device by 81 * using the BIODASDFMT ioctl. Therefore the correct state for the 82 * device is DASD_STATE_BASIC that allows to do basic i/o. 83 */ 84 dasd_set_target_state(base, DASD_STATE_BASIC); 85 /* 86 * Set i_size to zero, since read, write, etc. check against this 87 * value. 88 */ 89 set_capacity(bdev->bd_disk, 0); 90 dasd_put_device(base); 91 return 0; 92 } 93 94 /* 95 * Quiesce device. 96 */ 97 static int dasd_ioctl_quiesce(struct dasd_block *block) 98 { 99 unsigned long flags; 100 struct dasd_device *base; 101 102 base = block->base; 103 if (!capable (CAP_SYS_ADMIN)) 104 return -EACCES; 105 106 pr_info("%s: The DASD has been put in the quiesce " 107 "state\n", dev_name(&base->cdev->dev)); 108 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags); 109 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE); 110 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags); 111 return 0; 112 } 113 114 115 /* 116 * Resume device. 117 */ 118 static int dasd_ioctl_resume(struct dasd_block *block) 119 { 120 unsigned long flags; 121 struct dasd_device *base; 122 123 base = block->base; 124 if (!capable (CAP_SYS_ADMIN)) 125 return -EACCES; 126 127 pr_info("%s: I/O operations have been resumed " 128 "on the DASD\n", dev_name(&base->cdev->dev)); 129 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags); 130 dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE); 131 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags); 132 133 dasd_schedule_block_bh(block); 134 return 0; 135 } 136 137 /* 138 * Abort all failfast I/O on a device. 139 */ 140 static int dasd_ioctl_abortio(struct dasd_block *block) 141 { 142 unsigned long flags; 143 struct dasd_device *base; 144 struct dasd_ccw_req *cqr, *n; 145 146 base = block->base; 147 if (!capable(CAP_SYS_ADMIN)) 148 return -EACCES; 149 150 if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags)) 151 return 0; 152 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set"); 153 154 spin_lock_irqsave(&block->request_queue_lock, flags); 155 spin_lock(&block->queue_lock); 156 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) { 157 if (test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) && 158 cqr->callback_data && 159 cqr->callback_data != DASD_SLEEPON_START_TAG && 160 cqr->callback_data != DASD_SLEEPON_END_TAG) { 161 spin_unlock(&block->queue_lock); 162 blk_abort_request(cqr->callback_data); 163 spin_lock(&block->queue_lock); 164 } 165 } 166 spin_unlock(&block->queue_lock); 167 spin_unlock_irqrestore(&block->request_queue_lock, flags); 168 169 dasd_schedule_block_bh(block); 170 return 0; 171 } 172 173 /* 174 * Allow I/O on a device 175 */ 176 static int dasd_ioctl_allowio(struct dasd_block *block) 177 { 178 struct dasd_device *base; 179 180 base = block->base; 181 if (!capable(CAP_SYS_ADMIN)) 182 return -EACCES; 183 184 if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags)) 185 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset"); 186 187 return 0; 188 } 189 190 /* 191 * performs formatting of _device_ according to _fdata_ 192 * Note: The discipline's format_function is assumed to deliver formatting 193 * commands to format multiple units of the device. In terms of the ECKD 194 * devices this means CCWs are generated to format multiple tracks. 195 */ 196 static int 197 dasd_format(struct dasd_block *block, struct format_data_t *fdata) 198 { 199 struct dasd_device *base; 200 int rc; 201 202 base = block->base; 203 if (base->discipline->format_device == NULL) 204 return -EPERM; 205 206 if (base->state != DASD_STATE_BASIC) { 207 pr_warn("%s: The DASD cannot be formatted while it is enabled\n", 208 dev_name(&base->cdev->dev)); 209 return -EBUSY; 210 } 211 212 DBF_DEV_EVENT(DBF_NOTICE, base, 213 "formatting units %u to %u (%u B blocks) flags %u", 214 fdata->start_unit, 215 fdata->stop_unit, fdata->blksize, fdata->intensity); 216 217 /* Since dasdfmt keeps the device open after it was disabled, 218 * there still exists an inode for this device. 219 * We must update i_blkbits, otherwise we might get errors when 220 * enabling the device later. 221 */ 222 if (fdata->start_unit == 0) { 223 block->gdp->part0->bd_inode->i_blkbits = 224 blksize_bits(fdata->blksize); 225 } 226 227 rc = base->discipline->format_device(base, fdata, 1); 228 if (rc == -EAGAIN) 229 rc = base->discipline->format_device(base, fdata, 0); 230 231 return rc; 232 } 233 234 static int dasd_check_format(struct dasd_block *block, 235 struct format_check_t *cdata) 236 { 237 struct dasd_device *base; 238 int rc; 239 240 base = block->base; 241 if (!base->discipline->check_device_format) 242 return -ENOTTY; 243 244 rc = base->discipline->check_device_format(base, cdata, 1); 245 if (rc == -EAGAIN) 246 rc = base->discipline->check_device_format(base, cdata, 0); 247 248 return rc; 249 } 250 251 /* 252 * Format device. 253 */ 254 static int 255 dasd_ioctl_format(struct block_device *bdev, void __user *argp) 256 { 257 struct dasd_device *base; 258 struct format_data_t fdata; 259 int rc; 260 261 if (!capable(CAP_SYS_ADMIN)) 262 return -EACCES; 263 if (!argp) 264 return -EINVAL; 265 base = dasd_device_from_gendisk(bdev->bd_disk); 266 if (!base) 267 return -ENODEV; 268 if (base->features & DASD_FEATURE_READONLY || 269 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) { 270 dasd_put_device(base); 271 return -EROFS; 272 } 273 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) { 274 dasd_put_device(base); 275 return -EFAULT; 276 } 277 if (bdev_is_partition(bdev)) { 278 pr_warn("%s: The specified DASD is a partition and cannot be formatted\n", 279 dev_name(&base->cdev->dev)); 280 dasd_put_device(base); 281 return -EINVAL; 282 } 283 rc = dasd_format(base->block, &fdata); 284 dasd_put_device(base); 285 286 return rc; 287 } 288 289 /* 290 * Check device format 291 */ 292 static int dasd_ioctl_check_format(struct block_device *bdev, void __user *argp) 293 { 294 struct format_check_t cdata; 295 struct dasd_device *base; 296 int rc = 0; 297 298 if (!argp) 299 return -EINVAL; 300 301 base = dasd_device_from_gendisk(bdev->bd_disk); 302 if (!base) 303 return -ENODEV; 304 if (bdev_is_partition(bdev)) { 305 pr_warn("%s: The specified DASD is a partition and cannot be checked\n", 306 dev_name(&base->cdev->dev)); 307 rc = -EINVAL; 308 goto out_err; 309 } 310 311 if (copy_from_user(&cdata, argp, sizeof(cdata))) { 312 rc = -EFAULT; 313 goto out_err; 314 } 315 316 rc = dasd_check_format(base->block, &cdata); 317 if (rc) 318 goto out_err; 319 320 if (copy_to_user(argp, &cdata, sizeof(cdata))) 321 rc = -EFAULT; 322 323 out_err: 324 dasd_put_device(base); 325 326 return rc; 327 } 328 329 static int dasd_release_space(struct dasd_device *device, 330 struct format_data_t *rdata) 331 { 332 if (!device->discipline->is_ese && !device->discipline->is_ese(device)) 333 return -ENOTSUPP; 334 if (!device->discipline->release_space) 335 return -ENOTSUPP; 336 337 return device->discipline->release_space(device, rdata); 338 } 339 340 /* 341 * Release allocated space 342 */ 343 static int dasd_ioctl_release_space(struct block_device *bdev, void __user *argp) 344 { 345 struct format_data_t rdata; 346 struct dasd_device *base; 347 int rc = 0; 348 349 if (!capable(CAP_SYS_ADMIN)) 350 return -EACCES; 351 if (!argp) 352 return -EINVAL; 353 354 base = dasd_device_from_gendisk(bdev->bd_disk); 355 if (!base) 356 return -ENODEV; 357 if (base->features & DASD_FEATURE_READONLY || 358 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) { 359 rc = -EROFS; 360 goto out_err; 361 } 362 if (bdev_is_partition(bdev)) { 363 pr_warn("%s: The specified DASD is a partition and tracks cannot be released\n", 364 dev_name(&base->cdev->dev)); 365 rc = -EINVAL; 366 goto out_err; 367 } 368 369 if (copy_from_user(&rdata, argp, sizeof(rdata))) { 370 rc = -EFAULT; 371 goto out_err; 372 } 373 374 rc = dasd_release_space(base, &rdata); 375 376 out_err: 377 dasd_put_device(base); 378 379 return rc; 380 } 381 382 /* 383 * Swap driver iternal copy relation. 384 */ 385 static int 386 dasd_ioctl_copy_pair_swap(struct block_device *bdev, void __user *argp) 387 { 388 struct dasd_copypair_swap_data_t data; 389 struct dasd_device *device; 390 int rc; 391 392 if (!capable(CAP_SYS_ADMIN)) 393 return -EACCES; 394 395 device = dasd_device_from_gendisk(bdev->bd_disk); 396 if (!device) 397 return -ENODEV; 398 399 if (copy_from_user(&data, argp, sizeof(struct dasd_copypair_swap_data_t))) { 400 dasd_put_device(device); 401 return -EFAULT; 402 } 403 if (memchr_inv(data.reserved, 0, sizeof(data.reserved))) { 404 pr_warn("%s: Invalid swap data specified\n", 405 dev_name(&device->cdev->dev)); 406 dasd_put_device(device); 407 return DASD_COPYPAIRSWAP_INVALID; 408 } 409 if (bdev_is_partition(bdev)) { 410 pr_warn("%s: The specified DASD is a partition and cannot be swapped\n", 411 dev_name(&device->cdev->dev)); 412 dasd_put_device(device); 413 return DASD_COPYPAIRSWAP_INVALID; 414 } 415 if (!device->copy) { 416 pr_warn("%s: The specified DASD has no copy pair set up\n", 417 dev_name(&device->cdev->dev)); 418 dasd_put_device(device); 419 return -ENODEV; 420 } 421 if (!device->discipline->copy_pair_swap) { 422 dasd_put_device(device); 423 return -EOPNOTSUPP; 424 } 425 rc = device->discipline->copy_pair_swap(device, data.primary, 426 data.secondary); 427 dasd_put_device(device); 428 429 return rc; 430 } 431 432 #ifdef CONFIG_DASD_PROFILE 433 /* 434 * Reset device profile information 435 */ 436 static int dasd_ioctl_reset_profile(struct dasd_block *block) 437 { 438 dasd_profile_reset(&block->profile); 439 return 0; 440 } 441 442 /* 443 * Return device profile information 444 */ 445 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp) 446 { 447 struct dasd_profile_info_t *data; 448 int rc = 0; 449 450 data = kmalloc(sizeof(*data), GFP_KERNEL); 451 if (!data) 452 return -ENOMEM; 453 454 spin_lock_bh(&block->profile.lock); 455 if (block->profile.data) { 456 data->dasd_io_reqs = block->profile.data->dasd_io_reqs; 457 data->dasd_io_sects = block->profile.data->dasd_io_sects; 458 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs, 459 sizeof(data->dasd_io_secs)); 460 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times, 461 sizeof(data->dasd_io_times)); 462 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps, 463 sizeof(data->dasd_io_timps)); 464 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1, 465 sizeof(data->dasd_io_time1)); 466 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2, 467 sizeof(data->dasd_io_time2)); 468 memcpy(data->dasd_io_time2ps, 469 block->profile.data->dasd_io_time2ps, 470 sizeof(data->dasd_io_time2ps)); 471 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3, 472 sizeof(data->dasd_io_time3)); 473 memcpy(data->dasd_io_nr_req, 474 block->profile.data->dasd_io_nr_req, 475 sizeof(data->dasd_io_nr_req)); 476 spin_unlock_bh(&block->profile.lock); 477 } else { 478 spin_unlock_bh(&block->profile.lock); 479 rc = -EIO; 480 goto out; 481 } 482 if (copy_to_user(argp, data, sizeof(*data))) 483 rc = -EFAULT; 484 out: 485 kfree(data); 486 return rc; 487 } 488 #else 489 static int dasd_ioctl_reset_profile(struct dasd_block *block) 490 { 491 return -ENOTTY; 492 } 493 494 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp) 495 { 496 return -ENOTTY; 497 } 498 #endif 499 500 /* 501 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2. 502 */ 503 static int __dasd_ioctl_information(struct dasd_block *block, 504 struct dasd_information2_t *dasd_info) 505 { 506 struct subchannel_id sch_id; 507 struct ccw_dev_id dev_id; 508 struct dasd_device *base; 509 struct ccw_device *cdev; 510 struct list_head *l; 511 unsigned long flags; 512 int rc; 513 514 base = block->base; 515 if (!base->discipline || !base->discipline->fill_info) 516 return -EINVAL; 517 518 rc = base->discipline->fill_info(base, dasd_info); 519 if (rc) 520 return rc; 521 522 cdev = base->cdev; 523 ccw_device_get_id(cdev, &dev_id); 524 ccw_device_get_schid(cdev, &sch_id); 525 526 dasd_info->devno = dev_id.devno; 527 dasd_info->schid = sch_id.sch_no; 528 dasd_info->cu_type = cdev->id.cu_type; 529 dasd_info->cu_model = cdev->id.cu_model; 530 dasd_info->dev_type = cdev->id.dev_type; 531 dasd_info->dev_model = cdev->id.dev_model; 532 dasd_info->status = base->state; 533 /* 534 * The open_count is increased for every opener, that includes 535 * the blkdev_get in dasd_scan_partitions. 536 * This must be hidden from user-space. 537 */ 538 dasd_info->open_count = atomic_read(&block->open_count); 539 if (!block->bdev) 540 dasd_info->open_count++; 541 542 /* 543 * check if device is really formatted 544 * LDL / CDL was returned by 'fill_info' 545 */ 546 if ((base->state < DASD_STATE_READY) || 547 (dasd_check_blocksize(block->bp_block))) 548 dasd_info->format = DASD_FORMAT_NONE; 549 550 dasd_info->features |= 551 ((base->features & DASD_FEATURE_READONLY) != 0); 552 553 memcpy(dasd_info->type, base->discipline->name, 4); 554 555 spin_lock_irqsave(&block->queue_lock, flags); 556 list_for_each(l, &base->ccw_queue) 557 dasd_info->chanq_len++; 558 spin_unlock_irqrestore(&block->queue_lock, flags); 559 return 0; 560 } 561 562 static int dasd_ioctl_information(struct dasd_block *block, void __user *argp, 563 size_t copy_size) 564 { 565 struct dasd_information2_t *dasd_info; 566 int error; 567 568 dasd_info = kzalloc(sizeof(*dasd_info), GFP_KERNEL); 569 if (!dasd_info) 570 return -ENOMEM; 571 572 error = __dasd_ioctl_information(block, dasd_info); 573 if (!error && copy_to_user(argp, dasd_info, copy_size)) 574 error = -EFAULT; 575 kfree(dasd_info); 576 return error; 577 } 578 579 /* 580 * Set read only 581 */ 582 int dasd_set_read_only(struct block_device *bdev, bool ro) 583 { 584 struct dasd_device *base; 585 int rc; 586 587 /* do not manipulate hardware state for partitions */ 588 if (bdev_is_partition(bdev)) 589 return 0; 590 591 base = dasd_device_from_gendisk(bdev->bd_disk); 592 if (!base) 593 return -ENODEV; 594 if (!ro && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) 595 rc = -EROFS; 596 else 597 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, ro); 598 dasd_put_device(base); 599 return rc; 600 } 601 602 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd, 603 struct cmbdata __user *argp) 604 { 605 size_t size = _IOC_SIZE(cmd); 606 struct cmbdata data; 607 int ret; 608 609 ret = cmf_readall(block->base->cdev, &data); 610 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp)))) 611 return -EFAULT; 612 return ret; 613 } 614 615 int dasd_ioctl(struct block_device *bdev, fmode_t mode, 616 unsigned int cmd, unsigned long arg) 617 { 618 struct dasd_block *block; 619 struct dasd_device *base; 620 void __user *argp; 621 int rc; 622 623 if (is_compat_task()) 624 argp = compat_ptr(arg); 625 else 626 argp = (void __user *)arg; 627 628 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) 629 return -EINVAL; 630 631 base = dasd_device_from_gendisk(bdev->bd_disk); 632 if (!base) 633 return -ENODEV; 634 block = base->block; 635 rc = 0; 636 switch (cmd) { 637 case BIODASDDISABLE: 638 rc = dasd_ioctl_disable(bdev); 639 break; 640 case BIODASDENABLE: 641 rc = dasd_ioctl_enable(bdev); 642 break; 643 case BIODASDQUIESCE: 644 rc = dasd_ioctl_quiesce(block); 645 break; 646 case BIODASDRESUME: 647 rc = dasd_ioctl_resume(block); 648 break; 649 case BIODASDABORTIO: 650 rc = dasd_ioctl_abortio(block); 651 break; 652 case BIODASDALLOWIO: 653 rc = dasd_ioctl_allowio(block); 654 break; 655 case BIODASDFMT: 656 rc = dasd_ioctl_format(bdev, argp); 657 break; 658 case BIODASDCHECKFMT: 659 rc = dasd_ioctl_check_format(bdev, argp); 660 break; 661 case BIODASDINFO: 662 rc = dasd_ioctl_information(block, argp, 663 sizeof(struct dasd_information_t)); 664 break; 665 case BIODASDINFO2: 666 rc = dasd_ioctl_information(block, argp, 667 sizeof(struct dasd_information2_t)); 668 break; 669 case BIODASDPRRD: 670 rc = dasd_ioctl_read_profile(block, argp); 671 break; 672 case BIODASDPRRST: 673 rc = dasd_ioctl_reset_profile(block); 674 break; 675 case DASDAPIVER: 676 rc = dasd_ioctl_api_version(argp); 677 break; 678 case BIODASDCMFENABLE: 679 rc = enable_cmf(base->cdev); 680 break; 681 case BIODASDCMFDISABLE: 682 rc = disable_cmf(base->cdev); 683 break; 684 case BIODASDREADALLCMB: 685 rc = dasd_ioctl_readall_cmb(block, cmd, argp); 686 break; 687 case BIODASDRAS: 688 rc = dasd_ioctl_release_space(bdev, argp); 689 break; 690 case BIODASDCOPYPAIRSWAP: 691 rc = dasd_ioctl_copy_pair_swap(bdev, argp); 692 break; 693 default: 694 /* if the discipline has an ioctl method try it. */ 695 rc = -ENOTTY; 696 if (base->discipline->ioctl) 697 rc = base->discipline->ioctl(block, cmd, argp); 698 } 699 dasd_put_device(base); 700 return rc; 701 } 702 703 704 /** 705 * dasd_biodasdinfo() - fill out the dasd information structure 706 * @disk: [in] pointer to gendisk structure that references a DASD 707 * @info: [out] pointer to the dasd_information2_t structure 708 * 709 * Provide access to DASD specific information. 710 * The gendisk structure is checked if it belongs to the DASD driver by 711 * comparing the gendisk->fops pointer. 712 * If it does not belong to the DASD driver -EINVAL is returned. 713 * Otherwise the provided dasd_information2_t structure is filled out. 714 * 715 * Returns: 716 * %0 on success and a negative error value on failure. 717 */ 718 int dasd_biodasdinfo(struct gendisk *disk, struct dasd_information2_t *info) 719 { 720 struct dasd_device *base; 721 int error; 722 723 if (disk->fops != &dasd_device_operations) 724 return -EINVAL; 725 726 base = dasd_device_from_gendisk(disk); 727 if (!base) 728 return -ENODEV; 729 error = __dasd_ioctl_information(base->block, info); 730 dasd_put_device(base); 731 return error; 732 } 733 /* export that symbol_get in partition detection is possible */ 734 EXPORT_SYMBOL_GPL(dasd_biodasdinfo); 735