xref: /linux/drivers/s390/block/dasd_ioctl.c (revision 0be3ff0c)
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 	dasd_put_device(base);
58 	return 0;
59 }
60 
61 /*
62  * Disable device.
63  * Used by dasdfmt. Disable I/O operations but allow ioctls.
64  */
65 static int
66 dasd_ioctl_disable(struct block_device *bdev)
67 {
68 	struct dasd_device *base;
69 
70 	if (!capable(CAP_SYS_ADMIN))
71 		return -EACCES;
72 
73 	base = dasd_device_from_gendisk(bdev->bd_disk);
74 	if (!base)
75 		return -ENODEV;
76 	/*
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.
83 	 */
84 	dasd_set_target_state(base, DASD_STATE_BASIC);
85 	/*
86 	 * Set i_size to zero, since read, write, etc. check against this
87 	 * value.
88 	 */
89 	set_capacity(bdev->bd_disk, 0);
90 	dasd_put_device(base);
91 	return 0;
92 }
93 
94 /*
95  * Quiesce device.
96  */
97 static int dasd_ioctl_quiesce(struct dasd_block *block)
98 {
99 	unsigned long flags;
100 	struct dasd_device *base;
101 
102 	base = block->base;
103 	if (!capable (CAP_SYS_ADMIN))
104 		return -EACCES;
105 
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;
112 }
113 
114 
115 /*
116  * Resume device.
117  */
118 static int dasd_ioctl_resume(struct dasd_block *block)
119 {
120 	unsigned long flags;
121 	struct dasd_device *base;
122 
123 	base = block->base;
124 	if (!capable (CAP_SYS_ADMIN))
125 		return -EACCES;
126 
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);
132 
133 	dasd_schedule_block_bh(block);
134 	return 0;
135 }
136 
137 /*
138  * Abort all failfast I/O on a device.
139  */
140 static int dasd_ioctl_abortio(struct dasd_block *block)
141 {
142 	unsigned long flags;
143 	struct dasd_device *base;
144 	struct dasd_ccw_req *cqr, *n;
145 
146 	base = block->base;
147 	if (!capable(CAP_SYS_ADMIN))
148 		return -EACCES;
149 
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");
153 
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);
164 		}
165 	}
166 	spin_unlock(&block->queue_lock);
167 	spin_unlock_irqrestore(&block->request_queue_lock, flags);
168 
169 	dasd_schedule_block_bh(block);
170 	return 0;
171 }
172 
173 /*
174  * Allow I/O on a device
175  */
176 static int dasd_ioctl_allowio(struct dasd_block *block)
177 {
178 	struct dasd_device *base;
179 
180 	base = block->base;
181 	if (!capable(CAP_SYS_ADMIN))
182 		return -EACCES;
183 
184 	if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags))
185 		DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset");
186 
187 	return 0;
188 }
189 
190 /*
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.
195  */
196 static int
197 dasd_format(struct dasd_block *block, struct format_data_t *fdata)
198 {
199 	struct dasd_device *base;
200 	int rc;
201 
202 	base = block->base;
203 	if (base->discipline->format_device == NULL)
204 		return -EPERM;
205 
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;
210 	}
211 
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);
216 
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.
221 	 */
222 	if (fdata->start_unit == 0) {
223 		block->gdp->part0->bd_inode->i_blkbits =
224 			blksize_bits(fdata->blksize);
225 	}
226 
227 	rc = base->discipline->format_device(base, fdata, 1);
228 	if (rc == -EAGAIN)
229 		rc = base->discipline->format_device(base, fdata, 0);
230 
231 	return rc;
232 }
233 
234 static int dasd_check_format(struct dasd_block *block,
235 			     struct format_check_t *cdata)
236 {
237 	struct dasd_device *base;
238 	int rc;
239 
240 	base = block->base;
241 	if (!base->discipline->check_device_format)
242 		return -ENOTTY;
243 
244 	rc = base->discipline->check_device_format(base, cdata, 1);
245 	if (rc == -EAGAIN)
246 		rc = base->discipline->check_device_format(base, cdata, 0);
247 
248 	return rc;
249 }
250 
251 /*
252  * Format device.
253  */
254 static int
255 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
256 {
257 	struct dasd_device *base;
258 	struct format_data_t fdata;
259 	int rc;
260 
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;
272 	}
273 	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
274 		dasd_put_device(base);
275 		return -EFAULT;
276 	}
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;
282 	}
283 	rc = dasd_format(base->block, &fdata);
284 	dasd_put_device(base);
285 
286 	return rc;
287 }
288 
289 /*
290  * Check device format
291  */
292 static int dasd_ioctl_check_format(struct block_device *bdev, void __user *argp)
293 {
294 	struct format_check_t cdata;
295 	struct dasd_device *base;
296 	int rc = 0;
297 
298 	if (!argp)
299 		return -EINVAL;
300 
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;
309 	}
310 
311 	if (copy_from_user(&cdata, argp, sizeof(cdata))) {
312 		rc = -EFAULT;
313 		goto out_err;
314 	}
315 
316 	rc = dasd_check_format(base->block, &cdata);
317 	if (rc)
318 		goto out_err;
319 
320 	if (copy_to_user(argp, &cdata, sizeof(cdata)))
321 		rc = -EFAULT;
322 
323 out_err:
324 	dasd_put_device(base);
325 
326 	return rc;
327 }
328 
329 static int dasd_release_space(struct dasd_device *device,
330 			      struct format_data_t *rdata)
331 {
332 	if (!device->discipline->is_ese && !device->discipline->is_ese(device))
333 		return -ENOTSUPP;
334 	if (!device->discipline->release_space)
335 		return -ENOTSUPP;
336 
337 	return device->discipline->release_space(device, rdata);
338 }
339 
340 /*
341  * Release allocated space
342  */
343 static int dasd_ioctl_release_space(struct block_device *bdev, void __user *argp)
344 {
345 	struct format_data_t rdata;
346 	struct dasd_device *base;
347 	int rc = 0;
348 
349 	if (!capable(CAP_SYS_ADMIN))
350 		return -EACCES;
351 	if (!argp)
352 		return -EINVAL;
353 
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;
361 	}
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;
367 	}
368 
369 	if (copy_from_user(&rdata, argp, sizeof(rdata))) {
370 		rc = -EFAULT;
371 		goto out_err;
372 	}
373 
374 	rc = dasd_release_space(base, &rdata);
375 
376 out_err:
377 	dasd_put_device(base);
378 
379 	return rc;
380 }
381 
382 #ifdef CONFIG_DASD_PROFILE
383 /*
384  * Reset device profile information
385  */
386 static int dasd_ioctl_reset_profile(struct dasd_block *block)
387 {
388 	dasd_profile_reset(&block->profile);
389 	return 0;
390 }
391 
392 /*
393  * Return device profile information
394  */
395 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
396 {
397 	struct dasd_profile_info_t *data;
398 	int rc = 0;
399 
400 	data = kmalloc(sizeof(*data), GFP_KERNEL);
401 	if (!data)
402 		return -ENOMEM;
403 
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;
431 	}
432 	if (copy_to_user(argp, data, sizeof(*data)))
433 		rc = -EFAULT;
434 out:
435 	kfree(data);
436 	return rc;
437 }
438 #else
439 static int dasd_ioctl_reset_profile(struct dasd_block *block)
440 {
441 	return -ENOTTY;
442 }
443 
444 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
445 {
446 	return -ENOTTY;
447 }
448 #endif
449 
450 /*
451  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
452  */
453 static int __dasd_ioctl_information(struct dasd_block *block,
454 		struct dasd_information2_t *dasd_info)
455 {
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;
463 
464 	base = block->base;
465 	if (!base->discipline || !base->discipline->fill_info)
466 		return -EINVAL;
467 
468 	rc = base->discipline->fill_info(base, dasd_info);
469 	if (rc)
470 		return rc;
471 
472 	cdev = base->cdev;
473 	ccw_device_get_id(cdev, &dev_id);
474 	ccw_device_get_schid(cdev, &sch_id);
475 
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;
483 	/*
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.
487 	 */
488 	dasd_info->open_count = atomic_read(&block->open_count);
489 	if (!block->bdev)
490 		dasd_info->open_count++;
491 
492 	/*
493 	 * check if device is really formatted
494 	 * LDL / CDL was returned by 'fill_info'
495 	 */
496 	if ((base->state < DASD_STATE_READY) ||
497 	    (dasd_check_blocksize(block->bp_block)))
498 		dasd_info->format = DASD_FORMAT_NONE;
499 
500 	dasd_info->features |=
501 		((base->features & DASD_FEATURE_READONLY) != 0);
502 
503 	memcpy(dasd_info->type, base->discipline->name, 4);
504 
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;
510 }
511 
512 static int dasd_ioctl_information(struct dasd_block *block, void __user *argp,
513 		size_t copy_size)
514 {
515 	struct dasd_information2_t *dasd_info;
516 	int error;
517 
518 	dasd_info = kzalloc(sizeof(*dasd_info), GFP_KERNEL);
519 	if (!dasd_info)
520 		return -ENOMEM;
521 
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;
527 }
528 
529 /*
530  * Set read only
531  */
532 int dasd_set_read_only(struct block_device *bdev, bool ro)
533 {
534 	struct dasd_device *base;
535 	int rc;
536 
537 	/* do not manipulate hardware state for partitions */
538 	if (bdev_is_partition(bdev))
539 		return 0;
540 
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;
550 }
551 
552 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
553 				  struct cmbdata __user *argp)
554 {
555 	size_t size = _IOC_SIZE(cmd);
556 	struct cmbdata data;
557 	int ret;
558 
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;
563 }
564 
565 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
566 	       unsigned int cmd, unsigned long arg)
567 {
568 	struct dasd_block *block;
569 	struct dasd_device *base;
570 	void __user *argp;
571 	int rc;
572 
573 	if (is_compat_task())
574 		argp = compat_ptr(arg);
575 	else
576 		argp = (void __user *)arg;
577 
578 	if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg)
579 		return -EINVAL;
580 
581 	base = dasd_device_from_gendisk(bdev->bd_disk);
582 	if (!base)
583 		return -ENODEV;
584 	block = base->block;
585 	rc = 0;
586 	switch (cmd) {
587 	case BIODASDDISABLE:
588 		rc = dasd_ioctl_disable(bdev);
589 		break;
590 	case BIODASDENABLE:
591 		rc = dasd_ioctl_enable(bdev);
592 		break;
593 	case BIODASDQUIESCE:
594 		rc = dasd_ioctl_quiesce(block);
595 		break;
596 	case BIODASDRESUME:
597 		rc = dasd_ioctl_resume(block);
598 		break;
599 	case BIODASDABORTIO:
600 		rc = dasd_ioctl_abortio(block);
601 		break;
602 	case BIODASDALLOWIO:
603 		rc = dasd_ioctl_allowio(block);
604 		break;
605 	case BIODASDFMT:
606 		rc = dasd_ioctl_format(bdev, argp);
607 		break;
608 	case BIODASDCHECKFMT:
609 		rc = dasd_ioctl_check_format(bdev, argp);
610 		break;
611 	case BIODASDINFO:
612 		rc = dasd_ioctl_information(block, argp,
613 				sizeof(struct dasd_information_t));
614 		break;
615 	case BIODASDINFO2:
616 		rc = dasd_ioctl_information(block, argp,
617 				sizeof(struct dasd_information2_t));
618 		break;
619 	case BIODASDPRRD:
620 		rc = dasd_ioctl_read_profile(block, argp);
621 		break;
622 	case BIODASDPRRST:
623 		rc = dasd_ioctl_reset_profile(block);
624 		break;
625 	case DASDAPIVER:
626 		rc = dasd_ioctl_api_version(argp);
627 		break;
628 	case BIODASDCMFENABLE:
629 		rc = enable_cmf(base->cdev);
630 		break;
631 	case BIODASDCMFDISABLE:
632 		rc = disable_cmf(base->cdev);
633 		break;
634 	case BIODASDREADALLCMB:
635 		rc = dasd_ioctl_readall_cmb(block, cmd, argp);
636 		break;
637 	case BIODASDRAS:
638 		rc = dasd_ioctl_release_space(bdev, argp);
639 		break;
640 	default:
641 		/* if the discipline has an ioctl method try it. */
642 		rc = -ENOTTY;
643 		if (base->discipline->ioctl)
644 			rc = base->discipline->ioctl(block, cmd, argp);
645 	}
646 	dasd_put_device(base);
647 	return rc;
648 }
649 
650 
651 /**
652  * dasd_biodasdinfo() - fill out the dasd information structure
653  * @disk: [in] pointer to gendisk structure that references a DASD
654  * @info: [out] pointer to the dasd_information2_t structure
655  *
656  * Provide access to DASD specific information.
657  * The gendisk structure is checked if it belongs to the DASD driver by
658  * comparing the gendisk->fops pointer.
659  * If it does not belong to the DASD driver -EINVAL is returned.
660  * Otherwise the provided dasd_information2_t structure is filled out.
661  *
662  * Returns:
663  *   %0 on success and a negative error value on failure.
664  */
665 int dasd_biodasdinfo(struct gendisk *disk, struct dasd_information2_t *info)
666 {
667 	struct dasd_device *base;
668 	int error;
669 
670 	if (disk->fops != &dasd_device_operations)
671 		return -EINVAL;
672 
673 	base = dasd_device_from_gendisk(disk);
674 	if (!base)
675 		return -ENODEV;
676 	error = __dasd_ioctl_information(base->block, info);
677 	dasd_put_device(base);
678 	return error;
679 }
680 /* export that symbol_get in partition detection is possible */
681 EXPORT_SYMBOL_GPL(dasd_biodasdinfo);
682