xref: /linux/drivers/s390/block/dasd_ioctl.c (revision d344c5e0856ad03278d8700b503762dbc8b86e12)
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  * $Revision: 1.47 $
11  *
12  * i/o controls for the dasd driver.
13  */
14 #include <linux/config.h>
15 #include <linux/interrupt.h>
16 #include <linux/major.h>
17 #include <linux/fs.h>
18 #include <linux/blkpg.h>
19 
20 #include <asm/ccwdev.h>
21 #include <asm/uaccess.h>
22 
23 /* This is ugly... */
24 #define PRINTK_HEADER "dasd_ioctl:"
25 
26 #include "dasd_int.h"
27 
28 /*
29  * SECTION: ioctl functions.
30  */
31 static struct list_head dasd_ioctl_list = LIST_HEAD_INIT(dasd_ioctl_list);
32 
33 /*
34  * Find the ioctl with number no.
35  */
36 static struct dasd_ioctl *
37 dasd_find_ioctl(int no)
38 {
39 	struct dasd_ioctl *ioctl;
40 
41 	list_for_each_entry (ioctl, &dasd_ioctl_list, list)
42 		if (ioctl->no == no)
43 			return ioctl;
44 	return NULL;
45 }
46 
47 /*
48  * Register ioctl with number no.
49  */
50 int
51 dasd_ioctl_no_register(struct module *owner, int no, dasd_ioctl_fn_t handler)
52 {
53 	struct dasd_ioctl *new;
54 	if (dasd_find_ioctl(no))
55 		return -EBUSY;
56 	new = kmalloc(sizeof (struct dasd_ioctl), GFP_KERNEL);
57 	if (new == NULL)
58 		return -ENOMEM;
59 	new->owner = owner;
60 	new->no = no;
61 	new->handler = handler;
62 	list_add(&new->list, &dasd_ioctl_list);
63 	return 0;
64 }
65 
66 /*
67  * Deregister ioctl with number no.
68  */
69 int
70 dasd_ioctl_no_unregister(struct module *owner, int no, dasd_ioctl_fn_t handler)
71 {
72 	struct dasd_ioctl *old = dasd_find_ioctl(no);
73 	if (old == NULL)
74 		return -ENOENT;
75 	if (old->no != no || old->handler != handler || owner != old->owner)
76 		return -EINVAL;
77 	list_del(&old->list);
78 	kfree(old);
79 	return 0;
80 }
81 
82 int
83 dasd_ioctl(struct inode *inp, struct file *filp,
84 	   unsigned int no, unsigned long data)
85 {
86 	struct block_device *bdev = inp->i_bdev;
87 	struct dasd_device *device = bdev->bd_disk->private_data;
88 	struct dasd_ioctl *ioctl;
89 	const char *dir;
90 	int rc;
91 
92 	if ((_IOC_DIR(no) != _IOC_NONE) && (data == 0)) {
93 		PRINT_DEBUG("empty data ptr");
94 		return -EINVAL;
95 	}
96 	dir = _IOC_DIR (no) == _IOC_NONE ? "0" :
97 		_IOC_DIR (no) == _IOC_READ ? "r" :
98 		_IOC_DIR (no) == _IOC_WRITE ? "w" :
99 		_IOC_DIR (no) == (_IOC_READ | _IOC_WRITE) ? "rw" : "u";
100 	DBF_DEV_EVENT(DBF_DEBUG, device,
101 		      "ioctl 0x%08x %s'0x%x'%d(%d) with data %8lx", no,
102 		      dir, _IOC_TYPE(no), _IOC_NR(no), _IOC_SIZE(no), data);
103 	/* Search for ioctl no in the ioctl list. */
104 	list_for_each_entry(ioctl, &dasd_ioctl_list, list) {
105 		if (ioctl->no == no) {
106 			/* Found a matching ioctl. Call it. */
107 			if (!try_module_get(ioctl->owner))
108 				continue;
109 			rc = ioctl->handler(bdev, no, data);
110 			module_put(ioctl->owner);
111 			return rc;
112 		}
113 	}
114 	/* No ioctl with number no. */
115 	DBF_DEV_EVENT(DBF_INFO, device,
116 		      "unknown ioctl 0x%08x=%s'0x%x'%d(%d) data %8lx", no,
117 		      dir, _IOC_TYPE(no), _IOC_NR(no), _IOC_SIZE(no), data);
118 	return -EINVAL;
119 }
120 
121 static int
122 dasd_ioctl_api_version(struct block_device *bdev, int no, long args)
123 {
124 	int ver = DASD_API_VERSION;
125 	return put_user(ver, (int __user *) args);
126 }
127 
128 /*
129  * Enable device.
130  * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
131  */
132 static int
133 dasd_ioctl_enable(struct block_device *bdev, int no, long args)
134 {
135 	struct dasd_device *device;
136 
137 	if (!capable(CAP_SYS_ADMIN))
138 		return -EACCES;
139 	device = bdev->bd_disk->private_data;
140 	if (device == NULL)
141 		return -ENODEV;
142 	dasd_enable_device(device);
143 	/* Formatting the dasd device can change the capacity. */
144 	down(&bdev->bd_sem);
145 	i_size_write(bdev->bd_inode, (loff_t)get_capacity(device->gdp) << 9);
146 	up(&bdev->bd_sem);
147 	return 0;
148 }
149 
150 /*
151  * Disable device.
152  * Used by dasdfmt. Disable I/O operations but allow ioctls.
153  */
154 static int
155 dasd_ioctl_disable(struct block_device *bdev, int no, long args)
156 {
157 	struct dasd_device *device;
158 
159 	if (!capable(CAP_SYS_ADMIN))
160 		return -EACCES;
161 	device = bdev->bd_disk->private_data;
162 	if (device == NULL)
163 		return -ENODEV;
164 	/*
165 	 * Man this is sick. We don't do a real disable but only downgrade
166 	 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
167 	 * BIODASDDISABLE to disable accesses to the device via the block
168 	 * device layer but it still wants to do i/o on the device by
169 	 * using the BIODASDFMT ioctl. Therefore the correct state for the
170 	 * device is DASD_STATE_BASIC that allows to do basic i/o.
171 	 */
172 	dasd_set_target_state(device, DASD_STATE_BASIC);
173 	/*
174 	 * Set i_size to zero, since read, write, etc. check against this
175 	 * value.
176 	 */
177 	down(&bdev->bd_sem);
178 	i_size_write(bdev->bd_inode, 0);
179 	up(&bdev->bd_sem);
180 	return 0;
181 }
182 
183 /*
184  * Quiesce device.
185  */
186 static int
187 dasd_ioctl_quiesce(struct block_device *bdev, int no, long args)
188 {
189 	struct dasd_device *device;
190 	unsigned long flags;
191 
192 	if (!capable (CAP_SYS_ADMIN))
193 		return -EACCES;
194 
195 	device = bdev->bd_disk->private_data;
196 	if (device == NULL)
197 		return -ENODEV;
198 
199 	DEV_MESSAGE (KERN_DEBUG, device, "%s",
200 		     "Quiesce IO on device");
201 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
202 	device->stopped |= DASD_STOPPED_QUIESCE;
203 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
204 	return 0;
205 }
206 
207 
208 /*
209  * Quiesce device.
210  */
211 static int
212 dasd_ioctl_resume(struct block_device *bdev, int no, long args)
213 {
214 	struct dasd_device *device;
215 	unsigned long flags;
216 
217 	if (!capable (CAP_SYS_ADMIN))
218 		return -EACCES;
219 
220 	device = bdev->bd_disk->private_data;
221 	if (device == NULL)
222 		return -ENODEV;
223 
224 	DEV_MESSAGE (KERN_DEBUG, device, "%s",
225 		     "resume IO on device");
226 
227 	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
228 	device->stopped &= ~DASD_STOPPED_QUIESCE;
229 	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
230 
231 	dasd_schedule_bh (device);
232 	return 0;
233 }
234 
235 /*
236  * performs formatting of _device_ according to _fdata_
237  * Note: The discipline's format_function is assumed to deliver formatting
238  * commands to format a single unit of the device. In terms of the ECKD
239  * devices this means CCWs are generated to format a single track.
240  */
241 static int
242 dasd_format(struct dasd_device * device, struct format_data_t * fdata)
243 {
244 	struct dasd_ccw_req *cqr;
245 	int rc;
246 
247 	if (device->discipline->format_device == NULL)
248 		return -EPERM;
249 
250 	if (device->state != DASD_STATE_BASIC) {
251 		DEV_MESSAGE(KERN_WARNING, device, "%s",
252 			    "dasd_format: device is not disabled! ");
253 		return -EBUSY;
254 	}
255 
256 	DBF_DEV_EVENT(DBF_NOTICE, device,
257 		      "formatting units %d to %d (%d B blocks) flags %d",
258 		      fdata->start_unit,
259 		      fdata->stop_unit, fdata->blksize, fdata->intensity);
260 
261 	/* Since dasdfmt keeps the device open after it was disabled,
262 	 * there still exists an inode for this device.
263 	 * We must update i_blkbits, otherwise we might get errors when
264 	 * enabling the device later.
265 	 */
266 	if (fdata->start_unit == 0) {
267 		struct block_device *bdev = bdget_disk(device->gdp, 0);
268 		bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
269 		bdput(bdev);
270 	}
271 
272 	while (fdata->start_unit <= fdata->stop_unit) {
273 		cqr = device->discipline->format_device(device, fdata);
274 		if (IS_ERR(cqr))
275 			return PTR_ERR(cqr);
276 		rc = dasd_sleep_on_interruptible(cqr);
277 		dasd_sfree_request(cqr, cqr->device);
278 		if (rc) {
279 			if (rc != -ERESTARTSYS)
280 				DEV_MESSAGE(KERN_ERR, device,
281 					    " Formatting of unit %d failed "
282 					    "with rc = %d",
283 					    fdata->start_unit, rc);
284 			return rc;
285 		}
286 		fdata->start_unit++;
287 	}
288 	return 0;
289 }
290 
291 /*
292  * Format device.
293  */
294 static int
295 dasd_ioctl_format(struct block_device *bdev, int no, long args)
296 {
297 	struct dasd_device *device;
298 	struct format_data_t fdata;
299 
300 	if (!capable(CAP_SYS_ADMIN))
301 		return -EACCES;
302 	if (!args)
303 		return -EINVAL;
304 	/* fdata == NULL is no longer a valid arg to dasd_format ! */
305 	device = bdev->bd_disk->private_data;
306 
307 	if (device == NULL)
308 		return -ENODEV;
309 
310 	if (device->features & DASD_FEATURE_READONLY)
311 		return -EROFS;
312 	if (copy_from_user(&fdata, (void __user *) args,
313 			   sizeof (struct format_data_t)))
314 		return -EFAULT;
315 	if (bdev != bdev->bd_contains) {
316 		DEV_MESSAGE(KERN_WARNING, device, "%s",
317 			    "Cannot low-level format a partition");
318 		return -EINVAL;
319 	}
320 	return dasd_format(device, &fdata);
321 }
322 
323 #ifdef CONFIG_DASD_PROFILE
324 /*
325  * Reset device profile information
326  */
327 static int
328 dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
329 {
330 	struct dasd_device *device;
331 
332 	if (!capable(CAP_SYS_ADMIN))
333 		return -EACCES;
334 
335 	device = bdev->bd_disk->private_data;
336 	if (device == NULL)
337 		return -ENODEV;
338 
339 	memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
340 	return 0;
341 }
342 
343 /*
344  * Return device profile information
345  */
346 static int
347 dasd_ioctl_read_profile(struct block_device *bdev, int no, long args)
348 {
349 	struct dasd_device *device;
350 
351 	device = bdev->bd_disk->private_data;
352 	if (device == NULL)
353 		return -ENODEV;
354 
355 	if (copy_to_user((long __user *) args, (long *) &device->profile,
356 			 sizeof (struct dasd_profile_info_t)))
357 		return -EFAULT;
358 	return 0;
359 }
360 #else
361 static int
362 dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
363 {
364 	return -ENOSYS;
365 }
366 
367 static int
368 dasd_ioctl_read_profile(struct block_device *bdev, int no, long args)
369 {
370 	return -ENOSYS;
371 }
372 #endif
373 
374 /*
375  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
376  */
377 static int
378 dasd_ioctl_information(struct block_device *bdev, int no, long args)
379 {
380 	struct dasd_device *device;
381 	struct dasd_information2_t *dasd_info;
382 	unsigned long flags;
383 	int rc;
384 	struct ccw_device *cdev;
385 
386 	device = bdev->bd_disk->private_data;
387 	if (device == NULL)
388 		return -ENODEV;
389 
390 	if (!device->discipline->fill_info)
391 		return -EINVAL;
392 
393 	dasd_info = kmalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
394 	if (dasd_info == NULL)
395 		return -ENOMEM;
396 
397 	rc = device->discipline->fill_info(device, dasd_info);
398 	if (rc) {
399 		kfree(dasd_info);
400 		return rc;
401 	}
402 
403 	cdev = device->cdev;
404 
405 	dasd_info->devno = _ccw_device_get_device_number(device->cdev);
406 	dasd_info->schid = _ccw_device_get_subchannel_number(device->cdev);
407 	dasd_info->cu_type = cdev->id.cu_type;
408 	dasd_info->cu_model = cdev->id.cu_model;
409 	dasd_info->dev_type = cdev->id.dev_type;
410 	dasd_info->dev_model = cdev->id.dev_model;
411 	dasd_info->open_count = atomic_read(&device->open_count);
412 	dasd_info->status = device->state;
413 
414 	/*
415 	 * check if device is really formatted
416 	 * LDL / CDL was returned by 'fill_info'
417 	 */
418 	if ((device->state < DASD_STATE_READY) ||
419 	    (dasd_check_blocksize(device->bp_block)))
420 		dasd_info->format = DASD_FORMAT_NONE;
421 
422 	dasd_info->features |=
423 		((device->features & DASD_FEATURE_READONLY) != 0);
424 
425 	if (device->discipline)
426 		memcpy(dasd_info->type, device->discipline->name, 4);
427 	else
428 		memcpy(dasd_info->type, "none", 4);
429 	dasd_info->req_queue_len = 0;
430 	dasd_info->chanq_len = 0;
431 	if (device->request_queue->request_fn) {
432 		struct list_head *l;
433 #ifdef DASD_EXTENDED_PROFILING
434 		{
435 			struct list_head *l;
436 			spin_lock_irqsave(&device->lock, flags);
437 			list_for_each(l, &device->request_queue->queue_head)
438 				dasd_info->req_queue_len++;
439 			spin_unlock_irqrestore(&device->lock, flags);
440 		}
441 #endif				/* DASD_EXTENDED_PROFILING */
442 		spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
443 		list_for_each(l, &device->ccw_queue)
444 			dasd_info->chanq_len++;
445 		spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
446 				       flags);
447 	}
448 
449 	rc = 0;
450 	if (copy_to_user((long __user *) args, (long *) dasd_info,
451 			 ((no == (unsigned int) BIODASDINFO2) ?
452 			  sizeof (struct dasd_information2_t) :
453 			  sizeof (struct dasd_information_t))))
454 		rc = -EFAULT;
455 	kfree(dasd_info);
456 	return rc;
457 }
458 
459 /*
460  * Set read only
461  */
462 static int
463 dasd_ioctl_set_ro(struct block_device *bdev, int no, long args)
464 {
465 	struct dasd_device *device;
466 	int intval, rc;
467 
468 	if (!capable(CAP_SYS_ADMIN))
469 		return -EACCES;
470 	if (bdev != bdev->bd_contains)
471 		// ro setting is not allowed for partitions
472 		return -EINVAL;
473 	if (get_user(intval, (int __user *) args))
474 		return -EFAULT;
475 	device =  bdev->bd_disk->private_data;
476 	if (device == NULL)
477 		return -ENODEV;
478 
479 	set_disk_ro(bdev->bd_disk, intval);
480 	rc = dasd_set_feature(device->cdev, DASD_FEATURE_READONLY, intval);
481 
482 	return rc;
483 }
484 
485 /*
486  * Return disk geometry.
487  */
488 static int
489 dasd_ioctl_getgeo(struct block_device *bdev, int no, long args)
490 {
491 	struct hd_geometry geo = { 0, };
492 	struct dasd_device *device;
493 
494 	device =  bdev->bd_disk->private_data;
495 	if (device == NULL)
496 		return -ENODEV;
497 
498 	if (device == NULL || device->discipline == NULL ||
499 	    device->discipline->fill_geometry == NULL)
500 		return -EINVAL;
501 
502 	geo = (struct hd_geometry) {};
503 	device->discipline->fill_geometry(device, &geo);
504 	geo.start = get_start_sect(bdev) >> device->s2b_shift;
505 	if (copy_to_user((struct hd_geometry __user *) args, &geo,
506 			 sizeof (struct hd_geometry)))
507 		return -EFAULT;
508 
509 	return 0;
510 }
511 
512 /*
513  * List of static ioctls.
514  */
515 static struct { int no; dasd_ioctl_fn_t fn; } dasd_ioctls[] =
516 {
517 	{ BIODASDDISABLE, dasd_ioctl_disable },
518 	{ BIODASDENABLE, dasd_ioctl_enable },
519 	{ BIODASDQUIESCE, dasd_ioctl_quiesce },
520 	{ BIODASDRESUME, dasd_ioctl_resume },
521 	{ BIODASDFMT, dasd_ioctl_format },
522 	{ BIODASDINFO, dasd_ioctl_information },
523 	{ BIODASDINFO2, dasd_ioctl_information },
524 	{ BIODASDPRRD, dasd_ioctl_read_profile },
525 	{ BIODASDPRRST, dasd_ioctl_reset_profile },
526 	{ BLKROSET, dasd_ioctl_set_ro },
527 	{ DASDAPIVER, dasd_ioctl_api_version },
528 	{ HDIO_GETGEO, dasd_ioctl_getgeo },
529 	{ -1, NULL }
530 };
531 
532 int
533 dasd_ioctl_init(void)
534 {
535 	int i;
536 
537 	for (i = 0; dasd_ioctls[i].no != -1; i++)
538 		dasd_ioctl_no_register(NULL, dasd_ioctls[i].no,
539 				       dasd_ioctls[i].fn);
540 	return 0;
541 
542 }
543 
544 void
545 dasd_ioctl_exit(void)
546 {
547 	int i;
548 
549 	for (i = 0; dasd_ioctls[i].no != -1; i++)
550 		dasd_ioctl_no_unregister(NULL, dasd_ioctls[i].no,
551 					 dasd_ioctls[i].fn);
552 
553 }
554 
555 EXPORT_SYMBOL(dasd_ioctl_no_register);
556 EXPORT_SYMBOL(dasd_ioctl_no_unregister);
557