Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / s390 / block / dasd_ioctl.c
blob9f64244089465537b8ab127f5d3d866c7d699a13
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
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>
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>
27 /* This is ugly... */
28 #define PRINTK_HEADER "dasd_ioctl:"
30 #include "dasd_int.h"
33 static int
34 dasd_ioctl_api_version(void __user *argp)
36 int ver = DASD_API_VERSION;
37 return put_user(ver, (int __user *)argp);
41 * Enable device.
42 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
44 static int
45 dasd_ioctl_enable(struct block_device *bdev)
47 struct dasd_device *base;
49 if (!capable(CAP_SYS_ADMIN))
50 return -EACCES;
52 base = dasd_device_from_gendisk(bdev->bd_disk);
53 if (!base)
54 return -ENODEV;
56 dasd_enable_device(base);
57 dasd_put_device(base);
58 return 0;
62 * Disable device.
63 * Used by dasdfmt. Disable I/O operations but allow ioctls.
65 static int
66 dasd_ioctl_disable(struct block_device *bdev)
68 struct dasd_device *base;
70 if (!capable(CAP_SYS_ADMIN))
71 return -EACCES;
73 base = dasd_device_from_gendisk(bdev->bd_disk);
74 if (!base)
75 return -ENODEV;
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
87 * value.
89 set_capacity(bdev->bd_disk, 0);
90 dasd_put_device(base);
91 return 0;
95 * Quiesce device.
97 static int dasd_ioctl_quiesce(struct dasd_block *block)
99 unsigned long flags;
100 struct dasd_device *base;
102 base = block->base;
103 if (!capable (CAP_SYS_ADMIN))
104 return -EACCES;
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;
116 * Resume device.
118 static int dasd_ioctl_resume(struct dasd_block *block)
120 unsigned long flags;
121 struct dasd_device *base;
123 base = block->base;
124 if (!capable (CAP_SYS_ADMIN))
125 return -EACCES;
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 return 0;
138 * Abort all failfast I/O on a device.
140 static int dasd_ioctl_abortio(struct dasd_block *block)
142 unsigned long flags;
143 struct dasd_device *base;
144 struct dasd_ccw_req *cqr, *n;
146 base = block->base;
147 if (!capable(CAP_SYS_ADMIN))
148 return -EACCES;
150 if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags))
151 return 0;
152 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set");
154 spin_lock_irqsave(&block->request_queue_lock, flags);
155 spin_lock(&block->queue_lock);
156 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
157 if (test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
158 cqr->callback_data &&
159 cqr->callback_data != DASD_SLEEPON_START_TAG &&
160 cqr->callback_data != DASD_SLEEPON_END_TAG) {
161 spin_unlock(&block->queue_lock);
162 blk_abort_request(cqr->callback_data);
163 spin_lock(&block->queue_lock);
166 spin_unlock(&block->queue_lock);
167 spin_unlock_irqrestore(&block->request_queue_lock, flags);
169 dasd_schedule_block_bh(block);
170 return 0;
174 * Allow I/O on a device
176 static int dasd_ioctl_allowio(struct dasd_block *block)
178 struct dasd_device *base;
180 base = block->base;
181 if (!capable(CAP_SYS_ADMIN))
182 return -EACCES;
184 if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags))
185 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset");
187 return 0;
191 * performs formatting of _device_ according to _fdata_
192 * Note: The discipline's format_function is assumed to deliver formatting
193 * commands to format multiple units of the device. In terms of the ECKD
194 * devices this means CCWs are generated to format multiple tracks.
196 static int
197 dasd_format(struct dasd_block *block, struct format_data_t *fdata)
199 struct dasd_device *base;
200 int rc;
202 base = block->base;
203 if (base->discipline->format_device == NULL)
204 return -EPERM;
206 if (base->state != DASD_STATE_BASIC) {
207 pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
208 dev_name(&base->cdev->dev));
209 return -EBUSY;
212 DBF_DEV_EVENT(DBF_NOTICE, base,
213 "formatting units %u to %u (%u B blocks) flags %u",
214 fdata->start_unit,
215 fdata->stop_unit, fdata->blksize, fdata->intensity);
217 /* Since dasdfmt keeps the device open after it was disabled,
218 * there still exists an inode for this device.
219 * We must update i_blkbits, otherwise we might get errors when
220 * enabling the device later.
222 if (fdata->start_unit == 0) {
223 block->gdp->part0->bd_inode->i_blkbits =
224 blksize_bits(fdata->blksize);
227 rc = base->discipline->format_device(base, fdata, 1);
228 if (rc == -EAGAIN)
229 rc = base->discipline->format_device(base, fdata, 0);
231 return rc;
234 static int dasd_check_format(struct dasd_block *block,
235 struct format_check_t *cdata)
237 struct dasd_device *base;
238 int rc;
240 base = block->base;
241 if (!base->discipline->check_device_format)
242 return -ENOTTY;
244 rc = base->discipline->check_device_format(base, cdata, 1);
245 if (rc == -EAGAIN)
246 rc = base->discipline->check_device_format(base, cdata, 0);
248 return rc;
252 * Format device.
254 static int
255 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
257 struct dasd_device *base;
258 struct format_data_t fdata;
259 int rc;
261 if (!capable(CAP_SYS_ADMIN))
262 return -EACCES;
263 if (!argp)
264 return -EINVAL;
265 base = dasd_device_from_gendisk(bdev->bd_disk);
266 if (!base)
267 return -ENODEV;
268 if (base->features & DASD_FEATURE_READONLY ||
269 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
270 dasd_put_device(base);
271 return -EROFS;
273 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
274 dasd_put_device(base);
275 return -EFAULT;
277 if (bdev_is_partition(bdev)) {
278 pr_warn("%s: The specified DASD is a partition and cannot be formatted\n",
279 dev_name(&base->cdev->dev));
280 dasd_put_device(base);
281 return -EINVAL;
283 rc = dasd_format(base->block, &fdata);
284 dasd_put_device(base);
286 return rc;
290 * Check device format
292 static int dasd_ioctl_check_format(struct block_device *bdev, void __user *argp)
294 struct format_check_t cdata;
295 struct dasd_device *base;
296 int rc = 0;
298 if (!argp)
299 return -EINVAL;
301 base = dasd_device_from_gendisk(bdev->bd_disk);
302 if (!base)
303 return -ENODEV;
304 if (bdev_is_partition(bdev)) {
305 pr_warn("%s: The specified DASD is a partition and cannot be checked\n",
306 dev_name(&base->cdev->dev));
307 rc = -EINVAL;
308 goto out_err;
311 if (copy_from_user(&cdata, argp, sizeof(cdata))) {
312 rc = -EFAULT;
313 goto out_err;
316 rc = dasd_check_format(base->block, &cdata);
317 if (rc)
318 goto out_err;
320 if (copy_to_user(argp, &cdata, sizeof(cdata)))
321 rc = -EFAULT;
323 out_err:
324 dasd_put_device(base);
326 return rc;
329 static int dasd_release_space(struct dasd_device *device,
330 struct format_data_t *rdata)
332 if (!device->discipline->is_ese && !device->discipline->is_ese(device))
333 return -ENOTSUPP;
334 if (!device->discipline->release_space)
335 return -ENOTSUPP;
337 return device->discipline->release_space(device, rdata);
341 * Release allocated space
343 static int dasd_ioctl_release_space(struct block_device *bdev, void __user *argp)
345 struct format_data_t rdata;
346 struct dasd_device *base;
347 int rc = 0;
349 if (!capable(CAP_SYS_ADMIN))
350 return -EACCES;
351 if (!argp)
352 return -EINVAL;
354 base = dasd_device_from_gendisk(bdev->bd_disk);
355 if (!base)
356 return -ENODEV;
357 if (base->features & DASD_FEATURE_READONLY ||
358 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
359 rc = -EROFS;
360 goto out_err;
362 if (bdev_is_partition(bdev)) {
363 pr_warn("%s: The specified DASD is a partition and tracks cannot be released\n",
364 dev_name(&base->cdev->dev));
365 rc = -EINVAL;
366 goto out_err;
369 if (copy_from_user(&rdata, argp, sizeof(rdata))) {
370 rc = -EFAULT;
371 goto out_err;
374 rc = dasd_release_space(base, &rdata);
376 out_err:
377 dasd_put_device(base);
379 return rc;
382 #ifdef CONFIG_DASD_PROFILE
384 * Reset device profile information
386 static int dasd_ioctl_reset_profile(struct dasd_block *block)
388 dasd_profile_reset(&block->profile);
389 return 0;
393 * Return device profile information
395 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
397 struct dasd_profile_info_t *data;
398 int rc = 0;
400 data = kmalloc(sizeof(*data), GFP_KERNEL);
401 if (!data)
402 return -ENOMEM;
404 spin_lock_bh(&block->profile.lock);
405 if (block->profile.data) {
406 data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
407 data->dasd_io_sects = block->profile.data->dasd_io_sects;
408 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
409 sizeof(data->dasd_io_secs));
410 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
411 sizeof(data->dasd_io_times));
412 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
413 sizeof(data->dasd_io_timps));
414 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
415 sizeof(data->dasd_io_time1));
416 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
417 sizeof(data->dasd_io_time2));
418 memcpy(data->dasd_io_time2ps,
419 block->profile.data->dasd_io_time2ps,
420 sizeof(data->dasd_io_time2ps));
421 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
422 sizeof(data->dasd_io_time3));
423 memcpy(data->dasd_io_nr_req,
424 block->profile.data->dasd_io_nr_req,
425 sizeof(data->dasd_io_nr_req));
426 spin_unlock_bh(&block->profile.lock);
427 } else {
428 spin_unlock_bh(&block->profile.lock);
429 rc = -EIO;
430 goto out;
432 if (copy_to_user(argp, data, sizeof(*data)))
433 rc = -EFAULT;
434 out:
435 kfree(data);
436 return rc;
438 #else
439 static int dasd_ioctl_reset_profile(struct dasd_block *block)
441 return -ENOTTY;
444 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
446 return -ENOTTY;
448 #endif
451 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
453 static int __dasd_ioctl_information(struct dasd_block *block,
454 struct dasd_information2_t *dasd_info)
456 struct subchannel_id sch_id;
457 struct ccw_dev_id dev_id;
458 struct dasd_device *base;
459 struct ccw_device *cdev;
460 struct list_head *l;
461 unsigned long flags;
462 int rc;
464 base = block->base;
465 if (!base->discipline || !base->discipline->fill_info)
466 return -EINVAL;
468 rc = base->discipline->fill_info(base, dasd_info);
469 if (rc)
470 return rc;
472 cdev = base->cdev;
473 ccw_device_get_id(cdev, &dev_id);
474 ccw_device_get_schid(cdev, &sch_id);
476 dasd_info->devno = dev_id.devno;
477 dasd_info->schid = sch_id.sch_no;
478 dasd_info->cu_type = cdev->id.cu_type;
479 dasd_info->cu_model = cdev->id.cu_model;
480 dasd_info->dev_type = cdev->id.dev_type;
481 dasd_info->dev_model = cdev->id.dev_model;
482 dasd_info->status = base->state;
484 * The open_count is increased for every opener, that includes
485 * the blkdev_get in dasd_scan_partitions.
486 * This must be hidden from user-space.
488 dasd_info->open_count = atomic_read(&block->open_count);
489 if (!block->bdev)
490 dasd_info->open_count++;
493 * check if device is really formatted
494 * LDL / CDL was returned by 'fill_info'
496 if ((base->state < DASD_STATE_READY) ||
497 (dasd_check_blocksize(block->bp_block)))
498 dasd_info->format = DASD_FORMAT_NONE;
500 dasd_info->features |=
501 ((base->features & DASD_FEATURE_READONLY) != 0);
503 memcpy(dasd_info->type, base->discipline->name, 4);
505 spin_lock_irqsave(&block->queue_lock, flags);
506 list_for_each(l, &base->ccw_queue)
507 dasd_info->chanq_len++;
508 spin_unlock_irqrestore(&block->queue_lock, flags);
509 return 0;
512 static int dasd_ioctl_information(struct dasd_block *block, void __user *argp,
513 size_t copy_size)
515 struct dasd_information2_t *dasd_info;
516 int error;
518 dasd_info = kzalloc(sizeof(*dasd_info), GFP_KERNEL);
519 if (!dasd_info)
520 return -ENOMEM;
522 error = __dasd_ioctl_information(block, dasd_info);
523 if (!error && copy_to_user(argp, dasd_info, copy_size))
524 error = -EFAULT;
525 kfree(dasd_info);
526 return error;
530 * Set read only
532 int dasd_set_read_only(struct block_device *bdev, bool ro)
534 struct dasd_device *base;
535 int rc;
537 /* do not manipulate hardware state for partitions */
538 if (bdev_is_partition(bdev))
539 return 0;
541 base = dasd_device_from_gendisk(bdev->bd_disk);
542 if (!base)
543 return -ENODEV;
544 if (!ro && test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
545 rc = -EROFS;
546 else
547 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, ro);
548 dasd_put_device(base);
549 return rc;
552 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
553 struct cmbdata __user *argp)
555 size_t size = _IOC_SIZE(cmd);
556 struct cmbdata data;
557 int ret;
559 ret = cmf_readall(block->base->cdev, &data);
560 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
561 return -EFAULT;
562 return ret;
565 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
566 unsigned int cmd, unsigned long arg)
568 struct dasd_block *block;
569 struct dasd_device *base;
570 void __user *argp;
571 int rc;
573 if (is_compat_task())
574 argp = compat_ptr(arg);
575 else
576 argp = (void __user *)arg;
578 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
579 PRINT_DEBUG("empty data ptr");
580 return -EINVAL;
583 base = dasd_device_from_gendisk(bdev->bd_disk);
584 if (!base)
585 return -ENODEV;
586 block = base->block;
587 rc = 0;
588 switch (cmd) {
589 case BIODASDDISABLE:
590 rc = dasd_ioctl_disable(bdev);
591 break;
592 case BIODASDENABLE:
593 rc = dasd_ioctl_enable(bdev);
594 break;
595 case BIODASDQUIESCE:
596 rc = dasd_ioctl_quiesce(block);
597 break;
598 case BIODASDRESUME:
599 rc = dasd_ioctl_resume(block);
600 break;
601 case BIODASDABORTIO:
602 rc = dasd_ioctl_abortio(block);
603 break;
604 case BIODASDALLOWIO:
605 rc = dasd_ioctl_allowio(block);
606 break;
607 case BIODASDFMT:
608 rc = dasd_ioctl_format(bdev, argp);
609 break;
610 case BIODASDCHECKFMT:
611 rc = dasd_ioctl_check_format(bdev, argp);
612 break;
613 case BIODASDINFO:
614 rc = dasd_ioctl_information(block, argp,
615 sizeof(struct dasd_information_t));
616 break;
617 case BIODASDINFO2:
618 rc = dasd_ioctl_information(block, argp,
619 sizeof(struct dasd_information2_t));
620 break;
621 case BIODASDPRRD:
622 rc = dasd_ioctl_read_profile(block, argp);
623 break;
624 case BIODASDPRRST:
625 rc = dasd_ioctl_reset_profile(block);
626 break;
627 case DASDAPIVER:
628 rc = dasd_ioctl_api_version(argp);
629 break;
630 case BIODASDCMFENABLE:
631 rc = enable_cmf(base->cdev);
632 break;
633 case BIODASDCMFDISABLE:
634 rc = disable_cmf(base->cdev);
635 break;
636 case BIODASDREADALLCMB:
637 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
638 break;
639 case BIODASDRAS:
640 rc = dasd_ioctl_release_space(bdev, argp);
641 break;
642 default:
643 /* if the discipline has an ioctl method try it. */
644 rc = -ENOTTY;
645 if (base->discipline->ioctl)
646 rc = base->discipline->ioctl(block, cmd, argp);
648 dasd_put_device(base);
649 return rc;
654 * dasd_biodasdinfo() - fill out the dasd information structure
655 * @disk [in]: pointer to gendisk structure that references a DASD
656 * @info [out]: pointer to the dasd_information2_t structure
658 * Provide access to DASD specific information.
659 * The gendisk structure is checked if it belongs to the DASD driver by
660 * comparing the gendisk->fops pointer.
661 * If it does not belong to the DASD driver -EINVAL is returned.
662 * Otherwise the provided dasd_information2_t structure is filled out.
664 * Returns:
665 * %0 on success and a negative error value on failure.
667 int dasd_biodasdinfo(struct gendisk *disk, struct dasd_information2_t *info)
669 struct dasd_device *base;
670 int error;
672 if (disk->fops != &dasd_device_operations)
673 return -EINVAL;
675 base = dasd_device_from_gendisk(disk);
676 if (!base)
677 return -ENODEV;
678 error = __dasd_ioctl_information(base->block, info);
679 dasd_put_device(base);
680 return error;
682 /* export that symbol_get in partition detection is possible */
683 EXPORT_SYMBOL_GPL(dasd_biodasdinfo);