blob: bb6caf46bbd9a0b403a66ab4d3ae2803411c70fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080081dasd_ioctl_api_version(void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 int ver = DASD_API_VERSION;
Christoph Hellwig13c62042006-03-24 03:15:19 -080084 return put_user(ver, (int *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/*
88 * Enable device.
89 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
90 */
91static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080092dasd_ioctl_enable(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Christoph Hellwig13c62042006-03-24 03:15:19 -080094 struct dasd_device *device = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (!capable(CAP_SYS_ADMIN))
97 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -080098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 dasd_enable_device(device);
100 /* Formatting the dasd device can change the capacity. */
Arjan van de Venc039e312006-03-23 03:00:28 -0800101 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 i_size_write(bdev->bd_inode, (loff_t)get_capacity(device->gdp) << 9);
Arjan van de Venc039e312006-03-23 03:00:28 -0800103 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
105}
106
107/*
108 * Disable device.
109 * Used by dasdfmt. Disable I/O operations but allow ioctls.
110 */
111static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800112dasd_ioctl_disable(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800114 struct dasd_device *device = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (!capable(CAP_SYS_ADMIN))
117 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /*
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 */
Arjan van de Venc039e312006-03-23 03:00:28 -0800132 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 i_size_write(bdev->bd_inode, 0);
Arjan van de Venc039e312006-03-23 03:00:28 -0800134 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
138/*
139 * Quiesce device.
140 */
141static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800142dasd_ioctl_quiesce(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long flags;
145
146 if (!capable (CAP_SYS_ADMIN))
147 return -EACCES;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 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
Christoph Hellwig13c62042006-03-24 03:15:19 -0800162dasd_ioctl_resume(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned long flags;
165
166 if (!capable (CAP_SYS_ADMIN))
167 return -EACCES;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 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
Christoph Hellwig13c62042006-03-24 03:15:19 -0800240dasd_ioctl_format(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800242 struct dasd_device *device = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 struct format_data_t fdata;
244
245 if (!capable(CAP_SYS_ADMIN))
246 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800247 if (!argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EINVAL;
Horst Hummelf24acd42005-05-01 08:58:59 -0700249
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700250 if (device->features & DASD_FEATURE_READONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EROFS;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800252 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 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
Christoph Hellwig13c62042006-03-24 03:15:19 -0800267dasd_ioctl_reset_profile(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
270 return 0;
271}
272
273/*
274 * Return device profile information
275 */
276static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800277dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Horst Hummel9a7af282006-01-06 00:19:14 -0800279 if (dasd_profile_level == DASD_PROFILE_OFF)
280 return -EIO;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800281 if (copy_to_user(argp, &device->profile,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 sizeof (struct dasd_profile_info_t)))
283 return -EFAULT;
284 return 0;
285}
286#else
287static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800288dasd_ioctl_reset_profile(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 return -ENOSYS;
291}
292
293static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800294dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 return -ENOSYS;
297}
298#endif
299
300/*
301 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
302 */
303static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800304dasd_ioctl_information(struct dasd_device *device,
305 unsigned int cmd, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct dasd_information2_t *dasd_info;
308 unsigned long flags;
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700309 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct ccw_device *cdev;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 dasd_info->status = device->state;
Horst Hummel57467192006-02-01 03:06:36 -0800334 /*
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++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
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;
Horst Hummelf24acd42005-05-01 08:58:59 -0700350
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700351 dasd_info->features |=
352 ((device->features & DASD_FEATURE_READONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
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;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800379 if (copy_to_user(argp, dasd_info,
380 ((cmd == (unsigned int) BIODASDINFO2) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 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
Christoph Hellwig13c62042006-03-24 03:15:19 -0800392dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800394 struct dasd_device *device = bdev->bd_disk->private_data;
395 int intval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
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;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800402 if (get_user(intval, (int *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return -EFAULT;
Horst Hummelf24acd42005-05-01 08:58:59 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 set_disk_ro(bdev->bd_disk, intval);
Christoph Hellwig13c62042006-03-24 03:15:19 -0800406 return dasd_set_feature(device->cdev, DASD_FEATURE_READONLY, intval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800410dasd_ioctl(struct inode *inode, struct file *file,
411 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800413 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Christoph Hellwig13c62042006-03-24 03:15:19 -0800419 if (!device)
420 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Christoph Hellwig13c62042006-03-24 03:15:19 -0800422 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:
Christoph Hellwig1107ccf2006-03-24 03:15:20 -0800451 /* 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 */
Christoph Hellwig13c62042006-03-24 03:15:19 -0800459 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Christoph Hellwig13c62042006-03-24 03:15:19 -0800473long
474dasd_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800476 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Christoph Hellwig13c62042006-03-24 03:15:19 -0800478 lock_kernel();
479 rval = dasd_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
480 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Christoph Hellwig13c62042006-03-24 03:15:19 -0800482 return (rval == -EINVAL) ? -ENOIOCTLCMD : rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485EXPORT_SYMBOL(dasd_ioctl_no_register);
486EXPORT_SYMBOL(dasd_ioctl_no_unregister);