blob: 1632fec1660d0ac32f97fe41832c207ab6a8c7ed [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"
11
12#define THIN_METADATA_BLOCK_SIZE 4096
13
Mike Snitzerc4a69ec2012-03-28 18:41:28 +010014/*
15 * The metadata device is currently limited in size.
16 *
17 * We have one block of index, which can hold 255 index entries. Each
18 * index entry contains allocation info about 16k metadata blocks.
19 */
20#define THIN_METADATA_MAX_SECTORS (255 * (1 << 14) * (THIN_METADATA_BLOCK_SIZE / (1 << SECTOR_SHIFT)))
21
22/*
23 * A metadata device larger than 16GB triggers a warning.
24 */
25#define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
26
Joe Thornber991d9fa2011-10-31 20:21:18 +000027/*----------------------------------------------------------------*/
28
29struct dm_pool_metadata;
30struct dm_thin_device;
31
32/*
33 * Device identifier
34 */
35typedef uint64_t dm_thin_id;
36
37/*
38 * Reopens or creates a new, empty metadata volume.
39 */
40struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
Joe Thornber66b1edc2012-07-27 15:08:14 +010041 sector_t data_block_size,
42 bool format_device);
Joe Thornber991d9fa2011-10-31 20:21:18 +000043
44int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
45
46/*
47 * Compat feature flags. Any incompat flags beyond the ones
48 * specified below will prevent use of the thin metadata.
49 */
50#define THIN_FEATURE_COMPAT_SUPP 0UL
51#define THIN_FEATURE_COMPAT_RO_SUPP 0UL
52#define THIN_FEATURE_INCOMPAT_SUPP 0UL
53
54/*
55 * Device creation/deletion.
56 */
57int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev);
58
59/*
60 * An internal snapshot.
61 *
62 * You can only snapshot a quiesced origin i.e. one that is either
63 * suspended or not instanced at all.
64 */
65int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev,
66 dm_thin_id origin);
67
68/*
69 * Deletes a virtual device from the metadata. It _is_ safe to call this
70 * when that device is open. Operations on that device will just start
71 * failing. You still need to call close() on the device.
72 */
73int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
74 dm_thin_id dev);
75
76/*
77 * Commits _all_ metadata changes: device creation, deletion, mapping
78 * updates.
79 */
80int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
81
82/*
83 * Set/get userspace transaction id.
84 */
85int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
86 uint64_t current_id,
87 uint64_t new_id);
88
89int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
90 uint64_t *result);
91
92/*
93 * Hold/get root for userspace transaction.
Joe Thornbercc8394d2012-06-03 00:30:01 +010094 *
95 * The metadata snapshot is a copy of the current superblock (minus the
96 * space maps). Userland can access the data structures for READ
97 * operations only. A small performance hit is incurred by providing this
98 * copy of the metadata to userland due to extra copy-on-write operations
99 * on the metadata nodes. Release this as soon as you finish with it.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000100 */
Joe Thornbercc8394d2012-06-03 00:30:01 +0100101int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
102int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000103
Joe Thornbercc8394d2012-06-03 00:30:01 +0100104int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
105 dm_block_t *result);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000106
107/*
108 * Actions on a single virtual device.
109 */
110
111/*
112 * Opening the same device more than once will fail with -EBUSY.
113 */
114int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
115 struct dm_thin_device **td);
116
117int dm_pool_close_thin_device(struct dm_thin_device *td);
118
119dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
120
121struct dm_thin_lookup_result {
122 dm_block_t block;
Mike Snitzer17b7d632012-07-27 15:07:57 +0100123 unsigned shared:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000124};
125
126/*
127 * Returns:
128 * -EWOULDBLOCK iff @can_block is set and would block.
129 * -ENODATA iff that mapping is not present.
130 * 0 success
131 */
132int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
133 int can_block, struct dm_thin_lookup_result *result);
134
135/*
136 * Obtain an unused block.
137 */
138int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
139
140/*
141 * Insert or remove block.
142 */
143int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
144 dm_block_t data_block);
145
146int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block);
147
148/*
149 * Queries.
150 */
Joe Thornber40db5a52012-07-27 15:08:14 +0100151bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
152
Joe Thornber991d9fa2011-10-31 20:21:18 +0000153int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
154 dm_block_t *highest_mapped);
155
156int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result);
157
158int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd,
159 dm_block_t *result);
160
161int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
162 dm_block_t *result);
163
164int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
165 dm_block_t *result);
166
167int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result);
168
169int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result);
170
171/*
172 * Returns -ENOSPC if the new size is too small and already allocated
173 * blocks would be lost.
174 */
175int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
176
177/*----------------------------------------------------------------*/
178
179#endif