forked from nigelbabu/clang-format-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatted-dht-common.c
10391 lines (8676 loc) · 316 KB
/
formatted-dht-common.c
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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.
*/
/* TODO: add NS locking */
#include "dht-common.h"
#include "byte-order.h"
#include "defaults.h"
#include "dht-lock.h"
#include "glusterfs.h"
#include "libxlator.h"
#include "quota-common-utils.h"
#include "upcall-utils.h"
#include "xlator.h"
#include <libgen.h>
#include <signal.h>
#include <sys/time.h>
int run_defrag = 0;
int
dht_link2(xlator_t *this, xlator_t *dst_node, call_frame_t *frame, int ret);
int
dht_removexattr2(xlator_t *this, xlator_t *subvol, call_frame_t *frame,
int ret);
int
dht_setxattr2(xlator_t *this, xlator_t *subvol, call_frame_t *frame, int ret);
int
dht_rmdir_readdirp_do(call_frame_t *readdirp_frame, xlator_t *this);
int
dht_common_xattrop_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
int32_t op_ret, int32_t op_errno, dict_t *dict,
dict_t *xdata);
/* Sets the blocks and size values to fixed values. This is to be called
* only for dirs. The caller is responsible for checking the type
*/
int32_t
dht_set_fixed_dir_stat(struct iatt *stat)
{
if (stat) {
stat->ia_blocks = DHT_DIR_STAT_BLOCKS;
stat->ia_size = DHT_DIR_STAT_SIZE;
return 0;
}
return -1;
}
/* Set both DHT_IATT_IN_XDATA_KEY and DHT_MODE_IN_XDATA_KEY
* Use DHT_MODE_IN_XDATA_KEY if available. Else fall back to
* DHT_IATT_IN_XDATA_KEY
*/
int
dht_request_iatt_in_xdata(xlator_t *this, dict_t *xattr_req)
{
int ret = -1;
ret = dict_set_int8(xattr_req, DHT_MODE_IN_XDATA_KEY, 1);
ret = dict_set_int8(xattr_req, DHT_IATT_IN_XDATA_KEY, 1);
/* At least one call succeeded */
return ret;
}
/* Get both DHT_IATT_IN_XDATA_KEY and DHT_MODE_IN_XDATA_KEY
* Use DHT_MODE_IN_XDATA_KEY if available, else fall back to
* DHT_IATT_IN_XDATA_KEY
* This will return a dummy iatt with only the mode and type set
*/
int
dht_read_iatt_from_xdata(xlator_t *this, dict_t *xdata, struct iatt *stbuf)
{
int ret = -1;
int32_t mode = 0;
ret = dict_get_int32(xdata, DHT_MODE_IN_XDATA_KEY, &mode);
if (ret) {
ret = dict_get_bin(xdata, DHT_IATT_IN_XDATA_KEY, (void **)&stbuf);
} else {
stbuf->ia_prot = ia_prot_from_st_mode(mode);
stbuf->ia_type = ia_type_from_st_mode(mode);
}
return ret;
}
int
dht_rmdir_unlock(call_frame_t *frame, xlator_t *this);
char *xattrs_to_heal[] = {"user.",
POSIX_ACL_ACCESS_XATTR,
POSIX_ACL_DEFAULT_XATTR,
QUOTA_LIMIT_KEY,
QUOTA_LIMIT_OBJECTS_KEY,
GF_SELINUX_XATTR_KEY,
NULL};
/* Return true if key exists in array
*/
static gf_boolean_t
dht_match_xattr(const char *key)
{
return gf_get_index_by_elem(xattrs_to_heal, (char *)key) >= 0;
}
int
dht_aggregate_quota_xattr(dict_t *dst, char *key, data_t *value)
{
int ret = -1;
quota_meta_t *meta_dst = NULL;
quota_meta_t *meta_src = NULL;
int64_t * size = NULL;
int64_t dst_dir_count = 0;
int64_t src_dir_count = 0;
if (value == NULL) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_DATA_NULL,
"data value is NULL");
ret = -1;
goto out;
}
ret = dict_get_bin(dst, key, (void **)&meta_dst);
if (ret < 0) {
meta_dst = GF_CALLOC(1, sizeof(quota_meta_t), gf_common_quota_meta_t);
if (meta_dst == NULL) {
gf_msg("dht", GF_LOG_WARNING, ENOMEM, DHT_MSG_NO_MEMORY,
"Memory allocation failed");
ret = -1;
goto out;
}
ret = dict_set_bin(dst, key, meta_dst, sizeof(quota_meta_t));
if (ret < 0) {
gf_msg("dht", GF_LOG_WARNING, EINVAL, DHT_MSG_DICT_SET_FAILED,
"dht aggregate dict set failed");
GF_FREE(meta_dst);
ret = -1;
goto out;
}
}
if (value->len > sizeof(int64_t)) {
meta_src = data_to_bin(value);
meta_dst->size = hton64(ntoh64(meta_dst->size) +
ntoh64(meta_src->size));
meta_dst->file_count = hton64(ntoh64(meta_dst->file_count) +
ntoh64(meta_src->file_count));
if (value->len > (2 * sizeof(int64_t))) {
dst_dir_count = ntoh64(meta_dst->dir_count);
src_dir_count = ntoh64(meta_src->dir_count);
if (src_dir_count > dst_dir_count)
meta_dst->dir_count = meta_src->dir_count;
} else {
meta_dst->dir_count = 0;
}
} else {
size = data_to_bin(value);
meta_dst->size = hton64(ntoh64(meta_dst->size) + ntoh64(*size));
}
ret = 0;
out:
return ret;
}
int
add_opt(char **optsp, const char *opt)
{
char * newopts = NULL;
unsigned oldsize = 0;
unsigned newsize = 0;
if (*optsp == NULL)
newopts = gf_strdup(opt);
else {
oldsize = strlen(*optsp);
newsize = oldsize + 1 + strlen(opt) + 1;
newopts = GF_REALLOC(*optsp, newsize);
if (newopts)
sprintf(newopts + oldsize, ",%s", opt);
}
if (newopts == NULL) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_NO_MEMORY,
"Error to add choices in buffer in add_opt");
return -1;
}
*optsp = newopts;
return 0;
}
/* Return Choice list from Split brain status */
static char *
getChoices(const char *value)
{
int i = 0;
char *ptr = NULL;
char *tok = NULL;
char *result = NULL;
char *newval = NULL;
ptr = strstr(value, "Choices:");
if (!ptr) {
result = ptr;
goto out;
}
newval = gf_strdup(ptr);
if (!newval) {
result = newval;
goto out;
}
tok = strtok(newval, ":");
if (!tok) {
result = tok;
goto out;
}
while (tok) {
i++;
if (i == 2)
break;
tok = strtok(NULL, ":");
}
result = gf_strdup(tok);
out:
if (newval)
GF_FREE(newval);
return result;
}
/* This function prepare a list of choices for key
(replica.split-brain-status) in case of metadata split brain
only on the basis of key-value passed to this function.
After prepare the list of choices it update the same key in dict
with this value to reflect the same in
replica.split-brain-status attr for file.
*/
int
dht_aggregate_split_brain_xattr(dict_t *dst, char *key, data_t *value)
{
int ret = 0;
char *oldvalue = NULL;
char *old_choice = NULL;
char *new_choice = NULL;
char *full_choice = NULL;
char *status = NULL;
if (value == NULL) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_DATA_NULL,
"GF_AFR_SBRAIN_STATUS value is NULL");
ret = -1;
goto out;
}
ret = dict_get_str(dst, key, &oldvalue);
if (ret)
goto out;
if (oldvalue && (strstr(oldvalue, "not"))) {
gf_msg_debug("dht", 0, "Need to update split-brain status in dict");
ret = -1;
goto out;
}
if (oldvalue && (strstr(oldvalue, "metadata-split-brain:yes")) &&
(strstr(oldvalue, "data-split-brain:no"))) {
if (strstr(value->data, "not")) {
gf_msg_debug("dht", 0, "No need to update split-brain status");
ret = 0;
goto out;
}
if (strstr(value->data, "yes") &&
(strncmp(oldvalue, value->data, strlen(oldvalue)))) {
old_choice = getChoices(oldvalue);
if (!old_choice) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_NO_MEMORY,
"Error to get choices");
ret = -1;
goto out;
}
ret = add_opt(&full_choice, old_choice);
if (ret) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_NO_MEMORY,
"Error to add choices");
ret = -1;
goto out;
}
new_choice = getChoices(value->data);
if (!new_choice) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_NO_MEMORY,
"Error to get choices");
ret = -1;
goto out;
}
ret = add_opt(&full_choice, new_choice);
if (ret) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_NO_MEMORY,
"Error to add choices ");
ret = -1;
goto out;
}
ret = gf_asprintf(&status,
"data-split-brain:%s "
"metadata-split-brain:%s Choices:%s",
"no", "yes", full_choice);
if (-1 == ret) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_NO_MEMORY,
"Error to prepare status ");
goto out;
}
ret = dict_set_dynstr(dst, key, status);
if (ret) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_DICT_SET_FAILED,
"Failed to set full choice");
}
}
}
out:
if (old_choice)
GF_FREE(old_choice);
if (new_choice)
GF_FREE(new_choice);
if (full_choice)
GF_FREE(full_choice);
return ret;
}
int
dht_aggregate(dict_t *this, char *key, data_t *value, void *data)
{
dict_t *dst = NULL;
int32_t ret = -1;
data_t *dict_data = NULL;
dst = data;
/* compare split brain xattr only */
if (strcmp(key, GF_AFR_SBRAIN_STATUS) == 0) {
ret = dht_aggregate_split_brain_xattr(dst, key, value);
if (!ret)
goto out;
} else if (strcmp(key, QUOTA_SIZE_KEY) == 0) {
ret = dht_aggregate_quota_xattr(dst, key, value);
if (ret) {
gf_msg("dht", GF_LOG_WARNING, 0,
DHT_MSG_AGGREGATE_QUOTA_XATTR_FAILED,
"Failed to aggregate quota xattr");
}
goto out;
} else if (fnmatch(GF_XATTR_STIME_PATTERN, key, FNM_NOESCAPE) == 0) {
ret = gf_get_min_stime(THIS, dst, key, value);
goto out;
} else {
/* compare user xattrs only */
if (!strncmp(key, "user.", strlen("user."))) {
ret = dict_lookup(dst, key, &dict_data);
if (!ret && dict_data && value) {
ret = is_data_equal(dict_data, value);
if (!ret)
gf_msg_debug("dht", 0, "xattr mismatch for %s", key);
}
}
}
ret = dict_set(dst, key, value);
if (ret) {
gf_msg("dht", GF_LOG_WARNING, 0, DHT_MSG_DICT_SET_FAILED,
"Failed to set dictionary value: key = %s", key);
}
out:
return ret;
}
void
dht_aggregate_xattr(dict_t *dst, dict_t *src)
{
if ((dst == NULL) || (src == NULL)) {
goto out;
}
dict_foreach(src, dht_aggregate, dst);
out:
return;
}
/* Code to save hashed subvol on inode ctx as a mds subvol
*/
int
dht_inode_ctx_mdsvol_set(inode_t *inode, xlator_t *this, xlator_t *mds_subvol)
{
dht_inode_ctx_t *ctx = NULL;
int ret = -1;
uint64_t ctx_int = 0;
gf_boolean_t ctx_free = _gf_false;
LOCK(&inode->lock);
{
ret = __inode_ctx_get(inode, this, &ctx_int);
if (ctx_int) {
ctx = (dht_inode_ctx_t *)ctx_int;
ctx->mds_subvol = mds_subvol;
} else {
ctx = GF_CALLOC(1, sizeof(*ctx), gf_dht_mt_inode_ctx_t);
if (!ctx)
goto unlock;
ctx->mds_subvol = mds_subvol;
ctx_free = _gf_true;
ctx_int = (long)ctx;
ret = __inode_ctx_set(inode, this, &ctx_int);
}
}
unlock:
UNLOCK(&inode->lock);
if (ret && ctx_free)
GF_FREE(ctx);
return ret;
}
/*Code to get mds subvol from inode ctx */
int
dht_inode_ctx_mdsvol_get(inode_t *inode, xlator_t *this, xlator_t **mdsvol)
{
dht_inode_ctx_t *ctx = NULL;
int ret = -1;
if (!mdsvol)
return ret;
if (__is_root_gfid(inode->gfid)) {
(*mdsvol) = FIRST_CHILD(this);
return 0;
}
ret = dht_inode_ctx_get(inode, this, &ctx);
if (!ret && ctx) {
if (ctx->mds_subvol) {
*mdsvol = ctx->mds_subvol;
ret = 0;
} else {
ret = -1;
}
}
return ret;
}
/* TODO:
- use volumename in xattr instead of "dht"
- use NS locks
- handle all cases in self heal layout reconstruction
- complete linkfile selfheal
*/
int
dht_lookup_selfheal_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
int op_ret, int op_errno, dict_t *xdata)
{
dht_local_t * local = NULL;
dht_layout_t *layout = NULL;
dht_conf_t * conf = NULL;
int ret = -1;
GF_VALIDATE_OR_GOTO("dht", frame, out);
GF_VALIDATE_OR_GOTO("dht", this, out);
GF_VALIDATE_OR_GOTO("dht", frame->local, out);
local = frame->local;
conf = this->private;
ret = op_ret;
FRAME_SU_UNDO(frame, dht_local_t);
if (ret == 0) {
layout = local->selfheal.layout;
ret = dht_layout_set(this, local->inode, layout);
}
dht_inode_ctx_time_update(local->inode, this, &local->stbuf, 1);
if (local->loc.parent) {
dht_inode_ctx_time_update(local->loc.parent, this, &local->postparent,
1);
}
DHT_STRIP_PHASE1_FLAGS(&local->stbuf);
dht_set_fixed_dir_stat(&local->postparent);
/* Delete mds xattr at the time of STACK UNWIND */
GF_REMOVE_INTERNAL_XATTR(conf->mds_xattr_key, local->xattr);
DHT_STACK_UNWIND(lookup, frame, ret, local->op_errno, local->inode,
&local->stbuf, local->xattr, &local->postparent);
out:
return ret;
}
int
dht_discover_complete(xlator_t *this, call_frame_t *discover_frame)
{
dht_local_t * local = NULL;
dht_local_t * heal_local = NULL;
call_frame_t *main_frame = NULL;
call_frame_t *heal_frame = NULL;
int op_errno = 0;
int ret = -1;
dht_layout_t *layout = NULL;
dht_conf_t * conf = NULL;
uint32_t vol_commit_hash = 0;
xlator_t * source = NULL;
int heal_path = 0;
int error_while_marking_mds = 0;
int i = 0;
loc_t loc = {0};
int8_t is_read_only = 0, layout_anomalies = 0;
char gfid_local[GF_UUID_BUF_SIZE] = {0};
local = discover_frame->local;
layout = local->layout;
conf = this->private;
gf_uuid_unparse(local->gfid, gfid_local);
LOCK(&discover_frame->lock);
{
main_frame = local->main_frame;
local->main_frame = NULL;
}
UNLOCK(&discover_frame->lock);
if (!main_frame)
return 0;
/* Code to update all extended attributed from
subvol to local->xattr on that internal xattr has found
*/
if (conf->subvolume_cnt == 1)
local->need_xattr_heal = 0;
if (local->need_xattr_heal && (local->mds_xattr)) {
dht_dir_set_heal_xattr(this, local, local->xattr, local->mds_xattr,
NULL, NULL);
dict_unref(local->mds_xattr);
local->mds_xattr = NULL;
}
ret = dict_get_int8(local->xattr_req, QUOTA_READ_ONLY_KEY, &is_read_only);
if (ret < 0)
gf_msg_debug(this->name, 0, "key = %s not present in dict",
QUOTA_READ_ONLY_KEY);
if (local->file_count && local->dir_count) {
gf_msg(this->name, GF_LOG_ERROR, 0, DHT_MSG_FILE_TYPE_MISMATCH,
"path %s exists as a file on one subvolume "
"and directory on another. "
"Please fix it manually",
local->loc.path);
op_errno = EIO;
goto out;
}
if (local->cached_subvol) {
ret = dht_layout_preset(this, local->cached_subvol, local->inode);
if (ret < 0) {
gf_msg(this->name, GF_LOG_WARNING, 0, DHT_MSG_LAYOUT_SET_FAILED,
"failed to set layout for subvolume %s",
local->cached_subvol ? local->cached_subvol->name : "<nil>");
op_errno = EINVAL;
goto out;
}
} else {
ret = dht_layout_normalize(this, &local->loc, layout);
if ((ret < 0) || ((ret > 0) && (local->op_ret != 0))) {
/* either the layout is incorrect or the directory is
* not found even in one subvolume.
*/
gf_msg_debug(this->name, 0,
"normalizing failed on %s "
"(overlaps/holes present: %s, "
"ENOENT errors: %d)",
local->loc.path, (ret < 0) ? "yes" : "no",
(ret > 0) ? ret : 0);
layout_anomalies = 1;
} else if (local->inode) {
dht_layout_set(this, local->inode, layout);
}
}
if (!conf->vch_forced) {
ret = dict_get_uint32(local->xattr, conf->commithash_xattr_name,
&vol_commit_hash);
if (ret == 0) {
conf->vol_commit_hash = vol_commit_hash;
}
}
if (IA_ISDIR(local->stbuf.ia_type) && !is_read_only) {
for (i = 0; i < layout->cnt; i++) {
if (!source && !layout->list[i].err)
source = layout->list[i].xlator;
if (layout->list[i].err == ENOENT ||
layout->list[i].err == ESTALE) {
heal_path = 1;
}
if (source && heal_path)
break;
}
}
if (IA_ISDIR(local->stbuf.ia_type)) {
/* Call function to save hashed subvol on inode ctx if
internal mds xattr is not present and all subvols are up
*/
if (!local->op_ret && !__is_root_gfid(local->stbuf.ia_gfid))
(void)dht_common_mark_mdsxattr(discover_frame,
&error_while_marking_mds, 1);
if (local->need_xattr_heal && !heal_path) {
local->need_xattr_heal = 0;
ret = dht_dir_xattr_heal(this, local);
if (ret)
gf_msg(this->name, GF_LOG_ERROR, ret,
DHT_MSG_DIR_XATTR_HEAL_FAILED,
"xattr heal failed for "
"directory gfid is %s ",
gfid_local);
}
}
if (source && (heal_path || layout_anomalies || error_while_marking_mds)) {
gf_uuid_copy(loc.gfid, local->gfid);
if (gf_uuid_is_null(loc.gfid)) {
goto done;
}
if (local->inode)
loc.inode = inode_ref(local->inode);
else
goto done;
heal_frame = create_frame(this, this->ctx->pool);
if (heal_frame) {
heal_local = dht_local_init(heal_frame, &loc, NULL, 0);
if (!heal_local)
goto cleanup;
gf_uuid_copy(heal_local->gfid, local->gfid);
heal_frame->cookie = source;
heal_local->xattr = dict_ref(local->xattr);
heal_local->stbuf = local->stbuf;
heal_local->postparent = local->postparent;
heal_local->inode = inode_ref(loc.inode);
heal_local->main_frame = main_frame;
FRAME_SU_DO(heal_frame, dht_local_t);
ret = synctask_new(this->ctx->env, dht_heal_full_path,
dht_heal_full_path_done, heal_frame, heal_frame);
if (!ret) {
loc_wipe(&loc);
return 0;
}
/*
* Failed to spawn the synctask. Returning
* with out doing heal.
*/
cleanup:
loc_wipe(&loc);
DHT_STACK_DESTROY(heal_frame);
}
}
done:
dht_set_fixed_dir_stat(&local->postparent);
/* Delete mds xattr at the time of STACK UNWIND */
GF_REMOVE_INTERNAL_XATTR(conf->mds_xattr_key, local->xattr);
DHT_STACK_UNWIND(lookup, main_frame, local->op_ret, local->op_errno,
local->inode, &local->stbuf, local->xattr,
&local->postparent);
return 0;
out:
DHT_STACK_UNWIND(lookup, main_frame, -1, op_errno, NULL, NULL, NULL, NULL);
return ret;
}
int
dht_common_mark_mdsxattr_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
int op_ret, int op_errno, dict_t *xdata)
{
dht_local_t * local = NULL;
xlator_t * prev = cookie;
int ret = -1;
dht_conf_t * conf = 0;
dht_layout_t *layout = NULL;
GF_VALIDATE_OR_GOTO(this->name, frame, out);
GF_VALIDATE_OR_GOTO(this->name, frame->local, out);
local = frame->local;
conf = this->private;
layout = local->selfheal.layout;
if (op_ret) {
gf_msg_debug(this->name, op_ret,
"Failed to set %s on the MDS %s for path %s. ",
conf->mds_xattr_key, prev->name, local->loc.path);
} else {
/* Save mds subvol on inode ctx */
ret = dht_inode_ctx_mdsvol_set(local->inode, this, prev);
if (ret) {
gf_msg(this->name, GF_LOG_ERROR, 0, DHT_MSG_SET_INODE_CTX_FAILED,
"Failed to set mds subvol on inode ctx"
" %s for %s ",
prev->name, local->loc.path);
}
}
if (!local->mds_heal_fresh_lookup && layout) {
dht_selfheal_dir_setattr(frame, &local->loc, &local->stbuf, 0xffffffff,
layout);
}
out:
if (local && local->mds_heal_fresh_lookup)
DHT_STACK_DESTROY(frame);
return 0;
}
/* Common function call by revalidate/selfheal code path to populate
internal xattr if it is not present, mark_during_fresh_lookup value
determines either function is call by revalidate_cbk(discover_complete)
or call by selfheal code path while fresh lookup.
Here we do wind a call serially in case of fresh lookup and
for other lookup code path we do wind a call parallel.The reason
to wind a call serially is at the time of fresh lookup directory is not
discovered and at the time of revalidate_lookup directory is
already discovered. So, revalidate codepath can race with setxattr
codepath and can get into spurious heals because of an ongoing setxattr.
This can slow down revalidates, if healing happens in foreground.
However, if healing happens in background, there is no direct performance
penalty.
*/
int
dht_common_mark_mdsxattr(call_frame_t *frame, int *errst,
int mark_during_fresh_lookup)
{
dht_local_t *local = NULL;
xlator_t *this = NULL;
xlator_t *hashed_subvol = NULL;
int ret = 0;
int i = 0;
dict_t * xattrs = NULL;
char gfid_local[GF_UUID_BUF_SIZE] = {
0,
};
int32_t zero[1] = {0};
dht_conf_t * conf = 0;
dht_layout_t *layout = NULL;
dht_local_t * copy_local = NULL;
call_frame_t *xattr_frame = NULL;
gf_boolean_t vol_down = _gf_false;
this = frame->this;
GF_VALIDATE_OR_GOTO("dht", frame, out);
GF_VALIDATE_OR_GOTO("dht", this, out);
GF_VALIDATE_OR_GOTO(this->name, frame->local, out);
GF_VALIDATE_OR_GOTO(this->name, this->private, out);
local = frame->local;
conf = this->private;
layout = local->selfheal.layout;
local->mds_heal_fresh_lookup = mark_during_fresh_lookup;
gf_uuid_unparse(local->gfid, gfid_local);
/* Code to update hashed subvol consider as a mds subvol
and wind a setxattr call on hashed subvol to update
internal xattr
*/
if (!dict_get(local->xattr, conf->mds_xattr_key)) {
/* It means no internal MDS xattr has been set yet
*/
/* Check the status of all subvol are up while call
this function call by lookup code path
*/
if (mark_during_fresh_lookup) {
for (i = 0; i < conf->subvolume_cnt; i++) {
if (!conf->subvolume_status[i]) {
vol_down = _gf_true;
break;
}
}
if (vol_down) {
gf_msg_debug(this->name, 0,
"subvol %s is down. Unable to "
" save mds subvol on inode for "
" path %s gfid is %s ",
conf->subvolumes[i]->name, local->loc.path,
gfid_local);
goto out;
}
}
/* Calculate hashed subvol based on inode and parent node
*/
hashed_subvol = dht_inode_get_hashed_subvol(local->inode, this,
&local->loc);
if (!hashed_subvol) {
gf_msg(this->name, GF_LOG_DEBUG, 0,
DHT_MSG_HASHED_SUBVOL_GET_FAILED,
"Failed to get hashed subvol for path %s"
"gfid is %s ",
local->loc.path, gfid_local);
(*errst) = 1;
ret = -1;
goto out;
}
xattrs = dict_new();
if (!xattrs) {
gf_msg(this->name, GF_LOG_ERROR, ENOMEM, DHT_MSG_NO_MEMORY,
"dict_new failed");
ret = -1;
goto out;
}
/* Add internal MDS xattr on disk for hashed subvol
*/
ret = dht_dict_set_array(xattrs, conf->mds_xattr_key, zero, 1);
if (ret) {
gf_msg(this->name, GF_LOG_WARNING, ENOMEM, DHT_MSG_DICT_SET_FAILED,
"Failed to set dictionary"
" value:key = %s for "
"path %s",
conf->mds_xattr_key, local->loc.path);
ret = -1;
goto out;
}
/* Create a new frame to wind a call only while
this function call by revalidate_cbk code path
To wind a call parallel need to create a new frame
*/
if (mark_during_fresh_lookup) {
xattr_frame = create_frame(this, this->ctx->pool);
if (!xattr_frame) {
ret = -1;
goto out;
}
copy_local = dht_local_init(xattr_frame, &(local->loc), NULL, 0);
if (!copy_local) {
ret = -1;
DHT_STACK_DESTROY(xattr_frame);
goto out;
}
copy_local->stbuf = local->stbuf;
copy_local->mds_heal_fresh_lookup = mark_during_fresh_lookup;
if (!copy_local->inode)
copy_local->inode = inode_ref(local->inode);
gf_uuid_copy(copy_local->loc.gfid, local->gfid);
FRAME_SU_DO(xattr_frame, dht_local_t);
STACK_WIND_COOKIE(xattr_frame, dht_common_mark_mdsxattr_cbk,
hashed_subvol, hashed_subvol,
hashed_subvol->fops->setxattr, &local->loc,
xattrs, 0, NULL);
} else {
STACK_WIND_COOKIE(frame, dht_common_mark_mdsxattr_cbk,
(void *)hashed_subvol, hashed_subvol,
hashed_subvol->fops->setxattr, &local->loc,
xattrs, 0, NULL);
}
} else {
gf_msg_debug(this->name, 0,
"internal xattr %s is present on subvol"
"on path %s gfid is %s ",
conf->mds_xattr_key, local->loc.path, gfid_local);
if (!mark_during_fresh_lookup)
dht_selfheal_dir_setattr(frame, &local->loc, &local->stbuf,
0xffffffff, layout);
}
out:
if (xattrs)
dict_unref(xattrs);
return ret;
}
int
dht_discover_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
int op_errno, inode_t *inode, struct iatt *stbuf,
dict_t *xattr, struct iatt *postparent)
{
dht_local_t * local = NULL;
int this_call_cnt = 0;
xlator_t * prev = NULL;
dht_layout_t *layout = NULL;
int ret = -1;
int is_dir = 0;
int32_t check_mds = 0;
int is_linkfile = 0;
int attempt_unwind = 0;
dht_conf_t * conf = 0;
char gfid_local[GF_UUID_BUF_SIZE] = {0};
char gfid_node[GF_UUID_BUF_SIZE] = {0};
int32_t mds_xattr_val[1] = {0};
int errst = 0;
GF_VALIDATE_OR_GOTO("dht", frame, out);
GF_VALIDATE_OR_GOTO("dht", this, out);
GF_VALIDATE_OR_GOTO("dht", frame->local, out);
GF_VALIDATE_OR_GOTO("dht", this->private, out);
GF_VALIDATE_OR_GOTO("dht", cookie, out);
local = frame->local;
prev = cookie;
conf = this->private;
layout = local->layout;
/* Check if the gfid is different for file from other node */
if (!op_ret && gf_uuid_compare(local->gfid, stbuf->ia_gfid)) {
gf_uuid_unparse(stbuf->ia_gfid, gfid_node);
gf_uuid_unparse(local->gfid, gfid_local);
gf_msg(this->name, GF_LOG_WARNING, 0, DHT_MSG_GFID_MISMATCH,
"%s: gfid different on %s, gfid local = %s"
"gfid other = %s",
local->loc.path, prev->name, gfid_local, gfid_node);
}
LOCK(&frame->lock);
{
/* TODO: assert equal mode on stbuf->st_mode and
local->stbuf->st_mode
else mkdir/chmod/chown and fix
*/
ret = dht_layout_merge(this, layout, prev, op_ret, op_errno, xattr);
if (ret)
gf_msg(this->name, GF_LOG_WARNING, 0, DHT_MSG_LAYOUT_MERGE_FAILED,
"%s: failed to merge layouts for subvol %s", local->loc.path,
prev->name);
if (op_ret == -1) {
local->op_errno = op_errno;
gf_msg_debug(this->name, op_errno,
"lookup of %s on %s returned error", local->loc.path,
prev->name);
goto unlock;
}
is_linkfile = check_is_linkfile(inode, stbuf, xattr,
conf->link_xattr_name);
is_dir = check_is_dir(inode, stbuf, xattr);
if (is_dir) {
local->dir_count++;
} else {
local->file_count++;
if (!is_linkfile && !local->cached_subvol) {
/* real file */
/* Ok, we somehow managed to find a file on
* more than one subvol. ignore this or we
* will end up overwriting information while a
* a thread is potentially unwinding from
* dht_discover_complete
*/
local->cached_subvol = prev;
attempt_unwind = 1;
} else {
goto unlock;
}
}
local->op_ret = 0;