Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/raw.c |
| 3 | * |
| 4 | * Front-end raw character devices. These can be bound to any block |
| 5 | * devices to provide genuine Unix raw character device semantics. |
| 6 | * |
| 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 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Jonathan Corbet | c0bed68 | 2008-05-16 13:54:46 -0600 | [diff] [blame] | 22 | #include <linux/smp_lock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <asm/uaccess.h> |
| 26 | |
| 27 | struct raw_device_data { |
| 28 | struct block_device *binding; |
| 29 | int inuse; |
| 30 | }; |
| 31 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 32 | static struct class *raw_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | static struct raw_device_data raw_devices[MAX_RAW_MINORS]; |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 34 | static DEFINE_MUTEX(raw_mutex); |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 35 | static const struct file_operations raw_ctl_fops; /* forward declaration */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Open/close code for raw IO. |
| 39 | * |
| 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. |
| 43 | * |
| 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. |
| 46 | */ |
| 47 | static int raw_open(struct inode *inode, struct file *filp) |
| 48 | { |
| 49 | const int minor = iminor(inode); |
| 50 | struct block_device *bdev; |
| 51 | int err; |
| 52 | |
| 53 | if (minor == 0) { /* It is the control device */ |
| 54 | filp->f_op = &raw_ctl_fops; |
| 55 | return 0; |
| 56 | } |
| 57 | |
Jonathan Corbet | c0bed68 | 2008-05-16 13:54:46 -0600 | [diff] [blame] | 58 | lock_kernel(); |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 59 | mutex_lock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * All we need to do on open is check that the device is bound. |
| 63 | */ |
| 64 | bdev = raw_devices[minor].binding; |
| 65 | err = -ENODEV; |
| 66 | if (!bdev) |
| 67 | goto out; |
| 68 | igrab(bdev->bd_inode); |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 69 | err = blkdev_get(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (err) |
| 71 | goto out; |
| 72 | err = bd_claim(bdev, raw_open); |
| 73 | if (err) |
| 74 | goto out1; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 75 | err = set_blocksize(bdev, bdev_logical_block_size(bdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 81 | filp->f_path.dentry->d_inode->i_mapping = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | bdev->bd_inode->i_mapping; |
| 83 | filp->private_data = bdev; |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 84 | mutex_unlock(&raw_mutex); |
Jonathan Corbet | c0bed68 | 2008-05-16 13:54:46 -0600 | [diff] [blame] | 85 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return 0; |
| 87 | |
| 88 | out2: |
| 89 | bd_release(bdev); |
| 90 | out1: |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 91 | blkdev_put(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | out: |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 93 | mutex_unlock(&raw_mutex); |
Dan Carpenter | 996ff68 | 2009-03-27 13:28:48 +0300 | [diff] [blame] | 94 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return err; |
| 96 | } |
| 97 | |
| 98 | /* |
| 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. |
| 101 | */ |
| 102 | static int raw_release(struct inode *inode, struct file *filp) |
| 103 | { |
| 104 | const int minor= iminor(inode); |
| 105 | struct block_device *bdev; |
| 106 | |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 107 | mutex_lock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 113 | } |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 114 | mutex_unlock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 117 | blkdev_put(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Forward ioctls to the underlying block device. |
| 123 | */ |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame^] | 124 | static long |
| 125 | raw_ioctl(struct file *filp, unsigned int command, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | struct block_device *bdev = filp->private_data; |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame^] | 128 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame^] | 130 | lock_kernel(); |
| 131 | ret = blkdev_ioctl(bdev, 0, command, arg); |
| 132 | unlock_kernel(); |
| 133 | |
| 134 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void bind_device(struct raw_config_request *rq) |
| 138 | { |
Greg Kroah-Hartman | 38ca6c3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 139 | device_destroy(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor)); |
Greg Kroah-Hartman | 03457cd | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 140 | device_create(raw_class, NULL, MKDEV(RAW_MAJOR, rq->raw_minor), NULL, |
| 141 | "raw%d", rq->raw_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Deal with ioctls against the raw-device control interface, to bind |
| 146 | * and unbind other raw devices. |
| 147 | */ |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame^] | 148 | static long raw_ctl_ioctl(struct file *filp, unsigned int command, |
| 149 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct raw_config_request rq; |
| 152 | struct raw_device_data *rawdev; |
| 153 | int err = 0; |
| 154 | |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame^] | 155 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | switch (command) { |
| 157 | case RAW_SETBIND: |
| 158 | case RAW_GETBIND: |
| 159 | |
| 160 | /* First, find out which raw minor we want */ |
| 161 | |
| 162 | if (copy_from_user(&rq, (void __user *) arg, sizeof(rq))) { |
| 163 | err = -EFAULT; |
| 164 | goto out; |
| 165 | } |
| 166 | |
Jeff Moyer | 38584c1 | 2007-02-10 01:46:10 -0800 | [diff] [blame] | 167 | if (rq.raw_minor <= 0 || rq.raw_minor >= MAX_RAW_MINORS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | err = -EINVAL; |
| 169 | goto out; |
| 170 | } |
| 171 | rawdev = &raw_devices[rq.raw_minor]; |
| 172 | |
| 173 | if (command == RAW_SETBIND) { |
| 174 | dev_t dev; |
| 175 | |
| 176 | /* |
| 177 | * This is like making block devices, so demand the |
| 178 | * same capability |
| 179 | */ |
| 180 | if (!capable(CAP_SYS_ADMIN)) { |
| 181 | err = -EPERM; |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * For now, we don't need to check that the underlying |
| 187 | * block device is present or not: we can do that when |
| 188 | * the raw device is opened. Just check that the |
| 189 | * major/minor numbers make sense. |
| 190 | */ |
| 191 | |
| 192 | dev = MKDEV(rq.block_major, rq.block_minor); |
| 193 | if ((rq.block_major == 0 && rq.block_minor != 0) || |
| 194 | MAJOR(dev) != rq.block_major || |
| 195 | MINOR(dev) != rq.block_minor) { |
| 196 | err = -EINVAL; |
| 197 | goto out; |
| 198 | } |
| 199 | |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 200 | mutex_lock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (rawdev->inuse) { |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 202 | mutex_unlock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | err = -EBUSY; |
| 204 | goto out; |
| 205 | } |
| 206 | if (rawdev->binding) { |
| 207 | bdput(rawdev->binding); |
| 208 | module_put(THIS_MODULE); |
| 209 | } |
| 210 | if (rq.block_major == 0 && rq.block_minor == 0) { |
| 211 | /* unbind */ |
| 212 | rawdev->binding = NULL; |
Greg Kroah-Hartman | 38ca6c3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 213 | device_destroy(raw_class, |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 214 | MKDEV(RAW_MAJOR, rq.raw_minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } else { |
| 216 | rawdev->binding = bdget(dev); |
| 217 | if (rawdev->binding == NULL) |
| 218 | err = -ENOMEM; |
| 219 | else { |
| 220 | __module_get(THIS_MODULE); |
| 221 | bind_device(&rq); |
| 222 | } |
| 223 | } |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 224 | mutex_unlock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } else { |
| 226 | struct block_device *bdev; |
| 227 | |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 228 | mutex_lock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | bdev = rawdev->binding; |
| 230 | if (bdev) { |
| 231 | rq.block_major = MAJOR(bdev->bd_dev); |
| 232 | rq.block_minor = MINOR(bdev->bd_dev); |
| 233 | } else { |
| 234 | rq.block_major = rq.block_minor = 0; |
| 235 | } |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 236 | mutex_unlock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (copy_to_user((void __user *)arg, &rq, sizeof(rq))) { |
| 238 | err = -EFAULT; |
| 239 | goto out; |
| 240 | } |
| 241 | } |
| 242 | break; |
| 243 | default: |
| 244 | err = -EINVAL; |
| 245 | break; |
| 246 | } |
| 247 | out: |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame^] | 248 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return err; |
| 250 | } |
| 251 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 252 | static const struct file_operations raw_fops = { |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame^] | 253 | .read = do_sync_read, |
| 254 | .aio_read = generic_file_aio_read, |
| 255 | .write = do_sync_write, |
| 256 | .aio_write = blkdev_aio_write, |
| 257 | .fsync = blkdev_fsync, |
| 258 | .open = raw_open, |
| 259 | .release = raw_release, |
| 260 | .unlocked_ioctl = raw_ioctl, |
| 261 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 264 | static const struct file_operations raw_ctl_fops = { |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame^] | 265 | .unlocked_ioctl = raw_ctl_ioctl, |
| 266 | .open = raw_open, |
| 267 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 270 | static struct cdev raw_cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 272 | static char *raw_devnode(struct device *dev, mode_t *mode) |
Kay Sievers | 6fd4693 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 273 | { |
| 274 | return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev)); |
| 275 | } |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | static int __init raw_init(void) |
| 278 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | dev_t dev = MKDEV(RAW_MAJOR, 0); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 280 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 282 | ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw"); |
| 283 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | goto error; |
| 285 | |
| 286 | cdev_init(&raw_cdev, &raw_fops); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 287 | ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS); |
| 288 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | kobject_put(&raw_cdev.kobj); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 290 | goto error_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 293 | raw_class = class_create(THIS_MODULE, "raw"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | if (IS_ERR(raw_class)) { |
| 295 | printk(KERN_ERR "Error creating raw class.\n"); |
| 296 | cdev_del(&raw_cdev); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 297 | ret = PTR_ERR(raw_class); |
| 298 | goto error_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 300 | raw_class->devnode = raw_devnode; |
Greg Kroah-Hartman | 03457cd | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 301 | device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | return 0; |
| 304 | |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 305 | error_region: |
| 306 | unregister_chrdev_region(dev, MAX_RAW_MINORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | error: |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 308 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static void __exit raw_exit(void) |
| 312 | { |
Greg Kroah-Hartman | 38ca6c3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 313 | device_destroy(raw_class, MKDEV(RAW_MAJOR, 0)); |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 314 | class_destroy(raw_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | cdev_del(&raw_cdev); |
| 316 | unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS); |
| 317 | } |
| 318 | |
| 319 | module_init(raw_init); |
| 320 | module_exit(raw_exit); |
| 321 | MODULE_LICENSE("GPL"); |