^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2011-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-thin-metadata.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "persistent-data/dm-btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "persistent-data/dm-space-map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "persistent-data/dm-space-map-disk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "persistent-data/dm-transaction-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/device-mapper.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*--------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * As far as the metadata goes, there is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * - A superblock in block zero, taking up fewer than 512 bytes for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * atomic writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * - A space map managing the metadata blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * - A space map managing the data blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * - A btree mapping our internal thin dev ids onto struct disk_device_details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * - A hierarchical btree, with 2 levels which effectively maps (thin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * dev id, virtual block) -> block_time. Block time is a 64-bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * field holding the time in the low 24 bits, and block in the top 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * BTrees consist solely of btree_nodes, that fill a block. Some are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * internal nodes, as such their values are a __le64 pointing to other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * nodes. Leaf nodes can store data of any reasonable size (ie. much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * smaller than the block size). The nodes consist of the header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * followed by an array of keys, followed by an array of values. We have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * to binary search on the keys so they're all held together to help the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * cpu cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Space maps have 2 btrees:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * - One maps a uint64_t onto a struct index_entry. Which points to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * bitmap block, and has some details about how many free entries there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * are etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * - The bitmap blocks have a header (for the checksum). Then the rest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * of the block is pairs of bits. With the meaning being:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * 0 - ref count is 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * 1 - ref count is 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * 2 - ref count is 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * 3 - ref count is higher than 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * - If the count is higher than 2 then the ref count is entered in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * second btree that directly maps the block_address to a uint32_t ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * The space map metadata variant doesn't have a bitmaps btree. Instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * it has one single blocks worth of index_entries. This avoids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * recursive issues with the bitmap btree needing to allocate space in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * order to insert. With a small data block size such as 64k the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * metadata support data devices that are hundreds of terrabytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * The space maps allocate space linearly from front to back. Space that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * is freed in a transaction is never recycled within that transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * To try and avoid fragmenting _free_ space the allocator always goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * back and fills in gaps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * All metadata io is in THIN_METADATA_BLOCK_SIZE sized/aligned chunks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * from the block manager.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *--------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define DM_MSG_PREFIX "thin metadata"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define THIN_SUPERBLOCK_MAGIC 27022010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define THIN_SUPERBLOCK_LOCATION 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define THIN_VERSION 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define SECTOR_TO_BLOCK_SHIFT 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * For btree insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * 3 for btree insert +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * 2 for btree lookup used within space map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * For btree remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * 2 for shadow spine +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * 4 for rebalance 3 child node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define THIN_MAX_CONCURRENT_LOCKS 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* This should be plenty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define SPACE_MAP_ROOT_SIZE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Little endian on-disk superblock and device details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct thin_disk_superblock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) __le32 csum; /* Checksum of superblock except for this field. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) __le32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) __le64 blocknr; /* This block number, dm_block_t. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) __u8 uuid[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) __le64 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) __le32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) __le32 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) __le64 trans_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * Root held by userspace transactions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) __le64 held_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * 2-level btree mapping (dev_id, (dev block, time)) -> data block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) __le64 data_mapping_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * Device detail root mapping dev_id -> device_details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) __le64 device_details_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) __le32 data_block_size; /* In 512-byte sectors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) __le32 metadata_block_size; /* In 512-byte sectors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) __le64 metadata_nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) __le32 compat_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __le32 compat_ro_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) __le32 incompat_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct disk_device_details {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) __le64 mapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) __le64 transaction_id; /* When created. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) __le32 creation_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) __le32 snapshotted_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct dm_pool_metadata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct hlist_node hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct dm_block_manager *bm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct dm_space_map *metadata_sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct dm_space_map *data_sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct dm_transaction_manager *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct dm_transaction_manager *nb_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * Two-level btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * First level holds thin_dev_t.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * Second level holds mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct dm_btree_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Non-blocking version of the above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct dm_btree_info nb_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * Just the top level for deleting whole devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct dm_btree_info tl_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Just the bottom level for creating new devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct dm_btree_info bl_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * Describes the device details btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct dm_btree_info details_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct rw_semaphore root_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) uint32_t time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) dm_block_t root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) dm_block_t details_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct list_head thin_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) uint64_t trans_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) sector_t data_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * Pre-commit callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * This allows the thin provisioning target to run a callback before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * the metadata are committed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) dm_pool_pre_commit_fn pre_commit_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) void *pre_commit_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * We reserve a section of the metadata for commit overhead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * All reported space does *not* include this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) dm_block_t metadata_reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Set if a transaction has to be aborted but the attempt to roll back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * to the previous (good) transaction failed. The only pool metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * operation possible in this state is the closing of the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) bool fail_io:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * Set once a thin-pool has been accessed through one of the interfaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * that imply the pool is in-service (e.g. thin devices created/deleted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * thin-pool message, metadata snapshots, etc).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) bool in_service:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * Reading the space map roots can fail, so we read it into these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * buffers before the superblock is locked and updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct dm_thin_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct dm_pool_metadata *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dm_thin_id id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int open_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) bool changed:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) bool aborted_with_changes:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) uint64_t mapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) uint64_t transaction_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) uint32_t creation_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) uint32_t snapshotted_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * superblock validator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) #define SUPERBLOCK_CSUM_XOR 160774
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static void sb_prepare_for_write(struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct dm_block *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) size_t block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct thin_disk_superblock *disk_super = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) disk_super->blocknr = cpu_to_le64(dm_block_location(b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) block_size - sizeof(__le32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) SUPERBLOCK_CSUM_XOR));
^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 sb_check(struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct dm_block *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) size_t block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct thin_disk_superblock *disk_super = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) __le32 csum_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) DMERR("sb_check failed: blocknr %llu: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) "wanted %llu", le64_to_cpu(disk_super->blocknr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) (unsigned long long)dm_block_location(b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) DMERR("sb_check failed: magic %llu: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) "wanted %llu", le64_to_cpu(disk_super->magic),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) (unsigned long long)THIN_SUPERBLOCK_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) block_size - sizeof(__le32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) SUPERBLOCK_CSUM_XOR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (csum_le != disk_super->csum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) DMERR("sb_check failed: csum %u: wanted %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static struct dm_block_validator sb_validator = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .name = "superblock",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) .prepare_for_write = sb_prepare_for_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .check = sb_check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*----------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * Methods for the btree value types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) *--------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static uint64_t pack_block_time(dm_block_t b, uint32_t t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return (b << 24) | t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *b = v >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *t = v & ((1 << 24) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static void data_block_inc(void *context, const void *value_le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct dm_space_map *sm = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) __le64 v_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) uint64_t b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) uint32_t t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) memcpy(&v_le, value_le, sizeof(v_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) unpack_block_time(le64_to_cpu(v_le), &b, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) dm_sm_inc_block(sm, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static void data_block_dec(void *context, const void *value_le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct dm_space_map *sm = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) __le64 v_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) uint64_t b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) uint32_t t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) memcpy(&v_le, value_le, sizeof(v_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) unpack_block_time(le64_to_cpu(v_le), &b, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) dm_sm_dec_block(sm, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int data_block_equal(void *context, const void *value1_le, const void *value2_le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) __le64 v1_le, v2_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) uint64_t b1, b2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) uint32_t t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) memcpy(&v1_le, value1_le, sizeof(v1_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) memcpy(&v2_le, value2_le, sizeof(v2_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return b1 == b2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static void subtree_inc(void *context, const void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct dm_btree_info *info = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) __le64 root_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) uint64_t root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) memcpy(&root_le, value, sizeof(root_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) root = le64_to_cpu(root_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) dm_tm_inc(info->tm, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static void subtree_dec(void *context, const void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct dm_btree_info *info = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) __le64 root_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) uint64_t root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) memcpy(&root_le, value, sizeof(root_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) root = le64_to_cpu(root_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (dm_btree_del(info, root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) DMERR("btree delete failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static int subtree_equal(void *context, const void *value1_le, const void *value2_le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) __le64 v1_le, v2_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) memcpy(&v1_le, value1_le, sizeof(v1_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) memcpy(&v2_le, value2_le, sizeof(v2_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return v1_le == v2_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * Variant that is used for in-core only changes or code that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * shouldn't put the pool in service on its own (e.g. commit).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static inline void pmd_write_lock_in_core(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) __acquires(pmd->root_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) down_write(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static inline void pmd_write_lock(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) pmd_write_lock_in_core(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (unlikely(!pmd->in_service))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) pmd->in_service = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static inline void pmd_write_unlock(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) __releases(pmd->root_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) up_write(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static int superblock_lock_zero(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static int superblock_lock(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct dm_block *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) __le64 *data_le, zero = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * We can't use a validator here - it may be all zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) data_le = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) *result = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) for (i = 0; i < block_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (data_le[i] != zero) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) *result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^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) dm_bm_unlock(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static void __setup_btree_details(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) pmd->info.tm = pmd->tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) pmd->info.levels = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) pmd->info.value_type.context = pmd->data_sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) pmd->info.value_type.size = sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) pmd->info.value_type.inc = data_block_inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) pmd->info.value_type.dec = data_block_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) pmd->info.value_type.equal = data_block_equal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) pmd->nb_info.tm = pmd->nb_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) pmd->tl_info.tm = pmd->tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) pmd->tl_info.levels = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) pmd->tl_info.value_type.context = &pmd->bl_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) pmd->tl_info.value_type.size = sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) pmd->tl_info.value_type.inc = subtree_inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) pmd->tl_info.value_type.dec = subtree_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) pmd->tl_info.value_type.equal = subtree_equal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) pmd->bl_info.tm = pmd->tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) pmd->bl_info.levels = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) pmd->bl_info.value_type.context = pmd->data_sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) pmd->bl_info.value_type.size = sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) pmd->bl_info.value_type.inc = data_block_inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) pmd->bl_info.value_type.dec = data_block_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) pmd->bl_info.value_type.equal = data_block_equal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) pmd->details_info.tm = pmd->tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) pmd->details_info.levels = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) pmd->details_info.value_type.context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) pmd->details_info.value_type.size = sizeof(struct disk_device_details);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) pmd->details_info.value_type.inc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) pmd->details_info.value_type.dec = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) pmd->details_info.value_type.equal = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static int save_sm_roots(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) r = dm_sm_root_size(pmd->metadata_sm, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) r = dm_sm_copy_root(pmd->metadata_sm, &pmd->metadata_space_map_root, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) r = dm_sm_root_size(pmd->data_sm, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) return dm_sm_copy_root(pmd->data_sm, &pmd->data_space_map_root, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static void copy_sm_roots(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) struct thin_disk_superblock *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) memcpy(&disk->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) &pmd->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) sizeof(pmd->metadata_space_map_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) memcpy(&disk->data_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) &pmd->data_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) sizeof(pmd->data_space_map_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static int __write_initial_superblock(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) struct thin_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (bdev_size > THIN_METADATA_MAX_SECTORS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) bdev_size = THIN_METADATA_MAX_SECTORS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) r = dm_sm_commit(pmd->data_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) r = dm_tm_pre_commit(pmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) r = save_sm_roots(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) r = superblock_lock_zero(pmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) disk_super->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) disk_super->version = cpu_to_le32(THIN_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) disk_super->time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) disk_super->trans_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) disk_super->held_root = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) copy_sm_roots(pmd, disk_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) disk_super->data_mapping_root = cpu_to_le64(pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) disk_super->device_details_root = cpu_to_le64(pmd->details_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) return dm_tm_commit(pmd->tm, sblock);
^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 int __format_metadata(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) &pmd->tm, &pmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) DMERR("tm_create_with_sm failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (IS_ERR(pmd->data_sm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) DMERR("sm_disk_create failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) r = PTR_ERR(pmd->data_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) goto bad_cleanup_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (!pmd->nb_tm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) DMERR("could not create non-blocking clone tm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto bad_cleanup_data_sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) __setup_btree_details(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) r = dm_btree_empty(&pmd->info, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) goto bad_cleanup_nb_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) DMERR("couldn't create devices root");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) goto bad_cleanup_nb_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) r = __write_initial_superblock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) goto bad_cleanup_nb_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) bad_cleanup_nb_tm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) dm_tm_destroy(pmd->nb_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) bad_cleanup_data_sm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) dm_sm_destroy(pmd->data_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) bad_cleanup_tm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) dm_tm_destroy(pmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) dm_sm_destroy(pmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) static int __check_incompat_features(struct thin_disk_superblock *disk_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) uint32_t features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (features) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) DMERR("could not access metadata due to unsupported optional features (%lx).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) (unsigned long)features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * Check for read-only metadata to skip the following RDWR checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (get_disk_ro(pmd->bdev->bd_disk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (features) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) (unsigned long)features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) static int __open_metadata(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) struct thin_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) &sb_validator, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) DMERR("couldn't read superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /* Verify the data block size hasn't changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (le32_to_cpu(disk_super->data_block_size) != pmd->data_block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) DMERR("changing the data block size (from %u to %llu) is not supported",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) le32_to_cpu(disk_super->data_block_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) (unsigned long long)pmd->data_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) goto bad_unlock_sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) r = __check_incompat_features(disk_super, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) goto bad_unlock_sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) disk_super->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) sizeof(disk_super->metadata_space_map_root),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) &pmd->tm, &pmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) DMERR("tm_open_with_sm failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) goto bad_unlock_sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) sizeof(disk_super->data_space_map_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (IS_ERR(pmd->data_sm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) DMERR("sm_disk_open failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) r = PTR_ERR(pmd->data_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) goto bad_cleanup_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (!pmd->nb_tm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) DMERR("could not create non-blocking clone tm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) goto bad_cleanup_data_sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) __setup_btree_details(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) bad_cleanup_data_sm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) dm_sm_destroy(pmd->data_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) bad_cleanup_tm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) dm_tm_destroy(pmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) dm_sm_destroy(pmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) bad_unlock_sblock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) return r;
^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) static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) int r, unformatted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) r = __superblock_all_zeroes(pmd->bm, &unformatted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (unformatted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return format_device ? __format_metadata(pmd) : -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return __open_metadata(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) THIN_MAX_CONCURRENT_LOCKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (IS_ERR(pmd->bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) DMERR("could not create block manager");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) r = PTR_ERR(pmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) pmd->bm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) r = __open_or_format_metadata(pmd, format_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) dm_block_manager_destroy(pmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) pmd->bm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) dm_sm_destroy(pmd->data_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) dm_sm_destroy(pmd->metadata_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) dm_tm_destroy(pmd->nb_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) dm_tm_destroy(pmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) dm_block_manager_destroy(pmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) static int __begin_transaction(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) struct thin_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * We re-read the superblock every time. Shouldn't need to do this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * really.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) &sb_validator, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) pmd->time = le32_to_cpu(disk_super->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) pmd->root = le64_to_cpu(disk_super->data_mapping_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) pmd->details_root = le64_to_cpu(disk_super->device_details_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) pmd->trans_id = le64_to_cpu(disk_super->trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) pmd->flags = le32_to_cpu(disk_super->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static int __write_changed_details(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) struct dm_thin_device *td, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) struct disk_device_details details;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) uint64_t key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (!td->changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) key = td->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) details.transaction_id = cpu_to_le64(td->transaction_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) details.creation_time = cpu_to_le32(td->creation_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) __dm_bless_for_disk(&details);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) r = dm_btree_insert(&pmd->details_info, pmd->details_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) &key, &details, &pmd->details_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (td->open_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) td->changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) list_del(&td->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) kfree(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) static int __commit_transaction(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) struct thin_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) BUG_ON(!rwsem_is_locked(&pmd->root_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (unlikely(!pmd->in_service))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) if (pmd->pre_commit_fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) r = pmd->pre_commit_fn(pmd->pre_commit_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) DMERR("pre-commit callback failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) return r;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) r = __write_changed_details(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) r = dm_sm_commit(pmd->data_sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) r = dm_tm_pre_commit(pmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) r = save_sm_roots(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) r = superblock_lock(pmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) disk_super->time = cpu_to_le32(pmd->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) disk_super->data_mapping_root = cpu_to_le64(pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) disk_super->device_details_root = cpu_to_le64(pmd->details_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) disk_super->trans_id = cpu_to_le64(pmd->trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) disk_super->flags = cpu_to_le32(pmd->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) copy_sm_roots(pmd, disk_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) return dm_tm_commit(pmd->tm, sblock);
^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) static void __set_metadata_reserve(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) dm_block_t total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) dm_block_t max_blocks = 4096; /* 16M */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) r = dm_sm_get_nr_blocks(pmd->metadata_sm, &total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) DMERR("could not get size of metadata device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) pmd->metadata_reserve = max_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) pmd->metadata_reserve = min(max_blocks, div_u64(total, 10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) sector_t data_block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) bool format_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) struct dm_pool_metadata *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (!pmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) DMERR("could not allocate metadata struct");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) init_rwsem(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) pmd->time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) INIT_LIST_HEAD(&pmd->thin_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) pmd->fail_io = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) pmd->in_service = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) pmd->bdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) pmd->data_block_size = data_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) pmd->pre_commit_fn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) pmd->pre_commit_context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) r = __create_persistent_data_objects(pmd, format_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) kfree(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) return ERR_PTR(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) r = __begin_transaction(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (dm_pool_metadata_close(pmd) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) return ERR_PTR(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) __set_metadata_reserve(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) unsigned open_devices = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) struct dm_thin_device *td, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (td->open_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) open_devices++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) list_del(&td->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) kfree(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (open_devices) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) DMERR("attempt to close pmd when %u device(s) are still open",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) open_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) pmd_write_lock_in_core(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) if (!pmd->fail_io && !dm_bm_is_read_only(pmd->bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) r = __commit_transaction(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) DMWARN("%s: __commit_transaction() failed, error = %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) __func__, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) __destroy_persistent_data_objects(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) kfree(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) * __open_device: Returns @td corresponding to device with id @dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) * creating it if @create is set and incrementing @td->open_count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) * On failure, @td is undefined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) static int __open_device(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) dm_thin_id dev, int create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) struct dm_thin_device **td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) int r, changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) struct dm_thin_device *td2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) uint64_t key = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) struct disk_device_details details_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) * If the device is already open, return it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) list_for_each_entry(td2, &pmd->thin_devices, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (td2->id == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * May not create an already-open device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) if (create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) td2->open_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) *td = td2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * Check the device exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) &key, &details_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (r != -ENODATA || !create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) * Create new device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) details_le.mapped_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) details_le.transaction_id = cpu_to_le64(pmd->trans_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) details_le.creation_time = cpu_to_le32(pmd->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) details_le.snapshotted_time = cpu_to_le32(pmd->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) *td = kmalloc(sizeof(**td), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) if (!*td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) (*td)->pmd = pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) (*td)->id = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) (*td)->open_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) (*td)->changed = changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) (*td)->aborted_with_changes = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) (*td)->creation_time = le32_to_cpu(details_le.creation_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) list_add(&(*td)->list, &pmd->thin_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) static void __close_device(struct dm_thin_device *td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) --td->open_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) static int __create_thin(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) dm_thin_id dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) dm_block_t dev_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) uint64_t key = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) struct dm_thin_device *td;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) __le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) &key, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) * Create an empty btree for the mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) r = dm_btree_empty(&pmd->bl_info, &dev_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) return r;
^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) * Insert it into the main mapping tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) value = cpu_to_le64(dev_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) __dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) dm_btree_del(&pmd->bl_info, dev_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) r = __open_device(pmd, dev, 1, &td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) dm_btree_del(&pmd->bl_info, dev_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) __close_device(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) r = __create_thin(pmd, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) static int __set_snapshot_details(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) struct dm_thin_device *snap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) dm_thin_id origin, uint32_t time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) struct dm_thin_device *td;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) r = __open_device(pmd, origin, 0, &td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) td->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) td->snapshotted_time = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) snap->mapped_blocks = td->mapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) snap->snapshotted_time = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) __close_device(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) static int __create_snap(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) dm_thin_id dev, dm_thin_id origin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) dm_block_t origin_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) uint64_t key = origin, dev_key = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) struct dm_thin_device *td;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) __le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) /* check this device is unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) &dev_key, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) /* find the mapping tree for the origin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) origin_root = le64_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) /* clone the origin, an inc will do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) dm_tm_inc(pmd->tm, origin_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) /* insert into the main mapping tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) value = cpu_to_le64(origin_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) __dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) key = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) dm_tm_dec(pmd->tm, origin_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) pmd->time++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) r = __open_device(pmd, dev, 1, &td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) r = __set_snapshot_details(pmd, td, origin, pmd->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) __close_device(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) dm_btree_remove(&pmd->details_info, pmd->details_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) &key, &pmd->details_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) int dm_pool_create_snap(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) dm_thin_id dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) dm_thin_id origin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) r = __create_snap(pmd, dev, origin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) uint64_t key = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) struct dm_thin_device *td;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) /* TODO: failure should mark the transaction invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) r = __open_device(pmd, dev, 0, &td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (td->open_count > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) __close_device(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) list_del(&td->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) kfree(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) r = dm_btree_remove(&pmd->details_info, pmd->details_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) &key, &pmd->details_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) dm_thin_id dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) r = __delete_device(pmd, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) uint64_t current_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) uint64_t new_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) if (pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) if (pmd->trans_id != current_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) DMERR("mismatched transaction id");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) pmd->trans_id = new_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) uint64_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if (!pmd->fail_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) *result = pmd->trans_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) up_read(&pmd->root_lock);
^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) static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) int r, inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) struct thin_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) struct dm_block *copy, *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) dm_block_t held_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) * We commit to ensure the btree roots which we increment in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) * moment are up to date.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) r = __commit_transaction(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) DMWARN("%s: __commit_transaction() failed, error = %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) __func__, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) * Copy the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) &sb_validator, ©, &inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) BUG_ON(!inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) held_root = dm_block_location(copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) disk_super = dm_block_data(copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) if (le64_to_cpu(disk_super->held_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) DMWARN("Pool metadata snapshot already exists: release this before taking another.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) dm_tm_dec(pmd->tm, held_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) dm_tm_unlock(pmd->tm, copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) * Wipe the spacemap since we're not publishing this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) memset(&disk_super->data_space_map_root, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) sizeof(disk_super->data_space_map_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) memset(&disk_super->metadata_space_map_root, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) sizeof(disk_super->metadata_space_map_root));
^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) * Increment the data structures that need to be preserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) dm_tm_unlock(pmd->tm, copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) * Write the held root into the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) r = superblock_lock(pmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) dm_tm_dec(pmd->tm, held_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) disk_super->held_root = cpu_to_le64(held_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) r = __reserve_metadata_snap(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) static int __release_metadata_snap(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) struct thin_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) struct dm_block *sblock, *copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) dm_block_t held_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) r = superblock_lock(pmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) held_root = le64_to_cpu(disk_super->held_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) disk_super->held_root = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) if (!held_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) DMWARN("No pool metadata snapshot found: nothing to release.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, ©);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) disk_super = dm_block_data(copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) dm_btree_del(&pmd->info, le64_to_cpu(disk_super->data_mapping_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) dm_btree_del(&pmd->details_info, le64_to_cpu(disk_super->device_details_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) dm_sm_dec_block(pmd->metadata_sm, held_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) dm_tm_unlock(pmd->tm, copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) r = __release_metadata_snap(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) static int __get_metadata_snap(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) struct thin_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) &sb_validator, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) *result = le64_to_cpu(disk_super->held_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) r = __get_metadata_snap(pmd, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) struct dm_thin_device **td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) pmd_write_lock_in_core(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) r = __open_device(pmd, dev, 0, td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) int dm_pool_close_thin_device(struct dm_thin_device *td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) pmd_write_lock_in_core(td->pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) __close_device(td);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) pmd_write_unlock(td->pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) return td->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) * Check whether @time (of block creation) is older than @td's last snapshot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) * If so then the associated block is shared with the last snapshot device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) * Any block on a device created *after* the device last got snapshotted is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) * necessarily not shared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) return td->snapshotted_time > time;
^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) static void unpack_lookup_result(struct dm_thin_device *td, __le64 value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) struct dm_thin_lookup_result *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) uint64_t block_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) dm_block_t exception_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) uint32_t exception_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) block_time = le64_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) unpack_block_time(block_time, &exception_block, &exception_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) result->block = exception_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) result->shared = __snapshotted_since(td, exception_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) static int __find_block(struct dm_thin_device *td, dm_block_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) int can_issue_io, struct dm_thin_lookup_result *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) __le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) dm_block_t keys[2] = { td->id, block };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) struct dm_btree_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) if (can_issue_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) info = &pmd->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) info = &pmd->nb_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) r = dm_btree_lookup(info, pmd->root, keys, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) unpack_lookup_result(td, value, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) int can_issue_io, struct dm_thin_lookup_result *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (pmd->fail_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) return -EINVAL;
^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) r = __find_block(td, block, can_issue_io, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) static int __find_next_mapped_block(struct dm_thin_device *td, dm_block_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) dm_block_t *vblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) struct dm_thin_lookup_result *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) __le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) dm_block_t keys[2] = { td->id, block };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) r = dm_btree_lookup_next(&pmd->info, pmd->root, keys, vblock, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) unpack_lookup_result(td, value, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) static int __find_mapped_range(struct dm_thin_device *td,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) dm_block_t begin, dm_block_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) dm_block_t *thin_begin, dm_block_t *thin_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) dm_block_t *pool_begin, bool *maybe_shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) dm_block_t pool_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) struct dm_thin_lookup_result lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) if (end < begin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) r = __find_next_mapped_block(td, begin, &begin, &lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) if (begin >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) *thin_begin = begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) *pool_begin = lookup.block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) *maybe_shared = lookup.shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) begin++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) pool_end = *pool_begin + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) while (begin != end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) r = __find_block(td, begin, true, &lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) if (r == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if ((lookup.block != pool_end) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) (lookup.shared != *maybe_shared))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) pool_end++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) begin++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) *thin_end = begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) int dm_thin_find_mapped_range(struct dm_thin_device *td,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) dm_block_t begin, dm_block_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) dm_block_t *thin_begin, dm_block_t *thin_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) dm_block_t *pool_begin, bool *maybe_shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) if (!pmd->fail_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) r = __find_mapped_range(td, begin, end, thin_begin, thin_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) pool_begin, maybe_shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) static int __insert(struct dm_thin_device *td, dm_block_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) dm_block_t data_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) int r, inserted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) __le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) dm_block_t keys[2] = { td->id, block };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) value = cpu_to_le64(pack_block_time(data_block, pmd->time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) __dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) &pmd->root, &inserted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) td->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) if (inserted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) td->mapped_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) dm_block_t data_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) pmd_write_lock(td->pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) if (!td->pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) r = __insert(td, block, data_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) pmd_write_unlock(td->pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) static int __remove(struct dm_thin_device *td, dm_block_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) dm_block_t keys[2] = { td->id, block };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) td->mapped_blocks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) td->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) static int __remove_range(struct dm_thin_device *td, dm_block_t begin, dm_block_t end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) unsigned count, total_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) dm_block_t keys[1] = { td->id };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) __le64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) dm_block_t mapping_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) * Find the mapping tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) r = dm_btree_lookup(&pmd->tl_info, pmd->root, keys, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) * Remove from the mapping tree, taking care to inc the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) * ref count so it doesn't get deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) mapping_root = le64_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) dm_tm_inc(pmd->tm, mapping_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) r = dm_btree_remove(&pmd->tl_info, pmd->root, keys, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) * Remove leaves stops at the first unmapped entry, so we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) * loop round finding mapped ranges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) while (begin < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) r = dm_btree_lookup_next(&pmd->bl_info, mapping_root, &begin, &begin, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) if (r == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) if (begin >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) r = dm_btree_remove_leaves(&pmd->bl_info, mapping_root, &begin, end, &mapping_root, &count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) total_count += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) td->mapped_blocks -= total_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) td->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) * Reinsert the mapping tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) value = cpu_to_le64(mapping_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) __dm_bless_for_disk(&value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) return dm_btree_insert(&pmd->tl_info, pmd->root, keys, &value, &pmd->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) pmd_write_lock(td->pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) if (!td->pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) r = __remove(td, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) pmd_write_unlock(td->pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) int dm_thin_remove_range(struct dm_thin_device *td,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) dm_block_t begin, dm_block_t end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) pmd_write_lock(td->pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) if (!td->pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) r = __remove_range(td, begin, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) pmd_write_unlock(td->pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) int dm_pool_block_is_shared(struct dm_pool_metadata *pmd, dm_block_t b, bool *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) uint32_t ref_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) r = dm_sm_get_count(pmd->data_sm, b, &ref_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) *result = (ref_count > 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) int dm_pool_inc_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) for (; b != e; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) r = dm_sm_inc_block(pmd->data_sm, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) int dm_pool_dec_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) for (; b != e; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) r = dm_sm_dec_block(pmd->data_sm, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) down_read(&td->pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) r = td->changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) up_read(&td->pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)
^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) bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) bool r = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) struct dm_thin_device *td, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) if (td->changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) r = td->changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) bool dm_thin_aborted_changes(struct dm_thin_device *td)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) bool r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) down_read(&td->pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) r = td->aborted_with_changes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) up_read(&td->pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) r = dm_sm_new_block(pmd->data_sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) * Care is taken to not have commit be what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) * triggers putting the thin-pool in-service.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) pmd_write_lock_in_core(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) if (pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) r = __commit_transaction(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) * Open the next transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) r = __begin_transaction(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) struct dm_thin_device *td;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) list_for_each_entry(td, &pmd->thin_devices, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) td->aborted_with_changes = td->changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) if (pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) __set_abort_with_changes_flags(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) __destroy_persistent_data_objects(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) r = __create_persistent_data_objects(pmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) pmd->fail_io = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) r = dm_sm_get_nr_free(pmd->data_sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) r = dm_sm_get_nr_free(pmd->metadata_sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) if (*result < pmd->metadata_reserve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) *result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) *result -= pmd->metadata_reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) r = dm_sm_get_nr_blocks(pmd->data_sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) if (!pmd->fail_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) *result = td->mapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) __le64 value_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) dm_block_t thin_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) thin_root = le64_to_cpu(value_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) struct dm_pool_metadata *pmd = td->pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) r = __highest_block(td, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) dm_block_t old_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) r = dm_sm_get_nr_blocks(sm, &old_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) if (new_count == old_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) if (new_count < old_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) DMERR("cannot reduce size of space map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) return dm_sm_extend(sm, new_count - old_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) r = __resize_space_map(pmd->data_sm, new_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) if (!pmd->fail_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) r = __resize_space_map(pmd->metadata_sm, new_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) __set_metadata_reserve(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) pmd_write_lock_in_core(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) dm_bm_set_read_only(pmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) pmd_write_lock_in_core(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) dm_bm_set_read_write(pmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) dm_block_t threshold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) dm_sm_threshold_fn fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) pmd_write_lock_in_core(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) r = dm_sm_register_threshold_callback(pmd->metadata_sm, threshold, fn, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) void dm_pool_register_pre_commit_callback(struct dm_pool_metadata *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) dm_pool_pre_commit_fn fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) pmd_write_lock_in_core(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) pmd->pre_commit_fn = fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) pmd->pre_commit_context = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) struct thin_disk_superblock *disk_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) pmd_write_lock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) if (pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) pmd->flags |= THIN_METADATA_NEEDS_CHECK_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) r = superblock_lock(pmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) DMERR("couldn't lock superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) disk_super = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) disk_super->flags = cpu_to_le32(pmd->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) pmd_write_unlock(pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) bool needs_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) needs_check = pmd->flags & THIN_METADATA_NEEDS_CHECK_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) return needs_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) down_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) if (!pmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) dm_tm_issue_prefetches(pmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) up_read(&pmd->root_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) }