blob: 72261e4c516de89e836a2afb3d646d24b947ecfd [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>
16#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>
Christoph Hellwig8b2eb662006-03-24 03:15:21 -080022#include <asm/cmb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24
25/* This is ugly... */
26#define PRINTK_HEADER "dasd_ioctl:"
27
28#include "dasd_int.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080032dasd_ioctl_api_version(void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 int ver = DASD_API_VERSION;
Bastian Blankb5029622006-03-24 03:15:32 -080035 return put_user(ver, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38/*
39 * Enable device.
40 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
41 */
42static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080043dasd_ioctl_enable(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020045 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 if (!capable(CAP_SYS_ADMIN))
48 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -080049
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020050 base = dasd_device_from_gendisk(bdev->bd_disk);
51 if (!base)
52 return -ENODEV;
53
54 dasd_enable_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /* Formatting the dasd device can change the capacity. */
Arjan van de Venc039e312006-03-23 03:00:28 -080056 mutex_lock(&bdev->bd_mutex);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020057 i_size_write(bdev->bd_inode,
58 (loff_t)get_capacity(base->block->gdp) << 9);
Arjan van de Venc039e312006-03-23 03:00:28 -080059 mutex_unlock(&bdev->bd_mutex);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020060 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return 0;
62}
63
64/*
65 * Disable device.
66 * Used by dasdfmt. Disable I/O operations but allow ioctls.
67 */
68static int
Christoph Hellwig13c62042006-03-24 03:15:19 -080069dasd_ioctl_disable(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020071 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (!capable(CAP_SYS_ADMIN))
74 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -080075
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020076 base = dasd_device_from_gendisk(bdev->bd_disk);
77 if (!base)
78 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /*
80 * Man this is sick. We don't do a real disable but only downgrade
81 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
82 * BIODASDDISABLE to disable accesses to the device via the block
83 * device layer but it still wants to do i/o on the device by
84 * using the BIODASDFMT ioctl. Therefore the correct state for the
85 * device is DASD_STATE_BASIC that allows to do basic i/o.
86 */
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020087 dasd_set_target_state(base, DASD_STATE_BASIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /*
89 * Set i_size to zero, since read, write, etc. check against this
90 * value.
91 */
Arjan van de Venc039e312006-03-23 03:00:28 -080092 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 i_size_write(bdev->bd_inode, 0);
Arjan van de Venc039e312006-03-23 03:00:28 -080094 mutex_unlock(&bdev->bd_mutex);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020095 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97}
98
99/*
100 * Quiesce device.
101 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100102static int dasd_ioctl_quiesce(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100105 struct dasd_device *base;
Horst Hummel138c0142006-06-29 14:58:12 +0200106
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100107 base = block->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (!capable (CAP_SYS_ADMIN))
109 return -EACCES;
Horst Hummel138c0142006-06-29 14:58:12 +0200110
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200111 pr_info("%s: The DASD has been put in the quiesce "
112 "state\n", dev_name(&base->cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100113 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100114 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100115 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}
118
119
120/*
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100121 * Resume device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100123static int dasd_ioctl_resume(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100126 struct dasd_device *base;
Horst Hummel138c0142006-06-29 14:58:12 +0200127
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100128 base = block->base;
Horst Hummel138c0142006-06-29 14:58:12 +0200129 if (!capable (CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -EACCES;
131
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200132 pr_info("%s: I/O operations have been resumed "
133 "on the DASD\n", dev_name(&base->cdev->dev));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100134 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100135 dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100136 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
Horst Hummel138c0142006-06-29 14:58:12 +0200137
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100138 dasd_schedule_block_bh(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return 0;
140}
141
142/*
143 * performs formatting of _device_ according to _fdata_
144 * Note: The discipline's format_function is assumed to deliver formatting
145 * commands to format a single unit of the device. In terms of the ECKD
146 * devices this means CCWs are generated to format a single track.
147 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100148static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct dasd_ccw_req *cqr;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100151 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int rc;
153
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100154 base = block->base;
155 if (base->discipline->format_device == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -EPERM;
157
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100158 if (base->state != DASD_STATE_BASIC) {
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200159 pr_warning("%s: The DASD cannot be formatted while it is "
160 "enabled\n", dev_name(&base->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -EBUSY;
162 }
163
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100164 DBF_DEV_EVENT(DBF_NOTICE, base,
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100165 "formatting units %u to %u (%u B blocks) flags %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 fdata->start_unit,
167 fdata->stop_unit, fdata->blksize, fdata->intensity);
168
169 /* Since dasdfmt keeps the device open after it was disabled,
170 * there still exists an inode for this device.
171 * We must update i_blkbits, otherwise we might get errors when
172 * enabling the device later.
173 */
174 if (fdata->start_unit == 0) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100175 struct block_device *bdev = bdget_disk(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
177 bdput(bdev);
178 }
179
180 while (fdata->start_unit <= fdata->stop_unit) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100181 cqr = base->discipline->format_device(base, fdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (IS_ERR(cqr))
183 return PTR_ERR(cqr);
184 rc = dasd_sleep_on_interruptible(cqr);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100185 dasd_sfree_request(cqr, cqr->memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (rc) {
187 if (rc != -ERESTARTSYS)
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200188 pr_err("%s: Formatting unit %d failed with "
189 "rc=%d\n", dev_name(&base->cdev->dev),
190 fdata->start_unit, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return rc;
192 }
193 fdata->start_unit++;
194 }
195 return 0;
196}
197
198/*
199 * Format device.
200 */
201static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800202dasd_ioctl_format(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200204 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct format_data_t fdata;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200206 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (!capable(CAP_SYS_ADMIN))
209 return -EACCES;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800210 if (!argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -EINVAL;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200212 base = dasd_device_from_gendisk(bdev->bd_disk);
213 if (!base)
214 return -ENODEV;
215 if (base->features & DASD_FEATURE_READONLY ||
216 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
217 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -EROFS;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200219 }
220 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
221 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -EFAULT;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (bdev != bdev->bd_contains) {
Stefan Haberlandca99dab2009-09-11 10:28:30 +0200225 pr_warning("%s: The specified DASD is a partition and cannot "
226 "be formatted\n",
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200227 dev_name(&base->cdev->dev));
228 dasd_put_device(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EINVAL;
230 }
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200231 rc = dasd_format(base->block, &fdata);
232 dasd_put_device(base);
233 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236#ifdef CONFIG_DASD_PROFILE
237/*
238 * Reset device profile information
239 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100240static int dasd_ioctl_reset_profile(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100242 memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
246/*
247 * Return device profile information
248 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100249static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Horst Hummel9a7af282006-01-06 00:19:14 -0800251 if (dasd_profile_level == DASD_PROFILE_OFF)
252 return -EIO;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100253 if (copy_to_user(argp, &block->profile,
254 sizeof(struct dasd_profile_info_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EFAULT;
256 return 0;
257}
258#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100259static int dasd_ioctl_reset_profile(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 return -ENOSYS;
262}
263
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100264static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 return -ENOSYS;
267}
268#endif
269
270/*
271 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
272 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100273static int dasd_ioctl_information(struct dasd_block *block,
274 unsigned int cmd, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct dasd_information2_t *dasd_info;
277 unsigned long flags;
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700278 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100279 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct ccw_device *cdev;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200281 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100283 base = block->base;
Stefan Haberland294001a2010-01-27 10:12:35 +0100284 if (!base->discipline || !base->discipline->fill_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -EINVAL;
286
Horst Hummel554a8262006-03-24 03:15:24 -0800287 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (dasd_info == NULL)
289 return -ENOMEM;
290
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100291 rc = base->discipline->fill_info(base, dasd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (rc) {
293 kfree(dasd_info);
294 return rc;
295 }
296
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100297 cdev = base->cdev;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200298 ccw_device_get_id(cdev, &dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200300 dasd_info->devno = dev_id.devno;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100301 dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 dasd_info->cu_type = cdev->id.cu_type;
303 dasd_info->cu_model = cdev->id.cu_model;
304 dasd_info->dev_type = cdev->id.dev_type;
305 dasd_info->dev_model = cdev->id.dev_model;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100306 dasd_info->status = base->state;
Horst Hummel57467192006-02-01 03:06:36 -0800307 /*
308 * The open_count is increased for every opener, that includes
309 * the blkdev_get in dasd_scan_partitions.
310 * This must be hidden from user-space.
311 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100312 dasd_info->open_count = atomic_read(&block->open_count);
313 if (!block->bdev)
Horst Hummel57467192006-02-01 03:06:36 -0800314 dasd_info->open_count++;
Horst Hummel138c0142006-06-29 14:58:12 +0200315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
317 * check if device is really formatted
318 * LDL / CDL was returned by 'fill_info'
319 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100320 if ((base->state < DASD_STATE_READY) ||
321 (dasd_check_blocksize(block->bp_block)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 dasd_info->format = DASD_FORMAT_NONE;
Horst Hummelf24acd42005-05-01 08:58:59 -0700323
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700324 dasd_info->features |=
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100325 ((base->features & DASD_FEATURE_READONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Stefan Haberland294001a2010-01-27 10:12:35 +0100327 memcpy(dasd_info->type, base->discipline->name, 4);
Horst Hummel554a8262006-03-24 03:15:24 -0800328
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100329 if (block->request_queue->request_fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct list_head *l;
331#ifdef DASD_EXTENDED_PROFILING
332 {
333 struct list_head *l;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100334 spin_lock_irqsave(&block->lock, flags);
335 list_for_each(l, &block->request_queue->queue_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dasd_info->req_queue_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100337 spin_unlock_irqrestore(&block->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339#endif /* DASD_EXTENDED_PROFILING */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100340 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
341 list_for_each(l, &base->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dasd_info->chanq_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100343 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 flags);
345 }
346
347 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800348 if (copy_to_user(argp, dasd_info,
349 ((cmd == (unsigned int) BIODASDINFO2) ?
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100350 sizeof(struct dasd_information2_t) :
351 sizeof(struct dasd_information_t))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 rc = -EFAULT;
353 kfree(dasd_info);
354 return rc;
355}
356
357/*
358 * Set read only
359 */
360static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800361dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200363 struct dasd_device *base;
364 int intval, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (!capable(CAP_SYS_ADMIN))
367 return -EACCES;
368 if (bdev != bdev->bd_contains)
369 // ro setting is not allowed for partitions
370 return -EINVAL;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200371 if (get_user(intval, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -EFAULT;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200373 base = dasd_device_from_gendisk(bdev->bd_disk);
374 if (!base)
375 return -ENODEV;
376 if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
377 dasd_put_device(base);
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100378 return -EROFS;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 set_disk_ro(bdev->bd_disk, intval);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200381 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
382 dasd_put_device(base);
383 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100386static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
Heiko Carstens88034862010-01-13 20:44:29 +0100387 struct cmbdata __user *argp)
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800388{
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800389 size_t size = _IOC_SIZE(cmd);
390 struct cmbdata data;
391 int ret;
392
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100393 ret = cmf_readall(block->base->cdev, &data);
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800394 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
395 return -EFAULT;
396 return ret;
397}
398
Arnd Bergmanncfdb00a2010-05-31 22:38:40 +0200399int dasd_ioctl(struct block_device *bdev, fmode_t mode,
400 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200402 struct dasd_block *block;
403 struct dasd_device *base;
Heiko Carstens88034862010-01-13 20:44:29 +0100404 void __user *argp;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200405 int rc;
Heiko Carstens88034862010-01-13 20:44:29 +0100406
407 if (is_compat_task())
408 argp = compat_ptr(arg);
409 else
410 argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Christoph Hellwig13c62042006-03-24 03:15:19 -0800412 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
413 PRINT_DEBUG("empty data ptr");
414 return -EINVAL;
415 }
416
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200417 base = dasd_device_from_gendisk(bdev->bd_disk);
418 if (!base)
419 return -ENODEV;
420 block = base->block;
421 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800422 switch (cmd) {
423 case BIODASDDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200424 rc = dasd_ioctl_disable(bdev);
425 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800426 case BIODASDENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200427 rc = dasd_ioctl_enable(bdev);
428 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800429 case BIODASDQUIESCE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200430 rc = dasd_ioctl_quiesce(block);
431 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800432 case BIODASDRESUME:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200433 rc = dasd_ioctl_resume(block);
434 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800435 case BIODASDFMT:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200436 rc = dasd_ioctl_format(bdev, argp);
437 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800438 case BIODASDINFO:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200439 rc = dasd_ioctl_information(block, cmd, argp);
440 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800441 case BIODASDINFO2:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200442 rc = dasd_ioctl_information(block, cmd, argp);
443 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800444 case BIODASDPRRD:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200445 rc = dasd_ioctl_read_profile(block, argp);
446 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800447 case BIODASDPRRST:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200448 rc = dasd_ioctl_reset_profile(block);
449 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800450 case BLKROSET:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200451 rc = dasd_ioctl_set_ro(bdev, argp);
452 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800453 case DASDAPIVER:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200454 rc = dasd_ioctl_api_version(argp);
455 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800456 case BIODASDCMFENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200457 rc = enable_cmf(base->cdev);
458 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800459 case BIODASDCMFDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200460 rc = disable_cmf(base->cdev);
461 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800462 case BIODASDREADALLCMB:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200463 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
464 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800465 default:
Christoph Hellwig1107ccf2006-03-24 03:15:20 -0800466 /* if the discipline has an ioctl method try it. */
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200467 if (base->discipline->ioctl) {
468 rc = base->discipline->ioctl(block, cmd, argp);
469 if (rc == -ENOIOCTLCMD)
470 rc = -EINVAL;
471 } else
472 rc = -EINVAL;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800473 }
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200474 dasd_put_device(base);
475 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}