summaryrefslogtreecommitdiffstats
path: root/lib/raid/raid.c
blob: 0f9d64e0f8016aea1212f0da47c3918beab837c4 (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
/*
 * Copyright (C) 2011 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lib.h"
#include "toolcontext.h"
#include "segtype.h"
#include "display.h"
#include "text_export.h"
#include "text_import.h"
#include "config.h"
#include "str_list.h"
#include "targets.h"
#include "lvm-string.h"
#include "activate.h"
#include "metadata.h"
#include "lv_alloc.h"

static const char *_raid_name(const struct lv_segment *seg)
{
	return seg->segtype->name;
}

static int _raid_text_import_area_count(const struct config_node *sn,
					uint32_t *area_count)
{
	if (!get_config_uint32(sn, "device_count", area_count)) {
		log_error("Couldn't read 'device_count' for "
			  "segment '%s'.", config_parent_name(sn));
		return 0;
	}
	return 1;
}

static int
_raid_text_import_areas(struct lv_segment *seg, const struct config_node *sn,
			const struct config_node *cn)
{
	unsigned int s;
	const struct config_value *cv;
	struct logical_volume *lv1;
	const char *seg_name = config_parent_name(sn);

	if (!seg->area_count) {
		log_error("No areas found for segment %s", seg_name);
		return 0;
	}

	for (cv = cn->v, s = 0; cv && s < seg->area_count; s++, cv = cv->next) {
		if (cv->type != CFG_STRING) {
			log_error("Bad volume name in areas array for segment %s.", seg_name);
			return 0;
		}

		if (!cv->next) {
			log_error("Missing data device in areas array for segment %s.", seg_name);
			return 0;
		}

		/* Metadata device comes first */
		if (!(lv1 = find_lv(seg->lv->vg, cv->v.str))) {
			log_error("Couldn't find volume '%s' for segment '%s'.",
				  cv->v.str ? : "NULL", seg_name);
			return 0;
		}
		if (!set_lv_segment_area_lv(seg, s, lv1, 0, RAID_META))
				return_0;

		/* Data device comes second */
		cv = cv->next;
		if (!(lv1 = find_lv(seg->lv->vg, cv->v.str))) {
			log_error("Couldn't find volume '%s' for segment '%s'.",
				  cv->v.str ? : "NULL", seg_name);
			return 0;
		}
		if (!set_lv_segment_area_lv(seg, s, lv1, 0, RAID_IMAGE))
				return_0;
	}

	/*
	 * Check we read the correct number of RAID data/meta pairs.
	 */
	if (cv || (s < seg->area_count)) {
		log_error("Incorrect number of areas in area array "
			  "for segment '%s'.", seg_name);
		return 0;
	}

	return 1;
}

static int
_raid_text_import(struct lv_segment *seg, const struct config_node *sn,
		  struct dm_hash_table *pv_hash)
{
	const struct config_node *cn;

	if (find_config_node(sn, "region_size")) {
		if (!get_config_uint32(sn, "region_size", &seg->region_size)) {
			log_error("Couldn't read 'region_size' for "
				  "segment %s of logical volume %s.",
				  config_parent_name(sn), seg->lv->name);
			return 0;
		}
	}
	if (find_config_node(sn, "stripe_size")) {
		if (!get_config_uint32(sn, "stripe_size", &seg->stripe_size)) {
			log_error("Couldn't read 'stripe_size' for "
				  "segment %s of logical volume %s.",
				  config_parent_name(sn), seg->lv->name);
			return 0;
		}
	}
	if (!(cn = find_config_node(sn, "raids"))) {
		log_error("Couldn't find RAID array for "
			  "segment %s of logical volume %s.",
			  config_parent_name(sn), seg->lv->name);
		return 0;
	}

	if (!_raid_text_import_areas(seg, sn, cn)) {
		log_error("Failed to import RAID images");
		return 0;
	}

	seg->status |= RAID;

	return 1;
}

static int
_raid_text_export(const struct lv_segment *seg, struct formatter *f)
{
	outf(f, "device_count = %u", seg->area_count);
	if (seg->region_size)
		outf(f, "region_size = %" PRIu32, seg->region_size);
	if (seg->stripe_size)
		outf(f, "stripe_size = %" PRIu32, seg->stripe_size);

	return out_areas(f, seg, "raid");
}

