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