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