static int
_raid_add_target_line(struct dev_manager *dm __attribute__((unused)),
		      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)))
{
	if (!seg->area_count) {
		log_error(INTERNAL_ERROR "_raid_add_target_line called "
			  "with no areas for %s.", seg->lv->name);
		return 0;
	}

	if (!seg->region_size) {
		log_error("Missing region size for mirror segment.");
		return 0;
	}

	if (!dm_tree_node_add_raid_target(node, len, _raid_name(seg),
					  seg->region_size, seg->stripe_size,
					  0, 0))
		return_0;

	return add_areas_line(dm, seg, node, 0u, seg->area_count);
}

static int _raid_target_status_compatible(const char *type)
{
	return (strstr(type, "raid") != NULL);
}

static int _raid_target_percent(void **target_state,
				percent_t *percent,
				struct dm_pool *mem,
				struct cmd_context *cmd,
				struct lv_segment *seg, char *params,
				uint64_t *total_numerator,
				uint64_t *total_denominator)
{
	int i;
	uint64_t numerator, denominator;
	char *pos = params;
	/*
	 * Status line:
	 *    <raid_type> <#devs> <status_chars> <synced>/<total>
	 * Example:
	 *    raid1 2 AA 1024000/1024000
	 */
	for (i = 0; i < 3; i++) {
		pos = strstr(pos, " ");
		if (pos)
			pos++;
		else
			break;
	}
	if (!pos || (sscanf(pos, "%" PRIu64 "/%" PRIu64 "%n",
			    &numerator, &denominator, &i) != 2)) {
		log_error("Failed to parse %s status fraction: %s",
			  seg->segtype->name, params);
		return 0;
	}

	*total_numerator += numerator;
	*total_denominator += denominator;

	if (seg)
		seg->extents_copied = seg->area_len * numerator / denominator;

	*percent = make_percent(numerator, denominator);

	return 1;
}


static int
_raid_target_present(struct cmd_context *cmd,
		     const struct lv_segment *seg __attribute__((unused)),
		     unsigned *attributes __attribute__((unused)))
{
	static int _raid_checked = 0;
	static int _raid_present = 0;

	if (!_raid_checked)
		_raid_present = target_present(cmd, "raid", 1);

	_raid_checked = 1;

	return _raid_present;
}

static int
_raid_modules_needed(struct dm_pool *mem,
		     const struct lv_segment *seg __attribute__((unused)),
		     struct dm_list *modules)
{
	if (!str_list_add(mem, modules, "raid")) {
		log_error("raid module string list allocation failed");
		return 0;
	}

	return 1;
}

static void _raid_destroy(struct segment_type *segtype)
{
	dm_free((void *) segtype);
}

static struct segtype_handler _raid_ops = {
	.name = _raid_name,
	.text_import_area_count = _raid_text_import_area_count,
	.text_import = _raid_text_import,
	.text_export = _raid_text_export,
	.add_target_line = _raid_add_target_line,
	.target_status_compatible = _raid_target_status_compatible,
	.target_percent = _raid_target_percent,
	.target_present = _raid_target_present,
	.modules_needed = _raid_modules_needed,
	.destroy = _raid_destroy,
};

static struct segment_type *init_raid_segtype(struct cmd_context *cmd,
					      const char *raid_type)
{
	struct segment_type *segtype = dm_malloc(sizeof(*segtype));

	if (!segtype)
		return_NULL;

	segtype->cmd = cmd;

	segtype->flags = SEG_RAID;
	segtype->parity_devs = strstr(raid_type, "raid6") ? 2 : 1;

	segtype->ops = &_raid_ops;
	segtype->name = raid_type;

	segtype->private = NULL;

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

	return segtype;
}

struct segment_type *init_raid1_segtype(struct cmd_context *cmd)
{
	struct segment_type *segtype;

	segtype = init_raid_segtype(cmd, "raid1");
	if (!segtype)
		return NULL;

	segtype->flags |= SEG_AREAS_MIRRORED;
	segtype->parity_devs = 0;

	return segtype;
}
struct segment_type *init_raid4_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid4");
}
struct segment_type *init_raid5_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid5");
}
struct segment_type *init_raid5_la_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid5_la");
}
struct segment_type *init_raid5_ra_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid5_ra");
}
struct segment_type *init_raid5_ls_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid5_ls");
}
struct segment_type *init_raid5_rs_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid5_rs");
}
struct segment_type *init_raid6_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid6");
}
struct segment_type *init_raid6_zr_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid6_zr");
}
struct segment_type *init_raid6_nr_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid6_nr");
}
struct segment_type *init_raid6_nc_segtype(struct cmd_context *cmd)
{
	return init_raid_segtype(cmd, "raid6_nc");
}