x86, UV: Fix for nodes with memory and no cpus
[linux/fpc-iii.git] / drivers / s390 / block / dasd_ioctl.c
blob4ce3f72ee1c13e2485e9a781b389f0eda2fd1c08
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/major.h>
17 #include <linux/fs.h>
18 #include <linux/blkpg.h>
20 #include <asm/ccwdev.h>
21 #include <asm/cmb.h>
22 #include <asm/uaccess.h>
24 /* This is ugly... */
25 #define PRINTK_HEADER "dasd_ioctl:"
27 #include "dasd_int.h"
30 static int
31 dasd_ioctl_api_version(void __user *argp)
33 int ver = DASD_API_VERSION;
34 return put_user(ver, (int __user *)argp);
38 * Enable device.
39 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
41 static int
42 dasd_ioctl_enable(struct block_device *bdev)
44 struct dasd_block *block = bdev->bd_disk->private_data;
46 if (!capable(CAP_SYS_ADMIN))
47 return -EACCES;
49 dasd_enable_device(block->base);
50 /* Formatting the dasd device can change the capacity. */
51 mutex_lock(&bdev->bd_mutex);
52 i_size_write(bdev->bd_inode, (loff_t)get_capacity(block->gdp) << 9);
53 mutex_unlock(&bdev->bd_mutex);
54 return 0;
58 * Disable device.
59 * Used by dasdfmt. Disable I/O operations but allow ioctls.
61 static int
62 dasd_ioctl_disable(struct block_device *bdev)
64 struct dasd_block *block = bdev->bd_disk->private_data;
66 if (!capable(CAP_SYS_ADMIN))
67 return -EACCES;
70 * Man this is sick. We don't do a real disable but only downgrade
71 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
72 * BIODASDDISABLE to disable accesses to the device via the block
73 * device layer but it still wants to do i/o on the device by
74 * using the BIODASDFMT ioctl. Therefore the correct state for the
75 * device is DASD_STATE_BASIC that allows to do basic i/o.
77 dasd_set_target_state(block->base, DASD_STATE_BASIC);
79 * Set i_size to zero, since read, write, etc. check against this
80 * value.
82 mutex_lock(&bdev->bd_mutex);
83 i_size_write(bdev->bd_inode, 0);
84 mutex_unlock(&bdev->bd_mutex);
85 return 0;
89 * Quiesce device.
91 static int dasd_ioctl_quiesce(struct dasd_block *block)
93 unsigned long flags;
94 struct dasd_device *base;
96 base = block->base;
97 if (!capable (CAP_SYS_ADMIN))
98 return -EACCES;
100 dev_info(&base->cdev->dev, "The DASD has been put in the quiesce "
101 "state\n");
102 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
103 base->stopped |= DASD_STOPPED_QUIESCE;
104 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
105 return 0;
110 * Resume device.
112 static int dasd_ioctl_resume(struct dasd_block *block)
114 unsigned long flags;
115 struct dasd_device *base;
117 base = block->base;
118 if (!capable (CAP_SYS_ADMIN))
119 return -EACCES;
121 dev_info(&base->cdev->dev, "I/O operations have been resumed "
122 "on the DASD\n");
123 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
124 base->stopped &= ~DASD_STOPPED_QUIESCE;
125 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
127 dasd_schedule_block_bh(block);
128 return 0;
132 * performs formatting of _device_ according to _fdata_
133 * Note: The discipline's format_function is assumed to deliver formatting
134 * commands to format a single unit of the device. In terms of the ECKD
135 * devices this means CCWs are generated to format a single track.
137 static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
139 struct dasd_ccw_req *cqr;
140 struct dasd_device *base;
141 int rc;
143 base = block->base;
144 if (base->discipline->format_device == NULL)
145 return -EPERM;
147 if (base->state != DASD_STATE_BASIC) {
148 dev_warn(&base->cdev->dev,
149 "The DASD cannot be formatted while it is enabled\n");
150 return -EBUSY;
153 DBF_DEV_EVENT(DBF_NOTICE, base,
154 "formatting units %u to %u (%u B blocks) flags %u",
155 fdata->start_unit,
156 fdata->stop_unit, fdata->blksize, fdata->intensity);
158 /* Since dasdfmt keeps the device open after it was disabled,
159 * there still exists an inode for this device.
160 * We must update i_blkbits, otherwise we might get errors when
161 * enabling the device later.
163 if (fdata->start_unit == 0) {
164 struct block_device *bdev = bdget_disk(block->gdp, 0);
165 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
166 bdput(bdev);
169 while (fdata->start_unit <= fdata->stop_unit) {
170 cqr = base->discipline->format_device(base, fdata);
171 if (IS_ERR(cqr))
172 return PTR_ERR(cqr);
173 rc = dasd_sleep_on_interruptible(cqr);
174 dasd_sfree_request(cqr, cqr->memdev);
175 if (rc) {
176 if (rc != -ERESTARTSYS)
177 dev_err(&base->cdev->dev,
178 "Formatting unit %d failed with "
179 "rc=%d\n", fdata->start_unit, rc);
180 return rc;
182 fdata->start_unit++;
184 return 0;
188 * Format device.
190 static int
191 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
193 struct dasd_block *block = bdev->bd_disk->private_data;
194 struct format_data_t fdata;
196 if (!capable(CAP_SYS_ADMIN))
197 return -EACCES;
198 if (!argp)
199 return -EINVAL;
201 if (block->base->features & DASD_FEATURE_READONLY)
202 return -EROFS;
203 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
204 return -EFAULT;
205 if (bdev != bdev->bd_contains) {
206 dev_warn(&block->base->cdev->dev,
207 "The specified DASD is a partition and cannot be "
208 "formatted\n");
209 return -EINVAL;
211 return dasd_format(block, &fdata);
214 #ifdef CONFIG_DASD_PROFILE
216 * Reset device profile information
218 static int dasd_ioctl_reset_profile(struct dasd_block *block)
220 memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
221 return 0;
225 * Return device profile information
227 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
229 if (dasd_profile_level == DASD_PROFILE_OFF)
230 return -EIO;
231 if (copy_to_user(argp, &block->profile,
232 sizeof(struct dasd_profile_info_t)))
233 return -EFAULT;
234 return 0;
236 #else
237 static int dasd_ioctl_reset_profile(struct dasd_block *block)
239 return -ENOSYS;
242 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
244 return -ENOSYS;
246 #endif
249 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
251 static int dasd_ioctl_information(struct dasd_block *block,
252 unsigned int cmd, void __user *argp)
254 struct dasd_information2_t *dasd_info;
255 unsigned long flags;
256 int rc;
257 struct dasd_device *base;
258 struct ccw_device *cdev;
259 struct ccw_dev_id dev_id;
261 base = block->base;
262 if (!base->discipline->fill_info)
263 return -EINVAL;
265 dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
266 if (dasd_info == NULL)
267 return -ENOMEM;
269 rc = base->discipline->fill_info(base, dasd_info);
270 if (rc) {
271 kfree(dasd_info);
272 return rc;
275 cdev = base->cdev;
276 ccw_device_get_id(cdev, &dev_id);
278 dasd_info->devno = dev_id.devno;
279 dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
280 dasd_info->cu_type = cdev->id.cu_type;
281 dasd_info->cu_model = cdev->id.cu_model;
282 dasd_info->dev_type = cdev->id.dev_type;
283 dasd_info->dev_model = cdev->id.dev_model;
284 dasd_info->status = base->state;
286 * The open_count is increased for every opener, that includes
287 * the blkdev_get in dasd_scan_partitions.
288 * This must be hidden from user-space.
290 dasd_info->open_count = atomic_read(&block->open_count);
291 if (!block->bdev)
292 dasd_info->open_count++;
295 * check if device is really formatted
296 * LDL / CDL was returned by 'fill_info'
298 if ((base->state < DASD_STATE_READY) ||
299 (dasd_check_blocksize(block->bp_block)))
300 dasd_info->format = DASD_FORMAT_NONE;
302 dasd_info->features |=
303 ((base->features & DASD_FEATURE_READONLY) != 0);
305 if (base->discipline)
306 memcpy(dasd_info->type, base->discipline->name, 4);
307 else
308 memcpy(dasd_info->type, "none", 4);
310 if (block->request_queue->request_fn) {
311 struct list_head *l;
312 #ifdef DASD_EXTENDED_PROFILING
314 struct list_head *l;
315 spin_lock_irqsave(&block->lock, flags);
316 list_for_each(l, &block->request_queue->queue_head)
317 dasd_info->req_queue_len++;
318 spin_unlock_irqrestore(&block->lock, flags);
320 #endif /* DASD_EXTENDED_PROFILING */
321 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
322 list_for_each(l, &base->ccw_queue)
323 dasd_info->chanq_len++;
324 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
325 flags);
328 rc = 0;
329 if (copy_to_user(argp, dasd_info,
330 ((cmd == (unsigned int) BIODASDINFO2) ?
331 sizeof(struct dasd_information2_t) :
332 sizeof(struct dasd_information_t))))
333 rc = -EFAULT;
334 kfree(dasd_info);
335 return rc;
339 * Set read only
341 static int
342 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
344 struct dasd_block *block = bdev->bd_disk->private_data;
345 int intval;
347 if (!capable(CAP_SYS_ADMIN))
348 return -EACCES;
349 if (bdev != bdev->bd_contains)
350 // ro setting is not allowed for partitions
351 return -EINVAL;
352 if (get_user(intval, (int __user *)argp))
353 return -EFAULT;
355 set_disk_ro(bdev->bd_disk, intval);
356 return dasd_set_feature(block->base->cdev, DASD_FEATURE_READONLY, intval);
359 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
360 unsigned long arg)
362 struct cmbdata __user *argp = (void __user *) arg;
363 size_t size = _IOC_SIZE(cmd);
364 struct cmbdata data;
365 int ret;
367 ret = cmf_readall(block->base->cdev, &data);
368 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
369 return -EFAULT;
370 return ret;
373 static int
374 dasd_do_ioctl(struct block_device *bdev, fmode_t mode,
375 unsigned int cmd, unsigned long arg)
377 struct dasd_block *block = bdev->bd_disk->private_data;
378 void __user *argp = (void __user *)arg;
380 if (!block)
381 return -ENODEV;
383 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
384 PRINT_DEBUG("empty data ptr");
385 return -EINVAL;
388 switch (cmd) {
389 case BIODASDDISABLE:
390 return dasd_ioctl_disable(bdev);
391 case BIODASDENABLE:
392 return dasd_ioctl_enable(bdev);
393 case BIODASDQUIESCE:
394 return dasd_ioctl_quiesce(block);
395 case BIODASDRESUME:
396 return dasd_ioctl_resume(block);
397 case BIODASDFMT:
398 return dasd_ioctl_format(bdev, argp);
399 case BIODASDINFO:
400 return dasd_ioctl_information(block, cmd, argp);
401 case BIODASDINFO2:
402 return dasd_ioctl_information(block, cmd, argp);
403 case BIODASDPRRD:
404 return dasd_ioctl_read_profile(block, argp);
405 case BIODASDPRRST:
406 return dasd_ioctl_reset_profile(block);
407 case BLKROSET:
408 return dasd_ioctl_set_ro(bdev, argp);
409 case DASDAPIVER:
410 return dasd_ioctl_api_version(argp);
411 case BIODASDCMFENABLE:
412 return enable_cmf(block->base->cdev);
413 case BIODASDCMFDISABLE:
414 return disable_cmf(block->base->cdev);
415 case BIODASDREADALLCMB:
416 return dasd_ioctl_readall_cmb(block, cmd, arg);
417 default:
418 /* if the discipline has an ioctl method try it. */
419 if (block->base->discipline->ioctl) {
420 int rval = block->base->discipline->ioctl(block, cmd, argp);
421 if (rval != -ENOIOCTLCMD)
422 return rval;
425 return -EINVAL;
429 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
430 unsigned int cmd, unsigned long arg)
432 int rc;
434 lock_kernel();
435 rc = dasd_do_ioctl(bdev, mode, cmd, arg);
436 unlock_kernel();
437 return rc;