summaryrefslogtreecommitdiffstats
path: root/drivers/md/dm-vdo/indexer/indexer.h
blob: 3744aaf625b05108e6a85cd4c32191c69509ebff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright 2023 Red Hat
 */

#ifndef INDEXER_H
#define INDEXER_H

#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/wait.h>

#include "funnel-queue.h"

/*
 * UDS public API
 *
 * The Universal Deduplication System (UDS) is an efficient name-value store. When used for
 * deduplicating storage, the names are generally hashes of data blocks and the associated data is
 * where that block is located on the underlying storage medium. The stored names are expected to
 * be randomly distributed among the space of possible names. If this assumption is violated, the
 * UDS index will store fewer names than normal but will otherwise continue to work. The data
 * associated with each name can be any 16-byte value.
 *
 * A client must first create an index session to interact with an index. Once created, the session
 * can be shared among multiple threads or users. When a session is destroyed, it will also close
 * and save any associated index.
 *
 * To make a request, a client must allocate a uds_request structure and set the required fields
 * before launching it. UDS will invoke the provided callback to complete the request. After the
 * callback has been called, the uds_request structure can be freed or reused for a new request.
 * There are five types of requests:
 *
 * A UDS_UPDATE request will associate the provided name with the provided data. Any previous data
 * associated with that name will be discarded.
 *
 * A UDS_QUERY request will return the data associated with the provided name, if any. The entry
 * for the name will also be marked as most recent, as if the data had been updated.
 *
 * A UDS_POST request is a combination of UDS_QUERY and UDS_UPDATE. If there is already data
 * associated with the provided name, that data is returned. If there is no existing association,
 * the name is associated with the newly provided data. This request is equivalent to a UDS_QUERY
 * request followed by a UDS_UPDATE request if no data is found, but it is much more efficient.
 *
 * A UDS_QUERY_NO_UPDATE request will return the data associated with the provided name, but will
 * not change the recency of the entry for the name. This request is primarily useful for testing,
 * to determine whether an entry exists without changing the internal state of the index.
 *
 * A UDS_DELETE request removes any data associated with the provided name. This operation is
 * generally not necessary, because the index will automatically discard its oldest entries once it
 * becomes full.
 */

/* General UDS constants and structures */

enum uds_request_type {
	/* Create or update the mapping for a name, and make the name most recent. */
	UDS_UPDATE,

	/* Return any mapped data for a name, and make the name most recent. */
	UDS_QUERY,

	/*
	 * Return any mapped data for a name, or map the provided data to the name if there is no
	 * current data, and make the name most recent.
	 */
	UDS_POST,

	/* Return any mapped data for a name without updating its recency. */
	UDS_QUERY_NO_UPDATE,

	/* Remove any mapping for a name. */
	UDS_DELETE,

};

enum uds_open_index_type {
	/* Create a new index. */
	UDS_CREATE,

	/* Load an existing index and try to recover if necessary. */
	UDS_LOAD,

	/* Load an existing index, but only if it was saved cleanly. */
	UDS_NO_REBUILD,
};

enum {
	/* The record name size in bytes */
	UDS_RECORD_NAME_SIZE = 16,
	/* The maximum record data size in bytes */
	UDS_RECORD_DATA_SIZE = 16,
};

/*
 * A type representing a UDS memory configuration which is either a positive integer number of
 * gigabytes or one of the six special constants for configurations smaller than one gigabyte.
 */
typedef int uds_memory_config_size_t;

enum {
	/* The maximum configurable amount of memory */
	UDS_MEMORY_CONFIG_MAX = 1024,
	/* Flag indicating that the index has one less chapter than usual */
	UDS_MEMORY_CONFIG_REDUCED = 0x1000,
	UDS_MEMORY_CONFIG_REDUCED_MAX = 1024 + UDS_MEMORY_CONFIG_REDUCED,
	/* Special values indicating sizes less than 1 GB */
	UDS_MEMORY_CONFIG_256MB = -256,
	UDS_MEMORY_CONFIG_512MB = -512,
	UDS_MEMORY_CONFIG_768MB = -768,
	UDS_MEMORY_CONFIG_REDUCED_256MB = -1280,
	UDS_MEMORY_CONFIG_REDUCED_512MB = -1536,
	UDS_MEMORY_CONFIG_REDUCED_768MB = -1792,
};

