summaryrefslogtreecommitdiff
path: root/lib/integrity/integrity.c
blob: 788d4ebe6950398c77318f889727e1b4d4f4f1b6 (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
/*
 * Copyright (C) 2013-2016 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 "base/memory/zalloc.h"
#include "lib/misc/lib.h"
#include "lib/commands/toolcontext.h"
#include "lib/metadata/segtype.h"
#include "lib/display/display.h"
#include "lib/format_text/text_export.h"
#include "lib/config/config.h"
#include "lib/datastruct/str_list.h"
#include "lib/misc/lvm-string.h"
#include "lib/activate/activate.h"
#include "lib/metadata/metadata.h"
#include "lib/metadata/lv_alloc.h"
#include "lib/config/defaults.h"

#define SEG_LOG_ERROR(t, p...) \
        log_error(t " segment %s of logical volume %s.", ## p,	\
                  dm_config_parent_name(sn), seg->lv->name), 0;

static void _integrity_display(const struct lv_segment *seg)
{
	/* TODO: lvdisplay segments */
}

static int _integrity_text_import(struct lv_segment *seg,
				   const struct dm_config_node *sn,
				   struct dm_hash_table *pv_hash __attribute__((unused)))
{
	struct integrity_settings *set;
	struct logical_volume *origin_lv = NULL;
	struct logical_volume *meta_lv = NULL;
	const char *origin_name = NULL;
	const char *meta_dev = NULL;
	const char *mode = NULL;
	const char *hash = NULL;

	memset(&seg->integrity_settings, 0, sizeof(struct integrity_settings));
	set = &seg->integrity_settings;

	/* origin always set */

	if (!dm_config_has_node(sn, "origin"))
		return SEG_LOG_ERROR("origin not specified in");

	if (!dm_config_get_str(sn, "origin", &origin_name))
		return SEG_LOG_ERROR("origin must be a string in");

	if (!(origin_lv = find_lv(seg->lv->vg, origin_name)))
		return SEG_LOG_ERROR("Unknown LV specified for integrity origin %s in", origin_name);

	if (!set_lv_segment_area_lv(seg, 0, origin_lv, 0, 0))
		return_0;

	/* data_sectors always set */

	if (!dm_config_get_uint64(sn, "data_sectors", &seg->integrity_data_sectors))
		return SEG_LOG_ERROR("integrity data_sectors must be set in");

	/* mode always set */

	if (!dm_config_get_str(sn, "mode", &mode))
		return SEG_LOG_ERROR("integrity mode must be set in");

	if (strlen(mode) > 7)
		return SEG_LOG_ERROR("integrity mode invalid in");

	strncpy(set->mode, mode, 7);

	/* tag_size always set */

	if (!dm_config_get_uint32(sn, "tag_size", &set->tag_size))
		return SEG_LOG_ERROR("integrity tag_size must be set in");

	/* block_size always set */

	if (!dm_config_get_uint32(sn, "block_size", &set->block_size))
		return SEG_LOG_ERROR("integrity block_size invalid in");

	/* internal_hash always set */

	if (!dm_config_get_str(sn, "internal_hash", &hash))
		return SEG_LOG_ERROR("integrity internal_hash must be set in");

	if (!(set->internal_hash = dm_pool_strdup(seg->lv->vg->vgmem, hash)))
		return SEG_LOG_ERROR("integrity internal_hash failed to be set in");

	/* meta_dev optional */

	if (dm_config_has_node(sn, "meta_dev")) {
		if (!dm_config_get_str(sn, "meta_dev", &meta_dev))
			return SEG_LOG_ERROR("meta_dev must be a string in");

		if (!(meta_lv = find_lv(seg->lv->vg, meta_dev)))
			return SEG_LOG_ERROR("Unknown logical volume %s specified for integrity in", meta_dev);
	}

