Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / s390 / block / dasd_ioctl.c
blob91a64630cb0f4095f9fa9933189df9a520bd77ab
1 /*
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
10 * i/o controls for the dasd driver.
12 #include <linux/interrupt.h>
13 #include <linux/major.h>
14 #include <linux/fs.h>
15 #include <linux/blkpg.h>
17 #include <asm/ccwdev.h>
18 #include <asm/cmb.h>
19 #include <asm/uaccess.h>
21 /* This is ugly... */
22 #define PRINTK_HEADER "dasd_ioctl:"
24 #include "dasd_int.h"
27 static int
28 dasd_ioctl_api_version(void __user *argp)
30 int ver = DASD_API_VERSION;
31 return put_user(ver, (int __user *)argp);
35 * Enable device.
36 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
38 static int
39 dasd_ioctl_enable(struct block_device *bdev)
41 struct dasd_block *block = bdev->bd_disk->private_data;
43 if (!capable(CAP_SYS_ADMIN))
44 return -EACCES;
46 dasd_enable_device(block->base);
47 /* Formatting the dasd device can change the capacity. */
48 mutex_lock(&bdev->bd_mutex);
49 i_size_write(bdev->bd_inode, (loff_t)get_capacity(block->gdp) << 9);
50 mutex_unlock(&bdev->bd_mutex);
51 return 0;
55 * Disable device.
56 * Used by dasdfmt. Disable I/O operations but allow ioctls.
58 static int
59 dasd_ioctl_disable(struct block_device *bdev)
61 struct dasd_block *block = bdev->bd_disk->private_data;
63 if (!capable(CAP_SYS_ADMIN))
64 return -EACCES;
67 * Man this is sick. We don't do a real disable but only downgrade
68 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
69 * BIODASDDISABLE to disable accesses to the device via the block
70 * device layer but it still wants to do i/o on the device by
71 * using the BIODASDFMT ioctl. Therefore the correct state for the
72 * device is DASD_STATE_BASIC that allows to do basic i/o.
74 dasd_set_target_state(block->base, DASD_STATE_BASIC);
76 * Set i_size to zero, since read, write, etc. check against this
77 * value.
79 mutex_lock(&bdev->bd_mutex);
80 i_size_write(bdev->bd_inode, 0);
81 mutex_unlock(&bdev->bd_mutex);
82 return 0;
86 * Quiesce device.
88 static int dasd_ioctl_quiesce(struct dasd_block *block)
90 unsigned long flags;
91 struct dasd_device *base;
93 base = block->base;
94 if (!capable (CAP_SYS_ADMIN))
95 return -EACCES;
97 DEV_MESSAGE(KERN_DEBUG, base, "%s", "Quiesce IO on device");
98 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
99 base->stopped |= DASD_STOPPED_QUIESCE;
100 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
101 return 0;
106 * Quiesce device.
108 static int dasd_ioctl_resume(struct dasd_block *block)
110 unsigned long flags;
111 struct dasd_device *base;
113 base = block->base;
114 if (!capable (CAP_SYS_ADMIN))
115 return -EACCES;
117 DEV_MESSAGE(KERN_DEBUG, base, "%s", "resume IO on device");
118 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
119 base->stopped &= ~DASD_STOPPED_QUIESCE;
120 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
122 dasd_schedule_block_bh(block);
123 return 0;
127 * performs formatting of _device_ according to _fdata_
128 * Note: The discipline's format_function is assumed to deliver formatting
129 * commands to format a single unit of the device. In terms of the ECKD
130 * devices this means CCWs are generated to format a single track.
132 static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
134 struct dasd_ccw_req *cqr;
135 struct dasd_device *base;
136 int rc;
138 base = block->base;
139 if (base->discipline->format_device == NULL)
140 return -EPERM;
142 if (base->state != DASD_STATE_BASIC) {
143 DEV_MESSAGE(KERN_WARNING, base, "%s",
144 "dasd_format: device is not disabled! ");
145 return -EBUSY;
148 DBF_DEV_EVENT(DBF_NOTICE, base,
149 "formatting units %d to %d (%d B blocks) flags %d",
150 fdata->start_unit,
151 fdata->stop_unit, fdata->blksize, fdata->intensity);
153 /* Since dasdfmt keeps the device open after it was disabled,
154 * there still exists an inode for this device.
155 * We must update i_blkbits, otherwise we might get errors when
156 * enabling the device later.
158 if (fdata->start_unit == 0) {
159 struct block_device *bdev = bdget_disk(block->gdp, 0);
160 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
161 bdput(bdev);
164 while (fdata->start_unit <= fdata->stop_unit) {
165 cqr = base->discipline->format_device(base, fdata);
166 if (IS_ERR(cqr))
167 return PTR_ERR(cqr);
168 rc = dasd_sleep_on_interruptible(cqr);
169 dasd_sfree_request(cqr, cqr->memdev);
170 if (rc) {
171 if (rc != -ERESTARTSYS)
172 DEV_MESSAGE(KERN_ERR, base,
173 " Formatting of unit %d failed "
174 "with rc = %d",
175 fdata->start_unit, rc);
176 return rc;
178 fdata->start_unit++;
180 return 0;
184 * Format device.
186 static int
187 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
189 struct dasd_block *block = bdev->bd_disk->private_data;
190 struct format_data_t fdata;
192 if (!capable(CAP_SYS_ADMIN))
193 return -EACCES;
194 if (!argp)
195 return -EINVAL;
197 if (block->base->features & DASD_FEATURE_READONLY)
198 return -EROFS;
199 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
200 return -EFAULT;
201 if (bdev != bdev->bd_contains) {
202 DEV_MESSAGE(KERN_WARNING, block->base, "%s",
203 "Cannot low-level format a partition");
204 return -EINVAL;
206 return dasd_format(block, &fdata);
209 #ifdef CONFIG_DASD_PROFILE
211 * Reset device profile information
213 static int dasd_ioctl_reset_profile(struct dasd_block *block)
215 memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
216 return 0;
220 * Return device profile information
222 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
224 if (dasd_profile_level == DASD_PROFILE_OFF)
225 return -EIO;
226 if (copy_to_user(argp, &block->profile,
227 sizeof(struct dasd_profile_info_t)))
228 return -EFAULT;
229 return 0;
231 #else
232 static int dasd_ioctl_reset_profile(struct dasd_block *block)
234 return -ENOSYS;
237 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
239 return -ENOSYS;
241 #endif
244 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
246 static int dasd_ioctl_information(struct dasd_block *block,
247 unsigned int cmd, void __user *argp)
249 struct dasd_information2_t *dasd_info;
250 unsigned long flags;
251 int rc;
252 struct dasd_device *base;
253 struct ccw_device *cdev;
254 struct ccw_dev_id dev_id;
256 base = block->base;
257 if (!base->discipline->fill_info)
258 return -EINVAL;
260 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
261 if (dasd_info == NULL)
262 return -ENOMEM;
264 rc = base->discipline->fill_info(base, dasd_info);
265 if (rc) {
266 kfree(dasd_info);
267 return rc;
270 cdev = base->cdev;
271 ccw_device_get_id(cdev, &dev_id);
273 dasd_info->devno = dev_id.devno;
274 dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
275 dasd_info->cu_type = cdev->id.cu_type;
276 dasd_info->cu_model = cdev->id.cu_model;
277 dasd_info->dev_type = cdev->id.dev_type;
278 dasd_info->dev_model = cdev->id.dev_model;
279 dasd_info->status = base->state;
281 * The open_count is increased for every opener, that includes
282 * the blkdev_get in dasd_scan_partitions.
283 * This must be hidden from user-space.
285 dasd_info->open_count = atomic_read(&block->open_count);
286 if (!block->bdev)
287 dasd_info->open_count++;
290 * check if device is really formatted
291 * LDL / CDL was returned by 'fill_info'
293 if ((base->state < DASD_STATE_READY) ||
294 (dasd_check_blocksize(block->bp_block)))
295 dasd_info->format = DASD_FORMAT_NONE;
297 dasd_info->features |=
298 ((base->features & DASD_FEATURE_READONLY) != 0);
300 if (base->discipline)
301 memcpy(dasd_info->type, base->discipline->name, 4);
302 else
303 memcpy(dasd_info->type, "none", 4);
305 if (block->request_queue->request_fn) {
306 struct list_head *l;
307 #ifdef DASD_EXTENDED_PROFILING
309 struct list_head *l;
310 spin_lock_irqsave(&block->lock, flags);
311 list_for_each(l, &block->request_queue->queue_head)
312 dasd_info->req_queue_len++;
313 spin_unlock_irqrestore(&block->lock, flags);
315 #endif /* DASD_EXTENDED_PROFILING */
316 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
317 list_for_each(l, &base->ccw_queue)
318 dasd_info->chanq_len++;
319 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
320 flags);
323 rc = 0;
324 if (copy_to_user(argp, dasd_info,
325 ((cmd == (unsigned int) BIODASDINFO2) ?
326 sizeof(struct dasd_information2_t) :
327 sizeof(struct dasd_information_t))))
328 rc = -EFAULT;
329 kfree(dasd_info);
330 return rc;
334 * Set read only
336 static int
337 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
339 struct dasd_block *block = bdev->bd_disk->private_data;
340 int intval;
342 if (!capable(CAP_SYS_ADMIN))
343 return -EACCES;
344 if (bdev != bdev->bd_contains)
345 // ro setting is not allowed for partitions
346 return -EINVAL;
347 if (get_user(intval, (int __user *)argp))
348 return -EFAULT;
350 set_disk_ro(bdev->bd_disk, intval);
351 return dasd_set_feature(block->base->cdev, DASD_FEATURE_READONLY, intval);
354 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
355 unsigned long arg)
357 struct cmbdata __user *argp = (void __user *) arg;
358 size_t size = _IOC_SIZE(cmd);
359 struct cmbdata data;
360 int ret;
362 ret = cmf_readall(block->base->cdev, &data);
363 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
364 return -EFAULT;
365 return ret;
369 dasd_ioctl(struct inode *inode, struct file *file,
370 unsigned int cmd, unsigned long arg)
372 struct block_device *bdev = inode->i_bdev;
373 struct dasd_block *block = bdev->bd_disk->private_data;
374 void __user *argp = (void __user *)arg;
376 if (!block)
377 return -ENODEV;
379 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
380 PRINT_DEBUG("empty data ptr");
381 return -EINVAL;
384 switch (cmd) {
385 case BIODASDDISABLE:
386 return dasd_ioctl_disable(bdev);
387 case BIODASDENABLE:
388 return dasd_ioctl_enable(bdev);
389 case BIODASDQUIESCE:
390 return dasd_ioctl_quiesce(block);
391 case BIODASDRESUME:
392 return dasd_ioctl_resume(block);
393 case BIODASDFMT:
394 return dasd_ioctl_format(bdev, argp);
395 case BIODASDINFO:
396 return dasd_ioctl_information(block, cmd, argp);
397 case BIODASDINFO2:
398 return dasd_ioctl_information(block, cmd, argp);
399 case BIODASDPRRD:
400 return dasd_ioctl_read_profile(block, argp);
401 case BIODASDPRRST:
402 return dasd_ioctl_reset_profile(block);
403 case BLKROSET:
404 return dasd_ioctl_set_ro(bdev, argp);
405 case DASDAPIVER:
406 return dasd_ioctl_api_version(argp);
407 case BIODASDCMFENABLE:
408 return enable_cmf(block->base->cdev);
409 case BIODASDCMFDISABLE:
410 return disable_cmf(block->base->cdev);
411 case BIODASDREADALLCMB:
412 return dasd_ioctl_readall_cmb(block, cmd, arg);
413 default:
414 /* if the discipline has an ioctl method try it. */
415 if (block->base->discipline->ioctl) {
416 int rval = block->base->discipline->ioctl(block, cmd, argp);
417 if (rval != -ENOIOCTLCMD)
418 return rval;
421 return -EINVAL;
425 long
426 dasd_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
428 int rval;
430 lock_kernel();
431 rval = dasd_ioctl(filp->f_path.dentry->d_inode, filp, cmd, arg);
432 unlock_kernel();
434 return (rval == -EINVAL) ? -ENOIOCTLCMD : rval;