summaryrefslogtreecommitdiff
path: root/lib/vdo/vdo.c
blob: c34eb93075a03c9d07158eeb35c28429c86513c5 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * Copyright (C) 2018 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "lib/misc/lib.h"
#include "lib/activate/activate.h"
#include "lib/activate/targets.h"
#include "lib/commands/toolcontext.h"
#include "lib/datastruct/str_list.h"
#include "lib/display/display.h"
#include "lib/format_text/text_export.h"
#include "lib/log/lvm-logging.h"
#include "lib/metadata/metadata.h"
#include "lib/metadata/lv_alloc.h"
#include "lib/metadata/segtype.h"
#include "base/memory/zalloc.h"

static unsigned _feature_mask;

static int _bad_field(const char *field)
{
	log_error("Couldn't read '%s' for VDO segment.", field);
	return 0;
}

static int _import_bool(const struct dm_config_node *n,
			const char *name, bool *b)
{
	uint32_t t;

	if (dm_config_has_node(n, name)) {
		if (!dm_config_get_uint32(n, name, &t))
			return _bad_field(name);

		if (t) {
			*b = true;
			return 1;
		}
	}

	*b = false;

	return 1;
}

static void _print_yes_no(const char *name, bool value)
{
	log_print("  %s\t%s", name, value ? "yes" : "no");
}

/*
 * VDO linear mapping
 */
static const char *_vdo_name(const struct lv_segment *seg)
{
	return SEG_TYPE_NAME_VDO;
}

static void _vdo_display(const struct lv_segment *seg)
{
	display_stripe(seg, 0, "    ");
}

static int _vdo_text_import(struct lv_segment *seg,
			    const struct dm_config_node *n,
			    struct dm_hash_table *pv_hash __attribute__((unused)))
{
	struct logical_volume *vdo_pool_lv;
	const char *str;
	uint32_t vdo_offset;

	if (!dm_config_has_node(n, "vdo_pool") ||
	    !(str = dm_config_find_str(n, "vdo_pool", NULL)))
		return _bad_field("vdo_pool");
	if (!(vdo_pool_lv = find_lv(seg->lv->vg, str))) {
		log_error("Unknown VDO pool logical volume %s.", str);
		return 0;
	}

	if (!dm_config_get_uint32(n, "vdo_offset", &vdo_offset))
		return _bad_field("vdo_offset");

	if (!set_lv_segment_area_lv(seg, 0, vdo_pool_lv, vdo_offset, LV_VDO_POOL))
		return_0;

	seg->lv->status |= LV_VDO;

	return 1;
}

static int _vdo_text_export(const struct lv_segment *seg, struct formatter *f)
{

	if (!seg_is_vdo(seg)) {
		log_error(INTERNAL_ERROR "Passed segment is not VDO type.");
		return 0;
	}

	outf(f, "vdo_pool = \"%s\"", seg_lv(seg, 0)->name);
	outf(f, "vdo_offset = %u", seg_le(seg, 0));

	return 1;
}

#ifdef DEVMAPPER_SUPPORT
static int _vdo_target_status_compatible(const char *type)
{
	return (strcmp(type, TARGET_NAME_LINEAR) == 0);
}

static int _vdo_add_target_line(struct dev_manager *dm,
				struct dm_pool *mem __attribute__((unused)),
				struct cmd_context *cmd,
				void **target_state __attribute__((unused)),
				struct lv_segment *seg,
				const struct lv_activate_opts *laopts __attribute__((unused)),
				struct dm_tree_node *node, uint64_t len,
				uint32_t *pvmove_mirror_count __attribute__((unused)))
{
	char *vdo_pool_uuid;

	if (!(vdo_pool_uuid = build_dm_uuid(mem, seg_lv(seg, 0), lv_layer(seg_lv(seg, 0)))))
		return_0;

	if (!add_linear_area_to_dtree(node, len, seg->lv->vg->extent_size,
				      cmd->use_linear_target,
				      seg->lv->vg->name, seg->lv->name))
		return_0;

	if (!dm_tree_node_add_target_area(node, NULL, vdo_pool_uuid,
					  first_seg(seg_lv(seg, 0))->vdo_pool_header_size +
					  seg->lv->vg->extent_size * (uint64_t)seg_le(seg, 0)))
		return_0;

	return 1;
}

