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/segment.h"
39 #include "asm/uaccess.h"
41 #include "asm/types.h"
42 #include "asm/tlbflush.h"
43 #include "user_util.h"
45 #include "kern_util.h"
47 #include "mconsole_kern.h"
52 #include "2_5compat.h"
58 enum ubd_req
{ UBD_READ
, UBD_WRITE
, UBD_MMAP
};
60 struct io_thread_req
{
63 unsigned long offsets
[2];
64 unsigned long long offset
;
68 unsigned long sector_mask
;
69 unsigned long long cow_offset
;
70 unsigned long bitmap_words
[2];
72 unsigned long long map_offset
;
76 extern int open_ubd_file(char *file
, struct openflags
*openflags
,
77 char **backing_file_out
, int *bitmap_offset_out
,
78 unsigned long *bitmap_len_out
, int *data_offset_out
,
80 extern int create_cow_file(char *cow_file
, char *backing_file
,
81 struct openflags flags
, int sectorsize
,
82 int alignment
, int *bitmap_offset_out
,
83 unsigned long *bitmap_len_out
,
84 int *data_offset_out
);
85 extern int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
);
86 extern void do_io(struct io_thread_req
*req
);
88 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
93 bits
= sizeof(data
[0]) * 8;
96 return((data
[n
] & (1 << off
)) != 0);
99 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
104 bits
= sizeof(data
[0]) * 8;
107 data
[n
] |= (1 << off
);
109 /*End stuff from ubd_user.h*/
111 #define DRIVER_NAME "uml-blkdev"
113 static DEFINE_SPINLOCK(ubd_io_lock
);
114 static DEFINE_SPINLOCK(ubd_lock
);
116 static void (*do_ubd
)(void);
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 /* Changed in early boot */
126 static int ubd_do_mmap
= 0;
127 #define UBD_MMAP_BLOCK_SIZE PAGE_SIZE
129 static struct block_device_operations ubd_blops
= {
130 .owner
= THIS_MODULE
,
132 .release
= ubd_release
,
136 /* Protected by the queue_lock */
137 static request_queue_t
*ubd_queue
;
139 /* Protected by ubd_lock */
140 static int fake_major
= MAJOR_NR
;
142 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
143 static struct gendisk
*fake_gendisk
[MAX_DEV
];
145 #ifdef CONFIG_BLK_DEV_UBD_SYNC
146 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
149 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
153 /* Not protected - changed only in ubd_setup_common and then only to
156 static struct openflags global_openflags
= OPEN_FLAGS
;
159 /* This is the backing file, actually */
162 unsigned long *bitmap
;
163 unsigned long bitmap_len
;
173 struct openflags boot_openflags
;
174 struct openflags openflags
;
177 struct platform_device pdev
;
186 #define DEFAULT_COW { \
190 .bitmap_offset = 0, \
194 #define DEFAULT_UBD { \
199 .boot_openflags = OPEN_FLAGS, \
200 .openflags = OPEN_FLAGS, \
202 .cow = DEFAULT_COW, \
210 struct ubd ubd_dev
[MAX_DEV
] = { [ 0 ... MAX_DEV
- 1 ] = DEFAULT_UBD
};
212 static int ubd0_init(void)
214 struct ubd
*dev
= &ubd_dev
[0];
216 if(dev
->file
== NULL
)
217 dev
->file
= "root_fs";
221 __initcall(ubd0_init
);
223 /* Only changed by fake_ide_setup which is a setup */
224 static int fake_ide
= 0;
225 static struct proc_dir_entry
*proc_ide_root
= NULL
;
226 static struct proc_dir_entry
*proc_ide
= NULL
;
228 static void make_proc_ide(void)
230 proc_ide_root
= proc_mkdir("ide", NULL
);
231 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
234 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
235 int *eof
, void *data
)
239 strcpy(page
, "disk\n");
240 len
= strlen("disk\n");
244 if (len
<= 0) return 0;
251 static void make_ide_entries(char *dev_name
)
253 struct proc_dir_entry
*dir
, *ent
;
256 if(proc_ide_root
== NULL
) make_proc_ide();
258 dir
= proc_mkdir(dev_name
, proc_ide
);
261 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
265 ent
->read_proc
= proc_ide_read_media
;
266 ent
->write_proc
= NULL
;
267 sprintf(name
,"ide0/%s", dev_name
);
268 proc_symlink(dev_name
, proc_ide_root
, name
);
271 static int fake_ide_setup(char *str
)
277 __setup("fake_ide", fake_ide_setup
);
279 __uml_help(fake_ide_setup
,
281 " Create ide0 entries that map onto ubd devices.\n\n"
284 static int parse_unit(char **ptr
)
286 char *str
= *ptr
, *end
;
290 n
= simple_strtoul(str
, &end
, 0);
295 else if (('a' <= *str
) && (*str
<= 'h')) {
303 static int ubd_setup_common(char *str
, int *index_out
)
306 struct openflags flags
= global_openflags
;
310 if(index_out
) *index_out
= -1;
317 if(!strcmp(str
, "mmap")){
318 CHOOSE_MODE(printk("mmap not supported by the ubd "
319 "driver in tt mode\n"),
324 if(!strcmp(str
, "sync")){
325 global_openflags
= of_sync(global_openflags
);
328 major
= simple_strtoul(str
, &end
, 0);
329 if((*end
!= '\0') || (end
== str
)){
331 "ubd_setup : didn't parse major number\n");
336 spin_lock(&ubd_lock
);
337 if(fake_major
!= MAJOR_NR
){
338 printk(KERN_ERR
"Can't assign a fake major twice\n");
344 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
348 spin_unlock(&ubd_lock
);
352 n
= parse_unit(&str
);
354 printk(KERN_ERR
"ubd_setup : couldn't parse unit number "
359 printk(KERN_ERR
"ubd_setup : index %d out of range "
360 "(%d devices, from 0 to %d)\n", n
, MAX_DEV
, MAX_DEV
- 1);
365 spin_lock(&ubd_lock
);
368 if(dev
->file
!= NULL
){
369 printk(KERN_ERR
"ubd_setup : device already configured\n");
376 for (i
= 0; i
< 4; i
++) {
391 printk(KERN_ERR
"ubd_setup : Expected '=' or flag letter (r,s or d)\n");
398 printk(KERN_ERR
"ubd_setup : Too many flags specified\n");
400 printk(KERN_ERR
"ubd_setup : Expected '='\n");
405 backing_file
= strchr(str
, ',');
408 backing_file
= strchr(str
, ':');
413 printk(KERN_ERR
"Can't specify both 'd' and a "
416 *backing_file
= '\0';
421 dev
->cow
.file
= backing_file
;
422 dev
->boot_openflags
= flags
;
424 spin_unlock(&ubd_lock
);
428 static int ubd_setup(char *str
)
430 ubd_setup_common(str
, NULL
);
434 __setup("ubd", ubd_setup
);
435 __uml_help(ubd_setup
,
436 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
437 " This is used to associate a device with a file in the underlying\n"
438 " filesystem. When specifying two filenames, the first one is the\n"
439 " COW name and the second is the backing file name. As separator you can\n"
440 " use either a ':' or a ',': the first one allows writing things like;\n"
441 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
442 " while with a ',' the shell would not expand the 2nd '~'.\n"
443 " When using only one filename, UML will detect whether to thread it like\n"
444 " a COW file or a backing file. To override this detection, add the 'd'\n"
446 " ubd0d=BackingFile\n"
447 " Usually, there is a filesystem in the file, but \n"
448 " that's not required. Swap devices containing swap files can be\n"
449 " specified like this. Also, a file which doesn't contain a\n"
450 " filesystem can have its contents read in the virtual \n"
451 " machine by running 'dd' on the device. <n> must be in the range\n"
452 " 0 to 7. Appending an 'r' to the number will cause that device\n"
453 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
454 " an 's' will cause data to be written to disk on the host immediately.\n\n"
457 static int udb_setup(char *str
)
459 printk("udb%s specified on command line is almost certainly a ubd -> "
464 __setup("udb", udb_setup
);
465 __uml_help(udb_setup
,
467 " This option is here solely to catch ubd -> udb typos, which can be\n\n"
468 " to impossible to catch visually unless you specifically look for\n\n"
469 " them. The only result of any option starting with 'udb' is an error\n\n"
470 " in the boot output.\n\n"
473 static int fakehd_set
= 0;
474 static int fakehd(char *str
)
476 printk(KERN_INFO
"fakehd : Changing ubd name to \"hd\".\n");
481 __setup("fakehd", fakehd
);
484 " Change the ubd device name to \"hd\".\n\n"
487 static void do_ubd_request(request_queue_t
* q
);
489 /* Only changed by ubd_init, which is an initcall. */
492 /* Changed by ubd_handler, which is serialized because interrupts only
497 /* call ubd_finish if you need to serialize */
498 static void __ubd_finish(struct request
*req
, int error
)
506 nsect
= req
->current_nr_sectors
;
507 req
->sector
+= nsect
;
508 req
->buffer
+= nsect
<< 9;
510 req
->nr_sectors
-= nsect
;
511 req
->current_nr_sectors
= 0;
515 static inline void ubd_finish(struct request
*req
, int error
)
517 spin_lock(&ubd_io_lock
);
518 __ubd_finish(req
, error
);
519 spin_unlock(&ubd_io_lock
);
522 /* Called without ubd_io_lock held */
523 static void ubd_handler(void)
525 struct io_thread_req req
;
526 struct request
*rq
= elv_next_request(ubd_queue
);
531 n
= os_read_file(thread_fd
, &req
, sizeof(req
));
532 if(n
!= sizeof(req
)){
533 printk(KERN_ERR
"Pid %d - spurious interrupt in ubd_handler, "
534 "err = %d\n", os_getpid(), -n
);
535 spin_lock(&ubd_io_lock
);
537 spin_unlock(&ubd_io_lock
);
541 if((req
.op
!= UBD_MMAP
) &&
542 ((req
.offset
!= ((__u64
) (rq
->sector
)) << 9) ||
543 (req
.length
!= (rq
->current_nr_sectors
) << 9)))
544 panic("I/O op mismatch");
546 if(req
.map_fd
!= -1){
547 err
= physmem_subst_mapping(req
.buffer
, req
.map_fd
,
550 printk("ubd_handler - physmem_subst_mapping failed, "
554 ubd_finish(rq
, req
.error
);
555 reactivate_fd(thread_fd
, UBD_IRQ
);
556 do_ubd_request(ubd_queue
);
559 static irqreturn_t
ubd_intr(int irq
, void *dev
, struct pt_regs
*unused
)
565 /* Only changed by ubd_init, which is an initcall. */
566 static int io_pid
= -1;
568 void kill_io_thread(void)
571 os_kill_process(io_pid
, 1);
574 __uml_exitcall(kill_io_thread
);
576 static int ubd_file_size(struct ubd
*dev
, __u64
*size_out
)
580 file
= dev
->cow
.file
? dev
->cow
.file
: dev
->file
;
581 return(os_file_size(file
, size_out
));
584 static void ubd_close(struct ubd
*dev
)
587 physmem_forget_descriptor(dev
->fd
);
588 os_close_file(dev
->fd
);
589 if(dev
->cow
.file
== NULL
)
593 physmem_forget_descriptor(dev
->cow
.fd
);
594 os_close_file(dev
->cow
.fd
);
595 vfree(dev
->cow
.bitmap
);
596 dev
->cow
.bitmap
= NULL
;
599 static int ubd_open_dev(struct ubd
*dev
)
601 struct openflags flags
;
603 int err
, create_cow
, *create_ptr
;
605 dev
->openflags
= dev
->boot_openflags
;
607 create_ptr
= (dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
608 back_ptr
= dev
->no_cow
? NULL
: &dev
->cow
.file
;
609 dev
->fd
= open_ubd_file(dev
->file
, &dev
->openflags
, back_ptr
,
610 &dev
->cow
.bitmap_offset
, &dev
->cow
.bitmap_len
,
611 &dev
->cow
.data_offset
, create_ptr
);
613 if((dev
->fd
== -ENOENT
) && create_cow
){
614 dev
->fd
= create_cow_file(dev
->file
, dev
->cow
.file
,
615 dev
->openflags
, 1 << 9, PAGE_SIZE
,
616 &dev
->cow
.bitmap_offset
,
617 &dev
->cow
.bitmap_len
,
618 &dev
->cow
.data_offset
);
620 printk(KERN_INFO
"Creating \"%s\" as COW file for "
621 "\"%s\"\n", dev
->file
, dev
->cow
.file
);
626 printk("Failed to open '%s', errno = %d\n", dev
->file
,
631 if(dev
->cow
.file
!= NULL
){
633 dev
->cow
.bitmap
= (void *) vmalloc(dev
->cow
.bitmap_len
);
634 if(dev
->cow
.bitmap
== NULL
){
635 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
638 flush_tlb_kernel_vm();
640 err
= read_cow_bitmap(dev
->fd
, dev
->cow
.bitmap
,
641 dev
->cow
.bitmap_offset
,
642 dev
->cow
.bitmap_len
);
646 flags
= dev
->openflags
;
648 err
= open_ubd_file(dev
->cow
.file
, &flags
, NULL
, NULL
, NULL
,
650 if(err
< 0) goto error
;
655 os_close_file(dev
->fd
);
659 static int ubd_new_disk(int major
, u64 size
, int unit
,
660 struct gendisk
**disk_out
)
663 struct gendisk
*disk
;
664 char from
[sizeof("ubd/nnnnn\0")], to
[sizeof("discnnnnn/disc\0")];
667 disk
= alloc_disk(1 << UBD_SHIFT
);
672 disk
->first_minor
= unit
<< UBD_SHIFT
;
673 disk
->fops
= &ubd_blops
;
674 set_capacity(disk
, size
/ 512);
675 if(major
== MAJOR_NR
){
676 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
677 sprintf(disk
->devfs_name
, "ubd/disc%d", unit
);
678 sprintf(from
, "ubd/%d", unit
);
679 sprintf(to
, "disc%d/disc", unit
);
680 err
= devfs_mk_symlink(from
, to
);
682 printk("ubd_new_disk failed to make link from %s to "
683 "%s, error = %d\n", from
, to
, err
);
686 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
687 sprintf(disk
->devfs_name
, "ubd_fake/disc%d", unit
);
690 /* sysfs register (not for ide fake devices) */
691 if (major
== MAJOR_NR
) {
692 ubd_dev
[unit
].pdev
.id
= unit
;
693 ubd_dev
[unit
].pdev
.name
= DRIVER_NAME
;
694 platform_device_register(&ubd_dev
[unit
].pdev
);
695 disk
->driverfs_dev
= &ubd_dev
[unit
].pdev
.dev
;
698 disk
->private_data
= &ubd_dev
[unit
];
699 disk
->queue
= ubd_queue
;
706 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
708 static int ubd_add(int n
)
710 struct ubd
*dev
= &ubd_dev
[n
];
713 if(dev
->file
== NULL
)
716 if (ubd_open_dev(dev
))
719 err
= ubd_file_size(dev
, &dev
->size
);
723 dev
->size
= ROUND_BLOCK(dev
->size
);
725 err
= ubd_new_disk(MAJOR_NR
, dev
->size
, n
, &ubd_gendisk
[n
]);
729 if(fake_major
!= MAJOR_NR
)
730 ubd_new_disk(fake_major
, dev
->size
, n
,
733 /* perhaps this should also be under the "if (fake_major)" above */
734 /* using the fake_disk->disk_name and also the fakehd_set name */
736 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
742 static int ubd_config(char *str
)
746 str
= uml_strdup(str
);
748 printk(KERN_ERR
"ubd_config failed to strdup string\n");
751 err
= ubd_setup_common(str
, &n
);
756 if(n
== -1) return(0);
758 spin_lock(&ubd_lock
);
761 ubd_dev
[n
].file
= NULL
;
762 spin_unlock(&ubd_lock
);
767 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
772 n
= parse_unit(&name
);
773 if((n
>= MAX_DEV
) || (n
< 0)){
774 *error_out
= "ubd_get_config : device number out of range";
779 spin_lock(&ubd_lock
);
781 if(dev
->file
== NULL
){
782 CONFIG_CHUNK(str
, size
, len
, "", 1);
786 CONFIG_CHUNK(str
, size
, len
, dev
->file
, 0);
788 if(dev
->cow
.file
!= NULL
){
789 CONFIG_CHUNK(str
, size
, len
, ",", 0);
790 CONFIG_CHUNK(str
, size
, len
, dev
->cow
.file
, 1);
792 else CONFIG_CHUNK(str
, size
, len
, "", 1);
795 spin_unlock(&ubd_lock
);
799 static int ubd_remove(char *str
)
802 int n
, err
= -ENODEV
;
804 n
= parse_unit(&str
);
806 if((n
< 0) || (n
>= MAX_DEV
))
811 return(-EBUSY
); /* you cannot remove a open disk */
814 spin_lock(&ubd_lock
);
816 if(ubd_gendisk
[n
] == NULL
)
819 del_gendisk(ubd_gendisk
[n
]);
820 put_disk(ubd_gendisk
[n
]);
821 ubd_gendisk
[n
] = NULL
;
823 if(fake_gendisk
[n
] != NULL
){
824 del_gendisk(fake_gendisk
[n
]);
825 put_disk(fake_gendisk
[n
]);
826 fake_gendisk
[n
] = NULL
;
829 platform_device_unregister(&dev
->pdev
);
830 *dev
= ((struct ubd
) DEFAULT_UBD
);
833 spin_unlock(&ubd_lock
);
837 static struct mc_device ubd_mc
= {
839 .config
= ubd_config
,
840 .get_config
= ubd_get_config
,
841 .remove
= ubd_remove
,
844 static int ubd_mc_init(void)
846 mconsole_register_dev(&ubd_mc
);
850 __initcall(ubd_mc_init
);
852 static struct device_driver ubd_driver
= {
854 .bus
= &platform_bus_type
,
862 if (register_blkdev(MAJOR_NR
, "ubd"))
865 ubd_queue
= blk_init_queue(do_ubd_request
, &ubd_io_lock
);
867 unregister_blkdev(MAJOR_NR
, "ubd");
871 if (fake_major
!= MAJOR_NR
) {
872 char name
[sizeof("ubd_nnn\0")];
874 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
876 if (register_blkdev(fake_major
, "ubd"))
879 driver_register(&ubd_driver
);
880 for (i
= 0; i
< MAX_DEV
; i
++)
885 late_initcall(ubd_init
);
887 int ubd_driver_init(void){
891 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
892 if(global_openflags
.s
){
893 printk(KERN_INFO
"ubd: Synchronous mode\n");
894 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
895 * enough. So use anyway the io thread. */
897 stack
= alloc_stack(0, 0);
898 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
902 "ubd : Failed to start I/O thread (errno = %d) - "
903 "falling back to synchronous I/O\n", -io_pid
);
907 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
908 SA_INTERRUPT
, "ubd", ubd_dev
);
910 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
914 device_initcall(ubd_driver_init
);
916 static int ubd_open(struct inode
*inode
, struct file
*filp
)
918 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
919 struct ubd
*dev
= disk
->private_data
;
923 err
= ubd_open_dev(dev
);
925 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
926 disk
->disk_name
, dev
->file
, -err
);
931 set_disk_ro(disk
, !dev
->openflags
.w
);
933 /* This should no more be needed. And it didn't work anyway to exclude
934 * read-write remounting of filesystems.*/
935 /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
936 if(--dev->count == 0) ubd_close(dev);
943 static int ubd_release(struct inode
* inode
, struct file
* file
)
945 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
946 struct ubd
*dev
= disk
->private_data
;
948 if(--dev
->count
== 0)
953 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
954 __u64
*cow_offset
, unsigned long *bitmap
,
955 __u64 bitmap_offset
, unsigned long *bitmap_words
,
958 __u64 sector
= io_offset
>> 9;
959 int i
, update_bitmap
= 0;
961 for(i
= 0; i
< length
>> 9; i
++){
963 ubd_set_bit(i
, (unsigned char *) cow_mask
);
964 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
968 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
974 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
976 /* This takes care of the case where we're exactly at the end of the
977 * device, and *cow_offset + 1 is off the end. So, just back it up
978 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
979 * for the original diagnosis.
981 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
982 sizeof(unsigned long) - 1))
985 bitmap_words
[0] = bitmap
[*cow_offset
];
986 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
988 *cow_offset
*= sizeof(unsigned long);
989 *cow_offset
+= bitmap_offset
;
992 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
993 __u64 bitmap_offset
, __u64 bitmap_len
)
995 __u64 sector
= req
->offset
>> 9;
998 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
999 panic("Operation too long");
1001 if(req
->op
== UBD_READ
) {
1002 for(i
= 0; i
< req
->length
>> 9; i
++){
1003 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1004 ubd_set_bit(i
, (unsigned char *)
1008 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1009 &req
->cow_offset
, bitmap
, bitmap_offset
,
1010 req
->bitmap_words
, bitmap_len
);
1013 static int mmap_fd(struct request
*req
, struct ubd
*dev
, __u64 offset
)
1016 unsigned char *bitmap
;
1019 /* mmap must have been requested on the command line */
1023 /* The buffer must be page aligned */
1024 if(((unsigned long) req
->buffer
% UBD_MMAP_BLOCK_SIZE
) != 0)
1027 /* The request must be a page long */
1028 if((req
->current_nr_sectors
<< 9) != PAGE_SIZE
)
1031 if(dev
->cow
.file
== NULL
)
1034 sector
= offset
>> 9;
1035 bitmap
= (unsigned char *) dev
->cow
.bitmap
;
1036 bit
= ubd_test_bit(sector
, bitmap
);
1038 for(i
= 1; i
< req
->current_nr_sectors
; i
++){
1039 if(ubd_test_bit(sector
+ i
, bitmap
) != bit
)
1043 if(bit
|| (rq_data_dir(req
) == WRITE
))
1044 offset
+= dev
->cow
.data_offset
;
1046 /* The data on disk must be page aligned */
1047 if((offset
% UBD_MMAP_BLOCK_SIZE
) != 0)
1050 return(bit
? dev
->fd
: dev
->cow
.fd
);
1053 static int prepare_mmap_request(struct ubd
*dev
, int fd
, __u64 offset
,
1054 struct request
*req
,
1055 struct io_thread_req
*io_req
)
1059 if(rq_data_dir(req
) == WRITE
){
1060 /* Writes are almost no-ops since the new data is already in the
1064 if(dev
->cow
.file
!= NULL
)
1065 cowify_bitmap(io_req
->offset
, io_req
->length
,
1066 &io_req
->sector_mask
, &io_req
->cow_offset
,
1067 dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1068 io_req
->bitmap_words
,
1069 dev
->cow
.bitmap_len
);
1074 if((dev
->cow
.file
!= NULL
) && (fd
== dev
->cow
.fd
))
1076 else w
= dev
->openflags
.w
;
1078 if((dev
->cow
.file
!= NULL
) && (fd
== dev
->fd
))
1079 offset
+= dev
->cow
.data_offset
;
1081 err
= physmem_subst_mapping(req
->buffer
, fd
, offset
, w
);
1083 printk("physmem_subst_mapping failed, err = %d\n",
1089 io_req
->op
= UBD_MMAP
;
1090 io_req
->buffer
= req
->buffer
;
1094 /* Called with ubd_io_lock held */
1095 static int prepare_request(struct request
*req
, struct io_thread_req
*io_req
)
1097 struct gendisk
*disk
= req
->rq_disk
;
1098 struct ubd
*dev
= disk
->private_data
;
1102 if(req
->rq_status
== RQ_INACTIVE
) return(1);
1104 /* This should be impossible now */
1105 if((rq_data_dir(req
) == WRITE
) && !dev
->openflags
.w
){
1106 printk("Write attempted on readonly ubd device %s\n",
1108 end_request(req
, 0);
1112 offset
= ((__u64
) req
->sector
) << 9;
1113 len
= req
->current_nr_sectors
<< 9;
1115 io_req
->fds
[0] = (dev
->cow
.file
!= NULL
) ? dev
->cow
.fd
: dev
->fd
;
1116 io_req
->fds
[1] = dev
->fd
;
1117 io_req
->map_fd
= -1;
1118 io_req
->cow_offset
= -1;
1119 io_req
->offset
= offset
;
1120 io_req
->length
= len
;
1122 io_req
->sector_mask
= 0;
1124 fd
= mmap_fd(req
, dev
, io_req
->offset
);
1126 /* If mmapping is otherwise OK, but the first access to the
1127 * page is a write, then it's not mapped in yet. So we have
1128 * to write the data to disk first, then we can map the disk
1129 * page in and continue normally from there.
1131 if((rq_data_dir(req
) == WRITE
) && !is_remapped(req
->buffer
)){
1132 io_req
->map_fd
= dev
->fd
;
1133 io_req
->map_offset
= io_req
->offset
+
1134 dev
->cow
.data_offset
;
1137 else return(prepare_mmap_request(dev
, fd
, io_req
->offset
, req
,
1141 if(rq_data_dir(req
) == READ
)
1143 else dev
->nomap_writes
++;
1145 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1146 io_req
->offsets
[0] = 0;
1147 io_req
->offsets
[1] = dev
->cow
.data_offset
;
1148 io_req
->buffer
= req
->buffer
;
1149 io_req
->sectorsize
= 1 << 9;
1151 if(dev
->cow
.file
!= NULL
)
1152 cowify_req(io_req
, dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1153 dev
->cow
.bitmap_len
);
1158 /* Called with ubd_io_lock held */
1159 static void do_ubd_request(request_queue_t
*q
)
1161 struct io_thread_req io_req
;
1162 struct request
*req
;
1165 if(thread_fd
== -1){
1166 while((req
= elv_next_request(q
)) != NULL
){
1167 err
= prepare_request(req
, &io_req
);
1170 __ubd_finish(req
, io_req
.error
);
1175 if(do_ubd
|| (req
= elv_next_request(q
)) == NULL
)
1177 err
= prepare_request(req
, &io_req
);
1179 do_ubd
= ubd_handler
;
1180 n
= os_write_file(thread_fd
, (char *) &io_req
,
1182 if(n
!= sizeof(io_req
))
1183 printk("write to io thread failed, "
1184 "errno = %d\n", -n
);
1189 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1190 unsigned int cmd
, unsigned long arg
)
1192 struct hd_geometry __user
*loc
= (struct hd_geometry __user
*) arg
;
1193 struct ubd
*dev
= inode
->i_bdev
->bd_disk
->private_data
;
1194 struct hd_driveid ubd_id
= {
1201 struct hd_geometry g
;
1202 struct cdrom_volctrl volume
;
1204 if(!loc
) return(-EINVAL
);
1207 g
.cylinders
= dev
->size
/ (128 * 32 * 512);
1208 g
.start
= get_start_sect(inode
->i_bdev
);
1209 return(copy_to_user(loc
, &g
, sizeof(g
)) ? -EFAULT
: 0);
1211 case HDIO_GET_IDENTITY
:
1212 ubd_id
.cyls
= dev
->size
/ (128 * 32 * 512);
1213 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1219 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1221 volume
.channel0
= 255;
1222 volume
.channel1
= 255;
1223 volume
.channel2
= 255;
1224 volume
.channel3
= 255;
1225 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1232 static int ubd_check_remapped(int fd
, unsigned long address
, int is_write
,
1235 __u64 bitmap_offset
;
1236 unsigned long new_bitmap
[2];
1239 /* If it's not a write access, we can't do anything about it */
1243 /* We have a write */
1244 for(i
= 0; i
< sizeof(ubd_dev
) / sizeof(ubd_dev
[0]); i
++){
1245 struct ubd
*dev
= &ubd_dev
[i
];
1247 if((dev
->fd
!= fd
) && (dev
->cow
.fd
!= fd
))
1250 /* It's a write to a ubd device */
1252 /* This should be impossible now */
1253 if(!dev
->openflags
.w
){
1254 /* It's a write access on a read-only device - probably
1255 * shouldn't happen. If the kernel is trying to change
1256 * something with no intention of writing it back out,
1257 * then this message will clue us in that this needs
1260 printk("Write access to mapped page from readonly ubd "
1265 /* It's a write to a writeable ubd device - it must be COWed
1266 * because, otherwise, the page would have been mapped in
1271 panic("Write fault on writeable non-COW ubd device %d",
1274 /* It should also be an access to the backing file since the
1275 * COW pages should be mapped in read-write
1279 panic("Write fault on a backing page of ubd "
1282 /* So, we do the write, copying the backing data to the COW
1286 err
= os_seek_file(dev
->fd
, offset
+ dev
->cow
.data_offset
);
1288 panic("Couldn't seek to %lld in COW file of ubd "
1289 "device %d, err = %d",
1290 offset
+ dev
->cow
.data_offset
, i
, -err
);
1292 n
= os_write_file(dev
->fd
, (void *) address
, PAGE_SIZE
);
1294 panic("Couldn't copy data to COW file of ubd "
1295 "device %d, err = %d", i
, -n
);
1297 /* ... updating the COW bitmap... */
1299 cowify_bitmap(offset
, PAGE_SIZE
, NULL
, &bitmap_offset
,
1300 dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1301 new_bitmap
, dev
->cow
.bitmap_len
);
1303 err
= os_seek_file(dev
->fd
, bitmap_offset
);
1305 panic("Couldn't seek to %lld in COW file of ubd "
1306 "device %d, err = %d", bitmap_offset
, i
, -err
);
1308 n
= os_write_file(dev
->fd
, new_bitmap
, sizeof(new_bitmap
));
1309 if(n
!= sizeof(new_bitmap
))
1310 panic("Couldn't update bitmap of ubd device %d, "
1313 /* Maybe we can map the COW page in, and maybe we can't. If
1314 * it is a pre-V3 COW file, we can't, since the alignment will
1315 * be wrong. If it is a V3 or later COW file which has been
1316 * moved to a system with a larger page size, then maybe we
1317 * can't, depending on the exact location of the page.
1320 offset
+= dev
->cow
.data_offset
;
1322 /* Remove the remapping, putting the original anonymous page
1323 * back. If the COW file can be mapped in, that is done.
1324 * Otherwise, the COW page is read in.
1327 if(!physmem_remove_mapping((void *) address
))
1328 panic("Address 0x%lx not remapped by ubd device %d",
1330 if((offset
% UBD_MMAP_BLOCK_SIZE
) == 0)
1331 physmem_subst_mapping((void *) address
, dev
->fd
,
1334 err
= os_seek_file(dev
->fd
, offset
);
1336 panic("Couldn't seek to %lld in COW file of "
1337 "ubd device %d, err = %d", offset
, i
,
1340 n
= os_read_file(dev
->fd
, (void *) address
, PAGE_SIZE
);
1342 panic("Failed to read page from offset %llx of "
1343 "COW file of ubd device %d, err = %d",
1350 /* It's not a write on a ubd device */
1354 static struct remapper ubd_remapper
= {
1355 .list
= LIST_HEAD_INIT(ubd_remapper
.list
),
1356 .proc
= ubd_check_remapped
,
1359 static int ubd_remapper_setup(void)
1362 register_remapper(&ubd_remapper
);
1367 __initcall(ubd_remapper_setup
);
1369 static int same_backing_files(char *from_cmdline
, char *from_cow
, char *cow
)
1371 struct uml_stat buf1
, buf2
;
1374 if(from_cmdline
== NULL
) return(1);
1375 if(!strcmp(from_cmdline
, from_cow
)) return(1);
1377 err
= os_stat_file(from_cmdline
, &buf1
);
1379 printk("Couldn't stat '%s', err = %d\n", from_cmdline
, -err
);
1382 err
= os_stat_file(from_cow
, &buf2
);
1384 printk("Couldn't stat '%s', err = %d\n", from_cow
, -err
);
1387 if((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
1390 printk("Backing file mismatch - \"%s\" requested,\n"
1391 "\"%s\" specified in COW header of \"%s\"\n",
1392 from_cmdline
, from_cow
, cow
);
1396 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
1398 unsigned long modtime
;
1402 err
= os_file_modtime(file
, &modtime
);
1404 printk("Failed to get modification time of backing file "
1405 "\"%s\", err = %d\n", file
, -err
);
1409 err
= os_file_size(file
, &actual
);
1411 printk("Failed to get size of backing file \"%s\", "
1412 "err = %d\n", file
, -err
);
1417 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1419 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1420 "file\n", (unsigned long long) size
, actual
);
1423 if(modtime
!= mtime
){
1424 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1425 "file\n", mtime
, modtime
);
1431 int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
1435 err
= os_seek_file(fd
, offset
);
1439 err
= os_read_file(fd
, buf
, len
);
1446 int open_ubd_file(char *file
, struct openflags
*openflags
,
1447 char **backing_file_out
, int *bitmap_offset_out
,
1448 unsigned long *bitmap_len_out
, int *data_offset_out
,
1449 int *create_cow_out
)
1452 unsigned long long size
;
1453 __u32 version
, align
;
1455 int fd
, err
, sectorsize
, same
, mode
= 0644;
1457 fd
= os_open_file(file
, *openflags
, mode
);
1459 if((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
1460 *create_cow_out
= 1;
1462 ((fd
!= -EROFS
) && (fd
!= -EACCES
))) return(fd
);
1464 fd
= os_open_file(file
, *openflags
, mode
);
1469 err
= os_lock_file(fd
, openflags
->w
);
1471 printk("Failed to lock '%s', err = %d\n", file
, -err
);
1475 if(backing_file_out
== NULL
) return(fd
);
1477 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
1478 &size
, §orsize
, &align
, bitmap_offset_out
);
1479 if(err
&& (*backing_file_out
!= NULL
)){
1480 printk("Failed to read COW header from COW file \"%s\", "
1481 "errno = %d\n", file
, -err
);
1486 if(backing_file_out
== NULL
) return(fd
);
1488 same
= same_backing_files(*backing_file_out
, backing_file
, file
);
1490 if(!same
&& !backing_file_mismatch(*backing_file_out
, size
, mtime
)){
1491 printk("Switching backing file to '%s'\n", *backing_file_out
);
1492 err
= write_cow_header(file
, fd
, *backing_file_out
,
1493 sectorsize
, align
, &size
);
1495 printk("Switch failed, errno = %d\n", -err
);
1500 *backing_file_out
= backing_file
;
1501 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
1502 if(err
) goto out_close
;
1505 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
1506 bitmap_len_out
, data_offset_out
);
1514 int create_cow_file(char *cow_file
, char *backing_file
, struct openflags flags
,
1515 int sectorsize
, int alignment
, int *bitmap_offset_out
,
1516 unsigned long *bitmap_len_out
, int *data_offset_out
)
1521 fd
= open_ubd_file(cow_file
, &flags
, NULL
, NULL
, NULL
, NULL
, NULL
);
1524 printk("Open of COW file '%s' failed, errno = %d\n", cow_file
,
1529 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
1530 bitmap_offset_out
, bitmap_len_out
,
1539 static int update_bitmap(struct io_thread_req
*req
)
1543 if(req
->cow_offset
== -1)
1546 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1548 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1552 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1553 sizeof(req
->bitmap_words
));
1554 if(n
!= sizeof(req
->bitmap_words
)){
1555 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1563 void do_io(struct io_thread_req
*req
)
1567 int n
, nsectors
, start
, end
, bit
;
1571 if(req
->op
== UBD_MMAP
){
1572 /* Touch the page to force the host to do any necessary IO to
1573 * get it into memory
1575 n
= *((volatile int *) req
->buffer
);
1576 req
->error
= update_bitmap(req
);
1580 nsectors
= req
->length
/ req
->sectorsize
;
1583 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1585 while((end
< nsectors
) &&
1586 (ubd_test_bit(end
, (unsigned char *)
1587 &req
->sector_mask
) == bit
))
1590 off
= req
->offset
+ req
->offsets
[bit
] +
1591 start
* req
->sectorsize
;
1592 len
= (end
- start
) * req
->sectorsize
;
1593 buf
= &req
->buffer
[start
* req
->sectorsize
];
1595 err
= os_seek_file(req
->fds
[bit
], off
);
1597 printk("do_io - lseek failed : err = %d\n", -err
);
1601 if(req
->op
== UBD_READ
){
1606 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1608 printk("do_io - read failed, err = %d "
1609 "fd = %d\n", -n
, req
->fds
[bit
]);
1613 } while((n
< len
) && (n
!= 0));
1614 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1616 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1618 printk("do_io - write failed err = %d "
1619 "fd = %d\n", -n
, req
->fds
[bit
]);
1626 } while(start
< nsectors
);
1628 req
->error
= update_bitmap(req
);
1631 /* Changed in start_io_thread, which is serialized by being called only
1632 * from ubd_init, which is an initcall.
1636 /* Only changed by the io thread */
1639 int io_thread(void *arg
)
1641 struct io_thread_req req
;
1644 ignore_sigwinch_sig();
1646 n
= os_read_file(kernel_fd
, &req
, sizeof(req
));
1647 if(n
!= sizeof(req
)){
1649 printk("io_thread - read failed, fd = %d, "
1650 "err = %d\n", kernel_fd
, -n
);
1652 printk("io_thread - short read, fd = %d, "
1653 "length = %d\n", kernel_fd
, n
);
1659 n
= os_write_file(kernel_fd
, &req
, sizeof(req
));
1660 if(n
!= sizeof(req
))
1661 printk("io_thread - write failed, fd = %d, err = %d\n",
1667 * Overrides for Emacs so that we follow Linus's tabbing style.
1668 * Emacs will notice this stuff at the end of the file and automatically
1669 * adjust the settings for this buffer only. This must remain at the end
1671 * ---------------------------------------------------------------------------
1673 * c-file-style: "linux"