dasd_ioctl.c 14.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * 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>
7
 * Copyright IBM Corp. 1999, 2001
L
Linus Torvalds 已提交
8 9 10
 *
 * i/o controls for the dasd driver.
 */
S
Stefan Haberland 已提交
11 12 13

#define KMSG_COMPONENT "dasd"

L
Linus Torvalds 已提交
14
#include <linux/interrupt.h>
15
#include <linux/compat.h>
L
Linus Torvalds 已提交
16 17 18
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/blkpg.h>
19
#include <linux/slab.h>
20
#include <asm/compat.h>
L
Linus Torvalds 已提交
21
#include <asm/ccwdev.h>
S
Sebastian Ott 已提交
22
#include <asm/schid.h>
23
#include <asm/cmb.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32
#include <asm/uaccess.h>

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

#include "dasd_int.h"


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

/*
 * Enable device.
 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
 */
static int
44
dasd_ioctl_enable(struct block_device *bdev)
L
Linus Torvalds 已提交
45
{
46
	struct dasd_device *base;
L
Linus Torvalds 已提交
47 48 49

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

51 52 53 54 55
	base = dasd_device_from_gendisk(bdev->bd_disk);
	if (!base)
		return -ENODEV;

	dasd_enable_device(base);
L
Linus Torvalds 已提交
56
	/* Formatting the dasd device can change the capacity. */
57
	mutex_lock(&bdev->bd_mutex);
58 59
	i_size_write(bdev->bd_inode,
		     (loff_t)get_capacity(base->block->gdp) << 9);
60
	mutex_unlock(&bdev->bd_mutex);
61
	dasd_put_device(base);
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69
	return 0;
}

/*
 * Disable device.
 * Used by dasdfmt. Disable I/O operations but allow ioctls.
 */
static int
70
dasd_ioctl_disable(struct block_device *bdev)
L
Linus Torvalds 已提交
71
{
72
	struct dasd_device *base;
L
Linus Torvalds 已提交
73 74 75

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

77 78 79
	base = dasd_device_from_gendisk(bdev->bd_disk);
	if (!base)
		return -ENODEV;
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87
	/*
	 * 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.
	 */
88
	dasd_set_target_state(base, DASD_STATE_BASIC);
L
Linus Torvalds 已提交
89 90 91 92
	/*
	 * Set i_size to zero, since read, write, etc. check against this
	 * value.
	 */
93
	mutex_lock(&bdev->bd_mutex);
L
Linus Torvalds 已提交
94
	i_size_write(bdev->bd_inode, 0);
95
	mutex_unlock(&bdev->bd_mutex);
96
	dasd_put_device(base);
L
Linus Torvalds 已提交
97 98 99 100 101 102
	return 0;
}

/*
 * Quiesce device.
 */
103
static int dasd_ioctl_quiesce(struct dasd_block *block)
L
Linus Torvalds 已提交
104 105
{
	unsigned long flags;
106
	struct dasd_device *base;
107

108
	base = block->base;
L
Linus Torvalds 已提交
109 110
	if (!capable (CAP_SYS_ADMIN))
		return -EACCES;
111

112 113
	pr_info("%s: The DASD has been put in the quiesce "
		"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);
L
Linus Torvalds 已提交
117 118 119 120 121
	return 0;
}


/*
S
Stefan Haberland 已提交
122
 * Resume device.
L
Linus Torvalds 已提交
123
 */
124
static int dasd_ioctl_resume(struct dasd_block *block)
L
Linus Torvalds 已提交
125 126
{
	unsigned long flags;
127
	struct dasd_device *base;
128

129
	base = block->base;
130
	if (!capable (CAP_SYS_ADMIN))
L
Linus Torvalds 已提交
131 132
		return -EACCES;

133 134
	pr_info("%s: I/O operations have been resumed "
		"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);
L
Linus Torvalds 已提交
138

139
	dasd_schedule_block_bh(block);
L
Linus Torvalds 已提交
140 141 142
	return 0;
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/*
 * Abort all failfast I/O on a device.
 */
static int dasd_ioctl_abortio(struct dasd_block *block)
{
	unsigned long flags;
	struct dasd_device *base;
	struct dasd_ccw_req *cqr, *n;

	base = block->base;
	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags))
		return 0;
	DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set");

	spin_lock_irqsave(&block->request_queue_lock, flags);
	spin_lock(&block->queue_lock);
	list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
		if (test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
		    cqr->callback_data &&
		    cqr->callback_data != DASD_SLEEPON_START_TAG &&
		    cqr->callback_data != DASD_SLEEPON_END_TAG) {
			spin_unlock(&block->queue_lock);
			blk_abort_request(cqr->callback_data);
			spin_lock(&block->queue_lock);
		}
	}
	spin_unlock(&block->queue_lock);
	spin_unlock_irqrestore(&block->request_queue_lock, flags);

	dasd_schedule_block_bh(block);
	return 0;
}

