3 * Licensed under the GPL
6 /* 2001-09-28...2002-04-17
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.
22 #include "linux/kernel.h"
23 #include "linux/module.h"
24 #include "linux/blkdev.h"
25 #include "linux/ata.h"
26 #include "linux/hdreg.h"
27 #include "linux/init.h"
28 #include "linux/cdrom.h"
29 #include "linux/proc_fs.h"
30 #include "linux/seq_file.h"
31 #include "linux/ctype.h"
32 #include "linux/capability.h"
34 #include "linux/slab.h"
35 #include "linux/vmalloc.h"
36 #include "linux/mutex.h"
37 #include "linux/blkpg.h"
38 #include "linux/genhd.h"
39 #include "linux/spinlock.h"
40 #include "linux/platform_device.h"
41 #include "linux/scatterlist.h"
42 #include "asm/segment.h"
43 #include "asm/uaccess.h"
45 #include "asm/types.h"
46 #include "asm/tlbflush.h"
48 #include "kern_util.h"
50 #include "mconsole_kern.h"
60 enum ubd_req { UBD_READ, UBD_WRITE };
62 struct io_thread_req {
66 unsigned long offsets[2];
67 unsigned long long offset;
71 unsigned long sector_mask;
72 unsigned long long cow_offset;
73 unsigned long bitmap_words[2];
77 static inline int ubd_test_bit(__u64 bit, unsigned char *data)
82 bits = sizeof(data[0]) * 8;
85 return (data[n] & (1 << off)) != 0;
88 static inline void ubd_set_bit(__u64 bit, unsigned char *data)
93 bits = sizeof(data[0]) * 8;
96 data[n] |= (1 << off);
98 /*End stuff from ubd_user.h*/
100 #define DRIVER_NAME "uml-blkdev"
102 static DEFINE_MUTEX(ubd_lock);
103 static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */
105 static int ubd_open(struct block_device *bdev, fmode_t mode);
106 static int ubd_release(struct gendisk *disk, fmode_t mode);
107 static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
108 unsigned int cmd, unsigned long arg);
109 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
113 static const struct block_device_operations ubd_blops = {
114 .owner = THIS_MODULE,
116 .release = ubd_release,
118 .getgeo = ubd_getgeo,
121 /* Protected by ubd_lock */
122 static int fake_major = UBD_MAJOR;
123 static struct gendisk *ubd_gendisk[MAX_DEV];
124 static struct gendisk *fake_gendisk[MAX_DEV];
126 #ifdef CONFIG_BLK_DEV_UBD_SYNC
127 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
130 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
133 static struct openflags global_openflags = OPEN_FLAGS;
136 /* backing file name */
138 /* backing file fd */
140 unsigned long *bitmap;
141 unsigned long bitmap_len;
149 struct list_head restart;
150 /* name (and fd, below) of the file opened for writing, either the
151 * backing or the cow file. */
156 struct openflags boot_openflags;
157 struct openflags openflags;
161 struct platform_device pdev;
162 struct request_queue *queue;
164 struct scatterlist sg[MAX_SG];
165 struct request *request;
166 int start_sg, end_sg;
170 #define DEFAULT_COW { \
174 .bitmap_offset = 0, \
178 #define DEFAULT_UBD { \
183 .boot_openflags = OPEN_FLAGS, \
184 .openflags = OPEN_FLAGS, \
187 .cow = DEFAULT_COW, \
188 .lock = SPIN_LOCK_UNLOCKED, \
195 /* Protected by ubd_lock */
196 static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD };
198 /* Only changed by fake_ide_setup which is a setup */
199 static int fake_ide = 0;
200 static struct proc_dir_entry *proc_ide_root = NULL;
201 static struct proc_dir_entry *proc_ide = NULL;
203 static void make_proc_ide(void)
205 proc_ide_root = proc_mkdir("ide", NULL);
206 proc_ide = proc_mkdir("ide0", proc_ide_root);
209 static int fake_ide_media_proc_show(struct seq_file *m, void *v)
211 seq_puts(m, "disk\n");
215 static int fake_ide_media_proc_open(struct inode *inode, struct file *file)
217 return single_open(file, fake_ide_media_proc_show, NULL);
220 static const struct file_operations fake_ide_media_proc_fops = {
221 .owner = THIS_MODULE,
222 .open = fake_ide_media_proc_open,
225 .release = single_release,
228 static void make_ide_entries(const char *dev_name)
230 struct proc_dir_entry *dir, *ent;
233 if(proc_ide_root == NULL) make_proc_ide();
235 dir = proc_mkdir(dev_name, proc_ide);
238 ent = proc_create("media", S_IRUGO, dir, &fake_ide_media_proc_fops);
240 snprintf(name, sizeof(name), "ide0/%s", dev_name);
241 proc_symlink(dev_name, proc_ide_root, name);
244 static int fake_ide_setup(char *str)
250 __setup("fake_ide", fake_ide_setup);
252 __uml_help(fake_ide_setup,
254 " Create ide0 entries that map onto ubd devices.\n\n"
257 static int parse_unit(char **ptr)
259 char *str = *ptr, *end;
263 n = simple_strtoul(str, &end, 0);
268 else if (('a' <= *str) && (*str <= 'z')) {
276 /* If *index_out == -1 at exit, the passed option was a general one;
277 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
278 * should not be freed on exit.
280 static int ubd_setup_common(char *str, int *index_out, char **error_out)
283 struct openflags flags = global_openflags;
287 if(index_out) *index_out = -1;
294 if(!strcmp(str, "sync")){
295 global_openflags = of_sync(global_openflags);
300 major = simple_strtoul(str, &end, 0);
301 if((*end != '\0') || (end == str)){
302 *error_out = "Didn't parse major number";
306 mutex_lock(&ubd_lock);
307 if (fake_major != UBD_MAJOR) {
308 *error_out = "Can't assign a fake major twice";
314 printk(KERN_INFO "Setting extra ubd major number to %d\n",
318 mutex_unlock(&ubd_lock);
322 n = parse_unit(&str);
324 *error_out = "Couldn't parse device number";
328 *error_out = "Device number out of range";
333 mutex_lock(&ubd_lock);
335 ubd_dev = &ubd_devs[n];
336 if(ubd_dev->file != NULL){
337 *error_out = "Device is already configured";
345 for (i = 0; i < sizeof("rscd="); i++) {
363 *error_out = "Expected '=' or flag letter "
371 *error_out = "Too many flags specified";
373 *error_out = "Missing '='";
377 backing_file = strchr(str, ',');
379 if (backing_file == NULL)
380 backing_file = strchr(str, ':');
382 if(backing_file != NULL){
384 *error_out = "Can't specify both 'd' and a cow file";
388 *backing_file = '\0';
394 ubd_dev->cow.file = backing_file;
395 ubd_dev->boot_openflags = flags;
397 mutex_unlock(&ubd_lock);
401 static int ubd_setup(char *str)
406 err = ubd_setup_common(str, NULL, &error);
408 printk(KERN_ERR "Failed to initialize device with \"%s\" : "
413 __setup("ubd", ubd_setup);
414 __uml_help(ubd_setup,
415 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
416 " This is used to associate a device with a file in the underlying\n"
417 " filesystem. When specifying two filenames, the first one is the\n"
418 " COW name and the second is the backing file name. As separator you can\n"
419 " use either a ':' or a ',': the first one allows writing things like;\n"
420 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
421 " while with a ',' the shell would not expand the 2nd '~'.\n"
422 " When using only one filename, UML will detect whether to treat it like\n"
423 " a COW file or a backing file. To override this detection, add the 'd'\n"
425 " ubd0d=BackingFile\n"
426 " Usually, there is a filesystem in the file, but \n"
427 " that's not required. Swap devices containing swap files can be\n"
428 " specified like this. Also, a file which doesn't contain a\n"
429 " filesystem can have its contents read in the virtual \n"
430 " machine by running 'dd' on the device. <n> must be in the range\n"
431 " 0 to 7. Appending an 'r' to the number will cause that device\n"
432 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
433 " an 's' will cause data to be written to disk on the host immediately.\n"
434 " 'c' will cause the device to be treated as being shared between multiple\n"
435 " UMLs and file locking will be turned off - this is appropriate for a\n"
436 " cluster filesystem and inappropriate at almost all other times.\n\n"
439 static int udb_setup(char *str)
441 printk("udb%s specified on command line is almost certainly a ubd -> "
446 __setup("udb", udb_setup);
447 __uml_help(udb_setup,
449 " This option is here solely to catch ubd -> udb typos, which can be\n"
450 " to impossible to catch visually unless you specifically look for\n"
451 " them. The only result of any option starting with 'udb' is an error\n"
452 " in the boot output.\n\n"
455 static void do_ubd_request(struct request_queue * q);
457 /* Only changed by ubd_init, which is an initcall. */
458 static int thread_fd = -1;
459 static LIST_HEAD(restart);
461 /* XXX - move this inside ubd_intr. */
462 /* Called without dev->lock held, and only in interrupt context. */
463 static void ubd_handler(void)
465 struct io_thread_req *req;
467 struct list_head *list, *next_ele;
472 n = os_read_file(thread_fd, &req,
473 sizeof(struct io_thread_req *));
474 if(n != sizeof(req)){
477 printk(KERN_ERR "spurious interrupt in ubd_handler, "
482 blk_end_request(req->req, 0, req->length);
485 reactivate_fd(thread_fd, UBD_IRQ);
487 list_for_each_safe(list, next_ele, &restart){
488 ubd = container_of(list, struct ubd, restart);
489 list_del_init(&ubd->restart);
490 spin_lock_irqsave(&ubd->lock, flags);
491 do_ubd_request(ubd->queue);
492 spin_unlock_irqrestore(&ubd->lock, flags);
496 static irqreturn_t ubd_intr(int irq, void *dev)
502 /* Only changed by ubd_init, which is an initcall. */
503 static int io_pid = -1;
505 static void kill_io_thread(void)
508 os_kill_process(io_pid, 1);
511 __uml_exitcall(kill_io_thread);
513 static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
517 file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file;
518 return os_file_size(file, size_out);
521 static int read_cow_bitmap(int fd, void *buf, int offset, int len)
525 err = os_seek_file(fd, offset);
529 err = os_read_file(fd, buf, len);
536 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
538 unsigned long modtime;
539 unsigned long long actual;
542 err = os_file_modtime(file, &modtime);
544 printk(KERN_ERR "Failed to get modification time of backing "
545 "file \"%s\", err = %d\n", file, -err);
549 err = os_file_size(file, &actual);
551 printk(KERN_ERR "Failed to get size of backing file \"%s\", "
552 "err = %d\n", file, -err);
556 if (actual != size) {
557 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
559 printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
560 "vs backing file\n", (unsigned long long) size, actual);
563 if (modtime != mtime) {
564 printk(KERN_ERR "mtime mismatch (%ld vs %ld) of COW header vs "
565 "backing file\n", mtime, modtime);
571 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
573 struct uml_stat buf1, buf2;
576 if (from_cmdline == NULL)
578 if (!strcmp(from_cmdline, from_cow))
581 err = os_stat_file(from_cmdline, &buf1);
583 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
587 err = os_stat_file(from_cow, &buf2);
589 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
593 if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
596 printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
597 "\"%s\" specified in COW header of \"%s\"\n",
598 from_cmdline, from_cow, cow);
602 static int open_ubd_file(char *file, struct openflags *openflags, int shared,
603 char **backing_file_out, int *bitmap_offset_out,
604 unsigned long *bitmap_len_out, int *data_offset_out,
608 unsigned long long size;
609 __u32 version, align;
611 int fd, err, sectorsize, asked_switch, mode = 0644;
613 fd = os_open_file(file, *openflags, mode);
615 if ((fd == -ENOENT) && (create_cow_out != NULL))
618 ((fd != -EROFS) && (fd != -EACCES)))
621 fd = os_open_file(file, *openflags, mode);
627 printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
629 err = os_lock_file(fd, openflags->w);
631 printk(KERN_ERR "Failed to lock '%s', err = %d\n",
637 /* Successful return case! */
638 if (backing_file_out == NULL)
641 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
642 &size, §orsize, &align, bitmap_offset_out);
643 if (err && (*backing_file_out != NULL)) {
644 printk(KERN_ERR "Failed to read COW header from COW file "
645 "\"%s\", errno = %d\n", file, -err);
651 asked_switch = path_requires_switch(*backing_file_out, backing_file,
654 /* Allow switching only if no mismatch. */
655 if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
657 printk(KERN_ERR "Switching backing file to '%s'\n",
659 err = write_cow_header(file, fd, *backing_file_out,
660 sectorsize, align, &size);
662 printk(KERN_ERR "Switch failed, errno = %d\n", -err);
666 *backing_file_out = backing_file;
667 err = backing_file_mismatch(*backing_file_out, size, mtime);
672 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
673 bitmap_len_out, data_offset_out);
681 static int create_cow_file(char *cow_file, char *backing_file,
682 struct openflags flags,
683 int sectorsize, int alignment, int *bitmap_offset_out,
684 unsigned long *bitmap_len_out, int *data_offset_out)
689 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
692 printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
697 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
698 bitmap_offset_out, bitmap_len_out,
707 static void ubd_close_dev(struct ubd *ubd_dev)
709 os_close_file(ubd_dev->fd);
710 if(ubd_dev->cow.file == NULL)
713 os_close_file(ubd_dev->cow.fd);
714 vfree(ubd_dev->cow.bitmap);
715 ubd_dev->cow.bitmap = NULL;
718 static int ubd_open_dev(struct ubd *ubd_dev)
720 struct openflags flags;
722 int err, create_cow, *create_ptr;
725 ubd_dev->openflags = ubd_dev->boot_openflags;
727 create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
728 back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
730 fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
731 back_ptr, &ubd_dev->cow.bitmap_offset,
732 &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
735 if((fd == -ENOENT) && create_cow){
736 fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
737 ubd_dev->openflags, 1 << 9, PAGE_SIZE,
738 &ubd_dev->cow.bitmap_offset,
739 &ubd_dev->cow.bitmap_len,
740 &ubd_dev->cow.data_offset);
742 printk(KERN_INFO "Creating \"%s\" as COW file for "
743 "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
748 printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
754 if(ubd_dev->cow.file != NULL){
755 blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
758 ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
759 if(ubd_dev->cow.bitmap == NULL){
760 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
763 flush_tlb_kernel_vm();
765 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
766 ubd_dev->cow.bitmap_offset,
767 ubd_dev->cow.bitmap_len);
771 flags = ubd_dev->openflags;
773 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
774 NULL, NULL, NULL, NULL);
775 if(err < 0) goto error;
776 ubd_dev->cow.fd = err;
780 os_close_file(ubd_dev->fd);
784 static void ubd_device_release(struct device *dev)
786 struct ubd *ubd_dev = dev_get_drvdata(dev);
788 blk_cleanup_queue(ubd_dev->queue);
789 *ubd_dev = ((struct ubd) DEFAULT_UBD);
792 static int ubd_disk_register(int major, u64 size, int unit,
793 struct gendisk **disk_out)
795 struct gendisk *disk;
797 disk = alloc_disk(1 << UBD_SHIFT);
802 disk->first_minor = unit << UBD_SHIFT;
803 disk->fops = &ubd_blops;
804 set_capacity(disk, size / 512);
805 if (major == UBD_MAJOR)
806 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
808 sprintf(disk->disk_name, "ubd_fake%d", unit);
810 /* sysfs register (not for ide fake devices) */
811 if (major == UBD_MAJOR) {
812 ubd_devs[unit].pdev.id = unit;
813 ubd_devs[unit].pdev.name = DRIVER_NAME;
814 ubd_devs[unit].pdev.dev.release = ubd_device_release;
815 dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
816 platform_device_register(&ubd_devs[unit].pdev);
817 disk->driverfs_dev = &ubd_devs[unit].pdev.dev;
820 disk->private_data = &ubd_devs[unit];
821 disk->queue = ubd_devs[unit].queue;
828 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
830 static int ubd_add(int n, char **error_out)
832 struct ubd *ubd_dev = &ubd_devs[n];
835 if(ubd_dev->file == NULL)
838 err = ubd_file_size(ubd_dev, &ubd_dev->size);
840 *error_out = "Couldn't determine size of device's file";
844 ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
846 INIT_LIST_HEAD(&ubd_dev->restart);
847 sg_init_table(ubd_dev->sg, MAX_SG);
850 ubd_dev->queue = blk_init_queue(do_ubd_request, &ubd_dev->lock);
851 if (ubd_dev->queue == NULL) {
852 *error_out = "Failed to initialize device queue";
855 ubd_dev->queue->queuedata = ubd_dev;
857 blk_queue_max_segments(ubd_dev->queue, MAX_SG);
858 err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
860 *error_out = "Failed to register device";
864 if (fake_major != UBD_MAJOR)
865 ubd_disk_register(fake_major, ubd_dev->size, n,
869 * Perhaps this should also be under the "if (fake_major)" above
870 * using the fake_disk->disk_name
873 make_ide_entries(ubd_gendisk[n]->disk_name);
880 blk_cleanup_queue(ubd_dev->queue);
884 static int ubd_config(char *str, char **error_out)
888 /* This string is possibly broken up and stored, so it's only
889 * freed if ubd_setup_common fails, or if only general options
892 str = kstrdup(str, GFP_KERNEL);
894 *error_out = "Failed to allocate memory";
898 ret = ubd_setup_common(str, &n, error_out);
907 mutex_lock(&ubd_lock);
908 ret = ubd_add(n, error_out);
910 ubd_devs[n].file = NULL;
911 mutex_unlock(&ubd_lock);
921 static int ubd_get_config(char *name, char *str, int size, char **error_out)
926 n = parse_unit(&name);
927 if((n >= MAX_DEV) || (n < 0)){
928 *error_out = "ubd_get_config : device number out of range";
932 ubd_dev = &ubd_devs[n];
933 mutex_lock(&ubd_lock);
935 if(ubd_dev->file == NULL){
936 CONFIG_CHUNK(str, size, len, "", 1);
940 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
942 if(ubd_dev->cow.file != NULL){
943 CONFIG_CHUNK(str, size, len, ",", 0);
944 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
946 else CONFIG_CHUNK(str, size, len, "", 1);
949 mutex_unlock(&ubd_lock);
953 static int ubd_id(char **str, int *start_out, int *end_out)
959 *end_out = MAX_DEV - 1;
963 static int ubd_remove(int n, char **error_out)
965 struct gendisk *disk = ubd_gendisk[n];
969 mutex_lock(&ubd_lock);
971 ubd_dev = &ubd_devs[n];
973 if(ubd_dev->file == NULL)
976 /* you cannot remove a open disk */
978 if(ubd_dev->count > 0)
981 ubd_gendisk[n] = NULL;
987 if(fake_gendisk[n] != NULL){
988 del_gendisk(fake_gendisk[n]);
989 put_disk(fake_gendisk[n]);
990 fake_gendisk[n] = NULL;
994 platform_device_unregister(&ubd_dev->pdev);
996 mutex_unlock(&ubd_lock);
1000 /* All these are called by mconsole in process context and without
1001 * ubd-specific locks. The structure itself is const except for .list.
1003 static struct mc_device ubd_mc = {
1004 .list = LIST_HEAD_INIT(ubd_mc.list),
1006 .config = ubd_config,
1007 .get_config = ubd_get_config,
1009 .remove = ubd_remove,
1012 static int __init ubd_mc_init(void)
1014 mconsole_register_dev(&ubd_mc);
1018 __initcall(ubd_mc_init);
1020 static int __init ubd0_init(void)
1022 struct ubd *ubd_dev = &ubd_devs[0];
1024 mutex_lock(&ubd_lock);
1025 if(ubd_dev->file == NULL)
1026 ubd_dev->file = "root_fs";
1027 mutex_unlock(&ubd_lock);
1032 __initcall(ubd0_init);
1034 /* Used in ubd_init, which is an initcall */
1035 static struct platform_driver ubd_driver = {
1037 .name = DRIVER_NAME,
1041 static int __init ubd_init(void)
1046 if (register_blkdev(UBD_MAJOR, "ubd"))
1049 if (fake_major != UBD_MAJOR) {
1050 char name[sizeof("ubd_nnn\0")];
1052 snprintf(name, sizeof(name), "ubd_%d", fake_major);
1053 if (register_blkdev(fake_major, "ubd"))
1056 platform_driver_register(&ubd_driver);
1057 mutex_lock(&ubd_lock);
1058 for (i = 0; i < MAX_DEV; i++){
1059 err = ubd_add(i, &error);
1061 printk(KERN_ERR "Failed to initialize ubd device %d :"
1064 mutex_unlock(&ubd_lock);
1068 late_initcall(ubd_init);
1070 static int __init ubd_driver_init(void){
1071 unsigned long stack;
1074 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1075 if(global_openflags.s){
1076 printk(KERN_INFO "ubd: Synchronous mode\n");
1077 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1078 * enough. So use anyway the io thread. */
1080 stack = alloc_stack(0, 0);
1081 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
1085 "ubd : Failed to start I/O thread (errno = %d) - "
1086 "falling back to synchronous I/O\n", -io_pid);
1090 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
1091 IRQF_DISABLED, "ubd", ubd_devs);
1093 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
1097 device_initcall(ubd_driver_init);
1099 static int ubd_open(struct block_device *bdev, fmode_t mode)
1101 struct gendisk *disk = bdev->bd_disk;
1102 struct ubd *ubd_dev = disk->private_data;
1105 mutex_lock(&ubd_mutex);
1106 if(ubd_dev->count == 0){
1107 err = ubd_open_dev(ubd_dev);
1109 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
1110 disk->disk_name, ubd_dev->file, -err);
1115 set_disk_ro(disk, !ubd_dev->openflags.w);
1117 /* This should no more be needed. And it didn't work anyway to exclude
1118 * read-write remounting of filesystems.*/
1119 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1120 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1124 mutex_unlock(&ubd_mutex);
1128 static int ubd_release(struct gendisk *disk, fmode_t mode)
1130 struct ubd *ubd_dev = disk->private_data;
1132 mutex_lock(&ubd_mutex);
1133 if(--ubd_dev->count == 0)
1134 ubd_close_dev(ubd_dev);
1135 mutex_unlock(&ubd_mutex);
1139 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
1140 __u64 *cow_offset, unsigned long *bitmap,
1141 __u64 bitmap_offset, unsigned long *bitmap_words,
1144 __u64 sector = io_offset >> 9;
1145 int i, update_bitmap = 0;
1147 for(i = 0; i < length >> 9; i++){
1148 if(cow_mask != NULL)
1149 ubd_set_bit(i, (unsigned char *) cow_mask);
1150 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1154 ubd_set_bit(sector + i, (unsigned char *) bitmap);
1160 *cow_offset = sector / (sizeof(unsigned long) * 8);
1162 /* This takes care of the case where we're exactly at the end of the
1163 * device, and *cow_offset + 1 is off the end. So, just back it up
1164 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1165 * for the original diagnosis.
1167 if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
1168 sizeof(unsigned long)) - 1))
1171 bitmap_words[0] = bitmap[*cow_offset];
1172 bitmap_words[1] = bitmap[*cow_offset + 1];
1174 *cow_offset *= sizeof(unsigned long);
1175 *cow_offset += bitmap_offset;
1178 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
1179 __u64 bitmap_offset, __u64 bitmap_len)
1181 __u64 sector = req->offset >> 9;
1184 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
1185 panic("Operation too long");
1187 if(req->op == UBD_READ) {
1188 for(i = 0; i < req->length >> 9; i++){
1189 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1190 ubd_set_bit(i, (unsigned char *)
1194 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
1195 &req->cow_offset, bitmap, bitmap_offset,
1196 req->bitmap_words, bitmap_len);
1199 /* Called with dev->lock held */
1200 static void prepare_request(struct request *req, struct io_thread_req *io_req,
1201 unsigned long long offset, int page_offset,
1202 int len, struct page *page)
1204 struct gendisk *disk = req->rq_disk;
1205 struct ubd *ubd_dev = disk->private_data;
1208 io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd :
1210 io_req->fds[1] = ubd_dev->fd;
1211 io_req->cow_offset = -1;
1212 io_req->offset = offset;
1213 io_req->length = len;
1215 io_req->sector_mask = 0;
1217 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1218 io_req->offsets[0] = 0;
1219 io_req->offsets[1] = ubd_dev->cow.data_offset;
1220 io_req->buffer = page_address(page) + page_offset;
1221 io_req->sectorsize = 1 << 9;
1223 if(ubd_dev->cow.file != NULL)
1224 cowify_req(io_req, ubd_dev->cow.bitmap,
1225 ubd_dev->cow.bitmap_offset, ubd_dev->cow.bitmap_len);
1229 /* Called with dev->lock held */
1230 static void do_ubd_request(struct request_queue *q)
1232 struct io_thread_req *io_req;
1233 struct request *req;
1237 struct ubd *dev = q->queuedata;
1238 if(dev->end_sg == 0){
1239 struct request *req = blk_fetch_request(q);
1244 dev->rq_pos = blk_rq_pos(req);
1246 dev->end_sg = blk_rq_map_sg(q, req, dev->sg);
1250 while(dev->start_sg < dev->end_sg){
1251 struct scatterlist *sg = &dev->sg[dev->start_sg];
1253 io_req = kmalloc(sizeof(struct io_thread_req),
1256 if(list_empty(&dev->restart))
1257 list_add(&dev->restart, &restart);
1260 prepare_request(req, io_req,
1261 (unsigned long long)dev->rq_pos << 9,
1262 sg->offset, sg->length, sg_page(sg));
1264 n = os_write_file(thread_fd, &io_req,
1265 sizeof(struct io_thread_req *));
1266 if(n != sizeof(struct io_thread_req *)){
1268 printk("write to io thread failed, "
1269 "errno = %d\n", -n);
1270 else if(list_empty(&dev->restart))
1271 list_add(&dev->restart, &restart);
1276 dev->rq_pos += sg->length >> 9;
1280 dev->request = NULL;
1284 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1286 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1290 geo->cylinders = ubd_dev->size / (128 * 32 * 512);
1294 static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
1295 unsigned int cmd, unsigned long arg)
1297 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1298 u16 ubd_id[ATA_ID_WORDS];
1301 struct cdrom_volctrl volume;
1302 case HDIO_GET_IDENTITY:
1303 memset(&ubd_id, 0, ATA_ID_WORDS * 2);
1304 ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512);
1305 ubd_id[ATA_ID_HEADS] = 128;
1306 ubd_id[ATA_ID_SECTORS] = 32;
1307 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1313 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1315 volume.channel0 = 255;
1316 volume.channel1 = 255;
1317 volume.channel2 = 255;
1318 volume.channel3 = 255;
1319 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1326 static int update_bitmap(struct io_thread_req *req)
1330 if(req->cow_offset == -1)
1333 n = os_seek_file(req->fds[1], req->cow_offset);
1335 printk("do_io - bitmap lseek failed : err = %d\n", -n);
1339 n = os_write_file(req->fds[1], &req->bitmap_words,
1340 sizeof(req->bitmap_words));
1341 if(n != sizeof(req->bitmap_words)){
1342 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
1350 static void do_io(struct io_thread_req *req)
1354 int n, nsectors, start, end, bit;
1358 nsectors = req->length / req->sectorsize;
1361 bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
1363 while((end < nsectors) &&
1364 (ubd_test_bit(end, (unsigned char *)
1365 &req->sector_mask) == bit))
1368 off = req->offset + req->offsets[bit] +
1369 start * req->sectorsize;
1370 len = (end - start) * req->sectorsize;
1371 buf = &req->buffer[start * req->sectorsize];
1373 err = os_seek_file(req->fds[bit], off);
1375 printk("do_io - lseek failed : err = %d\n", -err);
1379 if(req->op == UBD_READ){
1384 n = os_read_file(req->fds[bit], buf, len);
1386 printk("do_io - read failed, err = %d "
1387 "fd = %d\n", -n, req->fds[bit]);
1391 } while((n < len) && (n != 0));
1392 if (n < len) memset(&buf[n], 0, len - n);
1394 n = os_write_file(req->fds[bit], buf, len);
1396 printk("do_io - write failed err = %d "
1397 "fd = %d\n", -n, req->fds[bit]);
1404 } while(start < nsectors);
1406 req->error = update_bitmap(req);
1409 /* Changed in start_io_thread, which is serialized by being called only
1410 * from ubd_init, which is an initcall.
1414 /* Only changed by the io thread. XXX: currently unused. */
1415 static int io_count = 0;
1417 int io_thread(void *arg)
1419 struct io_thread_req *req;
1422 ignore_sigwinch_sig();
1424 n = os_read_file(kernel_fd, &req,
1425 sizeof(struct io_thread_req *));
1426 if(n != sizeof(struct io_thread_req *)){
1428 printk("io_thread - read failed, fd = %d, "
1429 "err = %d\n", kernel_fd, -n);
1431 printk("io_thread - short read, fd = %d, "
1432 "length = %d\n", kernel_fd, n);
1438 n = os_write_file(kernel_fd, &req,
1439 sizeof(struct io_thread_req *));
1440 if(n != sizeof(struct io_thread_req *))
1441 printk("io_thread - write failed, fd = %d, err = %d\n",