summaryrefslogtreecommitdiffstats
path: root/src/lib-fs/fs-randomfail.c
blob: 48b1bf0431994fe7c261deab559ec5484523d8e9 (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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "istream.h"
#include "istream-private.h"
#include "istream-concat.h"
#include "istream-failure-at.h"
#include "ostream-failure-at.h"
#include "ostream.h"
#include "fs-api-private.h"


#define RANDOMFAIL_ERROR "Random failure injection"

static const char *fs_op_names[FS_OP_COUNT] = {
	"wait", "metadata", "prefetch", "read", "write", "lock", "exists",
	"stat", "copy", "rename", "delete", "iter"
};

struct randomfail_fs {
	struct fs fs;
	unsigned int op_probability[FS_OP_COUNT];
	uoff_t range_start[FS_OP_COUNT], range_end[FS_OP_COUNT];
};

struct randomfail_fs_file {
	struct fs_file file;
	struct fs_file *super_read;
	struct istream *input;
	bool op_pending[FS_OP_COUNT];

	struct ostream *super_output;
};

struct randomfail_fs_iter {
	struct fs_iter iter;
	struct fs_iter *super;
	unsigned int fail_pos;
};

#define RANDOMFAIL_FS(ptr)	container_of((ptr), struct randomfail_fs, fs)
#define RANDOMFAIL_FILE(ptr)	container_of((ptr), struct randomfail_fs_file, file)
#define RANDOMFAIL_ITER(ptr)	container_of((ptr), struct randomfail_fs_iter, iter)

static struct fs *fs_randomfail_alloc(void)
{
	struct randomfail_fs *fs;

	fs = i_new(struct randomfail_fs, 1);
	fs->fs = fs_class_randomfail;
	return &fs->fs;
}

static bool fs_op_find(const char *str, enum fs_op *op_r)
{
	enum fs_op op;

	for (op = 0; op < FS_OP_COUNT; op++) {
		if (strcmp(fs_op_names[op], str) == 0) {
			*op_r = op;
			return TRUE;
		}
	}
	return FALSE;
}

static int
fs_randomfail_add_probability(struct randomfail_fs *fs,
			      const char *key, const char *value,
			      const char **error_r)
{
	unsigned int num;
	enum fs_op op;
	bool invalid_value = FALSE;

	if (str_to_uint(value, &num) < 0 || num > 100)
		invalid_value = TRUE;
	if (fs_op_find(key, &op)) {
		if (invalid_value) {
			*error_r = "Invalid probability value";
			return -1;
		}
		fs->op_probability[op] = num;
		return 1;
	}
	if (strcmp(key, "all") == 0) {
		if (invalid_value) {
			*error_r = "Invalid probability value";
			return -1;
		}
		for (op = 0; op < FS_OP_COUNT; op++)
			fs->op_probability[op] = num;
		return 1;
	}
	return 0;
}

static int
fs_randomfail_add_probability_range(struct randomfail_fs *fs,
				    const char *key, const char *value,
				    const char **error_r)
{
	enum fs_op op;
	const char *p;
	uoff_t num1, num2;

	if (strcmp(key, "read-range") == 0)
		op = FS_OP_READ;
	else if (strcmp(key, "write-range") == 0)
		op = FS_OP_WRITE;
	else if (strcmp(key, "iter-range") == 0)
		op = FS_OP_ITER;
	else
		return 0;

	p = strchr(value, '-');
	if (p == NULL) {
		if (str_to_uoff(value, &num1) < 0) {
			*error_r = "Invalid range value";
			return -1;
		}
		num2 = num1;
	} else if (str_to_uoff(t_strdup_until(value, p), &num1) < 0 ||
		   str_to_uoff(p+1, &num2) < 0 || num1 > num2) {
		*error_r = "Invalid range values";
		return -1;
	}
	fs->range_start[op] = num1;
	fs->range_end[op] = num2;
	return 1;
}

static int fs_randomfail_parse_params(struct randomfail_fs *fs,
				      const char *params, const char **error_r)
{
	const char *const *tmp;
	int ret;

	for (tmp = t_strsplit_spaces(params, ","); *tmp != NULL; tmp++) {
		const char *key = *tmp;
		const char *value = strchr(key, '=');

		if (value == NULL) {
			*error_r = "Missing '='";
			return -1;
		}
		key = t_strdup_until(key, value++);
		if ((ret = fs_randomfail_add_probability(fs, key, value, error_r)) != 0) {
			if (ret < 0)
				return -1;
			continue;
		}
		if ((ret = fs_randomfail_add_probability_range(fs, key, value, error_r)) != 0) {
			if (ret < 0)
				return -1;
			continue;
		}
		*error_r = t_strdup_printf("Unknown key '%s'", key);
		return -1;
	}
	return 0;
}

static int
fs_randomfail_init(struct fs *_fs, const char *args,
		   const struct fs_settings *set, const char **error_r)
{
	struct randomfail_fs *fs = RANDOMFAIL_FS(_fs);
	const char *p, *parent_name, *parent_args, *error;

	p = strchr(args, ':');
	if (p == NULL) {
		*error_r = "Randomfail parameters missing";
		return -1;
	}
	if (fs_randomfail_parse_params(fs, t_strdup_until(args, p++), &error) < 0) {
		*error_r = t_strdup_printf(
			"Invalid randomfail parameters: %s", error);
		return -1;
	}
	args = p;

	if (*args == '\0') {
		*error_r = "Parent filesystem not given as parameter";
		return -1;
	}

	parent_args = strchr(args, ':');
	if (parent_args == NULL) {
		parent_name = args;
		parent_args = "";
	} else {
		parent_name = t_strdup_until(args, parent_args);
		parent_args++;
	}
	if (fs_init(parent_name, parent_args, set, &_fs->parent, error_r) < 0)
		return -1;
	return 0;
}

static void fs_randomfail_free(struct fs *_fs)
{
	struct randomfail_fs *fs = RANDOMFAIL_FS(_fs);

	i_free(fs);
}

static enum fs_properties fs_randomfail_get_properties(struct fs *_fs)
{
	return fs_get_properties(_fs->parent);
}

static struct fs_file *fs_randomfail_file_alloc(void)
{
	struct randomfail_fs_file *file = i_new(struct randomfail_fs_file, 1);
	return &file->file;
}

static void
fs_randomfail_file_init(struct fs_file *_file, const char *path,
			enum fs_open_mode mode, enum fs_open_flags flags)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);

	file->file.path = i_strdup(path);
	file->file.parent = fs_file_init_parent(_file, path, mode, flags);
}

