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
== '\0')
381 serial
= strsep(&str
, ",:");
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);
1245 io_pid
= start_io_thread(stack
+ PAGE_SIZE
- sizeof(void *),
1249 "ubd : Failed to start I/O thread (errno = %d) - "
1250 "falling back to synchronous I/O\n", -io_pid
);
1254 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
1255 0, "ubd", ubd_devs
);
1257 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
1261 device_initcall(ubd_driver_init
);
1263 static int ubd_open(struct block_device
*bdev
, fmode_t mode
)
1265 struct gendisk
*disk
= bdev
->bd_disk
;
1266 struct ubd
*ubd_dev
= disk
->private_data
;
1269 mutex_lock(&ubd_mutex
);
1270 if(ubd_dev
->count
== 0){
1271 err
= ubd_open_dev(ubd_dev
);
1273 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
1274 disk
->disk_name
, ubd_dev
->file
, -err
);
1279 set_disk_ro(disk
, !ubd_dev
->openflags
.w
);
1281 /* This should no more be needed. And it didn't work anyway to exclude
1282 * read-write remounting of filesystems.*/
1283 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1284 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1288 mutex_unlock(&ubd_mutex
);
1292 static void ubd_release(struct gendisk
*disk
, fmode_t mode
)
1294 struct ubd
*ubd_dev
= disk
->private_data
;
1296 mutex_lock(&ubd_mutex
);
1297 if(--ubd_dev
->count
== 0)
1298 ubd_close_dev(ubd_dev
);
1299 mutex_unlock(&ubd_mutex
);
1302 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
1303 __u64
*cow_offset
, unsigned long *bitmap
,
1304 __u64 bitmap_offset
, unsigned long *bitmap_words
,
1307 __u64 sector
= io_offset
>> SECTOR_SHIFT
;
1308 int i
, update_bitmap
= 0;
1310 for (i
= 0; i
< length
>> SECTOR_SHIFT
; i
++) {
1311 if(cow_mask
!= NULL
)
1312 ubd_set_bit(i
, (unsigned char *) cow_mask
);
1313 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1317 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
1323 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
1325 /* This takes care of the case where we're exactly at the end of the
1326 * device, and *cow_offset + 1 is off the end. So, just back it up
1327 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1328 * for the original diagnosis.
1330 if (*cow_offset
== (DIV_ROUND_UP(bitmap_len
,
1331 sizeof(unsigned long)) - 1))
1334 bitmap_words
[0] = bitmap
[*cow_offset
];
1335 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
1337 *cow_offset
*= sizeof(unsigned long);
1338 *cow_offset
+= bitmap_offset
;
1341 static void cowify_req(struct io_thread_req
*req
, struct io_desc
*segment
,
1342 unsigned long offset
, unsigned long *bitmap
,
1343 __u64 bitmap_offset
, __u64 bitmap_len
)
1345 __u64 sector
= offset
>> SECTOR_SHIFT
;
1348 if (segment
->length
> (sizeof(segment
->sector_mask
) * 8) << SECTOR_SHIFT
)
1349 panic("Operation too long");
1351 if (req_op(req
->req
) == REQ_OP_READ
) {
1352 for (i
= 0; i
< segment
->length
>> SECTOR_SHIFT
; i
++) {
1353 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
1354 ubd_set_bit(i
, (unsigned char *)
1355 &segment
->sector_mask
);
1358 cowify_bitmap(offset
, segment
->length
, &segment
->sector_mask
,
1359 &segment
->cow_offset
, bitmap
, bitmap_offset
,
1360 segment
->bitmap_words
, bitmap_len
);
1364 static void ubd_map_req(struct ubd
*dev
, struct io_thread_req
*io_req
,
1365 struct request
*req
)
1367 struct bio_vec bvec
;
1368 struct req_iterator iter
;
1370 unsigned long byte_offset
= io_req
->offset
;
1371 int op
= req_op(req
);
1373 if (op
== REQ_OP_WRITE_ZEROES
|| op
== REQ_OP_DISCARD
) {
1374 io_req
->io_desc
[0].buffer
= NULL
;
1375 io_req
->io_desc
[0].length
= blk_rq_bytes(req
);
1377 rq_for_each_segment(bvec
, req
, iter
) {
1378 BUG_ON(i
>= io_req
->desc_cnt
);
1380 io_req
->io_desc
[i
].buffer
=
1381 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
1382 io_req
->io_desc
[i
].length
= bvec
.bv_len
;
1387 if (dev
->cow
.file
) {
1388 for (i
= 0; i
< io_req
->desc_cnt
; i
++) {
1389 cowify_req(io_req
, &io_req
->io_desc
[i
], byte_offset
,
1390 dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1391 dev
->cow
.bitmap_len
);
1392 byte_offset
+= io_req
->io_desc
[i
].length
;
1398 static struct io_thread_req
*ubd_alloc_req(struct ubd
*dev
, struct request
*req
,
1401 struct io_thread_req
*io_req
;
1404 io_req
= kmalloc(sizeof(*io_req
) +
1405 (desc_cnt
* sizeof(struct io_desc
)),
1412 io_req
->fds
[0] = dev
->cow
.fd
;
1414 io_req
->fds
[0] = dev
->fd
;
1416 io_req
->sectorsize
= SECTOR_SIZE
;
1417 io_req
->fds
[1] = dev
->fd
;
1418 io_req
->offset
= (u64
) blk_rq_pos(req
) << SECTOR_SHIFT
;
1419 io_req
->offsets
[0] = 0;
1420 io_req
->offsets
[1] = dev
->cow
.data_offset
;
1422 for (i
= 0 ; i
< desc_cnt
; i
++) {
1423 io_req
->io_desc
[i
].sector_mask
= 0;
1424 io_req
->io_desc
[i
].cow_offset
= -1;
1430 static int ubd_submit_request(struct ubd
*dev
, struct request
*req
)
1433 struct io_thread_req
*io_req
;
1435 int op
= req_op(req
);
1437 if (op
== REQ_OP_FLUSH
)
1439 else if (op
== REQ_OP_WRITE_ZEROES
|| op
== REQ_OP_DISCARD
)
1442 segs
= blk_rq_nr_phys_segments(req
);
1444 io_req
= ubd_alloc_req(dev
, req
, segs
);
1448 io_req
->desc_cnt
= segs
;
1450 ubd_map_req(dev
, io_req
, req
);
1452 ret
= os_write_file(thread_fd
, &io_req
, sizeof(io_req
));
1453 if (ret
!= sizeof(io_req
)) {
1455 pr_err("write to io thread failed: %d\n", -ret
);
1461 static blk_status_t
ubd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1462 const struct blk_mq_queue_data
*bd
)
1464 struct ubd
*ubd_dev
= hctx
->queue
->queuedata
;
1465 struct request
*req
= bd
->rq
;
1466 int ret
= 0, res
= BLK_STS_OK
;
1468 blk_mq_start_request(req
);
1470 spin_lock_irq(&ubd_dev
->lock
);
1472 switch (req_op(req
)) {
1476 case REQ_OP_DISCARD
:
1477 case REQ_OP_WRITE_ZEROES
:
1478 ret
= ubd_submit_request(ubd_dev
, req
);
1482 res
= BLK_STS_NOTSUPP
;
1485 spin_unlock_irq(&ubd_dev
->lock
);
1489 res
= BLK_STS_RESOURCE
;
1491 res
= BLK_STS_DEV_RESOURCE
;
1497 static int ubd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1499 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1503 geo
->cylinders
= ubd_dev
->size
/ (128 * 32 * 512);
1507 static int ubd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1508 unsigned int cmd
, unsigned long arg
)
1510 struct ubd
*ubd_dev
= bdev
->bd_disk
->private_data
;
1511 u16 ubd_id
[ATA_ID_WORDS
];
1514 struct cdrom_volctrl volume
;
1515 case HDIO_GET_IDENTITY
:
1516 memset(&ubd_id
, 0, ATA_ID_WORDS
* 2);
1517 ubd_id
[ATA_ID_CYLS
] = ubd_dev
->size
/ (128 * 32 * 512);
1518 ubd_id
[ATA_ID_HEADS
] = 128;
1519 ubd_id
[ATA_ID_SECTORS
] = 32;
1520 if(copy_to_user((char __user
*) arg
, (char *) &ubd_id
,
1526 if(copy_from_user(&volume
, (char __user
*) arg
, sizeof(volume
)))
1528 volume
.channel0
= 255;
1529 volume
.channel1
= 255;
1530 volume
.channel2
= 255;
1531 volume
.channel3
= 255;
1532 if(copy_to_user((char __user
*) arg
, &volume
, sizeof(volume
)))
1539 static int map_error(int error_code
)
1541 switch (error_code
) {
1546 return BLK_STS_NOTSUPP
;
1548 return BLK_STS_NOSPC
;
1550 return BLK_STS_IOERR
;
1554 * Everything from here onwards *IS NOT PART OF THE KERNEL*
1556 * The following functions are part of UML hypervisor code.
1557 * All functions from here onwards are executed as a helper
1558 * thread and are not allowed to execute any kernel functions.
1560 * Any communication must occur strictly via shared memory and IPC.
1562 * Do not add printks, locks, kernel memory operations, etc - it
1563 * will result in unpredictable behaviour and/or crashes.
1566 static int update_bitmap(struct io_thread_req
*req
, struct io_desc
*segment
)
1570 if (segment
->cow_offset
== -1)
1571 return map_error(0);
1573 n
= os_pwrite_file(req
->fds
[1], &segment
->bitmap_words
,
1574 sizeof(segment
->bitmap_words
), segment
->cow_offset
);
1575 if (n
!= sizeof(segment
->bitmap_words
))
1576 return map_error(-n
);
1578 return map_error(0);
1581 static void do_io(struct io_thread_req
*req
, struct io_desc
*desc
)
1585 int n
, nsectors
, start
, end
, bit
;
1588 /* FLUSH is really a special case, we cannot "case" it with others */
1590 if (req_op(req
->req
) == REQ_OP_FLUSH
) {
1591 /* fds[0] is always either the rw image or our cow file */
1592 req
->error
= map_error(-os_sync_file(req
->fds
[0]));
1596 nsectors
= desc
->length
/ req
->sectorsize
;
1599 bit
= ubd_test_bit(start
, (unsigned char *) &desc
->sector_mask
);
1601 while((end
< nsectors
) &&
1602 (ubd_test_bit(end
, (unsigned char *) &desc
->sector_mask
) == bit
))
1605 off
= req
->offset
+ req
->offsets
[bit
] +
1606 start
* req
->sectorsize
;
1607 len
= (end
- start
) * req
->sectorsize
;
1608 if (desc
->buffer
!= NULL
)
1609 buf
= &desc
->buffer
[start
* req
->sectorsize
];
1611 switch (req_op(req
->req
)) {
1617 n
= os_pread_file(req
->fds
[bit
], buf
, len
, off
);
1619 req
->error
= map_error(-n
);
1622 } while((n
< len
) && (n
!= 0));
1623 if (n
< len
) memset(&buf
[n
], 0, len
- n
);
1626 n
= os_pwrite_file(req
->fds
[bit
], buf
, len
, off
);
1628 req
->error
= map_error(-n
);
1632 case REQ_OP_DISCARD
:
1633 case REQ_OP_WRITE_ZEROES
:
1634 n
= os_falloc_punch(req
->fds
[bit
], off
, len
);
1636 req
->error
= map_error(-n
);
1642 req
->error
= BLK_STS_NOTSUPP
;
1647 } while(start
< nsectors
);
1650 req
->error
= update_bitmap(req
, desc
);
1653 /* Changed in start_io_thread, which is serialized by being called only
1654 * from ubd_init, which is an initcall.
1658 /* Only changed by the io thread. XXX: currently unused. */
1659 static int io_count
= 0;
1661 int io_thread(void *arg
)
1663 int n
, count
, written
, res
;
1665 os_fix_helper_signals();
1668 n
= bulk_req_safe_read(
1682 for (count
= 0; count
< n
/sizeof(struct io_thread_req
*); count
++) {
1683 struct io_thread_req
*req
= (*io_req_buffer
)[count
];
1687 for (i
= 0; !req
->error
&& i
< req
->desc_cnt
; i
++)
1688 do_io(req
, &(req
->io_desc
[i
]));
1695 res
= os_write_file(kernel_fd
,
1696 ((char *) io_req_buffer
) + written
,
1704 } while (written
< n
);