vserver 1.9.5.x5
[linux-2.6.git] / drivers / s390 / block / dasd_ioctl.c
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/uaccess.h>
20
21 /* This is ugly... */
22 #define PRINTK_HEADER "dasd_ioctl:"
23
24 #include "dasd_int.h"
25
26 /*
27  * SECTION: ioctl functions.
28  */
29 static struct list_head dasd_ioctl_list = LIST_HEAD_INIT(dasd_ioctl_list);
30
31 /*
32  * Find the ioctl with number no.
33  */
34 static struct dasd_ioctl *
35 dasd_find_ioctl(int no)
36 {
37         struct dasd_ioctl *ioctl;
38
39         list_for_each_entry (ioctl, &dasd_ioctl_list, list)
40                 if (ioctl->no == no)
41                         return ioctl;
42         return NULL;
43 }
44
45 /*
46  * Register ioctl with number no.
47  */
48 int
49 dasd_ioctl_no_register(struct module *owner, int no, dasd_ioctl_fn_t handler)
50 {
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;
62 }
63
64 /*
65  * Deregister ioctl with number no.
66  */
67 int
68 dasd_ioctl_no_unregister(struct module *owner, int no, dasd_ioctl_fn_t handler)
69 {
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;
78 }
79
80 int
81 dasd_ioctl(struct inode *inp, struct file *filp,
82            unsigned int no, unsigned long data)
83 {
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;
89
90         if ((_IOC_DIR(no) != _IOC_NONE) && (data == 0)) {
91                 PRINT_DEBUG("empty data ptr");
92                 return -EINVAL;
93         }
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;
110                 }
111         }
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;
117 }
118
119 static int
120 dasd_ioctl_api_version(struct block_device *bdev, int no, long args)
121 {
122         int ver = DASD_API_VERSION;
123         return put_user(ver, (int __user *) args);
124 }
125
126 /*
127  * Enable device.
128  * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
129  */
130 static int
131 dasd_ioctl_enable(struct block_device *bdev, int no, long args)
132 {
133         struct dasd_device *device;
134
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;
146 }
147
148 /*
149  * Disable device.
150  * Used by dasdfmt. Disable I/O operations but allow ioctls.
151  */
152 static int
153 dasd_ioctl_disable(struct block_device *bdev, int no, long args)
154 {
155         struct dasd_device *device;
156
157         if (!capable(CAP_SYS_ADMIN))
158                 return -EACCES;
159         device = bdev->bd_disk->private_data;
160         if (device == NULL)
161                 return -ENODEV;
162         /*
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.
169          */
170         dasd_set_target_state(device, DASD_STATE_BASIC);
171         /*
172          * Set i_size to zero, since read, write, etc. check against this
173          * value.
174          */
175         down(&bdev->bd_sem);
176         i_size_write(bdev->bd_inode, 0);
177         up(&bdev->bd_sem);
178         return 0;
179 }
180
181 /*
182  * Quiesce device.
183  */
184 static int
185 dasd_ioctl_quiesce(struct block_device *bdev, int no, long args)
186 {
187         struct dasd_device *device;
188         unsigned long flags;
189         
190         if (!capable (CAP_SYS_ADMIN))
191                 return -EACCES;
192         
193         device = bdev->bd_disk->private_data;
194         if (device == NULL)
195                 return -ENODEV;
196         
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;
203 }
204
205
206 /*
207  * Quiesce device.
208  */
209 static int
210 dasd_ioctl_resume(struct block_device *bdev, int no, long args)
211 {
212         struct dasd_device *device;
213         unsigned long flags;
214         
215         if (!capable (CAP_SYS_ADMIN)) 
216                 return -EACCES;
217
218         device = bdev->bd_disk->private_data;
219         if (device == NULL)
220                 return -ENODEV;
221
222         DEV_MESSAGE (KERN_DEBUG, device, "%s",
223                      "resume IO on device");
224         
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);
228
229         dasd_schedule_bh (device);
230         return 0;
231 }
232
233 /*
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.
238  */
239 static int
240 dasd_format(struct dasd_device * device, struct format_data_t * fdata)
241 {
242         struct dasd_ccw_req *cqr;
243         int rc;
244
245         if (device->discipline->format_device == NULL)
246                 return -EPERM;
247
248         if (device->state != DASD_STATE_BASIC) {
249                 DEV_MESSAGE(KERN_WARNING, device, "%s",
250                             "dasd_format: device is not disabled! ");
251                 return -EBUSY;
252         }
253
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);
258
259         /* Since dasdfmt keeps the device open after it was disabled,
260          * there still exists an inode for this device.
261          * We must update i_blkbits, otherwise we might get errors when
262          * enabling the device later.
263          */
264         if (fdata->start_unit == 0) {
265                 struct block_device *bdev = bdget_disk(device->gdp, 0);
266                 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
267                 bdput(bdev);
268         }
269
270         while (fdata->start_unit <= fdata->stop_unit) {
271                 cqr = device->discipline->format_device(device, fdata);
272                 if (IS_ERR(cqr))
273                         return PTR_ERR(cqr);
274                 rc = dasd_sleep_on_interruptible(cqr);
275                 dasd_sfree_request(cqr, cqr->device);
276                 if (rc) {
277                         if (rc != -ERESTARTSYS)
278                                 DEV_MESSAGE(KERN_ERR, device,
279                                             " Formatting of unit %d failed "
280                                             "with rc = %d",
281                                             fdata->start_unit, rc);
282                         return rc;
283                 }
284                 fdata->start_unit++;
285         }
286         return 0;
287 }
288
289 /*
290  * Format device.
291  */
292 static int
293 dasd_ioctl_format(struct block_device *bdev, int no, long args)
294 {
295         struct dasd_device *device;
296         struct format_data_t fdata;
297
298         if (!capable(CAP_SYS_ADMIN))
299                 return -EACCES;
300         if (!args)
301                 return -EINVAL;
302         /* fdata == NULL is no longer a valid arg to dasd_format ! */
303         device = bdev->bd_disk->private_data;
304
305         if (device == NULL)
306                 return -ENODEV;
307         if (test_bit(DASD_FLAG_RO, &device->flags))
308                 return -EROFS;
309         if (copy_from_user(&fdata, (void __user *) args,
310                            sizeof (struct format_data_t)))
311                 return -EFAULT;
312         if (bdev != bdev->bd_contains) {
313                 DEV_MESSAGE(KERN_WARNING, device, "%s",
314                             "Cannot low-level format a partition");
315                 return -EINVAL;
316         }
317         return dasd_format(device, &fdata);
318 }
319
320 #ifdef CONFIG_DASD_PROFILE
321 /*
322  * Reset device profile information
323  */
324 static int
325 dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
326 {
327         struct dasd_device *device;
328
329         if (!capable(CAP_SYS_ADMIN))
330                 return -EACCES;
331
332         device = bdev->bd_disk->private_data;
333         if (device == NULL)
334                 return -ENODEV;
335
336         memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
337         return 0;
338 }
339
340 /*
341  * Return device profile information
342  */
343 static int
344 dasd_ioctl_read_profile(struct block_device *bdev, int no, long args)
345 {
346         struct dasd_device *device;
347
348         device = bdev->bd_disk->private_data;
349         if (device == NULL)
350                 return -ENODEV;
351
352         if (copy_to_user((long __user *) args, (long *) &device->profile,
353                          sizeof (struct dasd_profile_info_t)))
354                 return -EFAULT;
355         return 0;
356 }
357 #else
358 static int
359 dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
360 {
361         return -ENOSYS;
362 }
363
364 static int
365 dasd_ioctl_read_profile(struct block_device *bdev, int no, long args)
366 {
367         return -ENOSYS;
368 }
369 #endif
370
371 /*
372  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
373  */
374 static int
375 dasd_ioctl_information(struct block_device *bdev, int no, long args)
376 {
377         struct dasd_device *device;
378         struct dasd_information2_t *dasd_info;
379         unsigned long flags;
380         int rc;
381         struct ccw_device *cdev;
382
383         device = bdev->bd_disk->private_data;
384         if (device == NULL)
385                 return -ENODEV;
386
387         if (!device->discipline->fill_info)
388                 return -EINVAL;
389
390         dasd_info = kmalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
391         if (dasd_info == NULL)
392                 return -ENOMEM;
393
394         rc = device->discipline->fill_info(device, dasd_info);
395         if (rc) {
396                 kfree(dasd_info);
397                 return rc;
398         }
399
400         cdev = device->cdev;
401
402         dasd_info->devno = _ccw_device_get_device_number(device->cdev);
403         dasd_info->schid = _ccw_device_get_subchannel_number(device->cdev);
404         dasd_info->cu_type = cdev->id.cu_type;
405         dasd_info->cu_model = cdev->id.cu_model;
406         dasd_info->dev_type = cdev->id.dev_type;
407         dasd_info->dev_model = cdev->id.dev_model;
408         dasd_info->open_count = atomic_read(&device->open_count);
409         dasd_info->status = device->state;
410         
411         /*
412          * check if device is really formatted
413          * LDL / CDL was returned by 'fill_info'
414          */
415         if ((device->state < DASD_STATE_READY) ||
416             (dasd_check_blocksize(device->bp_block)))
417                 dasd_info->format = DASD_FORMAT_NONE;
418         
419         dasd_info->features |= test_bit(DASD_FLAG_RO, &device->flags) ?
420                 DASD_FEATURE_READONLY : DASD_FEATURE_DEFAULT;
421
422         if (device->discipline)
423                 memcpy(dasd_info->type, device->discipline->name, 4);
424         else
425                 memcpy(dasd_info->type, "none", 4);
426         dasd_info->req_queue_len = 0;
427         dasd_info->chanq_len = 0;
428         if (device->request_queue->request_fn) {
429                 struct list_head *l;
430 #ifdef DASD_EXTENDED_PROFILING
431                 {
432                         struct list_head *l;
433                         spin_lock_irqsave(&device->lock, flags);
434                         list_for_each(l, &device->request_queue->queue_head)
435                                 dasd_info->req_queue_len++;
436                         spin_unlock_irqrestore(&device->lock, flags);
437                 }
438 #endif                          /* DASD_EXTENDED_PROFILING */
439                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
440                 list_for_each(l, &device->ccw_queue)
441                         dasd_info->chanq_len++;
442                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
443                                        flags);
444         }
445
446         rc = 0;
447         if (copy_to_user((long __user *) args, (long *) dasd_info,
448                          ((no == (unsigned int) BIODASDINFO2) ?
449                           sizeof (struct dasd_information2_t) :
450                           sizeof (struct dasd_information_t))))
451                 rc = -EFAULT;
452         kfree(dasd_info);
453         return rc;
454 }
455
456 /*
457  * Set read only
458  */
459 static int
460 dasd_ioctl_set_ro(struct block_device *bdev, int no, long args)
461 {
462         struct dasd_device *device;
463         int intval;
464
465         if (!capable(CAP_SYS_ADMIN))
466                 return -EACCES;
467         if (bdev != bdev->bd_contains)
468                 // ro setting is not allowed for partitions
469                 return -EINVAL;
470         if (get_user(intval, (int __user *) args))
471                 return -EFAULT;
472         device =  bdev->bd_disk->private_data;
473         if (device == NULL)
474                 return -ENODEV;
475         set_disk_ro(bdev->bd_disk, intval);
476         if (intval)
477                 set_bit(DASD_FLAG_RO, &device->flags);
478         else
479                 clear_bit(DASD_FLAG_RO, &device->flags);
480         return 0;
481 }
482
483 /*
484  * Return disk geometry.
485  */
486 static int
487 dasd_ioctl_getgeo(struct block_device *bdev, int no, long args)
488 {
489         struct hd_geometry geo = { 0, };
490         struct dasd_device *device;
491
492         device =  bdev->bd_disk->private_data;
493         if (device == NULL)
494                 return -ENODEV;
495
496         if (device == NULL || device->discipline == NULL ||
497             device->discipline->fill_geometry == NULL)
498                 return -EINVAL;
499
500         geo = (struct hd_geometry) {};
501         device->discipline->fill_geometry(device, &geo);
502         geo.start = get_start_sect(bdev) >> device->s2b_shift;
503         if (copy_to_user((struct hd_geometry __user *) args, &geo,
504                          sizeof (struct hd_geometry)))
505                 return -EFAULT;
506
507         return 0;
508 }
509
510 /*
511  * List of static ioctls.
512  */
513 static struct { int no; dasd_ioctl_fn_t fn; } dasd_ioctls[] =
514 {
515         { BIODASDDISABLE, dasd_ioctl_disable },
516         { BIODASDENABLE, dasd_ioctl_enable },
517         { BIODASDQUIESCE, dasd_ioctl_quiesce },
518         { BIODASDRESUME, dasd_ioctl_resume },
519         { BIODASDFMT, dasd_ioctl_format },
520         { BIODASDINFO, dasd_ioctl_information },
521         { BIODASDINFO2, dasd_ioctl_information },
522         { BIODASDPRRD, dasd_ioctl_read_profile },
523         { BIODASDPRRST, dasd_ioctl_reset_profile },
524         { BLKROSET, dasd_ioctl_set_ro },
525         { DASDAPIVER, dasd_ioctl_api_version },
526         { HDIO_GETGEO, dasd_ioctl_getgeo },
527         { -1, NULL }
528 };
529
530 int
531 dasd_ioctl_init(void)
532 {
533         int i;
534
535         for (i = 0; dasd_ioctls[i].no != -1; i++)
536                 dasd_ioctl_no_register(NULL, dasd_ioctls[i].no,
537                                        dasd_ioctls[i].fn);
538         return 0;
539
540 }
541
542 void
543 dasd_ioctl_exit(void)
544 {
545         int i;
546
547         for (i = 0; dasd_ioctls[i].no != -1; i++)
548                 dasd_ioctl_no_unregister(NULL, dasd_ioctls[i].no,
549                                          dasd_ioctls[i].fn);
550
551 }
552
553 EXPORT_SYMBOL(dasd_ioctl_no_register);
554 EXPORT_SYMBOL(dasd_ioctl_no_unregister);