Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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