blob: 921d15ee56a0c687bd09e54846d90f2bbb082c86 [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
Mike Snitzer07f2b6e2014-02-14 11:58:41 -050028/*
29 * Thin metadata superblock flags.
30 */
31#define THIN_METADATA_NEEDS_CHECK_FLAG (1 << 0)
32
Joe Thornber991d9fa2011-10-31 20:21:18 +000033struct dm_pool_metadata;
34struct dm_thin_device;
35
36/*
37 * Device identifier
38 */
39typedef uint64_t dm_thin_id;
40
41/*
42 * Reopens or creates a new, empty metadata volume.
43 */
44struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
Joe Thornber66b1edc2012-07-27 15:08:14 +010045 sector_t data_block_size,
46 bool format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +000047
48int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
49
50/*
51 * Compat feature flags. Any incompat flags beyond the ones
52 * specified below will prevent use of the thin metadata.
53 */
54#define THIN_FEATURE_COMPAT_SUPP 0UL
55#define THIN_FEATURE_COMPAT_RO_SUPP 0UL
56#define THIN_FEATURE_INCOMPAT_SUPP 0UL
57
58/*
59 * Device creation/deletion.
60 */
61int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev);
62
63/*
64 * An internal snapshot.
65 *
66 * You can only snapshot a quiesced origin i.e. one that is either
67 * suspended or not instanced at all.
68 */
69int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev,
70 dm_thin_id origin);
71
72/*
73 * Deletes a virtual device from the metadata. It _is_ safe to call this
74 * when that device is open. Operations on that device will just start
75 * failing. You still need to call close() on the device.
76 */
77int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
78 dm_thin_id dev);
79
80/*
81 * Commits _all_ metadata changes: device creation, deletion, mapping
82 * updates.
83 */
84int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
85
86/*
Joe Thornberda105ed2012-07-27 15:08:15 +010087 * Discards all uncommitted changes. Rereads the superblock, rolling back
88 * to the last good transaction. Thin devices remain open.
89 * dm_thin_aborted_changes() tells you if they had uncommitted changes.
90 *
91 * If this call fails it's only useful to call dm_pool_metadata_close().
92 * All other methods will fail with -EINVAL.
93 */
94int dm_pool_abort_metadata(struct dm_pool_metadata *pmd);
95
96/*
Joe Thornber991d9fa2011-10-31 20:21:18 +000097 * Set/get userspace transaction id.
98 */
99int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
100 uint64_t current_id,
101 uint64_t new_id);
102
103int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
104 uint64_t *result);
105
106/*
107 * Hold/get root for userspace transaction.
Joe Thornbercc8394d2012-06-03 00:30:01 +0100108 *
109 * The metadata snapshot is a copy of the current superblock (minus the
110 * space maps). Userland can access the data structures for READ
111 * operations only. A small performance hit is incurred by providing this
112 * copy of the metadata to userland due to extra copy-on-write operations
113 * on the metadata nodes. Release this as soon as you finish with it.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000114 */
Joe Thornbercc8394d2012-06-03 00:30:01 +0100115int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
116int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000117
Joe Thornbercc8394d2012-06-03 00:30:01 +0100118int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
119 dm_block_t *result);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000120
121/*
122 * Actions on a single virtual device.
123 */
124
125/*
126 * Opening the same device more than once will fail with -EBUSY.
127 */
128int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
129 struct dm_thin_device **td);
130
131int dm_pool_close_thin_device(struct dm_thin_device *td);
132
133dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
134
135struct dm_thin_lookup_result {
136 dm_block_t block;
Mike Snitzer7f214662013-12-17 13:43:31 -0500137 bool shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000138};
139
140/*
141 * Returns:
Joe Thornbere5cfc692014-10-06 15:24:55 +0100142 * -EWOULDBLOCK iff @can_issue_io is set and would issue IO
Joe Thornber991d9fa2011-10-31 20:21:18 +0000143 * -ENODATA iff that mapping is not present.
144 * 0 success
145 */
146int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
Joe Thornbere5cfc692014-10-06 15:24:55 +0100147 int can_issue_io, struct dm_thin_lookup_result *result);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000148
149/*
150 * Obtain an unused block.
151 */
152int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
153
154/*
155 * Insert or remove block.
156 */
157int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
158 dm_block_t data_block);
159
160int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block);
161
162/*
163 * Queries.
164 */
Joe Thornber40db5a52012-07-27 15:08:14 +0100165bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
166
Mike Snitzer4d1662a2014-02-06 06:08:56 -0500167bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd);
168
Joe Thornberda105ed2012-07-27 15:08:15 +0100169bool dm_thin_aborted_changes(struct dm_thin_device *td);
170
Joe Thornber991d9fa2011-10-31 20:21:18 +0000171int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
172 dm_block_t *highest_mapped);
173
174int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result);
175
176int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd,
177 dm_block_t *result);
178
179int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
180 dm_block_t *result);
181
182int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
183 dm_block_t *result);
184
185int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result);
186
187int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result);
188
Joe Thornber19fa1a62013-12-17 12:09:40 -0500189int dm_pool_block_is_used(struct dm_pool_metadata *pmd, dm_block_t b, bool *result);
190
Joe Thornber991d9fa2011-10-31 20:21:18 +0000191/*
192 * Returns -ENOSPC if the new size is too small and already allocated
193 * blocks would be lost.
194 */
195int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
Joe Thornber24347e92013-05-10 14:37:19 +0100196int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000197
Joe Thornber12ba58a2012-07-27 15:08:15 +0100198/*
199 * Flicks the underlying block manager into read only mode, so you know
200 * that nothing is changing.
201 */
202void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd);
Joe Thornber9b7aaa62013-12-04 16:58:19 -0500203void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd);
Joe Thornber12ba58a2012-07-27 15:08:15 +0100204
Joe Thornberac8c3f32013-05-10 14:37:21 +0100205int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
206 dm_block_t threshold,
207 dm_sm_threshold_fn fn,
208 void *context);
209
Mike Snitzer07f2b6e2014-02-14 11:58:41 -0500210/*
211 * Updates the superblock immediately.
212 */
213int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd);
214bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd);
215
Joe Thornber8a01a6a2014-10-06 15:28:30 +0100216/*
217 * Issue any prefetches that may be useful.
218 */
219void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd);
220
Joe Thornber991d9fa2011-10-31 20:21:18 +0000221/*----------------------------------------------------------------*/
222
223#endif