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)
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)){
310 "ubd_setup : didn't parse major number\n");
315 mutex_lock(&ubd_lock);
316 if(fake_major != MAJOR_NR){
317 printk(KERN_ERR "Can't assign a fake major twice\n");
323 printk(KERN_INFO "Setting extra ubd major number to %d\n",
327 mutex_unlock(&ubd_lock);
331 n = parse_unit(&str);
333 printk(KERN_ERR "ubd_setup : couldn't parse unit number "
338 printk(KERN_ERR "ubd_setup : index %d out of range "
339 "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
344 mutex_lock(&ubd_lock);
346 ubd_dev = &ubd_devs[n];
347 if(ubd_dev->file != NULL){
348 printk(KERN_ERR "ubd_setup : device already configured\n");
355 for (i = 0; i < sizeof("rscd="); i++) {
373 printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r, s, c, or d)\n");
380 printk(KERN_ERR "ubd_setup : Too many flags specified\n");
382 printk(KERN_ERR "ubd_setup : Expected '='\n");
387 backing_file = strchr(str, ',');
390 backing_file = strchr(str, ':');
395 printk(KERN_ERR "Can't specify both 'd' and a "
398 *backing_file = '\0';
403 ubd_dev->cow.file = backing_file;
404 ubd_dev->boot_openflags = flags;
406 mutex_unlock(&ubd_lock);
410 static int ubd_setup(char *str)
412 ubd_setup_common(str, NULL);
416 __setup("ubd", ubd_setup);
417 __uml_help(ubd_setup,
418 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
419 " This is used to associate a device with a file in the underlying\n"
420 " filesystem. When specifying two filenames, the first one is the\n"
421 " COW name and the second is the backing file name. As separator you can\n"
422 " use either a ':' or a ',': the first one allows writing things like;\n"
423 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
424 " while with a ',' the shell would not expand the 2nd '~'.\n"
425 " When using only one filename, UML will detect whether to thread it like\n"
426 " a COW file or a backing file. To override this detection, add the 'd'\n"
428 " ubd0d=BackingFile\n"
429 " Usually, there is a filesystem in the file, but \n"
430 " that's not required. Swap devices containing swap files can be\n"
431 " specified like this. Also, a file which doesn't contain a\n"
432 " filesystem can have its contents read in the virtual \n"
433 " machine by running 'dd' on the device. <n> must be in the range\n"
434 " 0 to 7. Appending an 'r' to the number will cause that device\n"
435 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
436 " an 's' will cause data to be written to disk on the host immediately.\n\n"
439 static int udb_setup(char *str)
441 printk("udb%s specified on command line is almost certainly a ubd -> "
446 __setup("udb", udb_setup);
447 __uml_help(udb_setup,
449 " This option is here solely to catch ubd -> udb typos, which can be\n"
450 " to impossible to catch visually unless you specifically look for\n"
451 " them. The only result of any option starting with 'udb' is an error\n"
452 " in the boot output.\n\n"
455 static int fakehd_set = 0;
456 static int fakehd(char *str)
458 printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
463 __setup("fakehd", fakehd);
466 " Change the ubd device name to \"hd\".\n\n"
469 static void do_ubd_request(request_queue_t * q);
471 /* Only changed by ubd_init, which is an initcall. */
474 /* Changed by ubd_handler, which is serialized because interrupts only
476 * XXX: currently unused.
478 static int intr_count = 0;
480 /* call ubd_finish if you need to serialize */
481 static void __ubd_finish(struct request *req, int error)
489 nsect = req->current_nr_sectors;
490 req->sector += nsect;
491 req->buffer += nsect << 9;
493 req->nr_sectors -= nsect;
494 req->current_nr_sectors = 0;
498 /* Callable only from interrupt context - otherwise you need to do
499 * spin_lock_irq()/spin_lock_irqsave() */
500 static inline void ubd_finish(struct request *req, int error)
502 spin_lock(&ubd_io_lock);
503 __ubd_finish(req, error);
504 spin_unlock(&ubd_io_lock);
507 /* XXX - move this inside ubd_intr. */
508 /* Called without ubd_io_lock held, and only in interrupt context. */
509 static void ubd_handler(void)
511 struct io_thread_req req;
512 struct request *rq = elv_next_request(ubd_queue);
517 n = os_read_file(thread_fd, &req, sizeof(req));
518 if(n != sizeof(req)){
519 printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
520 "err = %d\n", os_getpid(), -n);
521 spin_lock(&ubd_io_lock);
523 spin_unlock(&ubd_io_lock);
527 ubd_finish(rq, req.error);
528 reactivate_fd(thread_fd, UBD_IRQ);
529 spin_lock(&ubd_io_lock);
530 do_ubd_request(ubd_queue);
531 spin_unlock(&ubd_io_lock);
534 static irqreturn_t ubd_intr(int irq, void *dev)
540 /* Only changed by ubd_init, which is an initcall. */
541 static int io_pid = -1;
543 void kill_io_thread(void)
546 os_kill_process(io_pid, 1);
549 __uml_exitcall(kill_io_thread);
551 static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
555 file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file;
556 return(os_file_size(file, size_out));
559 static void ubd_close_dev(struct ubd *ubd_dev)
561 os_close_file(ubd_dev->fd);
562 if(ubd_dev->cow.file == NULL)
565 os_close_file(ubd_dev->cow.fd);
566 vfree(ubd_dev->cow.bitmap);
567 ubd_dev->cow.bitmap = NULL;
570 static int ubd_open_dev(struct ubd *ubd_dev)
572 struct openflags flags;
574 int err, create_cow, *create_ptr;
577 ubd_dev->openflags = ubd_dev->boot_openflags;
579 create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
580 back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
582 fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
583 back_ptr, &ubd_dev->cow.bitmap_offset,
584 &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
587 if((fd == -ENOENT) && create_cow){
588 fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
589 ubd_dev->openflags, 1 << 9, PAGE_SIZE,
590 &ubd_dev->cow.bitmap_offset,
591 &ubd_dev->cow.bitmap_len,
592 &ubd_dev->cow.data_offset);
594 printk(KERN_INFO "Creating \"%s\" as COW file for "
595 "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
600 printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
606 if(ubd_dev->cow.file != NULL){
608 ubd_dev->cow.bitmap = (void *) vmalloc(ubd_dev->cow.bitmap_len);
609 if(ubd_dev->cow.bitmap == NULL){
610 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
613 flush_tlb_kernel_vm();
615 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
616 ubd_dev->cow.bitmap_offset,
617 ubd_dev->cow.bitmap_len);
621 flags = ubd_dev->openflags;
623 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
624 NULL, NULL, NULL, NULL);
625 if(err < 0) goto error;
626 ubd_dev->cow.fd = err;
630 os_close_file(ubd_dev->fd);
634 static int ubd_disk_register(int major, u64 size, int unit,
635 struct gendisk **disk_out)
638 struct gendisk *disk;
640 disk = alloc_disk(1 << UBD_SHIFT);
645 disk->first_minor = unit << UBD_SHIFT;
646 disk->fops = &ubd_blops;
647 set_capacity(disk, size / 512);
648 if(major == MAJOR_NR)
649 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
651 sprintf(disk->disk_name, "ubd_fake%d", unit);
653 /* sysfs register (not for ide fake devices) */
654 if (major == MAJOR_NR) {
655 ubd_devs[unit].pdev.id = unit;
656 ubd_devs[unit].pdev.name = DRIVER_NAME;
657 platform_device_register(&ubd_devs[unit].pdev);
658 disk->driverfs_dev = &ubd_devs[unit].pdev.dev;
661 disk->private_data = &ubd_devs[unit];
662 disk->queue = ubd_queue;
669 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
671 static int ubd_add(int n)
673 struct ubd *ubd_dev = &ubd_devs[n];
677 if(ubd_dev->file == NULL)
680 err = ubd_file_size(ubd_dev, &ubd_dev->size);
684 ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
686 err = ubd_disk_register(MAJOR_NR, ubd_dev->size, n, &ubd_gendisk[n]);
690 if(fake_major != MAJOR_NR)
691 ubd_disk_register(fake_major, ubd_dev->size, n,
694 /* perhaps this should also be under the "if (fake_major)" above */
695 /* using the fake_disk->disk_name and also the fakehd_set name */
697 make_ide_entries(ubd_gendisk[n]->disk_name);
704 static int ubd_config(char *str)
708 str = kstrdup(str, GFP_KERNEL);
710 printk(KERN_ERR "ubd_config failed to strdup string\n");
714 ret = ubd_setup_common(str, &n);
724 mutex_lock(&ubd_lock);
727 ubd_devs[n].file = NULL;
728 mutex_unlock(&ubd_lock);
738 static int ubd_get_config(char *name, char *str, int size, char **error_out)
743 n = parse_unit(&name);
744 if((n >= MAX_DEV) || (n < 0)){
745 *error_out = "ubd_get_config : device number out of range";
749 ubd_dev = &ubd_devs[n];
750 mutex_lock(&ubd_lock);
752 if(ubd_dev->file == NULL){
753 CONFIG_CHUNK(str, size, len, "", 1);
757 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
759 if(ubd_dev->cow.file != NULL){
760 CONFIG_CHUNK(str, size, len, ",", 0);
761 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
763 else CONFIG_CHUNK(str, size, len, "", 1);
766 mutex_unlock(&ubd_lock);
770 static int ubd_id(char **str, int *start_out, int *end_out)
776 *end_out = MAX_DEV - 1;
780 static int ubd_remove(int n)
785 mutex_lock(&ubd_lock);
787 if(ubd_gendisk[n] == NULL)
790 ubd_dev = &ubd_devs[n];
792 if(ubd_dev->file == NULL)
795 /* you cannot remove a open disk */
797 if(ubd_dev->count > 0)
800 del_gendisk(ubd_gendisk[n]);
801 put_disk(ubd_gendisk[n]);
802 ubd_gendisk[n] = NULL;
804 if(fake_gendisk[n] != NULL){
805 del_gendisk(fake_gendisk[n]);
806 put_disk(fake_gendisk[n]);
807 fake_gendisk[n] = NULL;
810 platform_device_unregister(&ubd_dev->pdev);
811 *ubd_dev = ((struct ubd) DEFAULT_UBD);
814 mutex_unlock(&ubd_lock);
818 /* All these are called by mconsole in process context and without ubd-specific locks. */
819 static struct mc_device ubd_mc = {
821 .config = ubd_config,
822 .get_config = ubd_get_config,
824 .remove = ubd_remove,
827 static int __init ubd_mc_init(void)
829 mconsole_register_dev(&ubd_mc);
833 __initcall(ubd_mc_init);
835 static int __init ubd0_init(void)
837 struct ubd *ubd_dev = &ubd_devs[0];
839 if(ubd_dev->file == NULL)
840 ubd_dev->file = "root_fs";
844 __initcall(ubd0_init);
846 static struct platform_driver ubd_driver = {
852 static int __init ubd_init(void)
856 if (register_blkdev(MAJOR_NR, "ubd"))
859 ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
861 unregister_blkdev(MAJOR_NR, "ubd");
865 if (fake_major != MAJOR_NR) {
866 char name[sizeof("ubd_nnn\0")];
868 snprintf(name, sizeof(name), "ubd_%d", fake_major);
869 if (register_blkdev(fake_major, "ubd"))
872 platform_driver_register(&ubd_driver);
873 for (i = 0; i < MAX_DEV; i++)
878 late_initcall(ubd_init);
880 static int __init ubd_driver_init(void){
884 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
885 if(global_openflags.s){
886 printk(KERN_INFO "ubd: Synchronous mode\n");
887 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
888 * enough. So use anyway the io thread. */
890 stack = alloc_stack(0, 0);
891 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
895 "ubd : Failed to start I/O thread (errno = %d) - "
896 "falling back to synchronous I/O\n", -io_pid);
900 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
901 IRQF_DISABLED, "ubd", ubd_devs);
903 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
907 device_initcall(ubd_driver_init);
909 static int ubd_open(struct inode *inode, struct file *filp)
911 struct gendisk *disk = inode->i_bdev->bd_disk;
912 struct ubd *ubd_dev = disk->private_data;
915 if(ubd_dev->count == 0){
916 err = ubd_open_dev(ubd_dev);
918 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
919 disk->disk_name, ubd_dev->file, -err);
924 set_disk_ro(disk, !ubd_dev->openflags.w);
926 /* This should no more be needed. And it didn't work anyway to exclude
927 * read-write remounting of filesystems.*/
928 /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
929 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
936 static int ubd_release(struct inode * inode, struct file * file)
938 struct gendisk *disk = inode->i_bdev->bd_disk;
939 struct ubd *ubd_dev = disk->private_data;
941 if(--ubd_dev->count == 0)
942 ubd_close_dev(ubd_dev);
946 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
947 __u64 *cow_offset, unsigned long *bitmap,
948 __u64 bitmap_offset, unsigned long *bitmap_words,
951 __u64 sector = io_offset >> 9;
952 int i, update_bitmap = 0;
954 for(i = 0; i < length >> 9; i++){
956 ubd_set_bit(i, (unsigned char *) cow_mask);
957 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
961 ubd_set_bit(sector + i, (unsigned char *) bitmap);
967 *cow_offset = sector / (sizeof(unsigned long) * 8);
969 /* This takes care of the case where we're exactly at the end of the
970 * device, and *cow_offset + 1 is off the end. So, just back it up
971 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
972 * for the original diagnosis.
974 if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
975 sizeof(unsigned long) - 1))
978 bitmap_words[0] = bitmap[*cow_offset];
979 bitmap_words[1] = bitmap[*cow_offset + 1];
981 *cow_offset *= sizeof(unsigned long);
982 *cow_offset += bitmap_offset;
985 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
986 __u64 bitmap_offset, __u64 bitmap_len)
988 __u64 sector = req->offset >> 9;
991 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
992 panic("Operation too long");
994 if(req->op == UBD_READ) {
995 for(i = 0; i < req->length >> 9; i++){
996 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
997 ubd_set_bit(i, (unsigned char *)
1001 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
1002 &req->cow_offset, bitmap, bitmap_offset,
1003 req->bitmap_words, bitmap_len);
1006 /* Called with ubd_io_lock held */
1007 static int prepare_request(struct request *req, struct io_thread_req *io_req)
1009 struct gendisk *disk = req->rq_disk;
1010 struct ubd *ubd_dev = disk->private_data;
1014 /* This should be impossible now */
1015 if((rq_data_dir(req) == WRITE) && !ubd_dev->openflags.w){
1016 printk("Write attempted on readonly ubd device %s\n",
1018 end_request(req, 0);
1022 offset = ((__u64) req->sector) << 9;
1023 len = req->current_nr_sectors << 9;
1025 io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd : ubd_dev->fd;
1026 io_req->fds[1] = ubd_dev->fd;
1027 io_req->cow_offset = -1;
1028 io_req->offset = offset;
1029 io_req->length = len;
1031 io_req->sector_mask = 0;
1033 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1034 io_req->offsets[0] = 0;
1035 io_req->offsets[1] = ubd_dev->cow.data_offset;
1036 io_req->buffer = req->buffer;
1037 io_req->sectorsize = 1 << 9;
1039 if(ubd_dev->cow.file != NULL)
1040 cowify_req(io_req, ubd_dev->cow.bitmap, ubd_dev->cow.bitmap_offset,
1041 ubd_dev->cow.bitmap_len);
1046 /* Called with ubd_io_lock held */
1047 static void do_ubd_request(request_queue_t *q)
1049 struct io_thread_req io_req;
1050 struct request *req;
1053 if(thread_fd == -1){
1054 while((req = elv_next_request(q)) != NULL){
1055 err = prepare_request(req, &io_req);
1058 __ubd_finish(req, io_req.error);
1063 if(do_ubd || (req = elv_next_request(q)) == NULL)
1065 err = prepare_request(req, &io_req);
1068 n = os_write_file(thread_fd, (char *) &io_req,
1070 if(n != sizeof(io_req))
1071 printk("write to io thread failed, "
1072 "errno = %d\n", -n);
1077 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1079 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1083 geo->cylinders = ubd_dev->size / (128 * 32 * 512);
1087 static int ubd_ioctl(struct inode * inode, struct file * file,
1088 unsigned int cmd, unsigned long arg)
1090 struct ubd *ubd_dev = inode->i_bdev->bd_disk->private_data;
1091 struct hd_driveid ubd_id = {
1098 struct cdrom_volctrl volume;
1099 case HDIO_GET_IDENTITY:
1100 ubd_id.cyls = ubd_dev->size / (128 * 32 * 512);
1101 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1107 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1109 volume.channel0 = 255;
1110 volume.channel1 = 255;
1111 volume.channel2 = 255;
1112 volume.channel3 = 255;
1113 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1120 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
1122 struct uml_stat buf1, buf2;
1125 if(from_cmdline == NULL)
1127 if(!strcmp(from_cmdline, from_cow))
1130 err = os_stat_file(from_cmdline, &buf1);
1132 printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
1135 err = os_stat_file(from_cow, &buf2);
1137 printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
1140 if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
1143 printk("Backing file mismatch - \"%s\" requested,\n"
1144 "\"%s\" specified in COW header of \"%s\"\n",
1145 from_cmdline, from_cow, cow);
1149 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
1151 unsigned long modtime;
1152 unsigned long long actual;
1155 err = os_file_modtime(file, &modtime);
1157 printk("Failed to get modification time of backing file "
1158 "\"%s\", err = %d\n", file, -err);
1162 err = os_file_size(file, &actual);
1164 printk("Failed to get size of backing file \"%s\", "
1165 "err = %d\n", file, -err);
1170 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1172 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1173 "file\n", (unsigned long long) size, actual);
1176 if(modtime != mtime){
1177 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1178 "file\n", mtime, modtime);
1184 int read_cow_bitmap(int fd, void *buf, int offset, int len)
1188 err = os_seek_file(fd, offset);
1192 err = os_read_file(fd, buf, len);
1199 int open_ubd_file(char *file, struct openflags *openflags, int shared,
1200 char **backing_file_out, int *bitmap_offset_out,
1201 unsigned long *bitmap_len_out, int *data_offset_out,
1202 int *create_cow_out)
1205 unsigned long long size;
1206 __u32 version, align;
1208 int fd, err, sectorsize, asked_switch, mode = 0644;
1210 fd = os_open_file(file, *openflags, mode);
1212 if ((fd == -ENOENT) && (create_cow_out != NULL))
1213 *create_cow_out = 1;
1214 if (!openflags->w ||
1215 ((fd != -EROFS) && (fd != -EACCES)))
1218 fd = os_open_file(file, *openflags, mode);
1224 printk("Not locking \"%s\" on the host\n", file);
1226 err = os_lock_file(fd, openflags->w);
1228 printk("Failed to lock '%s', err = %d\n", file, -err);
1233 /* Successful return case! */
1234 if(backing_file_out == NULL)
1237 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
1238 &size, §orsize, &align, bitmap_offset_out);
1239 if(err && (*backing_file_out != NULL)){
1240 printk("Failed to read COW header from COW file \"%s\", "
1241 "errno = %d\n", file, -err);
1247 asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
1249 /* Allow switching only if no mismatch. */
1250 if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
1251 printk("Switching backing file to '%s'\n", *backing_file_out);
1252 err = write_cow_header(file, fd, *backing_file_out,
1253 sectorsize, align, &size);
1255 printk("Switch failed, errno = %d\n", -err);
1259 *backing_file_out = backing_file;
1260 err = backing_file_mismatch(*backing_file_out, size, mtime);
1265 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
1266 bitmap_len_out, data_offset_out);
1274 int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
1275 int sectorsize, int alignment, int *bitmap_offset_out,
1276 unsigned long *bitmap_len_out, int *data_offset_out)
1281 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
1284 printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
1289 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
1290 bitmap_offset_out, bitmap_len_out,
1299 static int update_bitmap(struct io_thread_req *req)
1303 if(req->cow_offset == -1)
1306 n = os_seek_file(req->fds[1], req->cow_offset);
1308 printk("do_io - bitmap lseek failed : err = %d\n", -n);
1312 n = os_write_file(req->fds[1], &req->bitmap_words,
1313 sizeof(req->bitmap_words));
1314 if(n != sizeof(req->bitmap_words)){
1315 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
1323 void do_io(struct io_thread_req *req)
1327 int n, nsectors, start, end, bit;
1331 nsectors = req->length / req->sectorsize;
1334 bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
1336 while((end < nsectors) &&
1337 (ubd_test_bit(end, (unsigned char *)
1338 &req->sector_mask) == bit))
1341 off = req->offset + req->offsets[bit] +
1342 start * req->sectorsize;
1343 len = (end - start) * req->sectorsize;
1344 buf = &req->buffer[start * req->sectorsize];
1346 err = os_seek_file(req->fds[bit], off);
1348 printk("do_io - lseek failed : err = %d\n", -err);
1352 if(req->op == UBD_READ){
1357 n = os_read_file(req->fds[bit], buf, len);
1359 printk("do_io - read failed, err = %d "
1360 "fd = %d\n", -n, req->fds[bit]);
1364 } while((n < len) && (n != 0));
1365 if (n < len) memset(&buf[n], 0, len - n);
1367 n = os_write_file(req->fds[bit], buf, len);
1369 printk("do_io - write failed err = %d "
1370 "fd = %d\n", -n, req->fds[bit]);
1377 } while(start < nsectors);
1379 req->error = update_bitmap(req);
1382 /* Changed in start_io_thread, which is serialized by being called only
1383 * from ubd_init, which is an initcall.
1387 /* Only changed by the io thread. XXX: currently unused. */
1388 static int io_count = 0;
1390 int io_thread(void *arg)
1392 struct io_thread_req req;
1395 ignore_sigwinch_sig();
1397 n = os_read_file(kernel_fd, &req, sizeof(req));
1398 if(n != sizeof(req)){
1400 printk("io_thread - read failed, fd = %d, "
1401 "err = %d\n", kernel_fd, -n);
1403 printk("io_thread - short read, fd = %d, "
1404 "length = %d\n", kernel_fd, n);
1410 n = os_write_file(kernel_fd, &req, sizeof(req));
1411 if(n != sizeof(req))
1412 printk("io_thread - write failed, fd = %d, err = %d\n",