static void fs_randomfail_file_deinit(struct fs_file *_file)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);

	fs_file_free(_file);
	i_free(file->file.path);
	i_free(file);
}

static bool fs_random_fail(struct fs *_fs, struct event *event,
			   int divider, enum fs_op op)
{
	struct randomfail_fs *fs = RANDOMFAIL_FS(_fs);

	if (fs->op_probability[op] == 0)
		return FALSE;
	if ((unsigned int)i_rand_limit(100 * divider) <= fs->op_probability[op]) {
		fs_set_error(event, EIO, RANDOMFAIL_ERROR);
		return TRUE;
	}
	return FALSE;
}

static bool
fs_file_random_fail_begin(struct randomfail_fs_file *file, enum fs_op op)
{
	if (!file->op_pending[op]) {
		if (fs_random_fail(file->file.fs, file->file.event, 2, op))
			return TRUE;
	}
	file->op_pending[op] = TRUE;
	return FALSE;
}

static int
fs_file_random_fail_end(struct randomfail_fs_file *file,
			int ret, enum fs_op op)
{
	if (ret == 0 || errno != EAGAIN) {
		if (fs_random_fail(file->file.fs, file->file.event, 2, op))
			return -1;
		file->op_pending[op] = FALSE;
	}
	return ret;
}

static bool
fs_random_fail_range(struct fs *_fs, struct event *event,
		     enum fs_op op, uoff_t *offset_r)
{
	struct randomfail_fs *fs = RANDOMFAIL_FS(_fs);

	if (!fs_random_fail(_fs, event, 1, op))
		return FALSE;
	*offset_r = i_rand_minmax(fs->range_start[op], fs->range_end[op]);
	return TRUE;
}

static int
fs_randomfail_get_metadata(struct fs_file *_file,
			   enum fs_get_metadata_flags flags,
			   const ARRAY_TYPE(fs_metadata) **metadata_r)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);
	int ret;

	if (fs_file_random_fail_begin(file, FS_OP_METADATA))
		return -1;
	ret = fs_get_metadata_full(_file->parent, flags, metadata_r);
	return fs_file_random_fail_end(file, ret, FS_OP_METADATA);
}

