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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * Copyright (C) 2018 Google Limited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * This file is released under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "dm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "dm-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/dm-bufio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #define DM_MSG_PREFIX "bow"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) struct log_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 	u64 source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 	u64 dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 	u32 checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) struct log_sector {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 	u32 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	u16 header_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	u16 header_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	u32 block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	u32 count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	u32 sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	sector_t sector0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	struct log_entry entries[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * MAGIC is BOW in ascii
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define MAGIC 0x00574f42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define HEADER_VERSION 0x0100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * A sorted set of ranges representing the state of the data on the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * Use an rb_tree for fast lookup of a given sector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * Consecutive ranges are always of different type - operations on this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * set must merge matching consecutive ranges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * Top range is always of type TOP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) struct bow_range {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	struct rb_node		node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	sector_t		sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 		INVALID,	/* Type not set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		SECTOR0,	/* First sector - holds log record */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 		SECTOR0_CURRENT,/* Live contents of sector0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 		UNCHANGED,	/* Original contents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		TRIMMED,	/* Range has been trimmed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		CHANGED,	/* Range has been changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 		BACKUP,		/* Range is being used as a backup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		TOP,		/* Final range - sector is size of device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	} type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	struct list_head	trimmed_list; /* list of TRIMMED ranges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static const char * const readable_type[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	"Invalid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	"Sector0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	"Sector0_current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	"Unchanged",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	"Free",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	"Changed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	"Backup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	"Top",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) enum state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	TRIM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	CHECKPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	COMMITTED,
^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) struct bow_context {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	struct dm_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	u32 block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	u32 block_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	struct workqueue_struct *workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	struct dm_bufio_client *bufio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	struct mutex ranges_lock; /* Hold to access this struct and/or ranges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	struct rb_root ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	struct dm_kobject_holder kobj_holder;	/* for sysfs attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	atomic_t state; /* One of the enum state values above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	u64 trims_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	struct log_sector *log_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	struct list_head trimmed_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	bool forward_trims;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) sector_t range_top(struct bow_range *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	return container_of(rb_next(&br->node), struct bow_range, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) u64 range_size(struct bow_range *br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	return (range_top(br) - br->sector) * SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static sector_t bvec_top(struct bvec_iter *bi_iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	return bi_iter->bi_sector + bi_iter->bi_size / SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * Find the first range that overlaps with bi_iter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * bi_iter is set to the size of the overlapping sub-range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) static struct bow_range *find_first_overlapping_range(struct rb_root *ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 						      struct bvec_iter *bi_iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	struct rb_node *node = ranges->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	struct bow_range *br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		br = container_of(node, struct bow_range, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		if (br->sector <= bi_iter->bi_sector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		    && bi_iter->bi_sector < range_top(br))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		if (bi_iter->bi_sector < br->sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 			node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 			node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	WARN_ON(!node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	if (range_top(br) - bi_iter->bi_sector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	    < bi_iter->bi_size >> SECTOR_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		bi_iter->bi_size = (range_top(br) - bi_iter->bi_sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 			<< SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	return br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) void add_before(struct rb_root *ranges, struct bow_range *new_br,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 		struct bow_range *existing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	struct rb_node *parent = &(existing->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	struct rb_node **link = &(parent->rb_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	while (*link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		parent = *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		link = &((*link)->rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	rb_link_node(&new_br->node, parent, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	rb_insert_color(&new_br->node, ranges);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * Given a range br returned by find_first_overlapping_range, split br into a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  * leading range, a range matching the bi_iter and a trailing range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * Leading and trailing may end up size 0 and will then be deleted. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * new range matching the bi_iter is then returned and should have its type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  * and type specific fields populated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  * If bi_iter runs off the end of the range, bi_iter is truncated accordingly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static int split_range(struct bow_context *bc, struct bow_range **br,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		       struct bvec_iter *bi_iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	struct bow_range *new_br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	if (bi_iter->bi_sector < (*br)->sector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		WARN_ON(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		return BLK_STS_IOERR;
^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) 	if (bi_iter->bi_sector > (*br)->sector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		struct bow_range *leading_br =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 			kzalloc(sizeof(*leading_br), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		if (!leading_br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			return BLK_STS_RESOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		*leading_br = **br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		if (leading_br->type == TRIMMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 			list_add(&leading_br->trimmed_list, &bc->trimmed_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		add_before(&bc->ranges, leading_br, *br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		(*br)->sector = bi_iter->bi_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	if (bvec_top(bi_iter) >= range_top(*br)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		bi_iter->bi_size = (range_top(*br) - (*br)->sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 					* SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	/* new_br will be the beginning, existing br will be the tail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	new_br = kzalloc(sizeof(*new_br), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	if (!new_br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		return BLK_STS_RESOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	new_br->sector = (*br)->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	(*br)->sector = bvec_top(bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	add_before(&bc->ranges, new_br, *br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	*br = new_br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * Sets type of a range. May merge range into surrounding ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * Since br may be invalidated, always sets br to NULL to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  * usage after this is called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) static void set_type(struct bow_context *bc, struct bow_range **br, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	struct bow_range *prev = container_of(rb_prev(&(*br)->node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 						      struct bow_range, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	struct bow_range *next = container_of(rb_next(&(*br)->node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 						      struct bow_range, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if ((*br)->type == TRIMMED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		bc->trims_total -= range_size(*br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		list_del(&(*br)->trimmed_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	if (type == TRIMMED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		bc->trims_total += range_size(*br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		list_add(&(*br)->trimmed_list, &bc->trimmed_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	(*br)->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	if (next->type == type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		if (type == TRIMMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			list_del(&next->trimmed_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		rb_erase(&next->node, &bc->ranges);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		kfree(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	if (prev->type == type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		if (type == TRIMMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			list_del(&(*br)->trimmed_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		rb_erase(&(*br)->node, &bc->ranges);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		kfree(*br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	*br = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) static struct bow_range *find_free_range(struct bow_context *bc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	if (list_empty(&bc->trimmed_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		DMERR("Unable to find free space to back up to");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	return list_first_entry(&bc->trimmed_list, struct bow_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 				trimmed_list);
^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) static sector_t sector_to_page(struct bow_context const *bc, sector_t sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	WARN_ON((sector & (((sector_t)1 << (bc->block_shift - SECTOR_SHIFT)) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		!= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	return sector >> (bc->block_shift - SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) static int copy_data(struct bow_context const *bc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		     struct bow_range *source, struct bow_range *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		     u32 *checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	if (range_size(source) != range_size(dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		*checksum = sector_to_page(bc, source->sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	for (i = 0; i < range_size(source) >> bc->block_shift; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		struct dm_buffer *read_buffer, *write_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		u8 *read, *write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		sector_t page = sector_to_page(bc, source->sector) + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		read = dm_bufio_read(bc->bufio, page, &read_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		if (IS_ERR(read)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 			DMERR("Cannot read page %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			      (unsigned long long)page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			return PTR_ERR(read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		if (checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			*checksum = crc32(*checksum, read, bc->block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		write = dm_bufio_new(bc->bufio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 				     sector_to_page(bc, dest->sector) + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 				     &write_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		if (IS_ERR(write)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 			DMERR("Cannot write sector");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 			dm_bufio_release(read_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			return PTR_ERR(write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		memcpy(write, read, bc->block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		dm_bufio_mark_buffer_dirty(write_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		dm_bufio_release(write_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		dm_bufio_release(read_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	dm_bufio_write_dirty_buffers(bc->bufio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	return BLK_STS_OK;
^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) /****** logging functions ******/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) static int add_log_entry(struct bow_context *bc, sector_t source, sector_t dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			 unsigned int size, u32 checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) static int backup_log_sector(struct bow_context *bc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	struct bow_range *first_br, *free_br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	struct bvec_iter bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	u32 checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	first_br = container_of(rb_first(&bc->ranges), struct bow_range, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	if (first_br->type != SECTOR0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	if (range_size(first_br) != bc->block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	free_br = find_free_range(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	/* No space left - return this error to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	if (!free_br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		return BLK_STS_NOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	bi_iter.bi_sector = free_br->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	bi_iter.bi_size = bc->block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	ret = split_range(bc, &free_br, &bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	if (bi_iter.bi_size != bc->block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	ret = copy_data(bc, first_br, free_br, &checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	bc->log_sector->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	bc->log_sector->sequence++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	ret = add_log_entry(bc, first_br->sector, free_br->sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			    range_size(first_br), checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	set_type(bc, &free_br, BACKUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) static int add_log_entry(struct bow_context *bc, sector_t source, sector_t dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 			 unsigned int size, u32 checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	struct dm_buffer *sector_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	u8 *sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	if (sizeof(struct log_sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	    + sizeof(struct log_entry) * (bc->log_sector->count + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		> bc->block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		int ret = backup_log_sector(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	sector = dm_bufio_new(bc->bufio, 0, &sector_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	if (IS_ERR(sector)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		DMERR("Cannot write boot sector");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		dm_bufio_release(sector_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		return BLK_STS_NOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	bc->log_sector->entries[bc->log_sector->count].source = source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	bc->log_sector->entries[bc->log_sector->count].dest = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	bc->log_sector->entries[bc->log_sector->count].size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	bc->log_sector->entries[bc->log_sector->count].checksum = checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	bc->log_sector->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	memcpy(sector, bc->log_sector, bc->block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	dm_bufio_mark_buffer_dirty(sector_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	dm_bufio_release(sector_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	dm_bufio_write_dirty_buffers(bc->bufio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) static int prepare_log(struct bow_context *bc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	struct bow_range *free_br, *first_br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	struct bvec_iter bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	u32 checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	/* Carve out first sector as log sector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	first_br = container_of(rb_first(&bc->ranges), struct bow_range, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	if (first_br->type != UNCHANGED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	if (range_size(first_br) < bc->block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	bi_iter.bi_sector = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	bi_iter.bi_size = bc->block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	ret = split_range(bc, &first_br, &bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	first_br->type = SECTOR0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (range_size(first_br) != bc->block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	/* Find free sector for active sector0 reads/writes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	free_br = find_free_range(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	if (!free_br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		return BLK_STS_NOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	bi_iter.bi_sector = free_br->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	bi_iter.bi_size = bc->block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	ret = split_range(bc, &free_br, &bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	free_br->type = SECTOR0_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	/* Copy data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	ret = copy_data(bc, first_br, free_br, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	bc->log_sector->sector0 = free_br->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	/* Find free sector to back up original sector zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	free_br = find_free_range(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	if (!free_br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		return BLK_STS_NOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	bi_iter.bi_sector = free_br->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	bi_iter.bi_size = bc->block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	ret = split_range(bc, &free_br, &bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	/* Back up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	ret = copy_data(bc, first_br, free_br, &checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	 * Set up our replacement boot sector - it will get written when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	 * add the first log entry, which we do immediately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	bc->log_sector->magic = MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	bc->log_sector->header_version = HEADER_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	bc->log_sector->header_size = sizeof(*bc->log_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	bc->log_sector->block_size = bc->block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	bc->log_sector->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	bc->log_sector->sequence = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	/* Add log entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	ret = add_log_entry(bc, first_br->sector, free_br->sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 			    range_size(first_br), checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	set_type(bc, &free_br, BACKUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) static struct bow_range *find_sector0_current(struct bow_context *bc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	struct bvec_iter bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	bi_iter.bi_sector = bc->log_sector->sector0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	bi_iter.bi_size = bc->block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	return find_first_overlapping_range(&bc->ranges, &bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) /****** sysfs interface functions ******/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	struct bow_context *bc = container_of(kobj, struct bow_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 					      kobj_holder.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&bc->state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			   const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	struct bow_context *bc = container_of(kobj, struct bow_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 					      kobj_holder.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	enum state state, original_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	state = buf[0] - '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	if (state < TRIM || state > COMMITTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		DMERR("State value %d out of range", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	mutex_lock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	original_state = atomic_read(&bc->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (state != original_state + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		DMERR("Invalid state change from %d to %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		      original_state, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	DMINFO("Switching to state %s", state == CHECKPOINT ? "Checkpoint"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	       : state == COMMITTED ? "Committed" : "Unknown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	if (state == CHECKPOINT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		ret = prepare_log(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			DMERR("Failed to switch to checkpoint state");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	} else if (state == COMMITTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		struct bow_range *br = find_sector0_current(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		struct bow_range *sector0_br =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			container_of(rb_first(&bc->ranges), struct bow_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 				     node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		ret = copy_data(bc, br, sector0_br, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 			DMERR("Failed to switch to committed state");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	atomic_inc(&bc->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	mutex_unlock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) static ssize_t free_show(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	struct bow_context *bc = container_of(kobj, struct bow_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 					      kobj_holder.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	u64 trims_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	mutex_lock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	trims_total = bc->trims_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	mutex_unlock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	return scnprintf(buf, PAGE_SIZE, "%llu\n", trims_total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) static struct kobj_attribute attr_state = __ATTR_RW(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) static struct kobj_attribute attr_free = __ATTR_RO(free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) static struct attribute *bow_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	&attr_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	&attr_free.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) static struct kobj_type bow_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	.sysfs_ops = &kobj_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	.default_attrs = bow_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	.release = dm_kobject_release
^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) /****** constructor/destructor ******/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) static void dm_bow_dtr(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	struct bow_context *bc = (struct bow_context *) ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	struct kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	mutex_lock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	while (rb_first(&bc->ranges)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		struct bow_range *br = container_of(rb_first(&bc->ranges),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 						    struct bow_range, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		rb_erase(&br->node, &bc->ranges);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		kfree(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	mutex_unlock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (bc->workqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		destroy_workqueue(bc->workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	if (bc->bufio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		dm_bufio_client_destroy(bc->bufio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	kobj = &bc->kobj_holder.kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (kobj->state_initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		wait_for_completion(dm_get_completion_from_kobject(kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	kfree(bc->log_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	kfree(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) static void dm_bow_io_hints(struct dm_target *ti, struct queue_limits *limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	struct bow_context *bc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	const unsigned int block_size = bc->block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	limits->logical_block_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		max_t(unsigned int, limits->logical_block_size, block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	limits->physical_block_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		max_t(unsigned int, limits->physical_block_size, block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	limits->io_min = max_t(unsigned int, limits->io_min, block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	if (limits->max_discard_sectors == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		limits->discard_granularity = 1 << 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		limits->max_hw_discard_sectors = 1 << 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		limits->max_discard_sectors = 1 << 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		bc->forward_trims = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		limits->discard_granularity = 1 << 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		bc->forward_trims = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) static int dm_bow_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	struct bow_context *bc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	struct dm_arg_set as;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	static const struct dm_arg _args[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		{0, 1, "Invalid number of feature args"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	unsigned int opt_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	const char *opt_string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	char dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	as.argc = argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	as.argv = argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	err = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	while (opt_params--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		opt_string = dm_shift_arg(&as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		if (!opt_string) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			ti->error = "Not enough feature arguments";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		if (sscanf(opt_string, "block_size:%u%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 					&bc->block_size, &dummy) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 			if (bc->block_size < SECTOR_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 			    bc->block_size > 4096 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 			    !is_power_of_2(bc->block_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 				ti->error = "Invalid block_size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			ti->error = "Invalid feature arguments";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) static int dm_bow_ctr(struct dm_target *ti, unsigned int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct bow_context *bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	struct bow_range *br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	struct mapped_device *md = dm_table_get_md(ti->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (argc < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		ti->error = "Invalid argument count";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	bc = kzalloc(sizeof(*bc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	if (!bc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		ti->error = "Cannot allocate bow context";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	ti->num_flush_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	ti->num_discard_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	ti->num_write_same_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	ti->private = bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			    &bc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		ti->error = "Device lookup failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	bc->block_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		bdev_get_queue(bc->dev->bdev)->limits.logical_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (argc > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		ret = dm_bow_ctr_optional(ti, argc - 1, &argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	bc->block_shift = ilog2(bc->block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	bc->log_sector = kzalloc(bc->block_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if (!bc->log_sector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		ti->error = "Cannot allocate log sector";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	init_completion(&bc->kobj_holder.completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	ret = kobject_init_and_add(&bc->kobj_holder.kobj, &bow_ktype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 				   &disk_to_dev(dm_disk(md))->kobj, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 				   "bow");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		ti->error = "Cannot create sysfs node";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	mutex_init(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	bc->ranges = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	bc->bufio = dm_bufio_client_create(bc->dev->bdev, bc->block_size, 1, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 					   NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	if (IS_ERR(bc->bufio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		ti->error = "Cannot initialize dm-bufio";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		ret = PTR_ERR(bc->bufio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		bc->bufio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	bc->workqueue = alloc_workqueue("dm-bow",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 					WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 					| WQ_UNBOUND, num_online_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (!bc->workqueue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		ti->error = "Cannot allocate workqueue";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	INIT_LIST_HEAD(&bc->trimmed_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	br = kzalloc(sizeof(*br), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	if (!br) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		ti->error = "Cannot allocate ranges";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	br->sector = ti->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	br->type = TOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	rb_link_node(&br->node, NULL, &bc->ranges.rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	rb_insert_color(&br->node, &bc->ranges);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	br = kzalloc(sizeof(*br), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (!br) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		ti->error = "Cannot allocate ranges";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	br->sector = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	br->type = UNCHANGED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	rb_link_node(&br->node, bc->ranges.rb_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		     &bc->ranges.rb_node->rb_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	rb_insert_color(&br->node, &bc->ranges);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	ti->discards_supported = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	dm_bow_dtr(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) /****** Handle writes ******/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) static int prepare_unchanged_range(struct bow_context *bc, struct bow_range *br,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 				   struct bvec_iter *bi_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 				   bool record_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	struct bow_range *backup_br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct bvec_iter backup_bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	sector_t log_source, log_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	unsigned int log_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	u32 checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	int original_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	sector_t sector0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	/* Find a free range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	backup_br = find_free_range(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (!backup_br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		return BLK_STS_NOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	/* Carve out a backup range. This may be smaller than the br given */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	backup_bi.bi_sector = backup_br->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	backup_bi.bi_size = min(range_size(backup_br), (u64) bi_iter->bi_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	ret = split_range(bc, &backup_br, &backup_bi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	 * Carve out a changed range. This will not be smaller than the backup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	 * br since the backup br is smaller than the source range and iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	bi_iter->bi_size = backup_bi.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	ret = split_range(bc, &br, bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	if (range_size(br) != range_size(backup_br)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	/* Copy data over */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	ret = copy_data(bc, br, backup_br, record_checksum ? &checksum : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	/* Add an entry to the log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	log_source = br->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	log_dest = backup_br->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	log_size = range_size(br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	 * Set the types. Note that since set_type also amalgamates ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	 * we have to set both sectors to their final type before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	 * set_type on either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	original_type = br->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	sector0 = backup_br->sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	bc->trims_total -= range_size(backup_br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	if (backup_br->type == TRIMMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		list_del(&backup_br->trimmed_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	backup_br->type = br->type == SECTOR0_CURRENT ? SECTOR0_CURRENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 						      : BACKUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	br->type = CHANGED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	set_type(bc, &backup_br, backup_br->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	 * Add the log entry after marking the backup sector, since adding a log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	 * can cause another backup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	ret = add_log_entry(bc, log_source, log_dest, log_size, checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		br->type = original_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	/* Now it is safe to mark this backup successful */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	if (original_type == SECTOR0_CURRENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		bc->log_sector->sector0 = sector0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	set_type(bc, &br, br->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) static int prepare_free_range(struct bow_context *bc, struct bow_range *br,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			      struct bvec_iter *bi_iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	ret = split_range(bc, &br, bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	set_type(bc, &br, CHANGED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) static int prepare_changed_range(struct bow_context *bc, struct bow_range *br,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 				 struct bvec_iter *bi_iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	/* Nothing to do ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) static int prepare_one_range(struct bow_context *bc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			     struct bvec_iter *bi_iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	struct bow_range *br = find_first_overlapping_range(&bc->ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 							    bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	switch (br->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	case CHANGED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		return prepare_changed_range(bc, br, bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	case TRIMMED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		return prepare_free_range(bc, br, bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	case UNCHANGED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	case BACKUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		return prepare_unchanged_range(bc, br, bi_iter, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	 * We cannot track the checksum for the active sector0, since it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	 * may change at any point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	case SECTOR0_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		return prepare_unchanged_range(bc, br, bi_iter, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	case SECTOR0:	/* Handled in the dm_bow_map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	case TOP:	/* Illegal - top is off the end of the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) struct write_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	struct bow_context *bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) static void bow_write(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	struct write_work *ww = container_of(work, struct write_work, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	struct bow_context *bc = ww->bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	struct bio *bio = ww->bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	struct bvec_iter bi_iter = bio->bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	int ret = BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	kfree(ww);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	mutex_lock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		ret = prepare_one_range(bc, &bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		bi_iter.bi_sector += bi_iter.bi_size / SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		bi_iter.bi_size = bio->bi_iter.bi_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			- (bi_iter.bi_sector - bio->bi_iter.bi_sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			  * SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	} while (!ret && bi_iter.bi_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	mutex_unlock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		bio_set_dev(bio, bc->dev->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		submit_bio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		DMERR("Write failure with error %d", -ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		bio->bi_status = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) static int queue_write(struct bow_context *bc, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	struct write_work *ww = kmalloc(sizeof(*ww), GFP_NOIO | __GFP_NORETRY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 					| __GFP_NOMEMALLOC | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	if (!ww) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		DMERR("Failed to allocate write_work");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	INIT_WORK(&ww->work, bow_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	ww->bc = bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	ww->bio = bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	queue_work(bc->workqueue, &ww->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	return DM_MAPIO_SUBMITTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) static int handle_sector0(struct bow_context *bc, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	int ret = DM_MAPIO_REMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	if (bio->bi_iter.bi_size > bc->block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		struct bio * split = bio_split(bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 					       bc->block_size >> SECTOR_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 					       GFP_NOIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 					       &fs_bio_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		if (!split) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			DMERR("Failed to split bio");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			bio->bi_status = BLK_STS_RESOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			return DM_MAPIO_SUBMITTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		bio_chain(split, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		split->bi_iter.bi_sector = bc->log_sector->sector0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		bio_set_dev(split, bc->dev->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		submit_bio(split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		if (bio_data_dir(bio) == WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			ret = queue_write(bc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		bio->bi_iter.bi_sector = bc->log_sector->sector0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) static int add_trim(struct bow_context *bc, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	struct bow_range *br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	struct bvec_iter bi_iter = bio->bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	DMDEBUG("add_trim: %llu, %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		(unsigned long long)bio->bi_iter.bi_sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		bio->bi_iter.bi_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		br = find_first_overlapping_range(&bc->ranges, &bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		switch (br->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		case UNCHANGED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			if (!split_range(bc, &br, &bi_iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 				set_type(bc, &br, TRIMMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		case TRIMMED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 			/* Nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			/* No other case is legal in TRIM state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 			WARN_ON(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		bi_iter.bi_sector += bi_iter.bi_size / SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		bi_iter.bi_size = bio->bi_iter.bi_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			- (bi_iter.bi_sector - bio->bi_iter.bi_sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			  * SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	} while (bi_iter.bi_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	return DM_MAPIO_SUBMITTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) static int remove_trim(struct bow_context *bc, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	struct bow_range *br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	struct bvec_iter bi_iter = bio->bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	DMDEBUG("remove_trim: %llu, %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		(unsigned long long)bio->bi_iter.bi_sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		bio->bi_iter.bi_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		br = find_first_overlapping_range(&bc->ranges, &bi_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		switch (br->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		case UNCHANGED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			/* Nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		case TRIMMED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			if (!split_range(bc, &br, &bi_iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 				set_type(bc, &br, UNCHANGED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			/* No other case is legal in TRIM state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			WARN_ON(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		bi_iter.bi_sector += bi_iter.bi_size / SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		bi_iter.bi_size = bio->bi_iter.bi_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 			- (bi_iter.bi_sector - bio->bi_iter.bi_sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 			  * SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	} while (bi_iter.bi_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	return DM_MAPIO_REMAPPED;
^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) int remap_unless_illegal_trim(struct bow_context *bc, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	if (!bc->forward_trims && bio_op(bio) == REQ_OP_DISCARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		bio->bi_status = BLK_STS_NOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		return DM_MAPIO_SUBMITTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		bio_set_dev(bio, bc->dev->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		return DM_MAPIO_REMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	}
^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) /****** dm interface ******/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) static int dm_bow_map(struct dm_target *ti, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	int ret = DM_MAPIO_REMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	struct bow_context *bc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	if (likely(bc->state.counter == COMMITTED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		return remap_unless_illegal_trim(bc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	if (bio_data_dir(bio) == READ && bio->bi_iter.bi_sector != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		return remap_unless_illegal_trim(bc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if (atomic_read(&bc->state) != COMMITTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		enum state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		mutex_lock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		state = atomic_read(&bc->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		if (state == TRIM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			if (bio_op(bio) == REQ_OP_DISCARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 				ret = add_trim(bc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			else if (bio_data_dir(bio) == WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 				ret = remove_trim(bc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 				/* pass-through */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		} else if (state == CHECKPOINT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			if (bio->bi_iter.bi_sector == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 				ret = handle_sector0(bc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			else if (bio_data_dir(bio) == WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 				ret = queue_write(bc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 				/* pass-through */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			/* pass-through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		mutex_unlock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (ret == DM_MAPIO_REMAPPED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		return remap_unless_illegal_trim(bc, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) static void dm_bow_tablestatus(struct dm_target *ti, char *result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			       unsigned int maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	char *end = result + maxlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	struct bow_context *bc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	struct rb_node *i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	int trimmed_list_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	int trimmed_range_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	struct bow_range *br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	if (maxlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	result[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	list_for_each_entry(br, &bc->trimmed_list, trimmed_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		if (br->type == TRIMMED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 			++trimmed_list_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			scnprintf(result, end - result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 				  "ERROR: non-trimmed entry in trimmed_list");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	if (!rb_first(&bc->ranges)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		scnprintf(result, end - result, "ERROR: Empty ranges");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	if (container_of(rb_first(&bc->ranges), struct bow_range, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	    ->sector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		scnprintf(result, end - result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			 "ERROR: First range does not start at sector 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	mutex_lock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	for (i = rb_first(&bc->ranges); i; i = rb_next(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		struct bow_range *br = container_of(i, struct bow_range, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		result += scnprintf(result, end - result, "%s: %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 				    readable_type[br->type],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 				    (unsigned long long)br->sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		if (result >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		result += scnprintf(result, end - result, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		if (result >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		if (br->type == TRIMMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			++trimmed_range_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		if (br->type == TOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			if (br->sector != ti->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 				scnprintf(result, end - result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 					 "\nERROR: Top sector is incorrect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			if (&br->node != rb_last(&bc->ranges)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 				scnprintf(result, end - result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 					  "\nERROR: Top sector is not last");
^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) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		if (!rb_next(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			scnprintf(result, end - result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 				  "\nERROR: Last range not of type TOP");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		if (br->sector > range_top(br)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 			scnprintf(result, end - result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 				  "\nERROR: sectors out of order");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (trimmed_range_count != trimmed_list_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		scnprintf(result, end - result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			  "\nERROR: not all trimmed ranges in trimmed list");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	mutex_unlock(&bc->ranges_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) static void dm_bow_status(struct dm_target *ti, status_type_t type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			  unsigned int status_flags, char *result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			  unsigned int maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	case STATUSTYPE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		if (maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			result[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	case STATUSTYPE_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		dm_bow_tablestatus(ti, result, maxlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) int dm_bow_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	struct bow_context *bc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	struct dm_dev *dev = bc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	*bdev = dev->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	/* Only pass ioctls through if the device sizes match exactly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	return ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) static int dm_bow_iterate_devices(struct dm_target *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 				  iterate_devices_callout_fn fn, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	struct bow_context *bc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	return fn(ti, bc->dev, 0, ti->len, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) static struct target_type bow_target = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	.name   = "bow",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	.version = {1, 2, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	.features = DM_TARGET_PASSES_CRYPTO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	.ctr    = dm_bow_ctr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	.dtr    = dm_bow_dtr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	.map    = dm_bow_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	.status = dm_bow_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	.prepare_ioctl  = dm_bow_prepare_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	.iterate_devices = dm_bow_iterate_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	.io_hints = dm_bow_io_hints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) int __init dm_bow_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	int r = dm_register_target(&bow_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		DMERR("registering bow failed %d", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) void dm_bow_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	dm_unregister_target(&bow_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) module_init(dm_bow_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) module_exit(dm_bow_exit);