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
{
62 unsigned long offsets
[2];
63 unsigned long long offset
;
67 unsigned long sector_mask
;
68 unsigned long long cow_offset
;
69 unsigned long bitmap_words
[2];
73 extern int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
74 char **backing_file_out
, int *bitmap_offset_out
,
75 unsigned long *bitmap_len_out
, int *data_offset_out
,
77 extern int create_cow_file(char *cow_file
, char *backing_file
,
78 struct openflags flags
, int sectorsize
,
79 int alignment
, int *bitmap_offset_out
,
80 unsigned long *bitmap_len_out
,
81 int *data_offset_out
);
82 extern int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
);
83 extern void do_io(struct io_thread_req
*req
);
85 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
90 bits
= sizeof(data
[0]) * 8;
93 return((data
[n
] & (1 << off
)) != 0);
96 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
101 bits
= sizeof(data
[0]) * 8;
104 data
[n
] |= (1 << off
);
106 /*End stuff from ubd_user.h*/
108 #define DRIVER_NAME "uml-blkdev"
110 static DEFINE_MUTEX(ubd_lock
);
112 /* XXX - this made sense in 2.4 days, now it's only used as a boolean, and
113 * probably it doesn't make sense even for that. */
116 static int ubd_open(struct inode
* inode
, struct file
* filp
);
117 static int ubd_release(struct inode
* inode
, struct file
* file
);
118 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
119 unsigned int cmd
, unsigned long arg
);
120 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
);
124 static struct block_device_operations ubd_blops
= {
125 .owner
= THIS_MODULE
,
127 .release
= ubd_release
,
129 .getgeo
= ubd_getgeo
,
132 /* Protected by ubd_lock */
133 static int fake_major
= MAJOR_NR
;
134 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
135 static struct gendisk
*fake_gendisk
[MAX_DEV
];
137 #ifdef CONFIG_BLK_DEV_UBD_SYNC
138 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
141 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
144 static struct openflags global_openflags
= OPEN_FLAGS
;
147 /* backing file name */
149 /* backing file fd */
151 unsigned long *bitmap
;
152 unsigned long bitmap_len
;
158 /* name (and fd, below) of the file opened for writing, either the
159 * backing or the cow file. */
164 struct openflags boot_openflags
;
165 struct openflags openflags
;
169 struct platform_device pdev
;
170 struct request_queue
*queue
;
174 #define DEFAULT_COW { \
178 .bitmap_offset = 0, \
182 #define DEFAULT_UBD { \
187 .boot_openflags = OPEN_FLAGS, \
188 .openflags = OPEN_FLAGS, \
191 .cow = DEFAULT_COW, \
192 .lock = SPIN_LOCK_UNLOCKED, \
195 /* Protected by ubd_lock */
196 struct ubd ubd_devs
[MAX_DEV
] = { [ 0 ... MAX_DEV
- 1 ] = DEFAULT_UBD
};
198 /* Only changed by fake_ide_setup which is a setup */
199 static int fake_ide
= 0;
200 static struct proc_dir_entry
*proc_ide_root
= NULL
;
201 static struct proc_dir_entry
*proc_ide
= NULL
;
203 static void make_proc_ide(void)
205 proc_ide_root
= proc_mkdir("ide", NULL
);
206 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
209 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
210 int *eof
, void *data
)
214 strcpy(page
, "disk\n");
215 len
= strlen("disk\n");
219 if (len
<= 0) return 0;
226 static void make_ide_entries(char *dev_name
)
228 struct proc_dir_entry
*dir
, *ent
;
231 if(proc_ide_root
== NULL
) make_proc_ide();
233 dir
= proc_mkdir(dev_name
, proc_ide
);
236 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
240 ent
->read_proc
= proc_ide_read_media
;
241 ent
->write_proc
= NULL
;
242 sprintf(name
,"ide0/%s", dev_name
);
243 proc_symlink(dev_name
, proc_ide_root
, name
);
246 static int fake_ide_setup(char *str
)
252 __setup("fake_ide", fake_ide_setup
);
254 __uml_help(fake_ide_setup
,
256 " Create ide0 entries that map onto ubd devices.\n\n"
259 static int parse_unit(char **ptr
)
261 char *str
= *ptr
, *end
;
265 n
= simple_strtoul(str
, &end
, 0);
270 else if (('a' <= *str
) && (*str
<= 'z')) {
278 /* If *index_out == -1 at exit, the passed option was a general one;
279 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
280 * should not be freed on exit.
282 static int ubd_setup_common(char *str
, int *index_out
, char **error_out
)
285 struct openflags flags
= global_openflags
;
289 if(index_out
) *index_out
= -1;
296 if(!strcmp(str
, "sync")){
297 global_openflags
= of_sync(global_openflags
);
302 major
= simple_strtoul(str
, &end
, 0);
303 if((*end
!= '\0') || (end
== str
)){
304 *error_out
= "Didn't parse major number";
308 mutex_lock(&ubd_lock
);
309 if(fake_major
!= MAJOR_NR
){
310 *error_out
= "Can't assign a fake major twice";
316 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
320 mutex_unlock(&ubd_lock
);
324 n
= parse_unit(&str
);
326 *error_out
= "Couldn't parse device number";
330 *error_out
= "Device number out of range";
335 mutex_lock(&ubd_lock
);
337 ubd_dev
= &ubd_devs
[n
];
338 if(ubd_dev
->file
!= NULL
){
339 *error_out
= "Device is already configured";
347 for (i
= 0; i
< sizeof("rscd="); i
++) {
365 *error_out
= "Expected '=' or flag letter "
373 *error_out
= "Too many flags specified";
375 *error_out
= "Missing '='";
379 backing_file
= strchr(str
, ',');
381 if (backing_file
== NULL
)
382 backing_file
= strchr(str
, ':');
384 if(backing_file
!= NULL
){
386 *error_out
= "Can't specify both 'd' and a cow file";
390 *backing_file
= '\0';
396 ubd_dev
->cow
.file
= backing_file
;
397 ubd_dev
->boot_openflags
= flags
;
399 mutex_unlock(&ubd_lock
);
403 static int ubd_setup(char *str
)
408 err
= ubd_setup_common(str
, NULL
, &error
);
410 printk(KERN_ERR
"Failed to initialize device with \"%s\" : "
415 __setup("ubd", ubd_setup
);
416 __uml_help(ubd_setup
,
417 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
418 " This is used to associate a device with a file in the underlying\n"
419 " filesystem. When specifying two filenames, the first one is the\n"
420 " COW name and the second is the backing file name. As separator you can\n"
421 " use either a ':' or a ',': the first one allows writing things like;\n"
422 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
423 " while with a ',' the shell would not expand the 2nd '~'.\n"
424 " When using only one filename, UML will detect whether to treat it like\n"
425 " a COW file or a backing file. To override this detection, add the 'd'\n"
427 " ubd0d=BackingFile\n"
428 " Usually, there is a filesystem in the file, but \n"
429 " that's not required. Swap devices containing swap files can be\n"
430 " specified like this. Also, a file which doesn't contain a\n"
431 " filesystem can have its contents read in the virtual \n"
432 " machine by running 'dd' on the device. <n> must be in the range\n"
433 " 0 to 7. Appending an 'r' to the number will cause that device\n"
434 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
435 " an 's' will cause data to be written to disk on the host immediately.\n\n"
438 static int udb_setup(char *str
)
440 printk("udb%s specified on command line is almost certainly a ubd -> "
445 __setup("udb", udb_setup
);
446 __uml_help(udb_setup
,
448 " This option is here solely to catch ubd -> udb typos, which can be\n"
449 " to impossible to catch visually unless you specifically look for\n"
450 " them. The only result of any option starting with 'udb' is an error\n"
451 " in the boot output.\n\n"
454 static int fakehd_set
= 0;
455 static int fakehd(char *str
)
457 printk(KERN_INFO
"fakehd : Changing ubd name to \"hd\".\n");
462 __setup("fakehd", fakehd
);
465 " Change the ubd device name to \"hd\".\n\n"
468 static void do_ubd_request(request_queue_t
* q
);
470 /* Only changed by ubd_init, which is an initcall. */
473 /* call ubd_finish if you need to serialize */
474 static void __ubd_finish(struct request
*req
, int error
)
482 nsect
= req
->current_nr_sectors
;
483 req
->sector
+= nsect
;
484 req
->buffer
+= nsect
<< 9;
486 req
->nr_sectors
-= nsect
;
487 req
->current_nr_sectors
= 0;
491 /* Callable only from interrupt context - otherwise you need to do
492 * spin_lock_irq()/spin_lock_irqsave() */
493 static inline void ubd_finish(struct request
*req
, int error
)
495 struct ubd
*dev
= req
->rq_disk
->private_data
;
497 spin_lock(&dev
->lock
);
498 __ubd_finish(req
, error
);
499 spin_unlock(&dev
->lock
);
502 /* XXX - move this inside ubd_intr. */
503 /* Called without dev->lock held, and only in interrupt context. */
504 static void ubd_handler(void)
506 struct io_thread_req req
;
512 n
= os_read_file(thread_fd
, &req
, sizeof(req
));
513 if(n
!= sizeof(req
)){
514 printk(KERN_ERR
"Pid %d - spurious interrupt in ubd_handler, "
515 "err = %d\n", os_getpid(), -n
);
520 dev
= rq
->rq_disk
->private_data
;
522 ubd_finish(rq
, req
.error
);
523 reactivate_fd(thread_fd
, UBD_IRQ
);
524 spin_lock(&dev
->lock
);
525 do_ubd_request(dev
->queue
);
526 spin_unlock(&dev
->lock
);
529 static irqreturn_t
ubd_intr(int irq
, void *dev
)
535 /* Only changed by ubd_init, which is an initcall. */
536 static int io_pid
= -1;
538 void kill_io_thread(void)
541 os_kill_process(io_pid
, 1);
544 __uml_exitcall(kill_io_thread
);
546 static inline int ubd_file_size(struct ubd
*ubd_dev
, __u64
*size_out
)
550 file
= ubd_dev
->cow
.file
? ubd_dev
->cow
.file
: ubd_dev
->file
;
551 return(os_file_size(file
, size_out
));
554 static void ubd_close_dev(struct ubd
*ubd_dev
)
556 os_close_file(ubd_dev
->fd
);
557 if(ubd_dev
->cow
.file
== NULL
)
560 os_close_file(ubd_dev
->cow
.fd
);
561 vfree(ubd_dev
->cow
.bitmap
);
562 ubd_dev
->cow
.bitmap
= NULL
;
565 static int ubd_open_dev(struct ubd
*ubd_dev
)
567 struct openflags flags
;
569 int err
, create_cow
, *create_ptr
;
572 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
574 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
575 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
577 fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
578 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
579 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
582 if((fd
== -ENOENT
) && create_cow
){
583 fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
584 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
585 &ubd_dev
->cow
.bitmap_offset
,
586 &ubd_dev
->cow
.bitmap_len
,
587 &ubd_dev
->cow
.data_offset
);
589 printk(KERN_INFO
"Creating \"%s\" as COW file for "
590 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
595 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
601 if(ubd_dev
->cow
.file
!= NULL
){
603 ubd_dev
->cow
.bitmap
= (void *) vmalloc(ubd_dev
->cow
.bitmap_len
);
604 if(ubd_dev
->cow
.bitmap
== NULL
){
605 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
608 flush_tlb_kernel_vm();
610 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
611 ubd_dev
->cow
.bitmap_offset
,
612 ubd_dev
->cow
.bitmap_len
);
616 flags
= ubd_dev
->openflags
;
618 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
619 NULL
, NULL
, NULL
, NULL
);
620 if(err
< 0) goto error
;
621 ubd_dev
->cow
.fd
= err
;
625 os_close_file(ubd_dev
->fd
);
629 static int ubd_disk_register(int major
, u64 size
, int unit
,
630 struct gendisk
**disk_out
)
632 struct gendisk
*disk
;
634 disk
= alloc_disk(1 << UBD_SHIFT
);
639 disk
->first_minor
= unit
<< UBD_SHIFT
;
640 disk
->fops
= &ubd_blops
;
641 set_capacity(disk
, size
/ 512);
642 if(major
== MAJOR_NR
)
643 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
645 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
647 /* sysfs register (not for ide fake devices) */
648 if (major
== MAJOR_NR
) {
649 ubd_devs
[unit
].pdev
.id
= unit
;
650 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
651 platform_device_register(&ubd_devs
[unit
].pdev
);
652 disk
->driverfs_dev
= &ubd_devs
[unit
].pdev
.dev
;
655 disk
->private_data
= &ubd_devs
[unit
];
656 disk
->queue
= ubd_devs
[unit
].queue
;
663 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
665 static int ubd_add(int n
, char **error_out
)
667 struct ubd
*ubd_dev
= &ubd_devs
[n
];
670 if(ubd_dev
->file
== NULL
)
673 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
675 *error_out
= "Couldn't determine size of device's file";
679 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
682 ubd_dev
->queue
= blk_init_queue(do_ubd_request
, &ubd_dev
->lock
);
683 if (ubd_dev
->queue
== NULL
) {
684 *error_out
= "Failed to initialize device queue";
687 ubd_dev
->queue
->queuedata
= ubd_dev
;
689 err
= ubd_disk_register(MAJOR_NR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
691 *error_out
= "Failed to register device";
695 if(fake_major
!= MAJOR_NR
)
696 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
699 /* perhaps this should also be under the "if (fake_major)" above */
700 /* using the fake_disk->disk_name and also the fakehd_set name */
702 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
709 blk_cleanup_queue(ubd_dev
->queue
);
713 static int ubd_config(char *str
, char **error_out
)
717 /* This string is possibly broken up and stored, so it's only
718 * freed if ubd_setup_common fails, or if only general options
721 str
= kstrdup(str
, GFP_KERNEL
);
723 *error_out
= "Failed to allocate memory";
727 ret
= ubd_setup_common(str
, &n
, error_out
);
736 mutex_lock(&ubd_lock
);
737 ret
= ubd_add(n
, error_out
);
739 ubd_devs
[n
].file
= NULL
;
740 mutex_unlock(&ubd_lock
);
750 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
755 n
= parse_unit(&name
);
756 if((n
>= MAX_DEV
) || (n
< 0)){
757 *error_out
= "ubd_get_config : device number out of range";
761 ubd_dev
= &ubd_devs
[n
];
762 mutex_lock(&ubd_lock
);
764 if(ubd_dev
->file
== NULL
){
765 CONFIG_CHUNK(str
, size
, len
, "", 1);
769 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
771 if(ubd_dev
->cow
.file
!= NULL
){
772 CONFIG_CHUNK(str
, size
, len
, ",", 0);
773 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
775 else CONFIG_CHUNK(str
, size
, len
, "", 1);
778 mutex_unlock(&ubd_lock
);
782 static int ubd_id(char **str
, int *start_out
, int *end_out
)
788 *end_out
= MAX_DEV
- 1;
792 static int ubd_remove(int n
, char **error_out
)
797 mutex_lock(&ubd_lock
);
799 if(ubd_gendisk
[n
] == NULL
)
802 ubd_dev
= &ubd_devs
[n
];
804 if(ubd_dev
->file
== NULL
)
807 /* you cannot remove a open disk */
809 if(ubd_dev
->count
> 0)
812 del_gendisk(ubd_gendisk
[n
]);
813 put_disk(ubd_gendisk
[n
]);
814 ubd_gendisk
[n
] = NULL
;
816 if(fake_gendisk
[n
] != NULL
){
817 del_gendisk(fake_gendisk
[n
]);
818 put_disk(fake_gendisk
[n
]);
819 fake_gendisk
[n
] = NULL
;
822 blk_cleanup_queue(ubd_dev
->queue
);
823 platform_device_unregister(&ubd_dev
->pdev
);
824 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
827 mutex_unlock(&ubd_lock
);
831 /* All these are called by mconsole in process context and without
832 * ubd-specific locks. The structure itself is const except for .list.
834 static struct mc_device ubd_mc
= {
835 .list
= LIST_HEAD_INIT(ubd_mc
.list
),
837 .config
= ubd_config
,
838 .get_config
= ubd_get_config
,
840 .remove
= ubd_remove
,
843 static int __init
ubd_mc_init(void)
845 mconsole_register_dev(&ubd_mc
);
849 __initcall(ubd_mc_init
);
851 static int __init
ubd0_init(void)
853 struct ubd
*ubd_dev
= &ubd_devs
[0];
855 mutex_lock(&ubd_lock
);
856 if(ubd_dev
->file
== NULL
)
857 ubd_dev
->file
= "root_fs";
858 mutex_unlock(&ubd_lock
);
863 __initcall(ubd0_init
);
865 /* Used in ubd_init, which is an initcall */
866 static struct platform_driver ubd_driver
= {
872 static int __init
ubd_init(void)
877 if (register_blkdev(MAJOR_NR
, "ubd"))
880 if (fake_major
!= MAJOR_NR
) {
881 char name
[sizeof("ubd_nnn\0")];
883 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
884 if (register_blkdev(fake_major
, "ubd"))
887 platform_driver_register(&ubd_driver
);
888 mutex_lock(&ubd_lock
);
889 for (i
= 0; i
< MAX_DEV
; i
++){
890 err
= ubd_add(i
, &error
);
892 printk(KERN_ERR
"Failed to initialize ubd device %d :"
895 mutex_unlock(&ubd_lock
);
899 late_initcall(ubd_init
);
901 static int __init
ubd_driver_init(void){
905 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
906 if(global_openflags
.s
){
907 printk(KERN_INFO
"ubd: Synchronous mode\n");
908 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
909 * enough. So use anyway the io thread. */
911 stack
= alloc_stack(0, 0);
912 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
916 "ubd : Failed to start I/O thread (errno = %d) - "
917 "falling back to synchronous I/O\n", -io_pid
);
921 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
922 IRQF_DISABLED
, "ubd", ubd_devs
);
924 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
928 device_initcall(ubd_driver_init
);
930 static int ubd_open(struct inode
*inode
, struct file
*filp
)
932 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
933 struct ubd
*ubd_dev
= disk
->private_data
;
936 if(ubd_dev
->count
== 0){
937 err
= ubd_open_dev(ubd_dev
);
939 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
940 disk
->disk_name
, ubd_dev
->file
, -err
);
945 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
947 /* This should no more be needed. And it didn't work anyway to exclude
948 * read-write remounting of filesystems.*/
949 /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
950 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
957 static int ubd_release(struct inode
* inode
, struct file
* file
)
959 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
960 struct ubd
*ubd_dev
= disk
->private_data
;
962 if(--ubd_dev
->count
== 0)
963 ubd_close_dev(ubd_dev
);
967 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
968 __u64
*cow_offset
, unsigned long *bitmap
,
969 __u64 bitmap_offset
, unsigned long *bitmap_words
,
972 __u64 sector
= io_offset
>> 9;
973 int i
, update_bitmap
= 0;
975 for(i
= 0; i
< length
>> 9; i
++){
977 ubd_set_bit(i
, (unsigned char *) cow_mask
);
978 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
982 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
988 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
990 /* This takes care of the case where we're exactly at the end of the
991 * device, and *cow_offset + 1 is off the end. So, just back it up
992 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
993 * for the original diagnosis.
995 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
996 sizeof(unsigned long) - 1))
999 bitmap_words
[0] = bitmap
[*cow_offset
];
1000 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
1002 *cow_offset
*= sizeof(unsigned long);
1003 *cow_offset
+= bitmap_offset
;
1006 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
1007 __u64 bitmap_offset
, __u64 bitmap_len
)
1009 __u64 sector
= req
->offset
>> 9;
1012 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
1013 panic("Operation too long");
1015 if(req
->op
== UBD_READ
) {
1016 for(i
= 0; i
< req
->length
>> 9; i
++){
1017 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1018 ubd_set_bit(i
, (unsigned char *)
1022 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1023 &req
->cow_offset
, bitmap
, bitmap_offset
,
1024 req
->bitmap_words
, bitmap_len
);
1027 /* Called with dev->lock held */
1028 static int prepare_request(struct request
*req
, struct io_thread_req
*io_req
)
1030 struct gendisk
*disk
= req
->rq_disk
;
1031 struct ubd
*ubd_dev
= disk
->private_data
;
1035 /* This should be impossible now */
1036 if((rq_data_dir(req
) == WRITE
) && !ubd_dev
->openflags
.w
){
1037 printk("Write attempted on readonly ubd device %s\n",
1039 end_request(req
, 0);
1043 offset
= ((__u64
) req
->sector
) << 9;
1044 len
= req
->current_nr_sectors
<< 9;
1047 io_req
->fds
[0] = (ubd_dev
->cow
.file
!= NULL
) ? ubd_dev
->cow
.fd
: ubd_dev
->fd
;
1048 io_req
->fds
[1] = ubd_dev
->fd
;
1049 io_req
->cow_offset
= -1;
1050 io_req
->offset
= offset
;
1051 io_req
->length
= len
;
1053 io_req
->sector_mask
= 0;
1055 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1056 io_req
->offsets
[0] = 0;
1057 io_req
->offsets
[1] = ubd_dev
->cow
.data_offset
;
1058 io_req
->buffer
= req
->buffer
;
1059 io_req
->sectorsize
= 1 << 9;
1061 if(ubd_dev
->cow
.file
!= NULL
)
1062 cowify_req(io_req
, ubd_dev
->cow
.bitmap
, ubd_dev
->cow
.bitmap_offset
,
1063 ubd_dev
->cow
.bitmap_len
);
1068 /* Called with dev->lock held */
1069 static void do_ubd_request(request_queue_t
*q
)
1071 struct io_thread_req io_req
;
1072 struct request
*req
;
1075 if(thread_fd
== -1){
1076 while((req
= elv_next_request(q
)) != NULL
){
1077 err
= prepare_request(req
, &io_req
);
1080 __ubd_finish(req
, io_req
.error
);
1085 if(do_ubd
|| (req
= elv_next_request(q
)) == NULL
)
1087 err
= prepare_request(req
, &io_req
);
1090 n
= os_write_file(thread_fd
, (char *) &io_req
,
1092 if(n
!= sizeof(io_req
))
1093 printk("write to io thread failed, "
1094 "errno = %d\n", -n
);
1099 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1101 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1105 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1109 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1110 unsigned int cmd
, unsigned long arg
)
1112 struct ubd
*ubd_dev
= inode
->i_bdev
->bd_disk
->private_data
;
1113 struct hd_driveid ubd_id
= {
1120 struct cdrom_volctrl volume
;
1121 case HDIO_GET_IDENTITY
:
1122 ubd_id
.cyls
= ubd_dev
->size
/ (128 * 32 * 512);
1123 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1129 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1131 volume
.channel0
= 255;
1132 volume
.channel1
= 255;
1133 volume
.channel2
= 255;
1134 volume
.channel3
= 255;
1135 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1142 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
1144 struct uml_stat buf1
, buf2
;
1147 if(from_cmdline
== NULL
)
1149 if(!strcmp(from_cmdline
, from_cow
))
1152 err
= os_stat_file(from_cmdline
, &buf1
);
1154 printk("Couldn't stat '%s', err = %d\n", from_cmdline
, -err
);
1157 err
= os_stat_file(from_cow
, &buf2
);
1159 printk("Couldn't stat '%s', err = %d\n", from_cow
, -err
);
1162 if((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
1165 printk("Backing file mismatch - \"%s\" requested,\n"
1166 "\"%s\" specified in COW header of \"%s\"\n",
1167 from_cmdline
, from_cow
, cow
);
1171 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
1173 unsigned long modtime
;
1174 unsigned long long actual
;
1177 err
= os_file_modtime(file
, &modtime
);
1179 printk("Failed to get modification time of backing file "
1180 "\"%s\", err = %d\n", file
, -err
);
1184 err
= os_file_size(file
, &actual
);
1186 printk("Failed to get size of backing file \"%s\", "
1187 "err = %d\n", file
, -err
);
1192 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1194 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1195 "file\n", (unsigned long long) size
, actual
);
1198 if(modtime
!= mtime
){
1199 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1200 "file\n", mtime
, modtime
);
1206 int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
1210 err
= os_seek_file(fd
, offset
);
1214 err
= os_read_file(fd
, buf
, len
);
1221 int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
1222 char **backing_file_out
, int *bitmap_offset_out
,
1223 unsigned long *bitmap_len_out
, int *data_offset_out
,
1224 int *create_cow_out
)
1227 unsigned long long size
;
1228 __u32 version
, align
;
1230 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
1232 fd
= os_open_file(file
, *openflags
, mode
);
1234 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
1235 *create_cow_out
= 1;
1236 if (!openflags
->w
||
1237 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
1240 fd
= os_open_file(file
, *openflags
, mode
);
1246 printk("Not locking \"%s\" on the host\n", file
);
1248 err
= os_lock_file(fd
, openflags
->w
);
1250 printk("Failed to lock '%s', err = %d\n", file
, -err
);
1255 /* Successful return case! */
1256 if(backing_file_out
== NULL
)
1259 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
1260 &size
, §orsize
, &align
, bitmap_offset_out
);
1261 if(err
&& (*backing_file_out
!= NULL
)){
1262 printk("Failed to read COW header from COW file \"%s\", "
1263 "errno = %d\n", file
, -err
);
1269 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
, file
);
1271 /* Allow switching only if no mismatch. */
1272 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
, mtime
)) {
1273 printk("Switching backing file to '%s'\n", *backing_file_out
);
1274 err
= write_cow_header(file
, fd
, *backing_file_out
,
1275 sectorsize
, align
, &size
);
1277 printk("Switch failed, errno = %d\n", -err
);
1281 *backing_file_out
= backing_file
;
1282 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
1287 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
1288 bitmap_len_out
, data_offset_out
);
1296 int create_cow_file(char *cow_file
, char *backing_file
, struct openflags flags
,
1297 int sectorsize
, int alignment
, int *bitmap_offset_out
,
1298 unsigned long *bitmap_len_out
, int *data_offset_out
)
1303 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
1306 printk("Open of COW file '%s' failed, errno = %d\n", cow_file
,
1311 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
1312 bitmap_offset_out
, bitmap_len_out
,
1321 static int update_bitmap(struct io_thread_req
*req
)
1325 if(req
->cow_offset
== -1)
1328 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1330 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1334 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1335 sizeof(req
->bitmap_words
));
1336 if(n
!= sizeof(req
->bitmap_words
)){
1337 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1345 void do_io(struct io_thread_req
*req
)
1349 int n
, nsectors
, start
, end
, bit
;
1353 nsectors
= req
->length
/ req
->sectorsize
;
1356 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1358 while((end
< nsectors
) &&
1359 (ubd_test_bit(end
, (unsigned char *)
1360 &req
->sector_mask
) == bit
))
1363 off
= req
->offset
+ req
->offsets
[bit
] +
1364 start
* req
->sectorsize
;
1365 len
= (end
- start
) * req
->sectorsize
;
1366 buf
= &req
->buffer
[start
* req
->sectorsize
];
1368 err
= os_seek_file(req
->fds
[bit
], off
);
1370 printk("do_io - lseek failed : err = %d\n", -err
);
1374 if(req
->op
== UBD_READ
){
1379 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1381 printk("do_io - read failed, err = %d "
1382 "fd = %d\n", -n
, req
->fds
[bit
]);
1386 } while((n
< len
) && (n
!= 0));
1387 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1389 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1391 printk("do_io - write failed err = %d "
1392 "fd = %d\n", -n
, req
->fds
[bit
]);
1399 } while(start
< nsectors
);
1401 req
->error
= update_bitmap(req
);
1404 /* Changed in start_io_thread, which is serialized by being called only
1405 * from ubd_init, which is an initcall.
1409 /* Only changed by the io thread. XXX: currently unused. */
1410 static int io_count
= 0;
1412 int io_thread(void *arg
)
1414 struct io_thread_req req
;
1417 ignore_sigwinch_sig();
1419 n
= os_read_file(kernel_fd
, &req
, sizeof(req
));
1420 if(n
!= sizeof(req
)){
1422 printk("io_thread - read failed, fd = %d, "
1423 "err = %d\n", kernel_fd
, -n
);
1425 printk("io_thread - short read, fd = %d, "
1426 "length = %d\n", kernel_fd
, n
);
1432 n
= os_write_file(kernel_fd
, &req
, sizeof(req
));
1433 if(n
!= sizeof(req
))
1434 printk("io_thread - write failed, fd = %d, err = %d\n",