2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 /* 2001-09-28...2002-04-17
7 * Partition stuff by James_McMechan@hotmail.com
8 * old style ubd by setting UBD_SHIFT to 0
9 * 2002-09-27...2002-10-18 massive tinkering for 2.5
10 * partitions have changed in 2.5
11 * 2003-01-29 more tinkering for 2.5.59-1
12 * This should now address the sysfs problems and has
13 * the symlink for devfs to allow for booting with
14 * the common /dev/ubd/discX/... names rather than
15 * only /dev/ubdN/discN this version also has lots of
16 * clean ups preparing for ubd-many.
20 #define MAJOR_NR UBD_MAJOR
23 #include "linux/module.h"
24 #include "linux/blkdev.h"
25 #include "linux/hdreg.h"
26 #include "linux/init.h"
27 #include "linux/cdrom.h"
28 #include "linux/proc_fs.h"
29 #include "linux/ctype.h"
30 #include "linux/capability.h"
32 #include "linux/vmalloc.h"
33 #include "linux/blkpg.h"
34 #include "linux/genhd.h"
35 #include "linux/spinlock.h"
36 #include "linux/platform_device.h"
37 #include "asm/segment.h"
38 #include "asm/uaccess.h"
40 #include "asm/types.h"
41 #include "asm/tlbflush.h"
42 #include "user_util.h"
44 #include "kern_util.h"
46 #include "mconsole_kern.h"
56 enum ubd_req
{ UBD_READ
, UBD_WRITE
};
58 struct io_thread_req
{
61 unsigned long offsets
[2];
62 unsigned long long offset
;
66 unsigned long sector_mask
;
67 unsigned long long cow_offset
;
68 unsigned long bitmap_words
[2];
72 extern int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
73 char **backing_file_out
, int *bitmap_offset_out
,
74 unsigned long *bitmap_len_out
, int *data_offset_out
,
76 extern int create_cow_file(char *cow_file
, char *backing_file
,
77 struct openflags flags
, int sectorsize
,
78 int alignment
, int *bitmap_offset_out
,
79 unsigned long *bitmap_len_out
,
80 int *data_offset_out
);
81 extern int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
);
82 extern void do_io(struct io_thread_req
*req
);
84 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
89 bits
= sizeof(data
[0]) * 8;
92 return((data
[n
] & (1 << off
)) != 0);
95 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
100 bits
= sizeof(data
[0]) * 8;
103 data
[n
] |= (1 << off
);
105 /*End stuff from ubd_user.h*/
107 #define DRIVER_NAME "uml-blkdev"
109 /* Can be taken in interrupt context, and is passed to the block layer to lock
110 * the request queue. Kernel side code knows that. */
111 static DEFINE_SPINLOCK(ubd_io_lock
);
113 static DEFINE_MUTEX(ubd_lock
);
115 /* XXX - this made sense in 2.4 days, now it's only used as a boolean, and
116 * probably it doesn't make sense even for that. */
119 static int ubd_open(struct inode
* inode
, struct file
* filp
);
120 static int ubd_release(struct inode
* inode
, struct file
* file
);
121 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
122 unsigned int cmd
, unsigned long arg
);
123 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
);
127 static struct block_device_operations ubd_blops
= {
128 .owner
= THIS_MODULE
,
130 .release
= ubd_release
,
132 .getgeo
= ubd_getgeo
,
135 /* Protected by the queue_lock */
136 static request_queue_t
*ubd_queue
;
138 /* Protected by ubd_lock */
139 static int fake_major
= MAJOR_NR
;
141 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
142 static struct gendisk
*fake_gendisk
[MAX_DEV
];
144 #ifdef CONFIG_BLK_DEV_UBD_SYNC
145 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
148 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
152 /* Not protected - changed only in ubd_setup_common and then only to
155 static struct openflags global_openflags
= OPEN_FLAGS
;
158 /* backing file name */
160 /* backing file fd */
162 unsigned long *bitmap
;
163 unsigned long bitmap_len
;
169 /* name (and fd, below) of the file opened for writing, either the
170 * backing or the cow file. */
175 struct openflags boot_openflags
;
176 struct openflags openflags
;
180 struct platform_device pdev
;
183 #define DEFAULT_COW { \
187 .bitmap_offset = 0, \
191 #define DEFAULT_UBD { \
196 .boot_openflags = OPEN_FLAGS, \
197 .openflags = OPEN_FLAGS, \
200 .cow = DEFAULT_COW, \
203 struct ubd ubd_devs
[MAX_DEV
] = { [ 0 ... MAX_DEV
- 1 ] = DEFAULT_UBD
};
205 /* Only changed by fake_ide_setup which is a setup */
206 static int fake_ide
= 0;
207 static struct proc_dir_entry
*proc_ide_root
= NULL
;
208 static struct proc_dir_entry
*proc_ide
= NULL
;
210 static void make_proc_ide(void)
212 proc_ide_root
= proc_mkdir("ide", NULL
);
213 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
216 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
217 int *eof
, void *data
)
221 strcpy(page
, "disk\n");
222 len
= strlen("disk\n");
226 if (len
<= 0) return 0;
233 static void make_ide_entries(char *dev_name
)
235 struct proc_dir_entry
*dir
, *ent
;
238 if(proc_ide_root
== NULL
) make_proc_ide();
240 dir
= proc_mkdir(dev_name
, proc_ide
);
243 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
247 ent
->read_proc
= proc_ide_read_media
;
248 ent
->write_proc
= NULL
;
249 sprintf(name
,"ide0/%s", dev_name
);
250 proc_symlink(dev_name
, proc_ide_root
, name
);
253 static int fake_ide_setup(char *str
)
259 __setup("fake_ide", fake_ide_setup
);
261 __uml_help(fake_ide_setup
,
263 " Create ide0 entries that map onto ubd devices.\n\n"
266 static int parse_unit(char **ptr
)
268 char *str
= *ptr
, *end
;
272 n
= simple_strtoul(str
, &end
, 0);
277 else if (('a' <= *str
) && (*str
<= 'z')) {
285 /* If *index_out == -1 at exit, the passed option was a general one;
286 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
287 * should not be freed on exit.
289 static int ubd_setup_common(char *str
, int *index_out
, char **error_out
)
292 struct openflags flags
= global_openflags
;
296 if(index_out
) *index_out
= -1;
303 if(!strcmp(str
, "sync")){
304 global_openflags
= of_sync(global_openflags
);
307 major
= simple_strtoul(str
, &end
, 0);
308 if((*end
!= '\0') || (end
== str
)){
309 *error_out
= "Didn't parse major number";
314 mutex_lock(&ubd_lock
);
315 if(fake_major
!= MAJOR_NR
){
316 *error_out
= "Can't assign a fake major twice";
322 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
326 mutex_unlock(&ubd_lock
);
330 n
= parse_unit(&str
);
332 *error_out
= "Couldn't parse device number";
336 *error_out
= "Device number out of range";
341 mutex_lock(&ubd_lock
);
343 ubd_dev
= &ubd_devs
[n
];
344 if(ubd_dev
->file
!= NULL
){
345 *error_out
= "Device is already configured";
353 for (i
= 0; i
< sizeof("rscd="); i
++) {
371 *error_out
= "Expected '=' or flag letter "
379 *error_out
= "Too many flags specified";
381 *error_out
= "Missing '='";
385 backing_file
= strchr(str
, ',');
387 if (backing_file
== NULL
)
388 backing_file
= strchr(str
, ':');
390 if(backing_file
!= NULL
){
392 *error_out
= "Can't specify both 'd' and a cow file";
396 *backing_file
= '\0';
402 ubd_dev
->cow
.file
= backing_file
;
403 ubd_dev
->boot_openflags
= flags
;
405 mutex_unlock(&ubd_lock
);
409 static int ubd_setup(char *str
)
414 err
= ubd_setup_common(str
, NULL
, &error
);
416 printk(KERN_ERR
"Failed to initialize device with \"%s\" : "
421 __setup("ubd", ubd_setup
);
422 __uml_help(ubd_setup
,
423 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
424 " This is used to associate a device with a file in the underlying\n"
425 " filesystem. When specifying two filenames, the first one is the\n"
426 " COW name and the second is the backing file name. As separator you can\n"
427 " use either a ':' or a ',': the first one allows writing things like;\n"
428 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
429 " while with a ',' the shell would not expand the 2nd '~'.\n"
430 " When using only one filename, UML will detect whether to treat it like\n"
431 " a COW file or a backing file. To override this detection, add the 'd'\n"
433 " ubd0d=BackingFile\n"
434 " Usually, there is a filesystem in the file, but \n"
435 " that's not required. Swap devices containing swap files can be\n"
436 " specified like this. Also, a file which doesn't contain a\n"
437 " filesystem can have its contents read in the virtual \n"
438 " machine by running 'dd' on the device. <n> must be in the range\n"
439 " 0 to 7. Appending an 'r' to the number will cause that device\n"
440 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
441 " an 's' will cause data to be written to disk on the host immediately.\n\n"
444 static int udb_setup(char *str
)
446 printk("udb%s specified on command line is almost certainly a ubd -> "
451 __setup("udb", udb_setup
);
452 __uml_help(udb_setup
,
454 " This option is here solely to catch ubd -> udb typos, which can be\n"
455 " to impossible to catch visually unless you specifically look for\n"
456 " them. The only result of any option starting with 'udb' is an error\n"
457 " in the boot output.\n\n"
460 static int fakehd_set
= 0;
461 static int fakehd(char *str
)
463 printk(KERN_INFO
"fakehd : Changing ubd name to \"hd\".\n");
468 __setup("fakehd", fakehd
);
471 " Change the ubd device name to \"hd\".\n\n"
474 static void do_ubd_request(request_queue_t
* q
);
476 /* Only changed by ubd_init, which is an initcall. */
479 /* Changed by ubd_handler, which is serialized because interrupts only
481 * XXX: currently unused.
483 static int intr_count
= 0;
485 /* call ubd_finish if you need to serialize */
486 static void __ubd_finish(struct request
*req
, int error
)
494 nsect
= req
->current_nr_sectors
;
495 req
->sector
+= nsect
;
496 req
->buffer
+= nsect
<< 9;
498 req
->nr_sectors
-= nsect
;
499 req
->current_nr_sectors
= 0;
503 /* Callable only from interrupt context - otherwise you need to do
504 * spin_lock_irq()/spin_lock_irqsave() */
505 static inline void ubd_finish(struct request
*req
, int error
)
507 spin_lock(&ubd_io_lock
);
508 __ubd_finish(req
, error
);
509 spin_unlock(&ubd_io_lock
);
512 /* XXX - move this inside ubd_intr. */
513 /* Called without ubd_io_lock held, and only in interrupt context. */
514 static void ubd_handler(void)
516 struct io_thread_req req
;
517 struct request
*rq
= elv_next_request(ubd_queue
);
522 n
= os_read_file(thread_fd
, &req
, sizeof(req
));
523 if(n
!= sizeof(req
)){
524 printk(KERN_ERR
"Pid %d - spurious interrupt in ubd_handler, "
525 "err = %d\n", os_getpid(), -n
);
526 spin_lock(&ubd_io_lock
);
528 spin_unlock(&ubd_io_lock
);
532 ubd_finish(rq
, req
.error
);
533 reactivate_fd(thread_fd
, UBD_IRQ
);
534 spin_lock(&ubd_io_lock
);
535 do_ubd_request(ubd_queue
);
536 spin_unlock(&ubd_io_lock
);
539 static irqreturn_t
ubd_intr(int irq
, void *dev
)
545 /* Only changed by ubd_init, which is an initcall. */
546 static int io_pid
= -1;
548 void kill_io_thread(void)
551 os_kill_process(io_pid
, 1);
554 __uml_exitcall(kill_io_thread
);
556 static inline int ubd_file_size(struct ubd
*ubd_dev
, __u64
*size_out
)
560 file
= ubd_dev
->cow
.file
? ubd_dev
->cow
.file
: ubd_dev
->file
;
561 return(os_file_size(file
, size_out
));
564 static void ubd_close_dev(struct ubd
*ubd_dev
)
566 os_close_file(ubd_dev
->fd
);
567 if(ubd_dev
->cow
.file
== NULL
)
570 os_close_file(ubd_dev
->cow
.fd
);
571 vfree(ubd_dev
->cow
.bitmap
);
572 ubd_dev
->cow
.bitmap
= NULL
;
575 static int ubd_open_dev(struct ubd
*ubd_dev
)
577 struct openflags flags
;
579 int err
, create_cow
, *create_ptr
;
582 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
584 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
585 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
587 fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
588 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
589 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
592 if((fd
== -ENOENT
) && create_cow
){
593 fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
594 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
595 &ubd_dev
->cow
.bitmap_offset
,
596 &ubd_dev
->cow
.bitmap_len
,
597 &ubd_dev
->cow
.data_offset
);
599 printk(KERN_INFO
"Creating \"%s\" as COW file for "
600 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
605 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
611 if(ubd_dev
->cow
.file
!= NULL
){
613 ubd_dev
->cow
.bitmap
= (void *) vmalloc(ubd_dev
->cow
.bitmap_len
);
614 if(ubd_dev
->cow
.bitmap
== NULL
){
615 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
618 flush_tlb_kernel_vm();
620 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
621 ubd_dev
->cow
.bitmap_offset
,
622 ubd_dev
->cow
.bitmap_len
);
626 flags
= ubd_dev
->openflags
;
628 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
629 NULL
, NULL
, NULL
, NULL
);
630 if(err
< 0) goto error
;
631 ubd_dev
->cow
.fd
= err
;
635 os_close_file(ubd_dev
->fd
);
639 static int ubd_disk_register(int major
, u64 size
, int unit
,
640 struct gendisk
**disk_out
)
643 struct gendisk
*disk
;
645 disk
= alloc_disk(1 << UBD_SHIFT
);
650 disk
->first_minor
= unit
<< UBD_SHIFT
;
651 disk
->fops
= &ubd_blops
;
652 set_capacity(disk
, size
/ 512);
653 if(major
== MAJOR_NR
)
654 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
656 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
658 /* sysfs register (not for ide fake devices) */
659 if (major
== MAJOR_NR
) {
660 ubd_devs
[unit
].pdev
.id
= unit
;
661 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
662 platform_device_register(&ubd_devs
[unit
].pdev
);
663 disk
->driverfs_dev
= &ubd_devs
[unit
].pdev
.dev
;
666 disk
->private_data
= &ubd_devs
[unit
];
667 disk
->queue
= ubd_queue
;
674 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
676 static int ubd_add(int n
, char **error_out
)
678 struct ubd
*ubd_dev
= &ubd_devs
[n
];
681 if(ubd_dev
->file
== NULL
)
684 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
686 *error_out
= "Couldn't determine size of device's file";
690 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
692 err
= ubd_disk_register(MAJOR_NR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
696 if(fake_major
!= MAJOR_NR
)
697 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
700 /* perhaps this should also be under the "if (fake_major)" above */
701 /* using the fake_disk->disk_name and also the fakehd_set name */
703 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
710 static int ubd_config(char *str
, char **error_out
)
714 /* This string is possibly broken up and stored, so it's only
715 * freed if ubd_setup_common fails, or if only general options
718 str
= kstrdup(str
, GFP_KERNEL
);
720 *error_out
= "Failed to allocate memory";
724 ret
= ubd_setup_common(str
, &n
, error_out
);
733 mutex_lock(&ubd_lock
);
734 ret
= ubd_add(n
, error_out
);
736 ubd_devs
[n
].file
= NULL
;
737 mutex_unlock(&ubd_lock
);
747 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
752 n
= parse_unit(&name
);
753 if((n
>= MAX_DEV
) || (n
< 0)){
754 *error_out
= "ubd_get_config : device number out of range";
758 ubd_dev
= &ubd_devs
[n
];
759 mutex_lock(&ubd_lock
);
761 if(ubd_dev
->file
== NULL
){
762 CONFIG_CHUNK(str
, size
, len
, "", 1);
766 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
768 if(ubd_dev
->cow
.file
!= NULL
){
769 CONFIG_CHUNK(str
, size
, len
, ",", 0);
770 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
772 else CONFIG_CHUNK(str
, size
, len
, "", 1);
775 mutex_unlock(&ubd_lock
);
779 static int ubd_id(char **str
, int *start_out
, int *end_out
)
785 *end_out
= MAX_DEV
- 1;
789 static int ubd_remove(int n
, char **error_out
)
794 mutex_lock(&ubd_lock
);
796 if(ubd_gendisk
[n
] == NULL
)
799 ubd_dev
= &ubd_devs
[n
];
801 if(ubd_dev
->file
== NULL
)
804 /* you cannot remove a open disk */
806 if(ubd_dev
->count
> 0)
809 del_gendisk(ubd_gendisk
[n
]);
810 put_disk(ubd_gendisk
[n
]);
811 ubd_gendisk
[n
] = NULL
;
813 if(fake_gendisk
[n
] != NULL
){
814 del_gendisk(fake_gendisk
[n
]);
815 put_disk(fake_gendisk
[n
]);
816 fake_gendisk
[n
] = NULL
;
819 platform_device_unregister(&ubd_dev
->pdev
);
820 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
823 mutex_unlock(&ubd_lock
);
827 /* All these are called by mconsole in process context and without
828 * ubd-specific locks.
830 static struct mc_device ubd_mc
= {
831 .list
= LIST_HEAD_INIT(ubd_mc
.list
),
833 .config
= ubd_config
,
834 .get_config
= ubd_get_config
,
836 .remove
= ubd_remove
,
839 static int __init
ubd_mc_init(void)
841 mconsole_register_dev(&ubd_mc
);
845 __initcall(ubd_mc_init
);
847 static int __init
ubd0_init(void)
849 struct ubd
*ubd_dev
= &ubd_devs
[0];
851 if(ubd_dev
->file
== NULL
)
852 ubd_dev
->file
= "root_fs";
856 __initcall(ubd0_init
);
858 static struct platform_driver ubd_driver
= {
864 static int __init
ubd_init(void)
869 if (register_blkdev(MAJOR_NR
, "ubd"))
872 ubd_queue
= blk_init_queue(do_ubd_request
, &ubd_io_lock
);
874 unregister_blkdev(MAJOR_NR
, "ubd");
878 if (fake_major
!= MAJOR_NR
) {
879 char name
[sizeof("ubd_nnn\0")];
881 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
882 if (register_blkdev(fake_major
, "ubd"))
885 platform_driver_register(&ubd_driver
);
886 for (i
= 0; i
< MAX_DEV
; i
++){
887 err
= ubd_add(i
, &error
);
889 printk(KERN_ERR
"Failed to initialize ubd device %d :"
895 late_initcall(ubd_init
);
897 static int __init
ubd_driver_init(void){
901 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
902 if(global_openflags
.s
){
903 printk(KERN_INFO
"ubd: Synchronous mode\n");
904 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
905 * enough. So use anyway the io thread. */
907 stack
= alloc_stack(0, 0);
908 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
912 "ubd : Failed to start I/O thread (errno = %d) - "
913 "falling back to synchronous I/O\n", -io_pid
);
917 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
918 IRQF_DISABLED
, "ubd", ubd_devs
);
920 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
924 device_initcall(ubd_driver_init
);
926 static int ubd_open(struct inode
*inode
, struct file
*filp
)
928 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
929 struct ubd
*ubd_dev
= disk
->private_data
;
932 if(ubd_dev
->count
== 0){
933 err
= ubd_open_dev(ubd_dev
);
935 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
936 disk
->disk_name
, ubd_dev
->file
, -err
);
941 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
943 /* This should no more be needed. And it didn't work anyway to exclude
944 * read-write remounting of filesystems.*/
945 /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
946 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
953 static int ubd_release(struct inode
* inode
, struct file
* file
)
955 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
956 struct ubd
*ubd_dev
= disk
->private_data
;
958 if(--ubd_dev
->count
== 0)
959 ubd_close_dev(ubd_dev
);
963 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
964 __u64
*cow_offset
, unsigned long *bitmap
,
965 __u64 bitmap_offset
, unsigned long *bitmap_words
,
968 __u64 sector
= io_offset
>> 9;
969 int i
, update_bitmap
= 0;
971 for(i
= 0; i
< length
>> 9; i
++){
973 ubd_set_bit(i
, (unsigned char *) cow_mask
);
974 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
978 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
984 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
986 /* This takes care of the case where we're exactly at the end of the
987 * device, and *cow_offset + 1 is off the end. So, just back it up
988 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
989 * for the original diagnosis.
991 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
992 sizeof(unsigned long) - 1))
995 bitmap_words
[0] = bitmap
[*cow_offset
];
996 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
998 *cow_offset
*= sizeof(unsigned long);
999 *cow_offset
+= bitmap_offset
;
1002 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
1003 __u64 bitmap_offset
, __u64 bitmap_len
)
1005 __u64 sector
= req
->offset
>> 9;
1008 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
1009 panic("Operation too long");
1011 if(req
->op
== UBD_READ
) {
1012 for(i
= 0; i
< req
->length
>> 9; i
++){
1013 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1014 ubd_set_bit(i
, (unsigned char *)
1018 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1019 &req
->cow_offset
, bitmap
, bitmap_offset
,
1020 req
->bitmap_words
, bitmap_len
);
1023 /* Called with ubd_io_lock held */
1024 static int prepare_request(struct request
*req
, struct io_thread_req
*io_req
)
1026 struct gendisk
*disk
= req
->rq_disk
;
1027 struct ubd
*ubd_dev
= disk
->private_data
;
1031 /* This should be impossible now */
1032 if((rq_data_dir(req
) == WRITE
) && !ubd_dev
->openflags
.w
){
1033 printk("Write attempted on readonly ubd device %s\n",
1035 end_request(req
, 0);
1039 offset
= ((__u64
) req
->sector
) << 9;
1040 len
= req
->current_nr_sectors
<< 9;
1042 io_req
->fds
[0] = (ubd_dev
->cow
.file
!= NULL
) ? ubd_dev
->cow
.fd
: ubd_dev
->fd
;
1043 io_req
->fds
[1] = ubd_dev
->fd
;
1044 io_req
->cow_offset
= -1;
1045 io_req
->offset
= offset
;
1046 io_req
->length
= len
;
1048 io_req
->sector_mask
= 0;
1050 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1051 io_req
->offsets
[0] = 0;
1052 io_req
->offsets
[1] = ubd_dev
->cow
.data_offset
;
1053 io_req
->buffer
= req
->buffer
;
1054 io_req
->sectorsize
= 1 << 9;
1056 if(ubd_dev
->cow
.file
!= NULL
)
1057 cowify_req(io_req
, ubd_dev
->cow
.bitmap
, ubd_dev
->cow
.bitmap_offset
,
1058 ubd_dev
->cow
.bitmap_len
);
1063 /* Called with ubd_io_lock held */
1064 static void do_ubd_request(request_queue_t
*q
)
1066 struct io_thread_req io_req
;
1067 struct request
*req
;
1070 if(thread_fd
== -1){
1071 while((req
= elv_next_request(q
)) != NULL
){
1072 err
= prepare_request(req
, &io_req
);
1075 __ubd_finish(req
, io_req
.error
);
1080 if(do_ubd
|| (req
= elv_next_request(q
)) == NULL
)
1082 err
= prepare_request(req
, &io_req
);
1085 n
= os_write_file(thread_fd
, (char *) &io_req
,
1087 if(n
!= sizeof(io_req
))
1088 printk("write to io thread failed, "
1089 "errno = %d\n", -n
);
1094 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1096 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1100 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1104 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1105 unsigned int cmd
, unsigned long arg
)
1107 struct ubd
*ubd_dev
= inode
->i_bdev
->bd_disk
->private_data
;
1108 struct hd_driveid ubd_id
= {
1115 struct cdrom_volctrl volume
;
1116 case HDIO_GET_IDENTITY
:
1117 ubd_id
.cyls
= ubd_dev
->size
/ (128 * 32 * 512);
1118 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1124 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1126 volume
.channel0
= 255;
1127 volume
.channel1
= 255;
1128 volume
.channel2
= 255;
1129 volume
.channel3
= 255;
1130 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1137 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
1139 struct uml_stat buf1
, buf2
;
1142 if(from_cmdline
== NULL
)
1144 if(!strcmp(from_cmdline
, from_cow
))
1147 err
= os_stat_file(from_cmdline
, &buf1
);
1149 printk("Couldn't stat '%s', err = %d\n", from_cmdline
, -err
);
1152 err
= os_stat_file(from_cow
, &buf2
);
1154 printk("Couldn't stat '%s', err = %d\n", from_cow
, -err
);
1157 if((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
1160 printk("Backing file mismatch - \"%s\" requested,\n"
1161 "\"%s\" specified in COW header of \"%s\"\n",
1162 from_cmdline
, from_cow
, cow
);
1166 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
1168 unsigned long modtime
;
1169 unsigned long long actual
;
1172 err
= os_file_modtime(file
, &modtime
);
1174 printk("Failed to get modification time of backing file "
1175 "\"%s\", err = %d\n", file
, -err
);
1179 err
= os_file_size(file
, &actual
);
1181 printk("Failed to get size of backing file \"%s\", "
1182 "err = %d\n", file
, -err
);
1187 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1189 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1190 "file\n", (unsigned long long) size
, actual
);
1193 if(modtime
!= mtime
){
1194 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1195 "file\n", mtime
, modtime
);
1201 int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
1205 err
= os_seek_file(fd
, offset
);
1209 err
= os_read_file(fd
, buf
, len
);
1216 int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
1217 char **backing_file_out
, int *bitmap_offset_out
,
1218 unsigned long *bitmap_len_out
, int *data_offset_out
,
1219 int *create_cow_out
)
1222 unsigned long long size
;
1223 __u32 version
, align
;
1225 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
1227 fd
= os_open_file(file
, *openflags
, mode
);
1229 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
1230 *create_cow_out
= 1;
1231 if (!openflags
->w
||
1232 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
1235 fd
= os_open_file(file
, *openflags
, mode
);
1241 printk("Not locking \"%s\" on the host\n", file
);
1243 err
= os_lock_file(fd
, openflags
->w
);
1245 printk("Failed to lock '%s', err = %d\n", file
, -err
);
1250 /* Successful return case! */
1251 if(backing_file_out
== NULL
)
1254 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
1255 &size
, §orsize
, &align
, bitmap_offset_out
);
1256 if(err
&& (*backing_file_out
!= NULL
)){
1257 printk("Failed to read COW header from COW file \"%s\", "
1258 "errno = %d\n", file
, -err
);
1264 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
, file
);
1266 /* Allow switching only if no mismatch. */
1267 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
, mtime
)) {
1268 printk("Switching backing file to '%s'\n", *backing_file_out
);
1269 err
= write_cow_header(file
, fd
, *backing_file_out
,
1270 sectorsize
, align
, &size
);
1272 printk("Switch failed, errno = %d\n", -err
);
1276 *backing_file_out
= backing_file
;
1277 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
1282 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
1283 bitmap_len_out
, data_offset_out
);
1291 int create_cow_file(char *cow_file
, char *backing_file
, struct openflags flags
,
1292 int sectorsize
, int alignment
, int *bitmap_offset_out
,
1293 unsigned long *bitmap_len_out
, int *data_offset_out
)
1298 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
1301 printk("Open of COW file '%s' failed, errno = %d\n", cow_file
,
1306 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
1307 bitmap_offset_out
, bitmap_len_out
,
1316 static int update_bitmap(struct io_thread_req
*req
)
1320 if(req
->cow_offset
== -1)
1323 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1325 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1329 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1330 sizeof(req
->bitmap_words
));
1331 if(n
!= sizeof(req
->bitmap_words
)){
1332 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1340 void do_io(struct io_thread_req
*req
)
1344 int n
, nsectors
, start
, end
, bit
;
1348 nsectors
= req
->length
/ req
->sectorsize
;
1351 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1353 while((end
< nsectors
) &&
1354 (ubd_test_bit(end
, (unsigned char *)
1355 &req
->sector_mask
) == bit
))
1358 off
= req
->offset
+ req
->offsets
[bit
] +
1359 start
* req
->sectorsize
;
1360 len
= (end
- start
) * req
->sectorsize
;
1361 buf
= &req
->buffer
[start
* req
->sectorsize
];
1363 err
= os_seek_file(req
->fds
[bit
], off
);
1365 printk("do_io - lseek failed : err = %d\n", -err
);
1369 if(req
->op
== UBD_READ
){
1374 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1376 printk("do_io - read failed, err = %d "
1377 "fd = %d\n", -n
, req
->fds
[bit
]);
1381 } while((n
< len
) && (n
!= 0));
1382 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1384 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1386 printk("do_io - write failed err = %d "
1387 "fd = %d\n", -n
, req
->fds
[bit
]);
1394 } while(start
< nsectors
);
1396 req
->error
= update_bitmap(req
);
1399 /* Changed in start_io_thread, which is serialized by being called only
1400 * from ubd_init, which is an initcall.
1404 /* Only changed by the io thread. XXX: currently unused. */
1405 static int io_count
= 0;
1407 int io_thread(void *arg
)
1409 struct io_thread_req req
;
1412 ignore_sigwinch_sig();
1414 n
= os_read_file(kernel_fd
, &req
, sizeof(req
));
1415 if(n
!= sizeof(req
)){
1417 printk("io_thread - read failed, fd = %d, "
1418 "err = %d\n", kernel_fd
, -n
);
1420 printk("io_thread - short read, fd = %d, "
1421 "length = %d\n", kernel_fd
, n
);
1427 n
= os_write_file(kernel_fd
, &req
, sizeof(req
));
1428 if(n
!= sizeof(req
))
1429 printk("io_thread - write failed, fd = %d, err = %d\n",