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