blob: 792c69e78fe2ae6474635680dba3c9af436b554e [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 */
Stefan Haberlandfc19f382009-03-26 15:23:49 +010012
13#define KMSG_COMPONENT "dasd"
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
Heiko Carstens048cd4e2012-02-27 10:01:52 +010016#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/major.h>
18#include <linux/fs.h>
19#include <linux/blkpg.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Heiko Carstens88034862010-01-13 20:44:29 +010021#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/ccwdev.h>
Christoph Hellwig8b2eb662006-03-24 03:15:21 -080023#include <asm/cmb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25
26/* This is ugly... */
27#define PRINTK_HEADER "dasd_ioctl:"
28
29#include "dasd_int.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080033dasd_ioctl_api_version(void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 int ver = DASD_API_VERSION;
Bastian Blankb5029622006-03-24 03:15:32 -080036 return put_user(ver, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39/*
40 * Enable device.
41 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
42 */
43static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080044dasd_ioctl_enable(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020046 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 if (!capable(CAP_SYS_ADMIN))
49 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -080050
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020051 base = dasd_device_from_gendisk(bdev->bd_disk);
52 if (!base)
53 return -ENODEV;
54
55 dasd_enable_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /* Formatting the dasd device can change the capacity. */
Arjan van de Venc039e312006-03-23 03:00:28 -080057 mutex_lock(&bdev->bd_mutex);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020058 i_size_write(bdev->bd_inode,
59 (loff_t)get_capacity(base->block->gdp) << 9);
Arjan van de Venc039e312006-03-23 03:00:28 -080060 mutex_unlock(&bdev->bd_mutex);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020061 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
63}
64
65/*
66 * Disable device.
67 * Used by dasdfmt. Disable I/O operations but allow ioctls.
68 */
69static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080070dasd_ioctl_disable(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020072 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (!capable(CAP_SYS_ADMIN))
75 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -080076
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020077 base = dasd_device_from_gendisk(bdev->bd_disk);
78 if (!base)
79 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /*
81 * Man this is sick. We don't do a real disable but only downgrade
82 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
83 * BIODASDDISABLE to disable accesses to the device via the block
84 * device layer but it still wants to do i/o on the device by
85 * using the BIODASDFMT ioctl. Therefore the correct state for the
86 * device is DASD_STATE_BASIC that allows to do basic i/o.
87 */
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020088 dasd_set_target_state(base, DASD_STATE_BASIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /*
90 * Set i_size to zero, since read, write, etc. check against this
91 * value.
92 */
Arjan van de Venc039e312006-03-23 03:00:28 -080093 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 i_size_write(bdev->bd_inode, 0);
Arjan van de Venc039e312006-03-23 03:00:28 -080095 mutex_unlock(&bdev->bd_mutex);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020096 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
98}
99
100/*
101 * Quiesce device.
102 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100103static int dasd_ioctl_quiesce(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100106 struct dasd_device *base;
Horst Hummel138c0142006-06-29 14:58:12 +0200107
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100108 base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (!capable (CAP_SYS_ADMIN))
110 return -EACCES;
Horst Hummel138c0142006-06-29 14:58:12 +0200111
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200112 pr_info("%s: The DASD has been put in the quiesce "
113 "state\n", dev_name(&base->cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100114 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100115 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100116 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118}
119
120
121/*
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100122 * Resume device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100124static int dasd_ioctl_resume(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100127 struct dasd_device *base;
Horst Hummel138c0142006-06-29 14:58:12 +0200128
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100129 base = block->base;
Horst Hummel138c0142006-06-29 14:58:12 +0200130 if (!capable (CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return -EACCES;
132
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200133 pr_info("%s: I/O operations have been resumed "
134 "on the DASD\n", dev_name(&base->cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100135 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100136 dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100137 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
Horst Hummel138c0142006-06-29 14:58:12 +0200138
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100139 dasd_schedule_block_bh(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return 0;
141}
142
143/*
144 * performs formatting of _device_ according to _fdata_
145 * Note: The discipline's format_function is assumed to deliver formatting
146 * commands to format a single unit of the device. In terms of the ECKD
147 * devices this means CCWs are generated to format a single track.
148 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100149static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct dasd_ccw_req *cqr;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100152 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int rc;
154
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100155 base = block->base;
156 if (base->discipline->format_device == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EPERM;
158
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100159 if (base->state != DASD_STATE_BASIC) {
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200160 pr_warning("%s: The DASD cannot be formatted while it is "
161 "enabled\n", dev_name(&base->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EBUSY;
163 }
164
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100165 DBF_DEV_EVENT(DBF_NOTICE, base,
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100166 "formatting units %u to %u (%u B blocks) flags %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 fdata->start_unit,
168 fdata->stop_unit, fdata->blksize, fdata->intensity);
169
170 /* Since dasdfmt keeps the device open after it was disabled,
171 * there still exists an inode for this device.
172 * We must update i_blkbits, otherwise we might get errors when
173 * enabling the device later.
174 */
175 if (fdata->start_unit == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100176 struct block_device *bdev = bdget_disk(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
178 bdput(bdev);
179 }
180
181 while (fdata->start_unit <= fdata->stop_unit) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100182 cqr = base->discipline->format_device(base, fdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (IS_ERR(cqr))
184 return PTR_ERR(cqr);
185 rc = dasd_sleep_on_interruptible(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100186 dasd_sfree_request(cqr, cqr->memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (rc) {
188 if (rc != -ERESTARTSYS)
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200189 pr_err("%s: Formatting unit %d failed with "
190 "rc=%d\n", dev_name(&base->cdev->dev),
191 fdata->start_unit, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return rc;
193 }
194 fdata->start_unit++;
195 }
196 return 0;
197}
198
199/*
200 * Format device.
201 */
202static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800203dasd_ioctl_format(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200205 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct format_data_t fdata;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200207 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (!capable(CAP_SYS_ADMIN))
210 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800211 if (!argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200213 base = dasd_device_from_gendisk(bdev->bd_disk);
214 if (!base)
215 return -ENODEV;
216 if (base->features & DASD_FEATURE_READONLY ||
217 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
218 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EROFS;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200220 }
221 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
222 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EFAULT;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (bdev != bdev->bd_contains) {
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200226 pr_warning("%s: The specified DASD is a partition and cannot "
227 "be formatted\n",
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200228 dev_name(&base->cdev->dev));
229 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EINVAL;
231 }
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200232 rc = dasd_format(base->block, &fdata);
233 dasd_put_device(base);
234 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237#ifdef CONFIG_DASD_PROFILE
238/*
239 * Reset device profile information
240 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100241static int dasd_ioctl_reset_profile(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200243 dasd_profile_reset(&block->profile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
246
247/*
248 * Return device profile information
249 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100250static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200252 struct dasd_profile_info_t *data;
Julia Lawallba465d82011-08-24 17:15:10 +0200253 int rc = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200254
255 data = kmalloc(sizeof(*data), GFP_KERNEL);
256 if (!data)
257 return -ENOMEM;
258
259 spin_lock_bh(&block->profile.lock);
260 if (block->profile.data) {
261 data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
262 data->dasd_io_sects = block->profile.data->dasd_io_sects;
263 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
264 sizeof(data->dasd_io_secs));
265 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
266 sizeof(data->dasd_io_times));
267 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
268 sizeof(data->dasd_io_timps));
269 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
270 sizeof(data->dasd_io_time1));
271 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
272 sizeof(data->dasd_io_time2));
273 memcpy(data->dasd_io_time2ps,
274 block->profile.data->dasd_io_time2ps,
275 sizeof(data->dasd_io_time2ps));
276 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
277 sizeof(data->dasd_io_time3));
278 memcpy(data->dasd_io_nr_req,
279 block->profile.data->dasd_io_nr_req,
280 sizeof(data->dasd_io_nr_req));
281 spin_unlock_bh(&block->profile.lock);
282 } else {
283 spin_unlock_bh(&block->profile.lock);
Julia Lawallba465d82011-08-24 17:15:10 +0200284 rc = -EIO;
285 goto out;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200286 }
287 if (copy_to_user(argp, data, sizeof(*data)))
Julia Lawallba465d82011-08-24 17:15:10 +0200288 rc = -EFAULT;
289out:
290 kfree(data);
291 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100294static int dasd_ioctl_reset_profile(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 return -ENOSYS;
297}
298
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100299static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 return -ENOSYS;
302}
303#endif
304
305/*
306 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
307 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100308static int dasd_ioctl_information(struct dasd_block *block,
309 unsigned int cmd, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct dasd_information2_t *dasd_info;
312 unsigned long flags;
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700313 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100314 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct ccw_device *cdev;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200316 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100318 base = block->base;
Stefan Haberland294001a2010-01-27 10:12:35 +0100319 if (!base->discipline || !base->discipline->fill_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -EINVAL;
321
Horst Hummel554a8262006-03-24 03:15:24 -0800322 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (dasd_info == NULL)
324 return -ENOMEM;
325
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100326 rc = base->discipline->fill_info(base, dasd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (rc) {
328 kfree(dasd_info);
329 return rc;
330 }
331
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100332 cdev = base->cdev;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200333 ccw_device_get_id(cdev, &dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200335 dasd_info->devno = dev_id.devno;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100336 dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dasd_info->cu_type = cdev->id.cu_type;
338 dasd_info->cu_model = cdev->id.cu_model;
339 dasd_info->dev_type = cdev->id.dev_type;
340 dasd_info->dev_model = cdev->id.dev_model;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100341 dasd_info->status = base->state;
Horst Hummel57467192006-02-01 03:06:36 -0800342 /*
343 * The open_count is increased for every opener, that includes
344 * the blkdev_get in dasd_scan_partitions.
345 * This must be hidden from user-space.
346 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100347 dasd_info->open_count = atomic_read(&block->open_count);
348 if (!block->bdev)
Horst Hummel57467192006-02-01 03:06:36 -0800349 dasd_info->open_count++;
Horst Hummel138c0142006-06-29 14:58:12 +0200350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
352 * check if device is really formatted
353 * LDL / CDL was returned by 'fill_info'
354 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100355 if ((base->state < DASD_STATE_READY) ||
356 (dasd_check_blocksize(block->bp_block)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 dasd_info->format = DASD_FORMAT_NONE;
Horst Hummelf24acd42005-05-01 08:58:59 -0700358
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700359 dasd_info->features |=
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100360 ((base->features & DASD_FEATURE_READONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Stefan Haberland294001a2010-01-27 10:12:35 +0100362 memcpy(dasd_info->type, base->discipline->name, 4);
Horst Hummel554a8262006-03-24 03:15:24 -0800363
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100364 if (block->request_queue->request_fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct list_head *l;
366#ifdef DASD_EXTENDED_PROFILING
367 {
368 struct list_head *l;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100369 spin_lock_irqsave(&block->lock, flags);
370 list_for_each(l, &block->request_queue->queue_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 dasd_info->req_queue_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100372 spin_unlock_irqrestore(&block->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374#endif /* DASD_EXTENDED_PROFILING */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100375 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
376 list_for_each(l, &base->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 dasd_info->chanq_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100378 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 flags);
380 }
381
382 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800383 if (copy_to_user(argp, dasd_info,
384 ((cmd == (unsigned int) BIODASDINFO2) ?
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100385 sizeof(struct dasd_information2_t) :
386 sizeof(struct dasd_information_t))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 rc = -EFAULT;
388 kfree(dasd_info);
389 return rc;
390}
391
392/*
393 * Set read only
394 */
395static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800396dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200398 struct dasd_device *base;
399 int intval, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 if (!capable(CAP_SYS_ADMIN))
402 return -EACCES;
403 if (bdev != bdev->bd_contains)
404 // ro setting is not allowed for partitions
405 return -EINVAL;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200406 if (get_user(intval, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -EFAULT;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200408 base = dasd_device_from_gendisk(bdev->bd_disk);
409 if (!base)
410 return -ENODEV;
411 if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
412 dasd_put_device(base);
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100413 return -EROFS;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 set_disk_ro(bdev->bd_disk, intval);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200416 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
417 dasd_put_device(base);
418 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100421static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
Heiko Carstens88034862010-01-13 20:44:29 +0100422 struct cmbdata __user *argp)
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800423{
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800424 size_t size = _IOC_SIZE(cmd);
425 struct cmbdata data;
426 int ret;
427
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100428 ret = cmf_readall(block->base->cdev, &data);
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800429 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
430 return -EFAULT;
431 return ret;
432}
433
Arnd Bergmanncfdb00a2010-05-31 22:38:40 +0200434int dasd_ioctl(struct block_device *bdev, fmode_t mode,
435 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200437 struct dasd_block *block;
438 struct dasd_device *base;
Heiko Carstens88034862010-01-13 20:44:29 +0100439 void __user *argp;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200440 int rc;
Heiko Carstens88034862010-01-13 20:44:29 +0100441
442 if (is_compat_task())
443 argp = compat_ptr(arg);
444 else
445 argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Christoph Hellwig13c62042006-03-24 03:15:19 -0800447 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
448 PRINT_DEBUG("empty data ptr");
449 return -EINVAL;
450 }
451
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200452 base = dasd_device_from_gendisk(bdev->bd_disk);
453 if (!base)
454 return -ENODEV;
455 block = base->block;
456 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800457 switch (cmd) {
458 case BIODASDDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200459 rc = dasd_ioctl_disable(bdev);
460 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800461 case BIODASDENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200462 rc = dasd_ioctl_enable(bdev);
463 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800464 case BIODASDQUIESCE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200465 rc = dasd_ioctl_quiesce(block);
466 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800467 case BIODASDRESUME:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200468 rc = dasd_ioctl_resume(block);
469 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800470 case BIODASDFMT:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200471 rc = dasd_ioctl_format(bdev, argp);
472 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800473 case BIODASDINFO:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200474 rc = dasd_ioctl_information(block, cmd, argp);
475 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800476 case BIODASDINFO2:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200477 rc = dasd_ioctl_information(block, cmd, argp);
478 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800479 case BIODASDPRRD:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200480 rc = dasd_ioctl_read_profile(block, argp);
481 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800482 case BIODASDPRRST:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200483 rc = dasd_ioctl_reset_profile(block);
484 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800485 case BLKROSET:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200486 rc = dasd_ioctl_set_ro(bdev, argp);
487 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800488 case DASDAPIVER:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200489 rc = dasd_ioctl_api_version(argp);
490 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800491 case BIODASDCMFENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200492 rc = enable_cmf(base->cdev);
493 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800494 case BIODASDCMFDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200495 rc = disable_cmf(base->cdev);
496 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800497 case BIODASDREADALLCMB:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200498 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
499 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800500 default:
Christoph Hellwig1107ccf2006-03-24 03:15:20 -0800501 /* if the discipline has an ioctl method try it. */
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200502 if (base->discipline->ioctl) {
503 rc = base->discipline->ioctl(block, cmd, argp);
504 if (rc == -ENOIOCTLCMD)
505 rc = -EINVAL;
506 } else
507 rc = -EINVAL;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800508 }
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200509 dasd_put_device(base);
510 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}