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/compat.h> 22 #include <asm/ccwdev.h> 23 #include <asm/schid.h> 24 #include <asm/cmb.h> 25 #include <linux/uaccess.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 #ifdef CONFIG_DASD_PROFILE 338 /* 339 * Reset device profile information 340 */ 341 static int dasd_ioctl_reset_profile(struct dasd_block *block) 342 { 343 dasd_profile_reset(&block->profile); 344 return 0; 345 } 346 347 /* 348 * Return device profile information 349 */ 350 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp) 351 { 352 struct dasd_profile_info_t *data; 353 int rc = 0; 354 355 data = kmalloc(sizeof(*data), GFP_KERNEL); 356 if (!data) 357 return -ENOMEM; 358 359 spin_lock_bh(&block->profile.lock); 360 if (block->profile.data) { 361 data->dasd_io_reqs = block->profile.data->dasd_io_reqs; 362 data->dasd_io_sects = block->profile.data->dasd_io_sects; 363 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs, 364 sizeof(data->dasd_io_secs)); 365 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times, 366 sizeof(data->dasd_io_times)); 367 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps, 368 sizeof(data->dasd_io_timps)); 369 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1, 370 sizeof(data->dasd_io_time1)); 371 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2, 372 sizeof(data->dasd_io_time2)); 373 memcpy(data->dasd_io_time2ps, 374 block->profile.data->dasd_io_time2ps, 375 sizeof(data->dasd_io_time2ps)); 376 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3, 377 sizeof(data->dasd_io_time3)); 378 memcpy(data->dasd_io_nr_req, 379 block->profile.data->dasd_io_nr_req, 380 sizeof(data->dasd_io_nr_req)); 381 spin_unlock_bh(&block->profile.lock); 382 } else { 383 spin_unlock_bh(&block->profile.lock); 384 rc = -EIO; 385 goto out; 386 } 387 if (copy_to_user(argp, data, sizeof(*data))) 388 rc = -EFAULT; 389 out: 390 kfree(data); 391 return rc; 392 } 393 #else 394 static int dasd_ioctl_reset_profile(struct dasd_block *block) 395 { 396 return -ENOTTY; 397 } 398 399 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp) 400 { 401 return -ENOTTY; 402 } 403 #endif 404 405 /* 406 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2. 407 */ 408 static int dasd_ioctl_information(struct dasd_block *block, 409 unsigned int cmd, void __user *argp) 410 { 411 struct dasd_information2_t *dasd_info; 412 struct subchannel_id sch_id; 413 struct ccw_dev_id dev_id; 414 struct dasd_device *base; 415 struct ccw_device *cdev; 416 unsigned long flags; 417 int rc; 418 419 base = block->base; 420 if (!base->discipline || !base->discipline->fill_info) 421 return -EINVAL; 422 423 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL); 424 if (dasd_info == NULL) 425 return -ENOMEM; 426 427 rc = base->discipline->fill_info(base, dasd_info); 428 if (rc) { 429 kfree(dasd_info); 430 return rc; 431 } 432 433 cdev = base->cdev; 434 ccw_device_get_id(cdev, &dev_id); 435 ccw_device_get_schid(cdev, &sch_id); 436 437 dasd_info->devno = dev_id.devno; 438 dasd_info->schid = sch_id.sch_no; 439 dasd_info->cu_type = cdev->id.cu_type; 440 dasd_info->cu_model = cdev->id.cu_model; 441 dasd_info->dev_type = cdev->id.dev_type; 442 dasd_info->dev_model = cdev->id.dev_model; 443 dasd_info->status = base->state; 444 /* 445 * The open_count is increased for every opener, that includes 446 * the blkdev_get in dasd_scan_partitions. 447 * This must be hidden from user-space. 448 */ 449 dasd_info->open_count = atomic_read(&block->open_count); 450 if (!block->bdev) 451 dasd_info->open_count++; 452 453 /* 454 * check if device is really formatted 455 * LDL / CDL was returned by 'fill_info' 456 */ 457 if ((base->state < DASD_STATE_READY) || 458 (dasd_check_blocksize(block->bp_block))) 459 dasd_info->format = DASD_FORMAT_NONE; 460 461 dasd_info->features |= 462 ((base->features & DASD_FEATURE_READONLY) != 0); 463 464 memcpy(dasd_info->type, base->discipline->name, 4); 465 466 if (block->request_queue->request_fn) { 467 struct list_head *l; 468 #ifdef DASD_EXTENDED_PROFILING 469 { 470 struct list_head *l; 471 spin_lock_irqsave(&block->lock, flags); 472 list_for_each(l, &block->request_queue->queue_head) 473 dasd_info->req_queue_len++; 474 spin_unlock_irqrestore(&block->lock, flags); 475 } 476 #endif /* DASD_EXTENDED_PROFILING */ 477 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags); 478 list_for_each(l, &base->ccw_queue) 479 dasd_info->chanq_len++; 480 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), 481 flags); 482 } 483 484 rc = 0; 485 if (copy_to_user(argp, dasd_info, 486 ((cmd == (unsigned int) BIODASDINFO2) ? 487 sizeof(struct dasd_information2_t) : 488 sizeof(struct dasd_information_t)))) 489 rc = -EFAULT; 490 kfree(dasd_info); 491 return rc; 492 } 493 494 /* 495 * Set read only 496 */ 497 static int 498 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp) 499 { 500 struct dasd_device *base; 501 int intval, rc; 502 503 if (!capable(CAP_SYS_ADMIN)) 504 return -EACCES; 505 if (bdev != bdev->bd_contains) 506 // ro setting is not allowed for partitions 507 return -EINVAL; 508 if (get_user(intval, (int __user *)argp)) 509 return -EFAULT; 510 base = dasd_device_from_gendisk(bdev->bd_disk); 511 if (!base) 512 return -ENODEV; 513 if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) { 514 dasd_put_device(base); 515 return -EROFS; 516 } 517 set_disk_ro(bdev->bd_disk, intval); 518 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval); 519 dasd_put_device(base); 520 return rc; 521 } 522 523 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd, 524 struct cmbdata __user *argp) 525 { 526 size_t size = _IOC_SIZE(cmd); 527 struct cmbdata data; 528 int ret; 529 530 ret = cmf_readall(block->base->cdev, &data); 531 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp)))) 532 return -EFAULT; 533 return ret; 534 } 535 536 int dasd_ioctl(struct block_device *bdev, fmode_t mode, 537 unsigned int cmd, unsigned long arg) 538 { 539 struct dasd_block *block; 540 struct dasd_device *base; 541 void __user *argp; 542 int rc; 543 544 if (is_compat_task()) 545 argp = compat_ptr(arg); 546 else 547 argp = (void __user *)arg; 548 549 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) { 550 PRINT_DEBUG("empty data ptr"); 551 return -EINVAL; 552 } 553 554 base = dasd_device_from_gendisk(bdev->bd_disk); 555 if (!base) 556 return -ENODEV; 557 block = base->block; 558 rc = 0; 559 switch (cmd) { 560 case BIODASDDISABLE: 561 rc = dasd_ioctl_disable(bdev); 562 break; 563 case BIODASDENABLE: 564 rc = dasd_ioctl_enable(bdev); 565 break; 566 case BIODASDQUIESCE: 567 rc = dasd_ioctl_quiesce(block); 568 break; 569 case BIODASDRESUME: 570 rc = dasd_ioctl_resume(block); 571 break; 572 case BIODASDABORTIO: 573 rc = dasd_ioctl_abortio(block); 574 break; 575 case BIODASDALLOWIO: 576 rc = dasd_ioctl_allowio(block); 577 break; 578 case BIODASDFMT: 579 rc = dasd_ioctl_format(bdev, argp); 580 break; 581 case BIODASDCHECKFMT: 582 rc = dasd_ioctl_check_format(bdev, argp); 583 break; 584 case BIODASDINFO: 585 rc = dasd_ioctl_information(block, cmd, argp); 586 break; 587 case BIODASDINFO2: 588 rc = dasd_ioctl_information(block, cmd, argp); 589 break; 590 case BIODASDPRRD: 591 rc = dasd_ioctl_read_profile(block, argp); 592 break; 593 case BIODASDPRRST: 594 rc = dasd_ioctl_reset_profile(block); 595 break; 596 case BLKROSET: 597 rc = dasd_ioctl_set_ro(bdev, argp); 598 break; 599 case DASDAPIVER: 600 rc = dasd_ioctl_api_version(argp); 601 break; 602 case BIODASDCMFENABLE: 603 rc = enable_cmf(base->cdev); 604 break; 605 case BIODASDCMFDISABLE: 606 rc = disable_cmf(base->cdev); 607 break; 608 case BIODASDREADALLCMB: 609 rc = dasd_ioctl_readall_cmb(block, cmd, argp); 610 break; 611 default: 612 /* if the discipline has an ioctl method try it. */ 613 rc = -ENOTTY; 614 if (base->discipline->ioctl) 615 rc = base->discipline->ioctl(block, cmd, argp); 616 } 617 dasd_put_device(base); 618 return rc; 619 } 620