ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / s390 / block / dasd_ioctl.c
blob9caeaea5d09936c882e7c057e33a54f2b09a7720
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.
13 #define KMSG_COMPONENT "dasd"
15 #include <linux/interrupt.h>
16 #include <linux/compat.h>
17 #include <linux/major.h>
18 #include <linux/fs.h>
19 #include <linux/blkpg.h>
20 #include <linux/slab.h>
21 #include <asm/compat.h>
22 #include <asm/ccwdev.h>
23 #include <asm/cmb.h>
24 #include <asm/uaccess.h>
26 /* This is ugly... */
27 #define PRINTK_HEADER "dasd_ioctl:"
29 #include "dasd_int.h"
32 static int
33 dasd_ioctl_api_version(void __user *argp)
35 int ver = DASD_API_VERSION;
36 return put_user(ver, (int __user *)argp);
40 * Enable device.
41 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
43 static int
44 dasd_ioctl_enable(struct block_device *bdev)
46 struct dasd_device *base;
48 if (!capable(CAP_SYS_ADMIN))
49 return -EACCES;
51 base = dasd_device_from_gendisk(bdev->bd_disk);
52 if (!base)
53 return -ENODEV;
55 dasd_enable_device(base);
56 /* Formatting the dasd device can change the capacity. */
57 mutex_lock(&bdev->bd_mutex);
58 i_size_write(bdev->bd_inode,
59 (loff_t)get_capacity(base->block->gdp) << 9);
60 mutex_unlock(&bdev->bd_mutex);
61 dasd_put_device(base);
62 return 0;
66 * Disable device.
67 * Used by dasdfmt. Disable I/O operations but allow ioctls.
69 static int
70 dasd_ioctl_disable(struct block_device *bdev)
72 struct dasd_device *base;
74 if (!capable(CAP_SYS_ADMIN))
75 return -EACCES;
77 base = dasd_device_from_gendisk(bdev->bd_disk);
78 if (!base)
79 return -ENODEV;
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.
88 dasd_set_target_state(base, DASD_STATE_BASIC);
90 * Set i_size to zero, since read, write, etc. check against this
91 * value.
93 mutex_lock(&bdev->bd_mutex);
94 i_size_write(bdev->bd_inode, 0);
95 mutex_unlock(&bdev->bd_mutex);
96 dasd_put_device(base);
97 return 0;
101 * Quiesce device.
103 static int dasd_ioctl_quiesce(struct dasd_block *block)
105 unsigned long flags;
106 struct dasd_device *base;
108 base = block->base;
109 if (!capable (CAP_SYS_ADMIN))
110 return -EACCES;
112 pr_info("%s: The DASD has been put in the quiesce "
113 "state\n", dev_name(&base->cdev->dev));
114 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
115 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
116 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
117 return 0;
122 * Resume device.
124 static int dasd_ioctl_resume(struct dasd_block *block)
126 unsigned long flags;
127 struct dasd_device *base;
129 base = block->base;
130 if (!capable (CAP_SYS_ADMIN))
131 return -EACCES;
133 pr_info("%s: I/O operations have been resumed "
134 "on the DASD\n", dev_name(&base->cdev->dev));
135 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
136 dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
137 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
139 dasd_schedule_block_bh(block);
140 return 0;
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.
149 static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
151 struct dasd_ccw_req *cqr;
152 struct dasd_device *base;
153 int rc;
155 base = block->base;
156 if (base->discipline->format_device == NULL)
157 return -EPERM;
159 if (base->state != DASD_STATE_BASIC) {
160 pr_warning("%s: The DASD cannot be formatted while it is "
161 "enabled\n", dev_name(&base->cdev->dev));
162 return -EBUSY;
165 DBF_DEV_EVENT(DBF_NOTICE, base,
166 "formatting units %u to %u (%u B blocks) flags %u",
167 fdata->start_unit,
168 fdata->stop_unit, fdata->blksize, fdata->intensity);
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.
175 if (fdata->start_unit == 0) {
176 struct block_device *bdev = bdget_disk(block->gdp, 0);
177 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
178 bdput(bdev);
181 while (fdata->start_unit <= fdata->stop_unit) {
182 cqr = base->discipline->format_device(base, fdata);
183 if (IS_ERR(cqr))
184 return PTR_ERR(cqr);
185 rc = dasd_sleep_on_interruptible(cqr);
186 dasd_sfree_request(cqr, cqr->memdev);
187 if (rc) {
188 if (rc != -ERESTARTSYS)
189 pr_err("%s: Formatting unit %d failed with "
190 "rc=%d\n", dev_name(&base->cdev->dev),
191 fdata->start_unit, rc);
192 return rc;
194 fdata->start_unit++;
196 return 0;
200 * Format device.
202 static int
203 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
205 struct dasd_device *base;
206 struct format_data_t fdata;
207 int rc;
209 if (!capable(CAP_SYS_ADMIN))
210 return -EACCES;
211 if (!argp)
212 return -EINVAL;
213 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);
219 return -EROFS;
221 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
222 dasd_put_device(base);
223 return -EFAULT;
225 if (bdev != bdev->bd_contains) {
226 pr_warning("%s: The specified DASD is a partition and cannot "
227 "be formatted\n",
228 dev_name(&base->cdev->dev));
229 dasd_put_device(base);
230 return -EINVAL;
232 rc = dasd_format(base->block, &fdata);
233 dasd_put_device(base);
234 return rc;
237 #ifdef CONFIG_DASD_PROFILE
239 * Reset device profile information
241 static int dasd_ioctl_reset_profile(struct dasd_block *block)
243 memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
244 return 0;
248 * Return device profile information
250 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
252 if (dasd_profile_level == DASD_PROFILE_OFF)
253 return -EIO;
254 if (copy_to_user(argp, &block->profile,
255 sizeof(struct dasd_profile_info_t)))
256 return -EFAULT;
257 return 0;
259 #else
260 static int dasd_ioctl_reset_profile(struct dasd_block *block)
262 return -ENOSYS;
265 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
267 return -ENOSYS;
269 #endif
272 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
274 static int dasd_ioctl_information(struct dasd_block *block,
275 unsigned int cmd, void __user *argp)
277 struct dasd_information2_t *dasd_info;
278 unsigned long flags;
279 int rc;
280 struct dasd_device *base;
281 struct ccw_device *cdev;
282 struct ccw_dev_id dev_id;
284 base = block->base;
285 if (!base->discipline || !base->discipline->fill_info)
286 return -EINVAL;
288 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
289 if (dasd_info == NULL)
290 return -ENOMEM;
292 rc = base->discipline->fill_info(base, dasd_info);
293 if (rc) {
294 kfree(dasd_info);
295 return rc;
298 cdev = base->cdev;
299 ccw_device_get_id(cdev, &dev_id);
301 dasd_info->devno = dev_id.devno;
302 dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
303 dasd_info->cu_type = cdev->id.cu_type;
304 dasd_info->cu_model = cdev->id.cu_model;
305 dasd_info->dev_type = cdev->id.dev_type;
306 dasd_info->dev_model = cdev->id.dev_model;
307 dasd_info->status = base->state;
309 * The open_count is increased for every opener, that includes
310 * the blkdev_get in dasd_scan_partitions.
311 * This must be hidden from user-space.
313 dasd_info->open_count = atomic_read(&block->open_count);
314 if (!block->bdev)
315 dasd_info->open_count++;
318 * check if device is really formatted
319 * LDL / CDL was returned by 'fill_info'
321 if ((base->state < DASD_STATE_READY) ||
322 (dasd_check_blocksize(block->bp_block)))
323 dasd_info->format = DASD_FORMAT_NONE;
325 dasd_info->features |=
326 ((base->features & DASD_FEATURE_READONLY) != 0);
328 memcpy(dasd_info->type, base->discipline->name, 4);
330 if (block->request_queue->request_fn) {
331 struct list_head *l;
332 #ifdef DASD_EXTENDED_PROFILING
334 struct list_head *l;
335 spin_lock_irqsave(&block->lock, flags);
336 list_for_each(l, &block->request_queue->queue_head)
337 dasd_info->req_queue_len++;
338 spin_unlock_irqrestore(&block->lock, flags);
340 #endif /* DASD_EXTENDED_PROFILING */
341 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
342 list_for_each(l, &base->ccw_queue)
343 dasd_info->chanq_len++;
344 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
345 flags);
348 rc = 0;
349 if (copy_to_user(argp, dasd_info,
350 ((cmd == (unsigned int) BIODASDINFO2) ?
351 sizeof(struct dasd_information2_t) :
352 sizeof(struct dasd_information_t))))
353 rc = -EFAULT;
354 kfree(dasd_info);
355 return rc;
359 * Set read only
361 static int
362 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
364 struct dasd_device *base;
365 int intval, rc;
367 if (!capable(CAP_SYS_ADMIN))
368 return -EACCES;
369 if (bdev != bdev->bd_contains)
370 // ro setting is not allowed for partitions
371 return -EINVAL;
372 if (get_user(intval, (int __user *)argp))
373 return -EFAULT;
374 base = dasd_device_from_gendisk(bdev->bd_disk);
375 if (!base)
376 return -ENODEV;
377 if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
378 dasd_put_device(base);
379 return -EROFS;
381 set_disk_ro(bdev->bd_disk, intval);
382 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
383 dasd_put_device(base);
384 return rc;
387 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
388 struct cmbdata __user *argp)
390 size_t size = _IOC_SIZE(cmd);
391 struct cmbdata data;
392 int ret;
394 ret = cmf_readall(block->base->cdev, &data);
395 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
396 return -EFAULT;
397 return ret;
400 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
401 unsigned int cmd, unsigned long arg)
403 struct dasd_block *block;
404 struct dasd_device *base;
405 void __user *argp;
406 int rc;
408 if (is_compat_task())
409 argp = compat_ptr(arg);
410 else
411 argp = (void __user *)arg;
413 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
414 PRINT_DEBUG("empty data ptr");
415 return -EINVAL;
418 base = dasd_device_from_gendisk(bdev->bd_disk);
419 if (!base)
420 return -ENODEV;
421 block = base->block;
422 rc = 0;
423 switch (cmd) {
424 case BIODASDDISABLE:
425 rc = dasd_ioctl_disable(bdev);
426 break;
427 case BIODASDENABLE:
428 rc = dasd_ioctl_enable(bdev);
429 break;
430 case BIODASDQUIESCE:
431 rc = dasd_ioctl_quiesce(block);
432 break;
433 case BIODASDRESUME:
434 rc = dasd_ioctl_resume(block);
435 break;
436 case BIODASDFMT:
437 rc = dasd_ioctl_format(bdev, argp);
438 break;
439 case BIODASDINFO:
440 rc = dasd_ioctl_information(block, cmd, argp);
441 break;
442 case BIODASDINFO2:
443 rc = dasd_ioctl_information(block, cmd, argp);
444 break;
445 case BIODASDPRRD:
446 rc = dasd_ioctl_read_profile(block, argp);
447 break;
448 case BIODASDPRRST:
449 rc = dasd_ioctl_reset_profile(block);
450 break;
451 case BLKROSET:
452 rc = dasd_ioctl_set_ro(bdev, argp);
453 break;
454 case DASDAPIVER:
455 rc = dasd_ioctl_api_version(argp);
456 break;
457 case BIODASDCMFENABLE:
458 rc = enable_cmf(base->cdev);
459 break;
460 case BIODASDCMFDISABLE:
461 rc = disable_cmf(base->cdev);
462 break;
463 case BIODASDREADALLCMB:
464 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
465 break;
466 default:
467 /* if the discipline has an ioctl method try it. */
468 if (base->discipline->ioctl) {
469 rc = base->discipline->ioctl(block, cmd, argp);
470 if (rc == -ENOIOCTLCMD)
471 rc = -EINVAL;
472 } else
473 rc = -EINVAL;
475 dasd_put_device(base);
476 return rc;