/*
 * Allow I/O on a device
 */
static int dasd_ioctl_allowio(struct dasd_block *block)
{
	struct dasd_device *base;

	base = block->base;
	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags))
		DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset");

	return 0;
}

L
Linus Torvalds 已提交
196 197 198
/*
 * performs formatting of _device_ according to _fdata_
 * Note: The discipline's format_function is assumed to deliver formatting
199 200
 * commands to format multiple units of the device. In terms of the ECKD
 * devices this means CCWs are generated to format multiple tracks.
L
Linus Torvalds 已提交
201
 */
202 203
static int
dasd_format(struct dasd_block *block, struct format_data_t *fdata)
L
Linus Torvalds 已提交
204
{
205
	struct dasd_device *base;
S
Stefan Haberland 已提交
206
	int enable_pav = 1;
207 208
	int rc, retries;
	int start, stop;
L
Linus Torvalds 已提交
209

210 211
	base = block->base;
	if (base->discipline->format_device == NULL)
L
Linus Torvalds 已提交
212 213
		return -EPERM;

214
	if (base->state != DASD_STATE_BASIC) {
215 216
		pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
			dev_name(&base->cdev->dev));
L
Linus Torvalds 已提交
217 218 219
		return -EBUSY;
	}

220
	DBF_DEV_EVENT(DBF_NOTICE, base,
221
		      "formatting units %u to %u (%u B blocks) flags %u",
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230
		      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) {
231
		struct block_device *bdev = bdget_disk(block->gdp, 0);
L
Linus Torvalds 已提交
232 233 234 235
		bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
		bdput(bdev);
	}

236 237 238 239 240
	retries = 255;
	/* backup start- and endtrack for retries */
	start = fdata->start_unit;
	stop = fdata->stop_unit;
	do {
S
Stefan Haberland 已提交
241
		rc = base->discipline->format_device(base, fdata, enable_pav);
242 243 244 245
		if (rc) {
			if (rc == -EAGAIN) {
				retries--;
				/* disable PAV in case of errors */
S
Stefan Haberland 已提交
246
				enable_pav = 0;
247 248 249 250 251 252 253 254 255 256 257 258 259
				fdata->start_unit = start;
				fdata->stop_unit = stop;
			} else
				return rc;
		} else
			/* success */
			break;
	} while (retries);

	if (!retries)
		return -EIO;
	else
		return 0;
L
Linus Torvalds 已提交
260 261 262 263 264 265
}

/*
 * Format device.
 */
static int
266
dasd_ioctl_format(struct block_device *bdev, void __user *argp)
L
Linus Torvalds 已提交
267
{
268
	struct dasd_device *base;
L
Linus Torvalds 已提交
269
	struct format_data_t fdata;
270
	int rc;
L
Linus Torvalds 已提交
271 272 273

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
274
	if (!argp)
L
Linus Torvalds 已提交
275
		return -EINVAL;
276 277 278 279 280 281
	base = dasd_device_from_gendisk(bdev->bd_disk);
	if (!base)
		return -ENODEV;
	if (base->features & DASD_FEATURE_READONLY ||
	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
		dasd_put_device(base);
L
Linus Torvalds 已提交
282
		return -EROFS;
283 284 285
	}
	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
		dasd_put_device(base);
L
Linus Torvalds 已提交
286
		return -EFAULT;
287
	}
L
Linus Torvalds 已提交
288
	if (bdev != bdev->bd_contains) {
289 290
		pr_warning("%s: The specified DASD is a partition and cannot "
			   "be formatted\n",
291 292
			   dev_name(&base->cdev->dev));
		dasd_put_device(base);
L
Linus Torvalds 已提交
293 294
		return -EINVAL;
	}
295 296 297
	rc = dasd_format(base->block, &fdata);
	dasd_put_device(base);
	return rc;
L
Linus Torvalds 已提交
298 299 300 301 302 303
}

#ifdef CONFIG_DASD_PROFILE
/*
 * Reset device profile information
 */
304
static int dasd_ioctl_reset_profile(struct dasd_block *block)
L
Linus Torvalds 已提交
305
{
306
	dasd_profile_reset(&block->profile);
L
Linus Torvalds 已提交
307 308 309 310 311 312
	return 0;
}

/*
 * Return device profile information
 */
