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