block: share request flush fields with elevator_private
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / raw.c
blobbfe25ea9766b94caf11ffbc5ca4fb3b766c2c188
1 /*
2 * linux/drivers/char/raw.c
4 * Front-end raw character devices. These can be bound to any block
5 * devices to provide genuine Unix raw character device semantics.
7 * We reserve minor number 0 for a control interface. ioctl()s on this
8 * device are used to bind the other minor numbers to block devices.
9 */
11 #include <linux/init.h>
12 #include <linux/fs.h>
13 #include <linux/major.h>
14 #include <linux/blkdev.h>
15 #include <linux/module.h>
16 #include <linux/raw.h>
17 #include <linux/capability.h>
18 #include <linux/uio.h>
19 #include <linux/cdev.h>
20 #include <linux/device.h>
21 #include <linux/mutex.h>
22 #include <linux/gfp.h>
23 #include <linux/compat.h>
25 #include <asm/uaccess.h>
27 struct raw_device_data {
28 struct block_device *binding;
29 int inuse;
32 static struct class *raw_class;
33 static struct raw_device_data raw_devices[MAX_RAW_MINORS];
34 static DEFINE_MUTEX(raw_mutex);
35 static const struct file_operations raw_ctl_fops; /* forward declaration */
38 * Open/close code for raw IO.
40 * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
41 * point at the blockdev's address_space and set the file handle to use
42 * O_DIRECT.
44 * Set the device's soft blocksize to the minimum possible. This gives the
45 * finest possible alignment and has no adverse impact on performance.
47 static int raw_open(struct inode *inode, struct file *filp)
49 const int minor = iminor(inode);
50 struct block_device *bdev;
51 int err;
53 if (minor == 0) { /* It is the control device */
54 filp->f_op = &raw_ctl_fops;
55 return 0;
58 mutex_lock(&raw_mutex);
61 * All we need to do on open is check that the device is bound.
63 bdev = raw_devices[minor].binding;
64 err = -ENODEV;
65 if (!bdev)
66 goto out;
67 igrab(bdev->bd_inode);
68 err = blkdev_get(bdev, filp->f_mode);
69 if (err)
70 goto out;
71 err = bd_claim(bdev, raw_open);
72 if (err)
73 goto out1;
74 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
75 if (err)
76 goto out2;
77 filp->f_flags |= O_DIRECT;
78 filp->f_mapping = bdev->bd_inode->i_mapping;
79 if (++raw_devices[minor].inuse == 1)
80 filp->f_path.dentry->d_inode->i_mapping =
81 bdev->bd_inode->i_mapping;
82 filp->private_data = bdev;
83 mutex_unlock(&raw_mutex);
84 return 0;
86 out2:
87 bd_release(bdev);
88 out1:
89 blkdev_put(bdev, filp->f_mode);
90 out:
91 mutex_unlock(&raw_mutex);
92 return err;
96 * When the final fd which refers to this character-special node is closed, we
97 * make its ->mapping point back at its own i_data.
99 static int raw_release(struct inode *inode, struct file *filp)
101 const int minor= iminor(inode);
102 struct block_device *bdev;
104 mutex_lock(&raw_mutex);
105 bdev = raw_devices[minor].binding;
106 if (--raw_devices[minor].inuse == 0) {
107 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
108 inode->i_mapping = &inode->i_data;
109 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
111 mutex_unlock(&raw_mutex);
113 bd_release(bdev);
114 blkdev_put(bdev, filp->f_mode);
115 return 0;
119 * Forward ioctls to the underlying block device.
121 static long
122 raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
124 struct block_device *bdev = filp->private_data;
125 return blkdev_ioctl(bdev, 0, command, arg);
128 static int bind_set(int number, u64 major, u64 minor)
130 dev_t dev = MKDEV(major, minor);
131 struct raw_device_data *rawdev;
132 int err = 0;
134 if (number <= 0 || number >= MAX_RAW_MINORS)
135 return -EINVAL;
137 if (MAJOR(dev) != major || MINOR(dev) != minor)
138 return -EINVAL;
140 rawdev = &raw_devices[number];
143 * This is like making block devices, so demand the
144 * same capability
146 if (!capable(CAP_SYS_ADMIN))
147 return -EPERM;
150 * For now, we don't need to check that the underlying
151 * block device is present or not: we can do that when
152 * the raw device is opened. Just check that the
153 * major/minor numbers make sense.
156 if (MAJOR(dev) == 0 && dev != 0)
157 return -EINVAL;
159 mutex_lock(&raw_mutex);
160 if (rawdev->inuse) {
161 mutex_unlock(&raw_mutex);
162 return -EBUSY;
164 if (rawdev->binding) {
165 bdput(rawdev->binding);
166 module_put(THIS_MODULE);
168 if (!dev) {
169 /* unbind */
170 rawdev->binding = NULL;
171 device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
172 } else {
173 rawdev->binding = bdget(dev);
174 if (rawdev->binding == NULL) {
175 err = -ENOMEM;
176 } else {
177 dev_t raw = MKDEV(RAW_MAJOR, number);
178 __module_get(THIS_MODULE);
179 device_destroy(raw_class, raw);
180 device_create(raw_class, NULL, raw, NULL,
181 "raw%d", number);
184 mutex_unlock(&raw_mutex);
185 return err;
188 static int bind_get(int number, dev_t *dev)
190 struct raw_device_data *rawdev;
191 struct block_device *bdev;
193 if (number <= 0 || number >= MAX_RAW_MINORS)
194 return -EINVAL;
196 rawdev = &raw_devices[number];
198 mutex_lock(&raw_mutex);
199 bdev = rawdev->binding;
200 *dev = bdev ? bdev->bd_dev : 0;
201 mutex_unlock(&raw_mutex);
202 return 0;
206 * Deal with ioctls against the raw-device control interface, to bind
207 * and unbind other raw devices.
209 static long raw_ctl_ioctl(struct file *filp, unsigned int command,
210 unsigned long arg)
212 struct raw_config_request rq;
213 dev_t dev;
214 int err;
216 switch (command) {
217 case RAW_SETBIND:
218 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
219 return -EFAULT;
221 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
223 case RAW_GETBIND:
224 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
225 return -EFAULT;
227 err = bind_get(rq.raw_minor, &dev);
228 if (err)
229 return err;
231 rq.block_major = MAJOR(dev);
232 rq.block_minor = MINOR(dev);
234 if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
235 return -EFAULT;
237 return 0;
240 return -EINVAL;
243 #ifdef CONFIG_COMPAT
244 struct raw32_config_request {
245 compat_int_t raw_minor;
246 compat_u64 block_major;
247 compat_u64 block_minor;
250 static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
251 unsigned long arg)
253 struct raw32_config_request __user *user_req = compat_ptr(arg);
254 struct raw32_config_request rq;
255 dev_t dev;
256 int err = 0;
258 switch (cmd) {
259 case RAW_SETBIND:
260 if (copy_from_user(&rq, user_req, sizeof(rq)))
261 return -EFAULT;
263 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
265 case RAW_GETBIND:
266 if (copy_from_user(&rq, user_req, sizeof(rq)))
267 return -EFAULT;
269 err = bind_get(rq.raw_minor, &dev);
270 if (err)
271 return err;
273 rq.block_major = MAJOR(dev);
274 rq.block_minor = MINOR(dev);
276 if (copy_to_user(user_req, &rq, sizeof(rq)))
277 return -EFAULT;
279 return 0;
282 return -EINVAL;
284 #endif
286 static const struct file_operations raw_fops = {
287 .read = do_sync_read,
288 .aio_read = generic_file_aio_read,
289 .write = do_sync_write,
290 .aio_write = blkdev_aio_write,
291 .fsync = blkdev_fsync,
292 .open = raw_open,
293 .release = raw_release,
294 .unlocked_ioctl = raw_ioctl,
295 .llseek = default_llseek,
296 .owner = THIS_MODULE,
299 static const struct file_operations raw_ctl_fops = {
300 .unlocked_ioctl = raw_ctl_ioctl,
301 #ifdef CONFIG_COMPAT
302 .compat_ioctl = raw_ctl_compat_ioctl,
303 #endif
304 .open = raw_open,
305 .owner = THIS_MODULE,
306 .llseek = noop_llseek,
309 static struct cdev raw_cdev;
311 static char *raw_devnode(struct device *dev, mode_t *mode)
313 return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
316 static int __init raw_init(void)
318 dev_t dev = MKDEV(RAW_MAJOR, 0);
319 int ret;
321 ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
322 if (ret)
323 goto error;
325 cdev_init(&raw_cdev, &raw_fops);
326 ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
327 if (ret) {
328 kobject_put(&raw_cdev.kobj);
329 goto error_region;
332 raw_class = class_create(THIS_MODULE, "raw");
333 if (IS_ERR(raw_class)) {
334 printk(KERN_ERR "Error creating raw class.\n");
335 cdev_del(&raw_cdev);
336 ret = PTR_ERR(raw_class);
337 goto error_region;
339 raw_class->devnode = raw_devnode;
340 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
342 return 0;
344 error_region:
345 unregister_chrdev_region(dev, MAX_RAW_MINORS);
346 error:
347 return ret;
350 static void __exit raw_exit(void)
352 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
353 class_destroy(raw_class);
354 cdev_del(&raw_cdev);
355 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
358 module_init(raw_init);
359 module_exit(raw_exit);
360 MODULE_LICENSE("GPL");