313
static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
L
Linus Torvalds 已提交
314
{
315
	struct dasd_profile_info_t *data;
316
	int rc = 0;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

	data = kmalloc(sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	spin_lock_bh(&block->profile.lock);
	if (block->profile.data) {
		data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
		data->dasd_io_sects = block->profile.data->dasd_io_sects;
		memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
		       sizeof(data->dasd_io_secs));
		memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
		       sizeof(data->dasd_io_times));
		memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
		       sizeof(data->dasd_io_timps));
		memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
		       sizeof(data->dasd_io_time1));
		memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
		       sizeof(data->dasd_io_time2));
		memcpy(data->dasd_io_time2ps,
		       block->profile.data->dasd_io_time2ps,
		       sizeof(data->dasd_io_time2ps));
		memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
		       sizeof(data->dasd_io_time3));
		memcpy(data->dasd_io_nr_req,
		       block->profile.data->dasd_io_nr_req,
		       sizeof(data->dasd_io_nr_req));
		spin_unlock_bh(&block->profile.lock);
	} else {
		spin_unlock_bh(&block->profile.lock);
347 348
		rc = -EIO;
		goto out;
349 350
	}
	if (copy_to_user(argp, data, sizeof(*data)))
351 352 353 354
		rc = -EFAULT;
out:
	kfree(data);
	return rc;
L
Linus Torvalds 已提交
355 356
}
#else
357
static int dasd_ioctl_reset_profile(struct dasd_block *block)
L
Linus Torvalds 已提交
358
{
359
	return -ENOTTY;
L
Linus Torvalds 已提交
360 361
}

362
static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
L
Linus Torvalds 已提交
363
{
364
	return -ENOTTY;
L
Linus Torvalds 已提交
365 366 367 368 369 370
}
#endif

/*
 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
 */
371 372
static int dasd_ioctl_information(struct dasd_block *block,
				  unsigned int cmd, void __user *argp)
L
Linus Torvalds 已提交
373 374
{
	struct dasd_information2_t *dasd_info;
S
Sebastian Ott 已提交
375 376
	struct subchannel_id sch_id;
	struct ccw_dev_id dev_id;
377
	struct dasd_device *base;
L
Linus Torvalds 已提交
378
	struct ccw_device *cdev;
S
Sebastian Ott 已提交
379 380
	unsigned long flags;
	int rc;
L
Linus Torvalds 已提交
381

382
	base = block->base;
383
	if (!base->discipline || !base->discipline->fill_info)
L
Linus Torvalds 已提交
384 385
		return -EINVAL;

386
	dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
L
Linus Torvalds 已提交
387 388 389
	if (dasd_info == NULL)
		return -ENOMEM;

390
	rc = base->discipline->fill_info(base, dasd_info);
L
Linus Torvalds 已提交
391 392 393 394 395
	if (rc) {
		kfree(dasd_info);
		return rc;
	}

396
	cdev = base->cdev;
397
	ccw_device_get_id(cdev, &dev_id);
S
Sebastian Ott 已提交
398
	ccw_device_get_schid(cdev, &sch_id);
L
Linus Torvalds 已提交
399

400
	dasd_info->devno = dev_id.devno;
S
Sebastian Ott 已提交
401
	dasd_info->schid = sch_id.sch_no;
L
Linus Torvalds 已提交
402 403 404 405
	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;
406
	dasd_info->status = base->state;
H
Horst Hummel 已提交
407 408 409 410 411
	/*
	 * The open_count is increased for every opener, that includes
	 * the blkdev_get in dasd_scan_partitions.
	 * This must be hidden from user-space.
	 */
412 413
	dasd_info->open_count = atomic_read(&block->open_count);
	if (!block->bdev)
H
Horst Hummel 已提交
414
		dasd_info->open_count++;
415

L
Linus Torvalds 已提交
416 417 418 419
	/*
	 * check if device is really formatted
	 * LDL / CDL was returned by 'fill_info'
	 */
420 421
	if ((base->state < DASD_STATE_READY) ||
	    (dasd_check_blocksize(block->bp_block)))
L
Linus Torvalds 已提交
422
		dasd_info->format = DASD_FORMAT_NONE;
423

424
	dasd_info->features |=
425
		((base->features & DASD_FEATURE_READONLY) != 0);
L
Linus Torvalds 已提交
426

427
	memcpy(dasd_info->type, base->discipline->name, 4);
428

429
	if (block->request_queue->request_fn) {
L
Linus Torvalds 已提交
430 431 432 433
		struct list_head *l;
#ifdef DASD_EXTENDED_PROFILING
		{
			struct list_head *l;
434 435
			spin_lock_irqsave(&block->lock, flags);
			list_for_each(l, &block->request_queue->queue_head)
L
Linus Torvalds 已提交
436
				dasd_info->req_queue_len++;
437
			spin_unlock_irqrestore(&block->lock, flags);
L
Linus Torvalds 已提交
438 439
		}
#endif				/* DASD_EXTENDED_PROFILING */
440 441
		spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
		list_for_each(l, &base->ccw_queue)
L
Linus Torvalds 已提交
442
			dasd_info->chanq_len++;
443
		spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
L
Linus Torvalds 已提交
444 445 446 447
				       flags);
	}

	rc = 0;
