xref: /openbmc/linux/drivers/md/dm-exception-store.h (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
13bd94003SHeinz Mauelshagen /* SPDX-License-Identifier: GPL-2.0-only */
2aea53d92SJonathan Brassow /*
3aea53d92SJonathan Brassow  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
4aea53d92SJonathan Brassow  * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
5aea53d92SJonathan Brassow  *
6aea53d92SJonathan Brassow  * Device-mapper snapshot exception store.
7aea53d92SJonathan Brassow  *
8aea53d92SJonathan Brassow  * This file is released under the GPL.
9aea53d92SJonathan Brassow  */
10aea53d92SJonathan Brassow 
11aea53d92SJonathan Brassow #ifndef _LINUX_DM_EXCEPTION_STORE
12aea53d92SJonathan Brassow #define _LINUX_DM_EXCEPTION_STORE
13aea53d92SJonathan Brassow 
14aea53d92SJonathan Brassow #include <linux/blkdev.h>
15f79ae415SNikos Tsironis #include <linux/list_bl.h>
16a159c1acSJonathan Brassow #include <linux/device-mapper.h>
17aea53d92SJonathan Brassow 
18aea53d92SJonathan Brassow /*
19aea53d92SJonathan Brassow  * The snapshot code deals with largish chunks of the disk at a
20aea53d92SJonathan Brassow  * time. Typically 32k - 512k.
21aea53d92SJonathan Brassow  */
22aea53d92SJonathan Brassow typedef sector_t chunk_t;
23aea53d92SJonathan Brassow 
24aea53d92SJonathan Brassow /*
25aea53d92SJonathan Brassow  * An exception is used where an old chunk of data has been
26aea53d92SJonathan Brassow  * replaced by a new one.
27aea53d92SJonathan Brassow  * If chunk_t is 64 bits in size, the top 8 bits of new_chunk hold the number
28aea53d92SJonathan Brassow  * of chunks that follow contiguously.  Remaining bits hold the number of the
29aea53d92SJonathan Brassow  * chunk within the device.
30aea53d92SJonathan Brassow  */
311d4989c8SJon Brassow struct dm_exception {
32f79ae415SNikos Tsironis 	struct hlist_bl_node hash_list;
33aea53d92SJonathan Brassow 
34aea53d92SJonathan Brassow 	chunk_t old_chunk;
35aea53d92SJonathan Brassow 	chunk_t new_chunk;
36aea53d92SJonathan Brassow };
37aea53d92SJonathan Brassow 
38aea53d92SJonathan Brassow /*
39aea53d92SJonathan Brassow  * Abstraction to handle the meta/layout of exception stores (the
40aea53d92SJonathan Brassow  * COW device).
41aea53d92SJonathan Brassow  */
42b2a11465SJonathan Brassow struct dm_exception_store;
43b2a11465SJonathan Brassow struct dm_exception_store_type {
44493df71cSJonathan Brassow 	const char *name;
45493df71cSJonathan Brassow 	struct module *module;
46493df71cSJonathan Brassow 
47b0d3cc01SMike Snitzer 	int (*ctr)(struct dm_exception_store *store, char *options);
48b2a11465SJonathan Brassow 
49aea53d92SJonathan Brassow 	/*
50aea53d92SJonathan Brassow 	 * Destroys this object when you've finished with it.
51aea53d92SJonathan Brassow 	 */
52b2a11465SJonathan Brassow 	void (*dtr)(struct dm_exception_store *store);
53aea53d92SJonathan Brassow 
54aea53d92SJonathan Brassow 	/*
55aea53d92SJonathan Brassow 	 * The target shouldn't read the COW device until this is
56a159c1acSJonathan Brassow 	 * called.  As exceptions are read from the COW, they are
57a159c1acSJonathan Brassow 	 * reported back via the callback.
58aea53d92SJonathan Brassow 	 */
59a159c1acSJonathan Brassow 	int (*read_metadata)(struct dm_exception_store *store,
60a159c1acSJonathan Brassow 			     int (*callback)(void *callback_context,
61a159c1acSJonathan Brassow 					     chunk_t old, chunk_t new),
62a159c1acSJonathan Brassow 			     void *callback_context);
63aea53d92SJonathan Brassow 
64aea53d92SJonathan Brassow 	/*
65aea53d92SJonathan Brassow 	 * Find somewhere to store the next exception.
66aea53d92SJonathan Brassow 	 */
671ae25f9cSJonathan Brassow 	int (*prepare_exception)(struct dm_exception_store *store,
681d4989c8SJon Brassow 				 struct dm_exception *e);
69aea53d92SJonathan Brassow 
70aea53d92SJonathan Brassow 	/*
71aea53d92SJonathan Brassow 	 * Update the metadata with this exception.
72aea53d92SJonathan Brassow 	 */
731ae25f9cSJonathan Brassow 	void (*commit_exception)(struct dm_exception_store *store,
74385277bfSMikulas Patocka 				 struct dm_exception *e, int valid,
75aea53d92SJonathan Brassow 				 void (*callback)(void *, int success),
76aea53d92SJonathan Brassow 				 void *callback_context);
77aea53d92SJonathan Brassow 
78aea53d92SJonathan Brassow 	/*
794454a621SMikulas Patocka 	 * Returns 0 if the exception store is empty.
804454a621SMikulas Patocka 	 *
814454a621SMikulas Patocka 	 * If there are exceptions still to be merged, sets
824454a621SMikulas Patocka 	 * *last_old_chunk and *last_new_chunk to the most recent
834454a621SMikulas Patocka 	 * still-to-be-merged chunk and returns the number of
844454a621SMikulas Patocka 	 * consecutive previous ones.
854454a621SMikulas Patocka 	 */
864454a621SMikulas Patocka 	int (*prepare_merge)(struct dm_exception_store *store,
874454a621SMikulas Patocka 			     chunk_t *last_old_chunk, chunk_t *last_new_chunk);
884454a621SMikulas Patocka 
894454a621SMikulas Patocka 	/*
904454a621SMikulas Patocka 	 * Clear the last n exceptions.
914454a621SMikulas Patocka 	 * nr_merged must be <= the value returned by prepare_merge.
924454a621SMikulas Patocka 	 */
934454a621SMikulas Patocka 	int (*commit_merge)(struct dm_exception_store *store, int nr_merged);
944454a621SMikulas Patocka 
954454a621SMikulas Patocka 	/*
96aea53d92SJonathan Brassow 	 * The snapshot is invalid, note this in the metadata.
97aea53d92SJonathan Brassow 	 */
981ae25f9cSJonathan Brassow 	void (*drop_snapshot)(struct dm_exception_store *store);
99aea53d92SJonathan Brassow 
100*86a3238cSHeinz Mauelshagen 	unsigned int (*status)(struct dm_exception_store *store,
1011e302a92SJonathan Brassow 			       status_type_t status, char *result,
102*86a3238cSHeinz Mauelshagen 			       unsigned int maxlen);
103a159c1acSJonathan Brassow 
104aea53d92SJonathan Brassow 	/*
105aea53d92SJonathan Brassow 	 * Return how full the snapshot is.
106aea53d92SJonathan Brassow 	 */
107985903bbSMike Snitzer 	void (*usage)(struct dm_exception_store *store,
108985903bbSMike Snitzer 		      sector_t *total_sectors, sector_t *sectors_allocated,
109985903bbSMike Snitzer 		      sector_t *metadata_sectors);
110493df71cSJonathan Brassow 
111493df71cSJonathan Brassow 	/* For internal device-mapper use only. */
112493df71cSJonathan Brassow 	struct list_head list;
113b2a11465SJonathan Brassow };
114b2a11465SJonathan Brassow 
115fc56f6fbSMike Snitzer struct dm_snapshot;
116fc56f6fbSMike Snitzer 
117b2a11465SJonathan Brassow struct dm_exception_store {
118493df71cSJonathan Brassow 	struct dm_exception_store_type *type;
119fc56f6fbSMike Snitzer 	struct dm_snapshot *snap;
12049beb2b8SJonathan Brassow 
121d0216849SJonathan Brassow 	/* Size of data blocks saved - must be a power of 2 */
122*86a3238cSHeinz Mauelshagen 	unsigned int chunk_size;
123*86a3238cSHeinz Mauelshagen 	unsigned int chunk_mask;
124*86a3238cSHeinz Mauelshagen 	unsigned int chunk_shift;
125d0216849SJonathan Brassow 
126aea53d92SJonathan Brassow 	void *context;
127b0d3cc01SMike Snitzer 
128b0d3cc01SMike Snitzer 	bool userspace_supports_overflow;
129aea53d92SJonathan Brassow };
130aea53d92SJonathan Brassow 
131aea53d92SJonathan Brassow /*
132c2411045SMikulas Patocka  * Obtain the origin or cow device used by a given snapshot.
133fc56f6fbSMike Snitzer  */
134c2411045SMikulas Patocka struct dm_dev *dm_snap_origin(struct dm_snapshot *snap);
135fc56f6fbSMike Snitzer struct dm_dev *dm_snap_cow(struct dm_snapshot *snap);
136fc56f6fbSMike Snitzer 
137fc56f6fbSMike Snitzer /*
138aea53d92SJonathan Brassow  * Funtions to manipulate consecutive chunks
139aea53d92SJonathan Brassow  */
140aea53d92SJonathan Brassow #define DM_CHUNK_CONSECUTIVE_BITS 8
141aea53d92SJonathan Brassow #define DM_CHUNK_NUMBER_BITS 56
142aea53d92SJonathan Brassow 
dm_chunk_number(chunk_t chunk)143aea53d92SJonathan Brassow static inline chunk_t dm_chunk_number(chunk_t chunk)
144aea53d92SJonathan Brassow {
145aea53d92SJonathan Brassow 	return chunk & (chunk_t)((1ULL << DM_CHUNK_NUMBER_BITS) - 1ULL);
146aea53d92SJonathan Brassow }
147aea53d92SJonathan Brassow 
dm_consecutive_chunk_count(struct dm_exception * e)148*86a3238cSHeinz Mauelshagen static inline unsigned int dm_consecutive_chunk_count(struct dm_exception *e)
149aea53d92SJonathan Brassow {
150aea53d92SJonathan Brassow 	return e->new_chunk >> DM_CHUNK_NUMBER_BITS;
151aea53d92SJonathan Brassow }
152aea53d92SJonathan Brassow 
dm_consecutive_chunk_count_inc(struct dm_exception * e)1531d4989c8SJon Brassow static inline void dm_consecutive_chunk_count_inc(struct dm_exception *e)
154aea53d92SJonathan Brassow {
155aea53d92SJonathan Brassow 	e->new_chunk += (1ULL << DM_CHUNK_NUMBER_BITS);
156aea53d92SJonathan Brassow 
157aea53d92SJonathan Brassow 	BUG_ON(!dm_consecutive_chunk_count(e));
158aea53d92SJonathan Brassow }
159aea53d92SJonathan Brassow 
dm_consecutive_chunk_count_dec(struct dm_exception * e)1601e03f97eSMikulas Patocka static inline void dm_consecutive_chunk_count_dec(struct dm_exception *e)
1611e03f97eSMikulas Patocka {
1621e03f97eSMikulas Patocka 	BUG_ON(!dm_consecutive_chunk_count(e));
1631e03f97eSMikulas Patocka 
1641e03f97eSMikulas Patocka 	e->new_chunk -= (1ULL << DM_CHUNK_NUMBER_BITS);
1651e03f97eSMikulas Patocka }
1661e03f97eSMikulas Patocka 
16771fab00aSJonathan Brassow /*
16871fab00aSJonathan Brassow  * Return the number of sectors in the device.
16971fab00aSJonathan Brassow  */
get_dev_size(struct block_device * bdev)17071fab00aSJonathan Brassow static inline sector_t get_dev_size(struct block_device *bdev)
17171fab00aSJonathan Brassow {
1726dcbb52cSChristoph Hellwig 	return bdev_nr_sectors(bdev);
17371fab00aSJonathan Brassow }
17471fab00aSJonathan Brassow 
sector_to_chunk(struct dm_exception_store * store,sector_t sector)17571fab00aSJonathan Brassow static inline chunk_t sector_to_chunk(struct dm_exception_store *store,
17671fab00aSJonathan Brassow 				      sector_t sector)
17771fab00aSJonathan Brassow {
178102c6ddbSMikulas Patocka 	return sector >> store->chunk_shift;
17971fab00aSJonathan Brassow }
18071fab00aSJonathan Brassow 
181493df71cSJonathan Brassow int dm_exception_store_type_register(struct dm_exception_store_type *type);
182493df71cSJonathan Brassow int dm_exception_store_type_unregister(struct dm_exception_store_type *type);
183493df71cSJonathan Brassow 
1842defcc3fSMikulas Patocka int dm_exception_store_set_chunk_size(struct dm_exception_store *store,
185*86a3238cSHeinz Mauelshagen 				      unsigned int chunk_size,
1862defcc3fSMikulas Patocka 				      char **error);
1872defcc3fSMikulas Patocka 
188fee1998eSJonathan Brassow int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
189fc56f6fbSMike Snitzer 			      struct dm_snapshot *snap,
190*86a3238cSHeinz Mauelshagen 			      unsigned int *args_used,
191493df71cSJonathan Brassow 			      struct dm_exception_store **store);
192493df71cSJonathan Brassow void dm_exception_store_destroy(struct dm_exception_store *store);
193493df71cSJonathan Brassow 
1944db6bfe0SAlasdair G Kergon int dm_exception_store_init(void);
1954db6bfe0SAlasdair G Kergon void dm_exception_store_exit(void);
1964db6bfe0SAlasdair G Kergon 
197aea53d92SJonathan Brassow /*
198aea53d92SJonathan Brassow  * Two exception store implementations.
199aea53d92SJonathan Brassow  */
2004db6bfe0SAlasdair G Kergon int dm_persistent_snapshot_init(void);
2014db6bfe0SAlasdair G Kergon void dm_persistent_snapshot_exit(void);
2024db6bfe0SAlasdair G Kergon 
2034db6bfe0SAlasdair G Kergon int dm_transient_snapshot_init(void);
2044db6bfe0SAlasdair G Kergon void dm_transient_snapshot_exit(void);
2054db6bfe0SAlasdair G Kergon 
206aea53d92SJonathan Brassow #endif /* _LINUX_DM_EXCEPTION_STORE */
207