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 .getgeo = ubd_getgeo,
126 /* Protected by ubd_lock */
127 static int fake_major = UBD_MAJOR;
128 static struct gendisk *ubd_gendisk[MAX_DEV];
129 static struct gendisk *fake_gendisk[MAX_DEV];
131 #ifdef CONFIG_BLK_DEV_UBD_SYNC
132 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
135 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
138 static struct openflags global_openflags = OPEN_FLAGS;
141 /* backing file name */
143 /* backing file fd */
145 unsigned long *bitmap;
146 unsigned long bitmap_len;
154 /* name (and fd, below) of the file opened for writing, either the
155 * backing or the cow file. */
160 struct openflags boot_openflags;
161 struct openflags openflags;
166 struct platform_device pdev;
167 struct request_queue *queue;
168 struct blk_mq_tag_set tag_set;
172 #define DEFAULT_COW { \
176 .bitmap_offset = 0, \
180 #define DEFAULT_UBD { \
185 .boot_openflags = OPEN_FLAGS, \
186 .openflags = OPEN_FLAGS, \
190 .cow = DEFAULT_COW, \
191 .lock = __SPIN_LOCK_UNLOCKED(ubd_devs.lock), \
194 /* Protected by ubd_lock */
195 static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD };
197 /* Only changed by fake_ide_setup which is a setup */
198 static int fake_ide = 0;
199 static struct proc_dir_entry *proc_ide_root = NULL;
200 static struct proc_dir_entry *proc_ide = NULL;
202 static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
203 const struct blk_mq_queue_data *bd);
205 static void make_proc_ide(void)
207 proc_ide_root = proc_mkdir("ide", NULL);
208 proc_ide = proc_mkdir("ide0", proc_ide_root);
211 static int fake_ide_media_proc_show(struct seq_file *m, void *v)
213 seq_puts(m, "disk\n");
217 static void make_ide_entries(const char *dev_name)
219 struct proc_dir_entry *dir, *ent;
222 if(proc_ide_root == NULL) make_proc_ide();
224 dir = proc_mkdir(dev_name, proc_ide);
227 ent = proc_create_single("media", S_IRUGO, dir,
228 fake_ide_media_proc_show);
230 snprintf(name, sizeof(name), "ide0/%s", dev_name);
231 proc_symlink(dev_name, proc_ide_root, name);
234 static int fake_ide_setup(char *str)
240 __setup("fake_ide", fake_ide_setup);
242 __uml_help(fake_ide_setup,
244 " Create ide0 entries that map onto ubd devices.\n\n"
247 static int parse_unit(char **ptr)
249 char *str = *ptr, *end;
253 n = simple_strtoul(str, &end, 0);
258 else if (('a' <= *str) && (*str <= 'z')) {
266 /* If *index_out == -1 at exit, the passed option was a general one;
267 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
268 * should not be freed on exit.
270 static int ubd_setup_common(char *str, int *index_out, char **error_out)
273 struct openflags flags = global_openflags;
277 if(index_out) *index_out = -1;
284 if(!strcmp(str, "sync")){
285 global_openflags = of_sync(global_openflags);
290 major = simple_strtoul(str, &end, 0);
291 if((*end != '\0') || (end == str)){
292 *error_out = "Didn't parse major number";
296 mutex_lock(&ubd_lock);
297 if (fake_major != UBD_MAJOR) {
298 *error_out = "Can't assign a fake major twice";
304 printk(KERN_INFO "Setting extra ubd major number to %d\n",
308 mutex_unlock(&ubd_lock);
312 n = parse_unit(&str);
314 *error_out = "Couldn't parse device number";
318 *error_out = "Device number out of range";
323 mutex_lock(&ubd_lock);
325 ubd_dev = &ubd_devs[n];
326 if(ubd_dev->file != NULL){
327 *error_out = "Device is already configured";
335 for (i = 0; i < sizeof("rscdt="); i++) {
350 ubd_dev->no_trim = 1;
356 *error_out = "Expected '=' or flag letter "
364 *error_out = "Too many flags specified";
366 *error_out = "Missing '='";
370 backing_file = strchr(str, ',');
372 if (backing_file == NULL)
373 backing_file = strchr(str, ':');
375 if(backing_file != NULL){
377 *error_out = "Can't specify both 'd' and a cow file";
381 *backing_file = '\0';
387 ubd_dev->cow.file = backing_file;
388 ubd_dev->boot_openflags = flags;
390 mutex_unlock(&ubd_lock);
394 static int ubd_setup(char *str)
399 err = ubd_setup_common(str, NULL, &error);
401 printk(KERN_ERR "Failed to initialize device with \"%s\" : "
406 __setup("ubd", ubd_setup);
407 __uml_help(ubd_setup,
408 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
409 " This is used to associate a device with a file in the underlying\n"
410 " filesystem. When specifying two filenames, the first one is the\n"
411 " COW name and the second is the backing file name. As separator you can\n"
412 " use either a ':' or a ',': the first one allows writing things like;\n"
413 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
414 " while with a ',' the shell would not expand the 2nd '~'.\n"
415 " When using only one filename, UML will detect whether to treat it like\n"
416 " a COW file or a backing file. To override this detection, add the 'd'\n"
418 " ubd0d=BackingFile\n"
419 " Usually, there is a filesystem in the file, but \n"
420 " that's not required. Swap devices containing swap files can be\n"
421 " specified like this. Also, a file which doesn't contain a\n"
422 " filesystem can have its contents read in the virtual \n"
423 " machine by running 'dd' on the device. <n> must be in the range\n"
424 " 0 to 7. Appending an 'r' to the number will cause that device\n"
425 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
426 " an 's' will cause data to be written to disk on the host immediately.\n"
427 " 'c' will cause the device to be treated as being shared between multiple\n"
428 " UMLs and file locking will be turned off - this is appropriate for a\n"
429 " cluster filesystem and inappropriate at almost all other times.\n\n"
430 " 't' will disable trim/discard support on the device (enabled by default).\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 /* Only changed by ubd_init, which is an initcall. */
450 static int thread_fd = -1;
452 /* Function to read several request pointers at a time
453 * handling fractional reads if (and as) needed
456 static int bulk_req_safe_read(
458 struct io_thread_req * (*request_buffer)[],
459 struct io_thread_req **remainder,
467 if (*remainder_size > 0) {
469 (char *) request_buffer,
470 (char *) remainder, *remainder_size
477 ((char *) request_buffer) + *remainder_size,
478 sizeof(struct io_thread_req *)*max_recs
483 if ((n % sizeof(struct io_thread_req *)) > 0) {
485 * Read somehow returned not a multiple of dword
486 * theoretically possible, but never observed in the
487 * wild, so read routine must be able to handle it
489 *remainder_size = n % sizeof(struct io_thread_req *);
490 WARN(*remainder_size > 0, "UBD IPC read returned a partial result");
493 ((char *) request_buffer) +
494 (n/sizeof(struct io_thread_req *))*sizeof(struct io_thread_req *),
497 n = n - *remainder_size;
505 /* Called without dev->lock held, and only in interrupt context. */
506 static void ubd_handler(void)
512 n = bulk_req_safe_read(
522 printk(KERN_ERR "spurious interrupt in ubd_handler, "
526 for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
527 struct io_thread_req *io_req = (*irq_req_buffer)[count];
529 if ((io_req->error == BLK_STS_NOTSUPP) && (req_op(io_req->req) == REQ_OP_DISCARD)) {
530 blk_queue_max_discard_sectors(io_req->req->q, 0);
531 blk_queue_max_write_zeroes_sectors(io_req->req->q, 0);
532 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, io_req->req->q);
534 blk_mq_end_request(io_req->req, io_req->error);
540 static irqreturn_t ubd_intr(int irq, void *dev)
546 /* Only changed by ubd_init, which is an initcall. */
547 static int io_pid = -1;
549 static void kill_io_thread(void)
552 os_kill_process(io_pid, 1);
555 __uml_exitcall(kill_io_thread);
557 static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
567 unsigned long long size;
571 if (ubd_dev->file && ubd_dev->cow.file) {
572 file = ubd_dev->cow.file;
577 fd = os_open_file(ubd_dev->file, of_read(OPENFLAGS()), 0);
581 err = read_cow_header(file_reader, &fd, &version, &backing_file, \
582 &mtime, &size, §or_size, &align, &bitmap_offset);
586 file = ubd_dev->file;
591 return os_file_size(file, size_out);
594 static int read_cow_bitmap(int fd, void *buf, int offset, int len)
598 err = os_pread_file(fd, buf, len, offset);
605 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
607 unsigned long modtime;
608 unsigned long long actual;
611 err = os_file_modtime(file, &modtime);
613 printk(KERN_ERR "Failed to get modification time of backing "
614 "file \"%s\", err = %d\n", file, -err);
618 err = os_file_size(file, &actual);
620 printk(KERN_ERR "Failed to get size of backing file \"%s\", "
621 "err = %d\n", file, -err);
625 if (actual != size) {
626 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
628 printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
629 "vs backing file\n", (unsigned long long) size, actual);
632 if (modtime != mtime) {
633 printk(KERN_ERR "mtime mismatch (%ld vs %ld) of COW header vs "
634 "backing file\n", mtime, modtime);
640 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
642 struct uml_stat buf1, buf2;
645 if (from_cmdline == NULL)
647 if (!strcmp(from_cmdline, from_cow))
650 err = os_stat_file(from_cmdline, &buf1);
652 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
656 err = os_stat_file(from_cow, &buf2);
658 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
662 if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
665 printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
666 "\"%s\" specified in COW header of \"%s\"\n",
667 from_cmdline, from_cow, cow);
671 static int open_ubd_file(char *file, struct openflags *openflags, int shared,
672 char **backing_file_out, int *bitmap_offset_out,
673 unsigned long *bitmap_len_out, int *data_offset_out,
677 unsigned long long size;
678 __u32 version, align;
680 int fd, err, sectorsize, asked_switch, mode = 0644;
682 fd = os_open_file(file, *openflags, mode);
684 if ((fd == -ENOENT) && (create_cow_out != NULL))
687 ((fd != -EROFS) && (fd != -EACCES)))
690 fd = os_open_file(file, *openflags, mode);
696 printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
698 err = os_lock_file(fd, openflags->w);
700 printk(KERN_ERR "Failed to lock '%s', err = %d\n",
706 /* Successful return case! */
707 if (backing_file_out == NULL)
710 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
711 &size, §orsize, &align, bitmap_offset_out);
712 if (err && (*backing_file_out != NULL)) {
713 printk(KERN_ERR "Failed to read COW header from COW file "
714 "\"%s\", errno = %d\n", file, -err);
720 asked_switch = path_requires_switch(*backing_file_out, backing_file,
723 /* Allow switching only if no mismatch. */
724 if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
726 printk(KERN_ERR "Switching backing file to '%s'\n",
728 err = write_cow_header(file, fd, *backing_file_out,
729 sectorsize, align, &size);
731 printk(KERN_ERR "Switch failed, errno = %d\n", -err);
735 *backing_file_out = backing_file;
736 err = backing_file_mismatch(*backing_file_out, size, mtime);
741 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
742 bitmap_len_out, data_offset_out);
750 static int create_cow_file(char *cow_file, char *backing_file,
751 struct openflags flags,
752 int sectorsize, int alignment, int *bitmap_offset_out,
753 unsigned long *bitmap_len_out, int *data_offset_out)
758 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
761 printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
766 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
767 bitmap_offset_out, bitmap_len_out,
776 static void ubd_close_dev(struct ubd *ubd_dev)
778 os_close_file(ubd_dev->fd);
779 if(ubd_dev->cow.file == NULL)
782 os_close_file(ubd_dev->cow.fd);
783 vfree(ubd_dev->cow.bitmap);
784 ubd_dev->cow.bitmap = NULL;
787 static int ubd_open_dev(struct ubd *ubd_dev)
789 struct openflags flags;
791 int err, create_cow, *create_ptr;
794 ubd_dev->openflags = ubd_dev->boot_openflags;
796 create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
797 back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
799 fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
800 back_ptr, &ubd_dev->cow.bitmap_offset,
801 &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
804 if((fd == -ENOENT) && create_cow){
805 fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
806 ubd_dev->openflags, SECTOR_SIZE, PAGE_SIZE,
807 &ubd_dev->cow.bitmap_offset,
808 &ubd_dev->cow.bitmap_len,
809 &ubd_dev->cow.data_offset);
811 printk(KERN_INFO "Creating \"%s\" as COW file for "
812 "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
817 printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
823 if(ubd_dev->cow.file != NULL){
824 blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
827 ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
828 if(ubd_dev->cow.bitmap == NULL){
829 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
832 flush_tlb_kernel_vm();
834 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
835 ubd_dev->cow.bitmap_offset,
836 ubd_dev->cow.bitmap_len);
840 flags = ubd_dev->openflags;
842 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
843 NULL, NULL, NULL, NULL);
844 if(err < 0) goto error;
845 ubd_dev->cow.fd = err;
847 if (ubd_dev->no_trim == 0) {
848 ubd_dev->queue->limits.discard_granularity = SECTOR_SIZE;
849 ubd_dev->queue->limits.discard_alignment = SECTOR_SIZE;
850 blk_queue_max_discard_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
851 blk_queue_max_write_zeroes_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
852 blk_queue_flag_set(QUEUE_FLAG_DISCARD, ubd_dev->queue);
854 blk_queue_flag_set(QUEUE_FLAG_NONROT, ubd_dev->queue);
857 os_close_file(ubd_dev->fd);
861 static void ubd_device_release(struct device *dev)
863 struct ubd *ubd_dev = dev_get_drvdata(dev);
865 blk_cleanup_queue(ubd_dev->queue);
866 blk_mq_free_tag_set(&ubd_dev->tag_set);
867 *ubd_dev = ((struct ubd) DEFAULT_UBD);
870 static int ubd_disk_register(int major, u64 size, int unit,
871 struct gendisk **disk_out)
873 struct device *parent = NULL;
874 struct gendisk *disk;
876 disk = alloc_disk(1 << UBD_SHIFT);
881 disk->first_minor = unit << UBD_SHIFT;
882 disk->fops = &ubd_blops;
883 set_capacity(disk, size / 512);
884 if (major == UBD_MAJOR)
885 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
887 sprintf(disk->disk_name, "ubd_fake%d", unit);
889 /* sysfs register (not for ide fake devices) */
890 if (major == UBD_MAJOR) {
891 ubd_devs[unit].pdev.id = unit;
892 ubd_devs[unit].pdev.name = DRIVER_NAME;
893 ubd_devs[unit].pdev.dev.release = ubd_device_release;
894 dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
895 platform_device_register(&ubd_devs[unit].pdev);
896 parent = &ubd_devs[unit].pdev.dev;
899 disk->private_data = &ubd_devs[unit];
900 disk->queue = ubd_devs[unit].queue;
901 device_add_disk(parent, disk, NULL);
907 #define ROUND_BLOCK(n) ((n + (SECTOR_SIZE - 1)) & (-SECTOR_SIZE))
909 static const struct blk_mq_ops ubd_mq_ops = {
910 .queue_rq = ubd_queue_rq,
913 static int ubd_add(int n, char **error_out)
915 struct ubd *ubd_dev = &ubd_devs[n];
918 if(ubd_dev->file == NULL)
921 err = ubd_file_size(ubd_dev, &ubd_dev->size);
923 *error_out = "Couldn't determine size of device's file";
927 ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
929 ubd_dev->tag_set.ops = &ubd_mq_ops;
930 ubd_dev->tag_set.queue_depth = 64;
931 ubd_dev->tag_set.numa_node = NUMA_NO_NODE;
932 ubd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
933 ubd_dev->tag_set.driver_data = ubd_dev;
934 ubd_dev->tag_set.nr_hw_queues = 1;
936 err = blk_mq_alloc_tag_set(&ubd_dev->tag_set);
940 ubd_dev->queue = blk_mq_init_queue(&ubd_dev->tag_set);
941 if (IS_ERR(ubd_dev->queue)) {
942 err = PTR_ERR(ubd_dev->queue);
943 goto out_cleanup_tags;
946 ubd_dev->queue->queuedata = ubd_dev;
947 blk_queue_write_cache(ubd_dev->queue, true, false);
949 blk_queue_max_segments(ubd_dev->queue, MAX_SG);
950 blk_queue_segment_boundary(ubd_dev->queue, PAGE_SIZE - 1);
951 err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
953 *error_out = "Failed to register device";
954 goto out_cleanup_tags;
957 if (fake_major != UBD_MAJOR)
958 ubd_disk_register(fake_major, ubd_dev->size, n,
962 * Perhaps this should also be under the "if (fake_major)" above
963 * using the fake_disk->disk_name
966 make_ide_entries(ubd_gendisk[n]->disk_name);
973 blk_mq_free_tag_set(&ubd_dev->tag_set);
974 if (!(IS_ERR(ubd_dev->queue)))
975 blk_cleanup_queue(ubd_dev->queue);
979 static int ubd_config(char *str, char **error_out)
983 /* This string is possibly broken up and stored, so it's only
984 * freed if ubd_setup_common fails, or if only general options
987 str = kstrdup(str, GFP_KERNEL);
989 *error_out = "Failed to allocate memory";
993 ret = ubd_setup_common(str, &n, error_out);
1002 mutex_lock(&ubd_lock);
1003 ret = ubd_add(n, error_out);
1005 ubd_devs[n].file = NULL;
1006 mutex_unlock(&ubd_lock);
1016 static int ubd_get_config(char *name, char *str, int size, char **error_out)
1018 struct ubd *ubd_dev;
1021 n = parse_unit(&name);
1022 if((n >= MAX_DEV) || (n < 0)){
1023 *error_out = "ubd_get_config : device number out of range";
1027 ubd_dev = &ubd_devs[n];
1028 mutex_lock(&ubd_lock);
1030 if(ubd_dev->file == NULL){
1031 CONFIG_CHUNK(str, size, len, "", 1);
1035 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
1037 if(ubd_dev->cow.file != NULL){
1038 CONFIG_CHUNK(str, size, len, ",", 0);
1039 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
1041 else CONFIG_CHUNK(str, size, len, "", 1);
1044 mutex_unlock(&ubd_lock);
1048 static int ubd_id(char **str, int *start_out, int *end_out)
1052 n = parse_unit(str);
1054 *end_out = MAX_DEV - 1;
1058 static int ubd_remove(int n, char **error_out)
1060 struct gendisk *disk = ubd_gendisk[n];
1061 struct ubd *ubd_dev;
1064 mutex_lock(&ubd_lock);
1066 ubd_dev = &ubd_devs[n];
1068 if(ubd_dev->file == NULL)
1071 /* you cannot remove a open disk */
1073 if(ubd_dev->count > 0)
1076 ubd_gendisk[n] = NULL;
1082 if(fake_gendisk[n] != NULL){
1083 del_gendisk(fake_gendisk[n]);
1084 put_disk(fake_gendisk[n]);
1085 fake_gendisk[n] = NULL;
1089 platform_device_unregister(&ubd_dev->pdev);
1091 mutex_unlock(&ubd_lock);
1095 /* All these are called by mconsole in process context and without
1096 * ubd-specific locks. The structure itself is const except for .list.
1098 static struct mc_device ubd_mc = {
1099 .list = LIST_HEAD_INIT(ubd_mc.list),
1101 .config = ubd_config,
1102 .get_config = ubd_get_config,
1104 .remove = ubd_remove,
1107 static int __init ubd_mc_init(void)
1109 mconsole_register_dev(&ubd_mc);
1113 __initcall(ubd_mc_init);
1115 static int __init ubd0_init(void)
1117 struct ubd *ubd_dev = &ubd_devs[0];
1119 mutex_lock(&ubd_lock);
1120 if(ubd_dev->file == NULL)
1121 ubd_dev->file = "root_fs";
1122 mutex_unlock(&ubd_lock);
1127 __initcall(ubd0_init);
1129 /* Used in ubd_init, which is an initcall */
1130 static struct platform_driver ubd_driver = {
1132 .name = DRIVER_NAME,
1136 static int __init ubd_init(void)
1141 if (register_blkdev(UBD_MAJOR, "ubd"))
1144 if (fake_major != UBD_MAJOR) {
1145 char name[sizeof("ubd_nnn\0")];
1147 snprintf(name, sizeof(name), "ubd_%d", fake_major);
1148 if (register_blkdev(fake_major, "ubd"))
1152 irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
1153 sizeof(struct io_thread_req *),
1158 if (irq_req_buffer == NULL) {
1159 printk(KERN_ERR "Failed to initialize ubd buffering\n");
1162 io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
1163 sizeof(struct io_thread_req *),
1169 if (io_req_buffer == NULL) {
1170 printk(KERN_ERR "Failed to initialize ubd buffering\n");
1173 platform_driver_register(&ubd_driver);
1174 mutex_lock(&ubd_lock);
1175 for (i = 0; i < MAX_DEV; i++){
1176 err = ubd_add(i, &error);
1178 printk(KERN_ERR "Failed to initialize ubd device %d :"
1181 mutex_unlock(&ubd_lock);
1185 late_initcall(ubd_init);
1187 static int __init ubd_driver_init(void){
1188 unsigned long stack;
1191 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1192 if(global_openflags.s){
1193 printk(KERN_INFO "ubd: Synchronous mode\n");
1194 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1195 * enough. So use anyway the io thread. */
1197 stack = alloc_stack(0, 0);
1198 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
1202 "ubd : Failed to start I/O thread (errno = %d) - "
1203 "falling back to synchronous I/O\n", -io_pid);
1207 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
1208 0, "ubd", ubd_devs);
1210 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
1214 device_initcall(ubd_driver_init);
1216 static int ubd_open(struct block_device *bdev, fmode_t mode)
1218 struct gendisk *disk = bdev->bd_disk;
1219 struct ubd *ubd_dev = disk->private_data;
1222 mutex_lock(&ubd_mutex);
1223 if(ubd_dev->count == 0){
1224 err = ubd_open_dev(ubd_dev);
1226 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
1227 disk->disk_name, ubd_dev->file, -err);
1232 set_disk_ro(disk, !ubd_dev->openflags.w);
1234 /* This should no more be needed. And it didn't work anyway to exclude
1235 * read-write remounting of filesystems.*/
1236 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1237 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1241 mutex_unlock(&ubd_mutex);
1245 static void ubd_release(struct gendisk *disk, fmode_t mode)
1247 struct ubd *ubd_dev = disk->private_data;
1249 mutex_lock(&ubd_mutex);
1250 if(--ubd_dev->count == 0)
1251 ubd_close_dev(ubd_dev);
1252 mutex_unlock(&ubd_mutex);
1255 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
1256 __u64 *cow_offset, unsigned long *bitmap,
1257 __u64 bitmap_offset, unsigned long *bitmap_words,
1260 __u64 sector = io_offset >> SECTOR_SHIFT;
1261 int i, update_bitmap = 0;
1263 for (i = 0; i < length >> SECTOR_SHIFT; i++) {
1264 if(cow_mask != NULL)
1265 ubd_set_bit(i, (unsigned char *) cow_mask);
1266 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1270 ubd_set_bit(sector + i, (unsigned char *) bitmap);
1276 *cow_offset = sector / (sizeof(unsigned long) * 8);
1278 /* This takes care of the case where we're exactly at the end of the
1279 * device, and *cow_offset + 1 is off the end. So, just back it up
1280 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1281 * for the original diagnosis.
1283 if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
1284 sizeof(unsigned long)) - 1))
1287 bitmap_words[0] = bitmap[*cow_offset];
1288 bitmap_words[1] = bitmap[*cow_offset + 1];
1290 *cow_offset *= sizeof(unsigned long);
1291 *cow_offset += bitmap_offset;
1294 static void cowify_req(struct io_thread_req *req, struct io_desc *segment,
1295 unsigned long offset, unsigned long *bitmap,
1296 __u64 bitmap_offset, __u64 bitmap_len)
1298 __u64 sector = offset >> SECTOR_SHIFT;
1301 if (segment->length > (sizeof(segment->sector_mask) * 8) << SECTOR_SHIFT)
1302 panic("Operation too long");
1304 if (req_op(req->req) == REQ_OP_READ) {
1305 for (i = 0; i < segment->length >> SECTOR_SHIFT; i++) {
1306 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1307 ubd_set_bit(i, (unsigned char *)
1308 &segment->sector_mask);
1311 cowify_bitmap(offset, segment->length, &segment->sector_mask,
1312 &segment->cow_offset, bitmap, bitmap_offset,
1313 segment->bitmap_words, bitmap_len);
1317 static void ubd_map_req(struct ubd *dev, struct io_thread_req *io_req,
1318 struct request *req)
1320 struct bio_vec bvec;
1321 struct req_iterator iter;
1323 unsigned long byte_offset = io_req->offset;
1324 int op = req_op(req);
1326 if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD) {
1327 io_req->io_desc[0].buffer = NULL;
1328 io_req->io_desc[0].length = blk_rq_bytes(req);
1330 rq_for_each_segment(bvec, req, iter) {
1331 BUG_ON(i >= io_req->desc_cnt);
1333 io_req->io_desc[i].buffer =
1334 page_address(bvec.bv_page) + bvec.bv_offset;
1335 io_req->io_desc[i].length = bvec.bv_len;
1340 if (dev->cow.file) {
1341 for (i = 0; i < io_req->desc_cnt; i++) {
1342 cowify_req(io_req, &io_req->io_desc[i], byte_offset,
1343 dev->cow.bitmap, dev->cow.bitmap_offset,
1344 dev->cow.bitmap_len);
1345 byte_offset += io_req->io_desc[i].length;
1351 static struct io_thread_req *ubd_alloc_req(struct ubd *dev, struct request *req,
1354 struct io_thread_req *io_req;
1357 io_req = kmalloc(sizeof(*io_req) +
1358 (desc_cnt * sizeof(struct io_desc)),
1365 io_req->fds[0] = dev->cow.fd;
1367 io_req->fds[0] = dev->fd;
1369 io_req->sectorsize = SECTOR_SIZE;
1370 io_req->fds[1] = dev->fd;
1371 io_req->offset = (u64) blk_rq_pos(req) << SECTOR_SHIFT;
1372 io_req->offsets[0] = 0;
1373 io_req->offsets[1] = dev->cow.data_offset;
1375 for (i = 0 ; i < desc_cnt; i++) {
1376 io_req->io_desc[i].sector_mask = 0;
1377 io_req->io_desc[i].cow_offset = -1;
1383 static int ubd_submit_request(struct ubd *dev, struct request *req)
1386 struct io_thread_req *io_req;
1388 int op = req_op(req);
1390 if (op == REQ_OP_FLUSH)
1392 else if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD)
1395 segs = blk_rq_nr_phys_segments(req);
1397 io_req = ubd_alloc_req(dev, req, segs);
1401 io_req->desc_cnt = segs;
1403 ubd_map_req(dev, io_req, req);
1405 ret = os_write_file(thread_fd, &io_req, sizeof(io_req));
1406 if (ret != sizeof(io_req)) {
1408 pr_err("write to io thread failed: %d\n", -ret);
1414 static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
1415 const struct blk_mq_queue_data *bd)
1417 struct ubd *ubd_dev = hctx->queue->queuedata;
1418 struct request *req = bd->rq;
1419 int ret = 0, res = BLK_STS_OK;
1421 blk_mq_start_request(req);
1423 spin_lock_irq(&ubd_dev->lock);
1425 switch (req_op(req)) {
1429 case REQ_OP_DISCARD:
1430 case REQ_OP_WRITE_ZEROES:
1431 ret = ubd_submit_request(ubd_dev, req);
1435 res = BLK_STS_NOTSUPP;
1438 spin_unlock_irq(&ubd_dev->lock);
1442 res = BLK_STS_RESOURCE;
1444 res = BLK_STS_DEV_RESOURCE;
1450 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1452 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1456 geo->cylinders = ubd_dev->size / (128 * 32 * 512);
1460 static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
1461 unsigned int cmd, unsigned long arg)
1463 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1464 u16 ubd_id[ATA_ID_WORDS];
1467 struct cdrom_volctrl volume;
1468 case HDIO_GET_IDENTITY:
1469 memset(&ubd_id, 0, ATA_ID_WORDS * 2);
1470 ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512);
1471 ubd_id[ATA_ID_HEADS] = 128;
1472 ubd_id[ATA_ID_SECTORS] = 32;
1473 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1479 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1481 volume.channel0 = 255;
1482 volume.channel1 = 255;
1483 volume.channel2 = 255;
1484 volume.channel3 = 255;
1485 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1492 static int map_error(int error_code)
1494 switch (error_code) {
1499 return BLK_STS_NOTSUPP;
1501 return BLK_STS_NOSPC;
1503 return BLK_STS_IOERR;
1507 * Everything from here onwards *IS NOT PART OF THE KERNEL*
1509 * The following functions are part of UML hypervisor code.
1510 * All functions from here onwards are executed as a helper
1511 * thread and are not allowed to execute any kernel functions.
1513 * Any communication must occur strictly via shared memory and IPC.
1515 * Do not add printks, locks, kernel memory operations, etc - it
1516 * will result in unpredictable behaviour and/or crashes.
1519 static int update_bitmap(struct io_thread_req *req, struct io_desc *segment)
1523 if (segment->cow_offset == -1)
1524 return map_error(0);
1526 n = os_pwrite_file(req->fds[1], &segment->bitmap_words,
1527 sizeof(segment->bitmap_words), segment->cow_offset);
1528 if (n != sizeof(segment->bitmap_words))
1529 return map_error(-n);
1531 return map_error(0);
1534 static void do_io(struct io_thread_req *req, struct io_desc *desc)
1538 int n, nsectors, start, end, bit;
1541 /* FLUSH is really a special case, we cannot "case" it with others */
1543 if (req_op(req->req) == REQ_OP_FLUSH) {
1544 /* fds[0] is always either the rw image or our cow file */
1545 req->error = map_error(-os_sync_file(req->fds[0]));
1549 nsectors = desc->length / req->sectorsize;
1552 bit = ubd_test_bit(start, (unsigned char *) &desc->sector_mask);
1554 while((end < nsectors) &&
1555 (ubd_test_bit(end, (unsigned char *) &desc->sector_mask) == bit))
1558 off = req->offset + req->offsets[bit] +
1559 start * req->sectorsize;
1560 len = (end - start) * req->sectorsize;
1561 if (desc->buffer != NULL)
1562 buf = &desc->buffer[start * req->sectorsize];
1564 switch (req_op(req->req)) {
1570 n = os_pread_file(req->fds[bit], buf, len, off);
1572 req->error = map_error(-n);
1575 } while((n < len) && (n != 0));
1576 if (n < len) memset(&buf[n], 0, len - n);
1579 n = os_pwrite_file(req->fds[bit], buf, len, off);
1581 req->error = map_error(-n);
1585 case REQ_OP_DISCARD:
1586 case REQ_OP_WRITE_ZEROES:
1587 n = os_falloc_punch(req->fds[bit], off, len);
1589 req->error = map_error(-n);
1595 req->error = BLK_STS_NOTSUPP;
1600 } while(start < nsectors);
1603 req->error = update_bitmap(req, desc);
1606 /* Changed in start_io_thread, which is serialized by being called only
1607 * from ubd_init, which is an initcall.
1611 /* Only changed by the io thread. XXX: currently unused. */
1612 static int io_count = 0;
1614 int io_thread(void *arg)
1616 int n, count, written, res;
1618 os_fix_helper_signals();
1621 n = bulk_req_safe_read(
1635 for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
1636 struct io_thread_req *req = (*io_req_buffer)[count];
1640 for (i = 0; !req->error && i < req->desc_cnt; i++)
1641 do_io(req, &(req->io_desc[i]));
1648 res = os_write_file(kernel_fd,
1649 ((char *) io_req_buffer) + written,
1657 } while (written < n);