448 449
	if (copy_to_user(argp, dasd_info,
			 ((cmd == (unsigned int) BIODASDINFO2) ?
450 451
			  sizeof(struct dasd_information2_t) :
			  sizeof(struct dasd_information_t))))
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460
		rc = -EFAULT;
	kfree(dasd_info);
	return rc;
}

/*
 * Set read only
 */
static int
461
dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
L
Linus Torvalds 已提交
462
{
463 464
	struct dasd_device *base;
	int intval, rc;
L
Linus Torvalds 已提交
465 466 467 468 469 470

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
	if (bdev != bdev->bd_contains)
		// ro setting is not allowed for partitions
		return -EINVAL;
H
Heiko Carstens 已提交
471
	if (get_user(intval, (int __user *)argp))
L
Linus Torvalds 已提交
472
		return -EFAULT;
473 474 475 476 477
	base = dasd_device_from_gendisk(bdev->bd_disk);
	if (!base)
		return -ENODEV;
	if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
		dasd_put_device(base);
478
		return -EROFS;
479
	}
L
Linus Torvalds 已提交
480
	set_disk_ro(bdev->bd_disk, intval);
481 482 483
	rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
	dasd_put_device(base);
	return rc;
L
Linus Torvalds 已提交
484 485
}

486
static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
487
				  struct cmbdata __user *argp)
488 489 490 491 492
{
	size_t size = _IOC_SIZE(cmd);
	struct cmbdata data;
	int ret;

493
	ret = cmf_readall(block->base->cdev, &data);
494 495 496 497 498
	if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
		return -EFAULT;
	return ret;
}

499 500
int dasd_ioctl(struct block_device *bdev, fmode_t mode,
	       unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
501
{
502 503
	struct dasd_block *block;
	struct dasd_device *base;
504
	void __user *argp;
505
	int rc;
506 507 508 509 510

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

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

517 518 519 520 521
	base = dasd_device_from_gendisk(bdev->bd_disk);
	if (!base)
		return -ENODEV;
	block = base->block;
	rc = 0;
522 523
	switch (cmd) {
	case BIODASDDISABLE:
524 525
		rc = dasd_ioctl_disable(bdev);
		break;
526
	case BIODASDENABLE:
527 528
		rc = dasd_ioctl_enable(bdev);
		break;
529
	case BIODASDQUIESCE:
530 531
		rc = dasd_ioctl_quiesce(block);
		break;
532
	case BIODASDRESUME:
533 534
		rc = dasd_ioctl_resume(block);
		break;
535 536 537 538 539 540
	case BIODASDABORTIO:
		rc = dasd_ioctl_abortio(block);
		break;
	case BIODASDALLOWIO:
		rc = dasd_ioctl_allowio(block);
		break;
541
	case BIODASDFMT:
542 543
		rc = dasd_ioctl_format(bdev, argp);
		break;
544
	case BIODASDINFO:
545 546
		rc = dasd_ioctl_information(block, cmd, argp);
		break;
547
	case BIODASDINFO2:
548 549
		rc = dasd_ioctl_information(block, cmd, argp);
		break;
550
	case BIODASDPRRD:
551 552
		rc = dasd_ioctl_read_profile(block, argp);
		break;
553
	case BIODASDPRRST:
554 555
		rc = dasd_ioctl_reset_profile(block);
		break;
556
	case BLKROSET:
557 558
		rc = dasd_ioctl_set_ro(bdev, argp);
		break;
559
	case DASDAPIVER:
560 561
		rc = dasd_ioctl_api_version(argp);
		break;
562
	case BIODASDCMFENABLE:
563 564
		rc = enable_cmf(base->cdev);
		break;
565
	case BIODASDCMFDISABLE:
566 567
		rc = disable_cmf(base->cdev);
		break;
568
	case BIODASDREADALLCMB:
569 570
		rc = dasd_ioctl_readall_cmb(block, cmd, argp);
		break;
571
	default:
572
		/* if the discipline has an ioctl method try it. */
573 574
		rc = -ENOTTY;
		if (base->discipline->ioctl)
575
			rc = base->discipline->ioctl(block, cmd, argp);
576
	}
577 578
	dasd_put_device(base);
	return rc;
L
Linus Torvalds 已提交
579
}