summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/dht/src/dht-selfheal.c
blob: 4bde0b816554eab4c02f8d80318f2a369a9fb4ad (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
/*
   Copyright (c) 2008-2009 Z RESEARCH, Inc. <http://www.zresearch.com>
   This file is part of GlusterFS.

   GlusterFS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   GlusterFS is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif


#include "glusterfs.h"
#include "xlator.h"
#include "dht-common.h"


int
dht_selfheal_dir_finish (call_frame_t *frame, xlator_t *this, int ret)
{
	dht_local_t  *local = NULL;


	local = frame->local;
	local->selfheal.dir_cbk (frame, NULL, frame->this, ret,
				 local->op_errno);

	return 0;
}


int
dht_selfheal_dir_xattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
			    int op_ret, int op_errno)
{
	dht_local_t  *local = NULL;
	call_frame_t *prev = NULL;
	xlator_t     *subvol = NULL;
	int           i = 0;
	dht_layout_t *layout = NULL;
	int           err = 0;
	int           this_call_cnt = 0;

	local = frame->local;
	layout = local->selfheal.layout;
	prev = cookie;
	subvol = prev->this;

	if (op_ret == 0)
		err = 0;
	else
		err = op_errno;

	for (i = 0; i < layout->cnt; i++) {
		if (layout->list[i].xlator == subvol) {
			layout->list[i].err = err;
			break;
		}
	}

	this_call_cnt = dht_frame_return (frame);

	if (is_last_call (this_call_cnt)) {
		dht_selfheal_dir_finish (frame, this, 0);
	}

	return 0;
}


int
dht_selfheal_dir_xattr_persubvol (call_frame_t *frame, loc_t *loc,
				  dht_layout_t *layout, int i)
{
	xlator_t          *subvol = NULL;
	dict_t            *xattr = NULL;
	int                ret = 0;
	xlator_t          *this = NULL;
	int32_t           *disk_layout = NULL;


	subvol = layout->list[i].xlator;
	this = frame->this;

	xattr = get_new_dict ();
	if (!xattr) {
		gf_log (this->name, GF_LOG_ERROR,
			"Out of memory");
		goto err;
	}

	ret = dht_disk_layout_extract (this, layout, i, &disk_layout);
	if (ret == -1) {
		gf_log (this->name, GF_LOG_DEBUG,
			"failed to extract disk layout");
		goto err;
	}

	ret = dict_set_bin (xattr, "trusted.glusterfs.dht",
			    disk_layout, 4 * 4);
	if (ret == -1) {
		gf_log (this->name, GF_LOG_DEBUG,
			"failed to set xattr dictionary");
		goto err;
	}
	disk_layout = NULL;

	gf_log (this->name, GF_LOG_TRACE,
		"setting hash range %u - %u (type %d) on subvolume %s for %s",
		layout->list[i].start, layout->list[i].stop,
		layout->type, subvol->name, loc->path);

	dict_ref (xattr);

	STACK_WIND (frame, dht_selfheal_dir_xattr_cbk,
		    subvol, subvol->fops->setxattr,
		    loc, xattr, 0);

	dict_unref (xattr);

	return 0;

err:
	if (xattr)
		dict_destroy (xattr);

	if (disk_layout)
		FREE (disk_layout);

	dht_selfheal_dir_xattr_cbk (frame, subvol, frame->this,
				    -1, ENOMEM);
	return 0;
}


int
dht_selfheal_dir_xattr (call_frame_t *frame, loc_t *loc, dht_layout_t *layout)
{
	dht_local_t *local = NULL;
	int          missing_xattr = 0;
	int          i = 0;
	int          ret = 0;
	xlator_t    *this = NULL;

	local = frame->local;
	this = frame->this;

	for (i = 0; i < layout->cnt; i++) {
		if (layout->list[i].err != -1 || !layout->list[i].stop) {
			/* err != -1 would mean xattr present on the directory
			 * or the directory is itself non existant.
			 * !layout->list[i].stop would mean layout absent
			 */
			continue;
		}
		missing_xattr++;
	}

	gf_log (this->name, GF_LOG_TRACE,
		"%d subvolumes missing xattr for %s",
		missing_xattr, loc->path);

	if (missing_xattr == 0) {
		dht_selfheal_dir_finish (frame, this, 0);
		return 0;
	}

	local->call_cnt = missing_xattr;

	for (i = 0; i < layout->cnt; i++) {
		if (layout->list[i].err != -1 || !layout->list[i].stop)
			continue;

		ret = dht_selfheal_dir_xattr_persubvol (frame, loc, layout, i);

		if (--missing_xattr == 0)
			break;
	}
	return 0;
}