#endif

/*
 *  VDO pool
 */
static const char *_vdo_pool_name(const struct lv_segment *seg)
{
	return SEG_TYPE_NAME_VDO_POOL;
}

static void _vdo_pool_display(const struct lv_segment *seg)
{
	struct cmd_context *cmd = seg->lv->vg->cmd;
	const struct dm_vdo_target_params *vtp = &seg->vdo_params;

	log_print("  Virtual size\t%s", display_size(cmd, get_vdo_pool_virtual_size(seg)));
	log_print("  Header size\t\t%s", display_size(cmd, seg->vdo_pool_header_size));

	_print_yes_no("Compression\t", vtp->use_compression);
	_print_yes_no("Deduplication", vtp->use_deduplication);
	_print_yes_no("Metadata hints", vtp->use_metadata_hints);

	log_print("  Minimum IO size\t%s",
		  display_size(cmd, vtp->minimum_io_size));
	log_print("  Block map cache sz\t%s",
		  display_size(cmd, vtp->block_map_cache_size_mb * UINT64_C(2 * 1024)));
	log_print("  Block map era length\t%u", vtp->block_map_era_length);

	_print_yes_no("Sparse index", vtp->use_sparse_index);

	log_print("  Index memory size\t%s",
		  display_size(cmd, vtp->index_memory_size_mb * UINT64_C(2 * 1024)));

	log_print("  Slab size\t\t%s",
		  display_size(cmd, vtp->slab_size_mb * UINT64_C(2 * 1024)));

	log_print("  # Ack threads\t%u", (unsigned) vtp->ack_threads);
	log_print("  # Bio threads\t%u", (unsigned) vtp->bio_threads);
	log_print("  Bio rotation\t%u", (unsigned) vtp->bio_rotation);
	log_print("  # CPU threads\t%u", (unsigned) vtp->cpu_threads);
	log_print("  # Hash zone threads\t%u", (unsigned) vtp->hash_zone_threads);
	log_print("  # Logical threads\t%u", (unsigned) vtp->logical_threads);
	log_print("  # Physical threads\t%u", (unsigned) vtp->physical_threads);
	log_print("  Max discard\t%u", (unsigned) vtp->max_discard);
}

/* reused as _vdo_text_import_area_count */
static int _vdo_pool_text_import_area_count(const struct dm_config_node *sn __attribute__((unused)),
					    uint32_t *area_count)
{
	*area_count = 1;

	return 1;
}

static int _vdo_pool_text_import(struct lv_segment *seg,
				 const struct dm_config_node *n,
				 struct dm_hash_table *pv_hash __attribute__((unused)))
{
	struct dm_vdo_target_params *vtp = &seg->vdo_params;
	struct logical_volume *data_lv;
	const char *str;

	if (!dm_config_has_node(n, "data") ||
	    !(str = dm_config_find_str(n, "data", NULL)))
		return _bad_field("data");
	if (!(data_lv = find_lv(seg->lv->vg, str))) {
		log_error("Unknown logical volume %s.", str);
		return 0;
	}

	/*
	 * TODO: we may avoid printing settings with FIXED default values
	 *       so it would generate smaller metadata.
	 */
	if (!dm_config_get_uint32(n, "header_size", &seg->vdo_pool_header_size))
		return _bad_field("header_size");

	if (!dm_config_get_uint32(n, "virtual_extents", &seg->vdo_pool_virtual_extents))
		return _bad_field("virtual_extents");

	memset(vtp, 0, sizeof(*vtp));

	if (!_import_bool(n, "use_compression", &vtp->use_compression))
		return_0;

	if (!_import_bool(n, "use_deduplication", &vtp->use_deduplication))
		return_0;

	if (!_import_bool(n, "use_metadata_hints", &vtp->use_metadata_hints))
		return_0;

	if (!dm_config_get_uint32(n, "minimum_io_size", &vtp->minimum_io_size))
		return _bad_field("minimum_io_size");

	if (!dm_config_get_uint32(n, "block_map_cache_size_mb", &vtp->block_map_cache_size_mb))
		return _bad_field("block_map_cache_size_mb");

