summaryrefslogtreecommitdiff
path: root/lib/writecache/writecache.c
blob: aa2113271421bce53be560e3a2cde5e906f47ecc (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
/*
 * 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"

static int _writecache_cleaner_supported;
static int _writecache_max_age_supported;

#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 _writecache_display(const struct lv_segment *seg)
{
	/* TODO: lvdisplay segments */
}

static int _writecache_text_import(struct lv_segment *seg,
				   const struct dm_config_node *sn,
				   struct dm_hash_table *pv_hash __attribute__((unused)))
{
	struct logical_volume *origin_lv = NULL;
	struct logical_volume *fast_lv;
	const char *origin_name = NULL;
	const char *fast_name = NULL;

	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 writecache origin %s in", origin_name);

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

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

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

	if (!(fast_lv = find_lv(seg->lv->vg, fast_name)))
		return SEG_LOG_ERROR("Unknown logical volume %s specified for writecache in",
				     fast_name);

	if (!dm_config_get_uint32(sn, "writecache_block_size", &seg->writecache_block_size))
		return SEG_LOG_ERROR("writecache block_size must be set in");

	seg->origin = origin_lv;
	seg->writecache = fast_lv;
	seg->lv->status |= WRITECACHE;

	if (!add_seg_to_segs_using_this_lv(fast_lv, seg))
		return_0;

	memset(&seg->writecache_settings, 0, sizeof(struct writecache_settings));

	if (dm_config_has_node(sn, "high_watermark")) {
		if (!dm_config_get_uint64(sn, "high_watermark", &seg->writecache_settings.high_watermark))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.high_watermark_set = 1;
	}

	if (dm_config_has_node(sn, "low_watermark")) {
		if (!dm_config_get_uint64(sn, "low_watermark", &seg->writecache_settings.low_watermark))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.low_watermark_set = 1;
	}

	if (dm_config_has_node(sn, "writeback_jobs")) {
		if (!dm_config_get_uint64(sn, "writeback_jobs", &seg->writecache_settings.writeback_jobs))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.writeback_jobs_set = 1;
	}

	if (dm_config_has_node(sn, "autocommit_blocks")) {
		if (!dm_config_get_uint64(sn, "autocommit_blocks", &seg->writecache_settings.autocommit_blocks))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.autocommit_blocks_set = 1;
	}

	if (dm_config_has_node(sn, "autocommit_time")) {
		if (!dm_config_get_uint64(sn, "autocommit_time", &seg->writecache_settings.autocommit_time))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.autocommit_time_set = 1;
	}

	if (dm_config_has_node(sn, "fua")) {
		if (!dm_config_get_uint32(sn, "fua", &seg->writecache_settings.fua))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.fua_set = 1;
	}

	if (dm_config_has_node(sn, "nofua")) {
		if (!dm_config_get_uint32(sn, "nofua", &seg->writecache_settings.nofua))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.nofua_set = 1;
	}

	if (dm_config_has_node(sn, "cleaner")) {
		if (!dm_config_get_uint32(sn, "cleaner", &seg->writecache_settings.cleaner))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.cleaner_set = 1;
	}

	if (dm_config_has_node(sn, "max_age")) {
		if (!dm_config_get_uint32(sn, "max_age", &seg->writecache_settings.max_age))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		seg->writecache_settings.max_age_set = 1;
	}

	if (dm_config_has_node(sn, "writecache_setting_key")) {
		const char *key;
		const char *val;

		if (!dm_config_get_str(sn, "writecache_setting_key", &key))
			return SEG_LOG_ERROR("Unknown writecache_setting in");
		if (!dm_config_get_str(sn, "writecache_setting_val", &val))
			return SEG_LOG_ERROR("Unknown writecache_setting in");

		seg->writecache_settings.new_key = dm_pool_strdup(seg->lv->vg->vgmem, key);
		seg->writecache_settings.new_val = dm_pool_strdup(seg->lv->vg->vgmem, val);
	}

	return 1;
}

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

	return 1;
}

static int _writecache_text_export(const struct lv_segment *seg,
				   struct formatter *f)
{
	outf(f, "writecache = \"%s\"", seg->writecache->name);
	outf(f, "origin = \"%s\"", seg_lv(seg, 0)->name);
	outf(f, "writecache_block_size = %u", seg->writecache_block_size);

	if (seg->writecache_settings.high_watermark_set) {
	        outf(f, "high_watermark = %llu",
	                (unsigned long long)seg->writecache_settings.high_watermark);
	}

	if (seg->writecache_settings.low_watermark_set) {
	        outf(f, "low_watermark = %llu",
	                (unsigned long long)seg->writecache_settings.low_watermark);
	}

	if (seg->writecache_settings.writeback_jobs_set) {
	        outf(f, "writeback_jobs = %llu",
	                (unsigned long long)seg->writecache_settings.writeback_jobs);
	}

