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