dasd_ioctl.c 11.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * File...........: linux/drivers/s390/block/dasd_ioctl.c
 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
 *		    Horst Hummel <Horst.Hummel@de.ibm.com>
 *		    Carsten Otte <Cotte@de.ibm.com>
 *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
 * Bugreports.to..: <Linux390@de.ibm.com>
 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
 *
 * i/o controls for the dasd driver.
 */
S
Stefan Haberland 已提交
12 13 14

#define KMSG_COMPONENT "dasd"

L
Linus Torvalds 已提交
15 16 17 18
#include <linux/interrupt.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/blkpg.h>
A
Alexey Dobriyan 已提交
19
#include <linux/smp_lock.h>
20
#include <asm/compat.h>
L
Linus Torvalds 已提交
21
#include <asm/ccwdev.h>
22
#include <asm/cmb.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31
#include <asm/uaccess.h>

/* This is ugly... */
#define PRINTK_HEADER "dasd_ioctl:"

#include "dasd_int.h"


static int
32
dasd_ioctl_api_version(void __user *argp)
L
Linus Torvalds 已提交
33 34
{
	int ver = DASD_API_VERSION;
35
	return put_user(ver, (int __user *)argp);
L
Linus Torvalds 已提交
36 37 38 39 40 41 42
}

/*
 * Enable device.
 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
 */
static int
43
dasd_ioctl_enable(struct block_device *bdev)
L
Linus Torvalds 已提交
44
{
45
	struct dasd_block *block = bdev->bd_disk->private_data;
L
Linus Torvalds 已提交
46 47 48

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
49

50
	dasd_enable_device(block->base);
L
Linus Torvalds 已提交
51
	/* Formatting the dasd device can change the capacity. */
52
	mutex_lock(&bdev->bd_mutex);
53
	i_size_write(bdev->bd_inode, (loff_t)get_capacity(block->gdp) << 9);
54
	mutex_unlock(&bdev->bd_mutex);
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62
	return 0;
}

/*
 * Disable device.
 * Used by dasdfmt. Disable I/O operations but allow ioctls.
 */
static int
63
dasd_ioctl_disable(struct block_device *bdev)
L
Linus Torvalds 已提交
64
{
65
	struct dasd_block *block = bdev->bd_disk->private_data;
L
Linus Torvalds 已提交
66 67 68

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
69

L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77
	/*
	 * Man this is sick. We don't do a real disable but only downgrade
	 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
	 * BIODASDDISABLE to disable accesses to the device via the block
	 * device layer but it still wants to do i/o on the device by
	 * using the BIODASDFMT ioctl. Therefore the correct state for the
	 * device is DASD_STATE_BASIC that allows to do basic i/o.
	 */
78
	dasd_set_target_state(block->base, DASD_STATE_BASIC);
L
Linus Torvalds 已提交
79 80 81 82
	/*
	 * Set i_size to zero, since read, write, etc. check against this
	 * value.
	 */
83
	mutex_lock(&bdev->bd_mutex);
L
Linus Torvalds 已提交
84
	i_size_write(bdev->bd_inode, 0);
85
	mutex_unlock(&bdev->bd_mutex);
L
Linus Torvalds 已提交
86 87 88 89 90 91
	return 0;
}

/*
 * Quiesce device.
 */
92
static int dasd_ioctl_quiesce(struct dasd_block *block)
L
Linus Torvalds 已提交
93 94
{
	unsigned long flags;
95
	struct dasd_device *base;
96

97
	base = block->base;
L
Linus Torvalds 已提交
98 99
	if (!capable (CAP_SYS_ADMIN))
		return -EACCES;
100

101 102
	pr_info("%s: The DASD has been put in the quiesce "
		"state\n", dev_name(&base->cdev->dev));
103
	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
104
	dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
105
	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
L
Linus Torvalds 已提交
106 107 108 109 110
	return 0;
}


/*
S
Stefan Haberland 已提交
111
 * Resume device.
L
Linus Torvalds 已提交
112
 */
