patch-2_6_7-vs1_9_1_12
[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. We must update i_blkbits,
261          * otherwise we might get errors when enabling the device later.
262          */
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);
267         }
268
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;
282                 }
283                 fdata->start_unit++;
284         }
285         return 0;
286 }
287
288 /*
289  * Format device.
290  */
291 static int
292 dasd_ioctl_format(struct block_device *bdev, int no, long args)
293 {
294         struct dasd_device *device;
295         struct format_data_t fdata;
296
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;
303
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;
315         }
316         return dasd_format(device, &fdata);
317 }
318
319 #ifdef CONFIG_DASD_PROFILE
320 /*
321  * Reset device profile information
322  */
323 static int
324 dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
325 {
326         struct dasd_device *device;
327
328         if (!capable(CAP_SYS_ADMIN))
329                 return -EACCES;
330
331         device = bdev->bd_disk->private_data;
332         if (device == NULL)
333                 return -ENODEV;
334
335         memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
336         return 0;
337 }
338
339 /*
340  * Return device profile information
341  */
342 static int
343 dasd_ioctl_read_profile(struct block_device *bdev, int no, long args)
344 {
345         struct dasd_device *device;
346
347         device = bdev->bd_disk->private_data;
348         if (device == NULL)
349                 return -ENODEV;
350
351         if (copy_to_user((long __user *) args, (long *) &device->profile,
352                          sizeof (struct dasd_profile_info_t)))
353                 return -EFAULT;
354         return 0;
355 }
356 #else
357 static int
358 dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
359 {
360         return -ENOSYS;
361 }
362
363 static int
364 dasd_ioctl_read_profile(struct block_device *bdev, int no, long args)
365 {
366         return -ENOSYS;
367 }
368 #endif
369
370 /*
371  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
372  */
373 static int
374 dasd_ioctl_information(struct block_device *bdev, int no, long args)
375 {
376         struct dasd_device *device;
377         struct dasd_information2_t *dasd_info;
378         unsigned long flags;
379         int rc;
380         struct ccw_device *cdev;
381
382         device = bdev->bd_disk->private_data;
383         if (device == NULL)
384                 return -ENODEV;
385
386         if (!device->discipline->fill_info)
387                 return -EINVAL;
388
389         dasd_info = kmalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
390         if (dasd_info == NULL)
391                 return -ENOMEM;
392
393         rc = device->discipline->fill_info(device, dasd_info);
394         if (rc) {
395                 kfree(dasd_info);
396                 return rc;
397         }
398
399         cdev = device->cdev;
400
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;
409         
410         /*
411          * check if device is really formatted
412          * LDL / CDL was returned by 'fill_info'
413          */
414         if ((device->state < DASD_STATE_READY) ||
415             (dasd_check_blocksize(device->bp_block)))
416                 dasd_info->format = DASD_FORMAT_NONE;
417         
418         dasd_info->features |= test_bit(DASD_FLAG_RO, &device->flags) ?
419                 DASD_FEATURE_READONLY : DASD_FEATURE_DEFAULT;
420
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
430                 {
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);
436                 }
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);
443         }
444
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;
453 }
454
455 /*
456  * Set read only
457  */
458 static int
459 dasd_ioctl_set_ro(struct block_device *bdev, int no, long args)
460 {
461         struct dasd_device *device;
462         int intval;
463
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;
480 }
481
482 /*
483  * Return disk geometry.
484  */
485 static int
486 dasd_ioctl_getgeo(struct block_device *bdev, int no, long args)
487 {
488         struct hd_geometry geo = { 0, };
489         struct dasd_device *device;
490
491         device =  bdev->bd_disk->private_data;
492         if (device == NULL)
493                 return -ENODEV;
494
495         if (device == NULL || device->discipline == NULL ||
496             device->discipline->fill_geometry == NULL)
497                 return -EINVAL;
498
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;
505
506         return 0;
507 }
508
509 /*
510  * List of static ioctls.
511  */
512 static struct { int no; dasd_ioctl_fn_t fn; } dasd_ioctls[] =
513 {
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 }
527 };
528
529 int
530 dasd_ioctl_init(void)
531 {
532         int i;
533
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;
538
539 }
540
541 void
542 dasd_ioctl_exit(void)
543 {
544         int i;
545
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);
549
550 }
551
552 EXPORT_SYMBOL(dasd_ioctl_no_register);
553 EXPORT_SYMBOL(dasd_ioctl_no_unregister);