2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/raw.h>
|
|
|
|
#include <linux/capability.h>
|
|
|
|
#include <linux/uio.h>
|
|
|
|
#include <linux/cdev.h>
|
|
|
|
#include <linux/device.h>
|
2006-03-23 04:00:21 -07:00
|
|
|
#include <linux/mutex.h>
|
2008-05-16 13:54:46 -06:00
|
|
|
#include <linux/smp_lock.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 02:04:11 -06:00
|
|
|
#include <linux/gfp.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
|
|
|
struct raw_device_data {
|
|
|
|
struct block_device *binding;
|
|
|
|
int inuse;
|
|
|
|
};
|
|
|
|
|
2005-03-23 10:53:09 -07:00
|
|
|
static struct class *raw_class;
|
2005-04-16 16:20:36 -06:00
|
|
|
static struct raw_device_data raw_devices[MAX_RAW_MINORS];
|
2006-03-23 04:00:21 -07:00
|
|
|
static DEFINE_MUTEX(raw_mutex);
|
2006-07-03 01:24:21 -06:00
|
|
|
static const struct file_operations raw_ctl_fops; /* forward declaration */
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2008-05-16 13:54:46 -06:00
|
|
|
lock_kernel();
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_lock(&raw_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
igrab(bdev->bd_inode);
|
2007-10-08 11:24:05 -06:00
|
|
|
err = blkdev_get(bdev, filp->f_mode);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
err = bd_claim(bdev, raw_open);
|
|
|
|
if (err)
|
|
|
|
goto out1;
|
2009-05-22 15:17:49 -06:00
|
|
|
err = set_blocksize(bdev, bdev_logical_block_size(bdev));
|
2005-04-16 16:20:36 -06:00
|
|
|
if (err)
|
|
|
|
goto out2;
|
|
|
|
filp->f_flags |= O_DIRECT;
|
|
|
|
filp->f_mapping = bdev->bd_inode->i_mapping;
|
|
|
|
if (++raw_devices[minor].inuse == 1)
|
2006-12-08 03:36:55 -07:00
|
|
|
filp->f_path.dentry->d_inode->i_mapping =
|
2005-04-16 16:20:36 -06:00
|
|
|
bdev->bd_inode->i_mapping;
|
|
|
|
filp->private_data = bdev;
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_unlock(&raw_mutex);
|
2008-05-16 13:54:46 -06:00
|
|
|
unlock_kernel();
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out2:
|
|
|
|
bd_release(bdev);
|
|
|
|
out1:
|
2008-02-22 18:40:24 -07:00
|
|
|
blkdev_put(bdev, filp->f_mode);
|
2005-04-16 16:20:36 -06:00
|
|
|
out:
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_unlock(&raw_mutex);
|
2009-03-27 04:28:48 -06:00
|
|
|
unlock_kernel();
|
2005-04-16 16:20:36 -06:00
|
|
|
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;
|
|
|
|
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_lock(&raw_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
bdev = raw_devices[minor].binding;
|
|
|
|
if (--raw_devices[minor].inuse == 0) {
|
|
|
|
/* Here inode->i_mapping == bdev->bd_inode->i_mapping */
|
|
|
|
inode->i_mapping = &inode->i_data;
|
|
|
|
inode->i_mapping->backing_dev_info = &default_backing_dev_info;
|
|
|
|
}
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_unlock(&raw_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
bd_release(bdev);
|
2008-02-22 18:40:24 -07:00
|
|
|
blkdev_put(bdev, filp->f_mode);
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forward ioctls to the underlying block device.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
raw_ioctl(struct inode *inode, struct file *filp,
|
|
|
|
unsigned int command, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct block_device *bdev = filp->private_data;
|
|
|
|
|
2008-09-19 01:17:36 -06:00
|
|
|
return blkdev_ioctl(bdev, 0, command, arg);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bind_device(struct raw_config_request *rq)
|
|
|
|
{
|
2006-08-07 23:19:37 -06:00
|
|
|
device_destroy(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor));
|
2008-07-21 21:03:34 -06:00
|
|
|
device_create(raw_class, NULL, MKDEV(RAW_MAJOR, rq->raw_minor), NULL,
|
|
|
|
"raw%d", rq->raw_minor);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with ioctls against the raw-device control interface, to bind
|
|
|
|
* and unbind other raw devices.
|
|
|
|
*/
|
|
|
|
static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
|
|
|
|
unsigned int command, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct raw_config_request rq;
|
|
|
|
struct raw_device_data *rawdev;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
case RAW_SETBIND:
|
|
|
|
case RAW_GETBIND:
|
|
|
|
|
|
|
|
/* First, find out which raw minor we want */
|
|
|
|
|
|
|
|
if (copy_from_user(&rq, (void __user *) arg, sizeof(rq))) {
|
|
|
|
err = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-02-10 02:46:10 -07:00
|
|
|
if (rq.raw_minor <= 0 || rq.raw_minor >= MAX_RAW_MINORS) {
|
2005-04-16 16:20:36 -06:00
|
|
|
err = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rawdev = &raw_devices[rq.raw_minor];
|
|
|
|
|
|
|
|
if (command == RAW_SETBIND) {
|
|
|
|
dev_t dev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is like making block devices, so demand the
|
|
|
|
* same capability
|
|
|
|
*/
|
|
|
|
if (!capable(CAP_SYS_ADMIN)) {
|
|
|
|
err = -EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev = MKDEV(rq.block_major, rq.block_minor);
|
|
|
|
if ((rq.block_major == 0 && rq.block_minor != 0) ||
|
|
|
|
MAJOR(dev) != rq.block_major ||
|
|
|
|
MINOR(dev) != rq.block_minor) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_lock(&raw_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (rawdev->inuse) {
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_unlock(&raw_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
err = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (rawdev->binding) {
|
|
|
|
bdput(rawdev->binding);
|
|
|
|
module_put(THIS_MODULE);
|
|
|
|
}
|
|
|
|
if (rq.block_major == 0 && rq.block_minor == 0) {
|
|
|
|
/* unbind */
|
|
|
|
rawdev->binding = NULL;
|
2006-08-07 23:19:37 -06:00
|
|
|
device_destroy(raw_class,
|
2005-03-23 10:53:09 -07:00
|
|
|
MKDEV(RAW_MAJOR, rq.raw_minor));
|
2005-04-16 16:20:36 -06:00
|
|
|
} else {
|
|
|
|
rawdev->binding = bdget(dev);
|
|
|
|
if (rawdev->binding == NULL)
|
|
|
|
err = -ENOMEM;
|
|
|
|
else {
|
|
|
|
__module_get(THIS_MODULE);
|
|
|
|
bind_device(&rq);
|
|
|
|
}
|
|
|
|
}
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_unlock(&raw_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
} else {
|
|
|
|
struct block_device *bdev;
|
|
|
|
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_lock(&raw_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
bdev = rawdev->binding;
|
|
|
|
if (bdev) {
|
|
|
|
rq.block_major = MAJOR(bdev->bd_dev);
|
|
|
|
rq.block_minor = MINOR(bdev->bd_dev);
|
|
|
|
} else {
|
|
|
|
rq.block_major = rq.block_minor = 0;
|
|
|
|
}
|
2006-03-23 04:00:21 -07:00
|
|
|
mutex_unlock(&raw_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (copy_to_user((void __user *)arg, &rq, sizeof(rq))) {
|
|
|
|
err = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-07-03 01:24:21 -06:00
|
|
|
static const struct file_operations raw_fops = {
|
2006-10-01 00:28:48 -06:00
|
|
|
.read = do_sync_read,
|
2005-04-16 16:20:36 -06:00
|
|
|
.aio_read = generic_file_aio_read,
|
2006-10-01 00:28:48 -06:00
|
|
|
.write = do_sync_write,
|
2009-08-20 09:43:41 -06:00
|
|
|
.aio_write = blkdev_aio_write,
|
2010-04-06 15:35:00 -06:00
|
|
|
.fsync = blkdev_fsync,
|
2005-04-16 16:20:36 -06:00
|
|
|
.open = raw_open,
|
|
|
|
.release= raw_release,
|
|
|
|
.ioctl = raw_ioctl,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2006-07-03 01:24:21 -06:00
|
|
|
static const struct file_operations raw_ctl_fops = {
|
2005-04-16 16:20:36 -06:00
|
|
|
.ioctl = raw_ctl_ioctl,
|
|
|
|
.open = raw_open,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2007-09-12 16:06:57 -06:00
|
|
|
static struct cdev raw_cdev;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2009-09-18 15:01:12 -06:00
|
|
|
static char *raw_devnode(struct device *dev, mode_t *mode)
|
2009-04-30 07:23:42 -06:00
|
|
|
{
|
|
|
|
return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
|
|
|
|
}
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
static int __init raw_init(void)
|
|
|
|
{
|
|
|
|
dev_t dev = MKDEV(RAW_MAJOR, 0);
|
2006-09-29 03:00:46 -06:00
|
|
|
int ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-09-29 03:00:46 -06:00
|
|
|
ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
|
|
|
|
if (ret)
|
2005-04-16 16:20:36 -06:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
cdev_init(&raw_cdev, &raw_fops);
|
2006-09-29 03:00:46 -06:00
|
|
|
ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
|
|
|
|
if (ret) {
|
2005-04-16 16:20:36 -06:00
|
|
|
kobject_put(&raw_cdev.kobj);
|
2006-09-29 03:00:46 -06:00
|
|
|
goto error_region;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2005-03-23 10:53:09 -07:00
|
|
|
raw_class = class_create(THIS_MODULE, "raw");
|
2005-04-16 16:20:36 -06:00
|
|
|
if (IS_ERR(raw_class)) {
|
|
|
|
printk(KERN_ERR "Error creating raw class.\n");
|
|
|
|
cdev_del(&raw_cdev);
|
2006-09-29 03:00:46 -06:00
|
|
|
ret = PTR_ERR(raw_class);
|
|
|
|
goto error_region;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2009-09-18 15:01:12 -06:00
|
|
|
raw_class->devnode = raw_devnode;
|
2008-07-21 21:03:34 -06:00
|
|
|
device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2006-09-29 03:00:46 -06:00
|
|
|
error_region:
|
|
|
|
unregister_chrdev_region(dev, MAX_RAW_MINORS);
|
2005-04-16 16:20:36 -06:00
|
|
|
error:
|
2006-09-29 03:00:46 -06:00
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit raw_exit(void)
|
|
|
|
{
|
2006-08-07 23:19:37 -06:00
|
|
|
device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
|
2005-03-23 10:53:09 -07:00
|
|
|
class_destroy(raw_class);
|
2005-04-16 16:20:36 -06:00
|
|
|
cdev_del(&raw_cdev);
|
|
|
|
unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(raw_init);
|
|
|
|
module_exit(raw_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|