blob: c8319bef2730c3e63836895ecc02f315f3eb1f40 [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>
Christoph Hellwig8b2eb662006-03-24 03:15:21 -080019#include <asm/cmb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
21
22/* This is ugly... */
23#define PRINTK_HEADER "dasd_ioctl:"
24
25#include "dasd_int.h"
26
27/*
28 * SECTION: ioctl functions.
29 */
30static struct list_head dasd_ioctl_list = LIST_HEAD_INIT(dasd_ioctl_list);
31
32/*
33 * Find the ioctl with number no.
34 */
35static struct dasd_ioctl *
36dasd_find_ioctl(int no)
37{
38 struct dasd_ioctl *ioctl;
39
40 list_for_each_entry (ioctl, &dasd_ioctl_list, list)
41 if (ioctl->no == no)
42 return ioctl;
43 return NULL;
44}
45
46/*
47 * Register ioctl with number no.
48 */
49int
50dasd_ioctl_no_register(struct module *owner, int no, dasd_ioctl_fn_t handler)
51{
52 struct dasd_ioctl *new;
53 if (dasd_find_ioctl(no))
54 return -EBUSY;
55 new = kmalloc(sizeof (struct dasd_ioctl), GFP_KERNEL);
56 if (new == NULL)
57 return -ENOMEM;
58 new->owner = owner;
59 new->no = no;
60 new->handler = handler;
61 list_add(&new->list, &dasd_ioctl_list);
62 return 0;
63}
64
65/*
66 * Deregister ioctl with number no.
67 */
68int
69dasd_ioctl_no_unregister(struct module *owner, int no, dasd_ioctl_fn_t handler)
70{
71 struct dasd_ioctl *old = dasd_find_ioctl(no);
72 if (old == NULL)
73 return -ENOENT;
74 if (old->no != no || old->handler != handler || owner != old->owner)
75 return -EINVAL;
76 list_del(&old->list);
77 kfree(old);
78 return 0;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080082dasd_ioctl_api_version(void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 int ver = DASD_API_VERSION;
Christoph Hellwig13c62042006-03-24 03:15:19 -080085 return put_user(ver, (int *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/*
89 * Enable device.
90 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
91 */
92static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080093dasd_ioctl_enable(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Christoph Hellwig13c62042006-03-24 03:15:19 -080095 struct dasd_device *device = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (!capable(CAP_SYS_ADMIN))
98 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -080099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 dasd_enable_device(device);
101 /* Formatting the dasd device can change the capacity. */
Arjan van de Venc039e312006-03-23 03:00:28 -0800102 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 i_size_write(bdev->bd_inode, (loff_t)get_capacity(device->gdp) << 9);
Arjan van de Venc039e312006-03-23 03:00:28 -0800104 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106}
107
108/*
109 * Disable device.
110 * Used by dasdfmt. Disable I/O operations but allow ioctls.
111 */
112static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800113dasd_ioctl_disable(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800115 struct dasd_device *device = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (!capable(CAP_SYS_ADMIN))
118 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /*
121 * Man this is sick. We don't do a real disable but only downgrade
122 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
123 * BIODASDDISABLE to disable accesses to the device via the block
124 * device layer but it still wants to do i/o on the device by
125 * using the BIODASDFMT ioctl. Therefore the correct state for the
126 * device is DASD_STATE_BASIC that allows to do basic i/o.
127 */
128 dasd_set_target_state(device, DASD_STATE_BASIC);
129 /*
130 * Set i_size to zero, since read, write, etc. check against this
131 * value.
132 */
Arjan van de Venc039e312006-03-23 03:00:28 -0800133 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 i_size_write(bdev->bd_inode, 0);
Arjan van de Venc039e312006-03-23 03:00:28 -0800135 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
137}
138
139/*
140 * Quiesce device.
141 */
142static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800143dasd_ioctl_quiesce(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 unsigned long flags;
146
147 if (!capable (CAP_SYS_ADMIN))
148 return -EACCES;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 DEV_MESSAGE (KERN_DEBUG, device, "%s",
151 "Quiesce IO on device");
152 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
153 device->stopped |= DASD_STOPPED_QUIESCE;
154 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
155 return 0;
156}
157
158
159/*
160 * Quiesce device.
161 */
162static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800163dasd_ioctl_resume(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 unsigned long flags;
166
167 if (!capable (CAP_SYS_ADMIN))
168 return -EACCES;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 DEV_MESSAGE (KERN_DEBUG, device, "%s",
171 "resume IO on device");
172
173 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
174 device->stopped &= ~DASD_STOPPED_QUIESCE;
175 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
176
177 dasd_schedule_bh (device);
178 return 0;
179}
180
181/*
182 * performs formatting of _device_ according to _fdata_
183 * Note: The discipline's format_function is assumed to deliver formatting
184 * commands to format a single unit of the device. In terms of the ECKD
185 * devices this means CCWs are generated to format a single track.
186 */
187static int
188dasd_format(struct dasd_device * device, struct format_data_t * fdata)
189{
190 struct dasd_ccw_req *cqr;
191 int rc;
192
193 if (device->discipline->format_device == NULL)
194 return -EPERM;
195
196 if (device->state != DASD_STATE_BASIC) {
197 DEV_MESSAGE(KERN_WARNING, device, "%s",
198 "dasd_format: device is not disabled! ");
199 return -EBUSY;
200 }
201
202 DBF_DEV_EVENT(DBF_NOTICE, device,
203 "formatting units %d to %d (%d B blocks) flags %d",
204 fdata->start_unit,
205 fdata->stop_unit, fdata->blksize, fdata->intensity);
206
207 /* Since dasdfmt keeps the device open after it was disabled,
208 * there still exists an inode for this device.
209 * We must update i_blkbits, otherwise we might get errors when
210 * enabling the device later.
211 */
212 if (fdata->start_unit == 0) {
213 struct block_device *bdev = bdget_disk(device->gdp, 0);
214 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
215 bdput(bdev);
216 }
217
218 while (fdata->start_unit <= fdata->stop_unit) {
219 cqr = device->discipline->format_device(device, fdata);
220 if (IS_ERR(cqr))
221 return PTR_ERR(cqr);
222 rc = dasd_sleep_on_interruptible(cqr);
223 dasd_sfree_request(cqr, cqr->device);
224 if (rc) {
225 if (rc != -ERESTARTSYS)
226 DEV_MESSAGE(KERN_ERR, device,
227 " Formatting of unit %d failed "
228 "with rc = %d",
229 fdata->start_unit, rc);
230 return rc;
231 }
232 fdata->start_unit++;
233 }
234 return 0;
235}
236
237/*
238 * Format device.
239 */
240static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800241dasd_ioctl_format(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800243 struct dasd_device *device = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct format_data_t fdata;
245
246 if (!capable(CAP_SYS_ADMIN))
247 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800248 if (!argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -EINVAL;
Horst Hummelf24acd42005-05-01 08:58:59 -0700250
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700251 if (device->features & DASD_FEATURE_READONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -EROFS;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800253 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -EFAULT;
255 if (bdev != bdev->bd_contains) {
256 DEV_MESSAGE(KERN_WARNING, device, "%s",
257 "Cannot low-level format a partition");
258 return -EINVAL;
259 }
260 return dasd_format(device, &fdata);
261}
262
263#ifdef CONFIG_DASD_PROFILE
264/*
265 * Reset device profile information
266 */
267static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800268dasd_ioctl_reset_profile(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
271 return 0;
272}
273
274/*
275 * Return device profile information
276 */
277static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800278dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Horst Hummel9a7af282006-01-06 00:19:14 -0800280 if (dasd_profile_level == DASD_PROFILE_OFF)
281 return -EIO;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800282 if (copy_to_user(argp, &device->profile,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sizeof (struct dasd_profile_info_t)))
284 return -EFAULT;
285 return 0;
286}
287#else
288static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800289dasd_ioctl_reset_profile(struct dasd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 return -ENOSYS;
292}
293
294static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800295dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 return -ENOSYS;
298}
299#endif
300
301/*
302 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
303 */
304static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800305dasd_ioctl_information(struct dasd_device *device,
306 unsigned int cmd, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct dasd_information2_t *dasd_info;
309 unsigned long flags;
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700310 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct ccw_device *cdev;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!device->discipline->fill_info)
314 return -EINVAL;
315
316 dasd_info = kmalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
317 if (dasd_info == NULL)
318 return -ENOMEM;
319
320 rc = device->discipline->fill_info(device, dasd_info);
321 if (rc) {
322 kfree(dasd_info);
323 return rc;
324 }
325
326 cdev = device->cdev;
327
328 dasd_info->devno = _ccw_device_get_device_number(device->cdev);
329 dasd_info->schid = _ccw_device_get_subchannel_number(device->cdev);
330 dasd_info->cu_type = cdev->id.cu_type;
331 dasd_info->cu_model = cdev->id.cu_model;
332 dasd_info->dev_type = cdev->id.dev_type;
333 dasd_info->dev_model = cdev->id.dev_model;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dasd_info->status = device->state;
Horst Hummel57467192006-02-01 03:06:36 -0800335 /*
336 * The open_count is increased for every opener, that includes
337 * the blkdev_get in dasd_scan_partitions.
338 * This must be hidden from user-space.
339 */
340 dasd_info->open_count = atomic_read(&device->open_count);
341 if (!device->bdev)
342 dasd_info->open_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
345 * check if device is really formatted
346 * LDL / CDL was returned by 'fill_info'
347 */
348 if ((device->state < DASD_STATE_READY) ||
349 (dasd_check_blocksize(device->bp_block)))
350 dasd_info->format = DASD_FORMAT_NONE;
Horst Hummelf24acd42005-05-01 08:58:59 -0700351
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700352 dasd_info->features |=
353 ((device->features & DASD_FEATURE_READONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (device->discipline)
356 memcpy(dasd_info->type, device->discipline->name, 4);
357 else
358 memcpy(dasd_info->type, "none", 4);
359 dasd_info->req_queue_len = 0;
360 dasd_info->chanq_len = 0;
361 if (device->request_queue->request_fn) {
362 struct list_head *l;
363#ifdef DASD_EXTENDED_PROFILING
364 {
365 struct list_head *l;
366 spin_lock_irqsave(&device->lock, flags);
367 list_for_each(l, &device->request_queue->queue_head)
368 dasd_info->req_queue_len++;
369 spin_unlock_irqrestore(&device->lock, flags);
370 }
371#endif /* DASD_EXTENDED_PROFILING */
372 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
373 list_for_each(l, &device->ccw_queue)
374 dasd_info->chanq_len++;
375 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
376 flags);
377 }
378
379 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800380 if (copy_to_user(argp, dasd_info,
381 ((cmd == (unsigned int) BIODASDINFO2) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sizeof (struct dasd_information2_t) :
383 sizeof (struct dasd_information_t))))
384 rc = -EFAULT;
385 kfree(dasd_info);
386 return rc;
387}
388
389/*
390 * Set read only
391 */
392static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800393dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800395 struct dasd_device *device = bdev->bd_disk->private_data;
396 int intval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (!capable(CAP_SYS_ADMIN))
399 return -EACCES;
400 if (bdev != bdev->bd_contains)
401 // ro setting is not allowed for partitions
402 return -EINVAL;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800403 if (get_user(intval, (int *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -EFAULT;
Horst Hummelf24acd42005-05-01 08:58:59 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 set_disk_ro(bdev->bd_disk, intval);
Christoph Hellwig13c62042006-03-24 03:15:19 -0800407 return dasd_set_feature(device->cdev, DASD_FEATURE_READONLY, intval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800410static int
411dasd_ioctl_readall_cmb(struct dasd_device *device, unsigned int cmd,
412 unsigned long arg)
413{
414 struct cmbdata __user *argp = (void __user *) arg;
415 size_t size = _IOC_SIZE(cmd);
416 struct cmbdata data;
417 int ret;
418
419 ret = cmf_readall(device->cdev, &data);
420 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
421 return -EFAULT;
422 return ret;
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800426dasd_ioctl(struct inode *inode, struct file *file,
427 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800429 struct block_device *bdev = inode->i_bdev;
430 struct dasd_device *device = bdev->bd_disk->private_data;
431 void __user *argp = (void __user *)arg;
432 struct dasd_ioctl *ioctl;
433 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Christoph Hellwig13c62042006-03-24 03:15:19 -0800435 if (!device)
436 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Christoph Hellwig13c62042006-03-24 03:15:19 -0800438 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
439 PRINT_DEBUG("empty data ptr");
440 return -EINVAL;
441 }
442
443 switch (cmd) {
444 case BIODASDDISABLE:
445 return dasd_ioctl_disable(bdev);
446 case BIODASDENABLE:
447 return dasd_ioctl_enable(bdev);
448 case BIODASDQUIESCE:
449 return dasd_ioctl_quiesce(device);
450 case BIODASDRESUME:
451 return dasd_ioctl_resume(device);
452 case BIODASDFMT:
453 return dasd_ioctl_format(bdev, argp);
454 case BIODASDINFO:
455 return dasd_ioctl_information(device, cmd, argp);
456 case BIODASDINFO2:
457 return dasd_ioctl_information(device, cmd, argp);
458 case BIODASDPRRD:
459 return dasd_ioctl_read_profile(device, argp);
460 case BIODASDPRRST:
461 return dasd_ioctl_reset_profile(device);
462 case BLKROSET:
463 return dasd_ioctl_set_ro(bdev, argp);
464 case DASDAPIVER:
465 return dasd_ioctl_api_version(argp);
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800466 case BIODASDCMFENABLE:
467 return enable_cmf(device->cdev);
468 case BIODASDCMFDISABLE:
469 return disable_cmf(device->cdev);
470 case BIODASDREADALLCMB:
471 return dasd_ioctl_readall_cmb(device, cmd, arg);
Christoph Hellwig13c62042006-03-24 03:15:19 -0800472 default:
Christoph Hellwig1107ccf2006-03-24 03:15:20 -0800473 /* if the discipline has an ioctl method try it. */
474 if (device->discipline->ioctl) {
475 int rval = device->discipline->ioctl(device, cmd, argp);
476 if (rval != -ENOIOCTLCMD)
477 return rval;
478 }
479
480 /* else resort to the deprecated dynamic ioctl list */
Christoph Hellwig13c62042006-03-24 03:15:19 -0800481 list_for_each_entry(ioctl, &dasd_ioctl_list, list) {
482 if (ioctl->no == cmd) {
483 /* Found a matching ioctl. Call it. */
484 if (!try_module_get(ioctl->owner))
485 continue;
486 rc = ioctl->handler(bdev, cmd, arg);
487 module_put(ioctl->owner);
488 return rc;
489 }
490 }
491 return -EINVAL;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Christoph Hellwig13c62042006-03-24 03:15:19 -0800495long
496dasd_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Christoph Hellwig13c62042006-03-24 03:15:19 -0800498 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Christoph Hellwig13c62042006-03-24 03:15:19 -0800500 lock_kernel();
501 rval = dasd_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
502 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Christoph Hellwig13c62042006-03-24 03:15:19 -0800504 return (rval == -EINVAL) ? -ENOIOCTLCMD : rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507EXPORT_SYMBOL(dasd_ioctl_no_register);
508EXPORT_SYMBOL(dasd_ioctl_no_unregister);