^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2019 Arrikto, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/bitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device-mapper.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "persistent-data/dm-bitset.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "persistent-data/dm-space-map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "persistent-data/dm-block-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "persistent-data/dm-transaction-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "dm-clone-metadata.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define DM_MSG_PREFIX "clone metadata"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define SUPERBLOCK_LOCATION 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SUPERBLOCK_MAGIC 0x8af27f64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define SUPERBLOCK_CSUM_XOR 257649492
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define DM_CLONE_MAX_CONCURRENT_LOCKS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define UUID_LEN 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* Min and max dm-clone metadata versions supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define DM_CLONE_MIN_METADATA_VERSION 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define DM_CLONE_MAX_METADATA_VERSION 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * On-disk metadata layout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct superblock_disk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) __le32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) __le32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) __le64 blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) __u8 uuid[UUID_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) __le64 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) __le32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) __le64 region_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) __le64 target_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) __le64 bitset_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * Region and Dirty bitmaps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * dm-clone logically splits the source and destination devices in regions of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * fixed size. The destination device's regions are gradually hydrated, i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * we copy (clone) the source's regions to the destination device. Eventually,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * all regions will get hydrated and all I/O will be served from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * destination device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * We maintain an on-disk bitmap which tracks the state of each of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * destination device's regions, i.e., whether they are hydrated or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * To save constantly doing look ups on disk we keep an in core copy of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * on-disk bitmap, the region_map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * In order to track which regions are hydrated during a metadata transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * we use a second set of bitmaps, the dmap (dirty bitmap), which includes two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * bitmaps, namely dirty_regions and dirty_words. The dirty_regions bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * tracks the regions that got hydrated during the current metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * transaction. The dirty_words bitmap tracks the dirty words, i.e. longs, of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * the dirty_regions bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * This allows us to precisely track the regions that were hydrated during the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * current metadata transaction and update the metadata accordingly, when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * commit the current transaction. This is important because dm-clone should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * only commit the metadata of regions that were properly flushed to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * destination device beforehand. Otherwise, in case of a crash, we could end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * up with a corrupted dm-clone device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * When a region finishes hydrating dm-clone calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * dm_clone_set_region_hydrated(), or for discard requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * dm_clone_cond_set_range(), which sets the corresponding bits in region_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * and dmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * During a metadata commit we scan dmap->dirty_words and dmap->dirty_regions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * and update the on-disk metadata accordingly. Thus, we don't have to flush to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * disk the whole region_map. We can just flush the dirty region_map bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * We use the helper dmap->dirty_words bitmap, which is smaller than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * original region_map, to reduce the amount of memory accesses during a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * metadata commit. Moreover, as dm-bitset also accesses the on-disk bitmap in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * 64-bit word granularity, the dirty_words bitmap helps us avoid useless disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * accesses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * We could update directly the on-disk bitmap, when dm-clone calls either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * dm_clone_set_region_hydrated() or dm_clone_cond_set_range(), buts this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * inserts significant metadata I/O overhead in dm-clone's I/O path. Also, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * these two functions don't block, we can call them in interrupt context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * e.g., in a hooked overwrite bio's completion routine, and further reduce the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * I/O completion latency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * We maintain two dirty bitmap sets. During a metadata commit we atomically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * swap the currently used dmap with the unused one. This allows the metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * update functions to run concurrently with an ongoing commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct dirty_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned long *dirty_words;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) unsigned long *dirty_regions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned int changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct dm_clone_metadata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* The metadata block device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) sector_t target_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) sector_t region_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long nr_regions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned long nr_words;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* Spinlock protecting the region and dirty bitmaps. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) spinlock_t bitmap_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct dirty_map dmap[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct dirty_map *current_dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* Protected by lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct dirty_map *committing_dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * In core copy of the on-disk bitmap to save constantly doing look ups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unsigned long *region_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Protected by bitmap_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned int read_only;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct dm_block_manager *bm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct dm_space_map *sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct dm_transaction_manager *tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct rw_semaphore lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct dm_disk_bitset bitset_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dm_block_t bitset_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * Reading the space map root can fail, so we read it into this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * buffer before the superblock is locked and updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) bool hydration_done:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) bool fail_io:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /*---------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * Superblock validation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void sb_prepare_for_write(struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct dm_block *b, size_t sb_block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct superblock_disk *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) sb = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) sb->blocknr = cpu_to_le64(dm_block_location(b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) csum = dm_bm_checksum(&sb->flags, sb_block_size - sizeof(__le32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) SUPERBLOCK_CSUM_XOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) sb->csum = cpu_to_le32(csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int sb_check(struct dm_block_validator *v, struct dm_block *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) size_t sb_block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct superblock_disk *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) u32 csum, metadata_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) sb = dm_block_data(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (dm_block_location(b) != le64_to_cpu(sb->blocknr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) DMERR("Superblock check failed: blocknr %llu, expected %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) le64_to_cpu(sb->blocknr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) (unsigned long long)dm_block_location(b));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (le64_to_cpu(sb->magic) != SUPERBLOCK_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) DMERR("Superblock check failed: magic %llu, expected %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) le64_to_cpu(sb->magic),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) (unsigned long long)SUPERBLOCK_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) csum = dm_bm_checksum(&sb->flags, sb_block_size - sizeof(__le32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) SUPERBLOCK_CSUM_XOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (sb->csum != cpu_to_le32(csum)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) DMERR("Superblock check failed: checksum %u, expected %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) csum, le32_to_cpu(sb->csum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* Check metadata version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) metadata_version = le32_to_cpu(sb->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (metadata_version < DM_CLONE_MIN_METADATA_VERSION ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) metadata_version > DM_CLONE_MAX_METADATA_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) DMERR("Clone metadata version %u found, but only versions between %u and %u supported.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) metadata_version, DM_CLONE_MIN_METADATA_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) DM_CLONE_MAX_METADATA_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static struct dm_block_validator sb_validator = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) .name = "superblock",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) .prepare_for_write = sb_prepare_for_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) .check = sb_check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Check if the superblock is formatted or not. We consider the superblock to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * be formatted in case we find non-zero bytes in it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *formatted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsigned int i, nr_words;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) __le64 *data_le, zero = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * We don't use a validator here because the superblock could be all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) DMERR("Failed to read_lock superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) data_le = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) *formatted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* This assumes that the block size is a multiple of 8 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) BUG_ON(dm_bm_block_size(bm) % sizeof(__le64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) nr_words = dm_bm_block_size(bm) / sizeof(__le64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) for (i = 0; i < nr_words; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (data_le[i] != zero) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *formatted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /*---------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * Low-level metadata handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static inline int superblock_read_lock(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return dm_bm_read_lock(cmd->bm, SUPERBLOCK_LOCATION, &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static inline int superblock_write_lock(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return dm_bm_write_lock(cmd->bm, SUPERBLOCK_LOCATION, &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static inline int superblock_write_lock_zero(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct dm_block **sblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return dm_bm_write_lock_zero(cmd->bm, SUPERBLOCK_LOCATION, &sb_validator, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int __copy_sm_root(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) size_t root_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) r = dm_sm_root_size(cmd->sm, &root_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return dm_sm_copy_root(cmd->sm, &cmd->metadata_space_map_root, root_size);
^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) /* Save dm-clone metadata in superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static void __prepare_superblock(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct superblock_disk *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) sb->flags = cpu_to_le32(0UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /* FIXME: UUID is currently unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) memset(sb->uuid, 0, sizeof(sb->uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) sb->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) sb->version = cpu_to_le32(DM_CLONE_MAX_METADATA_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* Save the metadata space_map root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) memcpy(&sb->metadata_space_map_root, &cmd->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) sizeof(cmd->metadata_space_map_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) sb->region_size = cpu_to_le64(cmd->region_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) sb->target_size = cpu_to_le64(cmd->target_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) sb->bitset_root = cpu_to_le64(cmd->bitset_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int __open_metadata(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct superblock_disk *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) r = superblock_read_lock(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) DMERR("Failed to read_lock superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) sb = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /* Verify that target_size and region_size haven't changed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (cmd->region_size != le64_to_cpu(sb->region_size) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) cmd->target_size != le64_to_cpu(sb->target_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) DMERR("Region and/or target size don't match the ones in metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) goto out_with_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) r = dm_tm_open_with_sm(cmd->bm, SUPERBLOCK_LOCATION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) sb->metadata_space_map_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) sizeof(sb->metadata_space_map_root),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) &cmd->tm, &cmd->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) DMERR("dm_tm_open_with_sm failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) goto out_with_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) dm_disk_bitset_init(cmd->tm, &cmd->bitset_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) cmd->bitset_root = le64_to_cpu(sb->bitset_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) out_with_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) dm_bm_unlock(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int __format_metadata(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct superblock_disk *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) r = dm_tm_create_with_sm(cmd->bm, SUPERBLOCK_LOCATION, &cmd->tm, &cmd->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) DMERR("Failed to create transaction manager");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) dm_disk_bitset_init(cmd->tm, &cmd->bitset_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) r = dm_bitset_empty(&cmd->bitset_info, &cmd->bitset_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) DMERR("Failed to create empty on-disk bitset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto err_with_tm;
^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) r = dm_bitset_resize(&cmd->bitset_info, cmd->bitset_root, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) cmd->nr_regions, false, &cmd->bitset_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) DMERR("Failed to resize on-disk bitset to %lu entries", cmd->nr_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) goto err_with_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* Flush to disk all blocks, except the superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) r = dm_tm_pre_commit(cmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) DMERR("dm_tm_pre_commit failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) goto err_with_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) r = __copy_sm_root(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) DMERR("__copy_sm_root failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) goto err_with_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) r = superblock_write_lock_zero(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) DMERR("Failed to write_lock superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) goto err_with_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) sb = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) __prepare_superblock(cmd, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) r = dm_tm_commit(cmd->tm, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) DMERR("Failed to commit superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) goto err_with_tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) err_with_tm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) dm_sm_destroy(cmd->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) dm_tm_destroy(cmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static int __open_or_format_metadata(struct dm_clone_metadata *cmd, bool may_format_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) bool formatted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) r = __superblock_all_zeroes(cmd->bm, &formatted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (!formatted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return may_format_device ? __format_metadata(cmd) : -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return __open_metadata(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static int __create_persistent_data_structures(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) bool may_format_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /* Create block manager */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) cmd->bm = dm_block_manager_create(cmd->bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) DM_CLONE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) DM_CLONE_MAX_CONCURRENT_LOCKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (IS_ERR(cmd->bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) DMERR("Failed to create block manager");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return PTR_ERR(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) r = __open_or_format_metadata(cmd, may_format_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) dm_block_manager_destroy(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static void __destroy_persistent_data_structures(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) dm_sm_destroy(cmd->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) dm_tm_destroy(cmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) dm_block_manager_destroy(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /*---------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static size_t bitmap_size(unsigned long nr_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return BITS_TO_LONGS(nr_bits) * sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) static int __dirty_map_init(struct dirty_map *dmap, unsigned long nr_words,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) unsigned long nr_regions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) dmap->changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) dmap->dirty_words = kvzalloc(bitmap_size(nr_words), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!dmap->dirty_words)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) dmap->dirty_regions = kvzalloc(bitmap_size(nr_regions), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (!dmap->dirty_regions) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) kvfree(dmap->dirty_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static void __dirty_map_exit(struct dirty_map *dmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) kvfree(dmap->dirty_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) kvfree(dmap->dirty_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static int dirty_map_init(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (__dirty_map_init(&cmd->dmap[0], cmd->nr_words, cmd->nr_regions)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) DMERR("Failed to allocate dirty bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (__dirty_map_init(&cmd->dmap[1], cmd->nr_words, cmd->nr_regions)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) DMERR("Failed to allocate dirty bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) __dirty_map_exit(&cmd->dmap[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) cmd->current_dmap = &cmd->dmap[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) cmd->committing_dmap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static void dirty_map_exit(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) __dirty_map_exit(&cmd->dmap[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) __dirty_map_exit(&cmd->dmap[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static int __load_bitset_in_core(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) struct dm_bitset_cursor c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /* Flush bitset cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) r = dm_bitset_flush(&cmd->bitset_info, cmd->bitset_root, &cmd->bitset_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) r = dm_bitset_cursor_begin(&cmd->bitset_info, cmd->bitset_root, cmd->nr_regions, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) for (i = 0; ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (dm_bitset_cursor_get_value(&c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) __set_bit(i, cmd->region_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) __clear_bit(i, cmd->region_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (i >= (cmd->nr_regions - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) r = dm_bitset_cursor_next(&c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) dm_bitset_cursor_end(&c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) struct dm_clone_metadata *dm_clone_metadata_open(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) sector_t target_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) sector_t region_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct dm_clone_metadata *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (!cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) DMERR("Failed to allocate memory for dm-clone metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) cmd->bdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) cmd->target_size = target_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) cmd->region_size = region_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) cmd->nr_regions = dm_sector_div_up(cmd->target_size, cmd->region_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) cmd->nr_words = BITS_TO_LONGS(cmd->nr_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) init_rwsem(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) spin_lock_init(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) cmd->read_only = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) cmd->fail_io = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) cmd->hydration_done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) cmd->region_map = kvmalloc(bitmap_size(cmd->nr_regions), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (!cmd->region_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) DMERR("Failed to allocate memory for region bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto out_with_md;
^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) r = __create_persistent_data_structures(cmd, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) goto out_with_region_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) r = __load_bitset_in_core(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) DMERR("Failed to load on-disk region map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) goto out_with_pds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) r = dirty_map_init(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) goto out_with_pds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (bitmap_full(cmd->region_map, cmd->nr_regions))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) cmd->hydration_done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) out_with_pds:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) __destroy_persistent_data_structures(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) out_with_region_map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) kvfree(cmd->region_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) out_with_md:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return ERR_PTR(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) void dm_clone_metadata_close(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (!cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) __destroy_persistent_data_structures(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) dirty_map_exit(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) kvfree(cmd->region_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) kfree(cmd);
^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) bool dm_clone_is_hydration_done(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return cmd->hydration_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) bool dm_clone_is_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return dm_clone_is_hydration_done(cmd) || test_bit(region_nr, cmd->region_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) bool dm_clone_is_range_hydrated(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) unsigned long start, unsigned long nr_regions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) unsigned long bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (dm_clone_is_hydration_done(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) bit = find_next_zero_bit(cmd->region_map, cmd->nr_regions, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return (bit >= (start + nr_regions));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) unsigned int dm_clone_nr_of_hydrated_regions(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return bitmap_weight(cmd->region_map, cmd->nr_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) unsigned long dm_clone_find_next_unhydrated_region(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) unsigned long start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return find_next_zero_bit(cmd->region_map, cmd->nr_regions, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) static int __update_metadata_word(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) unsigned long *dirty_regions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) unsigned long word)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) unsigned long index = word * BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) unsigned long max_index = min(cmd->nr_regions, (word + 1) * BITS_PER_LONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) while (index < max_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (test_bit(index, dirty_regions)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) r = dm_bitset_set_bit(&cmd->bitset_info, cmd->bitset_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) index, &cmd->bitset_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) DMERR("dm_bitset_set_bit failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) __clear_bit(index, dirty_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static int __metadata_commit(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) struct dm_block *sblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) struct superblock_disk *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /* Flush bitset cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) r = dm_bitset_flush(&cmd->bitset_info, cmd->bitset_root, &cmd->bitset_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) DMERR("dm_bitset_flush failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) /* Flush to disk all blocks, except the superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) r = dm_tm_pre_commit(cmd->tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) DMERR("dm_tm_pre_commit failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) /* Save the space map root in cmd->metadata_space_map_root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) r = __copy_sm_root(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) DMERR("__copy_sm_root failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) /* Lock the superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) r = superblock_write_lock_zero(cmd, &sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) DMERR("Failed to write_lock superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) /* Save the metadata in superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) sb = dm_block_data(sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) __prepare_superblock(cmd, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) /* Unlock superblock and commit it to disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) r = dm_tm_commit(cmd->tm, sblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) DMERR("Failed to commit superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * FIXME: Find a more efficient way to check if the hydration is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (bitmap_full(cmd->region_map, cmd->nr_regions))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) cmd->hydration_done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) static int __flush_dmap(struct dm_clone_metadata *cmd, struct dirty_map *dmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) unsigned long word;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) word = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) word = find_next_bit(dmap->dirty_words, cmd->nr_words, word);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (word == cmd->nr_words)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) r = __update_metadata_word(cmd, dmap->dirty_regions, word);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) __clear_bit(word, dmap->dirty_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) word++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) } while (word < cmd->nr_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) r = __metadata_commit(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) /* Update the changed flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) spin_lock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) dmap->changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) spin_unlock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) int dm_clone_metadata_pre_commit(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) struct dirty_map *dmap, *next_dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) down_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) /* Get current dirty bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) dmap = cmd->current_dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) /* Get next dirty bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) next_dmap = (dmap == &cmd->dmap[0]) ? &cmd->dmap[1] : &cmd->dmap[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * The last commit failed, so we don't have a clean dirty-bitmap to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (WARN_ON(next_dmap->changed || cmd->committing_dmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) /* Swap dirty bitmaps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) spin_lock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) cmd->current_dmap = next_dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) spin_unlock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) /* Set old dirty bitmap as currently committing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) cmd->committing_dmap = dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) up_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) int dm_clone_metadata_commit(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) int r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) down_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (cmd->fail_io || dm_bm_is_read_only(cmd->bm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (WARN_ON(!cmd->committing_dmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) r = __flush_dmap(cmd, cmd->committing_dmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) /* Clear committing dmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) cmd->committing_dmap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) up_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) int dm_clone_set_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) struct dirty_map *dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) unsigned long word, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (unlikely(region_nr >= cmd->nr_regions)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) DMERR("Region %lu out of range (total number of regions %lu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) region_nr, cmd->nr_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) word = region_nr / BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) spin_lock_irqsave(&cmd->bitmap_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (cmd->read_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) dmap = cmd->current_dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) __set_bit(word, dmap->dirty_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) __set_bit(region_nr, dmap->dirty_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) __set_bit(region_nr, cmd->region_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) dmap->changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) spin_unlock_irqrestore(&cmd->bitmap_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) int dm_clone_cond_set_range(struct dm_clone_metadata *cmd, unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) unsigned long nr_regions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) struct dirty_map *dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) unsigned long word, region_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (unlikely(start >= cmd->nr_regions || (start + nr_regions) < start ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) (start + nr_regions) > cmd->nr_regions)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) DMERR("Invalid region range: start %lu, nr_regions %lu (total number of regions %lu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) start, nr_regions, cmd->nr_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) spin_lock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (cmd->read_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) dmap = cmd->current_dmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) for (region_nr = start; region_nr < (start + nr_regions); region_nr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (!test_bit(region_nr, cmd->region_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) word = region_nr / BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) __set_bit(word, dmap->dirty_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) __set_bit(region_nr, dmap->dirty_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) __set_bit(region_nr, cmd->region_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) dmap->changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) spin_unlock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) * WARNING: This must not be called concurrently with either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * dm_clone_set_region_hydrated() or dm_clone_cond_set_range(), as it changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) * cmd->region_map without taking the cmd->bitmap_lock spinlock. The only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) * exception is after setting the metadata to read-only mode, using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * dm_clone_metadata_set_read_only().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) * We don't take the spinlock because __load_bitset_in_core() does I/O, so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * may block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) int dm_clone_reload_in_core_bitset(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) down_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) if (cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) r = __load_bitset_in_core(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) up_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) bool dm_clone_changed_this_transaction(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) bool r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) spin_lock_irqsave(&cmd->bitmap_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) r = cmd->dmap[0].changed || cmd->dmap[1].changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) spin_unlock_irqrestore(&cmd->bitmap_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) int dm_clone_metadata_abort(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) int r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) down_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) if (cmd->fail_io || dm_bm_is_read_only(cmd->bm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) __destroy_persistent_data_structures(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) r = __create_persistent_data_structures(cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) /* If something went wrong we can neither write nor read the metadata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) cmd->fail_io = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) up_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) void dm_clone_metadata_set_read_only(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) down_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) spin_lock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) cmd->read_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) spin_unlock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (!cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) dm_bm_set_read_only(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) up_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) void dm_clone_metadata_set_read_write(struct dm_clone_metadata *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) down_write(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) spin_lock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) cmd->read_only = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) spin_unlock_irq(&cmd->bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (!cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) dm_bm_set_read_write(cmd->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) up_write(&cmd->lock);
^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) int dm_clone_get_free_metadata_block_count(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) down_read(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (!cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) r = dm_sm_get_nr_free(cmd->sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) up_read(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) int dm_clone_get_metadata_dev_size(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) dm_block_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) int r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) down_read(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (!cmd->fail_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) r = dm_sm_get_nr_blocks(cmd->sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) up_read(&cmd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }