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.
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/blkpg.h"
37 #include "linux/genhd.h"
38 #include "linux/spinlock.h"
39 #include "linux/platform_device.h"
40 #include "linux/scatterlist.h"
41 #include "asm/segment.h"
42 #include "asm/uaccess.h"
44 #include "asm/types.h"
45 #include "asm/tlbflush.h"
47 #include "kern_util.h"
49 #include "mconsole_kern.h"
59 enum ubd_req
{ UBD_READ
, UBD_WRITE
};
61 struct io_thread_req
{
65 unsigned long offsets
[2];
66 unsigned long long offset
;
70 unsigned long sector_mask
;
71 unsigned long long cow_offset
;
72 unsigned long bitmap_words
[2];
76 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
81 bits
= sizeof(data
[0]) * 8;
84 return (data
[n
] & (1 << off
)) != 0;
87 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
92 bits
= sizeof(data
[0]) * 8;
95 data
[n
] |= (1 << off
);
97 /*End stuff from ubd_user.h*/
99 #define DRIVER_NAME "uml-blkdev"
101 static DEFINE_MUTEX(ubd_lock
);
103 static int ubd_open(struct block_device
*bdev
, fmode_t mode
);
104 static int ubd_release(struct gendisk
*disk
, fmode_t mode
);
105 static int ubd_ioctl(struct block_device
*bdev
, fmode_t mode
,
106 unsigned int cmd
, unsigned long arg
);
107 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
);
111 static const struct block_device_operations ubd_blops
= {
112 .owner
= THIS_MODULE
,
114 .release
= ubd_release
,
116 .getgeo
= ubd_getgeo
,
119 /* Protected by ubd_lock */
120 static int fake_major
= UBD_MAJOR
;
121 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
122 static struct gendisk
*fake_gendisk
[MAX_DEV
];
124 #ifdef CONFIG_BLK_DEV_UBD_SYNC
125 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
128 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
131 static struct openflags global_openflags
= OPEN_FLAGS
;
134 /* backing file name */
136 /* backing file fd */
138 unsigned long *bitmap
;
139 unsigned long bitmap_len
;
147 struct list_head restart
;
148 /* name (and fd, below) of the file opened for writing, either the
149 * backing or the cow file. */
154 struct openflags boot_openflags
;
155 struct openflags openflags
;
159 struct platform_device pdev
;
160 struct request_queue
*queue
;
162 struct scatterlist sg
[MAX_SG
];
163 struct request
*request
;
164 int start_sg
, end_sg
;
167 #define DEFAULT_COW { \
171 .bitmap_offset = 0, \
175 #define DEFAULT_UBD { \
180 .boot_openflags = OPEN_FLAGS, \
181 .openflags = OPEN_FLAGS, \
184 .cow = DEFAULT_COW, \
185 .lock = SPIN_LOCK_UNLOCKED, \
191 /* Protected by ubd_lock */
192 static struct ubd ubd_devs
[MAX_DEV
] = { [0 ... MAX_DEV
- 1] = DEFAULT_UBD
};
194 /* Only changed by fake_ide_setup which is a setup */
195 static int fake_ide
= 0;
196 static struct proc_dir_entry
*proc_ide_root
= NULL
;
197 static struct proc_dir_entry
*proc_ide
= NULL
;
199 static void make_proc_ide(void)
201 proc_ide_root
= proc_mkdir("ide", NULL
);
202 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
205 static int fake_ide_media_proc_show(struct seq_file
*m
, void *v
)
207 seq_puts(m
, "disk\n");
211 static int fake_ide_media_proc_open(struct inode
*inode
, struct file
*file
)
213 return single_open(file
, fake_ide_media_proc_show
, NULL
);
216 static const struct file_operations fake_ide_media_proc_fops
= {
217 .owner
= THIS_MODULE
,
218 .open
= fake_ide_media_proc_open
,
221 .release
= single_release
,
224 static void make_ide_entries(const char *dev_name
)
226 struct proc_dir_entry
*dir
, *ent
;
229 if(proc_ide_root
== NULL
) make_proc_ide();
231 dir
= proc_mkdir(dev_name
, proc_ide
);
234 ent
= proc_create("media", S_IRUGO
, dir
, &fake_ide_media_proc_fops
);
236 snprintf(name
, sizeof(name
), "ide0/%s", dev_name
);
237 proc_symlink(dev_name
, proc_ide_root
, name
);
240 static int fake_ide_setup(char *str
)
246 __setup("fake_ide", fake_ide_setup
);
248 __uml_help(fake_ide_setup
,
250 " Create ide0 entries that map onto ubd devices.\n\n"
253 static int parse_unit(char **ptr
)
255 char *str
= *ptr
, *end
;
259 n
= simple_strtoul(str
, &end
, 0);
264 else if (('a' <= *str
) && (*str
<= 'z')) {
272 /* If *index_out == -1 at exit, the passed option was a general one;
273 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
274 * should not be freed on exit.
276 static int ubd_setup_common(char *str
, int *index_out
, char **error_out
)
279 struct openflags flags
= global_openflags
;
283 if(index_out
) *index_out
= -1;
290 if(!strcmp(str
, "sync")){
291 global_openflags
= of_sync(global_openflags
);
296 major
= simple_strtoul(str
, &end
, 0);
297 if((*end
!= '\0') || (end
== str
)){
298 *error_out
= "Didn't parse major number";
302 mutex_lock(&ubd_lock
);
303 if (fake_major
!= UBD_MAJOR
) {
304 *error_out
= "Can't assign a fake major twice";
310 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
314 mutex_unlock(&ubd_lock
);
318 n
= parse_unit(&str
);
320 *error_out
= "Couldn't parse device number";
324 *error_out
= "Device number out of range";
329 mutex_lock(&ubd_lock
);
331 ubd_dev
= &ubd_devs
[n
];
332 if(ubd_dev
->file
!= NULL
){
333 *error_out
= "Device is already configured";
341 for (i
= 0; i
< sizeof("rscd="); i
++) {
359 *error_out
= "Expected '=' or flag letter "
367 *error_out
= "Too many flags specified";
369 *error_out
= "Missing '='";
373 backing_file
= strchr(str
, ',');
375 if (backing_file
== NULL
)
376 backing_file
= strchr(str
, ':');
378 if(backing_file
!= NULL
){
380 *error_out
= "Can't specify both 'd' and a cow file";
384 *backing_file
= '\0';
390 ubd_dev
->cow
.file
= backing_file
;
391 ubd_dev
->boot_openflags
= flags
;
393 mutex_unlock(&ubd_lock
);
397 static int ubd_setup(char *str
)
402 err
= ubd_setup_common(str
, NULL
, &error
);
404 printk(KERN_ERR
"Failed to initialize device with \"%s\" : "
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 treat 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"
430 " 'c' will cause the device to be treated as being shared between multiple\n"
431 " UMLs and file locking will be turned off - this is appropriate for a\n"
432 " cluster filesystem and inappropriate at almost all other times.\n\n"
435 static int udb_setup(char *str
)
437 printk("udb%s specified on command line is almost certainly a ubd -> "
442 __setup("udb", udb_setup
);
443 __uml_help(udb_setup
,
445 " This option is here solely to catch ubd -> udb typos, which can be\n"
446 " to impossible to catch visually unless you specifically look for\n"
447 " them. The only result of any option starting with 'udb' is an error\n"
448 " in the boot output.\n\n"
451 static void do_ubd_request(struct request_queue
* q
);
453 /* Only changed by ubd_init, which is an initcall. */
454 static int thread_fd
= -1;
455 static LIST_HEAD(restart
);
457 /* XXX - move this inside ubd_intr. */
458 /* Called without dev->lock held, and only in interrupt context. */
459 static void ubd_handler(void)
461 struct io_thread_req
*req
;
463 struct list_head
*list
, *next_ele
;
468 n
= os_read_file(thread_fd
, &req
,
469 sizeof(struct io_thread_req
*));
470 if(n
!= sizeof(req
)){
473 printk(KERN_ERR
"spurious interrupt in ubd_handler, "
478 blk_end_request(req
->req
, 0, req
->length
);
481 reactivate_fd(thread_fd
, UBD_IRQ
);
483 list_for_each_safe(list
, next_ele
, &restart
){
484 ubd
= container_of(list
, struct ubd
, restart
);
485 list_del_init(&ubd
->restart
);
486 spin_lock_irqsave(&ubd
->lock
, flags
);
487 do_ubd_request(ubd
->queue
);
488 spin_unlock_irqrestore(&ubd
->lock
, flags
);
492 static irqreturn_t
ubd_intr(int irq
, void *dev
)
498 /* Only changed by ubd_init, which is an initcall. */
499 static int io_pid
= -1;
501 static void kill_io_thread(void)
504 os_kill_process(io_pid
, 1);
507 __uml_exitcall(kill_io_thread
);
509 static inline int ubd_file_size(struct ubd
*ubd_dev
, __u64
*size_out
)
513 file
= ubd_dev
->cow
.file
? ubd_dev
->cow
.file
: ubd_dev
->file
;
514 return os_file_size(file
, size_out
);
517 static int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
521 err
= os_seek_file(fd
, offset
);
525 err
= os_read_file(fd
, buf
, len
);
532 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
534 unsigned long modtime
;
535 unsigned long long actual
;
538 err
= os_file_modtime(file
, &modtime
);
540 printk(KERN_ERR
"Failed to get modification time of backing "
541 "file \"%s\", err = %d\n", file
, -err
);
545 err
= os_file_size(file
, &actual
);
547 printk(KERN_ERR
"Failed to get size of backing file \"%s\", "
548 "err = %d\n", file
, -err
);
552 if (actual
!= size
) {
553 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
555 printk(KERN_ERR
"Size mismatch (%llu vs %llu) of COW header "
556 "vs backing file\n", (unsigned long long) size
, actual
);
559 if (modtime
!= mtime
) {
560 printk(KERN_ERR
"mtime mismatch (%ld vs %ld) of COW header vs "
561 "backing file\n", mtime
, modtime
);
567 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
569 struct uml_stat buf1
, buf2
;
572 if (from_cmdline
== NULL
)
574 if (!strcmp(from_cmdline
, from_cow
))
577 err
= os_stat_file(from_cmdline
, &buf1
);
579 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cmdline
,
583 err
= os_stat_file(from_cow
, &buf2
);
585 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cow
,
589 if ((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
592 printk(KERN_ERR
"Backing file mismatch - \"%s\" requested, "
593 "\"%s\" specified in COW header of \"%s\"\n",
594 from_cmdline
, from_cow
, cow
);
598 static int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
599 char **backing_file_out
, int *bitmap_offset_out
,
600 unsigned long *bitmap_len_out
, int *data_offset_out
,
604 unsigned long long size
;
605 __u32 version
, align
;
607 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
609 fd
= os_open_file(file
, *openflags
, mode
);
611 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
614 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
617 fd
= os_open_file(file
, *openflags
, mode
);
623 printk(KERN_INFO
"Not locking \"%s\" on the host\n", file
);
625 err
= os_lock_file(fd
, openflags
->w
);
627 printk(KERN_ERR
"Failed to lock '%s', err = %d\n",
633 /* Successful return case! */
634 if (backing_file_out
== NULL
)
637 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
638 &size
, §orsize
, &align
, bitmap_offset_out
);
639 if (err
&& (*backing_file_out
!= NULL
)) {
640 printk(KERN_ERR
"Failed to read COW header from COW file "
641 "\"%s\", errno = %d\n", file
, -err
);
647 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
,
650 /* Allow switching only if no mismatch. */
651 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
,
653 printk(KERN_ERR
"Switching backing file to '%s'\n",
655 err
= write_cow_header(file
, fd
, *backing_file_out
,
656 sectorsize
, align
, &size
);
658 printk(KERN_ERR
"Switch failed, errno = %d\n", -err
);
662 *backing_file_out
= backing_file
;
663 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
668 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
669 bitmap_len_out
, data_offset_out
);
677 static int create_cow_file(char *cow_file
, char *backing_file
,
678 struct openflags flags
,
679 int sectorsize
, int alignment
, int *bitmap_offset_out
,
680 unsigned long *bitmap_len_out
, int *data_offset_out
)
685 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
688 printk(KERN_ERR
"Open of COW file '%s' failed, errno = %d\n",
693 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
694 bitmap_offset_out
, bitmap_len_out
,
703 static void ubd_close_dev(struct ubd
*ubd_dev
)
705 os_close_file(ubd_dev
->fd
);
706 if(ubd_dev
->cow
.file
== NULL
)
709 os_close_file(ubd_dev
->cow
.fd
);
710 vfree(ubd_dev
->cow
.bitmap
);
711 ubd_dev
->cow
.bitmap
= NULL
;
714 static int ubd_open_dev(struct ubd
*ubd_dev
)
716 struct openflags flags
;
718 int err
, create_cow
, *create_ptr
;
721 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
723 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
724 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
726 fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
727 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
728 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
731 if((fd
== -ENOENT
) && create_cow
){
732 fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
733 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
734 &ubd_dev
->cow
.bitmap_offset
,
735 &ubd_dev
->cow
.bitmap_len
,
736 &ubd_dev
->cow
.data_offset
);
738 printk(KERN_INFO
"Creating \"%s\" as COW file for "
739 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
744 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
750 if(ubd_dev
->cow
.file
!= NULL
){
751 blk_queue_max_hw_sectors(ubd_dev
->queue
, 8 * sizeof(long));
754 ubd_dev
->cow
.bitmap
= vmalloc(ubd_dev
->cow
.bitmap_len
);
755 if(ubd_dev
->cow
.bitmap
== NULL
){
756 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
759 flush_tlb_kernel_vm();
761 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
762 ubd_dev
->cow
.bitmap_offset
,
763 ubd_dev
->cow
.bitmap_len
);
767 flags
= ubd_dev
->openflags
;
769 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
770 NULL
, NULL
, NULL
, NULL
);
771 if(err
< 0) goto error
;
772 ubd_dev
->cow
.fd
= err
;
776 os_close_file(ubd_dev
->fd
);
780 static void ubd_device_release(struct device
*dev
)
782 struct ubd
*ubd_dev
= dev_get_drvdata(dev
);
784 blk_cleanup_queue(ubd_dev
->queue
);
785 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
788 static int ubd_disk_register(int major
, u64 size
, int unit
,
789 struct gendisk
**disk_out
)
791 struct gendisk
*disk
;
793 disk
= alloc_disk(1 << UBD_SHIFT
);
798 disk
->first_minor
= unit
<< UBD_SHIFT
;
799 disk
->fops
= &ubd_blops
;
800 set_capacity(disk
, size
/ 512);
801 if (major
== UBD_MAJOR
)
802 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
804 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
806 /* sysfs register (not for ide fake devices) */
807 if (major
== UBD_MAJOR
) {
808 ubd_devs
[unit
].pdev
.id
= unit
;
809 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
810 ubd_devs
[unit
].pdev
.dev
.release
= ubd_device_release
;
811 dev_set_drvdata(&ubd_devs
[unit
].pdev
.dev
, &ubd_devs
[unit
]);
812 platform_device_register(&ubd_devs
[unit
].pdev
);
813 disk
->driverfs_dev
= &ubd_devs
[unit
].pdev
.dev
;
816 disk
->private_data
= &ubd_devs
[unit
];
817 disk
->queue
= ubd_devs
[unit
].queue
;
824 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
826 static int ubd_add(int n
, char **error_out
)
828 struct ubd
*ubd_dev
= &ubd_devs
[n
];
831 if(ubd_dev
->file
== NULL
)
834 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
836 *error_out
= "Couldn't determine size of device's file";
840 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
842 INIT_LIST_HEAD(&ubd_dev
->restart
);
843 sg_init_table(ubd_dev
->sg
, MAX_SG
);
846 ubd_dev
->queue
= blk_init_queue(do_ubd_request
, &ubd_dev
->lock
);
847 if (ubd_dev
->queue
== NULL
) {
848 *error_out
= "Failed to initialize device queue";
851 ubd_dev
->queue
->queuedata
= ubd_dev
;
853 blk_queue_max_segments(ubd_dev
->queue
, MAX_SG
);
854 err
= ubd_disk_register(UBD_MAJOR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
856 *error_out
= "Failed to register device";
860 if (fake_major
!= UBD_MAJOR
)
861 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
865 * Perhaps this should also be under the "if (fake_major)" above
866 * using the fake_disk->disk_name
869 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
876 blk_cleanup_queue(ubd_dev
->queue
);
880 static int ubd_config(char *str
, char **error_out
)
884 /* This string is possibly broken up and stored, so it's only
885 * freed if ubd_setup_common fails, or if only general options
888 str
= kstrdup(str
, GFP_KERNEL
);
890 *error_out
= "Failed to allocate memory";
894 ret
= ubd_setup_common(str
, &n
, error_out
);
903 mutex_lock(&ubd_lock
);
904 ret
= ubd_add(n
, error_out
);
906 ubd_devs
[n
].file
= NULL
;
907 mutex_unlock(&ubd_lock
);
917 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
922 n
= parse_unit(&name
);
923 if((n
>= MAX_DEV
) || (n
< 0)){
924 *error_out
= "ubd_get_config : device number out of range";
928 ubd_dev
= &ubd_devs
[n
];
929 mutex_lock(&ubd_lock
);
931 if(ubd_dev
->file
== NULL
){
932 CONFIG_CHUNK(str
, size
, len
, "", 1);
936 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
938 if(ubd_dev
->cow
.file
!= NULL
){
939 CONFIG_CHUNK(str
, size
, len
, ",", 0);
940 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
942 else CONFIG_CHUNK(str
, size
, len
, "", 1);
945 mutex_unlock(&ubd_lock
);
949 static int ubd_id(char **str
, int *start_out
, int *end_out
)
955 *end_out
= MAX_DEV
- 1;
959 static int ubd_remove(int n
, char **error_out
)
961 struct gendisk
*disk
= ubd_gendisk
[n
];
965 mutex_lock(&ubd_lock
);
967 ubd_dev
= &ubd_devs
[n
];
969 if(ubd_dev
->file
== NULL
)
972 /* you cannot remove a open disk */
974 if(ubd_dev
->count
> 0)
977 ubd_gendisk
[n
] = NULL
;
983 if(fake_gendisk
[n
] != NULL
){
984 del_gendisk(fake_gendisk
[n
]);
985 put_disk(fake_gendisk
[n
]);
986 fake_gendisk
[n
] = NULL
;
990 platform_device_unregister(&ubd_dev
->pdev
);
992 mutex_unlock(&ubd_lock
);
996 /* All these are called by mconsole in process context and without
997 * ubd-specific locks. The structure itself is const except for .list.
999 static struct mc_device ubd_mc
= {
1000 .list
= LIST_HEAD_INIT(ubd_mc
.list
),
1002 .config
= ubd_config
,
1003 .get_config
= ubd_get_config
,
1005 .remove
= ubd_remove
,
1008 static int __init
ubd_mc_init(void)
1010 mconsole_register_dev(&ubd_mc
);
1014 __initcall(ubd_mc_init
);
1016 static int __init
ubd0_init(void)
1018 struct ubd
*ubd_dev
= &ubd_devs
[0];
1020 mutex_lock(&ubd_lock
);
1021 if(ubd_dev
->file
== NULL
)
1022 ubd_dev
->file
= "root_fs";
1023 mutex_unlock(&ubd_lock
);
1028 __initcall(ubd0_init
);
1030 /* Used in ubd_init, which is an initcall */
1031 static struct platform_driver ubd_driver
= {
1033 .name
= DRIVER_NAME
,
1037 static int __init
ubd_init(void)
1042 if (register_blkdev(UBD_MAJOR
, "ubd"))
1045 if (fake_major
!= UBD_MAJOR
) {
1046 char name
[sizeof("ubd_nnn\0")];
1048 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
1049 if (register_blkdev(fake_major
, "ubd"))
1052 platform_driver_register(&ubd_driver
);
1053 mutex_lock(&ubd_lock
);
1054 for (i
= 0; i
< MAX_DEV
; i
++){
1055 err
= ubd_add(i
, &error
);
1057 printk(KERN_ERR
"Failed to initialize ubd device %d :"
1060 mutex_unlock(&ubd_lock
);
1064 late_initcall(ubd_init
);
1066 static int __init
ubd_driver_init(void){
1067 unsigned long stack
;
1070 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1071 if(global_openflags
.s
){
1072 printk(KERN_INFO
"ubd: Synchronous mode\n");
1073 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1074 * enough. So use anyway the io thread. */
1076 stack
= alloc_stack(0, 0);
1077 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
1081 "ubd : Failed to start I/O thread (errno = %d) - "
1082 "falling back to synchronous I/O\n", -io_pid
);
1086 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
1087 IRQF_DISABLED
, "ubd", ubd_devs
);
1089 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
1093 device_initcall(ubd_driver_init
);
1095 static int ubd_open(struct block_device
*bdev
, fmode_t mode
)
1097 struct gendisk
*disk
= bdev
->bd_disk
;
1098 struct ubd
*ubd_dev
= disk
->private_data
;
1101 if(ubd_dev
->count
== 0){
1102 err
= ubd_open_dev(ubd_dev
);
1104 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
1105 disk
->disk_name
, ubd_dev
->file
, -err
);
1110 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
1112 /* This should no more be needed. And it didn't work anyway to exclude
1113 * read-write remounting of filesystems.*/
1114 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1115 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1122 static int ubd_release(struct gendisk
*disk
, fmode_t mode
)
1124 struct ubd
*ubd_dev
= disk
->private_data
;
1126 if(--ubd_dev
->count
== 0)
1127 ubd_close_dev(ubd_dev
);
1131 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
1132 __u64
*cow_offset
, unsigned long *bitmap
,
1133 __u64 bitmap_offset
, unsigned long *bitmap_words
,
1136 __u64 sector
= io_offset
>> 9;
1137 int i
, update_bitmap
= 0;
1139 for(i
= 0; i
< length
>> 9; i
++){
1140 if(cow_mask
!= NULL
)
1141 ubd_set_bit(i
, (unsigned char *) cow_mask
);
1142 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1146 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
1152 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
1154 /* This takes care of the case where we're exactly at the end of the
1155 * device, and *cow_offset + 1 is off the end. So, just back it up
1156 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1157 * for the original diagnosis.
1159 if (*cow_offset
== (DIV_ROUND_UP(bitmap_len
,
1160 sizeof(unsigned long)) - 1))
1163 bitmap_words
[0] = bitmap
[*cow_offset
];
1164 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
1166 *cow_offset
*= sizeof(unsigned long);
1167 *cow_offset
+= bitmap_offset
;
1170 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
1171 __u64 bitmap_offset
, __u64 bitmap_len
)
1173 __u64 sector
= req
->offset
>> 9;
1176 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
1177 panic("Operation too long");
1179 if(req
->op
== UBD_READ
) {
1180 for(i
= 0; i
< req
->length
>> 9; i
++){
1181 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1182 ubd_set_bit(i
, (unsigned char *)
1186 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1187 &req
->cow_offset
, bitmap
, bitmap_offset
,
1188 req
->bitmap_words
, bitmap_len
);
1191 /* Called with dev->lock held */
1192 static void prepare_request(struct request
*req
, struct io_thread_req
*io_req
,
1193 unsigned long long offset
, int page_offset
,
1194 int len
, struct page
*page
)
1196 struct gendisk
*disk
= req
->rq_disk
;
1197 struct ubd
*ubd_dev
= disk
->private_data
;
1200 io_req
->fds
[0] = (ubd_dev
->cow
.file
!= NULL
) ? ubd_dev
->cow
.fd
:
1202 io_req
->fds
[1] = ubd_dev
->fd
;
1203 io_req
->cow_offset
= -1;
1204 io_req
->offset
= offset
;
1205 io_req
->length
= len
;
1207 io_req
->sector_mask
= 0;
1209 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1210 io_req
->offsets
[0] = 0;
1211 io_req
->offsets
[1] = ubd_dev
->cow
.data_offset
;
1212 io_req
->buffer
= page_address(page
) + page_offset
;
1213 io_req
->sectorsize
= 1 << 9;
1215 if(ubd_dev
->cow
.file
!= NULL
)
1216 cowify_req(io_req
, ubd_dev
->cow
.bitmap
,
1217 ubd_dev
->cow
.bitmap_offset
, ubd_dev
->cow
.bitmap_len
);
1221 /* Called with dev->lock held */
1222 static void do_ubd_request(struct request_queue
*q
)
1224 struct io_thread_req
*io_req
;
1225 struct request
*req
;
1230 struct ubd
*dev
= q
->queuedata
;
1231 if(dev
->end_sg
== 0){
1232 struct request
*req
= blk_fetch_request(q
);
1238 dev
->end_sg
= blk_rq_map_sg(q
, req
, dev
->sg
);
1242 sector
= blk_rq_pos(req
);
1243 while(dev
->start_sg
< dev
->end_sg
){
1244 struct scatterlist
*sg
= &dev
->sg
[dev
->start_sg
];
1246 io_req
= kmalloc(sizeof(struct io_thread_req
),
1249 if(list_empty(&dev
->restart
))
1250 list_add(&dev
->restart
, &restart
);
1253 prepare_request(req
, io_req
,
1254 (unsigned long long)sector
<< 9,
1255 sg
->offset
, sg
->length
, sg_page(sg
));
1257 sector
+= sg
->length
>> 9;
1258 n
= os_write_file(thread_fd
, &io_req
,
1259 sizeof(struct io_thread_req
*));
1260 if(n
!= sizeof(struct io_thread_req
*)){
1262 printk("write to io thread failed, "
1263 "errno = %d\n", -n
);
1264 else if(list_empty(&dev
->restart
))
1265 list_add(&dev
->restart
, &restart
);
1273 dev
->request
= NULL
;
1277 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1279 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1283 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1287 static int ubd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1288 unsigned int cmd
, unsigned long arg
)
1290 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1291 u16 ubd_id
[ATA_ID_WORDS
];
1294 struct cdrom_volctrl volume
;
1295 case HDIO_GET_IDENTITY
:
1296 memset(&ubd_id
, 0, ATA_ID_WORDS
* 2);
1297 ubd_id
[ATA_ID_CYLS
] = ubd_dev
->size
/ (128 * 32 * 512);
1298 ubd_id
[ATA_ID_HEADS
] = 128;
1299 ubd_id
[ATA_ID_SECTORS
] = 32;
1300 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1306 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1308 volume
.channel0
= 255;
1309 volume
.channel1
= 255;
1310 volume
.channel2
= 255;
1311 volume
.channel3
= 255;
1312 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1319 static int update_bitmap(struct io_thread_req
*req
)
1323 if(req
->cow_offset
== -1)
1326 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1328 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1332 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1333 sizeof(req
->bitmap_words
));
1334 if(n
!= sizeof(req
->bitmap_words
)){
1335 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1343 static void do_io(struct io_thread_req
*req
)
1347 int n
, nsectors
, start
, end
, bit
;
1351 nsectors
= req
->length
/ req
->sectorsize
;
1354 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1356 while((end
< nsectors
) &&
1357 (ubd_test_bit(end
, (unsigned char *)
1358 &req
->sector_mask
) == bit
))
1361 off
= req
->offset
+ req
->offsets
[bit
] +
1362 start
* req
->sectorsize
;
1363 len
= (end
- start
) * req
->sectorsize
;
1364 buf
= &req
->buffer
[start
* req
->sectorsize
];
1366 err
= os_seek_file(req
->fds
[bit
], off
);
1368 printk("do_io - lseek failed : err = %d\n", -err
);
1372 if(req
->op
== UBD_READ
){
1377 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1379 printk("do_io - read failed, err = %d "
1380 "fd = %d\n", -n
, req
->fds
[bit
]);
1384 } while((n
< len
) && (n
!= 0));
1385 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1387 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1389 printk("do_io - write failed err = %d "
1390 "fd = %d\n", -n
, req
->fds
[bit
]);
1397 } while(start
< nsectors
);
1399 req
->error
= update_bitmap(req
);
1402 /* Changed in start_io_thread, which is serialized by being called only
1403 * from ubd_init, which is an initcall.
1407 /* Only changed by the io thread. XXX: currently unused. */
1408 static int io_count
= 0;
1410 int io_thread(void *arg
)
1412 struct io_thread_req
*req
;
1415 ignore_sigwinch_sig();
1417 n
= os_read_file(kernel_fd
, &req
,
1418 sizeof(struct io_thread_req
*));
1419 if(n
!= sizeof(struct io_thread_req
*)){
1421 printk("io_thread - read failed, fd = %d, "
1422 "err = %d\n", kernel_fd
, -n
);
1424 printk("io_thread - short read, fd = %d, "
1425 "length = %d\n", kernel_fd
, n
);
1431 n
= os_write_file(kernel_fd
, &req
,
1432 sizeof(struct io_thread_req
*));
1433 if(n
!= sizeof(struct io_thread_req
*))
1434 printk("io_thread - write failed, fd = %d, err = %d\n",