1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2019 Arrikto, Inc. All Rights Reserved.
6 #ifndef DM_CLONE_METADATA_H
7 #define DM_CLONE_METADATA_H
9 #include "persistent-data/dm-block-manager.h"
10 #include "persistent-data/dm-space-map-metadata.h"
12 #define DM_CLONE_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE
15 * The metadata device is currently limited in size.
17 #define DM_CLONE_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS
20 * A metadata device larger than 16GB triggers a warning.
22 #define DM_CLONE_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
24 #define SPACE_MAP_ROOT_SIZE 128
26 /* dm-clone metadata */
27 struct dm_clone_metadata;
30 * Set region status to hydrated.
32 * @cmd: The dm-clone metadata
33 * @region_nr: The region number
35 * This function doesn't block, so it's safe to call it from interrupt context.
37 int dm_clone_set_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr);
40 * Set status of all regions in the provided range to hydrated, if not already
43 * @cmd: The dm-clone metadata
44 * @start: Starting region number
45 * @nr_regions: Number of regions in the range
47 * This function doesn't block, so it's safe to call it from interrupt context.
49 int dm_clone_cond_set_range(struct dm_clone_metadata *cmd, unsigned long start,
50 unsigned long nr_regions);
53 * Read existing or create fresh metadata.
55 * @bdev: The device storing the metadata
56 * @target_size: The target size
57 * @region_size: The region size
59 * @returns: The dm-clone metadata
61 * This function reads the superblock of @bdev and checks if it's all zeroes.
62 * If it is, it formats @bdev and creates fresh metadata. If it isn't, it
63 * validates the metadata stored in @bdev.
65 struct dm_clone_metadata *dm_clone_metadata_open(struct block_device *bdev,
67 sector_t region_size);
70 * Free the resources related to metadata management.
72 void dm_clone_metadata_close(struct dm_clone_metadata *cmd);
75 * Commit dm-clone metadata to disk.
77 int dm_clone_metadata_commit(struct dm_clone_metadata *cmd);
80 * Reload the in core copy of the on-disk bitmap.
82 * This should be used after aborting a metadata transaction and setting the
83 * metadata to read-only, to invalidate the in-core cache and make it match the
86 * WARNING: It must not be called concurrently with either
87 * dm_clone_set_region_hydrated() or dm_clone_cond_set_range(), as it updates
88 * the region bitmap without taking the relevant spinlock. We don't take the
89 * spinlock because dm_clone_reload_in_core_bitset() does I/O, so it may block.
91 * But, it's safe to use it after calling dm_clone_metadata_set_read_only(),
92 * because the latter sets the metadata to read-only mode. Both
93 * dm_clone_set_region_hydrated() and dm_clone_cond_set_range() refuse to touch
94 * the region bitmap, after calling dm_clone_metadata_set_read_only().
96 int dm_clone_reload_in_core_bitset(struct dm_clone_metadata *cmd);
99 * Check whether dm-clone's metadata changed this transaction.
101 bool dm_clone_changed_this_transaction(struct dm_clone_metadata *cmd);
104 * Abort current metadata transaction and rollback metadata to the last
105 * committed transaction.
107 int dm_clone_metadata_abort(struct dm_clone_metadata *cmd);
110 * Switches metadata to a read only mode. Once read-only mode has been entered
111 * the following functions will return -EPERM:
113 * dm_clone_metadata_commit()
114 * dm_clone_set_region_hydrated()
115 * dm_clone_cond_set_range()
116 * dm_clone_metadata_abort()
118 void dm_clone_metadata_set_read_only(struct dm_clone_metadata *cmd);
119 void dm_clone_metadata_set_read_write(struct dm_clone_metadata *cmd);
122 * Returns true if the hydration of the destination device is finished.
124 bool dm_clone_is_hydration_done(struct dm_clone_metadata *cmd);
127 * Returns true if region @region_nr is hydrated.
129 bool dm_clone_is_region_hydrated(struct dm_clone_metadata *cmd, unsigned long region_nr);
132 * Returns true if all the regions in the range are hydrated.
134 bool dm_clone_is_range_hydrated(struct dm_clone_metadata *cmd,
135 unsigned long start, unsigned long nr_regions);
138 * Returns the number of hydrated regions.
140 unsigned long dm_clone_nr_of_hydrated_regions(struct dm_clone_metadata *cmd);
143 * Returns the first unhydrated region with region_nr >= @start
145 unsigned long dm_clone_find_next_unhydrated_region(struct dm_clone_metadata *cmd,
146 unsigned long start);
149 * Get the number of free metadata blocks.
151 int dm_clone_get_free_metadata_block_count(struct dm_clone_metadata *cmd, dm_block_t *result);
154 * Get the total number of metadata blocks.
156 int dm_clone_get_metadata_dev_size(struct dm_clone_metadata *cmd, dm_block_t *result);
158 #endif /* DM_CLONE_METADATA_H */