113
static int dasd_ioctl_resume(struct dasd_block *block)
L
Linus Torvalds 已提交
114 115
{
	unsigned long flags;
116
	struct dasd_device *base;
117

118
	base = block->base;
119
	if (!capable (CAP_SYS_ADMIN))
L
Linus Torvalds 已提交
120 121
		return -EACCES;

122 123
	pr_info("%s: I/O operations have been resumed "
		"on the DASD\n", dev_name(&base->cdev->dev));
124
	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
125
	dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
126
	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
L
Linus Torvalds 已提交
127

128
	dasd_schedule_block_bh(block);
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137
	return 0;
}

/*
 * performs formatting of _device_ according to _fdata_
 * Note: The discipline's format_function is assumed to deliver formatting
 * commands to format a single unit of the device. In terms of the ECKD
 * devices this means CCWs are generated to format a single track.
 */
138
static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
L
Linus Torvalds 已提交
139 140
{
	struct dasd_ccw_req *cqr;
141
	struct dasd_device *base;
L
Linus Torvalds 已提交
142 143
	int rc;

144 145
	base = block->base;
	if (base->discipline->format_device == NULL)
L
Linus Torvalds 已提交
146 147
		return -EPERM;

148
	if (base->state != DASD_STATE_BASIC) {
149 150
		pr_warning("%s: The DASD cannot be formatted while it is "
			   "enabled\n",  dev_name(&base->cdev->dev));
L
Linus Torvalds 已提交
151 152 153
		return -EBUSY;
	}

154
	DBF_DEV_EVENT(DBF_NOTICE, base,
155
		      "formatting units %u to %u (%u B blocks) flags %u",
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164
		      fdata->start_unit,
		      fdata->stop_unit, fdata->blksize, fdata->intensity);

	/* Since dasdfmt keeps the device open after it was disabled,
	 * there still exists an inode for this device.
	 * We must update i_blkbits, otherwise we might get errors when
	 * enabling the device later.
	 */
	if (fdata->start_unit == 0) {
165
		struct block_device *bdev = bdget_disk(block->gdp, 0);
L
Linus Torvalds 已提交
166 167 168 169 170
		bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
		bdput(bdev);
	}

	while (fdata->start_unit <= fdata->stop_unit) {
171
		cqr = base->discipline->format_device(base, fdata);
L
Linus Torvalds 已提交
172 173 174
		if (IS_ERR(cqr))
			return PTR_ERR(cqr);
		rc = dasd_sleep_on_interruptible(cqr);
175
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
176 177
		if (rc) {
			if (rc != -ERESTARTSYS)
178 179 180
				pr_err("%s: Formatting unit %d failed with "
				       "rc=%d\n", dev_name(&base->cdev->dev),
				       fdata->start_unit, rc);
L
Linus Torvalds 已提交
181 182 183 184 185 186 187 188 189 190 191
			return rc;
		}
		fdata->start_unit++;
	}
	return 0;
}

/*
 * Format device.
 */
static int
192
dasd_ioctl_format(struct block_device *bdev, void __user *argp)
L
Linus Torvalds 已提交
193
{
194
	struct dasd_block *block = bdev->bd_disk->private_data;
L
Linus Torvalds 已提交
195 196 197 198
	struct format_data_t fdata;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
199
	if (!argp)
L
Linus Torvalds 已提交
200
		return -EINVAL;
201

202 203
	if (block->base->features & DASD_FEATURE_READONLY ||
	    test_bit(DASD_FLAG_DEVICE_RO, &block->base->flags))
L
Linus Torvalds 已提交
204
		return -EROFS;
205
	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
L
Linus Torvalds 已提交
206 207
		return -EFAULT;
	if (bdev != bdev->bd_contains) {
208 209 210
		pr_warning("%s: The specified DASD is a partition and cannot "
			   "be formatted\n",
			   dev_name(&block->base->cdev->dev));
L
Linus Torvalds 已提交
211 212
		return -EINVAL;
	}
213
	return dasd_format(block, &fdata);
L
Linus Torvalds 已提交
214 215 216 217 218 219
}

#ifdef CONFIG_DASD_PROFILE
/*
 * Reset device profile information
 */
220
static int dasd_ioctl_reset_profile(struct dasd_block *block)
L
Linus Torvalds 已提交
221
{
222
	memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
L
Linus Torvalds 已提交
223 224 225 226 227 228
	return 0;
}

