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