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/config.h"
24 #include "linux/module.h"
25 #include "linux/blkdev.h"
26 #include "linux/hdreg.h"
27 #include "linux/init.h"
28 #include "linux/devfs_fs_kernel.h"
29 #include "linux/cdrom.h"
30 #include "linux/proc_fs.h"
31 #include "linux/ctype.h"
32 #include "linux/capability.h"
34 #include "linux/vmalloc.h"
35 #include "linux/blkpg.h"
36 #include "linux/genhd.h"
37 #include "linux/spinlock.h"
38 #include "asm/atomic.h"
39 #include "asm/segment.h"
40 #include "asm/uaccess.h"
42 #include "asm/types.h"
43 #include "asm/tlbflush.h"
44 #include "user_util.h"
46 #include "kern_util.h"
48 #include "mconsole_kern.h"
59 enum ubd_req { UBD_READ, UBD_WRITE };
61 struct io_thread_req {
64 unsigned long offsets[2];
65 unsigned long long offset;
75 extern int open_ubd_file(char *file, struct openflags *openflags,
76 char **backing_file_out, int *bitmap_offset_out,
77 unsigned long *bitmap_len_out, int *data_offset_out,
79 extern int create_cow_file(char *cow_file, char *backing_file,
80 struct openflags flags, int sectorsize,
81 int alignment, int *bitmap_offset_out,
82 unsigned long *bitmap_len_out,
83 int *data_offset_out);
84 extern int read_cow_bitmap(int fd, void *buf, int offset, int len);
85 extern void do_io(struct io_thread_req *req, struct request *r,
86 unsigned long *bitmap);
88 static inline int ubd_test_bit(__u64 bit, void *data)
90 unsigned char *buffer = data;
94 bits = sizeof(buffer[0]) * 8;
97 return((buffer[n] & (1 << off)) != 0);
100 static inline void ubd_set_bit(__u64 bit, void *data)
102 unsigned char *buffer = data;
106 bits = sizeof(buffer[0]) * 8;
109 buffer[n] |= (1 << off);
111 /*End stuff from ubd_user.h*/
113 #define DRIVER_NAME "uml-blkdev"
115 static DEFINE_SPINLOCK(ubd_io_lock);
116 static DEFINE_SPINLOCK(ubd_lock);
118 static int ubd_open(struct inode * inode, struct file * filp);
119 static int ubd_release(struct inode * inode, struct file * file);
120 static int ubd_ioctl(struct inode * inode, struct file * file,
121 unsigned int cmd, unsigned long arg);
125 static struct block_device_operations ubd_blops = {
126 .owner = THIS_MODULE,
128 .release = ubd_release,
132 /* Protected by the queue_lock */
133 static request_queue_t *ubd_queue;
135 /* Protected by ubd_lock */
136 static int fake_major = MAJOR_NR;
138 static struct gendisk *ubd_gendisk[MAX_DEV];
139 static struct gendisk *fake_gendisk[MAX_DEV];
141 #ifdef CONFIG_BLK_DEV_UBD_SYNC
142 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
145 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
149 /* Not protected - changed only in ubd_setup_common and then only to
152 static struct openflags global_openflags = OPEN_FLAGS;
155 /* This is the backing file, actually */
158 unsigned long *bitmap;
159 unsigned long bitmap_len;
171 struct openflags boot_openflags;
172 struct openflags openflags;
175 struct platform_device pdev;
176 struct scatterlist sg[MAX_SG];
179 #define DEFAULT_COW { \
183 .bitmap_offset = 0, \
187 #define DEFAULT_UBD { \
192 .boot_openflags = OPEN_FLAGS, \
193 .openflags = OPEN_FLAGS, \
195 .cow = DEFAULT_COW, \
198 struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
200 static int ubd0_init(void)
202 struct ubd *dev = &ubd_dev[0];
204 if(dev->file == NULL)
205 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 <= 'h')) {
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);
349 if(dev->file != NULL){
350 printk(KERN_ERR "ubd_setup : device already configured\n");
357 for (i = 0; i < 4; i++) {
372 printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r,s 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);
471 /* Changed by ubd_handler, which is serialized because interrupts only
476 static void ubd_end_request(struct request *req, int bytes, int uptodate)
478 if (!end_that_request_first(req, uptodate, bytes >> 9)) {
479 add_disk_randomness(req->rq_disk);
480 end_that_request_last(req);
484 /* call ubd_finish if you need to serialize */
485 static void __ubd_finish(struct request *req, int bytes)
488 ubd_end_request(req, 0, 0);
492 ubd_end_request(req, bytes, 1);
495 static inline void ubd_finish(struct request *req, int bytes)
497 spin_lock(&ubd_io_lock);
498 __ubd_finish(req, bytes);
499 spin_unlock(&ubd_io_lock);
504 struct aio_context aio;
508 struct aio_context aio;
511 struct bitmap_io *bitmap;
515 static int ubd_reply_fd = -1;
517 static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
519 struct aio_thread_reply reply;
522 int err, n, fd = (int) (long) dev;
525 err = os_read_file(fd, &reply, sizeof(reply));
529 printk("ubd_aio_handler - read returned err %d\n",
534 aio = container_of(reply.data, struct ubd_aio, aio);
539 req->nr_sectors -= aio->len >> 9;
541 if((aio->bitmap != NULL) &&
542 (atomic_dec_and_test(&aio->bitmap->count))){
543 aio->aio = aio->bitmap->aio;
547 submit_aio(&aio->aio);
550 if((req->nr_sectors == 0) &&
551 (aio->bitmap == NULL)){
552 int len = req->hard_nr_sectors << 9;
553 ubd_finish(req, len);
556 if(aio->bitmap_buf != NULL)
557 kfree(aio->bitmap_buf);
562 ubd_finish(aio->req, n);
563 if(aio->bitmap != NULL)
565 if(aio->bitmap_buf != NULL)
566 kfree(aio->bitmap_buf);
570 reactivate_fd(fd, UBD_IRQ);
572 do_ubd_request(ubd_queue);
577 static int ubd_file_size(struct ubd *dev, __u64 *size_out)
581 file = dev->cow.file ? dev->cow.file : dev->file;
582 return(os_file_size(file, size_out));
585 static void ubd_close(struct ubd *dev)
587 os_close_file(dev->fd);
588 if(dev->cow.file == NULL)
591 os_close_file(dev->cow.fd);
592 vfree(dev->cow.bitmap);
593 dev->cow.bitmap = NULL;
596 static int ubd_open_dev(struct ubd *dev)
598 struct openflags flags;
600 int err, create_cow, *create_ptr;
602 dev->openflags = dev->boot_openflags;
604 create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
605 back_ptr = dev->no_cow ? NULL : &dev->cow.file;
606 dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
607 &dev->cow.bitmap_offset, &dev->cow.bitmap_len,
608 &dev->cow.data_offset, create_ptr);
610 if((dev->fd == -ENOENT) && create_cow){
611 dev->fd = create_cow_file(dev->file, dev->cow.file,
612 dev->openflags, 1 << 9, PAGE_SIZE,
613 &dev->cow.bitmap_offset,
614 &dev->cow.bitmap_len,
615 &dev->cow.data_offset);
617 printk(KERN_INFO "Creating \"%s\" as COW file for "
618 "\"%s\"\n", dev->file, dev->cow.file);
623 printk("Failed to open '%s', errno = %d\n", dev->file,
628 if(dev->cow.file != NULL){
630 dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
631 if(dev->cow.bitmap == NULL){
632 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
635 flush_tlb_kernel_vm();
637 err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
638 dev->cow.bitmap_offset,
639 dev->cow.bitmap_len);
643 flags = dev->openflags;
645 err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL,
647 if(err < 0) goto error;
652 os_close_file(dev->fd);
656 static int ubd_new_disk(int major, u64 size, int unit,
657 struct gendisk **disk_out)
660 struct gendisk *disk;
661 char from[sizeof("ubd/nnnnn\0")], to[sizeof("discnnnnn/disc\0")];
664 disk = alloc_disk(1 << UBD_SHIFT);
669 disk->first_minor = unit << UBD_SHIFT;
670 disk->fops = &ubd_blops;
671 set_capacity(disk, size / 512);
672 if(major == MAJOR_NR){
673 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
674 sprintf(disk->devfs_name, "ubd/disc%d", unit);
675 sprintf(from, "ubd/%d", unit);
676 sprintf(to, "disc%d/disc", unit);
677 err = devfs_mk_symlink(from, to);
679 printk("ubd_new_disk failed to make link from %s to "
680 "%s, error = %d\n", from, to, err);
683 sprintf(disk->disk_name, "ubd_fake%d", unit);
684 sprintf(disk->devfs_name, "ubd_fake/disc%d", unit);
687 /* sysfs register (not for ide fake devices) */
688 if (major == MAJOR_NR) {
689 ubd_dev[unit].pdev.id = unit;
690 ubd_dev[unit].pdev.name = DRIVER_NAME;
691 platform_device_register(&ubd_dev[unit].pdev);
692 disk->driverfs_dev = &ubd_dev[unit].pdev.dev;
695 disk->private_data = &ubd_dev[unit];
696 disk->queue = ubd_queue;
703 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
705 static int ubd_add(int n)
707 struct ubd *dev = &ubd_dev[n];
711 if(dev->file == NULL)
714 if (ubd_open_dev(dev))
717 err = ubd_file_size(dev, &dev->size);
721 dev->size = ROUND_BLOCK(dev->size);
723 err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
727 if(fake_major != MAJOR_NR)
728 ubd_new_disk(fake_major, dev->size, n,
731 /* perhaps this should also be under the "if (fake_major)" above */
732 /* using the fake_disk->disk_name and also the fakehd_set name */
734 make_ide_entries(ubd_gendisk[n]->disk_name);
743 static int ubd_config(char *str)
747 str = uml_strdup(str);
749 printk(KERN_ERR "ubd_config failed to strdup string\n");
752 err = ubd_setup_common(str, &n);
757 if(n == -1) return(0);
759 spin_lock(&ubd_lock);
762 ubd_dev[n].file = NULL;
763 spin_unlock(&ubd_lock);
768 static int ubd_get_config(char *name, char *str, int size, char **error_out)
773 n = parse_unit(&name);
774 if((n >= MAX_DEV) || (n < 0)){
775 *error_out = "ubd_get_config : device number out of range";
780 spin_lock(&ubd_lock);
782 if(dev->file == NULL){
783 CONFIG_CHUNK(str, size, len, "", 1);
787 CONFIG_CHUNK(str, size, len, dev->file, 0);
789 if(dev->cow.file != NULL){
790 CONFIG_CHUNK(str, size, len, ",", 0);
791 CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
793 else CONFIG_CHUNK(str, size, len, "", 1);
796 spin_unlock(&ubd_lock);
800 static int ubd_id(char **str, int *start_out, int *end_out)
806 *end_out = MAX_DEV - 1;
810 static int ubd_remove(int n)
815 spin_lock(&ubd_lock);
817 if(ubd_gendisk[n] == NULL)
822 if(dev->file == NULL)
825 /* you cannot remove a open disk */
830 del_gendisk(ubd_gendisk[n]);
831 put_disk(ubd_gendisk[n]);
832 ubd_gendisk[n] = NULL;
834 if(fake_gendisk[n] != NULL){
835 del_gendisk(fake_gendisk[n]);
836 put_disk(fake_gendisk[n]);
837 fake_gendisk[n] = NULL;
840 platform_device_unregister(&dev->pdev);
841 *dev = ((struct ubd) DEFAULT_UBD);
844 spin_unlock(&ubd_lock);
848 static struct mc_device ubd_mc = {
850 .config = ubd_config,
851 .get_config = ubd_get_config,
853 .remove = ubd_remove,
856 static int ubd_mc_init(void)
858 mconsole_register_dev(&ubd_mc);
862 __initcall(ubd_mc_init);
864 static struct device_driver ubd_driver = {
866 .bus = &platform_bus_type,
873 ubd_reply_fd = init_aio_irq(UBD_IRQ, "ubd", ubd_intr);
875 printk("Setting up ubd AIO failed, err = %d\n", ubd_reply_fd);
878 if (register_blkdev(MAJOR_NR, "ubd"))
881 ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
883 unregister_blkdev(MAJOR_NR, "ubd");
887 blk_queue_max_hw_segments(ubd_queue, MAX_SG);
888 if (fake_major != MAJOR_NR) {
889 char name[sizeof("ubd_nnn\0")];
891 snprintf(name, sizeof(name), "ubd_%d", fake_major);
893 if (register_blkdev(fake_major, "ubd"))
896 driver_register(&ubd_driver);
897 for (i = 0; i < MAX_DEV; i++)
903 late_initcall(ubd_init);
905 static int ubd_open(struct inode *inode, struct file *filp)
907 struct gendisk *disk = inode->i_bdev->bd_disk;
908 struct ubd *dev = disk->private_data;
912 err = ubd_open_dev(dev);
914 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
915 disk->disk_name, dev->file, -err);
920 set_disk_ro(disk, !dev->openflags.w);
922 /* This should no more be needed. And it didn't work anyway to exclude
923 * read-write remounting of filesystems.*/
924 /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
925 if(--dev->count == 0) ubd_close(dev);
932 static int ubd_release(struct inode * inode, struct file * file)
934 struct gendisk *disk = inode->i_bdev->bd_disk;
935 struct ubd *dev = disk->private_data;
937 if(--dev->count == 0)
942 static void cowify_bitmap(struct io_thread_req *req, unsigned long *bitmap)
944 __u64 sector = req->offset / req->sectorsize;
947 for(i = 0; i < req->length / req->sectorsize; i++){
948 if(ubd_test_bit(sector + i, bitmap))
951 if(req->bitmap_start == -1)
952 req->bitmap_start = sector + i;
953 req->bitmap_end = sector + i + 1;
955 ubd_set_bit(sector + i, bitmap);
959 /* Called with ubd_io_lock held */
960 static int prepare_request(struct request *req, struct io_thread_req *io_req,
961 unsigned long long offset, int page_offset,
962 int len, struct page *page)
964 struct gendisk *disk = req->rq_disk;
965 struct ubd *dev = disk->private_data;
967 /* This should be impossible now */
968 if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
969 printk("Write attempted on readonly ubd device %s\n",
971 ubd_end_request(req, 0, 0);
975 io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
976 io_req->fds[1] = dev->fd;
977 io_req->offset = offset;
978 io_req->length = len;
980 io_req->op = (rq_data_dir(req) == READ) ? AIO_READ : AIO_WRITE;
981 io_req->offsets[0] = 0;
982 io_req->offsets[1] = dev->cow.data_offset;
983 io_req->buffer = page_address(page) + page_offset;
984 io_req->sectorsize = 1 << 9;
985 io_req->bitmap_offset = dev->cow.bitmap_offset;
986 io_req->bitmap_start = -1;
987 io_req->bitmap_end = -1;
989 if((dev->cow.file != NULL) && (io_req->op == UBD_WRITE))
990 cowify_bitmap(io_req, dev->cow.bitmap);
994 /* Called with ubd_io_lock held */
995 static void do_ubd_request(request_queue_t *q)
997 struct io_thread_req io_req;
1005 while((req = elv_next_request(q)) != NULL){
1006 struct gendisk *disk = req->rq_disk;
1007 struct ubd *dev = disk->private_data;
1010 blkdev_dequeue_request(req);
1012 sector = req->sector;
1013 n = blk_rq_map_sg(q, req, dev->sg);
1015 for(i = 0; i < n; i++){
1016 struct scatterlist *sg = &dev->sg[i];
1018 err = prepare_request(req, &io_req, sector << 9,
1019 sg->offset, sg->length,
1024 sector += sg->length >> 9;
1025 do_io(&io_req, req, dev->cow.bitmap);
1031 static int ubd_ioctl(struct inode * inode, struct file * file,
1032 unsigned int cmd, unsigned long arg)
1034 struct hd_geometry __user *loc = (struct hd_geometry __user *) arg;
1035 struct ubd *dev = inode->i_bdev->bd_disk->private_data;
1036 struct hd_driveid ubd_id = {
1043 struct hd_geometry g;
1044 struct cdrom_volctrl volume;
1046 if(!loc) return(-EINVAL);
1049 g.cylinders = dev->size / (128 * 32 * 512);
1050 g.start = get_start_sect(inode->i_bdev);
1051 return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
1053 case HDIO_GET_IDENTITY:
1054 ubd_id.cyls = dev->size / (128 * 32 * 512);
1055 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1061 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1063 volume.channel0 = 255;
1064 volume.channel1 = 255;
1065 volume.channel2 = 255;
1066 volume.channel3 = 255;
1067 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1074 static int same_backing_files(char *from_cmdline, char *from_cow, char *cow)
1076 struct uml_stat buf1, buf2;
1079 if(from_cmdline == NULL) return(1);
1080 if(!strcmp(from_cmdline, from_cow)) return(1);
1082 err = os_stat_file(from_cmdline, &buf1);
1084 printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
1087 err = os_stat_file(from_cow, &buf2);
1089 printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
1092 if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
1095 printk("Backing file mismatch - \"%s\" requested,\n"
1096 "\"%s\" specified in COW header of \"%s\"\n",
1097 from_cmdline, from_cow, cow);
1101 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
1103 unsigned long modtime;
1107 err = os_file_modtime(file, &modtime);
1109 printk("Failed to get modification time of backing file "
1110 "\"%s\", err = %d\n", file, -err);
1114 err = os_file_size(file, &actual);
1116 printk("Failed to get size of backing file \"%s\", "
1117 "err = %d\n", file, -err);
1122 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1124 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1125 "file\n", (unsigned long long) size, actual);
1128 if(modtime != mtime){
1129 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1130 "file\n", mtime, modtime);
1136 int read_cow_bitmap(int fd, void *buf, int offset, int len)
1140 err = os_seek_file(fd, offset);
1144 err = os_read_file(fd, buf, len);
1151 int open_ubd_file(char *file, struct openflags *openflags,
1152 char **backing_file_out, int *bitmap_offset_out,
1153 unsigned long *bitmap_len_out, int *data_offset_out,
1154 int *create_cow_out)
1157 unsigned long long size;
1158 __u32 version, align;
1160 int fd, err, sectorsize, same, mode = 0644;
1162 fd = os_open_file(file, *openflags, mode);
1164 if((fd == -ENOENT) && (create_cow_out != NULL))
1165 *create_cow_out = 1;
1167 ((fd != -EROFS) && (fd != -EACCES))) return(fd);
1169 fd = os_open_file(file, *openflags, mode);
1174 err = os_lock_file(fd, openflags->w);
1176 printk("Failed to lock '%s', err = %d\n", file, -err);
1180 if(backing_file_out == NULL) return(fd);
1182 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
1183 &size, §orsize, &align, bitmap_offset_out);
1184 if(err && (*backing_file_out != NULL)){
1185 printk("Failed to read COW header from COW file \"%s\", "
1186 "errno = %d\n", file, -err);
1191 if(backing_file_out == NULL) return(fd);
1193 same = same_backing_files(*backing_file_out, backing_file, file);
1195 if(!same && !backing_file_mismatch(*backing_file_out, size, mtime)){
1196 printk("Switching backing file to '%s'\n", *backing_file_out);
1197 err = write_cow_header(file, fd, *backing_file_out,
1198 sectorsize, align, &size);
1200 printk("Switch failed, errno = %d\n", -err);
1205 *backing_file_out = backing_file;
1206 err = backing_file_mismatch(*backing_file_out, size, mtime);
1207 if(err) goto out_close;
1210 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
1211 bitmap_len_out, data_offset_out);
1219 int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
1220 int sectorsize, int alignment, int *bitmap_offset_out,
1221 unsigned long *bitmap_len_out, int *data_offset_out)
1226 fd = open_ubd_file(cow_file, &flags, NULL, NULL, NULL, NULL, NULL);
1229 printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
1234 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
1235 bitmap_offset_out, bitmap_len_out,
1244 void do_io(struct io_thread_req *req, struct request *r, unsigned long *bitmap)
1246 struct ubd_aio *aio;
1247 struct bitmap_io *bitmap_io = NULL;
1249 void *bitmap_buf = NULL;
1250 unsigned long len, sector;
1251 int nsectors, start, end, bit, err;
1254 if(req->bitmap_start != -1){
1255 /* Round up to the nearest word */
1256 int round = sizeof(unsigned long);
1257 len = (req->bitmap_end - req->bitmap_start +
1258 round * 8 - 1) / (round * 8);
1261 off = req->bitmap_start / (8 * round);
1264 bitmap_io = kmalloc(sizeof(*bitmap_io), GFP_KERNEL);
1265 if(bitmap_io == NULL){
1266 printk("Failed to kmalloc bitmap IO\n");
1271 bitmap_buf = kmalloc(len, GFP_KERNEL);
1272 if(bitmap_buf == NULL){
1273 printk("do_io : kmalloc of bitmap chunk "
1279 memcpy(bitmap_buf, &bitmap[off / sizeof(bitmap[0])], len);
1281 *bitmap_io = ((struct bitmap_io)
1282 { .count = ATOMIC_INIT(0),
1283 .aio = INIT_AIO(AIO_WRITE, req->fds[1],
1285 req->bitmap_offset + off,
1289 nsectors = req->length / req->sectorsize;
1295 sector = req->offset / req->sectorsize;
1296 bit = ubd_test_bit(sector + start, bitmap);
1298 while((end < nsectors) &&
1299 (ubd_test_bit(sector + end, bitmap) == bit))
1303 off = req->offsets[bit] + req->offset +
1304 start * req->sectorsize;
1305 len = (end - start) * req->sectorsize;
1306 buf = &req->buffer[start * req->sectorsize];
1308 aio = kmalloc(sizeof(*aio), GFP_KERNEL);
1314 *aio = ((struct ubd_aio)
1315 { .aio = INIT_AIO(req->op, req->fds[bit], buf,
1316 len, off, ubd_reply_fd),
1319 .bitmap = bitmap_io,
1320 .bitmap_buf = bitmap_buf });
1322 if(aio->bitmap != NULL)
1323 atomic_inc(&aio->bitmap->count);
1325 err = submit_aio(&aio->aio);
1327 printk("do_io - submit_aio failed, "
1334 } while(start < nsectors);