/*
 * Return device profile information
 */
229
static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
L
Linus Torvalds 已提交
230
{
231 232
	if (dasd_profile_level == DASD_PROFILE_OFF)
		return -EIO;
233 234
	if (copy_to_user(argp, &block->profile,
			 sizeof(struct dasd_profile_info_t)))
L
Linus Torvalds 已提交
235 236 237 238
		return -EFAULT;
	return 0;
}
#else
239
static int dasd_ioctl_reset_profile(struct dasd_block *block)
L
Linus Torvalds 已提交
240 241 242 243
{
	return -ENOSYS;
}

244
static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252
{
	return -ENOSYS;
}
#endif

/*
 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
 */
253 254
static int dasd_ioctl_information(struct dasd_block *block,
				  unsigned int cmd, void __user *argp)
L
Linus Torvalds 已提交
255 256 257
{
	struct dasd_information2_t *dasd_info;
	unsigned long flags;
258
	int rc;
259
	struct dasd_device *base;
L
Linus Torvalds 已提交
260
	struct ccw_device *cdev;
261
	struct ccw_dev_id dev_id;
L
Linus Torvalds 已提交
262

263
	base = block->base;
264
	if (!base->discipline || !base->discipline->fill_info)
L
Linus Torvalds 已提交
265 266
		return -EINVAL;

267
	dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
L
Linus Torvalds 已提交
268 269 270
	if (dasd_info == NULL)
		return -ENOMEM;

271
	rc = base->discipline->fill_info(base, dasd_info);
L
Linus Torvalds 已提交
272 273 274 275 276
	if (rc) {
		kfree(dasd_info);
		return rc;
	}

277
	cdev = base->cdev;
278
	ccw_device_get_id(cdev, &dev_id);
L
Linus Torvalds 已提交
279

280
	dasd_info->devno = dev_id.devno;
281
	dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
L
Linus Torvalds 已提交
282 283 284 285
	dasd_info->cu_type = cdev->id.cu_type;
	dasd_info->cu_model = cdev->id.cu_model;
	dasd_info->dev_type = cdev->id.dev_type;
	dasd_info->dev_model = cdev->id.dev_model;
286
	dasd_info->status = base->state;
H
Horst Hummel 已提交
287 288 289 290 291
	/*
	 * The open_count is increased for every opener, that includes
	 * the blkdev_get in dasd_scan_partitions.
	 * This must be hidden from user-space.
	 */
292 293
	dasd_info->open_count = atomic_read(&block->open_count);
	if (!block->bdev)
H
Horst Hummel 已提交
294
		dasd_info->open_count++;
295

L
Linus Torvalds 已提交
296 297 298 299
	/*
	 * check if device is really formatted
	 * LDL / CDL was returned by 'fill_info'
	 */
300 301
	if ((base->state < DASD_STATE_READY) ||
	    (dasd_check_blocksize(block->bp_block)))
L
Linus Torvalds 已提交
302
		dasd_info->format = DASD_FORMAT_NONE;
303

304
	dasd_info->features |=
305
		((base->features & DASD_FEATURE_READONLY) != 0);
L
Linus Torvalds 已提交
306

307
	memcpy(dasd_info->type, base->discipline->name, 4);
308

309
	if (block->request_queue->request_fn) {
L
Linus Torvalds 已提交
310 311 312 313
		struct list_head *l;
#ifdef DASD_EXTENDED_PROFILING
		{
			struct list_head *l;
314 315
			spin_lock_irqsave(&block->lock, flags);
			list_for_each(l, &block->request_queue->queue_head)
L
Linus Torvalds 已提交
316
				dasd_info->req_queue_len++;
317
			spin_unlock_irqrestore(&block->lock, flags);
L
Linus Torvalds 已提交
318 319
		}
#endif				/* DASD_EXTENDED_PROFILING */
320 321
		spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
		list_for_each(l, &base->ccw_queue)
L
Linus Torvalds 已提交
322
			dasd_info->chanq_len++;
323
		spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
L
Linus Torvalds 已提交
324 325 326 327
				       flags);
	}

	rc = 0;
