Import 2.3.9pre5
[davej-history.git] / drivers / block / loop.c
blob26ffb26cac3da1a72a13d98d4b8e704d645de509
1 /*
2 * linux/drivers/block/loop.c
4 * Written by Theodore Ts'o, 3/29/93
5 *
6 * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
7 * permitted under the GNU Public License.
9 * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
10 * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
12 * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
13 * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
15 * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
17 * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
19 * Loadable modules and other fixes by AK, 1998
21 * Make real block number available to downstream transfer functions, enables
22 * CBC (and relatives) mode encryption requiring unique IVs per data block.
23 * Reed H. Petty, rhp@draper.net
25 * Still To Fix:
26 * - Advisory locking is ignored here.
27 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
28 * - Should use the underlying filesystems/devices read function if possible
29 * to support read ahead (and for write)
32 #include <linux/module.h>
34 #include <linux/sched.h>
35 #include <linux/fs.h>
36 #include <linux/file.h>
37 #include <linux/stat.h>
38 #include <linux/errno.h>
39 #include <linux/major.h>
41 #include <linux/init.h>
43 #include <asm/uaccess.h>
45 #include <linux/loop.h>
47 #define MAJOR_NR LOOP_MAJOR
49 #define DEVICE_NAME "loop"
50 #define DEVICE_REQUEST do_lo_request
51 #define DEVICE_NR(device) (MINOR(device))
52 #define DEVICE_ON(device)
53 #define DEVICE_OFF(device)
54 #define DEVICE_NO_RANDOM
55 #define TIMEOUT_VALUE (6 * HZ)
56 #include <linux/blk.h>
58 #define MAX_LOOP 8
59 static struct loop_device loop_dev[MAX_LOOP];
60 static int loop_sizes[MAX_LOOP];
61 static int loop_blksizes[MAX_LOOP];
63 #define FALSE 0
64 #define TRUE (!FALSE)
66 /* Forward declaration of function to create missing blocks in the
67 backing file (can happen if the backing file is sparse) */
68 static int create_missing_block(struct loop_device *lo, int block, int blksize);
71 * Transfer functions
73 static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf,
74 char *loop_buf, int size, int real_block)
76 if (cmd == READ)
77 memcpy(loop_buf, raw_buf, size);
78 else
79 memcpy(raw_buf, loop_buf, size);
80 return 0;
83 static int transfer_xor(struct loop_device *lo, int cmd, char *raw_buf,
84 char *loop_buf, int size, int real_block)
86 char *in, *out, *key;
87 int i, keysize;
89 if (cmd == READ) {
90 in = raw_buf;
91 out = loop_buf;
92 } else {
93 in = loop_buf;
94 out = raw_buf;
96 key = lo->lo_encrypt_key;
97 keysize = lo->lo_encrypt_key_size;
98 for (i=0; i < size; i++)
99 *out++ = *in++ ^ key[(i & 511) % keysize];
100 return 0;
103 static int none_status(struct loop_device *lo, struct loop_info *info)
105 return 0;
108 static int xor_status(struct loop_device *lo, struct loop_info *info)
110 if (info->lo_encrypt_key_size < 0)
111 return -EINVAL;
112 return 0;
115 struct loop_func_table none_funcs = {
116 number: LO_CRYPT_NONE,
117 transfer: transfer_none,
118 init: none_status
121 struct loop_func_table xor_funcs = {
122 number: LO_CRYPT_XOR,
123 transfer: transfer_xor,
124 init: xor_status
127 /* xfer_funcs[0] is special - its release function is never called */
128 struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
129 &none_funcs,
130 &xor_funcs
133 #define MAX_DISK_SIZE 1024*1024*1024
135 static void figure_loop_size(struct loop_device *lo)
137 int size;
139 if (S_ISREG(lo->lo_dentry->d_inode->i_mode))
140 size = (lo->lo_dentry->d_inode->i_size - lo->lo_offset) / BLOCK_SIZE;
141 else {
142 kdev_t lodev = lo->lo_device;
143 if (blk_size[MAJOR(lodev)])
144 size = blk_size[MAJOR(lodev)][MINOR(lodev)] -
145 lo->lo_offset / BLOCK_SIZE;
146 else
147 size = MAX_DISK_SIZE;
150 loop_sizes[lo->lo_number] = size;
153 static void do_lo_request(void)
155 int real_block, block, offset, len, blksize, size;
156 char *dest_addr;
157 struct loop_device *lo;
158 struct buffer_head *bh;
159 struct request *current_request;
160 int block_present;
162 repeat:
163 INIT_REQUEST;
164 current_request=CURRENT;
165 CURRENT=current_request->next;
166 if (MINOR(current_request->rq_dev) >= MAX_LOOP)
167 goto error_out;
168 lo = &loop_dev[MINOR(current_request->rq_dev)];
169 if (!lo->lo_dentry || !lo->transfer)
170 goto error_out;
172 blksize = BLOCK_SIZE;
173 if (blksize_size[MAJOR(lo->lo_device)]) {
174 blksize = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)];
175 if (!blksize)
176 blksize = BLOCK_SIZE;
179 dest_addr = current_request->buffer;
181 if (blksize < 512) {
182 block = current_request->sector * (512/blksize);
183 offset = 0;
184 } else {
185 block = current_request->sector / (blksize >> 9);
186 offset = (current_request->sector % (blksize >> 9)) << 9;
188 block += lo->lo_offset / blksize;
189 offset += lo->lo_offset % blksize;
190 if (offset > blksize) {
191 block++;
192 offset -= blksize;
194 len = current_request->current_nr_sectors << 9;
196 if (current_request->cmd == WRITE) {
197 if (lo->lo_flags & LO_FLAGS_READ_ONLY)
198 goto error_out;
199 } else if (current_request->cmd != READ) {
200 printk(KERN_ERR "unknown loop device command (%d)?!?", current_request->cmd);
201 goto error_out;
203 spin_unlock_irq(&io_request_lock);
204 while (len > 0) {
206 size = blksize - offset;
207 if (size > len)
208 size = len;
210 real_block = block;
211 block_present = TRUE;
213 if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
214 real_block = bmap(lo->lo_dentry->d_inode, block);
215 if (!real_block) {
217 /* The backing file is a sparse file and this block
218 doesn't exist. If reading, return zeros. If
219 writing, force the underlying FS to create
220 the block */
221 if (current_request->cmd == READ) {
222 memset(dest_addr, 0, size);
223 block_present = FALSE;
224 } else {
225 if (!create_missing_block(lo, block, blksize)) {
226 goto error_out_lock;
228 real_block = bmap(lo->lo_dentry->d_inode, block);
234 if (block_present) {
235 bh = getblk(lo->lo_device, real_block, blksize);
236 if (!bh) {
237 printk(KERN_ERR "loop: device %s: getblk(-, %d, %d) returned NULL",
238 kdevname(lo->lo_device),
239 block, blksize);
240 goto error_out_lock;
242 if (!buffer_uptodate(bh) && ((current_request->cmd == READ) ||
243 (offset || (len < blksize)))) {
244 ll_rw_block(READ, 1, &bh);
245 wait_on_buffer(bh);
246 if (!buffer_uptodate(bh)) {
247 brelse(bh);
248 goto error_out_lock;
252 if ((lo->transfer)(lo, current_request->cmd, bh->b_data + offset,
253 dest_addr, size, real_block)) {
254 printk(KERN_ERR "loop: transfer error block %d\n", block);
255 brelse(bh);
256 goto error_out_lock;
259 if (current_request->cmd == WRITE) {
260 mark_buffer_uptodate(bh, 1);
261 mark_buffer_dirty(bh, 1);
263 brelse(bh);
265 dest_addr += size;
266 len -= size;
267 offset = 0;
268 block++;
270 spin_lock_irq(&io_request_lock);
271 current_request->next=CURRENT;
272 CURRENT=current_request;
273 end_request(1);
274 goto repeat;
275 error_out_lock:
276 spin_lock_irq(&io_request_lock);
277 error_out:
278 current_request->next=CURRENT;
279 CURRENT=current_request;
280 end_request(0);
281 goto repeat;
284 static int create_missing_block(struct loop_device *lo, int block, int blksize)
286 struct file *file;
287 loff_t new_offset;
288 char zero_buf[1] = { 0 };
289 ssize_t retval;
290 mm_segment_t old_fs;
291 struct inode *inode;
293 file = lo->lo_backing_file;
294 if (file == NULL) {
295 printk(KERN_WARNING "loop: cannot create block - no backing file\n");
296 return FALSE;
299 if (file->f_op == NULL) {
300 printk(KERN_WARNING "loop: cannot create block - no file ops\n");
301 return FALSE;
304 new_offset = block * blksize;
306 if (file->f_op->llseek != NULL) {
307 file->f_op->llseek(file, new_offset, 0);
308 } else {
309 /* Do what the default llseek() code would have done */
310 file->f_pos = new_offset;
311 file->f_reada = 0;
312 file->f_version = ++event;
315 if (file->f_op->write == NULL) {
316 printk(KERN_WARNING "loop: cannot create block - file not writeable\n");
317 return FALSE;
320 old_fs = get_fs();
321 set_fs(get_ds());
323 inode = file->f_dentry->d_inode;
324 down(&inode->i_sem);
325 retval = file->f_op->write(file, zero_buf, 1, &file->f_pos);
326 up(&inode->i_sem);
328 set_fs(old_fs);
330 if (retval < 0) {
331 printk(KERN_WARNING "loop: cannot create block - FS write failed: code %d\n",
332 retval);
333 return FALSE;
334 } else {
335 return TRUE;
339 static int loop_set_fd(struct loop_device *lo, kdev_t dev, unsigned int arg)
341 struct file *file;
342 struct inode *inode;
343 int error;
345 MOD_INC_USE_COUNT;
347 error = -EBUSY;
348 if (lo->lo_dentry)
349 goto out;
351 error = -EBADF;
352 file = fget(arg);
353 if (!file)
354 goto out;
356 error = -EINVAL;
357 inode = file->f_dentry->d_inode;
358 if (!inode) {
359 printk(KERN_ERR "loop_set_fd: NULL inode?!?\n");
360 goto out_putf;
363 if (S_ISBLK(inode->i_mode)) {
364 error = blkdev_open(inode, file);
365 lo->lo_device = inode->i_rdev;
366 lo->lo_flags = 0;
368 /* Backed by a block device - don't need to hold onto
369 a file structure */
370 lo->lo_backing_file = NULL;
371 } else if (S_ISREG(inode->i_mode)) {
373 /* Backed by a regular file - we need to hold onto
374 a file structure for this file. We'll use it to
375 write to blocks that are not already present in
376 a sparse file. We create a new file structure
377 based on the one passed to us via 'arg'. This is
378 to avoid changing the file structure that the
379 caller is using */
381 lo->lo_device = inode->i_dev;
382 lo->lo_flags = LO_FLAGS_DO_BMAP;
384 error = -ENFILE;
385 lo->lo_backing_file = get_empty_filp();
386 if (lo->lo_backing_file) {
387 lo->lo_backing_file->f_mode = file->f_mode;
388 lo->lo_backing_file->f_pos = file->f_pos;
389 lo->lo_backing_file->f_flags = file->f_flags;
390 lo->lo_backing_file->f_owner = file->f_owner;
391 lo->lo_backing_file->f_dentry = file->f_dentry;
392 lo->lo_backing_file->f_op = file->f_op;
393 lo->lo_backing_file->private_data = file->private_data;
395 error = get_write_access(inode);
396 if (error) {
397 put_filp(lo->lo_backing_file);
398 lo->lo_backing_file = NULL;
402 if (error)
403 goto out_putf;
405 if (IS_RDONLY (inode) || is_read_only(lo->lo_device)) {
406 lo->lo_flags |= LO_FLAGS_READ_ONLY;
407 set_device_ro(dev, 1);
408 } else {
409 invalidate_inode_pages (inode);
410 set_device_ro(dev, 0);
413 lo->lo_dentry = dget(file->f_dentry);
414 lo->transfer = NULL;
415 lo->ioctl = NULL;
416 figure_loop_size(lo);
418 out_putf:
419 fput(file);
420 out:
421 if (error)
422 MOD_DEC_USE_COUNT;
423 return error;
426 static int loop_release_xfer(struct loop_device *lo)
428 int err = 0;
429 if (lo->lo_encrypt_type) {
430 struct loop_func_table *xfer= xfer_funcs[lo->lo_encrypt_type];
431 if (xfer && xfer->release)
432 err = xfer->release(lo);
433 if (xfer && xfer->unlock)
434 xfer->unlock(lo);
435 lo->lo_encrypt_type = 0;
437 return err;
440 static int loop_init_xfer(struct loop_device *lo, int type,struct loop_info *i)
442 int err = 0;
443 if (type) {
444 struct loop_func_table *xfer = xfer_funcs[type];
445 if (xfer->init)
446 err = xfer->init(lo, i);
447 if (!err) {
448 lo->lo_encrypt_type = type;
449 if (xfer->lock)
450 xfer->lock(lo);
453 return err;
456 static int loop_clr_fd(struct loop_device *lo, kdev_t dev)
458 struct dentry *dentry = lo->lo_dentry;
460 if (!dentry)
461 return -ENXIO;
462 if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
463 return -EBUSY;
465 if (S_ISBLK(dentry->d_inode->i_mode))
466 blkdev_release (dentry->d_inode);
467 lo->lo_dentry = NULL;
469 if (lo->lo_backing_file != NULL) {
470 fput(lo->lo_backing_file);
471 lo->lo_backing_file = NULL;
472 } else {
473 dput(dentry);
476 loop_release_xfer(lo);
477 lo->transfer = NULL;
478 lo->ioctl = NULL;
479 lo->lo_device = 0;
480 lo->lo_encrypt_type = 0;
481 lo->lo_offset = 0;
482 lo->lo_encrypt_key_size = 0;
483 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
484 memset(lo->lo_name, 0, LO_NAME_SIZE);
485 loop_sizes[lo->lo_number] = 0;
486 invalidate_buffers(dev);
487 MOD_DEC_USE_COUNT;
488 return 0;
491 static int loop_set_status(struct loop_device *lo, struct loop_info *arg)
493 struct loop_info info;
494 int err;
495 unsigned int type;
497 if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid &&
498 !capable(CAP_SYS_ADMIN))
499 return -EPERM;
500 if (!lo->lo_dentry)
501 return -ENXIO;
502 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
503 return -EFAULT;
504 if ((unsigned int) info.lo_encrypt_key_size > LO_KEY_SIZE)
505 return -EINVAL;
506 type = info.lo_encrypt_type;
507 if (info.lo_encrypt_key_size == 0 && type == LO_CRYPT_XOR)
508 return -EINVAL;
509 if (type >= MAX_LO_CRYPT || xfer_funcs[type] == NULL)
510 return -EINVAL;
511 err = loop_release_xfer(lo);
512 if (!err)
513 err = loop_init_xfer(lo, type, &info);
514 if (err)
515 return err;
517 lo->lo_offset = info.lo_offset;
518 strncpy(lo->lo_name, info.lo_name, LO_NAME_SIZE);
520 lo->transfer = xfer_funcs[type]->transfer;
521 lo->ioctl = xfer_funcs[type]->ioctl;
522 lo->lo_encrypt_key_size = info.lo_encrypt_key_size;
523 lo->lo_init[0] = info.lo_init[0];
524 lo->lo_init[1] = info.lo_init[1];
525 if (info.lo_encrypt_key_size) {
526 memcpy(lo->lo_encrypt_key, info.lo_encrypt_key,
527 info.lo_encrypt_key_size);
528 lo->lo_key_owner = current->uid;
530 figure_loop_size(lo);
531 return 0;
534 static int loop_get_status(struct loop_device *lo, struct loop_info *arg)
536 struct loop_info info;
538 if (!lo->lo_dentry)
539 return -ENXIO;
540 if (!arg)
541 return -EINVAL;
542 memset(&info, 0, sizeof(info));
543 info.lo_number = lo->lo_number;
544 info.lo_device = kdev_t_to_nr(lo->lo_dentry->d_inode->i_dev);
545 info.lo_inode = lo->lo_dentry->d_inode->i_ino;
546 info.lo_rdevice = kdev_t_to_nr(lo->lo_device);
547 info.lo_offset = lo->lo_offset;
548 info.lo_flags = lo->lo_flags;
549 strncpy(info.lo_name, lo->lo_name, LO_NAME_SIZE);
550 info.lo_encrypt_type = lo->lo_encrypt_type;
551 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
552 info.lo_encrypt_key_size = lo->lo_encrypt_key_size;
553 memcpy(info.lo_encrypt_key, lo->lo_encrypt_key,
554 lo->lo_encrypt_key_size);
556 return copy_to_user(arg, &info, sizeof(info)) ? -EFAULT : 0;
559 static int lo_ioctl(struct inode * inode, struct file * file,
560 unsigned int cmd, unsigned long arg)
562 struct loop_device *lo;
563 int dev;
565 if (!inode)
566 return -EINVAL;
567 if (MAJOR(inode->i_rdev) != MAJOR_NR) {
568 printk(KERN_WARNING "lo_ioctl: pseudo-major != %d\n", MAJOR_NR);
569 return -ENODEV;
571 dev = MINOR(inode->i_rdev);
572 if (dev >= MAX_LOOP)
573 return -ENODEV;
574 lo = &loop_dev[dev];
575 switch (cmd) {
576 case LOOP_SET_FD:
577 return loop_set_fd(lo, inode->i_rdev, arg);
578 case LOOP_CLR_FD:
579 return loop_clr_fd(lo, inode->i_rdev);
580 case LOOP_SET_STATUS:
581 return loop_set_status(lo, (struct loop_info *) arg);
582 case LOOP_GET_STATUS:
583 return loop_get_status(lo, (struct loop_info *) arg);
584 case BLKGETSIZE: /* Return device size */
585 if (!lo->lo_dentry)
586 return -ENXIO;
587 if (!arg)
588 return -EINVAL;
589 return put_user(loop_sizes[lo->lo_number] << 1, (long *) arg);
590 default:
591 return lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
593 return 0;
596 static int lo_open(struct inode *inode, struct file *file)
598 struct loop_device *lo;
599 int dev, type;
602 if (!inode)
603 return -EINVAL;
604 if (MAJOR(inode->i_rdev) != MAJOR_NR) {
605 printk(KERN_WARNING "lo_open: pseudo-major != %d\n", MAJOR_NR);
606 return -ENODEV;
608 dev = MINOR(inode->i_rdev);
609 if (dev >= MAX_LOOP) {
610 return -ENODEV;
612 lo = &loop_dev[dev];
614 type = lo->lo_encrypt_type;
615 if (type && xfer_funcs[type] && xfer_funcs[type]->lock)
616 xfer_funcs[type]->lock(lo);
617 lo->lo_refcnt++;
618 MOD_INC_USE_COUNT;
619 return 0;
622 static int lo_release(struct inode *inode, struct file *file)
624 struct loop_device *lo;
625 int dev, err;
627 if (!inode)
628 return 0;
629 if (MAJOR(inode->i_rdev) != MAJOR_NR) {
630 printk(KERN_WARNING "lo_release: pseudo-major != %d\n", MAJOR_NR);
631 return 0;
633 dev = MINOR(inode->i_rdev);
634 if (dev >= MAX_LOOP)
635 return 0;
636 err = fsync_dev(inode->i_rdev);
637 lo = &loop_dev[dev];
638 if (lo->lo_refcnt <= 0)
639 printk(KERN_ERR "lo_release: refcount(%d) <= 0\n", lo->lo_refcnt);
640 else {
641 int type = lo->lo_encrypt_type;
642 --lo->lo_refcnt;
643 if (xfer_funcs[type] && xfer_funcs[type]->unlock)
644 xfer_funcs[type]->unlock(lo);
645 MOD_DEC_USE_COUNT;
647 return err;
650 static struct file_operations lo_fops = {
651 NULL, /* lseek - default */
652 block_read, /* read - general block-dev read */
653 block_write, /* write - general block-dev write */
654 NULL, /* readdir - bad */
655 NULL, /* poll */
656 lo_ioctl, /* ioctl */
657 NULL, /* mmap */
658 lo_open, /* open */
659 NULL, /* flush */
660 lo_release /* release */
664 * And now the modules code and kernel interface.
666 #ifdef MODULE
667 #define loop_init init_module
668 #endif
670 int loop_register_transfer(struct loop_func_table *funcs)
672 if ((unsigned)funcs->number > MAX_LO_CRYPT || xfer_funcs[funcs->number])
673 return -EINVAL;
674 xfer_funcs[funcs->number] = funcs;
675 return 0;
678 int loop_unregister_transfer(int number)
680 struct loop_device *lo;
682 if ((unsigned)number >= MAX_LO_CRYPT)
683 return -EINVAL;
684 for (lo = &loop_dev[0]; lo < &loop_dev[MAX_LOOP]; lo++) {
685 int type = lo->lo_encrypt_type;
686 if (type == number) {
687 xfer_funcs[type]->release(lo);
688 lo->transfer = NULL;
689 lo->lo_encrypt_type = 0;
692 xfer_funcs[number] = NULL;
693 return 0;
696 EXPORT_SYMBOL(loop_register_transfer);
697 EXPORT_SYMBOL(loop_unregister_transfer);
699 int __init loop_init(void)
701 int i;
703 if (register_blkdev(MAJOR_NR, "loop", &lo_fops)) {
704 printk(KERN_WARNING "Unable to get major number %d for loop device\n",
705 MAJOR_NR);
706 return -EIO;
708 #ifndef MODULE
709 printk(KERN_INFO "loop: registered device at major %d\n", MAJOR_NR);
710 #endif
712 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
713 for (i=0; i < MAX_LOOP; i++) {
714 memset(&loop_dev[i], 0, sizeof(struct loop_device));
715 loop_dev[i].lo_number = i;
717 memset(&loop_sizes, 0, sizeof(loop_sizes));
718 memset(&loop_blksizes, 0, sizeof(loop_blksizes));
719 blk_size[MAJOR_NR] = loop_sizes;
720 blksize_size[MAJOR_NR] = loop_blksizes;
722 return 0;
725 #ifdef MODULE
726 void cleanup_module(void)
728 if (unregister_blkdev(MAJOR_NR, "loop") != 0)
729 printk(KERN_WARNING "loop: cannot unregister blkdev\n");
731 #endif