1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Cambridge Greys Ltd
4 * Copyright (C) 2015-2016 Anton Ivanov (aivanov@brocade.com)
5 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
8 /* 2001-09-28...2002-04-17
9 * Partition stuff by James_McMechan@hotmail.com
10 * old style ubd by setting UBD_SHIFT to 0
11 * 2002-09-27...2002-10-18 massive tinkering for 2.5
12 * partitions have changed in 2.5
13 * 2003-01-29 more tinkering for 2.5.59-1
14 * This should now address the sysfs problems and has
15 * the symlink for devfs to allow for booting with
16 * the common /dev/ubd/discX/... names rather than
17 * only /dev/ubdN/discN this version also has lots of
18 * clean ups preparing for ubd-many.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/blkdev.h>
27 #include <linux/blk-mq.h>
28 #include <linux/ata.h>
29 #include <linux/hdreg.h>
30 #include <linux/cdrom.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/ctype.h>
34 #include <linux/slab.h>
35 #include <linux/vmalloc.h>
36 #include <linux/platform_device.h>
37 #include <linux/scatterlist.h>
38 #include <asm/tlbflush.h>
39 #include <kern_util.h>
40 #include "mconsole_kern.h"
47 /* Max request size is determined by sector mask - 32K */
48 #define UBD_MAX_REQUEST (8 * sizeof(long))
53 unsigned long sector_mask;
54 unsigned long long cow_offset;
55 unsigned long bitmap_words[2];
58 struct io_thread_req {
61 unsigned long offsets[2];
62 unsigned long long offset;
67 /* io_desc has to be the last element of the struct */
68 struct io_desc io_desc[];
72 static struct io_thread_req * (*irq_req_buffer)[];
73 static struct io_thread_req *irq_remainder;
74 static int irq_remainder_size;
76 static struct io_thread_req * (*io_req_buffer)[];
77 static struct io_thread_req *io_remainder;
78 static int io_remainder_size;
82 static inline int ubd_test_bit(__u64 bit, unsigned char *data)
87 bits = sizeof(data[0]) * 8;
90 return (data[n] & (1 << off)) != 0;
93 static inline void ubd_set_bit(__u64 bit, unsigned char *data)
98 bits = sizeof(data[0]) * 8;
101 data[n] |= (1 << off);
103 /*End stuff from ubd_user.h*/
105 #define DRIVER_NAME "uml-blkdev"
107 static DEFINE_MUTEX(ubd_lock);
108 static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */
110 static int ubd_open(struct block_device *bdev, fmode_t mode);
111 static void ubd_release(struct gendisk *disk, fmode_t mode);
112 static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
113 unsigned int cmd, unsigned long arg);
114 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
118 static const struct block_device_operations ubd_blops = {
119 .owner = THIS_MODULE,
121 .release = ubd_release,
123 .compat_ioctl = blkdev_compat_ptr_ioctl,
124 .getgeo = ubd_getgeo,
127 /* Protected by ubd_lock */
128 static int fake_major = UBD_MAJOR;
129 static struct gendisk *ubd_gendisk[MAX_DEV];
130 static struct gendisk *fake_gendisk[MAX_DEV];
132 #ifdef CONFIG_BLK_DEV_UBD_SYNC
133 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
136 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
139 static struct openflags global_openflags = OPEN_FLAGS;
142 /* backing file name */
144 /* backing file fd */
146 unsigned long *bitmap;
147 unsigned long bitmap_len;
155 /* name (and fd, below) of the file opened for writing, either the
156 * backing or the cow file. */
161 struct openflags boot_openflags;
162 struct openflags openflags;
167 struct platform_device pdev;
168 struct request_queue *queue;
169 struct blk_mq_tag_set tag_set;
173 #define DEFAULT_COW { \
177 .bitmap_offset = 0, \
181 #define DEFAULT_UBD { \
186 .boot_openflags = OPEN_FLAGS, \
187 .openflags = OPEN_FLAGS, \
191 .cow = DEFAULT_COW, \
192 .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 blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
204 const struct blk_mq_queue_data *bd);
206 static void make_proc_ide(void)
208 proc_ide_root = proc_mkdir("ide", NULL);
209 proc_ide = proc_mkdir("ide0", proc_ide_root);
212 static int fake_ide_media_proc_show(struct seq_file *m, void *v)
214 seq_puts(m, "disk\n");
218 static void make_ide_entries(const char *dev_name)
220 struct proc_dir_entry *dir, *ent;
223 if(proc_ide_root == NULL) make_proc_ide();
225 dir = proc_mkdir(dev_name, proc_ide);
228 ent = proc_create_single("media", S_IRUGO, dir,
229 fake_ide_media_proc_show);
231 snprintf(name, sizeof(name), "ide0/%s", dev_name);
232 proc_symlink(dev_name, proc_ide_root, name);
235 static int fake_ide_setup(char *str)
241 __setup("fake_ide", fake_ide_setup);
243 __uml_help(fake_ide_setup,
245 " Create ide0 entries that map onto ubd devices.\n\n"
248 static int parse_unit(char **ptr)
250 char *str = *ptr, *end;
254 n = simple_strtoul(str, &end, 0);
259 else if (('a' <= *str) && (*str <= 'z')) {
267 /* If *index_out == -1 at exit, the passed option was a general one;
268 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
269 * should not be freed on exit.
271 static int ubd_setup_common(char *str, int *index_out, char **error_out)
274 struct openflags flags = global_openflags;
278 if(index_out) *index_out = -1;
285 if(!strcmp(str, "sync")){
286 global_openflags = of_sync(global_openflags);
291 major = simple_strtoul(str, &end, 0);
292 if((*end != '\0') || (end == str)){
293 *error_out = "Didn't parse major number";
297 mutex_lock(&ubd_lock);
298 if (fake_major != UBD_MAJOR) {
299 *error_out = "Can't assign a fake major twice";
305 printk(KERN_INFO "Setting extra ubd major number to %d\n",
309 mutex_unlock(&ubd_lock);
313 n = parse_unit(&str);
315 *error_out = "Couldn't parse device number";
319 *error_out = "Device number out of range";
324 mutex_lock(&ubd_lock);
326 ubd_dev = &ubd_devs[n];
327 if(ubd_dev->file != NULL){
328 *error_out = "Device is already configured";
336 for (i = 0; i < sizeof("rscdt="); i++) {
351 ubd_dev->no_trim = 1;
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"
431 " 't' will disable trim/discard support on the device (enabled by default).\n\n"
434 static int udb_setup(char *str)
436 printk("udb%s specified on command line is almost certainly a ubd -> "
441 __setup("udb", udb_setup);
442 __uml_help(udb_setup,
444 " This option is here solely to catch ubd -> udb typos, which can be\n"
445 " to impossible to catch visually unless you specifically look for\n"
446 " them. The only result of any option starting with 'udb' is an error\n"
447 " in the boot output.\n\n"
450 /* Only changed by ubd_init, which is an initcall. */
451 static int thread_fd = -1;
453 /* Function to read several request pointers at a time
454 * handling fractional reads if (and as) needed
457 static int bulk_req_safe_read(
459 struct io_thread_req * (*request_buffer)[],
460 struct io_thread_req **remainder,
468 if (*remainder_size > 0) {
470 (char *) request_buffer,
471 (char *) remainder, *remainder_size
478 ((char *) request_buffer) + *remainder_size,
479 sizeof(struct io_thread_req *)*max_recs
484 if ((n % sizeof(struct io_thread_req *)) > 0) {
486 * Read somehow returned not a multiple of dword
487 * theoretically possible, but never observed in the
488 * wild, so read routine must be able to handle it
490 *remainder_size = n % sizeof(struct io_thread_req *);
491 WARN(*remainder_size > 0, "UBD IPC read returned a partial result");
494 ((char *) request_buffer) +
495 (n/sizeof(struct io_thread_req *))*sizeof(struct io_thread_req *),
498 n = n - *remainder_size;
506 /* Called without dev->lock held, and only in interrupt context. */
507 static void ubd_handler(void)
513 n = bulk_req_safe_read(
523 printk(KERN_ERR "spurious interrupt in ubd_handler, "
527 for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
528 struct io_thread_req *io_req = (*irq_req_buffer)[count];
530 if ((io_req->error == BLK_STS_NOTSUPP) && (req_op(io_req->req) == REQ_OP_DISCARD)) {
531 blk_queue_max_discard_sectors(io_req->req->q, 0);
532 blk_queue_max_write_zeroes_sectors(io_req->req->q, 0);
533 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, io_req->req->q);
535 blk_mq_end_request(io_req->req, io_req->error);
541 static irqreturn_t ubd_intr(int irq, void *dev)
547 /* Only changed by ubd_init, which is an initcall. */
548 static int io_pid = -1;
550 static void kill_io_thread(void)
553 os_kill_process(io_pid, 1);
556 __uml_exitcall(kill_io_thread);
558 static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
568 unsigned long long size;
572 if (ubd_dev->file && ubd_dev->cow.file) {
573 file = ubd_dev->cow.file;
578 fd = os_open_file(ubd_dev->file, of_read(OPENFLAGS()), 0);
582 err = read_cow_header(file_reader, &fd, &version, &backing_file, \
583 &mtime, &size, §or_size, &align, &bitmap_offset);
587 file = ubd_dev->file;
592 return os_file_size(file, size_out);
595 static int read_cow_bitmap(int fd, void *buf, int offset, int len)
599 err = os_pread_file(fd, buf, len, offset);
606 static int backing_file_mismatch(char *file, __u64 size, time64_t mtime)
609 unsigned long long actual;
612 err = os_file_modtime(file, &modtime);
614 printk(KERN_ERR "Failed to get modification time of backing "
615 "file \"%s\", err = %d\n", file, -err);
619 err = os_file_size(file, &actual);
621 printk(KERN_ERR "Failed to get size of backing file \"%s\", "
622 "err = %d\n", file, -err);
626 if (actual != size) {
627 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
629 printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
630 "vs backing file\n", (unsigned long long) size, actual);
633 if (modtime != mtime) {
634 printk(KERN_ERR "mtime mismatch (%lld vs %lld) of COW header vs "
635 "backing file\n", mtime, modtime);
641 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
643 struct uml_stat buf1, buf2;
646 if (from_cmdline == NULL)
648 if (!strcmp(from_cmdline, from_cow))
651 err = os_stat_file(from_cmdline, &buf1);
653 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
657 err = os_stat_file(from_cow, &buf2);
659 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
663 if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
666 printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
667 "\"%s\" specified in COW header of \"%s\"\n",
668 from_cmdline, from_cow, cow);
672 static int open_ubd_file(char *file, struct openflags *openflags, int shared,
673 char **backing_file_out, int *bitmap_offset_out,
674 unsigned long *bitmap_len_out, int *data_offset_out,
678 unsigned long long size;
679 __u32 version, align;
681 int fd, err, sectorsize, asked_switch, mode = 0644;
683 fd = os_open_file(file, *openflags, mode);
685 if ((fd == -ENOENT) && (create_cow_out != NULL))
688 ((fd != -EROFS) && (fd != -EACCES)))
691 fd = os_open_file(file, *openflags, mode);
697 printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
699 err = os_lock_file(fd, openflags->w);
701 printk(KERN_ERR "Failed to lock '%s', err = %d\n",
707 /* Successful return case! */
708 if (backing_file_out == NULL)
711 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
712 &size, §orsize, &align, bitmap_offset_out);
713 if (err && (*backing_file_out != NULL)) {
714 printk(KERN_ERR "Failed to read COW header from COW file "
715 "\"%s\", errno = %d\n", file, -err);
721 asked_switch = path_requires_switch(*backing_file_out, backing_file,
724 /* Allow switching only if no mismatch. */
725 if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
727 printk(KERN_ERR "Switching backing file to '%s'\n",
729 err = write_cow_header(file, fd, *backing_file_out,
730 sectorsize, align, &size);
732 printk(KERN_ERR "Switch failed, errno = %d\n", -err);
736 *backing_file_out = backing_file;
737 err = backing_file_mismatch(*backing_file_out, size, mtime);
742 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
743 bitmap_len_out, data_offset_out);
751 static int create_cow_file(char *cow_file, char *backing_file,
752 struct openflags flags,
753 int sectorsize, int alignment, int *bitmap_offset_out,
754 unsigned long *bitmap_len_out, int *data_offset_out)
759 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
762 printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
767 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
768 bitmap_offset_out, bitmap_len_out,
777 static void ubd_close_dev(struct ubd *ubd_dev)
779 os_close_file(ubd_dev->fd);
780 if(ubd_dev->cow.file == NULL)
783 os_close_file(ubd_dev->cow.fd);
784 vfree(ubd_dev->cow.bitmap);
785 ubd_dev->cow.bitmap = NULL;
788 static int ubd_open_dev(struct ubd *ubd_dev)
790 struct openflags flags;
792 int err, create_cow, *create_ptr;
795 ubd_dev->openflags = ubd_dev->boot_openflags;
797 create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
798 back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
800 fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
801 back_ptr, &ubd_dev->cow.bitmap_offset,
802 &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
805 if((fd == -ENOENT) && create_cow){
806 fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
807 ubd_dev->openflags, SECTOR_SIZE, PAGE_SIZE,
808 &ubd_dev->cow.bitmap_offset,
809 &ubd_dev->cow.bitmap_len,
810 &ubd_dev->cow.data_offset);
812 printk(KERN_INFO "Creating \"%s\" as COW file for "
813 "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
818 printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
824 if(ubd_dev->cow.file != NULL){
825 blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
828 ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
829 if(ubd_dev->cow.bitmap == NULL){
830 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
833 flush_tlb_kernel_vm();
835 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
836 ubd_dev->cow.bitmap_offset,
837 ubd_dev->cow.bitmap_len);
841 flags = ubd_dev->openflags;
843 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
844 NULL, NULL, NULL, NULL);
845 if(err < 0) goto error;
846 ubd_dev->cow.fd = err;
848 if (ubd_dev->no_trim == 0) {
849 ubd_dev->queue->limits.discard_granularity = SECTOR_SIZE;
850 ubd_dev->queue->limits.discard_alignment = SECTOR_SIZE;
851 blk_queue_max_discard_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
852 blk_queue_max_write_zeroes_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
853 blk_queue_flag_set(QUEUE_FLAG_DISCARD, ubd_dev->queue);
855 blk_queue_flag_set(QUEUE_FLAG_NONROT, ubd_dev->queue);
858 os_close_file(ubd_dev->fd);
862 static void ubd_device_release(struct device *dev)
864 struct ubd *ubd_dev = dev_get_drvdata(dev);
866 blk_cleanup_queue(ubd_dev->queue);
867 blk_mq_free_tag_set(&ubd_dev->tag_set);
868 *ubd_dev = ((struct ubd) DEFAULT_UBD);
871 static int ubd_disk_register(int major, u64 size, int unit,
872 struct gendisk **disk_out)
874 struct device *parent = NULL;
875 struct gendisk *disk;
877 disk = alloc_disk(1 << UBD_SHIFT);
882 disk->first_minor = unit << UBD_SHIFT;
883 disk->fops = &ubd_blops;
884 set_capacity(disk, size / 512);
885 if (major == UBD_MAJOR)
886 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
888 sprintf(disk->disk_name, "ubd_fake%d", unit);
890 /* sysfs register (not for ide fake devices) */
891 if (major == UBD_MAJOR) {
892 ubd_devs[unit].pdev.id = unit;
893 ubd_devs[unit].pdev.name = DRIVER_NAME;
894 ubd_devs[unit].pdev.dev.release = ubd_device_release;
895 dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
896 platform_device_register(&ubd_devs[unit].pdev);
897 parent = &ubd_devs[unit].pdev.dev;
900 disk->private_data = &ubd_devs[unit];
901 disk->queue = ubd_devs[unit].queue;
902 device_add_disk(parent, disk, NULL);
908 #define ROUND_BLOCK(n) ((n + (SECTOR_SIZE - 1)) & (-SECTOR_SIZE))
910 static const struct blk_mq_ops ubd_mq_ops = {
911 .queue_rq = ubd_queue_rq,
914 static int ubd_add(int n, char **error_out)
916 struct ubd *ubd_dev = &ubd_devs[n];
919 if(ubd_dev->file == NULL)
922 err = ubd_file_size(ubd_dev, &ubd_dev->size);
924 *error_out = "Couldn't determine size of device's file";
928 ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
930 ubd_dev->tag_set.ops = &ubd_mq_ops;
931 ubd_dev->tag_set.queue_depth = 64;
932 ubd_dev->tag_set.numa_node = NUMA_NO_NODE;
933 ubd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
934 ubd_dev->tag_set.driver_data = ubd_dev;
935 ubd_dev->tag_set.nr_hw_queues = 1;
937 err = blk_mq_alloc_tag_set(&ubd_dev->tag_set);
941 ubd_dev->queue = blk_mq_init_queue(&ubd_dev->tag_set);
942 if (IS_ERR(ubd_dev->queue)) {
943 err = PTR_ERR(ubd_dev->queue);
944 goto out_cleanup_tags;
947 ubd_dev->queue->queuedata = ubd_dev;
948 blk_queue_write_cache(ubd_dev->queue, true, false);
950 blk_queue_max_segments(ubd_dev->queue, MAX_SG);
951 blk_queue_segment_boundary(ubd_dev->queue, PAGE_SIZE - 1);
952 err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
954 *error_out = "Failed to register device";
955 goto out_cleanup_tags;
958 if (fake_major != UBD_MAJOR)
959 ubd_disk_register(fake_major, ubd_dev->size, n,
963 * Perhaps this should also be under the "if (fake_major)" above
964 * using the fake_disk->disk_name
967 make_ide_entries(ubd_gendisk[n]->disk_name);
974 blk_mq_free_tag_set(&ubd_dev->tag_set);
975 if (!(IS_ERR(ubd_dev->queue)))
976 blk_cleanup_queue(ubd_dev->queue);
980 static int ubd_config(char *str, char **error_out)
984 /* This string is possibly broken up and stored, so it's only
985 * freed if ubd_setup_common fails, or if only general options
988 str = kstrdup(str, GFP_KERNEL);
990 *error_out = "Failed to allocate memory";
994 ret = ubd_setup_common(str, &n, error_out);
1003 mutex_lock(&ubd_lock);
1004 ret = ubd_add(n, error_out);
1006 ubd_devs[n].file = NULL;
1007 mutex_unlock(&ubd_lock);
1017 static int ubd_get_config(char *name, char *str, int size, char **error_out)
1019 struct ubd *ubd_dev;
1022 n = parse_unit(&name);
1023 if((n >= MAX_DEV) || (n < 0)){
1024 *error_out = "ubd_get_config : device number out of range";
1028 ubd_dev = &ubd_devs[n];
1029 mutex_lock(&ubd_lock);
1031 if(ubd_dev->file == NULL){
1032 CONFIG_CHUNK(str, size, len, "", 1);
1036 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
1038 if(ubd_dev->cow.file != NULL){
1039 CONFIG_CHUNK(str, size, len, ",", 0);
1040 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
1042 else CONFIG_CHUNK(str, size, len, "", 1);
1045 mutex_unlock(&ubd_lock);
1049 static int ubd_id(char **str, int *start_out, int *end_out)
1053 n = parse_unit(str);
1055 *end_out = MAX_DEV - 1;
1059 static int ubd_remove(int n, char **error_out)
1061 struct gendisk *disk = ubd_gendisk[n];
1062 struct ubd *ubd_dev;
1065 mutex_lock(&ubd_lock);
1067 ubd_dev = &ubd_devs[n];
1069 if(ubd_dev->file == NULL)
1072 /* you cannot remove a open disk */
1074 if(ubd_dev->count > 0)
1077 ubd_gendisk[n] = NULL;
1083 if(fake_gendisk[n] != NULL){
1084 del_gendisk(fake_gendisk[n]);
1085 put_disk(fake_gendisk[n]);
1086 fake_gendisk[n] = NULL;
1090 platform_device_unregister(&ubd_dev->pdev);
1092 mutex_unlock(&ubd_lock);
1096 /* All these are called by mconsole in process context and without
1097 * ubd-specific locks. The structure itself is const except for .list.
1099 static struct mc_device ubd_mc = {
1100 .list = LIST_HEAD_INIT(ubd_mc.list),
1102 .config = ubd_config,
1103 .get_config = ubd_get_config,
1105 .remove = ubd_remove,
1108 static int __init ubd_mc_init(void)
1110 mconsole_register_dev(&ubd_mc);
1114 __initcall(ubd_mc_init);
1116 static int __init ubd0_init(void)
1118 struct ubd *ubd_dev = &ubd_devs[0];
1120 mutex_lock(&ubd_lock);
1121 if(ubd_dev->file == NULL)
1122 ubd_dev->file = "root_fs";
1123 mutex_unlock(&ubd_lock);
1128 __initcall(ubd0_init);
1130 /* Used in ubd_init, which is an initcall */
1131 static struct platform_driver ubd_driver = {
1133 .name = DRIVER_NAME,
1137 static int __init ubd_init(void)
1142 if (register_blkdev(UBD_MAJOR, "ubd"))
1145 if (fake_major != UBD_MAJOR) {
1146 char name[sizeof("ubd_nnn\0")];
1148 snprintf(name, sizeof(name), "ubd_%d", fake_major);
1149 if (register_blkdev(fake_major, "ubd"))
1153 irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
1154 sizeof(struct io_thread_req *),
1159 if (irq_req_buffer == NULL) {
1160 printk(KERN_ERR "Failed to initialize ubd buffering\n");
1163 io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
1164 sizeof(struct io_thread_req *),
1170 if (io_req_buffer == NULL) {
1171 printk(KERN_ERR "Failed to initialize ubd buffering\n");
1174 platform_driver_register(&ubd_driver);
1175 mutex_lock(&ubd_lock);
1176 for (i = 0; i < MAX_DEV; i++){
1177 err = ubd_add(i, &error);
1179 printk(KERN_ERR "Failed to initialize ubd device %d :"
1182 mutex_unlock(&ubd_lock);
1186 late_initcall(ubd_init);
1188 static int __init ubd_driver_init(void){
1189 unsigned long stack;
1192 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1193 if(global_openflags.s){
1194 printk(KERN_INFO "ubd: Synchronous mode\n");
1195 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1196 * enough. So use anyway the io thread. */
1198 stack = alloc_stack(0, 0);
1199 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
1203 "ubd : Failed to start I/O thread (errno = %d) - "
1204 "falling back to synchronous I/O\n", -io_pid);
1208 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
1209 0, "ubd", ubd_devs);
1211 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
1215 device_initcall(ubd_driver_init);
1217 static int ubd_open(struct block_device *bdev, fmode_t mode)
1219 struct gendisk *disk = bdev->bd_disk;
1220 struct ubd *ubd_dev = disk->private_data;
1223 mutex_lock(&ubd_mutex);
1224 if(ubd_dev->count == 0){
1225 err = ubd_open_dev(ubd_dev);
1227 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
1228 disk->disk_name, ubd_dev->file, -err);
1233 set_disk_ro(disk, !ubd_dev->openflags.w);
1235 /* This should no more be needed. And it didn't work anyway to exclude
1236 * read-write remounting of filesystems.*/
1237 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1238 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1242 mutex_unlock(&ubd_mutex);
1246 static void ubd_release(struct gendisk *disk, fmode_t mode)
1248 struct ubd *ubd_dev = disk->private_data;
1250 mutex_lock(&ubd_mutex);
1251 if(--ubd_dev->count == 0)
1252 ubd_close_dev(ubd_dev);
1253 mutex_unlock(&ubd_mutex);
1256 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
1257 __u64 *cow_offset, unsigned long *bitmap,
1258 __u64 bitmap_offset, unsigned long *bitmap_words,
1261 __u64 sector = io_offset >> SECTOR_SHIFT;
1262 int i, update_bitmap = 0;
1264 for (i = 0; i < length >> SECTOR_SHIFT; i++) {
1265 if(cow_mask != NULL)
1266 ubd_set_bit(i, (unsigned char *) cow_mask);
1267 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1271 ubd_set_bit(sector + i, (unsigned char *) bitmap);
1277 *cow_offset = sector / (sizeof(unsigned long) * 8);
1279 /* This takes care of the case where we're exactly at the end of the
1280 * device, and *cow_offset + 1 is off the end. So, just back it up
1281 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1282 * for the original diagnosis.
1284 if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
1285 sizeof(unsigned long)) - 1))
1288 bitmap_words[0] = bitmap[*cow_offset];
1289 bitmap_words[1] = bitmap[*cow_offset + 1];
1291 *cow_offset *= sizeof(unsigned long);
1292 *cow_offset += bitmap_offset;
1295 static void cowify_req(struct io_thread_req *req, struct io_desc *segment,
1296 unsigned long offset, unsigned long *bitmap,
1297 __u64 bitmap_offset, __u64 bitmap_len)
1299 __u64 sector = offset >> SECTOR_SHIFT;
1302 if (segment->length > (sizeof(segment->sector_mask) * 8) << SECTOR_SHIFT)
1303 panic("Operation too long");
1305 if (req_op(req->req) == REQ_OP_READ) {
1306 for (i = 0; i < segment->length >> SECTOR_SHIFT; i++) {
1307 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1308 ubd_set_bit(i, (unsigned char *)
1309 &segment->sector_mask);
1312 cowify_bitmap(offset, segment->length, &segment->sector_mask,
1313 &segment->cow_offset, bitmap, bitmap_offset,
1314 segment->bitmap_words, bitmap_len);
1318 static void ubd_map_req(struct ubd *dev, struct io_thread_req *io_req,
1319 struct request *req)
1321 struct bio_vec bvec;
1322 struct req_iterator iter;
1324 unsigned long byte_offset = io_req->offset;
1325 int op = req_op(req);
1327 if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD) {
1328 io_req->io_desc[0].buffer = NULL;
1329 io_req->io_desc[0].length = blk_rq_bytes(req);
1331 rq_for_each_segment(bvec, req, iter) {
1332 BUG_ON(i >= io_req->desc_cnt);
1334 io_req->io_desc[i].buffer =
1335 page_address(bvec.bv_page) + bvec.bv_offset;
1336 io_req->io_desc[i].length = bvec.bv_len;
1341 if (dev->cow.file) {
1342 for (i = 0; i < io_req->desc_cnt; i++) {
1343 cowify_req(io_req, &io_req->io_desc[i], byte_offset,
1344 dev->cow.bitmap, dev->cow.bitmap_offset,
1345 dev->cow.bitmap_len);
1346 byte_offset += io_req->io_desc[i].length;
1352 static struct io_thread_req *ubd_alloc_req(struct ubd *dev, struct request *req,
1355 struct io_thread_req *io_req;
1358 io_req = kmalloc(sizeof(*io_req) +
1359 (desc_cnt * sizeof(struct io_desc)),
1366 io_req->fds[0] = dev->cow.fd;
1368 io_req->fds[0] = dev->fd;
1370 io_req->sectorsize = SECTOR_SIZE;
1371 io_req->fds[1] = dev->fd;
1372 io_req->offset = (u64) blk_rq_pos(req) << SECTOR_SHIFT;
1373 io_req->offsets[0] = 0;
1374 io_req->offsets[1] = dev->cow.data_offset;
1376 for (i = 0 ; i < desc_cnt; i++) {
1377 io_req->io_desc[i].sector_mask = 0;
1378 io_req->io_desc[i].cow_offset = -1;
1384 static int ubd_submit_request(struct ubd *dev, struct request *req)
1387 struct io_thread_req *io_req;
1389 int op = req_op(req);
1391 if (op == REQ_OP_FLUSH)
1393 else if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD)
1396 segs = blk_rq_nr_phys_segments(req);
1398 io_req = ubd_alloc_req(dev, req, segs);
1402 io_req->desc_cnt = segs;
1404 ubd_map_req(dev, io_req, req);
1406 ret = os_write_file(thread_fd, &io_req, sizeof(io_req));
1407 if (ret != sizeof(io_req)) {
1409 pr_err("write to io thread failed: %d\n", -ret);
1415 static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
1416 const struct blk_mq_queue_data *bd)
1418 struct ubd *ubd_dev = hctx->queue->queuedata;
1419 struct request *req = bd->rq;
1420 int ret = 0, res = BLK_STS_OK;
1422 blk_mq_start_request(req);
1424 spin_lock_irq(&ubd_dev->lock);
1426 switch (req_op(req)) {
1430 case REQ_OP_DISCARD:
1431 case REQ_OP_WRITE_ZEROES:
1432 ret = ubd_submit_request(ubd_dev, req);
1436 res = BLK_STS_NOTSUPP;
1439 spin_unlock_irq(&ubd_dev->lock);
1443 res = BLK_STS_RESOURCE;
1445 res = BLK_STS_DEV_RESOURCE;
1451 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1453 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1457 geo->cylinders = ubd_dev->size / (128 * 32 * 512);
1461 static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
1462 unsigned int cmd, unsigned long arg)
1464 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1465 u16 ubd_id[ATA_ID_WORDS];
1468 struct cdrom_volctrl volume;
1469 case HDIO_GET_IDENTITY:
1470 memset(&ubd_id, 0, ATA_ID_WORDS * 2);
1471 ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512);
1472 ubd_id[ATA_ID_HEADS] = 128;
1473 ubd_id[ATA_ID_SECTORS] = 32;
1474 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1480 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1482 volume.channel0 = 255;
1483 volume.channel1 = 255;
1484 volume.channel2 = 255;
1485 volume.channel3 = 255;
1486 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1493 static int map_error(int error_code)
1495 switch (error_code) {
1500 return BLK_STS_NOTSUPP;
1502 return BLK_STS_NOSPC;
1504 return BLK_STS_IOERR;
1508 * Everything from here onwards *IS NOT PART OF THE KERNEL*
1510 * The following functions are part of UML hypervisor code.
1511 * All functions from here onwards are executed as a helper
1512 * thread and are not allowed to execute any kernel functions.
1514 * Any communication must occur strictly via shared memory and IPC.
1516 * Do not add printks, locks, kernel memory operations, etc - it
1517 * will result in unpredictable behaviour and/or crashes.
1520 static int update_bitmap(struct io_thread_req *req, struct io_desc *segment)
1524 if (segment->cow_offset == -1)
1525 return map_error(0);
1527 n = os_pwrite_file(req->fds[1], &segment->bitmap_words,
1528 sizeof(segment->bitmap_words), segment->cow_offset);
1529 if (n != sizeof(segment->bitmap_words))
1530 return map_error(-n);
1532 return map_error(0);
1535 static void do_io(struct io_thread_req *req, struct io_desc *desc)
1539 int n, nsectors, start, end, bit;
1542 /* FLUSH is really a special case, we cannot "case" it with others */
1544 if (req_op(req->req) == REQ_OP_FLUSH) {
1545 /* fds[0] is always either the rw image or our cow file */
1546 req->error = map_error(-os_sync_file(req->fds[0]));
1550 nsectors = desc->length / req->sectorsize;
1553 bit = ubd_test_bit(start, (unsigned char *) &desc->sector_mask);
1555 while((end < nsectors) &&
1556 (ubd_test_bit(end, (unsigned char *) &desc->sector_mask) == bit))
1559 off = req->offset + req->offsets[bit] +
1560 start * req->sectorsize;
1561 len = (end - start) * req->sectorsize;
1562 if (desc->buffer != NULL)
1563 buf = &desc->buffer[start * req->sectorsize];
1565 switch (req_op(req->req)) {
1571 n = os_pread_file(req->fds[bit], buf, len, off);
1573 req->error = map_error(-n);
1576 } while((n < len) && (n != 0));
1577 if (n < len) memset(&buf[n], 0, len - n);
1580 n = os_pwrite_file(req->fds[bit], buf, len, off);
1582 req->error = map_error(-n);
1586 case REQ_OP_DISCARD:
1587 case REQ_OP_WRITE_ZEROES:
1588 n = os_falloc_punch(req->fds[bit], off, len);
1590 req->error = map_error(-n);
1596 req->error = BLK_STS_NOTSUPP;
1601 } while(start < nsectors);
1604 req->error = update_bitmap(req, desc);
1607 /* Changed in start_io_thread, which is serialized by being called only
1608 * from ubd_init, which is an initcall.
1612 /* Only changed by the io thread. XXX: currently unused. */
1613 static int io_count = 0;
1615 int io_thread(void *arg)
1617 int n, count, written, res;
1619 os_fix_helper_signals();
1622 n = bulk_req_safe_read(
1636 for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
1637 struct io_thread_req *req = (*io_req_buffer)[count];
1641 for (i = 0; !req->error && i < req->desc_cnt; i++)
1642 do_io(req, &(req->io_desc[i]));
1649 res = os_write_file(kernel_fd,
1650 ((char *) io_req_buffer) + written,
1658 } while (written < n);