xref: /linux/drivers/s390/block/dasd_ioctl.c (revision 24942c8e5cc8696064ee207ff29d4cf21f70dafc)
1 /*
2  * File...........: linux/drivers/s390/block/dasd_ioctl.c
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  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 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/major.h>
17 #include <linux/fs.h>
18 #include <linux/blkpg.h>
19 #include <linux/slab.h>
20 #include <asm/compat.h>
21 #include <asm/ccwdev.h>
22 #include <asm/cmb.h>
23 #include <asm/uaccess.h>
24 
25 /* This is ugly... */
26 #define PRINTK_HEADER "dasd_ioctl:"
27 
28 #include "dasd_int.h"
29 
30 
31 static int
32 dasd_ioctl_api_version(void __user *argp)
33 {
34 	int ver = DASD_API_VERSION;
35 	return put_user(ver, (int __user *)argp);
36 }
37 
38 /*
39  * Enable device.
40  * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
41  */
42 static int
43 dasd_ioctl_enable(struct block_device *bdev)
44 {
45 	struct dasd_device *base;
46 
47 	if (!capable(CAP_SYS_ADMIN))
48 		return -EACCES;
49 
50 	base = dasd_device_from_gendisk(bdev->bd_disk);
51 	if (!base)
52 		return -ENODEV;
53 
54 	dasd_enable_device(base);
55 	/* Formatting the dasd device can change the capacity. */
56 	mutex_lock(&bdev->bd_mutex);
57 	i_size_write(bdev->bd_inode,
58 		     (loff_t)get_capacity(base->block->gdp) << 9);
59 	mutex_unlock(&bdev->bd_mutex);
60 	dasd_put_device(base);
61 	return 0;
62 }
63 
64 /*
65  * Disable device.
66  * Used by dasdfmt. Disable I/O operations but allow ioctls.
67  */
68 static int
69 dasd_ioctl_disable(struct block_device *bdev)
70 {
71 	struct dasd_device *base;
72 
73 	if (!capable(CAP_SYS_ADMIN))
74 		return -EACCES;
75 
76 	base = dasd_device_from_gendisk(bdev->bd_disk);
77 	if (!base)
78 		return -ENODEV;
79 	/*
80 	 * Man this is sick. We don't do a real disable but only downgrade
81 	 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
82 	 * BIODASDDISABLE to disable accesses to the device via the block
83 	 * device layer but it still wants to do i/o on the device by
84 	 * using the BIODASDFMT ioctl. Therefore the correct state for the
85 	 * device is DASD_STATE_BASIC that allows to do basic i/o.
86 	 */
87 	dasd_set_target_state(base, DASD_STATE_BASIC);
88 	/*
89 	 * Set i_size to zero, since read, write, etc. check against this
90 	 * value.
91 	 */
92 	mutex_lock(&bdev->bd_mutex);
93 	i_size_write(bdev->bd_inode, 0);
94 	mutex_unlock(&bdev->bd_mutex);
95 	dasd_put_device(base);
96 	return 0;
97 }
98 
99 /*
100  * Quiesce device.
101  */
102 static int dasd_ioctl_quiesce(struct dasd_block *block)
103 {
104 	unsigned long flags;
105 	struct dasd_device *base;
106 
107 	base = block->base;
108 	if (!capable (CAP_SYS_ADMIN))
109 		return -EACCES;
110 
111 	pr_info("%s: The DASD has been put in the quiesce "
112 		"state\n", dev_name(&base->cdev->dev));
113 	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
114 	dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
115 	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
116 	return 0;
117 }
118 
119 
120 /*
121  * Resume device.
122  */
123 static int dasd_ioctl_resume(struct dasd_block *block)
124 {
125 	unsigned long flags;
126 	struct dasd_device *base;
127 
128 	base = block->base;
129 	if (!capable (CAP_SYS_ADMIN))
130 		return -EACCES;
131 
132 	pr_info("%s: I/O operations have been resumed "
133 		"on the DASD\n", dev_name(&base->cdev->dev));
134 	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
135 	dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
136 	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
137 
138 	dasd_schedule_block_bh(block);
139 	return 0;
140 }
141 
142 /*
143  * performs formatting of _device_ according to _fdata_
144  * Note: The discipline's format_function is assumed to deliver formatting
145  * commands to format a single unit of the device. In terms of the ECKD
146  * devices this means CCWs are generated to format a single track.
147  */
148 static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
149 {
150 	struct dasd_ccw_req *cqr;
151 	struct dasd_device *base;
152 	int rc;
153 
154 	base = block->base;
155 	if (base->discipline->format_device == NULL)
156 		return -EPERM;
157 
158 	if (base->state != DASD_STATE_BASIC) {
159 		pr_warning("%s: The DASD cannot be formatted while it is "
160 			   "enabled\n",  dev_name(&base->cdev->dev));
161 		return -EBUSY;
162 	}
163 
164 	DBF_DEV_EVENT(DBF_NOTICE, base,
165 		      "formatting units %u to %u (%u B blocks) flags %u",
166 		      fdata->start_unit,
167 		      fdata->stop_unit, fdata->blksize, fdata->intensity);
168 
169 	/* Since dasdfmt keeps the device open after it was disabled,
170 	 * there still exists an inode for this device.
171 	 * We must update i_blkbits, otherwise we might get errors when
172 	 * enabling the device later.
173 	 */
174 	if (fdata->start_unit == 0) {
175 		struct block_device *bdev = bdget_disk(block->gdp, 0);
176 		bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
177 		bdput(bdev);
178 	}
179 
180 	while (fdata->start_unit <= fdata->stop_unit) {
181 		cqr = base->discipline->format_device(base, fdata);
182 		if (IS_ERR(cqr))
183 			return PTR_ERR(cqr);
184 		rc = dasd_sleep_on_interruptible(cqr);
185 		dasd_sfree_request(cqr, cqr->memdev);
186 		if (rc) {
187 			if (rc != -ERESTARTSYS)
188 				pr_err("%s: Formatting unit %d failed with "
189 				       "rc=%d\n", dev_name(&base->cdev->dev),
190 				       fdata->start_unit, rc);
191 			return rc;
192 		}
193 		fdata->start_unit++;
194 	}
195 	return 0;
196 }
197 
198 /*
199  * Format device.
200  */
201 static int
202 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
203 {
204 	struct dasd_device *base;
205 	struct format_data_t fdata;
206 	int rc;
207 
208 	if (!capable(CAP_SYS_ADMIN))
209 		return -EACCES;
210 	if (!argp)
211 		return -EINVAL;
212 	base = dasd_device_from_gendisk(bdev->bd_disk);
213 	if (!base)
214 		return -ENODEV;
215 	if (base->features & DASD_FEATURE_READONLY ||
216 	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
217 		dasd_put_device(base);
218 		return -EROFS;
219 	}
220 	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
221 		dasd_put_device(base);
222 		return -EFAULT;
223 	}
224 	if (bdev != bdev->bd_contains) {
225 		pr_warning("%s: The specified DASD is a partition and cannot "
226 			   "be formatted\n",
227 			   dev_name(&base->cdev->dev));
228 		dasd_put_device(base);
229 		return -EINVAL;
230 	}
231 	rc = dasd_format(base->block, &fdata);
232 	dasd_put_device(base);
233 	return rc;
234 }
235 
236 #ifdef CONFIG_DASD_PROFILE
237 /*
238  * Reset device profile information
239  */
240 static int dasd_ioctl_reset_profile(struct dasd_block *block)
241 {
242 	dasd_profile_reset(&block->profile);
243 	return 0;
244 }
245 
246 /*
247  * Return device profile information
248  */
249 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
250 {
251 	struct dasd_profile_info_t *data;
252 	int rc = 0;
253 
254 	data = kmalloc(sizeof(*data), GFP_KERNEL);
255 	if (!data)
256 		return -ENOMEM;
257 
258 	spin_lock_bh(&block->profile.lock);
259 	if (block->profile.data) {
260 		data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
261 		data->dasd_io_sects = block->profile.data->dasd_io_sects;
262 		memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
263 		       sizeof(data->dasd_io_secs));
264 		memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
265 		       sizeof(data->dasd_io_times));
266 		memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
267 		       sizeof(data->dasd_io_timps));
268 		memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
269 		       sizeof(data->dasd_io_time1));
270 		memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
271 		       sizeof(data->dasd_io_time2));
272 		memcpy(data->dasd_io_time2ps,
273 		       block->profile.data->dasd_io_time2ps,
274 		       sizeof(data->dasd_io_time2ps));
275 		memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
276 		       sizeof(data->dasd_io_time3));
277 		memcpy(data->dasd_io_nr_req,
278 		       block->profile.data->dasd_io_nr_req,
279 		       sizeof(data->dasd_io_nr_req));
280 		spin_unlock_bh(&block->profile.lock);
281 	} else {
282 		spin_unlock_bh(&block->profile.lock);
283 		rc = -EIO;
284 		goto out;
285 	}
286 	if (copy_to_user(argp, data, sizeof(*data)))
287 		rc = -EFAULT;
288 out:
289 	kfree(data);
290 	return rc;
291 }
292 #else
293 static int dasd_ioctl_reset_profile(struct dasd_block *block)
294 {
295 	return -ENOSYS;
296 }
297 
298 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
299 {
300 	return -ENOSYS;
301 }
302 #endif
303 
304 /*
305  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
306  */
307 static int dasd_ioctl_information(struct dasd_block *block,
308 				  unsigned int cmd, void __user *argp)
309 {
310 	struct dasd_information2_t *dasd_info;
311 	unsigned long flags;
312 	int rc;
313 	struct dasd_device *base;
314 	struct ccw_device *cdev;
315 	struct ccw_dev_id dev_id;
316 
317 	base = block->base;
318 	if (!base->discipline || !base->discipline->fill_info)
319 		return -EINVAL;
320 
321 	dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
322 	if (dasd_info == NULL)
323 		return -ENOMEM;
324 
325 	rc = base->discipline->fill_info(base, dasd_info);
326 	if (rc) {
327 		kfree(dasd_info);
328 		return rc;
329 	}
330 
331 	cdev = base->cdev;
332 	ccw_device_get_id(cdev, &dev_id);
333 
334 	dasd_info->devno = dev_id.devno;
335 	dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
336 	dasd_info->cu_type = cdev->id.cu_type;
337 	dasd_info->cu_model = cdev->id.cu_model;
338 	dasd_info->dev_type = cdev->id.dev_type;
339 	dasd_info->dev_model = cdev->id.dev_model;
340 	dasd_info->status = base->state;
341 	/*
342 	 * The open_count is increased for every opener, that includes
343 	 * the blkdev_get in dasd_scan_partitions.
344 	 * This must be hidden from user-space.
345 	 */
346 	dasd_info->open_count = atomic_read(&block->open_count);
347 	if (!block->bdev)
348 		dasd_info->open_count++;
349 
350 	/*
351 	 * check if device is really formatted
352 	 * LDL / CDL was returned by 'fill_info'
353 	 */
354 	if ((base->state < DASD_STATE_READY) ||
355 	    (dasd_check_blocksize(block->bp_block)))
356 		dasd_info->format = DASD_FORMAT_NONE;
357 
358 	dasd_info->features |=
359 		((base->features & DASD_FEATURE_READONLY) != 0);
360 
361 	memcpy(dasd_info->type, base->discipline->name, 4);
362 
363 	if (block->request_queue->request_fn) {
364 		struct list_head *l;
365 #ifdef DASD_EXTENDED_PROFILING
366 		{
367 			struct list_head *l;
368 			spin_lock_irqsave(&block->lock, flags);
369 			list_for_each(l, &block->request_queue->queue_head)
370 				dasd_info->req_queue_len++;
371 			spin_unlock_irqrestore(&block->lock, flags);
372 		}
373 #endif				/* DASD_EXTENDED_PROFILING */
374 		spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
375 		list_for_each(l, &base->ccw_queue)
376 			dasd_info->chanq_len++;
377 		spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
378 				       flags);
379 	}
380 
381 	rc = 0;
382 	if (copy_to_user(argp, dasd_info,
383 			 ((cmd == (unsigned int) BIODASDINFO2) ?
384 			  sizeof(struct dasd_information2_t) :
385 			  sizeof(struct dasd_information_t))))
386 		rc = -EFAULT;
387 	kfree(dasd_info);
388 	return rc;
389 }
390 
391 /*
392  * Set read only
393  */
394 static int
395 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
396 {
397 	struct dasd_device *base;
398 	int intval, rc;
399 
400 	if (!capable(CAP_SYS_ADMIN))
401 		return -EACCES;
402 	if (bdev != bdev->bd_contains)
403 		// ro setting is not allowed for partitions
404 		return -EINVAL;
405 	if (get_user(intval, (int __user *)argp))
406 		return -EFAULT;
407 	base = dasd_device_from_gendisk(bdev->bd_disk);
408 	if (!base)
409 		return -ENODEV;
410 	if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
411 		dasd_put_device(base);
412 		return -EROFS;
413 	}
414 	set_disk_ro(bdev->bd_disk, intval);
415 	rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
416 	dasd_put_device(base);
417 	return rc;
418 }
419 
420 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
421 				  struct cmbdata __user *argp)
422 {
423 	size_t size = _IOC_SIZE(cmd);
424 	struct cmbdata data;
425 	int ret;
426 
427 	ret = cmf_readall(block->base->cdev, &data);
428 	if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
429 		return -EFAULT;
430 	return ret;
431 }
432 
433 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
434 	       unsigned int cmd, unsigned long arg)
435 {
436 	struct dasd_block *block;
437 	struct dasd_device *base;
438 	void __user *argp;
439 	int rc;
440 
441 	if (is_compat_task())
442 		argp = compat_ptr(arg);
443 	else
444 		argp = (void __user *)arg;
445 
446 	if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
447 		PRINT_DEBUG("empty data ptr");
448 		return -EINVAL;
449 	}
450 
451 	base = dasd_device_from_gendisk(bdev->bd_disk);
452 	if (!base)
453 		return -ENODEV;
454 	block = base->block;
455 	rc = 0;
456 	switch (cmd) {
457 	case BIODASDDISABLE:
458 		rc = dasd_ioctl_disable(bdev);
459 		break;
460 	case BIODASDENABLE:
461 		rc = dasd_ioctl_enable(bdev);
462 		break;
463 	case BIODASDQUIESCE:
464 		rc = dasd_ioctl_quiesce(block);
465 		break;
466 	case BIODASDRESUME:
467 		rc = dasd_ioctl_resume(block);
468 		break;
469 	case BIODASDFMT:
470 		rc = dasd_ioctl_format(bdev, argp);
471 		break;
472 	case BIODASDINFO:
473 		rc = dasd_ioctl_information(block, cmd, argp);
474 		break;
475 	case BIODASDINFO2:
476 		rc = dasd_ioctl_information(block, cmd, argp);
477 		break;
478 	case BIODASDPRRD:
479 		rc = dasd_ioctl_read_profile(block, argp);
480 		break;
481 	case BIODASDPRRST:
482 		rc = dasd_ioctl_reset_profile(block);
483 		break;
484 	case BLKROSET:
485 		rc = dasd_ioctl_set_ro(bdev, argp);
486 		break;
487 	case DASDAPIVER:
488 		rc = dasd_ioctl_api_version(argp);
489 		break;
490 	case BIODASDCMFENABLE:
491 		rc = enable_cmf(base->cdev);
492 		break;
493 	case BIODASDCMFDISABLE:
494 		rc = disable_cmf(base->cdev);
495 		break;
496 	case BIODASDREADALLCMB:
497 		rc = dasd_ioctl_readall_cmb(block, cmd, argp);
498 		break;
499 	default:
500 		/* if the discipline has an ioctl method try it. */
501 		if (base->discipline->ioctl) {
502 			rc = base->discipline->ioctl(block, cmd, argp);
503 			if (rc == -ENOIOCTLCMD)
504 				rc = -EINVAL;
505 		} else
506 			rc = -EINVAL;
507 	}
508 	dasd_put_device(base);
509 	return rc;
510 }
511