Orange Pi5 kernel

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

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * Copyright (C) 2012 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * This file is released under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "dm-cache-metadata.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "persistent-data/dm-array.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "persistent-data/dm-bitset.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "persistent-data/dm-space-map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "persistent-data/dm-space-map-disk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "persistent-data/dm-transaction-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/device-mapper.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #define DM_MSG_PREFIX   "cache metadata"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define CACHE_SUPERBLOCK_MAGIC 06142003
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define CACHE_SUPERBLOCK_LOCATION 0
^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)  * defines a range of metadata versions that this module can handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define MIN_CACHE_VERSION 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define MAX_CACHE_VERSION 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  *  3 for btree insert +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  *  2 for btree lookup used within space map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define CACHE_MAX_CONCURRENT_LOCKS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define SPACE_MAP_ROOT_SIZE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) enum superblock_flag_bits {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	/* for spotting crashes that would invalidate the dirty bitset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	CLEAN_SHUTDOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	/* metadata must be checked using the tools */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	NEEDS_CHECK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * Each mapping from cache block -> origin block carries a set of flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) enum mapping_bits {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	 * A valid mapping.  Because we're using an array we clear this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	 * flag for an non existant mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	M_VALID = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	 * The data on the cache is different from that on the origin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	 * This flag is only used by metadata format 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	M_DIRTY = 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) struct cache_disk_superblock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	__le32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	__le32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	__le64 blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	__u8 uuid[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	__le64 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	__le32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	__u8 policy_name[CACHE_POLICY_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	__le32 policy_hint_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	__le64 mapping_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	__le64 hint_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	__le64 discard_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	__le64 discard_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	__le64 discard_nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	__le32 data_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	__le32 metadata_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	__le32 cache_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	__le32 compat_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	__le32 compat_ro_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	__le32 incompat_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	__le32 read_hits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	__le32 read_misses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	__le32 write_hits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	__le32 write_misses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	__le32 policy_version[CACHE_POLICY_VERSION_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	 * Metadata format 2 fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	__le64 dirty_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) struct dm_cache_metadata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	refcount_t ref_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	unsigned version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct dm_block_manager *bm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	struct dm_space_map *metadata_sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	struct dm_transaction_manager *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	struct dm_array_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	struct dm_array_info hint_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	struct dm_disk_bitset discard_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	struct rw_semaphore root_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	dm_block_t root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	dm_block_t hint_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	dm_block_t discard_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	sector_t discard_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	dm_dblock_t discard_nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	sector_t data_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	dm_cblock_t cache_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	bool changed:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	bool clean_when_opened:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	char policy_name[CACHE_POLICY_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	unsigned policy_version[CACHE_POLICY_VERSION_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	size_t policy_hint_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	struct dm_cache_statistics stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	 * Reading the space map root can fail, so we read it into this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	 * buffer before the superblock is locked and updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	 * Set if a transaction has to be aborted but the attempt to roll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	 * back to the previous (good) transaction failed.  The only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	 * metadata operation permissible in this state is the closing of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	 * the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	bool fail_io: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) 	 * Metadata format 2 fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	dm_block_t dirty_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	struct dm_disk_bitset dirty_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	 * These structures are used when loading metadata.  They're too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	 * big to put on the stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	struct dm_array_cursor mapping_cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	struct dm_array_cursor hint_cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	struct dm_bitset_cursor dirty_cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) /*-------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * superblock validator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  *-----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) #define SUPERBLOCK_CSUM_XOR 9031977
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static void sb_prepare_for_write(struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 				 struct dm_block *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 				 size_t sb_block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	struct cache_disk_superblock *disk_super = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	disk_super->blocknr = cpu_to_le64(dm_block_location(b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 						      sb_block_size - sizeof(__le32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 						      SUPERBLOCK_CSUM_XOR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) static int check_metadata_version(struct cache_disk_superblock *disk_super)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	uint32_t metadata_version = le32_to_cpu(disk_super->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		      metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static int sb_check(struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		    struct dm_block *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		    size_t sb_block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	struct cache_disk_superblock *disk_super = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	__le32 csum_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		DMERR("sb_check failed: blocknr %llu: wanted %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		      le64_to_cpu(disk_super->blocknr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		      (unsigned long long)dm_block_location(b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		return -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		DMERR("sb_check failed: magic %llu: wanted %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		      le64_to_cpu(disk_super->magic),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		      (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 					     sb_block_size - sizeof(__le32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 					     SUPERBLOCK_CSUM_XOR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	if (csum_le != disk_super->csum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		DMERR("sb_check failed: csum %u: wanted %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		      le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	return check_metadata_version(disk_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) static struct dm_block_validator sb_validator = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	.name = "superblock",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	.prepare_for_write = sb_prepare_for_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	.check = sb_check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) static int superblock_read_lock(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 				struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			       &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) static int superblock_lock_zero(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 				struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 				     &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) static int superblock_lock(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 			   struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 				&sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	struct dm_block *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	__le64 *data_le, zero = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	 * We can't use a validator here - it may be all zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	data_le = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	*result = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	for (i = 0; i < sb_block_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		if (data_le[i] != zero) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			*result = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	dm_bm_unlock(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) static void __setup_mapping_info(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	struct dm_btree_value_type vt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	vt.context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	vt.size = sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	vt.inc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	vt.dec = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	vt.equal = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	dm_array_info_init(&cmd->info, cmd->tm, &vt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	if (cmd->policy_hint_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		vt.size = sizeof(__le32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) static int __save_sm_root(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	size_t metadata_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			       metadata_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) static void __copy_sm_root(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 			   struct cache_disk_superblock *disk_super)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	memcpy(&disk_super->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	       &cmd->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	       sizeof(cmd->metadata_space_map_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) static bool separate_dirty_bits(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	return cmd->version >= 2;
^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) static int __write_initial_superblock(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	struct cache_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	/* FIXME: see if we can lose the max sectors limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	r = dm_tm_pre_commit(cmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	 * dm_sm_copy_root() can fail.  So we need to do it before we start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	 * updating the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	r = __save_sm_root(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	r = superblock_lock_zero(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	disk_super->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	disk_super->version = cpu_to_le32(cmd->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	disk_super->policy_hint_size = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	__copy_sm_root(cmd, disk_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	disk_super->mapping_root = cpu_to_le64(cmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	disk_super->hint_root = cpu_to_le64(cmd->hint_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	disk_super->discard_root = cpu_to_le64(cmd->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	disk_super->cache_blocks = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	disk_super->read_hits = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	disk_super->read_misses = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	disk_super->write_hits = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	disk_super->write_misses = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if (separate_dirty_bits(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	return dm_tm_commit(cmd->tm, sblock);
^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 __format_metadata(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 				 &cmd->tm, &cmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		DMERR("tm_create_with_sm failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	__setup_mapping_info(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	r = dm_array_empty(&cmd->info, &cmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	if (separate_dirty_bits(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		r = dm_bitset_empty(&cmd->dirty_info, &cmd->dirty_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	cmd->discard_block_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	cmd->discard_nr_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	r = __write_initial_superblock(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	cmd->clean_when_opened = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	dm_tm_destroy(cmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	dm_sm_destroy(cmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) static int __check_incompat_features(struct cache_disk_superblock *disk_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 				     struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	uint32_t incompat_flags, features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	incompat_flags = le32_to_cpu(disk_super->incompat_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	features = incompat_flags & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	if (features) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		DMERR("could not access metadata due to unsupported optional features (%lx).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		      (unsigned long)features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	}
^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) 	 * Check for read-only metadata to skip the following RDWR checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	if (get_disk_ro(cmd->bdev->bd_disk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	if (features) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		      (unsigned long)features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	return 0;
^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 __open_metadata(struct dm_cache_metadata *cmd)
^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) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	struct cache_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	unsigned long sb_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	r = superblock_read_lock(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		DMERR("couldn't read lock superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		return r;
^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) 	disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	/* Verify the data block size hasn't changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		DMERR("changing the data block size (from %u to %llu) is not supported",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		      le32_to_cpu(disk_super->data_block_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		      (unsigned long long)cmd->data_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	r = __check_incompat_features(disk_super, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			       disk_super->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			       sizeof(disk_super->metadata_space_map_root),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 			       &cmd->tm, &cmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		DMERR("tm_open_with_sm failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	__setup_mapping_info(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	sb_flags = le32_to_cpu(disk_super->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 				     bool format_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	bool unformatted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	r = __superblock_all_zeroes(cmd->bm, &unformatted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if (unformatted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		return format_device ? __format_metadata(cmd) : -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	return __open_metadata(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 					    bool may_format_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 					  CACHE_MAX_CONCURRENT_LOCKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (IS_ERR(cmd->bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		DMERR("could not create block manager");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		r = PTR_ERR(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		cmd->bm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	r = __open_or_format_metadata(cmd, may_format_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		dm_block_manager_destroy(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		cmd->bm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	dm_sm_destroy(cmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	dm_tm_destroy(cmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	dm_block_manager_destroy(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) typedef unsigned long (*flags_mutator)(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) static void update_flags(struct cache_disk_superblock *disk_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 			 flags_mutator mutator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	disk_super->flags = cpu_to_le32(sb_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) static unsigned long set_clean_shutdown(unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	set_bit(CLEAN_SHUTDOWN, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) static unsigned long clear_clean_shutdown(unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	clear_bit(CLEAN_SHUTDOWN, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) static void read_superblock_fields(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 				   struct cache_disk_superblock *disk_super)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	cmd->version = le32_to_cpu(disk_super->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	cmd->flags = le32_to_cpu(disk_super->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	cmd->root = le64_to_cpu(disk_super->mapping_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	cmd->hint_root = le64_to_cpu(disk_super->hint_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	cmd->discard_root = le64_to_cpu(disk_super->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (separate_dirty_bits(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		cmd->dirty_root = le64_to_cpu(disk_super->dirty_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	cmd->changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  * The mutator updates the superblock flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 				     flags_mutator mutator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	struct cache_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	r = superblock_lock(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	update_flags(disk_super, mutator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	read_superblock_fields(cmd, disk_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	return dm_bm_flush(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) static int __begin_transaction(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	struct cache_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	 * We re-read the superblock every time.  Shouldn't need to do this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	 * really.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	r = superblock_read_lock(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	read_superblock_fields(cmd, disk_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) static int __commit_transaction(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 				flags_mutator mutator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	struct cache_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	if (separate_dirty_bits(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		r = dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 				    &cmd->dirty_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			    &cmd->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	r = dm_tm_pre_commit(cmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	r = __save_sm_root(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	r = superblock_lock(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	disk_super->flags = cpu_to_le32(cmd->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	if (mutator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		update_flags(disk_super, mutator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	disk_super->mapping_root = cpu_to_le64(cmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	if (separate_dirty_bits(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	disk_super->hint_root = cpu_to_le64(cmd->hint_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	disk_super->discard_root = cpu_to_le64(cmd->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	disk_super->policy_hint_size = cpu_to_le32(cmd->policy_hint_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	__copy_sm_root(cmd, disk_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	return dm_tm_commit(cmd->tm, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^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)  * The mappings are held in a dm-array that has 64-bit values stored in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723)  * little-endian format.  The index is the cblock, the high 48bits of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724)  * value are the oblock and the low 16 bit the flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) #define FLAGS_MASK ((1 << 16) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) static __le64 pack_value(dm_oblock_t block, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	uint64_t value = from_oblock(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	value <<= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	value = value | (flags & FLAGS_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	return cpu_to_le64(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	uint64_t value = le64_to_cpu(value_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	uint64_t b = value >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	*block = to_oblock(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	*flags = value & FLAGS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 					       sector_t data_block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 					       bool may_format_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 					       size_t policy_hint_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 					       unsigned metadata_version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	struct dm_cache_metadata *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	if (!cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		DMERR("could not allocate metadata struct");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	cmd->version = metadata_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	refcount_set(&cmd->ref_count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	init_rwsem(&cmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	cmd->bdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	cmd->data_block_size = data_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	cmd->cache_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	cmd->policy_hint_size = policy_hint_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	cmd->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	cmd->fail_io = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	r = __create_persistent_data_objects(cmd, may_format_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		return ERR_PTR(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	r = __begin_transaction_flags(cmd, clear_clean_shutdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		dm_cache_metadata_close(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		return ERR_PTR(r);
^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) 	return cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787)  * We keep a little list of ref counted metadata objects to prevent two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788)  * different target instances creating separate bufio instances.  This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789)  * an issue if a table is reloaded before the suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) static DEFINE_MUTEX(table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) static LIST_HEAD(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) static struct dm_cache_metadata *lookup(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	struct dm_cache_metadata *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	list_for_each_entry(cmd, &table, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		if (cmd->bdev == bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			refcount_inc(&cmd->ref_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			return cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 						sector_t data_block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 						bool may_format_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 						size_t policy_hint_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 						unsigned metadata_version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	struct dm_cache_metadata *cmd, *cmd2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	mutex_lock(&table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	cmd = lookup(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	mutex_unlock(&table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		return cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	cmd = metadata_open(bdev, data_block_size, may_format_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 			    policy_hint_size, metadata_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	if (!IS_ERR(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		mutex_lock(&table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		cmd2 = lookup(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		if (cmd2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 			mutex_unlock(&table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 			__destroy_persistent_data_objects(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			return cmd2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		list_add(&cmd->list, &table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		mutex_unlock(&table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	return cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	if (cmd->data_block_size != data_block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		DMERR("data_block_size (%llu) different from that in metadata (%llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		      (unsigned long long) data_block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		      (unsigned long long) cmd->data_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 						 sector_t data_block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 						 bool may_format_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 						 size_t policy_hint_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 						 unsigned metadata_version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size, may_format_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 						       policy_hint_size, metadata_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		dm_cache_metadata_close(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	return cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	if (refcount_dec_and_test(&cmd->ref_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		mutex_lock(&table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		list_del(&cmd->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		mutex_unlock(&table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		if (!cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			__destroy_persistent_data_objects(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883)  * Checks that the given cache block is either unmapped or clean.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) static int block_clean_combined_dirty(struct dm_cache_metadata *cmd, dm_cblock_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 				      bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	__le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	dm_oblock_t ob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	unsigned flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	unpack_value(value, &ob, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	*result = !((flags & M_VALID) && (flags & M_DIRTY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) static int blocks_are_clean_combined_dirty(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 					   dm_cblock_t begin, dm_cblock_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 					   bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	*result = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	while (begin != end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		r = block_clean_combined_dirty(cmd, begin, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			DMERR("block_clean_combined_dirty failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (!*result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			DMERR("cache block %llu is dirty",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			      (unsigned long long) from_cblock(begin));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		begin = to_cblock(from_cblock(begin) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) static int blocks_are_clean_separate_dirty(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 					   dm_cblock_t begin, dm_cblock_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 					   bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	bool dirty_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	*result = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	if (from_cblock(cmd->cache_blocks) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		/* Nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 				   from_cblock(cmd->cache_blocks), &cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		DMERR("%s: dm_bitset_cursor_begin for dirty failed", __func__);
^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) 	r = dm_bitset_cursor_skip(&cmd->dirty_cursor, from_cblock(begin));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		DMERR("%s: dm_bitset_cursor_skip for dirty failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		dm_bitset_cursor_end(&cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	while (begin != end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		 * We assume that unmapped blocks have their dirty bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		 * cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		dirty_flag = dm_bitset_cursor_get_value(&cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		if (dirty_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			DMERR("%s: cache block %llu is dirty", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			      (unsigned long long) from_cblock(begin));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			dm_bitset_cursor_end(&cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			*result = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		begin = to_cblock(from_cblock(begin) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		if (begin == end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		r = dm_bitset_cursor_next(&cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			DMERR("%s: dm_bitset_cursor_next for dirty failed", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			dm_bitset_cursor_end(&cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	dm_bitset_cursor_end(&cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 					dm_cblock_t begin, dm_cblock_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 					bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (separate_dirty_bits(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		return blocks_are_clean_separate_dirty(cmd, begin, end, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		return blocks_are_clean_combined_dirty(cmd, begin, end, result);
^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) static bool cmd_write_lock(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	down_write(&cmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		up_write(&cmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) #define WRITE_LOCK(cmd)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	do {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		if (!cmd_write_lock((cmd)))	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			return -EINVAL;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	} while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) #define WRITE_LOCK_VOID(cmd)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	do {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		if (!cmd_write_lock((cmd)))	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			return;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	} while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) #define WRITE_UNLOCK(cmd) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	up_write(&(cmd)->root_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) static bool cmd_read_lock(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	down_read(&cmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	if (cmd->fail_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		up_read(&cmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) #define READ_LOCK(cmd)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	do {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		if (!cmd_read_lock((cmd)))	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			return -EINVAL;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	} while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) #define READ_LOCK_VOID(cmd)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	do {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		if (!cmd_read_lock((cmd)))	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 			return;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	} while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) #define READ_UNLOCK(cmd) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	up_read(&(cmd)->root_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	bool clean;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	__le64 null_mapping = pack_value(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	__dm_bless_for_disk(&null_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			__dm_unbless_for_disk(&null_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		if (!clean) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			DMERR("unable to shrink cache due to dirty blocks");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			__dm_unbless_for_disk(&null_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			    from_cblock(new_cache_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			    &null_mapping, &cmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	if (separate_dirty_bits(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		r = dm_bitset_resize(&cmd->dirty_info, cmd->dirty_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 				     from_cblock(cmd->cache_blocks), from_cblock(new_cache_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 				     false, &cmd->dirty_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			goto out;
^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) 	cmd->cache_blocks = new_cache_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	cmd->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 				   sector_t discard_block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 				   dm_dblock_t new_nr_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	r = dm_bitset_resize(&cmd->discard_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			     cmd->discard_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			     from_dblock(cmd->discard_nr_blocks),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			     from_dblock(new_nr_entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			     false, &cmd->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		cmd->discard_block_size = discard_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		cmd->discard_nr_blocks = new_nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	cmd->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 				 from_dblock(b), &cmd->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 				   from_dblock(b), &cmd->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) static int __discard(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		     dm_dblock_t dblock, bool discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	cmd->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) int dm_cache_set_discard(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 			 dm_dblock_t dblock, bool discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	r = __discard(cmd, dblock, discard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	return r;
^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) static int __load_discards(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 			   load_discard_fn fn, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	uint32_t b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	struct dm_bitset_cursor c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	if (from_dblock(cmd->discard_nr_blocks) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		/* nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	if (cmd->clean_when_opened) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, &cmd->discard_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		r = dm_bitset_cursor_begin(&cmd->discard_info, cmd->discard_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 					   from_dblock(cmd->discard_nr_blocks), &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		for (b = 0; ; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 			r = fn(context, cmd->discard_block_size, to_dblock(b),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			       dm_bitset_cursor_get_value(&c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			if (b >= (from_dblock(cmd->discard_nr_blocks) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			r = dm_bitset_cursor_next(&c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 				break;
^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) 		dm_bitset_cursor_end(&c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			r = fn(context, cmd->discard_block_size, to_dblock(b), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) int dm_cache_load_discards(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			   load_discard_fn fn, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	READ_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	r = __load_discards(cmd, fn, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	return r;
^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) int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	READ_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	*result = cmd->cache_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	return 0;
^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 int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
^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) 	__le64 value = pack_value(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	__dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			       &value, &cmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	cmd->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	r = __remove(cmd, cblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) static int __insert(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		    dm_cblock_t cblock, dm_oblock_t oblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	__le64 value = pack_value(oblock, M_VALID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	__dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			       &value, &cmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	cmd->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			    dm_cblock_t cblock, dm_oblock_t oblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	r = __insert(cmd, cblock, oblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) struct thunk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	load_mapping_fn fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	void *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	struct dm_cache_metadata *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	bool respect_dirty_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	bool hints_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) static bool policy_unchanged(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			     struct dm_cache_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	const char *policy_name = dm_cache_policy_get_name(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	const unsigned *policy_version = dm_cache_policy_get_version(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	 * Ensure policy names match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	 * Ensure policy major versions match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	if (cmd->policy_version[0] != policy_version[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	 * Ensure policy hint sizes match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	if (cmd->policy_hint_size != policy_hint_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) static bool hints_array_initialized(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	return cmd->hint_root && cmd->policy_hint_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) static bool hints_array_available(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 				  struct dm_cache_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		hints_array_initialized(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) static int __load_mapping_v1(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			     uint64_t cb, bool hints_valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			     struct dm_array_cursor *mapping_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			     struct dm_array_cursor *hint_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			     load_mapping_fn fn, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	__le64 mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	__le32 hint = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	__le64 *mapping_value_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	__le32 *hint_value_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	dm_oblock_t oblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	unsigned flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	bool dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	memcpy(&mapping, mapping_value_le, sizeof(mapping));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	unpack_value(mapping, &oblock, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	if (flags & M_VALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		if (hints_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			memcpy(&hint, hint_value_le, sizeof(hint));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		if (cmd->clean_when_opened)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			dirty = flags & M_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		r = fn(context, oblock, to_cblock(cb), dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		       le32_to_cpu(hint), hints_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 			DMERR("policy couldn't load cache block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			      (unsigned long long) from_cblock(to_cblock(cb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) static int __load_mapping_v2(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			     uint64_t cb, bool hints_valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			     struct dm_array_cursor *mapping_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			     struct dm_array_cursor *hint_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			     struct dm_bitset_cursor *dirty_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			     load_mapping_fn fn, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	__le64 mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	__le32 hint = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	__le64 *mapping_value_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	__le32 *hint_value_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	dm_oblock_t oblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	unsigned flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	bool dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	memcpy(&mapping, mapping_value_le, sizeof(mapping));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	unpack_value(mapping, &oblock, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	if (flags & M_VALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		if (hints_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 			dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			memcpy(&hint, hint_value_le, sizeof(hint));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		if (cmd->clean_when_opened)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			dirty = dm_bitset_cursor_get_value(dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		r = fn(context, oblock, to_cblock(cb), dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		       le32_to_cpu(hint), hints_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			DMERR("policy couldn't load cache block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 			      (unsigned long long) from_cblock(to_cblock(cb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) static int __load_mappings(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 			   struct dm_cache_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 			   load_mapping_fn fn, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	uint64_t cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	bool hints_valid = hints_array_available(cmd, policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	if (from_cblock(cmd->cache_blocks) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		/* Nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	r = dm_array_cursor_begin(&cmd->info, cmd->root, &cmd->mapping_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	if (hints_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		r = dm_array_cursor_begin(&cmd->hint_info, cmd->hint_root, &cmd->hint_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 			dm_array_cursor_end(&cmd->mapping_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	if (separate_dirty_bits(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 					   from_cblock(cmd->cache_blocks),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 					   &cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			dm_array_cursor_end(&cmd->hint_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			dm_array_cursor_end(&cmd->mapping_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	for (cb = 0; ; cb++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		if (separate_dirty_bits(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 			r = __load_mapping_v2(cmd, cb, hints_valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 					      &cmd->mapping_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 					      &cmd->hint_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 					      &cmd->dirty_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 					      fn, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 			r = __load_mapping_v1(cmd, cb, hints_valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 					      &cmd->mapping_cursor, &cmd->hint_cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 					      fn, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		 * We need to break out before we move the cursors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		if (cb >= (from_cblock(cmd->cache_blocks) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		r = dm_array_cursor_next(&cmd->mapping_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			DMERR("dm_array_cursor_next for mapping failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		if (hints_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 			r = dm_array_cursor_next(&cmd->hint_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 			if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 				dm_array_cursor_end(&cmd->hint_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 				hints_valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		if (separate_dirty_bits(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 			r = dm_bitset_cursor_next(&cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 			if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 				DMERR("dm_bitset_cursor_next for dirty failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 			}
^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) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	dm_array_cursor_end(&cmd->mapping_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	if (hints_valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		dm_array_cursor_end(&cmd->hint_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	if (separate_dirty_bits(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		dm_bitset_cursor_end(&cmd->dirty_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 			   struct dm_cache_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 			   load_mapping_fn fn, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	READ_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	r = __load_mappings(cmd, policy, fn, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	__le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	dm_oblock_t oblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	unsigned flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	memcpy(&value, leaf, sizeof(value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	unpack_value(value, &oblock, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) static int __dump_mappings(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) void dm_cache_dump(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	READ_LOCK_VOID(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	__dump_mappings(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	READ_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	r = cmd->changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	unsigned flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	dm_oblock_t oblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	__le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	unpack_value(value, &oblock, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		/* nothing to be done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	__dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			       &value, &cmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	cmd->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) static int __set_dirty_bits_v1(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	for (i = 0; i < nr_bits; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		r = __dirty(cmd, to_cblock(i), test_bit(i, bits));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) static int is_dirty_callback(uint32_t index, bool *value, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	unsigned long *bits = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	*value = test_bit(index, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) static int __set_dirty_bits_v2(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	/* nr_bits is really just a sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	if (nr_bits != from_cblock(cmd->cache_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		DMERR("dirty bitset is wrong size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	r = dm_bitset_del(&cmd->dirty_info, cmd->dirty_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	cmd->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	return dm_bitset_new(&cmd->dirty_info, &cmd->dirty_root, nr_bits, is_dirty_callback, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 			    unsigned nr_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 			    unsigned long *bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	if (separate_dirty_bits(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		r = __set_dirty_bits_v2(cmd, nr_bits, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		r = __set_dirty_bits_v1(cmd, nr_bits, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 				 struct dm_cache_statistics *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	READ_LOCK_VOID(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	*stats = cmd->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 				 struct dm_cache_statistics *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	WRITE_LOCK_VOID(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	cmd->stats = *stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 				 clear_clean_shutdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	if (cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	r = __commit_transaction(cmd, mutator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	r = __begin_transaction(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 					   dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	READ_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	if (!cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		r = dm_sm_get_nr_free(cmd->metadata_sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	return r;
^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) int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 				   dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	READ_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	if (!cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	return r;
^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) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) static int get_hint(uint32_t index, void *value_le, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	uint32_t value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	struct dm_cache_policy *policy = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	value = policy_get_hint(policy, to_cblock(index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	*((__le32 *) value_le) = cpu_to_le32(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)  * It's quicker to always delete the hint array, and recreate with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)  * dm_array_new().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	size_t hint_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	const char *policy_name = dm_cache_policy_get_name(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	const unsigned *policy_version = dm_cache_policy_get_version(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	if (!policy_name[0] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	    (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	hint_size = dm_cache_policy_get_hint_size(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	if (!hint_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		return 0; /* short-circuit hints initialization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	cmd->policy_hint_size = hint_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	if (cmd->hint_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		r = dm_array_del(&cmd->hint_info, cmd->hint_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	return dm_array_new(&cmd->hint_info, &cmd->hint_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 			    from_cblock(cmd->cache_blocks),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 			    get_hint, policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	r = write_hints(cmd, policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	READ_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	WRITE_LOCK_VOID(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	dm_bm_set_read_only(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	WRITE_LOCK_VOID(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	dm_bm_set_read_write(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	struct cache_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	set_bit(NEEDS_CHECK, &cmd->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	r = superblock_lock(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		DMERR("couldn't read superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	disk_super->flags = cpu_to_le32(cmd->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	READ_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	*result = !!test_bit(NEEDS_CHECK, &cmd->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	READ_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	WRITE_LOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	__destroy_persistent_data_objects(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	r = __create_persistent_data_objects(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		cmd->fail_io = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	WRITE_UNLOCK(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) }