On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / char / raw.c
blob8f0b8d5c824c1c442a236215142d7670025064cf
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/smp_lock.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 lock_kernel();
59 mutex_lock(&raw_mutex);
62 * All we need to do on open is check that the device is bound.
64 bdev = raw_devices[minor].binding;
65 err = -ENODEV;
66 if (!bdev)
67 goto out;
68 igrab(bdev->bd_inode);
69 err = blkdev_get(bdev, filp->f_mode);
70 if (err)
71 goto out;
72 err = bd_claim(bdev, raw_open);
73 if (err)
74 goto out1;
75 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
76 if (err)
77 goto out2;
78 filp->f_flags |= O_DIRECT;
79 filp->f_mapping = bdev->bd_inode->i_mapping;
80 if (++raw_devices[minor].inuse == 1)
81 filp->f_path.dentry->d_inode->i_mapping =
82 bdev->bd_inode->i_mapping;
83 filp->private_data = bdev;
84 mutex_unlock(&raw_mutex);
85 unlock_kernel();
86 return 0;
88 out2:
89 bd_release(bdev);
90 out1:
91 blkdev_put(bdev, filp->f_mode);
92 out:
93 mutex_unlock(&raw_mutex);
94 unlock_kernel();
95 return err;
99 * When the final fd which refers to this character-special node is closed, we
100 * make its ->mapping point back at its own i_data.
102 static int raw_release(struct inode *inode, struct file *filp)
104 const int minor= iminor(inode);
105 struct block_device *bdev;
107 mutex_lock(&raw_mutex);
108 bdev = raw_devices[minor].binding;
109 if (--raw_devices[minor].inuse == 0) {
110 /* Here inode->i_mapping == bdev->bd_inode->i_mapping */
111 inode->i_mapping = &inode->i_data;
112 inode->i_mapping->backing_dev_info = &default_backing_dev_info;
114 mutex_unlock(&raw_mutex);
116 bd_release(bdev);
117 blkdev_put(bdev, filp->f_mode);
118 return 0;
122 * Forward ioctls to the underlying block device.
124 static int
125 raw_ioctl(struct inode *inode, struct file *filp,
126 unsigned int command, unsigned long arg)
128 struct block_device *bdev = filp->private_data;
130 return blkdev_ioctl(bdev, 0, command, arg);
133 static int bind_set(int number, u64 major, u64 minor)
135 dev_t dev = MKDEV(major, minor);
136 struct raw_device_data *rawdev;
137 int err = 0;
139 if (number <= 0 || number >= MAX_RAW_MINORS)
140 return -EINVAL;
142 if (MAJOR(dev) != major || MINOR(dev) != minor)
143 return -EINVAL;
145 rawdev = &raw_devices[number];
148 * This is like making block devices, so demand the
149 * same capability
151 if (!capable(CAP_SYS_ADMIN))
152 return -EPERM;
155 * For now, we don't need to check that the underlying
156 * block device is present or not: we can do that when
157 * the raw device is opened. Just check that the
158 * major/minor numbers make sense.
161 if (MAJOR(dev) == 0 && dev != 0)
162 return -EINVAL;
164 mutex_lock(&raw_mutex);
165 if (rawdev->inuse) {
166 mutex_unlock(&raw_mutex);
167 return -EBUSY;
169 if (rawdev->binding) {
170 bdput(rawdev->binding);
171 module_put(THIS_MODULE);
173 if (!dev) {
174 /* unbind */
175 rawdev->binding = NULL;
176 device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
177 } else {
178 rawdev->binding = bdget(dev);
179 if (rawdev->binding == NULL) {
180 err = -ENOMEM;
181 } else {
182 dev_t raw = MKDEV(RAW_MAJOR, number);
183 __module_get(THIS_MODULE);
184 device_destroy(raw_class, raw);
185 device_create(raw_class, NULL, raw, NULL,
186 "raw%d", number);
189 mutex_unlock(&raw_mutex);
190 return err;
193 static int bind_get(int number, dev_t *dev)
195 struct raw_device_data *rawdev;
196 struct block_device *bdev;
198 if (number <= 0 || number >= MAX_RAW_MINORS)
199 return -EINVAL;
201 rawdev = &raw_devices[number];
203 mutex_lock(&raw_mutex);
204 bdev = rawdev->binding;
205 *dev = bdev ? bdev->bd_dev : 0;
206 mutex_unlock(&raw_mutex);
207 return 0;
211 * Deal with ioctls against the raw-device control interface, to bind
212 * and unbind other raw devices.
214 static int raw_ctl_ioctl(struct inode *inode, struct file *filp,
215 unsigned int command, unsigned long arg)
217 struct raw_config_request rq;
218 dev_t dev;
219 int err;
221 switch (command) {
222 case RAW_SETBIND:
223 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
224 return -EFAULT;
226 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
228 case RAW_GETBIND:
229 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
230 return -EFAULT;
232 err = bind_get(rq.raw_minor, &dev);
233 if (err)
234 return err;
236 rq.block_major = MAJOR(dev);
237 rq.block_minor = MINOR(dev);
239 if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
240 return -EFAULT;
242 return 0;
245 return -EINVAL;
248 #ifdef CONFIG_COMPAT
249 struct raw32_config_request {
250 compat_int_t raw_minor;
251 compat_u64 block_major;
252 compat_u64 block_minor;
255 static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
256 unsigned long arg)
258 struct raw32_config_request __user *user_req = compat_ptr(arg);
259 struct raw32_config_request rq;
260 dev_t dev;
261 int err = 0;
263 switch (cmd) {
264 case RAW_SETBIND:
265 if (copy_from_user(&rq, user_req, sizeof(rq)))
266 return -EFAULT;
268 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
270 case RAW_GETBIND:
271 if (copy_from_user(&rq, user_req, sizeof(rq)))
272 return -EFAULT;
274 err = bind_get(rq.raw_minor, &dev);
275 if (err)
276 return err;
278 rq.block_major = MAJOR(dev);
279 rq.block_minor = MINOR(dev);
281 if (copy_to_user(user_req, &rq, sizeof(rq)))
282 return -EFAULT;
284 return 0;
287 return -EINVAL;
289 #endif
291 static const struct file_operations raw_fops = {
292 .read = do_sync_read,
293 .aio_read = generic_file_aio_read,
294 .write = do_sync_write,
295 .aio_write = blkdev_aio_write,
296 .open = raw_open,
297 .release= raw_release,
298 .ioctl = raw_ioctl,
299 .owner = THIS_MODULE,
302 static const struct file_operations raw_ctl_fops = {
303 .ioctl = raw_ctl_ioctl,
304 #ifdef CONFIG_COMPAT
305 .compat_ioctl = raw_ctl_compat_ioctl,
306 #endif
307 .open = raw_open,
308 .owner = THIS_MODULE,
311 static struct cdev raw_cdev;
313 static char *raw_devnode(struct device *dev, mode_t *mode)
315 return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
318 static int __init raw_init(void)
320 dev_t dev = MKDEV(RAW_MAJOR, 0);
321 int ret;
323 ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw");
324 if (ret)
325 goto error;
327 cdev_init(&raw_cdev, &raw_fops);
328 ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS);
329 if (ret) {
330 kobject_put(&raw_cdev.kobj);
331 goto error_region;
334 raw_class = class_create(THIS_MODULE, "raw");
335 if (IS_ERR(raw_class)) {
336 printk(KERN_ERR "Error creating raw class.\n");
337 cdev_del(&raw_cdev);
338 ret = PTR_ERR(raw_class);
339 goto error_region;
341 raw_class->devnode = raw_devnode;
342 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
344 return 0;
346 error_region:
347 unregister_chrdev_region(dev, MAX_RAW_MINORS);
348 error:
349 return ret;
352 static void __exit raw_exit(void)
354 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
355 class_destroy(raw_class);
356 cdev_del(&raw_cdev);
357 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS);
360 module_init(raw_init);
361 module_exit(raw_exit);
362 MODULE_LICENSE("GPL");