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);
239 ent->read_proc = proc_ide_read_media;
240 ent->write_proc = NULL;
241 sprintf(name,"ide0/%s", dev_name);
242 proc_symlink(dev_name, proc_ide_root, name);
245 static int fake_ide_setup(char *str)
251 __setup("fake_ide", fake_ide_setup);
253 __uml_help(fake_ide_setup,
255 " Create ide0 entries that map onto ubd devices.\n\n"
258 static int parse_unit(char **ptr)
260 char *str = *ptr, *end;
264 n = simple_strtoul(str, &end, 0);
269 else if (('a' <= *str) && (*str <= 'z')) {
277 /* If *index_out == -1 at exit, the passed option was a general one;
278 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
279 * should not be freed on exit.
281 static int ubd_setup_common(char *str, int *index_out, char **error_out)
284 struct openflags flags = global_openflags;
288 if(index_out) *index_out = -1;
295 if(!strcmp(str, "sync")){
296 global_openflags = of_sync(global_openflags);
301 major = simple_strtoul(str, &end, 0);
302 if((*end != '\0') || (end == str)){
303 *error_out = "Didn't parse major number";
307 mutex_lock(&ubd_lock);
308 if(fake_major != MAJOR_NR){
309 *error_out = "Can't assign a fake major twice";
315 printk(KERN_INFO "Setting extra ubd major number to %d\n",
319 mutex_unlock(&ubd_lock);
323 n = parse_unit(&str);
325 *error_out = "Couldn't parse device number";
329 *error_out = "Device number out of range";
334 mutex_lock(&ubd_lock);
336 ubd_dev = &ubd_devs[n];
337 if(ubd_dev->file != NULL){
338 *error_out = "Device is already configured";
346 for (i = 0; i < sizeof("rscd="); i++) {
364 *error_out = "Expected '=' or flag letter "
372 *error_out = "Too many flags specified";
374 *error_out = "Missing '='";
378 backing_file = strchr(str, ',');
380 if (backing_file == NULL)
381 backing_file = strchr(str, ':');
383 if(backing_file != NULL){
385 *error_out = "Can't specify both 'd' and a cow file";
389 *backing_file = '\0';
395 ubd_dev->cow.file = backing_file;
396 ubd_dev->boot_openflags = flags;
398 mutex_unlock(&ubd_lock);
402 static int ubd_setup(char *str)
407 err = ubd_setup_common(str, NULL, &error);
409 printk(KERN_ERR "Failed to initialize device with \"%s\" : "
414 __setup("ubd", ubd_setup);
415 __uml_help(ubd_setup,
416 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
417 " This is used to associate a device with a file in the underlying\n"
418 " filesystem. When specifying two filenames, the first one is the\n"
419 " COW name and the second is the backing file name. As separator you can\n"
420 " use either a ':' or a ',': the first one allows writing things like;\n"
421 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
422 " while with a ',' the shell would not expand the 2nd '~'.\n"
423 " When using only one filename, UML will detect whether to treat it like\n"
424 " a COW file or a backing file. To override this detection, add the 'd'\n"
426 " ubd0d=BackingFile\n"
427 " Usually, there is a filesystem in the file, but \n"
428 " that's not required. Swap devices containing swap files can be\n"
429 " specified like this. Also, a file which doesn't contain a\n"
430 " filesystem can have its contents read in the virtual \n"
431 " machine by running 'dd' on the device. <n> must be in the range\n"
432 " 0 to 7. Appending an 'r' to the number will cause that device\n"
433 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
434 " an 's' will cause data to be written to disk on the host immediately.\n\n"
437 static int udb_setup(char *str)
439 printk("udb%s specified on command line is almost certainly a ubd -> "
444 __setup("udb", udb_setup);
445 __uml_help(udb_setup,
447 " This option is here solely to catch ubd -> udb typos, which can be\n"
448 " to impossible to catch visually unless you specifically look for\n"
449 " them. The only result of any option starting with 'udb' is an error\n"
450 " in the boot output.\n\n"
453 static int fakehd_set = 0;
454 static int fakehd(char *str)
456 printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
461 __setup("fakehd", fakehd);
464 " Change the ubd device name to \"hd\".\n\n"
467 static void do_ubd_request(request_queue_t * q);
469 /* Only changed by ubd_init, which is an initcall. */
472 /* call ubd_finish if you need to serialize */
473 static void __ubd_finish(struct request *req, int error)
481 nsect = req->current_nr_sectors;
482 req->sector += nsect;
483 req->buffer += nsect << 9;
485 req->nr_sectors -= nsect;
486 req->current_nr_sectors = 0;
490 /* Callable only from interrupt context - otherwise you need to do
491 * spin_lock_irq()/spin_lock_irqsave() */
492 static inline void ubd_finish(struct request *req, int error)
494 struct ubd *dev = req->rq_disk->private_data;
496 spin_lock(&dev->lock);
497 __ubd_finish(req, error);
498 spin_unlock(&dev->lock);
501 /* XXX - move this inside ubd_intr. */
502 /* Called without dev->lock held, and only in interrupt context. */
503 static void ubd_handler(void)
505 struct io_thread_req req;
511 n = os_read_file(thread_fd, &req, sizeof(req));
512 if(n != sizeof(req)){
513 printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
514 "err = %d\n", os_getpid(), -n);
519 dev = rq->rq_disk->private_data;
521 ubd_finish(rq, req.error);
522 reactivate_fd(thread_fd, UBD_IRQ);
523 spin_lock(&dev->lock);
524 do_ubd_request(dev->queue);
525 spin_unlock(&dev->lock);
528 static irqreturn_t ubd_intr(int irq, void *dev)
534 /* Only changed by ubd_init, which is an initcall. */
535 static int io_pid = -1;
537 void kill_io_thread(void)
540 os_kill_process(io_pid, 1);
543 __uml_exitcall(kill_io_thread);
545 static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
549 file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file;
550 return(os_file_size(file, size_out));
553 static void ubd_close_dev(struct ubd *ubd_dev)
555 os_close_file(ubd_dev->fd);
556 if(ubd_dev->cow.file == NULL)
559 os_close_file(ubd_dev->cow.fd);
560 vfree(ubd_dev->cow.bitmap);
561 ubd_dev->cow.bitmap = NULL;
564 static int ubd_open_dev(struct ubd *ubd_dev)
566 struct openflags flags;
568 int err, create_cow, *create_ptr;
571 ubd_dev->openflags = ubd_dev->boot_openflags;
573 create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
574 back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
576 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((fd == -ENOENT) && create_cow){
582 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);
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,
600 if(ubd_dev->cow.file != NULL){
602 ubd_dev->cow.bitmap = (void *) vmalloc(ubd_dev->cow.bitmap_len);
603 if(ubd_dev->cow.bitmap == NULL){
604 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
607 flush_tlb_kernel_vm();
609 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
610 ubd_dev->cow.bitmap_offset,
611 ubd_dev->cow.bitmap_len);
615 flags = ubd_dev->openflags;
617 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
618 NULL, NULL, NULL, NULL);
619 if(err < 0) goto error;
620 ubd_dev->cow.fd = err;
624 os_close_file(ubd_dev->fd);
628 static int ubd_disk_register(int major, u64 size, int unit,
629 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_devs[unit].queue;
662 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
664 static int ubd_add(int n, char **error_out)
666 struct ubd *ubd_dev = &ubd_devs[n];
669 if(ubd_dev->file == NULL)
672 err = ubd_file_size(ubd_dev, &ubd_dev->size);
674 *error_out = "Couldn't determine size of device's file";
678 ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
681 ubd_dev->queue = blk_init_queue(do_ubd_request, &ubd_dev->lock);
682 if (ubd_dev->queue == NULL) {
683 *error_out = "Failed to initialize device queue";
686 ubd_dev->queue->queuedata = ubd_dev;
688 err = ubd_disk_register(MAJOR_NR, ubd_dev->size, n, &ubd_gendisk[n]);
690 *error_out = "Failed to register device";
694 if(fake_major != MAJOR_NR)
695 ubd_disk_register(fake_major, ubd_dev->size, n,
698 /* perhaps this should also be under the "if (fake_major)" above */
699 /* using the fake_disk->disk_name and also the fakehd_set name */
701 make_ide_entries(ubd_gendisk[n]->disk_name);
708 blk_cleanup_queue(ubd_dev->queue);
712 static int ubd_config(char *str, char **error_out)
716 /* This string is possibly broken up and stored, so it's only
717 * freed if ubd_setup_common fails, or if only general options
720 str = kstrdup(str, GFP_KERNEL);
722 *error_out = "Failed to allocate memory";
726 ret = ubd_setup_common(str, &n, error_out);
735 mutex_lock(&ubd_lock);
736 ret = ubd_add(n, error_out);
738 ubd_devs[n].file = NULL;
739 mutex_unlock(&ubd_lock);
749 static int ubd_get_config(char *name, char *str, int size, char **error_out)
754 n = parse_unit(&name);
755 if((n >= MAX_DEV) || (n < 0)){
756 *error_out = "ubd_get_config : device number out of range";
760 ubd_dev = &ubd_devs[n];
761 mutex_lock(&ubd_lock);
763 if(ubd_dev->file == NULL){
764 CONFIG_CHUNK(str, size, len, "", 1);
768 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
770 if(ubd_dev->cow.file != NULL){
771 CONFIG_CHUNK(str, size, len, ",", 0);
772 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
774 else CONFIG_CHUNK(str, size, len, "", 1);
777 mutex_unlock(&ubd_lock);
781 static int ubd_id(char **str, int *start_out, int *end_out)
787 *end_out = MAX_DEV - 1;
791 static int ubd_remove(int n, char **error_out)
796 mutex_lock(&ubd_lock);
798 if(ubd_gendisk[n] == NULL)
801 ubd_dev = &ubd_devs[n];
803 if(ubd_dev->file == NULL)
806 /* you cannot remove a open disk */
808 if(ubd_dev->count > 0)
811 del_gendisk(ubd_gendisk[n]);
812 put_disk(ubd_gendisk[n]);
813 ubd_gendisk[n] = NULL;
815 if(fake_gendisk[n] != NULL){
816 del_gendisk(fake_gendisk[n]);
817 put_disk(fake_gendisk[n]);
818 fake_gendisk[n] = NULL;
821 blk_cleanup_queue(ubd_dev->queue);
822 platform_device_unregister(&ubd_dev->pdev);
823 *ubd_dev = ((struct ubd) DEFAULT_UBD);
826 mutex_unlock(&ubd_lock);
830 /* All these are called by mconsole in process context and without
831 * ubd-specific locks. The structure itself is const except for .list.
833 static struct mc_device ubd_mc = {
834 .list = LIST_HEAD_INIT(ubd_mc.list),
836 .config = ubd_config,
837 .get_config = ubd_get_config,
839 .remove = ubd_remove,
842 static int __init ubd_mc_init(void)
844 mconsole_register_dev(&ubd_mc);
848 __initcall(ubd_mc_init);
850 static int __init ubd0_init(void)
852 struct ubd *ubd_dev = &ubd_devs[0];
854 mutex_lock(&ubd_lock);
855 if(ubd_dev->file == NULL)
856 ubd_dev->file = "root_fs";
857 mutex_unlock(&ubd_lock);
862 __initcall(ubd0_init);
864 /* Used in ubd_init, which is an initcall */
865 static struct platform_driver ubd_driver = {
871 static int __init ubd_init(void)
876 if (register_blkdev(MAJOR_NR, "ubd"))
879 if (fake_major != MAJOR_NR) {
880 char name[sizeof("ubd_nnn\0")];
882 snprintf(name, sizeof(name), "ubd_%d", fake_major);
883 if (register_blkdev(fake_major, "ubd"))
886 platform_driver_register(&ubd_driver);
887 mutex_lock(&ubd_lock);
888 for (i = 0; i < MAX_DEV; i++){
889 err = ubd_add(i, &error);
891 printk(KERN_ERR "Failed to initialize ubd device %d :"
894 mutex_unlock(&ubd_lock);
898 late_initcall(ubd_init);
900 static int __init ubd_driver_init(void){
904 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
905 if(global_openflags.s){
906 printk(KERN_INFO "ubd: Synchronous mode\n");
907 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
908 * enough. So use anyway the io thread. */
910 stack = alloc_stack(0, 0);
911 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
915 "ubd : Failed to start I/O thread (errno = %d) - "
916 "falling back to synchronous I/O\n", -io_pid);
920 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
921 IRQF_DISABLED, "ubd", ubd_devs);
923 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
927 device_initcall(ubd_driver_init);
929 static int ubd_open(struct inode *inode, struct file *filp)
931 struct gendisk *disk = inode->i_bdev->bd_disk;
932 struct ubd *ubd_dev = disk->private_data;
935 if(ubd_dev->count == 0){
936 err = ubd_open_dev(ubd_dev);
938 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
939 disk->disk_name, ubd_dev->file, -err);
944 set_disk_ro(disk, !ubd_dev->openflags.w);
946 /* This should no more be needed. And it didn't work anyway to exclude
947 * read-write remounting of filesystems.*/
948 /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
949 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
956 static int ubd_release(struct inode * inode, struct file * file)
958 struct gendisk *disk = inode->i_bdev->bd_disk;
959 struct ubd *ubd_dev = disk->private_data;
961 if(--ubd_dev->count == 0)
962 ubd_close_dev(ubd_dev);
966 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
967 __u64 *cow_offset, unsigned long *bitmap,
968 __u64 bitmap_offset, unsigned long *bitmap_words,
971 __u64 sector = io_offset >> 9;
972 int i, update_bitmap = 0;
974 for(i = 0; i < length >> 9; i++){
976 ubd_set_bit(i, (unsigned char *) cow_mask);
977 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
981 ubd_set_bit(sector + i, (unsigned char *) bitmap);
987 *cow_offset = sector / (sizeof(unsigned long) * 8);
989 /* This takes care of the case where we're exactly at the end of the
990 * device, and *cow_offset + 1 is off the end. So, just back it up
991 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
992 * for the original diagnosis.
994 if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
995 sizeof(unsigned long) - 1))
998 bitmap_words[0] = bitmap[*cow_offset];
999 bitmap_words[1] = bitmap[*cow_offset + 1];
1001 *cow_offset *= sizeof(unsigned long);
1002 *cow_offset += bitmap_offset;
1005 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
1006 __u64 bitmap_offset, __u64 bitmap_len)
1008 __u64 sector = req->offset >> 9;
1011 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
1012 panic("Operation too long");
1014 if(req->op == UBD_READ) {
1015 for(i = 0; i < req->length >> 9; i++){
1016 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1017 ubd_set_bit(i, (unsigned char *)
1021 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
1022 &req->cow_offset, bitmap, bitmap_offset,
1023 req->bitmap_words, bitmap_len);
1026 /* Called with dev->lock held */
1027 static int prepare_request(struct request *req, struct io_thread_req *io_req)
1029 struct gendisk *disk = req->rq_disk;
1030 struct ubd *ubd_dev = disk->private_data;
1034 /* This should be impossible now */
1035 if((rq_data_dir(req) == WRITE) && !ubd_dev->openflags.w){
1036 printk("Write attempted on readonly ubd device %s\n",
1038 end_request(req, 0);
1042 offset = ((__u64) req->sector) << 9;
1043 len = req->current_nr_sectors << 9;
1046 io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd : ubd_dev->fd;
1047 io_req->fds[1] = ubd_dev->fd;
1048 io_req->cow_offset = -1;
1049 io_req->offset = offset;
1050 io_req->length = len;
1052 io_req->sector_mask = 0;
1054 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1055 io_req->offsets[0] = 0;
1056 io_req->offsets[1] = ubd_dev->cow.data_offset;
1057 io_req->buffer = req->buffer;
1058 io_req->sectorsize = 1 << 9;
1060 if(ubd_dev->cow.file != NULL)
1061 cowify_req(io_req, ubd_dev->cow.bitmap, ubd_dev->cow.bitmap_offset,
1062 ubd_dev->cow.bitmap_len);
1067 /* Called with dev->lock held */
1068 static void do_ubd_request(request_queue_t *q)
1070 struct io_thread_req io_req;
1071 struct request *req;
1074 if(thread_fd == -1){
1075 while((req = elv_next_request(q)) != NULL){
1076 err = prepare_request(req, &io_req);
1079 __ubd_finish(req, io_req.error);
1084 if(do_ubd || (req = elv_next_request(q)) == NULL)
1086 err = prepare_request(req, &io_req);
1089 n = os_write_file(thread_fd, (char *) &io_req,
1091 if(n != sizeof(io_req))
1092 printk("write to io thread failed, "
1093 "errno = %d\n", -n);
1098 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1100 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1104 geo->cylinders = ubd_dev->size / (128 * 32 * 512);
1108 static int ubd_ioctl(struct inode * inode, struct file * file,
1109 unsigned int cmd, unsigned long arg)
1111 struct ubd *ubd_dev = inode->i_bdev->bd_disk->private_data;
1112 struct hd_driveid ubd_id = {
1119 struct cdrom_volctrl volume;
1120 case HDIO_GET_IDENTITY:
1121 ubd_id.cyls = ubd_dev->size / (128 * 32 * 512);
1122 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1128 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1130 volume.channel0 = 255;
1131 volume.channel1 = 255;
1132 volume.channel2 = 255;
1133 volume.channel3 = 255;
1134 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1141 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
1143 struct uml_stat buf1, buf2;
1146 if(from_cmdline == NULL)
1148 if(!strcmp(from_cmdline, from_cow))
1151 err = os_stat_file(from_cmdline, &buf1);
1153 printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
1156 err = os_stat_file(from_cow, &buf2);
1158 printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
1161 if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
1164 printk("Backing file mismatch - \"%s\" requested,\n"
1165 "\"%s\" specified in COW header of \"%s\"\n",
1166 from_cmdline, from_cow, cow);
1170 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
1172 unsigned long modtime;
1173 unsigned long long actual;
1176 err = os_file_modtime(file, &modtime);
1178 printk("Failed to get modification time of backing file "
1179 "\"%s\", err = %d\n", file, -err);
1183 err = os_file_size(file, &actual);
1185 printk("Failed to get size of backing file \"%s\", "
1186 "err = %d\n", file, -err);
1191 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1193 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1194 "file\n", (unsigned long long) size, actual);
1197 if(modtime != mtime){
1198 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1199 "file\n", mtime, modtime);
1205 int read_cow_bitmap(int fd, void *buf, int offset, int len)
1209 err = os_seek_file(fd, offset);
1213 err = os_read_file(fd, buf, len);
1220 int open_ubd_file(char *file, struct openflags *openflags, int shared,
1221 char **backing_file_out, int *bitmap_offset_out,
1222 unsigned long *bitmap_len_out, int *data_offset_out,
1223 int *create_cow_out)
1226 unsigned long long size;
1227 __u32 version, align;
1229 int fd, err, sectorsize, asked_switch, mode = 0644;
1231 fd = os_open_file(file, *openflags, mode);
1233 if ((fd == -ENOENT) && (create_cow_out != NULL))
1234 *create_cow_out = 1;
1235 if (!openflags->w ||
1236 ((fd != -EROFS) && (fd != -EACCES)))
1239 fd = os_open_file(file, *openflags, mode);
1245 printk("Not locking \"%s\" on the host\n", file);
1247 err = os_lock_file(fd, openflags->w);
1249 printk("Failed to lock '%s', err = %d\n", file, -err);
1254 /* Successful return case! */
1255 if(backing_file_out == NULL)
1258 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
1259 &size, §orsize, &align, bitmap_offset_out);
1260 if(err && (*backing_file_out != NULL)){
1261 printk("Failed to read COW header from COW file \"%s\", "
1262 "errno = %d\n", file, -err);
1268 asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
1270 /* Allow switching only if no mismatch. */
1271 if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
1272 printk("Switching backing file to '%s'\n", *backing_file_out);
1273 err = write_cow_header(file, fd, *backing_file_out,
1274 sectorsize, align, &size);
1276 printk("Switch failed, errno = %d\n", -err);
1280 *backing_file_out = backing_file;
1281 err = backing_file_mismatch(*backing_file_out, size, mtime);
1286 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
1287 bitmap_len_out, data_offset_out);
1295 int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
1296 int sectorsize, int alignment, int *bitmap_offset_out,
1297 unsigned long *bitmap_len_out, int *data_offset_out)
1302 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
1305 printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
1310 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
1311 bitmap_offset_out, bitmap_len_out,
1320 static int update_bitmap(struct io_thread_req *req)
1324 if(req->cow_offset == -1)
1327 n = os_seek_file(req->fds[1], req->cow_offset);
1329 printk("do_io - bitmap lseek failed : err = %d\n", -n);
1333 n = os_write_file(req->fds[1], &req->bitmap_words,
1334 sizeof(req->bitmap_words));
1335 if(n != sizeof(req->bitmap_words)){
1336 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
1344 void do_io(struct io_thread_req *req)
1348 int n, nsectors, start, end, bit;
1352 nsectors = req->length / req->sectorsize;
1355 bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
1357 while((end < nsectors) &&
1358 (ubd_test_bit(end, (unsigned char *)
1359 &req->sector_mask) == bit))
1362 off = req->offset + req->offsets[bit] +
1363 start * req->sectorsize;
1364 len = (end - start) * req->sectorsize;
1365 buf = &req->buffer[start * req->sectorsize];
1367 err = os_seek_file(req->fds[bit], off);
1369 printk("do_io - lseek failed : err = %d\n", -err);
1373 if(req->op == UBD_READ){
1378 n = os_read_file(req->fds[bit], buf, len);
1380 printk("do_io - read failed, err = %d "
1381 "fd = %d\n", -n, req->fds[bit]);
1385 } while((n < len) && (n != 0));
1386 if (n < len) memset(&buf[n], 0, len - n);
1388 n = os_write_file(req->fds[bit], buf, len);
1390 printk("do_io - write failed err = %d "
1391 "fd = %d\n", -n, req->fds[bit]);
1398 } while(start < nsectors);
1400 req->error = update_bitmap(req);
1403 /* Changed in start_io_thread, which is serialized by being called only
1404 * from ubd_init, which is an initcall.
1408 /* Only changed by the io thread. XXX: currently unused. */
1409 static int io_count = 0;
1411 int io_thread(void *arg)
1413 struct io_thread_req req;
1416 ignore_sigwinch_sig();
1418 n = os_read_file(kernel_fd, &req, sizeof(req));
1419 if(n != sizeof(req)){
1421 printk("io_thread - read failed, fd = %d, "
1422 "err = %d\n", kernel_fd, -n);
1424 printk("io_thread - short read, fd = %d, "
1425 "length = %d\n", kernel_fd, n);
1431 n = os_write_file(kernel_fd, &req, sizeof(req));
1432 if(n != sizeof(req))
1433 printk("io_thread - write failed, fd = %d, err = %d\n",