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>
16 #include <linux/blkpg.h>
18 #include <asm/ccwdev.h>
19 #include <asm/uaccess.h>
22 #define PRINTK_HEADER "dasd_ioctl:"
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)
46 * Register ioctl with number no.
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))
54 new = kmalloc(sizeof (struct dasd_ioctl), GFP_KERNEL);
59 new->handler = handler;
60 list_add(&new->list, &dasd_ioctl_list);
65 * Deregister ioctl with number no.
68 dasd_ioctl_no_unregister(struct module *owner, int no, dasd_ioctl_fn_t handler)
70 struct dasd_ioctl *old = dasd_find_ioctl(no);
73 if (old->no != no || old->handler != handler || owner != old->owner)
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;
90 if ((_IOC_DIR(no) != _IOC_NONE) && (data == 0)) {
91 PRINT_DEBUG("empty data ptr");
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))
107 rc = ioctl->handler(bdev, no, data);
108 module_put(ioctl->owner);
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);
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);
128 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
131 dasd_ioctl_enable(struct block_device *bdev, int no, long args)
133 struct dasd_device *device;
135 if (!capable(CAP_SYS_ADMIN))
137 device = bdev->bd_disk->private_data;
140 dasd_enable_device(device);
141 /* Formatting the dasd device can change the capacity. */
143 i_size_write(bdev->bd_inode, (loff_t)get_capacity(device->gdp) << 9);
150 * Used by dasdfmt. Disable I/O operations but allow ioctls.
153 dasd_ioctl_disable(struct block_device *bdev, int no, long args)
155 struct dasd_device *device;
157 if (!capable(CAP_SYS_ADMIN))
159 device = bdev->bd_disk->private_data;
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
176 i_size_write(bdev->bd_inode, 0);
185 dasd_ioctl_quiesce(struct block_device *bdev, int no, long args)
187 struct dasd_device *device;
190 if (!capable (CAP_SYS_ADMIN))
193 device = bdev->bd_disk->private_data;
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);
210 dasd_ioctl_resume(struct block_device *bdev, int no, long args)
212 struct dasd_device *device;
215 if (!capable (CAP_SYS_ADMIN))
218 device = bdev->bd_disk->private_data;
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);
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.
240 dasd_format(struct dasd_device * device, struct format_data_t * fdata)
242 struct dasd_ccw_req *cqr;
245 if (device->discipline->format_device == NULL)
248 if (device->state != DASD_STATE_BASIC) {
249 DEV_MESSAGE(KERN_WARNING, device, "%s",
250 "dasd_format: device is not disabled! ");
254 DBF_DEV_EVENT(DBF_NOTICE, device,
255 "formatting units %d to %d (%d B blocks) flags %d",
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);
269 while (fdata->start_unit <= fdata->stop_unit) {
270 cqr = device->discipline->format_device(device, fdata);
273 rc = dasd_sleep_on_interruptible(cqr);
274 dasd_sfree_request(cqr, cqr->device);
276 if (rc != -ERESTARTSYS)
277 DEV_MESSAGE(KERN_ERR, device,
278 " Formatting of unit %d failed "
280 fdata->start_unit, rc);
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))
301 /* fdata == NULL is no longer a valid arg to dasd_format ! */
302 device = bdev->bd_disk->private_data;
306 if (test_bit(DASD_FLAG_RO, &device->flags))
308 if (copy_from_user(&fdata, (void __user *) args,
309 sizeof (struct format_data_t)))
311 if (bdev != bdev->bd_contains) {
312 DEV_MESSAGE(KERN_WARNING, device, "%s",
313 "Cannot low-level format a partition");
316 return dasd_format(device, &fdata);
319 #ifdef CONFIG_DASD_PROFILE
321 * Reset device profile information
324 dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
326 struct dasd_device *device;
328 if (!capable(CAP_SYS_ADMIN))
331 device = bdev->bd_disk->private_data;
335 memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
340 * Return device profile information
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;
351 if (copy_to_user((long __user *) args, (long *) &device->profile,
352 sizeof (struct dasd_profile_info_t)))
358 dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
364 dasd_ioctl_read_profile(struct block_device *bdev, int no, long args)
371 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
374 dasd_ioctl_information(struct block_device *bdev, int no, long args)
376 struct dasd_device *device;
377 struct dasd_information2_t *dasd_info;
380 struct ccw_device *cdev;
382 device = bdev->bd_disk->private_data;
386 if (!device->discipline->fill_info)
389 dasd_info = kmalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
390 if (dasd_info == NULL)
393 rc = device->discipline->fill_info(device, dasd_info);
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);
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) {
429 #ifdef DASD_EXTENDED_PROFILING
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),
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))))
459 dasd_ioctl_set_ro(struct block_device *bdev, int no, long args)
461 struct dasd_device *device;
464 if (!capable(CAP_SYS_ADMIN))
466 if (bdev != bdev->bd_contains)
467 // ro setting is not allowed for partitions
469 if (get_user(intval, (int __user *) args))
471 device = bdev->bd_disk->private_data;
474 set_disk_ro(bdev->bd_disk, intval);
476 set_bit(DASD_FLAG_RO, &device->flags);
478 clear_bit(DASD_FLAG_RO, &device->flags);
483 * Return disk geometry.
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;
495 if (device == NULL || device->discipline == NULL ||
496 device->discipline->fill_geometry == NULL)
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)))
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 },
530 dasd_ioctl_init(void)
534 for (i = 0; dasd_ioctls[i].no != -1; i++)
535 dasd_ioctl_no_register(NULL, dasd_ioctls[i].no,
542 dasd_ioctl_exit(void)
546 for (i = 0; dasd_ioctls[i].no != -1; i++)
547 dasd_ioctl_no_unregister(NULL, dasd_ioctls[i].no,
552 EXPORT_SYMBOL(dasd_ioctl_no_register);
553 EXPORT_SYMBOL(dasd_ioctl_no_unregister);