summaryrefslogtreecommitdiff
path: root/drivers/md/dm-vdo/indexer/config.c
blob: cd20ee8b9a02467c8cbb0745d8a26c740e77edef (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright 2023 Red Hat
 */

#include "config.h"

#include "logger.h"
#include "memory-alloc.h"
#include "numeric.h"
#include "string-utils.h"
#include "thread-utils.h"

static const u8 INDEX_CONFIG_MAGIC[] = "ALBIC";
static const u8 INDEX_CONFIG_VERSION_6_02[] = "06.02";
static const u8 INDEX_CONFIG_VERSION_8_02[] = "08.02";

#define DEFAULT_VOLUME_READ_THREADS 2
#define MAX_VOLUME_READ_THREADS 16
#define INDEX_CONFIG_MAGIC_LENGTH (sizeof(INDEX_CONFIG_MAGIC) - 1)
#define INDEX_CONFIG_VERSION_LENGTH ((int)(sizeof(INDEX_CONFIG_VERSION_6_02) - 1))

static bool is_version(const u8 *version, u8 *buffer)
{
	return memcmp(version, buffer, INDEX_CONFIG_VERSION_LENGTH) == 0;
}

static bool are_matching_configurations(struct uds_configuration *saved_config,
					struct index_geometry *saved_geometry,
					struct uds_configuration *user)
{
	struct index_geometry *geometry = user->geometry;
	bool result = true;

	if (saved_geometry->record_pages_per_chapter != geometry->record_pages_per_chapter) {
		uds_log_error("Record pages per chapter (%u) does not match (%u)",
			      saved_geometry->record_pages_per_chapter,
			      geometry->record_pages_per_chapter);
		result = false;
	}

	if (saved_geometry->chapters_per_volume != geometry->chapters_per_volume) {
		uds_log_error("Chapter count (%u) does not match (%u)",
			      saved_geometry->chapters_per_volume,
			      geometry->chapters_per_volume);
		result = false;
	}

	if (saved_geometry->sparse_chapters_per_volume != geometry->sparse_chapters_per_volume) {
		uds_log_error("Sparse chapter count (%u) does not match (%u)",
			      saved_geometry->sparse_chapters_per_volume,
			      geometry->sparse_chapters_per_volume);
		result = false;
	}

	if (saved_config->cache_chapters != user->cache_chapters) {
		uds_log_error("Cache size (%u) does not match (%u)",
			      saved_config->cache_chapters, user->cache_chapters);
		result = false;
	}

	if (saved_config->volume_index_mean_delta != user->volume_index_mean_delta) {
		uds_log_error("Volume index mean delta (%u) does not match (%u)",
			      saved_config->volume_index_mean_delta,
			      user->volume_index_mean_delta);
		result = false;
	}

	if (saved_geometry->bytes_per_page != geometry->bytes_per_page) {
		uds_log_error("Bytes per page value (%zu) does not match (%zu)",
			      saved_geometry->bytes_per_page, geometry->bytes_per_page);
		result = false;
	}

	if (saved_config->sparse_sample_rate != user->sparse_sample_rate) {
		uds_log_error("Sparse sample rate (%u) does not match (%u)",
			      saved_config->sparse_sample_rate,
			      user->sparse_sample_rate);
		result = false;
	}

	if (saved_config->nonce != user->nonce) {
		uds_log_error("Nonce (%llu) does not match (%llu)",
			      (unsigned long long) saved_config->nonce,
			      (unsigned long long) user->nonce);
		result = false;
	}

	return result;
}

/* Read the configuration and validate it against the provided one. */
int uds_validate_config_contents(struct buffered_reader *reader,
				 struct uds_configuration *user_config)
{
	int result;
	struct uds_configuration config;
	struct index_geometry geometry;
	u8 version_buffer[INDEX_CONFIG_VERSION_LENGTH];
	u32 bytes_per_page;
	u8 buffer[sizeof(struct uds_configuration_6_02)];
	size_t offset = 0;

	result = uds_verify_buffered_data(reader, INDEX_CONFIG_MAGIC,
					  INDEX_CONFIG_MAGIC_LENGTH);
	if (result != UDS_SUCCESS)
		return result;

	result = uds_read_from_buffered_reader(reader, version_buffer,
					       INDEX_CONFIG_VERSION_LENGTH);
	if (result != UDS_SUCCESS)
		return uds_log_error_strerror(result, "cannot read index config version");

	if (!is_version(INDEX_CONFIG_VERSION_6_02, version_buffer) &&
	    !is_version(INDEX_CONFIG_VERSION_8_02, version_buffer)) {
		return uds_log_error_strerror(UDS_CORRUPT_DATA,
					      "unsupported configuration version: '%.*s'",
					      INDEX_CONFIG_VERSION_LENGTH,
					      version_buffer);
	}

	result = uds_read_from_buffered_reader(reader, buffer, sizeof(buffer));
	if (result != UDS_SUCCESS)
		return uds_log_error_strerror(result, "cannot read config data");

	decode_u32_le(buffer, &offset, &geometry.record_pages_per_chapter);
	decode_u32_le(buffer, &offset, &geometry.chapters_per_volume);
	decode_u32_le(buffer, &offset, &geometry.sparse_chapters_per_volume);
	decode_u32_le(buffer, &offset, &config.cache_chapters);
	offset += sizeof(u32);
	decode_u32_le(buffer, &offset, &config.volume_index_mean_delta);
	decode_u32_le(buffer, &offset, &bytes_per_page);
	geometry.bytes_per_page = bytes_per_page;
	decode_u32_le(buffer, &offset, &config.sparse_sample_rate);
	decode_u64_le(buffer, &offset, &config.nonce);

	result = ASSERT(offset == sizeof(struct uds_configuration_6_02),
			"%zu bytes read but not decoded",
			sizeof(struct uds_configuration_6_02) - offset);
	if (result != UDS_SUCCESS)
		return UDS_CORRUPT_DATA;

	if (is_version(INDEX_CONFIG_VERSION_6_02, version_buffer)) {
		user_config->geometry->remapped_virtual = 0;
		user_config->geometry->remapped_physical = 0;
	} else {
		u8 remapping[sizeof(u64) + sizeof(u64)];

		result = uds_read_from_buffered_reader(reader, remapping,
						       sizeof(remapping));
		if (result != UDS_SUCCESS)
			return uds_log_error_strerror(result, "cannot read converted config");

		offset = 0;
		decode_u64_le(remapping, &offset,
			      &user_config->geometry->remapped_virtual);
		decode_u64_le(remapping, &offset,
			      &user_config->geometry->remapped_physical);
	}

	if (!are_matching_configurations(&config, &geometry, user_config)) {
		uds_log_warning("Supplied configuration does not match save");
		return UDS_NO_INDEX;
	}

	return UDS_SUCCESS;
}

/*
 * Write the configuration to stable storage. If the superblock version is < 4, write the 6.02
 * version; otherwise write the 8.02 version, indicating the configuration is for an index that has
 * been reduced by one chapter.
 */
int uds_write_config_contents(struct buffered_writer *writer,
			      struct uds_configuration *config, u32 version)
{
	int result;
	struct index_geometry *geometry = config->geometry;
	u8 buffer[sizeof(struct uds_configuration_8_02)];
	size_t offset = 0;

	result = uds_write_to_buffered_writer(writer, INDEX_CONFIG_MAGIC,
					      INDEX_CONFIG_MAGIC_LENGTH);
	if (result != UDS_SUCCESS)
		return result;

	/*
	 * If version is < 4, the index has not been reduced by a chapter so it must be written out
	 * as version 6.02 so that it is still compatible with older versions of UDS.
	 */
	if (version >= 4) {
		result = uds_write_to_buffered_writer(writer, INDEX_CONFIG_VERSION_8_02,
						      INDEX_CONFIG_VERSION_LENGTH);
		if (result != UDS_SUCCESS)
			return result;
	} else {
		result = uds_write_to_buffered_writer(writer, INDEX_CONFIG_VERSION_6_02,
						      INDEX_CONFIG_VERSION_LENGTH);
		if (result != UDS_SUCCESS)
			return result;
	}

	encode_u32_le(buffer, &offset, geometry->record_pages_per_chapter);
	encode_u32_le(buffer, &offset, geometry->chapters_per_volume);
	encode_u32_le(buffer, &offset, geometry->sparse_chapters_per_volume);
	encode_u32_le(buffer, &offset, config->cache_chapters);
	encode_u32_le(buffer, &offset, 0);
	encode_u32_le(buffer, &offset, config->volume_index_mean_delta);
	encode_u32_le(buffer, &offset, geometry->bytes_per_page);
	encode_u32_le(buffer, &offset, config->sparse_sample_rate);
	encode_u64_le(buffer, &offset, config->nonce);

	result = ASSERT(offset == sizeof(struct uds_configuration_6_02),
			"%zu bytes encoded, of %zu expected", offset,
			sizeof(struct uds_configuration_6_02));
	if (result != UDS_SUCCESS)
		return result;

	if (version >= 4) {
		encode_u64_le(buffer, &offset, geometry->remapped_virtual);
		encode_u64_le(buffer, &offset, geometry->remapped_physical);
	}

	return uds_write_to_buffered_writer(writer, buffer, offset);
}

/* Compute configuration parameters that depend on memory size. */
static int compute_memory_sizes(uds_memory_config_size_t mem_gb, bool sparse,
				u32 *chapters_per_volume, u32 *record_pages_per_chapter,
				u32 *sparse_chapters_per_volume)
{
	u32 reduced_chapters = 0;
	u32 base_chapters;

	if (mem_gb == UDS_MEMORY_CONFIG_256MB) {
		base_chapters = DEFAULT_CHAPTERS_PER_VOLUME;
		*record_pages_per_chapter = SMALL_RECORD_PAGES_PER_CHAPTER;
	} else if (mem_gb == UDS_MEMORY_CONFIG_512MB) {
		base_chapters = DEFAULT_CHAPTERS_PER_VOLUME;
		*record_pages_per_chapter = 2 * SMALL_RECORD_PAGES_PER_CHAPTER;
	} else if (mem_gb == UDS_MEMORY_CONFIG_768MB) {
		base_chapters = DEFAULT_CHAPTERS_PER_VOLUME;
		*record_pages_per_chapter = 3 * SMALL_RECORD_PAGES_PER_CHAPTER;
	} else if ((mem_gb >= 1) && (mem_gb <= UDS_MEMORY_CONFIG_MAX)) {
		base_chapters = mem_gb * DEFAULT_CHAPTERS_PER_VOLUME;
		*record_pages_per_chapter = DEFAULT_RECORD_PAGES_PER_CHAPTER;
	} else if (mem_gb == UDS_MEMORY_CONFIG_REDUCED_256MB) {
		reduced_chapters = 1;
		base_chapters = DEFAULT_CHAPTERS_PER_VOLUME;
		*record_pages_per_chapter = SMALL_RECORD_PAGES_PER_CHAPTER;
	} else if (mem_gb == UDS_MEMORY_CONFIG_REDUCED_512MB) {
		reduced_chapters = 1;
		base_chapters = DEFAULT_CHAPTERS_PER_VOLUME;
		*record_pages_per_chapter = 2 * SMALL_RECORD_PAGES_PER_CHAPTER;
	} else if (mem_gb == UDS_MEMORY_CONFIG_REDUCED_768MB) {
		reduced_chapters = 1;
		base_chapters = DEFAULT_CHAPTERS_PER_VOLUME;
		*record_pages_per_chapter = 3 * SMALL_RECORD_PAGES_PER_CHAPTER;
	} else if ((mem_gb >= 1 + UDS_MEMORY_CONFIG_REDUCED) &&
		   (mem_gb <= UDS_MEMORY_CONFIG_REDUCED_MAX)) {
		reduced_chapters = 1;
		base_chapters = ((mem_gb - UDS_MEMORY_CONFIG_REDUCED) *
				 DEFAULT_CHAPTERS_PER_VOLUME);
		*record_pages_per_chapter = DEFAULT_RECORD_PAGES_PER_CHAPTER;
	} else {
		uds_log_error("received invalid memory size");
		return -EINVAL;
	}

	if (sparse) {
		/* Make 95% of chapters sparse, allowing 10x more records. */
		*sparse_chapters_per_volume = (19 * base_chapters) / 2;
		base_chapters *= 10;
	} else {
		*sparse_chapters_per_volume = 0;
	}

	*chapters_per_volume = base_chapters - reduced_chapters;
	return UDS_SUCCESS;
}

static unsigned int __must_check normalize_zone_count(unsigned int requested)
{
	unsigned int zone_count = requested;

	if (zone_count == 0)
		zone_count = num_online_cpus() / 2;

	if (zone_count < 1)
		zone_count = 1;

	if (zone_count > MAX_ZONES)
		zone_count = MAX_ZONES;

	uds_log_info("Using %u indexing zone%s for concurrency.",
		     zone_count, zone_count == 1 ? "" : "s");
	return zone_count;
}

static unsigned int __must_check normalize_read_threads(unsigned int requested)
{
	unsigned int read_threads = requested;

	if (read_threads < 1)
		read_threads = DEFAULT_VOLUME_READ_THREADS;

	if (read_threads > MAX_VOLUME_READ_THREADS)
		read_threads = MAX_VOLUME_READ_THREADS;

	return read_threads;
}

int uds_make_configuration(const struct uds_parameters *params,
			   struct uds_configuration **config_ptr)
{
	struct uds_configuration *config;
	u32 chapters_per_volume = 0;
	u32 record_pages_per_chapter = 0;
	u32 sparse_chapters_per_volume = 0;
	int result;

	result = compute_memory_sizes(params->memory_size, params->sparse,
				      &chapters_per_volume, &record_pages_per_chapter,
				      &sparse_chapters_per_volume);
	if (result != UDS_SUCCESS)
		return result;

	result = vdo_allocate(1, struct uds_configuration, __func__, &config);
	if (result != UDS_SUCCESS)
		return result;

	result = uds_make_index_geometry(DEFAULT_BYTES_PER_PAGE, record_pages_per_chapter,
					 chapters_per_volume, sparse_chapters_per_volume,
					 0, 0, &config->geometry);
	if (result != UDS_SUCCESS) {
		uds_free_configuration(config);
		return result;
	}

	config->zone_count = normalize_zone_count(params->zone_count);
	config->read_threads = normalize_read_threads(params->read_threads);

	config->cache_chapters = DEFAULT_CACHE_CHAPTERS;
	config->volume_index_mean_delta = DEFAULT_VOLUME_INDEX_MEAN_DELTA;
	config->sparse_sample_rate = (params->sparse ? DEFAULT_SPARSE_SAMPLE_RATE : 0);
	config->nonce = params->nonce;
	config->bdev = params->bdev;
	config->offset = params->offset;
	config->size = params->size;

	*config_ptr = config;
	return UDS_SUCCESS;
}

void uds_free_configuration(struct uds_configuration *config)
{
	if (config != NULL) {
		uds_free_index_geometry(config->geometry);
		vdo_free(config);
	}
}

void uds_log_configuration(struct uds_configuration *config)
{
	struct index_geometry *geometry = config->geometry;

	uds_log_debug("Configuration:");
	uds_log_debug("  Record pages per chapter:   %10u", geometry->record_pages_per_chapter);
	uds_log_debug("  Chapters per volume:        %10u", geometry->chapters_per_volume);
	uds_log_debug("  Sparse chapters per volume: %10u", geometry->sparse_chapters_per_volume);
	uds_log_debug("  Cache size (chapters):      %10u", config->cache_chapters);
	uds_log_debug("  Volume index mean delta:    %10u", config->volume_index_mean_delta);
	uds_log_debug("  Bytes per page:             %10zu", geometry->bytes_per_page);
	uds_log_debug("  Sparse sample rate:         %10u", config->sparse_sample_rate);
	uds_log_debug("  Nonce:                      %llu", (unsigned long long) config->nonce);
}