int
dht_selfheal_dir_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
			    int op_ret, int op_errno,
                            inode_t *inode, struct stat *stbuf,
                            struct stat *preparent, struct stat *postparent)
{
	dht_local_t   *local = NULL;
	dht_layout_t  *layout = NULL;
	call_frame_t  *prev = NULL;
	xlator_t      *subvol = NULL;
	int            i = 0;
	int            this_call_cnt = 0;


	local  = frame->local;
	layout = local->selfheal.layout;
	prev   = cookie;
	subvol = prev->this;

        dht_stat_merge (this, &local->stbuf, stbuf, prev->this);
        if (prev->this == local->hashed_subvol)
                local->st_ino = local->stbuf.st_ino;

        dht_stat_merge (this, &local->preparent, preparent, prev->this);
        dht_stat_merge (this, &local->postparent, postparent, prev->this);

	if ((op_ret == 0) || (op_errno == EEXIST)) {
		for (i = 0; i < layout->cnt; i++) {
			if (layout->list[i].xlator == subvol) {
				layout->list[i].err = -1;
				break;
			}
		}
	}

	this_call_cnt = dht_frame_return (frame);

	if (is_last_call (this_call_cnt)) {
		dht_selfheal_dir_xattr (frame, &local->loc, layout);
	}

	return 0;
}


int
dht_selfheal_dir_mkdir (call_frame_t *frame, loc_t *loc,
			dht_layout_t *layout, int force)
{
	int           missing_dirs = 0;
	int           i = 0;
	dht_local_t  *local = NULL;
	xlator_t     *this = NULL;


	local = frame->local;
	this = frame->this;

	for (i = 0; i < layout->cnt; i++) {
		if (layout->list[i].err == ENOENT || force)
			missing_dirs++;
	}

	if (missing_dirs == 0) {
		dht_selfheal_dir_xattr (frame, loc, layout);
		return 0;
	}

	local->call_cnt = missing_dirs;
	for (i = 0; i < layout->cnt; i++) {
		if (layout->list[i].err == ENOENT || force) {
			gf_log (this->name, GF_LOG_TRACE,
				"creating directory %s on subvol %s",
				loc->path, layout->list[i].xlator->name);

			STACK_WIND (frame, dht_selfheal_dir_mkdir_cbk,
				    layout->list[i].xlator,
				    layout->list[i].xlator->fops->mkdir,
				    loc, local->stbuf.st_mode);
		}
	}

	return 0;
}

void
dht_selfheal_layout_new_directory (call_frame_t *frame, loc_t *loc,
				   dht_layout_t *layout)
{
	dht_conf_t  *conf = NULL;
	xlator_t    *this = NULL;
	uint32_t     chunk = 0;
	int          i = 0;
	uint32_t     start = 0;
	int          cnt = 0;
	int          err = 0;

	this = frame->this;
	conf = this->private;

	for (i = 0; i < layout->cnt; i++) {
		err = layout->list[i].err;
		if (err == -1 || err == 0) {
			layout->list[i].err = -1;
			cnt++;
		}
	}

        /* no subvolume has enough space, but can't stop directory creation */
        if (!cnt) {
                for (i = 0; i < layout->cnt; i++) {
                        err = layout->list[i].err;
                        if (err == ENOSPC) {
                                layout->list[i].err = -1;
                                cnt++;
                        }
                }
        }

	chunk = ((unsigned long) 0xffffffff) / cnt;

	start = 0;
	for (i = 0; i < layout->cnt; i++) {
		err = layout->list[i].err;
		if (err == -1) {
			layout->list[i].start = start;
			layout->list[i].stop  = start + chunk - 1;
			
			start = start + chunk;

			gf_log (this->name, GF_LOG_TRACE,
				"gave fix: %u - %u on %s for %s",
				layout->list[i].start, layout->list[i].stop,
				layout->list[i].xlator->name, loc->path);
			if (--cnt == 0) {
				layout->list[i].stop = 0xffffffff;
				break;
			}
		}
	}
}


