blob: 25a0f2f8b0b98c93376294e25512e43c53701f72 [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/*
Hannes Reinecke5ea34a02013-01-30 09:26:19 +0000144 * Abort all failfast I/O on a device.
145 */
146static int dasd_ioctl_abortio(struct dasd_block *block)
147{
148 unsigned long flags;
149 struct dasd_device *base;
150 struct dasd_ccw_req *cqr, *n;
151
152 base = block->base;
153 if (!capable(CAP_SYS_ADMIN))
154 return -EACCES;
155
156 if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags))
157 return 0;
158 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set");
159
160 spin_lock_irqsave(&block->request_queue_lock, flags);
161 spin_lock(&block->queue_lock);
162 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
163 if (test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
164 cqr->callback_data &&
165 cqr->callback_data != DASD_SLEEPON_START_TAG &&
166 cqr->callback_data != DASD_SLEEPON_END_TAG) {
167 spin_unlock(&block->queue_lock);
168 blk_abort_request(cqr->callback_data);
169 spin_lock(&block->queue_lock);
170 }
171 }
172 spin_unlock(&block->queue_lock);
173 spin_unlock_irqrestore(&block->request_queue_lock, flags);
174
175 dasd_schedule_block_bh(block);
176 return 0;
177}
178
179/*
180 * Allow I/O on a device
181 */
182static int dasd_ioctl_allowio(struct dasd_block *block)
183{
184 struct dasd_device *base;
185
186 base = block->base;
187 if (!capable(CAP_SYS_ADMIN))
188 return -EACCES;
189
190 if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags))
191 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset");
192
193 return 0;
194}
195
196/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * performs formatting of _device_ according to _fdata_
198 * Note: The discipline's format_function is assumed to deliver formatting
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200199 * commands to format multiple units of the device. In terms of the ECKD
200 * devices this means CCWs are generated to format multiple tracks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200202static int
203dasd_format(struct dasd_block *block, struct format_data_t *fdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100205 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int rc;
207
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100208 base = block->base;
209 if (base->discipline->format_device == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -EPERM;
211
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100212 if (base->state != DASD_STATE_BASIC) {
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200213 pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
214 dev_name(&base->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -EBUSY;
216 }
217
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100218 DBF_DEV_EVENT(DBF_NOTICE, base,
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100219 "formatting units %u to %u (%u B blocks) flags %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 fdata->start_unit,
221 fdata->stop_unit, fdata->blksize, fdata->intensity);
222
223 /* Since dasdfmt keeps the device open after it was disabled,
224 * there still exists an inode for this device.
225 * We must update i_blkbits, otherwise we might get errors when
226 * enabling the device later.
227 */
228 if (fdata->start_unit == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100229 struct block_device *bdev = bdget_disk(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
231 bdput(bdev);
232 }
233
Stefan Haberlandd42e1712013-04-15 16:22:23 +0200234 rc = base->discipline->format_device(base, fdata);
235 if (rc)
236 return rc;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239}
240
241/*
242 * Format device.
243 */
244static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800245dasd_ioctl_format(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200247 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct format_data_t fdata;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200249 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (!capable(CAP_SYS_ADMIN))
252 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800253 if (!argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200255 base = dasd_device_from_gendisk(bdev->bd_disk);
256 if (!base)
257 return -ENODEV;
258 if (base->features & DASD_FEATURE_READONLY ||
259 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
260 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -EROFS;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200262 }
263 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
264 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EFAULT;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (bdev != bdev->bd_contains) {
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200268 pr_warning("%s: The specified DASD is a partition and cannot "
269 "be formatted\n",
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200270 dev_name(&base->cdev->dev));
271 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -EINVAL;
273 }
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200274 rc = dasd_format(base->block, &fdata);
275 dasd_put_device(base);
276 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279#ifdef CONFIG_DASD_PROFILE
280/*
281 * Reset device profile information
282 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100283static int dasd_ioctl_reset_profile(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200285 dasd_profile_reset(&block->profile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287}
288
289/*
290 * Return device profile information
291 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100292static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200294 struct dasd_profile_info_t *data;
Julia Lawallba465d82011-08-24 17:15:10 +0200295 int rc = 0;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200296
297 data = kmalloc(sizeof(*data), GFP_KERNEL);
298 if (!data)
299 return -ENOMEM;
300
301 spin_lock_bh(&block->profile.lock);
302 if (block->profile.data) {
303 data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
304 data->dasd_io_sects = block->profile.data->dasd_io_sects;
305 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
306 sizeof(data->dasd_io_secs));
307 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
308 sizeof(data->dasd_io_times));
309 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
310 sizeof(data->dasd_io_timps));
311 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
312 sizeof(data->dasd_io_time1));
313 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
314 sizeof(data->dasd_io_time2));
315 memcpy(data->dasd_io_time2ps,
316 block->profile.data->dasd_io_time2ps,
317 sizeof(data->dasd_io_time2ps));
318 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
319 sizeof(data->dasd_io_time3));
320 memcpy(data->dasd_io_nr_req,
321 block->profile.data->dasd_io_nr_req,
322 sizeof(data->dasd_io_nr_req));
323 spin_unlock_bh(&block->profile.lock);
324 } else {
325 spin_unlock_bh(&block->profile.lock);
Julia Lawallba465d82011-08-24 17:15:10 +0200326 rc = -EIO;
327 goto out;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200328 }
329 if (copy_to_user(argp, data, sizeof(*data)))
Julia Lawallba465d82011-08-24 17:15:10 +0200330 rc = -EFAULT;
331out:
332 kfree(data);
333 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100336static int dasd_ioctl_reset_profile(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Heiko Carstens2267a0c2012-09-06 14:47:30 +0200338 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100341static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Heiko Carstens2267a0c2012-09-06 14:47:30 +0200343 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345#endif
346
347/*
348 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
349 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100350static int dasd_ioctl_information(struct dasd_block *block,
351 unsigned int cmd, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct dasd_information2_t *dasd_info;
Sebastian Ott287a0932012-10-29 16:50:20 +0100354 struct subchannel_id sch_id;
355 struct ccw_dev_id dev_id;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100356 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct ccw_device *cdev;
Sebastian Ott287a0932012-10-29 16:50:20 +0100358 unsigned long flags;
359 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100361 base = block->base;
Stefan Haberland294001a2010-01-27 10:12:35 +0100362 if (!base->discipline || !base->discipline->fill_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -EINVAL;
364
Horst Hummel554a8262006-03-24 03:15:24 -0800365 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (dasd_info == NULL)
367 return -ENOMEM;
368
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100369 rc = base->discipline->fill_info(base, dasd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (rc) {
371 kfree(dasd_info);
372 return rc;
373 }
374
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100375 cdev = base->cdev;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200376 ccw_device_get_id(cdev, &dev_id);
Sebastian Ott287a0932012-10-29 16:50:20 +0100377 ccw_device_get_schid(cdev, &sch_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200379 dasd_info->devno = dev_id.devno;
Sebastian Ott287a0932012-10-29 16:50:20 +0100380 dasd_info->schid = sch_id.sch_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 dasd_info->cu_type = cdev->id.cu_type;
382 dasd_info->cu_model = cdev->id.cu_model;
383 dasd_info->dev_type = cdev->id.dev_type;
384 dasd_info->dev_model = cdev->id.dev_model;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100385 dasd_info->status = base->state;
Horst Hummel57467192006-02-01 03:06:36 -0800386 /*
387 * The open_count is increased for every opener, that includes
388 * the blkdev_get in dasd_scan_partitions.
389 * This must be hidden from user-space.
390 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100391 dasd_info->open_count = atomic_read(&block->open_count);
392 if (!block->bdev)
Horst Hummel57467192006-02-01 03:06:36 -0800393 dasd_info->open_count++;
Horst Hummel138c0142006-06-29 14:58:12 +0200394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * check if device is really formatted
397 * LDL / CDL was returned by 'fill_info'
398 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100399 if ((base->state < DASD_STATE_READY) ||
400 (dasd_check_blocksize(block->bp_block)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dasd_info->format = DASD_FORMAT_NONE;
Horst Hummelf24acd42005-05-01 08:58:59 -0700402
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700403 dasd_info->features |=
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100404 ((base->features & DASD_FEATURE_READONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Stefan Haberland294001a2010-01-27 10:12:35 +0100406 memcpy(dasd_info->type, base->discipline->name, 4);
Horst Hummel554a8262006-03-24 03:15:24 -0800407
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100408 if (block->request_queue->request_fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct list_head *l;
410#ifdef DASD_EXTENDED_PROFILING
411 {
412 struct list_head *l;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100413 spin_lock_irqsave(&block->lock, flags);
414 list_for_each(l, &block->request_queue->queue_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 dasd_info->req_queue_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100416 spin_unlock_irqrestore(&block->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418#endif /* DASD_EXTENDED_PROFILING */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100419 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
420 list_for_each(l, &base->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dasd_info->chanq_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100422 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 flags);
424 }
425
426 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800427 if (copy_to_user(argp, dasd_info,
428 ((cmd == (unsigned int) BIODASDINFO2) ?
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100429 sizeof(struct dasd_information2_t) :
430 sizeof(struct dasd_information_t))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 rc = -EFAULT;
432 kfree(dasd_info);
433 return rc;
434}
435
436/*
437 * Set read only
438 */
439static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800440dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200442 struct dasd_device *base;
443 int intval, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (!capable(CAP_SYS_ADMIN))
446 return -EACCES;
447 if (bdev != bdev->bd_contains)
448 // ro setting is not allowed for partitions
449 return -EINVAL;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200450 if (get_user(intval, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -EFAULT;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200452 base = dasd_device_from_gendisk(bdev->bd_disk);
453 if (!base)
454 return -ENODEV;
455 if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
456 dasd_put_device(base);
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100457 return -EROFS;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 set_disk_ro(bdev->bd_disk, intval);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200460 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
461 dasd_put_device(base);
462 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100465static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
Heiko Carstens88034862010-01-13 20:44:29 +0100466 struct cmbdata __user *argp)
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800467{
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800468 size_t size = _IOC_SIZE(cmd);
469 struct cmbdata data;
470 int ret;
471
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100472 ret = cmf_readall(block->base->cdev, &data);
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800473 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
474 return -EFAULT;
475 return ret;
476}
477
Arnd Bergmanncfdb00a2010-05-31 22:38:40 +0200478int dasd_ioctl(struct block_device *bdev, fmode_t mode,
479 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200481 struct dasd_block *block;
482 struct dasd_device *base;
Heiko Carstens88034862010-01-13 20:44:29 +0100483 void __user *argp;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200484 int rc;
Heiko Carstens88034862010-01-13 20:44:29 +0100485
486 if (is_compat_task())
487 argp = compat_ptr(arg);
488 else
489 argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Christoph Hellwig13c62042006-03-24 03:15:19 -0800491 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
492 PRINT_DEBUG("empty data ptr");
493 return -EINVAL;
494 }
495
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200496 base = dasd_device_from_gendisk(bdev->bd_disk);
497 if (!base)
498 return -ENODEV;
499 block = base->block;
500 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800501 switch (cmd) {
502 case BIODASDDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200503 rc = dasd_ioctl_disable(bdev);
504 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800505 case BIODASDENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200506 rc = dasd_ioctl_enable(bdev);
507 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800508 case BIODASDQUIESCE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200509 rc = dasd_ioctl_quiesce(block);
510 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800511 case BIODASDRESUME:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200512 rc = dasd_ioctl_resume(block);
513 break;
Hannes Reinecke5ea34a02013-01-30 09:26:19 +0000514 case BIODASDABORTIO:
515 rc = dasd_ioctl_abortio(block);
516 break;
517 case BIODASDALLOWIO:
518 rc = dasd_ioctl_allowio(block);
519 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800520 case BIODASDFMT:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200521 rc = dasd_ioctl_format(bdev, argp);
522 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800523 case BIODASDINFO:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200524 rc = dasd_ioctl_information(block, cmd, argp);
525 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800526 case BIODASDINFO2:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200527 rc = dasd_ioctl_information(block, cmd, argp);
528 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800529 case BIODASDPRRD:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200530 rc = dasd_ioctl_read_profile(block, argp);
531 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800532 case BIODASDPRRST:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200533 rc = dasd_ioctl_reset_profile(block);
534 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800535 case BLKROSET:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200536 rc = dasd_ioctl_set_ro(bdev, argp);
537 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800538 case DASDAPIVER:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200539 rc = dasd_ioctl_api_version(argp);
540 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800541 case BIODASDCMFENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200542 rc = enable_cmf(base->cdev);
543 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800544 case BIODASDCMFDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200545 rc = disable_cmf(base->cdev);
546 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800547 case BIODASDREADALLCMB:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200548 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
549 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800550 default:
Christoph Hellwig1107ccf2006-03-24 03:15:20 -0800551 /* if the discipline has an ioctl method try it. */
Heiko Carstens6b79d142012-08-27 10:59:42 +0200552 rc = -ENOTTY;
553 if (base->discipline->ioctl)
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200554 rc = base->discipline->ioctl(block, cmd, argp);
Christoph Hellwig13c62042006-03-24 03:15:19 -0800555 }
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200556 dasd_put_device(base);
557 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}