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

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

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

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

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

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

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

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

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

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

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

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

306 307
	if (base->discipline)
		memcpy(dasd_info->type, base->discipline->name, 4);
L
Linus Torvalds 已提交
308 309
	else
		memcpy(dasd_info->type, "none", 4);
310

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

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

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

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
	if (bdev != bdev->bd_contains)
		// ro setting is not allowed for partitions
		return -EINVAL;
H
Heiko Carstens 已提交
353
	if (get_user(intval, (int __user *)argp))
L
Linus Torvalds 已提交
354
		return -EFAULT;
355

L
Linus Torvalds 已提交
356
	set_disk_ro(bdev->bd_disk, intval);
357
	return dasd_set_feature(block->base->cdev, DASD_FEATURE_READONLY, intval);
L
Linus Torvalds 已提交
358 359
}

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

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

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

381
	if (!block)
382 383 384 385 386 387
                return -ENODEV;

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

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

426 427
		return -EINVAL;
	}
L
Linus Torvalds 已提交
428
}
429 430 431 432 433 434 435 436 437 438 439

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;
}