1 // SPDX-License-Identifier: GPL-2.0
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
10 * i/o controls for the dasd driver.
13 #define KMSG_COMPONENT "dasd"
15 #include <linux/interrupt.h>
16 #include <linux/compat.h>
17 #include <linux/major.h>
19 #include <linux/blkpg.h>
20 #include <linux/slab.h>
21 #include <asm/ccwdev.h>
22 #include <asm/schid.h>
24 #include <linux/uaccess.h>
25 #include <linux/dasd_mod.h>
28 #define PRINTK_HEADER "dasd_ioctl:"
34 dasd_ioctl_api_version(void __user *argp)
36 int ver = DASD_API_VERSION;
37 return put_user(ver, (int __user *)argp);
42 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
45 dasd_ioctl_enable(struct block_device *bdev)
47 struct dasd_device *base;
49 if (!capable(CAP_SYS_ADMIN))
52 base = dasd_device_from_gendisk(bdev->bd_disk);
56 dasd_enable_device(base);
57 dasd_put_device(base);
63 * Used by dasdfmt. Disable I/O operations but allow ioctls.
66 dasd_ioctl_disable(struct block_device *bdev)
68 struct dasd_device *base;
70 if (!capable(CAP_SYS_ADMIN))
73 base = dasd_device_from_gendisk(bdev->bd_disk);
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.
84 dasd_set_target_state(base, DASD_STATE_BASIC);
86 * Set i_size to zero, since read, write, etc. check against this
89 set_capacity(bdev->bd_disk, 0);
90 dasd_put_device(base);
97 static int dasd_ioctl_quiesce(struct dasd_block *block)
100 struct dasd_device *base;
103 if (!capable (CAP_SYS_ADMIN))
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);
118 static int dasd_ioctl_resume(struct dasd_block *block)
121 struct dasd_device *base;
124 if (!capable (CAP_SYS_ADMIN))
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);
133 dasd_schedule_block_bh(block);
134 dasd_schedule_device_bh(base);
139 * Abort all failfast I/O on a device.
141 static int dasd_ioctl_abortio(struct dasd_block *block)
144 struct dasd_device *base;
145 struct dasd_ccw_req *cqr, *n;
148 if (!capable(CAP_SYS_ADMIN))
151 if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags))
153 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set");
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);
167 spin_unlock(&block->queue_lock);
168 spin_unlock_irqrestore(&block->request_queue_lock, flags);
170 dasd_schedule_block_bh(block);
175 * Allow I/O on a device
177 static int dasd_ioctl_allowio(struct dasd_block *block)
179 struct dasd_device *base;
182 if (!capable(CAP_SYS_ADMIN))
185 if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags))
186 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset");
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.
198 dasd_format(struct dasd_block *block, struct format_data_t *fdata)
200 struct dasd_device *base;
204 if (base->discipline->format_device == NULL)
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));
213 DBF_DEV_EVENT(DBF_NOTICE, base,
214 "formatting units %u to %u (%u B blocks) flags %u",
216 fdata->stop_unit, fdata->blksize, fdata->intensity);
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.
223 if (fdata->start_unit == 0) {
224 block->gdp->part0->bd_inode->i_blkbits =
225 blksize_bits(fdata->blksize);
228 rc = base->discipline->format_device(base, fdata, 1);
230 rc = base->discipline->format_device(base, fdata, 0);
235 static int dasd_check_format(struct dasd_block *block,
236 struct format_check_t *cdata)
238 struct dasd_device *base;
242 if (!base->discipline->check_device_format)
245 rc = base->discipline->check_device_format(base, cdata, 1);
247 rc = base->discipline->check_device_format(base, cdata, 0);
256 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
258 struct dasd_device *base;
259 struct format_data_t fdata;
262 if (!capable(CAP_SYS_ADMIN))
266 base = dasd_device_from_gendisk(bdev->bd_disk);
269 if (base->features & DASD_FEATURE_READONLY ||
270 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
271 dasd_put_device(base);
274 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
275 dasd_put_device(base);
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);
284 rc = dasd_format(base->block, &fdata);
285 dasd_put_device(base);
291 * Check device format
293 static int dasd_ioctl_check_format(struct block_device *bdev, void __user *argp)
295 struct format_check_t cdata;
296 struct dasd_device *base;
302 base = dasd_device_from_gendisk(bdev->bd_disk);
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));
312 if (copy_from_user(&cdata, argp, sizeof(cdata))) {
317 rc = dasd_check_format(base->block, &cdata);
321 if (copy_to_user(argp, &cdata, sizeof(cdata)))
325 dasd_put_device(base);
330 static int dasd_release_space(struct dasd_device *device,
331 struct format_data_t *rdata)
333 if (!device->discipline->is_ese && !device->discipline->is_ese(device))
335 if (!device->discipline->release_space)
338 return device->discipline->release_space(device, rdata);
342 * Release allocated space
344 static int dasd_ioctl_release_space(struct block_device *bdev, void __user *argp)
346 struct format_data_t rdata;
347 struct dasd_device *base;
350 if (!capable(CAP_SYS_ADMIN))
355 base = dasd_device_from_gendisk(bdev->bd_disk);
358 if (base->features & DASD_FEATURE_READONLY ||
359 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
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));
370 if (copy_from_user(&rdata, argp, sizeof(rdata))) {
375 rc = dasd_release_space(base, &rdata);
378 dasd_put_device(base);
384 * Swap driver iternal copy relation.
387 dasd_ioctl_copy_pair_swap(struct block_device *bdev, void __user *argp)
389 struct dasd_copypair_swap_data_t data;
390 struct dasd_device *device;
393 if (!capable(CAP_SYS_ADMIN))
396 device = dasd_device_from_gendisk(bdev->bd_disk);
400 if (copy_from_user(&data, argp, sizeof(struct dasd_copypair_swap_data_t))) {
401 dasd_put_device(device);
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;
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;
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);
422 if (!device->discipline->copy_pair_swap) {
423 dasd_put_device(device);
426 rc = device->discipline->copy_pair_swap(device, data.primary,
428 dasd_put_device(device);
433 #ifdef CONFIG_DASD_PROFILE
435 * Reset device profile information
437 static int dasd_ioctl_reset_profile(struct dasd_block *block)
439 dasd_profile_reset(&block->profile);
444 * Return device profile information
446 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
448 struct dasd_profile_info_t *data;
451 data = kmalloc(sizeof(*data), GFP_KERNEL);
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);
479 spin_unlock_bh(&block->profile.lock);
483 if (copy_to_user(argp, data, sizeof(*data)))
490 static int dasd_ioctl_reset_profile(struct dasd_block *block)
495 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
502 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
504 static int __dasd_ioctl_information(struct dasd_block *block,
505 struct dasd_information2_t *dasd_info)
507 struct subchannel_id sch_id;
508 struct ccw_dev_id dev_id;
509 struct dasd_device *base;
510 struct ccw_device *cdev;
516 if (!base->discipline || !base->discipline->fill_info)
519 rc = base->discipline->fill_info(base, dasd_info);
524 ccw_device_get_id(cdev, &dev_id);
525 ccw_device_get_schid(cdev, &sch_id);
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;
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.
539 dasd_info->open_count = atomic_read(&block->open_count);
540 if (!block->bdev_handle)
541 dasd_info->open_count++;
544 * check if device is really formatted
545 * LDL / CDL was returned by 'fill_info'
547 if ((base->state < DASD_STATE_READY) ||
548 (dasd_check_blocksize(block->bp_block)))
549 dasd_info->format = DASD_FORMAT_NONE;
551 dasd_info->features |=
552 ((base->features & DASD_FEATURE_READONLY) != 0);
554 memcpy(dasd_info->type, base->discipline->name, 4);
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);
563 static int dasd_ioctl_information(struct dasd_block *block, void __user *argp,
566 struct dasd_information2_t *dasd_info;
569 dasd_info = kzalloc(sizeof(*dasd_info), GFP_KERNEL);
573 error = __dasd_ioctl_information(block, dasd_info);
574 if (!error && copy_to_user(argp, dasd_info, copy_size))
583 int dasd_set_read_only(struct block_device *bdev, bool ro)
585 struct dasd_device *base;
588 /* do not manipulate hardware state for partitions */
589 if (bdev_is_partition(bdev))
592 base = dasd_device_from_gendisk(bdev->bd_disk);
595 if (!ro && test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
598 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, ro);
599 dasd_put_device(base);
603 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
604 struct cmbdata __user *argp)
606 size_t size = _IOC_SIZE(cmd);
610 ret = cmf_readall(block->base->cdev, &data);
611 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
616 int dasd_ioctl(struct block_device *bdev, blk_mode_t mode,
617 unsigned int cmd, unsigned long arg)
619 struct dasd_block *block;
620 struct dasd_device *base;
624 if (is_compat_task())
625 argp = compat_ptr(arg);
627 argp = (void __user *)arg;
629 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg)
632 base = dasd_device_from_gendisk(bdev->bd_disk);
639 rc = dasd_ioctl_disable(bdev);
642 rc = dasd_ioctl_enable(bdev);
645 rc = dasd_ioctl_quiesce(block);
648 rc = dasd_ioctl_resume(block);
651 rc = dasd_ioctl_abortio(block);
654 rc = dasd_ioctl_allowio(block);
657 rc = dasd_ioctl_format(bdev, argp);
659 case BIODASDCHECKFMT:
660 rc = dasd_ioctl_check_format(bdev, argp);
663 rc = dasd_ioctl_information(block, argp,
664 sizeof(struct dasd_information_t));
667 rc = dasd_ioctl_information(block, argp,
668 sizeof(struct dasd_information2_t));
671 rc = dasd_ioctl_read_profile(block, argp);
674 rc = dasd_ioctl_reset_profile(block);
677 rc = dasd_ioctl_api_version(argp);
679 case BIODASDCMFENABLE:
680 rc = enable_cmf(base->cdev);
682 case BIODASDCMFDISABLE:
683 rc = disable_cmf(base->cdev);
685 case BIODASDREADALLCMB:
686 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
689 rc = dasd_ioctl_release_space(bdev, argp);
691 case BIODASDCOPYPAIRSWAP:
692 rc = dasd_ioctl_copy_pair_swap(bdev, argp);
695 /* if the discipline has an ioctl method try it. */
697 if (base->discipline->ioctl)
698 rc = base->discipline->ioctl(block, cmd, argp);
700 dasd_put_device(base);
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
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.
717 * %0 on success and a negative error value on failure.
719 int dasd_biodasdinfo(struct gendisk *disk, struct dasd_information2_t *info)
721 struct dasd_device *base;
724 if (disk->fops != &dasd_device_operations)
727 base = dasd_device_from_gendisk(disk);
730 error = __dasd_ioctl_information(base->block, info);
731 dasd_put_device(base);
734 /* export that symbol_get in partition detection is possible */
735 EXPORT_SYMBOL_GPL(dasd_biodasdinfo);