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 /* This is the backing file, actually */
156 unsigned long *bitmap;
157 unsigned long bitmap_len;
167 struct openflags boot_openflags;
168 struct openflags openflags;
172 struct platform_device pdev;
175 #define DEFAULT_COW { \
179 .bitmap_offset = 0, \
183 #define DEFAULT_UBD { \
188 .boot_openflags = OPEN_FLAGS, \
189 .openflags = OPEN_FLAGS, \
192 .cow = DEFAULT_COW, \
195 struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
197 static int ubd0_init(void)
199 struct ubd *dev = &ubd_dev[0];
201 if(dev->file == NULL)
202 dev->file = "root_fs";
206 __initcall(ubd0_init);
208 /* Only changed by fake_ide_setup which is a setup */
209 static int fake_ide = 0;
210 static struct proc_dir_entry *proc_ide_root = NULL;
211 static struct proc_dir_entry *proc_ide = NULL;
213 static void make_proc_ide(void)
215 proc_ide_root = proc_mkdir("ide", NULL);
216 proc_ide = proc_mkdir("ide0", proc_ide_root);
219 static int proc_ide_read_media(char *page, char **start, off_t off, int count,
220 int *eof, void *data)
224 strcpy(page, "disk\n");
225 len = strlen("disk\n");
229 if (len <= 0) return 0;
236 static void make_ide_entries(char *dev_name)
238 struct proc_dir_entry *dir, *ent;
241 if(proc_ide_root == NULL) make_proc_ide();
243 dir = proc_mkdir(dev_name, proc_ide);
246 ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
250 ent->read_proc = proc_ide_read_media;
251 ent->write_proc = NULL;
252 sprintf(name,"ide0/%s", dev_name);
253 proc_symlink(dev_name, proc_ide_root, name);
256 static int fake_ide_setup(char *str)
262 __setup("fake_ide", fake_ide_setup);
264 __uml_help(fake_ide_setup,
266 " Create ide0 entries that map onto ubd devices.\n\n"
269 static int parse_unit(char **ptr)
271 char *str = *ptr, *end;
275 n = simple_strtoul(str, &end, 0);
280 else if (('a' <= *str) && (*str <= 'h')) {
288 static int ubd_setup_common(char *str, int *index_out)
291 struct openflags flags = global_openflags;
295 if(index_out) *index_out = -1;
302 if(!strcmp(str, "sync")){
303 global_openflags = of_sync(global_openflags);
306 major = simple_strtoul(str, &end, 0);
307 if((*end != '\0') || (end == str)){
309 "ubd_setup : didn't parse major number\n");
314 spin_lock(&ubd_lock);
315 if(fake_major != MAJOR_NR){
316 printk(KERN_ERR "Can't assign a fake major twice\n");
322 printk(KERN_INFO "Setting extra ubd major number to %d\n",
326 spin_unlock(&ubd_lock);
330 n = parse_unit(&str);
332 printk(KERN_ERR "ubd_setup : couldn't parse unit number "
337 printk(KERN_ERR "ubd_setup : index %d out of range "
338 "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
343 spin_lock(&ubd_lock);
346 if(dev->file != NULL){
347 printk(KERN_ERR "ubd_setup : device already configured\n");
354 for (i = 0; i < sizeof("rscd="); i++) {
372 printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r, s, c, or d)\n");
379 printk(KERN_ERR "ubd_setup : Too many flags specified\n");
381 printk(KERN_ERR "ubd_setup : Expected '='\n");
386 backing_file = strchr(str, ',');
389 backing_file = strchr(str, ':');
394 printk(KERN_ERR "Can't specify both 'd' and a "
397 *backing_file = '\0';
402 dev->cow.file = backing_file;
403 dev->boot_openflags = flags;
405 spin_unlock(&ubd_lock);
409 static int ubd_setup(char *str)
411 ubd_setup_common(str, NULL);
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 thread 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 /* Changed by ubd_handler, which is serialized because interrupts only
478 /* call ubd_finish if you need to serialize */
479 static void __ubd_finish(struct request *req, int error)
487 nsect = req->current_nr_sectors;
488 req->sector += nsect;
489 req->buffer += nsect << 9;
491 req->nr_sectors -= nsect;
492 req->current_nr_sectors = 0;
496 static inline void ubd_finish(struct request *req, int error)
498 spin_lock(&ubd_io_lock);
499 __ubd_finish(req, error);
500 spin_unlock(&ubd_io_lock);
503 /* Called without ubd_io_lock held */
504 static void ubd_handler(void)
506 struct io_thread_req req;
507 struct request *rq = elv_next_request(ubd_queue);
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);
516 spin_lock(&ubd_io_lock);
518 spin_unlock(&ubd_io_lock);
522 ubd_finish(rq, req.error);
523 reactivate_fd(thread_fd, UBD_IRQ);
524 do_ubd_request(ubd_queue);
527 static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
533 /* Only changed by ubd_init, which is an initcall. */
534 static int io_pid = -1;
536 void kill_io_thread(void)
539 os_kill_process(io_pid, 1);
542 __uml_exitcall(kill_io_thread);
544 static int ubd_file_size(struct ubd *dev, __u64 *size_out)
548 file = dev->cow.file ? dev->cow.file : dev->file;
549 return(os_file_size(file, size_out));
552 static void ubd_close(struct ubd *dev)
554 os_close_file(dev->fd);
555 if(dev->cow.file == NULL)
558 os_close_file(dev->cow.fd);
559 vfree(dev->cow.bitmap);
560 dev->cow.bitmap = NULL;
563 static int ubd_open_dev(struct ubd *dev)
565 struct openflags flags;
567 int err, create_cow, *create_ptr;
569 dev->openflags = dev->boot_openflags;
571 create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
572 back_ptr = dev->no_cow ? NULL : &dev->cow.file;
573 dev->fd = open_ubd_file(dev->file, &dev->openflags, dev->shared,
574 back_ptr, &dev->cow.bitmap_offset,
575 &dev->cow.bitmap_len, &dev->cow.data_offset,
578 if((dev->fd == -ENOENT) && create_cow){
579 dev->fd = create_cow_file(dev->file, dev->cow.file,
580 dev->openflags, 1 << 9, PAGE_SIZE,
581 &dev->cow.bitmap_offset,
582 &dev->cow.bitmap_len,
583 &dev->cow.data_offset);
585 printk(KERN_INFO "Creating \"%s\" as COW file for "
586 "\"%s\"\n", dev->file, dev->cow.file);
591 printk("Failed to open '%s', errno = %d\n", dev->file,
596 if(dev->cow.file != NULL){
598 dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
599 if(dev->cow.bitmap == NULL){
600 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
603 flush_tlb_kernel_vm();
605 err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
606 dev->cow.bitmap_offset,
607 dev->cow.bitmap_len);
611 flags = dev->openflags;
613 err = open_ubd_file(dev->cow.file, &flags, dev->shared, NULL,
614 NULL, NULL, NULL, NULL);
615 if(err < 0) goto error;
620 os_close_file(dev->fd);
624 static int ubd_new_disk(int major, u64 size, int unit,
625 struct gendisk **disk_out)
628 struct gendisk *disk;
630 disk = alloc_disk(1 << UBD_SHIFT);
635 disk->first_minor = unit << UBD_SHIFT;
636 disk->fops = &ubd_blops;
637 set_capacity(disk, size / 512);
638 if(major == MAJOR_NR)
639 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
641 sprintf(disk->disk_name, "ubd_fake%d", unit);
643 /* sysfs register (not for ide fake devices) */
644 if (major == MAJOR_NR) {
645 ubd_dev[unit].pdev.id = unit;
646 ubd_dev[unit].pdev.name = DRIVER_NAME;
647 platform_device_register(&ubd_dev[unit].pdev);
648 disk->driverfs_dev = &ubd_dev[unit].pdev.dev;
651 disk->private_data = &ubd_dev[unit];
652 disk->queue = ubd_queue;
659 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
661 static int ubd_add(int n)
663 struct ubd *dev = &ubd_dev[n];
667 if(dev->file == NULL)
670 if (ubd_open_dev(dev))
673 err = ubd_file_size(dev, &dev->size);
677 dev->size = ROUND_BLOCK(dev->size);
679 err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
683 if(fake_major != MAJOR_NR)
684 ubd_new_disk(fake_major, 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);
699 static int ubd_config(char *str)
703 str = kstrdup(str, GFP_KERNEL);
705 printk(KERN_ERR "ubd_config failed to strdup string\n");
708 err = ubd_setup_common(str, &n);
713 if(n == -1) return(0);
715 spin_lock(&ubd_lock);
718 ubd_dev[n].file = NULL;
719 spin_unlock(&ubd_lock);
724 static int ubd_get_config(char *name, char *str, int size, char **error_out)
729 n = parse_unit(&name);
730 if((n >= MAX_DEV) || (n < 0)){
731 *error_out = "ubd_get_config : device number out of range";
736 spin_lock(&ubd_lock);
738 if(dev->file == NULL){
739 CONFIG_CHUNK(str, size, len, "", 1);
743 CONFIG_CHUNK(str, size, len, dev->file, 0);
745 if(dev->cow.file != NULL){
746 CONFIG_CHUNK(str, size, len, ",", 0);
747 CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
749 else CONFIG_CHUNK(str, size, len, "", 1);
752 spin_unlock(&ubd_lock);
756 static int ubd_id(char **str, int *start_out, int *end_out)
762 *end_out = MAX_DEV - 1;
766 static int ubd_remove(int n)
771 spin_lock(&ubd_lock);
773 if(ubd_gendisk[n] == NULL)
778 if(dev->file == NULL)
781 /* you cannot remove a open disk */
786 del_gendisk(ubd_gendisk[n]);
787 put_disk(ubd_gendisk[n]);
788 ubd_gendisk[n] = NULL;
790 if(fake_gendisk[n] != NULL){
791 del_gendisk(fake_gendisk[n]);
792 put_disk(fake_gendisk[n]);
793 fake_gendisk[n] = NULL;
796 platform_device_unregister(&dev->pdev);
797 *dev = ((struct ubd) DEFAULT_UBD);
800 spin_unlock(&ubd_lock);
804 static struct mc_device ubd_mc = {
806 .config = ubd_config,
807 .get_config = ubd_get_config,
809 .remove = ubd_remove,
812 static int ubd_mc_init(void)
814 mconsole_register_dev(&ubd_mc);
818 __initcall(ubd_mc_init);
820 static struct platform_driver ubd_driver = {
830 if (register_blkdev(MAJOR_NR, "ubd"))
833 ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
835 unregister_blkdev(MAJOR_NR, "ubd");
839 if (fake_major != MAJOR_NR) {
840 char name[sizeof("ubd_nnn\0")];
842 snprintf(name, sizeof(name), "ubd_%d", fake_major);
843 if (register_blkdev(fake_major, "ubd"))
846 platform_driver_register(&ubd_driver);
847 for (i = 0; i < MAX_DEV; i++)
852 late_initcall(ubd_init);
854 int ubd_driver_init(void){
858 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
859 if(global_openflags.s){
860 printk(KERN_INFO "ubd: Synchronous mode\n");
861 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
862 * enough. So use anyway the io thread. */
864 stack = alloc_stack(0, 0);
865 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
869 "ubd : Failed to start I/O thread (errno = %d) - "
870 "falling back to synchronous I/O\n", -io_pid);
874 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
875 IRQF_DISABLED, "ubd", ubd_dev);
877 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
881 device_initcall(ubd_driver_init);
883 static int ubd_open(struct inode *inode, struct file *filp)
885 struct gendisk *disk = inode->i_bdev->bd_disk;
886 struct ubd *dev = disk->private_data;
890 err = ubd_open_dev(dev);
892 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
893 disk->disk_name, dev->file, -err);
898 set_disk_ro(disk, !dev->openflags.w);
900 /* This should no more be needed. And it didn't work anyway to exclude
901 * read-write remounting of filesystems.*/
902 /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
903 if(--dev->count == 0) ubd_close(dev);
910 static int ubd_release(struct inode * inode, struct file * file)
912 struct gendisk *disk = inode->i_bdev->bd_disk;
913 struct ubd *dev = disk->private_data;
915 if(--dev->count == 0)
920 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
921 __u64 *cow_offset, unsigned long *bitmap,
922 __u64 bitmap_offset, unsigned long *bitmap_words,
925 __u64 sector = io_offset >> 9;
926 int i, update_bitmap = 0;
928 for(i = 0; i < length >> 9; i++){
930 ubd_set_bit(i, (unsigned char *) cow_mask);
931 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
935 ubd_set_bit(sector + i, (unsigned char *) bitmap);
941 *cow_offset = sector / (sizeof(unsigned long) * 8);
943 /* This takes care of the case where we're exactly at the end of the
944 * device, and *cow_offset + 1 is off the end. So, just back it up
945 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
946 * for the original diagnosis.
948 if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
949 sizeof(unsigned long) - 1))
952 bitmap_words[0] = bitmap[*cow_offset];
953 bitmap_words[1] = bitmap[*cow_offset + 1];
955 *cow_offset *= sizeof(unsigned long);
956 *cow_offset += bitmap_offset;
959 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
960 __u64 bitmap_offset, __u64 bitmap_len)
962 __u64 sector = req->offset >> 9;
965 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
966 panic("Operation too long");
968 if(req->op == UBD_READ) {
969 for(i = 0; i < req->length >> 9; i++){
970 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
971 ubd_set_bit(i, (unsigned char *)
975 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
976 &req->cow_offset, bitmap, bitmap_offset,
977 req->bitmap_words, bitmap_len);
980 /* Called with ubd_io_lock held */
981 static int prepare_request(struct request *req, struct io_thread_req *io_req)
983 struct gendisk *disk = req->rq_disk;
984 struct ubd *dev = disk->private_data;
988 if(req->rq_status == RQ_INACTIVE) return(1);
990 /* This should be impossible now */
991 if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
992 printk("Write attempted on readonly ubd device %s\n",
998 offset = ((__u64) req->sector) << 9;
999 len = req->current_nr_sectors << 9;
1001 io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
1002 io_req->fds[1] = dev->fd;
1003 io_req->cow_offset = -1;
1004 io_req->offset = offset;
1005 io_req->length = len;
1007 io_req->sector_mask = 0;
1009 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1010 io_req->offsets[0] = 0;
1011 io_req->offsets[1] = dev->cow.data_offset;
1012 io_req->buffer = req->buffer;
1013 io_req->sectorsize = 1 << 9;
1015 if(dev->cow.file != NULL)
1016 cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
1017 dev->cow.bitmap_len);
1022 /* Called with ubd_io_lock held */
1023 static void do_ubd_request(request_queue_t *q)
1025 struct io_thread_req io_req;
1026 struct request *req;
1029 if(thread_fd == -1){
1030 while((req = elv_next_request(q)) != NULL){
1031 err = prepare_request(req, &io_req);
1034 __ubd_finish(req, io_req.error);
1039 if(do_ubd || (req = elv_next_request(q)) == NULL)
1041 err = prepare_request(req, &io_req);
1043 do_ubd = ubd_handler;
1044 n = os_write_file(thread_fd, (char *) &io_req,
1046 if(n != sizeof(io_req))
1047 printk("write to io thread failed, "
1048 "errno = %d\n", -n);
1053 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1055 struct ubd *dev = bdev->bd_disk->private_data;
1059 geo->cylinders = dev->size / (128 * 32 * 512);
1063 static int ubd_ioctl(struct inode * inode, struct file * file,
1064 unsigned int cmd, unsigned long arg)
1066 struct ubd *dev = inode->i_bdev->bd_disk->private_data;
1067 struct hd_driveid ubd_id = {
1074 struct cdrom_volctrl volume;
1075 case HDIO_GET_IDENTITY:
1076 ubd_id.cyls = dev->size / (128 * 32 * 512);
1077 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1083 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1085 volume.channel0 = 255;
1086 volume.channel1 = 255;
1087 volume.channel2 = 255;
1088 volume.channel3 = 255;
1089 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1096 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
1098 struct uml_stat buf1, buf2;
1101 if(from_cmdline == NULL)
1103 if(!strcmp(from_cmdline, from_cow))
1106 err = os_stat_file(from_cmdline, &buf1);
1108 printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
1111 err = os_stat_file(from_cow, &buf2);
1113 printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
1116 if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
1119 printk("Backing file mismatch - \"%s\" requested,\n"
1120 "\"%s\" specified in COW header of \"%s\"\n",
1121 from_cmdline, from_cow, cow);
1125 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
1127 unsigned long modtime;
1128 unsigned long long actual;
1131 err = os_file_modtime(file, &modtime);
1133 printk("Failed to get modification time of backing file "
1134 "\"%s\", err = %d\n", file, -err);
1138 err = os_file_size(file, &actual);
1140 printk("Failed to get size of backing file \"%s\", "
1141 "err = %d\n", file, -err);
1146 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1148 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1149 "file\n", (unsigned long long) size, actual);
1152 if(modtime != mtime){
1153 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1154 "file\n", mtime, modtime);
1160 int read_cow_bitmap(int fd, void *buf, int offset, int len)
1164 err = os_seek_file(fd, offset);
1168 err = os_read_file(fd, buf, len);
1175 int open_ubd_file(char *file, struct openflags *openflags, int shared,
1176 char **backing_file_out, int *bitmap_offset_out,
1177 unsigned long *bitmap_len_out, int *data_offset_out,
1178 int *create_cow_out)
1181 unsigned long long size;
1182 __u32 version, align;
1184 int fd, err, sectorsize, asked_switch, mode = 0644;
1186 fd = os_open_file(file, *openflags, mode);
1188 if ((fd == -ENOENT) && (create_cow_out != NULL))
1189 *create_cow_out = 1;
1190 if (!openflags->w ||
1191 ((fd != -EROFS) && (fd != -EACCES)))
1194 fd = os_open_file(file, *openflags, mode);
1200 printk("Not locking \"%s\" on the host\n", file);
1202 err = os_lock_file(fd, openflags->w);
1204 printk("Failed to lock '%s', err = %d\n", file, -err);
1209 /* Successful return case! */
1210 if(backing_file_out == NULL)
1213 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
1214 &size, §orsize, &align, bitmap_offset_out);
1215 if(err && (*backing_file_out != NULL)){
1216 printk("Failed to read COW header from COW file \"%s\", "
1217 "errno = %d\n", file, -err);
1223 asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
1225 /* Allow switching only if no mismatch. */
1226 if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
1227 printk("Switching backing file to '%s'\n", *backing_file_out);
1228 err = write_cow_header(file, fd, *backing_file_out,
1229 sectorsize, align, &size);
1231 printk("Switch failed, errno = %d\n", -err);
1235 *backing_file_out = backing_file;
1236 err = backing_file_mismatch(*backing_file_out, size, mtime);
1241 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
1242 bitmap_len_out, data_offset_out);
1250 int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
1251 int sectorsize, int alignment, int *bitmap_offset_out,
1252 unsigned long *bitmap_len_out, int *data_offset_out)
1257 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
1260 printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
1265 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
1266 bitmap_offset_out, bitmap_len_out,
1275 static int update_bitmap(struct io_thread_req *req)
1279 if(req->cow_offset == -1)
1282 n = os_seek_file(req->fds[1], req->cow_offset);
1284 printk("do_io - bitmap lseek failed : err = %d\n", -n);
1288 n = os_write_file(req->fds[1], &req->bitmap_words,
1289 sizeof(req->bitmap_words));
1290 if(n != sizeof(req->bitmap_words)){
1291 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
1299 void do_io(struct io_thread_req *req)
1303 int n, nsectors, start, end, bit;
1307 nsectors = req->length / req->sectorsize;
1310 bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
1312 while((end < nsectors) &&
1313 (ubd_test_bit(end, (unsigned char *)
1314 &req->sector_mask) == bit))
1317 off = req->offset + req->offsets[bit] +
1318 start * req->sectorsize;
1319 len = (end - start) * req->sectorsize;
1320 buf = &req->buffer[start * req->sectorsize];
1322 err = os_seek_file(req->fds[bit], off);
1324 printk("do_io - lseek failed : err = %d\n", -err);
1328 if(req->op == UBD_READ){
1333 n = os_read_file(req->fds[bit], buf, len);
1335 printk("do_io - read failed, err = %d "
1336 "fd = %d\n", -n, req->fds[bit]);
1340 } while((n < len) && (n != 0));
1341 if (n < len) memset(&buf[n], 0, len - n);
1343 n = os_write_file(req->fds[bit], buf, len);
1345 printk("do_io - write failed err = %d "
1346 "fd = %d\n", -n, req->fds[bit]);
1353 } while(start < nsectors);
1355 req->error = update_bitmap(req);
1358 /* Changed in start_io_thread, which is serialized by being called only
1359 * from ubd_init, which is an initcall.
1363 /* Only changed by the io thread */
1366 int io_thread(void *arg)
1368 struct io_thread_req req;
1371 ignore_sigwinch_sig();
1373 n = os_read_file(kernel_fd, &req, sizeof(req));
1374 if(n != sizeof(req)){
1376 printk("io_thread - read failed, fd = %d, "
1377 "err = %d\n", kernel_fd, -n);
1379 printk("io_thread - short read, fd = %d, "
1380 "length = %d\n", kernel_fd, n);
1386 n = os_write_file(kernel_fd, &req, sizeof(req));
1387 if(n != sizeof(req))
1388 printk("io_thread - write failed, fd = %d, err = %d\n",