2 * Copyright (C) 2015-2016 Anton Ivanov (aivanov@brocade.com)
3 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
4 * Licensed under the GPL
7 /* 2001-09-28...2002-04-17
8 * Partition stuff by James_McMechan@hotmail.com
9 * old style ubd by setting UBD_SHIFT to 0
10 * 2002-09-27...2002-10-18 massive tinkering for 2.5
11 * partitions have changed in 2.5
12 * 2003-01-29 more tinkering for 2.5.59-1
13 * This should now address the sysfs problems and has
14 * the symlink for devfs to allow for booting with
15 * the common /dev/ubd/discX/... names rather than
16 * only /dev/ubdN/discN this version also has lots of
17 * clean ups preparing for ubd-many.
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/blkdev.h>
26 #include <linux/blk-mq.h>
27 #include <linux/ata.h>
28 #include <linux/hdreg.h>
29 #include <linux/cdrom.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/ctype.h>
33 #include <linux/slab.h>
34 #include <linux/vmalloc.h>
35 #include <linux/platform_device.h>
36 #include <linux/scatterlist.h>
37 #include <asm/tlbflush.h>
38 #include <kern_util.h>
39 #include "mconsole_kern.h"
46 enum ubd_req
{ UBD_READ
, UBD_WRITE
, UBD_FLUSH
};
48 struct io_thread_req
{
52 unsigned long offsets
[2];
53 unsigned long long offset
;
57 unsigned long sector_mask
;
58 unsigned long long cow_offset
;
59 unsigned long bitmap_words
[2];
64 static struct io_thread_req
* (*irq_req_buffer
)[];
65 static struct io_thread_req
*irq_remainder
;
66 static int irq_remainder_size
;
68 static struct io_thread_req
* (*io_req_buffer
)[];
69 static struct io_thread_req
*io_remainder
;
70 static int io_remainder_size
;
74 static inline int ubd_test_bit(__u64 bit
, unsigned char *data
)
79 bits
= sizeof(data
[0]) * 8;
82 return (data
[n
] & (1 << off
)) != 0;
85 static inline void ubd_set_bit(__u64 bit
, unsigned char *data
)
90 bits
= sizeof(data
[0]) * 8;
93 data
[n
] |= (1 << off
);
95 /*End stuff from ubd_user.h*/
97 #define DRIVER_NAME "uml-blkdev"
99 static DEFINE_MUTEX(ubd_lock
);
100 static DEFINE_MUTEX(ubd_mutex
); /* replaces BKL, might not be needed */
102 static int ubd_open(struct block_device
*bdev
, fmode_t mode
);
103 static void ubd_release(struct gendisk
*disk
, fmode_t mode
);
104 static int ubd_ioctl(struct block_device
*bdev
, fmode_t mode
,
105 unsigned int cmd
, unsigned long arg
);
106 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
);
110 static const 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
= UBD_MAJOR
;
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 /* name (and fd, below) of the file opened for writing, either the
147 * backing or the cow file. */
152 struct openflags boot_openflags
;
153 struct openflags openflags
;
157 struct platform_device pdev
;
158 struct request_queue
*queue
;
159 struct blk_mq_tag_set tag_set
;
163 #define DEFAULT_COW { \
167 .bitmap_offset = 0, \
171 #define DEFAULT_UBD { \
176 .boot_openflags = OPEN_FLAGS, \
177 .openflags = OPEN_FLAGS, \
180 .cow = DEFAULT_COW, \
181 .lock = __SPIN_LOCK_UNLOCKED(ubd_devs.lock), \
184 /* Protected by ubd_lock */
185 static struct ubd ubd_devs
[MAX_DEV
] = { [0 ... MAX_DEV
- 1] = DEFAULT_UBD
};
187 /* Only changed by fake_ide_setup which is a setup */
188 static int fake_ide
= 0;
189 static struct proc_dir_entry
*proc_ide_root
= NULL
;
190 static struct proc_dir_entry
*proc_ide
= NULL
;
192 static blk_status_t
ubd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
193 const struct blk_mq_queue_data
*bd
);
195 static void make_proc_ide(void)
197 proc_ide_root
= proc_mkdir("ide", NULL
);
198 proc_ide
= proc_mkdir("ide0", proc_ide_root
);
201 static int fake_ide_media_proc_show(struct seq_file
*m
, void *v
)
203 seq_puts(m
, "disk\n");
207 static void make_ide_entries(const char *dev_name
)
209 struct proc_dir_entry
*dir
, *ent
;
212 if(proc_ide_root
== NULL
) make_proc_ide();
214 dir
= proc_mkdir(dev_name
, proc_ide
);
217 ent
= proc_create_single("media", S_IRUGO
, dir
,
218 fake_ide_media_proc_show
);
220 snprintf(name
, sizeof(name
), "ide0/%s", dev_name
);
221 proc_symlink(dev_name
, proc_ide_root
, name
);
224 static int fake_ide_setup(char *str
)
230 __setup("fake_ide", fake_ide_setup
);
232 __uml_help(fake_ide_setup
,
234 " Create ide0 entries that map onto ubd devices.\n\n"
237 static int parse_unit(char **ptr
)
239 char *str
= *ptr
, *end
;
243 n
= simple_strtoul(str
, &end
, 0);
248 else if (('a' <= *str
) && (*str
<= 'z')) {
256 /* If *index_out == -1 at exit, the passed option was a general one;
257 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
258 * should not be freed on exit.
260 static int ubd_setup_common(char *str
, int *index_out
, char **error_out
)
263 struct openflags flags
= global_openflags
;
267 if(index_out
) *index_out
= -1;
274 if(!strcmp(str
, "sync")){
275 global_openflags
= of_sync(global_openflags
);
280 major
= simple_strtoul(str
, &end
, 0);
281 if((*end
!= '\0') || (end
== str
)){
282 *error_out
= "Didn't parse major number";
286 mutex_lock(&ubd_lock
);
287 if (fake_major
!= UBD_MAJOR
) {
288 *error_out
= "Can't assign a fake major twice";
294 printk(KERN_INFO
"Setting extra ubd major number to %d\n",
298 mutex_unlock(&ubd_lock
);
302 n
= parse_unit(&str
);
304 *error_out
= "Couldn't parse device number";
308 *error_out
= "Device number out of range";
313 mutex_lock(&ubd_lock
);
315 ubd_dev
= &ubd_devs
[n
];
316 if(ubd_dev
->file
!= NULL
){
317 *error_out
= "Device is already configured";
325 for (i
= 0; i
< sizeof("rscd="); i
++) {
343 *error_out
= "Expected '=' or flag letter "
351 *error_out
= "Too many flags specified";
353 *error_out
= "Missing '='";
357 backing_file
= strchr(str
, ',');
359 if (backing_file
== NULL
)
360 backing_file
= strchr(str
, ':');
362 if(backing_file
!= NULL
){
364 *error_out
= "Can't specify both 'd' and a cow file";
368 *backing_file
= '\0';
374 ubd_dev
->cow
.file
= backing_file
;
375 ubd_dev
->boot_openflags
= flags
;
377 mutex_unlock(&ubd_lock
);
381 static int ubd_setup(char *str
)
386 err
= ubd_setup_common(str
, NULL
, &error
);
388 printk(KERN_ERR
"Failed to initialize device with \"%s\" : "
393 __setup("ubd", ubd_setup
);
394 __uml_help(ubd_setup
,
395 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
396 " This is used to associate a device with a file in the underlying\n"
397 " filesystem. When specifying two filenames, the first one is the\n"
398 " COW name and the second is the backing file name. As separator you can\n"
399 " use either a ':' or a ',': the first one allows writing things like;\n"
400 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
401 " while with a ',' the shell would not expand the 2nd '~'.\n"
402 " When using only one filename, UML will detect whether to treat it like\n"
403 " a COW file or a backing file. To override this detection, add the 'd'\n"
405 " ubd0d=BackingFile\n"
406 " Usually, there is a filesystem in the file, but \n"
407 " that's not required. Swap devices containing swap files can be\n"
408 " specified like this. Also, a file which doesn't contain a\n"
409 " filesystem can have its contents read in the virtual \n"
410 " machine by running 'dd' on the device. <n> must be in the range\n"
411 " 0 to 7. Appending an 'r' to the number will cause that device\n"
412 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
413 " an 's' will cause data to be written to disk on the host immediately.\n"
414 " 'c' will cause the device to be treated as being shared between multiple\n"
415 " UMLs and file locking will be turned off - this is appropriate for a\n"
416 " cluster filesystem and inappropriate at almost all other times.\n\n"
419 static int udb_setup(char *str
)
421 printk("udb%s specified on command line is almost certainly a ubd -> "
426 __setup("udb", udb_setup
);
427 __uml_help(udb_setup
,
429 " This option is here solely to catch ubd -> udb typos, which can be\n"
430 " to impossible to catch visually unless you specifically look for\n"
431 " them. The only result of any option starting with 'udb' is an error\n"
432 " in the boot output.\n\n"
435 /* Only changed by ubd_init, which is an initcall. */
436 static int thread_fd
= -1;
438 /* Function to read several request pointers at a time
439 * handling fractional reads if (and as) needed
442 static int bulk_req_safe_read(
444 struct io_thread_req
* (*request_buffer
)[],
445 struct io_thread_req
**remainder
,
453 if (*remainder_size
> 0) {
455 (char *) request_buffer
,
456 (char *) remainder
, *remainder_size
463 ((char *) request_buffer
) + *remainder_size
,
464 sizeof(struct io_thread_req
*)*max_recs
469 if ((n
% sizeof(struct io_thread_req
*)) > 0) {
471 * Read somehow returned not a multiple of dword
472 * theoretically possible, but never observed in the
473 * wild, so read routine must be able to handle it
475 *remainder_size
= n
% sizeof(struct io_thread_req
*);
476 WARN(*remainder_size
> 0, "UBD IPC read returned a partial result");
479 ((char *) request_buffer
) +
480 (n
/sizeof(struct io_thread_req
*))*sizeof(struct io_thread_req
*),
483 n
= n
- *remainder_size
;
491 /* Called without dev->lock held, and only in interrupt context. */
492 static void ubd_handler(void)
498 n
= bulk_req_safe_read(
508 printk(KERN_ERR
"spurious interrupt in ubd_handler, "
512 for (count
= 0; count
< n
/sizeof(struct io_thread_req
*); count
++) {
513 struct io_thread_req
*io_req
= (*irq_req_buffer
)[count
];
514 int err
= io_req
->error
? BLK_STS_IOERR
: BLK_STS_OK
;
516 if (!blk_update_request(io_req
->req
, err
, io_req
->length
))
517 __blk_mq_end_request(io_req
->req
, err
);
523 reactivate_fd(thread_fd
, UBD_IRQ
);
526 static irqreturn_t
ubd_intr(int irq
, void *dev
)
532 /* Only changed by ubd_init, which is an initcall. */
533 static int io_pid
= -1;
535 static void kill_io_thread(void)
538 os_kill_process(io_pid
, 1);
541 __uml_exitcall(kill_io_thread
);
543 static inline int ubd_file_size(struct ubd
*ubd_dev
, __u64
*size_out
)
553 unsigned long long size
;
557 if (ubd_dev
->file
&& ubd_dev
->cow
.file
) {
558 file
= ubd_dev
->cow
.file
;
563 fd
= os_open_file(ubd_dev
->file
, of_read(OPENFLAGS()), 0);
567 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, \
568 &mtime
, &size
, §or_size
, &align
, &bitmap_offset
);
572 file
= ubd_dev
->file
;
577 return os_file_size(file
, size_out
);
580 static int read_cow_bitmap(int fd
, void *buf
, int offset
, int len
)
584 err
= os_pread_file(fd
, buf
, len
, offset
);
591 static int backing_file_mismatch(char *file
, __u64 size
, time_t mtime
)
593 unsigned long modtime
;
594 unsigned long long actual
;
597 err
= os_file_modtime(file
, &modtime
);
599 printk(KERN_ERR
"Failed to get modification time of backing "
600 "file \"%s\", err = %d\n", file
, -err
);
604 err
= os_file_size(file
, &actual
);
606 printk(KERN_ERR
"Failed to get size of backing file \"%s\", "
607 "err = %d\n", file
, -err
);
611 if (actual
!= size
) {
612 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
614 printk(KERN_ERR
"Size mismatch (%llu vs %llu) of COW header "
615 "vs backing file\n", (unsigned long long) size
, actual
);
618 if (modtime
!= mtime
) {
619 printk(KERN_ERR
"mtime mismatch (%ld vs %ld) of COW header vs "
620 "backing file\n", mtime
, modtime
);
626 static int path_requires_switch(char *from_cmdline
, char *from_cow
, char *cow
)
628 struct uml_stat buf1
, buf2
;
631 if (from_cmdline
== NULL
)
633 if (!strcmp(from_cmdline
, from_cow
))
636 err
= os_stat_file(from_cmdline
, &buf1
);
638 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cmdline
,
642 err
= os_stat_file(from_cow
, &buf2
);
644 printk(KERN_ERR
"Couldn't stat '%s', err = %d\n", from_cow
,
648 if ((buf1
.ust_dev
== buf2
.ust_dev
) && (buf1
.ust_ino
== buf2
.ust_ino
))
651 printk(KERN_ERR
"Backing file mismatch - \"%s\" requested, "
652 "\"%s\" specified in COW header of \"%s\"\n",
653 from_cmdline
, from_cow
, cow
);
657 static int open_ubd_file(char *file
, struct openflags
*openflags
, int shared
,
658 char **backing_file_out
, int *bitmap_offset_out
,
659 unsigned long *bitmap_len_out
, int *data_offset_out
,
663 unsigned long long size
;
664 __u32 version
, align
;
666 int fd
, err
, sectorsize
, asked_switch
, mode
= 0644;
668 fd
= os_open_file(file
, *openflags
, mode
);
670 if ((fd
== -ENOENT
) && (create_cow_out
!= NULL
))
673 ((fd
!= -EROFS
) && (fd
!= -EACCES
)))
676 fd
= os_open_file(file
, *openflags
, mode
);
682 printk(KERN_INFO
"Not locking \"%s\" on the host\n", file
);
684 err
= os_lock_file(fd
, openflags
->w
);
686 printk(KERN_ERR
"Failed to lock '%s', err = %d\n",
692 /* Successful return case! */
693 if (backing_file_out
== NULL
)
696 err
= read_cow_header(file_reader
, &fd
, &version
, &backing_file
, &mtime
,
697 &size
, §orsize
, &align
, bitmap_offset_out
);
698 if (err
&& (*backing_file_out
!= NULL
)) {
699 printk(KERN_ERR
"Failed to read COW header from COW file "
700 "\"%s\", errno = %d\n", file
, -err
);
706 asked_switch
= path_requires_switch(*backing_file_out
, backing_file
,
709 /* Allow switching only if no mismatch. */
710 if (asked_switch
&& !backing_file_mismatch(*backing_file_out
, size
,
712 printk(KERN_ERR
"Switching backing file to '%s'\n",
714 err
= write_cow_header(file
, fd
, *backing_file_out
,
715 sectorsize
, align
, &size
);
717 printk(KERN_ERR
"Switch failed, errno = %d\n", -err
);
721 *backing_file_out
= backing_file
;
722 err
= backing_file_mismatch(*backing_file_out
, size
, mtime
);
727 cow_sizes(version
, size
, sectorsize
, align
, *bitmap_offset_out
,
728 bitmap_len_out
, data_offset_out
);
736 static int create_cow_file(char *cow_file
, char *backing_file
,
737 struct openflags flags
,
738 int sectorsize
, int alignment
, int *bitmap_offset_out
,
739 unsigned long *bitmap_len_out
, int *data_offset_out
)
744 fd
= open_ubd_file(cow_file
, &flags
, 0, NULL
, NULL
, NULL
, NULL
, NULL
);
747 printk(KERN_ERR
"Open of COW file '%s' failed, errno = %d\n",
752 err
= init_cow_file(fd
, cow_file
, backing_file
, sectorsize
, alignment
,
753 bitmap_offset_out
, bitmap_len_out
,
762 static void ubd_close_dev(struct ubd
*ubd_dev
)
764 os_close_file(ubd_dev
->fd
);
765 if(ubd_dev
->cow
.file
== NULL
)
768 os_close_file(ubd_dev
->cow
.fd
);
769 vfree(ubd_dev
->cow
.bitmap
);
770 ubd_dev
->cow
.bitmap
= NULL
;
773 static int ubd_open_dev(struct ubd
*ubd_dev
)
775 struct openflags flags
;
777 int err
, create_cow
, *create_ptr
;
780 ubd_dev
->openflags
= ubd_dev
->boot_openflags
;
782 create_ptr
= (ubd_dev
->cow
.file
!= NULL
) ? &create_cow
: NULL
;
783 back_ptr
= ubd_dev
->no_cow
? NULL
: &ubd_dev
->cow
.file
;
785 fd
= open_ubd_file(ubd_dev
->file
, &ubd_dev
->openflags
, ubd_dev
->shared
,
786 back_ptr
, &ubd_dev
->cow
.bitmap_offset
,
787 &ubd_dev
->cow
.bitmap_len
, &ubd_dev
->cow
.data_offset
,
790 if((fd
== -ENOENT
) && create_cow
){
791 fd
= create_cow_file(ubd_dev
->file
, ubd_dev
->cow
.file
,
792 ubd_dev
->openflags
, 1 << 9, PAGE_SIZE
,
793 &ubd_dev
->cow
.bitmap_offset
,
794 &ubd_dev
->cow
.bitmap_len
,
795 &ubd_dev
->cow
.data_offset
);
797 printk(KERN_INFO
"Creating \"%s\" as COW file for "
798 "\"%s\"\n", ubd_dev
->file
, ubd_dev
->cow
.file
);
803 printk("Failed to open '%s', errno = %d\n", ubd_dev
->file
,
809 if(ubd_dev
->cow
.file
!= NULL
){
810 blk_queue_max_hw_sectors(ubd_dev
->queue
, 8 * sizeof(long));
813 ubd_dev
->cow
.bitmap
= vmalloc(ubd_dev
->cow
.bitmap_len
);
814 if(ubd_dev
->cow
.bitmap
== NULL
){
815 printk(KERN_ERR
"Failed to vmalloc COW bitmap\n");
818 flush_tlb_kernel_vm();
820 err
= read_cow_bitmap(ubd_dev
->fd
, ubd_dev
->cow
.bitmap
,
821 ubd_dev
->cow
.bitmap_offset
,
822 ubd_dev
->cow
.bitmap_len
);
826 flags
= ubd_dev
->openflags
;
828 err
= open_ubd_file(ubd_dev
->cow
.file
, &flags
, ubd_dev
->shared
, NULL
,
829 NULL
, NULL
, NULL
, NULL
);
830 if(err
< 0) goto error
;
831 ubd_dev
->cow
.fd
= err
;
835 os_close_file(ubd_dev
->fd
);
839 static void ubd_device_release(struct device
*dev
)
841 struct ubd
*ubd_dev
= dev_get_drvdata(dev
);
843 blk_cleanup_queue(ubd_dev
->queue
);
844 blk_mq_free_tag_set(&ubd_dev
->tag_set
);
845 *ubd_dev
= ((struct ubd
) DEFAULT_UBD
);
848 static int ubd_disk_register(int major
, u64 size
, int unit
,
849 struct gendisk
**disk_out
)
851 struct device
*parent
= NULL
;
852 struct gendisk
*disk
;
854 disk
= alloc_disk(1 << UBD_SHIFT
);
859 disk
->first_minor
= unit
<< UBD_SHIFT
;
860 disk
->fops
= &ubd_blops
;
861 set_capacity(disk
, size
/ 512);
862 if (major
== UBD_MAJOR
)
863 sprintf(disk
->disk_name
, "ubd%c", 'a' + unit
);
865 sprintf(disk
->disk_name
, "ubd_fake%d", unit
);
867 /* sysfs register (not for ide fake devices) */
868 if (major
== UBD_MAJOR
) {
869 ubd_devs
[unit
].pdev
.id
= unit
;
870 ubd_devs
[unit
].pdev
.name
= DRIVER_NAME
;
871 ubd_devs
[unit
].pdev
.dev
.release
= ubd_device_release
;
872 dev_set_drvdata(&ubd_devs
[unit
].pdev
.dev
, &ubd_devs
[unit
]);
873 platform_device_register(&ubd_devs
[unit
].pdev
);
874 parent
= &ubd_devs
[unit
].pdev
.dev
;
877 disk
->private_data
= &ubd_devs
[unit
];
878 disk
->queue
= ubd_devs
[unit
].queue
;
879 device_add_disk(parent
, disk
, NULL
);
885 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
887 static const struct blk_mq_ops ubd_mq_ops
= {
888 .queue_rq
= ubd_queue_rq
,
891 static int ubd_add(int n
, char **error_out
)
893 struct ubd
*ubd_dev
= &ubd_devs
[n
];
896 if(ubd_dev
->file
== NULL
)
899 err
= ubd_file_size(ubd_dev
, &ubd_dev
->size
);
901 *error_out
= "Couldn't determine size of device's file";
905 ubd_dev
->size
= ROUND_BLOCK(ubd_dev
->size
);
907 ubd_dev
->tag_set
.ops
= &ubd_mq_ops
;
908 ubd_dev
->tag_set
.queue_depth
= 64;
909 ubd_dev
->tag_set
.numa_node
= NUMA_NO_NODE
;
910 ubd_dev
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
;
911 ubd_dev
->tag_set
.driver_data
= ubd_dev
;
912 ubd_dev
->tag_set
.nr_hw_queues
= 1;
914 err
= blk_mq_alloc_tag_set(&ubd_dev
->tag_set
);
918 ubd_dev
->queue
= blk_mq_init_queue(&ubd_dev
->tag_set
);
919 if (IS_ERR(ubd_dev
->queue
)) {
920 err
= PTR_ERR(ubd_dev
->queue
);
924 ubd_dev
->queue
->queuedata
= ubd_dev
;
925 blk_queue_write_cache(ubd_dev
->queue
, true, false);
927 blk_queue_max_segments(ubd_dev
->queue
, MAX_SG
);
928 err
= ubd_disk_register(UBD_MAJOR
, ubd_dev
->size
, n
, &ubd_gendisk
[n
]);
930 *error_out
= "Failed to register device";
931 goto out_cleanup_tags
;
934 if (fake_major
!= UBD_MAJOR
)
935 ubd_disk_register(fake_major
, ubd_dev
->size
, n
,
939 * Perhaps this should also be under the "if (fake_major)" above
940 * using the fake_disk->disk_name
943 make_ide_entries(ubd_gendisk
[n
]->disk_name
);
950 blk_mq_free_tag_set(&ubd_dev
->tag_set
);
952 blk_cleanup_queue(ubd_dev
->queue
);
956 static int ubd_config(char *str
, char **error_out
)
960 /* This string is possibly broken up and stored, so it's only
961 * freed if ubd_setup_common fails, or if only general options
964 str
= kstrdup(str
, GFP_KERNEL
);
966 *error_out
= "Failed to allocate memory";
970 ret
= ubd_setup_common(str
, &n
, error_out
);
979 mutex_lock(&ubd_lock
);
980 ret
= ubd_add(n
, error_out
);
982 ubd_devs
[n
].file
= NULL
;
983 mutex_unlock(&ubd_lock
);
993 static int ubd_get_config(char *name
, char *str
, int size
, char **error_out
)
998 n
= parse_unit(&name
);
999 if((n
>= MAX_DEV
) || (n
< 0)){
1000 *error_out
= "ubd_get_config : device number out of range";
1004 ubd_dev
= &ubd_devs
[n
];
1005 mutex_lock(&ubd_lock
);
1007 if(ubd_dev
->file
== NULL
){
1008 CONFIG_CHUNK(str
, size
, len
, "", 1);
1012 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->file
, 0);
1014 if(ubd_dev
->cow
.file
!= NULL
){
1015 CONFIG_CHUNK(str
, size
, len
, ",", 0);
1016 CONFIG_CHUNK(str
, size
, len
, ubd_dev
->cow
.file
, 1);
1018 else CONFIG_CHUNK(str
, size
, len
, "", 1);
1021 mutex_unlock(&ubd_lock
);
1025 static int ubd_id(char **str
, int *start_out
, int *end_out
)
1029 n
= parse_unit(str
);
1031 *end_out
= MAX_DEV
- 1;
1035 static int ubd_remove(int n
, char **error_out
)
1037 struct gendisk
*disk
= ubd_gendisk
[n
];
1038 struct ubd
*ubd_dev
;
1041 mutex_lock(&ubd_lock
);
1043 ubd_dev
= &ubd_devs
[n
];
1045 if(ubd_dev
->file
== NULL
)
1048 /* you cannot remove a open disk */
1050 if(ubd_dev
->count
> 0)
1053 ubd_gendisk
[n
] = NULL
;
1059 if(fake_gendisk
[n
] != NULL
){
1060 del_gendisk(fake_gendisk
[n
]);
1061 put_disk(fake_gendisk
[n
]);
1062 fake_gendisk
[n
] = NULL
;
1066 platform_device_unregister(&ubd_dev
->pdev
);
1068 mutex_unlock(&ubd_lock
);
1072 /* All these are called by mconsole in process context and without
1073 * ubd-specific locks. The structure itself is const except for .list.
1075 static struct mc_device ubd_mc
= {
1076 .list
= LIST_HEAD_INIT(ubd_mc
.list
),
1078 .config
= ubd_config
,
1079 .get_config
= ubd_get_config
,
1081 .remove
= ubd_remove
,
1084 static int __init
ubd_mc_init(void)
1086 mconsole_register_dev(&ubd_mc
);
1090 __initcall(ubd_mc_init
);
1092 static int __init
ubd0_init(void)
1094 struct ubd
*ubd_dev
= &ubd_devs
[0];
1096 mutex_lock(&ubd_lock
);
1097 if(ubd_dev
->file
== NULL
)
1098 ubd_dev
->file
= "root_fs";
1099 mutex_unlock(&ubd_lock
);
1104 __initcall(ubd0_init
);
1106 /* Used in ubd_init, which is an initcall */
1107 static struct platform_driver ubd_driver
= {
1109 .name
= DRIVER_NAME
,
1113 static int __init
ubd_init(void)
1118 if (register_blkdev(UBD_MAJOR
, "ubd"))
1121 if (fake_major
!= UBD_MAJOR
) {
1122 char name
[sizeof("ubd_nnn\0")];
1124 snprintf(name
, sizeof(name
), "ubd_%d", fake_major
);
1125 if (register_blkdev(fake_major
, "ubd"))
1129 irq_req_buffer
= kmalloc_array(UBD_REQ_BUFFER_SIZE
,
1130 sizeof(struct io_thread_req
*),
1135 if (irq_req_buffer
== NULL
) {
1136 printk(KERN_ERR
"Failed to initialize ubd buffering\n");
1139 io_req_buffer
= kmalloc_array(UBD_REQ_BUFFER_SIZE
,
1140 sizeof(struct io_thread_req
*),
1146 if (io_req_buffer
== NULL
) {
1147 printk(KERN_ERR
"Failed to initialize ubd buffering\n");
1150 platform_driver_register(&ubd_driver
);
1151 mutex_lock(&ubd_lock
);
1152 for (i
= 0; i
< MAX_DEV
; i
++){
1153 err
= ubd_add(i
, &error
);
1155 printk(KERN_ERR
"Failed to initialize ubd device %d :"
1158 mutex_unlock(&ubd_lock
);
1162 late_initcall(ubd_init
);
1164 static int __init
ubd_driver_init(void){
1165 unsigned long stack
;
1168 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1169 if(global_openflags
.s
){
1170 printk(KERN_INFO
"ubd: Synchronous mode\n");
1171 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1172 * enough. So use anyway the io thread. */
1174 stack
= alloc_stack(0, 0);
1175 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
1179 "ubd : Failed to start I/O thread (errno = %d) - "
1180 "falling back to synchronous I/O\n", -io_pid
);
1184 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
1185 0, "ubd", ubd_devs
);
1187 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
1191 device_initcall(ubd_driver_init
);
1193 static int ubd_open(struct block_device
*bdev
, fmode_t mode
)
1195 struct gendisk
*disk
= bdev
->bd_disk
;
1196 struct ubd
*ubd_dev
= disk
->private_data
;
1199 mutex_lock(&ubd_mutex
);
1200 if(ubd_dev
->count
== 0){
1201 err
= ubd_open_dev(ubd_dev
);
1203 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
1204 disk
->disk_name
, ubd_dev
->file
, -err
);
1209 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
1211 /* This should no more be needed. And it didn't work anyway to exclude
1212 * read-write remounting of filesystems.*/
1213 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1214 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1218 mutex_unlock(&ubd_mutex
);
1222 static void ubd_release(struct gendisk
*disk
, fmode_t mode
)
1224 struct ubd
*ubd_dev
= disk
->private_data
;
1226 mutex_lock(&ubd_mutex
);
1227 if(--ubd_dev
->count
== 0)
1228 ubd_close_dev(ubd_dev
);
1229 mutex_unlock(&ubd_mutex
);
1232 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
1233 __u64
*cow_offset
, unsigned long *bitmap
,
1234 __u64 bitmap_offset
, unsigned long *bitmap_words
,
1237 __u64 sector
= io_offset
>> 9;
1238 int i
, update_bitmap
= 0;
1240 for(i
= 0; i
< length
>> 9; i
++){
1241 if(cow_mask
!= NULL
)
1242 ubd_set_bit(i
, (unsigned char *) cow_mask
);
1243 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1247 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
1253 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
1255 /* This takes care of the case where we're exactly at the end of the
1256 * device, and *cow_offset + 1 is off the end. So, just back it up
1257 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1258 * for the original diagnosis.
1260 if (*cow_offset
== (DIV_ROUND_UP(bitmap_len
,
1261 sizeof(unsigned long)) - 1))
1264 bitmap_words
[0] = bitmap
[*cow_offset
];
1265 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
1267 *cow_offset
*= sizeof(unsigned long);
1268 *cow_offset
+= bitmap_offset
;
1271 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
1272 __u64 bitmap_offset
, __u64 bitmap_len
)
1274 __u64 sector
= req
->offset
>> 9;
1277 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
1278 panic("Operation too long");
1280 if(req
->op
== UBD_READ
) {
1281 for(i
= 0; i
< req
->length
>> 9; i
++){
1282 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1283 ubd_set_bit(i
, (unsigned char *)
1287 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
1288 &req
->cow_offset
, bitmap
, bitmap_offset
,
1289 req
->bitmap_words
, bitmap_len
);
1292 static int ubd_queue_one_vec(struct blk_mq_hw_ctx
*hctx
, struct request
*req
,
1293 u64 off
, struct bio_vec
*bvec
)
1295 struct ubd
*dev
= hctx
->queue
->queuedata
;
1296 struct io_thread_req
*io_req
;
1299 io_req
= kmalloc(sizeof(struct io_thread_req
), GFP_ATOMIC
);
1305 io_req
->fds
[0] = dev
->cow
.fd
;
1307 io_req
->fds
[0] = dev
->fd
;
1309 if (req_op(req
) == REQ_OP_FLUSH
) {
1310 io_req
->op
= UBD_FLUSH
;
1312 io_req
->fds
[1] = dev
->fd
;
1313 io_req
->cow_offset
= -1;
1314 io_req
->offset
= off
;
1315 io_req
->length
= bvec
->bv_len
;
1317 io_req
->sector_mask
= 0;
1319 io_req
->op
= rq_data_dir(req
) == READ
? UBD_READ
: UBD_WRITE
;
1320 io_req
->offsets
[0] = 0;
1321 io_req
->offsets
[1] = dev
->cow
.data_offset
;
1322 io_req
->buffer
= page_address(bvec
->bv_page
) + bvec
->bv_offset
;
1323 io_req
->sectorsize
= 1 << 9;
1325 if (dev
->cow
.file
) {
1326 cowify_req(io_req
, dev
->cow
.bitmap
,
1327 dev
->cow
.bitmap_offset
, dev
->cow
.bitmap_len
);
1331 ret
= os_write_file(thread_fd
, &io_req
, sizeof(io_req
));
1332 if (ret
!= sizeof(io_req
)) {
1334 pr_err("write to io thread failed: %d\n", -ret
);
1341 static blk_status_t
ubd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1342 const struct blk_mq_queue_data
*bd
)
1344 struct request
*req
= bd
->rq
;
1347 blk_mq_start_request(req
);
1349 if (req_op(req
) == REQ_OP_FLUSH
) {
1350 ret
= ubd_queue_one_vec(hctx
, req
, 0, NULL
);
1352 struct req_iterator iter
;
1353 struct bio_vec bvec
;
1354 u64 off
= (u64
)blk_rq_pos(req
) << 9;
1356 rq_for_each_segment(bvec
, req
, iter
) {
1357 ret
= ubd_queue_one_vec(hctx
, req
, off
, &bvec
);
1365 blk_mq_requeue_request(req
, true);
1370 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1372 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1376 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1380 static int ubd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1381 unsigned int cmd
, unsigned long arg
)
1383 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1384 u16 ubd_id
[ATA_ID_WORDS
];
1387 struct cdrom_volctrl volume
;
1388 case HDIO_GET_IDENTITY
:
1389 memset(&ubd_id
, 0, ATA_ID_WORDS
* 2);
1390 ubd_id
[ATA_ID_CYLS
] = ubd_dev
->size
/ (128 * 32 * 512);
1391 ubd_id
[ATA_ID_HEADS
] = 128;
1392 ubd_id
[ATA_ID_SECTORS
] = 32;
1393 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1399 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1401 volume
.channel0
= 255;
1402 volume
.channel1
= 255;
1403 volume
.channel2
= 255;
1404 volume
.channel3
= 255;
1405 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1412 static int update_bitmap(struct io_thread_req
*req
)
1416 if(req
->cow_offset
== -1)
1419 n
= os_pwrite_file(req
->fds
[1], &req
->bitmap_words
,
1420 sizeof(req
->bitmap_words
), req
->cow_offset
);
1421 if(n
!= sizeof(req
->bitmap_words
)){
1422 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n
,
1430 static void do_io(struct io_thread_req
*req
)
1434 int n
, nsectors
, start
, end
, bit
;
1437 if (req
->op
== UBD_FLUSH
) {
1438 /* fds[0] is always either the rw image or our cow file */
1439 n
= os_sync_file(req
->fds
[0]);
1441 printk("do_io - sync failed err = %d "
1442 "fd = %d\n", -n
, req
->fds
[0]);
1448 nsectors
= req
->length
/ req
->sectorsize
;
1451 bit
= ubd_test_bit(start
, (unsigned char *) &req
->sector_mask
);
1453 while((end
< nsectors
) &&
1454 (ubd_test_bit(end
, (unsigned char *)
1455 &req
->sector_mask
) == bit
))
1458 off
= req
->offset
+ req
->offsets
[bit
] +
1459 start
* req
->sectorsize
;
1460 len
= (end
- start
) * req
->sectorsize
;
1461 buf
= &req
->buffer
[start
* req
->sectorsize
];
1463 if(req
->op
== UBD_READ
){
1468 n
= os_pread_file(req
->fds
[bit
], buf
, len
, off
);
1470 printk("do_io - read failed, err = %d "
1471 "fd = %d\n", -n
, req
->fds
[bit
]);
1475 } while((n
< len
) && (n
!= 0));
1476 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1478 n
= os_pwrite_file(req
->fds
[bit
], buf
, len
, off
);
1480 printk("do_io - write failed err = %d "
1481 "fd = %d\n", -n
, req
->fds
[bit
]);
1488 } while(start
< nsectors
);
1490 req
->error
= update_bitmap(req
);
1493 /* Changed in start_io_thread, which is serialized by being called only
1494 * from ubd_init, which is an initcall.
1498 /* Only changed by the io thread. XXX: currently unused. */
1499 static int io_count
= 0;
1501 int io_thread(void *arg
)
1503 int n
, count
, written
, res
;
1505 os_fix_helper_signals();
1508 n
= bulk_req_safe_read(
1520 printk("io_thread - read failed, fd = %d, "
1523 kernel_fd
, -n
, io_remainder_size
);
1527 for (count
= 0; count
< n
/sizeof(struct io_thread_req
*); count
++) {
1529 do_io((*io_req_buffer
)[count
]);
1535 res
= os_write_file(kernel_fd
, ((char *) io_req_buffer
) + written
, n
);
1539 if (res
!= -EAGAIN
) {
1540 printk("io_thread - write failed, fd = %d, "
1541 "err = %d\n", kernel_fd
, -n
);
1547 } while (written
< n
);