	if (dm_config_has_node(sn, "recalculate")) {
		if (!dm_config_get_uint32(sn, "recalculate", &seg->integrity_recalculate))
			return SEG_LOG_ERROR("integrity recalculate error in");
	}

	/* the rest are optional */

	if (dm_config_has_node(sn, "journal_sectors")) {
		if (!dm_config_get_uint32(sn, "journal_sectors", &set->journal_sectors))
			return SEG_LOG_ERROR("Unknown integrity_setting in");
		set->journal_sectors_set = 1;
	}

	if (dm_config_has_node(sn, "interleave_sectors")) {
		if (!dm_config_get_uint32(sn, "interleave_sectors", &set->interleave_sectors))
			return SEG_LOG_ERROR("Unknown integrity_setting in");
		set->interleave_sectors_set = 1;
	}

	if (dm_config_has_node(sn, "buffer_sectors")) {
		if (!dm_config_get_uint32(sn, "buffer_sectors", &set->buffer_sectors))
			return SEG_LOG_ERROR("Unknown integrity_setting in");
		set->buffer_sectors_set = 1;
	}

	if (dm_config_has_node(sn, "journal_watermark")) {
		if (!dm_config_get_uint32(sn, "journal_watermark", &set->journal_watermark))
			return SEG_LOG_ERROR("Unknown integrity_setting in");
		set->journal_watermark_set = 1;
	}

	if (dm_config_has_node(sn, "commit_time")) {
		if (!dm_config_get_uint32(sn, "commit_time", &set->commit_time))
			return SEG_LOG_ERROR("Unknown integrity_setting in");
		set->commit_time_set = 1;
	}

	if (dm_config_has_node(sn, "bitmap_flush_interval")) {
		if (!dm_config_get_uint32(sn, "bitmap_flush_interval", &set->bitmap_flush_interval))
			return SEG_LOG_ERROR("Unknown integrity_setting in");
		set->bitmap_flush_interval_set = 1;
	}

	if (dm_config_has_node(sn, "sectors_per_bit")) {
		if (!dm_config_get_uint64(sn, "sectors_per_bit", &set->sectors_per_bit))
			return SEG_LOG_ERROR("Unknown integrity_setting in");
		set->sectors_per_bit_set = 1;
	}

	seg->origin = origin_lv;
	seg->integrity_meta_dev = meta_lv;
	seg->lv->status |= INTEGRITY;

	if (meta_lv)
		meta_lv->status |= INTEGRITY_METADATA;

	if (meta_lv && !add_seg_to_segs_using_this_lv(meta_lv, seg))
		return_0;

	return 1;
}

static int _integrity_text_import_area_count(const struct dm_config_node *sn,
					      uint32_t *area_count)
{
	*area_count = 1;

	return 1;
}

static int _integrity_text_export(const struct lv_segment *seg,
				   struct formatter *f)
{
	const struct integrity_settings *set = &seg->integrity_settings;

	outf(f, "origin = \"%s\"", seg_lv(seg, 0)->name);
	outf(f, "data_sectors = %llu", (unsigned long long)seg->integrity_data_sectors);

	outf(f, "mode = \"%s\"", set->mode);
	outf(f, "tag_size = %u", set->tag_size);
	outf(f, "block_size = %u", set->block_size);
	outf(f, "internal_hash = \"%s\"", set->internal_hash);

	if (seg->integrity_meta_dev)
		outf(f, "meta_dev = \"%s\"", seg->integrity_meta_dev->name);

	if (seg->integrity_recalculate)
		outf(f, "recalculate = %u", seg->integrity_recalculate);

	if (set->journal_sectors_set)
		outf(f, "journal_sectors = %u", set->journal_sectors);

	if (set->interleave_sectors_set)
		outf(f, "interleave_sectors = %u", set->interleave_sectors);

	if (set->buffer_sectors_set)
		outf(f, "buffer_sectors = %u", set->buffer_sectors);

	if (set->journal_watermark_set)
		outf(f, "journal_watermark = %u", set->journal_watermark);