struct uds_record_name {
	unsigned char name[UDS_RECORD_NAME_SIZE];
};

struct uds_record_data {
	unsigned char data[UDS_RECORD_DATA_SIZE];
};

struct uds_volume_record {
	struct uds_record_name name;
	struct uds_record_data data;
};

struct uds_parameters {
	/* The block_device used for storage */
	struct block_device *bdev;
	/* The maximum allowable size of the index on storage */
	size_t size;
	/* The offset where the index should start */
	off_t offset;
	/* The maximum memory allocation, in GB */
	uds_memory_config_size_t memory_size;
	/* Whether the index should include sparse chapters */
	bool sparse;
	/* A 64-bit nonce to validate the index */
	u64 nonce;
	/* The number of threads used to process index requests */
	unsigned int zone_count;
	/* The number of threads used to read volume pages */
	unsigned int read_threads;
};

/*
 * These statistics capture characteristics of the current index, including resource usage and
 * requests processed since the index was opened.
 */
struct uds_index_stats {
	/* The total number of records stored in the index */
	u64 entries_indexed;
	/* An estimate of the index's memory usage, in bytes */
	u64 memory_used;
	/* The number of collisions recorded in the volume index */
	u64 collisions;
	/* The number of entries discarded from the index since startup */
	u64 entries_discarded;
	/* The time at which these statistics were fetched */
	s64 current_time;
	/* The number of post calls that found an existing entry */
	u64 posts_found;
	/* The number of post calls that added an entry */
	u64 posts_not_found;
	/*
	 * The number of post calls that found an existing entry that is current enough to only
	 * exist in memory and not have been committed to disk yet
	 */
	u64 in_memory_posts_found;
	/*
	 * The number of post calls that found an existing entry in the dense portion of the index
	 */
	u64 dense_posts_found;
	/*
	 * The number of post calls that found an existing entry in the sparse portion of the index
	 */
	u64 sparse_posts_found;
	/* The number of update calls that updated an existing entry */
	u64 updates_found;
	/* The number of update calls that added a new entry */
	u64 updates_not_found;
	/* The number of delete requests that deleted an existing entry */
	u64 deletions_found;
	/* The number of delete requests that did nothing */
	u64 deletions_not_found;
	/* The number of query calls that found existing entry */
	u64 queries_found;
	/* The number of query calls that did not find an entry */
	u64 queries_not_found;
	/* The total number of requests processed */
	u64 requests;
};

enum uds_index_region {
	/* No location information has been determined */
	UDS_LOCATION_UNKNOWN = 0,
	/* The index page entry has been found */
	UDS_LOCATION_INDEX_PAGE_LOOKUP,
	/* The record page entry has been found */
	UDS_LOCATION_RECORD_PAGE_LOOKUP,
	/* The record is not in the index */
	UDS_LOCATION_UNAVAILABLE,
	/* The record was found in the open chapter */
	UDS_LOCATION_IN_OPEN_CHAPTER,
	/* The record was found in the dense part of the index */
	UDS_LOCATION_IN_DENSE,
	/* The record was found in the sparse part of the index */
	UDS_LOCATION_IN_SPARSE,
} __packed;

/* Zone message requests are used to communicate between index zones. */
enum uds_zone_message_type {
	/* A standard request with no message */
	UDS_MESSAGE_NONE = 0,
	/* Add a chapter to the sparse chapter index cache */
	UDS_MESSAGE_SPARSE_CACHE_BARRIER,
	/* Close a chapter to keep the zone from falling behind */
	UDS_MESSAGE_ANNOUNCE_CHAPTER_CLOSED,
} __packed;

struct uds_zone_message {
	/* The type of message, determining how it will be processed */
	enum uds_zone_message_type type;
	/* The virtual chapter number to which the message applies */
	u64 virtual_chapter;
};

