2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 /* 2001-09-28...2002-04-17
7 * Partition stuff by James_McMechan@hotmail.com
8 * old style ubd by setting UBD_SHIFT to 0
9 * 2002-09-27...2002-10-18 massive tinkering for 2.5
10 * partitions have changed in 2.5
11 * 2003-01-29 more tinkering for 2.5.59-1
12 * This should now address the sysfs problems and has
13 * the symlink for devfs to allow for booting with
14 * the common /dev/ubd/discX/... names rather than
15 * only /dev/ubdN/discN this version also has lots of
16 * clean ups preparing for ubd-many.
20 #define MAJOR_NR UBD_MAJOR
23 #include "linux/module.h"
24 #include "linux/blkdev.h"
25 #include "linux/hdreg.h"
26 #include "linux/init.h"
27 #include "linux/cdrom.h"
28 #include "linux/proc_fs.h"
29 #include "linux/ctype.h"
30 #include "linux/capability.h"
32 #include "linux/vmalloc.h"
33 #include "linux/blkpg.h"
34 #include "linux/genhd.h"
35 #include "linux/spinlock.h"
36 #include "linux/platform_device.h"
37 #include "asm/segment.h"
38 #include "asm/uaccess.h"
40 #include "asm/types.h"
41 #include "asm/tlbflush.h"
42 #include "user_util.h"
44 #include "kern_util.h"
46 #include "mconsole_kern.h"
56 enum ubd_req
{ UBD_READ
, UBD_WRITE
};
58 struct io_thread_req
{
61 unsigned long offsets
[2];
62 unsigned long long offset
;
66 unsigned long sector_mask
;
67 unsigned long long cow_offset
;
68 unsigned long bitmap_words
[2];
72 extern int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
73 char **backing_file_out
, int *bitmap_offset_out
,
74 unsigned long *bitmap_len_out
, int *data_offset_out
,
76 extern int create_cow_file(char *cow_file
, char *backing_file
,
77 struct openflags flags
, int sectorsize
,
78 int alignment
, int *bitmap_offset_out
,
79 unsigned long *bitmap_len_out
,
80 int *data_offset_out
);
81 extern int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
);
82 extern void do_io(struct io_thread_req
*req
);
84 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
89 bits
= sizeof(data
[0]) * 8;
92 return((data
[n
] & (1 << off
)) != 0);
95 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
100 bits
= sizeof(data
[0]) * 8;
103 data
[n
] |= (1 << off
);
105 /*End stuff from ubd_user.h*/
107 #define DRIVER_NAME "uml-blkdev"
109 /* Can be taken in interrupt context, and is passed to the block layer to lock
110 * the request queue. Kernel side code knows that. */
111 static DEFINE_SPINLOCK(ubd_io_lock
);
113 static DEFINE_MUTEX(ubd_lock
);
115 /* XXX - this made sense in 2.4 days, now it's only used as a boolean, and
116 * probably it doesn't make sense even for that. */
119 static int ubd_open(struct inode
* inode
, struct file
* filp
);
120 static int ubd_release(struct inode
* inode
, struct file
* file
);
121 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
122 unsigned int cmd
, unsigned long arg
);
123 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
);
127 static struct block_device_operations ubd_blops
= {
128 .owner
= THIS_MODULE
,
130 .release
= ubd_release
,
132 .getgeo
= ubd_getgeo
,
135 /* Protected by the queue_lock */
136 static request_queue_t
*ubd_queue
;
138 /* Protected by ubd_lock */
139 static int fake_major
= MAJOR_NR
;
141 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
142 static struct gendisk
*fake_gendisk
[MAX_DEV
];
144 #ifdef CONFIG_BLK_DEV_UBD_SYNC
145 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
148 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
152 /* Not protected - changed only in ubd_setup_common and then only to
155 static struct openflags global_openflags
= OPEN_FLAGS
;
158 /* backing file name */
160 /* backing file fd */
162 unsigned long *bitmap
;
163 unsigned long bitmap_len
;
169 /* name (and fd, below) of the file opened for writing, either the
170 * backing or the cow file. */
175 struct openflags boot_openflags
;
176 struct openflags openflags
;
180 struct platform_device pdev
;
183 #define DEFAULT_COW { \
187 .bitmap_offset = 0, \
191 #define DEFAULT_UBD { \
196 .boot_openflags = OPEN_FLAGS, \
197 .openflags = OPEN_FLAGS, \
200 .cow = DEFAULT_COW, \
203 struct ubd ubd_devs
[MAX_DEV
] = { [ 0 ... MAX_DEV
- 1 ] = DEFAULT_UBD
};
205 static int ubd0_init(void)
207 struct ubd
*ubd_dev
= &ubd_devs
[0];
209 if(ubd_dev
->file
== NULL
)
210 ubd_dev
->file
= "root_fs";
214 __initcall(ubd0_init
);
216 /* Only changed by fake_ide_setup which is a setup */
217 static int fake_ide
= 0;
218 static struct proc_dir_entry
*proc_ide_root
= NULL
;
219 static struct proc_dir_entry
*proc_ide
= NULL
;
221 static void make_proc_ide(void)
223 proc_ide_root
= proc_mkdir("ide", NULL
);
224 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
227 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
228 int *eof
, void *data
)
232 strcpy(page
, "disk\n");
233 len
= strlen("disk\n");
237 if (len
<= 0) return 0;
244 static void make_ide_entries(char *dev_name
)
246 struct proc_dir_entry
*dir
, *ent
;
249 if(proc_ide_root
== NULL
) make_proc_ide();
251 dir
= proc_mkdir(dev_name
, proc_ide
);
254 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
258 ent
->read_proc
= proc_ide_read_media
;
259 ent
->write_proc
= NULL
;
260 sprintf(name
,"ide0/%s", dev_name
);
261 proc_symlink(dev_name
, proc_ide_root
, name
);
264 static int fake_ide_setup(char *str
)
270 __setup("fake_ide", fake_ide_setup
);
272 __uml_help(fake_ide_setup
,
274 " Create ide0 entries that map onto ubd devices.\n\n"
277 static int parse_unit(char **ptr
)
279 char *str
= *ptr
, *end
;
283 n
= simple_strtoul(str
, &end
, 0);
288 else if (('a' <= *str
) && (*str
<= 'z')) {
296 static int ubd_setup_common(char *str
, int *index_out
)
299 struct openflags flags
= global_openflags
;
303 if(index_out
) *index_out
= -1;
310 if(!strcmp(str
, "sync")){
311 global_openflags
= of_sync(global_openflags
);
314 major
= simple_strtoul(str
, &end
, 0);
315 if((*end
!= '\0') || (end
== str
)){
317 "ubd_setup : didn't parse major number\n");
322 mutex_lock(&ubd_lock
);
323 if(fake_major
!= MAJOR_NR
){
324 printk(KERN_ERR
"Can't assign a fake major twice\n");
330 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
334 mutex_unlock(&ubd_lock
);
338 n
= parse_unit(&str
);
340 printk(KERN_ERR
"ubd_setup : couldn't parse unit number "
345 printk(KERN_ERR
"ubd_setup : index %d out of range "
346 "(%d devices, from 0 to %d)\n", n
, MAX_DEV
, MAX_DEV
- 1);
351 mutex_lock(&ubd_lock
);
353 ubd_dev
= &ubd_devs
[n
];
354 if(ubd_dev
->file
!= NULL
){
355 printk(KERN_ERR
"ubd_setup : device already configured\n");
362 for (i
= 0; i
< sizeof("rscd="); i
++) {
380 printk(KERN_ERR
"ubd_setup : Expected '=' or flag letter (r, s, c, or d)\n");
387 printk(KERN_ERR
"ubd_setup : Too many flags specified\n");
389 printk(KERN_ERR
"ubd_setup : Expected '='\n");
394 backing_file
= strchr(str
, ',');
397 backing_file
= strchr(str
, ':');
402 printk(KERN_ERR
"Can't specify both 'd' and a "
405 *backing_file
= '\0';
410 ubd_dev
->cow
.file
= backing_file
;
411 ubd_dev
->boot_openflags
= flags
;
413 mutex_unlock(&ubd_lock
);
417 static int ubd_setup(char *str
)
419 ubd_setup_common(str
, NULL
);
423 __setup("ubd", ubd_setup
);
424 __uml_help(ubd_setup
,
425 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
426 " This is used to associate a device with a file in the underlying\n"
427 " filesystem. When specifying two filenames, the first one is the\n"
428 " COW name and the second is the backing file name. As separator you can\n"
429 " use either a ':' or a ',': the first one allows writing things like;\n"
430 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
431 " while with a ',' the shell would not expand the 2nd '~'.\n"
432 " When using only one filename, UML will detect whether to thread it like\n"
433 " a COW file or a backing file. To override this detection, add the 'd'\n"
435 " ubd0d=BackingFile\n"
436 " Usually, there is a filesystem in the file, but \n"
437 " that's not required. Swap devices containing swap files can be\n"
438 " specified like this. Also, a file which doesn't contain a\n"
439 " filesystem can have its contents read in the virtual \n"
440 " machine by running 'dd' on the device. <n> must be in the range\n"
441 " 0 to 7. Appending an 'r' to the number will cause that device\n"
442 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
443 " an 's' will cause data to be written to disk on the host immediately.\n\n"
446 static int udb_setup(char *str
)
448 printk("udb%s specified on command line is almost certainly a ubd -> "
453 __setup("udb", udb_setup
);
454 __uml_help(udb_setup
,
456 " This option is here solely to catch ubd -> udb typos, which can be\n"
457 " to impossible to catch visually unless you specifically look for\n"
458 " them. The only result of any option starting with 'udb' is an error\n"
459 " in the boot output.\n\n"
462 static int fakehd_set
= 0;
463 static int fakehd(char *str
)
465 printk(KERN_INFO
"fakehd : Changing ubd name to \"hd\".\n");
470 __setup("fakehd", fakehd
);
473 " Change the ubd device name to \"hd\".\n\n"
476 static void do_ubd_request(request_queue_t
* q
);
478 /* Only changed by ubd_init, which is an initcall. */
481 /* Changed by ubd_handler, which is serialized because interrupts only
486 /* call ubd_finish if you need to serialize */
487 static void __ubd_finish(struct request
*req
, int error
)
495 nsect
= req
->current_nr_sectors
;
496 req
->sector
+= nsect
;
497 req
->buffer
+= nsect
<< 9;
499 req
->nr_sectors
-= nsect
;
500 req
->current_nr_sectors
= 0;
504 /* Callable only from interrupt context - otherwise you need to do
505 * spin_lock_irq()/spin_lock_irqsave() */
506 static inline void ubd_finish(struct request
*req
, int error
)
508 spin_lock(&ubd_io_lock
);
509 __ubd_finish(req
, error
);
510 spin_unlock(&ubd_io_lock
);
513 /* XXX - move this inside ubd_intr. */
514 /* Called without ubd_io_lock held, and only in interrupt context. */
515 static void ubd_handler(void)
517 struct io_thread_req req
;
518 struct request
*rq
= elv_next_request(ubd_queue
);
523 n
= os_read_file(thread_fd
, &req
, sizeof(req
));
524 if(n
!= sizeof(req
)){
525 printk(KERN_ERR
"Pid %d - spurious interrupt in ubd_handler, "
526 "err = %d\n", os_getpid(), -n
);
527 spin_lock(&ubd_io_lock
);
529 spin_unlock(&ubd_io_lock
);
533 ubd_finish(rq
, req
.error
);
534 reactivate_fd(thread_fd
, UBD_IRQ
);
535 spin_lock(&ubd_io_lock
);
536 do_ubd_request(ubd_queue
);
537 spin_unlock(&ubd_io_lock
);
540 static irqreturn_t
ubd_intr(int irq
, void *dev
)
546 /* Only changed by ubd_init, which is an initcall. */
547 static int io_pid
= -1;
549 void kill_io_thread(void)
552 os_kill_process(io_pid
, 1);
555 __uml_exitcall(kill_io_thread
);
557 static int ubd_file_size(struct ubd
*ubd_dev
, __u64
*size_out
)
561 file
= ubd_dev
->cow
.file
? ubd_dev
->cow
.file
: ubd_dev
->file
;
562 return(os_file_size(file
, size_out
));
565 static void ubd_close_dev(struct ubd
*ubd_dev
)
567 os_close_file(ubd_dev
->fd
);
568 if(ubd_dev
->cow
.file
== NULL
)
571 os_close_file(ubd_dev
->cow
.fd
);
572 vfree(ubd_dev
->cow
.bitmap
);
573 ubd_dev
->cow
.bitmap
= NULL
;
576 static int ubd_open_dev(struct ubd
*ubd_dev
)
578 struct openflags flags
;
580 int err
, create_cow
, *create_ptr
;
583 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
585 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
586 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
588 fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
589 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
590 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
593 if((fd
== -ENOENT
) && create_cow
){
594 fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
595 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
596 &ubd_dev
->cow
.bitmap_offset
,
597 &ubd_dev
->cow
.bitmap_len
,
598 &ubd_dev
->cow
.data_offset
);
600 printk(KERN_INFO
"Creating \"%s\" as COW file for "
601 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
606 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
612 if(ubd_dev
->cow
.file
!= NULL
){
614 ubd_dev
->cow
.bitmap
= (void *) vmalloc(ubd_dev
->cow
.bitmap_len
);
615 if(ubd_dev
->cow
.bitmap
== NULL
){
616 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
619 flush_tlb_kernel_vm();
621 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
622 ubd_dev
->cow
.bitmap_offset
,
623 ubd_dev
->cow
.bitmap_len
);
627 flags
= ubd_dev
->openflags
;
629 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
630 NULL
, NULL
, NULL
, NULL
);
631 if(err
< 0) goto error
;
632 ubd_dev
->cow
.fd
= err
;
636 os_close_file(ubd_dev
->fd
);
640 static int ubd_disk_register(int major
, u64 size
, int unit
,
641 struct gendisk
**disk_out
)
644 struct gendisk
*disk
;
646 disk
= alloc_disk(1 << UBD_SHIFT
);
651 disk
->first_minor
= unit
<< UBD_SHIFT
;
652 disk
->fops
= &ubd_blops
;
653 set_capacity(disk
, size
/ 512);
654 if(major
== MAJOR_NR
)
655 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
657 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
659 /* sysfs register (not for ide fake devices) */
660 if (major
== MAJOR_NR
) {
661 ubd_devs
[unit
].pdev
.id
= unit
;
662 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
663 platform_device_register(&ubd_devs
[unit
].pdev
);
664 disk
->driverfs_dev
= &ubd_devs
[unit
].pdev
.dev
;
667 disk
->private_data
= &ubd_devs
[unit
];
668 disk
->queue
= ubd_queue
;
675 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
677 static int ubd_add(int n
)
679 struct ubd
*ubd_dev
= &ubd_devs
[n
];
683 if(ubd_dev
->file
== NULL
)
686 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
690 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
692 err
= ubd_disk_register(MAJOR_NR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
696 if(fake_major
!= MAJOR_NR
)
697 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
700 /* perhaps this should also be under the "if (fake_major)" above */
701 /* using the fake_disk->disk_name and also the fakehd_set name */
703 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
710 static int ubd_config(char *str
)
714 str
= kstrdup(str
, GFP_KERNEL
);
716 printk(KERN_ERR
"ubd_config failed to strdup string\n");
720 ret
= ubd_setup_common(str
, &n
);
730 mutex_lock(&ubd_lock
);
733 ubd_devs
[n
].file
= NULL
;
734 mutex_unlock(&ubd_lock
);
744 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
749 n
= parse_unit(&name
);
750 if((n
>= MAX_DEV
) || (n
< 0)){
751 *error_out
= "ubd_get_config : device number out of range";
755 ubd_dev
= &ubd_devs
[n
];
756 mutex_lock(&ubd_lock
);
758 if(ubd_dev
->file
== NULL
){
759 CONFIG_CHUNK(str
, size
, len
, "", 1);
763 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
765 if(ubd_dev
->cow
.file
!= NULL
){
766 CONFIG_CHUNK(str
, size
, len
, ",", 0);
767 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
769 else CONFIG_CHUNK(str
, size
, len
, "", 1);
772 mutex_unlock(&ubd_lock
);
776 static int ubd_id(char **str
, int *start_out
, int *end_out
)
782 *end_out
= MAX_DEV
- 1;
786 static int ubd_remove(int n
)
791 mutex_lock(&ubd_lock
);
793 if(ubd_gendisk
[n
] == NULL
)
796 ubd_dev
= &ubd_devs
[n
];
798 if(ubd_dev
->file
== NULL
)
801 /* you cannot remove a open disk */
803 if(ubd_dev
->count
> 0)
806 del_gendisk(ubd_gendisk
[n
]);
807 put_disk(ubd_gendisk
[n
]);
808 ubd_gendisk
[n
] = NULL
;
810 if(fake_gendisk
[n
] != NULL
){
811 del_gendisk(fake_gendisk
[n
]);
812 put_disk(fake_gendisk
[n
]);
813 fake_gendisk
[n
] = NULL
;
816 platform_device_unregister(&ubd_dev
->pdev
);
817 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
820 mutex_unlock(&ubd_lock
);
824 static struct mc_device ubd_mc
= {
826 .config
= ubd_config
,
827 .get_config
= ubd_get_config
,
829 .remove
= ubd_remove
,
832 static int ubd_mc_init(void)
834 mconsole_register_dev(&ubd_mc
);
838 __initcall(ubd_mc_init
);
840 static struct platform_driver ubd_driver
= {
850 if (register_blkdev(MAJOR_NR
, "ubd"))
853 ubd_queue
= blk_init_queue(do_ubd_request
, &ubd_io_lock
);
855 unregister_blkdev(MAJOR_NR
, "ubd");
859 if (fake_major
!= MAJOR_NR
) {
860 char name
[sizeof("ubd_nnn\0")];
862 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
863 if (register_blkdev(fake_major
, "ubd"))
866 platform_driver_register(&ubd_driver
);
867 for (i
= 0; i
< MAX_DEV
; i
++)
872 late_initcall(ubd_init
);
874 int ubd_driver_init(void){
878 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
879 if(global_openflags
.s
){
880 printk(KERN_INFO
"ubd: Synchronous mode\n");
881 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
882 * enough. So use anyway the io thread. */
884 stack
= alloc_stack(0, 0);
885 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
889 "ubd : Failed to start I/O thread (errno = %d) - "
890 "falling back to synchronous I/O\n", -io_pid
);
894 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
895 IRQF_DISABLED
, "ubd", ubd_devs
);
897 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
901 device_initcall(ubd_driver_init
);
903 static int ubd_open(struct inode
*inode
, struct file
*filp
)
905 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
906 struct ubd
*ubd_dev
= disk
->private_data
;
909 if(ubd_dev
->count
== 0){
910 err
= ubd_open_dev(ubd_dev
);
912 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
913 disk
->disk_name
, ubd_dev
->file
, -err
);
918 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
920 /* This should no more be needed. And it didn't work anyway to exclude
921 * read-write remounting of filesystems.*/
922 /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
923 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
930 static int ubd_release(struct inode
* inode
, struct file
* file
)
932 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
933 struct ubd
*ubd_dev
= disk
->private_data
;
935 if(--ubd_dev
->count
== 0)
936 ubd_close_dev(ubd_dev
);
940 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
941 __u64
*cow_offset
, unsigned long *bitmap
,
942 __u64 bitmap_offset
, unsigned long *bitmap_words
,
945 __u64 sector
= io_offset
>> 9;
946 int i
, update_bitmap
= 0;
948 for(i
= 0; i
< length
>> 9; i
++){
950 ubd_set_bit(i
, (unsigned char *) cow_mask
);
951 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
955 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
961 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
963 /* This takes care of the case where we're exactly at the end of the
964 * device, and *cow_offset + 1 is off the end. So, just back it up
965 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
966 * for the original diagnosis.
968 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
969 sizeof(unsigned long) - 1))
972 bitmap_words
[0] = bitmap
[*cow_offset
];
973 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
975 *cow_offset
*= sizeof(unsigned long);
976 *cow_offset
+= bitmap_offset
;
979 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
980 __u64 bitmap_offset
, __u64 bitmap_len
)
982 __u64 sector
= req
->offset
>> 9;
985 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
986 panic("Operation too long");
988 if(req
->op
== UBD_READ
) {
989 for(i
= 0; i
< req
->length
>> 9; i
++){
990 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
991 ubd_set_bit(i
, (unsigned char *)
995 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
996 &req
->cow_offset
, bitmap
, bitmap_offset
,
997 req
->bitmap_words
, bitmap_len
);
1000 /* Called with ubd_io_lock held */
1001 static int prepare_request(struct request
*req
, struct io_thread_req
*io_req
)
1003 struct gendisk
*disk
= req
->rq_disk
;
1004 struct ubd
*ubd_dev
= disk
->private_data
;
1008 /* This should be impossible now */
1009 if((rq_data_dir(req
) == WRITE
) && !ubd_dev
->openflags
.w
){
1010 printk("Write attempted on readonly ubd device %s\n",
1012 end_request(req
, 0);
1016 offset
= ((__u64
) req
->sector
) << 9;
1017 len
= req
->current_nr_sectors
<< 9;
1019 io_req
->fds
[0] = (ubd_dev
->cow
.file
!= NULL
) ? ubd_dev
->cow
.fd
: ubd_dev
->fd
;
1020 io_req
->fds
[1] = ubd_dev
->fd
;
1021 io_req
->cow_offset
= -1;
1022 io_req
->offset
= offset
;
1023 io_req
->length
= len
;
1025 io_req
->sector_mask
= 0;
1027 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1028 io_req
->offsets
[0] = 0;
1029 io_req
->offsets
[1] = ubd_dev
->cow
.data_offset
;
1030 io_req
->buffer
= req
->buffer
;
1031 io_req
->sectorsize
= 1 << 9;
1033 if(ubd_dev
->cow
.file
!= NULL
)
1034 cowify_req(io_req
, ubd_dev
->cow
.bitmap
, ubd_dev
->cow
.bitmap_offset
,
1035 ubd_dev
->cow
.bitmap_len
);
1040 /* Called with ubd_io_lock held */
1041 static void do_ubd_request(request_queue_t
*q
)
1043 struct io_thread_req io_req
;
1044 struct request
*req
;
1047 if(thread_fd
== -1){
1048 while((req
= elv_next_request(q
)) != NULL
){
1049 err
= prepare_request(req
, &io_req
);
1052 __ubd_finish(req
, io_req
.error
);
1057 if(do_ubd
|| (req
= elv_next_request(q
)) == NULL
)
1059 err
= prepare_request(req
, &io_req
);
1062 n
= os_write_file(thread_fd
, (char *) &io_req
,
1064 if(n
!= sizeof(io_req
))
1065 printk("write to io thread failed, "
1066 "errno = %d\n", -n
);
1071 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1073 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1077 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1081 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1082 unsigned int cmd
, unsigned long arg
)
1084 struct ubd
*ubd_dev
= inode
->i_bdev
->bd_disk
->private_data
;
1085 struct hd_driveid ubd_id
= {
1092 struct cdrom_volctrl volume
;
1093 case HDIO_GET_IDENTITY
:
1094 ubd_id
.cyls
= ubd_dev
->size
/ (128 * 32 * 512);
1095 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1101 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1103 volume
.channel0
= 255;
1104 volume
.channel1
= 255;
1105 volume
.channel2
= 255;
1106 volume
.channel3
= 255;
1107 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1114 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
1116 struct uml_stat buf1
, buf2
;
1119 if(from_cmdline
== NULL
)
1121 if(!strcmp(from_cmdline
, from_cow
))
1124 err
= os_stat_file(from_cmdline
, &buf1
);
1126 printk("Couldn't stat '%s', err = %d\n", from_cmdline
, -err
);
1129 err
= os_stat_file(from_cow
, &buf2
);
1131 printk("Couldn't stat '%s', err = %d\n", from_cow
, -err
);
1134 if((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
1137 printk("Backing file mismatch - \"%s\" requested,\n"
1138 "\"%s\" specified in COW header of \"%s\"\n",
1139 from_cmdline
, from_cow
, cow
);
1143 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
1145 unsigned long modtime
;
1146 unsigned long long actual
;
1149 err
= os_file_modtime(file
, &modtime
);
1151 printk("Failed to get modification time of backing file "
1152 "\"%s\", err = %d\n", file
, -err
);
1156 err
= os_file_size(file
, &actual
);
1158 printk("Failed to get size of backing file \"%s\", "
1159 "err = %d\n", file
, -err
);
1164 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1166 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1167 "file\n", (unsigned long long) size
, actual
);
1170 if(modtime
!= mtime
){
1171 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1172 "file\n", mtime
, modtime
);
1178 int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
1182 err
= os_seek_file(fd
, offset
);
1186 err
= os_read_file(fd
, buf
, len
);
1193 int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
1194 char **backing_file_out
, int *bitmap_offset_out
,
1195 unsigned long *bitmap_len_out
, int *data_offset_out
,
1196 int *create_cow_out
)
1199 unsigned long long size
;
1200 __u32 version
, align
;
1202 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
1204 fd
= os_open_file(file
, *openflags
, mode
);
1206 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
1207 *create_cow_out
= 1;
1208 if (!openflags
->w
||
1209 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
1212 fd
= os_open_file(file
, *openflags
, mode
);
1218 printk("Not locking \"%s\" on the host\n", file
);
1220 err
= os_lock_file(fd
, openflags
->w
);
1222 printk("Failed to lock '%s', err = %d\n", file
, -err
);
1227 /* Successful return case! */
1228 if(backing_file_out
== NULL
)
1231 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
1232 &size
, §orsize
, &align
, bitmap_offset_out
);
1233 if(err
&& (*backing_file_out
!= NULL
)){
1234 printk("Failed to read COW header from COW file \"%s\", "
1235 "errno = %d\n", file
, -err
);
1241 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
, file
);
1243 /* Allow switching only if no mismatch. */
1244 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
, mtime
)) {
1245 printk("Switching backing file to '%s'\n", *backing_file_out
);
1246 err
= write_cow_header(file
, fd
, *backing_file_out
,
1247 sectorsize
, align
, &size
);
1249 printk("Switch failed, errno = %d\n", -err
);
1253 *backing_file_out
= backing_file
;
1254 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
1259 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
1260 bitmap_len_out
, data_offset_out
);
1268 int create_cow_file(char *cow_file
, char *backing_file
, struct openflags flags
,
1269 int sectorsize
, int alignment
, int *bitmap_offset_out
,
1270 unsigned long *bitmap_len_out
, int *data_offset_out
)
1275 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
1278 printk("Open of COW file '%s' failed, errno = %d\n", cow_file
,
1283 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
1284 bitmap_offset_out
, bitmap_len_out
,
1293 static int update_bitmap(struct io_thread_req
*req
)
1297 if(req
->cow_offset
== -1)
1300 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1302 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1306 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1307 sizeof(req
->bitmap_words
));
1308 if(n
!= sizeof(req
->bitmap_words
)){
1309 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1317 void do_io(struct io_thread_req
*req
)
1321 int n
, nsectors
, start
, end
, bit
;
1325 nsectors
= req
->length
/ req
->sectorsize
;
1328 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1330 while((end
< nsectors
) &&
1331 (ubd_test_bit(end
, (unsigned char *)
1332 &req
->sector_mask
) == bit
))
1335 off
= req
->offset
+ req
->offsets
[bit
] +
1336 start
* req
->sectorsize
;
1337 len
= (end
- start
) * req
->sectorsize
;
1338 buf
= &req
->buffer
[start
* req
->sectorsize
];
1340 err
= os_seek_file(req
->fds
[bit
], off
);
1342 printk("do_io - lseek failed : err = %d\n", -err
);
1346 if(req
->op
== UBD_READ
){
1351 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1353 printk("do_io - read failed, err = %d "
1354 "fd = %d\n", -n
, req
->fds
[bit
]);
1358 } while((n
< len
) && (n
!= 0));
1359 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1361 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1363 printk("do_io - write failed err = %d "
1364 "fd = %d\n", -n
, req
->fds
[bit
]);
1371 } while(start
< nsectors
);
1373 req
->error
= update_bitmap(req
);
1376 /* Changed in start_io_thread, which is serialized by being called only
1377 * from ubd_init, which is an initcall.
1381 /* Only changed by the io thread */
1384 int io_thread(void *arg
)
1386 struct io_thread_req req
;
1389 ignore_sigwinch_sig();
1391 n
= os_read_file(kernel_fd
, &req
, sizeof(req
));
1392 if(n
!= sizeof(req
)){
1394 printk("io_thread - read failed, fd = %d, "
1395 "err = %d\n", kernel_fd
, -n
);
1397 printk("io_thread - short read, fd = %d, "
1398 "length = %d\n", kernel_fd
, n
);
1404 n
= os_write_file(kernel_fd
, &req
, sizeof(req
));
1405 if(n
!= sizeof(req
))
1406 printk("io_thread - write failed, fd = %d, err = %d\n",