	if (set->commit_time_set)
		outf(f, "commit_time = %u", set->commit_time);

	if (set->bitmap_flush_interval)
		outf(f, "bitmap_flush_interval = %u", set->bitmap_flush_interval);

	if (set->sectors_per_bit)
		outf(f, "sectors_per_bit = %llu", (unsigned long long)set->sectors_per_bit);

	return 1;
}

static void _destroy(struct segment_type *segtype)
{
	free((void *) segtype);
}

#ifdef DEVMAPPER_SUPPORT

static int _target_present(struct cmd_context *cmd,
			   const struct lv_segment *seg __attribute__((unused)),
			   unsigned *attributes __attribute__((unused)))
{
	static int _integrity_checked = 0;
	static int _integrity_present = 0;
	uint32_t maj, min, patchlevel;

	if (!activation())
		return 0;

	if (!_integrity_checked) {
		_integrity_checked = 1;
		_integrity_present =  target_present(cmd, TARGET_NAME_INTEGRITY, 1);

		if (!target_version(TARGET_NAME_INTEGRITY, &maj, &min, &patchlevel))
			return 0;

		if (maj < 1 || min < 3) {
			log_error("Integrity target version older than minimum 1.3.0");
			return 0;
		}
	}

	return _integrity_present;
}

static int _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_INTEGRITY)) {
		log_error("String list allocation failed for integrity module.");
		return 0;
	}

	return 1;
}
#endif /* DEVMAPPER_SUPPORT */

#ifdef DEVMAPPER_SUPPORT
static int _integrity_add_target_line(struct dev_manager *dm,
				 struct dm_pool *mem,
				 struct cmd_context *cmd __attribute__((unused)),
				 void **target_state __attribute__((unused)),
				 struct lv_segment *seg,
				 const struct lv_activate_opts *laopts,
				 struct dm_tree_node *node, uint64_t len,
				 uint32_t *pvmove_mirror_count __attribute__((unused)))
{
	char *origin_uuid;
	char *meta_uuid = NULL;

	if (!seg_is_integrity(seg)) {
		log_error(INTERNAL_ERROR "Passed segment is not integrity.");
		return 0;
	}

	if (!(origin_uuid = build_dm_uuid(mem, seg_lv(seg, 0), NULL)))
		return_0;

	if (seg->integrity_meta_dev) {
		if (!(meta_uuid = build_dm_uuid(mem, seg->integrity_meta_dev, NULL)))
			return_0;
	}

	if (!seg->integrity_data_sectors) {
		log_error("_integrity_add_target_line zero size");
		return_0;
	}

	if (!dm_tree_node_add_integrity_target(node, seg->integrity_data_sectors,
					       origin_uuid, meta_uuid,
					       &seg->integrity_settings,
					       seg->integrity_recalculate))
		return_0;

	return 1;
}
#endif /* DEVMAPPER_SUPPORT */

static struct segtype_handler _integrity_ops = {
	.display = _integrity_display,
	.text_import = _integrity_text_import,
	.text_import_area_count = _integrity_text_import_area_count,
	.text_export = _integrity_text_export,
#ifdef DEVMAPPER_SUPPORT
	.add_target_line = _integrity_add_target_line,
	.target_present = _target_present,
	.modules_needed = _modules_needed,
#endif
	.destroy = _destroy,
};

int init_integrity_segtypes(struct cmd_context *cmd,
			struct segtype_library *seglib)
{
	struct segment_type *segtype = zalloc(sizeof(*segtype));

	if (!segtype) {
		log_error("Failed to allocate memory for integrity segtype");
		return 0;
	}

	segtype->name = SEG_TYPE_NAME_INTEGRITY;
	segtype->flags = SEG_INTEGRITY;
	segtype->ops = &_integrity_ops;

	if (!lvm_register_segtype(seglib, segtype))
		return_0;
	log_very_verbose("Initialised segtype: %s", segtype->name);

	return 1;
}