blob: eb4e034378cd0d4daba20ee6988343b571ac000f [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 Weinhuber4fa52aa2011-07-24 10:48:32 +0200242 dasd_profile_reset(&block->profile);
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{
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200251 struct dasd_profile_info_t *data;
252
253 data = kmalloc(sizeof(*data), GFP_KERNEL);
254 if (!data)
255 return -ENOMEM;
256
257 spin_lock_bh(&block->profile.lock);
258 if (block->profile.data) {
259 data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
260 data->dasd_io_sects = block->profile.data->dasd_io_sects;
261 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
262 sizeof(data->dasd_io_secs));
263 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
264 sizeof(data->dasd_io_times));
265 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
266 sizeof(data->dasd_io_timps));
267 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
268 sizeof(data->dasd_io_time1));
269 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
270 sizeof(data->dasd_io_time2));
271 memcpy(data->dasd_io_time2ps,
272 block->profile.data->dasd_io_time2ps,
273 sizeof(data->dasd_io_time2ps));
274 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
275 sizeof(data->dasd_io_time3));
276 memcpy(data->dasd_io_nr_req,
277 block->profile.data->dasd_io_nr_req,
278 sizeof(data->dasd_io_nr_req));
279 spin_unlock_bh(&block->profile.lock);
280 } else {
281 spin_unlock_bh(&block->profile.lock);
Horst Hummel9a7af282006-01-06 00:19:14 -0800282 return -EIO;
Stefan Weinhuber4fa52aa2011-07-24 10:48:32 +0200283 }
284 if (copy_to_user(argp, data, sizeof(*data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -EFAULT;
286 return 0;
287}
288#else
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100289static int dasd_ioctl_reset_profile(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 return -ENOSYS;
292}
293
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100294static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 return -ENOSYS;
297}
298#endif
299
300/*
301 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
302 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100303static int dasd_ioctl_information(struct dasd_block *block,
304 unsigned int cmd, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct dasd_information2_t *dasd_info;
307 unsigned long flags;
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700308 int rc;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100309 struct dasd_device *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct ccw_device *cdev;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200311 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100313 base = block->base;
Stefan Haberland294001a2010-01-27 10:12:35 +0100314 if (!base->discipline || !base->discipline->fill_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -EINVAL;
316
Horst Hummel554a8262006-03-24 03:15:24 -0800317 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (dasd_info == NULL)
319 return -ENOMEM;
320
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100321 rc = base->discipline->fill_info(base, dasd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (rc) {
323 kfree(dasd_info);
324 return rc;
325 }
326
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100327 cdev = base->cdev;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200328 ccw_device_get_id(cdev, &dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200330 dasd_info->devno = dev_id.devno;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100331 dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 dasd_info->cu_type = cdev->id.cu_type;
333 dasd_info->cu_model = cdev->id.cu_model;
334 dasd_info->dev_type = cdev->id.dev_type;
335 dasd_info->dev_model = cdev->id.dev_model;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100336 dasd_info->status = base->state;
Horst Hummel57467192006-02-01 03:06:36 -0800337 /*
338 * The open_count is increased for every opener, that includes
339 * the blkdev_get in dasd_scan_partitions.
340 * This must be hidden from user-space.
341 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100342 dasd_info->open_count = atomic_read(&block->open_count);
343 if (!block->bdev)
Horst Hummel57467192006-02-01 03:06:36 -0800344 dasd_info->open_count++;
Horst Hummel138c0142006-06-29 14:58:12 +0200345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /*
347 * check if device is really formatted
348 * LDL / CDL was returned by 'fill_info'
349 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100350 if ((base->state < DASD_STATE_READY) ||
351 (dasd_check_blocksize(block->bp_block)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 dasd_info->format = DASD_FORMAT_NONE;
Horst Hummelf24acd42005-05-01 08:58:59 -0700353
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700354 dasd_info->features |=
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100355 ((base->features & DASD_FEATURE_READONLY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Stefan Haberland294001a2010-01-27 10:12:35 +0100357 memcpy(dasd_info->type, base->discipline->name, 4);
Horst Hummel554a8262006-03-24 03:15:24 -0800358
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100359 if (block->request_queue->request_fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct list_head *l;
361#ifdef DASD_EXTENDED_PROFILING
362 {
363 struct list_head *l;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100364 spin_lock_irqsave(&block->lock, flags);
365 list_for_each(l, &block->request_queue->queue_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 dasd_info->req_queue_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100367 spin_unlock_irqrestore(&block->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369#endif /* DASD_EXTENDED_PROFILING */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100370 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
371 list_for_each(l, &base->ccw_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dasd_info->chanq_len++;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100373 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 flags);
375 }
376
377 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800378 if (copy_to_user(argp, dasd_info,
379 ((cmd == (unsigned int) BIODASDINFO2) ?
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100380 sizeof(struct dasd_information2_t) :
381 sizeof(struct dasd_information_t))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 rc = -EFAULT;
383 kfree(dasd_info);
384 return rc;
385}
386
387/*
388 * Set read only
389 */
390static int
Christoph Hellwig13c62042006-03-24 03:15:19 -0800391dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200393 struct dasd_device *base;
394 int intval, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (!capable(CAP_SYS_ADMIN))
397 return -EACCES;
398 if (bdev != bdev->bd_contains)
399 // ro setting is not allowed for partitions
400 return -EINVAL;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200401 if (get_user(intval, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -EFAULT;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200403 base = dasd_device_from_gendisk(bdev->bd_disk);
404 if (!base)
405 return -ENODEV;
406 if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
407 dasd_put_device(base);
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100408 return -EROFS;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 set_disk_ro(bdev->bd_disk, intval);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200411 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
412 dasd_put_device(base);
413 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100416static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
Heiko Carstens88034862010-01-13 20:44:29 +0100417 struct cmbdata __user *argp)
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800418{
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800419 size_t size = _IOC_SIZE(cmd);
420 struct cmbdata data;
421 int ret;
422
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100423 ret = cmf_readall(block->base->cdev, &data);
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800424 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
425 return -EFAULT;
426 return ret;
427}
428
Arnd Bergmanncfdb00a2010-05-31 22:38:40 +0200429int dasd_ioctl(struct block_device *bdev, fmode_t mode,
430 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200432 struct dasd_block *block;
433 struct dasd_device *base;
Heiko Carstens88034862010-01-13 20:44:29 +0100434 void __user *argp;
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200435 int rc;
Heiko Carstens88034862010-01-13 20:44:29 +0100436
437 if (is_compat_task())
438 argp = compat_ptr(arg);
439 else
440 argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Christoph Hellwig13c62042006-03-24 03:15:19 -0800442 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
443 PRINT_DEBUG("empty data ptr");
444 return -EINVAL;
445 }
446
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200447 base = dasd_device_from_gendisk(bdev->bd_disk);
448 if (!base)
449 return -ENODEV;
450 block = base->block;
451 rc = 0;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800452 switch (cmd) {
453 case BIODASDDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200454 rc = dasd_ioctl_disable(bdev);
455 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800456 case BIODASDENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200457 rc = dasd_ioctl_enable(bdev);
458 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800459 case BIODASDQUIESCE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200460 rc = dasd_ioctl_quiesce(block);
461 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800462 case BIODASDRESUME:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200463 rc = dasd_ioctl_resume(block);
464 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800465 case BIODASDFMT:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200466 rc = dasd_ioctl_format(bdev, argp);
467 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800468 case BIODASDINFO:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200469 rc = dasd_ioctl_information(block, cmd, argp);
470 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800471 case BIODASDINFO2:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200472 rc = dasd_ioctl_information(block, cmd, argp);
473 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800474 case BIODASDPRRD:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200475 rc = dasd_ioctl_read_profile(block, argp);
476 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800477 case BIODASDPRRST:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200478 rc = dasd_ioctl_reset_profile(block);
479 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800480 case BLKROSET:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200481 rc = dasd_ioctl_set_ro(bdev, argp);
482 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800483 case DASDAPIVER:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200484 rc = dasd_ioctl_api_version(argp);
485 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800486 case BIODASDCMFENABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200487 rc = enable_cmf(base->cdev);
488 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800489 case BIODASDCMFDISABLE:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200490 rc = disable_cmf(base->cdev);
491 break;
Christoph Hellwig8b2eb662006-03-24 03:15:21 -0800492 case BIODASDREADALLCMB:
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200493 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
494 break;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800495 default:
Christoph Hellwig1107ccf2006-03-24 03:15:20 -0800496 /* if the discipline has an ioctl method try it. */
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200497 if (base->discipline->ioctl) {
498 rc = base->discipline->ioctl(block, cmd, argp);
499 if (rc == -ENOIOCTLCMD)
500 rc = -EINVAL;
501 } else
502 rc = -EINVAL;
Christoph Hellwig13c62042006-03-24 03:15:19 -0800503 }
Stefan Weinhuber65f8da42011-04-20 10:15:30 +0200504 dasd_put_device(base);
505 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}