raw.c 8.31 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * linux/drivers/char/raw.c
 *
 * Front-end raw character devices.  These can be bound to any block
 * devices to provide genuine Unix raw character device semantics.
 *
 * We reserve minor number 0 for a control interface.  ioctl()s on this
 * device are used to bind the other minor numbers to block devices.
 */

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/major.h>
#include <linux/blkdev.h>
15
#include <linux/backing-dev.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20 21
#include <linux/module.h>
#include <linux/raw.h>
#include <linux/capability.h>
#include <linux/uio.h>
#include <linux/cdev.h>
#include <linux/device.h>
22
#include <linux/mutex.h>
23
#include <linux/gfp.h>
24
#include <linux/compat.h>
25
#include <linux/vmalloc.h>
Linus Torvalds's avatar
Linus Torvalds committed
26

27
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30 31 32 33

struct raw_device_data {
	struct block_device *binding;
	int inuse;
};

34
static struct class *raw_class;
35
static struct raw_device_data *raw_devices;
36
static DEFINE_MUTEX(raw_mutex);
37
static const struct file_operations raw_ctl_fops; /* forward declaration */
Linus Torvalds's avatar
Linus Torvalds committed
38

39 40 41 42 43
static int max_raw_minors = MAX_RAW_MINORS;

module_param(max_raw_minors, int, 0);
MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)");

Linus Torvalds's avatar
Linus Torvalds committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*
 * Open/close code for raw IO.
 *
 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
 * point at the blockdev's address_space and set the file handle to use
 * O_DIRECT.
 *
 * Set the device's soft blocksize to the minimum possible.  This gives the
 * finest possible alignment and has no adverse impact on performance.
 */
