2 * linux/drivers/block/loop.c
4 * Written by Theodore Ts'o, 3/29/93
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 * Maximum number of loop devices now dynamic via max_loop module parameter.
26 * Russell Kroll <rkroll@exploits.org> 19990701
28 * Maximum number of loop devices when compiled-in now selectable by passing
29 * max_loop=<1-255> to the kernel on boot.
30 * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
33 * - Advisory locking is ignored here.
34 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
35 * - Should use the underlying filesystems/devices read function if possible
36 * to support read ahead (and for write)
39 * - The block number as IV passing to low level transfer functions is broken:
40 * it passes the underlying device's block number instead of the
41 * offset. This makes it change for a given block when the file is
42 * moved/restored/copied and also doesn't work over NFS.
45 #include <linux/module.h>
47 #include <linux/sched.h>
49 #include <linux/file.h>
50 #include <linux/stat.h>
51 #include <linux/errno.h>
52 #include <linux/major.h>
54 #include <linux/init.h>
56 #include <asm/uaccess.h>
58 #include <linux/loop.h>
60 #define MAJOR_NR LOOP_MAJOR
62 #define DEVICE_NAME "loop"
63 #define DEVICE_REQUEST do_lo_request
64 #define DEVICE_NR(device) (MINOR(device))
65 #define DEVICE_ON(device)
66 #define DEVICE_OFF(device)
67 #define DEVICE_NO_RANDOM
68 #define TIMEOUT_VALUE (6 * HZ)
69 #include <linux/blk.h>
71 #include <linux/malloc.h>
72 static int max_loop
= 8;
73 static struct loop_device
*loop_dev
;
74 static int *loop_sizes
;
75 static int *loop_blksizes
;
80 /* Forward declaration of function to create missing blocks in the
81 backing file (can happen if the backing file is sparse) */
82 static int create_missing_block(struct loop_device
*lo
, int block
, int blksize
);
87 static int transfer_none(struct loop_device
*lo
, int cmd
, char *raw_buf
,
88 char *loop_buf
, int size
, int real_block
)
91 memcpy(loop_buf
, raw_buf
, size
);
93 memcpy(raw_buf
, loop_buf
, size
);
97 static int transfer_xor(struct loop_device
*lo
, int cmd
, char *raw_buf
,
98 char *loop_buf
, int size
, int real_block
)
100 char *in
, *out
, *key
;
110 key
= lo
->lo_encrypt_key
;
111 keysize
= lo
->lo_encrypt_key_size
;
112 for (i
=0; i
< size
; i
++)
113 *out
++ = *in
++ ^ key
[(i
& 511) % keysize
];
117 static int none_status(struct loop_device
*lo
, struct loop_info
*info
)
122 static int xor_status(struct loop_device
*lo
, struct loop_info
*info
)
124 if (info
->lo_encrypt_key_size
<= 0)
129 struct loop_func_table none_funcs
= {
130 number
: LO_CRYPT_NONE
,
131 transfer
: transfer_none
,
135 struct loop_func_table xor_funcs
= {
136 number
: LO_CRYPT_XOR
,
137 transfer
: transfer_xor
,
141 /* xfer_funcs[0] is special - its release function is never called */
142 struct loop_func_table
*xfer_funcs
[MAX_LO_CRYPT
] = {
147 #define MAX_DISK_SIZE 1024*1024*1024
149 static void figure_loop_size(struct loop_device
*lo
)
153 if (S_ISREG(lo
->lo_dentry
->d_inode
->i_mode
))
154 size
= (lo
->lo_dentry
->d_inode
->i_size
- lo
->lo_offset
) >> BLOCK_SIZE_BITS
;
156 kdev_t lodev
= lo
->lo_device
;
157 if (blk_size
[MAJOR(lodev
)])
158 size
= blk_size
[MAJOR(lodev
)][MINOR(lodev
)] -
159 (lo
->lo_offset
>> BLOCK_SIZE_BITS
);
161 size
= MAX_DISK_SIZE
;
164 loop_sizes
[lo
->lo_number
] = size
;
167 static void do_lo_request(void)
169 int real_block
, block
, offset
, len
, blksize
, size
;
171 struct loop_device
*lo
;
172 struct buffer_head
*bh
;
173 struct request
*current_request
;
178 current_request
=CURRENT
;
179 CURRENT
=current_request
->next
;
180 if (MINOR(current_request
->rq_dev
) >= max_loop
)
182 lo
= &loop_dev
[MINOR(current_request
->rq_dev
)];
183 if (!lo
->lo_dentry
|| !lo
->transfer
)
186 blksize
= BLOCK_SIZE
;
187 if (blksize_size
[MAJOR(lo
->lo_device
)]) {
188 blksize
= blksize_size
[MAJOR(lo
->lo_device
)][MINOR(lo
->lo_device
)];
190 blksize
= BLOCK_SIZE
;
193 dest_addr
= current_request
->buffer
;
196 block
= current_request
->sector
* (512/blksize
);
199 block
= current_request
->sector
/ (blksize
>> 9);
200 offset
= (current_request
->sector
% (blksize
>> 9)) << 9;
202 block
+= lo
->lo_offset
/ blksize
;
203 offset
+= lo
->lo_offset
% blksize
;
204 if (offset
> blksize
) {
208 len
= current_request
->current_nr_sectors
<< 9;
210 if (current_request
->cmd
== WRITE
) {
211 if (lo
->lo_flags
& LO_FLAGS_READ_ONLY
)
213 } else if (current_request
->cmd
!= READ
) {
214 printk(KERN_ERR
"unknown loop device command (%d)?!?", current_request
->cmd
);
217 spin_unlock_irq(&io_request_lock
);
220 size
= blksize
- offset
;
225 block_present
= TRUE
;
227 if (lo
->lo_flags
& LO_FLAGS_DO_BMAP
) {
228 real_block
= bmap(lo
->lo_dentry
->d_inode
, block
);
231 /* The backing file is a sparse file and this block
232 doesn't exist. If reading, return zeros. If
233 writing, force the underlying FS to create
235 if (current_request
->cmd
== READ
) {
236 memset(dest_addr
, 0, size
);
237 block_present
= FALSE
;
239 if (!create_missing_block(lo
, block
, blksize
)) {
242 real_block
= bmap(lo
->lo_dentry
->d_inode
, block
);
249 bh
= getblk(lo
->lo_device
, real_block
, blksize
);
251 printk(KERN_ERR
"loop: device %s: getblk(-, %d, %d) returned NULL",
252 kdevname(lo
->lo_device
),
256 if (!buffer_uptodate(bh
) && ((current_request
->cmd
== READ
) ||
257 (offset
|| (len
< blksize
)))) {
258 ll_rw_block(READ
, 1, &bh
);
260 if (!buffer_uptodate(bh
)) {
266 if ((lo
->transfer
)(lo
, current_request
->cmd
, bh
->b_data
+ offset
,
267 dest_addr
, size
, real_block
)) {
268 printk(KERN_ERR
"loop: transfer error block %d\n", block
);
273 if (current_request
->cmd
== WRITE
) {
274 mark_buffer_uptodate(bh
, 1);
275 mark_buffer_dirty(bh
, 1);
284 spin_lock_irq(&io_request_lock
);
285 current_request
->next
=CURRENT
;
286 CURRENT
=current_request
;
290 spin_lock_irq(&io_request_lock
);
292 current_request
->next
=CURRENT
;
293 CURRENT
=current_request
;
298 static int create_missing_block(struct loop_device
*lo
, int block
, int blksize
)
302 char zero_buf
[1] = { 0 };
307 file
= lo
->lo_backing_file
;
309 printk(KERN_WARNING
"loop: cannot create block - no backing file\n");
313 if (file
->f_op
== NULL
) {
314 printk(KERN_WARNING
"loop: cannot create block - no file ops\n");
318 new_offset
= block
* blksize
;
320 if (file
->f_op
->llseek
!= NULL
) {
321 file
->f_op
->llseek(file
, new_offset
, 0);
323 /* Do what the default llseek() code would have done */
324 file
->f_pos
= new_offset
;
326 file
->f_version
= ++event
;
329 if (file
->f_op
->write
== NULL
) {
330 printk(KERN_WARNING
"loop: cannot create block - file not writeable\n");
337 inode
= file
->f_dentry
->d_inode
;
339 retval
= file
->f_op
->write(file
, zero_buf
, 1, &file
->f_pos
);
345 printk(KERN_WARNING
"loop: cannot create block - FS write failed: code %d\n",
353 static int loop_set_fd(struct loop_device
*lo
, kdev_t dev
, unsigned int arg
)
371 inode
= file
->f_dentry
->d_inode
;
373 printk(KERN_ERR
"loop_set_fd: NULL inode?!?\n");
377 if (S_ISBLK(inode
->i_mode
)) {
378 error
= blkdev_open(inode
, file
);
379 lo
->lo_device
= inode
->i_rdev
;
382 /* Backed by a block device - don't need to hold onto
384 lo
->lo_backing_file
= NULL
;
385 } else if (S_ISREG(inode
->i_mode
)) {
386 if (!inode
->i_op
->get_block
) {
387 printk(KERN_ERR
"loop: device has no block access/not implemented\n");
391 /* Backed by a regular file - we need to hold onto
392 a file structure for this file. We'll use it to
393 write to blocks that are not already present in
394 a sparse file. We create a new file structure
395 based on the one passed to us via 'arg'. This is
396 to avoid changing the file structure that the
399 lo
->lo_device
= inode
->i_dev
;
400 lo
->lo_flags
= LO_FLAGS_DO_BMAP
;
403 lo
->lo_backing_file
= get_empty_filp();
404 if (lo
->lo_backing_file
) {
405 lo
->lo_backing_file
->f_mode
= file
->f_mode
;
406 lo
->lo_backing_file
->f_pos
= file
->f_pos
;
407 lo
->lo_backing_file
->f_flags
= file
->f_flags
;
408 lo
->lo_backing_file
->f_owner
= file
->f_owner
;
409 lo
->lo_backing_file
->f_dentry
= file
->f_dentry
;
410 lo
->lo_backing_file
->f_op
= file
->f_op
;
411 lo
->lo_backing_file
->private_data
= file
->private_data
;
412 file_moveto(lo
->lo_backing_file
, file
);
414 error
= get_write_access(inode
);
416 put_filp(lo
->lo_backing_file
);
417 lo
->lo_backing_file
= NULL
;
424 if (IS_RDONLY (inode
) || is_read_only(lo
->lo_device
)) {
425 lo
->lo_flags
|= LO_FLAGS_READ_ONLY
;
426 set_device_ro(dev
, 1);
428 vmtruncate (inode
, 0);
429 set_device_ro(dev
, 0);
432 lo
->lo_dentry
= dget(file
->f_dentry
);
435 figure_loop_size(lo
);
445 static int loop_release_xfer(struct loop_device
*lo
)
448 if (lo
->lo_encrypt_type
) {
449 struct loop_func_table
*xfer
= xfer_funcs
[lo
->lo_encrypt_type
];
450 if (xfer
&& xfer
->release
)
451 err
= xfer
->release(lo
);
452 if (xfer
&& xfer
->unlock
)
454 lo
->lo_encrypt_type
= 0;
459 static int loop_init_xfer(struct loop_device
*lo
, int type
,struct loop_info
*i
)
463 struct loop_func_table
*xfer
= xfer_funcs
[type
];
465 err
= xfer
->init(lo
, i
);
467 lo
->lo_encrypt_type
= type
;
475 static int loop_clr_fd(struct loop_device
*lo
, kdev_t dev
)
477 struct dentry
*dentry
= lo
->lo_dentry
;
481 if (lo
->lo_refcnt
> 1) /* we needed one fd for the ioctl */
484 if (S_ISBLK(dentry
->d_inode
->i_mode
))
485 blkdev_release (dentry
->d_inode
);
486 lo
->lo_dentry
= NULL
;
488 if (lo
->lo_backing_file
!= NULL
) {
489 fput(lo
->lo_backing_file
);
490 lo
->lo_backing_file
= NULL
;
495 loop_release_xfer(lo
);
499 lo
->lo_encrypt_type
= 0;
501 lo
->lo_encrypt_key_size
= 0;
502 memset(lo
->lo_encrypt_key
, 0, LO_KEY_SIZE
);
503 memset(lo
->lo_name
, 0, LO_NAME_SIZE
);
504 loop_sizes
[lo
->lo_number
] = 0;
505 invalidate_buffers(dev
);
510 static int loop_set_status(struct loop_device
*lo
, struct loop_info
*arg
)
512 struct loop_info info
;
516 if (lo
->lo_encrypt_key_size
&& lo
->lo_key_owner
!= current
->uid
&&
517 !capable(CAP_SYS_ADMIN
))
521 if (copy_from_user(&info
, arg
, sizeof (struct loop_info
)))
523 if ((unsigned int) info
.lo_encrypt_key_size
> LO_KEY_SIZE
)
525 type
= info
.lo_encrypt_type
;
526 if (type
>= MAX_LO_CRYPT
|| xfer_funcs
[type
] == NULL
)
528 err
= loop_release_xfer(lo
);
530 err
= loop_init_xfer(lo
, type
, &info
);
534 lo
->lo_offset
= info
.lo_offset
;
535 strncpy(lo
->lo_name
, info
.lo_name
, LO_NAME_SIZE
);
537 lo
->transfer
= xfer_funcs
[type
]->transfer
;
538 lo
->ioctl
= xfer_funcs
[type
]->ioctl
;
539 lo
->lo_encrypt_key_size
= info
.lo_encrypt_key_size
;
540 lo
->lo_init
[0] = info
.lo_init
[0];
541 lo
->lo_init
[1] = info
.lo_init
[1];
542 if (info
.lo_encrypt_key_size
) {
543 memcpy(lo
->lo_encrypt_key
, info
.lo_encrypt_key
,
544 info
.lo_encrypt_key_size
);
545 lo
->lo_key_owner
= current
->uid
;
547 figure_loop_size(lo
);
551 static int loop_get_status(struct loop_device
*lo
, struct loop_info
*arg
)
553 struct loop_info info
;
559 memset(&info
, 0, sizeof(info
));
560 info
.lo_number
= lo
->lo_number
;
561 info
.lo_device
= kdev_t_to_nr(lo
->lo_dentry
->d_inode
->i_dev
);
562 info
.lo_inode
= lo
->lo_dentry
->d_inode
->i_ino
;
563 info
.lo_rdevice
= kdev_t_to_nr(lo
->lo_device
);
564 info
.lo_offset
= lo
->lo_offset
;
565 info
.lo_flags
= lo
->lo_flags
;
566 strncpy(info
.lo_name
, lo
->lo_name
, LO_NAME_SIZE
);
567 info
.lo_encrypt_type
= lo
->lo_encrypt_type
;
568 if (lo
->lo_encrypt_key_size
&& capable(CAP_SYS_ADMIN
)) {
569 info
.lo_encrypt_key_size
= lo
->lo_encrypt_key_size
;
570 memcpy(info
.lo_encrypt_key
, lo
->lo_encrypt_key
,
571 lo
->lo_encrypt_key_size
);
573 return copy_to_user(arg
, &info
, sizeof(info
)) ? -EFAULT
: 0;
576 static int lo_ioctl(struct inode
* inode
, struct file
* file
,
577 unsigned int cmd
, unsigned long arg
)
579 struct loop_device
*lo
;
584 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
585 printk(KERN_WARNING
"lo_ioctl: pseudo-major != %d\n", MAJOR_NR
);
588 dev
= MINOR(inode
->i_rdev
);
594 return loop_set_fd(lo
, inode
->i_rdev
, arg
);
596 return loop_clr_fd(lo
, inode
->i_rdev
);
597 case LOOP_SET_STATUS
:
598 return loop_set_status(lo
, (struct loop_info
*) arg
);
599 case LOOP_GET_STATUS
:
600 return loop_get_status(lo
, (struct loop_info
*) arg
);
601 case BLKGETSIZE
: /* Return device size */
606 return put_user(loop_sizes
[lo
->lo_number
] << 1, (long *) arg
);
608 return lo
->ioctl
? lo
->ioctl(lo
, cmd
, arg
) : -EINVAL
;
613 static int lo_open(struct inode
*inode
, struct file
*file
)
615 struct loop_device
*lo
;
621 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
622 printk(KERN_WARNING
"lo_open: pseudo-major != %d\n", MAJOR_NR
);
625 dev
= MINOR(inode
->i_rdev
);
626 if (dev
>= max_loop
) {
631 type
= lo
->lo_encrypt_type
;
632 if (type
&& xfer_funcs
[type
] && xfer_funcs
[type
]->lock
)
633 xfer_funcs
[type
]->lock(lo
);
639 static int lo_release(struct inode
*inode
, struct file
*file
)
641 struct loop_device
*lo
;
646 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
647 printk(KERN_WARNING
"lo_release: pseudo-major != %d\n", MAJOR_NR
);
650 dev
= MINOR(inode
->i_rdev
);
653 err
= fsync_dev(inode
->i_rdev
);
655 if (lo
->lo_refcnt
<= 0)
656 printk(KERN_ERR
"lo_release: refcount(%d) <= 0\n", lo
->lo_refcnt
);
658 int type
= lo
->lo_encrypt_type
;
660 if (xfer_funcs
[type
] && xfer_funcs
[type
]->unlock
)
661 xfer_funcs
[type
]->unlock(lo
);
667 static struct file_operations lo_fops
= {
668 NULL
, /* lseek - default */
669 block_read
, /* read - general block-dev read */
670 block_write
, /* write - general block-dev write */
671 NULL
, /* readdir - bad */
673 lo_ioctl
, /* ioctl */
677 lo_release
/* release */
681 * And now the modules code and kernel interface.
684 #define loop_init init_module
685 MODULE_PARM(max_loop
, "i");
686 MODULE_PARM_DESC(max_loop
, "Maximum number of loop devices (1-255)");
689 int loop_register_transfer(struct loop_func_table
*funcs
)
691 if ((unsigned)funcs
->number
> MAX_LO_CRYPT
|| xfer_funcs
[funcs
->number
])
693 xfer_funcs
[funcs
->number
] = funcs
;
697 int loop_unregister_transfer(int number
)
699 struct loop_device
*lo
;
701 if ((unsigned)number
>= MAX_LO_CRYPT
)
703 for (lo
= &loop_dev
[0]; lo
< &loop_dev
[max_loop
]; lo
++) {
704 int type
= lo
->lo_encrypt_type
;
705 if (type
== number
) {
706 xfer_funcs
[type
]->release(lo
);
708 lo
->lo_encrypt_type
= 0;
711 xfer_funcs
[number
] = NULL
;
715 EXPORT_SYMBOL(loop_register_transfer
);
716 EXPORT_SYMBOL(loop_unregister_transfer
);
718 int __init
loop_init(void)
722 if (register_blkdev(MAJOR_NR
, "loop", &lo_fops
)) {
723 printk(KERN_WARNING
"Unable to get major number %d for loop device\n",
728 if ((max_loop
< 1) || (max_loop
> 255)) {
729 printk (KERN_WARNING
"loop: invalid max_loop (must be between 1 and 255), using default (8)\n");
733 printk(KERN_INFO
"loop: registered device at major %d\n", MAJOR_NR
);
734 printk(KERN_INFO
"loop: enabling %d loop devices\n", max_loop
);
736 loop_dev
= kmalloc (max_loop
* sizeof(struct loop_device
), GFP_KERNEL
);
738 printk (KERN_ERR
"loop: Unable to create loop_dev\n");
742 loop_sizes
= kmalloc(max_loop
* sizeof(int), GFP_KERNEL
);
744 printk (KERN_ERR
"loop: Unable to create loop_sizes\n");
749 loop_blksizes
= kmalloc (max_loop
* sizeof(int), GFP_KERNEL
);
750 if (!loop_blksizes
) {
751 printk (KERN_ERR
"loop: Unable to create loop_blksizes\n");
757 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
758 for (i
=0; i
< max_loop
; i
++) {
759 memset(&loop_dev
[i
], 0, sizeof(struct loop_device
));
760 loop_dev
[i
].lo_number
= i
;
762 memset(loop_sizes
, 0, max_loop
* sizeof(int));
763 memset(loop_blksizes
, 0, max_loop
* sizeof(int));
764 blk_size
[MAJOR_NR
] = loop_sizes
;
765 blksize_size
[MAJOR_NR
] = loop_blksizes
;
771 void cleanup_module(void)
773 if (unregister_blkdev(MAJOR_NR
, "loop") != 0)
774 printk(KERN_WARNING
"loop: cannot unregister blkdev\n");
778 kfree (loop_blksizes
);
783 static int __init
max_loop_setup(char *str
)
785 max_loop
= simple_strtol(str
,NULL
,0);
789 __setup("max_loop=", max_loop_setup
);