Orange Pi5 kernel

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

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) #include "dm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3) #include "persistent-data/dm-transaction-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) #include "persistent-data/dm-bitset.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include "persistent-data/dm-space-map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/dm-io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/dm-kcopyd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/mempool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #define DM_MSG_PREFIX "era"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #define SUPERBLOCK_LOCATION 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #define SUPERBLOCK_MAGIC 2126579579
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #define SUPERBLOCK_CSUM_XOR 146538381
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #define MIN_ERA_VERSION 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #define MAX_ERA_VERSION 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define INVALID_WRITESET_ROOT SUPERBLOCK_LOCATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define MIN_BLOCK_SIZE 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * Writeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) struct writeset_metadata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	uint32_t nr_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	dm_block_t root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) struct writeset {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	struct writeset_metadata md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	 * An in core copy of the bits to save constantly doing look ups on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	 * disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	unsigned long *bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * This does not free off the on disk bitset as this will normally be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * after digesting into the era array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) static void writeset_free(struct writeset *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	vfree(ws->bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	ws->bits = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) static int setup_on_disk_bitset(struct dm_disk_bitset *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 				unsigned nr_bits, dm_block_t *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	r = dm_bitset_empty(info, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	return dm_bitset_resize(info, *root, 0, nr_bits, false, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) static size_t bitset_size(unsigned nr_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	return sizeof(unsigned long) * dm_div_up(nr_bits, BITS_PER_LONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * Allocates memory for the in core bitset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) static int writeset_alloc(struct writeset *ws, dm_block_t nr_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	ws->bits = vzalloc(bitset_size(nr_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	if (!ws->bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		DMERR("%s: couldn't allocate in memory bitset", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		return -ENOMEM;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * Wipes the in-core bitset, and creates a new on disk bitset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) static int writeset_init(struct dm_disk_bitset *info, struct writeset *ws,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 			 dm_block_t nr_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	memset(ws->bits, 0, bitset_size(nr_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	ws->md.nr_bits = nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	r = setup_on_disk_bitset(info, ws->md.nr_bits, &ws->md.root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		DMERR("%s: setup_on_disk_bitset failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) static bool writeset_marked(struct writeset *ws, dm_block_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	return test_bit(block, ws->bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) static int writeset_marked_on_disk(struct dm_disk_bitset *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 				   struct writeset_metadata *m, dm_block_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 				   bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	dm_block_t old = m->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	 * The bitset was flushed when it was archived, so we know there'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	 * be no change to the root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	int r = dm_bitset_test_bit(info, m->root, block, &m->root, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		DMERR("%s: dm_bitset_test_bit failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	BUG_ON(m->root != old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  * Returns < 0 on error, 0 if the bit wasn't previously set, 1 if it was.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) static int writeset_test_and_set(struct dm_disk_bitset *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 				 struct writeset *ws, uint32_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	if (!test_bit(block, ws->bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		r = dm_bitset_set_bit(info, ws->md.root, block, &ws->md.root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 			/* FIXME: fail mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		return 0;
^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) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * On disk metadata layout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #define SPACE_MAP_ROOT_SIZE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) #define UUID_LEN 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) struct writeset_disk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	__le32 nr_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	__le64 root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) struct superblock_disk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	__le32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	__le32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	__le64 blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	__u8 uuid[UUID_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	__le64 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	__le32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	__le32 data_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	__le32 metadata_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	__le32 nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	__le32 current_era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	struct writeset_disk current_writeset;
^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) 	 * Only these two fields are valid within the metadata snapshot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	__le64 writeset_tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	__le64 era_array_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	__le64 metadata_snap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190)  * Superblock validation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) static void sb_prepare_for_write(struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 				 struct dm_block *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 				 size_t sb_block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	struct superblock_disk *disk = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	disk->blocknr = cpu_to_le64(dm_block_location(b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	disk->csum = cpu_to_le32(dm_bm_checksum(&disk->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 						sb_block_size - sizeof(__le32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 						SUPERBLOCK_CSUM_XOR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) static int check_metadata_version(struct superblock_disk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	uint32_t metadata_version = le32_to_cpu(disk->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	if (metadata_version < MIN_ERA_VERSION || metadata_version > MAX_ERA_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		DMERR("Era metadata version %u found, but only versions between %u and %u supported.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		      metadata_version, MIN_ERA_VERSION, MAX_ERA_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	return 0;
^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) static int sb_check(struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		    struct dm_block *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		    size_t sb_block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct superblock_disk *disk = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	__le32 csum_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	if (dm_block_location(b) != le64_to_cpu(disk->blocknr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		DMERR("sb_check failed: blocknr %llu: wanted %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		      le64_to_cpu(disk->blocknr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		      (unsigned long long)dm_block_location(b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		return -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (le64_to_cpu(disk->magic) != SUPERBLOCK_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		DMERR("sb_check failed: magic %llu: wanted %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		      le64_to_cpu(disk->magic),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		      (unsigned long long) SUPERBLOCK_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		return -EILSEQ;
^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) 	csum_le = cpu_to_le32(dm_bm_checksum(&disk->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 					     sb_block_size - sizeof(__le32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 					     SUPERBLOCK_CSUM_XOR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	if (csum_le != disk->csum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		DMERR("sb_check failed: csum %u: wanted %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		      le32_to_cpu(csum_le), le32_to_cpu(disk->csum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		return -EILSEQ;
^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) 	return check_metadata_version(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) static struct dm_block_validator sb_validator = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	.name = "superblock",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	.prepare_for_write = sb_prepare_for_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	.check = sb_check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) };
^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)  * Low level metadata handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) #define DM_ERA_METADATA_BLOCK_SIZE 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) #define ERA_MAX_CONCURRENT_LOCKS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) struct era_metadata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	struct dm_block_manager *bm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	struct dm_space_map *sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	struct dm_transaction_manager *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	dm_block_t block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	uint32_t nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	uint32_t current_era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	 * We preallocate 2 writesets.  When an era rolls over we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	 * switch between them. This means the allocation is done at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	 * preresume time, rather than on the io path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	struct writeset writesets[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	struct writeset *current_writeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	dm_block_t writeset_tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	dm_block_t era_array_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	struct dm_disk_bitset bitset_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	struct dm_btree_info writeset_tree_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	struct dm_array_info era_array_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	dm_block_t metadata_snap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	 * A flag that is set whenever a writeset has been archived.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	bool archived_writesets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	 * Reading the space map root can fail, so we read it into this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	 * buffer before the superblock is locked and updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) static int superblock_read_lock(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 				struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	return dm_bm_read_lock(md->bm, SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			       &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) static int superblock_lock_zero(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 				struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	return dm_bm_write_lock_zero(md->bm, SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 				     &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static int superblock_lock(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			   struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	return dm_bm_write_lock(md->bm, SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 				&sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) /* FIXME: duplication with cache and thin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	struct dm_block *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	__le64 *data_le, zero = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	 * We can't use a validator here - it may be all zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	data_le = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	*result = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	for (i = 0; i < sb_block_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		if (data_le[i] != zero) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			*result = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		}
^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) 	dm_bm_unlock(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) static void ws_pack(const struct writeset_metadata *core, struct writeset_disk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	disk->nr_bits = cpu_to_le32(core->nr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	disk->root = cpu_to_le64(core->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) static void ws_unpack(const struct writeset_disk *disk, struct writeset_metadata *core)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	core->nr_bits = le32_to_cpu(disk->nr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	core->root = le64_to_cpu(disk->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) static void ws_inc(void *context, const void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	struct era_metadata *md = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	struct writeset_disk ws_d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	dm_block_t b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	memcpy(&ws_d, value, sizeof(ws_d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	b = le64_to_cpu(ws_d.root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	dm_tm_inc(md->tm, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) static void ws_dec(void *context, const void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	struct era_metadata *md = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	struct writeset_disk ws_d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	dm_block_t b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	memcpy(&ws_d, value, sizeof(ws_d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	b = le64_to_cpu(ws_d.root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	dm_bitset_del(&md->bitset_info, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) static int ws_eq(void *context, const void *value1, const void *value2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	return !memcmp(value1, value2, sizeof(struct writeset_disk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) static void setup_writeset_tree_info(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	struct dm_btree_value_type *vt = &md->writeset_tree_info.value_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	md->writeset_tree_info.tm = md->tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	md->writeset_tree_info.levels = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	vt->context = md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	vt->size = sizeof(struct writeset_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	vt->inc = ws_inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	vt->dec = ws_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	vt->equal = ws_eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) static void setup_era_array_info(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	struct dm_btree_value_type vt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	vt.context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	vt.size = sizeof(__le32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	vt.inc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	vt.dec = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	vt.equal = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	dm_array_info_init(&md->era_array_info, md->tm, &vt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) static void setup_infos(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	dm_disk_bitset_init(md->tm, &md->bitset_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	setup_writeset_tree_info(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	setup_era_array_info(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) static int create_fresh_metadata(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	r = dm_tm_create_with_sm(md->bm, SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 				 &md->tm, &md->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		DMERR("dm_tm_create_with_sm failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	setup_infos(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		DMERR("couldn't create new writeset tree");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	r = dm_array_empty(&md->era_array_info, &md->era_array_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		DMERR("couldn't create era array");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	dm_sm_destroy(md->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	dm_tm_destroy(md->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) static int save_sm_root(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	size_t metadata_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	r = dm_sm_root_size(md->sm, &metadata_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	return dm_sm_copy_root(md->sm, &md->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			       metadata_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	memcpy(&disk->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	       &md->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	       sizeof(md->metadata_space_map_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * Writes a superblock, including the static fields that don't get updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  * with every commit (possible optimisation here).  'md' should be fully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  * constructed when this is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	disk->flags = cpu_to_le32(0ul);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	/* FIXME: can't keep blanking the uuid (uuid is currently unused though) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	memset(disk->uuid, 0, sizeof(disk->uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	disk->version = cpu_to_le32(MAX_ERA_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	copy_sm_root(md, disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	disk->data_block_size = cpu_to_le32(md->block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	disk->metadata_block_size = cpu_to_le32(DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	disk->nr_blocks = cpu_to_le32(md->nr_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	disk->current_era = cpu_to_le32(md->current_era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	ws_pack(&md->current_writeset->md, &disk->current_writeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	disk->writeset_tree_root = cpu_to_le64(md->writeset_tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	disk->era_array_root = cpu_to_le64(md->era_array_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	disk->metadata_snap = cpu_to_le64(md->metadata_snap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) static int write_superblock(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	struct superblock_disk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	r = save_sm_root(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		DMERR("%s: save_sm_root failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	r = superblock_lock_zero(md, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	disk = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	prepare_superblock(md, disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	return dm_tm_commit(md->tm, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535)  * Assumes block_size and the infos are set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) static int format_metadata(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	r = create_fresh_metadata(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	r = write_superblock(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		dm_sm_destroy(md->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		dm_tm_destroy(md->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) static int open_metadata(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	struct superblock_disk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	r = superblock_read_lock(md, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		DMERR("couldn't read_lock superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	disk = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	/* Verify the data block size hasn't changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (le32_to_cpu(disk->data_block_size) != md->block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		DMERR("changing the data block size (from %u to %llu) is not supported",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		      le32_to_cpu(disk->data_block_size), md->block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	r = dm_tm_open_with_sm(md->bm, SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			       disk->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 			       sizeof(disk->metadata_space_map_root),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			       &md->tm, &md->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		DMERR("dm_tm_open_with_sm failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	setup_infos(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	md->nr_blocks = le32_to_cpu(disk->nr_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	md->current_era = le32_to_cpu(disk->current_era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	ws_unpack(&disk->current_writeset, &md->current_writeset->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	md->writeset_tree_root = le64_to_cpu(disk->writeset_tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	md->era_array_root = le64_to_cpu(disk->era_array_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	md->metadata_snap = le64_to_cpu(disk->metadata_snap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	md->archived_writesets = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) static int open_or_format_metadata(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 				   bool may_format)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	bool unformatted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	r = superblock_all_zeroes(md->bm, &unformatted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (unformatted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		return may_format ? format_metadata(md) : -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	return open_metadata(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) static int create_persistent_data_objects(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 					  bool may_format)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	md->bm = dm_block_manager_create(md->bdev, DM_ERA_METADATA_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 					 ERA_MAX_CONCURRENT_LOCKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	if (IS_ERR(md->bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		DMERR("could not create block manager");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		return PTR_ERR(md->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	r = open_or_format_metadata(md, may_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		dm_block_manager_destroy(md->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) static void destroy_persistent_data_objects(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	dm_sm_destroy(md->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	dm_tm_destroy(md->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	dm_block_manager_destroy(md->bm);
^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)  * This waits until all era_map threads have picked up the new filter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) static void swap_writeset(struct era_metadata *md, struct writeset *new_writeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	rcu_assign_pointer(md->current_writeset, new_writeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658)  * Writesets get 'digested' into the main era array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660)  * We're using a coroutine here so the worker thread can do the digestion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661)  * thus avoiding synchronisation of the metadata.  Digesting a whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662)  * writeset in one go would cause too much latency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) struct digest {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	uint32_t era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	unsigned nr_bits, current_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	struct writeset_metadata writeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	__le32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	struct dm_disk_bitset info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	int (*step)(struct era_metadata *, struct digest *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) static int metadata_digest_lookup_writeset(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 					   struct digest *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) static int metadata_digest_remove_writeset(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 					   struct digest *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	uint64_t key = d->era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	r = dm_btree_remove(&md->writeset_tree_info, md->writeset_tree_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			    &key, &md->writeset_tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		DMERR("%s: dm_btree_remove failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	d->step = metadata_digest_lookup_writeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) #define INSERTS_PER_STEP 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) static int metadata_digest_transcribe_writeset(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 					       struct digest *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	bool marked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	unsigned b, e = min(d->current_bit + INSERTS_PER_STEP, d->nr_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	for (b = d->current_bit; b < e; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		r = writeset_marked_on_disk(&d->info, &d->writeset, b, &marked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			DMERR("%s: writeset_marked_on_disk failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			return r;
^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) 		if (!marked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		__dm_bless_for_disk(&d->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		r = dm_array_set_value(&md->era_array_info, md->era_array_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 				       b, &d->value, &md->era_array_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 			DMERR("%s: dm_array_set_value failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		}
^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) 	if (b == d->nr_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		d->step = metadata_digest_remove_writeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		d->current_bit = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	return 0;
^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) static int metadata_digest_lookup_writeset(struct era_metadata *md,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 					   struct digest *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	uint64_t key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	struct writeset_disk disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	r = dm_btree_find_lowest_key(&md->writeset_tree_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 				     md->writeset_tree_root, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	d->era = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	r = dm_btree_lookup(&md->writeset_tree_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 			    md->writeset_tree_root, &key, &disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		if (r == -ENODATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			d->step = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		DMERR("%s: dm_btree_lookup failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	ws_unpack(&disk, &d->writeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	d->value = cpu_to_le32(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	 * We initialise another bitset info to avoid any caching side effects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	 * with the previous one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	dm_disk_bitset_init(md->tm, &d->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	d->nr_bits = min(d->writeset.nr_bits, md->nr_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	d->current_bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	d->step = metadata_digest_transcribe_writeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) static int metadata_digest_start(struct era_metadata *md, struct digest *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	if (d->step)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	memset(d, 0, sizeof(*d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	d->step = metadata_digest_lookup_writeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784)  * High level metadata interface.  Target methods should use these, and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785)  * the lower level ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786)  *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) static struct era_metadata *metadata_open(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 					  sector_t block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 					  bool may_format)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	struct era_metadata *md = kzalloc(sizeof(*md), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	md->bdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	md->block_size = block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	md->writesets[0].md.root = INVALID_WRITESET_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	md->writesets[1].md.root = INVALID_WRITESET_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	md->current_writeset = &md->writesets[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	r = create_persistent_data_objects(md, may_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		kfree(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		return ERR_PTR(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	return md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) static void metadata_close(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	writeset_free(&md->writesets[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	writeset_free(&md->writesets[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	destroy_persistent_data_objects(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	kfree(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) static bool valid_nr_blocks(dm_block_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	 * dm_bitset restricts us to 2^32.  test_bit & co. restrict us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	 * further to 2^31 - 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	return n < (1ull << 31);
^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) static int metadata_resize(struct era_metadata *md, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	dm_block_t *new_size = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	__le32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	if (!valid_nr_blocks(*new_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		DMERR("Invalid number of origin blocks %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		      (unsigned long long) *new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		return -EINVAL;
^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) 	writeset_free(&md->writesets[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	writeset_free(&md->writesets[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	r = writeset_alloc(&md->writesets[0], *new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		DMERR("%s: writeset_alloc failed for writeset 0", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	r = writeset_alloc(&md->writesets[1], *new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		DMERR("%s: writeset_alloc failed for writeset 1", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		writeset_free(&md->writesets[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	value = cpu_to_le32(0u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	__dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	r = dm_array_resize(&md->era_array_info, md->era_array_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			    md->nr_blocks, *new_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			    &value, &md->era_array_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		DMERR("%s: dm_array_resize failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		writeset_free(&md->writesets[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		writeset_free(&md->writesets[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	md->nr_blocks = *new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) static int metadata_era_archive(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	uint64_t keys[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	struct writeset_disk value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			    &md->current_writeset->md.root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		DMERR("%s: dm_bitset_flush failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	ws_pack(&md->current_writeset->md, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	keys[0] = md->current_era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	__dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			    keys, &value, &md->writeset_tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		DMERR("%s: couldn't insert writeset into btree", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		/* FIXME: fail mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	md->current_writeset->md.root = INVALID_WRITESET_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	md->archived_writesets = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	return 0;
^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 struct writeset *next_writeset(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	return (md->current_writeset == &md->writesets[0]) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		&md->writesets[1] : &md->writesets[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) static int metadata_new_era(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	struct writeset *new_writeset = next_writeset(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	r = writeset_init(&md->bitset_info, new_writeset, md->nr_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		DMERR("%s: writeset_init failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		return r;
^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) 	swap_writeset(md, new_writeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	md->current_era++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) static int metadata_era_rollover(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		r = metadata_era_archive(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			DMERR("%s: metadata_archive_era failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			/* FIXME: fail mode? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	r = metadata_new_era(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		DMERR("%s: new era failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		/* FIXME: fail mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) static bool metadata_current_marked(struct era_metadata *md, dm_block_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	bool r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	struct writeset *ws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	ws = rcu_dereference(md->current_writeset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	r = writeset_marked(ws, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) static int metadata_commit(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 				    &md->current_writeset->md.root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			DMERR("%s: bitset flush failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	r = dm_tm_pre_commit(md->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		DMERR("%s: pre commit failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	r = save_sm_root(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		DMERR("%s: save_sm_root failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	r = superblock_lock(md, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		DMERR("%s: superblock lock failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	prepare_superblock(md, dm_block_data(sblock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	return dm_tm_commit(md->tm, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) static int metadata_checkpoint(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	 * For now we just rollover, but later I want to put a check in to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	 * avoid this if the filter is still pretty fresh.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	return metadata_era_rollover(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)  * Metadata snapshots allow userland to access era data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) static int metadata_take_snap(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	int r, inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	struct dm_block *clone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	if (md->metadata_snap != SUPERBLOCK_LOCATION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		DMERR("%s: metadata snapshot already exists", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	r = metadata_era_rollover(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		DMERR("%s: era rollover failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	r = metadata_commit(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		DMERR("%s: pre commit failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	r = dm_sm_inc_block(md->sm, SUPERBLOCK_LOCATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		DMERR("%s: couldn't increment superblock", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	r = dm_tm_shadow_block(md->tm, SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			       &sb_validator, &clone, &inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		DMERR("%s: couldn't shadow superblock", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		dm_sm_dec_block(md->sm, SUPERBLOCK_LOCATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	BUG_ON(!inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	r = dm_sm_inc_block(md->sm, md->writeset_tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		DMERR("%s: couldn't inc writeset tree root", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		dm_tm_unlock(md->tm, clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	r = dm_sm_inc_block(md->sm, md->era_array_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		DMERR("%s: couldn't inc era tree root", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		dm_sm_dec_block(md->sm, md->writeset_tree_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		dm_tm_unlock(md->tm, clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	md->metadata_snap = dm_block_location(clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	dm_tm_unlock(md->tm, clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) static int metadata_drop_snap(struct era_metadata *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	dm_block_t location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	struct dm_block *clone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	struct superblock_disk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	if (md->metadata_snap == SUPERBLOCK_LOCATION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		DMERR("%s: no snap to drop", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	r = dm_tm_read_lock(md->tm, md->metadata_snap, &sb_validator, &clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		DMERR("%s: couldn't read lock superblock clone", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	 * Whatever happens now we'll commit with no record of the metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	 * snap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	md->metadata_snap = SUPERBLOCK_LOCATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	disk = dm_block_data(clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	r = dm_btree_del(&md->writeset_tree_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			 le64_to_cpu(disk->writeset_tree_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		DMERR("%s: error deleting writeset tree clone", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		dm_tm_unlock(md->tm, clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	r = dm_array_del(&md->era_array_info, le64_to_cpu(disk->era_array_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		DMERR("%s: error deleting era array clone", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		dm_tm_unlock(md->tm, clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	location = dm_block_location(clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	dm_tm_unlock(md->tm, clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	return dm_sm_dec_block(md->sm, location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) struct metadata_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	dm_block_t used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	dm_block_t total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	dm_block_t snap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	uint32_t era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) static int metadata_get_stats(struct era_metadata *md, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	struct metadata_stats *s = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	dm_block_t nr_free, nr_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	r = dm_sm_get_nr_free(md->sm, &nr_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		DMERR("dm_sm_get_nr_free returned %d", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	r = dm_sm_get_nr_blocks(md->sm, &nr_total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		DMERR("dm_pool_get_metadata_dev_size returned %d", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	s->used = nr_total - nr_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	s->total = nr_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	s->snap = md->metadata_snap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	s->era = md->current_era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) struct era {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	struct dm_target *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	struct dm_dev *metadata_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	struct dm_dev *origin_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	dm_block_t nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	uint32_t sectors_per_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	int sectors_per_block_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	struct era_metadata *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	struct workqueue_struct *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	struct work_struct worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	spinlock_t deferred_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	struct bio_list deferred_bios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	spinlock_t rpc_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	struct list_head rpc_calls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	struct digest digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	atomic_t suspended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) struct rpc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	int (*fn0)(struct era_metadata *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	int (*fn1)(struct era_metadata *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	void *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	struct completion complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)  * Remapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)  *---------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) static bool block_size_is_power_of_two(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	return era->sectors_per_block_shift >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) static dm_block_t get_block(struct era *era, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	sector_t block_nr = bio->bi_iter.bi_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	if (!block_size_is_power_of_two(era))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		(void) sector_div(block_nr, era->sectors_per_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		block_nr >>= era->sectors_per_block_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	return block_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) static void remap_to_origin(struct era *era, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	bio_set_dev(bio, era->origin_dev->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)  * Worker thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)  *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) static void wake_worker(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	if (!atomic_read(&era->suspended))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		queue_work(era->wq, &era->worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) static void process_old_eras(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	if (!era->digest.step)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	r = era->digest.step(era->md, &era->digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		DMERR("%s: digest step failed, stopping digestion", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		era->digest.step = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	} else if (era->digest.step)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		wake_worker(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) static void process_deferred_bios(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	struct bio_list deferred_bios, marked_bios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	struct blk_plug plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	bool commit_needed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	bool failed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	struct writeset *ws = era->md->current_writeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	bio_list_init(&deferred_bios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	bio_list_init(&marked_bios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	spin_lock(&era->deferred_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	bio_list_merge(&deferred_bios, &era->deferred_bios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	bio_list_init(&era->deferred_bios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	spin_unlock(&era->deferred_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	if (bio_list_empty(&deferred_bios))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	while ((bio = bio_list_pop(&deferred_bios))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		r = writeset_test_and_set(&era->md->bitset_info, ws,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 					  get_block(era, bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			 * This is bad news, we need to rollback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			 * FIXME: finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			failed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		} else if (r == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			commit_needed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		bio_list_add(&marked_bios, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	if (commit_needed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		r = metadata_commit(era->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			failed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	if (failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		while ((bio = bio_list_pop(&marked_bios)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 			bio_io_error(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		while ((bio = bio_list_pop(&marked_bios))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 			 * Only update the in-core writeset if the on-disk one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 			 * was updated too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			if (commit_needed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 				set_bit(get_block(era, bio), ws->bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			submit_bio_noacct(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) static void process_rpc_calls(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	bool need_commit = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	struct list_head calls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	struct rpc *rpc, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	INIT_LIST_HEAD(&calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	spin_lock(&era->rpc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	list_splice_init(&era->rpc_calls, &calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	spin_unlock(&era->rpc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	list_for_each_entry_safe(rpc, tmp, &calls, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		rpc->result = rpc->fn0 ? rpc->fn0(era->md) : rpc->fn1(era->md, rpc->arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		need_commit = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	if (need_commit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		r = metadata_commit(era->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 			list_for_each_entry_safe(rpc, tmp, &calls, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 				rpc->result = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	list_for_each_entry_safe(rpc, tmp, &calls, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		complete(&rpc->complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) static void kick_off_digest(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	if (era->md->archived_writesets) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		era->md->archived_writesets = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		metadata_digest_start(era->md, &era->digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) static void do_work(struct work_struct *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	struct era *era = container_of(ws, struct era, worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	kick_off_digest(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	process_old_eras(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	process_deferred_bios(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	process_rpc_calls(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) static void defer_bio(struct era *era, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	spin_lock(&era->deferred_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	bio_list_add(&era->deferred_bios, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	spin_unlock(&era->deferred_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	wake_worker(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)  * Make an rpc call to the worker to change the metadata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) static int perform_rpc(struct era *era, struct rpc *rpc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	rpc->result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	init_completion(&rpc->complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	spin_lock(&era->rpc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	list_add(&rpc->list, &era->rpc_calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	spin_unlock(&era->rpc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	wake_worker(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	wait_for_completion(&rpc->complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	return rpc->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) static int in_worker0(struct era *era, int (*fn)(struct era_metadata *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	struct rpc rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	rpc.fn0 = fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	rpc.fn1 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	return perform_rpc(era, &rpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) static int in_worker1(struct era *era,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		      int (*fn)(struct era_metadata *, void *), void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	struct rpc rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	rpc.fn0 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	rpc.fn1 = fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	rpc.arg = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	return perform_rpc(era, &rpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) static void start_worker(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	atomic_set(&era->suspended, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) static void stop_worker(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	atomic_set(&era->suspended, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	flush_workqueue(era->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)  * Target methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)  *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) static void era_destroy(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	if (era->md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		metadata_close(era->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	if (era->wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		destroy_workqueue(era->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	if (era->origin_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		dm_put_device(era->ti, era->origin_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	if (era->metadata_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		dm_put_device(era->ti, era->metadata_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	kfree(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) static dm_block_t calc_nr_blocks(struct era *era)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	return dm_sector_div_up(era->ti->len, era->sectors_per_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) static bool valid_block_size(dm_block_t block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	bool greater_than_zero = block_size > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	bool multiple_of_min_block_size = (block_size & (MIN_BLOCK_SIZE - 1)) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	return greater_than_zero && multiple_of_min_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)  * <metadata dev> <data dev> <data block size (sectors)>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) static int era_ctr(struct dm_target *ti, unsigned argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	char dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	struct era *era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	struct era_metadata *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	if (argc != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		ti->error = "Invalid argument count";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	era = kzalloc(sizeof(*era), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	if (!era) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		ti->error = "Error allocating era structure";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	era->ti = ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &era->metadata_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		ti->error = "Error opening metadata device";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		era_destroy(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &era->origin_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		ti->error = "Error opening data device";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		era_destroy(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	r = sscanf(argv[2], "%u%c", &era->sectors_per_block, &dummy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	if (r != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		ti->error = "Error parsing block size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		era_destroy(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	r = dm_set_target_max_io_len(ti, era->sectors_per_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		ti->error = "could not set max io len";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		era_destroy(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	if (!valid_block_size(era->sectors_per_block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		ti->error = "Invalid block size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		era_destroy(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	if (era->sectors_per_block & (era->sectors_per_block - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		era->sectors_per_block_shift = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		era->sectors_per_block_shift = __ffs(era->sectors_per_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	if (IS_ERR(md)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		ti->error = "Error reading metadata";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		era_destroy(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		return PTR_ERR(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	era->md = md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	era->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	if (!era->wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		ti->error = "could not create workqueue for metadata object";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		era_destroy(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	INIT_WORK(&era->worker, do_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	spin_lock_init(&era->deferred_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	bio_list_init(&era->deferred_bios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	spin_lock_init(&era->rpc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	INIT_LIST_HEAD(&era->rpc_calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	ti->private = era;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	ti->num_flush_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	ti->flush_supported = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	ti->num_discard_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) static void era_dtr(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	era_destroy(ti->private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) static int era_map(struct dm_target *ti, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct era *era = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	dm_block_t block = get_block(era, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	 * All bios get remapped to the origin device.  We do this now, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	 * it may not get issued until later.  Depending on whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	 * block is marked in this era.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	remap_to_origin(era, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	 * REQ_PREFLUSH bios carry no data, so we're not interested in them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	if (!(bio->bi_opf & REQ_PREFLUSH) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	    (bio_data_dir(bio) == WRITE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	    !metadata_current_marked(era->md, block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		defer_bio(era, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		return DM_MAPIO_SUBMITTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	return DM_MAPIO_REMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) static void era_postsuspend(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	struct era *era = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	r = in_worker0(era, metadata_era_archive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		DMERR("%s: couldn't archive current era", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		/* FIXME: fail mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	stop_worker(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) static int era_preresume(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	struct era *era = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	dm_block_t new_size = calc_nr_blocks(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	if (era->nr_blocks != new_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		r = metadata_resize(era->md, &new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 			DMERR("%s: metadata_resize failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		r = metadata_commit(era->md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 			DMERR("%s: metadata_commit failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		era->nr_blocks = new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	start_worker(era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	r = in_worker0(era, metadata_era_rollover);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		DMERR("%s: metadata_era_rollover failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605)  * Status format:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)  * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)  * <current era> <held metadata root | '-'>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) static void era_status(struct dm_target *ti, status_type_t type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		       unsigned status_flags, char *result, unsigned maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	struct era *era = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	ssize_t sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	struct metadata_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	char buf[BDEVNAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	case STATUSTYPE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		r = in_worker1(era, metadata_get_stats, &stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		DMEMIT("%u %llu/%llu %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		       (unsigned) (DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		       (unsigned long long) stats.used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		       (unsigned long long) stats.total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		       (unsigned) stats.era);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		if (stats.snap != SUPERBLOCK_LOCATION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 			DMEMIT(" %llu", stats.snap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			DMEMIT(" -");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	case STATUSTYPE_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		format_dev_t(buf, era->metadata_dev->bdev->bd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		DMEMIT("%s ", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		format_dev_t(buf, era->origin_dev->bdev->bd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		DMEMIT("%s %u", buf, era->sectors_per_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	DMEMIT("Error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) static int era_message(struct dm_target *ti, unsigned argc, char **argv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		       char *result, unsigned maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	struct era *era = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	if (argc != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		DMERR("incorrect number of message arguments");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	if (!strcasecmp(argv[0], "checkpoint"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		return in_worker0(era, metadata_checkpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	if (!strcasecmp(argv[0], "take_metadata_snap"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		return in_worker0(era, metadata_take_snap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	if (!strcasecmp(argv[0], "drop_metadata_snap"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		return in_worker0(era, metadata_drop_snap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	DMERR("unsupported message '%s'", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) static sector_t get_dev_size(struct dm_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) static int era_iterate_devices(struct dm_target *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 			       iterate_devices_callout_fn fn, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	struct era *era = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	return fn(ti, era->origin_dev, 0, get_dev_size(era->origin_dev), data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) static void era_io_hints(struct dm_target *ti, struct queue_limits *limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	struct era *era = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	 * If the system-determined stacked limits are compatible with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	 * era device's blocksize (io_opt is a factor) do not override them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	if (io_opt_sectors < era->sectors_per_block ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	    do_div(io_opt_sectors, era->sectors_per_block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		blk_limits_io_min(limits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		blk_limits_io_opt(limits, era->sectors_per_block << SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) static struct target_type era_target = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	.name = "era",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	.version = {1, 0, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	.ctr = era_ctr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	.dtr = era_dtr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	.map = era_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	.postsuspend = era_postsuspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	.preresume = era_preresume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	.status = era_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	.message = era_message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	.iterate_devices = era_iterate_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	.io_hints = era_io_hints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) static int __init dm_era_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	r = dm_register_target(&era_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		DMERR("era target registration failed: %d", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) static void __exit dm_era_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	dm_unregister_target(&era_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) module_init(dm_era_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) module_exit(dm_era_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) MODULE_DESCRIPTION(DM_NAME " era target");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) MODULE_LICENSE("GPL");