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"
57 enum ubd_req
{ UBD_READ
, UBD_WRITE
};
59 struct io_thread_req
{
62 unsigned long offsets
[2];
63 unsigned long long offset
;
67 unsigned long sector_mask
;
68 unsigned long long cow_offset
;
69 unsigned long bitmap_words
[2];
73 extern int open_ubd_file(char *file
, struct openflags
*openflags
,
74 char **backing_file_out
, int *bitmap_offset_out
,
75 unsigned long *bitmap_len_out
, int *data_offset_out
,
77 extern int create_cow_file(char *cow_file
, char *backing_file
,
78 struct openflags flags
, int sectorsize
,
79 int alignment
, int *bitmap_offset_out
,
80 unsigned long *bitmap_len_out
,
81 int *data_offset_out
);
82 extern int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
);
83 extern void do_io(struct io_thread_req
*req
);
85 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
90 bits
= sizeof(data
[0]) * 8;
93 return((data
[n
] & (1 << off
)) != 0);
96 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
101 bits
= sizeof(data
[0]) * 8;
104 data
[n
] |= (1 << off
);
106 /*End stuff from ubd_user.h*/
108 #define DRIVER_NAME "uml-blkdev"
110 static DEFINE_SPINLOCK(ubd_io_lock
);
111 static DEFINE_SPINLOCK(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
);
122 static struct block_device_operations ubd_blops
= {
123 .owner
= THIS_MODULE
,
125 .release
= ubd_release
,
129 /* Protected by the queue_lock */
130 static request_queue_t
*ubd_queue
;
132 /* Protected by ubd_lock */
133 static int fake_major
= MAJOR_NR
;
135 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
136 static struct gendisk
*fake_gendisk
[MAX_DEV
];
138 #ifdef CONFIG_BLK_DEV_UBD_SYNC
139 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
142 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
146 /* Not protected - changed only in ubd_setup_common and then only to
149 static struct openflags global_openflags
= OPEN_FLAGS
;
152 /* This is the backing file, actually */
155 unsigned long *bitmap
;
156 unsigned long bitmap_len
;
166 struct openflags boot_openflags
;
167 struct openflags openflags
;
170 struct platform_device pdev
;
173 #define DEFAULT_COW { \
177 .bitmap_offset = 0, \
181 #define DEFAULT_UBD { \
186 .boot_openflags = OPEN_FLAGS, \
187 .openflags = OPEN_FLAGS, \
189 .cow = DEFAULT_COW, \
192 struct ubd ubd_dev
[MAX_DEV
] = { [ 0 ... MAX_DEV
- 1 ] = DEFAULT_UBD
};
194 static int ubd0_init(void)
196 struct ubd
*dev
= &ubd_dev
[0];
198 if(dev
->file
== NULL
)
199 dev
->file
= "root_fs";
203 __initcall(ubd0_init
);
205 /* Only changed by fake_ide_setup which is a setup */
206 static int fake_ide
= 0;
207 static struct proc_dir_entry
*proc_ide_root
= NULL
;
208 static struct proc_dir_entry
*proc_ide
= NULL
;
210 static void make_proc_ide(void)
212 proc_ide_root
= proc_mkdir("ide", NULL
);
213 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
216 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
217 int *eof
, void *data
)
221 strcpy(page
, "disk\n");
222 len
= strlen("disk\n");
226 if (len
<= 0) return 0;
233 static void make_ide_entries(char *dev_name
)
235 struct proc_dir_entry
*dir
, *ent
;
238 if(proc_ide_root
== NULL
) make_proc_ide();
240 dir
= proc_mkdir(dev_name
, proc_ide
);
243 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
247 ent
->read_proc
= proc_ide_read_media
;
248 ent
->write_proc
= NULL
;
249 sprintf(name
,"ide0/%s", dev_name
);
250 proc_symlink(dev_name
, proc_ide_root
, name
);
253 static int fake_ide_setup(char *str
)
259 __setup("fake_ide", fake_ide_setup
);
261 __uml_help(fake_ide_setup
,
263 " Create ide0 entries that map onto ubd devices.\n\n"
266 static int parse_unit(char **ptr
)
268 char *str
= *ptr
, *end
;
272 n
= simple_strtoul(str
, &end
, 0);
277 else if (('a' <= *str
) && (*str
<= 'h')) {
285 static int ubd_setup_common(char *str
, int *index_out
)
288 struct openflags flags
= global_openflags
;
292 if(index_out
) *index_out
= -1;
299 if(!strcmp(str
, "sync")){
300 global_openflags
= of_sync(global_openflags
);
303 major
= simple_strtoul(str
, &end
, 0);
304 if((*end
!= '\0') || (end
== str
)){
306 "ubd_setup : didn't parse major number\n");
311 spin_lock(&ubd_lock
);
312 if(fake_major
!= MAJOR_NR
){
313 printk(KERN_ERR
"Can't assign a fake major twice\n");
319 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
323 spin_unlock(&ubd_lock
);
327 n
= parse_unit(&str
);
329 printk(KERN_ERR
"ubd_setup : couldn't parse unit number "
334 printk(KERN_ERR
"ubd_setup : index %d out of range "
335 "(%d devices, from 0 to %d)\n", n
, MAX_DEV
, MAX_DEV
- 1);
340 spin_lock(&ubd_lock
);
343 if(dev
->file
!= NULL
){
344 printk(KERN_ERR
"ubd_setup : device already configured\n");
351 for (i
= 0; i
< 4; i
++) {
366 printk(KERN_ERR
"ubd_setup : Expected '=' or flag letter (r,s or d)\n");
373 printk(KERN_ERR
"ubd_setup : Too many flags specified\n");
375 printk(KERN_ERR
"ubd_setup : Expected '='\n");
380 backing_file
= strchr(str
, ',');
383 backing_file
= strchr(str
, ':');
388 printk(KERN_ERR
"Can't specify both 'd' and a "
391 *backing_file
= '\0';
396 dev
->cow
.file
= backing_file
;
397 dev
->boot_openflags
= flags
;
399 spin_unlock(&ubd_lock
);
403 static int ubd_setup(char *str
)
405 ubd_setup_common(str
, NULL
);
409 __setup("ubd", ubd_setup
);
410 __uml_help(ubd_setup
,
411 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
412 " This is used to associate a device with a file in the underlying\n"
413 " filesystem. When specifying two filenames, the first one is the\n"
414 " COW name and the second is the backing file name. As separator you can\n"
415 " use either a ':' or a ',': the first one allows writing things like;\n"
416 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
417 " while with a ',' the shell would not expand the 2nd '~'.\n"
418 " When using only one filename, UML will detect whether to thread it like\n"
419 " a COW file or a backing file. To override this detection, add the 'd'\n"
421 " ubd0d=BackingFile\n"
422 " Usually, there is a filesystem in the file, but \n"
423 " that's not required. Swap devices containing swap files can be\n"
424 " specified like this. Also, a file which doesn't contain a\n"
425 " filesystem can have its contents read in the virtual \n"
426 " machine by running 'dd' on the device. <n> must be in the range\n"
427 " 0 to 7. Appending an 'r' to the number will cause that device\n"
428 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
429 " an 's' will cause data to be written to disk on the host immediately.\n\n"
432 static int udb_setup(char *str
)
434 printk("udb%s specified on command line is almost certainly a ubd -> "
439 __setup("udb", udb_setup
);
440 __uml_help(udb_setup
,
442 " This option is here solely to catch ubd -> udb typos, which can be\n"
443 " to impossible to catch visually unless you specifically look for\n"
444 " them. The only result of any option starting with 'udb' is an error\n"
445 " in the boot output.\n\n"
448 static int fakehd_set
= 0;
449 static int fakehd(char *str
)
451 printk(KERN_INFO
"fakehd : Changing ubd name to \"hd\".\n");
456 __setup("fakehd", fakehd
);
459 " Change the ubd device name to \"hd\".\n\n"
462 static void do_ubd_request(request_queue_t
* q
);
464 /* Only changed by ubd_init, which is an initcall. */
467 /* Changed by ubd_handler, which is serialized because interrupts only
472 /* call ubd_finish if you need to serialize */
473 static void __ubd_finish(struct request
*req
, int error
)
481 nsect
= req
->current_nr_sectors
;
482 req
->sector
+= nsect
;
483 req
->buffer
+= nsect
<< 9;
485 req
->nr_sectors
-= nsect
;
486 req
->current_nr_sectors
= 0;
490 static inline void ubd_finish(struct request
*req
, int error
)
492 spin_lock(&ubd_io_lock
);
493 __ubd_finish(req
, error
);
494 spin_unlock(&ubd_io_lock
);
497 /* Called without ubd_io_lock held */
498 static void ubd_handler(void)
500 struct io_thread_req req
;
501 struct request
*rq
= elv_next_request(ubd_queue
);
506 n
= os_read_file(thread_fd
, &req
, sizeof(req
));
507 if(n
!= sizeof(req
)){
508 printk(KERN_ERR
"Pid %d - spurious interrupt in ubd_handler, "
509 "err = %d\n", os_getpid(), -n
);
510 spin_lock(&ubd_io_lock
);
512 spin_unlock(&ubd_io_lock
);
516 ubd_finish(rq
, req
.error
);
517 reactivate_fd(thread_fd
, UBD_IRQ
);
518 do_ubd_request(ubd_queue
);
521 static irqreturn_t
ubd_intr(int irq
, void *dev
, struct pt_regs
*unused
)
527 /* Only changed by ubd_init, which is an initcall. */
528 static int io_pid
= -1;
530 void kill_io_thread(void)
533 os_kill_process(io_pid
, 1);
536 __uml_exitcall(kill_io_thread
);
538 static int ubd_file_size(struct ubd
*dev
, __u64
*size_out
)
542 file
= dev
->cow
.file
? dev
->cow
.file
: dev
->file
;
543 return(os_file_size(file
, size_out
));
546 static void ubd_close(struct ubd
*dev
)
548 os_close_file(dev
->fd
);
549 if(dev
->cow
.file
== NULL
)
552 os_close_file(dev
->cow
.fd
);
553 vfree(dev
->cow
.bitmap
);
554 dev
->cow
.bitmap
= NULL
;
557 static int ubd_open_dev(struct ubd
*dev
)
559 struct openflags flags
;
561 int err
, create_cow
, *create_ptr
;
563 dev
->openflags
= dev
->boot_openflags
;
565 create_ptr
= (dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
566 back_ptr
= dev
->no_cow
? NULL
: &dev
->cow
.file
;
567 dev
->fd
= open_ubd_file(dev
->file
, &dev
->openflags
, back_ptr
,
568 &dev
->cow
.bitmap_offset
, &dev
->cow
.bitmap_len
,
569 &dev
->cow
.data_offset
, create_ptr
);
571 if((dev
->fd
== -ENOENT
) && create_cow
){
572 dev
->fd
= create_cow_file(dev
->file
, dev
->cow
.file
,
573 dev
->openflags
, 1 << 9, PAGE_SIZE
,
574 &dev
->cow
.bitmap_offset
,
575 &dev
->cow
.bitmap_len
,
576 &dev
->cow
.data_offset
);
578 printk(KERN_INFO
"Creating \"%s\" as COW file for "
579 "\"%s\"\n", dev
->file
, dev
->cow
.file
);
584 printk("Failed to open '%s', errno = %d\n", dev
->file
,
589 if(dev
->cow
.file
!= NULL
){
591 dev
->cow
.bitmap
= (void *) vmalloc(dev
->cow
.bitmap_len
);
592 if(dev
->cow
.bitmap
== NULL
){
593 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
596 flush_tlb_kernel_vm();
598 err
= read_cow_bitmap(dev
->fd
, dev
->cow
.bitmap
,
599 dev
->cow
.bitmap_offset
,
600 dev
->cow
.bitmap_len
);
604 flags
= dev
->openflags
;
606 err
= open_ubd_file(dev
->cow
.file
, &flags
, NULL
, NULL
, NULL
,
608 if(err
< 0) goto error
;
613 os_close_file(dev
->fd
);
617 static int ubd_new_disk(int major
, u64 size
, int unit
,
618 struct gendisk
**disk_out
)
621 struct gendisk
*disk
;
622 char from
[sizeof("ubd/nnnnn\0")], to
[sizeof("discnnnnn/disc\0")];
625 disk
= alloc_disk(1 << UBD_SHIFT
);
630 disk
->first_minor
= unit
<< UBD_SHIFT
;
631 disk
->fops
= &ubd_blops
;
632 set_capacity(disk
, size
/ 512);
633 if(major
== MAJOR_NR
){
634 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
635 sprintf(disk
->devfs_name
, "ubd/disc%d", unit
);
636 sprintf(from
, "ubd/%d", unit
);
637 sprintf(to
, "disc%d/disc", unit
);
638 err
= devfs_mk_symlink(from
, to
);
640 printk("ubd_new_disk failed to make link from %s to "
641 "%s, error = %d\n", from
, to
, err
);
644 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
645 sprintf(disk
->devfs_name
, "ubd_fake/disc%d", unit
);
648 /* sysfs register (not for ide fake devices) */
649 if (major
== MAJOR_NR
) {
650 ubd_dev
[unit
].pdev
.id
= unit
;
651 ubd_dev
[unit
].pdev
.name
= DRIVER_NAME
;
652 platform_device_register(&ubd_dev
[unit
].pdev
);
653 disk
->driverfs_dev
= &ubd_dev
[unit
].pdev
.dev
;
656 disk
->private_data
= &ubd_dev
[unit
];
657 disk
->queue
= ubd_queue
;
664 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
666 static int ubd_add(int n
)
668 struct ubd
*dev
= &ubd_dev
[n
];
671 if(dev
->file
== NULL
)
674 if (ubd_open_dev(dev
))
677 err
= ubd_file_size(dev
, &dev
->size
);
681 dev
->size
= ROUND_BLOCK(dev
->size
);
683 err
= ubd_new_disk(MAJOR_NR
, dev
->size
, n
, &ubd_gendisk
[n
]);
687 if(fake_major
!= MAJOR_NR
)
688 ubd_new_disk(fake_major
, dev
->size
, n
,
691 /* perhaps this should also be under the "if (fake_major)" above */
692 /* using the fake_disk->disk_name and also the fakehd_set name */
694 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
700 static int ubd_config(char *str
)
704 str
= uml_strdup(str
);
706 printk(KERN_ERR
"ubd_config failed to strdup string\n");
709 err
= ubd_setup_common(str
, &n
);
714 if(n
== -1) return(0);
716 spin_lock(&ubd_lock
);
719 ubd_dev
[n
].file
= NULL
;
720 spin_unlock(&ubd_lock
);
725 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
730 n
= parse_unit(&name
);
731 if((n
>= MAX_DEV
) || (n
< 0)){
732 *error_out
= "ubd_get_config : device number out of range";
737 spin_lock(&ubd_lock
);
739 if(dev
->file
== NULL
){
740 CONFIG_CHUNK(str
, size
, len
, "", 1);
744 CONFIG_CHUNK(str
, size
, len
, dev
->file
, 0);
746 if(dev
->cow
.file
!= NULL
){
747 CONFIG_CHUNK(str
, size
, len
, ",", 0);
748 CONFIG_CHUNK(str
, size
, len
, dev
->cow
.file
, 1);
750 else CONFIG_CHUNK(str
, size
, len
, "", 1);
753 spin_unlock(&ubd_lock
);
757 static int ubd_id(char **str
, int *start_out
, int *end_out
)
763 *end_out
= MAX_DEV
- 1;
767 static int ubd_remove(int n
)
772 spin_lock(&ubd_lock
);
774 if(ubd_gendisk
[n
] == NULL
)
779 if(dev
->file
== NULL
)
782 /* you cannot remove a open disk */
787 del_gendisk(ubd_gendisk
[n
]);
788 put_disk(ubd_gendisk
[n
]);
789 ubd_gendisk
[n
] = NULL
;
791 if(fake_gendisk
[n
] != NULL
){
792 del_gendisk(fake_gendisk
[n
]);
793 put_disk(fake_gendisk
[n
]);
794 fake_gendisk
[n
] = NULL
;
797 platform_device_unregister(&dev
->pdev
);
798 *dev
= ((struct ubd
) DEFAULT_UBD
);
801 spin_unlock(&ubd_lock
);
805 static struct mc_device ubd_mc
= {
807 .config
= ubd_config
,
808 .get_config
= ubd_get_config
,
810 .remove
= ubd_remove
,
813 static int ubd_mc_init(void)
815 mconsole_register_dev(&ubd_mc
);
819 __initcall(ubd_mc_init
);
821 static struct device_driver ubd_driver
= {
823 .bus
= &platform_bus_type
,
831 if (register_blkdev(MAJOR_NR
, "ubd"))
834 ubd_queue
= blk_init_queue(do_ubd_request
, &ubd_io_lock
);
836 unregister_blkdev(MAJOR_NR
, "ubd");
840 if (fake_major
!= MAJOR_NR
) {
841 char name
[sizeof("ubd_nnn\0")];
843 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
845 if (register_blkdev(fake_major
, "ubd"))
848 driver_register(&ubd_driver
);
849 for (i
= 0; i
< MAX_DEV
; i
++)
854 late_initcall(ubd_init
);
856 int ubd_driver_init(void){
860 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
861 if(global_openflags
.s
){
862 printk(KERN_INFO
"ubd: Synchronous mode\n");
863 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
864 * enough. So use anyway the io thread. */
866 stack
= alloc_stack(0, 0);
867 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
871 "ubd : Failed to start I/O thread (errno = %d) - "
872 "falling back to synchronous I/O\n", -io_pid
);
876 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
877 SA_INTERRUPT
, "ubd", ubd_dev
);
879 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
883 device_initcall(ubd_driver_init
);
885 static int ubd_open(struct inode
*inode
, struct file
*filp
)
887 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
888 struct ubd
*dev
= disk
->private_data
;
892 err
= ubd_open_dev(dev
);
894 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
895 disk
->disk_name
, dev
->file
, -err
);
900 set_disk_ro(disk
, !dev
->openflags
.w
);
902 /* This should no more be needed. And it didn't work anyway to exclude
903 * read-write remounting of filesystems.*/
904 /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
905 if(--dev->count == 0) ubd_close(dev);
912 static int ubd_release(struct inode
* inode
, struct file
* file
)
914 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
915 struct ubd
*dev
= disk
->private_data
;
917 if(--dev
->count
== 0)
922 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
923 __u64
*cow_offset
, unsigned long *bitmap
,
924 __u64 bitmap_offset
, unsigned long *bitmap_words
,
927 __u64 sector
= io_offset
>> 9;
928 int i
, update_bitmap
= 0;
930 for(i
= 0; i
< length
>> 9; i
++){
932 ubd_set_bit(i
, (unsigned char *) cow_mask
);
933 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
937 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
943 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
945 /* This takes care of the case where we're exactly at the end of the
946 * device, and *cow_offset + 1 is off the end. So, just back it up
947 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
948 * for the original diagnosis.
950 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
951 sizeof(unsigned long) - 1))
954 bitmap_words
[0] = bitmap
[*cow_offset
];
955 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
957 *cow_offset
*= sizeof(unsigned long);
958 *cow_offset
+= bitmap_offset
;
961 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
962 __u64 bitmap_offset
, __u64 bitmap_len
)
964 __u64 sector
= req
->offset
>> 9;
967 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
968 panic("Operation too long");
970 if(req
->op
== UBD_READ
) {
971 for(i
= 0; i
< req
->length
>> 9; i
++){
972 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
973 ubd_set_bit(i
, (unsigned char *)
977 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
978 &req
->cow_offset
, bitmap
, bitmap_offset
,
979 req
->bitmap_words
, bitmap_len
);
982 /* Called with ubd_io_lock held */
983 static int prepare_request(struct request
*req
, struct io_thread_req
*io_req
)
985 struct gendisk
*disk
= req
->rq_disk
;
986 struct ubd
*dev
= disk
->private_data
;
990 if(req
->rq_status
== RQ_INACTIVE
) return(1);
992 /* This should be impossible now */
993 if((rq_data_dir(req
) == WRITE
) && !dev
->openflags
.w
){
994 printk("Write attempted on readonly ubd device %s\n",
1000 offset
= ((__u64
) req
->sector
) << 9;
1001 len
= req
->current_nr_sectors
<< 9;
1003 io_req
->fds
[0] = (dev
->cow
.file
!= NULL
) ? dev
->cow
.fd
: dev
->fd
;
1004 io_req
->fds
[1] = dev
->fd
;
1005 io_req
->cow_offset
= -1;
1006 io_req
->offset
= offset
;
1007 io_req
->length
= len
;
1009 io_req
->sector_mask
= 0;
1011 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1012 io_req
->offsets
[0] = 0;
1013 io_req
->offsets
[1] = dev
->cow
.data_offset
;
1014 io_req
->buffer
= req
->buffer
;
1015 io_req
->sectorsize
= 1 << 9;
1017 if(dev
->cow
.file
!= NULL
)
1018 cowify_req(io_req
, dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1019 dev
->cow
.bitmap_len
);
1024 /* Called with ubd_io_lock held */
1025 static void do_ubd_request(request_queue_t
*q
)
1027 struct io_thread_req io_req
;
1028 struct request
*req
;
1031 if(thread_fd
== -1){
1032 while((req
= elv_next_request(q
)) != NULL
){
1033 err
= prepare_request(req
, &io_req
);
1036 __ubd_finish(req
, io_req
.error
);
1041 if(do_ubd
|| (req
= elv_next_request(q
)) == NULL
)
1043 err
= prepare_request(req
, &io_req
);
1045 do_ubd
= ubd_handler
;
1046 n
= os_write_file(thread_fd
, (char *) &io_req
,
1048 if(n
!= sizeof(io_req
))
1049 printk("write to io thread failed, "
1050 "errno = %d\n", -n
);
1055 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1056 unsigned int cmd
, unsigned long arg
)
1058 struct hd_geometry __user
*loc
= (struct hd_geometry __user
*) arg
;
1059 struct ubd
*dev
= inode
->i_bdev
->bd_disk
->private_data
;
1060 struct hd_driveid ubd_id
= {
1067 struct hd_geometry g
;
1068 struct cdrom_volctrl volume
;
1070 if(!loc
) return(-EINVAL
);
1073 g
.cylinders
= dev
->size
/ (128 * 32 * 512);
1074 g
.start
= get_start_sect(inode
->i_bdev
);
1075 return(copy_to_user(loc
, &g
, sizeof(g
)) ? -EFAULT
: 0);
1077 case HDIO_GET_IDENTITY
:
1078 ubd_id
.cyls
= dev
->size
/ (128 * 32 * 512);
1079 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1085 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1087 volume
.channel0
= 255;
1088 volume
.channel1
= 255;
1089 volume
.channel2
= 255;
1090 volume
.channel3
= 255;
1091 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1098 static int same_backing_files(char *from_cmdline
, char *from_cow
, char *cow
)
1100 struct uml_stat buf1
, buf2
;
1103 if(from_cmdline
== NULL
) return(1);
1104 if(!strcmp(from_cmdline
, from_cow
)) return(1);
1106 err
= os_stat_file(from_cmdline
, &buf1
);
1108 printk("Couldn't stat '%s', err = %d\n", from_cmdline
, -err
);
1111 err
= os_stat_file(from_cow
, &buf2
);
1113 printk("Couldn't stat '%s', err = %d\n", from_cow
, -err
);
1116 if((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
1119 printk("Backing file mismatch - \"%s\" requested,\n"
1120 "\"%s\" specified in COW header of \"%s\"\n",
1121 from_cmdline
, from_cow
, cow
);
1125 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
1127 unsigned long modtime
;
1131 err
= os_file_modtime(file
, &modtime
);
1133 printk("Failed to get modification time of backing file "
1134 "\"%s\", err = %d\n", file
, -err
);
1138 err
= os_file_size(file
, &actual
);
1140 printk("Failed to get size of backing file \"%s\", "
1141 "err = %d\n", file
, -err
);
1146 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1148 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1149 "file\n", (unsigned long long) size
, actual
);
1152 if(modtime
!= mtime
){
1153 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1154 "file\n", mtime
, modtime
);
1160 int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
1164 err
= os_seek_file(fd
, offset
);
1168 err
= os_read_file(fd
, buf
, len
);
1175 int open_ubd_file(char *file
, struct openflags
*openflags
,
1176 char **backing_file_out
, int *bitmap_offset_out
,
1177 unsigned long *bitmap_len_out
, int *data_offset_out
,
1178 int *create_cow_out
)
1181 unsigned long long size
;
1182 __u32 version
, align
;
1184 int fd
, err
, sectorsize
, same
, mode
= 0644;
1186 fd
= os_open_file(file
, *openflags
, mode
);
1188 if((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
1189 *create_cow_out
= 1;
1191 ((fd
!= -EROFS
) && (fd
!= -EACCES
))) return(fd
);
1193 fd
= os_open_file(file
, *openflags
, mode
);
1198 err
= os_lock_file(fd
, openflags
->w
);
1200 printk("Failed to lock '%s', err = %d\n", file
, -err
);
1204 if(backing_file_out
== NULL
) return(fd
);
1206 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
1207 &size
, §orsize
, &align
, bitmap_offset_out
);
1208 if(err
&& (*backing_file_out
!= NULL
)){
1209 printk("Failed to read COW header from COW file \"%s\", "
1210 "errno = %d\n", file
, -err
);
1215 if(backing_file_out
== NULL
) return(fd
);
1217 same
= same_backing_files(*backing_file_out
, backing_file
, file
);
1219 if(!same
&& !backing_file_mismatch(*backing_file_out
, size
, mtime
)){
1220 printk("Switching backing file to '%s'\n", *backing_file_out
);
1221 err
= write_cow_header(file
, fd
, *backing_file_out
,
1222 sectorsize
, align
, &size
);
1224 printk("Switch failed, errno = %d\n", -err
);
1229 *backing_file_out
= backing_file
;
1230 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
1231 if(err
) goto out_close
;
1234 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
1235 bitmap_len_out
, data_offset_out
);
1243 int create_cow_file(char *cow_file
, char *backing_file
, struct openflags flags
,
1244 int sectorsize
, int alignment
, int *bitmap_offset_out
,
1245 unsigned long *bitmap_len_out
, int *data_offset_out
)
1250 fd
= open_ubd_file(cow_file
, &flags
, NULL
, NULL
, NULL
, NULL
, NULL
);
1253 printk("Open of COW file '%s' failed, errno = %d\n", cow_file
,
1258 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
1259 bitmap_offset_out
, bitmap_len_out
,
1268 static int update_bitmap(struct io_thread_req
*req
)
1272 if(req
->cow_offset
== -1)
1275 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1277 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1281 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1282 sizeof(req
->bitmap_words
));
1283 if(n
!= sizeof(req
->bitmap_words
)){
1284 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1292 void do_io(struct io_thread_req
*req
)
1296 int n
, nsectors
, start
, end
, bit
;
1300 nsectors
= req
->length
/ req
->sectorsize
;
1303 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1305 while((end
< nsectors
) &&
1306 (ubd_test_bit(end
, (unsigned char *)
1307 &req
->sector_mask
) == bit
))
1310 off
= req
->offset
+ req
->offsets
[bit
] +
1311 start
* req
->sectorsize
;
1312 len
= (end
- start
) * req
->sectorsize
;
1313 buf
= &req
->buffer
[start
* req
->sectorsize
];
1315 err
= os_seek_file(req
->fds
[bit
], off
);
1317 printk("do_io - lseek failed : err = %d\n", -err
);
1321 if(req
->op
== UBD_READ
){
1326 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1328 printk("do_io - read failed, err = %d "
1329 "fd = %d\n", -n
, req
->fds
[bit
]);
1333 } while((n
< len
) && (n
!= 0));
1334 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1336 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1338 printk("do_io - write failed err = %d "
1339 "fd = %d\n", -n
, req
->fds
[bit
]);
1346 } while(start
< nsectors
);
1348 req
->error
= update_bitmap(req
);
1351 /* Changed in start_io_thread, which is serialized by being called only
1352 * from ubd_init, which is an initcall.
1356 /* Only changed by the io thread */
1359 int io_thread(void *arg
)
1361 struct io_thread_req req
;
1364 ignore_sigwinch_sig();
1366 n
= os_read_file(kernel_fd
, &req
, sizeof(req
));
1367 if(n
!= sizeof(req
)){
1369 printk("io_thread - read failed, fd = %d, "
1370 "err = %d\n", kernel_fd
, -n
);
1372 printk("io_thread - short read, fd = %d, "
1373 "length = %d\n", kernel_fd
, n
);
1379 n
= os_write_file(kernel_fd
, &req
, sizeof(req
));
1380 if(n
!= sizeof(req
))
1381 printk("io_thread - write failed, fd = %d, err = %d\n",
1387 * Overrides for Emacs so that we follow Linus's tabbing style.
1388 * Emacs will notice this stuff at the end of the file and automatically
1389 * adjust the settings for this buffer only. This must remain at the end
1391 * ---------------------------------------------------------------------------
1393 * c-file-style: "linux"