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
;
161 unsigned long *bitmap
;
162 unsigned long bitmap_len
;
172 struct openflags boot_openflags
;
173 struct openflags openflags
;
176 struct platform_device pdev
;
185 #define DEFAULT_COW { \
189 .bitmap_offset = 0, \
193 #define DEFAULT_UBD { \
198 .boot_openflags = OPEN_FLAGS, \
199 .openflags = OPEN_FLAGS, \
201 .cow = DEFAULT_COW, \
209 struct ubd ubd_dev
[MAX_DEV
] = { [ 0 ... MAX_DEV
- 1 ] = DEFAULT_UBD
};
211 static int ubd0_init(void)
213 struct ubd
*dev
= &ubd_dev
[0];
215 if(dev
->file
== NULL
)
216 dev
->file
= "root_fs";
220 __initcall(ubd0_init
);
222 /* Only changed by fake_ide_setup which is a setup */
223 static int fake_ide
= 0;
224 static struct proc_dir_entry
*proc_ide_root
= NULL
;
225 static struct proc_dir_entry
*proc_ide
= NULL
;
227 static void make_proc_ide(void)
229 proc_ide_root
= proc_mkdir("ide", NULL
);
230 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
233 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
234 int *eof
, void *data
)
238 strcpy(page
, "disk\n");
239 len
= strlen("disk\n");
243 if (len
<= 0) return 0;
250 static void make_ide_entries(char *dev_name
)
252 struct proc_dir_entry
*dir
, *ent
;
255 if(proc_ide_root
== NULL
) make_proc_ide();
257 dir
= proc_mkdir(dev_name
, proc_ide
);
260 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
264 ent
->read_proc
= proc_ide_read_media
;
265 ent
->write_proc
= NULL
;
266 sprintf(name
,"ide0/%s", dev_name
);
267 proc_symlink(dev_name
, proc_ide_root
, name
);
270 static int fake_ide_setup(char *str
)
276 __setup("fake_ide", fake_ide_setup
);
278 __uml_help(fake_ide_setup
,
280 " Create ide0 entries that map onto ubd devices.\n\n"
283 static int parse_unit(char **ptr
)
285 char *str
= *ptr
, *end
;
289 n
= simple_strtoul(str
, &end
, 0);
294 else if (('a' <= *str
) && (*str
<= 'h')) {
302 static int ubd_setup_common(char *str
, int *index_out
)
305 struct openflags flags
= global_openflags
;
309 if(index_out
) *index_out
= -1;
316 if(!strcmp(str
, "mmap")){
317 CHOOSE_MODE(printk("mmap not supported by the ubd "
318 "driver in tt mode\n"),
323 if(!strcmp(str
, "sync")){
324 global_openflags
= of_sync(global_openflags
);
327 major
= simple_strtoul(str
, &end
, 0);
328 if((*end
!= '\0') || (end
== str
)){
330 "ubd_setup : didn't parse major number\n");
335 spin_lock(&ubd_lock
);
336 if(fake_major
!= MAJOR_NR
){
337 printk(KERN_ERR
"Can't assign a fake major twice\n");
343 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
347 spin_unlock(&ubd_lock
);
351 n
= parse_unit(&str
);
353 printk(KERN_ERR
"ubd_setup : couldn't parse unit number "
358 printk(KERN_ERR
"ubd_setup : index %d out of range "
359 "(%d devices, from 0 to %d)\n", n
, MAX_DEV
, MAX_DEV
- 1);
364 spin_lock(&ubd_lock
);
367 if(dev
->file
!= NULL
){
368 printk(KERN_ERR
"ubd_setup : device already configured\n");
375 for (i
= 0; i
< 4; i
++) {
390 printk(KERN_ERR
"ubd_setup : Expected '=' or flag letter (r,s or d)\n");
397 printk(KERN_ERR
"ubd_setup : Too many flags specified\n");
399 printk(KERN_ERR
"ubd_setup : Expected '='\n");
404 backing_file
= strchr(str
, ',');
407 backing_file
= strchr(str
, ':');
412 printk(KERN_ERR
"Can't specify both 'd' and a "
415 *backing_file
= '\0';
420 dev
->cow
.file
= backing_file
;
421 dev
->boot_openflags
= flags
;
423 spin_unlock(&ubd_lock
);
427 static int ubd_setup(char *str
)
429 ubd_setup_common(str
, NULL
);
433 __setup("ubd", ubd_setup
);
434 __uml_help(ubd_setup
,
435 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
436 " This is used to associate a device with a file in the underlying\n"
437 " filesystem. When specifying two filenames, the first one is the\n"
438 " COW name and the second is the backing file name. As separator you can\n"
439 " use either a ':' or a ',': the first one allows writing things like;\n"
440 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
441 " while with a ',' the shell would not expand the 2nd '~'.\n"
442 " When using only one filename, UML will detect whether to thread it like\n"
443 " a COW file or a backing file. To override this detection, add the 'd'\n"
445 " ubd0d=BackingFile\n"
446 " Usually, there is a filesystem in the file, but \n"
447 " that's not required. Swap devices containing swap files can be\n"
448 " specified like this. Also, a file which doesn't contain a\n"
449 " filesystem can have its contents read in the virtual \n"
450 " machine by running 'dd' on the device. <n> must be in the range\n"
451 " 0 to 7. Appending an 'r' to the number will cause that device\n"
452 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
453 " an 's' will cause data to be written to disk on the host immediately.\n\n"
456 static int udb_setup(char *str
)
458 printk("udb%s specified on command line is almost certainly a ubd -> "
463 __setup("udb", udb_setup
);
464 __uml_help(udb_setup
,
466 " This option is here solely to catch ubd -> udb typos, which can be\n\n"
467 " to impossible to catch visually unless you specifically look for\n\n"
468 " them. The only result of any option starting with 'udb' is an error\n\n"
469 " in the boot output.\n\n"
472 static int fakehd_set
= 0;
473 static int fakehd(char *str
)
475 printk(KERN_INFO
"fakehd : Changing ubd name to \"hd\".\n");
480 __setup("fakehd", fakehd
);
483 " Change the ubd device name to \"hd\".\n\n"
486 static void do_ubd_request(request_queue_t
* q
);
488 /* Only changed by ubd_init, which is an initcall. */
491 /* Changed by ubd_handler, which is serialized because interrupts only
496 /* call ubd_finish if you need to serialize */
497 static void __ubd_finish(struct request
*req
, int error
)
505 nsect
= req
->current_nr_sectors
;
506 req
->sector
+= nsect
;
507 req
->buffer
+= nsect
<< 9;
509 req
->nr_sectors
-= nsect
;
510 req
->current_nr_sectors
= 0;
514 static inline void ubd_finish(struct request
*req
, int error
)
516 spin_lock(&ubd_io_lock
);
517 __ubd_finish(req
, error
);
518 spin_unlock(&ubd_io_lock
);
521 /* Called without ubd_io_lock held */
522 static void ubd_handler(void)
524 struct io_thread_req req
;
525 struct request
*rq
= elv_next_request(ubd_queue
);
530 n
= os_read_file(thread_fd
, &req
, sizeof(req
));
531 if(n
!= sizeof(req
)){
532 printk(KERN_ERR
"Pid %d - spurious interrupt in ubd_handler, "
533 "err = %d\n", os_getpid(), -n
);
534 spin_lock(&ubd_io_lock
);
536 spin_unlock(&ubd_io_lock
);
540 if((req
.op
!= UBD_MMAP
) &&
541 ((req
.offset
!= ((__u64
) (rq
->sector
)) << 9) ||
542 (req
.length
!= (rq
->current_nr_sectors
) << 9)))
543 panic("I/O op mismatch");
545 if(req
.map_fd
!= -1){
546 err
= physmem_subst_mapping(req
.buffer
, req
.map_fd
,
549 printk("ubd_handler - physmem_subst_mapping failed, "
553 ubd_finish(rq
, req
.error
);
554 reactivate_fd(thread_fd
, UBD_IRQ
);
555 do_ubd_request(ubd_queue
);
558 static irqreturn_t
ubd_intr(int irq
, void *dev
, struct pt_regs
*unused
)
564 /* Only changed by ubd_init, which is an initcall. */
565 static int io_pid
= -1;
567 void kill_io_thread(void)
570 os_kill_process(io_pid
, 1);
573 __uml_exitcall(kill_io_thread
);
575 static int ubd_file_size(struct ubd
*dev
, __u64
*size_out
)
579 file
= dev
->cow
.file
? dev
->cow
.file
: dev
->file
;
580 return(os_file_size(file
, size_out
));
583 static void ubd_close(struct ubd
*dev
)
586 physmem_forget_descriptor(dev
->fd
);
587 os_close_file(dev
->fd
);
588 if(dev
->cow
.file
== NULL
)
592 physmem_forget_descriptor(dev
->cow
.fd
);
593 os_close_file(dev
->cow
.fd
);
594 vfree(dev
->cow
.bitmap
);
595 dev
->cow
.bitmap
= NULL
;
598 static int ubd_open_dev(struct ubd
*dev
)
600 struct openflags flags
;
602 int err
, create_cow
, *create_ptr
;
604 dev
->openflags
= dev
->boot_openflags
;
606 create_ptr
= (dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
607 back_ptr
= dev
->no_cow
? NULL
: &dev
->cow
.file
;
608 dev
->fd
= open_ubd_file(dev
->file
, &dev
->openflags
, back_ptr
,
609 &dev
->cow
.bitmap_offset
, &dev
->cow
.bitmap_len
,
610 &dev
->cow
.data_offset
, create_ptr
);
612 if((dev
->fd
== -ENOENT
) && create_cow
){
613 dev
->fd
= create_cow_file(dev
->file
, dev
->cow
.file
,
614 dev
->openflags
, 1 << 9, PAGE_SIZE
,
615 &dev
->cow
.bitmap_offset
,
616 &dev
->cow
.bitmap_len
,
617 &dev
->cow
.data_offset
);
619 printk(KERN_INFO
"Creating \"%s\" as COW file for "
620 "\"%s\"\n", dev
->file
, dev
->cow
.file
);
625 printk("Failed to open '%s', errno = %d\n", dev
->file
,
630 if(dev
->cow
.file
!= NULL
){
632 dev
->cow
.bitmap
= (void *) vmalloc(dev
->cow
.bitmap_len
);
633 if(dev
->cow
.bitmap
== NULL
){
634 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
637 flush_tlb_kernel_vm();
639 err
= read_cow_bitmap(dev
->fd
, dev
->cow
.bitmap
,
640 dev
->cow
.bitmap_offset
,
641 dev
->cow
.bitmap_len
);
645 flags
= dev
->openflags
;
647 err
= open_ubd_file(dev
->cow
.file
, &flags
, NULL
, NULL
, NULL
,
649 if(err
< 0) goto error
;
654 os_close_file(dev
->fd
);
658 static int ubd_new_disk(int major
, u64 size
, int unit
,
659 struct gendisk
**disk_out
)
662 struct gendisk
*disk
;
663 char from
[sizeof("ubd/nnnnn\0")], to
[sizeof("discnnnnn/disc\0")];
666 disk
= alloc_disk(1 << UBD_SHIFT
);
671 disk
->first_minor
= unit
<< UBD_SHIFT
;
672 disk
->fops
= &ubd_blops
;
673 set_capacity(disk
, size
/ 512);
674 if(major
== MAJOR_NR
){
675 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
676 sprintf(disk
->devfs_name
, "ubd/disc%d", unit
);
677 sprintf(from
, "ubd/%d", unit
);
678 sprintf(to
, "disc%d/disc", unit
);
679 err
= devfs_mk_symlink(from
, to
);
681 printk("ubd_new_disk failed to make link from %s to "
682 "%s, error = %d\n", from
, to
, err
);
685 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
686 sprintf(disk
->devfs_name
, "ubd_fake/disc%d", unit
);
689 /* sysfs register (not for ide fake devices) */
690 if (major
== MAJOR_NR
) {
691 ubd_dev
[unit
].pdev
.id
= unit
;
692 ubd_dev
[unit
].pdev
.name
= DRIVER_NAME
;
693 platform_device_register(&ubd_dev
[unit
].pdev
);
694 disk
->driverfs_dev
= &ubd_dev
[unit
].pdev
.dev
;
697 disk
->private_data
= &ubd_dev
[unit
];
698 disk
->queue
= ubd_queue
;
705 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
707 static int ubd_add(int n
)
709 struct ubd
*dev
= &ubd_dev
[n
];
712 if(dev
->file
== NULL
)
715 if (ubd_open_dev(dev
))
718 err
= ubd_file_size(dev
, &dev
->size
);
722 dev
->size
= ROUND_BLOCK(dev
->size
);
724 err
= ubd_new_disk(MAJOR_NR
, dev
->size
, n
, &ubd_gendisk
[n
]);
728 if(fake_major
!= MAJOR_NR
)
729 ubd_new_disk(fake_major
, dev
->size
, n
,
732 /* perhaps this should also be under the "if (fake_major)" above */
733 /* using the fake_disk->disk_name and also the fakehd_set name */
735 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
741 static int ubd_config(char *str
)
745 str
= uml_strdup(str
);
747 printk(KERN_ERR
"ubd_config failed to strdup string\n");
750 err
= ubd_setup_common(str
, &n
);
755 if(n
== -1) return(0);
757 spin_lock(&ubd_lock
);
760 ubd_dev
[n
].file
= NULL
;
761 spin_unlock(&ubd_lock
);
766 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
771 n
= parse_unit(&name
);
772 if((n
>= MAX_DEV
) || (n
< 0)){
773 *error_out
= "ubd_get_config : device number out of range";
778 spin_lock(&ubd_lock
);
780 if(dev
->file
== NULL
){
781 CONFIG_CHUNK(str
, size
, len
, "", 1);
785 CONFIG_CHUNK(str
, size
, len
, dev
->file
, 0);
787 if(dev
->cow
.file
!= NULL
){
788 CONFIG_CHUNK(str
, size
, len
, ",", 0);
789 CONFIG_CHUNK(str
, size
, len
, dev
->cow
.file
, 1);
791 else CONFIG_CHUNK(str
, size
, len
, "", 1);
794 spin_unlock(&ubd_lock
);
798 static int ubd_remove(char *str
)
801 int n
, err
= -ENODEV
;
803 n
= parse_unit(&str
);
805 if((n
< 0) || (n
>= MAX_DEV
))
810 return(-EBUSY
); /* you cannot remove a open disk */
813 spin_lock(&ubd_lock
);
815 if(ubd_gendisk
[n
] == NULL
)
818 del_gendisk(ubd_gendisk
[n
]);
819 put_disk(ubd_gendisk
[n
]);
820 ubd_gendisk
[n
] = NULL
;
822 if(fake_gendisk
[n
] != NULL
){
823 del_gendisk(fake_gendisk
[n
]);
824 put_disk(fake_gendisk
[n
]);
825 fake_gendisk
[n
] = NULL
;
828 platform_device_unregister(&dev
->pdev
);
829 *dev
= ((struct ubd
) DEFAULT_UBD
);
832 spin_unlock(&ubd_lock
);
836 static struct mc_device ubd_mc
= {
838 .config
= ubd_config
,
839 .get_config
= ubd_get_config
,
840 .remove
= ubd_remove
,
843 static int ubd_mc_init(void)
845 mconsole_register_dev(&ubd_mc
);
849 __initcall(ubd_mc_init
);
851 static struct device_driver ubd_driver
= {
853 .bus
= &platform_bus_type
,
861 if (register_blkdev(MAJOR_NR
, "ubd"))
864 ubd_queue
= blk_init_queue(do_ubd_request
, &ubd_io_lock
);
866 unregister_blkdev(MAJOR_NR
, "ubd");
870 if (fake_major
!= MAJOR_NR
) {
871 char name
[sizeof("ubd_nnn\0")];
873 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
875 if (register_blkdev(fake_major
, "ubd"))
878 driver_register(&ubd_driver
);
879 for (i
= 0; i
< MAX_DEV
; i
++)
884 late_initcall(ubd_init
);
886 int ubd_driver_init(void){
890 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
891 if(global_openflags
.s
){
892 printk(KERN_INFO
"ubd: Synchronous mode\n");
893 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
894 * enough. So use anyway the io thread. */
896 stack
= alloc_stack(0, 0);
897 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
901 "ubd : Failed to start I/O thread (errno = %d) - "
902 "falling back to synchronous I/O\n", -io_pid
);
906 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
907 SA_INTERRUPT
, "ubd", ubd_dev
);
909 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
913 device_initcall(ubd_driver_init
);
915 static int ubd_open(struct inode
*inode
, struct file
*filp
)
917 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
918 struct ubd
*dev
= disk
->private_data
;
922 err
= ubd_open_dev(dev
);
924 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
925 disk
->disk_name
, dev
->file
, -err
);
930 if((filp
->f_mode
& FMODE_WRITE
) && !dev
->openflags
.w
){
931 if(--dev
->count
== 0) ubd_close(dev
);
938 static int ubd_release(struct inode
* inode
, struct file
* file
)
940 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
941 struct ubd
*dev
= disk
->private_data
;
943 if(--dev
->count
== 0)
948 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
949 __u64
*cow_offset
, unsigned long *bitmap
,
950 __u64 bitmap_offset
, unsigned long *bitmap_words
,
953 __u64 sector
= io_offset
>> 9;
954 int i
, update_bitmap
= 0;
956 for(i
= 0; i
< length
>> 9; i
++){
958 ubd_set_bit(i
, (unsigned char *) cow_mask
);
959 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
963 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
969 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
971 /* This takes care of the case where we're exactly at the end of the
972 * device, and *cow_offset + 1 is off the end. So, just back it up
973 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
974 * for the original diagnosis.
976 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
977 sizeof(unsigned long) - 1))
980 bitmap_words
[0] = bitmap
[*cow_offset
];
981 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
983 *cow_offset
*= sizeof(unsigned long);
984 *cow_offset
+= bitmap_offset
;
987 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
988 __u64 bitmap_offset
, __u64 bitmap_len
)
990 __u64 sector
= req
->offset
>> 9;
993 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
994 panic("Operation too long");
996 if(req
->op
== UBD_READ
) {
997 for(i
= 0; i
< req
->length
>> 9; i
++){
998 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
999 ubd_set_bit(i
, (unsigned char *)
1003 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1004 &req
->cow_offset
, bitmap
, bitmap_offset
,
1005 req
->bitmap_words
, bitmap_len
);
1008 static int mmap_fd(struct request
*req
, struct ubd
*dev
, __u64 offset
)
1011 unsigned char *bitmap
;
1014 /* mmap must have been requested on the command line */
1018 /* The buffer must be page aligned */
1019 if(((unsigned long) req
->buffer
% UBD_MMAP_BLOCK_SIZE
) != 0)
1022 /* The request must be a page long */
1023 if((req
->current_nr_sectors
<< 9) != PAGE_SIZE
)
1026 if(dev
->cow
.file
== NULL
)
1029 sector
= offset
>> 9;
1030 bitmap
= (unsigned char *) dev
->cow
.bitmap
;
1031 bit
= ubd_test_bit(sector
, bitmap
);
1033 for(i
= 1; i
< req
->current_nr_sectors
; i
++){
1034 if(ubd_test_bit(sector
+ i
, bitmap
) != bit
)
1038 if(bit
|| (rq_data_dir(req
) == WRITE
))
1039 offset
+= dev
->cow
.data_offset
;
1041 /* The data on disk must be page aligned */
1042 if((offset
% UBD_MMAP_BLOCK_SIZE
) != 0)
1045 return(bit
? dev
->fd
: dev
->cow
.fd
);
1048 static int prepare_mmap_request(struct ubd
*dev
, int fd
, __u64 offset
,
1049 struct request
*req
,
1050 struct io_thread_req
*io_req
)
1054 if(rq_data_dir(req
) == WRITE
){
1055 /* Writes are almost no-ops since the new data is already in the
1059 if(dev
->cow
.file
!= NULL
)
1060 cowify_bitmap(io_req
->offset
, io_req
->length
,
1061 &io_req
->sector_mask
, &io_req
->cow_offset
,
1062 dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1063 io_req
->bitmap_words
,
1064 dev
->cow
.bitmap_len
);
1069 if((dev
->cow
.file
!= NULL
) && (fd
== dev
->cow
.fd
))
1071 else w
= dev
->openflags
.w
;
1073 if((dev
->cow
.file
!= NULL
) && (fd
== dev
->fd
))
1074 offset
+= dev
->cow
.data_offset
;
1076 err
= physmem_subst_mapping(req
->buffer
, fd
, offset
, w
);
1078 printk("physmem_subst_mapping failed, err = %d\n",
1084 io_req
->op
= UBD_MMAP
;
1085 io_req
->buffer
= req
->buffer
;
1089 /* Called with ubd_io_lock held */
1090 static int prepare_request(struct request
*req
, struct io_thread_req
*io_req
)
1092 struct gendisk
*disk
= req
->rq_disk
;
1093 struct ubd
*dev
= disk
->private_data
;
1097 if(req
->rq_status
== RQ_INACTIVE
) return(1);
1099 if((rq_data_dir(req
) == WRITE
) && !dev
->openflags
.w
){
1100 printk("Write attempted on readonly ubd device %s\n",
1102 end_request(req
, 0);
1106 offset
= ((__u64
) req
->sector
) << 9;
1107 len
= req
->current_nr_sectors
<< 9;
1109 io_req
->fds
[0] = (dev
->cow
.file
!= NULL
) ? dev
->cow
.fd
: dev
->fd
;
1110 io_req
->fds
[1] = dev
->fd
;
1111 io_req
->map_fd
= -1;
1112 io_req
->cow_offset
= -1;
1113 io_req
->offset
= offset
;
1114 io_req
->length
= len
;
1116 io_req
->sector_mask
= 0;
1118 fd
= mmap_fd(req
, dev
, io_req
->offset
);
1120 /* If mmapping is otherwise OK, but the first access to the
1121 * page is a write, then it's not mapped in yet. So we have
1122 * to write the data to disk first, then we can map the disk
1123 * page in and continue normally from there.
1125 if((rq_data_dir(req
) == WRITE
) && !is_remapped(req
->buffer
)){
1126 io_req
->map_fd
= dev
->fd
;
1127 io_req
->map_offset
= io_req
->offset
+
1128 dev
->cow
.data_offset
;
1131 else return(prepare_mmap_request(dev
, fd
, io_req
->offset
, req
,
1135 if(rq_data_dir(req
) == READ
)
1137 else dev
->nomap_writes
++;
1139 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1140 io_req
->offsets
[0] = 0;
1141 io_req
->offsets
[1] = dev
->cow
.data_offset
;
1142 io_req
->buffer
= req
->buffer
;
1143 io_req
->sectorsize
= 1 << 9;
1145 if(dev
->cow
.file
!= NULL
)
1146 cowify_req(io_req
, dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1147 dev
->cow
.bitmap_len
);
1152 /* Called with ubd_io_lock held */
1153 static void do_ubd_request(request_queue_t
*q
)
1155 struct io_thread_req io_req
;
1156 struct request
*req
;
1159 if(thread_fd
== -1){
1160 while((req
= elv_next_request(q
)) != NULL
){
1161 err
= prepare_request(req
, &io_req
);
1164 __ubd_finish(req
, io_req
.error
);
1169 if(do_ubd
|| (req
= elv_next_request(q
)) == NULL
)
1171 err
= prepare_request(req
, &io_req
);
1173 do_ubd
= ubd_handler
;
1174 n
= os_write_file(thread_fd
, (char *) &io_req
,
1176 if(n
!= sizeof(io_req
))
1177 printk("write to io thread failed, "
1178 "errno = %d\n", -n
);
1183 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1184 unsigned int cmd
, unsigned long arg
)
1186 struct hd_geometry __user
*loc
= (struct hd_geometry __user
*) arg
;
1187 struct ubd
*dev
= inode
->i_bdev
->bd_disk
->private_data
;
1188 struct hd_driveid ubd_id
= {
1195 struct hd_geometry g
;
1196 struct cdrom_volctrl volume
;
1198 if(!loc
) return(-EINVAL
);
1201 g
.cylinders
= dev
->size
/ (128 * 32 * 512);
1202 g
.start
= get_start_sect(inode
->i_bdev
);
1203 return(copy_to_user(loc
, &g
, sizeof(g
)) ? -EFAULT
: 0);
1205 case HDIO_GET_IDENTITY
:
1206 ubd_id
.cyls
= dev
->size
/ (128 * 32 * 512);
1207 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1213 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1215 volume
.channel0
= 255;
1216 volume
.channel1
= 255;
1217 volume
.channel2
= 255;
1218 volume
.channel3
= 255;
1219 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1226 static int ubd_check_remapped(int fd
, unsigned long address
, int is_write
,
1229 __u64 bitmap_offset
;
1230 unsigned long new_bitmap
[2];
1233 /* If it's not a write access, we can't do anything about it */
1237 /* We have a write */
1238 for(i
= 0; i
< sizeof(ubd_dev
) / sizeof(ubd_dev
[0]); i
++){
1239 struct ubd
*dev
= &ubd_dev
[i
];
1241 if((dev
->fd
!= fd
) && (dev
->cow
.fd
!= fd
))
1244 /* It's a write to a ubd device */
1246 if(!dev
->openflags
.w
){
1247 /* It's a write access on a read-only device - probably
1248 * shouldn't happen. If the kernel is trying to change
1249 * something with no intention of writing it back out,
1250 * then this message will clue us in that this needs
1253 printk("Write access to mapped page from readonly ubd "
1258 /* It's a write to a writeable ubd device - it must be COWed
1259 * because, otherwise, the page would have been mapped in
1264 panic("Write fault on writeable non-COW ubd device %d",
1267 /* It should also be an access to the backing file since the
1268 * COW pages should be mapped in read-write
1272 panic("Write fault on a backing page of ubd "
1275 /* So, we do the write, copying the backing data to the COW
1279 err
= os_seek_file(dev
->fd
, offset
+ dev
->cow
.data_offset
);
1281 panic("Couldn't seek to %lld in COW file of ubd "
1282 "device %d, err = %d",
1283 offset
+ dev
->cow
.data_offset
, i
, -err
);
1285 n
= os_write_file(dev
->fd
, (void *) address
, PAGE_SIZE
);
1287 panic("Couldn't copy data to COW file of ubd "
1288 "device %d, err = %d", i
, -n
);
1290 /* ... updating the COW bitmap... */
1292 cowify_bitmap(offset
, PAGE_SIZE
, NULL
, &bitmap_offset
,
1293 dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1294 new_bitmap
, dev
->cow
.bitmap_len
);
1296 err
= os_seek_file(dev
->fd
, bitmap_offset
);
1298 panic("Couldn't seek to %lld in COW file of ubd "
1299 "device %d, err = %d", bitmap_offset
, i
, -err
);
1301 n
= os_write_file(dev
->fd
, new_bitmap
, sizeof(new_bitmap
));
1302 if(n
!= sizeof(new_bitmap
))
1303 panic("Couldn't update bitmap of ubd device %d, "
1306 /* Maybe we can map the COW page in, and maybe we can't. If
1307 * it is a pre-V3 COW file, we can't, since the alignment will
1308 * be wrong. If it is a V3 or later COW file which has been
1309 * moved to a system with a larger page size, then maybe we
1310 * can't, depending on the exact location of the page.
1313 offset
+= dev
->cow
.data_offset
;
1315 /* Remove the remapping, putting the original anonymous page
1316 * back. If the COW file can be mapped in, that is done.
1317 * Otherwise, the COW page is read in.
1320 if(!physmem_remove_mapping((void *) address
))
1321 panic("Address 0x%lx not remapped by ubd device %d",
1323 if((offset
% UBD_MMAP_BLOCK_SIZE
) == 0)
1324 physmem_subst_mapping((void *) address
, dev
->fd
,
1327 err
= os_seek_file(dev
->fd
, offset
);
1329 panic("Couldn't seek to %lld in COW file of "
1330 "ubd device %d, err = %d", offset
, i
,
1333 n
= os_read_file(dev
->fd
, (void *) address
, PAGE_SIZE
);
1335 panic("Failed to read page from offset %llx of "
1336 "COW file of ubd device %d, err = %d",
1343 /* It's not a write on a ubd device */
1347 static struct remapper ubd_remapper
= {
1348 .list
= LIST_HEAD_INIT(ubd_remapper
.list
),
1349 .proc
= ubd_check_remapped
,
1352 static int ubd_remapper_setup(void)
1355 register_remapper(&ubd_remapper
);
1360 __initcall(ubd_remapper_setup
);
1362 static int same_backing_files(char *from_cmdline
, char *from_cow
, char *cow
)
1364 struct uml_stat buf1
, buf2
;
1367 if(from_cmdline
== NULL
) return(1);
1368 if(!strcmp(from_cmdline
, from_cow
)) return(1);
1370 err
= os_stat_file(from_cmdline
, &buf1
);
1372 printk("Couldn't stat '%s', err = %d\n", from_cmdline
, -err
);
1375 err
= os_stat_file(from_cow
, &buf2
);
1377 printk("Couldn't stat '%s', err = %d\n", from_cow
, -err
);
1380 if((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
1383 printk("Backing file mismatch - \"%s\" requested,\n"
1384 "\"%s\" specified in COW header of \"%s\"\n",
1385 from_cmdline
, from_cow
, cow
);
1389 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
1391 unsigned long modtime
;
1395 err
= os_file_modtime(file
, &modtime
);
1397 printk("Failed to get modification time of backing file "
1398 "\"%s\", err = %d\n", file
, -err
);
1402 err
= os_file_size(file
, &actual
);
1404 printk("Failed to get size of backing file \"%s\", "
1405 "err = %d\n", file
, -err
);
1410 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1412 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1413 "file\n", (unsigned long long) size
, actual
);
1416 if(modtime
!= mtime
){
1417 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1418 "file\n", mtime
, modtime
);
1424 int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
1428 err
= os_seek_file(fd
, offset
);
1432 err
= os_read_file(fd
, buf
, len
);
1439 int open_ubd_file(char *file
, struct openflags
*openflags
,
1440 char **backing_file_out
, int *bitmap_offset_out
,
1441 unsigned long *bitmap_len_out
, int *data_offset_out
,
1442 int *create_cow_out
)
1445 unsigned long long size
;
1446 __u32 version
, align
;
1448 int fd
, err
, sectorsize
, same
, mode
= 0644;
1450 fd
= os_open_file(file
, *openflags
, mode
);
1452 if((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
1453 *create_cow_out
= 1;
1455 ((fd
!= -EROFS
) && (fd
!= -EACCES
))) return(fd
);
1457 fd
= os_open_file(file
, *openflags
, mode
);
1462 err
= os_lock_file(fd
, openflags
->w
);
1464 printk("Failed to lock '%s', err = %d\n", file
, -err
);
1468 if(backing_file_out
== NULL
) return(fd
);
1470 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
1471 &size
, §orsize
, &align
, bitmap_offset_out
);
1472 if(err
&& (*backing_file_out
!= NULL
)){
1473 printk("Failed to read COW header from COW file \"%s\", "
1474 "errno = %d\n", file
, -err
);
1479 if(backing_file_out
== NULL
) return(fd
);
1481 same
= same_backing_files(*backing_file_out
, backing_file
, file
);
1483 if(!same
&& !backing_file_mismatch(*backing_file_out
, size
, mtime
)){
1484 printk("Switching backing file to '%s'\n", *backing_file_out
);
1485 err
= write_cow_header(file
, fd
, *backing_file_out
,
1486 sectorsize
, align
, &size
);
1488 printk("Switch failed, errno = %d\n", -err
);
1493 *backing_file_out
= backing_file
;
1494 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
1495 if(err
) goto out_close
;
1498 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
1499 bitmap_len_out
, data_offset_out
);
1507 int create_cow_file(char *cow_file
, char *backing_file
, struct openflags flags
,
1508 int sectorsize
, int alignment
, int *bitmap_offset_out
,
1509 unsigned long *bitmap_len_out
, int *data_offset_out
)
1514 fd
= open_ubd_file(cow_file
, &flags
, NULL
, NULL
, NULL
, NULL
, NULL
);
1517 printk("Open of COW file '%s' failed, errno = %d\n", cow_file
,
1522 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
1523 bitmap_offset_out
, bitmap_len_out
,
1532 static int update_bitmap(struct io_thread_req
*req
)
1536 if(req
->cow_offset
== -1)
1539 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1541 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1545 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1546 sizeof(req
->bitmap_words
));
1547 if(n
!= sizeof(req
->bitmap_words
)){
1548 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1556 void do_io(struct io_thread_req
*req
)
1560 int n
, nsectors
, start
, end
, bit
;
1564 if(req
->op
== UBD_MMAP
){
1565 /* Touch the page to force the host to do any necessary IO to
1566 * get it into memory
1568 n
= *((volatile int *) req
->buffer
);
1569 req
->error
= update_bitmap(req
);
1573 nsectors
= req
->length
/ req
->sectorsize
;
1576 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1578 while((end
< nsectors
) &&
1579 (ubd_test_bit(end
, (unsigned char *)
1580 &req
->sector_mask
) == bit
))
1583 off
= req
->offset
+ req
->offsets
[bit
] +
1584 start
* req
->sectorsize
;
1585 len
= (end
- start
) * req
->sectorsize
;
1586 buf
= &req
->buffer
[start
* req
->sectorsize
];
1588 err
= os_seek_file(req
->fds
[bit
], off
);
1590 printk("do_io - lseek failed : err = %d\n", -err
);
1594 if(req
->op
== UBD_READ
){
1599 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1601 printk("do_io - read failed, err = %d "
1602 "fd = %d\n", -n
, req
->fds
[bit
]);
1606 } while((n
< len
) && (n
!= 0));
1607 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1610 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1612 printk("do_io - write failed err = %d "
1613 "fd = %d\n", -n
, req
->fds
[bit
]);
1620 } while(start
< nsectors
);
1622 req
->error
= update_bitmap(req
);
1625 /* Changed in start_io_thread, which is serialized by being called only
1626 * from ubd_init, which is an initcall.
1630 /* Only changed by the io thread */
1633 int io_thread(void *arg
)
1635 struct io_thread_req req
;
1638 ignore_sigwinch_sig();
1640 n
= os_read_file(kernel_fd
, &req
, sizeof(req
));
1641 if(n
!= sizeof(req
)){
1643 printk("io_thread - read failed, fd = %d, "
1644 "err = %d\n", kernel_fd
, -n
);
1646 printk("io_thread - short read, fd = %d, "
1647 "length = %d\n", kernel_fd
, n
);
1653 n
= os_write_file(kernel_fd
, &req
, sizeof(req
));
1654 if(n
!= sizeof(req
))
1655 printk("io_thread - write failed, fd = %d, err = %d\n",
1661 * Overrides for Emacs so that we follow Linus's tabbing style.
1662 * Emacs will notice this stuff at the end of the file and automatically
1663 * adjust the settings for this buffer only. This must remain at the end
1665 * ---------------------------------------------------------------------------
1667 * c-file-style: "linux"