	if (seg->writecache_settings.autocommit_blocks_set) {
	        outf(f, "autocommit_blocks = %llu",
	                (unsigned long long)seg->writecache_settings.autocommit_blocks);
	}

	if (seg->writecache_settings.autocommit_time_set) {
	        outf(f, "autocommit_time = %llu",
	                (unsigned long long)seg->writecache_settings.autocommit_time);
	}

	if (seg->writecache_settings.fua_set) {
	        outf(f, "fua = %u", seg->writecache_settings.fua);
	}

	if (seg->writecache_settings.nofua_set) {
	        outf(f, "nofua = %u", seg->writecache_settings.nofua);
	}

	if (seg->writecache_settings.cleaner_set && seg->writecache_settings.cleaner) {
	        outf(f, "cleaner = %u", seg->writecache_settings.cleaner);
	}

	if (seg->writecache_settings.max_age_set) {
	        outf(f, "max_age = %u", seg->writecache_settings.max_age);
	}

	if (seg->writecache_settings.new_key && seg->writecache_settings.new_val) {
	        outf(f, "writecache_setting_key = \"%s\"",
	                seg->writecache_settings.new_key);

	        outf(f, "writecache_setting_val = \"%s\"",
	                seg->writecache_settings.new_val);
	}

	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 _writecache_checked = 0;
	static int _writecache_present = 0;
	uint32_t maj, min, patchlevel;

	if (!activation())
		return 0;

	if (!_writecache_checked) {
		_writecache_checked = 1;
		if (!(_writecache_present = target_present_version(cmd, TARGET_NAME_WRITECACHE, 1,
								   &maj, &min, &patchlevel)))
			return 0;

		if (maj < 1) {
			log_error("dm-writecache module version older than minimum 1.0.0");
			return 0;
		}

		if (min >= 3) {
			_writecache_cleaner_supported = 1;
			_writecache_max_age_supported = 1;
		}
	}

	return _writecache_present;
}

bool writecache_cleaner_supported(struct cmd_context *cmd)
{
	_target_present(cmd, NULL, NULL);
	return _writecache_cleaner_supported ? true : false;
}

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_WRITECACHE)) {
		log_error("String list allocation failed for writecache module.");
		return 0;
	}

	return 1;
}

#else
bool writecache_cleaner_supported(struct cmd_context *cmd)
{
	return 0;
}
#endif /* DEVMAPPER_SUPPORT */

#ifdef DEVMAPPER_SUPPORT
static int _writecache_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 __attribute__((unused)),
				 struct dm_tree_node *node, uint64_t len,
				 uint32_t *pvmove_mirror_count __attribute__((unused)))
{
	char *origin_uuid;
	char *fast_uuid;
	int pmem;

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

	if (!seg->writecache) {
		log_error(INTERNAL_ERROR "Passed segment has no writecache.");
		return 0;
	}

	if (!_writecache_cleaner_supported && seg->writecache_settings.cleaner_set && seg->writecache_settings.cleaner) {
		log_warn("WARNING: ignoring writecache setting \"cleaner\" which is not supported by kernel for LV %s.", seg->lv->name);
		seg->writecache_settings.cleaner = 0;
		seg->writecache_settings.cleaner_set = 0;
	}

	if (!_writecache_max_age_supported && seg->writecache_settings.max_age_set) {
		log_warn("WARNING: ignoring writecache setting \"max_age\" which is not supported by kernel for LV %s.", seg->lv->name);
		seg->writecache_settings.max_age = 0;
		seg->writecache_settings.max_age_set = 0;
	}

	if ((pmem = lv_on_pmem(seg->writecache)) < 0)
		return_0;

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

	if (!(fast_uuid = build_dm_uuid(mem, seg->writecache, "cvol")))
		return_0;

	if (!dm_tree_node_add_writecache_target(node, len,
						origin_uuid, fast_uuid,
						pmem,
						seg->writecache_block_size,
						&seg->writecache_settings))
		return_0;

	return 1;
}
#endif /* DEVMAPPER_SUPPORT */

static struct segtype_handler _writecache_ops = {
	.display = _writecache_display,
	.text_import = _writecache_text_import,
	.text_import_area_count = _writecache_text_import_area_count,
	.text_export = _writecache_text_export,
#ifdef DEVMAPPER_SUPPORT
	.add_target_line = _writecache_add_target_line,
	.target_present = _target_present,
	.modules_needed = _modules_needed,
#endif
	.destroy = _destroy,
};

int init_writecache_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 writecache segtype");
		return 0;
	}

	segtype->name = SEG_TYPE_NAME_WRITECACHE;
	segtype->flags = SEG_WRITECACHE;
	segtype->ops = &_writecache_ops;

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

	return 1;
}