static bool fs_randomfail_prefetch(struct fs_file *_file, uoff_t length)
{
	if (fs_random_fail(_file->fs, _file->event, 1, FS_OP_PREFETCH))
		return TRUE;
	return fs_prefetch(_file->parent, length);
}

static ssize_t fs_randomfail_read(struct fs_file *_file, void *buf, size_t size)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);
	int ret;

	if (fs_file_random_fail_begin(file, FS_OP_READ))
		return -1;
	ret = fs_read(_file->parent, buf, size);
	if (fs_file_random_fail_end(file, ret < 0 ? -1 : 0, FS_OP_READ) < 0)
		return -1;
	return ret;
}

static struct istream *
fs_randomfail_read_stream(struct fs_file *_file, size_t max_buffer_size)
{
	struct istream *input, *input2;
	uoff_t offset;

	input = fs_read_stream(_file->parent, max_buffer_size);
	if (!fs_random_fail_range(_file->fs, _file->event, FS_OP_READ, &offset))
		return input;
	input2 = i_stream_create_failure_at(input, offset, EIO, RANDOMFAIL_ERROR);
	i_stream_unref(&input);
	return input2;
}

static int fs_randomfail_write(struct fs_file *_file, const void *data, size_t size)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);
	int ret;

	if (fs_file_random_fail_begin(file, FS_OP_WRITE))
		return -1;
	ret = fs_write(_file->parent, data, size);
	return fs_file_random_fail_end(file, ret, FS_OP_EXISTS);
}

static void fs_randomfail_write_stream(struct fs_file *_file)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);
	uoff_t offset;

	i_assert(_file->output == NULL);

	file->super_output = fs_write_stream(_file->parent);
	if (!fs_random_fail_range(_file->fs, _file->event, FS_OP_WRITE, &offset))
		_file->output = file->super_output;
	else {
		_file->output = o_stream_create_failure_at(file->super_output, offset,
							   RANDOMFAIL_ERROR);
	}
}

static int fs_randomfail_write_stream_finish(struct fs_file *_file, bool success)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);

	if (_file->output != NULL) {
		if (_file->output == file->super_output)
			_file->output = NULL;
		else
			o_stream_unref(&_file->output);
		if (!success) {
			fs_write_stream_abort_parent(_file, &file->super_output);
			return -1;
		}
		if (fs_random_fail(_file->fs, _file->event, 1, FS_OP_WRITE)) {
			fs_write_stream_abort_error(_file->parent, &file->super_output, RANDOMFAIL_ERROR);
			return -1;
		}
	}
	return fs_write_stream_finish(_file->parent, &file->super_output);
}

static int
fs_randomfail_lock(struct fs_file *_file, unsigned int secs, struct fs_lock **lock_r)
{
	if (fs_random_fail(_file->fs, _file->event, 1, FS_OP_LOCK))
		return -1;
	return fs_lock(_file->parent, secs, lock_r);
}

static void fs_randomfail_unlock(struct fs_lock *_lock ATTR_UNUSED)
{
	i_unreached();
}

static int fs_randomfail_exists(struct fs_file *_file)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);
	int ret;

	if (fs_file_random_fail_begin(file, FS_OP_EXISTS))
		return -1;
	ret = fs_exists(_file->parent);
	return fs_file_random_fail_end(file, ret, FS_OP_EXISTS);
}

static int fs_randomfail_stat(struct fs_file *_file, struct stat *st_r)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);
	int ret;

	if (fs_file_random_fail_begin(file, FS_OP_STAT))
		return -1;
	ret = fs_stat(_file->parent, st_r);
	return fs_file_random_fail_end(file, ret, FS_OP_STAT);
}

static int fs_randomfail_get_nlinks(struct fs_file *_file, nlink_t *nlinks_r)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);
	int ret;

	if (fs_file_random_fail_begin(file, FS_OP_STAT))
		return -1;
	ret = fs_get_nlinks(_file->parent, nlinks_r);
	return fs_file_random_fail_end(file, ret, FS_OP_STAT);
}

