File: /usr/src/linux/drivers/char/raw.c
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/fs.h>
12 #include <linux/iobuf.h>
13 #include <linux/major.h>
14 #include <linux/blkdev.h>
15 #include <linux/raw.h>
16 #include <linux/capability.h>
17 #include <linux/smp_lock.h>
18 #include <asm/uaccess.h>
19
20 #define dprintk(x...)
21
22 typedef struct raw_device_data_s {
23 struct block_device *binding;
24 int inuse, sector_size, sector_bits;
25 struct semaphore mutex;
26 } raw_device_data_t;
27
28 static raw_device_data_t raw_devices[256];
29
30 static ssize_t rw_raw_dev(int rw, struct file *, char *, size_t, loff_t *);
31
32 ssize_t raw_read(struct file *, char *, size_t, loff_t *);
33 ssize_t raw_write(struct file *, const char *, size_t, loff_t *);
34 int raw_open(struct inode *, struct file *);
35 int raw_release(struct inode *, struct file *);
36 int raw_ctl_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
37
38
39 static struct file_operations raw_fops = {
40 read: raw_read,
41 write: raw_write,
42 open: raw_open,
43 release: raw_release,
44 };
45
46 static struct file_operations raw_ctl_fops = {
47 ioctl: raw_ctl_ioctl,
48 open: raw_open,
49 };
50
51 static int __init raw_init(void)
52 {
53 int i;
54 register_chrdev(RAW_MAJOR, "raw", &raw_fops);
55
56 for (i = 0; i < 256; i++)
57 init_MUTEX(&raw_devices[i].mutex);
58
59 return 0;
60 }
61
62 __initcall(raw_init);
63
64 /*
65 * Open/close code for raw IO.
66 */
67
68 int raw_open(struct inode *inode, struct file *filp)
69 {
70 int minor;
71 struct block_device * bdev;
72 kdev_t rdev; /* it should eventually go away */
73 int err;
74 int sector_size;
75 int sector_bits;
76
77 minor = MINOR(inode->i_rdev);
78
79 /*
80 * Is it the control device?
81 */
82
83 if (minor == 0) {
84 filp->f_op = &raw_ctl_fops;
85 return 0;
86 }
87
88 if (!filp->f_iobuf) {
89 err = alloc_kiovec(1, &filp->f_iobuf);
90 if (err)
91 return err;
92 }
93
94 down(&raw_devices[minor].mutex);
95 /*
96 * No, it is a normal raw device. All we need to do on open is
97 * to check that the device is bound, and force the underlying
98 * block device to a sector-size blocksize.
99 */
100
101 bdev = raw_devices[minor].binding;
102 err = -ENODEV;
103 if (!bdev)
104 goto out;
105
106 atomic_inc(&bdev->bd_count);
107 rdev = to_kdev_t(bdev->bd_dev);
108 err = blkdev_get(bdev, filp->f_mode, 0, BDEV_RAW);
109 if (err)
110 goto out;
111
112 /*
113 * Don't change the blocksize if we already have users using
114 * this device
115 */
116
117 if (raw_devices[minor].inuse++)
118 goto out;
119
120 /*
121 * Don't interfere with mounted devices: we cannot safely set
122 * the blocksize on a device which is already mounted.
123 */
124
125 sector_size = 512;
126 if (is_mounted(rdev)) {
127 if (blksize_size[MAJOR(rdev)])
128 sector_size = blksize_size[MAJOR(rdev)][MINOR(rdev)];
129 } else {
130 if (hardsect_size[MAJOR(rdev)])
131 sector_size = hardsect_size[MAJOR(rdev)][MINOR(rdev)];
132 }
133
134 set_blocksize(rdev, sector_size);
135 raw_devices[minor].sector_size = sector_size;
136
137 for (sector_bits = 0; !(sector_size & 1); )
138 sector_size>>=1, sector_bits++;
139 raw_devices[minor].sector_bits = sector_bits;
140
141 out:
142 up(&raw_devices[minor].mutex);
143
144 return err;
145 }
146
147 int raw_release(struct inode *inode, struct file *filp)
148 {
149 int minor;
150 struct block_device *bdev;
151
152 minor = MINOR(inode->i_rdev);
153 down(&raw_devices[minor].mutex);
154 bdev = raw_devices[minor].binding;
155 raw_devices[minor].inuse--;
156 up(&raw_devices[minor].mutex);
157 blkdev_put(bdev, BDEV_RAW);
158 return 0;
159 }
160
161
162
163 /*
164 * Deal with ioctls against the raw-device control interface, to bind
165 * and unbind other raw devices.
166 */
167
168 int raw_ctl_ioctl(struct inode *inode,
169 struct file *flip,
170 unsigned int command,
171 unsigned long arg)
172 {
173 struct raw_config_request rq;
174 int err = 0;
175 int minor;
176
177 switch (command) {
178 case RAW_SETBIND:
179 case RAW_GETBIND:
180
181 /* First, find out which raw minor we want */
182
183 err = copy_from_user(&rq, (void *) arg, sizeof(rq));
184 if (err)
185 break;
186
187 minor = rq.raw_minor;
188 if (minor <= 0 || minor > MINORMASK) {
189 err = -EINVAL;
190 break;
191 }
192
193 if (command == RAW_SETBIND) {
194 /*
195 * This is like making block devices, so demand the
196 * same capability
197 */
198 if (!capable(CAP_SYS_ADMIN)) {
199 err = -EPERM;
200 break;
201 }
202
203 /*
204 * For now, we don't need to check that the underlying
205 * block device is present or not: we can do that when
206 * the raw device is opened. Just check that the
207 * major/minor numbers make sense.
208 */
209
210 if ((rq.block_major == NODEV &&
211 rq.block_minor != NODEV) ||
212 rq.block_major > MAX_BLKDEV ||
213 rq.block_minor > MINORMASK) {
214 err = -EINVAL;
215 break;
216 }
217
218 down(&raw_devices[minor].mutex);
219 if (raw_devices[minor].inuse) {
220 up(&raw_devices[minor].mutex);
221 err = -EBUSY;
222 break;
223 }
224 if (raw_devices[minor].binding)
225 bdput(raw_devices[minor].binding);
226 raw_devices[minor].binding =
227 bdget(kdev_t_to_nr(MKDEV(rq.block_major, rq.block_minor)));
228 up(&raw_devices[minor].mutex);
229 } else {
230 struct block_device *bdev;
231 kdev_t dev;
232
233 bdev = raw_devices[minor].binding;
234 if (bdev) {
235 dev = to_kdev_t(bdev->bd_dev);
236 rq.block_major = MAJOR(dev);
237 rq.block_minor = MINOR(dev);
238 } else {
239 rq.block_major = rq.block_minor = 0;
240 }
241 err = copy_to_user((void *) arg, &rq, sizeof(rq));
242 }
243 break;
244
245 default:
246 err = -EINVAL;
247 }
248
249 return err;
250 }
251
252
253
254 ssize_t raw_read(struct file *filp, char * buf,
255 size_t size, loff_t *offp)
256 {
257 return rw_raw_dev(READ, filp, buf, size, offp);
258 }
259
260 ssize_t raw_write(struct file *filp, const char *buf,
261 size_t size, loff_t *offp)
262 {
263 return rw_raw_dev(WRITE, filp, (char *) buf, size, offp);
264 }
265
266 #define SECTOR_BITS 9
267 #define SECTOR_SIZE (1U << SECTOR_BITS)
268 #define SECTOR_MASK (SECTOR_SIZE - 1)
269
270 ssize_t rw_raw_dev(int rw, struct file *filp, char *buf,
271 size_t size, loff_t *offp)
272 {
273 struct kiobuf * iobuf;
274 int new_iobuf;
275 int err = 0;
276 unsigned long blocknr, blocks;
277 size_t transferred;
278 int iosize;
279 int i;
280 int minor;
281 kdev_t dev;
282 unsigned long limit;
283
284 int sector_size, sector_bits, sector_mask;
285 int max_sectors;
286
287 /*
288 * First, a few checks on device size limits
289 */
290
291 minor = MINOR(filp->f_dentry->d_inode->i_rdev);
292
293 new_iobuf = 0;
294 iobuf = filp->f_iobuf;
295 if (test_and_set_bit(0, &filp->f_iobuf_lock)) {
296 /*
297 * A parallel read/write is using the preallocated iobuf
298 * so just run slow and allocate a new one.
299 */
300 err = alloc_kiovec(1, &iobuf);
301 if (err)
302 goto out;
303 new_iobuf = 1;
304 }
305
306 dev = to_kdev_t(raw_devices[minor].binding->bd_dev);
307 sector_size = raw_devices[minor].sector_size;
308 sector_bits = raw_devices[minor].sector_bits;
309 sector_mask = sector_size- 1;
310 max_sectors = KIO_MAX_SECTORS >> (sector_bits - 9);
311
312 if (blk_size[MAJOR(dev)])
313 limit = (((loff_t) blk_size[MAJOR(dev)][MINOR(dev)]) << BLOCK_SIZE_BITS) >> sector_bits;
314 else
315 limit = INT_MAX;
316 dprintk ("rw_raw_dev: dev %d:%d (+%d)\n",
317 MAJOR(dev), MINOR(dev), limit);
318
319 err = -EINVAL;
320 if ((*offp & sector_mask) || (size & sector_mask))
321 goto out_free;
322 err = 0;
323 if (size)
324 err = -ENXIO;
325 if ((*offp >> sector_bits) >= limit)
326 goto out_free;
327
328 /*
329 * Split the IO into KIO_MAX_SECTORS chunks, mapping and
330 * unmapping the single kiobuf as we go to perform each chunk of
331 * IO.
332 */
333
334 transferred = 0;
335 blocknr = *offp >> sector_bits;
336 while (size > 0) {
337 blocks = size >> sector_bits;
338 if (blocks > max_sectors)
339 blocks = max_sectors;
340 if (blocks > limit - blocknr)
341 blocks = limit - blocknr;
342 if (!blocks)
343 break;
344
345 iosize = blocks << sector_bits;
346
347 err = map_user_kiobuf(rw, iobuf, (unsigned long) buf, iosize);
348 if (err)
349 break;
350
351 for (i=0; i < blocks; i++)
352 iobuf->blocks[i] = blocknr++;
353
354 err = brw_kiovec(rw, 1, &iobuf, dev, iobuf->blocks, sector_size);
355
356 if (rw == READ && err > 0)
357 mark_dirty_kiobuf(iobuf, err);
358
359 if (err >= 0) {
360 transferred += err;
361 size -= err;
362 buf += err;
363 }
364
365 unmap_kiobuf(iobuf);
366
367 if (err != iosize)
368 break;
369 }
370
371 if (transferred) {
372 *offp += transferred;
373 err = transferred;
374 }
375
376 out_free:
377 if (!new_iobuf)
378 clear_bit(0, &filp->f_iobuf_lock);
379 else
380 free_kiovec(1, &iobuf);
381 out:
382 return err;
383 }
384