summaryrefslogtreecommitdiffstats
path: root/src/pmdk/src/libpmempool/check_backup.c
blob: f339bf04b5c535cb2d92e2c06a6fa54bd3444c60 (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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2016-2018, Intel Corporation */

/*
 * check_backup.c -- pre-check backup
 */

#include <stddef.h>
#include <stdint.h>
#include <unistd.h>

#include "out.h"
#include "file.h"
#include "os.h"
#include "libpmempool.h"
#include "pmempool.h"
#include "pool.h"
#include "check_util.h"

enum question {
	Q_OVERWRITE_EXISTING_FILE,
	Q_OVERWRITE_EXISTING_PARTS
};

/*
 * location_release -- (internal) release poolset structure
 */
static void
location_release(location *loc)
{
	if (loc->set) {
		util_poolset_free(loc->set);
		loc->set = NULL;
	}
}

/*
 * backup_nonpoolset_requirements -- (internal) check backup requirements
 */
static int
backup_nonpoolset_requirements(PMEMpoolcheck *ppc, location *loc)
{
	LOG(3, "backup_path %s", ppc->backup_path);

	int exists = util_file_exists(ppc->backup_path);
	if (exists < 0) {
		return CHECK_ERR(ppc,
				"unable to access the backup destination: %s",
				ppc->backup_path);
	}

	if (!exists) {
		errno = 0;
		return 0;
	}

	if ((size_t)util_file_get_size(ppc->backup_path) !=
			ppc->pool->set_file->size) {
		ppc->result = CHECK_RESULT_ERROR;
		return CHECK_ERR(ppc,
			"destination of the backup does not match the size of the source pool file: %s",
			ppc->backup_path);
	}

	if (CHECK_WITHOUT_FIXING(ppc)) {
		location_release(loc);
		loc->step = CHECK_STEP_COMPLETE;
		return 0;
	}

	CHECK_ASK(ppc, Q_OVERWRITE_EXISTING_FILE,
		"destination of the backup already exists.|Do you want to overwrite it?");

	return check_questions_sequence_validate(ppc);
}

/*
 * backup_nonpoolset_overwrite -- (internal) overwrite pool
 */
static int
backup_nonpoolset_overwrite(PMEMpoolcheck *ppc, location *loc,
	uint32_t question, void *context)
{
	LOG(3, NULL);

	ASSERTne(loc, NULL);

	switch (question) {
	case Q_OVERWRITE_EXISTING_FILE:
		if (pool_copy(ppc->pool, ppc->backup_path, 1 /* overwrite */)) {
			location_release(loc);
			ppc->result = CHECK_RESULT_ERROR;
			return CHECK_ERR(ppc, "cannot perform backup");
		}

		location_release(loc);
		loc->step = CHECK_STEP_COMPLETE;
		return 0;
	default:
		ERR("not implemented question id: %u", question);
	}

	return 0;
}

/*
 * backup_nonpoolset_create -- (internal) create backup
 */
static int
backup_nonpoolset_create(PMEMpoolcheck *ppc, location *loc)
{
	CHECK_INFO(ppc, "creating backup file: %s", ppc->backup_path);

	if (pool_copy(ppc->pool, ppc->backup_path, 0)) {
		location_release(loc);
		ppc->result = CHECK_RESULT_ERROR;
		return CHECK_ERR(ppc, "cannot perform backup");
	}

	location_release(loc);
	loc->step = CHECK_STEP_COMPLETE;
	return 0;
}

/*
 * backup_poolset_requirements -- (internal) check backup requirements
 */
static int
backup_poolset_requirements(PMEMpoolcheck *ppc, location *loc)
{
	LOG(3, "backup_path %s", ppc->backup_path);

	if (ppc->pool->set_file->poolset->nreplicas > 1) {
		CHECK_INFO(ppc,
			"backup of a poolset with multiple replicas is not supported");
		goto err;
	}

	if (pool_set_parse(&loc->set, ppc->backup_path)) {
		CHECK_INFO_ERRNO(ppc, "invalid poolset backup file: %s",
			ppc->backup_path);
		goto err;
	}

	if (loc->set->nreplicas > 1) {
		CHECK_INFO(ppc,
			"backup to a poolset with multiple replicas is not supported");
		goto err_poolset;
	}

	ASSERTeq(loc->set->nreplicas, 1);
	struct pool_replica *srep = ppc->pool->set_file->poolset->replica[0];
	struct pool_replica *drep = loc->set->replica[0];
	if (srep->nparts != drep->nparts) {
		CHECK_INFO(ppc,
			"number of part files in the backup poolset must match number of part files in the source poolset");
		goto err_poolset;
	}

	int overwrite_required = 0;
	for (unsigned p = 0; p < srep->nparts; p++) {
		int exists = util_file_exists(drep->part[p].path);
		if (exists < 0) {
			CHECK_INFO(ppc,
				"unable to access the part of the destination poolset: %s",
				ppc->backup_path);
			goto err_poolset;
		}

		if (srep->part[p].filesize != drep->part[p].filesize) {
			CHECK_INFO(ppc,
				"size of the part %u of the backup poolset does not match source poolset",
				p);
			goto err_poolset;
		}

		if (!exists) {
			errno = 0;
			continue;
		}

		overwrite_required = true;

		if ((size_t)util_file_get_size(drep->part[p].path) !=
				srep->part[p].filesize) {
			CHECK_INFO(ppc,
				"destination of the backup part does not match size of the source part file: %s",
				drep->part[p].path);
			goto err_poolset;
		}
	}

	if (CHECK_WITHOUT_FIXING(ppc)) {
		location_release(loc);
		loc->step = CHECK_STEP_COMPLETE;
		return 0;
	}

	if (overwrite_required) {
		CHECK_ASK(ppc, Q_OVERWRITE_EXISTING_PARTS,
			"part files of the destination poolset of the backup already exist.|"
			"Do you want to overwrite them?");
	}

	return check_questions_sequence_validate(ppc);

err_poolset:
	location_release(loc);
err:
	ppc->result = CHECK_RESULT_ERROR;
	return CHECK_ERR(ppc, "unable to backup poolset");
}

/*
 * backup_poolset -- (internal) backup the poolset
 */
static int
backup_poolset(PMEMpoolcheck *ppc, location *loc, int overwrite)
{
	struct pool_replica *srep = ppc->pool->set_file->poolset->replica[0];
	struct pool_replica *drep = loc->set->replica[0];
	for (unsigned p = 0; p < srep->nparts; p++) {
		if (overwrite == 0) {
			CHECK_INFO(ppc, "creating backup file: %s",
				drep->part[p].path);
		}
		if (pool_set_part_copy(&drep->part[p], &srep->part[p],
				overwrite)) {
			location_release(loc);
			ppc->result = CHECK_RESULT_ERROR;
			CHECK_INFO(ppc, "unable to create backup file");
			return CHECK_ERR(ppc, "unable to backup poolset");
		}
	}

	return 0;
}

/*
 * backup_poolset_overwrite -- (internal) backup poolset with overwrite
 */
static int
backup_poolset_overwrite(PMEMpoolcheck *ppc, location *loc,
	uint32_t question, void *context)
{
	LOG(3, NULL);

	ASSERTne(loc, NULL);

	switch (question) {
	case Q_OVERWRITE_EXISTING_PARTS:
		if (backup_poolset(ppc, loc, 1 /* overwrite */)) {
			location_release(loc);
			ppc->result = CHECK_RESULT_ERROR;
			return CHECK_ERR(ppc, "cannot perform backup");
		}

		location_release(loc);
		loc->step = CHECK_STEP_COMPLETE;
		return 0;
	default:
		ERR("not implemented question id: %u", question);
	}

	return 0;
}

/*
 * backup_poolset_create -- (internal) backup poolset
 */
static int
backup_poolset_create(PMEMpoolcheck *ppc, location *loc)
{
	if (backup_poolset(ppc, loc, 0)) {
		location_release(loc);
		ppc->result = CHECK_RESULT_ERROR;
		return CHECK_ERR(ppc, "cannot perform backup");
	}

	location_release(loc);
	loc->step = CHECK_STEP_COMPLETE;
	return 0;
}

struct step {
	int (*check)(PMEMpoolcheck *, location *);
	int (*fix)(PMEMpoolcheck *, location *, uint32_t, void *);
	int poolset;
};

static const struct step steps[] = {
	{
		.check		= backup_nonpoolset_requirements,
		.poolset	= false,
	},
	{
		.fix		= backup_nonpoolset_overwrite,
		.poolset	= false,
	},
	{
		.check		= backup_nonpoolset_create,
		.poolset	= false
	},
	{
		.check		= backup_poolset_requirements,
		.poolset	= true,
	},
	{
		.fix		= backup_poolset_overwrite,
		.poolset	= true,
	},
	{
		.check		= backup_poolset_create,
		.poolset	= true
	},
	{
		.check		= NULL,
		.fix		= NULL,
	},
};

/*
 * step_exe -- (internal) perform single step according to its parameters
 */
static int
step_exe(PMEMpoolcheck *ppc, location *loc)
{
	ASSERT(loc->step < ARRAY_SIZE(steps));

	const struct step *step = &steps[loc->step++];

	if (step->poolset == 0 && ppc->pool->params.is_poolset == 1)
		return 0;

	if (!step->fix)
		return step->check(ppc, loc);

	if (!check_has_answer(ppc->data))
		return 0;

	if (check_answer_loop(ppc, loc, NULL, 1, step->fix))
		return -1;

	ppc->result = CHECK_RESULT_CONSISTENT;

	return 0;
}

/*
 * check_backup -- perform backup if requested and needed
 */
void
check_backup(PMEMpoolcheck *ppc)
{
	LOG(3, "backup_path %s", ppc->backup_path);

	if (ppc->backup_path == NULL)
		return;

	location *loc = check_get_step_data(ppc->data);

	/* do all checks */
	while (CHECK_NOT_COMPLETE(loc, steps)) {
		if (step_exe(ppc, loc))
			break;
	}
}