struct uds_index_session;
struct uds_index;
struct uds_request;

/* Once this callback has been invoked, the uds_request structure can be reused or freed. */
typedef void (*uds_request_callback_fn)(struct uds_request *request);

struct uds_request {
	/* These input fields must be set before launching a request. */

	/* The name of the record to look up or create */
	struct uds_record_name record_name;
	/* New data to associate with the record name, if applicable */
	struct uds_record_data new_metadata;
	/* A callback to invoke when the request is complete */
	uds_request_callback_fn callback;
	/* The index session that will manage this request */
	struct uds_index_session *session;
	/* The type of operation to perform, as describe above */
	enum uds_request_type type;

	/* These output fields are set when a request is complete. */

	/* The existing data associated with the request name, if any */
	struct uds_record_data old_metadata;
	/* Either UDS_SUCCESS or an error code for the request */
	int status;
	/* True if the record name had an existing entry in the index */
	bool found;

	/*
	 * The remaining fields are used internally and should not be altered by clients. The index
	 * relies on zone_number being the first field in this section.
	 */

	/* The number of the zone which will process this request*/
	unsigned int zone_number;
	/* A link for adding a request to a lock-free queue */
	struct funnel_queue_entry queue_link;
	/* A link for adding a request to a standard linked list */
	struct uds_request *next_request;
	/* A pointer to the index processing this request */
	struct uds_index *index;
	/* Control message for coordinating between zones */
	struct uds_zone_message zone_message;
	/* If true, process request immediately by waking the worker thread */
	bool unbatched;
	/* If true, continue this request before processing newer requests */
	bool requeued;
	/* The virtual chapter containing the record name, if known */
	u64 virtual_chapter;
	/* The region of the index containing the record name */
	enum uds_index_region location;
};

/* Compute the number of bytes needed to store an index. */
int __must_check uds_compute_index_size(const struct uds_parameters *parameters,
					u64 *index_size);

/* A session is required for most index operations. */
int __must_check uds_create_index_session(struct uds_index_session **session);

/* Destroying an index session also closes and saves the associated index. */
int uds_destroy_index_session(struct uds_index_session *session);

/*
 * Create or open an index with an existing session. This operation fails if the index session is
 * suspended, or if there is already an open index.
 */
int __must_check uds_open_index(enum uds_open_index_type open_type,
				const struct uds_parameters *parameters,
				struct uds_index_session *session);

/*
 * Wait until all callbacks for index operations are complete, and prevent new index operations
 * from starting. New index operations will fail with EBUSY until the session is resumed. Also
 * optionally saves the index.
 */
int __must_check uds_suspend_index_session(struct uds_index_session *session, bool save);

/*
 * Allow new index operations for an index, whether it was suspended or not. If the index is
 * suspended and the supplied block device differs from the current backing store, the index will
 * start using the new backing store instead.
 */
int __must_check uds_resume_index_session(struct uds_index_session *session,
					  struct block_device *bdev);

/* Wait until all outstanding index operations are complete. */
int __must_check uds_flush_index_session(struct uds_index_session *session);

/* Close an index. This operation fails if the index session is suspended. */
int __must_check uds_close_index(struct uds_index_session *session);

/* Get index statistics since the last time the index was opened. */
int __must_check uds_get_index_session_stats(struct uds_index_session *session,
					     struct uds_index_stats *stats);

/* This function will fail if any required field of the request is not set. */
int __must_check uds_launch_request(struct uds_request *request);

struct cond_var {
	wait_queue_head_t wait_queue;
};

static inline void uds_init_cond(struct cond_var *cv)
{
	init_waitqueue_head(&cv->wait_queue);
}

static inline void uds_signal_cond(struct cond_var *cv)
{
	wake_up(&cv->wait_queue);
}

static inline void uds_broadcast_cond(struct cond_var *cv)
{
	wake_up_all(&cv->wait_queue);
}

void uds_wait_cond(struct cond_var *cv, struct mutex *mutex);

#endif /* INDEXER_H */