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 static DEFINE_SPINLOCK(ubd_io_lock);
110 static DEFINE_SPINLOCK(ubd_lock);
112 static void (*do_ubd)(void);
114 static int ubd_open(struct inode * inode, struct file * filp);
115 static int ubd_release(struct inode * inode, struct file * file);
116 static int ubd_ioctl(struct inode * inode, struct file * file,
117 unsigned int cmd, unsigned long arg);
118 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
122 static struct block_device_operations ubd_blops = {
123 .owner = THIS_MODULE,
125 .release = ubd_release,
127 .getgeo = ubd_getgeo,
130 /* Protected by the queue_lock */
131 static request_queue_t *ubd_queue;
133 /* Protected by ubd_lock */
134 static int fake_major = MAJOR_NR;
136 static struct gendisk *ubd_gendisk[MAX_DEV];
137 static struct gendisk *fake_gendisk[MAX_DEV];
139 #ifdef CONFIG_BLK_DEV_UBD_SYNC
140 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
143 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
147 /* Not protected - changed only in ubd_setup_common and then only to
150 static struct openflags global_openflags = OPEN_FLAGS;
153 /* backing file name */
155 /* backing file fd */
157 unsigned long *bitmap;
158 unsigned long bitmap_len;
164 /* name (and fd, below) of the file opened for writing, either the
165 * backing or the cow file. */
170 struct openflags boot_openflags;
171 struct openflags openflags;
175 struct platform_device pdev;
178 #define DEFAULT_COW { \
182 .bitmap_offset = 0, \
186 #define DEFAULT_UBD { \
191 .boot_openflags = OPEN_FLAGS, \
192 .openflags = OPEN_FLAGS, \
195 .cow = DEFAULT_COW, \
198 struct ubd ubd_devs[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
200 static int ubd0_init(void)
202 struct ubd *ubd_dev = &ubd_devs[0];
204 if(ubd_dev->file == NULL)
205 ubd_dev->file = "root_fs";
209 __initcall(ubd0_init);
211 /* Only changed by fake_ide_setup which is a setup */
212 static int fake_ide = 0;
213 static struct proc_dir_entry *proc_ide_root = NULL;
214 static struct proc_dir_entry *proc_ide = NULL;
216 static void make_proc_ide(void)
218 proc_ide_root = proc_mkdir("ide", NULL);
219 proc_ide = proc_mkdir("ide0", proc_ide_root);
222 static int proc_ide_read_media(char *page, char **start, off_t off, int count,
223 int *eof, void *data)
227 strcpy(page, "disk\n");
228 len = strlen("disk\n");
232 if (len <= 0) return 0;
239 static void make_ide_entries(char *dev_name)
241 struct proc_dir_entry *dir, *ent;
244 if(proc_ide_root == NULL) make_proc_ide();
246 dir = proc_mkdir(dev_name, proc_ide);
249 ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
253 ent->read_proc = proc_ide_read_media;
254 ent->write_proc = NULL;
255 sprintf(name,"ide0/%s", dev_name);
256 proc_symlink(dev_name, proc_ide_root, name);
259 static int fake_ide_setup(char *str)
265 __setup("fake_ide", fake_ide_setup);
267 __uml_help(fake_ide_setup,
269 " Create ide0 entries that map onto ubd devices.\n\n"
272 static int parse_unit(char **ptr)
274 char *str = *ptr, *end;
278 n = simple_strtoul(str, &end, 0);
283 else if (('a' <= *str) && (*str <= 'z')) {
291 static int ubd_setup_common(char *str, int *index_out)
294 struct openflags flags = global_openflags;
298 if(index_out) *index_out = -1;
305 if(!strcmp(str, "sync")){
306 global_openflags = of_sync(global_openflags);
309 major = simple_strtoul(str, &end, 0);
310 if((*end != '\0') || (end == str)){
312 "ubd_setup : didn't parse major number\n");
317 spin_lock(&ubd_lock);
318 if(fake_major != MAJOR_NR){
319 printk(KERN_ERR "Can't assign a fake major twice\n");
325 printk(KERN_INFO "Setting extra ubd major number to %d\n",
329 spin_unlock(&ubd_lock);
333 n = parse_unit(&str);
335 printk(KERN_ERR "ubd_setup : couldn't parse unit number "
340 printk(KERN_ERR "ubd_setup : index %d out of range "
341 "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
346 spin_lock(&ubd_lock);
348 ubd_dev = &ubd_devs[n];
349 if(ubd_dev->file != NULL){
350 printk(KERN_ERR "ubd_setup : device already configured\n");
357 for (i = 0; i < sizeof("rscd="); i++) {
375 printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r, s, c, or d)\n");
382 printk(KERN_ERR "ubd_setup : Too many flags specified\n");
384 printk(KERN_ERR "ubd_setup : Expected '='\n");
389 backing_file = strchr(str, ',');
392 backing_file = strchr(str, ':');
397 printk(KERN_ERR "Can't specify both 'd' and a "
400 *backing_file = '\0';
405 ubd_dev->cow.file = backing_file;
406 ubd_dev->boot_openflags = flags;
408 spin_unlock(&ubd_lock);
412 static int ubd_setup(char *str)
414 ubd_setup_common(str, NULL);
418 __setup("ubd", ubd_setup);
419 __uml_help(ubd_setup,
420 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
421 " This is used to associate a device with a file in the underlying\n"
422 " filesystem. When specifying two filenames, the first one is the\n"
423 " COW name and the second is the backing file name. As separator you can\n"
424 " use either a ':' or a ',': the first one allows writing things like;\n"
425 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
426 " while with a ',' the shell would not expand the 2nd '~'.\n"
427 " When using only one filename, UML will detect whether to thread it like\n"
428 " a COW file or a backing file. To override this detection, add the 'd'\n"
430 " ubd0d=BackingFile\n"
431 " Usually, there is a filesystem in the file, but \n"
432 " that's not required. Swap devices containing swap files can be\n"
433 " specified like this. Also, a file which doesn't contain a\n"
434 " filesystem can have its contents read in the virtual \n"
435 " machine by running 'dd' on the device. <n> must be in the range\n"
436 " 0 to 7. Appending an 'r' to the number will cause that device\n"
437 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
438 " an 's' will cause data to be written to disk on the host immediately.\n\n"
441 static int udb_setup(char *str)
443 printk("udb%s specified on command line is almost certainly a ubd -> "
448 __setup("udb", udb_setup);
449 __uml_help(udb_setup,
451 " This option is here solely to catch ubd -> udb typos, which can be\n"
452 " to impossible to catch visually unless you specifically look for\n"
453 " them. The only result of any option starting with 'udb' is an error\n"
454 " in the boot output.\n\n"
457 static int fakehd_set = 0;
458 static int fakehd(char *str)
460 printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
465 __setup("fakehd", fakehd);
468 " Change the ubd device name to \"hd\".\n\n"
471 static void do_ubd_request(request_queue_t * q);
473 /* Only changed by ubd_init, which is an initcall. */
476 /* Changed by ubd_handler, which is serialized because interrupts only
481 /* call ubd_finish if you need to serialize */
482 static void __ubd_finish(struct request *req, int error)
490 nsect = req->current_nr_sectors;
491 req->sector += nsect;
492 req->buffer += nsect << 9;
494 req->nr_sectors -= nsect;
495 req->current_nr_sectors = 0;
499 static inline void ubd_finish(struct request *req, int error)
501 spin_lock(&ubd_io_lock);
502 __ubd_finish(req, error);
503 spin_unlock(&ubd_io_lock);
506 /* Called without ubd_io_lock held */
507 static void ubd_handler(void)
509 struct io_thread_req req;
510 struct request *rq = elv_next_request(ubd_queue);
515 n = os_read_file(thread_fd, &req, sizeof(req));
516 if(n != sizeof(req)){
517 printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
518 "err = %d\n", os_getpid(), -n);
519 spin_lock(&ubd_io_lock);
521 spin_unlock(&ubd_io_lock);
525 ubd_finish(rq, req.error);
526 reactivate_fd(thread_fd, UBD_IRQ);
527 do_ubd_request(ubd_queue);
530 static irqreturn_t ubd_intr(int irq, void *dev)
536 /* Only changed by ubd_init, which is an initcall. */
537 static int io_pid = -1;
539 void kill_io_thread(void)
542 os_kill_process(io_pid, 1);
545 __uml_exitcall(kill_io_thread);
547 static int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
551 file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file;
552 return(os_file_size(file, size_out));
555 static void ubd_close(struct ubd *ubd_dev)
557 os_close_file(ubd_dev->fd);
558 if(ubd_dev->cow.file == NULL)
561 os_close_file(ubd_dev->cow.fd);
562 vfree(ubd_dev->cow.bitmap);
563 ubd_dev->cow.bitmap = NULL;
566 static int ubd_open_dev(struct ubd *ubd_dev)
568 struct openflags flags;
570 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;
576 ubd_dev->fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
577 back_ptr, &ubd_dev->cow.bitmap_offset,
578 &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
581 if((ubd_dev->fd == -ENOENT) && create_cow){
582 ubd_dev->fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
583 ubd_dev->openflags, 1 << 9, PAGE_SIZE,
584 &ubd_dev->cow.bitmap_offset,
585 &ubd_dev->cow.bitmap_len,
586 &ubd_dev->cow.data_offset);
587 if(ubd_dev->fd >= 0){
588 printk(KERN_INFO "Creating \"%s\" as COW file for "
589 "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
594 printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
599 if(ubd_dev->cow.file != NULL){
601 ubd_dev->cow.bitmap = (void *) vmalloc(ubd_dev->cow.bitmap_len);
602 if(ubd_dev->cow.bitmap == NULL){
603 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
606 flush_tlb_kernel_vm();
608 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
609 ubd_dev->cow.bitmap_offset,
610 ubd_dev->cow.bitmap_len);
614 flags = ubd_dev->openflags;
616 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
617 NULL, NULL, NULL, NULL);
618 if(err < 0) goto error;
619 ubd_dev->cow.fd = err;
623 os_close_file(ubd_dev->fd);
627 static int ubd_new_disk(int major, u64 size, int unit,
628 struct gendisk **disk_out)
631 struct gendisk *disk;
633 disk = alloc_disk(1 << UBD_SHIFT);
638 disk->first_minor = unit << UBD_SHIFT;
639 disk->fops = &ubd_blops;
640 set_capacity(disk, size / 512);
641 if(major == MAJOR_NR)
642 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
644 sprintf(disk->disk_name, "ubd_fake%d", unit);
646 /* sysfs register (not for ide fake devices) */
647 if (major == MAJOR_NR) {
648 ubd_devs[unit].pdev.id = unit;
649 ubd_devs[unit].pdev.name = DRIVER_NAME;
650 platform_device_register(&ubd_devs[unit].pdev);
651 disk->driverfs_dev = &ubd_devs[unit].pdev.dev;
654 disk->private_data = &ubd_devs[unit];
655 disk->queue = ubd_queue;
662 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
664 static int ubd_add(int n)
666 struct ubd *ubd_dev = &ubd_devs[n];
670 if(ubd_dev->file == NULL)
673 err = ubd_file_size(ubd_dev, &ubd_dev->size);
677 ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
679 err = ubd_new_disk(MAJOR_NR, ubd_dev->size, n, &ubd_gendisk[n]);
683 if(fake_major != MAJOR_NR)
684 ubd_new_disk(fake_major, ubd_dev->size, n,
687 /* perhaps this should also be under the "if (fake_major)" above */
688 /* using the fake_disk->disk_name and also the fakehd_set name */
690 make_ide_entries(ubd_gendisk[n]->disk_name);
697 static int ubd_config(char *str)
701 str = kstrdup(str, GFP_KERNEL);
703 printk(KERN_ERR "ubd_config failed to strdup string\n");
706 err = ubd_setup_common(str, &n);
711 if(n == -1) return(0);
713 spin_lock(&ubd_lock);
716 ubd_devs[n].file = NULL;
717 spin_unlock(&ubd_lock);
722 static int ubd_get_config(char *name, char *str, int size, char **error_out)
727 n = parse_unit(&name);
728 if((n >= MAX_DEV) || (n < 0)){
729 *error_out = "ubd_get_config : device number out of range";
733 ubd_dev = &ubd_devs[n];
734 spin_lock(&ubd_lock);
736 if(ubd_dev->file == NULL){
737 CONFIG_CHUNK(str, size, len, "", 1);
741 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
743 if(ubd_dev->cow.file != NULL){
744 CONFIG_CHUNK(str, size, len, ",", 0);
745 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
747 else CONFIG_CHUNK(str, size, len, "", 1);
750 spin_unlock(&ubd_lock);
754 static int ubd_id(char **str, int *start_out, int *end_out)
760 *end_out = MAX_DEV - 1;
764 static int ubd_remove(int n)
769 spin_lock(&ubd_lock);
771 if(ubd_gendisk[n] == NULL)
774 ubd_dev = &ubd_devs[n];
776 if(ubd_dev->file == NULL)
779 /* you cannot remove a open disk */
781 if(ubd_dev->count > 0)
784 del_gendisk(ubd_gendisk[n]);
785 put_disk(ubd_gendisk[n]);
786 ubd_gendisk[n] = NULL;
788 if(fake_gendisk[n] != NULL){
789 del_gendisk(fake_gendisk[n]);
790 put_disk(fake_gendisk[n]);
791 fake_gendisk[n] = NULL;
794 platform_device_unregister(&ubd_dev->pdev);
795 *ubd_dev = ((struct ubd) DEFAULT_UBD);
798 spin_unlock(&ubd_lock);
802 static struct mc_device ubd_mc = {
804 .config = ubd_config,
805 .get_config = ubd_get_config,
807 .remove = ubd_remove,
810 static int ubd_mc_init(void)
812 mconsole_register_dev(&ubd_mc);
816 __initcall(ubd_mc_init);
818 static struct platform_driver ubd_driver = {
828 if (register_blkdev(MAJOR_NR, "ubd"))
831 ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
833 unregister_blkdev(MAJOR_NR, "ubd");
837 if (fake_major != MAJOR_NR) {
838 char name[sizeof("ubd_nnn\0")];
840 snprintf(name, sizeof(name), "ubd_%d", fake_major);
841 if (register_blkdev(fake_major, "ubd"))
844 platform_driver_register(&ubd_driver);
845 for (i = 0; i < MAX_DEV; i++)
850 late_initcall(ubd_init);
852 int ubd_driver_init(void){
856 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
857 if(global_openflags.s){
858 printk(KERN_INFO "ubd: Synchronous mode\n");
859 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
860 * enough. So use anyway the io thread. */
862 stack = alloc_stack(0, 0);
863 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
867 "ubd : Failed to start I/O thread (errno = %d) - "
868 "falling back to synchronous I/O\n", -io_pid);
872 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
873 IRQF_DISABLED, "ubd", ubd_devs);
875 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
879 device_initcall(ubd_driver_init);
881 static int ubd_open(struct inode *inode, struct file *filp)
883 struct gendisk *disk = inode->i_bdev->bd_disk;
884 struct ubd *ubd_dev = disk->private_data;
887 if(ubd_dev->count == 0){
888 err = ubd_open_dev(ubd_dev);
890 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
891 disk->disk_name, ubd_dev->file, -err);
896 set_disk_ro(disk, !ubd_dev->openflags.w);
898 /* This should no more be needed. And it didn't work anyway to exclude
899 * read-write remounting of filesystems.*/
900 /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
901 if(--ubd_dev->count == 0) ubd_close(ubd_dev);
908 static int ubd_release(struct inode * inode, struct file * file)
910 struct gendisk *disk = inode->i_bdev->bd_disk;
911 struct ubd *ubd_dev = disk->private_data;
913 if(--ubd_dev->count == 0)
918 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
919 __u64 *cow_offset, unsigned long *bitmap,
920 __u64 bitmap_offset, unsigned long *bitmap_words,
923 __u64 sector = io_offset >> 9;
924 int i, update_bitmap = 0;
926 for(i = 0; i < length >> 9; i++){
928 ubd_set_bit(i, (unsigned char *) cow_mask);
929 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
933 ubd_set_bit(sector + i, (unsigned char *) bitmap);
939 *cow_offset = sector / (sizeof(unsigned long) * 8);
941 /* This takes care of the case where we're exactly at the end of the
942 * device, and *cow_offset + 1 is off the end. So, just back it up
943 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
944 * for the original diagnosis.
946 if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
947 sizeof(unsigned long) - 1))
950 bitmap_words[0] = bitmap[*cow_offset];
951 bitmap_words[1] = bitmap[*cow_offset + 1];
953 *cow_offset *= sizeof(unsigned long);
954 *cow_offset += bitmap_offset;
957 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
958 __u64 bitmap_offset, __u64 bitmap_len)
960 __u64 sector = req->offset >> 9;
963 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
964 panic("Operation too long");
966 if(req->op == UBD_READ) {
967 for(i = 0; i < req->length >> 9; i++){
968 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
969 ubd_set_bit(i, (unsigned char *)
973 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
974 &req->cow_offset, bitmap, bitmap_offset,
975 req->bitmap_words, bitmap_len);
978 /* Called with ubd_io_lock held */
979 static int prepare_request(struct request *req, struct io_thread_req *io_req)
981 struct gendisk *disk = req->rq_disk;
982 struct ubd *ubd_dev = disk->private_data;
986 /* This should be impossible now */
987 if((rq_data_dir(req) == WRITE) && !ubd_dev->openflags.w){
988 printk("Write attempted on readonly ubd device %s\n",
994 offset = ((__u64) req->sector) << 9;
995 len = req->current_nr_sectors << 9;
997 io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd : ubd_dev->fd;
998 io_req->fds[1] = ubd_dev->fd;
999 io_req->cow_offset = -1;
1000 io_req->offset = offset;
1001 io_req->length = len;
1003 io_req->sector_mask = 0;
1005 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1006 io_req->offsets[0] = 0;
1007 io_req->offsets[1] = ubd_dev->cow.data_offset;
1008 io_req->buffer = req->buffer;
1009 io_req->sectorsize = 1 << 9;
1011 if(ubd_dev->cow.file != NULL)
1012 cowify_req(io_req, ubd_dev->cow.bitmap, ubd_dev->cow.bitmap_offset,
1013 ubd_dev->cow.bitmap_len);
1018 /* Called with ubd_io_lock held */
1019 static void do_ubd_request(request_queue_t *q)
1021 struct io_thread_req io_req;
1022 struct request *req;
1025 if(thread_fd == -1){
1026 while((req = elv_next_request(q)) != NULL){
1027 err = prepare_request(req, &io_req);
1030 __ubd_finish(req, io_req.error);
1035 if(do_ubd || (req = elv_next_request(q)) == NULL)
1037 err = prepare_request(req, &io_req);
1039 do_ubd = ubd_handler;
1040 n = os_write_file(thread_fd, (char *) &io_req,
1042 if(n != sizeof(io_req))
1043 printk("write to io thread failed, "
1044 "errno = %d\n", -n);
1049 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1051 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1055 geo->cylinders = ubd_dev->size / (128 * 32 * 512);
1059 static int ubd_ioctl(struct inode * inode, struct file * file,
1060 unsigned int cmd, unsigned long arg)
1062 struct ubd *ubd_dev = inode->i_bdev->bd_disk->private_data;
1063 struct hd_driveid ubd_id = {
1070 struct cdrom_volctrl volume;
1071 case HDIO_GET_IDENTITY:
1072 ubd_id.cyls = ubd_dev->size / (128 * 32 * 512);
1073 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1079 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1081 volume.channel0 = 255;
1082 volume.channel1 = 255;
1083 volume.channel2 = 255;
1084 volume.channel3 = 255;
1085 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1092 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
1094 struct uml_stat buf1, buf2;
1097 if(from_cmdline == NULL)
1099 if(!strcmp(from_cmdline, from_cow))
1102 err = os_stat_file(from_cmdline, &buf1);
1104 printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
1107 err = os_stat_file(from_cow, &buf2);
1109 printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
1112 if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
1115 printk("Backing file mismatch - \"%s\" requested,\n"
1116 "\"%s\" specified in COW header of \"%s\"\n",
1117 from_cmdline, from_cow, cow);
1121 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
1123 unsigned long modtime;
1124 unsigned long long actual;
1127 err = os_file_modtime(file, &modtime);
1129 printk("Failed to get modification time of backing file "
1130 "\"%s\", err = %d\n", file, -err);
1134 err = os_file_size(file, &actual);
1136 printk("Failed to get size of backing file \"%s\", "
1137 "err = %d\n", file, -err);
1142 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1144 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1145 "file\n", (unsigned long long) size, actual);
1148 if(modtime != mtime){
1149 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1150 "file\n", mtime, modtime);
1156 int read_cow_bitmap(int fd, void *buf, int offset, int len)
1160 err = os_seek_file(fd, offset);
1164 err = os_read_file(fd, buf, len);
1171 int open_ubd_file(char *file, struct openflags *openflags, int shared,
1172 char **backing_file_out, int *bitmap_offset_out,
1173 unsigned long *bitmap_len_out, int *data_offset_out,
1174 int *create_cow_out)
1177 unsigned long long size;
1178 __u32 version, align;
1180 int fd, err, sectorsize, asked_switch, mode = 0644;
1182 fd = os_open_file(file, *openflags, mode);
1184 if ((fd == -ENOENT) && (create_cow_out != NULL))
1185 *create_cow_out = 1;
1186 if (!openflags->w ||
1187 ((fd != -EROFS) && (fd != -EACCES)))
1190 fd = os_open_file(file, *openflags, mode);
1196 printk("Not locking \"%s\" on the host\n", file);
1198 err = os_lock_file(fd, openflags->w);
1200 printk("Failed to lock '%s', err = %d\n", file, -err);
1205 /* Successful return case! */
1206 if(backing_file_out == NULL)
1209 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
1210 &size, §orsize, &align, bitmap_offset_out);
1211 if(err && (*backing_file_out != NULL)){
1212 printk("Failed to read COW header from COW file \"%s\", "
1213 "errno = %d\n", file, -err);
1219 asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
1221 /* Allow switching only if no mismatch. */
1222 if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
1223 printk("Switching backing file to '%s'\n", *backing_file_out);
1224 err = write_cow_header(file, fd, *backing_file_out,
1225 sectorsize, align, &size);
1227 printk("Switch failed, errno = %d\n", -err);
1231 *backing_file_out = backing_file;
1232 err = backing_file_mismatch(*backing_file_out, size, mtime);
1237 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
1238 bitmap_len_out, data_offset_out);
1246 int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
1247 int sectorsize, int alignment, int *bitmap_offset_out,
1248 unsigned long *bitmap_len_out, int *data_offset_out)
1253 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
1256 printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
1261 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
1262 bitmap_offset_out, bitmap_len_out,
1271 static int update_bitmap(struct io_thread_req *req)
1275 if(req->cow_offset == -1)
1278 n = os_seek_file(req->fds[1], req->cow_offset);
1280 printk("do_io - bitmap lseek failed : err = %d\n", -n);
1284 n = os_write_file(req->fds[1], &req->bitmap_words,
1285 sizeof(req->bitmap_words));
1286 if(n != sizeof(req->bitmap_words)){
1287 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
1295 void do_io(struct io_thread_req *req)
1299 int n, nsectors, start, end, bit;
1303 nsectors = req->length / req->sectorsize;
1306 bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
1308 while((end < nsectors) &&
1309 (ubd_test_bit(end, (unsigned char *)
1310 &req->sector_mask) == bit))
1313 off = req->offset + req->offsets[bit] +
1314 start * req->sectorsize;
1315 len = (end - start) * req->sectorsize;
1316 buf = &req->buffer[start * req->sectorsize];
1318 err = os_seek_file(req->fds[bit], off);
1320 printk("do_io - lseek failed : err = %d\n", -err);
1324 if(req->op == UBD_READ){
1329 n = os_read_file(req->fds[bit], buf, len);
1331 printk("do_io - read failed, err = %d "
1332 "fd = %d\n", -n, req->fds[bit]);
1336 } while((n < len) && (n != 0));
1337 if (n < len) memset(&buf[n], 0, len - n);
1339 n = os_write_file(req->fds[bit], buf, len);
1341 printk("do_io - write failed err = %d "
1342 "fd = %d\n", -n, req->fds[bit]);
1349 } while(start < nsectors);
1351 req->error = update_bitmap(req);
1354 /* Changed in start_io_thread, which is serialized by being called only
1355 * from ubd_init, which is an initcall.
1359 /* Only changed by the io thread */
1362 int io_thread(void *arg)
1364 struct io_thread_req req;
1367 ignore_sigwinch_sig();
1369 n = os_read_file(kernel_fd, &req, sizeof(req));
1370 if(n != sizeof(req)){
1372 printk("io_thread - read failed, fd = %d, "
1373 "err = %d\n", kernel_fd, -n);
1375 printk("io_thread - short read, fd = %d, "
1376 "length = %d\n", kernel_fd, n);
1382 n = os_write_file(kernel_fd, &req, sizeof(req));
1383 if(n != sizeof(req))
1384 printk("io_thread - write failed, fd = %d, err = %d\n",