[PATCH] s390: use normal switch statement for ioctls in dasd_ioctlc
[deliverable/linux.git] / drivers / s390 / block / dasd_ioctl.c
CommitLineData
1da177e4
LT
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 */
29static struct list_head dasd_ioctl_list = LIST_HEAD_INIT(dasd_ioctl_list);
30
31/*
32 * Find the ioctl with number no.
33 */
34static struct dasd_ioctl *
35dasd_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 */
48int
49dasd_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 */
67int
68dasd_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
1da177e4 80static int
13c6204f 81dasd_ioctl_api_version(void __user *argp)
1da177e4
LT
82{
83 int ver = DASD_API_VERSION;
13c6204f 84 return put_user(ver, (int *)argp);
1da177e4
LT
85}
86
87/*
88 * Enable device.
89 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
90 */
91static int
13c6204f 92dasd_ioctl_enable(struct block_device *bdev)
1da177e4 93{
13c6204f 94 struct dasd_device *device = bdev->bd_disk->private_data;
1da177e4
LT
95
96 if (!capable(CAP_SYS_ADMIN))
97 return -EACCES;
13c6204f 98
1da177e4
LT
99 dasd_enable_device(device);
100 /* Formatting the dasd device can change the capacity. */
c039e313 101 mutex_lock(&bdev->bd_mutex);
1da177e4 102 i_size_write(bdev->bd_inode, (loff_t)get_capacity(device->gdp) << 9);
c039e313 103 mutex_unlock(&bdev->bd_mutex);
1da177e4
LT
104 return 0;
105}
106
107/*
108 * Disable device.
109 * Used by dasdfmt. Disable I/O operations but allow ioctls.
110 */
111static int
13c6204f 112dasd_ioctl_disable(struct block_device *bdev)
1da177e4 113{
13c6204f 114 struct dasd_device *device = bdev->bd_disk->private_data;
1da177e4
LT
115
116 if (!capable(CAP_SYS_ADMIN))
117 return -EACCES;
13c6204f 118
1da177e4
LT
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 */
c039e313 132 mutex_lock(&bdev->bd_mutex);
1da177e4 133 i_size_write(bdev->bd_inode, 0);
c039e313 134 mutex_unlock(&bdev->bd_mutex);
1da177e4
LT
135 return 0;
136}
137
138/*
139 * Quiesce device.
140 */
141static int
13c6204f 142dasd_ioctl_quiesce(struct dasd_device *device)
1da177e4 143{
1da177e4
LT
144 unsigned long flags;
145
146 if (!capable (CAP_SYS_ADMIN))
147 return -EACCES;
148
1da177e4
LT
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 */
161static int
13c6204f 162dasd_ioctl_resume(struct dasd_device *device)
1da177e4 163{
1da177e4
LT
164 unsigned long flags;
165
166 if (!capable (CAP_SYS_ADMIN))
167 return -EACCES;
168
1da177e4
LT
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 */
186static int
187dasd_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 */
239static int
13c6204f 240dasd_ioctl_format(struct block_device *bdev, void __user *argp)
1da177e4 241{
13c6204f 242 struct dasd_device *device = bdev->bd_disk->private_data;
1da177e4
LT
243 struct format_data_t fdata;
244
245 if (!capable(CAP_SYS_ADMIN))
246 return -EACCES;
13c6204f 247 if (!argp)
1da177e4 248 return -EINVAL;
f24acd45 249
c6eb7b77 250 if (device->features & DASD_FEATURE_READONLY)
1da177e4 251 return -EROFS;
13c6204f 252 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
1da177e4
LT
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 */
266static int
13c6204f 267dasd_ioctl_reset_profile(struct dasd_device *device)
1da177e4 268{
1da177e4
LT
269 memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
270 return 0;
271}
272
273/*
274 * Return device profile information
275 */
276static int
13c6204f 277dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
1da177e4 278{
9a7af289
HH
279 if (dasd_profile_level == DASD_PROFILE_OFF)
280 return -EIO;
13c6204f 281 if (copy_to_user(argp, &device->profile,
1da177e4
LT
282 sizeof (struct dasd_profile_info_t)))
283 return -EFAULT;
284 return 0;
285}
286#else
287static int
13c6204f 288dasd_ioctl_reset_profile(struct dasd_device *device)
1da177e4
LT
289{
290 return -ENOSYS;
291}
292
293static int
13c6204f 294dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
1da177e4
LT
295{
296 return -ENOSYS;
297}
298#endif
299
300/*
301 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
302 */
303static int
13c6204f
CH
304dasd_ioctl_information(struct dasd_device *device,
305 unsigned int cmd, void __user *argp)
1da177e4 306{
1da177e4
LT
307 struct dasd_information2_t *dasd_info;
308 unsigned long flags;
c6eb7b77 309 int rc;
1da177e4
LT
310 struct ccw_device *cdev;
311
1da177e4
LT
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;
1da177e4 333 dasd_info->status = device->state;
57467195
HH
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++;
1da177e4
LT
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;
f24acd45 350
c6eb7b77
HH
351 dasd_info->features |=
352 ((device->features & DASD_FEATURE_READONLY) != 0);
1da177e4
LT
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;
13c6204f
CH
379 if (copy_to_user(argp, dasd_info,
380 ((cmd == (unsigned int) BIODASDINFO2) ?
1da177e4
LT
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 */
391static int
13c6204f 392dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
1da177e4 393{
13c6204f
CH
394 struct dasd_device *device = bdev->bd_disk->private_data;
395 int intval;
1da177e4
LT
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;
13c6204f 402 if (get_user(intval, (int *)argp))
1da177e4 403 return -EFAULT;
f24acd45 404
1da177e4 405 set_disk_ro(bdev->bd_disk, intval);
13c6204f 406 return dasd_set_feature(device->cdev, DASD_FEATURE_READONLY, intval);
1da177e4
LT
407}
408
1da177e4 409int
13c6204f
CH
410dasd_ioctl(struct inode *inode, struct file *file,
411 unsigned int cmd, unsigned long arg)
1da177e4 412{
13c6204f
CH
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;
1da177e4 418
13c6204f
CH
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 }
1da177e4 426
13c6204f
CH
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:
1107ccfb
CH
451 /* if the discipline has an ioctl method try it. */
452 if (device->discipline->ioctl) {
453 int rval = device->discipline->ioctl(device, cmd, argp);
454 if (rval != -ENOIOCTLCMD)
455 return rval;
456 }
457
458 /* else resort to the deprecated dynamic ioctl list */
13c6204f
CH
459 list_for_each_entry(ioctl, &dasd_ioctl_list, list) {
460 if (ioctl->no == cmd) {
461 /* Found a matching ioctl. Call it. */
462 if (!try_module_get(ioctl->owner))
463 continue;
464 rc = ioctl->handler(bdev, cmd, arg);
465 module_put(ioctl->owner);
466 return rc;
467 }
468 }
469 return -EINVAL;
470 }
1da177e4
LT
471}
472
13c6204f
CH
473long
474dasd_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4 475{
13c6204f 476 int rval;
1da177e4 477
13c6204f
CH
478 lock_kernel();
479 rval = dasd_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
480 unlock_kernel();
1da177e4 481
13c6204f 482 return (rval == -EINVAL) ? -ENOIOCTLCMD : rval;
1da177e4
LT
483}
484
485EXPORT_SYMBOL(dasd_ioctl_no_register);
486EXPORT_SYMBOL(dasd_ioctl_no_unregister);
This page took 0.151494 seconds and 5 git commands to generate.