static int raw_open(struct inode *inode, struct file *filp)
{
	const int minor = iminor(inode);
	struct block_device *bdev;
	int err;

	if (minor == 0) {	/* It is the control device */
		filp->f_op = &raw_ctl_fops;
		return 0;
	}

65
	mutex_lock(&raw_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
66 67 68 69 70 71 72 73

	/*
	 * All we need to do on open is check that the device is bound.
	 */
	bdev = raw_devices[minor].binding;
	err = -ENODEV;
	if (!bdev)
		goto out;
74
	bdgrab(bdev);
75
	err = blkdev_get(bdev, filp->f_mode | FMODE_EXCL, raw_open);
Linus Torvalds's avatar
Linus Torvalds committed
76 77
	if (err)
		goto out;
78
	err = set_blocksize(bdev, bdev_logical_block_size(bdev));
Linus Torvalds's avatar
Linus Torvalds committed
79
	if (err)
80
		goto out1;
Linus Torvalds's avatar
Linus Torvalds committed
81 82 83
	filp->f_flags |= O_DIRECT;
	filp->f_mapping = bdev->bd_inode->i_mapping;
	if (++raw_devices[minor].inuse == 1)
Al Viro's avatar
Al Viro committed
84
		file_inode(filp)->i_mapping =
Linus Torvalds's avatar
Linus Torvalds committed
85 86
			bdev->bd_inode->i_mapping;
	filp->private_data = bdev;
87
	mutex_unlock(&raw_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
88 89 90
	return 0;

out1:
91
	blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
Linus Torvalds's avatar
Linus Torvalds committed
92
out:
93
	mutex_unlock(&raw_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
94 95 96 97 98 99 100 101 102 103 104 105
	return err;
}

/*
 * When the final fd which refers to this character-special node is closed, we
 * make its ->mapping point back at its own i_data.
 */
static int raw_release(struct inode *inode, struct file *filp)
{
	const int minor= iminor(inode);
	struct block_device *bdev;

106
	mutex_lock(&raw_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
107
	bdev = raw_devices[minor].binding;
108
	if (--raw_devices[minor].inuse == 0)
Linus Torvalds's avatar
Linus Torvalds committed
109 110
		/* Here  inode->i_mapping == bdev->bd_inode->i_mapping  */
		inode->i_mapping = &inode->i_data;
111
	mutex_unlock(&raw_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
112

113
	blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118 119
	return 0;
}

/*
 * Forward ioctls to the underlying block device.
 */
120 121
static long
raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
122 123
{
	struct block_device *bdev = filp->private_data;
124 125 126 127 128 129 130 131 132
	return blkdev_ioctl(bdev, 0, command, arg);
}

static int bind_set(int number, u64 major, u64 minor)
{
	dev_t dev = MKDEV(major, minor);
	struct raw_device_data *rawdev;
	int err = 0;

133
	if (number <= 0 || number >= max_raw_minors)
134 135 136 137 138 139
		return -EINVAL;

	if (MAJOR(dev) != major || MINOR(dev) != minor)
		return -EINVAL;

	rawdev = &raw_devices[number];
Linus Torvalds's avatar
Linus Torvalds committed
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	/*
	 * This is like making block devices, so demand the
	 * same capability
	 */
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	/*
	 * For now, we don't need to check that the underlying
	 * block device is present or not: we can do that when
	 * the raw device is opened.  Just check that the
	 * major/minor numbers make sense.
	 */

	if (MAJOR(dev) == 0 && dev != 0)
		return -EINVAL;
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
	mutex_lock(&raw_mutex);
	if (rawdev->inuse) {
		mutex_unlock(&raw_mutex);
		return -EBUSY;
	}
	if (rawdev->binding) {
		bdput(rawdev->binding);
		module_put(THIS_MODULE);
	}
	if (!dev) {
		/* unbind */
		rawdev->binding = NULL;
		device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
	} else {
		rawdev->binding = bdget(dev);
		if (rawdev->binding == NULL) {
			err = -ENOMEM;
		} else {
			dev_t raw = MKDEV(RAW_MAJOR, number);
			__module_get(THIS_MODULE);
			device_destroy(raw_class, raw);
			device_create(raw_class, NULL, raw, NULL,
				      "raw%d", number);
		}
	}
	mutex_unlock(&raw_mutex);
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
185 186
}

187
static int bind_get(int number, dev_t *dev)
Linus Torvalds's avatar
Linus Torvalds committed
188
{
189 190 191
	struct raw_device_data *rawdev;
	struct block_device *bdev;

192
	if (number <= 0 || number >= max_raw_minors)
193 194 195 196 197 198 199 200 201
		return -EINVAL;

	rawdev = &raw_devices[number];

	mutex_lock(&raw_mutex);
	bdev = rawdev->binding;
	*dev = bdev ? bdev->bd_dev : 0;
	mutex_unlock(&raw_mutex);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
202 203 204 205 206 207
}

/*
 * Deal with ioctls against the raw-device control interface, to bind
 * and unbind other raw devices.
 */
208 209
static long raw_ctl_ioctl(struct file *filp, unsigned int command,
			  unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
210 211
{
	struct raw_config_request rq;
212 213
	dev_t dev;
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216

	switch (command) {
	case RAW_SETBIND:
217 218 219 220 221
		if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
			return -EFAULT;

		return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);

Linus Torvalds's avatar
Linus Torvalds committed
222
	case RAW_GETBIND:
223 224
		if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
			return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
225

226 227 228
		err = bind_get(rq.raw_minor, &dev);
		if (err)
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
229

230 231
		rq.block_major = MAJOR(dev);
		rq.block_minor = MINOR(dev);
Linus Torvalds's avatar
Linus Torvalds committed
232

233 234 235 236
		if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
			return -EFAULT;

		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
237
	}
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

	return -EINVAL;
}

#ifdef CONFIG_COMPAT
struct raw32_config_request {
	compat_int_t	raw_minor;
	compat_u64	block_major;
	compat_u64	block_minor;
};

static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
				unsigned long arg)
{
	struct raw32_config_request __user *user_req = compat_ptr(arg);
	struct raw32_config_request rq;
	dev_t dev;
	int err = 0;

	switch (cmd) {
	case RAW_SETBIND:
		if (copy_from_user(&rq, user_req, sizeof(rq)))
			return -EFAULT;

		return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);

	case RAW_GETBIND:
		if (copy_from_user(&rq, user_req, sizeof(rq)))
			return -EFAULT;

		err = bind_get(rq.raw_minor, &dev);
		if (err)
			return err;

		rq.block_major = MAJOR(dev);
		rq.block_minor = MINOR(dev);

		if (copy_to_user(user_req, &rq, sizeof(rq)))
			return -EFAULT;

		return 0;
	}

	return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
282
}
283
#endif
Linus Torvalds's avatar
Linus Torvalds committed
284

285
static const struct file_operations raw_fops = {
286
	.read_iter	= blkdev_read_iter,
287
	.write_iter	= blkdev_write_iter,
288 289 290 291
	.fsync		= blkdev_fsync,
	.open		= raw_open,
	.release	= raw_release,
	.unlocked_ioctl = raw_ioctl,
292
	.llseek		= default_llseek,
293
	.owner		= THIS_MODULE,
Linus Torvalds's avatar
Linus Torvalds committed
294 295
};

296
static const struct file_operations raw_ctl_fops = {
297
	.unlocked_ioctl = raw_ctl_ioctl,
298 299 300
#ifdef CONFIG_COMPAT
	.compat_ioctl	= raw_ctl_compat_ioctl,
#endif
301 302
	.open		= raw_open,
	.owner		= THIS_MODULE,
303
	.llseek		= noop_llseek,
Linus Torvalds's avatar
Linus Torvalds committed
304 305
};

306
static struct cdev raw_cdev;
Linus Torvalds's avatar
Linus Torvalds committed
307

308
static char *raw_devnode(struct device *dev, umode_t *mode)
309 310 311 312
{
	return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
}

Linus Torvalds's avatar
Linus Torvalds committed
313 314 315
static int __init raw_init(void)
{
	dev_t dev = MKDEV(RAW_MAJOR, 0);
316
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
317

318 319 320 321 322 323
	if (max_raw_minors < 1 || max_raw_minors > 65536) {
		printk(KERN_WARNING "raw: invalid max_raw_minors (must be"
			" between 1 and 65536), using %d\n", MAX_RAW_MINORS);
		max_raw_minors = MAX_RAW_MINORS;
	}

324
	raw_devices = vzalloc(sizeof(struct raw_device_data) * max_raw_minors);
325 326 327 328 329 330 331
	if (!raw_devices) {
		printk(KERN_ERR "Not enough memory for raw device structures\n");
		ret = -ENOMEM;
		goto error;
	}

	ret = register_chrdev_region(dev, max_raw_minors, "raw");
332
	if (ret)
Linus Torvalds's avatar
Linus Torvalds committed
333 334 335
		goto error;

	cdev_init(&raw_cdev, &raw_fops);
336
	ret = cdev_add(&raw_cdev, dev, max_raw_minors);
337
	if (ret)
338
		goto error_region;
339
	raw_class = class_create(THIS_MODULE, "raw");
Linus Torvalds's avatar
Linus Torvalds committed
340 341 342
	if (IS_ERR(raw_class)) {
		printk(KERN_ERR "Error creating raw class.\n");
		cdev_del(&raw_cdev);
343 344
		ret = PTR_ERR(raw_class);
		goto error_region;
Linus Torvalds's avatar
Linus Torvalds committed
345
	}
346
	raw_class->devnode = raw_devnode;
347
	device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
Linus Torvalds's avatar
Linus Torvalds committed
348 349 350

	return 0;

351
error_region:
352
	unregister_chrdev_region(dev, max_raw_minors);
Linus Torvalds's avatar
Linus Torvalds committed
353
error:
354
	vfree(raw_devices);
355
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359
}

static void __exit raw_exit(void)
{
360
	device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
361
	class_destroy(raw_class);
Linus Torvalds's avatar
Linus Torvalds committed
362
	cdev_del(&raw_cdev);
363
	unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), max_raw_minors);
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367 368
}

module_init(raw_init);
module_exit(raw_exit);
MODULE_LICENSE("GPL");