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