	if (!dm_config_get_uint32(n, "block_map_era_length", &vtp->block_map_era_length))
		return _bad_field("block_map_era_length");

	if (!_import_bool(n, "use_sparse_index", &vtp->use_sparse_index))
		return_0;

	if (!dm_config_get_uint32(n, "index_memory_size_mb", &vtp->index_memory_size_mb))
		return _bad_field("index_memory_size_mb");

	if (!dm_config_get_uint32(n, "max_discard", &vtp->max_discard))
		return _bad_field("max_discard");

	if (!dm_config_get_uint32(n, "slab_size_mb", &vtp->slab_size_mb))
		return _bad_field("slab_size_mb");

	if (!dm_config_get_uint32(n, "ack_threads", &vtp->ack_threads))
		return _bad_field("ack_threads");

	if (!dm_config_get_uint32(n, "bio_threads", &vtp->bio_threads))
		return _bad_field("bio_threads");

	if (!dm_config_get_uint32(n, "bio_rotation", &vtp->bio_rotation))
		return _bad_field("bio_rotation");

	if (!dm_config_get_uint32(n, "cpu_threads", &vtp->cpu_threads))
		return _bad_field("cpu_threads");

	if (!dm_config_get_uint32(n, "hash_zone_threads", &vtp->hash_zone_threads))
		return _bad_field("hash_zone_threads");

	if (!dm_config_get_uint32(n, "logical_threads", &vtp->logical_threads))
		return _bad_field("logical_threads");

	if (!dm_config_get_uint32(n, "physical_threads", &vtp->physical_threads))
		return _bad_field("physical_threads");


	if (!set_lv_segment_area_lv(seg, 0, data_lv, 0, LV_VDO_POOL_DATA))
		return_0;

	seg->lv->status |= LV_VDO_POOL;
	lv_set_hidden(data_lv);

	return 1;
}

static int _vdo_pool_text_export(const struct lv_segment *seg, struct formatter *f)
{
	const struct dm_vdo_target_params *vtp = &seg->vdo_params;

	outf(f, "data = \"%s\"", seg_lv(seg, 0)->name);
	outsize(f, seg->vdo_pool_header_size, "header_size = %u\t",
		seg->vdo_pool_header_size);
	outsize(f, seg->vdo_pool_virtual_extents * (uint64_t) seg->lv->vg->extent_size,
		"virtual_extents = %u\t", seg->vdo_pool_virtual_extents);

	outnl(f);

	if (vtp->use_compression)
		outf(f, "use_compression = 1");
	if (vtp->use_deduplication)
		outf(f, "use_deduplication = 1");
	if (vtp->use_metadata_hints)
		outf(f, "use_metadata_hints = 1");

	outsize(f, vtp->minimum_io_size, "minimum_io_size = %u", vtp->minimum_io_size);

	outsize(f, vtp->block_map_cache_size_mb * UINT64_C(2 * 1024),
		"block_map_cache_size_mb = %u", vtp->block_map_cache_size_mb);
	outf(f, "block_map_era_length = %u", vtp->block_map_era_length);

	if (vtp->use_sparse_index)
		outf(f, "use_sparse_index = 1");
	// TODO - conditionally
	outsize(f, vtp->index_memory_size_mb * UINT64_C(2 * 1024),
		"index_memory_size_mb = %u", vtp->index_memory_size_mb);

	outf(f, "max_discard = %u", vtp->max_discard);

	// TODO - conditionally
	outsize(f, vtp->slab_size_mb * UINT64_C(2 * 1024),
		"slab_size_mb = %u", vtp->slab_size_mb);
	outf(f, "ack_threads = %u", (unsigned) vtp->ack_threads);
	outf(f, "bio_threads = %u", (unsigned) vtp->bio_threads);
	outf(f, "bio_rotation = %u", (unsigned) vtp->bio_rotation);
	outf(f, "cpu_threads = %u", (unsigned) vtp->cpu_threads);
	outf(f, "hash_zone_threads = %u", (unsigned) vtp->hash_zone_threads);
	outf(f, "logical_threads = %u", (unsigned) vtp->logical_threads);
	outf(f, "physical_threads = %u", (unsigned) vtp->physical_threads);

	return 1;
}

