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/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(ubd_devs.lock), \
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
)
523 unsigned long long size
;
527 if (ubd_dev
->file
&& ubd_dev
->cow
.file
) {
528 file
= ubd_dev
->cow
.file
;
533 fd
= os_open_file(ubd_dev
->file
, global_openflags
, 0);
537 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, \
538 &mtime
, &size
, §or_size
, &align
, &bitmap_offset
);
542 file
= ubd_dev
->file
;
547 return os_file_size(file
, size_out
);
550 static int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
554 err
= os_seek_file(fd
, offset
);
558 err
= os_read_file(fd
, buf
, len
);
565 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
567 unsigned long modtime
;
568 unsigned long long actual
;
571 err
= os_file_modtime(file
, &modtime
);
573 printk(KERN_ERR
"Failed to get modification time of backing "
574 "file \"%s\", err = %d\n", file
, -err
);
578 err
= os_file_size(file
, &actual
);
580 printk(KERN_ERR
"Failed to get size of backing file \"%s\", "
581 "err = %d\n", file
, -err
);
585 if (actual
!= size
) {
586 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
588 printk(KERN_ERR
"Size mismatch (%llu vs %llu) of COW header "
589 "vs backing file\n", (unsigned long long) size
, actual
);
592 if (modtime
!= mtime
) {
593 printk(KERN_ERR
"mtime mismatch (%ld vs %ld) of COW header vs "
594 "backing file\n", mtime
, modtime
);
600 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
602 struct uml_stat buf1
, buf2
;
605 if (from_cmdline
== NULL
)
607 if (!strcmp(from_cmdline
, from_cow
))
610 err
= os_stat_file(from_cmdline
, &buf1
);
612 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cmdline
,
616 err
= os_stat_file(from_cow
, &buf2
);
618 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cow
,
622 if ((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
625 printk(KERN_ERR
"Backing file mismatch - \"%s\" requested, "
626 "\"%s\" specified in COW header of \"%s\"\n",
627 from_cmdline
, from_cow
, cow
);
631 static int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
632 char **backing_file_out
, int *bitmap_offset_out
,
633 unsigned long *bitmap_len_out
, int *data_offset_out
,
637 unsigned long long size
;
638 __u32 version
, align
;
640 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
642 fd
= os_open_file(file
, *openflags
, mode
);
644 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
647 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
650 fd
= os_open_file(file
, *openflags
, mode
);
656 printk(KERN_INFO
"Not locking \"%s\" on the host\n", file
);
658 err
= os_lock_file(fd
, openflags
->w
);
660 printk(KERN_ERR
"Failed to lock '%s', err = %d\n",
666 /* Successful return case! */
667 if (backing_file_out
== NULL
)
670 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
671 &size
, §orsize
, &align
, bitmap_offset_out
);
672 if (err
&& (*backing_file_out
!= NULL
)) {
673 printk(KERN_ERR
"Failed to read COW header from COW file "
674 "\"%s\", errno = %d\n", file
, -err
);
680 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
,
683 /* Allow switching only if no mismatch. */
684 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
,
686 printk(KERN_ERR
"Switching backing file to '%s'\n",
688 err
= write_cow_header(file
, fd
, *backing_file_out
,
689 sectorsize
, align
, &size
);
691 printk(KERN_ERR
"Switch failed, errno = %d\n", -err
);
695 *backing_file_out
= backing_file
;
696 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
701 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
702 bitmap_len_out
, data_offset_out
);
710 static int create_cow_file(char *cow_file
, char *backing_file
,
711 struct openflags flags
,
712 int sectorsize
, int alignment
, int *bitmap_offset_out
,
713 unsigned long *bitmap_len_out
, int *data_offset_out
)
718 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
721 printk(KERN_ERR
"Open of COW file '%s' failed, errno = %d\n",
726 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
727 bitmap_offset_out
, bitmap_len_out
,
736 static void ubd_close_dev(struct ubd
*ubd_dev
)
738 os_close_file(ubd_dev
->fd
);
739 if(ubd_dev
->cow
.file
== NULL
)
742 os_close_file(ubd_dev
->cow
.fd
);
743 vfree(ubd_dev
->cow
.bitmap
);
744 ubd_dev
->cow
.bitmap
= NULL
;
747 static int ubd_open_dev(struct ubd
*ubd_dev
)
749 struct openflags flags
;
751 int err
, create_cow
, *create_ptr
;
754 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
756 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
757 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
759 fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
760 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
761 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
764 if((fd
== -ENOENT
) && create_cow
){
765 fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
766 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
767 &ubd_dev
->cow
.bitmap_offset
,
768 &ubd_dev
->cow
.bitmap_len
,
769 &ubd_dev
->cow
.data_offset
);
771 printk(KERN_INFO
"Creating \"%s\" as COW file for "
772 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
777 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
783 if(ubd_dev
->cow
.file
!= NULL
){
784 blk_queue_max_hw_sectors(ubd_dev
->queue
, 8 * sizeof(long));
787 ubd_dev
->cow
.bitmap
= vmalloc(ubd_dev
->cow
.bitmap_len
);
788 if(ubd_dev
->cow
.bitmap
== NULL
){
789 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
792 flush_tlb_kernel_vm();
794 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
795 ubd_dev
->cow
.bitmap_offset
,
796 ubd_dev
->cow
.bitmap_len
);
800 flags
= ubd_dev
->openflags
;
802 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
803 NULL
, NULL
, NULL
, NULL
);
804 if(err
< 0) goto error
;
805 ubd_dev
->cow
.fd
= err
;
809 os_close_file(ubd_dev
->fd
);
813 static void ubd_device_release(struct device
*dev
)
815 struct ubd
*ubd_dev
= dev_get_drvdata(dev
);
817 blk_cleanup_queue(ubd_dev
->queue
);
818 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
821 static int ubd_disk_register(int major
, u64 size
, int unit
,
822 struct gendisk
**disk_out
)
824 struct gendisk
*disk
;
826 disk
= alloc_disk(1 << UBD_SHIFT
);
831 disk
->first_minor
= unit
<< UBD_SHIFT
;
832 disk
->fops
= &ubd_blops
;
833 set_capacity(disk
, size
/ 512);
834 if (major
== UBD_MAJOR
)
835 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
837 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
839 /* sysfs register (not for ide fake devices) */
840 if (major
== UBD_MAJOR
) {
841 ubd_devs
[unit
].pdev
.id
= unit
;
842 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
843 ubd_devs
[unit
].pdev
.dev
.release
= ubd_device_release
;
844 dev_set_drvdata(&ubd_devs
[unit
].pdev
.dev
, &ubd_devs
[unit
]);
845 platform_device_register(&ubd_devs
[unit
].pdev
);
846 disk
->driverfs_dev
= &ubd_devs
[unit
].pdev
.dev
;
849 disk
->private_data
= &ubd_devs
[unit
];
850 disk
->queue
= ubd_devs
[unit
].queue
;
857 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
859 static int ubd_add(int n
, char **error_out
)
861 struct ubd
*ubd_dev
= &ubd_devs
[n
];
864 if(ubd_dev
->file
== NULL
)
867 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
869 *error_out
= "Couldn't determine size of device's file";
873 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
875 INIT_LIST_HEAD(&ubd_dev
->restart
);
876 sg_init_table(ubd_dev
->sg
, MAX_SG
);
879 ubd_dev
->queue
= blk_init_queue(do_ubd_request
, &ubd_dev
->lock
);
880 if (ubd_dev
->queue
== NULL
) {
881 *error_out
= "Failed to initialize device queue";
884 ubd_dev
->queue
->queuedata
= ubd_dev
;
886 blk_queue_max_segments(ubd_dev
->queue
, MAX_SG
);
887 err
= ubd_disk_register(UBD_MAJOR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
889 *error_out
= "Failed to register device";
893 if (fake_major
!= UBD_MAJOR
)
894 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
898 * Perhaps this should also be under the "if (fake_major)" above
899 * using the fake_disk->disk_name
902 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
909 blk_cleanup_queue(ubd_dev
->queue
);
913 static int ubd_config(char *str
, char **error_out
)
917 /* This string is possibly broken up and stored, so it's only
918 * freed if ubd_setup_common fails, or if only general options
921 str
= kstrdup(str
, GFP_KERNEL
);
923 *error_out
= "Failed to allocate memory";
927 ret
= ubd_setup_common(str
, &n
, error_out
);
936 mutex_lock(&ubd_lock
);
937 ret
= ubd_add(n
, error_out
);
939 ubd_devs
[n
].file
= NULL
;
940 mutex_unlock(&ubd_lock
);
950 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
955 n
= parse_unit(&name
);
956 if((n
>= MAX_DEV
) || (n
< 0)){
957 *error_out
= "ubd_get_config : device number out of range";
961 ubd_dev
= &ubd_devs
[n
];
962 mutex_lock(&ubd_lock
);
964 if(ubd_dev
->file
== NULL
){
965 CONFIG_CHUNK(str
, size
, len
, "", 1);
969 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
971 if(ubd_dev
->cow
.file
!= NULL
){
972 CONFIG_CHUNK(str
, size
, len
, ",", 0);
973 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
975 else CONFIG_CHUNK(str
, size
, len
, "", 1);
978 mutex_unlock(&ubd_lock
);
982 static int ubd_id(char **str
, int *start_out
, int *end_out
)
988 *end_out
= MAX_DEV
- 1;
992 static int ubd_remove(int n
, char **error_out
)
994 struct gendisk
*disk
= ubd_gendisk
[n
];
998 mutex_lock(&ubd_lock
);
1000 ubd_dev
= &ubd_devs
[n
];
1002 if(ubd_dev
->file
== NULL
)
1005 /* you cannot remove a open disk */
1007 if(ubd_dev
->count
> 0)
1010 ubd_gendisk
[n
] = NULL
;
1016 if(fake_gendisk
[n
] != NULL
){
1017 del_gendisk(fake_gendisk
[n
]);
1018 put_disk(fake_gendisk
[n
]);
1019 fake_gendisk
[n
] = NULL
;
1023 platform_device_unregister(&ubd_dev
->pdev
);
1025 mutex_unlock(&ubd_lock
);
1029 /* All these are called by mconsole in process context and without
1030 * ubd-specific locks. The structure itself is const except for .list.
1032 static struct mc_device ubd_mc
= {
1033 .list
= LIST_HEAD_INIT(ubd_mc
.list
),
1035 .config
= ubd_config
,
1036 .get_config
= ubd_get_config
,
1038 .remove
= ubd_remove
,
1041 static int __init
ubd_mc_init(void)
1043 mconsole_register_dev(&ubd_mc
);
1047 __initcall(ubd_mc_init
);
1049 static int __init
ubd0_init(void)
1051 struct ubd
*ubd_dev
= &ubd_devs
[0];
1053 mutex_lock(&ubd_lock
);
1054 if(ubd_dev
->file
== NULL
)
1055 ubd_dev
->file
= "root_fs";
1056 mutex_unlock(&ubd_lock
);
1061 __initcall(ubd0_init
);
1063 /* Used in ubd_init, which is an initcall */
1064 static struct platform_driver ubd_driver
= {
1066 .name
= DRIVER_NAME
,
1070 static int __init
ubd_init(void)
1075 if (register_blkdev(UBD_MAJOR
, "ubd"))
1078 if (fake_major
!= UBD_MAJOR
) {
1079 char name
[sizeof("ubd_nnn\0")];
1081 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
1082 if (register_blkdev(fake_major
, "ubd"))
1085 platform_driver_register(&ubd_driver
);
1086 mutex_lock(&ubd_lock
);
1087 for (i
= 0; i
< MAX_DEV
; i
++){
1088 err
= ubd_add(i
, &error
);
1090 printk(KERN_ERR
"Failed to initialize ubd device %d :"
1093 mutex_unlock(&ubd_lock
);
1097 late_initcall(ubd_init
);
1099 static int __init
ubd_driver_init(void){
1100 unsigned long stack
;
1103 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1104 if(global_openflags
.s
){
1105 printk(KERN_INFO
"ubd: Synchronous mode\n");
1106 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1107 * enough. So use anyway the io thread. */
1109 stack
= alloc_stack(0, 0);
1110 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
1114 "ubd : Failed to start I/O thread (errno = %d) - "
1115 "falling back to synchronous I/O\n", -io_pid
);
1119 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
1120 IRQF_DISABLED
, "ubd", ubd_devs
);
1122 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
1126 device_initcall(ubd_driver_init
);
1128 static int ubd_open(struct block_device
*bdev
, fmode_t mode
)
1130 struct gendisk
*disk
= bdev
->bd_disk
;
1131 struct ubd
*ubd_dev
= disk
->private_data
;
1134 mutex_lock(&ubd_mutex
);
1135 if(ubd_dev
->count
== 0){
1136 err
= ubd_open_dev(ubd_dev
);
1138 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
1139 disk
->disk_name
, ubd_dev
->file
, -err
);
1144 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
1146 /* This should no more be needed. And it didn't work anyway to exclude
1147 * read-write remounting of filesystems.*/
1148 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1149 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1153 mutex_unlock(&ubd_mutex
);
1157 static int ubd_release(struct gendisk
*disk
, fmode_t mode
)
1159 struct ubd
*ubd_dev
= disk
->private_data
;
1161 mutex_lock(&ubd_mutex
);
1162 if(--ubd_dev
->count
== 0)
1163 ubd_close_dev(ubd_dev
);
1164 mutex_unlock(&ubd_mutex
);
1168 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
1169 __u64
*cow_offset
, unsigned long *bitmap
,
1170 __u64 bitmap_offset
, unsigned long *bitmap_words
,
1173 __u64 sector
= io_offset
>> 9;
1174 int i
, update_bitmap
= 0;
1176 for(i
= 0; i
< length
>> 9; i
++){
1177 if(cow_mask
!= NULL
)
1178 ubd_set_bit(i
, (unsigned char *) cow_mask
);
1179 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1183 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
1189 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
1191 /* This takes care of the case where we're exactly at the end of the
1192 * device, and *cow_offset + 1 is off the end. So, just back it up
1193 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1194 * for the original diagnosis.
1196 if (*cow_offset
== (DIV_ROUND_UP(bitmap_len
,
1197 sizeof(unsigned long)) - 1))
1200 bitmap_words
[0] = bitmap
[*cow_offset
];
1201 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
1203 *cow_offset
*= sizeof(unsigned long);
1204 *cow_offset
+= bitmap_offset
;
1207 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
1208 __u64 bitmap_offset
, __u64 bitmap_len
)
1210 __u64 sector
= req
->offset
>> 9;
1213 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
1214 panic("Operation too long");
1216 if(req
->op
== UBD_READ
) {
1217 for(i
= 0; i
< req
->length
>> 9; i
++){
1218 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1219 ubd_set_bit(i
, (unsigned char *)
1223 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1224 &req
->cow_offset
, bitmap
, bitmap_offset
,
1225 req
->bitmap_words
, bitmap_len
);
1228 /* Called with dev->lock held */
1229 static void prepare_request(struct request
*req
, struct io_thread_req
*io_req
,
1230 unsigned long long offset
, int page_offset
,
1231 int len
, struct page
*page
)
1233 struct gendisk
*disk
= req
->rq_disk
;
1234 struct ubd
*ubd_dev
= disk
->private_data
;
1237 io_req
->fds
[0] = (ubd_dev
->cow
.file
!= NULL
) ? ubd_dev
->cow
.fd
:
1239 io_req
->fds
[1] = ubd_dev
->fd
;
1240 io_req
->cow_offset
= -1;
1241 io_req
->offset
= offset
;
1242 io_req
->length
= len
;
1244 io_req
->sector_mask
= 0;
1246 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1247 io_req
->offsets
[0] = 0;
1248 io_req
->offsets
[1] = ubd_dev
->cow
.data_offset
;
1249 io_req
->buffer
= page_address(page
) + page_offset
;
1250 io_req
->sectorsize
= 1 << 9;
1252 if(ubd_dev
->cow
.file
!= NULL
)
1253 cowify_req(io_req
, ubd_dev
->cow
.bitmap
,
1254 ubd_dev
->cow
.bitmap_offset
, ubd_dev
->cow
.bitmap_len
);
1258 /* Called with dev->lock held */
1259 static void do_ubd_request(struct request_queue
*q
)
1261 struct io_thread_req
*io_req
;
1262 struct request
*req
;
1266 struct ubd
*dev
= q
->queuedata
;
1267 if(dev
->end_sg
== 0){
1268 struct request
*req
= blk_fetch_request(q
);
1273 dev
->rq_pos
= blk_rq_pos(req
);
1275 dev
->end_sg
= blk_rq_map_sg(q
, req
, dev
->sg
);
1279 while(dev
->start_sg
< dev
->end_sg
){
1280 struct scatterlist
*sg
= &dev
->sg
[dev
->start_sg
];
1282 io_req
= kmalloc(sizeof(struct io_thread_req
),
1285 if(list_empty(&dev
->restart
))
1286 list_add(&dev
->restart
, &restart
);
1289 prepare_request(req
, io_req
,
1290 (unsigned long long)dev
->rq_pos
<< 9,
1291 sg
->offset
, sg
->length
, sg_page(sg
));
1293 n
= os_write_file(thread_fd
, &io_req
,
1294 sizeof(struct io_thread_req
*));
1295 if(n
!= sizeof(struct io_thread_req
*)){
1297 printk("write to io thread failed, "
1298 "errno = %d\n", -n
);
1299 else if(list_empty(&dev
->restart
))
1300 list_add(&dev
->restart
, &restart
);
1305 dev
->rq_pos
+= sg
->length
>> 9;
1309 dev
->request
= NULL
;
1313 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1315 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1319 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1323 static int ubd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1324 unsigned int cmd
, unsigned long arg
)
1326 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1327 u16 ubd_id
[ATA_ID_WORDS
];
1330 struct cdrom_volctrl volume
;
1331 case HDIO_GET_IDENTITY
:
1332 memset(&ubd_id
, 0, ATA_ID_WORDS
* 2);
1333 ubd_id
[ATA_ID_CYLS
] = ubd_dev
->size
/ (128 * 32 * 512);
1334 ubd_id
[ATA_ID_HEADS
] = 128;
1335 ubd_id
[ATA_ID_SECTORS
] = 32;
1336 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1342 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1344 volume
.channel0
= 255;
1345 volume
.channel1
= 255;
1346 volume
.channel2
= 255;
1347 volume
.channel3
= 255;
1348 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1355 static int update_bitmap(struct io_thread_req
*req
)
1359 if(req
->cow_offset
== -1)
1362 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1364 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1368 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1369 sizeof(req
->bitmap_words
));
1370 if(n
!= sizeof(req
->bitmap_words
)){
1371 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1379 static void do_io(struct io_thread_req
*req
)
1383 int n
, nsectors
, start
, end
, bit
;
1387 nsectors
= req
->length
/ req
->sectorsize
;
1390 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1392 while((end
< nsectors
) &&
1393 (ubd_test_bit(end
, (unsigned char *)
1394 &req
->sector_mask
) == bit
))
1397 off
= req
->offset
+ req
->offsets
[bit
] +
1398 start
* req
->sectorsize
;
1399 len
= (end
- start
) * req
->sectorsize
;
1400 buf
= &req
->buffer
[start
* req
->sectorsize
];
1402 err
= os_seek_file(req
->fds
[bit
], off
);
1404 printk("do_io - lseek failed : err = %d\n", -err
);
1408 if(req
->op
== UBD_READ
){
1413 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1415 printk("do_io - read failed, err = %d "
1416 "fd = %d\n", -n
, req
->fds
[bit
]);
1420 } while((n
< len
) && (n
!= 0));
1421 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1423 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1425 printk("do_io - write failed err = %d "
1426 "fd = %d\n", -n
, req
->fds
[bit
]);
1433 } while(start
< nsectors
);
1435 req
->error
= update_bitmap(req
);
1438 /* Changed in start_io_thread, which is serialized by being called only
1439 * from ubd_init, which is an initcall.
1443 /* Only changed by the io thread. XXX: currently unused. */
1444 static int io_count
= 0;
1446 int io_thread(void *arg
)
1448 struct io_thread_req
*req
;
1451 ignore_sigwinch_sig();
1453 n
= os_read_file(kernel_fd
, &req
,
1454 sizeof(struct io_thread_req
*));
1455 if(n
!= sizeof(struct io_thread_req
*)){
1457 printk("io_thread - read failed, fd = %d, "
1458 "err = %d\n", kernel_fd
, -n
);
1460 printk("io_thread - short read, fd = %d, "
1461 "length = %d\n", kernel_fd
, n
);
1467 n
= os_write_file(kernel_fd
, &req
,
1468 sizeof(struct io_thread_req
*));
1469 if(n
!= sizeof(struct io_thread_req
*))
1470 printk("io_thread - write failed, fd = %d, err = %d\n",