static int fs_randomfail_copy(struct fs_file *_src, struct fs_file *_dest)
{
	struct randomfail_fs_file *dest = RANDOMFAIL_FILE(_dest);
	int ret;

	if (fs_file_random_fail_begin(dest, FS_OP_COPY))
		return -1;

	if (_src != NULL)
		ret = fs_copy(_src->parent, _dest->parent);
	else
		ret = fs_copy_finish_async(_dest->parent);
	return fs_file_random_fail_end(dest, ret, FS_OP_COPY);
}

static int fs_randomfail_rename(struct fs_file *_src, struct fs_file *_dest)
{
	struct randomfail_fs_file *dest = RANDOMFAIL_FILE(_dest);
	int ret;

	if (fs_file_random_fail_begin(dest, FS_OP_RENAME))
		return -1;
	ret = fs_rename(_src->parent, _dest->parent);
	return fs_file_random_fail_end(dest, ret, FS_OP_RENAME);
}

static int fs_randomfail_delete(struct fs_file *_file)
{
	struct randomfail_fs_file *file = RANDOMFAIL_FILE(_file);
	int ret;

	if (fs_file_random_fail_begin(file, FS_OP_DELETE))
		return -1;
	ret = fs_delete(_file->parent);
	return fs_file_random_fail_end(file, ret, FS_OP_DELETE);
}

static struct fs_iter *fs_randomfail_iter_alloc(void)
{
	struct randomfail_fs_iter *iter = i_new(struct randomfail_fs_iter, 1);
	return &iter->iter;
}

static void
fs_randomfail_iter_init(struct fs_iter *_iter, const char *path,
			enum fs_iter_flags flags)
{
	struct randomfail_fs_iter *iter = RANDOMFAIL_ITER(_iter);
	uoff_t pos;

	iter->super = fs_iter_init_parent(_iter, path, flags);
	if (fs_random_fail_range(_iter->fs, _iter->event, FS_OP_ITER, &pos))
		iter->fail_pos = pos + 1;
}

static const char *fs_randomfail_iter_next(struct fs_iter *_iter)
{
	struct randomfail_fs_iter *iter = RANDOMFAIL_ITER(_iter);
	const char *fname;

	if (iter->fail_pos > 0) {
		if (iter->fail_pos == 1)
			return NULL;
		iter->fail_pos--;
	}

	iter->super->async_callback = _iter->async_callback;
	iter->super->async_context = _iter->async_context;

	fname = fs_iter_next(iter->super);
	_iter->async_have_more = iter->super->async_have_more;
	return fname;
}

static int fs_randomfail_iter_deinit(struct fs_iter *_iter)
{
	struct randomfail_fs_iter *iter = RANDOMFAIL_ITER(_iter);
	const char *error;
	int ret;

	if ((ret = fs_iter_deinit(&iter->super, &error)) < 0)
		fs_set_error_errno(_iter->event, "%s", error);
	if (iter->fail_pos == 1) {
		fs_set_error(_iter->event, EIO, RANDOMFAIL_ERROR);
		ret = -1;
	}
	return ret;
}

const struct fs fs_class_randomfail = {
	.name = "randomfail",
	.v = {
		fs_randomfail_alloc,
		fs_randomfail_init,
		NULL,
		fs_randomfail_free,
		fs_randomfail_get_properties,
		fs_randomfail_file_alloc,
		fs_randomfail_file_init,
		fs_randomfail_file_deinit,
		fs_wrapper_file_close,
		fs_wrapper_file_get_path,
		fs_wrapper_set_async_callback,
		fs_wrapper_wait_async,
		fs_wrapper_set_metadata,
		fs_randomfail_get_metadata,
		fs_randomfail_prefetch,
		fs_randomfail_read,
		fs_randomfail_read_stream,
		fs_randomfail_write,
		fs_randomfail_write_stream,
		fs_randomfail_write_stream_finish,
		fs_randomfail_lock,
		fs_randomfail_unlock,
		fs_randomfail_exists,
		fs_randomfail_stat,
		fs_randomfail_copy,
		fs_randomfail_rename,
		fs_randomfail_delete,
		fs_randomfail_iter_alloc,
		fs_randomfail_iter_init,
		fs_randomfail_iter_next,
		fs_randomfail_iter_deinit,
		NULL,
		fs_randomfail_get_nlinks,
	}
};