#ifdef DEVMAPPER_SUPPORT
static int _vdo_pool_target_status_compatible(const char *type)
{
	return (strcmp(type, TARGET_NAME_VDO) == 0);
}

static int _vdo_pool_add_target_line(struct dev_manager *dm,
				     struct dm_pool *mem __attribute__((unused)),
				     struct cmd_context *cmd __attribute__((unused)),
				     void **target_state __attribute__((unused)),
				     struct lv_segment *seg,
				     const struct lv_activate_opts *laopts __attribute__((unused)),
				     struct dm_tree_node *node, uint64_t len,
				     uint32_t *pvmove_mirror_count __attribute__((unused)))
{
	char *data_uuid;

	if (!seg_is_vdo_pool(seg)) {
		log_error(INTERNAL_ERROR "Passed segment is not VDO pool.");
		return 0;
	}

	if (!(data_uuid = build_dm_uuid(mem, seg_lv(seg, 0), lv_layer(seg_lv(seg, 0)))))
		return_0;

	/* VDO uses virtual size instead of its physical size */
	if (!dm_tree_node_add_vdo_target(node, get_vdo_pool_virtual_size(seg),
					 data_uuid, seg_lv(seg, 0)->size,
					 &seg->vdo_params))
		return_0;

	return 1;
}

static int _vdo_target_present(struct cmd_context *cmd,
			       const struct lv_segment *seg __attribute__((unused)),
			       unsigned *attributes __attribute__((unused)))
{
	/* List of features with their kernel target version */
	static const struct feature {
		uint32_t maj;
		uint32_t min;
		unsigned vdo_feature;
		const char *feature;
	} _features[] = {
		{ 1, 1, 0, "" },
		//{ 9, 9, VDO_FEATURE_RESIZE, "resize" },
	};
	//static const char _lvmconf[] = "global/vdo_disabled_features";
	static int _vdo_checked = 0;
	static int _vdo_present = 0;
	static unsigned _vdo_attrs = 0;
	uint32_t i, maj, min, patchlevel;
	const struct segment_type *segtype;

	if (!activation())
		return 0;

	if (!_vdo_checked) {
		_vdo_checked = 1;

		if (!target_present_version(cmd, TARGET_NAME_VDO, 0,
					    &maj, &min, &patchlevel)) {
			/* Try to load kmod VDO module */
			if (!module_present(cmd, MODULE_NAME_VDO) ||
			    !target_version(TARGET_NAME_VDO, &maj, &min, &patchlevel))
				return_0;
		}

		if (maj < 6 || (maj == 6 && min < 2)) {
			log_warn("WARNING: VDO target version %u.%u.%u is too old.",
				 maj, min, patchlevel);
			return 0;
		}

		/* If stripe target was already detected, reuse its result */
		if (!(segtype = get_segtype_from_string(cmd, SEG_TYPE_NAME_STRIPED)) ||
		    !segtype->ops->target_present || !segtype->ops->target_present(cmd, NULL, NULL)) {
			/* Linear/Stripe targer is for mapping LVs on top of single VDO volume. */
			if (!target_present(cmd, TARGET_NAME_LINEAR, 0) ||
			    !target_present(cmd, TARGET_NAME_STRIPED, 0))
				return 0;
		}

		_vdo_present = 1;
		/* Prepare for adding supported features */
		for (i = 0; i < DM_ARRAY_SIZE(_features); ++i)
			if ((maj > _features[i].maj) ||
			    (maj == _features[i].maj && min >= _features[i].min))
				_vdo_attrs |= _features[i].vdo_feature;
			else
				log_very_verbose("Target %s does not support %s.",
						 TARGET_NAME_VDO,
						 _features[i].feature);
	}

	if (attributes) {
		*attributes = _vdo_attrs & _feature_mask;
	}

	return _vdo_present;
}

static int _vdo_modules_needed(struct dm_pool *mem,
			   const struct lv_segment *seg __attribute__((unused)),
			   struct dm_list *modules)
{
	if (!str_list_add(mem, modules, MODULE_NAME_VDO)) {
		log_error("String list allocation failed for VDO module.");
		return 0;
	}

	return 1;
}

