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
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>
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>
59 static struct loop_device loop_dev
[MAX_LOOP
];
60 static int loop_sizes
[MAX_LOOP
];
61 static int loop_blksizes
[MAX_LOOP
];
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
);
73 static int transfer_none(struct loop_device
*lo
, int cmd
, char *raw_buf
,
74 char *loop_buf
, int size
, int real_block
)
77 memcpy(loop_buf
, raw_buf
, size
);
79 memcpy(raw_buf
, loop_buf
, size
);
83 static int transfer_xor(struct loop_device
*lo
, int cmd
, char *raw_buf
,
84 char *loop_buf
, int size
, int real_block
)
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
];
103 static int none_status(struct loop_device
*lo
, struct loop_info
*info
)
108 static int xor_status(struct loop_device
*lo
, struct loop_info
*info
)
110 if (info
->lo_encrypt_key_size
< 0)
115 struct loop_func_table none_funcs
= {
116 number
: LO_CRYPT_NONE
,
117 transfer
: transfer_none
,
121 struct loop_func_table xor_funcs
= {
122 number
: LO_CRYPT_XOR
,
123 transfer
: transfer_xor
,
127 /* xfer_funcs[0] is special - its release function is never called */
128 struct loop_func_table
*xfer_funcs
[MAX_LO_CRYPT
] = {
133 #define MAX_DISK_SIZE 1024*1024*1024
135 static void figure_loop_size(struct loop_device
*lo
)
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
;
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
;
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
;
157 struct loop_device
*lo
;
158 struct buffer_head
*bh
;
159 struct request
*current_request
;
164 current_request
=CURRENT
;
165 CURRENT
=current_request
->next
;
166 if (MINOR(current_request
->rq_dev
) >= MAX_LOOP
)
168 lo
= &loop_dev
[MINOR(current_request
->rq_dev
)];
169 if (!lo
->lo_dentry
|| !lo
->transfer
)
172 blksize
= BLOCK_SIZE
;
173 if (blksize_size
[MAJOR(lo
->lo_device
)]) {
174 blksize
= blksize_size
[MAJOR(lo
->lo_device
)][MINOR(lo
->lo_device
)];
176 blksize
= BLOCK_SIZE
;
179 dest_addr
= current_request
->buffer
;
182 block
= current_request
->sector
* (512/blksize
);
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
) {
194 len
= current_request
->current_nr_sectors
<< 9;
196 if (current_request
->cmd
== WRITE
) {
197 if (lo
->lo_flags
& LO_FLAGS_READ_ONLY
)
199 } else if (current_request
->cmd
!= READ
) {
200 printk(KERN_ERR
"unknown loop device command (%d)?!?", current_request
->cmd
);
203 spin_unlock_irq(&io_request_lock
);
206 size
= blksize
- offset
;
211 block_present
= TRUE
;
213 if (lo
->lo_flags
& LO_FLAGS_DO_BMAP
) {
214 real_block
= bmap(lo
->lo_dentry
->d_inode
, 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
221 if (current_request
->cmd
== READ
) {
222 memset(dest_addr
, 0, size
);
223 block_present
= FALSE
;
225 if (!create_missing_block(lo
, block
, blksize
)) {
228 real_block
= bmap(lo
->lo_dentry
->d_inode
, block
);
235 bh
= getblk(lo
->lo_device
, real_block
, blksize
);
237 printk(KERN_ERR
"loop: device %s: getblk(-, %d, %d) returned NULL",
238 kdevname(lo
->lo_device
),
242 if (!buffer_uptodate(bh
) && ((current_request
->cmd
== READ
) ||
243 (offset
|| (len
< blksize
)))) {
244 ll_rw_block(READ
, 1, &bh
);
246 if (!buffer_uptodate(bh
)) {
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
);
259 if (current_request
->cmd
== WRITE
) {
260 mark_buffer_uptodate(bh
, 1);
261 mark_buffer_dirty(bh
, 1);
270 spin_lock_irq(&io_request_lock
);
271 current_request
->next
=CURRENT
;
272 CURRENT
=current_request
;
276 spin_lock_irq(&io_request_lock
);
278 current_request
->next
=CURRENT
;
279 CURRENT
=current_request
;
284 static int create_missing_block(struct loop_device
*lo
, int block
, int blksize
)
288 char zero_buf
[1] = { 0 };
293 file
= lo
->lo_backing_file
;
295 printk(KERN_WARNING
"loop: cannot create block - no backing file\n");
299 if (file
->f_op
== NULL
) {
300 printk(KERN_WARNING
"loop: cannot create block - no file ops\n");
304 new_offset
= block
* blksize
;
306 if (file
->f_op
->llseek
!= NULL
) {
307 file
->f_op
->llseek(file
, new_offset
, 0);
309 /* Do what the default llseek() code would have done */
310 file
->f_pos
= new_offset
;
312 file
->f_version
= ++event
;
315 if (file
->f_op
->write
== NULL
) {
316 printk(KERN_WARNING
"loop: cannot create block - file not writeable\n");
323 inode
= file
->f_dentry
->d_inode
;
325 retval
= file
->f_op
->write(file
, zero_buf
, 1, &file
->f_pos
);
331 printk(KERN_WARNING
"loop: cannot create block - FS write failed: code %d\n",
339 static int loop_set_fd(struct loop_device
*lo
, kdev_t dev
, unsigned int arg
)
357 inode
= file
->f_dentry
->d_inode
;
359 printk(KERN_ERR
"loop_set_fd: NULL inode?!?\n");
363 if (S_ISBLK(inode
->i_mode
)) {
364 error
= blkdev_open(inode
, file
);
365 lo
->lo_device
= inode
->i_rdev
;
368 /* Backed by a block device - don't need to hold onto
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
381 lo
->lo_device
= inode
->i_dev
;
382 lo
->lo_flags
= LO_FLAGS_DO_BMAP
;
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
);
397 put_filp(lo
->lo_backing_file
);
398 lo
->lo_backing_file
= NULL
;
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);
409 invalidate_inode_pages (inode
);
410 set_device_ro(dev
, 0);
413 lo
->lo_dentry
= dget(file
->f_dentry
);
416 figure_loop_size(lo
);
426 static int loop_release_xfer(struct loop_device
*lo
)
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
)
435 lo
->lo_encrypt_type
= 0;
440 static int loop_init_xfer(struct loop_device
*lo
, int type
,struct loop_info
*i
)
444 struct loop_func_table
*xfer
= xfer_funcs
[type
];
446 err
= xfer
->init(lo
, i
);
448 lo
->lo_encrypt_type
= type
;
456 static int loop_clr_fd(struct loop_device
*lo
, kdev_t dev
)
458 struct dentry
*dentry
= lo
->lo_dentry
;
462 if (lo
->lo_refcnt
> 1) /* we needed one fd for the ioctl */
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
;
476 loop_release_xfer(lo
);
480 lo
->lo_encrypt_type
= 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
);
491 static int loop_set_status(struct loop_device
*lo
, struct loop_info
*arg
)
493 struct loop_info info
;
497 if (lo
->lo_encrypt_key_size
&& lo
->lo_key_owner
!= current
->uid
&&
498 !capable(CAP_SYS_ADMIN
))
502 if (copy_from_user(&info
, arg
, sizeof (struct loop_info
)))
504 if ((unsigned int) info
.lo_encrypt_key_size
> LO_KEY_SIZE
)
506 type
= info
.lo_encrypt_type
;
507 if (info
.lo_encrypt_key_size
== 0 && type
== LO_CRYPT_XOR
)
509 if (type
>= MAX_LO_CRYPT
|| xfer_funcs
[type
] == NULL
)
511 err
= loop_release_xfer(lo
);
513 err
= loop_init_xfer(lo
, type
, &info
);
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
);
534 static int loop_get_status(struct loop_device
*lo
, struct loop_info
*arg
)
536 struct loop_info info
;
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
;
567 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
568 printk(KERN_WARNING
"lo_ioctl: pseudo-major != %d\n", MAJOR_NR
);
571 dev
= MINOR(inode
->i_rdev
);
577 return loop_set_fd(lo
, inode
->i_rdev
, arg
);
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 */
589 return put_user(loop_sizes
[lo
->lo_number
] << 1, (long *) arg
);
591 return lo
->ioctl
? lo
->ioctl(lo
, cmd
, arg
) : -EINVAL
;
596 static int lo_open(struct inode
*inode
, struct file
*file
)
598 struct loop_device
*lo
;
604 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
605 printk(KERN_WARNING
"lo_open: pseudo-major != %d\n", MAJOR_NR
);
608 dev
= MINOR(inode
->i_rdev
);
609 if (dev
>= MAX_LOOP
) {
614 type
= lo
->lo_encrypt_type
;
615 if (type
&& xfer_funcs
[type
] && xfer_funcs
[type
]->lock
)
616 xfer_funcs
[type
]->lock(lo
);
622 static int lo_release(struct inode
*inode
, struct file
*file
)
624 struct loop_device
*lo
;
629 if (MAJOR(inode
->i_rdev
) != MAJOR_NR
) {
630 printk(KERN_WARNING
"lo_release: pseudo-major != %d\n", MAJOR_NR
);
633 dev
= MINOR(inode
->i_rdev
);
636 err
= fsync_dev(inode
->i_rdev
);
638 if (lo
->lo_refcnt
<= 0)
639 printk(KERN_ERR
"lo_release: refcount(%d) <= 0\n", lo
->lo_refcnt
);
641 int type
= lo
->lo_encrypt_type
;
643 if (xfer_funcs
[type
] && xfer_funcs
[type
]->unlock
)
644 xfer_funcs
[type
]->unlock(lo
);
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 */
656 lo_ioctl
, /* ioctl */
660 lo_release
/* release */
664 * And now the modules code and kernel interface.
667 #define loop_init init_module
670 int loop_register_transfer(struct loop_func_table
*funcs
)
672 if ((unsigned)funcs
->number
> MAX_LO_CRYPT
|| xfer_funcs
[funcs
->number
])
674 xfer_funcs
[funcs
->number
] = funcs
;
678 int loop_unregister_transfer(int number
)
680 struct loop_device
*lo
;
682 if ((unsigned)number
>= MAX_LO_CRYPT
)
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
);
689 lo
->lo_encrypt_type
= 0;
692 xfer_funcs
[number
] = NULL
;
696 EXPORT_SYMBOL(loop_register_transfer
);
697 EXPORT_SYMBOL(loop_unregister_transfer
);
699 int __init
loop_init(void)
703 if (register_blkdev(MAJOR_NR
, "loop", &lo_fops
)) {
704 printk(KERN_WARNING
"Unable to get major number %d for loop device\n",
709 printk(KERN_INFO
"loop: registered device at major %d\n", MAJOR_NR
);
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
;
726 void cleanup_module(void)
728 if (unregister_blkdev(MAJOR_NR
, "loop") != 0)
729 printk(KERN_WARNING
"loop: cannot unregister blkdev\n");