xref: /openbmc/linux/drivers/s390/block/dasd_ioctl.c (revision 95777591)
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 
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 static int dasd_check_format(struct dasd_block *block,
242 			     struct format_check_t *cdata)
243 {
244 	struct dasd_device *base;
245 	int rc;
246 
247 	base = block->base;
248 	if (!base->discipline->check_device_format)
249 		return -ENOTTY;
250 
251 	rc = base->discipline->check_device_format(base, cdata, 1);
252 	if (rc == -EAGAIN)
253 		rc = base->discipline->check_device_format(base, cdata, 0);
254 
255 	return rc;
256 }
257 
258 /*
259  * Format device.
260  */
261 static int
262 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
263 {
264 	struct dasd_device *base;
265 	struct format_data_t fdata;
266 	int rc;
267 
268 	if (!capable(CAP_SYS_ADMIN))
269 		return -EACCES;
270 	if (!argp)
271 		return -EINVAL;
272 	base = dasd_device_from_gendisk(bdev->bd_disk);
273 	if (!base)
274 		return -ENODEV;
275 	if (base->features & DASD_FEATURE_READONLY ||
276 	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
277 		dasd_put_device(base);
278 		return -EROFS;
279 	}
280 	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
281 		dasd_put_device(base);
282 		return -EFAULT;
283 	}
284 	if (bdev != bdev->bd_contains) {
285 		pr_warn("%s: The specified DASD is a partition and cannot be formatted\n",
286 			dev_name(&base->cdev->dev));
287 		dasd_put_device(base);
288 		return -EINVAL;
289 	}
290 	rc = dasd_format(base->block, &fdata);
291 	dasd_put_device(base);
292 
293 	return rc;
294 }
295 
296 /*
297  * Check device format
298  */
299 static int dasd_ioctl_check_format(struct block_device *bdev, void __user *argp)
300 {
301 	struct format_check_t cdata;
302 	struct dasd_device *base;
303 	int rc = 0;
304 
305 	if (!argp)
306 		return -EINVAL;
307 
308 	base = dasd_device_from_gendisk(bdev->bd_disk);
309 	if (!base)
310 		return -ENODEV;
311 	if (bdev != bdev->bd_contains) {
312 		pr_warn("%s: The specified DASD is a partition and cannot be checked\n",
313 			dev_name(&base->cdev->dev));
314 		rc = -EINVAL;
315 		goto out_err;
316 	}
317 
318 	if (copy_from_user(&cdata, argp, sizeof(cdata))) {
319 		rc = -EFAULT;
320 		goto out_err;
321 	}
322 
323 	rc = dasd_check_format(base->block, &cdata);
324 	if (rc)
325 		goto out_err;
326 
327 	if (copy_to_user(argp, &cdata, sizeof(cdata)))
328 		rc = -EFAULT;
329 
330 out_err:
331 	dasd_put_device(base);
332 
333 	return rc;
334 }
335 
336 #ifdef CONFIG_DASD_PROFILE
337 /*
338  * Reset device profile information
339  */
340 static int dasd_ioctl_reset_profile(struct dasd_block *block)
341 {
342 	dasd_profile_reset(&block->profile);
343 	return 0;
344 }
345 
346 /*
347  * Return device profile information
348  */
349 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
350 {
351 	struct dasd_profile_info_t *data;
352 	int rc = 0;
353 
354 	data = kmalloc(sizeof(*data), GFP_KERNEL);
355 	if (!data)
356 		return -ENOMEM;
357 
358 	spin_lock_bh(&block->profile.lock);
359 	if (block->profile.data) {
360 		data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
361 		data->dasd_io_sects = block->profile.data->dasd_io_sects;
362 		memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
363 		       sizeof(data->dasd_io_secs));
364 		memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
365 		       sizeof(data->dasd_io_times));
366 		memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
367 		       sizeof(data->dasd_io_timps));
368 		memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
369 		       sizeof(data->dasd_io_time1));
370 		memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
371 		       sizeof(data->dasd_io_time2));
372 		memcpy(data->dasd_io_time2ps,
373 		       block->profile.data->dasd_io_time2ps,
374 		       sizeof(data->dasd_io_time2ps));
375 		memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
376 		       sizeof(data->dasd_io_time3));
377 		memcpy(data->dasd_io_nr_req,
378 		       block->profile.data->dasd_io_nr_req,
379 		       sizeof(data->dasd_io_nr_req));
380 		spin_unlock_bh(&block->profile.lock);
381 	} else {
382 		spin_unlock_bh(&block->profile.lock);
383 		rc = -EIO;
384 		goto out;
385 	}
386 	if (copy_to_user(argp, data, sizeof(*data)))
387 		rc = -EFAULT;
388 out:
389 	kfree(data);
390 	return rc;
391 }
392 #else
393 static int dasd_ioctl_reset_profile(struct dasd_block *block)
394 {
395 	return -ENOTTY;
396 }
397 
398 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
399 {
400 	return -ENOTTY;
401 }
402 #endif
403 
404 /*
405  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
406  */
407 static int dasd_ioctl_information(struct dasd_block *block,
408 				  unsigned int cmd, void __user *argp)
409 {
410 	struct dasd_information2_t *dasd_info;
411 	struct subchannel_id sch_id;
412 	struct ccw_dev_id dev_id;
413 	struct dasd_device *base;
414 	struct ccw_device *cdev;
415 	struct list_head *l;
416 	unsigned long flags;
417 	int rc;
418 
419 	base = block->base;
420 	if (!base->discipline || !base->discipline->fill_info)
421 		return -EINVAL;
422 
423 	dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
424 	if (dasd_info == NULL)
425 		return -ENOMEM;
426 
427 	rc = base->discipline->fill_info(base, dasd_info);
428 	if (rc) {
429 		kfree(dasd_info);
430 		return rc;
431 	}
432 
433 	cdev = base->cdev;
434 	ccw_device_get_id(cdev, &dev_id);
435 	ccw_device_get_schid(cdev, &sch_id);
436 
437 	dasd_info->devno = dev_id.devno;
438 	dasd_info->schid = sch_id.sch_no;
439 	dasd_info->cu_type = cdev->id.cu_type;
440 	dasd_info->cu_model = cdev->id.cu_model;
441 	dasd_info->dev_type = cdev->id.dev_type;
442 	dasd_info->dev_model = cdev->id.dev_model;
443 	dasd_info->status = base->state;
444 	/*
445 	 * The open_count is increased for every opener, that includes
446 	 * the blkdev_get in dasd_scan_partitions.
447 	 * This must be hidden from user-space.
448 	 */
449 	dasd_info->open_count = atomic_read(&block->open_count);
450 	if (!block->bdev)
451 		dasd_info->open_count++;
452 
453 	/*
454 	 * check if device is really formatted
455 	 * LDL / CDL was returned by 'fill_info'
456 	 */
457 	if ((base->state < DASD_STATE_READY) ||
458 	    (dasd_check_blocksize(block->bp_block)))
459 		dasd_info->format = DASD_FORMAT_NONE;
460 
461 	dasd_info->features |=
462 		((base->features & DASD_FEATURE_READONLY) != 0);
463 
464 	memcpy(dasd_info->type, base->discipline->name, 4);
465 
466 	spin_lock_irqsave(&block->queue_lock, flags);
467 	list_for_each(l, &base->ccw_queue)
468 		dasd_info->chanq_len++;
469 	spin_unlock_irqrestore(&block->queue_lock, flags);
470 
471 	rc = 0;
472 	if (copy_to_user(argp, dasd_info,
473 			 ((cmd == (unsigned int) BIODASDINFO2) ?
474 			  sizeof(struct dasd_information2_t) :
475 			  sizeof(struct dasd_information_t))))
476 		rc = -EFAULT;
477 	kfree(dasd_info);
478 	return rc;
479 }
480 
481 /*
482  * Set read only
483  */
484 static int
485 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
486 {
487 	struct dasd_device *base;
488 	int intval, rc;
489 
490 	if (!capable(CAP_SYS_ADMIN))
491 		return -EACCES;
492 	if (bdev != bdev->bd_contains)
493 		// ro setting is not allowed for partitions
494 		return -EINVAL;
495 	if (get_user(intval, (int __user *)argp))
496 		return -EFAULT;
497 	base = dasd_device_from_gendisk(bdev->bd_disk);
498 	if (!base)
499 		return -ENODEV;
500 	if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
501 		dasd_put_device(base);
502 		return -EROFS;
503 	}
504 	set_disk_ro(bdev->bd_disk, intval);
505 	rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
506 	dasd_put_device(base);
507 	return rc;
508 }
509 
510 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
511 				  struct cmbdata __user *argp)
512 {
513 	size_t size = _IOC_SIZE(cmd);
514 	struct cmbdata data;
515 	int ret;
516 
517 	ret = cmf_readall(block->base->cdev, &data);
518 	if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
519 		return -EFAULT;
520 	return ret;
521 }
522 
523 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
524 	       unsigned int cmd, unsigned long arg)
525 {
526 	struct dasd_block *block;
527 	struct dasd_device *base;
528 	void __user *argp;
529 	int rc;
530 
531 	if (is_compat_task())
532 		argp = compat_ptr(arg);
533 	else
534 		argp = (void __user *)arg;
535 
536 	if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
537 		PRINT_DEBUG("empty data ptr");
538 		return -EINVAL;
539 	}
540 
541 	base = dasd_device_from_gendisk(bdev->bd_disk);
542 	if (!base)
543 		return -ENODEV;
544 	block = base->block;
545 	rc = 0;
546 	switch (cmd) {
547 	case BIODASDDISABLE:
548 		rc = dasd_ioctl_disable(bdev);
549 		break;
550 	case BIODASDENABLE:
551 		rc = dasd_ioctl_enable(bdev);
552 		break;
553 	case BIODASDQUIESCE:
554 		rc = dasd_ioctl_quiesce(block);
555 		break;
556 	case BIODASDRESUME:
557 		rc = dasd_ioctl_resume(block);
558 		break;
559 	case BIODASDABORTIO:
560 		rc = dasd_ioctl_abortio(block);
561 		break;
562 	case BIODASDALLOWIO:
563 		rc = dasd_ioctl_allowio(block);
564 		break;
565 	case BIODASDFMT:
566 		rc = dasd_ioctl_format(bdev, argp);
567 		break;
568 	case BIODASDCHECKFMT:
569 		rc = dasd_ioctl_check_format(bdev, argp);
570 		break;
571 	case BIODASDINFO:
572 		rc = dasd_ioctl_information(block, cmd, argp);
573 		break;
574 	case BIODASDINFO2:
575 		rc = dasd_ioctl_information(block, cmd, argp);
576 		break;
577 	case BIODASDPRRD:
578 		rc = dasd_ioctl_read_profile(block, argp);
579 		break;
580 	case BIODASDPRRST:
581 		rc = dasd_ioctl_reset_profile(block);
582 		break;
583 	case BLKROSET:
584 		rc = dasd_ioctl_set_ro(bdev, argp);
585 		break;
586 	case DASDAPIVER:
587 		rc = dasd_ioctl_api_version(argp);
588 		break;
589 	case BIODASDCMFENABLE:
590 		rc = enable_cmf(base->cdev);
591 		break;
592 	case BIODASDCMFDISABLE:
593 		rc = disable_cmf(base->cdev);
594 		break;
595 	case BIODASDREADALLCMB:
596 		rc = dasd_ioctl_readall_cmb(block, cmd, argp);
597 		break;
598 	default:
599 		/* if the discipline has an ioctl method try it. */
600 		rc = -ENOTTY;
601 		if (base->discipline->ioctl)
602 			rc = base->discipline->ioctl(block, cmd, argp);
603 	}
604 	dasd_put_device(base);
605 	return rc;
606 }
607