Debian Codegraph @

xlators/cluster/afr/src/afr-self-heal-data.c

raw
1 /*
2 Copyright (c) 2013 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10
11 #include "afr.h"
12 #include "afr-self-heal.h"
13 #include "protocol-common.h"
14 #include "afr-messages.h"
15 #include <glusterfs/events.h>
16 #include <openssl/md5.h>
17
18 #define HAS_HOLES(i) ((i->ia_blocks * 512) < (i->ia_size))
19 static int
20 __checksum_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
21 int op_errno, uint32_t weak, uint8_t *strong, dict_t *xdata)
22 {
23 afr_local_t *local = NULL;
24 struct afr_reply *replies = NULL;
25 int i = (long)cookie;
26
27 local = frame->local;
28 replies = local->replies;
29
30 replies[i].valid = 1;
31 replies[i].op_ret = op_ret;
32 replies[i].op_errno = op_errno;
33 if (xdata) {
34 replies[i].buf_has_zeroes = dict_get_str_boolean(
35 xdata, "buf-has-zeroes", _gf_false);
36 replies[i].fips_mode_rchecksum = dict_get_str_boolean(
37 xdata, "fips-mode-rchecksum", _gf_false);
38 }
39 if (strong) {
40 if (replies[i].fips_mode_rchecksum) {
41 memcpy(local->replies[i].checksum, strong, SHA256_DIGEST_LENGTH);
42 } else {
43 memcpy(local->replies[i].checksum, strong, MD5_DIGEST_LENGTH);
44 }
45 }
46
47 syncbarrier_wake(&local->barrier);
48 return 0;
49 }
50
51 static gf_boolean_t
52 __afr_can_skip_data_block_heal(call_frame_t *frame, xlator_t *this, fd_t *fd,
53 int source, unsigned char *healed_sinks,
54 off_t offset, size_t size, struct iatt *poststat)
55 {
56 afr_private_t *priv = NULL;
57 afr_local_t *local = NULL;
58 unsigned char *wind_subvols = NULL;
59 gf_boolean_t checksum_match = _gf_true;
60 struct afr_reply *replies = NULL;
61 dict_t *xdata = NULL;
62 int i = 0;
63
64 priv = this->private;
65 local = frame->local;
66 replies = local->replies;
67
68 xdata = dict_new();
69 if (!xdata)
70 goto out;
71 if (dict_set_int32_sizen(xdata, "check-zero-filled", 1)) {
72 dict_unref(xdata);
73 goto out;
74 }
75
76 wind_subvols = alloca0(priv->child_count);
77 for (i = 0; i < priv->child_count; i++) {
78 if (i == source || healed_sinks[i])
79 wind_subvols[i] = 1;
80 }
81
82 AFR_ONLIST(wind_subvols, frame, __checksum_cbk, rchecksum, fd, offset, size,
83 xdata);
84 if (xdata)
85 dict_unref(xdata);
86
87 if (!replies[source].valid || replies[source].op_ret != 0)
88 return _gf_false;
89
90 for (i = 0; i < priv->child_count; i++) {
91 if (i == source)
92 continue;
93 if (replies[i].valid) {
94 if (memcmp(replies[source].checksum, replies[i].checksum,
95 replies[source].fips_mode_rchecksum
96 ? SHA256_DIGEST_LENGTH
97 : MD5_DIGEST_LENGTH)) {
98 checksum_match = _gf_false;
99 break;
100 }
101 }
102 }
103
104 if (checksum_match) {
105 if (HAS_HOLES(poststat))
106 return _gf_true;
107
108 /* For non-sparse files, we might be better off writing the
109 * zeroes to sinks to avoid mismatch of disk-usage in bricks. */
110 if (local->replies[source].buf_has_zeroes)
111 return _gf_false;
112 else
113 return _gf_true;
114 }
115 out:
116 return _gf_false;
117 }
118
119 static gf_boolean_t
120 __afr_is_sink_zero_filled(xlator_t *this, fd_t *fd, size_t size, off_t offset,
121 int sink)
122 {
123 afr_private_t *priv = NULL;
124 struct iobref *iobref = NULL;
125 struct iovec *iovec = NULL;
126 int count = 0;
127 int ret = 0;
128 gf_boolean_t zero_filled = _gf_false;
129
130 priv = this->private;
131 ret = syncop_readv(priv->children[sink], fd, size, offset, 0, &iovec,
132 &count, &iobref, NULL, NULL, NULL);
133 if (ret < 0)
134 goto out;
135 ret = iov_0filled(iovec, count);
136 if (!ret)
137 zero_filled = _gf_true;
138 out:
139 if (iovec)
140 GF_FREE(iovec);
141 if (iobref)
142 iobref_unref(iobref);
143 return zero_filled;
144 }
145
146 static int
147 __afr_selfheal_data_read_write(call_frame_t *frame, xlator_t *this, fd_t *fd,
148 int source, unsigned char *healed_sinks,
149 off_t offset, size_t size,
150 struct afr_reply *replies, int type)
151 {
152 struct iovec *iovec = NULL;
153 int count = 0;
154 struct iobref *iobref = NULL;
155 int ret = 0;
156 int i = 0;
157 afr_private_t *priv = NULL;
158
159 priv = this->private;
160
161 ret = syncop_readv(priv->children[source], fd, size, offset, 0, &iovec,
162 &count, &iobref, NULL, NULL, NULL);
163 if (ret <= 0)
164 return ret;
165
166 for (i = 0; i < priv->child_count; i++) {
167 if (!healed_sinks[i])
168 continue;
169
170 /*
171 * TODO: Use fiemap() and discard() to heal holes
172 * in the future.
173 *
174 * For now,
175 *
176 * - if the source had any holes at all,
177 * AND
178 * - if we are writing past the original file size
179 * of the sink
180 * AND
181 * - is NOT the last block of the source file. if
182 * the block contains EOF, it has to be written
183 * in order to set the file size even if the
184 * last block is 0-filled.
185 * AND
186 * - if the read buffer is filled with only 0's
187 *
188 * then, skip writing to this source. We don't depend
189 * on the write to happen to update the size as we
190 * have performed an ftruncate() upfront anyways.
191 */
192 #define is_last_block(o, b, s) ((s >= o) && (s <= (o + b)))
193 if (HAS_HOLES((&replies[source].poststat)) &&
194 offset >= replies[i].poststat.ia_size &&
195 !is_last_block(offset, size, replies[source].poststat.ia_size) &&
196 (iov_0filled(iovec, count) == 0))
197 continue;
198
199 /* Avoid filling up sparse regions of the sink with 0-filled
200 * writes.*/
201 if (type == AFR_SELFHEAL_DATA_FULL &&
202 HAS_HOLES((&replies[source].poststat)) &&
203 ((offset + size) <= replies[i].poststat.ia_size) &&
204 (iov_0filled(iovec, count) == 0) &&
205 __afr_is_sink_zero_filled(this, fd, size, offset, i)) {
206 continue;
207 }
208
209 ret = syncop_writev(priv->children[i], fd, iovec, count, offset, iobref,
210 0, NULL, NULL, NULL, NULL);
211 if (ret != iov_length(iovec, count)) {
212 /* write() failed on this sink. unset the corresponding
213 member in sinks[] (which is healed_sinks[] in the
214 caller) so that this server does NOT get considered
215 as successfully healed.
216 */
217 healed_sinks[i] = 0;
218 }
219 }
220 if (iovec)
221 GF_FREE(iovec);
222 if (iobref)
223 iobref_unref(iobref);
224
225 return ret;
226 }
227
228 static gf_boolean_t
229 afr_source_sinks_locked(xlator_t *this, unsigned char *locked_on, int source,
230 unsigned char *healed_sinks)
231 {
232 afr_private_t *priv = this->private;
233 int i = 0;
234
235 if (!locked_on[source])
236 return _gf_false;
237
238 for (i = 0; i < priv->child_count; i++) {
239 if (healed_sinks[i] && locked_on[i])
240 return _gf_true;
241 }
242
243 return _gf_false;
244 }
245
246 static int
247 afr_selfheal_data_block(call_frame_t *frame, xlator_t *this, fd_t *fd,
248 int source, unsigned char *healed_sinks, off_t offset,
249 size_t size, int type, struct afr_reply *replies)
250 {
251 int ret = -1;
252 afr_private_t *priv = NULL;
253 unsigned char *data_lock = NULL;
254
255 priv = this->private;
256 data_lock = alloca0(priv->child_count);
257
258 gf_msg_debug(this->name, 0, "gfid:%s, offset=%jd, size=%zu",
259 uuid_utoa(fd->inode->gfid), offset, size);
260
261 ret = afr_selfheal_inodelk(frame, this, fd->inode, this->name, offset, size,
262 data_lock);
263 {
264 if (!afr_source_sinks_locked(this, data_lock, source, healed_sinks)) {
265 ret = -ENOTCONN;
266 goto unlock;
267 }
268
269 if (type == AFR_SELFHEAL_DATA_DIFF &&
270 __afr_can_skip_data_block_heal(frame, this, fd, source,
271 healed_sinks, offset, size,
272 &replies[source].poststat)) {
273 ret = 0;
274 goto unlock;
275 }
276
277 ret = __afr_selfheal_data_read_write(
278 frame, this, fd, source, healed_sinks, offset, size, replies, type);
279 }
280 unlock:
281 afr_selfheal_uninodelk(frame, this, fd->inode, this->name, offset, size,
282 data_lock);
283 return ret;
284 }
285
286 static int
287 afr_selfheal_data_fsync(call_frame_t *frame, xlator_t *this, fd_t *fd,
288 unsigned char *healed_sinks)
289 {
290 afr_local_t *local = NULL;
291 afr_private_t *priv = NULL;
292 int i = 0;
293
294 local = frame->local;
295 priv = this->private;
296
297 if (!priv->ensure_durability)
298 return 0;
299
300 AFR_ONLIST(healed_sinks, frame, afr_sh_generic_fop_cbk, fsync, fd, 0, NULL);
301
302 for (i = 0; i < priv->child_count; i++)
303 if (healed_sinks[i] && local->replies[i].op_ret != 0)
304 /* fsync() failed. Do NOT consider this server
305 as successfully healed. Mark it so.
306 */
307 healed_sinks[i] = 0;
308 return 0;
309 }
310
311 static int
312 afr_data_self_heal_type_get(afr_private_t *priv, unsigned char *healed_sinks,
313 int source, struct afr_reply *replies)
314 {
315 int type = AFR_SELFHEAL_DATA_FULL;
316 int i = 0;
317
318 if (priv->data_self_heal_algorithm == AFR_SELFHEAL_DATA_DYNAMIC) {
319 type = AFR_SELFHEAL_DATA_FULL;
320 for (i = 0; i < priv->child_count; i++) {
321 if (!healed_sinks[i] && i != source)
322 continue;
323 if (replies[i].poststat.ia_size) {
324 type = AFR_SELFHEAL_DATA_DIFF;
325 break;
326 }
327 }
328 } else {
329 type = priv->data_self_heal_algorithm;
330 }
331 return type;
332 }
333
334 static int
335 afr_selfheal_data_do(call_frame_t *frame, xlator_t *this, fd_t *fd, int source,
336 unsigned char *healed_sinks, struct afr_reply *replies)
337 {
338 afr_private_t *priv = NULL;
339 off_t off = 0;
340 size_t block = 0;
341 int type = AFR_SELFHEAL_DATA_FULL;
342 int ret = -1;
343 call_frame_t *iter_frame = NULL;
344 unsigned char arbiter_sink_status = 0;
345
346 gf_msg(this->name, GF_LOG_INFO, 0, AFR_MSG_SELF_HEAL_INFO,
347 "performing data selfheal on %s", uuid_utoa(fd->inode->gfid));
348
349 priv = this->private;
350 if (priv->arbiter_count) {
351 arbiter_sink_status = healed_sinks[ARBITER_BRICK_INDEX];
352 healed_sinks[ARBITER_BRICK_INDEX] = 0;
353 }
354
355 block = 128 * 1024 * priv->data_self_heal_window_size;
356 if (HAS_HOLES((&replies[source].poststat))) {
357 /*Reduce the possibility of data-block allocations in case of files
358 * with holes. Correct way to fix it would be to use seek fop while
359 * healing data*/
360 block = 128 * 1024;
361 }
362
363 type = afr_data_self_heal_type_get(priv, healed_sinks, source, replies);
364
365 iter_frame = afr_copy_frame(frame);
366 if (!iter_frame) {
367 ret = -ENOMEM;
368 goto out;
369 }
370
371 for (off = 0; off < replies[source].poststat.ia_size; off += block) {
372 if (AFR_COUNT(healed_sinks, priv->child_count) == 0) {
373 ret = -ENOTCONN;
374 goto out;
375 }
376
377 ret = afr_selfheal_data_block(iter_frame, this, fd, source,
378 healed_sinks, off, block, type, replies);
379 if (ret < 0)
380 goto out;
381
382 AFR_STACK_RESET(iter_frame);
383 if (iter_frame->local == NULL) {
384 ret = -ENOTCONN;
385 goto out;
386 }
387 }
388
389 ret = afr_selfheal_data_fsync(frame, this, fd, healed_sinks);
390
391 out:
392 if (arbiter_sink_status)
393 healed_sinks[ARBITER_BRICK_INDEX] = arbiter_sink_status;
394
395 if (iter_frame)
396 AFR_STACK_DESTROY(iter_frame);
397 return ret;
398 }
399
400 static int
401 __afr_selfheal_truncate_sinks(call_frame_t *frame, xlator_t *this, fd_t *fd,
402 unsigned char *healed_sinks, uint64_t size)
403 {
404 afr_local_t *local = NULL;
405 afr_private_t *priv = NULL;
406 int i = 0;
407
408 local = frame->local;
409 priv = this->private;
410
411 /* This will send truncate on the arbiter brick as well if it is marked as
412 * sink. If changelog is enabled on the volume it captures truncate as a
413 * data transactions on the arbiter brick. This will help geo-rep to
414 * properly sync the data from primary to secondary if arbiter is the ACTIVE
415 * brick during syncing and which had got some entries healed for data as
416 * part of self heal.
417 */
418 AFR_ONLIST(healed_sinks, frame, afr_sh_generic_fop_cbk, ftruncate, fd, size,
419 NULL);
420
421 for (i = 0; i < priv->child_count; i++)
422 if (healed_sinks[i] && local->replies[i].op_ret == -1)
423 /* truncate() failed. Do NOT consider this server
424 as successfully healed. Mark it so.
425 */
426 healed_sinks[i] = 0;
427
428 return 0;
429 }
430
431 static gf_boolean_t
432 afr_has_source_witnesses(xlator_t *this, unsigned char *sources,
433 uint64_t *witness)
434 {
435 int i = 0;
436 afr_private_t *priv = NULL;
437
438 priv = this->private;
439
440 for (i = 0; i < priv->child_count; i++) {
441 if (sources[i] && witness[i])
442 return _gf_true;
443 }
444 return _gf_false;
445 }
446
447 static gf_boolean_t
448 afr_does_size_mismatch(xlator_t *this, unsigned char *sources,
449 struct afr_reply *replies)
450 {
451 int i = 0;
452 afr_private_t *priv = NULL;
453 struct iatt *min = NULL;
454 struct iatt *max = NULL;
455
456 priv = this->private;
457
458 for (i = 0; i < priv->child_count; i++) {
459 if (!replies[i].valid)
460 continue;
461
462 if (replies[i].op_ret < 0)
463 continue;
464
465 if (!sources[i])
466 continue;
467
468 if (AFR_IS_ARBITER_BRICK(priv, i) && (replies[i].poststat.ia_size == 0))
469 continue;
470
471 if (!min)
472 min = &replies[i].poststat;
473
474 if (!max)
475 max = &replies[i].poststat;
476
477 if (min->ia_size > replies[i].poststat.ia_size)
478 min = &replies[i].poststat;
479
480 if (max->ia_size < replies[i].poststat.ia_size)
481 max = &replies[i].poststat;
482 }
483
484 if (min && max) {
485 if (min->ia_size != max->ia_size)
486 return _gf_true;
487 }
488
489 return _gf_false;
490 }
491
492 static void
493 afr_mark_biggest_witness_as_source(xlator_t *this, unsigned char *sources,
494 uint64_t *witness)
495 {
496 int i = 0;
497 afr_private_t *priv = NULL;
498 uint64_t biggest_witness = 0;
499
500 priv = this->private;
501 /* Find source with biggest witness count */
502 for (i = 0; i < priv->child_count; i++) {
503 if (!sources[i])
504 continue;
505 if (biggest_witness < witness[i])
506 biggest_witness = witness[i];
507 }
508
509 /* Mark files with less witness count as not source */
510 for (i = 0; i < priv->child_count; i++) {
511 if (!sources[i])
512 continue;
513 if (witness[i] < biggest_witness)
514 sources[i] = 0;
515 }
516
517 return;
518 }
519
520 /* This is a tie breaker function. Only one source be assigned here */
521 static void
522 afr_mark_newest_file_as_source(xlator_t *this, unsigned char *sources,
523 struct afr_reply *replies)
524 {
525 int i = 0;
526 afr_private_t *priv = NULL;
527 int source = -1;
528 uint64_t max_ctime = 0;
529
530 priv = this->private;
531 /* Find source with latest ctime */
532 for (i = 0; i < priv->child_count; i++) {
533 if (!sources[i])
534 continue;
535
536 if (max_ctime <= replies[i].poststat.ia_ctime) {
537 source = i;
538 max_ctime = replies[i].poststat.ia_ctime;
539 }
540 }
541
542 /* Only mark one of the files as source to break ties */
543 memset(sources, 0, sizeof(*sources) * priv->child_count);
544 sources[source] = 1;
545 }
546
547 static int
548 __afr_selfheal_data_finalize_source(
549 call_frame_t *frame, xlator_t *this, inode_t *inode, unsigned char *sources,
550 unsigned char *sinks, unsigned char *healed_sinks, unsigned char *locked_on,
551 unsigned char *undid_pending, struct afr_reply *replies, uint64_t *witness)
552 {
553 afr_private_t *priv = NULL;
554 int source = -1;
555 int sources_count = 0;
556 priv = this->private;
557
558 sources_count = AFR_COUNT(sources, priv->child_count);
559
560 if ((AFR_CMP(locked_on, healed_sinks, priv->child_count) == 0) ||
561 !sources_count) {
562 /* split brain */
563 source = afr_mark_split_brain_source_sinks(
564 frame, this, inode, sources, sinks, healed_sinks, locked_on,
565 replies, AFR_DATA_TRANSACTION);
566 if (source < 0) {
567 gf_event(EVENT_AFR_SPLIT_BRAIN,
568 "client-pid=%d;"
569 "subvol=%s;type=data;"
570 "file=%s",
571 this->ctx->cmd_args.client_pid, this->name,
572 uuid_utoa(inode->gfid));
573 return -EIO;
574 }
575
576 _afr_fav_child_reset_sink_xattrs(
577 frame, this, inode, source, healed_sinks, undid_pending,
578 AFR_DATA_TRANSACTION, locked_on, replies);
579 goto out;
580 }
581
582 /* No split brain at this point. If we were called from
583 * afr_heal_splitbrain_file(), abort.*/
584 if (afr_dict_contains_heal_op(frame))
585 return -EIO;
586
587 /* If there are no witnesses/size-mismatches on sources we are done*/
588 if (!afr_does_size_mismatch(this, sources, replies) &&
589 !afr_has_source_witnesses(this, sources, witness))
590 goto out;
591
592 afr_mark_largest_file_as_source(this, sources, replies);
593 afr_mark_biggest_witness_as_source(this, sources, witness);
594 afr_mark_newest_file_as_source(this, sources, replies);
595 if (priv->arbiter_count)
596 /* Choose non-arbiter brick as source for empty files. */
597 afr_mark_source_sinks_if_file_empty(this, sources, sinks, healed_sinks,
598 locked_on, replies,
599 AFR_DATA_TRANSACTION);
600
601 out:
602 afr_mark_active_sinks(this, sources, locked_on, healed_sinks);
603 source = afr_choose_source_by_policy(priv, sources, AFR_DATA_TRANSACTION);
604
605 return source;
606 }
607
608 /*
609 * __afr_selfheal_data_prepare:
610 *
611 * This function inspects the on-disk xattrs and determines which subvols
612 * are sources and sinks.
613 *
614 * The return value is the index of the subvolume to be used as the source
615 * for self-healing, or -1 if no healing is necessary/split brain.
616 */
617 int
618 __afr_selfheal_data_prepare(call_frame_t *frame, xlator_t *this, inode_t *inode,
619 unsigned char *locked_on, unsigned char *sources,
620 unsigned char *sinks, unsigned char *healed_sinks,
621 unsigned char *undid_pending,
622 struct afr_reply *replies, unsigned char *pflag)
623 {
624 int ret = -1;
625 int source = -1;
626 afr_private_t *priv = NULL;
627 uint64_t *witness = NULL;
628
629 priv = this->private;
630
631 ret = afr_selfheal_unlocked_discover(frame, inode, inode->gfid, replies);
632
633 if (ret)
634 return ret;
635
636 witness = alloca0(priv->child_count * sizeof(*witness));
637 ret = afr_selfheal_find_direction(frame, this, replies,
638 AFR_DATA_TRANSACTION, locked_on, sources,
639 sinks, witness, pflag);
640 if (ret)
641 return ret;
642
643 /* Initialize the healed_sinks[] array optimistically to
644 the intersection of to-be-healed (i.e sinks[]) and
645 the list of servers which are up (i.e locked_on[]).
646 As we encounter failures in the healing process, we
647 will unmark the respective servers in the healed_sinks[]
648 array.
649 */
650 AFR_INTERSECT(healed_sinks, sinks, locked_on, priv->child_count);
651
652 source = __afr_selfheal_data_finalize_source(
653 frame, this, inode, sources, sinks, healed_sinks, locked_on,
654 undid_pending, replies, witness);
655 if (source < 0)
656 return -EIO;
657
658 return source;
659 }
660
661 static int
662 __afr_selfheal_data(call_frame_t *frame, xlator_t *this, fd_t *fd,
663 unsigned char *locked_on)
664 {
665 afr_private_t *priv = NULL;
666 int ret = -1;
667 unsigned char *sources = NULL;
668 unsigned char *sinks = NULL;
669 unsigned char *data_lock = NULL;
670 unsigned char *healed_sinks = NULL;
671 unsigned char *undid_pending = NULL;
672 struct afr_reply *locked_replies = NULL;
673 int source = -1;
674 gf_boolean_t did_sh = _gf_true;
675 gf_boolean_t is_arbiter_the_only_sink = _gf_false;
676 gf_boolean_t empty_file = _gf_false;
677
678 priv = this->private;
679
680 sources = alloca0(priv->child_count);
681 sinks = alloca0(priv->child_count);
682 healed_sinks = alloca0(priv->child_count);
683 data_lock = alloca0(priv->child_count);
684 undid_pending = alloca0(priv->child_count);
685
686 locked_replies = alloca0(sizeof(*locked_replies) * priv->child_count);
687
688 ret = afr_selfheal_inodelk(frame, this, fd->inode, this->name, 0, 0,
689 data_lock);
690 {
691 if (ret < priv->child_count) {
692 gf_msg_debug(this->name, 0,
693 "%s: Skipping "
694 "self-heal as only %d number "
695 "of subvolumes "
696 "could be locked",
697 uuid_utoa(fd->inode->gfid), ret);
698 ret = -ENOTCONN;
699 goto unlock;
700 }
701
702 ret = __afr_selfheal_data_prepare(frame, this, fd->inode, data_lock,
703 sources, sinks, healed_sinks,
704 undid_pending, locked_replies, NULL);
705 if (ret < 0)
706 goto unlock;
707
708 if (AFR_COUNT(healed_sinks, priv->child_count) == 0) {
709 did_sh = _gf_false;
710 goto unlock;
711 }
712
713 source = ret;
714
715 if (AFR_IS_ARBITER_BRICK(priv, source)) {
716 empty_file = afr_is_file_empty_on_all_children(priv,
717 locked_replies);
718 if (empty_file)
719 goto restore_time;
720
721 did_sh = _gf_false;
722 goto unlock;
723 }
724
725 ret = __afr_selfheal_truncate_sinks(
726 frame, this, fd, healed_sinks,
727 locked_replies[source].poststat.ia_size);
728 if (ret < 0)
729 goto unlock;
730
731 if (priv->arbiter_count &&
732 AFR_COUNT(healed_sinks, priv->child_count) == 1 &&
733 healed_sinks[ARBITER_BRICK_INDEX]) {
734 is_arbiter_the_only_sink = _gf_true;
735 goto restore_time;
736 }
737 ret = 0;
738 }
739 unlock:
740 afr_selfheal_uninodelk(frame, this, fd->inode, this->name, 0, 0, data_lock);
741 if (ret < 0)
742 goto out;
743
744 if (!did_sh)
745 goto out;
746
747 ret = afr_selfheal_data_do(frame, this, fd, source, healed_sinks,
748 locked_replies);
749 if (ret)
750 goto out;
751 restore_time:
752 afr_selfheal_restore_time(frame, this, fd->inode, source, healed_sinks,
753 locked_replies);
754
755 if (!is_arbiter_the_only_sink && !empty_file) {
756 ret = afr_selfheal_inodelk(frame, this, fd->inode, this->name, 0, 0,
757 data_lock);
758 if (ret < priv->child_count) {
759 ret = -ENOTCONN;
760 did_sh = _gf_false;
761 goto skip_undo_pending;
762 }
763 }
764 ret = afr_selfheal_undo_pending(
765 frame, this, fd->inode, sources, sinks, healed_sinks, undid_pending,
766 AFR_DATA_TRANSACTION, locked_replies, data_lock);
767 skip_undo_pending:
768 afr_selfheal_uninodelk(frame, this, fd->inode, this->name, 0, 0, data_lock);
769 out:
770
771 if (did_sh)
772 afr_log_selfheal(fd->inode->gfid, this, ret, "data", source, sources,
773 healed_sinks);
774 else
775 ret = 1;
776
777 if (locked_replies)
778 afr_replies_wipe(locked_replies, priv->child_count);
779
780 return ret;
781 }
782
783 static int
784 afr_selfheal_data_open_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
785 int32_t op_ret, int32_t op_errno, fd_t *fd,
786 dict_t *xdata)
787 {
788 afr_local_t *local = NULL;
789 int i = (long)cookie;
790
791 local = frame->local;
792
793 local->replies[i].valid = 1;
794 local->replies[i].op_ret = op_ret;
795 local->replies[i].op_errno = op_errno;
796
797 syncbarrier_wake(&local->barrier);
798
799 return 0;
800 }
801
802 int
803 afr_selfheal_data_open(xlator_t *this, inode_t *inode, fd_t **fd)
804 {
805 int ret = 0;
806 fd_t *fd_tmp = NULL;
807 loc_t loc = {
808 0,
809 };
810 call_frame_t *frame = NULL;
811 afr_local_t *local = NULL;
812 afr_private_t *priv = NULL;
813 int i = 0;
814
815 priv = this->private;
816
817 fd_tmp = fd_create(inode, 0);
818 if (!fd_tmp)
819 return -ENOMEM;
820
821 loc.inode = inode_ref(inode);
822 gf_uuid_copy(loc.gfid, inode->gfid);
823
824 frame = afr_frame_create(this, &ret);
825 if (!frame) {
826 ret = -ret;
827 fd_unref(fd_tmp);
828 goto out;
829 }
830 local = frame->local;
831
832 AFR_ONLIST(local->child_up, frame, afr_selfheal_data_open_cbk, open, &loc,
833 O_RDWR | O_LARGEFILE, fd_tmp, NULL);
834
835 ret = -ENOTCONN;
836 for (i = 0; i < priv->child_count; i++) {
837 if (!local->replies[i].valid)
838 continue;
839
840 if (local->replies[i].op_ret < 0) {
841 ret = -local->replies[i].op_errno;
842 continue;
843 }
844
845 ret = 0;
846 break;
847 }
848
849 if (ret < 0) {
850 fd_unref(fd_tmp);
851 goto out;
852 } else {
853 fd_bind(fd_tmp);
854 }
855
856 *fd = fd_tmp;
857 out:
858 loc_wipe(&loc);
859 if (frame)
860 AFR_STACK_DESTROY(frame);
861 return ret;
862 }
863
864 int
865 afr_selfheal_data(call_frame_t *frame, xlator_t *this, fd_t *fd)
866 {
867 afr_private_t *priv = NULL;
868 unsigned char *locked_on = NULL;
869 int ret = 0;
870 inode_t *inode = fd->inode;
871
872 priv = this->private;
873
874 locked_on = alloca0(priv->child_count);
875
876 ret = afr_selfheal_tie_breaker_inodelk(frame, this, inode, priv->sh_domain,
877 0, 0, locked_on);
878 {
879 if (ret < priv->child_count) {
880 gf_msg_debug(this->name, 0,
881 "%s: Skipping "
882 "self-heal as only %d number of "
883 "subvolumes could be locked",
884 uuid_utoa(fd->inode->gfid), ret);
885 /* Either less than two subvols available, or another
886 selfheal (from another server) is in progress. Skip
887 for now in any case there isn't anything to do.
888 */
889 ret = -ENOTCONN;
890 goto unlock;
891 }
892
893 ret = __afr_selfheal_data(frame, this, fd, locked_on);
894 }
895 unlock:
896 afr_selfheal_uninodelk(frame, this, inode, priv->sh_domain, 0, 0,
897 locked_on);
898
899 return ret;
900 }