2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 /* 2001-09-28...2002-04-17
7 * Partition stuff by James_McMechan@hotmail.com
8 * old style ubd by setting UBD_SHIFT to 0
9 * 2002-09-27...2002-10-18 massive tinkering for 2.5
10 * partitions have changed in 2.5
11 * 2003-01-29 more tinkering for 2.5.59-1
12 * This should now address the sysfs problems and has
13 * the symlink for devfs to allow for booting with
14 * the common /dev/ubd/discX/... names rather than
15 * only /dev/ubdN/discN this version also has lots of
16 * clean ups preparing for ubd-many.
20 #define MAJOR_NR UBD_MAJOR
23 #include "linux/module.h"
24 #include "linux/blkdev.h"
25 #include "linux/hdreg.h"
26 #include "linux/init.h"
27 #include "linux/cdrom.h"
28 #include "linux/proc_fs.h"
29 #include "linux/ctype.h"
30 #include "linux/capability.h"
32 #include "linux/vmalloc.h"
33 #include "linux/blkpg.h"
34 #include "linux/genhd.h"
35 #include "linux/spinlock.h"
36 #include "linux/platform_device.h"
37 #include "asm/segment.h"
38 #include "asm/uaccess.h"
40 #include "asm/types.h"
41 #include "asm/tlbflush.h"
42 #include "user_util.h"
44 #include "kern_util.h"
46 #include "mconsole_kern.h"
56 enum ubd_req
{ UBD_READ
, UBD_WRITE
};
58 struct io_thread_req
{
61 unsigned long offsets
[2];
62 unsigned long long offset
;
66 unsigned long sector_mask
;
67 unsigned long long cow_offset
;
68 unsigned long bitmap_words
[2];
72 extern int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
73 char **backing_file_out
, int *bitmap_offset_out
,
74 unsigned long *bitmap_len_out
, int *data_offset_out
,
76 extern int create_cow_file(char *cow_file
, char *backing_file
,
77 struct openflags flags
, int sectorsize
,
78 int alignment
, int *bitmap_offset_out
,
79 unsigned long *bitmap_len_out
,
80 int *data_offset_out
);
81 extern int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
);
82 extern void do_io(struct io_thread_req
*req
);
84 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
89 bits
= sizeof(data
[0]) * 8;
92 return((data
[n
] & (1 << off
)) != 0);
95 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
100 bits
= sizeof(data
[0]) * 8;
103 data
[n
] |= (1 << off
);
105 /*End stuff from ubd_user.h*/
107 #define DRIVER_NAME "uml-blkdev"
109 static DEFINE_SPINLOCK(ubd_io_lock
);
111 static DEFINE_MUTEX(ubd_lock
);
113 static void (*do_ubd
)(void);
115 static int ubd_open(struct inode
* inode
, struct file
* filp
);
116 static int ubd_release(struct inode
* inode
, struct file
* file
);
117 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
118 unsigned int cmd
, unsigned long arg
);
119 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
);
123 static struct block_device_operations ubd_blops
= {
124 .owner
= THIS_MODULE
,
126 .release
= ubd_release
,
128 .getgeo
= ubd_getgeo
,
131 /* Protected by the queue_lock */
132 static request_queue_t
*ubd_queue
;
134 /* Protected by ubd_lock */
135 static int fake_major
= MAJOR_NR
;
137 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
138 static struct gendisk
*fake_gendisk
[MAX_DEV
];
140 #ifdef CONFIG_BLK_DEV_UBD_SYNC
141 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
144 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
148 /* Not protected - changed only in ubd_setup_common and then only to
151 static struct openflags global_openflags
= OPEN_FLAGS
;
154 /* backing file name */
156 /* backing file fd */
158 unsigned long *bitmap
;
159 unsigned long bitmap_len
;
165 /* name (and fd, below) of the file opened for writing, either the
166 * backing or the cow file. */
171 struct openflags boot_openflags
;
172 struct openflags openflags
;
176 struct platform_device pdev
;
179 #define DEFAULT_COW { \
183 .bitmap_offset = 0, \
187 #define DEFAULT_UBD { \
192 .boot_openflags = OPEN_FLAGS, \
193 .openflags = OPEN_FLAGS, \
196 .cow = DEFAULT_COW, \
199 struct ubd ubd_devs
[MAX_DEV
] = { [ 0 ... MAX_DEV
- 1 ] = DEFAULT_UBD
};
201 static int ubd0_init(void)
203 struct ubd
*ubd_dev
= &ubd_devs
[0];
205 if(ubd_dev
->file
== NULL
)
206 ubd_dev
->file
= "root_fs";
210 __initcall(ubd0_init
);
212 /* Only changed by fake_ide_setup which is a setup */
213 static int fake_ide
= 0;
214 static struct proc_dir_entry
*proc_ide_root
= NULL
;
215 static struct proc_dir_entry
*proc_ide
= NULL
;
217 static void make_proc_ide(void)
219 proc_ide_root
= proc_mkdir("ide", NULL
);
220 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
223 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
224 int *eof
, void *data
)
228 strcpy(page
, "disk\n");
229 len
= strlen("disk\n");
233 if (len
<= 0) return 0;
240 static void make_ide_entries(char *dev_name
)
242 struct proc_dir_entry
*dir
, *ent
;
245 if(proc_ide_root
== NULL
) make_proc_ide();
247 dir
= proc_mkdir(dev_name
, proc_ide
);
250 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
254 ent
->read_proc
= proc_ide_read_media
;
255 ent
->write_proc
= NULL
;
256 sprintf(name
,"ide0/%s", dev_name
);
257 proc_symlink(dev_name
, proc_ide_root
, name
);
260 static int fake_ide_setup(char *str
)
266 __setup("fake_ide", fake_ide_setup
);
268 __uml_help(fake_ide_setup
,
270 " Create ide0 entries that map onto ubd devices.\n\n"
273 static int parse_unit(char **ptr
)
275 char *str
= *ptr
, *end
;
279 n
= simple_strtoul(str
, &end
, 0);
284 else if (('a' <= *str
) && (*str
<= 'z')) {
292 static int ubd_setup_common(char *str
, int *index_out
)
295 struct openflags flags
= global_openflags
;
299 if(index_out
) *index_out
= -1;
306 if(!strcmp(str
, "sync")){
307 global_openflags
= of_sync(global_openflags
);
310 major
= simple_strtoul(str
, &end
, 0);
311 if((*end
!= '\0') || (end
== str
)){
313 "ubd_setup : didn't parse major number\n");
318 mutex_lock(&ubd_lock
);
319 if(fake_major
!= MAJOR_NR
){
320 printk(KERN_ERR
"Can't assign a fake major twice\n");
326 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
330 mutex_unlock(&ubd_lock
);
334 n
= parse_unit(&str
);
336 printk(KERN_ERR
"ubd_setup : couldn't parse unit number "
341 printk(KERN_ERR
"ubd_setup : index %d out of range "
342 "(%d devices, from 0 to %d)\n", n
, MAX_DEV
, MAX_DEV
- 1);
347 mutex_lock(&ubd_lock
);
349 ubd_dev
= &ubd_devs
[n
];
350 if(ubd_dev
->file
!= NULL
){
351 printk(KERN_ERR
"ubd_setup : device already configured\n");
358 for (i
= 0; i
< sizeof("rscd="); i
++) {
376 printk(KERN_ERR
"ubd_setup : Expected '=' or flag letter (r, s, c, or d)\n");
383 printk(KERN_ERR
"ubd_setup : Too many flags specified\n");
385 printk(KERN_ERR
"ubd_setup : Expected '='\n");
390 backing_file
= strchr(str
, ',');
393 backing_file
= strchr(str
, ':');
398 printk(KERN_ERR
"Can't specify both 'd' and a "
401 *backing_file
= '\0';
406 ubd_dev
->cow
.file
= backing_file
;
407 ubd_dev
->boot_openflags
= flags
;
409 mutex_unlock(&ubd_lock
);
413 static int ubd_setup(char *str
)
415 ubd_setup_common(str
, NULL
);
419 __setup("ubd", ubd_setup
);
420 __uml_help(ubd_setup
,
421 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
422 " This is used to associate a device with a file in the underlying\n"
423 " filesystem. When specifying two filenames, the first one is the\n"
424 " COW name and the second is the backing file name. As separator you can\n"
425 " use either a ':' or a ',': the first one allows writing things like;\n"
426 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
427 " while with a ',' the shell would not expand the 2nd '~'.\n"
428 " When using only one filename, UML will detect whether to thread it like\n"
429 " a COW file or a backing file. To override this detection, add the 'd'\n"
431 " ubd0d=BackingFile\n"
432 " Usually, there is a filesystem in the file, but \n"
433 " that's not required. Swap devices containing swap files can be\n"
434 " specified like this. Also, a file which doesn't contain a\n"
435 " filesystem can have its contents read in the virtual \n"
436 " machine by running 'dd' on the device. <n> must be in the range\n"
437 " 0 to 7. Appending an 'r' to the number will cause that device\n"
438 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
439 " an 's' will cause data to be written to disk on the host immediately.\n\n"
442 static int udb_setup(char *str
)
444 printk("udb%s specified on command line is almost certainly a ubd -> "
449 __setup("udb", udb_setup
);
450 __uml_help(udb_setup
,
452 " This option is here solely to catch ubd -> udb typos, which can be\n"
453 " to impossible to catch visually unless you specifically look for\n"
454 " them. The only result of any option starting with 'udb' is an error\n"
455 " in the boot output.\n\n"
458 static int fakehd_set
= 0;
459 static int fakehd(char *str
)
461 printk(KERN_INFO
"fakehd : Changing ubd name to \"hd\".\n");
466 __setup("fakehd", fakehd
);
469 " Change the ubd device name to \"hd\".\n\n"
472 static void do_ubd_request(request_queue_t
* q
);
474 /* Only changed by ubd_init, which is an initcall. */
477 /* Changed by ubd_handler, which is serialized because interrupts only
482 /* call ubd_finish if you need to serialize */
483 static void __ubd_finish(struct request
*req
, int error
)
491 nsect
= req
->current_nr_sectors
;
492 req
->sector
+= nsect
;
493 req
->buffer
+= nsect
<< 9;
495 req
->nr_sectors
-= nsect
;
496 req
->current_nr_sectors
= 0;
500 static inline void ubd_finish(struct request
*req
, int error
)
502 spin_lock(&ubd_io_lock
);
503 __ubd_finish(req
, error
);
504 spin_unlock(&ubd_io_lock
);
507 /* Called without ubd_io_lock held */
508 static void ubd_handler(void)
510 struct io_thread_req req
;
511 struct request
*rq
= elv_next_request(ubd_queue
);
516 n
= os_read_file(thread_fd
, &req
, sizeof(req
));
517 if(n
!= sizeof(req
)){
518 printk(KERN_ERR
"Pid %d - spurious interrupt in ubd_handler, "
519 "err = %d\n", os_getpid(), -n
);
520 spin_lock(&ubd_io_lock
);
522 spin_unlock(&ubd_io_lock
);
526 ubd_finish(rq
, req
.error
);
527 reactivate_fd(thread_fd
, UBD_IRQ
);
528 do_ubd_request(ubd_queue
);
531 static irqreturn_t
ubd_intr(int irq
, void *dev
)
537 /* Only changed by ubd_init, which is an initcall. */
538 static int io_pid
= -1;
540 void kill_io_thread(void)
543 os_kill_process(io_pid
, 1);
546 __uml_exitcall(kill_io_thread
);
548 static int ubd_file_size(struct ubd
*ubd_dev
, __u64
*size_out
)
552 file
= ubd_dev
->cow
.file
? ubd_dev
->cow
.file
: ubd_dev
->file
;
553 return(os_file_size(file
, size_out
));
556 static void ubd_close_dev(struct ubd
*ubd_dev
)
558 os_close_file(ubd_dev
->fd
);
559 if(ubd_dev
->cow
.file
== NULL
)
562 os_close_file(ubd_dev
->cow
.fd
);
563 vfree(ubd_dev
->cow
.bitmap
);
564 ubd_dev
->cow
.bitmap
= NULL
;
567 static int ubd_open_dev(struct ubd
*ubd_dev
)
569 struct openflags flags
;
571 int err
, create_cow
, *create_ptr
;
573 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
575 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
576 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
577 ubd_dev
->fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
578 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
579 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
582 if((ubd_dev
->fd
== -ENOENT
) && create_cow
){
583 ubd_dev
->fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
584 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
585 &ubd_dev
->cow
.bitmap_offset
,
586 &ubd_dev
->cow
.bitmap_len
,
587 &ubd_dev
->cow
.data_offset
);
588 if(ubd_dev
->fd
>= 0){
589 printk(KERN_INFO
"Creating \"%s\" as COW file for "
590 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
595 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
600 if(ubd_dev
->cow
.file
!= NULL
){
602 ubd_dev
->cow
.bitmap
= (void *) vmalloc(ubd_dev
->cow
.bitmap_len
);
603 if(ubd_dev
->cow
.bitmap
== NULL
){
604 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
607 flush_tlb_kernel_vm();
609 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
610 ubd_dev
->cow
.bitmap_offset
,
611 ubd_dev
->cow
.bitmap_len
);
615 flags
= ubd_dev
->openflags
;
617 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
618 NULL
, NULL
, NULL
, NULL
);
619 if(err
< 0) goto error
;
620 ubd_dev
->cow
.fd
= err
;
624 os_close_file(ubd_dev
->fd
);
628 static int ubd_disk_register(int major
, u64 size
, int unit
,
629 struct gendisk
**disk_out
)
632 struct gendisk
*disk
;
634 disk
= alloc_disk(1 << UBD_SHIFT
);
639 disk
->first_minor
= unit
<< UBD_SHIFT
;
640 disk
->fops
= &ubd_blops
;
641 set_capacity(disk
, size
/ 512);
642 if(major
== MAJOR_NR
)
643 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
645 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
647 /* sysfs register (not for ide fake devices) */
648 if (major
== MAJOR_NR
) {
649 ubd_devs
[unit
].pdev
.id
= unit
;
650 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
651 platform_device_register(&ubd_devs
[unit
].pdev
);
652 disk
->driverfs_dev
= &ubd_devs
[unit
].pdev
.dev
;
655 disk
->private_data
= &ubd_devs
[unit
];
656 disk
->queue
= ubd_queue
;
663 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
665 static int ubd_add(int n
)
667 struct ubd
*ubd_dev
= &ubd_devs
[n
];
671 if(ubd_dev
->file
== NULL
)
674 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
678 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
680 err
= ubd_disk_register(MAJOR_NR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
684 if(fake_major
!= MAJOR_NR
)
685 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
688 /* perhaps this should also be under the "if (fake_major)" above */
689 /* using the fake_disk->disk_name and also the fakehd_set name */
691 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
698 static int ubd_config(char *str
)
702 str
= kstrdup(str
, GFP_KERNEL
);
704 printk(KERN_ERR
"ubd_config failed to strdup string\n");
707 err
= ubd_setup_common(str
, &n
);
712 if(n
== -1) return(0);
714 mutex_lock(&ubd_lock
);
717 ubd_devs
[n
].file
= NULL
;
718 mutex_unlock(&ubd_lock
);
723 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
728 n
= parse_unit(&name
);
729 if((n
>= MAX_DEV
) || (n
< 0)){
730 *error_out
= "ubd_get_config : device number out of range";
734 ubd_dev
= &ubd_devs
[n
];
735 mutex_lock(&ubd_lock
);
737 if(ubd_dev
->file
== NULL
){
738 CONFIG_CHUNK(str
, size
, len
, "", 1);
742 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
744 if(ubd_dev
->cow
.file
!= NULL
){
745 CONFIG_CHUNK(str
, size
, len
, ",", 0);
746 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
748 else CONFIG_CHUNK(str
, size
, len
, "", 1);
751 mutex_unlock(&ubd_lock
);
755 static int ubd_id(char **str
, int *start_out
, int *end_out
)
761 *end_out
= MAX_DEV
- 1;
765 static int ubd_remove(int n
)
770 mutex_lock(&ubd_lock
);
772 if(ubd_gendisk
[n
] == NULL
)
775 ubd_dev
= &ubd_devs
[n
];
777 if(ubd_dev
->file
== NULL
)
780 /* you cannot remove a open disk */
782 if(ubd_dev
->count
> 0)
785 del_gendisk(ubd_gendisk
[n
]);
786 put_disk(ubd_gendisk
[n
]);
787 ubd_gendisk
[n
] = NULL
;
789 if(fake_gendisk
[n
] != NULL
){
790 del_gendisk(fake_gendisk
[n
]);
791 put_disk(fake_gendisk
[n
]);
792 fake_gendisk
[n
] = NULL
;
795 platform_device_unregister(&ubd_dev
->pdev
);
796 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
799 mutex_unlock(&ubd_lock
);
803 static struct mc_device ubd_mc
= {
805 .config
= ubd_config
,
806 .get_config
= ubd_get_config
,
808 .remove
= ubd_remove
,
811 static int ubd_mc_init(void)
813 mconsole_register_dev(&ubd_mc
);
817 __initcall(ubd_mc_init
);
819 static struct platform_driver ubd_driver
= {
829 if (register_blkdev(MAJOR_NR
, "ubd"))
832 ubd_queue
= blk_init_queue(do_ubd_request
, &ubd_io_lock
);
834 unregister_blkdev(MAJOR_NR
, "ubd");
838 if (fake_major
!= MAJOR_NR
) {
839 char name
[sizeof("ubd_nnn\0")];
841 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
842 if (register_blkdev(fake_major
, "ubd"))
845 platform_driver_register(&ubd_driver
);
846 for (i
= 0; i
< MAX_DEV
; i
++)
851 late_initcall(ubd_init
);
853 int ubd_driver_init(void){
857 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
858 if(global_openflags
.s
){
859 printk(KERN_INFO
"ubd: Synchronous mode\n");
860 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
861 * enough. So use anyway the io thread. */
863 stack
= alloc_stack(0, 0);
864 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
868 "ubd : Failed to start I/O thread (errno = %d) - "
869 "falling back to synchronous I/O\n", -io_pid
);
873 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
874 IRQF_DISABLED
, "ubd", ubd_devs
);
876 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
880 device_initcall(ubd_driver_init
);
882 static int ubd_open(struct inode
*inode
, struct file
*filp
)
884 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
885 struct ubd
*ubd_dev
= disk
->private_data
;
888 if(ubd_dev
->count
== 0){
889 err
= ubd_open_dev(ubd_dev
);
891 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
892 disk
->disk_name
, ubd_dev
->file
, -err
);
897 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
899 /* This should no more be needed. And it didn't work anyway to exclude
900 * read-write remounting of filesystems.*/
901 /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
902 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
909 static int ubd_release(struct inode
* inode
, struct file
* file
)
911 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
912 struct ubd
*ubd_dev
= disk
->private_data
;
914 if(--ubd_dev
->count
== 0)
915 ubd_close_dev(ubd_dev
);
919 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
920 __u64
*cow_offset
, unsigned long *bitmap
,
921 __u64 bitmap_offset
, unsigned long *bitmap_words
,
924 __u64 sector
= io_offset
>> 9;
925 int i
, update_bitmap
= 0;
927 for(i
= 0; i
< length
>> 9; i
++){
929 ubd_set_bit(i
, (unsigned char *) cow_mask
);
930 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
934 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
940 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
942 /* This takes care of the case where we're exactly at the end of the
943 * device, and *cow_offset + 1 is off the end. So, just back it up
944 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
945 * for the original diagnosis.
947 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
948 sizeof(unsigned long) - 1))
951 bitmap_words
[0] = bitmap
[*cow_offset
];
952 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
954 *cow_offset
*= sizeof(unsigned long);
955 *cow_offset
+= bitmap_offset
;
958 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
959 __u64 bitmap_offset
, __u64 bitmap_len
)
961 __u64 sector
= req
->offset
>> 9;
964 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
965 panic("Operation too long");
967 if(req
->op
== UBD_READ
) {
968 for(i
= 0; i
< req
->length
>> 9; i
++){
969 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
970 ubd_set_bit(i
, (unsigned char *)
974 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
975 &req
->cow_offset
, bitmap
, bitmap_offset
,
976 req
->bitmap_words
, bitmap_len
);
979 /* Called with ubd_io_lock held */
980 static int prepare_request(struct request
*req
, struct io_thread_req
*io_req
)
982 struct gendisk
*disk
= req
->rq_disk
;
983 struct ubd
*ubd_dev
= disk
->private_data
;
987 /* This should be impossible now */
988 if((rq_data_dir(req
) == WRITE
) && !ubd_dev
->openflags
.w
){
989 printk("Write attempted on readonly ubd device %s\n",
995 offset
= ((__u64
) req
->sector
) << 9;
996 len
= req
->current_nr_sectors
<< 9;
998 io_req
->fds
[0] = (ubd_dev
->cow
.file
!= NULL
) ? ubd_dev
->cow
.fd
: ubd_dev
->fd
;
999 io_req
->fds
[1] = ubd_dev
->fd
;
1000 io_req
->cow_offset
= -1;
1001 io_req
->offset
= offset
;
1002 io_req
->length
= len
;
1004 io_req
->sector_mask
= 0;
1006 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1007 io_req
->offsets
[0] = 0;
1008 io_req
->offsets
[1] = ubd_dev
->cow
.data_offset
;
1009 io_req
->buffer
= req
->buffer
;
1010 io_req
->sectorsize
= 1 << 9;
1012 if(ubd_dev
->cow
.file
!= NULL
)
1013 cowify_req(io_req
, ubd_dev
->cow
.bitmap
, ubd_dev
->cow
.bitmap_offset
,
1014 ubd_dev
->cow
.bitmap_len
);
1019 /* Called with ubd_io_lock held */
1020 static void do_ubd_request(request_queue_t
*q
)
1022 struct io_thread_req io_req
;
1023 struct request
*req
;
1026 if(thread_fd
== -1){
1027 while((req
= elv_next_request(q
)) != NULL
){
1028 err
= prepare_request(req
, &io_req
);
1031 __ubd_finish(req
, io_req
.error
);
1036 if(do_ubd
|| (req
= elv_next_request(q
)) == NULL
)
1038 err
= prepare_request(req
, &io_req
);
1040 do_ubd
= ubd_handler
;
1041 n
= os_write_file(thread_fd
, (char *) &io_req
,
1043 if(n
!= sizeof(io_req
))
1044 printk("write to io thread failed, "
1045 "errno = %d\n", -n
);
1050 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1052 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1056 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1060 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1061 unsigned int cmd
, unsigned long arg
)
1063 struct ubd
*ubd_dev
= inode
->i_bdev
->bd_disk
->private_data
;
1064 struct hd_driveid ubd_id
= {
1071 struct cdrom_volctrl volume
;
1072 case HDIO_GET_IDENTITY
:
1073 ubd_id
.cyls
= ubd_dev
->size
/ (128 * 32 * 512);
1074 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1080 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1082 volume
.channel0
= 255;
1083 volume
.channel1
= 255;
1084 volume
.channel2
= 255;
1085 volume
.channel3
= 255;
1086 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1093 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
1095 struct uml_stat buf1
, buf2
;
1098 if(from_cmdline
== NULL
)
1100 if(!strcmp(from_cmdline
, from_cow
))
1103 err
= os_stat_file(from_cmdline
, &buf1
);
1105 printk("Couldn't stat '%s', err = %d\n", from_cmdline
, -err
);
1108 err
= os_stat_file(from_cow
, &buf2
);
1110 printk("Couldn't stat '%s', err = %d\n", from_cow
, -err
);
1113 if((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
1116 printk("Backing file mismatch - \"%s\" requested,\n"
1117 "\"%s\" specified in COW header of \"%s\"\n",
1118 from_cmdline
, from_cow
, cow
);
1122 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
1124 unsigned long modtime
;
1125 unsigned long long actual
;
1128 err
= os_file_modtime(file
, &modtime
);
1130 printk("Failed to get modification time of backing file "
1131 "\"%s\", err = %d\n", file
, -err
);
1135 err
= os_file_size(file
, &actual
);
1137 printk("Failed to get size of backing file \"%s\", "
1138 "err = %d\n", file
, -err
);
1143 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1145 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1146 "file\n", (unsigned long long) size
, actual
);
1149 if(modtime
!= mtime
){
1150 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1151 "file\n", mtime
, modtime
);
1157 int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
1161 err
= os_seek_file(fd
, offset
);
1165 err
= os_read_file(fd
, buf
, len
);
1172 int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
1173 char **backing_file_out
, int *bitmap_offset_out
,
1174 unsigned long *bitmap_len_out
, int *data_offset_out
,
1175 int *create_cow_out
)
1178 unsigned long long size
;
1179 __u32 version
, align
;
1181 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
1183 fd
= os_open_file(file
, *openflags
, mode
);
1185 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
1186 *create_cow_out
= 1;
1187 if (!openflags
->w
||
1188 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
1191 fd
= os_open_file(file
, *openflags
, mode
);
1197 printk("Not locking \"%s\" on the host\n", file
);
1199 err
= os_lock_file(fd
, openflags
->w
);
1201 printk("Failed to lock '%s', err = %d\n", file
, -err
);
1206 /* Successful return case! */
1207 if(backing_file_out
== NULL
)
1210 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
1211 &size
, §orsize
, &align
, bitmap_offset_out
);
1212 if(err
&& (*backing_file_out
!= NULL
)){
1213 printk("Failed to read COW header from COW file \"%s\", "
1214 "errno = %d\n", file
, -err
);
1220 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
, file
);
1222 /* Allow switching only if no mismatch. */
1223 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
, mtime
)) {
1224 printk("Switching backing file to '%s'\n", *backing_file_out
);
1225 err
= write_cow_header(file
, fd
, *backing_file_out
,
1226 sectorsize
, align
, &size
);
1228 printk("Switch failed, errno = %d\n", -err
);
1232 *backing_file_out
= backing_file
;
1233 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
1238 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
1239 bitmap_len_out
, data_offset_out
);
1247 int create_cow_file(char *cow_file
, char *backing_file
, struct openflags flags
,
1248 int sectorsize
, int alignment
, int *bitmap_offset_out
,
1249 unsigned long *bitmap_len_out
, int *data_offset_out
)
1254 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
1257 printk("Open of COW file '%s' failed, errno = %d\n", cow_file
,
1262 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
1263 bitmap_offset_out
, bitmap_len_out
,
1272 static int update_bitmap(struct io_thread_req
*req
)
1276 if(req
->cow_offset
== -1)
1279 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1281 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1285 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1286 sizeof(req
->bitmap_words
));
1287 if(n
!= sizeof(req
->bitmap_words
)){
1288 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1296 void do_io(struct io_thread_req
*req
)
1300 int n
, nsectors
, start
, end
, bit
;
1304 nsectors
= req
->length
/ req
->sectorsize
;
1307 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1309 while((end
< nsectors
) &&
1310 (ubd_test_bit(end
, (unsigned char *)
1311 &req
->sector_mask
) == bit
))
1314 off
= req
->offset
+ req
->offsets
[bit
] +
1315 start
* req
->sectorsize
;
1316 len
= (end
- start
) * req
->sectorsize
;
1317 buf
= &req
->buffer
[start
* req
->sectorsize
];
1319 err
= os_seek_file(req
->fds
[bit
], off
);
1321 printk("do_io - lseek failed : err = %d\n", -err
);
1325 if(req
->op
== UBD_READ
){
1330 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1332 printk("do_io - read failed, err = %d "
1333 "fd = %d\n", -n
, req
->fds
[bit
]);
1337 } while((n
< len
) && (n
!= 0));
1338 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1340 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1342 printk("do_io - write failed err = %d "
1343 "fd = %d\n", -n
, req
->fds
[bit
]);
1350 } while(start
< nsectors
);
1352 req
->error
= update_bitmap(req
);
1355 /* Changed in start_io_thread, which is serialized by being called only
1356 * from ubd_init, which is an initcall.
1360 /* Only changed by the io thread */
1363 int io_thread(void *arg
)
1365 struct io_thread_req req
;
1368 ignore_sigwinch_sig();
1370 n
= os_read_file(kernel_fd
, &req
, sizeof(req
));
1371 if(n
!= sizeof(req
)){
1373 printk("io_thread - read failed, fd = %d, "
1374 "err = %d\n", kernel_fd
, -n
);
1376 printk("io_thread - short read, fd = %d, "
1377 "length = %d\n", kernel_fd
, n
);
1383 n
= os_write_file(kernel_fd
, &req
, sizeof(req
));
1384 if(n
!= sizeof(req
))
1385 printk("io_thread - write failed, fd = %d, err = %d\n",