xref: /openbmc/linux/drivers/md/dm-thin-metadata.h (revision 24347e9595704464f62a4ed8f46abf62b4c79cdd)
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 /*
83da105ed5SJoe Thornber  * Discards all uncommitted changes.  Rereads the superblock, rolling back
84da105ed5SJoe Thornber  * to the last good transaction.  Thin devices remain open.
85da105ed5SJoe Thornber  * dm_thin_aborted_changes() tells you if they had uncommitted changes.
86da105ed5SJoe Thornber  *
87da105ed5SJoe Thornber  * If this call fails it's only useful to call dm_pool_metadata_close().
88da105ed5SJoe Thornber  * All other methods will fail with -EINVAL.
89da105ed5SJoe Thornber  */
90da105ed5SJoe Thornber int dm_pool_abort_metadata(struct dm_pool_metadata *pmd);
91da105ed5SJoe Thornber 
92da105ed5SJoe Thornber /*
93991d9fa0SJoe Thornber  * Set/get userspace transaction id.
94991d9fa0SJoe Thornber  */
95991d9fa0SJoe Thornber int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
96991d9fa0SJoe Thornber 					uint64_t current_id,
97991d9fa0SJoe Thornber 					uint64_t new_id);
98991d9fa0SJoe Thornber 
99991d9fa0SJoe Thornber int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
100991d9fa0SJoe Thornber 					uint64_t *result);
101991d9fa0SJoe Thornber 
102991d9fa0SJoe Thornber /*
103991d9fa0SJoe Thornber  * Hold/get root for userspace transaction.
104cc8394d8SJoe Thornber  *
105cc8394d8SJoe Thornber  * The metadata snapshot is a copy of the current superblock (minus the
106cc8394d8SJoe Thornber  * space maps).  Userland can access the data structures for READ
107cc8394d8SJoe Thornber  * operations only.  A small performance hit is incurred by providing this
108cc8394d8SJoe Thornber  * copy of the metadata to userland due to extra copy-on-write operations
109cc8394d8SJoe Thornber  * on the metadata nodes.  Release this as soon as you finish with it.
110991d9fa0SJoe Thornber  */
111cc8394d8SJoe Thornber int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
112cc8394d8SJoe Thornber int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
113991d9fa0SJoe Thornber 
114cc8394d8SJoe Thornber int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
115991d9fa0SJoe Thornber 			      dm_block_t *result);
116991d9fa0SJoe Thornber 
117991d9fa0SJoe Thornber /*
118991d9fa0SJoe Thornber  * Actions on a single virtual device.
119991d9fa0SJoe Thornber  */
120991d9fa0SJoe Thornber 
121991d9fa0SJoe Thornber /*
122991d9fa0SJoe Thornber  * Opening the same device more than once will fail with -EBUSY.
123991d9fa0SJoe Thornber  */
124991d9fa0SJoe Thornber int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
125991d9fa0SJoe Thornber 			     struct dm_thin_device **td);
126991d9fa0SJoe Thornber 
127991d9fa0SJoe Thornber int dm_pool_close_thin_device(struct dm_thin_device *td);
128991d9fa0SJoe Thornber 
129991d9fa0SJoe Thornber dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
130991d9fa0SJoe Thornber 
131991d9fa0SJoe Thornber struct dm_thin_lookup_result {
132991d9fa0SJoe Thornber 	dm_block_t block;
13317b7d63fSMike Snitzer 	unsigned shared:1;
134991d9fa0SJoe Thornber };
135991d9fa0SJoe Thornber 
136991d9fa0SJoe Thornber /*
137991d9fa0SJoe Thornber  * Returns:
138991d9fa0SJoe Thornber  *   -EWOULDBLOCK iff @can_block is set and would block.
139991d9fa0SJoe Thornber  *   -ENODATA iff that mapping is not present.
140991d9fa0SJoe Thornber  *   0 success
141991d9fa0SJoe Thornber  */
142991d9fa0SJoe Thornber int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
143991d9fa0SJoe Thornber 		       int can_block, struct dm_thin_lookup_result *result);
144991d9fa0SJoe Thornber 
145991d9fa0SJoe Thornber /*
146991d9fa0SJoe Thornber  * Obtain an unused block.
147991d9fa0SJoe Thornber  */
148991d9fa0SJoe Thornber int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result);
149991d9fa0SJoe Thornber 
150991d9fa0SJoe Thornber /*
151991d9fa0SJoe Thornber  * Insert or remove block.
152991d9fa0SJoe Thornber  */
153991d9fa0SJoe Thornber int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
154991d9fa0SJoe Thornber 			 dm_block_t data_block);
155991d9fa0SJoe Thornber 
156991d9fa0SJoe Thornber int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block);
157991d9fa0SJoe Thornber 
158991d9fa0SJoe Thornber /*
159991d9fa0SJoe Thornber  * Queries.
160991d9fa0SJoe Thornber  */
16140db5a53SJoe Thornber bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
16240db5a53SJoe Thornber 
163da105ed5SJoe Thornber bool dm_thin_aborted_changes(struct dm_thin_device *td);
164da105ed5SJoe Thornber 
165991d9fa0SJoe Thornber int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
166991d9fa0SJoe Thornber 				     dm_block_t *highest_mapped);
167991d9fa0SJoe Thornber 
168991d9fa0SJoe Thornber int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result);
169991d9fa0SJoe Thornber 
170991d9fa0SJoe Thornber int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd,
171991d9fa0SJoe Thornber 				 dm_block_t *result);
172991d9fa0SJoe Thornber 
173991d9fa0SJoe Thornber int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
174991d9fa0SJoe Thornber 					  dm_block_t *result);
175991d9fa0SJoe Thornber 
176991d9fa0SJoe Thornber int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
177991d9fa0SJoe Thornber 				  dm_block_t *result);
178991d9fa0SJoe Thornber 
179991d9fa0SJoe Thornber int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result);
180991d9fa0SJoe Thornber 
181991d9fa0SJoe Thornber int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result);
182991d9fa0SJoe Thornber 
183991d9fa0SJoe Thornber /*
184991d9fa0SJoe Thornber  * Returns -ENOSPC if the new size is too small and already allocated
185991d9fa0SJoe Thornber  * blocks would be lost.
186991d9fa0SJoe Thornber  */
187991d9fa0SJoe Thornber int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
188*24347e95SJoe Thornber int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_size);
189991d9fa0SJoe Thornber 
19012ba58afSJoe Thornber /*
19112ba58afSJoe Thornber  * Flicks the underlying block manager into read only mode, so you know
19212ba58afSJoe Thornber  * that nothing is changing.
19312ba58afSJoe Thornber  */
19412ba58afSJoe Thornber void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd);
19512ba58afSJoe Thornber 
196991d9fa0SJoe Thornber /*----------------------------------------------------------------*/
197991d9fa0SJoe Thornber 
198991d9fa0SJoe Thornber #endif
199