VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   33 Branches   55 Tags
dbddf429dc514 (Alex Dewar                     2019-08-25 10:49:16 +0100    1) // SPDX-License-Identifier: GPL-2.0
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800    2) /*
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000    3)  * Copyright (C) 2018 Cambridge Greys Ltd
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000    4)  * Copyright (C) 2015-2016 Anton Ivanov (aivanov@brocade.com)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    5)  * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    6)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    7) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    8) /* 2001-09-28...2002-04-17
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    9)  * Partition stuff by James_McMechan@hotmail.com
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   10)  * old style ubd by setting UBD_SHIFT to 0
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   11)  * 2002-09-27...2002-10-18 massive tinkering for 2.5
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   12)  * partitions have changed in 2.5
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   13)  * 2003-01-29 more tinkering for 2.5.59-1
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   14)  * This should now address the sysfs problems and has
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   15)  * the symlink for devfs to allow for booting with
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   16)  * the common /dev/ubd/discX/... names rather than
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   17)  * only /dev/ubdN/discN this version also has lots of
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   18)  * clean ups preparing for ubd-many.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   19)  * James McMechan
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   20)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   21) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   22) #define UBD_SHIFT 4
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   23) 
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   24) #include <linux/module.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   25) #include <linux/init.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   26) #include <linux/blkdev.h>
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100   27) #include <linux/blk-mq.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   28) #include <linux/ata.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   29) #include <linux/hdreg.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   30) #include <linux/cdrom.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   31) #include <linux/proc_fs.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   32) #include <linux/seq_file.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   33) #include <linux/ctype.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   34) #include <linux/slab.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   35) #include <linux/vmalloc.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   36) #include <linux/platform_device.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   37) #include <linux/scatterlist.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   38) #include <asm/tlbflush.h>
37185b3324087 (Al Viro                        2012-10-08 03:27:32 +0100   39) #include <kern_util.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   40) #include "mconsole_kern.h"
37185b3324087 (Al Viro                        2012-10-08 03:27:32 +0100   41) #include <init.h>
37185b3324087 (Al Viro                        2012-10-08 03:27:32 +0100   42) #include <irq_kern.h>
8ea3c06a2e4d9 (Al Viro                        2011-08-18 18:04:41 -0400   43) #include "ubd.h"
37185b3324087 (Al Viro                        2012-10-08 03:27:32 +0100   44) #include <os.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   45) #include "cow.h"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   46) 
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000   47) /* Max request size is determined by sector mask - 32K */
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000   48) #define UBD_MAX_REQUEST (8 * sizeof(long))
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   49) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   50) struct io_desc {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   51) 	char *buffer;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   52) 	unsigned long length;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   53) 	unsigned long sector_mask;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   54) 	unsigned long long cow_offset;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   55) 	unsigned long bitmap_words[2];
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   56) };
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   57) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   58) struct io_thread_req {
62f96cb01e8de (Jeff Dike                      2007-02-10 01:44:16 -0800   59) 	struct request *req;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   60) 	int fds[2];
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   61) 	unsigned long offsets[2];
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   62) 	unsigned long long offset;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   63) 	int sectorsize;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   64) 	int error;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   65) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   66) 	int desc_cnt;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   67) 	/* io_desc has to be the last element of the struct */
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500   68) 	struct io_desc io_desc[];
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   69) };
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   70) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   71) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   72) static struct io_thread_req * (*irq_req_buffer)[];
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   73) static struct io_thread_req *irq_remainder;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   74) static int irq_remainder_size;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   75) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   76) static struct io_thread_req * (*io_req_buffer)[];
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   77) static struct io_thread_req *io_remainder;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   78) static int io_remainder_size;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   79) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   80) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000   81) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400   82) static inline int ubd_test_bit(__u64 bit, unsigned char *data)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   83) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   84) 	__u64 n;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   85) 	int bits, off;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   86) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400   87) 	bits = sizeof(data[0]) * 8;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   88) 	n = bit / bits;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   89) 	off = bit % bits;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700   90) 	return (data[n] & (1 << off)) != 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   91) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   92) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400   93) static inline void ubd_set_bit(__u64 bit, unsigned char *data)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   94) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   95) 	__u64 n;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   96) 	int bits, off;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   97) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400   98) 	bits = sizeof(data[0]) * 8;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   99) 	n = bit / bits;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  100) 	off = bit % bits;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  101) 	data[n] |= (1 << off);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  102) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  103) /*End stuff from ubd_user.h*/
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  104) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  105) #define DRIVER_NAME "uml-blkdev"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  106) 
d7fb2c3865ca0 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:07 -0800  107) static DEFINE_MUTEX(ubd_lock);
9a181c5861713 (Arnd Bergmann                  2010-09-11 18:38:03 +0200  108) static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  109) 
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500  110) static int ubd_open(struct block_device *bdev, fmode_t mode);
db2a144bedd58 (Al Viro                        2013-05-05 21:52:57 -0400  111) static void ubd_release(struct gendisk *disk, fmode_t mode);
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500  112) static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  113) 		     unsigned int cmd, unsigned long arg);
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800  114) static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  115) 
97d88ac83540f (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:03 -0800  116) #define MAX_DEV (16)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  117) 
83d5cde47dedf (Alexey Dobriyan                2009-09-21 17:01:13 -0700  118) static const struct block_device_operations ubd_blops = {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  119)         .owner		= THIS_MODULE,
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500  120)         .open		= ubd_open,
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500  121)         .release	= ubd_release,
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500  122)         .ioctl		= ubd_ioctl,
ab0cf1e425eaa (Arnd Bergmann                  2019-11-30 20:09:07 +0100  123)         .compat_ioctl	= blkdev_compat_ptr_ioctl,
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800  124) 	.getgeo		= ubd_getgeo,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  125) };
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  126) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  127) /* Protected by ubd_lock */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  128) static struct gendisk *ubd_gendisk[MAX_DEV];
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800  129) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  130) #ifdef CONFIG_BLK_DEV_UBD_SYNC
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  131) #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  132) 					 .cl = 1 })
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  133) #else
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  134) #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  135) 					 .cl = 1 })
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  136) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  137) static struct openflags global_openflags = OPEN_FLAGS;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  138) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  139) struct cow {
2a9d32f682b2b (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:04 -0800  140) 	/* backing file name */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  141) 	char *file;
2a9d32f682b2b (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:04 -0800  142) 	/* backing file fd */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  143) 	int fd;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  144) 	unsigned long *bitmap;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  145) 	unsigned long bitmap_len;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  146) 	int bitmap_offset;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  147) 	int data_offset;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  148) };
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  149) 
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  150) #define MAX_SG 64
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  151) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  152) struct ubd {
2a9d32f682b2b (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:04 -0800  153) 	/* name (and fd, below) of the file opened for writing, either the
2a9d32f682b2b (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:04 -0800  154) 	 * backing or the cow file. */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  155) 	char *file;
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  156) 	char *serial;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  157) 	int count;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  158) 	int fd;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  159) 	__u64 size;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  160) 	struct openflags boot_openflags;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  161) 	struct openflags openflags;
84e945e399ce9 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:10 -0800  162) 	unsigned shared:1;
84e945e399ce9 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:10 -0800  163) 	unsigned no_cow:1;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  164) 	unsigned no_trim:1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  165) 	struct cow cow;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  166) 	struct platform_device pdev;
62f96cb01e8de (Jeff Dike                      2007-02-10 01:44:16 -0800  167) 	struct request_queue *queue;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  168) 	struct blk_mq_tag_set tag_set;
62f96cb01e8de (Jeff Dike                      2007-02-10 01:44:16 -0800  169) 	spinlock_t lock;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  170) };
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  171) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  172) #define DEFAULT_COW { \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  173) 	.file =			NULL, \
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  174) 	.fd =			-1,	\
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  175) 	.bitmap =		NULL, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  176) 	.bitmap_offset =	0, \
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  177) 	.data_offset =		0, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  178) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  179) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  180) #define DEFAULT_UBD { \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  181) 	.file = 		NULL, \
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  182) 	.serial =		NULL, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  183) 	.count =		0, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  184) 	.fd =			-1, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  185) 	.size =			-1, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  186) 	.boot_openflags =	OPEN_FLAGS, \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  187) 	.openflags =		OPEN_FLAGS, \
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  188) 	.no_cow =               0, \
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  189) 	.no_trim =		0, \
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800  190) 	.shared =		0, \
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  191) 	.cow =			DEFAULT_COW, \
22e6500458990 (Thomas Gleixner                2011-01-23 15:21:25 +0100  192) 	.lock =			__SPIN_LOCK_UNLOCKED(ubd_devs.lock), \
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  193) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  194) 
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800  195) /* Protected by ubd_lock */
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  196) static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD };
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  197) 
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  198) static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  199) 				 const struct blk_mq_queue_data *bd);
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  200) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  201) static int fake_ide_setup(char *str)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  202) {
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  203) 	pr_warn("The fake_ide option has been removed\n");
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  204) 	return 1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  205) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  206) __setup("fake_ide", fake_ide_setup);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  207) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  208) __uml_help(fake_ide_setup,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  209) "fake_ide\n"
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  210) "    Obsolete stub.\n\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  211) );
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  212) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  213) static int parse_unit(char **ptr)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  214) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  215) 	char *str = *ptr, *end;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  216) 	int n = -1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  217) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  218) 	if(isdigit(*str)) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  219) 		n = simple_strtoul(str, &end, 0);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  220) 		if(end == str)
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  221) 			return -1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  222) 		*ptr = end;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  223) 	}
97d88ac83540f (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:03 -0800  224) 	else if (('a' <= *str) && (*str <= 'z')) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  225) 		n = *str - 'a';
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  226) 		str++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  227) 		*ptr = str;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  228) 	}
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  229) 	return n;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  230) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  231) 
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800  232) /* If *index_out == -1 at exit, the passed option was a general one;
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800  233)  * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800  234)  * should not be freed on exit.
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800  235)  */
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  236) static int ubd_setup_common(char *str, int *index_out, char **error_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  237) {
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  238) 	struct ubd *ubd_dev;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  239) 	struct openflags flags = global_openflags;
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  240) 	char *file, *backing_file, *serial;
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800  241) 	int n, err = 0, i;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  242) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  243) 	if(index_out) *index_out = -1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  244) 	n = *str;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  245) 	if(n == '='){
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  246) 		str++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  247) 		if(!strcmp(str, "sync")){
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  248) 			global_openflags = of_sync(global_openflags);
9ca55299f2ee0 (Daniel Walter                  2019-04-02 10:43:32 +0200  249) 			return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  250) 		}
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800  251) 
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  252) 		pr_warn("fake major not supported any more\n");
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  253) 		return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  254) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  255) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  256) 	n = parse_unit(&str);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  257) 	if(n < 0){
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  258) 		*error_out = "Couldn't parse device number";
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  259) 		return -EINVAL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  260) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  261) 	if(n >= MAX_DEV){
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  262) 		*error_out = "Device number out of range";
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  263) 		return 1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  264) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  265) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  266) 	err = -EBUSY;
d7fb2c3865ca0 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:07 -0800  267) 	mutex_lock(&ubd_lock);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  268) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  269) 	ubd_dev = &ubd_devs[n];
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  270) 	if(ubd_dev->file != NULL){
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  271) 		*error_out = "Device is already configured";
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  272) 		goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  273) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  274) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  275) 	if (index_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  276) 		*index_out = n;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  277) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  278) 	err = -EINVAL;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  279) 	for (i = 0; i < sizeof("rscdt="); i++) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  280) 		switch (*str) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  281) 		case 'r':
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  282) 			flags.w = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  283) 			break;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  284) 		case 's':
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  285) 			flags.s = 1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  286) 			break;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  287) 		case 'd':
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  288) 			ubd_dev->no_cow = 1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  289) 			break;
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800  290) 		case 'c':
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  291) 			ubd_dev->shared = 1;
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800  292) 			break;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  293) 		case 't':
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  294) 			ubd_dev->no_trim = 1;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  295) 			break;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  296) 		case '=':
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  297) 			str++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  298) 			goto break_loop;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  299) 		default:
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  300) 			*error_out = "Expected '=' or flag letter "
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  301) 				"(r, s, c, t or d)";
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  302) 			goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  303) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  304) 		str++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  305) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  306) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  307) 	if (*str == '=')
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  308) 		*error_out = "Too many flags specified";
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  309) 	else
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  310) 		*error_out = "Missing '='";
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  311) 	goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  312) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  313) break_loop:
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  314) 	file = strsep(&str, ",:");
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  315) 	if (*file == '\0')
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  316) 		file = NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  317) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  318) 	backing_file = strsep(&str, ",:");
94c41b3a7c370 (Hajime Tazaki                  2020-12-21 11:24:34 +0900  319) 	if (backing_file && *backing_file == '\0')
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  320) 		backing_file = NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  321) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  322) 	serial = strsep(&str, ",:");
94c41b3a7c370 (Hajime Tazaki                  2020-12-21 11:24:34 +0900  323) 	if (serial && *serial == '\0')
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  324) 		serial = NULL;
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  325) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  326) 	if (backing_file && ubd_dev->no_cow) {
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  327) 		*error_out = "Can't specify both 'd' and a cow file";
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  328) 		goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  329) 	}
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  330) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  331) 	err = 0;
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  332) 	ubd_dev->file = file;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  333) 	ubd_dev->cow.file = backing_file;
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  334) 	ubd_dev->serial = serial;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  335) 	ubd_dev->boot_openflags = flags;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  336) out:
d7fb2c3865ca0 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:07 -0800  337) 	mutex_unlock(&ubd_lock);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  338) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  339) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  340) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  341) static int ubd_setup(char *str)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  342) {
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  343) 	char *error;
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  344) 	int err;
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  345) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  346) 	err = ubd_setup_common(str, NULL, &error);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  347) 	if(err)
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  348) 		printk(KERN_ERR "Failed to initialize device with \"%s\" : "
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  349) 		       "%s\n", str, error);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  350) 	return 1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  351) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  352) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  353) __setup("ubd", ubd_setup);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  354) __uml_help(ubd_setup,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  355) "ubd<n><flags>=<filename>[(:|,)<filename2>][(:|,)<serial>]\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  356) "    This is used to associate a device with a file in the underlying\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  357) "    filesystem. When specifying two filenames, the first one is the\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  358) "    COW name and the second is the backing file name. As separator you can\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  359) "    use either a ':' or a ',': the first one allows writing things like;\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  360) "	ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  361) "    while with a ',' the shell would not expand the 2nd '~'.\n"
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  362) "    When using only one filename, UML will detect whether to treat it like\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  363) "    a COW file or a backing file. To override this detection, add the 'd'\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  364) "    flag:\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  365) "	ubd0d=BackingFile\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  366) "    Usually, there is a filesystem in the file, but \n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  367) "    that's not required. Swap devices containing swap files can be\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  368) "    specified like this. Also, a file which doesn't contain a\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  369) "    filesystem can have its contents read in the virtual \n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  370) "    machine by running 'dd' on the device. <n> must be in the range\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  371) "    0 to 7. Appending an 'r' to the number will cause that device\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  372) "    to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
20ede453af796 (Jeff Dike                      2008-02-04 22:30:37 -0800  373) "    an 's' will cause data to be written to disk on the host immediately.\n"
20ede453af796 (Jeff Dike                      2008-02-04 22:30:37 -0800  374) "    'c' will cause the device to be treated as being shared between multiple\n"
20ede453af796 (Jeff Dike                      2008-02-04 22:30:37 -0800  375) "    UMLs and file locking will be turned off - this is appropriate for a\n"
20ede453af796 (Jeff Dike                      2008-02-04 22:30:37 -0800  376) "    cluster filesystem and inappropriate at almost all other times.\n\n"
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  377) "    't' will disable trim/discard support on the device (enabled by default).\n\n"
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  378) "    An optional device serial number can be exposed using the serial parameter\n"
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  379) "    on the cmdline which is exposed as a sysfs entry. This is particularly\n"
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  380) "    useful when a unique number should be given to the device. Note when\n"
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  381) "    specifying a label, the filename2 must be also presented. It can be\n"
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  382) "    an empty string, in which case the backing file is not used:\n"
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  383) "       ubd0=File,,Serial\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  384) );
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  385) 
8299ca5ce10c9 (Jeff Dike                      2008-02-04 22:30:48 -0800  386) static int udb_setup(char *str)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  387) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  388) 	printk("udb%s specified on command line is almost certainly a ubd -> "
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  389) 	       "udb TYPO\n", str);
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  390) 	return 1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  391) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  392) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  393) __setup("udb", udb_setup);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  394) __uml_help(udb_setup,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  395) "udb\n"
0894e27e7999b (Jeff Dike                      2005-05-28 15:51:55 -0700  396) "    This option is here solely to catch ubd -> udb typos, which can be\n"
0894e27e7999b (Jeff Dike                      2005-05-28 15:51:55 -0700  397) "    to impossible to catch visually unless you specifically look for\n"
0894e27e7999b (Jeff Dike                      2005-05-28 15:51:55 -0700  398) "    them.  The only result of any option starting with 'udb' is an error\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  399) "    in the boot output.\n\n"
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  400) );
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  401) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  402) /* Only changed by ubd_init, which is an initcall. */
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  403) static int thread_fd = -1;
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  404) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  405) /* Function to read several request pointers at a time
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  406) * handling fractional reads if (and as) needed
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  407) */
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  408) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  409) static int bulk_req_safe_read(
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  410) 	int fd,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  411) 	struct io_thread_req * (*request_buffer)[],
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  412) 	struct io_thread_req **remainder,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  413) 	int *remainder_size,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  414) 	int max_recs
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  415) 	)
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  416) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  417) 	int n = 0;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  418) 	int res = 0;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  419) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  420) 	if (*remainder_size > 0) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  421) 		memmove(
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  422) 			(char *) request_buffer,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  423) 			(char *) remainder, *remainder_size
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  424) 		);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  425) 		n = *remainder_size;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  426) 	}
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  427) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  428) 	res = os_read_file(
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  429) 			fd,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  430) 			((char *) request_buffer) + *remainder_size,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  431) 			sizeof(struct io_thread_req *)*max_recs
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  432) 				- *remainder_size
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  433) 		);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  434) 	if (res > 0) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  435) 		n += res;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  436) 		if ((n % sizeof(struct io_thread_req *)) > 0) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  437) 			/*
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  438) 			* Read somehow returned not a multiple of dword
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  439) 			* theoretically possible, but never observed in the
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  440) 			* wild, so read routine must be able to handle it
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  441) 			*/
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  442) 			*remainder_size = n % sizeof(struct io_thread_req *);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  443) 			WARN(*remainder_size > 0, "UBD IPC read returned a partial result");
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  444) 			memmove(
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  445) 				remainder,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  446) 				((char *) request_buffer) +
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  447) 					(n/sizeof(struct io_thread_req *))*sizeof(struct io_thread_req *),
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  448) 				*remainder_size
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  449) 			);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  450) 			n = n - *remainder_size;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  451) 		}
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  452) 	} else {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  453) 		n = res;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  454) 	}
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  455) 	return n;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  456) }
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  457) 
62f96cb01e8de (Jeff Dike                      2007-02-10 01:44:16 -0800  458) /* Called without dev->lock held, and only in interrupt context. */
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  459) static void ubd_handler(void)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  460) {
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  461) 	int n;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  462) 	int count;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  463) 
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  464) 	while(1){
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  465) 		n = bulk_req_safe_read(
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  466) 			thread_fd,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  467) 			irq_req_buffer,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  468) 			&irq_remainder,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  469) 			&irq_remainder_size,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  470) 			UBD_REQ_BUFFER_SIZE
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  471) 		);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  472) 		if (n < 0) {
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  473) 			if(n == -EAGAIN)
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  474) 				break;
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  475) 			printk(KERN_ERR "spurious interrupt in ubd_handler, "
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  476) 			       "err = %d\n", -n);
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  477) 			return;
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  478) 		}
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  479) 		for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  480) 			struct io_thread_req *io_req = (*irq_req_buffer)[count];
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  481) 
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  482) 			if ((io_req->error == BLK_STS_NOTSUPP) && (req_op(io_req->req) == REQ_OP_DISCARD)) {
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  483) 				blk_queue_max_discard_sectors(io_req->req->q, 0);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  484) 				blk_queue_max_write_zeroes_sectors(io_req->req->q, 0);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  485) 				blk_queue_flag_clear(QUEUE_FLAG_DISCARD, io_req->req->q);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  486) 			}
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500  487) 			blk_mq_end_request(io_req->req, io_req->error);
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  488) 			kfree(io_req);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000  489) 		}
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  490) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  491) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  492) 
7bea96fd22a8f (Al Viro                        2006-10-08 22:49:34 +0100  493) static irqreturn_t ubd_intr(int irq, void *dev)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  494) {
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  495) 	ubd_handler();
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  496) 	return IRQ_HANDLED;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  497) }
09ace81c1d737 (Jeff Dike                      2005-09-03 15:57:46 -0700  498) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  499) /* Only changed by ubd_init, which is an initcall. */
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  500) static int io_pid = -1;
09ace81c1d737 (Jeff Dike                      2005-09-03 15:57:46 -0700  501) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  502) static void kill_io_thread(void)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  503) {
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800  504) 	if(io_pid != -1)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  505) 		os_kill_process(io_pid, 1);
09ace81c1d737 (Jeff Dike                      2005-09-03 15:57:46 -0700  506) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  507) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  508) __uml_exitcall(kill_io_thread);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400  509) 
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800  510) static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  511) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  512) 	char *file;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  513) 	int fd;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  514) 	int err;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  515) 
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  516) 	__u32 version;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  517) 	__u32 align;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  518) 	char *backing_file;
853bc0ab341b0 (Arnd Bergmann                  2019-11-05 09:39:51 +0100  519) 	time64_t mtime;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  520) 	unsigned long long size;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  521) 	int sector_size;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  522) 	int bitmap_offset;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  523) 
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  524) 	if (ubd_dev->file && ubd_dev->cow.file) {
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  525) 		file = ubd_dev->cow.file;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  526) 
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  527) 		goto out;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  528) 	}
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  529) 
d4afcba95fca4 (Martin Pärtel                  2012-08-02 00:44:22 +0200  530) 	fd = os_open_file(ubd_dev->file, of_read(OPENFLAGS()), 0);
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  531) 	if (fd < 0)
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  532) 		return fd;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  533) 
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  534) 	err = read_cow_header(file_reader, &fd, &version, &backing_file, \
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  535) 		&mtime, &size, &sector_size, &align, &bitmap_offset);
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  536) 	os_close_file(fd);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  537) 
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  538) 	if(err == -EINVAL)
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  539) 		file = ubd_dev->file;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  540) 	else
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  541) 		file = backing_file;
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  542) 
8535639810e57 (Richard Weinberger             2011-11-02 13:17:27 +0100  543) out:
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  544) 	return os_file_size(file, size_out);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  545) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  546) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  547) static int read_cow_bitmap(int fd, void *buf, int offset, int len)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  548) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  549) 	int err;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  550) 
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000  551) 	err = os_pread_file(fd, buf, len, offset);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  552) 	if (err < 0)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  553) 		return err;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  554) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  555) 	return 0;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  556) }
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  557) 
853bc0ab341b0 (Arnd Bergmann                  2019-11-05 09:39:51 +0100  558) static int backing_file_mismatch(char *file, __u64 size, time64_t mtime)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  559) {
853bc0ab341b0 (Arnd Bergmann                  2019-11-05 09:39:51 +0100  560) 	time64_t modtime;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  561) 	unsigned long long actual;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  562) 	int err;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  563) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  564) 	err = os_file_modtime(file, &modtime);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  565) 	if (err < 0) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  566) 		printk(KERN_ERR "Failed to get modification time of backing "
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  567) 		       "file \"%s\", err = %d\n", file, -err);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  568) 		return err;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  569) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  570) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  571) 	err = os_file_size(file, &actual);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  572) 	if (err < 0) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  573) 		printk(KERN_ERR "Failed to get size of backing file \"%s\", "
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  574) 		       "err = %d\n", file, -err);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  575) 		return err;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  576) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  577) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  578) 	if (actual != size) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  579) 		/*__u64 can be a long on AMD64 and with %lu GCC complains; so
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  580) 		 * the typecast.*/
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  581) 		printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  582) 		       "vs backing file\n", (unsigned long long) size, actual);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  583) 		return -EINVAL;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  584) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  585) 	if (modtime != mtime) {
853bc0ab341b0 (Arnd Bergmann                  2019-11-05 09:39:51 +0100  586) 		printk(KERN_ERR "mtime mismatch (%lld vs %lld) of COW header vs "
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  587) 		       "backing file\n", mtime, modtime);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  588) 		return -EINVAL;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  589) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  590) 	return 0;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  591) }
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  592) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  593) static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  594) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  595) 	struct uml_stat buf1, buf2;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  596) 	int err;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  597) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  598) 	if (from_cmdline == NULL)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  599) 		return 0;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  600) 	if (!strcmp(from_cmdline, from_cow))
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  601) 		return 0;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  602) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  603) 	err = os_stat_file(from_cmdline, &buf1);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  604) 	if (err < 0) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  605) 		printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  606) 		       -err);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  607) 		return 0;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  608) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  609) 	err = os_stat_file(from_cow, &buf2);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  610) 	if (err < 0) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  611) 		printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  612) 		       -err);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  613) 		return 1;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  614) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  615) 	if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  616) 		return 0;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  617) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  618) 	printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  619) 	       "\"%s\" specified in COW header of \"%s\"\n",
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  620) 	       from_cmdline, from_cow, cow);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  621) 	return 1;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  622) }
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  623) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  624) static int open_ubd_file(char *file, struct openflags *openflags, int shared,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  625) 		  char **backing_file_out, int *bitmap_offset_out,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  626) 		  unsigned long *bitmap_len_out, int *data_offset_out,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  627) 		  int *create_cow_out)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  628) {
853bc0ab341b0 (Arnd Bergmann                  2019-11-05 09:39:51 +0100  629) 	time64_t mtime;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  630) 	unsigned long long size;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  631) 	__u32 version, align;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  632) 	char *backing_file;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  633) 	int fd, err, sectorsize, asked_switch, mode = 0644;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  634) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  635) 	fd = os_open_file(file, *openflags, mode);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  636) 	if (fd < 0) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  637) 		if ((fd == -ENOENT) && (create_cow_out != NULL))
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  638) 			*create_cow_out = 1;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  639) 		if (!openflags->w ||
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  640) 		    ((fd != -EROFS) && (fd != -EACCES)))
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  641) 			return fd;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  642) 		openflags->w = 0;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  643) 		fd = os_open_file(file, *openflags, mode);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  644) 		if (fd < 0)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  645) 			return fd;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  646) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  647) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  648) 	if (shared)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  649) 		printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  650) 	else {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  651) 		err = os_lock_file(fd, openflags->w);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  652) 		if (err < 0) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  653) 			printk(KERN_ERR "Failed to lock '%s', err = %d\n",
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  654) 			       file, -err);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  655) 			goto out_close;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  656) 		}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  657) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  658) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  659) 	/* Successful return case! */
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  660) 	if (backing_file_out == NULL)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  661) 		return fd;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  662) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  663) 	err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  664) 			      &size, &sectorsize, &align, bitmap_offset_out);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  665) 	if (err && (*backing_file_out != NULL)) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  666) 		printk(KERN_ERR "Failed to read COW header from COW file "
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  667) 		       "\"%s\", errno = %d\n", file, -err);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  668) 		goto out_close;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  669) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  670) 	if (err)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  671) 		return fd;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  672) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  673) 	asked_switch = path_requires_switch(*backing_file_out, backing_file,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  674) 					    file);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  675) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  676) 	/* Allow switching only if no mismatch. */
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  677) 	if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  678) 						   mtime)) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  679) 		printk(KERN_ERR "Switching backing file to '%s'\n",
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  680) 		       *backing_file_out);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  681) 		err = write_cow_header(file, fd, *backing_file_out,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  682) 				       sectorsize, align, &size);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  683) 		if (err) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  684) 			printk(KERN_ERR "Switch failed, errno = %d\n", -err);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  685) 			goto out_close;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  686) 		}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  687) 	} else {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  688) 		*backing_file_out = backing_file;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  689) 		err = backing_file_mismatch(*backing_file_out, size, mtime);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  690) 		if (err)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  691) 			goto out_close;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  692) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  693) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  694) 	cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  695) 		  bitmap_len_out, data_offset_out);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  696) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  697) 	return fd;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  698)  out_close:
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  699) 	os_close_file(fd);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  700) 	return err;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  701) }
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  702) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  703) static int create_cow_file(char *cow_file, char *backing_file,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  704) 		    struct openflags flags,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  705) 		    int sectorsize, int alignment, int *bitmap_offset_out,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  706) 		    unsigned long *bitmap_len_out, int *data_offset_out)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  707) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  708) 	int err, fd;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  709) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  710) 	flags.c = 1;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  711) 	fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  712) 	if (fd < 0) {
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  713) 		err = fd;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  714) 		printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  715) 		       cow_file, -err);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  716) 		goto out;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  717) 	}
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  718) 
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  719) 	err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  720) 			    bitmap_offset_out, bitmap_len_out,
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  721) 			    data_offset_out);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  722) 	if (!err)
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  723) 		return fd;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  724) 	os_close_file(fd);
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  725)  out:
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  726) 	return err;
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  727) }
5dc62b1b64083 (WANG Cong                      2008-04-28 02:13:58 -0700  728) 
5f75a4f887a35 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:06 -0800  729) static void ubd_close_dev(struct ubd *ubd_dev)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  730) {
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  731) 	os_close_file(ubd_dev->fd);
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  732) 	if(ubd_dev->cow.file == NULL)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  733) 		return;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  734) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  735) 	os_close_file(ubd_dev->cow.fd);
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  736) 	vfree(ubd_dev->cow.bitmap);
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  737) 	ubd_dev->cow.bitmap = NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  738) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  739) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  740) static int ubd_open_dev(struct ubd *ubd_dev)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  741) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  742) 	struct openflags flags;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  743) 	char **back_ptr;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  744) 	int err, create_cow, *create_ptr;
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  745) 	int fd;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  746) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  747) 	ubd_dev->openflags = ubd_dev->boot_openflags;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  748) 	create_cow = 0;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  749) 	create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  750) 	back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  751) 
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  752) 	fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  753) 				back_ptr, &ubd_dev->cow.bitmap_offset,
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  754) 				&ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800  755) 				create_ptr);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  756) 
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  757) 	if((fd == -ENOENT) && create_cow){
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  758) 		fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000  759) 					  ubd_dev->openflags, SECTOR_SIZE, PAGE_SIZE,
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  760) 					  &ubd_dev->cow.bitmap_offset,
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  761) 					  &ubd_dev->cow.bitmap_len,
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  762) 					  &ubd_dev->cow.data_offset);
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  763) 		if(fd >= 0){
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  764) 			printk(KERN_INFO "Creating \"%s\" as COW file for "
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  765) 			       "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  766) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  767) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  768) 
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  769) 	if(fd < 0){
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  770) 		printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  771) 		       -fd);
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  772) 		return fd;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  773) 	}
0bf16bffeef65 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:11 -0800  774) 	ubd_dev->fd = fd;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  775) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  776) 	if(ubd_dev->cow.file != NULL){
086fa5ff0854c (Martin K. Petersen             2010-02-26 00:20:38 -0500  777) 		blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
f4768ffd1d4b7 (Jeff Dike                      2007-08-22 14:01:53 -0700  778) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  779) 		err = -ENOMEM;
da2486ba2c3e6 (Jesper Juhl                    2007-10-16 01:27:19 -0700  780) 		ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  781) 		if(ubd_dev->cow.bitmap == NULL){
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  782) 			printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  783) 			goto error;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  784) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  785) 		flush_tlb_kernel_vm();
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  786) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  787) 		err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  788) 				      ubd_dev->cow.bitmap_offset,
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  789) 				      ubd_dev->cow.bitmap_len);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  790) 		if(err < 0)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  791) 			goto error;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  792) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  793) 		flags = ubd_dev->openflags;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  794) 		flags.w = 0;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  795) 		err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800  796) 				    NULL, NULL, NULL, NULL);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  797) 		if(err < 0) goto error;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  798) 		ubd_dev->cow.fd = err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  799) 	}
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  800) 	if (ubd_dev->no_trim == 0) {
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  801) 		ubd_dev->queue->limits.discard_granularity = SECTOR_SIZE;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  802) 		ubd_dev->queue->limits.discard_alignment = SECTOR_SIZE;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  803) 		blk_queue_max_discard_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  804) 		blk_queue_max_write_zeroes_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  805) 		blk_queue_flag_set(QUEUE_FLAG_DISCARD, ubd_dev->queue);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000  806) 	}
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000  807) 	blk_queue_flag_set(QUEUE_FLAG_NONROT, ubd_dev->queue);
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  808) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  809)  error:
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  810) 	os_close_file(ubd_dev->fd);
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  811) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  812) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  813) 
2e3f5251ac716 (Jeff Dike                      2007-05-06 14:51:29 -0700  814) static void ubd_device_release(struct device *dev)
2e3f5251ac716 (Jeff Dike                      2007-05-06 14:51:29 -0700  815) {
8691b97b99d8b (Greg Kroah-Hartman             2009-05-04 12:40:54 -0700  816) 	struct ubd *ubd_dev = dev_get_drvdata(dev);
2e3f5251ac716 (Jeff Dike                      2007-05-06 14:51:29 -0700  817) 
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  818) 	blk_mq_free_tag_set(&ubd_dev->tag_set);
2e3f5251ac716 (Jeff Dike                      2007-05-06 14:51:29 -0700  819) 	*ubd_dev = ((struct ubd) DEFAULT_UBD);
2e3f5251ac716 (Jeff Dike                      2007-05-06 14:51:29 -0700  820) }
2e3f5251ac716 (Jeff Dike                      2007-05-06 14:51:29 -0700  821) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  822) static ssize_t serial_show(struct device *dev,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  823) 			   struct device_attribute *attr, char *buf)
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  824) {
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  825) 	struct gendisk *disk = dev_to_disk(dev);
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  826) 	struct ubd *ubd_dev = disk->private_data;
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  827) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  828) 	if (!ubd_dev)
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  829) 		return 0;
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  830) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  831) 	return sprintf(buf, "%s", ubd_dev->serial);
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  832) }
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  833) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  834) static DEVICE_ATTR_RO(serial);
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  835) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  836) static struct attribute *ubd_attrs[] = {
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  837) 	&dev_attr_serial.attr,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  838) 	NULL,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  839) };
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  840) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  841) static umode_t ubd_attrs_are_visible(struct kobject *kobj,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  842) 				     struct attribute *a, int n)
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  843) {
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  844) 	return a->mode;
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  845) }
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  846) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  847) static const struct attribute_group ubd_attr_group = {
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  848) 	.attrs = ubd_attrs,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  849) 	.is_visible = ubd_attrs_are_visible,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  850) };
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  851) 
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  852) static const struct attribute_group *ubd_attr_groups[] = {
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  853) 	&ubd_attr_group,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  854) 	NULL,
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  855) };
ef3ba87cb7c91 (Christopher Obbard             2020-11-23 18:31:37 +0000  856) 
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  857) static void ubd_disk_register(int major, u64 size, int unit,
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  858) 			      struct gendisk *disk)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  859) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  860) 	disk->major = major;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  861) 	disk->first_minor = unit << UBD_SHIFT;
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  862) 	disk->minors = 1 << UBD_SHIFT;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  863) 	disk->fops = &ubd_blops;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  864) 	set_capacity(disk, size / 512);
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  865) 	sprintf(disk->disk_name, "ubd%c", 'a' + unit);
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  866) 
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  867) 	ubd_devs[unit].pdev.id   = unit;
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  868) 	ubd_devs[unit].pdev.name = DRIVER_NAME;
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  869) 	ubd_devs[unit].pdev.dev.release = ubd_device_release;
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  870) 	dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  871) 	platform_device_register(&ubd_devs[unit].pdev);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  872) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  873) 	disk->private_data = &ubd_devs[unit];
62f96cb01e8de (Jeff Dike                      2007-02-10 01:44:16 -0800  874) 	disk->queue = ubd_devs[unit].queue;
7eb90f7e90a85 (Christoph Hellwig              2021-06-14 08:07:58 +0200  875) 	device_add_disk(&ubd_devs[unit].pdev.dev, disk, ubd_attr_groups);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  876) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  877) 
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000  878) #define ROUND_BLOCK(n) ((n + (SECTOR_SIZE - 1)) & (-SECTOR_SIZE))
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  879) 
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  880) static const struct blk_mq_ops ubd_mq_ops = {
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  881) 	.queue_rq = ubd_queue_rq,
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  882) };
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  883) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  884) static int ubd_add(int n, char **error_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  885) {
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  886) 	struct ubd *ubd_dev = &ubd_devs[n];
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  887) 	struct gendisk *disk;
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  888) 	int err = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  889) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  890) 	if(ubd_dev->file == NULL)
ec7cf783dd6cf (Jeff Dike                      2005-09-03 15:57:29 -0700  891) 		goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  892) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  893) 	err = ubd_file_size(ubd_dev, &ubd_dev->size);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  894) 	if(err < 0){
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  895) 		*error_out = "Couldn't determine size of device's file";
80c1374914028 (Jeff Dike                      2006-09-29 01:58:51 -0700  896) 		goto out;
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  897) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  898) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  899) 	ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  900) 
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  901) 	ubd_dev->tag_set.ops = &ubd_mq_ops;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  902) 	ubd_dev->tag_set.queue_depth = 64;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  903) 	ubd_dev->tag_set.numa_node = NUMA_NO_NODE;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  904) 	ubd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  905) 	ubd_dev->tag_set.driver_data = ubd_dev;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  906) 	ubd_dev->tag_set.nr_hw_queues = 1;
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700  907) 
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  908) 	err = blk_mq_alloc_tag_set(&ubd_dev->tag_set);
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  909) 	if (err)
80c1374914028 (Jeff Dike                      2006-09-29 01:58:51 -0700  910) 		goto out;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  911) 
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  912) 	disk = blk_mq_alloc_disk(&ubd_dev->tag_set, ubd_dev);
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  913) 	if (IS_ERR(disk)) {
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  914) 		err = PTR_ERR(disk);
aea05eb56e470 (Anton Ivanov                   2019-02-26 15:55:25 +0000  915) 		goto out_cleanup_tags;
62f96cb01e8de (Jeff Dike                      2007-02-10 01:44:16 -0800  916) 	}
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  917) 	ubd_dev->queue = disk->queue;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  918) 
f935a8ce0a60a (Jens Axboe                     2016-03-30 10:19:17 -0600  919) 	blk_queue_write_cache(ubd_dev->queue, true, false);
8a78362c4eefc (Martin K. Petersen             2010-02-26 00:20:39 -0500  920) 	blk_queue_max_segments(ubd_dev->queue, MAX_SG);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500  921) 	blk_queue_segment_boundary(ubd_dev->queue, PAGE_SIZE - 1);
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  922) 	ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, disk);
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  923) 	ubd_gendisk[n] = disk;
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  924) 	return 0;
62f96cb01e8de (Jeff Dike                      2007-02-10 01:44:16 -0800  925) 
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  926) out_cleanup_tags:
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100  927) 	blk_mq_free_tag_set(&ubd_dev->tag_set);
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  928) out:
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200  929) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  930) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  931) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  932) static int ubd_config(char *str, char **error_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  933) {
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  934) 	int n, ret;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  935) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  936) 	/* This string is possibly broken up and stored, so it's only
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  937) 	 * freed if ubd_setup_common fails, or if only general options
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  938) 	 * were set.
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  939) 	 */
970d6e3a3461e (Jeff Dike                      2006-01-06 00:18:48 -0800  940) 	str = kstrdup(str, GFP_KERNEL);
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  941) 	if (str == NULL) {
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  942) 		*error_out = "Failed to allocate memory";
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  943) 		return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  944) 	}
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  945) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  946) 	ret = ubd_setup_common(str, &n, error_out);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  947) 	if (ret)
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  948) 		goto err_free;
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  949) 
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  950) 	if (n == -1) {
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  951) 		ret = 0;
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800  952) 		goto err_free;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  953) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  954) 
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  955) 	mutex_lock(&ubd_lock);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800  956) 	ret = ubd_add(n, error_out);
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  957) 	if (ret)
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  958) 		ubd_devs[n].file = NULL;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  959) 	mutex_unlock(&ubd_lock);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  960) 
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  961) out:
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  962) 	return ret;
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  963) 
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  964) err_free:
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  965) 	kfree(str);
e7f6552f23749 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:09 -0800  966) 	goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  967) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  968) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  969) static int ubd_get_config(char *name, char *str, int size, char **error_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  970) {
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  971) 	struct ubd *ubd_dev;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  972) 	int n, len = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  973) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  974) 	n = parse_unit(&name);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  975) 	if((n >= MAX_DEV) || (n < 0)){
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  976) 		*error_out = "ubd_get_config : device number out of range";
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  977) 		return -1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  978) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  979) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  980) 	ubd_dev = &ubd_devs[n];
d7fb2c3865ca0 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:07 -0800  981) 	mutex_lock(&ubd_lock);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  982) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  983) 	if(ubd_dev->file == NULL){
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  984) 		CONFIG_CHUNK(str, size, len, "", 1);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  985) 		goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  986) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  987) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  988) 	CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  989) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  990) 	if(ubd_dev->cow.file != NULL){
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  991) 		CONFIG_CHUNK(str, size, len, ",", 0);
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800  992) 		CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  993) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  994) 	else CONFIG_CHUNK(str, size, len, "", 1);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  995) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  996)  out:
d7fb2c3865ca0 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:07 -0800  997) 	mutex_unlock(&ubd_lock);
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700  998) 	return len;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  999) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1000) 
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1001) static int ubd_id(char **str, int *start_out, int *end_out)
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1002) {
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1003) 	int n;
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1004) 
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1005) 	n = parse_unit(str);
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1006) 	*start_out = 0;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1007) 	*end_out = MAX_DEV - 1;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1008) 	return n;
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1009) }
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1010) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1011) static int ubd_remove(int n, char **error_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1012) {
2e3f5251ac716 (Jeff Dike                      2007-05-06 14:51:29 -0700 1013) 	struct gendisk *disk = ubd_gendisk[n];
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1014) 	struct ubd *ubd_dev;
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1015) 	int err = -ENODEV;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1016) 
d7fb2c3865ca0 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:07 -0800 1017) 	mutex_lock(&ubd_lock);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1018) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1019) 	ubd_dev = &ubd_devs[n];
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1020) 
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1021) 	if(ubd_dev->file == NULL)
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1022) 		goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1023) 
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1024) 	/* you cannot remove a open disk */
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1025) 	err = -EBUSY;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1026) 	if(ubd_dev->count > 0)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1027) 		goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1028) 
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1029) 	ubd_gendisk[n] = NULL;
b47d2debf2294 (Jeff Dike                      2007-05-06 14:51:01 -0700 1030) 	if(disk != NULL){
b47d2debf2294 (Jeff Dike                      2007-05-06 14:51:01 -0700 1031) 		del_gendisk(disk);
35efb594c3a8b (Christoph Hellwig              2021-06-14 08:07:59 +0200 1032) 		blk_cleanup_disk(disk);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1033) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1034) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1035) 	err = 0;
2e3f5251ac716 (Jeff Dike                      2007-05-06 14:51:29 -0700 1036) 	platform_device_unregister(&ubd_dev->pdev);
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1037) out:
d7fb2c3865ca0 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:07 -0800 1038) 	mutex_unlock(&ubd_lock);
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1039) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1040) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1041) 
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1042) /* All these are called by mconsole in process context and without
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800 1043)  * ubd-specific locks.  The structure itself is const except for .list.
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1044)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1045) static struct mc_device ubd_mc = {
84f48d4f2b511 (Jeff Dike                      2007-02-10 01:44:01 -0800 1046) 	.list		= LIST_HEAD_INIT(ubd_mc.list),
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1047) 	.name		= "ubd",
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1048) 	.config		= ubd_config,
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1049) 	.get_config	= ubd_get_config,
29d56cfe3ca59 (Jeff Dike                      2005-06-25 14:55:25 -0700 1050) 	.id		= ubd_id,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1051) 	.remove		= ubd_remove,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1052) };
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1053) 
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1054) static int __init ubd_mc_init(void)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1055) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1056) 	mconsole_register_dev(&ubd_mc);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1057) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1058) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1059) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1060) __initcall(ubd_mc_init);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1061) 
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1062) static int __init ubd0_init(void)
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1063) {
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1064) 	struct ubd *ubd_dev = &ubd_devs[0];
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1065) 
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800 1066) 	mutex_lock(&ubd_lock);
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1067) 	if(ubd_dev->file == NULL)
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1068) 		ubd_dev->file = "root_fs";
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800 1069) 	mutex_unlock(&ubd_lock);
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800 1070) 
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1071) 	return 0;
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1072) }
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1073) 
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1074) __initcall(ubd0_init);
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1075) 
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800 1076) /* Used in ubd_init, which is an initcall */
3ae5eaec1d2d9 (Russell King                   2005-11-09 22:32:44 +0000 1077) static struct platform_driver ubd_driver = {
3ae5eaec1d2d9 (Russell King                   2005-11-09 22:32:44 +0000 1078) 	.driver = {
3ae5eaec1d2d9 (Russell King                   2005-11-09 22:32:44 +0000 1079) 		.name  = DRIVER_NAME,
3ae5eaec1d2d9 (Russell King                   2005-11-09 22:32:44 +0000 1080) 	},
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1081) };
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1082) 
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1083) static int __init ubd_init(void)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1084) {
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1085) 	char *error;
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1086) 	int i, err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1087) 
792dd4fc317e1 (Christoph Hellwig              2009-03-31 15:23:39 -0700 1088) 	if (register_blkdev(UBD_MAJOR, "ubd"))
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1089) 		return -1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1090) 
6da2ec56059c3 (Kees Cook                      2018-06-12 13:55:00 -0700 1091) 	irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
6da2ec56059c3 (Kees Cook                      2018-06-12 13:55:00 -0700 1092) 				       sizeof(struct io_thread_req *),
6da2ec56059c3 (Kees Cook                      2018-06-12 13:55:00 -0700 1093) 				       GFP_KERNEL
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1094) 		);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1095) 	irq_remainder = 0;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1096) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1097) 	if (irq_req_buffer == NULL) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1098) 		printk(KERN_ERR "Failed to initialize ubd buffering\n");
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1099) 		return -1;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1100) 	}
6da2ec56059c3 (Kees Cook                      2018-06-12 13:55:00 -0700 1101) 	io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
6da2ec56059c3 (Kees Cook                      2018-06-12 13:55:00 -0700 1102) 				      sizeof(struct io_thread_req *),
6da2ec56059c3 (Kees Cook                      2018-06-12 13:55:00 -0700 1103) 				      GFP_KERNEL
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1104) 		);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1105) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1106) 	io_remainder = 0;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1107) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1108) 	if (io_req_buffer == NULL) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1109) 		printk(KERN_ERR "Failed to initialize ubd buffering\n");
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1110) 		return -1;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1111) 	}
3ae5eaec1d2d9 (Russell King                   2005-11-09 22:32:44 +0000 1112) 	platform_driver_register(&ubd_driver);
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1113) 	mutex_lock(&ubd_lock);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1114) 	for (i = 0; i < MAX_DEV; i++){
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1115) 		err = ubd_add(i, &error);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1116) 		if(err)
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1117) 			printk(KERN_ERR "Failed to initialize ubd device %d :"
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1118) 			       "%s\n", i, error);
f28169d200017 (Jeff Dike                      2007-02-10 01:43:53 -0800 1119) 	}
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1120) 	mutex_unlock(&ubd_lock);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1121) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1122) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1123) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1124) late_initcall(ubd_init);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1125) 
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1126) static int __init ubd_driver_init(void){
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1127) 	unsigned long stack;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1128) 	int err;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1129) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1130) 	/* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1131) 	if(global_openflags.s){
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1132) 		printk(KERN_INFO "ubd: Synchronous mode\n");
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1133) 		/* Letting ubd=sync be like using ubd#s= instead of ubd#= is
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1134) 		 * enough. So use anyway the io thread. */
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1135) 	}
2fcb4090cd735 (Johannes Berg                  2021-01-10 19:05:08 +0100 1136) 	stack = alloc_stack(0, 0);
558f9b2f94dbd (YiFei Zhu                      2021-04-20 00:56:10 -0500 1137) 	io_pid = start_io_thread(stack + PAGE_SIZE, &thread_fd);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1138) 	if(io_pid < 0){
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800 1139) 		printk(KERN_ERR
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1140) 		       "ubd : Failed to start I/O thread (errno = %d) - "
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1141) 		       "falling back to synchronous I/O\n", -io_pid);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1142) 		io_pid = -1;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1143) 		return 0;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1144) 	}
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800 1145) 	err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
c0b79a90b1556 (Yong Zhang                     2011-09-22 16:58:46 +0800 1146) 			     0, "ubd", ubd_devs);
36d46a5907ba1 (Johannes Berg                  2020-12-02 12:59:50 +0100 1147) 	if(err < 0)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1148) 		printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
f4c57a78e2c49 (Jeff Dike                      2006-03-31 02:30:10 -0800 1149) 	return 0;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1150) }
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1151) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1152) device_initcall(ubd_driver_init);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1153) 
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500 1154) static int ubd_open(struct block_device *bdev, fmode_t mode)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1155) {
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500 1156) 	struct gendisk *disk = bdev->bd_disk;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1157) 	struct ubd *ubd_dev = disk->private_data;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1158) 	int err = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1159) 
9a181c5861713 (Arnd Bergmann                  2010-09-11 18:38:03 +0200 1160) 	mutex_lock(&ubd_mutex);
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1161) 	if(ubd_dev->count == 0){
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1162) 		err = ubd_open_dev(ubd_dev);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1163) 		if(err){
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1164) 			printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1165) 			       disk->disk_name, ubd_dev->file, -err);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1166) 			goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1167) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1168) 	}
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1169) 	ubd_dev->count++;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1170) 	set_disk_ro(disk, !ubd_dev->openflags.w);
2c49be99ede1e (Paolo 'Blaisorblade' Giarrusso 2005-05-01 08:58:57 -0700 1171) 
2c49be99ede1e (Paolo 'Blaisorblade' Giarrusso 2005-05-01 08:58:57 -0700 1172) 	/* This should no more be needed. And it didn't work anyway to exclude
2c49be99ede1e (Paolo 'Blaisorblade' Giarrusso 2005-05-01 08:58:57 -0700 1173) 	 * read-write remounting of filesystems.*/
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500 1174) 	/*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
5f75a4f887a35 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:06 -0800 1175) 	        if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1176) 	        err = -EROFS;
2c49be99ede1e (Paolo 'Blaisorblade' Giarrusso 2005-05-01 08:58:57 -0700 1177) 	}*/
6e9624b8caec2 (Arnd Bergmann                  2010-08-07 18:25:34 +0200 1178) out:
9a181c5861713 (Arnd Bergmann                  2010-09-11 18:38:03 +0200 1179) 	mutex_unlock(&ubd_mutex);
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1180) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1181) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1182) 
db2a144bedd58 (Al Viro                        2013-05-05 21:52:57 -0400 1183) static void ubd_release(struct gendisk *disk, fmode_t mode)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1184) {
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1185) 	struct ubd *ubd_dev = disk->private_data;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1186) 
9a181c5861713 (Arnd Bergmann                  2010-09-11 18:38:03 +0200 1187) 	mutex_lock(&ubd_mutex);
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1188) 	if(--ubd_dev->count == 0)
5f75a4f887a35 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:06 -0800 1189) 		ubd_close_dev(ubd_dev);
9a181c5861713 (Arnd Bergmann                  2010-09-11 18:38:03 +0200 1190) 	mutex_unlock(&ubd_mutex);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1191) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1192) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1193) static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1194) 			  __u64 *cow_offset, unsigned long *bitmap,
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1195) 			  __u64 bitmap_offset, unsigned long *bitmap_words,
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1196) 			  __u64 bitmap_len)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1197) {
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1198) 	__u64 sector = io_offset >> SECTOR_SHIFT;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1199) 	int i, update_bitmap = 0;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1200) 
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1201) 	for (i = 0; i < length >> SECTOR_SHIFT; i++) {
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1202) 		if(cow_mask != NULL)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1203) 			ubd_set_bit(i, (unsigned char *) cow_mask);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1204) 		if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1205) 			continue;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1206) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1207) 		update_bitmap = 1;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1208) 		ubd_set_bit(sector + i, (unsigned char *) bitmap);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1209) 	}
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1210) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1211) 	if(!update_bitmap)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1212) 		return;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1213) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1214) 	*cow_offset = sector / (sizeof(unsigned long) * 8);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1215) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1216) 	/* This takes care of the case where we're exactly at the end of the
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1217) 	 * device, and *cow_offset + 1 is off the end.  So, just back it up
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1218) 	 * by one word.  Thanks to Lynn Kerby for the fix and James McMechan
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1219) 	 * for the original diagnosis.
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1220) 	 */
6d0742426c9ad (Jiri Olsa                      2008-05-12 14:01:56 -0700 1221) 	if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
6d0742426c9ad (Jiri Olsa                      2008-05-12 14:01:56 -0700 1222) 					 sizeof(unsigned long)) - 1))
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1223) 		(*cow_offset)--;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1224) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1225) 	bitmap_words[0] = bitmap[*cow_offset];
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1226) 	bitmap_words[1] = bitmap[*cow_offset + 1];
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1227) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1228) 	*cow_offset *= sizeof(unsigned long);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1229) 	*cow_offset += bitmap_offset;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1230) }
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1231) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1232) static void cowify_req(struct io_thread_req *req, struct io_desc *segment,
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1233) 		       unsigned long offset, unsigned long *bitmap,
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1234) 		       __u64 bitmap_offset, __u64 bitmap_len)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1235) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1236) 	__u64 sector = offset >> SECTOR_SHIFT;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1237) 	int i;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1238) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1239) 	if (segment->length > (sizeof(segment->sector_mask) * 8) << SECTOR_SHIFT)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1240) 		panic("Operation too long");
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1241) 
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1242) 	if (req_op(req->req) == REQ_OP_READ) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1243) 		for (i = 0; i < segment->length >> SECTOR_SHIFT; i++) {
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1244) 			if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
6c29256c5703a (Jeff Dike                      2006-03-27 01:14:37 -0800 1245) 				ubd_set_bit(i, (unsigned char *)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1246) 					    &segment->sector_mask);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1247) 		}
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1248) 	} else {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1249) 		cowify_bitmap(offset, segment->length, &segment->sector_mask,
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1250) 			      &segment->cow_offset, bitmap, bitmap_offset,
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1251) 			      segment->bitmap_words, bitmap_len);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1252) 	}
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1253) }
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1254) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1255) static void ubd_map_req(struct ubd *dev, struct io_thread_req *io_req,
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1256) 			struct request *req)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1257) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1258) 	struct bio_vec bvec;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1259) 	struct req_iterator iter;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1260) 	int i = 0;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1261) 	unsigned long byte_offset = io_req->offset;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1262) 	int op = req_op(req);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1263) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1264) 	if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1265) 		io_req->io_desc[0].buffer = NULL;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1266) 		io_req->io_desc[0].length = blk_rq_bytes(req);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1267) 	} else {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1268) 		rq_for_each_segment(bvec, req, iter) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1269) 			BUG_ON(i >= io_req->desc_cnt);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1270) 
25d84545beaae (Christoph Hellwig              2021-08-04 11:56:30 +0200 1271) 			io_req->io_desc[i].buffer = bvec_virt(&bvec);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1272) 			io_req->io_desc[i].length = bvec.bv_len;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1273) 			i++;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1274) 		}
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1275) 	}
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1276) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1277) 	if (dev->cow.file) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1278) 		for (i = 0; i < io_req->desc_cnt; i++) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1279) 			cowify_req(io_req, &io_req->io_desc[i], byte_offset,
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1280) 				   dev->cow.bitmap, dev->cow.bitmap_offset,
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1281) 				   dev->cow.bitmap_len);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1282) 			byte_offset += io_req->io_desc[i].length;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1283) 		}
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1284) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1285) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1286) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1287) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1288) static struct io_thread_req *ubd_alloc_req(struct ubd *dev, struct request *req,
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1289) 					   int desc_cnt)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1290) {
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1291) 	struct io_thread_req *io_req;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1292) 	int i;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1293) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1294) 	io_req = kmalloc(sizeof(*io_req) +
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1295) 			 (desc_cnt * sizeof(struct io_desc)),
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1296) 			 GFP_ATOMIC);
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1297) 	if (!io_req)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1298) 		return NULL;
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 1299) 
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 1300) 	io_req->req = req;
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1301) 	if (dev->cow.file)
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1302) 		io_req->fds[0] = dev->cow.fd;
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1303) 	else
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1304) 		io_req->fds[0] = dev->fd;
0033dfd92a564 (Anton Ivanov                   2018-11-08 13:07:23 +0000 1305) 	io_req->error = 0;
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1306) 	io_req->sectorsize = SECTOR_SIZE;
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1307) 	io_req->fds[1] = dev->fd;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1308) 	io_req->offset = (u64) blk_rq_pos(req) << SECTOR_SHIFT;
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1309) 	io_req->offsets[0] = 0;
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1310) 	io_req->offsets[1] = dev->cow.data_offset;
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1311) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1312) 	for (i = 0 ; i < desc_cnt; i++) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1313) 		io_req->io_desc[i].sector_mask = 0;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1314) 		io_req->io_desc[i].cow_offset = -1;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1315) 	}
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1316) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1317) 	return io_req;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1318) }
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1319) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1320) static int ubd_submit_request(struct ubd *dev, struct request *req)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1321) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1322) 	int segs = 0;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1323) 	struct io_thread_req *io_req;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1324) 	int ret;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1325) 	int op = req_op(req);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1326) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1327) 	if (op == REQ_OP_FLUSH)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1328) 		segs = 0;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1329) 	else if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1330) 		segs = 1;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1331) 	else
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1332) 		segs = blk_rq_nr_phys_segments(req);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1333) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1334) 	io_req = ubd_alloc_req(dev, req, segs);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1335) 	if (!io_req)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1336) 		return -ENOMEM;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1337) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1338) 	io_req->desc_cnt = segs;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1339) 	if (segs)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1340) 		ubd_map_req(dev, io_req, req);
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1341) 
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1342) 	ret = os_write_file(thread_fd, &io_req, sizeof(io_req));
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1343) 	if (ret != sizeof(io_req)) {
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1344) 		if (ret != -EAGAIN)
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1345) 			pr_err("write to io thread failed: %d\n", -ret);
bc1d72e73be63 (Richard Weinberger             2013-08-18 13:30:07 +0200 1346) 		kfree(io_req);
bc1d72e73be63 (Richard Weinberger             2013-08-18 13:30:07 +0200 1347) 	}
ecb0a83e3198f (Christoph Hellwig              2018-10-18 22:55:03 +0200 1348) 	return ret;
bc1d72e73be63 (Richard Weinberger             2013-08-18 13:30:07 +0200 1349) }
bc1d72e73be63 (Richard Weinberger             2013-08-18 13:30:07 +0200 1350) 
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100 1351) static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100 1352) 				 const struct blk_mq_queue_data *bd)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1353) {
6961cd4d0fde9 (Jens Axboe                     2018-11-07 14:34:05 -0700 1354) 	struct ubd *ubd_dev = hctx->queue->queuedata;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100 1355) 	struct request *req = bd->rq;
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1356) 	int ret = 0, res = BLK_STS_OK;
a0044bdf60c21 (Jeff Dike                      2007-05-06 14:51:36 -0700 1357) 
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100 1358) 	blk_mq_start_request(req);
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100 1359) 
6961cd4d0fde9 (Jens Axboe                     2018-11-07 14:34:05 -0700 1360) 	spin_lock_irq(&ubd_dev->lock);
6961cd4d0fde9 (Jens Axboe                     2018-11-07 14:34:05 -0700 1361) 
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1362) 	switch (req_op(req)) {
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1363) 	case REQ_OP_FLUSH:
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1364) 	case REQ_OP_READ:
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1365) 	case REQ_OP_WRITE:
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1366) 	case REQ_OP_DISCARD:
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1367) 	case REQ_OP_WRITE_ZEROES:
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1368) 		ret = ubd_submit_request(ubd_dev, req);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1369) 		break;
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1370) 	default:
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1371) 		WARN_ON_ONCE(1);
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1372) 		res = BLK_STS_NOTSUPP;
4e6da0fe8058d (Richard Weinberger             2017-11-26 13:33:11 +0100 1373) 	}
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1374) 
6961cd4d0fde9 (Jens Axboe                     2018-11-07 14:34:05 -0700 1375) 	spin_unlock_irq(&ubd_dev->lock);
6961cd4d0fde9 (Jens Axboe                     2018-11-07 14:34:05 -0700 1376) 
d848074b2f1eb (Anton Ivanov                   2019-10-29 09:13:34 +0000 1377) 	if (ret < 0) {
d848074b2f1eb (Anton Ivanov                   2019-10-29 09:13:34 +0000 1378) 		if (ret == -ENOMEM)
d848074b2f1eb (Anton Ivanov                   2019-10-29 09:13:34 +0000 1379) 			res = BLK_STS_RESOURCE;
d848074b2f1eb (Anton Ivanov                   2019-10-29 09:13:34 +0000 1380) 		else
d848074b2f1eb (Anton Ivanov                   2019-10-29 09:13:34 +0000 1381) 			res = BLK_STS_DEV_RESOURCE;
d848074b2f1eb (Anton Ivanov                   2019-10-29 09:13:34 +0000 1382) 	}
6961cd4d0fde9 (Jens Axboe                     2018-11-07 14:34:05 -0700 1383) 
53766defb8c86 (Anton Ivanov                   2018-11-14 18:41:07 +0000 1384) 	return res;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1385) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1386) 
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800 1387) static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800 1388) {
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1389) 	struct ubd *ubd_dev = bdev->bd_disk->private_data;
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800 1390) 
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800 1391) 	geo->heads = 128;
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800 1392) 	geo->sectors = 32;
7d314e346d608 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:05 -0800 1393) 	geo->cylinders = ubd_dev->size / (128 * 32 * 512);
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800 1394) 	return 0;
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800 1395) }
a885c8c4316e1 (Christoph Hellwig              2006-01-08 01:02:50 -0800 1396) 
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500 1397) static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1398) 		     unsigned int cmd, unsigned long arg)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1399) {
a625c998e638c (Al Viro                        2008-03-02 09:16:26 -0500 1400) 	struct ubd *ubd_dev = bdev->bd_disk->private_data;
73855e13b2bce (Bartlomiej Zolnierkiewicz      2009-04-01 21:42:21 +0200 1401) 	u16 ubd_id[ATA_ID_WORDS];
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1402) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1403) 	switch (cmd) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1404) 		struct cdrom_volctrl volume;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1405) 	case HDIO_GET_IDENTITY:
73855e13b2bce (Bartlomiej Zolnierkiewicz      2009-04-01 21:42:21 +0200 1406) 		memset(&ubd_id, 0, ATA_ID_WORDS * 2);
73855e13b2bce (Bartlomiej Zolnierkiewicz      2009-04-01 21:42:21 +0200 1407) 		ubd_id[ATA_ID_CYLS]	= ubd_dev->size / (128 * 32 * 512);
73855e13b2bce (Bartlomiej Zolnierkiewicz      2009-04-01 21:42:21 +0200 1408) 		ubd_id[ATA_ID_HEADS]	= 128;
73855e13b2bce (Bartlomiej Zolnierkiewicz      2009-04-01 21:42:21 +0200 1409) 		ubd_id[ATA_ID_SECTORS]	= 32;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1410) 		if(copy_to_user((char __user *) arg, (char *) &ubd_id,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1411) 				 sizeof(ubd_id)))
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1412) 			return -EFAULT;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1413) 		return 0;
b8831a1d2c78c (Jeff Dike                      2007-02-10 01:44:17 -0800 1414) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1415) 	case CDROMVOLREAD:
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1416) 		if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1417) 			return -EFAULT;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1418) 		volume.channel0 = 255;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1419) 		volume.channel1 = 255;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1420) 		volume.channel2 = 255;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1421) 		volume.channel3 = 255;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1422) 		if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1423) 			return -EFAULT;
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1424) 		return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1425) 	}
dc764e5087bce (Jeff Dike                      2007-05-06 14:51:41 -0700 1426) 	return -EINVAL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1427) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1428) 
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1429) static int map_error(int error_code)
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1430) {
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1431) 	switch (error_code) {
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1432) 	case 0:
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1433) 		return BLK_STS_OK;
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1434) 	case ENOSYS:
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1435) 	case EOPNOTSUPP:
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1436) 		return BLK_STS_NOTSUPP;
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1437) 	case ENOSPC:
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1438) 		return BLK_STS_NOSPC;
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1439) 	}
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1440) 	return BLK_STS_IOERR;
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1441) }
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1442) 
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1443) /*
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1444)  * Everything from here onwards *IS NOT PART OF THE KERNEL*
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1445)  *
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1446)  * The following functions are part of UML hypervisor code.
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1447)  * All functions from here onwards are executed as a helper
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1448)  * thread and are not allowed to execute any kernel functions.
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1449)  *
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1450)  * Any communication must occur strictly via shared memory and IPC.
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1451)  *
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1452)  * Do not add printks, locks, kernel memory operations, etc - it
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1453)  * will result in unpredictable behaviour and/or crashes.
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1454)  */
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1455) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1456) static int update_bitmap(struct io_thread_req *req, struct io_desc *segment)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1457) {
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1458) 	int n;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1459) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1460) 	if (segment->cow_offset == -1)
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1461) 		return map_error(0);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1462) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1463) 	n = os_pwrite_file(req->fds[1], &segment->bitmap_words,
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1464) 			  sizeof(segment->bitmap_words), segment->cow_offset);
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1465) 	if (n != sizeof(segment->bitmap_words))
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1466) 		return map_error(-n);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1467) 
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1468) 	return map_error(0);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1469) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1470) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1471) static void do_io(struct io_thread_req *req, struct io_desc *desc)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1472) {
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1473) 	char *buf = NULL;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1474) 	unsigned long len;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1475) 	int n, nsectors, start, end, bit;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1476) 	__u64 off;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1477) 
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1478) 	/* FLUSH is really a special case, we cannot "case" it with others */
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1479) 
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1480) 	if (req_op(req->req) == REQ_OP_FLUSH) {
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 1481) 		/* fds[0] is always either the rw image or our cow file */
a41421edb926f (Anton Ivanov                   2018-11-14 18:41:08 +0000 1482) 		req->error = map_error(-os_sync_file(req->fds[0]));
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 1483) 		return;
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 1484) 	}
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 1485) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1486) 	nsectors = desc->length / req->sectorsize;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1487) 	start = 0;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1488) 	do {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1489) 		bit = ubd_test_bit(start, (unsigned char *) &desc->sector_mask);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1490) 		end = start;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1491) 		while((end < nsectors) &&
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1492) 		      (ubd_test_bit(end, (unsigned char *) &desc->sector_mask) == bit))
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1493) 			end++;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1494) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1495) 		off = req->offset + req->offsets[bit] +
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1496) 			start * req->sectorsize;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1497) 		len = (end - start) * req->sectorsize;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1498) 		if (desc->buffer != NULL)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1499) 			buf = &desc->buffer[start * req->sectorsize];
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1500) 
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1501) 		switch (req_op(req->req)) {
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1502) 		case REQ_OP_READ:
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1503) 			n = 0;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1504) 			do {
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1505) 				buf = &buf[n];
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1506) 				len -= n;
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 1507) 				n = os_pread_file(req->fds[bit], buf, len, off);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1508) 				if (n < 0) {
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1509) 					req->error = map_error(-n);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1510) 					return;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1511) 				}
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1512) 			} while((n < len) && (n != 0));
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1513) 			if (n < len) memset(&buf[n], 0, len - n);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1514) 			break;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1515) 		case REQ_OP_WRITE:
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 1516) 			n = os_pwrite_file(req->fds[bit], buf, len, off);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1517) 			if(n != len){
a43c83161a5ec (Anton Ivanov                   2018-11-14 18:41:06 +0000 1518) 				req->error = map_error(-n);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1519) 				return;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1520) 			}
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1521) 			break;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1522) 		case REQ_OP_DISCARD:
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1523) 		case REQ_OP_WRITE_ZEROES:
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1524) 			n = os_falloc_punch(req->fds[bit], off, len);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1525) 			if (n) {
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1526) 				req->error = map_error(-n);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1527) 				return;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1528) 			}
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1529) 			break;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1530) 		default:
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1531) 			WARN_ON_ONCE(1);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1532) 			req->error = BLK_STS_NOTSUPP;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 1533) 			return;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1534) 		}
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1535) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1536) 		start = end;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1537) 	} while(start < nsectors);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1538) 
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1539) 	req->offset += len;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1540) 	req->error = update_bitmap(req, desc);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1541) }
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1542) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1543) /* Changed in start_io_thread, which is serialized by being called only
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1544)  * from ubd_init, which is an initcall.
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1545)  */
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1546) int kernel_fd = -1;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1547) 
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1548) /* Only changed by the io thread. XXX: currently unused. */
d8d7c28ec0b50 (Paolo 'Blaisorblade' Giarrusso 2006-10-30 22:07:12 -0800 1549) static int io_count = 0;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1550) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1551) int io_thread(void *arg)
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1552) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1553) 	int n, count, written, res;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1554) 
91d44ff860a9e (Richard Weinberger             2013-08-18 13:30:08 +0200 1555) 	os_fix_helper_signals();
91d44ff860a9e (Richard Weinberger             2013-08-18 13:30:08 +0200 1556) 
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1557) 	while(1){
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1558) 		n = bulk_req_safe_read(
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1559) 			kernel_fd,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1560) 			io_req_buffer,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1561) 			&io_remainder,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1562) 			&io_remainder_size,
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1563) 			UBD_REQ_BUFFER_SIZE
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1564) 		);
e355b2f55efc4 (Gabriel Krisman Bertazi        2020-03-16 20:45:07 -0400 1565) 		if (n <= 0) {
e355b2f55efc4 (Gabriel Krisman Bertazi        2020-03-16 20:45:07 -0400 1566) 			if (n == -EAGAIN)
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1567) 				ubd_read_poll(-1);
e355b2f55efc4 (Gabriel Krisman Bertazi        2020-03-16 20:45:07 -0400 1568) 
e355b2f55efc4 (Gabriel Krisman Bertazi        2020-03-16 20:45:07 -0400 1569) 			continue;
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1570) 		}
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1571) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1572) 		for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1573) 			struct io_thread_req *req = (*io_req_buffer)[count];
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1574) 			int i;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1575) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1576) 			io_count++;
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1577) 			for (i = 0; !req->error && i < req->desc_cnt; i++)
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1578) 				do_io(req, &(req->io_desc[i]));
fc6b6a872dcd4 (Gabriel Krisman Bertazi        2020-11-21 23:13:56 -0500 1579) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1580) 		}
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1581) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1582) 		written = 0;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1583) 
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1584) 		do {
6e682d53fc1ef (Gabriel Krisman Bertazi        2020-03-16 20:45:06 -0400 1585) 			res = os_write_file(kernel_fd,
6e682d53fc1ef (Gabriel Krisman Bertazi        2020-03-16 20:45:06 -0400 1586) 					    ((char *) io_req_buffer) + written,
6e682d53fc1ef (Gabriel Krisman Bertazi        2020-03-16 20:45:06 -0400 1587) 					    n - written);
ff6a17989c08b (Anton Ivanov                   2017-11-20 21:17:58 +0000 1588) 			if (res >= 0) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1589) 				written += res;
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1590) 			}
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1591) 			if (written < n) {
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1592) 				ubd_write_poll(-1);
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1593) 			}
f88f0bdfc32f3 (Anton Ivanov                   2016-11-09 20:43:25 +0000 1594) 		} while (written < n);
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1595) 	}
91acb21f084aa (Jeff Dike                      2005-10-10 23:10:32 -0400 1596) 
1b57e9c27882a (Jeff Dike                      2006-01-06 00:18:49 -0800 1597) 	return 0;
1b57e9c27882a (Jeff Dike                      2006-01-06 00:18:49 -0800 1598) }