blob: 82ea384d36ff9869b10864bc7e7d7f89d2bf3a4f [file] [log] [blame]
Joe Thornber991d9fa2011-10-31 20:21:18 +00001/*
2 * Copyright (C) 2010-2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
7#ifndef DM_THIN_METADATA_H
8#define DM_THIN_METADATA_H
9
10#include "persistent-data/dm-block-manager.h"
Joe Thornberac8c3f32013-05-10 14:37:21 +010011#include "persistent-data/dm-space-map.h"
Mike Snitzer7d489352014-02-12 23:58:15 -050012#include "persistent-data/dm-space-map-metadata.h"
Joe Thornber991d9fa2011-10-31 20:21:18 +000013
Mike Snitzer7d489352014-02-12 23:58:15 -050014#define THIN_METADATA_BLOCK_SIZE DM_SM_METADATA_BLOCK_SIZE
Joe Thornber991d9fa2011-10-31 20:21:18 +000015
Mike Snitzerc4a69ec2012-03-28 18:41:28 +010016/*
17 * The metadata device is currently limited in size.
Mike Snitzerc4a69ec2012-03-28 18:41:28 +010018 */
Mike Snitzer7d489352014-02-12 23:58:15 -050019#define THIN_METADATA_MAX_SECTORS DM_SM_METADATA_MAX_SECTORS
Mike Snitzerc4a69ec2012-03-28 18:41:28 +010020
21/*
22 * A metadata device larger than 16GB triggers a warning.
23 */
24#define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
25
Joe Thornber991d9fa2011-10-31 20:21:18 +000026/*----------------------------------------------------------------*/
27
28struct dm_pool_metadata;
29struct dm_thin_device;
30
31/*
32 * Device identifier
33 */
34typedef uint64_t dm_thin_id;
35
36/*
37 * Reopens or creates a new, empty metadata volume.
38 */
39struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
Joe Thornber66b1edc2012-07-27 15:08:14 +010040 sector_t data_block_size,
41 bool format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +000042
43int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
44
45/*
46 * Compat feature flags. Any incompat flags beyond the ones
47 * specified below will prevent use of the thin metadata.
48 */
49#define THIN_FEATURE_COMPAT_SUPP 0UL
50#define THIN_FEATURE_COMPAT_RO_SUPP 0UL
51#define THIN_FEATURE_INCOMPAT_SUPP 0UL
52
53/*
54 * Device creation/deletion.
55 */
56int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev);
57
58/*
59 * An internal snapshot.
60 *
61 * You can only snapshot a quiesced origin i.e. one that is either
62 * suspended or not instanced at all.
63 */
64int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev,
65 dm_thin_id origin);
66
67/*
68 * Deletes a virtual device from the metadata. It _is_ safe to call this
69 * when that device is open. Operations on that device will just start
70 * failing. You still need to call close() on the device.
71 */
72int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
73 dm_thin_id dev);
74
75/*
76 * Commits _all_ metadata changes: device creation, deletion, mapping
77 * updates.
78 */
79int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
80
81/*
Joe Thornberda105ed2012-07-27 15:08:15 +010082 * Discards all uncommitted changes. Rereads the superblock, rolling back
83 * to the last good transaction. Thin devices remain open.
84 * dm_thin_aborted_changes() tells you if they had uncommitted changes.
85 *
86 * If this call fails it's only useful to call dm_pool_metadata_close().
87 * All other methods will fail with -EINVAL.
88 */
89int dm_pool_abort_metadata(struct dm_pool_metadata *pmd);
90
91/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000092 * Set/get userspace transaction id.
93 */
94int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
95 uint64_t current_id,
96 uint64_t new_id);
97
98int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
99 uint64_t *result);
100
101/*
102 * Hold/get root for userspace transaction.
Joe Thornbercc8394d2012-06-03 00:30:01 +0100103 *
104 * The metadata snapshot is a copy of the current superblock (minus the
105 * space maps). Userland can access the data structures for READ
106 * operations only. A small performance hit is incurred by providing this
107 * copy of the metadata to userland due to extra copy-on-write operations
108 * on the metadata nodes. Release this as soon as you finish with it.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000109 */
Joe Thornbercc8394d2012-06-03 00:30:01 +0100110int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
111int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000112
Joe Thornbercc8394d2012-06-03 00:30:01 +0100113int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
114 dm_block_t *result);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000115
116/*
117 * Actions on a single virtual device.
118 */
119
120/*
121 * Opening the same device more than once will fail with -EBUSY.
122 */
123int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
124 struct dm_thin_device **td);
125
126int dm_pool_close_thin_device(struct dm_thin_device *td);
127
128dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
129
130struct dm_thin_lookup_result {
131 dm_block_t block;
Mike Snitzer7f214662013-12-17 13:43:31 -0500132 bool shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000133};
134
135/*
136 * Returns:
137 * -EWOULDBLOCK iff @can_block is set and would block.
138 * -ENODATA iff that mapping is not present.
139 * 0 success
140 */
141int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
142 int can_block, struct dm_thin_lookup_result *result);
143
144/*
145 * Obtain an unused block.
146 */
147int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
148
149/*
150 * Insert or remove block.
151 */
152int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
153 dm_block_t data_block);
154
155int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block);
156
157/*
158 * Queries.
159 */
Joe Thornber40db5a52012-07-27 15:08:14 +0100160bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
161
Mike Snitzer4d1662a2014-02-06 06:08:56 -0500162bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd);
163
Joe Thornberda105ed2012-07-27 15:08:15 +0100164bool dm_thin_aborted_changes(struct dm_thin_device *td);
165
Joe Thornber991d9fa2011-10-31 20:21:18 +0000166int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
167 dm_block_t *highest_mapped);
168
169int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result);
170
171int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd,
172 dm_block_t *result);
173
174int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
175 dm_block_t *result);
176
177int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
178 dm_block_t *result);
179
180int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result);
181
182int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result);
183
Joe Thornber19fa1a62013-12-17 12:09:40 -0500184int dm_pool_block_is_used(struct dm_pool_metadata *pmd, dm_block_t b, bool *result);
185
Joe Thornber991d9fa2011-10-31 20:21:18 +0000186/*
187 * Returns -ENOSPC if the new size is too small and already allocated
188 * blocks would be lost.
189 */
190int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
Joe Thornber24347e92013-05-10 14:37:19 +0100191int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000192
Joe Thornber12ba58a2012-07-27 15:08:15 +0100193/*
194 * Flicks the underlying block manager into read only mode, so you know
195 * that nothing is changing.
196 */
197void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd);
Joe Thornber9b7aaa62013-12-04 16:58:19 -0500198void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd);
Joe Thornber12ba58a2012-07-27 15:08:15 +0100199
Joe Thornberac8c3f32013-05-10 14:37:21 +0100200int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
201 dm_block_t threshold,
202 dm_sm_threshold_fn fn,
203 void *context);
204
Joe Thornber991d9fa2011-10-31 20:21:18 +0000205/*----------------------------------------------------------------*/
206
207#endif