summaryrefslogtreecommitdiffstats
path: root/extras/stripe-merge.c
blob: 32768badd366a85c02f7e1a3d0ee5c6cbf7981f4 (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
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

/*
 * stripe-merge.c
 *
 * This program recovers an original file based on the striped files stored on
 * the individual bricks of a striped volume. The file format and stripe
 * geometry is validated through the extended attributes stored in the file.
 *
 * TODO: Support optional xattr recovery (i.e., user xattrs). Perhaps provide a
 * 	 command-line flag to toggle this behavior.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <attr/xattr.h>
#include <fnmatch.h>

#define ATTRNAME_STRIPE_INDEX "trusted.*.stripe-index"
#define ATTRNAME_STRIPE_COUNT "trusted.*.stripe-count"
#define ATTRNAME_STRIPE_SIZE "trusted.*.stripe-size"
#define ATTRNAME_STRIPE_COALESCE "trusted.*.stripe-coalesce"

#define INVALID_FD -1
#define INVALID_MODE UINT32_MAX

struct file_stripe_info {
	int stripe_count;
	int stripe_size;
	int coalesce;
	mode_t mode;
	int fd[0];
};

static int close_files(struct file_stripe_info *);

static struct
file_stripe_info *alloc_file_stripe_info(int count)
{
	int i;
	struct file_stripe_info *finfo;

	finfo = calloc(1, sizeof(struct file_stripe_info) +
		(sizeof(int) * count));
	if (!finfo)
		return NULL;

	for (i = 0; i < count; i++)
		finfo->fd[i] = INVALID_FD;

	finfo->mode = INVALID_MODE;
	finfo->coalesce = INVALID_FD;

	return finfo;
}

/*
 * Search for an attribute matching the provided pattern. Return a count for
 * the total number of matching entries (including 0). Allocate a buffer for
 * the first matching entry found.
 */
static int
get_stripe_attr_name(const char *path, const char *pattern, char **attrname)
{
	char attrbuf[4096];
	char *ptr, *match = NULL;
	int len, r, match_count = 0;

	if (!path || !pattern || !attrname)
		return -1;

	len = listxattr(path, attrbuf, sizeof(attrbuf));
	if (len < 0)
		return len;

	ptr = attrbuf;
	while (ptr) {
		r = fnmatch(pattern, ptr, 0);
		if (!r) {
			if (!match)
				match = ptr;
			match_count++;
		} else if (r != FNM_NOMATCH) {
			return -1;
		}

		len -= strlen(ptr) + 1;
		if (len > 0)
			ptr += strlen(ptr) + 1;
		else
			ptr = NULL;
	}

	if (match)
		*attrname = strdup(match);

	return match_count;
}

/*
 * Get the integer representation of a named attribute.
 */
static int
get_stripe_attr_val(const char *path, const char *attr, int *val)
{
	char attrbuf[4096];
	int len;

	if (!path || !attr || !val)
		return -1;

	len = getxattr(path, attr, attrbuf, sizeof(attrbuf));
	if (len < 0)
		return len;

	*val = atoi(attrbuf);

	return 0;
}

/*
 * Get an attribute name/value (assumed to be an integer) pair based on a
 * specified search pattern. A buffer is allocated for the exact attr name
 * returned. Optionally, skip the pattern search if a buffer is provided
 * (which should contain an attribute name).
 *
 * Returns the attribute count or -1 on error. The value parameter is set only
 * when a single attribute is found.
 */
static int
get_attr(const char *path, const char *pattern, char **buf, int *val)
{
	int count = 1;

	if (!buf)
		return -1;

	if (!*buf) {
		count = get_stripe_attr_name(path, pattern, buf);
		if (count > 1) {
			/* pattern isn't good enough */
			fprintf(stderr, "ERROR: duplicate attributes found "
				"matching pattern: %s\n", pattern);
			free(*buf);
			*buf = NULL;
			return count;
		} else if (count < 1) {
			return count;
		}
	}

	if (get_stripe_attr_val(path, *buf, val) < 0)
		return -1;

	return count;
}

/*
 * validate_and_open_files()
 *
 * Open the provided source files and validate the extended attributes. Verify
 * that the geometric attributes are consistent across all of the files and
 * print a warning if any files are missing. We proceed without error in the
 * latter case to support partial recovery.
 */
static struct
file_stripe_info *validate_and_open_files(char *paths[], int count)
{
	int i, val, tmp;
	struct stat sbuf;
	char *stripe_count_attr = NULL;
	char *stripe_size_attr = NULL;
	char *stripe_index_attr = NULL;
	char *stripe_coalesce_attr = NULL;
	struct file_stripe_info *finfo = NULL;

	for (i = 0; i < count; i++) {
		if (!paths[i])
			goto err;

		/*
		 * Check the stripe count first so we can allocate the info
		 * struct with the appropriate number of fds.
		 */
		if (get_attr(paths[i], ATTRNAME_STRIPE_COUNT,
				&stripe_count_attr, &val) != 1) {
			fprintf(stderr, "ERROR: %s: attribute: '%s'\n",
				paths[i], ATTRNAME_STRIPE_COUNT);
			goto err;
		}
		if (!finfo) {
			finfo = alloc_file_stripe_info(val);
			if (!finfo)
				goto err;

			if (val != count)
				fprintf(stderr, "WARNING: %s: stripe-count "
					"(%d) != file count (%d). Result may "
					"be incomplete.\n", paths[i], val,
					count);

			finfo->stripe_count = val;
		} else if (val != finfo->stripe_count) {
			fprintf(stderr, "ERROR %s: invalid stripe count: %d "
				"(expected %d)\n", paths[i], val,
				finfo->stripe_count);
			goto err;
		}

		/*
		 * Get and validate the chunk size.
		 */
		if (get_attr(paths[i], ATTRNAME_STRIPE_SIZE, &stripe_size_attr,
				&val) != 1) {
			fprintf(stderr, "ERROR: %s: attribute: '%s'\n",
				paths[i], ATTRNAME_STRIPE_SIZE);
			goto err;
		}

		if (!finfo->stripe_size) {
			finfo->stripe_size = val;
		} else if (val != finfo->stripe_size) {
			fprintf(stderr, "ERROR: %s: invalid stripe size: %d "
				"(expected %d)\n", paths[i], val,
				finfo->stripe_size);
			goto err;
		}

		/*
		 * stripe-coalesce is a backward compatible attribute. If the
		 * attribute does not exist, assume a value of zero for the
		 * traditional stripe format.
		 */
		tmp = get_attr(paths[i], ATTRNAME_STRIPE_COALESCE,
				&stripe_coalesce_attr, &val);
		if (!tmp) {
			val = 0;
		} else if (tmp != 1) {
			fprintf(stderr, "ERROR: %s: attribute: '%s'\n",
				paths[i], ATTRNAME_STRIPE_COALESCE);
			goto err;
		}

		if (finfo->coalesce == INVALID_FD) {
			finfo->coalesce = val;
		} else if (val != finfo->coalesce) {
			fprintf(stderr, "ERROR: %s: invalid coalesce flag\n",
				paths[i]);
			goto err;
		}

		/*
		 * Get/validate the stripe index and open the file in the
		 * appropriate fd slot.
		 */
		if (get_attr(paths[i], ATTRNAME_STRIPE_INDEX,
				&stripe_index_attr, &val) != 1) {
			fprintf(stderr, "ERROR: %s: attribute: '%s'\n",
				paths[i], ATTRNAME_STRIPE_INDEX);
			goto err;
		}
		if (finfo->fd[val] != INVALID_FD) {
			fprintf(stderr, "ERROR: %s: duplicate stripe index: "
				"%d\n", paths[i], val);
			goto err;
		}

		finfo->fd[val] = open(paths[i], O_RDONLY);
		if (finfo->fd[val] < 0)
			goto err;

		/*
		 * Get the creation mode for the file.
		 */
		if (fstat(finfo->fd[val], &sbuf) < 0)
			goto err;
		if (finfo->mode == INVALID_MODE) {
			finfo->mode = sbuf.st_mode;
		} else if (sbuf.st_mode != finfo->mode) {
			fprintf(stderr, "ERROR: %s: invalid mode\n", paths[i]);
			goto err;
		}
	}

	free(stripe_count_attr);
	free(stripe_size_attr);
	free(stripe_index_attr);
	free(stripe_coalesce_attr);

	return finfo;
err:

	if (stripe_count_attr)
		free(stripe_count_attr);
	if (stripe_size_attr)
		free(stripe_size_attr);
	if (stripe_index_attr)
		free(stripe_index_attr);
	if (stripe_coalesce_attr)
		free(stripe_coalesce_attr);

	if (finfo) {
		close_files(finfo);
		free(finfo);
	}

	return NULL;
}

static int
close_files(struct file_stripe_info *finfo)
{
	int i, ret;

	if (!finfo)
		return -1;

	for (i = 0; i < finfo->stripe_count; i++) {
		if (finfo->fd[i] == INVALID_FD)
			continue;

		ret = close(finfo->fd[i]);
		if (ret < 0)
			return ret;
	}

	return ret;
}

/*
 * Generate the original file using files striped in the coalesced format.
 * Data in the striped files is stored at a coalesced offset based on the
 * stripe number.
 *
 * Walk through the finfo fds (which are already ordered) and and iteratively
 * copy stripe_size bytes from the source files to the target file. If a source
 * file is missing, seek past the associated stripe_size bytes in the target
 * file.
 */
static int
generate_file_coalesce(int target, struct file_stripe_info *finfo)
{
	char *buf;
	int ret = 0;
	int r, w, i;

	buf = malloc(finfo->stripe_size);
	if (!buf)
		return -1;

	i = 0;
	while (1) {
		if (finfo->fd[i] == INVALID_FD) {
			if (lseek(target, finfo->stripe_size, SEEK_CUR) < 0)
				break;

			i = (i + 1) % finfo->stripe_count;
			continue;
		}

		r = read(finfo->fd[i], buf, finfo->stripe_size);
		if (r < 0) {
			ret = r;
			break;
		}
		if (!r)
			break;

		w = write(target, buf, r);
		if (w < 0) {
			ret = w;
			break;
		}

		i = (i + 1) % finfo->stripe_count;
	}

	free(buf);
	return ret;
}

/*
 * Generate the original file using files striped with the traditional stripe
 * format. Data in the striped files is stored at the equivalent offset from
 * the source file.
 */
static int
generate_file_traditional(int target, struct file_stripe_info *finfo)
{
	int i, j, max_ret, ret;
	char buf[finfo->stripe_count][4096];

	do {
		char newbuf[4096] = {0, };

		max_ret = 0;
		for (i = 0; i < finfo->stripe_count; i++) {
			memset(buf[i], 0, 4096);
			ret = read(finfo->fd[i], buf[i], 4096);
			if (ret > max_ret)
				max_ret = ret;
		}
		for (i = 0; i < max_ret; i++)
			for (j = 0; j < finfo->stripe_count; j++)
				newbuf[i] |= buf[j][i];
			write(target, newbuf, max_ret);
	} while (max_ret);

	return 0;
}

static int
generate_file(int target, struct file_stripe_info *finfo)
{
	if (finfo->coalesce)
		return generate_file_coalesce(target, finfo);

	return generate_file_traditional(target, finfo);
}

static void
usage(char *name)
{
	fprintf(stderr, "Usage: %s [-o <outputfile>] <inputfile1> "
		"<inputfile2> ...\n", name);
}

int
main(int argc, char *argv[])
{
	int file_count, opt;
	char *opath = NULL;
	int targetfd;
	struct file_stripe_info *finfo;

	while ((opt = getopt(argc, argv, "o:")) != -1) {
		switch (opt) {
		case 'o':
			opath = optarg;
			break;
		default:
			usage(argv[0]);
			return -1;
		}
	}

	file_count = argc - optind;

	if (!opath || !file_count) {
		usage(argv[0]);
		return -1;
	}

	finfo = validate_and_open_files(&argv[optind], file_count);
	if (!finfo)
		goto err;

	targetfd = open(opath, O_RDWR|O_CREAT, finfo->mode);
	if (targetfd < 0)
		goto err;

	if (generate_file(targetfd, finfo) < 0)
		goto err;

	if (fsync(targetfd) < 0)
		fprintf(stderr, "ERROR: %s\n", strerror(errno));
	if (close(targetfd) < 0)
		fprintf(stderr, "ERROR: %s\n", strerror(errno));

	close_files(finfo);
	free(finfo);

	return 0;

err:
	if (finfo) {
		close_files(finfo);
		free(finfo);
	}

	return -1;
}