dasd_ioctl.c 10.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */
#include <linux/interrupt.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/blkpg.h>

#include <asm/ccwdev.h>
18
#include <asm/cmb.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27
#include <asm/uaccess.h>

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

#include "dasd_int.h"


static int
28
dasd_ioctl_api_version(void __user *argp)
L
Linus Torvalds 已提交
29 30
{
	int ver = DASD_API_VERSION;
31
	return put_user(ver, (int __user *)argp);
L
Linus Torvalds 已提交
32 33 34 35 36 37 38
}

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

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

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

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

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

L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73
	/*
	 * 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.
	 */
74
	dasd_set_target_state(block->base, DASD_STATE_BASIC);
L
Linus Torvalds 已提交
75 76 77 78
	/*
	 * Set i_size to zero, since read, write, etc. check against this
	 * value.
	 */
79
	mutex_lock(&bdev->bd_mutex);
L
Linus Torvalds 已提交
80
	i_size_write(bdev->bd_inode, 0);
81
	mutex_unlock(&bdev->bd_mutex);
L
Linus Torvalds 已提交
82 83 84 85 86 87
	return 0;
}

/*
 * Quiesce device.
 */
88
static int dasd_ioctl_quiesce(struct dasd_block *block)
L
Linus Torvalds 已提交
89 90
{
	unsigned long flags;
91
	struct dasd_device *base;
92

93
	base = block->base;
L
Linus Torvalds 已提交
94 95
	if (!capable (CAP_SYS_ADMIN))
		return -EACCES;
96

97 98 99 100
	DEV_MESSAGE(KERN_DEBUG, base, "%s", "Quiesce IO on device");
	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
	base->stopped |= DASD_STOPPED_QUIESCE;
	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
L
Linus Torvalds 已提交
101 102 103 104 105 106 107
	return 0;
}


/*
 * Quiesce device.
 */
