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 "linux/platform_device.h"
39 #include "asm/segment.h"
40 #include "asm/uaccess.h"
42 #include "asm/types.h"
43 #include "asm/tlbflush.h"
44 #include "user_util.h"
46 #include "kern_util.h"
48 #include "mconsole_kern.h"
58 enum ubd_req { UBD_READ, UBD_WRITE };
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];
74 extern int open_ubd_file(char *file, struct openflags *openflags,
75 char **backing_file_out, int *bitmap_offset_out,
76 unsigned long *bitmap_len_out, int *data_offset_out,
78 extern int create_cow_file(char *cow_file, char *backing_file,
79 struct openflags flags, int sectorsize,
80 int alignment, int *bitmap_offset_out,
81 unsigned long *bitmap_len_out,
82 int *data_offset_out);
83 extern int read_cow_bitmap(int fd, void *buf, int offset, int len);
84 extern void do_io(struct io_thread_req *req);
86 static inline int ubd_test_bit(__u64 bit, unsigned char *data)
91 bits = sizeof(data[0]) * 8;
94 return((data[n] & (1 << off)) != 0);
97 static inline void ubd_set_bit(__u64 bit, unsigned char *data)
102 bits = sizeof(data[0]) * 8;
105 data[n] |= (1 << off);
107 /*End stuff from ubd_user.h*/
109 #define DRIVER_NAME "uml-blkdev"
111 static DEFINE_SPINLOCK(ubd_io_lock);
112 static DEFINE_SPINLOCK(ubd_lock);
114 static void (*do_ubd)(void);
116 static int ubd_open(struct inode * inode, struct file * filp);
117 static int ubd_release(struct inode * inode, struct file * file);
118 static int ubd_ioctl(struct inode * inode, struct file * file,
119 unsigned int cmd, unsigned long arg);
123 static struct block_device_operations ubd_blops = {
124 .owner = THIS_MODULE,
126 .release = ubd_release,
130 /* Protected by the queue_lock */
131 static request_queue_t *ubd_queue;
133 /* Protected by ubd_lock */
134 static int fake_major = MAJOR_NR;
136 static struct gendisk *ubd_gendisk[MAX_DEV];
137 static struct gendisk *fake_gendisk[MAX_DEV];
139 #ifdef CONFIG_BLK_DEV_UBD_SYNC
140 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
143 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
147 /* Not protected - changed only in ubd_setup_common and then only to
150 static struct openflags global_openflags = OPEN_FLAGS;
153 /* This is the backing file, actually */
156 unsigned long *bitmap;
157 unsigned long bitmap_len;
167 struct openflags boot_openflags;
168 struct openflags openflags;
171 struct platform_device pdev;
174 #define DEFAULT_COW { \
178 .bitmap_offset = 0, \
182 #define DEFAULT_UBD { \
187 .boot_openflags = OPEN_FLAGS, \
188 .openflags = OPEN_FLAGS, \
190 .cow = DEFAULT_COW, \
193 struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
195 static int ubd0_init(void)
197 struct ubd *dev = &ubd_dev[0];
199 if(dev->file == NULL)
200 dev->file = "root_fs";
204 __initcall(ubd0_init);
206 /* Only changed by fake_ide_setup which is a setup */
207 static int fake_ide = 0;
208 static struct proc_dir_entry *proc_ide_root = NULL;
209 static struct proc_dir_entry *proc_ide = NULL;
211 static void make_proc_ide(void)
213 proc_ide_root = proc_mkdir("ide", NULL);
214 proc_ide = proc_mkdir("ide0", proc_ide_root);
217 static int proc_ide_read_media(char *page, char **start, off_t off, int count,
218 int *eof, void *data)
222 strcpy(page, "disk\n");
223 len = strlen("disk\n");
227 if (len <= 0) return 0;
234 static void make_ide_entries(char *dev_name)
236 struct proc_dir_entry *dir, *ent;
239 if(proc_ide_root == NULL) make_proc_ide();
241 dir = proc_mkdir(dev_name, proc_ide);
244 ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
248 ent->read_proc = proc_ide_read_media;
249 ent->write_proc = NULL;
250 sprintf(name,"ide0/%s", dev_name);
251 proc_symlink(dev_name, proc_ide_root, name);
254 static int fake_ide_setup(char *str)
260 __setup("fake_ide", fake_ide_setup);
262 __uml_help(fake_ide_setup,
264 " Create ide0 entries that map onto ubd devices.\n\n"
267 static int parse_unit(char **ptr)
269 char *str = *ptr, *end;
273 n = simple_strtoul(str, &end, 0);
278 else if (('a' <= *str) && (*str <= 'h')) {
286 static int ubd_setup_common(char *str, int *index_out)
289 struct openflags flags = global_openflags;
293 if(index_out) *index_out = -1;
300 if(!strcmp(str, "sync")){
301 global_openflags = of_sync(global_openflags);
304 major = simple_strtoul(str, &end, 0);
305 if((*end != '\0') || (end == str)){
307 "ubd_setup : didn't parse major number\n");
312 spin_lock(&ubd_lock);
313 if(fake_major != MAJOR_NR){
314 printk(KERN_ERR "Can't assign a fake major twice\n");
320 printk(KERN_INFO "Setting extra ubd major number to %d\n",
324 spin_unlock(&ubd_lock);
328 n = parse_unit(&str);
330 printk(KERN_ERR "ubd_setup : couldn't parse unit number "
335 printk(KERN_ERR "ubd_setup : index %d out of range "
336 "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
341 spin_lock(&ubd_lock);
344 if(dev->file != NULL){
345 printk(KERN_ERR "ubd_setup : device already configured\n");
352 for (i = 0; i < 4; i++) {
367 printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r,s or d)\n");
374 printk(KERN_ERR "ubd_setup : Too many flags specified\n");
376 printk(KERN_ERR "ubd_setup : Expected '='\n");
381 backing_file = strchr(str, ',');
384 backing_file = strchr(str, ':');
389 printk(KERN_ERR "Can't specify both 'd' and a "
392 *backing_file = '\0';
397 dev->cow.file = backing_file;
398 dev->boot_openflags = flags;
400 spin_unlock(&ubd_lock);
404 static int ubd_setup(char *str)
406 ubd_setup_common(str, NULL);
410 __setup("ubd", ubd_setup);
411 __uml_help(ubd_setup,
412 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
413 " This is used to associate a device with a file in the underlying\n"
414 " filesystem. When specifying two filenames, the first one is the\n"
415 " COW name and the second is the backing file name. As separator you can\n"
416 " use either a ':' or a ',': the first one allows writing things like;\n"
417 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
418 " while with a ',' the shell would not expand the 2nd '~'.\n"
419 " When using only one filename, UML will detect whether to thread it like\n"
420 " a COW file or a backing file. To override this detection, add the 'd'\n"
422 " ubd0d=BackingFile\n"
423 " Usually, there is a filesystem in the file, but \n"
424 " that's not required. Swap devices containing swap files can be\n"
425 " specified like this. Also, a file which doesn't contain a\n"
426 " filesystem can have its contents read in the virtual \n"
427 " machine by running 'dd' on the device. <n> must be in the range\n"
428 " 0 to 7. Appending an 'r' to the number will cause that device\n"
429 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
430 " an 's' will cause data to be written to disk on the host immediately.\n\n"
433 static int udb_setup(char *str)
435 printk("udb%s specified on command line is almost certainly a ubd -> "
440 __setup("udb", udb_setup);
441 __uml_help(udb_setup,
443 " This option is here solely to catch ubd -> udb typos, which can be\n"
444 " to impossible to catch visually unless you specifically look for\n"
445 " them. The only result of any option starting with 'udb' is an error\n"
446 " in the boot output.\n\n"
449 static int fakehd_set = 0;
450 static int fakehd(char *str)
452 printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
457 __setup("fakehd", fakehd);
460 " Change the ubd device name to \"hd\".\n\n"
463 static void do_ubd_request(request_queue_t * q);
465 /* Only changed by ubd_init, which is an initcall. */
468 /* Changed by ubd_handler, which is serialized because interrupts only
473 /* call ubd_finish if you need to serialize */
474 static void __ubd_finish(struct request *req, int error)
482 nsect = req->current_nr_sectors;
483 req->sector += nsect;
484 req->buffer += nsect << 9;
486 req->nr_sectors -= nsect;
487 req->current_nr_sectors = 0;
491 static inline void ubd_finish(struct request *req, int error)
493 spin_lock(&ubd_io_lock);
494 __ubd_finish(req, error);
495 spin_unlock(&ubd_io_lock);
498 /* Called without ubd_io_lock held */
499 static void ubd_handler(void)
501 struct io_thread_req req;
502 struct request *rq = elv_next_request(ubd_queue);
507 n = os_read_file(thread_fd, &req, sizeof(req));
508 if(n != sizeof(req)){
509 printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
510 "err = %d\n", os_getpid(), -n);
511 spin_lock(&ubd_io_lock);
513 spin_unlock(&ubd_io_lock);
517 ubd_finish(rq, req.error);
518 reactivate_fd(thread_fd, UBD_IRQ);
519 do_ubd_request(ubd_queue);
522 static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
528 /* Only changed by ubd_init, which is an initcall. */
529 static int io_pid = -1;
531 void kill_io_thread(void)
534 os_kill_process(io_pid, 1);
537 __uml_exitcall(kill_io_thread);
539 static int ubd_file_size(struct ubd *dev, __u64 *size_out)
543 file = dev->cow.file ? dev->cow.file : dev->file;
544 return(os_file_size(file, size_out));
547 static void ubd_close(struct ubd *dev)
549 os_close_file(dev->fd);
550 if(dev->cow.file == NULL)
553 os_close_file(dev->cow.fd);
554 vfree(dev->cow.bitmap);
555 dev->cow.bitmap = NULL;
558 static int ubd_open_dev(struct ubd *dev)
560 struct openflags flags;
562 int err, create_cow, *create_ptr;
564 dev->openflags = dev->boot_openflags;
566 create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
567 back_ptr = dev->no_cow ? NULL : &dev->cow.file;
568 dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
569 &dev->cow.bitmap_offset, &dev->cow.bitmap_len,
570 &dev->cow.data_offset, create_ptr);
572 if((dev->fd == -ENOENT) && create_cow){
573 dev->fd = create_cow_file(dev->file, dev->cow.file,
574 dev->openflags, 1 << 9, PAGE_SIZE,
575 &dev->cow.bitmap_offset,
576 &dev->cow.bitmap_len,
577 &dev->cow.data_offset);
579 printk(KERN_INFO "Creating \"%s\" as COW file for "
580 "\"%s\"\n", dev->file, dev->cow.file);
585 printk("Failed to open '%s', errno = %d\n", dev->file,
590 if(dev->cow.file != NULL){
592 dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
593 if(dev->cow.bitmap == NULL){
594 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
597 flush_tlb_kernel_vm();
599 err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
600 dev->cow.bitmap_offset,
601 dev->cow.bitmap_len);
605 flags = dev->openflags;
607 err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL,
609 if(err < 0) goto error;
614 os_close_file(dev->fd);
618 static int ubd_new_disk(int major, u64 size, int unit,
619 struct gendisk **disk_out)
622 struct gendisk *disk;
623 char from[sizeof("ubd/nnnnn\0")], to[sizeof("discnnnnn/disc\0")];
626 disk = alloc_disk(1 << UBD_SHIFT);
631 disk->first_minor = unit << UBD_SHIFT;
632 disk->fops = &ubd_blops;
633 set_capacity(disk, size / 512);
634 if(major == MAJOR_NR){
635 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
636 sprintf(disk->devfs_name, "ubd/disc%d", unit);
637 sprintf(from, "ubd/%d", unit);
638 sprintf(to, "disc%d/disc", unit);
639 err = devfs_mk_symlink(from, to);
641 printk("ubd_new_disk failed to make link from %s to "
642 "%s, error = %d\n", from, to, err);
645 sprintf(disk->disk_name, "ubd_fake%d", unit);
646 sprintf(disk->devfs_name, "ubd_fake/disc%d", unit);
649 /* sysfs register (not for ide fake devices) */
650 if (major == MAJOR_NR) {
651 ubd_dev[unit].pdev.id = unit;
652 ubd_dev[unit].pdev.name = DRIVER_NAME;
653 platform_device_register(&ubd_dev[unit].pdev);
654 disk->driverfs_dev = &ubd_dev[unit].pdev.dev;
657 disk->private_data = &ubd_dev[unit];
658 disk->queue = ubd_queue;
665 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
667 static int ubd_add(int n)
669 struct ubd *dev = &ubd_dev[n];
673 if(dev->file == NULL)
676 if (ubd_open_dev(dev))
679 err = ubd_file_size(dev, &dev->size);
683 dev->size = ROUND_BLOCK(dev->size);
685 err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
689 if(fake_major != MAJOR_NR)
690 ubd_new_disk(fake_major, dev->size, n,
693 /* perhaps this should also be under the "if (fake_major)" above */
694 /* using the fake_disk->disk_name and also the fakehd_set name */
696 make_ide_entries(ubd_gendisk[n]->disk_name);
705 static int ubd_config(char *str)
709 str = uml_strdup(str);
711 printk(KERN_ERR "ubd_config failed to strdup string\n");
714 err = ubd_setup_common(str, &n);
719 if(n == -1) return(0);
721 spin_lock(&ubd_lock);
724 ubd_dev[n].file = NULL;
725 spin_unlock(&ubd_lock);
730 static int ubd_get_config(char *name, char *str, int size, char **error_out)
735 n = parse_unit(&name);
736 if((n >= MAX_DEV) || (n < 0)){
737 *error_out = "ubd_get_config : device number out of range";
742 spin_lock(&ubd_lock);
744 if(dev->file == NULL){
745 CONFIG_CHUNK(str, size, len, "", 1);
749 CONFIG_CHUNK(str, size, len, dev->file, 0);
751 if(dev->cow.file != NULL){
752 CONFIG_CHUNK(str, size, len, ",", 0);
753 CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
755 else CONFIG_CHUNK(str, size, len, "", 1);
758 spin_unlock(&ubd_lock);
762 static int ubd_id(char **str, int *start_out, int *end_out)
768 *end_out = MAX_DEV - 1;
772 static int ubd_remove(int n)
777 spin_lock(&ubd_lock);
779 if(ubd_gendisk[n] == NULL)
784 if(dev->file == NULL)
787 /* you cannot remove a open disk */
792 del_gendisk(ubd_gendisk[n]);
793 put_disk(ubd_gendisk[n]);
794 ubd_gendisk[n] = NULL;
796 if(fake_gendisk[n] != NULL){
797 del_gendisk(fake_gendisk[n]);
798 put_disk(fake_gendisk[n]);
799 fake_gendisk[n] = NULL;
802 platform_device_unregister(&dev->pdev);
803 *dev = ((struct ubd) DEFAULT_UBD);
806 spin_unlock(&ubd_lock);
810 static struct mc_device ubd_mc = {
812 .config = ubd_config,
813 .get_config = ubd_get_config,
815 .remove = ubd_remove,
818 static int ubd_mc_init(void)
820 mconsole_register_dev(&ubd_mc);
824 __initcall(ubd_mc_init);
826 static struct device_driver ubd_driver = {
828 .bus = &platform_bus_type,
836 if (register_blkdev(MAJOR_NR, "ubd"))
839 ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
841 unregister_blkdev(MAJOR_NR, "ubd");
845 if (fake_major != MAJOR_NR) {
846 char name[sizeof("ubd_nnn\0")];
848 snprintf(name, sizeof(name), "ubd_%d", fake_major);
850 if (register_blkdev(fake_major, "ubd"))
853 driver_register(&ubd_driver);
854 for (i = 0; i < MAX_DEV; i++)
859 late_initcall(ubd_init);
861 int ubd_driver_init(void){
865 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
866 if(global_openflags.s){
867 printk(KERN_INFO "ubd: Synchronous mode\n");
868 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
869 * enough. So use anyway the io thread. */
871 stack = alloc_stack(0, 0);
872 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
876 "ubd : Failed to start I/O thread (errno = %d) - "
877 "falling back to synchronous I/O\n", -io_pid);
881 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
882 SA_INTERRUPT, "ubd", ubd_dev);
884 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
888 device_initcall(ubd_driver_init);
890 static int ubd_open(struct inode *inode, struct file *filp)
892 struct gendisk *disk = inode->i_bdev->bd_disk;
893 struct ubd *dev = disk->private_data;
897 err = ubd_open_dev(dev);
899 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
900 disk->disk_name, dev->file, -err);
905 set_disk_ro(disk, !dev->openflags.w);
907 /* This should no more be needed. And it didn't work anyway to exclude
908 * read-write remounting of filesystems.*/
909 /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
910 if(--dev->count == 0) ubd_close(dev);
917 static int ubd_release(struct inode * inode, struct file * file)
919 struct gendisk *disk = inode->i_bdev->bd_disk;
920 struct ubd *dev = disk->private_data;
922 if(--dev->count == 0)
927 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
928 __u64 *cow_offset, unsigned long *bitmap,
929 __u64 bitmap_offset, unsigned long *bitmap_words,
932 __u64 sector = io_offset >> 9;
933 int i, update_bitmap = 0;
935 for(i = 0; i < length >> 9; i++){
937 ubd_set_bit(i, (unsigned char *) cow_mask);
938 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
942 ubd_set_bit(sector + i, (unsigned char *) bitmap);
948 *cow_offset = sector / (sizeof(unsigned long) * 8);
950 /* This takes care of the case where we're exactly at the end of the
951 * device, and *cow_offset + 1 is off the end. So, just back it up
952 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
953 * for the original diagnosis.
955 if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
956 sizeof(unsigned long) - 1))
959 bitmap_words[0] = bitmap[*cow_offset];
960 bitmap_words[1] = bitmap[*cow_offset + 1];
962 *cow_offset *= sizeof(unsigned long);
963 *cow_offset += bitmap_offset;
966 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
967 __u64 bitmap_offset, __u64 bitmap_len)
969 __u64 sector = req->offset >> 9;
972 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
973 panic("Operation too long");
975 if(req->op == UBD_READ) {
976 for(i = 0; i < req->length >> 9; i++){
977 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
978 ubd_set_bit(i, (unsigned char *)
982 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
983 &req->cow_offset, bitmap, bitmap_offset,
984 req->bitmap_words, bitmap_len);
987 /* Called with ubd_io_lock held */
988 static int prepare_request(struct request *req, struct io_thread_req *io_req)
990 struct gendisk *disk = req->rq_disk;
991 struct ubd *dev = disk->private_data;
995 if(req->rq_status == RQ_INACTIVE) return(1);
997 /* This should be impossible now */
998 if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
999 printk("Write attempted on readonly ubd device %s\n",
1001 end_request(req, 0);
1005 offset = ((__u64) req->sector) << 9;
1006 len = req->current_nr_sectors << 9;
1008 io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
1009 io_req->fds[1] = dev->fd;
1010 io_req->cow_offset = -1;
1011 io_req->offset = offset;
1012 io_req->length = len;
1014 io_req->sector_mask = 0;
1016 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1017 io_req->offsets[0] = 0;
1018 io_req->offsets[1] = dev->cow.data_offset;
1019 io_req->buffer = req->buffer;
1020 io_req->sectorsize = 1 << 9;
1022 if(dev->cow.file != NULL)
1023 cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
1024 dev->cow.bitmap_len);
1029 /* Called with ubd_io_lock held */
1030 static void do_ubd_request(request_queue_t *q)
1032 struct io_thread_req io_req;
1033 struct request *req;
1036 if(thread_fd == -1){
1037 while((req = elv_next_request(q)) != NULL){
1038 err = prepare_request(req, &io_req);
1041 __ubd_finish(req, io_req.error);
1046 if(do_ubd || (req = elv_next_request(q)) == NULL)
1048 err = prepare_request(req, &io_req);
1050 do_ubd = ubd_handler;
1051 n = os_write_file(thread_fd, (char *) &io_req,
1053 if(n != sizeof(io_req))
1054 printk("write to io thread failed, "
1055 "errno = %d\n", -n);
1060 static int ubd_ioctl(struct inode * inode, struct file * file,
1061 unsigned int cmd, unsigned long arg)
1063 struct hd_geometry __user *loc = (struct hd_geometry __user *) arg;
1064 struct ubd *dev = inode->i_bdev->bd_disk->private_data;
1065 struct hd_driveid ubd_id = {
1072 struct hd_geometry g;
1073 struct cdrom_volctrl volume;
1075 if(!loc) return(-EINVAL);
1078 g.cylinders = dev->size / (128 * 32 * 512);
1079 g.start = get_start_sect(inode->i_bdev);
1080 return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
1082 case HDIO_GET_IDENTITY:
1083 ubd_id.cyls = dev->size / (128 * 32 * 512);
1084 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1090 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1092 volume.channel0 = 255;
1093 volume.channel1 = 255;
1094 volume.channel2 = 255;
1095 volume.channel3 = 255;
1096 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1103 static int same_backing_files(char *from_cmdline, char *from_cow, char *cow)
1105 struct uml_stat buf1, buf2;
1108 if(from_cmdline == NULL) return(1);
1109 if(!strcmp(from_cmdline, from_cow)) return(1);
1111 err = os_stat_file(from_cmdline, &buf1);
1113 printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
1116 err = os_stat_file(from_cow, &buf2);
1118 printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
1121 if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
1124 printk("Backing file mismatch - \"%s\" requested,\n"
1125 "\"%s\" specified in COW header of \"%s\"\n",
1126 from_cmdline, from_cow, cow);
1130 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
1132 unsigned long modtime;
1136 err = os_file_modtime(file, &modtime);
1138 printk("Failed to get modification time of backing file "
1139 "\"%s\", err = %d\n", file, -err);
1143 err = os_file_size(file, &actual);
1145 printk("Failed to get size of backing file \"%s\", "
1146 "err = %d\n", file, -err);
1151 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
1153 printk("Size mismatch (%llu vs %llu) of COW header vs backing "
1154 "file\n", (unsigned long long) size, actual);
1157 if(modtime != mtime){
1158 printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
1159 "file\n", mtime, modtime);
1165 int read_cow_bitmap(int fd, void *buf, int offset, int len)
1169 err = os_seek_file(fd, offset);
1173 err = os_read_file(fd, buf, len);
1180 int open_ubd_file(char *file, struct openflags *openflags,
1181 char **backing_file_out, int *bitmap_offset_out,
1182 unsigned long *bitmap_len_out, int *data_offset_out,
1183 int *create_cow_out)
1186 unsigned long long size;
1187 __u32 version, align;
1189 int fd, err, sectorsize, same, mode = 0644;
1191 fd = os_open_file(file, *openflags, mode);
1193 if((fd == -ENOENT) && (create_cow_out != NULL))
1194 *create_cow_out = 1;
1196 ((fd != -EROFS) && (fd != -EACCES))) return(fd);
1198 fd = os_open_file(file, *openflags, mode);
1203 err = os_lock_file(fd, openflags->w);
1205 printk("Failed to lock '%s', err = %d\n", file, -err);
1209 if(backing_file_out == NULL) return(fd);
1211 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
1212 &size, §orsize, &align, bitmap_offset_out);
1213 if(err && (*backing_file_out != NULL)){
1214 printk("Failed to read COW header from COW file \"%s\", "
1215 "errno = %d\n", file, -err);
1220 if(backing_file_out == NULL) return(fd);
1222 same = same_backing_files(*backing_file_out, backing_file, file);
1224 if(!same && !backing_file_mismatch(*backing_file_out, size, mtime)){
1225 printk("Switching backing file to '%s'\n", *backing_file_out);
1226 err = write_cow_header(file, fd, *backing_file_out,
1227 sectorsize, align, &size);
1229 printk("Switch failed, errno = %d\n", -err);
1234 *backing_file_out = backing_file;
1235 err = backing_file_mismatch(*backing_file_out, size, mtime);
1236 if(err) goto out_close;
1239 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
1240 bitmap_len_out, data_offset_out);
1248 int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
1249 int sectorsize, int alignment, int *bitmap_offset_out,
1250 unsigned long *bitmap_len_out, int *data_offset_out)
1255 fd = open_ubd_file(cow_file, &flags, NULL, NULL, NULL, NULL, NULL);
1258 printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
1263 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
1264 bitmap_offset_out, bitmap_len_out,
1273 static int update_bitmap(struct io_thread_req *req)
1277 if(req->cow_offset == -1)
1280 n = os_seek_file(req->fds[1], req->cow_offset);
1282 printk("do_io - bitmap lseek failed : err = %d\n", -n);
1286 n = os_write_file(req->fds[1], &req->bitmap_words,
1287 sizeof(req->bitmap_words));
1288 if(n != sizeof(req->bitmap_words)){
1289 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
1297 void do_io(struct io_thread_req *req)
1301 int n, nsectors, start, end, bit;
1305 nsectors = req->length / req->sectorsize;
1308 bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
1310 while((end < nsectors) &&
1311 (ubd_test_bit(end, (unsigned char *)
1312 &req->sector_mask) == bit))
1315 off = req->offset + req->offsets[bit] +
1316 start * req->sectorsize;
1317 len = (end - start) * req->sectorsize;
1318 buf = &req->buffer[start * req->sectorsize];
1320 err = os_seek_file(req->fds[bit], off);
1322 printk("do_io - lseek failed : err = %d\n", -err);
1326 if(req->op == UBD_READ){
1331 n = os_read_file(req->fds[bit], buf, len);
1333 printk("do_io - read failed, err = %d "
1334 "fd = %d\n", -n, req->fds[bit]);
1338 } while((n < len) && (n != 0));
1339 if (n < len) memset(&buf[n], 0, len - n);
1341 n = os_write_file(req->fds[bit], buf, len);
1343 printk("do_io - write failed err = %d "
1344 "fd = %d\n", -n, req->fds[bit]);
1351 } while(start < nsectors);
1353 req->error = update_bitmap(req);
1356 /* Changed in start_io_thread, which is serialized by being called only
1357 * from ubd_init, which is an initcall.
1361 /* Only changed by the io thread */
1364 int io_thread(void *arg)
1366 struct io_thread_req req;
1369 ignore_sigwinch_sig();
1371 n = os_read_file(kernel_fd, &req, sizeof(req));
1372 if(n != sizeof(req)){
1374 printk("io_thread - read failed, fd = %d, "
1375 "err = %d\n", kernel_fd, -n);
1377 printk("io_thread - short read, fd = %d, "
1378 "length = %d\n", kernel_fd, n);
1384 n = os_write_file(kernel_fd, &req, sizeof(req));
1385 if(n != sizeof(req))
1386 printk("io_thread - write failed, fd = %d, err = %d\n",
1392 * Overrides for Emacs so that we follow Linus's tabbing style.
1393 * Emacs will notice this stuff at the end of the file and automatically
1394 * adjust the settings for this buffer only. This must remain at the end
1396 * ---------------------------------------------------------------------------
1398 * c-file-style: "linux"