int
dht_selfheal_dir_getafix (call_frame_t *frame, loc_t *loc,
			  dht_layout_t *layout)
{
	dht_conf_t  *conf = NULL;
	xlator_t    *this = NULL;
	dht_local_t *local = NULL;
	int          missing = -1;
	int          down = -1;
	int          holes = -1;
	int          ret = -1;
	int          i = -1;
	int          overlaps = -1;

	this = frame->this;
	conf = this->private;
	local = frame->local;

	missing = local->selfheal.missing;
	down = local->selfheal.down;
	holes = local->selfheal.hole_cnt;
	overlaps = local->selfheal.overlaps_cnt;

	if ((missing + down) == conf->subvolume_cnt) {
		dht_selfheal_layout_new_directory (frame, loc, layout);
		ret = 0;
	}

	if (holes <= down) {
		/* the down subvol might fill up the holes */
		ret = 0;
	}

	if (holes || missing || overlaps) {
		dht_selfheal_layout_new_directory (frame, loc, layout);
		ret = 0;
	}

	for (i = 0; i < layout->cnt; i++) {
		/* directory not present */
		if (layout->list[i].err == ENOENT) {
			ret = 0;
			break;
		}
	}

	/* TODO: give a fix to these non-virgins */

	return ret;
}

int
dht_selfheal_new_directory (call_frame_t *frame, 
			    dht_selfheal_dir_cbk_t dir_cbk,
			    dht_layout_t *layout)
{
	dht_local_t *local = NULL;

	local = frame->local;

	local->selfheal.dir_cbk = dir_cbk;
	local->selfheal.layout = layout;

	dht_layout_sort_volname (layout);
	dht_selfheal_layout_new_directory (frame, &local->loc, layout);	
	dht_selfheal_dir_xattr (frame, &local->loc, layout);
	return 0;
}


int
dht_selfheal_directory (call_frame_t *frame, dht_selfheal_dir_cbk_t dir_cbk,
			loc_t *loc, dht_layout_t *layout)
{
	dht_local_t *local    = NULL;
	uint32_t     holes    = 0;
	uint32_t     overlaps = 0;
	uint32_t     missing  = 0;
	uint32_t     down     = 0;
	uint32_t     misc     = 0;
	int          ret      = 0;
	xlator_t    *this     = NULL;

	local = frame->local;
	this = frame->this;

	ret = dht_layout_anomalies (this, loc, layout,
				    &local->selfheal.hole_cnt,
				    &local->selfheal.overlaps_cnt,
				    &local->selfheal.missing,
				    &local->selfheal.down,
				    &local->selfheal.misc);

	holes    = local->selfheal.hole_cnt;
	overlaps = local->selfheal.overlaps_cnt;
	missing  = local->selfheal.missing;
	down     = local->selfheal.down;
	misc     = local->selfheal.misc;

	local->selfheal.dir_cbk = dir_cbk;
	local->selfheal.layout = layout;

	if (down) {
		gf_log (this->name, GF_LOG_DEBUG,
			"%d subvolumes down -- not fixing", down);
		ret = 0;
		goto sorry_no_fix;
	}

	if (misc) {
		gf_log (this->name, GF_LOG_DEBUG,
			"%d subvolumes have unrecoverable errors", misc);
		ret = 0;
		goto sorry_no_fix;
	}

	dht_layout_sort_volname (layout);
	ret = dht_selfheal_dir_getafix (frame, loc, layout);

	if (ret == -1) {
		gf_log (this->name, GF_LOG_DEBUG,
			"not able to form layout for the directory");
		goto sorry_no_fix;
	}

	dht_selfheal_dir_mkdir (frame, loc, layout, 0);

	return 0;

sorry_no_fix:
	/* TODO: need to put appropriate local->op_errno */
	dht_selfheal_dir_finish (frame, this, ret);

	return 0;
}


int
dht_selfheal_restore (call_frame_t *frame, dht_selfheal_dir_cbk_t dir_cbk,
		      loc_t *loc, dht_layout_t *layout)
{
	int          ret = 0;
	dht_local_t *local    = NULL;


	local = frame->local;

	local->selfheal.dir_cbk = dir_cbk;
	local->selfheal.layout = layout;

	ret = dht_selfheal_dir_mkdir (frame, loc, layout, 1);

	return 0;
}