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