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. */
162 struct openflags boot_openflags;
163 struct openflags openflags;
168 struct platform_device pdev;
169 struct request_queue *queue;
170 struct blk_mq_tag_set tag_set;
174 #define DEFAULT_COW { \
178 .bitmap_offset = 0, \
182 #define DEFAULT_UBD { \
188 .boot_openflags = OPEN_FLAGS, \
189 .openflags = OPEN_FLAGS, \
193 .cow = DEFAULT_COW, \
194 .lock = __SPIN_LOCK_UNLOCKED(ubd_devs.lock), \
197 /* Protected by ubd_lock */
198 static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD };
200 /* Only changed by fake_ide_setup which is a setup */
201 static int fake_ide = 0;
202 static struct proc_dir_entry *proc_ide_root = NULL;
203 static struct proc_dir_entry *proc_ide = NULL;
205 static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
206 const struct blk_mq_queue_data *bd);
208 static void make_proc_ide(void)
210 proc_ide_root = proc_mkdir("ide", NULL);
211 proc_ide = proc_mkdir("ide0", proc_ide_root);
214 static int fake_ide_media_proc_show(struct seq_file *m, void *v)
216 seq_puts(m, "disk\n");
220 static void make_ide_entries(const char *dev_name)
222 struct proc_dir_entry *dir, *ent;
225 if(proc_ide_root == NULL) make_proc_ide();
227 dir = proc_mkdir(dev_name, proc_ide);
230 ent = proc_create_single("media", S_IRUGO, dir,
231 fake_ide_media_proc_show);
233 snprintf(name, sizeof(name), "ide0/%s", dev_name);
234 proc_symlink(dev_name, proc_ide_root, name);
237 static int fake_ide_setup(char *str)
243 __setup("fake_ide", fake_ide_setup);
245 __uml_help(fake_ide_setup,
247 " Create ide0 entries that map onto ubd devices.\n\n"
250 static int parse_unit(char **ptr)
252 char *str = *ptr, *end;
256 n = simple_strtoul(str, &end, 0);
261 else if (('a' <= *str) && (*str <= 'z')) {
269 /* If *index_out == -1 at exit, the passed option was a general one;
270 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
271 * should not be freed on exit.
273 static int ubd_setup_common(char *str, int *index_out, char **error_out)
276 struct openflags flags = global_openflags;
277 char *file, *backing_file, *serial;
280 if(index_out) *index_out = -1;
287 if(!strcmp(str, "sync")){
288 global_openflags = of_sync(global_openflags);
293 major = simple_strtoul(str, &end, 0);
294 if((*end != '\0') || (end == str)){
295 *error_out = "Didn't parse major number";
299 mutex_lock(&ubd_lock);
300 if (fake_major != UBD_MAJOR) {
301 *error_out = "Can't assign a fake major twice";
307 printk(KERN_INFO "Setting extra ubd major number to %d\n",
311 mutex_unlock(&ubd_lock);
315 n = parse_unit(&str);
317 *error_out = "Couldn't parse device number";
321 *error_out = "Device number out of range";
326 mutex_lock(&ubd_lock);
328 ubd_dev = &ubd_devs[n];
329 if(ubd_dev->file != NULL){
330 *error_out = "Device is already configured";
338 for (i = 0; i < sizeof("rscdt="); i++) {
353 ubd_dev->no_trim = 1;
359 *error_out = "Expected '=' or flag letter "
367 *error_out = "Too many flags specified";
369 *error_out = "Missing '='";
373 file = strsep(&str, ",:");
377 backing_file = strsep(&str, ",:");
378 if (backing_file && *backing_file == '\0')
381 serial = strsep(&str, ",:");
382 if (serial && *serial == '\0')
385 if (backing_file && ubd_dev->no_cow) {
386 *error_out = "Can't specify both 'd' and a cow file";
391 ubd_dev->file = file;
392 ubd_dev->cow.file = backing_file;
393 ubd_dev->serial = serial;
394 ubd_dev->boot_openflags = flags;
396 mutex_unlock(&ubd_lock);
400 static int ubd_setup(char *str)
405 err = ubd_setup_common(str, NULL, &error);
407 printk(KERN_ERR "Failed to initialize device with \"%s\" : "
412 __setup("ubd", ubd_setup);
413 __uml_help(ubd_setup,
414 "ubd<n><flags>=<filename>[(:|,)<filename2>][(:|,)<serial>]\n"
415 " This is used to associate a device with a file in the underlying\n"
416 " filesystem. When specifying two filenames, the first one is the\n"
417 " COW name and the second is the backing file name. As separator you can\n"
418 " use either a ':' or a ',': the first one allows writing things like;\n"
419 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
420 " while with a ',' the shell would not expand the 2nd '~'.\n"
421 " When using only one filename, UML will detect whether to treat it like\n"
422 " a COW file or a backing file. To override this detection, add the 'd'\n"
424 " ubd0d=BackingFile\n"
425 " Usually, there is a filesystem in the file, but \n"
426 " that's not required. Swap devices containing swap files can be\n"
427 " specified like this. Also, a file which doesn't contain a\n"
428 " filesystem can have its contents read in the virtual \n"
429 " machine by running 'dd' on the device. <n> must be in the range\n"
430 " 0 to 7. Appending an 'r' to the number will cause that device\n"
431 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
432 " an 's' will cause data to be written to disk on the host immediately.\n"
433 " 'c' will cause the device to be treated as being shared between multiple\n"
434 " UMLs and file locking will be turned off - this is appropriate for a\n"
435 " cluster filesystem and inappropriate at almost all other times.\n\n"
436 " 't' will disable trim/discard support on the device (enabled by default).\n\n"
437 " An optional device serial number can be exposed using the serial parameter\n"
438 " on the cmdline which is exposed as a sysfs entry. This is particularly\n"
439 " useful when a unique number should be given to the device. Note when\n"
440 " specifying a label, the filename2 must be also presented. It can be\n"
441 " an empty string, in which case the backing file is not used:\n"
442 " ubd0=File,,Serial\n"
445 static int udb_setup(char *str)
447 printk("udb%s specified on command line is almost certainly a ubd -> "
452 __setup("udb", udb_setup);
453 __uml_help(udb_setup,
455 " This option is here solely to catch ubd -> udb typos, which can be\n"
456 " to impossible to catch visually unless you specifically look for\n"
457 " them. The only result of any option starting with 'udb' is an error\n"
458 " in the boot output.\n\n"
461 /* Only changed by ubd_init, which is an initcall. */
462 static int thread_fd = -1;
464 /* Function to read several request pointers at a time
465 * handling fractional reads if (and as) needed
468 static int bulk_req_safe_read(
470 struct io_thread_req * (*request_buffer)[],
471 struct io_thread_req **remainder,
479 if (*remainder_size > 0) {
481 (char *) request_buffer,
482 (char *) remainder, *remainder_size
489 ((char *) request_buffer) + *remainder_size,
490 sizeof(struct io_thread_req *)*max_recs
495 if ((n % sizeof(struct io_thread_req *)) > 0) {
497 * Read somehow returned not a multiple of dword
498 * theoretically possible, but never observed in the
499 * wild, so read routine must be able to handle it
501 *remainder_size = n % sizeof(struct io_thread_req *);
502 WARN(*remainder_size > 0, "UBD IPC read returned a partial result");
505 ((char *) request_buffer) +
506 (n/sizeof(struct io_thread_req *))*sizeof(struct io_thread_req *),
509 n = n - *remainder_size;
517 /* Called without dev->lock held, and only in interrupt context. */
518 static void ubd_handler(void)
524 n = bulk_req_safe_read(
534 printk(KERN_ERR "spurious interrupt in ubd_handler, "
538 for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
539 struct io_thread_req *io_req = (*irq_req_buffer)[count];
541 if ((io_req->error == BLK_STS_NOTSUPP) && (req_op(io_req->req) == REQ_OP_DISCARD)) {
542 blk_queue_max_discard_sectors(io_req->req->q, 0);
543 blk_queue_max_write_zeroes_sectors(io_req->req->q, 0);
544 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, io_req->req->q);
546 blk_mq_end_request(io_req->req, io_req->error);
552 static irqreturn_t ubd_intr(int irq, void *dev)
558 /* Only changed by ubd_init, which is an initcall. */
559 static int io_pid = -1;
561 static void kill_io_thread(void)
564 os_kill_process(io_pid, 1);
567 __uml_exitcall(kill_io_thread);
569 static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
579 unsigned long long size;
583 if (ubd_dev->file && ubd_dev->cow.file) {
584 file = ubd_dev->cow.file;
589 fd = os_open_file(ubd_dev->file, of_read(OPENFLAGS()), 0);
593 err = read_cow_header(file_reader, &fd, &version, &backing_file, \
594 &mtime, &size, §or_size, &align, &bitmap_offset);
598 file = ubd_dev->file;
603 return os_file_size(file, size_out);
606 static int read_cow_bitmap(int fd, void *buf, int offset, int len)
610 err = os_pread_file(fd, buf, len, offset);
617 static int backing_file_mismatch(char *file, __u64 size, time64_t mtime)
620 unsigned long long actual;
623 err = os_file_modtime(file, &modtime);
625 printk(KERN_ERR "Failed to get modification time of backing "
626 "file \"%s\", err = %d\n", file, -err);
630 err = os_file_size(file, &actual);
632 printk(KERN_ERR "Failed to get size of backing file \"%s\", "
633 "err = %d\n", file, -err);
637 if (actual != size) {
638 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
640 printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
641 "vs backing file\n", (unsigned long long) size, actual);
644 if (modtime != mtime) {
645 printk(KERN_ERR "mtime mismatch (%lld vs %lld) of COW header vs "
646 "backing file\n", mtime, modtime);
652 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
654 struct uml_stat buf1, buf2;
657 if (from_cmdline == NULL)
659 if (!strcmp(from_cmdline, from_cow))
662 err = os_stat_file(from_cmdline, &buf1);
664 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
668 err = os_stat_file(from_cow, &buf2);
670 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
674 if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
677 printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
678 "\"%s\" specified in COW header of \"%s\"\n",
679 from_cmdline, from_cow, cow);
683 static int open_ubd_file(char *file, struct openflags *openflags, int shared,
684 char **backing_file_out, int *bitmap_offset_out,
685 unsigned long *bitmap_len_out, int *data_offset_out,
689 unsigned long long size;
690 __u32 version, align;
692 int fd, err, sectorsize, asked_switch, mode = 0644;
694 fd = os_open_file(file, *openflags, mode);
696 if ((fd == -ENOENT) && (create_cow_out != NULL))
699 ((fd != -EROFS) && (fd != -EACCES)))
702 fd = os_open_file(file, *openflags, mode);
708 printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
710 err = os_lock_file(fd, openflags->w);
712 printk(KERN_ERR "Failed to lock '%s', err = %d\n",
718 /* Successful return case! */
719 if (backing_file_out == NULL)
722 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
723 &size, §orsize, &align, bitmap_offset_out);
724 if (err && (*backing_file_out != NULL)) {
725 printk(KERN_ERR "Failed to read COW header from COW file "
726 "\"%s\", errno = %d\n", file, -err);
732 asked_switch = path_requires_switch(*backing_file_out, backing_file,
735 /* Allow switching only if no mismatch. */
736 if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
738 printk(KERN_ERR "Switching backing file to '%s'\n",
740 err = write_cow_header(file, fd, *backing_file_out,
741 sectorsize, align, &size);
743 printk(KERN_ERR "Switch failed, errno = %d\n", -err);
747 *backing_file_out = backing_file;
748 err = backing_file_mismatch(*backing_file_out, size, mtime);
753 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
754 bitmap_len_out, data_offset_out);
762 static int create_cow_file(char *cow_file, char *backing_file,
763 struct openflags flags,
764 int sectorsize, int alignment, int *bitmap_offset_out,
765 unsigned long *bitmap_len_out, int *data_offset_out)
770 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
773 printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
778 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
779 bitmap_offset_out, bitmap_len_out,
788 static void ubd_close_dev(struct ubd *ubd_dev)
790 os_close_file(ubd_dev->fd);
791 if(ubd_dev->cow.file == NULL)
794 os_close_file(ubd_dev->cow.fd);
795 vfree(ubd_dev->cow.bitmap);
796 ubd_dev->cow.bitmap = NULL;
799 static int ubd_open_dev(struct ubd *ubd_dev)
801 struct openflags flags;
803 int err, create_cow, *create_ptr;
806 ubd_dev->openflags = ubd_dev->boot_openflags;
808 create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
809 back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
811 fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
812 back_ptr, &ubd_dev->cow.bitmap_offset,
813 &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
816 if((fd == -ENOENT) && create_cow){
817 fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
818 ubd_dev->openflags, SECTOR_SIZE, PAGE_SIZE,
819 &ubd_dev->cow.bitmap_offset,
820 &ubd_dev->cow.bitmap_len,
821 &ubd_dev->cow.data_offset);
823 printk(KERN_INFO "Creating \"%s\" as COW file for "
824 "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
829 printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
835 if(ubd_dev->cow.file != NULL){
836 blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
839 ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
840 if(ubd_dev->cow.bitmap == NULL){
841 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
844 flush_tlb_kernel_vm();
846 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
847 ubd_dev->cow.bitmap_offset,
848 ubd_dev->cow.bitmap_len);
852 flags = ubd_dev->openflags;
854 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
855 NULL, NULL, NULL, NULL);
856 if(err < 0) goto error;
857 ubd_dev->cow.fd = err;
859 if (ubd_dev->no_trim == 0) {
860 ubd_dev->queue->limits.discard_granularity = SECTOR_SIZE;
861 ubd_dev->queue->limits.discard_alignment = SECTOR_SIZE;
862 blk_queue_max_discard_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
863 blk_queue_max_write_zeroes_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
864 blk_queue_flag_set(QUEUE_FLAG_DISCARD, ubd_dev->queue);
866 blk_queue_flag_set(QUEUE_FLAG_NONROT, ubd_dev->queue);
869 os_close_file(ubd_dev->fd);
873 static void ubd_device_release(struct device *dev)
875 struct ubd *ubd_dev = dev_get_drvdata(dev);
877 blk_cleanup_queue(ubd_dev->queue);
878 blk_mq_free_tag_set(&ubd_dev->tag_set);
879 *ubd_dev = ((struct ubd) DEFAULT_UBD);
882 static ssize_t serial_show(struct device *dev,
883 struct device_attribute *attr, char *buf)
885 struct gendisk *disk = dev_to_disk(dev);
886 struct ubd *ubd_dev = disk->private_data;
891 return sprintf(buf, "%s", ubd_dev->serial);
894 static DEVICE_ATTR_RO(serial);
896 static struct attribute *ubd_attrs[] = {
897 &dev_attr_serial.attr,
901 static umode_t ubd_attrs_are_visible(struct kobject *kobj,
902 struct attribute *a, int n)
907 static const struct attribute_group ubd_attr_group = {
909 .is_visible = ubd_attrs_are_visible,
912 static const struct attribute_group *ubd_attr_groups[] = {
917 static int ubd_disk_register(int major, u64 size, int unit,
918 struct gendisk **disk_out)
920 struct device *parent = NULL;
921 struct gendisk *disk;
923 disk = alloc_disk(1 << UBD_SHIFT);
928 disk->first_minor = unit << UBD_SHIFT;
929 disk->fops = &ubd_blops;
930 set_capacity(disk, size / 512);
931 if (major == UBD_MAJOR)
932 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
934 sprintf(disk->disk_name, "ubd_fake%d", unit);
936 /* sysfs register (not for ide fake devices) */
937 if (major == UBD_MAJOR) {
938 ubd_devs[unit].pdev.id = unit;
939 ubd_devs[unit].pdev.name = DRIVER_NAME;
940 ubd_devs[unit].pdev.dev.release = ubd_device_release;
941 dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
942 platform_device_register(&ubd_devs[unit].pdev);
943 parent = &ubd_devs[unit].pdev.dev;
946 disk->private_data = &ubd_devs[unit];
947 disk->queue = ubd_devs[unit].queue;
948 device_add_disk(parent, disk, ubd_attr_groups);
954 #define ROUND_BLOCK(n) ((n + (SECTOR_SIZE - 1)) & (-SECTOR_SIZE))
956 static const struct blk_mq_ops ubd_mq_ops = {
957 .queue_rq = ubd_queue_rq,
960 static int ubd_add(int n, char **error_out)
962 struct ubd *ubd_dev = &ubd_devs[n];
965 if(ubd_dev->file == NULL)
968 err = ubd_file_size(ubd_dev, &ubd_dev->size);
970 *error_out = "Couldn't determine size of device's file";
974 ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
976 ubd_dev->tag_set.ops = &ubd_mq_ops;
977 ubd_dev->tag_set.queue_depth = 64;
978 ubd_dev->tag_set.numa_node = NUMA_NO_NODE;
979 ubd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
980 ubd_dev->tag_set.driver_data = ubd_dev;
981 ubd_dev->tag_set.nr_hw_queues = 1;
983 err = blk_mq_alloc_tag_set(&ubd_dev->tag_set);
987 ubd_dev->queue = blk_mq_init_queue(&ubd_dev->tag_set);
988 if (IS_ERR(ubd_dev->queue)) {
989 err = PTR_ERR(ubd_dev->queue);
990 goto out_cleanup_tags;
993 ubd_dev->queue->queuedata = ubd_dev;
994 blk_queue_write_cache(ubd_dev->queue, true, false);
996 blk_queue_max_segments(ubd_dev->queue, MAX_SG);
997 blk_queue_segment_boundary(ubd_dev->queue, PAGE_SIZE - 1);
998 err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
1000 *error_out = "Failed to register device";
1001 goto out_cleanup_tags;
1004 if (fake_major != UBD_MAJOR)
1005 ubd_disk_register(fake_major, ubd_dev->size, n,
1009 * Perhaps this should also be under the "if (fake_major)" above
1010 * using the fake_disk->disk_name
1013 make_ide_entries(ubd_gendisk[n]->disk_name);
1020 blk_mq_free_tag_set(&ubd_dev->tag_set);
1021 if (!(IS_ERR(ubd_dev->queue)))
1022 blk_cleanup_queue(ubd_dev->queue);
1026 static int ubd_config(char *str, char **error_out)
1030 /* This string is possibly broken up and stored, so it's only
1031 * freed if ubd_setup_common fails, or if only general options
1034 str = kstrdup(str, GFP_KERNEL);
1036 *error_out = "Failed to allocate memory";
1040 ret = ubd_setup_common(str, &n, error_out);
1049 mutex_lock(&ubd_lock);
1050 ret = ubd_add(n, error_out);
1052 ubd_devs[n].file = NULL;
1053 mutex_unlock(&ubd_lock);
1063 static int ubd_get_config(char *name, char *str, int size, char **error_out)
1065 struct ubd *ubd_dev;
1068 n = parse_unit(&name);
1069 if((n >= MAX_DEV) || (n < 0)){
1070 *error_out = "ubd_get_config : device number out of range";
1074 ubd_dev = &ubd_devs[n];
1075 mutex_lock(&ubd_lock);
1077 if(ubd_dev->file == NULL){
1078 CONFIG_CHUNK(str, size, len, "", 1);
1082 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
1084 if(ubd_dev->cow.file != NULL){
1085 CONFIG_CHUNK(str, size, len, ",", 0);
1086 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
1088 else CONFIG_CHUNK(str, size, len, "", 1);
1091 mutex_unlock(&ubd_lock);
1095 static int ubd_id(char **str, int *start_out, int *end_out)
1099 n = parse_unit(str);
1101 *end_out = MAX_DEV - 1;
1105 static int ubd_remove(int n, char **error_out)
1107 struct gendisk *disk = ubd_gendisk[n];
1108 struct ubd *ubd_dev;
1111 mutex_lock(&ubd_lock);
1113 ubd_dev = &ubd_devs[n];
1115 if(ubd_dev->file == NULL)
1118 /* you cannot remove a open disk */
1120 if(ubd_dev->count > 0)
1123 ubd_gendisk[n] = NULL;
1129 if(fake_gendisk[n] != NULL){
1130 del_gendisk(fake_gendisk[n]);
1131 put_disk(fake_gendisk[n]);
1132 fake_gendisk[n] = NULL;
1136 platform_device_unregister(&ubd_dev->pdev);
1138 mutex_unlock(&ubd_lock);
1142 /* All these are called by mconsole in process context and without
1143 * ubd-specific locks. The structure itself is const except for .list.
1145 static struct mc_device ubd_mc = {
1146 .list = LIST_HEAD_INIT(ubd_mc.list),
1148 .config = ubd_config,
1149 .get_config = ubd_get_config,
1151 .remove = ubd_remove,
1154 static int __init ubd_mc_init(void)
1156 mconsole_register_dev(&ubd_mc);
1160 __initcall(ubd_mc_init);
1162 static int __init ubd0_init(void)
1164 struct ubd *ubd_dev = &ubd_devs[0];
1166 mutex_lock(&ubd_lock);
1167 if(ubd_dev->file == NULL)
1168 ubd_dev->file = "root_fs";
1169 mutex_unlock(&ubd_lock);
1174 __initcall(ubd0_init);
1176 /* Used in ubd_init, which is an initcall */
1177 static struct platform_driver ubd_driver = {
1179 .name = DRIVER_NAME,
1183 static int __init ubd_init(void)
1188 if (register_blkdev(UBD_MAJOR, "ubd"))
1191 if (fake_major != UBD_MAJOR) {
1192 char name[sizeof("ubd_nnn\0")];
1194 snprintf(name, sizeof(name), "ubd_%d", fake_major);
1195 if (register_blkdev(fake_major, "ubd"))
1199 irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
1200 sizeof(struct io_thread_req *),
1205 if (irq_req_buffer == NULL) {
1206 printk(KERN_ERR "Failed to initialize ubd buffering\n");
1209 io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
1210 sizeof(struct io_thread_req *),
1216 if (io_req_buffer == NULL) {
1217 printk(KERN_ERR "Failed to initialize ubd buffering\n");
1220 platform_driver_register(&ubd_driver);
1221 mutex_lock(&ubd_lock);
1222 for (i = 0; i < MAX_DEV; i++){
1223 err = ubd_add(i, &error);
1225 printk(KERN_ERR "Failed to initialize ubd device %d :"
1228 mutex_unlock(&ubd_lock);
1232 late_initcall(ubd_init);
1234 static int __init ubd_driver_init(void){
1235 unsigned long stack;
1238 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1239 if(global_openflags.s){
1240 printk(KERN_INFO "ubd: Synchronous mode\n");
1241 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1242 * enough. So use anyway the io thread. */
1244 stack = alloc_stack(0, 0);
1245 io_pid = start_io_thread(stack + PAGE_SIZE, &thread_fd);
1248 "ubd : Failed to start I/O thread (errno = %d) - "
1249 "falling back to synchronous I/O\n", -io_pid);
1253 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
1254 0, "ubd", ubd_devs);
1256 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
1260 device_initcall(ubd_driver_init);
1262 static int ubd_open(struct block_device *bdev, fmode_t mode)
1264 struct gendisk *disk = bdev->bd_disk;
1265 struct ubd *ubd_dev = disk->private_data;
1268 mutex_lock(&ubd_mutex);
1269 if(ubd_dev->count == 0){
1270 err = ubd_open_dev(ubd_dev);
1272 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
1273 disk->disk_name, ubd_dev->file, -err);
1278 set_disk_ro(disk, !ubd_dev->openflags.w);
1280 /* This should no more be needed. And it didn't work anyway to exclude
1281 * read-write remounting of filesystems.*/
1282 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1283 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1287 mutex_unlock(&ubd_mutex);
1291 static void ubd_release(struct gendisk *disk, fmode_t mode)
1293 struct ubd *ubd_dev = disk->private_data;
1295 mutex_lock(&ubd_mutex);
1296 if(--ubd_dev->count == 0)
1297 ubd_close_dev(ubd_dev);
1298 mutex_unlock(&ubd_mutex);
1301 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
1302 __u64 *cow_offset, unsigned long *bitmap,
1303 __u64 bitmap_offset, unsigned long *bitmap_words,
1306 __u64 sector = io_offset >> SECTOR_SHIFT;
1307 int i, update_bitmap = 0;
1309 for (i = 0; i < length >> SECTOR_SHIFT; i++) {
1310 if(cow_mask != NULL)
1311 ubd_set_bit(i, (unsigned char *) cow_mask);
1312 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1316 ubd_set_bit(sector + i, (unsigned char *) bitmap);
1322 *cow_offset = sector / (sizeof(unsigned long) * 8);
1324 /* This takes care of the case where we're exactly at the end of the
1325 * device, and *cow_offset + 1 is off the end. So, just back it up
1326 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1327 * for the original diagnosis.
1329 if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
1330 sizeof(unsigned long)) - 1))
1333 bitmap_words[0] = bitmap[*cow_offset];
1334 bitmap_words[1] = bitmap[*cow_offset + 1];
1336 *cow_offset *= sizeof(unsigned long);
1337 *cow_offset += bitmap_offset;
1340 static void cowify_req(struct io_thread_req *req, struct io_desc *segment,
1341 unsigned long offset, unsigned long *bitmap,
1342 __u64 bitmap_offset, __u64 bitmap_len)
1344 __u64 sector = offset >> SECTOR_SHIFT;
1347 if (segment->length > (sizeof(segment->sector_mask) * 8) << SECTOR_SHIFT)
1348 panic("Operation too long");
1350 if (req_op(req->req) == REQ_OP_READ) {
1351 for (i = 0; i < segment->length >> SECTOR_SHIFT; i++) {
1352 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1353 ubd_set_bit(i, (unsigned char *)
1354 &segment->sector_mask);
1357 cowify_bitmap(offset, segment->length, &segment->sector_mask,
1358 &segment->cow_offset, bitmap, bitmap_offset,
1359 segment->bitmap_words, bitmap_len);
1363 static void ubd_map_req(struct ubd *dev, struct io_thread_req *io_req,
1364 struct request *req)
1366 struct bio_vec bvec;
1367 struct req_iterator iter;
1369 unsigned long byte_offset = io_req->offset;
1370 int op = req_op(req);
1372 if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD) {
1373 io_req->io_desc[0].buffer = NULL;
1374 io_req->io_desc[0].length = blk_rq_bytes(req);
1376 rq_for_each_segment(bvec, req, iter) {
1377 BUG_ON(i >= io_req->desc_cnt);
1379 io_req->io_desc[i].buffer =
1380 page_address(bvec.bv_page) + bvec.bv_offset;
1381 io_req->io_desc[i].length = bvec.bv_len;
1386 if (dev->cow.file) {
1387 for (i = 0; i < io_req->desc_cnt; i++) {
1388 cowify_req(io_req, &io_req->io_desc[i], byte_offset,
1389 dev->cow.bitmap, dev->cow.bitmap_offset,
1390 dev->cow.bitmap_len);
1391 byte_offset += io_req->io_desc[i].length;
1397 static struct io_thread_req *ubd_alloc_req(struct ubd *dev, struct request *req,
1400 struct io_thread_req *io_req;
1403 io_req = kmalloc(sizeof(*io_req) +
1404 (desc_cnt * sizeof(struct io_desc)),
1411 io_req->fds[0] = dev->cow.fd;
1413 io_req->fds[0] = dev->fd;
1415 io_req->sectorsize = SECTOR_SIZE;
1416 io_req->fds[1] = dev->fd;
1417 io_req->offset = (u64) blk_rq_pos(req) << SECTOR_SHIFT;
1418 io_req->offsets[0] = 0;
1419 io_req->offsets[1] = dev->cow.data_offset;
1421 for (i = 0 ; i < desc_cnt; i++) {
1422 io_req->io_desc[i].sector_mask = 0;
1423 io_req->io_desc[i].cow_offset = -1;
1429 static int ubd_submit_request(struct ubd *dev, struct request *req)
1432 struct io_thread_req *io_req;
1434 int op = req_op(req);
1436 if (op == REQ_OP_FLUSH)
1438 else if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD)
1441 segs = blk_rq_nr_phys_segments(req);
1443 io_req = ubd_alloc_req(dev, req, segs);
1447 io_req->desc_cnt = segs;
1449 ubd_map_req(dev, io_req, req);
1451 ret = os_write_file(thread_fd, &io_req, sizeof(io_req));
1452 if (ret != sizeof(io_req)) {
1454 pr_err("write to io thread failed: %d\n", -ret);
1460 static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
1461 const struct blk_mq_queue_data *bd)
1463 struct ubd *ubd_dev = hctx->queue->queuedata;
1464 struct request *req = bd->rq;
1465 int ret = 0, res = BLK_STS_OK;
1467 blk_mq_start_request(req);
1469 spin_lock_irq(&ubd_dev->lock);
1471 switch (req_op(req)) {
1475 case REQ_OP_DISCARD:
1476 case REQ_OP_WRITE_ZEROES:
1477 ret = ubd_submit_request(ubd_dev, req);
1481 res = BLK_STS_NOTSUPP;
1484 spin_unlock_irq(&ubd_dev->lock);
1488 res = BLK_STS_RESOURCE;
1490 res = BLK_STS_DEV_RESOURCE;
1496 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1498 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1502 geo->cylinders = ubd_dev->size / (128 * 32 * 512);
1506 static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
1507 unsigned int cmd, unsigned long arg)
1509 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1510 u16 ubd_id[ATA_ID_WORDS];
1513 struct cdrom_volctrl volume;
1514 case HDIO_GET_IDENTITY:
1515 memset(&ubd_id, 0, ATA_ID_WORDS * 2);
1516 ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512);
1517 ubd_id[ATA_ID_HEADS] = 128;
1518 ubd_id[ATA_ID_SECTORS] = 32;
1519 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1525 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1527 volume.channel0 = 255;
1528 volume.channel1 = 255;
1529 volume.channel2 = 255;
1530 volume.channel3 = 255;
1531 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1538 static int map_error(int error_code)
1540 switch (error_code) {
1545 return BLK_STS_NOTSUPP;
1547 return BLK_STS_NOSPC;
1549 return BLK_STS_IOERR;
1553 * Everything from here onwards *IS NOT PART OF THE KERNEL*
1555 * The following functions are part of UML hypervisor code.
1556 * All functions from here onwards are executed as a helper
1557 * thread and are not allowed to execute any kernel functions.
1559 * Any communication must occur strictly via shared memory and IPC.
1561 * Do not add printks, locks, kernel memory operations, etc - it
1562 * will result in unpredictable behaviour and/or crashes.
1565 static int update_bitmap(struct io_thread_req *req, struct io_desc *segment)
1569 if (segment->cow_offset == -1)
1570 return map_error(0);
1572 n = os_pwrite_file(req->fds[1], &segment->bitmap_words,
1573 sizeof(segment->bitmap_words), segment->cow_offset);
1574 if (n != sizeof(segment->bitmap_words))
1575 return map_error(-n);
1577 return map_error(0);
1580 static void do_io(struct io_thread_req *req, struct io_desc *desc)
1584 int n, nsectors, start, end, bit;
1587 /* FLUSH is really a special case, we cannot "case" it with others */
1589 if (req_op(req->req) == REQ_OP_FLUSH) {
1590 /* fds[0] is always either the rw image or our cow file */
1591 req->error = map_error(-os_sync_file(req->fds[0]));
1595 nsectors = desc->length / req->sectorsize;
1598 bit = ubd_test_bit(start, (unsigned char *) &desc->sector_mask);
1600 while((end < nsectors) &&
1601 (ubd_test_bit(end, (unsigned char *) &desc->sector_mask) == bit))
1604 off = req->offset + req->offsets[bit] +
1605 start * req->sectorsize;
1606 len = (end - start) * req->sectorsize;
1607 if (desc->buffer != NULL)
1608 buf = &desc->buffer[start * req->sectorsize];
1610 switch (req_op(req->req)) {
1616 n = os_pread_file(req->fds[bit], buf, len, off);
1618 req->error = map_error(-n);
1621 } while((n < len) && (n != 0));
1622 if (n < len) memset(&buf[n], 0, len - n);
1625 n = os_pwrite_file(req->fds[bit], buf, len, off);
1627 req->error = map_error(-n);
1631 case REQ_OP_DISCARD:
1632 case REQ_OP_WRITE_ZEROES:
1633 n = os_falloc_punch(req->fds[bit], off, len);
1635 req->error = map_error(-n);
1641 req->error = BLK_STS_NOTSUPP;
1646 } while(start < nsectors);
1649 req->error = update_bitmap(req, desc);
1652 /* Changed in start_io_thread, which is serialized by being called only
1653 * from ubd_init, which is an initcall.
1657 /* Only changed by the io thread. XXX: currently unused. */
1658 static int io_count = 0;
1660 int io_thread(void *arg)
1662 int n, count, written, res;
1664 os_fix_helper_signals();
1667 n = bulk_req_safe_read(
1681 for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
1682 struct io_thread_req *req = (*io_req_buffer)[count];
1686 for (i = 0; !req->error && i < req->desc_cnt; i++)
1687 do_io(req, &(req->io_desc[i]));
1694 res = os_write_file(kernel_fd,
1695 ((char *) io_req_buffer) + written,
1703 } while (written < n);