^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) #ifndef DM_CLONE_METADATA_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define DM_CLONE_METADATA_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "persistent-data/dm-block-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "persistent-data/dm-space-map-metadata.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define DM_CLONE_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * The metadata device is currently limited in size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DM_CLONE_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * A metadata device larger than 16GB triggers a warning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define DM_CLONE_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SPACE_MAP_ROOT_SIZE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* dm-clone metadata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct dm_clone_metadata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Set region status to hydrated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * @cmd: The dm-clone metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * @region_nr: The region number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * This function doesn't block, so it's safe to call it from interrupt context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int dm_clone_set_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Set status of all regions in the provided range to hydrated, if not already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * hydrated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @cmd: The dm-clone metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @start: Starting region number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @nr_regions: Number of regions in the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * This function doesn't block, but since it uses spin_lock_irq()/spin_unlock_irq()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * it's NOT safe to call it from any context where interrupts are disabled, e.g.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * from interrupt context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int dm_clone_cond_set_range(struct dm_clone_metadata *cmd, unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long nr_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Read existing or create fresh metadata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @bdev: The device storing the metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @target_size: The target size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @region_size: The region size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * @returns: The dm-clone metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * This function reads the superblock of @bdev and checks if it's all zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * If it is, it formats @bdev and creates fresh metadata. If it isn't, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * validates the metadata stored in @bdev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct dm_clone_metadata *dm_clone_metadata_open(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) sector_t target_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) sector_t region_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * Free the resources related to metadata management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) void dm_clone_metadata_close(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * Commit dm-clone metadata to disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * We use a two phase commit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * 1. dm_clone_metadata_pre_commit(): Prepare the current transaction for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * committing. After this is called, all subsequent metadata updates, done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * through either dm_clone_set_region_hydrated() or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * dm_clone_cond_set_range(), will be part of the **next** transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * 2. dm_clone_metadata_commit(): Actually commit the current transaction to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * disk and start a new transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * This allows dm-clone to flush the destination device after step (1) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * ensure that all freshly hydrated regions, for which we are updating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * metadata, are properly written to non-volatile storage and won't be lost in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * case of a crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int dm_clone_metadata_pre_commit(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int dm_clone_metadata_commit(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * Reload the in core copy of the on-disk bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * This should be used after aborting a metadata transaction and setting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * metadata to read-only, to invalidate the in-core cache and make it match the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * on-disk metadata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * WARNING: It must not be called concurrently with either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * dm_clone_set_region_hydrated() or dm_clone_cond_set_range(), as it updates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * the region bitmap without taking the relevant spinlock. We don't take the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * spinlock because dm_clone_reload_in_core_bitset() does I/O, so it may block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * But, it's safe to use it after calling dm_clone_metadata_set_read_only(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * because the latter sets the metadata to read-only mode. Both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * dm_clone_set_region_hydrated() and dm_clone_cond_set_range() refuse to touch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * the region bitmap, after calling dm_clone_metadata_set_read_only().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int dm_clone_reload_in_core_bitset(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Check whether dm-clone's metadata changed this transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) bool dm_clone_changed_this_transaction(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Abort current metadata transaction and rollback metadata to the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * committed transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int dm_clone_metadata_abort(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Switches metadata to a read only mode. Once read-only mode has been entered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * the following functions will return -EPERM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * dm_clone_metadata_pre_commit()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * dm_clone_metadata_commit()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * dm_clone_set_region_hydrated()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * dm_clone_cond_set_range()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * dm_clone_metadata_abort()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void dm_clone_metadata_set_read_only(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void dm_clone_metadata_set_read_write(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * Returns true if the hydration of the destination device is finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) bool dm_clone_is_hydration_done(struct dm_clone_metadata *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * Returns true if region @region_nr is hydrated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) bool dm_clone_is_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Returns true if all the regions in the range are hydrated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) bool dm_clone_is_range_hydrated(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned long start, unsigned long nr_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * Returns the number of hydrated regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) unsigned int dm_clone_nr_of_hydrated_regions(struct dm_clone_metadata *cmd);
^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) * Returns the first unhydrated region with region_nr >= @start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) unsigned long dm_clone_find_next_unhydrated_region(struct dm_clone_metadata *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned long start);
^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) * Get the number of free metadata blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int dm_clone_get_free_metadata_block_count(struct dm_clone_metadata *cmd, dm_block_t *result);
^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) * Get the total number of metadata blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int dm_clone_get_metadata_dev_size(struct dm_clone_metadata *cmd, dm_block_t *result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) #endif /* DM_CLONE_METADATA_H */