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/hdreg.h"
26 #include "linux/init.h"
27 #include "linux/cdrom.h"
28 #include "linux/proc_fs.h"
29 #include "linux/ctype.h"
30 #include "linux/capability.h"
32 #include "linux/vmalloc.h"
33 #include "linux/blkpg.h"
34 #include "linux/genhd.h"
35 #include "linux/spinlock.h"
36 #include "linux/platform_device.h"
37 #include "linux/scatterlist.h"
38 #include "asm/segment.h"
39 #include "asm/uaccess.h"
41 #include "asm/types.h"
42 #include "asm/tlbflush.h"
44 #include "kern_util.h"
46 #include "mconsole_kern.h"
56 enum ubd_req
{ UBD_READ
, UBD_WRITE
};
58 struct io_thread_req
{
62 unsigned long offsets
[2];
63 unsigned long long offset
;
67 unsigned long sector_mask
;
68 unsigned long long cow_offset
;
69 unsigned long bitmap_words
[2];
73 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
78 bits
= sizeof(data
[0]) * 8;
81 return (data
[n
] & (1 << off
)) != 0;
84 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
89 bits
= sizeof(data
[0]) * 8;
92 data
[n
] |= (1 << off
);
94 /*End stuff from ubd_user.h*/
96 #define DRIVER_NAME "uml-blkdev"
98 static DEFINE_MUTEX(ubd_lock
);
100 static int ubd_open(struct block_device
*bdev
, fmode_t mode
);
101 static int ubd_release(struct gendisk
*disk
, fmode_t mode
);
102 static int ubd_ioctl(struct block_device
*bdev
, fmode_t mode
,
103 unsigned int cmd
, unsigned long arg
);
104 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
);
108 static struct block_device_operations ubd_blops
= {
109 .owner
= THIS_MODULE
,
111 .release
= ubd_release
,
113 .getgeo
= ubd_getgeo
,
116 /* Protected by ubd_lock */
117 static int fake_major
= UBD_MAJOR
;
118 static struct gendisk
*ubd_gendisk
[MAX_DEV
];
119 static struct gendisk
*fake_gendisk
[MAX_DEV
];
121 #ifdef CONFIG_BLK_DEV_UBD_SYNC
122 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
125 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
128 static struct openflags global_openflags
= OPEN_FLAGS
;
131 /* backing file name */
133 /* backing file fd */
135 unsigned long *bitmap
;
136 unsigned long bitmap_len
;
144 struct list_head restart
;
145 /* name (and fd, below) of the file opened for writing, either the
146 * backing or the cow file. */
151 struct openflags boot_openflags
;
152 struct openflags openflags
;
156 struct platform_device pdev
;
157 struct request_queue
*queue
;
159 struct scatterlist sg
[MAX_SG
];
160 struct request
*request
;
161 int start_sg
, end_sg
;
164 #define DEFAULT_COW { \
168 .bitmap_offset = 0, \
172 #define DEFAULT_UBD { \
177 .boot_openflags = OPEN_FLAGS, \
178 .openflags = OPEN_FLAGS, \
181 .cow = DEFAULT_COW, \
182 .lock = SPIN_LOCK_UNLOCKED, \
188 /* Protected by ubd_lock */
189 static struct ubd ubd_devs
[MAX_DEV
] = { [0 ... MAX_DEV
- 1] = DEFAULT_UBD
};
191 /* Only changed by fake_ide_setup which is a setup */
192 static int fake_ide
= 0;
193 static struct proc_dir_entry
*proc_ide_root
= NULL
;
194 static struct proc_dir_entry
*proc_ide
= NULL
;
196 static void make_proc_ide(void)
198 proc_ide_root
= proc_mkdir("ide", NULL
);
199 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
202 static int proc_ide_read_media(char *page
, char **start
, off_t off
, int count
,
203 int *eof
, void *data
)
207 strcpy(page
, "disk\n");
208 len
= strlen("disk\n");
212 if (len
<= 0) return 0;
219 static void make_ide_entries(const char *dev_name
)
221 struct proc_dir_entry
*dir
, *ent
;
224 if(proc_ide_root
== NULL
) make_proc_ide();
226 dir
= proc_mkdir(dev_name
, proc_ide
);
229 ent
= create_proc_entry("media", S_IFREG
|S_IRUGO
, dir
);
232 ent
->read_proc
= proc_ide_read_media
;
233 ent
->write_proc
= NULL
;
234 snprintf(name
, sizeof(name
), "ide0/%s", dev_name
);
235 proc_symlink(dev_name
, proc_ide_root
, name
);
238 static int fake_ide_setup(char *str
)
244 __setup("fake_ide", fake_ide_setup
);
246 __uml_help(fake_ide_setup
,
248 " Create ide0 entries that map onto ubd devices.\n\n"
251 static int parse_unit(char **ptr
)
253 char *str
= *ptr
, *end
;
257 n
= simple_strtoul(str
, &end
, 0);
262 else if (('a' <= *str
) && (*str
<= 'z')) {
270 /* If *index_out == -1 at exit, the passed option was a general one;
271 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
272 * should not be freed on exit.
274 static int ubd_setup_common(char *str
, int *index_out
, char **error_out
)
277 struct openflags flags
= global_openflags
;
281 if(index_out
) *index_out
= -1;
288 if(!strcmp(str
, "sync")){
289 global_openflags
= of_sync(global_openflags
);
294 major
= simple_strtoul(str
, &end
, 0);
295 if((*end
!= '\0') || (end
== str
)){
296 *error_out
= "Didn't parse major number";
300 mutex_lock(&ubd_lock
);
301 if (fake_major
!= UBD_MAJOR
) {
302 *error_out
= "Can't assign a fake major twice";
308 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
312 mutex_unlock(&ubd_lock
);
316 n
= parse_unit(&str
);
318 *error_out
= "Couldn't parse device number";
322 *error_out
= "Device number out of range";
327 mutex_lock(&ubd_lock
);
329 ubd_dev
= &ubd_devs
[n
];
330 if(ubd_dev
->file
!= NULL
){
331 *error_out
= "Device is already configured";
339 for (i
= 0; i
< sizeof("rscd="); i
++) {
357 *error_out
= "Expected '=' or flag letter "
365 *error_out
= "Too many flags specified";
367 *error_out
= "Missing '='";
371 backing_file
= strchr(str
, ',');
373 if (backing_file
== NULL
)
374 backing_file
= strchr(str
, ':');
376 if(backing_file
!= NULL
){
378 *error_out
= "Can't specify both 'd' and a cow file";
382 *backing_file
= '\0';
388 ubd_dev
->cow
.file
= backing_file
;
389 ubd_dev
->boot_openflags
= flags
;
391 mutex_unlock(&ubd_lock
);
395 static int ubd_setup(char *str
)
400 err
= ubd_setup_common(str
, NULL
, &error
);
402 printk(KERN_ERR
"Failed to initialize device with \"%s\" : "
407 __setup("ubd", ubd_setup
);
408 __uml_help(ubd_setup
,
409 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
410 " This is used to associate a device with a file in the underlying\n"
411 " filesystem. When specifying two filenames, the first one is the\n"
412 " COW name and the second is the backing file name. As separator you can\n"
413 " use either a ':' or a ',': the first one allows writing things like;\n"
414 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
415 " while with a ',' the shell would not expand the 2nd '~'.\n"
416 " When using only one filename, UML will detect whether to treat it like\n"
417 " a COW file or a backing file. To override this detection, add the 'd'\n"
419 " ubd0d=BackingFile\n"
420 " Usually, there is a filesystem in the file, but \n"
421 " that's not required. Swap devices containing swap files can be\n"
422 " specified like this. Also, a file which doesn't contain a\n"
423 " filesystem can have its contents read in the virtual \n"
424 " machine by running 'dd' on the device. <n> must be in the range\n"
425 " 0 to 7. Appending an 'r' to the number will cause that device\n"
426 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
427 " an 's' will cause data to be written to disk on the host immediately.\n"
428 " 'c' will cause the device to be treated as being shared between multiple\n"
429 " UMLs and file locking will be turned off - this is appropriate for a\n"
430 " cluster filesystem and inappropriate at almost all other times.\n\n"
433 static int udb_setup(char *str
)
435 printk("udb%s specified on command line is almost certainly a ubd -> "
440 __setup("udb", udb_setup
);
441 __uml_help(udb_setup
,
443 " This option is here solely to catch ubd -> udb typos, which can be\n"
444 " to impossible to catch visually unless you specifically look for\n"
445 " them. The only result of any option starting with 'udb' is an error\n"
446 " in the boot output.\n\n"
449 static void do_ubd_request(struct request_queue
* q
);
451 /* Only changed by ubd_init, which is an initcall. */
452 static int thread_fd
= -1;
454 static void ubd_end_request(struct request
*req
, int bytes
, int error
)
456 blk_end_request(req
, error
, bytes
);
459 /* Callable only from interrupt context - otherwise you need to do
460 * spin_lock_irq()/spin_lock_irqsave() */
461 static inline void ubd_finish(struct request
*req
, int bytes
)
464 ubd_end_request(req
, 0, -EIO
);
467 ubd_end_request(req
, bytes
, 0);
470 static LIST_HEAD(restart
);
472 /* XXX - move this inside ubd_intr. */
473 /* Called without dev->lock held, and only in interrupt context. */
474 static void ubd_handler(void)
476 struct io_thread_req
*req
;
479 struct list_head
*list
, *next_ele
;
484 n
= os_read_file(thread_fd
, &req
,
485 sizeof(struct io_thread_req
*));
486 if(n
!= sizeof(req
)){
489 printk(KERN_ERR
"spurious interrupt in ubd_handler, "
495 rq
->nr_sectors
-= req
->length
>> 9;
496 if(rq
->nr_sectors
== 0)
497 ubd_finish(rq
, rq
->hard_nr_sectors
<< 9);
500 reactivate_fd(thread_fd
, UBD_IRQ
);
502 list_for_each_safe(list
, next_ele
, &restart
){
503 ubd
= container_of(list
, struct ubd
, restart
);
504 list_del_init(&ubd
->restart
);
505 spin_lock_irqsave(&ubd
->lock
, flags
);
506 do_ubd_request(ubd
->queue
);
507 spin_unlock_irqrestore(&ubd
->lock
, flags
);
511 static irqreturn_t
ubd_intr(int irq
, void *dev
)
517 /* Only changed by ubd_init, which is an initcall. */
518 static int io_pid
= -1;
520 static void kill_io_thread(void)
523 os_kill_process(io_pid
, 1);
526 __uml_exitcall(kill_io_thread
);
528 static inline int ubd_file_size(struct ubd
*ubd_dev
, __u64
*size_out
)
532 file
= ubd_dev
->cow
.file
? ubd_dev
->cow
.file
: ubd_dev
->file
;
533 return os_file_size(file
, size_out
);
536 static int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
540 err
= os_seek_file(fd
, offset
);
544 err
= os_read_file(fd
, buf
, len
);
551 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
553 unsigned long modtime
;
554 unsigned long long actual
;
557 err
= os_file_modtime(file
, &modtime
);
559 printk(KERN_ERR
"Failed to get modification time of backing "
560 "file \"%s\", err = %d\n", file
, -err
);
564 err
= os_file_size(file
, &actual
);
566 printk(KERN_ERR
"Failed to get size of backing file \"%s\", "
567 "err = %d\n", file
, -err
);
571 if (actual
!= size
) {
572 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
574 printk(KERN_ERR
"Size mismatch (%llu vs %llu) of COW header "
575 "vs backing file\n", (unsigned long long) size
, actual
);
578 if (modtime
!= mtime
) {
579 printk(KERN_ERR
"mtime mismatch (%ld vs %ld) of COW header vs "
580 "backing file\n", mtime
, modtime
);
586 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
588 struct uml_stat buf1
, buf2
;
591 if (from_cmdline
== NULL
)
593 if (!strcmp(from_cmdline
, from_cow
))
596 err
= os_stat_file(from_cmdline
, &buf1
);
598 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cmdline
,
602 err
= os_stat_file(from_cow
, &buf2
);
604 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cow
,
608 if ((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
611 printk(KERN_ERR
"Backing file mismatch - \"%s\" requested, "
612 "\"%s\" specified in COW header of \"%s\"\n",
613 from_cmdline
, from_cow
, cow
);
617 static int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
618 char **backing_file_out
, int *bitmap_offset_out
,
619 unsigned long *bitmap_len_out
, int *data_offset_out
,
623 unsigned long long size
;
624 __u32 version
, align
;
626 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
628 fd
= os_open_file(file
, *openflags
, mode
);
630 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
633 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
636 fd
= os_open_file(file
, *openflags
, mode
);
642 printk(KERN_INFO
"Not locking \"%s\" on the host\n", file
);
644 err
= os_lock_file(fd
, openflags
->w
);
646 printk(KERN_ERR
"Failed to lock '%s', err = %d\n",
652 /* Successful return case! */
653 if (backing_file_out
== NULL
)
656 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
657 &size
, §orsize
, &align
, bitmap_offset_out
);
658 if (err
&& (*backing_file_out
!= NULL
)) {
659 printk(KERN_ERR
"Failed to read COW header from COW file "
660 "\"%s\", errno = %d\n", file
, -err
);
666 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
,
669 /* Allow switching only if no mismatch. */
670 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
,
672 printk(KERN_ERR
"Switching backing file to '%s'\n",
674 err
= write_cow_header(file
, fd
, *backing_file_out
,
675 sectorsize
, align
, &size
);
677 printk(KERN_ERR
"Switch failed, errno = %d\n", -err
);
681 *backing_file_out
= backing_file
;
682 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
687 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
688 bitmap_len_out
, data_offset_out
);
696 static int create_cow_file(char *cow_file
, char *backing_file
,
697 struct openflags flags
,
698 int sectorsize
, int alignment
, int *bitmap_offset_out
,
699 unsigned long *bitmap_len_out
, int *data_offset_out
)
704 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
707 printk(KERN_ERR
"Open of COW file '%s' failed, errno = %d\n",
712 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
713 bitmap_offset_out
, bitmap_len_out
,
722 static void ubd_close_dev(struct ubd
*ubd_dev
)
724 os_close_file(ubd_dev
->fd
);
725 if(ubd_dev
->cow
.file
== NULL
)
728 os_close_file(ubd_dev
->cow
.fd
);
729 vfree(ubd_dev
->cow
.bitmap
);
730 ubd_dev
->cow
.bitmap
= NULL
;
733 static int ubd_open_dev(struct ubd
*ubd_dev
)
735 struct openflags flags
;
737 int err
, create_cow
, *create_ptr
;
740 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
742 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
743 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
745 fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
746 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
747 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
750 if((fd
== -ENOENT
) && create_cow
){
751 fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
752 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
753 &ubd_dev
->cow
.bitmap_offset
,
754 &ubd_dev
->cow
.bitmap_len
,
755 &ubd_dev
->cow
.data_offset
);
757 printk(KERN_INFO
"Creating \"%s\" as COW file for "
758 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
763 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
769 if(ubd_dev
->cow
.file
!= NULL
){
770 blk_queue_max_sectors(ubd_dev
->queue
, 8 * sizeof(long));
773 ubd_dev
->cow
.bitmap
= vmalloc(ubd_dev
->cow
.bitmap_len
);
774 if(ubd_dev
->cow
.bitmap
== NULL
){
775 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
778 flush_tlb_kernel_vm();
780 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
781 ubd_dev
->cow
.bitmap_offset
,
782 ubd_dev
->cow
.bitmap_len
);
786 flags
= ubd_dev
->openflags
;
788 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
789 NULL
, NULL
, NULL
, NULL
);
790 if(err
< 0) goto error
;
791 ubd_dev
->cow
.fd
= err
;
795 os_close_file(ubd_dev
->fd
);
799 static void ubd_device_release(struct device
*dev
)
801 struct ubd
*ubd_dev
= dev
->driver_data
;
803 blk_cleanup_queue(ubd_dev
->queue
);
804 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
807 static int ubd_disk_register(int major
, u64 size
, int unit
,
808 struct gendisk
**disk_out
)
810 struct gendisk
*disk
;
812 disk
= alloc_disk(1 << UBD_SHIFT
);
817 disk
->first_minor
= unit
<< UBD_SHIFT
;
818 disk
->fops
= &ubd_blops
;
819 set_capacity(disk
, size
/ 512);
820 if (major
== UBD_MAJOR
)
821 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
823 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
825 /* sysfs register (not for ide fake devices) */
826 if (major
== UBD_MAJOR
) {
827 ubd_devs
[unit
].pdev
.id
= unit
;
828 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
829 ubd_devs
[unit
].pdev
.dev
.release
= ubd_device_release
;
830 ubd_devs
[unit
].pdev
.dev
.driver_data
= &ubd_devs
[unit
];
831 platform_device_register(&ubd_devs
[unit
].pdev
);
832 disk
->driverfs_dev
= &ubd_devs
[unit
].pdev
.dev
;
835 disk
->private_data
= &ubd_devs
[unit
];
836 disk
->queue
= ubd_devs
[unit
].queue
;
843 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
845 static int ubd_add(int n
, char **error_out
)
847 struct ubd
*ubd_dev
= &ubd_devs
[n
];
850 if(ubd_dev
->file
== NULL
)
853 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
855 *error_out
= "Couldn't determine size of device's file";
859 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
861 INIT_LIST_HEAD(&ubd_dev
->restart
);
862 sg_init_table(ubd_dev
->sg
, MAX_SG
);
865 ubd_dev
->queue
= blk_init_queue(do_ubd_request
, &ubd_dev
->lock
);
866 if (ubd_dev
->queue
== NULL
) {
867 *error_out
= "Failed to initialize device queue";
870 ubd_dev
->queue
->queuedata
= ubd_dev
;
872 blk_queue_max_hw_segments(ubd_dev
->queue
, MAX_SG
);
873 err
= ubd_disk_register(UBD_MAJOR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
875 *error_out
= "Failed to register device";
879 if (fake_major
!= UBD_MAJOR
)
880 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
884 * Perhaps this should also be under the "if (fake_major)" above
885 * using the fake_disk->disk_name
888 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
895 blk_cleanup_queue(ubd_dev
->queue
);
899 static int ubd_config(char *str
, char **error_out
)
903 /* This string is possibly broken up and stored, so it's only
904 * freed if ubd_setup_common fails, or if only general options
907 str
= kstrdup(str
, GFP_KERNEL
);
909 *error_out
= "Failed to allocate memory";
913 ret
= ubd_setup_common(str
, &n
, error_out
);
922 mutex_lock(&ubd_lock
);
923 ret
= ubd_add(n
, error_out
);
925 ubd_devs
[n
].file
= NULL
;
926 mutex_unlock(&ubd_lock
);
936 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
941 n
= parse_unit(&name
);
942 if((n
>= MAX_DEV
) || (n
< 0)){
943 *error_out
= "ubd_get_config : device number out of range";
947 ubd_dev
= &ubd_devs
[n
];
948 mutex_lock(&ubd_lock
);
950 if(ubd_dev
->file
== NULL
){
951 CONFIG_CHUNK(str
, size
, len
, "", 1);
955 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
957 if(ubd_dev
->cow
.file
!= NULL
){
958 CONFIG_CHUNK(str
, size
, len
, ",", 0);
959 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
961 else CONFIG_CHUNK(str
, size
, len
, "", 1);
964 mutex_unlock(&ubd_lock
);
968 static int ubd_id(char **str
, int *start_out
, int *end_out
)
974 *end_out
= MAX_DEV
- 1;
978 static int ubd_remove(int n
, char **error_out
)
980 struct gendisk
*disk
= ubd_gendisk
[n
];
984 mutex_lock(&ubd_lock
);
986 ubd_dev
= &ubd_devs
[n
];
988 if(ubd_dev
->file
== NULL
)
991 /* you cannot remove a open disk */
993 if(ubd_dev
->count
> 0)
996 ubd_gendisk
[n
] = NULL
;
1002 if(fake_gendisk
[n
] != NULL
){
1003 del_gendisk(fake_gendisk
[n
]);
1004 put_disk(fake_gendisk
[n
]);
1005 fake_gendisk
[n
] = NULL
;
1009 platform_device_unregister(&ubd_dev
->pdev
);
1011 mutex_unlock(&ubd_lock
);
1015 /* All these are called by mconsole in process context and without
1016 * ubd-specific locks. The structure itself is const except for .list.
1018 static struct mc_device ubd_mc
= {
1019 .list
= LIST_HEAD_INIT(ubd_mc
.list
),
1021 .config
= ubd_config
,
1022 .get_config
= ubd_get_config
,
1024 .remove
= ubd_remove
,
1027 static int __init
ubd_mc_init(void)
1029 mconsole_register_dev(&ubd_mc
);
1033 __initcall(ubd_mc_init
);
1035 static int __init
ubd0_init(void)
1037 struct ubd
*ubd_dev
= &ubd_devs
[0];
1039 mutex_lock(&ubd_lock
);
1040 if(ubd_dev
->file
== NULL
)
1041 ubd_dev
->file
= "root_fs";
1042 mutex_unlock(&ubd_lock
);
1047 __initcall(ubd0_init
);
1049 /* Used in ubd_init, which is an initcall */
1050 static struct platform_driver ubd_driver
= {
1052 .name
= DRIVER_NAME
,
1056 static int __init
ubd_init(void)
1061 if (register_blkdev(UBD_MAJOR
, "ubd"))
1064 if (fake_major
!= UBD_MAJOR
) {
1065 char name
[sizeof("ubd_nnn\0")];
1067 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
1068 if (register_blkdev(fake_major
, "ubd"))
1071 platform_driver_register(&ubd_driver
);
1072 mutex_lock(&ubd_lock
);
1073 for (i
= 0; i
< MAX_DEV
; i
++){
1074 err
= ubd_add(i
, &error
);
1076 printk(KERN_ERR
"Failed to initialize ubd device %d :"
1079 mutex_unlock(&ubd_lock
);
1083 late_initcall(ubd_init
);
1085 static int __init
ubd_driver_init(void){
1086 unsigned long stack
;
1089 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1090 if(global_openflags
.s
){
1091 printk(KERN_INFO
"ubd: Synchronous mode\n");
1092 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1093 * enough. So use anyway the io thread. */
1095 stack
= alloc_stack(0, 0);
1096 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
1100 "ubd : Failed to start I/O thread (errno = %d) - "
1101 "falling back to synchronous I/O\n", -io_pid
);
1105 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
1106 IRQF_DISABLED
, "ubd", ubd_devs
);
1108 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
1112 device_initcall(ubd_driver_init
);
1114 static int ubd_open(struct block_device
*bdev
, fmode_t mode
)
1116 struct gendisk
*disk
= bdev
->bd_disk
;
1117 struct ubd
*ubd_dev
= disk
->private_data
;
1120 if(ubd_dev
->count
== 0){
1121 err
= ubd_open_dev(ubd_dev
);
1123 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
1124 disk
->disk_name
, ubd_dev
->file
, -err
);
1129 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
1131 /* This should no more be needed. And it didn't work anyway to exclude
1132 * read-write remounting of filesystems.*/
1133 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1134 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1141 static int ubd_release(struct gendisk
*disk
, fmode_t mode
)
1143 struct ubd
*ubd_dev
= disk
->private_data
;
1145 if(--ubd_dev
->count
== 0)
1146 ubd_close_dev(ubd_dev
);
1150 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
1151 __u64
*cow_offset
, unsigned long *bitmap
,
1152 __u64 bitmap_offset
, unsigned long *bitmap_words
,
1155 __u64 sector
= io_offset
>> 9;
1156 int i
, update_bitmap
= 0;
1158 for(i
= 0; i
< length
>> 9; i
++){
1159 if(cow_mask
!= NULL
)
1160 ubd_set_bit(i
, (unsigned char *) cow_mask
);
1161 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1165 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
1171 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
1173 /* This takes care of the case where we're exactly at the end of the
1174 * device, and *cow_offset + 1 is off the end. So, just back it up
1175 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1176 * for the original diagnosis.
1178 if (*cow_offset
== (DIV_ROUND_UP(bitmap_len
,
1179 sizeof(unsigned long)) - 1))
1182 bitmap_words
[0] = bitmap
[*cow_offset
];
1183 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
1185 *cow_offset
*= sizeof(unsigned long);
1186 *cow_offset
+= bitmap_offset
;
1189 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
1190 __u64 bitmap_offset
, __u64 bitmap_len
)
1192 __u64 sector
= req
->offset
>> 9;
1195 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
1196 panic("Operation too long");
1198 if(req
->op
== UBD_READ
) {
1199 for(i
= 0; i
< req
->length
>> 9; i
++){
1200 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1201 ubd_set_bit(i
, (unsigned char *)
1205 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1206 &req
->cow_offset
, bitmap
, bitmap_offset
,
1207 req
->bitmap_words
, bitmap_len
);
1210 /* Called with dev->lock held */
1211 static void prepare_request(struct request
*req
, struct io_thread_req
*io_req
,
1212 unsigned long long offset
, int page_offset
,
1213 int len
, struct page
*page
)
1215 struct gendisk
*disk
= req
->rq_disk
;
1216 struct ubd
*ubd_dev
= disk
->private_data
;
1219 io_req
->fds
[0] = (ubd_dev
->cow
.file
!= NULL
) ? ubd_dev
->cow
.fd
:
1221 io_req
->fds
[1] = ubd_dev
->fd
;
1222 io_req
->cow_offset
= -1;
1223 io_req
->offset
= offset
;
1224 io_req
->length
= len
;
1226 io_req
->sector_mask
= 0;
1228 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1229 io_req
->offsets
[0] = 0;
1230 io_req
->offsets
[1] = ubd_dev
->cow
.data_offset
;
1231 io_req
->buffer
= page_address(page
) + page_offset
;
1232 io_req
->sectorsize
= 1 << 9;
1234 if(ubd_dev
->cow
.file
!= NULL
)
1235 cowify_req(io_req
, ubd_dev
->cow
.bitmap
,
1236 ubd_dev
->cow
.bitmap_offset
, ubd_dev
->cow
.bitmap_len
);
1240 /* Called with dev->lock held */
1241 static void do_ubd_request(struct request_queue
*q
)
1243 struct io_thread_req
*io_req
;
1244 struct request
*req
;
1245 int n
, last_sectors
;
1248 struct ubd
*dev
= q
->queuedata
;
1249 if(dev
->end_sg
== 0){
1250 struct request
*req
= elv_next_request(q
);
1255 blkdev_dequeue_request(req
);
1257 dev
->end_sg
= blk_rq_map_sg(q
, req
, dev
->sg
);
1262 while(dev
->start_sg
< dev
->end_sg
){
1263 struct scatterlist
*sg
= &dev
->sg
[dev
->start_sg
];
1265 req
->sector
+= last_sectors
;
1266 io_req
= kmalloc(sizeof(struct io_thread_req
),
1269 if(list_empty(&dev
->restart
))
1270 list_add(&dev
->restart
, &restart
);
1273 prepare_request(req
, io_req
,
1274 (unsigned long long) req
->sector
<< 9,
1275 sg
->offset
, sg
->length
, sg_page(sg
));
1277 last_sectors
= sg
->length
>> 9;
1278 n
= os_write_file(thread_fd
, &io_req
,
1279 sizeof(struct io_thread_req
*));
1280 if(n
!= sizeof(struct io_thread_req
*)){
1282 printk("write to io thread failed, "
1283 "errno = %d\n", -n
);
1284 else if(list_empty(&dev
->restart
))
1285 list_add(&dev
->restart
, &restart
);
1293 dev
->request
= NULL
;
1297 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1299 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1303 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1307 static int ubd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1308 unsigned int cmd
, unsigned long arg
)
1310 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1311 struct hd_driveid ubd_id
= {
1318 struct cdrom_volctrl volume
;
1319 case HDIO_GET_IDENTITY
:
1320 ubd_id
.cyls
= ubd_dev
->size
/ (128 * 32 * 512);
1321 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1327 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1329 volume
.channel0
= 255;
1330 volume
.channel1
= 255;
1331 volume
.channel2
= 255;
1332 volume
.channel3
= 255;
1333 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1340 static int update_bitmap(struct io_thread_req
*req
)
1344 if(req
->cow_offset
== -1)
1347 n
= os_seek_file(req
->fds
[1], req
->cow_offset
);
1349 printk("do_io - bitmap lseek failed : err = %d\n", -n
);
1353 n
= os_write_file(req
->fds
[1], &req
->bitmap_words
,
1354 sizeof(req
->bitmap_words
));
1355 if(n
!= sizeof(req
->bitmap_words
)){
1356 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1364 static void do_io(struct io_thread_req
*req
)
1368 int n
, nsectors
, start
, end
, bit
;
1372 nsectors
= req
->length
/ req
->sectorsize
;
1375 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1377 while((end
< nsectors
) &&
1378 (ubd_test_bit(end
, (unsigned char *)
1379 &req
->sector_mask
) == bit
))
1382 off
= req
->offset
+ req
->offsets
[bit
] +
1383 start
* req
->sectorsize
;
1384 len
= (end
- start
) * req
->sectorsize
;
1385 buf
= &req
->buffer
[start
* req
->sectorsize
];
1387 err
= os_seek_file(req
->fds
[bit
], off
);
1389 printk("do_io - lseek failed : err = %d\n", -err
);
1393 if(req
->op
== UBD_READ
){
1398 n
= os_read_file(req
->fds
[bit
], buf
, len
);
1400 printk("do_io - read failed, err = %d "
1401 "fd = %d\n", -n
, req
->fds
[bit
]);
1405 } while((n
< len
) && (n
!= 0));
1406 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1408 n
= os_write_file(req
->fds
[bit
], buf
, len
);
1410 printk("do_io - write failed err = %d "
1411 "fd = %d\n", -n
, req
->fds
[bit
]);
1418 } while(start
< nsectors
);
1420 req
->error
= update_bitmap(req
);
1423 /* Changed in start_io_thread, which is serialized by being called only
1424 * from ubd_init, which is an initcall.
1428 /* Only changed by the io thread. XXX: currently unused. */
1429 static int io_count
= 0;
1431 int io_thread(void *arg
)
1433 struct io_thread_req
*req
;
1436 ignore_sigwinch_sig();
1438 n
= os_read_file(kernel_fd
, &req
,
1439 sizeof(struct io_thread_req
*));
1440 if(n
!= sizeof(struct io_thread_req
*)){
1442 printk("io_thread - read failed, fd = %d, "
1443 "err = %d\n", kernel_fd
, -n
);
1445 printk("io_thread - short read, fd = %d, "
1446 "length = %d\n", kernel_fd
, n
);
1452 n
= os_write_file(kernel_fd
, &req
,
1453 sizeof(struct io_thread_req
*));
1454 if(n
!= sizeof(struct io_thread_req
*))
1455 printk("io_thread - write failed, fd = %d, err = %d\n",