xref: /openbmc/linux/drivers/s390/block/dasd_ioctl.c (revision 11a163f2)
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 	/* Formatting the dasd device can change the capacity. */
58 	bd_set_nr_sectors(bdev, get_capacity(base->block->gdp));
59 	dasd_put_device(base);
60 	return 0;
61 }
62 
63 /*
64  * Disable device.
65  * Used by dasdfmt. Disable I/O operations but allow ioctls.
66  */
67 static int
68 dasd_ioctl_disable(struct block_device *bdev)
69 {
70 	struct dasd_device *base;
71 
72 	if (!capable(CAP_SYS_ADMIN))
73 		return -EACCES;
74 
75 	base = dasd_device_from_gendisk(bdev->bd_disk);
76 	if (!base)
77 		return -ENODEV;
78 	/*
79 	 * Man this is sick. We don't do a real disable but only downgrade
80 	 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
81 	 * BIODASDDISABLE to disable accesses to the device via the block
82 	 * device layer but it still wants to do i/o on the device by
83 	 * using the BIODASDFMT ioctl. Therefore the correct state for the
84 	 * device is DASD_STATE_BASIC that allows to do basic i/o.
85 	 */
86 	dasd_set_target_state(base, DASD_STATE_BASIC);
87 	/*
88 	 * Set i_size to zero, since read, write, etc. check against this
89 	 * value.
90 	 */
91 	bd_set_nr_sectors(bdev, 0);
92 	dasd_put_device(base);
93 	return 0;
94 }
95 
96 /*
97  * Quiesce device.
98  */
99 static int dasd_ioctl_quiesce(struct dasd_block *block)
100 {
101 	unsigned long flags;
102 	struct dasd_device *base;
103 
104 	base = block->base;
105 	if (!capable (CAP_SYS_ADMIN))
106 		return -EACCES;
107 
108 	pr_info("%s: The DASD has been put in the quiesce "
109 		"state\n", dev_name(&base->cdev->dev));
110 	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
111 	dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
112 	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
113 	return 0;
114 }
115 
116 
117 /*
118  * Resume device.
119  */
120 static int dasd_ioctl_resume(struct dasd_block *block)
121 {
122 	unsigned long flags;
123 	struct dasd_device *base;
124 
125 	base = block->base;
126 	if (!capable (CAP_SYS_ADMIN))
127 		return -EACCES;
128 
129 	pr_info("%s: I/O operations have been resumed "
130 		"on the DASD\n", dev_name(&base->cdev->dev));
131 	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
132 	dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
133 	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
134 
135 	dasd_schedule_block_bh(block);
136 	return 0;
137 }
138 
139 /*
140  * Abort all failfast I/O on a device.
141  */
142 static int dasd_ioctl_abortio(struct dasd_block *block)
143 {
144 	unsigned long flags;
145 	struct dasd_device *base;
146 	struct dasd_ccw_req *cqr, *n;
147 
148 	base = block->base;
149 	if (!capable(CAP_SYS_ADMIN))
150 		return -EACCES;
151 
152 	if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags))
153 		return 0;
154 	DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set");
155 
156 	spin_lock_irqsave(&block->request_queue_lock, flags);
157 	spin_lock(&block->queue_lock);
158 	list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
159 		if (test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
160 		    cqr->callback_data &&
161 		    cqr->callback_data != DASD_SLEEPON_START_TAG &&
162 		    cqr->callback_data != DASD_SLEEPON_END_TAG) {
163 			spin_unlock(&block->queue_lock);
164 			blk_abort_request(cqr->callback_data);
165 			spin_lock(&block->queue_lock);
166 		}
167 	}
168 	spin_unlock(&block->queue_lock);
169 	spin_unlock_irqrestore(&block->request_queue_lock, flags);
170 
171 	dasd_schedule_block_bh(block);
172 	return 0;
173 }
174 
175 /*
176  * Allow I/O on a device
177  */
178 static int dasd_ioctl_allowio(struct dasd_block *block)
179 {
180 	struct dasd_device *base;
181 
182 	base = block->base;
183 	if (!capable(CAP_SYS_ADMIN))
184 		return -EACCES;
185 
186 	if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags))
187 		DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset");
188 
189 	return 0;
190 }
191 
192 /*
193  * performs formatting of _device_ according to _fdata_
194  * Note: The discipline's format_function is assumed to deliver formatting
195  * commands to format multiple units of the device. In terms of the ECKD
196  * devices this means CCWs are generated to format multiple tracks.
197  */
198 static int
199 dasd_format(struct dasd_block *block, struct format_data_t *fdata)
200 {
201 	struct dasd_device *base;
202 	int rc;
203 
204 	base = block->base;
205 	if (base->discipline->format_device == NULL)
206 		return -EPERM;
207 
208 	if (base->state != DASD_STATE_BASIC) {
209 		pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
210 			dev_name(&base->cdev->dev));
211 		return -EBUSY;
212 	}
213 
214 	DBF_DEV_EVENT(DBF_NOTICE, base,
215 		      "formatting units %u to %u (%u B blocks) flags %u",
216 		      fdata->start_unit,
217 		      fdata->stop_unit, fdata->blksize, fdata->intensity);
218 
219 	/* Since dasdfmt keeps the device open after it was disabled,
220 	 * there still exists an inode for this device.
221 	 * We must update i_blkbits, otherwise we might get errors when
222 	 * enabling the device later.
223 	 */
224 	if (fdata->start_unit == 0) {
225 		struct block_device *bdev = bdget_disk(block->gdp, 0);
226 		bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
227 		bdput(bdev);
228 	}
229 
230 	rc = base->discipline->format_device(base, fdata, 1);
231 	if (rc == -EAGAIN)
232 		rc = base->discipline->format_device(base, fdata, 0);
233 
234 	return rc;
235 }
236 
237 static int dasd_check_format(struct dasd_block *block,
238 			     struct format_check_t *cdata)
239 {
240 	struct dasd_device *base;
241 	int rc;
242 
243 	base = block->base;
244 	if (!base->discipline->check_device_format)
245 		return -ENOTTY;
246 
247 	rc = base->discipline->check_device_format(base, cdata, 1);
248 	if (rc == -EAGAIN)
249 		rc = base->discipline->check_device_format(base, cdata, 0);
250 
251 	return rc;
252 }
253 
254 /*
255  * Format device.
256  */
257 static int
258 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
259 {
260 	struct dasd_device *base;
261 	struct format_data_t fdata;
262 	int rc;
263 
264 	if (!capable(CAP_SYS_ADMIN))
265 		return -EACCES;
266 	if (!argp)
267 		return -EINVAL;
268 	base = dasd_device_from_gendisk(bdev->bd_disk);
269 	if (!base)
270 		return -ENODEV;
271 	if (base->features & DASD_FEATURE_READONLY ||
272 	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
273 		dasd_put_device(base);
274 		return -EROFS;
275 	}
276 	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
277 		dasd_put_device(base);
278 		return -EFAULT;
279 	}
280 	if (bdev_is_partition(bdev)) {
281 		pr_warn("%s: The specified DASD is a partition and cannot be formatted\n",
282 			dev_name(&base->cdev->dev));
283 		dasd_put_device(base);
284 		return -EINVAL;
285 	}
286 	rc = dasd_format(base->block, &fdata);
287 	dasd_put_device(base);
288 
289 	return rc;
290 }
291 
292 /*
293  * Check device format
294  */
295 static int dasd_ioctl_check_format(struct block_device *bdev, void __user *argp)
296 {
297 	struct format_check_t cdata;
298 	struct dasd_device *base;
299 	int rc = 0;
300 
301 	if (!argp)
302 		return -EINVAL;
303 
304 	base = dasd_device_from_gendisk(bdev->bd_disk);
305 	if (!base)
306 		return -ENODEV;
307 	if (bdev_is_partition(bdev)) {
308 		pr_warn("%s: The specified DASD is a partition and cannot be checked\n",
309 			dev_name(&base->cdev->dev));
310 		rc = -EINVAL;
311 		goto out_err;
312 	}
313 
314 	if (copy_from_user(&cdata, argp, sizeof(cdata))) {
315 		rc = -EFAULT;
316 		goto out_err;
317 	}
318 
319 	rc = dasd_check_format(base->block, &cdata);
320 	if (rc)
321 		goto out_err;
322 
323 	if (copy_to_user(argp, &cdata, sizeof(cdata)))
324 		rc = -EFAULT;
325 
326 out_err:
327 	dasd_put_device(base);
328 
329 	return rc;
330 }
331 
332 static int dasd_release_space(struct dasd_device *device,
333 			      struct format_data_t *rdata)
334 {
335 	if (!device->discipline->is_ese && !device->discipline->is_ese(device))
336 		return -ENOTSUPP;
337 	if (!device->discipline->release_space)
338 		return -ENOTSUPP;
339 
340 	return device->discipline->release_space(device, rdata);
341 }
342 
343 /*
344  * Release allocated space
345  */
346 static int dasd_ioctl_release_space(struct block_device *bdev, void __user *argp)
347 {
348 	struct format_data_t rdata;
349 	struct dasd_device *base;
350 	int rc = 0;
351 
352 	if (!capable(CAP_SYS_ADMIN))
353 		return -EACCES;
354 	if (!argp)
355 		return -EINVAL;
356 
357 	base = dasd_device_from_gendisk(bdev->bd_disk);
358 	if (!base)
359 		return -ENODEV;
360 	if (base->features & DASD_FEATURE_READONLY ||
361 	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
362 		rc = -EROFS;
363 		goto out_err;
364 	}
365 	if (bdev_is_partition(bdev)) {
366 		pr_warn("%s: The specified DASD is a partition and tracks cannot be released\n",
367 			dev_name(&base->cdev->dev));
368 		rc = -EINVAL;
369 		goto out_err;
370 	}
371 
372 	if (copy_from_user(&rdata, argp, sizeof(rdata))) {
373 		rc = -EFAULT;
374 		goto out_err;
375 	}
376 
377 	rc = dasd_release_space(base, &rdata);
378 
379 out_err:
380 	dasd_put_device(base);
381 
382 	return rc;
383 }
384 
385 #ifdef CONFIG_DASD_PROFILE
386 /*
387  * Reset device profile information
388  */
389 static int dasd_ioctl_reset_profile(struct dasd_block *block)
390 {
391 	dasd_profile_reset(&block->profile);
392 	return 0;
393 }
394 
395 /*
396  * Return device profile information
397  */
398 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
399 {
400 	struct dasd_profile_info_t *data;
401 	int rc = 0;
402 
403 	data = kmalloc(sizeof(*data), GFP_KERNEL);
404 	if (!data)
405 		return -ENOMEM;
406 
407 	spin_lock_bh(&block->profile.lock);
408 	if (block->profile.data) {
409 		data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
410 		data->dasd_io_sects = block->profile.data->dasd_io_sects;
411 		memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
412 		       sizeof(data->dasd_io_secs));
413 		memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
414 		       sizeof(data->dasd_io_times));
415 		memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
416 		       sizeof(data->dasd_io_timps));
417 		memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
418 		       sizeof(data->dasd_io_time1));
419 		memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
420 		       sizeof(data->dasd_io_time2));
421 		memcpy(data->dasd_io_time2ps,
422 		       block->profile.data->dasd_io_time2ps,
423 		       sizeof(data->dasd_io_time2ps));
424 		memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
425 		       sizeof(data->dasd_io_time3));
426 		memcpy(data->dasd_io_nr_req,
427 		       block->profile.data->dasd_io_nr_req,
428 		       sizeof(data->dasd_io_nr_req));
429 		spin_unlock_bh(&block->profile.lock);
430 	} else {
431 		spin_unlock_bh(&block->profile.lock);
432 		rc = -EIO;
433 		goto out;
434 	}
435 	if (copy_to_user(argp, data, sizeof(*data)))
436 		rc = -EFAULT;
437 out:
438 	kfree(data);
439 	return rc;
440 }
441 #else
442 static int dasd_ioctl_reset_profile(struct dasd_block *block)
443 {
444 	return -ENOTTY;
445 }
446 
447 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
448 {
449 	return -ENOTTY;
450 }
451 #endif
452 
453 /*
454  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
455  */
456 static int __dasd_ioctl_information(struct dasd_block *block,
457 		struct dasd_information2_t *dasd_info)
458 {
459 	struct subchannel_id sch_id;
460 	struct ccw_dev_id dev_id;
461 	struct dasd_device *base;
462 	struct ccw_device *cdev;
463 	struct list_head *l;
464 	unsigned long flags;
465 	int rc;
466 
467 	base = block->base;
468 	if (!base->discipline || !base->discipline->fill_info)
469 		return -EINVAL;
470 
471 	rc = base->discipline->fill_info(base, dasd_info);
472 	if (rc)
473 		return rc;
474 
475 	cdev = base->cdev;
476 	ccw_device_get_id(cdev, &dev_id);
477 	ccw_device_get_schid(cdev, &sch_id);
478 
479 	dasd_info->devno = dev_id.devno;
480 	dasd_info->schid = sch_id.sch_no;
481 	dasd_info->cu_type = cdev->id.cu_type;
482 	dasd_info->cu_model = cdev->id.cu_model;
483 	dasd_info->dev_type = cdev->id.dev_type;
484 	dasd_info->dev_model = cdev->id.dev_model;
485 	dasd_info->status = base->state;
486 	/*
487 	 * The open_count is increased for every opener, that includes
488 	 * the blkdev_get in dasd_scan_partitions.
489 	 * This must be hidden from user-space.
490 	 */
491 	dasd_info->open_count = atomic_read(&block->open_count);
492 	if (!block->bdev)
493 		dasd_info->open_count++;
494 
495 	/*
496 	 * check if device is really formatted
497 	 * LDL / CDL was returned by 'fill_info'
498 	 */
499 	if ((base->state < DASD_STATE_READY) ||
500 	    (dasd_check_blocksize(block->bp_block)))
501 		dasd_info->format = DASD_FORMAT_NONE;
502 
503 	dasd_info->features |=
504 		((base->features & DASD_FEATURE_READONLY) != 0);
505 
506 	memcpy(dasd_info->type, base->discipline->name, 4);
507 
508 	spin_lock_irqsave(&block->queue_lock, flags);
509 	list_for_each(l, &base->ccw_queue)
510 		dasd_info->chanq_len++;
511 	spin_unlock_irqrestore(&block->queue_lock, flags);
512 	return 0;
513 }
514 
515 static int dasd_ioctl_information(struct dasd_block *block, void __user *argp,
516 		size_t copy_size)
517 {
518 	struct dasd_information2_t *dasd_info;
519 	int error;
520 
521 	dasd_info = kzalloc(sizeof(*dasd_info), GFP_KERNEL);
522 	if (!dasd_info)
523 		return -ENOMEM;
524 
525 	error = __dasd_ioctl_information(block, dasd_info);
526 	if (!error && copy_to_user(argp, dasd_info, copy_size))
527 		error = -EFAULT;
528 	kfree(dasd_info);
529 	return error;
530 }
531 
532 /*
533  * Set read only
534  */
535 static int
536 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
537 {
538 	struct dasd_device *base;
539 	int intval, rc;
540 
541 	if (!capable(CAP_SYS_ADMIN))
542 		return -EACCES;
543 	if (bdev_is_partition(bdev))
544 		// ro setting is not allowed for partitions
545 		return -EINVAL;
546 	if (get_user(intval, (int __user *)argp))
547 		return -EFAULT;
548 	base = dasd_device_from_gendisk(bdev->bd_disk);
549 	if (!base)
550 		return -ENODEV;
551 	if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
552 		dasd_put_device(base);
553 		return -EROFS;
554 	}
555 	set_disk_ro(bdev->bd_disk, intval);
556 	rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
557 	dasd_put_device(base);
558 	return rc;
559 }
560 
561 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
562 				  struct cmbdata __user *argp)
563 {
564 	size_t size = _IOC_SIZE(cmd);
565 	struct cmbdata data;
566 	int ret;
567 
568 	ret = cmf_readall(block->base->cdev, &data);
569 	if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
570 		return -EFAULT;
571 	return ret;
572 }
573 
574 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
575 	       unsigned int cmd, unsigned long arg)
576 {
577 	struct dasd_block *block;
578 	struct dasd_device *base;
579 	void __user *argp;
580 	int rc;
581 
582 	if (is_compat_task())
583 		argp = compat_ptr(arg);
584 	else
585 		argp = (void __user *)arg;
586 
587 	if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
588 		PRINT_DEBUG("empty data ptr");
589 		return -EINVAL;
590 	}
591 
592 	base = dasd_device_from_gendisk(bdev->bd_disk);
593 	if (!base)
594 		return -ENODEV;
595 	block = base->block;
596 	rc = 0;
597 	switch (cmd) {
598 	case BIODASDDISABLE:
599 		rc = dasd_ioctl_disable(bdev);
600 		break;
601 	case BIODASDENABLE:
602 		rc = dasd_ioctl_enable(bdev);
603 		break;
604 	case BIODASDQUIESCE:
605 		rc = dasd_ioctl_quiesce(block);
606 		break;
607 	case BIODASDRESUME:
608 		rc = dasd_ioctl_resume(block);
609 		break;
610 	case BIODASDABORTIO:
611 		rc = dasd_ioctl_abortio(block);
612 		break;
613 	case BIODASDALLOWIO:
614 		rc = dasd_ioctl_allowio(block);
615 		break;
616 	case BIODASDFMT:
617 		rc = dasd_ioctl_format(bdev, argp);
618 		break;
619 	case BIODASDCHECKFMT:
620 		rc = dasd_ioctl_check_format(bdev, argp);
621 		break;
622 	case BIODASDINFO:
623 		rc = dasd_ioctl_information(block, argp,
624 				sizeof(struct dasd_information_t));
625 		break;
626 	case BIODASDINFO2:
627 		rc = dasd_ioctl_information(block, argp,
628 				sizeof(struct dasd_information2_t));
629 		break;
630 	case BIODASDPRRD:
631 		rc = dasd_ioctl_read_profile(block, argp);
632 		break;
633 	case BIODASDPRRST:
634 		rc = dasd_ioctl_reset_profile(block);
635 		break;
636 	case BLKROSET:
637 		rc = dasd_ioctl_set_ro(bdev, argp);
638 		break;
639 	case DASDAPIVER:
640 		rc = dasd_ioctl_api_version(argp);
641 		break;
642 	case BIODASDCMFENABLE:
643 		rc = enable_cmf(base->cdev);
644 		break;
645 	case BIODASDCMFDISABLE:
646 		rc = disable_cmf(base->cdev);
647 		break;
648 	case BIODASDREADALLCMB:
649 		rc = dasd_ioctl_readall_cmb(block, cmd, argp);
650 		break;
651 	case BIODASDRAS:
652 		rc = dasd_ioctl_release_space(bdev, argp);
653 		break;
654 	default:
655 		/* if the discipline has an ioctl method try it. */
656 		rc = -ENOTTY;
657 		if (base->discipline->ioctl)
658 			rc = base->discipline->ioctl(block, cmd, argp);
659 	}
660 	dasd_put_device(base);
661 	return rc;
662 }
663 
664 
665 /**
666  * dasd_biodasdinfo() - fill out the dasd information structure
667  * @disk [in]: pointer to gendisk structure that references a DASD
668  * @info [out]: pointer to the dasd_information2_t structure
669  *
670  * Provide access to DASD specific information.
671  * The gendisk structure is checked if it belongs to the DASD driver by
672  * comparing the gendisk->fops pointer.
673  * If it does not belong to the DASD driver -EINVAL is returned.
674  * Otherwise the provided dasd_information2_t structure is filled out.
675  *
676  * Returns:
677  *   %0 on success and a negative error value on failure.
678  */
679 int dasd_biodasdinfo(struct gendisk *disk, struct dasd_information2_t *info)
680 {
681 	struct dasd_device *base;
682 	int error;
683 
684 	if (disk->fops != &dasd_device_operations)
685 		return -EINVAL;
686 
687 	base = dasd_device_from_gendisk(disk);
688 	if (!base)
689 		return -ENODEV;
690 	error = __dasd_ioctl_information(base->block, info);
691 	dasd_put_device(base);
692 	return error;
693 }
694 /* export that symbol_get in partition detection is possible */
695 EXPORT_SYMBOL_GPL(dasd_biodasdinfo);
696