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 * Still fixed at 8 devices when compiled into the kernel normally.
27 * Russell Kroll <rkroll@exploits.org> 19990701
30 * - Advisory locking is ignored here.
31 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
32 * - Should use the underlying filesystems/devices read function if possible
33 * to support read ahead (and for write)
36 * - The block number as IV passing to low level transfer functions is broken:
37 * it passes the underlying device's block number instead of the
38 * offset. This makes it change for a given block when the file is
39 * moved/restored/copied and also doesn't work over NFS.
42 #include <linux/module.h>
44 #include <linux/sched.h>
46 #include <linux/file.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #include <linux/major.h>
51 #include <linux/init.h>
53 #include <asm/uaccess.h>
55 #include <linux/loop.h>
57 #define MAJOR_NR LOOP_MAJOR
59 #define DEVICE_NAME "loop"
60 #define DEVICE_REQUEST do_lo_request
61 #define DEVICE_NR(device) (MINOR(device))
62 #define DEVICE_ON(device)
63 #define DEVICE_OFF(device)
64 #define DEVICE_NO_RANDOM
65 #define TIMEOUT_VALUE (6 * HZ)
66 #include <linux/blk.h>
68 #include <linux/malloc.h>
69 static int max_loop
= 8;
70 static struct loop_device
*loop_dev
;
71 static int *loop_sizes
;
72 static int *loop_blksizes
;
77 /* Forward declaration of function to create missing blocks in the
78 backing file (can happen if the backing file is sparse) */
79 static int create_missing_block(struct loop_device
*lo
, int block
, int blksize
);
84 static int transfer_none(struct loop_device
*lo
, int cmd
, char *raw_buf
,
85 char *loop_buf
, int size
, int real_block
)
88 memcpy(loop_buf
, raw_buf
, size
);
90 memcpy(raw_buf
, loop_buf
, size
);
94 static int transfer_xor(struct loop_device
*lo
, int cmd
, char *raw_buf
,
95 char *loop_buf
, int size
, int real_block
)
107 key
= lo
->lo_encrypt_key
;
108 keysize
= lo
->lo_encrypt_key_size
;
109 for (i
=0; i
< size
; i
++)
110 *out
++ = *in
++ ^ key
[(i
& 511) % keysize
];
114 static int none_status(struct loop_device
*lo
, struct loop_info
*info
)
119 static int xor_status(struct loop_device
*lo
, struct loop_info
*info
)
121 if (info
->lo_encrypt_key_size
<= 0)
126 struct loop_func_table none_funcs
= {
127 number
: LO_CRYPT_NONE
,
128 transfer
: transfer_none
,
132 struct loop_func_table xor_funcs
= {
133 number
: LO_CRYPT_XOR
,
134 transfer
: transfer_xor
,
138 /* xfer_funcs[0] is special - its release function is never called */
139 struct loop_func_table
*xfer_funcs
[MAX_LO_CRYPT
] = {
144 #define MAX_DISK_SIZE 1024*1024*1024
146 static void figure_loop_size(struct loop_device
*lo
)
150 if (S_ISREG(lo
->lo_dentry
->d_inode
->i_mode
))
151 size
= (lo
->lo_dentry
->d_inode
->i_size
- lo
->lo_offset
) / BLOCK_SIZE
;
153 kdev_t lodev
= lo
->lo_device
;
154 if (blk_size
[MAJOR(lodev
)])
155 size
= blk_size
[MAJOR(lodev
)][MINOR(lodev
)] -
156 lo
->lo_offset
/ BLOCK_SIZE
;
158 size
= MAX_DISK_SIZE
;
161 loop_sizes
[lo
->lo_number
] = size
;
164 static void do_lo_request(void)
166 int real_block
, block
, offset
, len
, blksize
, size
;
168 struct loop_device
*lo
;
169 struct buffer_head
*bh
;
170 struct request
*current_request
;
175 current_request
=CURRENT
;
176 CURRENT
=current_request
->next
;
177 if (MINOR(current_request
->rq_dev
) >= max_loop
)
179 lo
= &loop_dev
[MINOR(current_request
->rq_dev
)];
180 if (!lo
->lo_dentry
|| !lo
->transfer
)
183 blksize
= BLOCK_SIZE
;
184 if (blksize_size
[MAJOR(lo
->lo_device
)]) {
185 blksize
= blksize_size
[MAJOR(lo
->lo_device
)][MINOR(lo
->lo_device
)];
187 blksize
= BLOCK_SIZE
;
190 dest_addr
= current_request
->buffer
;
193 block
= current_request
->sector
* (512/blksize
);
196 block
= current_request
->sector
/ (blksize
>> 9);
197 offset
= (current_request
->sector
% (blksize
>> 9)) << 9;
199 block
+= lo
->lo_offset
/ blksize
;
200 offset
+= lo
->lo_offset
% blksize
;
201 if (offset
> blksize
) {
205 len
= current_request
->current_nr_sectors
<< 9;
207 if (current_request
->cmd
== WRITE
) {
208 if (lo
->lo_flags
& LO_FLAGS_READ_ONLY
)
210 } else if (current_request
->cmd
!= READ
) {
211 printk(KERN_ERR
"unknown loop device command (%d)?!?", current_request
->cmd
);
214 spin_unlock_irq(&io_request_lock
);
217 size
= blksize
- offset
;
222 block_present
= TRUE
;
224 if (lo
->lo_flags
& LO_FLAGS_DO_BMAP
) {
225 real_block
= bmap(lo
->lo_dentry
->d_inode
, block
);
228 /* The backing file is a sparse file and this block
229 doesn't exist. If reading, return zeros. If
230 writing, force the underlying FS to create
232 if (current_request
->cmd
== READ
) {
233 memset(dest_addr
, 0, size
);
234 block_present
= FALSE
;
236 if (!create_missing_block(lo
, block
, blksize
)) {
239 real_block
= bmap(lo
->lo_dentry
->d_inode
, block
);
246 bh
= getblk(lo
->lo_device
, real_block
, blksize
);
248 printk(KERN_ERR
"loop: device %s: getblk(-, %d, %d) returned NULL",
249 kdevname(lo
->lo_device
),
253 if (!buffer_uptodate(bh
) && ((current_request
->cmd
== READ
) ||
254 (offset
|| (len
< blksize
)))) {
255 ll_rw_block(READ
, 1, &bh
);
257 if (!buffer_uptodate(bh
)) {
263 if ((lo
->transfer
)(lo
, current_request
->cmd
, bh
->b_data
+ offset
,
264 dest_addr
, size
, real_block
)) {
265 printk(KERN_ERR
"loop: transfer error block %d\n", block
);
270 if (current_request
->cmd
== WRITE
) {
271 mark_buffer_uptodate(bh
, 1);
272 mark_buffer_dirty(bh
, 1);
281 spin_lock_irq(&io_request_lock
);
282 current_request
->next
=CURRENT
;
283 CURRENT
=current_request
;
287 spin_lock_irq(&io_request_lock
);
289 current_request
->next
=CURRENT
;
290 CURRENT
=current_request
;
295 static int create_missing_block(struct loop_device
*lo
, int block
, int blksize
)
299 char zero_buf
[1] = { 0 };
304 file
= lo
->lo_backing_file
;
306 printk(KERN_WARNING
"loop: cannot create block - no backing file\n");
310 if (file
->f_op
== NULL
) {
311 printk(KERN_WARNING
"loop: cannot create block - no file ops\n");
315 new_offset
= block
* blksize
;
317 if (file
->f_op
->llseek
!= NULL
) {
318 file
->f_op
->llseek(file
, new_offset
, 0);
320 /* Do what the default llseek() code would have done */
321 file
->f_pos
= new_offset
;
323 file
->f_version
= ++event
;
326 if (file
->f_op
->write
== NULL
) {
327 printk(KERN_WARNING
"loop: cannot create block - file not writeable\n");
334 inode
= file
->f_dentry
->d_inode
;
336 retval
= file
->f_op
->write(file
, zero_buf
, 1, &file
->f_pos
);
342 printk(KERN_WARNING
"loop: cannot create block - FS write failed: code %d\n",
350 static int loop_set_fd(struct loop_device
*lo
, kdev_t dev
, unsigned int arg
)
368 inode
= file
->f_dentry
->d_inode
;
370 printk(KERN_ERR
"loop_set_fd: NULL inode?!?\n");
374 if (S_ISBLK(inode
->i_mode
)) {
375 error
= blkdev_open(inode
, file
);
376 lo
->lo_device
= inode
->i_rdev
;
379 /* Backed by a block device - don't need to hold onto
381 lo
->lo_backing_file
= NULL
;
382 } else if (S_ISREG(inode
->i_mode
)) {
383 if (!inode
->i_op
->get_block
) {
384 printk(KERN_ERR
"loop: device has no block access/not implemented\n");
388 /* Backed by a regular file - we need to hold onto
389 a file structure for this file. We'll use it to
390 write to blocks that are not already present in
391 a sparse file. We create a new file structure
392 based on the one passed to us via 'arg'. This is
393 to avoid changing the file structure that the
396 lo
->lo_device
= inode
->i_dev
;
397 lo
->lo_flags
= LO_FLAGS_DO_BMAP
;
400 lo
->lo_backing_file
= get_empty_filp();
401 if (lo
->lo_backing_file
) {
402 lo
->lo_backing_file
->f_mode
= file
->f_mode
;
403 lo
->lo_backing_file
->f_pos
= file
->f_pos
;
404 lo
->lo_backing_file
->f_flags
= file
->f_flags
;
405 lo
->lo_backing_file
->f_owner
= file
->f_owner
;
406 lo
->lo_backing_file
->f_dentry
= file
->f_dentry
;
407 lo
->lo_backing_file
->f_op
= file
->f_op
;
408 lo
->lo_backing_file
->private_data
= file
->private_data
;
409 file_moveto(lo
->lo_backing_file
, file
);
411 error
= get_write_access(inode
);
413 put_filp(lo
->lo_backing_file
);
414 lo
->lo_backing_file
= NULL
;
421 if (IS_RDONLY (inode
) || is_read_only(lo
->lo_device
)) {
422 lo
->lo_flags
|= LO_FLAGS_READ_ONLY
;
423 set_device_ro(dev
, 1);
425 invalidate_inode_pages (inode
);
426 set_device_ro(dev
, 0);
429 lo
->lo_dentry
= dget(file
->f_dentry
);
432 figure_loop_size(lo
);
442 static int loop_release_xfer(struct loop_device
*lo
)
445 if (lo
->lo_encrypt_type
) {
446 struct loop_func_table
*xfer
= xfer_funcs
[lo
->lo_encrypt_type
];
447 if (xfer
&& xfer
->release
)
448 err
= xfer
->release(lo
);
449 if (xfer
&& xfer
->unlock
)
451 lo
->lo_encrypt_type
= 0;
456 static int loop_init_xfer(struct loop_device
*lo
, int type
,struct loop_info
*i
)
460 struct loop_func_table
*xfer
= xfer_funcs
[type
];
462 err
= xfer
->init(lo
, i
);
464 lo
->lo_encrypt_type
= type
;
472 static int loop_clr_fd(struct loop_device
*lo
, kdev_t dev
)
474 struct dentry
*dentry
= lo
->lo_dentry
;
478 if (lo
->lo_refcnt
> 1) /* we needed one fd for the ioctl */
481 if (S_ISBLK(dentry
->d_inode
->i_mode
))
482 blkdev_release (dentry
->d_inode
);
483 lo
->lo_dentry
= NULL
;
485 if (lo
->lo_backing_file
!= NULL
) {
486 fput(lo
->lo_backing_file
);
487 lo
->lo_backing_file
= NULL
;
492 loop_release_xfer(lo
);
496 lo
->lo_encrypt_type
= 0;
498 lo
->lo_encrypt_key_size
= 0;
499 memset(lo
->lo_encrypt_key
, 0, LO_KEY_SIZE
);
500 memset(lo
->lo_name
, 0, LO_NAME_SIZE
);
501 loop_sizes
[lo
->lo_number
] = 0;
502 invalidate_buffers(dev
);
507 static int loop_set_status(struct loop_device
*lo
, struct loop_info
*arg
)
509 struct loop_info info
;
513 if (lo
->lo_encrypt_key_size
&& lo
->lo_key_owner
!= current
->uid
&&
514 !capable(CAP_SYS_ADMIN
))
518 if (copy_from_user(&info
, arg
, sizeof (struct loop_info
)))
520 if ((unsigned int) info
.lo_encrypt_key_size
> LO_KEY_SIZE
)
522 type
= info
.lo_encrypt_type
;
523 if (type
>= MAX_LO_CRYPT
|| xfer_funcs
[type
] == NULL
)
525 err
= loop_release_xfer(lo
);
527 err
= loop_init_xfer(lo
, type
, &info
);
531 lo
->lo_offset
= info
.lo_offset
;
532 strncpy(lo
->lo_name
, info
.lo_name
, LO_NAME_SIZE
);
534 lo
->transfer
= xfer_funcs
[type
]->transfer
;
535 lo
->ioctl
= xfer_funcs
[type
]->ioctl
;
536 lo
->lo_encrypt_key_size
= info
.lo_encrypt_key_size
;
537 lo
->lo_init
[0] = info
.lo_init
[0];
538 lo
->lo_init
[1] = info
.lo_init
[1];
539 if (info
.lo_encrypt_key_size
) {
540 memcpy(lo
->lo_encrypt_key
, info
.lo_encrypt_key
,
541 info
.lo_encrypt_key_size
);
542 lo
->lo_key_owner
= current
->uid
;
544 figure_loop_size(lo
);
548 static int loop_get_status(struct loop_device
*lo
, struct loop_info
*arg
)
550 struct loop_info info
;
556 memset(&info
, 0, sizeof(info
));
557 info
.lo_number
= lo
->lo_number
;
558 info
.lo_device
= kdev_t_to_nr(lo
->lo_dentry
->d_inode
->i_dev
);
559 info
.lo_inode
= lo
->lo_dentry
->d_inode
->i_ino
;
560 info
.lo_rdevice
= kdev_t_to_nr(lo
->lo_device
);
561 info
.lo_offset
= lo
->lo_offset
;
562 info
.lo_flags
= lo
->lo_flags
;
563 strncpy(info
.lo_name
, lo
->lo_name
, LO_NAME_SIZE
);
564 info
.lo_encrypt_type
= lo
->lo_encrypt_type
;
565 if (lo
->lo_encrypt_key_size
&& capable(CAP_SYS_ADMIN
)) {
566 info
.lo_encrypt_key_size
= lo
->lo_encrypt_key_size
;
567 memcpy(info
.lo_encrypt_key
, lo
->lo_encrypt_key
,
568 lo
->lo_encrypt_key_size
);
570 return copy_to_user(arg
, &info
, sizeof(info
)) ? -EFAULT
: 0;
573 static int lo_ioctl(struct inode
* inode
, struct file
* file
,
574 unsigned int cmd
, unsigned long arg
)
576 struct loop_device
*lo
;
581 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
582 printk(KERN_WARNING
"lo_ioctl: pseudo-major != %d\n", MAJOR_NR
);
585 dev
= MINOR(inode
->i_rdev
);
591 return loop_set_fd(lo
, inode
->i_rdev
, arg
);
593 return loop_clr_fd(lo
, inode
->i_rdev
);
594 case LOOP_SET_STATUS
:
595 return loop_set_status(lo
, (struct loop_info
*) arg
);
596 case LOOP_GET_STATUS
:
597 return loop_get_status(lo
, (struct loop_info
*) arg
);
598 case BLKGETSIZE
: /* Return device size */
603 return put_user(loop_sizes
[lo
->lo_number
] << 1, (long *) arg
);
605 return lo
->ioctl
? lo
->ioctl(lo
, cmd
, arg
) : -EINVAL
;
610 static int lo_open(struct inode
*inode
, struct file
*file
)
612 struct loop_device
*lo
;
618 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
619 printk(KERN_WARNING
"lo_open: pseudo-major != %d\n", MAJOR_NR
);
622 dev
= MINOR(inode
->i_rdev
);
623 if (dev
>= max_loop
) {
628 type
= lo
->lo_encrypt_type
;
629 if (type
&& xfer_funcs
[type
] && xfer_funcs
[type
]->lock
)
630 xfer_funcs
[type
]->lock(lo
);
636 static int lo_release(struct inode
*inode
, struct file
*file
)
638 struct loop_device
*lo
;
643 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
644 printk(KERN_WARNING
"lo_release: pseudo-major != %d\n", MAJOR_NR
);
647 dev
= MINOR(inode
->i_rdev
);
650 err
= fsync_dev(inode
->i_rdev
);
652 if (lo
->lo_refcnt
<= 0)
653 printk(KERN_ERR
"lo_release: refcount(%d) <= 0\n", lo
->lo_refcnt
);
655 int type
= lo
->lo_encrypt_type
;
657 if (xfer_funcs
[type
] && xfer_funcs
[type
]->unlock
)
658 xfer_funcs
[type
]->unlock(lo
);
664 static struct file_operations lo_fops
= {
665 NULL
, /* lseek - default */
666 block_read
, /* read - general block-dev read */
667 block_write
, /* write - general block-dev write */
668 NULL
, /* readdir - bad */
670 lo_ioctl
, /* ioctl */
674 lo_release
/* release */
678 * And now the modules code and kernel interface.
681 #define loop_init init_module
682 MODULE_PARM(max_loop
, "i");
683 MODULE_PARM_DESC(max_loop
, "Maximum number of loop devices (1-255)");
686 int loop_register_transfer(struct loop_func_table
*funcs
)
688 if ((unsigned)funcs
->number
> MAX_LO_CRYPT
|| xfer_funcs
[funcs
->number
])
690 xfer_funcs
[funcs
->number
] = funcs
;
694 int loop_unregister_transfer(int number
)
696 struct loop_device
*lo
;
698 if ((unsigned)number
>= MAX_LO_CRYPT
)
700 for (lo
= &loop_dev
[0]; lo
< &loop_dev
[max_loop
]; lo
++) {
701 int type
= lo
->lo_encrypt_type
;
702 if (type
== number
) {
703 xfer_funcs
[type
]->release(lo
);
705 lo
->lo_encrypt_type
= 0;
708 xfer_funcs
[number
] = NULL
;
712 EXPORT_SYMBOL(loop_register_transfer
);
713 EXPORT_SYMBOL(loop_unregister_transfer
);
715 int __init
loop_init(void)
719 if (register_blkdev(MAJOR_NR
, "loop", &lo_fops
)) {
720 printk(KERN_WARNING
"Unable to get major number %d for loop device\n",
725 if ((max_loop
< 1) || (max_loop
> 255)) {
726 printk (KERN_WARNING
"loop: max_loop must be between 1 and 255\n");
731 printk(KERN_INFO
"loop: registered device at major %d\n", MAJOR_NR
);
733 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
);