1 /* 2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com> 3 * Horst Hummel <Horst.Hummel@de.ibm.com> 4 * Carsten Otte <Cotte@de.ibm.com> 5 * Martin Schwidefsky <schwidefsky@de.ibm.com> 6 * Bugreports.to..: <Linux390@de.ibm.com> 7 * Copyright IBM Corp. 1999, 2001 8 * 9 * i/o controls for the dasd driver. 10 */ 11 12 #define KMSG_COMPONENT "dasd" 13 14 #include <linux/interrupt.h> 15 #include <linux/compat.h> 16 #include <linux/major.h> 17 #include <linux/fs.h> 18 #include <linux/blkpg.h> 19 #include <linux/slab.h> 20 #include <asm/compat.h> 21 #include <asm/ccwdev.h> 22 #include <asm/schid.h> 23 #include <asm/cmb.h> 24 #include <asm/uaccess.h> 25 26 /* This is ugly... */ 27 #define PRINTK_HEADER "dasd_ioctl:" 28 29 #include "dasd_int.h" 30 31 32 static int 33 dasd_ioctl_api_version(void __user *argp) 34 { 35 int ver = DASD_API_VERSION; 36 return put_user(ver, (int __user *)argp); 37 } 38 39 /* 40 * Enable device. 41 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection 42 */ 43 static int 44 dasd_ioctl_enable(struct block_device *bdev) 45 { 46 struct dasd_device *base; 47 48 if (!capable(CAP_SYS_ADMIN)) 49 return -EACCES; 50 51 base = dasd_device_from_gendisk(bdev->bd_disk); 52 if (!base) 53 return -ENODEV; 54 55 dasd_enable_device(base); 56 /* Formatting the dasd device can change the capacity. */ 57 mutex_lock(&bdev->bd_mutex); 58 i_size_write(bdev->bd_inode, 59 (loff_t)get_capacity(base->block->gdp) << 9); 60 mutex_unlock(&bdev->bd_mutex); 61 dasd_put_device(base); 62 return 0; 63 } 64 65 /* 66 * Disable device. 67 * Used by dasdfmt. Disable I/O operations but allow ioctls. 68 */ 69 static int 70 dasd_ioctl_disable(struct block_device *bdev) 71 { 72 struct dasd_device *base; 73 74 if (!capable(CAP_SYS_ADMIN)) 75 return -EACCES; 76 77 base = dasd_device_from_gendisk(bdev->bd_disk); 78 if (!base) 79 return -ENODEV; 80 /* 81 * Man this is sick. We don't do a real disable but only downgrade 82 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses 83 * BIODASDDISABLE to disable accesses to the device via the block 84 * device layer but it still wants to do i/o on the device by 85 * using the BIODASDFMT ioctl. Therefore the correct state for the 86 * device is DASD_STATE_BASIC that allows to do basic i/o. 87 */ 88 dasd_set_target_state(base, DASD_STATE_BASIC); 89 /* 90 * Set i_size to zero, since read, write, etc. check against this 91 * value. 92 */ 93 mutex_lock(&bdev->bd_mutex); 94 i_size_write(bdev->bd_inode, 0); 95 mutex_unlock(&bdev->bd_mutex); 96 dasd_put_device(base); 97 return 0; 98 } 99 100 /* 101 * Quiesce device. 102 */ 103 static int dasd_ioctl_quiesce(struct dasd_block *block) 104 { 105 unsigned long flags; 106 struct dasd_device *base; 107 108 base = block->base; 109 if (!capable (CAP_SYS_ADMIN)) 110 return -EACCES; 111 112 pr_info("%s: The DASD has been put in the quiesce " 113 "state\n", dev_name(&base->cdev->dev)); 114 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags); 115 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE); 116 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags); 117 return 0; 118 } 119 120 121 /* 122 * Resume device. 123 */ 124 static int dasd_ioctl_resume(struct dasd_block *block) 125 { 126 unsigned long flags; 127 struct dasd_device *base; 128 129 base = block->base; 130 if (!capable (CAP_SYS_ADMIN)) 131 return -EACCES; 132 133 pr_info("%s: I/O operations have been resumed " 134 "on the DASD\n", dev_name(&base->cdev->dev)); 135 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags); 136 dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE); 137 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags); 138 139 dasd_schedule_block_bh(block); 140 return 0; 141 } 142 143 /* 144 * Abort all failfast I/O on a device. 145 */ 146 static int dasd_ioctl_abortio(struct dasd_block *block) 147 { 148 unsigned long flags; 149 struct dasd_device *base; 150 struct dasd_ccw_req *cqr, *n; 151 152 base = block->base; 153 if (!capable(CAP_SYS_ADMIN)) 154 return -EACCES; 155 156 if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags)) 157 return 0; 158 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set"); 159 160 spin_lock_irqsave(&block->request_queue_lock, flags); 161 spin_lock(&block->queue_lock); 162 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) { 163 if (test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) && 164 cqr->callback_data && 165 cqr->callback_data != DASD_SLEEPON_START_TAG && 166 cqr->callback_data != DASD_SLEEPON_END_TAG) { 167 spin_unlock(&block->queue_lock); 168 blk_abort_request(cqr->callback_data); 169 spin_lock(&block->queue_lock); 170 } 171 } 172 spin_unlock(&block->queue_lock); 173 spin_unlock_irqrestore(&block->request_queue_lock, flags); 174 175 dasd_schedule_block_bh(block); 176 return 0; 177 } 178 179 /* 180 * Allow I/O on a device 181 */ 182 static int dasd_ioctl_allowio(struct dasd_block *block) 183 { 184 struct dasd_device *base; 185 186 base = block->base; 187 if (!capable(CAP_SYS_ADMIN)) 188 return -EACCES; 189 190 if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags)) 191 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset"); 192 193 return 0; 194 } 195 196 /* 197 * performs formatting of _device_ according to _fdata_ 198 * Note: The discipline's format_function is assumed to deliver formatting 199 * commands to format multiple units of the device. In terms of the ECKD 200 * devices this means CCWs are generated to format multiple tracks. 201 */ 202 static int 203 dasd_format(struct dasd_block *block, struct format_data_t *fdata) 204 { 205 struct dasd_device *base; 206 int rc; 207 208 base = block->base; 209 if (base->discipline->format_device == NULL) 210 return -EPERM; 211 212 if (base->state != DASD_STATE_BASIC) { 213 pr_warn("%s: The DASD cannot be formatted while it is enabled\n", 214 dev_name(&base->cdev->dev)); 215 return -EBUSY; 216 } 217 218 DBF_DEV_EVENT(DBF_NOTICE, base, 219 "formatting units %u to %u (%u B blocks) flags %u", 220 fdata->start_unit, 221 fdata->stop_unit, fdata->blksize, fdata->intensity); 222 223 /* Since dasdfmt keeps the device open after it was disabled, 224 * there still exists an inode for this device. 225 * We must update i_blkbits, otherwise we might get errors when 226 * enabling the device later. 227 */ 228 if (fdata->start_unit == 0) { 229 struct block_device *bdev = bdget_disk(block->gdp, 0); 230 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize); 231 bdput(bdev); 232 } 233 234 rc = base->discipline->format_device(base, fdata, 1); 235 if (rc == -EAGAIN) 236 rc = base->discipline->format_device(base, fdata, 0); 237 238 return rc; 239 } 240 241 /* 242 * Format device. 243 */ 244 static int 245 dasd_ioctl_format(struct block_device *bdev, void __user *argp) 246 { 247 struct dasd_device *base; 248 struct format_data_t fdata; 249 int rc; 250 251 if (!capable(CAP_SYS_ADMIN)) 252 return -EACCES; 253 if (!argp) 254 return -EINVAL; 255 base = dasd_device_from_gendisk(bdev->bd_disk); 256 if (!base) 257 return -ENODEV; 258 if (base->features & DASD_FEATURE_READONLY || 259 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) { 260 dasd_put_device(base); 261 return -EROFS; 262 } 263 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) { 264 dasd_put_device(base); 265 return -EFAULT; 266 } 267 if (bdev != bdev->bd_contains) { 268 pr_warn("%s: The specified DASD is a partition and cannot be formatted\n", 269 dev_name(&base->cdev->dev)); 270 dasd_put_device(base); 271 return -EINVAL; 272 } 273 rc = dasd_format(base->block, &fdata); 274 dasd_put_device(base); 275 return rc; 276 } 277 278 #ifdef CONFIG_DASD_PROFILE 279 /* 280 * Reset device profile information 281 */ 282 static int dasd_ioctl_reset_profile(struct dasd_block *block) 283 { 284 dasd_profile_reset(&block->profile); 285 return 0; 286 } 287 288 /* 289 * Return device profile information 290 */ 291 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp) 292 { 293 struct dasd_profile_info_t *data; 294 int rc = 0; 295 296 data = kmalloc(sizeof(*data), GFP_KERNEL); 297 if (!data) 298 return -ENOMEM; 299 300 spin_lock_bh(&block->profile.lock); 301 if (block->profile.data) { 302 data->dasd_io_reqs = block->profile.data->dasd_io_reqs; 303 data->dasd_io_sects = block->profile.data->dasd_io_sects; 304 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs, 305 sizeof(data->dasd_io_secs)); 306 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times, 307 sizeof(data->dasd_io_times)); 308 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps, 309 sizeof(data->dasd_io_timps)); 310 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1, 311 sizeof(data->dasd_io_time1)); 312 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2, 313 sizeof(data->dasd_io_time2)); 314 memcpy(data->dasd_io_time2ps, 315 block->profile.data->dasd_io_time2ps, 316 sizeof(data->dasd_io_time2ps)); 317 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3, 318 sizeof(data->dasd_io_time3)); 319 memcpy(data->dasd_io_nr_req, 320 block->profile.data->dasd_io_nr_req, 321 sizeof(data->dasd_io_nr_req)); 322 spin_unlock_bh(&block->profile.lock); 323 } else { 324 spin_unlock_bh(&block->profile.lock); 325 rc = -EIO; 326 goto out; 327 } 328 if (copy_to_user(argp, data, sizeof(*data))) 329 rc = -EFAULT; 330 out: 331 kfree(data); 332 return rc; 333 } 334 #else 335 static int dasd_ioctl_reset_profile(struct dasd_block *block) 336 { 337 return -ENOTTY; 338 } 339 340 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp) 341 { 342 return -ENOTTY; 343 } 344 #endif 345 346 /* 347 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2. 348 */ 349 static int dasd_ioctl_information(struct dasd_block *block, 350 unsigned int cmd, void __user *argp) 351 { 352 struct dasd_information2_t *dasd_info; 353 struct subchannel_id sch_id; 354 struct ccw_dev_id dev_id; 355 struct dasd_device *base; 356 struct ccw_device *cdev; 357 unsigned long flags; 358 int rc; 359 360 base = block->base; 361 if (!base->discipline || !base->discipline->fill_info) 362 return -EINVAL; 363 364 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL); 365 if (dasd_info == NULL) 366 return -ENOMEM; 367 368 rc = base->discipline->fill_info(base, dasd_info); 369 if (rc) { 370 kfree(dasd_info); 371 return rc; 372 } 373 374 cdev = base->cdev; 375 ccw_device_get_id(cdev, &dev_id); 376 ccw_device_get_schid(cdev, &sch_id); 377 378 dasd_info->devno = dev_id.devno; 379 dasd_info->schid = sch_id.sch_no; 380 dasd_info->cu_type = cdev->id.cu_type; 381 dasd_info->cu_model = cdev->id.cu_model; 382 dasd_info->dev_type = cdev->id.dev_type; 383 dasd_info->dev_model = cdev->id.dev_model; 384 dasd_info->status = base->state; 385 /* 386 * The open_count is increased for every opener, that includes 387 * the blkdev_get in dasd_scan_partitions. 388 * This must be hidden from user-space. 389 */ 390 dasd_info->open_count = atomic_read(&block->open_count); 391 if (!block->bdev) 392 dasd_info->open_count++; 393 394 /* 395 * check if device is really formatted 396 * LDL / CDL was returned by 'fill_info' 397 */ 398 if ((base->state < DASD_STATE_READY) || 399 (dasd_check_blocksize(block->bp_block))) 400 dasd_info->format = DASD_FORMAT_NONE; 401 402 dasd_info->features |= 403 ((base->features & DASD_FEATURE_READONLY) != 0); 404 405 memcpy(dasd_info->type, base->discipline->name, 4); 406 407 if (block->request_queue->request_fn) { 408 struct list_head *l; 409 #ifdef DASD_EXTENDED_PROFILING 410 { 411 struct list_head *l; 412 spin_lock_irqsave(&block->lock, flags); 413 list_for_each(l, &block->request_queue->queue_head) 414 dasd_info->req_queue_len++; 415 spin_unlock_irqrestore(&block->lock, flags); 416 } 417 #endif /* DASD_EXTENDED_PROFILING */ 418 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags); 419 list_for_each(l, &base->ccw_queue) 420 dasd_info->chanq_len++; 421 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), 422 flags); 423 } 424 425 rc = 0; 426 if (copy_to_user(argp, dasd_info, 427 ((cmd == (unsigned int) BIODASDINFO2) ? 428 sizeof(struct dasd_information2_t) : 429 sizeof(struct dasd_information_t)))) 430 rc = -EFAULT; 431 kfree(dasd_info); 432 return rc; 433 } 434 435 /* 436 * Set read only 437 */ 438 static int 439 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp) 440 { 441 struct dasd_device *base; 442 int intval, rc; 443 444 if (!capable(CAP_SYS_ADMIN)) 445 return -EACCES; 446 if (bdev != bdev->bd_contains) 447 // ro setting is not allowed for partitions 448 return -EINVAL; 449 if (get_user(intval, (int __user *)argp)) 450 return -EFAULT; 451 base = dasd_device_from_gendisk(bdev->bd_disk); 452 if (!base) 453 return -ENODEV; 454 if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) { 455 dasd_put_device(base); 456 return -EROFS; 457 } 458 set_disk_ro(bdev->bd_disk, intval); 459 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval); 460 dasd_put_device(base); 461 return rc; 462 } 463 464 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd, 465 struct cmbdata __user *argp) 466 { 467 size_t size = _IOC_SIZE(cmd); 468 struct cmbdata data; 469 int ret; 470 471 ret = cmf_readall(block->base->cdev, &data); 472 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp)))) 473 return -EFAULT; 474 return ret; 475 } 476 477 int dasd_ioctl(struct block_device *bdev, fmode_t mode, 478 unsigned int cmd, unsigned long arg) 479 { 480 struct dasd_block *block; 481 struct dasd_device *base; 482 void __user *argp; 483 int rc; 484 485 if (is_compat_task()) 486 argp = compat_ptr(arg); 487 else 488 argp = (void __user *)arg; 489 490 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) { 491 PRINT_DEBUG("empty data ptr"); 492 return -EINVAL; 493 } 494 495 base = dasd_device_from_gendisk(bdev->bd_disk); 496 if (!base) 497 return -ENODEV; 498 block = base->block; 499 rc = 0; 500 switch (cmd) { 501 case BIODASDDISABLE: 502 rc = dasd_ioctl_disable(bdev); 503 break; 504 case BIODASDENABLE: 505 rc = dasd_ioctl_enable(bdev); 506 break; 507 case BIODASDQUIESCE: 508 rc = dasd_ioctl_quiesce(block); 509 break; 510 case BIODASDRESUME: 511 rc = dasd_ioctl_resume(block); 512 break; 513 case BIODASDABORTIO: 514 rc = dasd_ioctl_abortio(block); 515 break; 516 case BIODASDALLOWIO: 517 rc = dasd_ioctl_allowio(block); 518 break; 519 case BIODASDFMT: 520 rc = dasd_ioctl_format(bdev, argp); 521 break; 522 case BIODASDINFO: 523 rc = dasd_ioctl_information(block, cmd, argp); 524 break; 525 case BIODASDINFO2: 526 rc = dasd_ioctl_information(block, cmd, argp); 527 break; 528 case BIODASDPRRD: 529 rc = dasd_ioctl_read_profile(block, argp); 530 break; 531 case BIODASDPRRST: 532 rc = dasd_ioctl_reset_profile(block); 533 break; 534 case BLKROSET: 535 rc = dasd_ioctl_set_ro(bdev, argp); 536 break; 537 case DASDAPIVER: 538 rc = dasd_ioctl_api_version(argp); 539 break; 540 case BIODASDCMFENABLE: 541 rc = enable_cmf(base->cdev); 542 break; 543 case BIODASDCMFDISABLE: 544 rc = disable_cmf(base->cdev); 545 break; 546 case BIODASDREADALLCMB: 547 rc = dasd_ioctl_readall_cmb(block, cmd, argp); 548 break; 549 default: 550 /* if the discipline has an ioctl method try it. */ 551 rc = -ENOTTY; 552 if (base->discipline->ioctl) 553 rc = base->discipline->ioctl(block, cmd, argp); 554 } 555 dasd_put_device(base); 556 return rc; 557 } 558