xref: /openbmc/linux/drivers/md/dm-thin-metadata.h (revision 40db5a537655aa0b17a82a4b5596f8d27400edd8)
1991d9fa0SJoe Thornber /*
2991d9fa0SJoe Thornber  * Copyright (C) 2010-2011 Red Hat, Inc.
3991d9fa0SJoe Thornber  *
4991d9fa0SJoe Thornber  * This file is released under the GPL.
5991d9fa0SJoe Thornber  */
6991d9fa0SJoe Thornber 
7991d9fa0SJoe Thornber #ifndef DM_THIN_METADATA_H
8991d9fa0SJoe Thornber #define DM_THIN_METADATA_H
9991d9fa0SJoe Thornber 
10991d9fa0SJoe Thornber #include "persistent-data/dm-block-manager.h"
11991d9fa0SJoe Thornber 
12991d9fa0SJoe Thornber #define THIN_METADATA_BLOCK_SIZE 4096
13991d9fa0SJoe Thornber 
14c4a69ecdSMike Snitzer /*
15c4a69ecdSMike Snitzer  * The metadata device is currently limited in size.
16c4a69ecdSMike Snitzer  *
17c4a69ecdSMike Snitzer  * We have one block of index, which can hold 255 index entries.  Each
18c4a69ecdSMike Snitzer  * index entry contains allocation info about 16k metadata blocks.
19c4a69ecdSMike Snitzer  */
20c4a69ecdSMike Snitzer #define THIN_METADATA_MAX_SECTORS (255 * (1 << 14) * (THIN_METADATA_BLOCK_SIZE / (1 << SECTOR_SHIFT)))
21c4a69ecdSMike Snitzer 
22c4a69ecdSMike Snitzer /*
23c4a69ecdSMike Snitzer  * A metadata device larger than 16GB triggers a warning.
24c4a69ecdSMike Snitzer  */
25c4a69ecdSMike Snitzer #define THIN_METADATA_MAX_SECTORS_WARNING (16 * (1024 * 1024 * 1024 >> SECTOR_SHIFT))
26c4a69ecdSMike Snitzer 
27991d9fa0SJoe Thornber /*----------------------------------------------------------------*/
28991d9fa0SJoe Thornber 
29991d9fa0SJoe Thornber struct dm_pool_metadata;
30991d9fa0SJoe Thornber struct dm_thin_device;
31991d9fa0SJoe Thornber 
32991d9fa0SJoe Thornber /*
33991d9fa0SJoe Thornber  * Device identifier
34991d9fa0SJoe Thornber  */
35991d9fa0SJoe Thornber typedef uint64_t dm_thin_id;
36991d9fa0SJoe Thornber 
37991d9fa0SJoe Thornber /*
38991d9fa0SJoe Thornber  * Reopens or creates a new, empty metadata volume.
39991d9fa0SJoe Thornber  */
40991d9fa0SJoe Thornber struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
4166b1edc0SJoe Thornber 					       sector_t data_block_size,
4266b1edc0SJoe Thornber 					       bool format_device);
43991d9fa0SJoe Thornber 
44991d9fa0SJoe Thornber int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
45991d9fa0SJoe Thornber 
46991d9fa0SJoe Thornber /*
47991d9fa0SJoe Thornber  * Compat feature flags.  Any incompat flags beyond the ones
48991d9fa0SJoe Thornber  * specified below will prevent use of the thin metadata.
49991d9fa0SJoe Thornber  */
50991d9fa0SJoe Thornber #define THIN_FEATURE_COMPAT_SUPP	  0UL
51991d9fa0SJoe Thornber #define THIN_FEATURE_COMPAT_RO_SUPP	  0UL
52991d9fa0SJoe Thornber #define THIN_FEATURE_INCOMPAT_SUPP	  0UL
53991d9fa0SJoe Thornber 
54991d9fa0SJoe Thornber /*
55991d9fa0SJoe Thornber  * Device creation/deletion.
56991d9fa0SJoe Thornber  */
57991d9fa0SJoe Thornber int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev);
58991d9fa0SJoe Thornber 
59991d9fa0SJoe Thornber /*
60991d9fa0SJoe Thornber  * An internal snapshot.
61991d9fa0SJoe Thornber  *
62991d9fa0SJoe Thornber  * You can only snapshot a quiesced origin i.e. one that is either
63991d9fa0SJoe Thornber  * suspended or not instanced at all.
64991d9fa0SJoe Thornber  */
65991d9fa0SJoe Thornber int dm_pool_create_snap(struct dm_pool_metadata *pmd, dm_thin_id dev,
66991d9fa0SJoe Thornber 			dm_thin_id origin);
67991d9fa0SJoe Thornber 
68991d9fa0SJoe Thornber /*
69991d9fa0SJoe Thornber  * Deletes a virtual device from the metadata.  It _is_ safe to call this
70991d9fa0SJoe Thornber  * when that device is open.  Operations on that device will just start
71991d9fa0SJoe Thornber  * failing.  You still need to call close() on the device.
72991d9fa0SJoe Thornber  */
73991d9fa0SJoe Thornber int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
74991d9fa0SJoe Thornber 			       dm_thin_id dev);
75991d9fa0SJoe Thornber 
76991d9fa0SJoe Thornber /*
77991d9fa0SJoe Thornber  * Commits _all_ metadata changes: device creation, deletion, mapping
78991d9fa0SJoe Thornber  * updates.
79991d9fa0SJoe Thornber  */
80991d9fa0SJoe Thornber int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
81991d9fa0SJoe Thornber 
82991d9fa0SJoe Thornber /*
83991d9fa0SJoe Thornber  * Set/get userspace transaction id.
84991d9fa0SJoe Thornber  */
85991d9fa0SJoe Thornber int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
86991d9fa0SJoe Thornber 					uint64_t current_id,
87991d9fa0SJoe Thornber 					uint64_t new_id);
88991d9fa0SJoe Thornber 
89991d9fa0SJoe Thornber int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
90991d9fa0SJoe Thornber 					uint64_t *result);
91991d9fa0SJoe Thornber 
92991d9fa0SJoe Thornber /*
93991d9fa0SJoe Thornber  * Hold/get root for userspace transaction.
94cc8394d8SJoe Thornber  *
95cc8394d8SJoe Thornber  * The metadata snapshot is a copy of the current superblock (minus the
96cc8394d8SJoe Thornber  * space maps).  Userland can access the data structures for READ
97cc8394d8SJoe Thornber  * operations only.  A small performance hit is incurred by providing this
98cc8394d8SJoe Thornber  * copy of the metadata to userland due to extra copy-on-write operations
99cc8394d8SJoe Thornber  * on the metadata nodes.  Release this as soon as you finish with it.
100991d9fa0SJoe Thornber  */
101cc8394d8SJoe Thornber int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
102cc8394d8SJoe Thornber int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
103991d9fa0SJoe Thornber 
104cc8394d8SJoe Thornber int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
105991d9fa0SJoe Thornber 			      dm_block_t *result);
106991d9fa0SJoe Thornber 
107991d9fa0SJoe Thornber /*
108991d9fa0SJoe Thornber  * Actions on a single virtual device.
109991d9fa0SJoe Thornber  */
110991d9fa0SJoe Thornber 
111991d9fa0SJoe Thornber /*
112991d9fa0SJoe Thornber  * Opening the same device more than once will fail with -EBUSY.
113991d9fa0SJoe Thornber  */
114991d9fa0SJoe Thornber int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
115991d9fa0SJoe Thornber 			     struct dm_thin_device **td);
116991d9fa0SJoe Thornber 
117991d9fa0SJoe Thornber int dm_pool_close_thin_device(struct dm_thin_device *td);
118991d9fa0SJoe Thornber 
119991d9fa0SJoe Thornber dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
120991d9fa0SJoe Thornber 
121991d9fa0SJoe Thornber struct dm_thin_lookup_result {
122991d9fa0SJoe Thornber 	dm_block_t block;
12317b7d63fSMike Snitzer 	unsigned shared:1;
124991d9fa0SJoe Thornber };
125991d9fa0SJoe Thornber 
126991d9fa0SJoe Thornber /*
127991d9fa0SJoe Thornber  * Returns:
128991d9fa0SJoe Thornber  *   -EWOULDBLOCK iff @can_block is set and would block.
129991d9fa0SJoe Thornber  *   -ENODATA iff that mapping is not present.
130991d9fa0SJoe Thornber  *   0 success
131991d9fa0SJoe Thornber  */
132991d9fa0SJoe Thornber int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
133991d9fa0SJoe Thornber 		       int can_block, struct dm_thin_lookup_result *result);
134991d9fa0SJoe Thornber 
135991d9fa0SJoe Thornber /*
136991d9fa0SJoe Thornber  * Obtain an unused block.
137991d9fa0SJoe Thornber  */
138991d9fa0SJoe Thornber int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
139991d9fa0SJoe Thornber 
140991d9fa0SJoe Thornber /*
141991d9fa0SJoe Thornber  * Insert or remove block.
142991d9fa0SJoe Thornber  */
143991d9fa0SJoe Thornber int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
144991d9fa0SJoe Thornber 			 dm_block_t data_block);
145991d9fa0SJoe Thornber 
146991d9fa0SJoe Thornber int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block);
147991d9fa0SJoe Thornber 
148991d9fa0SJoe Thornber /*
149991d9fa0SJoe Thornber  * Queries.
150991d9fa0SJoe Thornber  */
151*40db5a53SJoe Thornber bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
152*40db5a53SJoe Thornber 
153991d9fa0SJoe Thornber int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
154991d9fa0SJoe Thornber 				     dm_block_t *highest_mapped);
155991d9fa0SJoe Thornber 
156991d9fa0SJoe Thornber int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result);
157991d9fa0SJoe Thornber 
158991d9fa0SJoe Thornber int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd,
159991d9fa0SJoe Thornber 				 dm_block_t *result);
160991d9fa0SJoe Thornber 
161991d9fa0SJoe Thornber int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
162991d9fa0SJoe Thornber 					  dm_block_t *result);
163991d9fa0SJoe Thornber 
164991d9fa0SJoe Thornber int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
165991d9fa0SJoe Thornber 				  dm_block_t *result);
166991d9fa0SJoe Thornber 
167991d9fa0SJoe Thornber int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result);
168991d9fa0SJoe Thornber 
169991d9fa0SJoe Thornber int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result);
170991d9fa0SJoe Thornber 
171991d9fa0SJoe Thornber /*
172991d9fa0SJoe Thornber  * Returns -ENOSPC if the new size is too small and already allocated
173991d9fa0SJoe Thornber  * blocks would be lost.
174991d9fa0SJoe Thornber  */
175991d9fa0SJoe Thornber int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
176991d9fa0SJoe Thornber 
177991d9fa0SJoe Thornber /*----------------------------------------------------------------*/
178991d9fa0SJoe Thornber 
179991d9fa0SJoe Thornber #endif
180