328 329
	if (copy_to_user(argp, dasd_info,
			 ((cmd == (unsigned int) BIODASDINFO2) ?
330 331
			  sizeof(struct dasd_information2_t) :
			  sizeof(struct dasd_information_t))))
L
Linus Torvalds 已提交
332 333 334 335 336 337 338 339 340
		rc = -EFAULT;
	kfree(dasd_info);
	return rc;
}

/*
 * Set read only
 */
static int
341
dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
L
Linus Torvalds 已提交
342
{
343
	struct dasd_block *block =  bdev->bd_disk->private_data;
344
	int intval;
L
Linus Torvalds 已提交
345 346 347 348 349 350

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
	if (bdev != bdev->bd_contains)
		// ro setting is not allowed for partitions
		return -EINVAL;
H
Heiko Carstens 已提交
351
	if (get_user(intval, (int __user *)argp))
L
Linus Torvalds 已提交
352
		return -EFAULT;
353 354
	if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &block->base->flags))
		return -EROFS;
L
Linus Torvalds 已提交
355
	set_disk_ro(bdev->bd_disk, intval);
356
	return dasd_set_feature(block->base->cdev, DASD_FEATURE_READONLY, intval);
L
Linus Torvalds 已提交
357 358
}

359
static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
360
				  struct cmbdata __user *argp)
361 362 363 364 365
{
	size_t size = _IOC_SIZE(cmd);
	struct cmbdata data;
	int ret;

366
	ret = cmf_readall(block->base->cdev, &data);
367 368 369 370 371
	if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
		return -EFAULT;
	return ret;
}

372 373 374
static int
dasd_do_ioctl(struct block_device *bdev, fmode_t mode,
	      unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
375
{
376
	struct dasd_block *block = bdev->bd_disk->private_data;
377 378 379 380 381 382
	void __user *argp;

	if (is_compat_task())
		argp = compat_ptr(arg);
	else
		argp = (void __user *)arg;
L
Linus Torvalds 已提交
383

384
	if (!block)
385 386 387 388 389 390
                return -ENODEV;

	if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
		PRINT_DEBUG("empty data ptr");
		return -EINVAL;
	}
L
Linus Torvalds 已提交
391

392 393 394 395 396 397
	switch (cmd) {
	case BIODASDDISABLE:
		return dasd_ioctl_disable(bdev);
	case BIODASDENABLE:
		return dasd_ioctl_enable(bdev);
	case BIODASDQUIESCE:
398
		return dasd_ioctl_quiesce(block);
399
	case BIODASDRESUME:
400
		return dasd_ioctl_resume(block);
401 402 403
	case BIODASDFMT:
		return dasd_ioctl_format(bdev, argp);
	case BIODASDINFO:
404
		return dasd_ioctl_information(block, cmd, argp);
405
	case BIODASDINFO2:
406
		return dasd_ioctl_information(block, cmd, argp);
407
	case BIODASDPRRD:
408
		return dasd_ioctl_read_profile(block, argp);
409
	case BIODASDPRRST:
410
		return dasd_ioctl_reset_profile(block);
411 412 413 414
	case BLKROSET:
		return dasd_ioctl_set_ro(bdev, argp);
	case DASDAPIVER:
		return dasd_ioctl_api_version(argp);
415
	case BIODASDCMFENABLE:
416
		return enable_cmf(block->base->cdev);
417
	case BIODASDCMFDISABLE:
418
		return disable_cmf(block->base->cdev);
419
	case BIODASDREADALLCMB:
420
		return dasd_ioctl_readall_cmb(block, cmd, argp);
421
	default:
422
		/* if the discipline has an ioctl method try it. */
423 424
		if (block->base->discipline->ioctl) {
			int rval = block->base->discipline->ioctl(block, cmd, argp);
425 426 427 428
			if (rval != -ENOIOCTLCMD)
				return rval;
		}

429 430
		return -EINVAL;
	}
L
Linus Torvalds 已提交
431
}
432 433 434 435 436 437 438 439 440 441 442

int dasd_ioctl(struct block_device *bdev, fmode_t mode,
	       unsigned int cmd, unsigned long arg)
{
	int rc;

	lock_kernel();
	rc = dasd_do_ioctl(bdev, mode, cmd, arg);
	unlock_kernel();
	return rc;
}