#  ifdef DMEVENTD
/* FIXME Cache this */
static int _vdo_pool_target_registered(struct lv_segment *seg, int *pending, int *monitored)
{
	return target_registered_with_dmeventd(seg->lv->vg->cmd,
					       seg->segtype->dso,
					       seg->lv, pending, monitored);
}

/* FIXME This gets run while suspended and performs banned operations. */
static int _vdo_pool_target_set_events(struct lv_segment *seg, int evmask, int set)
{
	/* FIXME Make timeout (10) configurable */
	return target_register_events(seg->lv->vg->cmd,
				      seg->segtype->dso,
				      seg->lv, evmask, set, 10);
}

static int _vdo_pool_target_register_events(struct lv_segment *seg,
					    int events)
{
	return _vdo_pool_target_set_events(seg, events, 1);
}

static int _vdo_pool_target_unregister_events(struct lv_segment *seg,
					      int events)
{
	return _vdo_pool_target_set_events(seg, events, 0);
}

#  endif /* DMEVENTD */
#endif

/* reused as _vdo_destroy */
static void _vdo_pool_destroy(struct segment_type *segtype)
{
	free((void *)segtype->dso);
	free((void *)segtype);
}

static struct segtype_handler _vdo_ops = {
	.name = _vdo_name,
	.display = _vdo_display,
	.text_import = _vdo_text_import,
	.text_import_area_count = _vdo_pool_text_import_area_count,
	.text_export = _vdo_text_export,

#ifdef DEVMAPPER_SUPPORT
	.target_status_compatible = _vdo_target_status_compatible,
	.add_target_line = _vdo_add_target_line,
	.target_present = _vdo_target_present,
	.modules_needed = _vdo_modules_needed,
#endif
	.destroy = _vdo_pool_destroy,
};

static struct segtype_handler _vdo_pool_ops = {
	.name = _vdo_pool_name,
	.display = _vdo_pool_display,
	.text_import = _vdo_pool_text_import,
	.text_import_area_count = _vdo_pool_text_import_area_count,
	.text_export = _vdo_pool_text_export,

#ifdef DEVMAPPER_SUPPORT
	.target_status_compatible = _vdo_pool_target_status_compatible,
	.add_target_line = _vdo_pool_add_target_line,
	.target_present = _vdo_target_present,
	.modules_needed = _vdo_modules_needed,

#  ifdef DMEVENTD
	.target_monitored = _vdo_pool_target_registered,
	.target_monitor_events = _vdo_pool_target_register_events,
	.target_unmonitor_events = _vdo_pool_target_unregister_events,
#  endif /* DMEVENTD */
#endif
	.destroy = _vdo_pool_destroy,
};

int init_vdo_segtypes(struct cmd_context *cmd,
		      struct segtype_library *seglib)
{
	struct segment_type *segtype, *pool_segtype;

	if (!(segtype = zalloc(sizeof(*segtype))) ||
	    !(pool_segtype = zalloc(sizeof(*segtype)))) {
		log_error("Failed to allocate memory for VDO segtypes.");
		free(segtype);
		return 0;
	}

	segtype->name = SEG_TYPE_NAME_VDO;
	segtype->flags = SEG_VDO | SEG_VIRTUAL | SEG_ONLY_EXCLUSIVE;
	segtype->ops = &_vdo_ops;

	if (!lvm_register_segtype(seglib, segtype)) {
		free(pool_segtype);
		return_0;
	}

	pool_segtype->name = SEG_TYPE_NAME_VDO_POOL;
	pool_segtype->flags = SEG_VDO_POOL | SEG_ONLY_EXCLUSIVE;
	pool_segtype->ops = &_vdo_pool_ops;
#ifdef DEVMAPPER_SUPPORT
#  ifdef DMEVENTD
	pool_segtype->dso = get_monitor_dso_path(cmd, dmeventd_vdo_library_CFG);
	if (pool_segtype->dso)
		pool_segtype->flags |= SEG_MONITORED;
#  endif /* DMEVENTD */
#endif

	if (!lvm_register_segtype(seglib, pool_segtype))
		return_0;

	log_very_verbose("Initialised segtypes: %s, %s.", segtype->name, pool_segtype->name);

	/* Reset mask for recalc */
	_feature_mask = 0;

	return 1;
}