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/kernel.h"
24 #include "linux/module.h"
25 #include "linux/blkdev.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/ctype.h"
31 #include "linux/capability.h"
33 #include "linux/vmalloc.h"
34 #include "linux/blkpg.h"
35 #include "linux/genhd.h"
36 #include "linux/spinlock.h"
37 #include "linux/platform_device.h"
38 #include "linux/scatterlist.h"
39 #include "asm/segment.h"
40 #include "asm/uaccess.h"
42 #include "asm/types.h"
43 #include "asm/tlbflush.h"
45 #include "kern_util.h"
47 #include "mconsole_kern.h"
52 #include "kern_util.h"
58 enum ubd_req
{ UBD_READ
, UBD_WRITE
};
60 struct io_thread_req
{
64 unsigned long offsets
[2];
65 unsigned long long offset
;
69 unsigned long sector_mask
;
70 unsigned long long cow_offset
;
71 unsigned long bitmap_words
[2];
75 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
80 bits
= sizeof(data
[0]) * 8;
83 return (data
[n
] & (1 << off
)) != 0;
86 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
91 bits
= sizeof(data
[0]) * 8;
94 data
[n
] |= (1 << off
);
96 /*End stuff from ubd_user.h*/
98 #define DRIVER_NAME "uml-blkdev"
100 static DEFINE_MUTEX(ubd_lock
);
102 static int ubd_open(struct inode
* inode
, struct file
* filp
);
103 static int ubd_release(struct inode
* inode
, struct file
* file
);
104 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
105 unsigned int cmd
, unsigned long arg
);
106 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
);
110 static struct block_device_operations ubd_blops
= {
111 .owner
= THIS_MODULE
,
113 .release
= ubd_release
,
115 .getgeo
= ubd_getgeo
,
118 /* Protected by ubd_lock */
119 static int fake_major
= MAJOR_NR
;
120 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
121 static struct gendisk
*fake_gendisk
[MAX_DEV
];
123 #ifdef CONFIG_BLK_DEV_UBD_SYNC
124 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
127 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
130 static struct openflags global_openflags
= OPEN_FLAGS
;
133 /* backing file name */
135 /* backing file fd */
137 unsigned long *bitmap
;
138 unsigned long bitmap_len
;
146 struct list_head restart
;
147 /* name (and fd, below) of the file opened for writing, either the
148 * backing or the cow file. */
153 struct openflags boot_openflags
;
154 struct openflags openflags
;
158 struct platform_device pdev
;
159 struct request_queue
*queue
;
161 struct scatterlist sg
[MAX_SG
];
162 struct request
*request
;
163 int start_sg
, end_sg
;
166 #define DEFAULT_COW { \
170 .bitmap_offset = 0, \
174 #define DEFAULT_UBD { \
179 .boot_openflags = OPEN_FLAGS, \
180 .openflags = OPEN_FLAGS, \
183 .cow = DEFAULT_COW, \
184 .lock = SPIN_LOCK_UNLOCKED, \
190 /* Protected by ubd_lock */
191 static struct ubd ubd_devs
[MAX_DEV
] = { [0 ... MAX_DEV
- 1] = DEFAULT_UBD
};
193 /* Only changed by fake_ide_setup which is a setup */
194 static int fake_ide
= 0;
195 static struct proc_dir_entry
*proc_ide_root
= NULL
;
196 static struct proc_dir_entry
*proc_ide
= NULL
;
198 static void make_proc_ide(void)
200 proc_ide_root
= proc_mkdir("ide", NULL
);
201 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
204 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
205 int *eof
, void *data
)
209 strcpy(page
, "disk\n");
210 len
= strlen("disk\n");
214 if (len
<= 0) return 0;
221 static void make_ide_entries(const char *dev_name
)
223 struct proc_dir_entry
*dir
, *ent
;
226 if(proc_ide_root
== NULL
) make_proc_ide();
228 dir
= proc_mkdir(dev_name
, proc_ide
);
231 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
234 ent
->read_proc
= proc_ide_read_media
;
235 ent
->write_proc
= NULL
;
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
!= MAJOR_NR
){
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;
456 static void ubd_end_request(struct request
*req
, int bytes
, int error
)
458 blk_end_request(req
, error
, bytes
);
461 /* Callable only from interrupt context - otherwise you need to do
462 * spin_lock_irq()/spin_lock_irqsave() */
463 static inline void ubd_finish(struct request
*req
, int bytes
)
466 ubd_end_request(req
, 0, -EIO
);
469 ubd_end_request(req
, bytes
, 0);
472 static LIST_HEAD(restart
);
474 /* XXX - move this inside ubd_intr. */
475 /* Called without dev->lock held, and only in interrupt context. */
476 static void ubd_handler(void)
478 struct io_thread_req
*req
;
481 struct list_head
*list
, *next_ele
;
486 n
= os_read_file(thread_fd
, &req
,
487 sizeof(struct io_thread_req
*));
488 if(n
!= sizeof(req
)){
491 printk(KERN_ERR
"spurious interrupt in ubd_handler, "
497 rq
->nr_sectors
-= req
->length
>> 9;
498 if(rq
->nr_sectors
== 0)
499 ubd_finish(rq
, rq
->hard_nr_sectors
<< 9);
502 reactivate_fd(thread_fd
, UBD_IRQ
);
504 list_for_each_safe(list
, next_ele
, &restart
){
505 ubd
= container_of(list
, struct ubd
, restart
);
506 list_del_init(&ubd
->restart
);
507 spin_lock_irqsave(&ubd
->lock
, flags
);
508 do_ubd_request(ubd
->queue
);
509 spin_unlock_irqrestore(&ubd
->lock
, flags
);
513 static irqreturn_t
ubd_intr(int irq
, void *dev
)
519 /* Only changed by ubd_init, which is an initcall. */
520 static int io_pid
= -1;
522 static void kill_io_thread(void)
525 os_kill_process(io_pid
, 1);
528 __uml_exitcall(kill_io_thread
);
530 static inline int ubd_file_size(struct ubd
*ubd_dev
, __u64
*size_out
)
534 file
= ubd_dev
->cow
.file
? ubd_dev
->cow
.file
: ubd_dev
->file
;
535 return os_file_size(file
, size_out
);
538 static int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
542 err
= os_seek_file(fd
, offset
);
546 err
= os_read_file(fd
, buf
, len
);
553 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
555 unsigned long modtime
;
556 unsigned long long actual
;
559 err
= os_file_modtime(file
, &modtime
);
561 printk(KERN_ERR
"Failed to get modification time of backing "
562 "file \"%s\", err = %d\n", file
, -err
);
566 err
= os_file_size(file
, &actual
);
568 printk(KERN_ERR
"Failed to get size of backing file \"%s\", "
569 "err = %d\n", file
, -err
);
573 if (actual
!= size
) {
574 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
576 printk(KERN_ERR
"Size mismatch (%llu vs %llu) of COW header "
577 "vs backing file\n", (unsigned long long) size
, actual
);
580 if (modtime
!= mtime
) {
581 printk(KERN_ERR
"mtime mismatch (%ld vs %ld) of COW header vs "
582 "backing file\n", mtime
, modtime
);
588 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
590 struct uml_stat buf1
, buf2
;
593 if (from_cmdline
== NULL
)
595 if (!strcmp(from_cmdline
, from_cow
))
598 err
= os_stat_file(from_cmdline
, &buf1
);
600 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cmdline
,
604 err
= os_stat_file(from_cow
, &buf2
);
606 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cow
,
610 if ((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
613 printk(KERN_ERR
"Backing file mismatch - \"%s\" requested, "
614 "\"%s\" specified in COW header of \"%s\"\n",
615 from_cmdline
, from_cow
, cow
);
619 static int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
620 char **backing_file_out
, int *bitmap_offset_out
,
621 unsigned long *bitmap_len_out
, int *data_offset_out
,
625 unsigned long long size
;
626 __u32 version
, align
;
628 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
630 fd
= os_open_file(file
, *openflags
, mode
);
632 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
635 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
638 fd
= os_open_file(file
, *openflags
, mode
);
644 printk(KERN_INFO
"Not locking \"%s\" on the host\n", file
);
646 err
= os_lock_file(fd
, openflags
->w
);
648 printk(KERN_ERR
"Failed to lock '%s', err = %d\n",
654 /* Successful return case! */
655 if (backing_file_out
== NULL
)
658 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
659 &size
, §orsize
, &align
, bitmap_offset_out
);
660 if (err
&& (*backing_file_out
!= NULL
)) {
661 printk(KERN_ERR
"Failed to read COW header from COW file "
662 "\"%s\", errno = %d\n", file
, -err
);
668 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
,
671 /* Allow switching only if no mismatch. */
672 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
,
674 printk(KERN_ERR
"Switching backing file to '%s'\n",
676 err
= write_cow_header(file
, fd
, *backing_file_out
,
677 sectorsize
, align
, &size
);
679 printk(KERN_ERR
"Switch failed, errno = %d\n", -err
);
683 *backing_file_out
= backing_file
;
684 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
689 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
690 bitmap_len_out
, data_offset_out
);
698 static int create_cow_file(char *cow_file
, char *backing_file
,
699 struct openflags flags
,
700 int sectorsize
, int alignment
, int *bitmap_offset_out
,
701 unsigned long *bitmap_len_out
, int *data_offset_out
)
706 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
709 printk(KERN_ERR
"Open of COW file '%s' failed, errno = %d\n",
714 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
715 bitmap_offset_out
, bitmap_len_out
,
724 static void ubd_close_dev(struct ubd
*ubd_dev
)
726 os_close_file(ubd_dev
->fd
);
727 if(ubd_dev
->cow
.file
== NULL
)
730 os_close_file(ubd_dev
->cow
.fd
);
731 vfree(ubd_dev
->cow
.bitmap
);
732 ubd_dev
->cow
.bitmap
= NULL
;
735 static int ubd_open_dev(struct ubd
*ubd_dev
)
737 struct openflags flags
;
739 int err
, create_cow
, *create_ptr
;
742 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
744 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
745 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
747 fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
748 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
749 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
752 if((fd
== -ENOENT
) && create_cow
){
753 fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
754 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
755 &ubd_dev
->cow
.bitmap_offset
,
756 &ubd_dev
->cow
.bitmap_len
,
757 &ubd_dev
->cow
.data_offset
);
759 printk(KERN_INFO
"Creating \"%s\" as COW file for "
760 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
765 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
771 if(ubd_dev
->cow
.file
!= NULL
){
772 blk_queue_max_sectors(ubd_dev
->queue
, 8 * sizeof(long));
775 ubd_dev
->cow
.bitmap
= vmalloc(ubd_dev
->cow
.bitmap_len
);
776 if(ubd_dev
->cow
.bitmap
== NULL
){
777 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
780 flush_tlb_kernel_vm();
782 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
783 ubd_dev
->cow
.bitmap_offset
,
784 ubd_dev
->cow
.bitmap_len
);
788 flags
= ubd_dev
->openflags
;
790 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
791 NULL
, NULL
, NULL
, NULL
);
792 if(err
< 0) goto error
;
793 ubd_dev
->cow
.fd
= err
;
797 os_close_file(ubd_dev
->fd
);
801 static void ubd_device_release(struct device
*dev
)
803 struct ubd
*ubd_dev
= dev
->driver_data
;
805 blk_cleanup_queue(ubd_dev
->queue
);
806 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
809 static int ubd_disk_register(int major
, u64 size
, int unit
,
810 struct gendisk
**disk_out
)
812 struct gendisk
*disk
;
814 disk
= alloc_disk(1 << UBD_SHIFT
);
819 disk
->first_minor
= unit
<< UBD_SHIFT
;
820 disk
->fops
= &ubd_blops
;
821 set_capacity(disk
, size
/ 512);
822 if(major
== MAJOR_NR
)
823 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
825 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
827 /* sysfs register (not for ide fake devices) */
828 if (major
== MAJOR_NR
) {
829 ubd_devs
[unit
].pdev
.id
= unit
;
830 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
831 ubd_devs
[unit
].pdev
.dev
.release
= ubd_device_release
;
832 ubd_devs
[unit
].pdev
.dev
.driver_data
= &ubd_devs
[unit
];
833 platform_device_register(&ubd_devs
[unit
].pdev
);
834 disk
->driverfs_dev
= &ubd_devs
[unit
].pdev
.dev
;
837 disk
->private_data
= &ubd_devs
[unit
];
838 disk
->queue
= ubd_devs
[unit
].queue
;
845 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
847 static int ubd_add(int n
, char **error_out
)
849 struct ubd
*ubd_dev
= &ubd_devs
[n
];
852 if(ubd_dev
->file
== NULL
)
855 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
857 *error_out
= "Couldn't determine size of device's file";
861 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
863 INIT_LIST_HEAD(&ubd_dev
->restart
);
864 sg_init_table(ubd_dev
->sg
, MAX_SG
);
867 ubd_dev
->queue
= blk_init_queue(do_ubd_request
, &ubd_dev
->lock
);
868 if (ubd_dev
->queue
== NULL
) {
869 *error_out
= "Failed to initialize device queue";
872 ubd_dev
->queue
->queuedata
= ubd_dev
;
874 blk_queue_max_hw_segments(ubd_dev
->queue
, MAX_SG
);
875 err
= ubd_disk_register(MAJOR_NR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
877 *error_out
= "Failed to register device";
881 if(fake_major
!= MAJOR_NR
)
882 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
886 * Perhaps this should also be under the "if (fake_major)" above
887 * using the fake_disk->disk_name
890 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
897 blk_cleanup_queue(ubd_dev
->queue
);
901 static int ubd_config(char *str
, char **error_out
)
905 /* This string is possibly broken up and stored, so it's only
906 * freed if ubd_setup_common fails, or if only general options
909 str
= kstrdup(str
, GFP_KERNEL
);
911 *error_out
= "Failed to allocate memory";
915 ret
= ubd_setup_common(str
, &n
, error_out
);
924 mutex_lock(&ubd_lock
);
925 ret
= ubd_add(n
, error_out
);
927 ubd_devs
[n
].file
= NULL
;
928 mutex_unlock(&ubd_lock
);
938 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
943 n
= parse_unit(&name
);
944 if((n
>= MAX_DEV
) || (n
< 0)){
945 *error_out
= "ubd_get_config : device number out of range";
949 ubd_dev
= &ubd_devs
[n
];
950 mutex_lock(&ubd_lock
);
952 if(ubd_dev
->file
== NULL
){
953 CONFIG_CHUNK(str
, size
, len
, "", 1);
957 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
959 if(ubd_dev
->cow
.file
!= NULL
){
960 CONFIG_CHUNK(str
, size
, len
, ",", 0);
961 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
963 else CONFIG_CHUNK(str
, size
, len
, "", 1);
966 mutex_unlock(&ubd_lock
);
970 static int ubd_id(char **str
, int *start_out
, int *end_out
)
976 *end_out
= MAX_DEV
- 1;
980 static int ubd_remove(int n
, char **error_out
)
982 struct gendisk
*disk
= ubd_gendisk
[n
];
986 mutex_lock(&ubd_lock
);
988 ubd_dev
= &ubd_devs
[n
];
990 if(ubd_dev
->file
== NULL
)
993 /* you cannot remove a open disk */
995 if(ubd_dev
->count
> 0)
998 ubd_gendisk
[n
] = NULL
;
1004 if(fake_gendisk
[n
] != NULL
){
1005 del_gendisk(fake_gendisk
[n
]);
1006 put_disk(fake_gendisk
[n
]);
1007 fake_gendisk
[n
] = NULL
;
1011 platform_device_unregister(&ubd_dev
->pdev
);
1013 mutex_unlock(&ubd_lock
);
1017 /* All these are called by mconsole in process context and without
1018 * ubd-specific locks. The structure itself is const except for .list.
1020 static struct mc_device ubd_mc
= {
1021 .list
= LIST_HEAD_INIT(ubd_mc
.list
),
1023 .config
= ubd_config
,
1024 .get_config
= ubd_get_config
,
1026 .remove
= ubd_remove
,
1029 static int __init
ubd_mc_init(void)
1031 mconsole_register_dev(&ubd_mc
);
1035 __initcall(ubd_mc_init
);
1037 static int __init
ubd0_init(void)
1039 struct ubd
*ubd_dev
= &ubd_devs
[0];
1041 mutex_lock(&ubd_lock
);
1042 if(ubd_dev
->file
== NULL
)
1043 ubd_dev
->file
= "root_fs";
1044 mutex_unlock(&ubd_lock
);
1049 __initcall(ubd0_init
);
1051 /* Used in ubd_init, which is an initcall */
1052 static struct platform_driver ubd_driver
= {
1054 .name
= DRIVER_NAME
,
1058 static int __init
ubd_init(void)
1063 if (register_blkdev(MAJOR_NR
, "ubd"))
1066 if (fake_major
!= MAJOR_NR
) {
1067 char name
[sizeof("ubd_nnn\0")];
1069 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
1070 if (register_blkdev(fake_major
, "ubd"))
1073 platform_driver_register(&ubd_driver
);
1074 mutex_lock(&ubd_lock
);
1075 for (i
= 0; i
< MAX_DEV
; i
++){
1076 err
= ubd_add(i
, &error
);
1078 printk(KERN_ERR
"Failed to initialize ubd device %d :"
1081 mutex_unlock(&ubd_lock
);
1085 late_initcall(ubd_init
);
1087 static int __init
ubd_driver_init(void){
1088 unsigned long stack
;
1091 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1092 if(global_openflags
.s
){
1093 printk(KERN_INFO
"ubd: Synchronous mode\n");
1094 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1095 * enough. So use anyway the io thread. */
1097 stack
= alloc_stack(0, 0);
1098 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
1102 "ubd : Failed to start I/O thread (errno = %d) - "
1103 "falling back to synchronous I/O\n", -io_pid
);
1107 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
1108 IRQF_DISABLED
, "ubd", ubd_devs
);
1110 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
1114 device_initcall(ubd_driver_init
);
1116 static int ubd_open(struct inode
*inode
, struct file
*filp
)
1118 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1119 struct ubd
*ubd_dev
= disk
->private_data
;
1122 if(ubd_dev
->count
== 0){
1123 err
= ubd_open_dev(ubd_dev
);
1125 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
1126 disk
->disk_name
, ubd_dev
->file
, -err
);
1131 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
1133 /* This should no more be needed. And it didn't work anyway to exclude
1134 * read-write remounting of filesystems.*/
1135 /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1136 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1143 static int ubd_release(struct inode
* inode
, struct file
* file
)
1145 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1146 struct ubd
*ubd_dev
= disk
->private_data
;
1148 if(--ubd_dev
->count
== 0)
1149 ubd_close_dev(ubd_dev
);
1153 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
1154 __u64
*cow_offset
, unsigned long *bitmap
,
1155 __u64 bitmap_offset
, unsigned long *bitmap_words
,
1158 __u64 sector
= io_offset
>> 9;
1159 int i
, update_bitmap
= 0;
1161 for(i
= 0; i
< length
>> 9; i
++){
1162 if(cow_mask
!= NULL
)
1163 ubd_set_bit(i
, (unsigned char *) cow_mask
);
1164 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1168 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
1174 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
1176 /* This takes care of the case where we're exactly at the end of the
1177 * device, and *cow_offset + 1 is off the end. So, just back it up
1178 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1179 * for the original diagnosis.
1181 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
1182 sizeof(unsigned long) - 1))
1185 bitmap_words
[0] = bitmap
[*cow_offset
];
1186 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
1188 *cow_offset
*= sizeof(unsigned long);
1189 *cow_offset
+= bitmap_offset
;
1192 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
1193 __u64 bitmap_offset
, __u64 bitmap_len
)
1195 __u64 sector
= req
->offset
>> 9;
1198 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
1199 panic("Operation too long");
1201 if(req
->op
== UBD_READ
) {
1202 for(i
= 0; i
< req
->length
>> 9; i
++){
1203 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1204 ubd_set_bit(i
, (unsigned char *)
1208 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1209 &req
->cow_offset
, bitmap
, bitmap_offset
,
1210 req
->bitmap_words
, bitmap_len
);
1213 /* Called with dev->lock held */
1214 static void prepare_request(struct request
*req
, struct io_thread_req
*io_req
,
1215 unsigned long long offset
, int page_offset
,
1216 int len
, struct page
*page
)
1218 struct gendisk
*disk
= req
->rq_disk
;
1219 struct ubd
*ubd_dev
= disk
->private_data
;
1222 io_req
->fds
[0] = (ubd_dev
->cow
.file
!= NULL
) ? ubd_dev
->cow
.fd
:
1224 io_req
->fds
[1] = ubd_dev
->fd
;
1225 io_req
->cow_offset
= -1;
1226 io_req
->offset
= offset
;
1227 io_req
->length
= len
;
1229 io_req
->sector_mask
= 0;
1231 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1232 io_req
->offsets
[0] = 0;
1233 io_req
->offsets
[1] = ubd_dev
->cow
.data_offset
;
1234 io_req
->buffer
= page_address(page
) + page_offset
;
1235 io_req
->sectorsize
= 1 << 9;
1237 if(ubd_dev
->cow
.file
!= NULL
)
1238 cowify_req(io_req
, ubd_dev
->cow
.bitmap
,
1239 ubd_dev
->cow
.bitmap_offset
, ubd_dev
->cow
.bitmap_len
);
1243 /* Called with dev->lock held */
1244 static void do_ubd_request(struct request_queue
*q
)
1246 struct io_thread_req
*io_req
;
1247 struct request
*req
;
1248 int n
, last_sectors
;
1251 struct ubd
*dev
= q
->queuedata
;
1252 if(dev
->end_sg
== 0){
1253 struct request
*req
= elv_next_request(q
);
1258 blkdev_dequeue_request(req
);
1260 dev
->end_sg
= blk_rq_map_sg(q
, req
, dev
->sg
);
1265 while(dev
->start_sg
< dev
->end_sg
){
1266 struct scatterlist
*sg
= &dev
->sg
[dev
->start_sg
];
1268 req
->sector
+= last_sectors
;
1269 io_req
= kmalloc(sizeof(struct io_thread_req
),
1272 if(list_empty(&dev
->restart
))
1273 list_add(&dev
->restart
, &restart
);
1276 prepare_request(req
, io_req
,
1277 (unsigned long long) req
->sector
<< 9,
1278 sg
->offset
, sg
->length
, sg_page(sg
));
1280 last_sectors
= sg
->length
>> 9;
1281 n
= os_write_file(thread_fd
, &io_req
,
1282 sizeof(struct io_thread_req
*));
1283 if(n
!= sizeof(struct io_thread_req
*)){
1285 printk("write to io thread failed, "
1286 "errno = %d\n", -n
);
1287 else if(list_empty(&dev
->restart
))
1288 list_add(&dev
->restart
, &restart
);
1296 dev
->request
= NULL
;
1300 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1302 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1306 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1310 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1311 unsigned int cmd
, unsigned long arg
)
1313 struct ubd
*ubd_dev
= inode
->i_bdev
->bd_disk
->private_data
;
1314 struct hd_driveid ubd_id
= {
1321 struct cdrom_volctrl volume
;
1322 case HDIO_GET_IDENTITY
:
1323 ubd_id
.cyls
= ubd_dev
->size
/ (128 * 32 * 512);
1324 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1330 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1332 volume
.channel0
= 255;
1333 volume
.channel1
= 255;
1334 volume
.channel2
= 255;
1335 volume
.channel3
= 255;
1336 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1343 static int update_bitmap(struct io_thread_req
*req
)
1347 if(req
->cow_offset
== -1)
1350 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1352 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1356 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1357 sizeof(req
->bitmap_words
));
1358 if(n
!= sizeof(req
->bitmap_words
)){
1359 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1367 static void do_io(struct io_thread_req
*req
)
1371 int n
, nsectors
, start
, end
, bit
;
1375 nsectors
= req
->length
/ req
->sectorsize
;
1378 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1380 while((end
< nsectors
) &&
1381 (ubd_test_bit(end
, (unsigned char *)
1382 &req
->sector_mask
) == bit
))
1385 off
= req
->offset
+ req
->offsets
[bit
] +
1386 start
* req
->sectorsize
;
1387 len
= (end
- start
) * req
->sectorsize
;
1388 buf
= &req
->buffer
[start
* req
->sectorsize
];
1390 err
= os_seek_file(req
->fds
[bit
], off
);
1392 printk("do_io - lseek failed : err = %d\n", -err
);
1396 if(req
->op
== UBD_READ
){
1401 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1403 printk("do_io - read failed, err = %d "
1404 "fd = %d\n", -n
, req
->fds
[bit
]);
1408 } while((n
< len
) && (n
!= 0));
1409 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1411 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1413 printk("do_io - write failed err = %d "
1414 "fd = %d\n", -n
, req
->fds
[bit
]);
1421 } while(start
< nsectors
);
1423 req
->error
= update_bitmap(req
);
1426 /* Changed in start_io_thread, which is serialized by being called only
1427 * from ubd_init, which is an initcall.
1431 /* Only changed by the io thread. XXX: currently unused. */
1432 static int io_count
= 0;
1434 int io_thread(void *arg
)
1436 struct io_thread_req
*req
;
1439 ignore_sigwinch_sig();
1441 n
= os_read_file(kernel_fd
, &req
,
1442 sizeof(struct io_thread_req
*));
1443 if(n
!= sizeof(struct io_thread_req
*)){
1445 printk("io_thread - read failed, fd = %d, "
1446 "err = %d\n", kernel_fd
, -n
);
1448 printk("io_thread - short read, fd = %d, "
1449 "length = %d\n", kernel_fd
, n
);
1455 n
= os_write_file(kernel_fd
, &req
,
1456 sizeof(struct io_thread_req
*));
1457 if(n
!= sizeof(struct io_thread_req
*))
1458 printk("io_thread - write failed, fd = %d, err = %d\n",