blob: 03c0e044455335df0c6d3a0cc5defbf5c15dd0a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02007 * Copyright IBM Corp. 1999, 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * i/o controls for the dasd driver.
10 */
Stefan Haberlandfc19f382009-03-26 15:23:49 +010011
12#define KMSG_COMPONENT "dasd"
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/interrupt.h>
Heiko Carstens048cd4e2012-02-27 10:01:52 +010015#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/major.h>
17#include <linux/fs.h>
18#include <linux/blkpg.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Heiko Carstens88034862010-01-13 20:44:29 +010020#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/ccwdev.h>
Sebastian Ott287a0932012-10-29 16:50:20 +010022#include <asm/schid.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{
Heiko Carstens2267a0c2012-09-06 14:47:30 +0200296 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
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{
Heiko Carstens2267a0c2012-09-06 14:47:30 +0200301 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
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;
Sebastian Ott287a0932012-10-29 16:50:20 +0100312 struct subchannel_id sch_id;
313 struct ccw_dev_id dev_id;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100314 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct ccw_device *cdev;
Sebastian Ott287a0932012-10-29 16:50:20 +0100316 unsigned long flags;
317 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100319 base = block->base;
Stefan Haberland294001a2010-01-27 10:12:35 +0100320 if (!base->discipline || !base->discipline->fill_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -EINVAL;
322
Horst Hummel554a8262006-03-24 03:15:24 -0800323 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (dasd_info == NULL)
325 return -ENOMEM;
326
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100327 rc = base->discipline->fill_info(base, dasd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (rc) {
329 kfree(dasd_info);
330 return rc;
331 }
332
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100333 cdev = base->cdev;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200334 ccw_device_get_id(cdev, &dev_id);
Sebastian Ott287a0932012-10-29 16:50:20 +0100335 ccw_device_get_schid(cdev, &sch_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200337 dasd_info->devno = dev_id.devno;
Sebastian Ott287a0932012-10-29 16:50:20 +0100338 dasd_info->schid = sch_id.sch_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 dasd_info->cu_type = cdev->id.cu_type;
340 dasd_info->cu_model = cdev->id.cu_model;
341 dasd_info->dev_type = cdev->id.dev_type;
342 dasd_info->dev_model = cdev->id.dev_model;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100343 dasd_info->status = base->state;
Horst Hummel57467192006-02-01 03:06:36 -0800344 /*
345 * The open_count is increased for every opener, that includes
346 * the blkdev_get in dasd_scan_partitions.
347 * This must be hidden from user-space.
348 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100349 dasd_info->open_count = atomic_read(&block->open_count);
350 if (!block->bdev)
Horst Hummel57467192006-02-01 03:06:36 -0800351 dasd_info->open_count++;
Horst Hummel138c0142006-06-29 14:58:12 +0200352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
354 * check if device is really formatted
355 * LDL / CDL was returned by 'fill_info'
356 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100357 if ((base->state < DASD_STATE_READY) ||
358 (dasd_check_blocksize(block->bp_block)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dasd_info->format = DASD_FORMAT_NONE;
Horst Hummelf24acd42005-05-01 08:58:59 -0700360
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700361 dasd_info->features |=
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100362 ((base->features & DASD_FEATURE_READONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Stefan Haberland294001a2010-01-27 10:12:35 +0100364 memcpy(dasd_info->type, base->discipline->name, 4);
Horst Hummel554a8262006-03-24 03:15:24 -0800365
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100366 if (block->request_queue->request_fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct list_head *l;
368#ifdef DASD_EXTENDED_PROFILING
369 {
370 struct list_head *l;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100371 spin_lock_irqsave(&block->lock, flags);
372 list_for_each(l, &block->request_queue->queue_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 dasd_info->req_queue_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100374 spin_unlock_irqrestore(&block->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376#endif /* DASD_EXTENDED_PROFILING */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100377 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
378 list_for_each(l, &base->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 dasd_info->chanq_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100380 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 flags);
382 }
383
384 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800385 if (copy_to_user(argp, dasd_info,
386 ((cmd == (unsigned int) BIODASDINFO2) ?
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100387 sizeof(struct dasd_information2_t) :
388 sizeof(struct dasd_information_t))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 rc = -EFAULT;
390 kfree(dasd_info);
391 return rc;
392}
393
394/*
395 * Set read only
396 */
397static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800398dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200400 struct dasd_device *base;
401 int intval, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!capable(CAP_SYS_ADMIN))
404 return -EACCES;
405 if (bdev != bdev->bd_contains)
406 // ro setting is not allowed for partitions
407 return -EINVAL;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200408 if (get_user(intval, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return -EFAULT;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200410 base = dasd_device_from_gendisk(bdev->bd_disk);
411 if (!base)
412 return -ENODEV;
413 if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
414 dasd_put_device(base);
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100415 return -EROFS;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 set_disk_ro(bdev->bd_disk, intval);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200418 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
419 dasd_put_device(base);
420 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100423static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
Heiko Carstens88034862010-01-13 20:44:29 +0100424 struct cmbdata __user *argp)
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800425{
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800426 size_t size = _IOC_SIZE(cmd);
427 struct cmbdata data;
428 int ret;
429
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100430 ret = cmf_readall(block->base->cdev, &data);
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800431 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
432 return -EFAULT;
433 return ret;
434}
435
Arnd Bergmanncfdb00a2010-05-31 22:38:40 +0200436int dasd_ioctl(struct block_device *bdev, fmode_t mode,
437 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200439 struct dasd_block *block;
440 struct dasd_device *base;
Heiko Carstens88034862010-01-13 20:44:29 +0100441 void __user *argp;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200442 int rc;
Heiko Carstens88034862010-01-13 20:44:29 +0100443
444 if (is_compat_task())
445 argp = compat_ptr(arg);
446 else
447 argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Christoph Hellwig13c62042006-03-24 03:15:19 -0800449 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
450 PRINT_DEBUG("empty data ptr");
451 return -EINVAL;
452 }
453
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200454 base = dasd_device_from_gendisk(bdev->bd_disk);
455 if (!base)
456 return -ENODEV;
457 block = base->block;
458 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800459 switch (cmd) {
460 case BIODASDDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200461 rc = dasd_ioctl_disable(bdev);
462 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800463 case BIODASDENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200464 rc = dasd_ioctl_enable(bdev);
465 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800466 case BIODASDQUIESCE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200467 rc = dasd_ioctl_quiesce(block);
468 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800469 case BIODASDRESUME:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200470 rc = dasd_ioctl_resume(block);
471 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800472 case BIODASDFMT:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200473 rc = dasd_ioctl_format(bdev, argp);
474 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800475 case BIODASDINFO:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200476 rc = dasd_ioctl_information(block, cmd, argp);
477 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800478 case BIODASDINFO2:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200479 rc = dasd_ioctl_information(block, cmd, argp);
480 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800481 case BIODASDPRRD:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200482 rc = dasd_ioctl_read_profile(block, argp);
483 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800484 case BIODASDPRRST:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200485 rc = dasd_ioctl_reset_profile(block);
486 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800487 case BLKROSET:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200488 rc = dasd_ioctl_set_ro(bdev, argp);
489 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800490 case DASDAPIVER:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200491 rc = dasd_ioctl_api_version(argp);
492 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800493 case BIODASDCMFENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200494 rc = enable_cmf(base->cdev);
495 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800496 case BIODASDCMFDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200497 rc = disable_cmf(base->cdev);
498 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800499 case BIODASDREADALLCMB:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200500 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
501 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800502 default:
Christoph Hellwig1107ccf2006-03-24 03:15:20 -0800503 /* if the discipline has an ioctl method try it. */
Heiko Carstens6b79d142012-08-27 10:59:42 +0200504 rc = -ENOTTY;
505 if (base->discipline->ioctl)
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200506 rc = base->discipline->ioctl(block, cmd, argp);
Christoph Hellwig13c62042006-03-24 03:15:19 -0800507 }
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200508 dasd_put_device(base);
509 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}