1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2010-2011 Red Hat, Inc.
5 * This file is released under the GPL.
8 #ifndef DM_THIN_METADATA_H
9 #define DM_THIN_METADATA_H
11 #include "persistent-data/dm-block-manager.h"
12 #include "persistent-data/dm-space-map.h"
13 #include "persistent-data/dm-space-map-metadata.h"
15 #define THIN_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE
18 * The metadata device is currently limited in size.
20 #define THIN_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS
23 * A metadata device larger than 16GB triggers a warning.
25 #define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
27 /*----------------------------------------------------------------*/
30 * Thin metadata superblock flags.
32 #define THIN_METADATA_NEEDS_CHECK_FLAG (1 << 0)
34 struct dm_pool_metadata;
35 struct dm_thin_device;
40 typedef uint64_t dm_thin_id;
43 * Reopens or creates a new, empty metadata volume.
45 struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
46 sector_t data_block_size,
49 int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
52 * Compat feature flags. Any incompat flags beyond the ones
53 * specified below will prevent use of the thin metadata.
55 #define THIN_FEATURE_COMPAT_SUPP 0UL
56 #define THIN_FEATURE_COMPAT_RO_SUPP 0UL
57 #define THIN_FEATURE_INCOMPAT_SUPP 0UL
60 * Device creation/deletion.
62 int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev);
65 * An internal snapshot.
67 * You can only snapshot a quiesced origin i.e. one that is either
68 * suspended or not instanced at all.
70 int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev,
74 * Deletes a virtual device from the metadata. It _is_ safe to call this
75 * when that device is open. Operations on that device will just start
76 * failing. You still need to call close() on the device.
78 int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
82 * Commits _all_ metadata changes: device creation, deletion, mapping
85 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
88 * Discards all uncommitted changes. Rereads the superblock, rolling back
89 * to the last good transaction. Thin devices remain open.
90 * dm_thin_aborted_changes() tells you if they had uncommitted changes.
92 * If this call fails it's only useful to call dm_pool_metadata_close().
93 * All other methods will fail with -EINVAL.
95 int dm_pool_abort_metadata(struct dm_pool_metadata *pmd);
98 * Set/get userspace transaction id.
100 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
104 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
108 * Hold/get root for userspace transaction.
110 * The metadata snapshot is a copy of the current superblock (minus the
111 * space maps). Userland can access the data structures for READ
112 * operations only. A small performance hit is incurred by providing this
113 * copy of the metadata to userland due to extra copy-on-write operations
114 * on the metadata nodes. Release this as soon as you finish with it.
116 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
117 int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
119 int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
123 * Actions on a single virtual device.
127 * Opening the same device more than once will fail with -EBUSY.
129 int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
130 struct dm_thin_device **td);
132 int dm_pool_close_thin_device(struct dm_thin_device *td);
134 dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
136 struct dm_thin_lookup_result {
143 * -EWOULDBLOCK iff @can_issue_io is set and would issue IO
144 * -ENODATA iff that mapping is not present.
147 int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
148 int can_issue_io, struct dm_thin_lookup_result *result);
151 * Retrieve the next run of contiguously mapped blocks. Useful for working
152 * out where to break up IO. Returns 0 on success, < 0 on error.
154 int dm_thin_find_mapped_range(struct dm_thin_device *td,
155 dm_block_t begin, dm_block_t end,
156 dm_block_t *thin_begin, dm_block_t *thin_end,
157 dm_block_t *pool_begin, bool *maybe_shared);
160 * Obtain an unused block.
162 int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
165 * Insert or remove block.
167 int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
168 dm_block_t data_block);
170 int dm_thin_remove_range(struct dm_thin_device *td,
171 dm_block_t begin, dm_block_t end);
176 bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
178 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd);
180 bool dm_thin_aborted_changes(struct dm_thin_device *td);
182 int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
183 dm_block_t *highest_mapped);
185 int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result);
187 int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd,
190 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
193 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
196 int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result);
198 int dm_pool_block_is_shared(struct dm_pool_metadata *pmd, dm_block_t b, bool *result);
200 int dm_pool_inc_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e);
201 int dm_pool_dec_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e);
204 * Returns -ENOSPC if the new size is too small and already allocated
205 * blocks would be lost.
207 int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
208 int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
211 * Flicks the underlying block manager into read only mode, so you know
212 * that nothing is changing.
214 void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd);
215 void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd);
217 int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
218 dm_block_t threshold,
219 dm_sm_threshold_fn fn,
223 * Updates the superblock immediately.
225 int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd);
226 bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd);
229 * Issue any prefetches that may be useful.
231 void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd);
233 /* Pre-commit callback */
234 typedef int (*dm_pool_pre_commit_fn)(void *context);
236 void dm_pool_register_pre_commit_callback(struct dm_pool_metadata *pmd,
237 dm_pool_pre_commit_fn fn,
240 /*----------------------------------------------------------------*/