108
static int dasd_ioctl_resume(struct dasd_block *block)
L
Linus Torvalds 已提交
109 110
{
	unsigned long flags;
111
	struct dasd_device *base;
112

113
	base = block->base;
114
	if (!capable (CAP_SYS_ADMIN))
L
Linus Torvalds 已提交
115 116
		return -EACCES;

117 118 119 120
	DEV_MESSAGE(KERN_DEBUG, base, "%s", "resume IO on device");
	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
	base->stopped &= ~DASD_STOPPED_QUIESCE;
	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
L
Linus Torvalds 已提交
121

122
	dasd_schedule_block_bh(block);
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130 131
	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.
 */
132
static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
L
Linus Torvalds 已提交
133 134
{
	struct dasd_ccw_req *cqr;
135
	struct dasd_device *base;
L
Linus Torvalds 已提交
136 137
	int rc;

138 139
	base = block->base;
	if (base->discipline->format_device == NULL)
L
Linus Torvalds 已提交
140 141
		return -EPERM;

142 143
	if (base->state != DASD_STATE_BASIC) {
		DEV_MESSAGE(KERN_WARNING, base, "%s",
L
Linus Torvalds 已提交
144 145 146 147
			    "dasd_format: device is not disabled! ");
		return -EBUSY;
	}

148
	DBF_DEV_EVENT(DBF_NOTICE, base,
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156 157 158
		      "formatting units %d to %d (%d B blocks) flags %d",
		      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) {
159
		struct block_device *bdev = bdget_disk(block->gdp, 0);
L
Linus Torvalds 已提交
160 161 162 163 164
		bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
		bdput(bdev);
	}

	while (fdata->start_unit <= fdata->stop_unit) {
165
		cqr = base->discipline->format_device(base, fdata);
L
Linus Torvalds 已提交
166 167 168
		if (IS_ERR(cqr))
			return PTR_ERR(cqr);
		rc = dasd_sleep_on_interruptible(cqr);
169
		dasd_sfree_request(cqr, cqr->memdev);
L
Linus Torvalds 已提交
170 171
		if (rc) {
			if (rc != -ERESTARTSYS)
172
				DEV_MESSAGE(KERN_ERR, base,
L
Linus Torvalds 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186
					    " Formatting of unit %d failed "
					    "with rc = %d",
					    fdata->start_unit, rc);
			return rc;
		}
		fdata->start_unit++;
	}
	return 0;
}

/*
 * Format device.
 */
static int
187
dasd_ioctl_format(struct block_device *bdev, void __user *argp)
L
Linus Torvalds 已提交
188
{
189
	struct dasd_block *block = bdev->bd_disk->private_data;
L
Linus Torvalds 已提交
190 191 192 193
	struct format_data_t fdata;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
194
	if (!argp)
L
Linus Torvalds 已提交
195
		return -EINVAL;
196

197
	if (block->base->features & DASD_FEATURE_READONLY)
L
Linus Torvalds 已提交
198
		return -EROFS;
199
	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
L
Linus Torvalds 已提交
200 201
		return -EFAULT;
	if (bdev != bdev->bd_contains) {
202
		DEV_MESSAGE(KERN_WARNING, block->base, "%s",
L
Linus Torvalds 已提交
203 204 205
			    "Cannot low-level format a partition");
		return -EINVAL;
	}
206
	return dasd_format(block, &fdata);
L
Linus Torvalds 已提交
207 208 209 210 211 212
}

#ifdef CONFIG_DASD_PROFILE
/*
 * Reset device profile information
 */
213
static int dasd_ioctl_reset_profile(struct dasd_block *block)
L
Linus Torvalds 已提交
214
{
215
	memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
L
Linus Torvalds 已提交
216 217 218 219 220 221
	return 0;
}

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

237
static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245
{
	return -ENOSYS;
}
#endif

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

256 257
	base = block->base;
	if (!base->discipline->fill_info)
L
Linus Torvalds 已提交
258 259
		return -EINVAL;

260
	dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
L
Linus Torvalds 已提交
261 262 263
	if (dasd_info == NULL)
		return -ENOMEM;

264
	rc = base->discipline->fill_info(base, dasd_info);
L
Linus Torvalds 已提交
265 266 267 268 269
	if (rc) {
		kfree(dasd_info);
		return rc;
	}

270
	cdev = base->cdev;
271
	ccw_device_get_id(cdev, &dev_id);
L
Linus Torvalds 已提交
272

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

L
Linus Torvalds 已提交
289 290 291 292
	/*
	 * check if device is really formatted
	 * LDL / CDL was returned by 'fill_info'
	 */
293 294
	if ((base->state < DASD_STATE_READY) ||
	    (dasd_check_blocksize(block->bp_block)))
L
Linus Torvalds 已提交
295
		dasd_info->format = DASD_FORMAT_NONE;
296

297
	dasd_info->features |=
298
		((base->features & DASD_FEATURE_READONLY) != 0);
L
Linus Torvalds 已提交
299

300 301
	if (base->discipline)
		memcpy(dasd_info->type, base->discipline->name, 4);
L
Linus Torvalds 已提交
302 303
	else
		memcpy(dasd_info->type, "none", 4);
304

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

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

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

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

L
Linus Torvalds 已提交
350
	set_disk_ro(bdev->bd_disk, intval);
351
	return dasd_set_feature(block->base->cdev, DASD_FEATURE_READONLY, intval);
L
Linus Torvalds 已提交
352 353
}

354
static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
355 356 357 358 359 360 361
		unsigned long arg)
{
	struct cmbdata __user *argp = (void __user *) arg;
	size_t size = _IOC_SIZE(cmd);
	struct cmbdata data;
	int ret;

362
	ret = cmf_readall(block->base->cdev, &data);
363 364 365 366 367
	if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
		return -EFAULT;
	return ret;
}

368 369 370
static int
dasd_do_ioctl(struct block_device *bdev, fmode_t mode,
	      unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
371
{
372
	struct dasd_block *block = bdev->bd_disk->private_data;
373
	void __user *argp = (void __user *)arg;
L
Linus Torvalds 已提交
374

375
	if (!block)
376 377 378 379 380 381
                return -ENODEV;

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

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

420 421
		return -EINVAL;
	}
L
Linus Torvalds 已提交
422
}
423 424 425 426 427 428 429 430 431 432 433

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