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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
|
/*
Copyright (c) 2012 DataLab, s.l. <http://www.datalab.es>
This file is part of the cluster/ec translator for GlusterFS.
The cluster/ec translator for 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.
The cluster/ec translator for 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 the cluster/ec translator for GlusterFS. If not, see
<http://www.gnu.org/licenses/>.
*/
#include "byte-order.h"
#include "ec-mem-types.h"
#include "ec-data.h"
#include "ec-helpers.h"
#include "ec-combine.h"
#include "ec-common.h"
#include "ec-fops.h"
#include "ec-method.h"
#include "ec.h"
int32_t ec_child_valid(ec_t * ec, ec_fop_data_t * fop, int32_t idx)
{
return (idx < ec->nodes) && (((fop->remaining >> idx) & 1) == 1);
}
int32_t ec_child_next(ec_t * ec, ec_fop_data_t * fop, int32_t idx)
{
while (!ec_child_valid(ec, fop, idx))
{
if (++idx >= ec->nodes)
{
idx = 0;
}
if (idx == fop->first)
{
return -1;
}
}
return idx;
}
uintptr_t ec_inode_good(inode_t * inode, xlator_t * xl)
{
ec_inode_t * ctx;
uintptr_t bad = 0;
ctx = ec_inode_get(inode, xl);
if (ctx != NULL)
{
bad = ctx->bad;
}
return ~bad;
}
uintptr_t ec_fd_good(fd_t * fd, xlator_t * xl)
{
ec_fd_t * ctx;
uintptr_t bad = 0;
ctx = ec_fd_get(fd, xl);
if ((ctx != NULL) && (ctx->loc.inode != NULL))
{
bad = ctx->bad;
}
return ~bad;
}
uintptr_t ec_update_inode(ec_fop_data_t * fop, inode_t * inode, uintptr_t good,
uintptr_t bad)
{
ec_inode_t * ctx = NULL;
if (inode != NULL)
{
LOCK(&inode->lock);
ctx = __ec_inode_get(inode, fop->xl);
if (ctx != NULL)
{
ctx->bad &= ~good;
bad |= ctx->bad;
ctx->bad = bad;
}
UNLOCK(&inode->lock);
}
return bad;
}
uintptr_t ec_update_fd(ec_fop_data_t * fop, fd_t * fd, uintptr_t good,
uintptr_t bad)
{
ec_fd_t * ctx = NULL;
LOCK(&fd->lock);
ctx = __ec_fd_get(fd, fop->xl);
if ((ctx != NULL) && (ctx->loc.inode != NULL))
{
ctx->bad &= ~good;
bad |= ctx->bad;
ctx->bad = bad;
}
UNLOCK(&fd->lock);
return bad;
}
int32_t ec_heal_report(call_frame_t * frame, void * cookie, xlator_t * this,
int32_t op_ret, int32_t op_errno, uintptr_t mask,
uintptr_t good, uintptr_t bad, dict_t * xdata)
{
if (op_ret < 0)
{
gf_log(this->name, GF_LOG_WARNING, "Heal failed (error %d)", op_errno);
}
else
{
gf_log(this->name, GF_LOG_INFO, "Heal succeeded on %d/%d subvolumes",
ec_bits_count(mask & ~ (good | bad)),
ec_bits_count(mask & ~good));
}
return 0;
}
void ec_check_status(ec_fop_data_t * fop)
{
ec_t * ec = fop->xl->private;
if ((ec->xl_up & ~(fop->remaining | fop->good)) == 0)
{
return;
}
gf_log(fop->xl->name, GF_LOG_WARNING, "Operation failed on some "
"subvolumes (up=%lX, mask=%lX, "
"remaining=%lX, good=%lX, bad=%lX)",
ec->xl_up, fop->mask, fop->remaining, fop->good, fop->bad);
if (fop->fd != NULL)
{
ec_fheal(fop->frame, fop->xl, -1, EC_MINIMUM_ONE, ec_heal_report, NULL,
fop->fd, NULL);
}
else
{
ec_heal(fop->frame, fop->xl, -1, EC_MINIMUM_ONE, ec_heal_report, NULL,
&fop->loc[0], NULL);
if (fop->loc[1].inode != NULL)
{
ec_heal(fop->frame, fop->xl, -1, EC_MINIMUM_ONE, ec_heal_report,
NULL, &fop->loc[1], NULL);
}
}
}
void ec_update_bad(ec_fop_data_t * fop, uintptr_t good)
{
uintptr_t bad;
int32_t update = 0;
bad = fop->mask & ~(fop->remaining | good);
if ((fop->bad & bad) != bad)
{
fop->bad |= bad;
update = 1;
}
if ((fop->good & good) != good)
{
fop->good |= good;
update = 1;
}
if (update && (fop->parent == NULL))
{
if ((fop->flags & EC_FLAG_UPDATE_LOC_PARENT) != 0)
{
ec_update_inode(fop, fop->loc[0].parent, good, bad);
}
if ((fop->flags & EC_FLAG_UPDATE_LOC_INODE) != 0)
{
ec_update_inode(fop, fop->loc[0].inode, good, bad);
}
ec_update_inode(fop, fop->loc[1].inode, good, bad);
if ((fop->flags & EC_FLAG_UPDATE_FD_INODE) != 0)
{
ec_update_inode(fop, fop->fd->inode, good, bad);
}
if ((fop->flags & EC_FLAG_UPDATE_FD) != 0)
{
ec_update_fd(fop, fop->fd, good, bad);
}
ec_check_status(fop);
}
}
void __ec_fop_set_error(ec_fop_data_t * fop, int32_t error)
{
if ((error != 0) && (fop->error == 0))
{
fop->error = error;
}
}
void ec_fop_set_error(ec_fop_data_t * fop, int32_t error)
{
LOCK(&fop->lock);
__ec_fop_set_error(fop, error);
UNLOCK(&fop->lock);
}
int32_t ec_check_complete(ec_fop_data_t * fop, ec_resume_f resume)
{
int32_t error = -1;
LOCK(&fop->lock);
GF_ASSERT(fop->resume == NULL);
if (fop->jobs != 0)
{
ec_trace("WAIT", fop, "resume=%p", resume);
fop->resume = resume;
}
else
{
error = fop->error;
fop->error = 0;
}
UNLOCK(&fop->lock);
return error;
}
void ec_wait_winds(ec_fop_data_t * fop)
{
LOCK(&fop->lock);
if (fop->winds > 0)
{
fop->jobs++;
fop->refs++;
fop->flags |= EC_FLAG_WAITING_WINDS;
}
UNLOCK(&fop->lock);
}
void ec_resume(ec_fop_data_t * fop, int32_t error)
{
ec_resume_f resume = NULL;
LOCK(&fop->lock);
__ec_fop_set_error(fop, error);
if (--fop->jobs == 0)
{
resume = fop->resume;
fop->resume = NULL;
if (resume != NULL)
{
ec_trace("RESUME", fop, "error=%d", error);
if (fop->error != 0)
{
error = fop->error;
}
fop->error = 0;
}
}
UNLOCK(&fop->lock);
if (resume != NULL)
{
resume(fop, error);
}
ec_fop_data_release(fop);
}
void ec_resume_parent(ec_fop_data_t * fop, int32_t error)
{
ec_fop_data_t * parent;
parent = fop->parent;
if (parent != NULL)
{
fop->parent = NULL;
ec_resume(parent, error);
}
}
void ec_complete(ec_fop_data_t * fop)
{
ec_cbk_data_t * cbk = NULL;
int32_t resume = 0;
LOCK(&fop->lock);
ec_trace("COMPLETE", fop, "");
if (--fop->winds == 0)
{
if (fop->answer == NULL)
{
if (!list_empty(&fop->cbk_list))
{
cbk = list_entry(fop->cbk_list.next, ec_cbk_data_t, list);
if ((cbk->count >= fop->minimum) &&
((cbk->op_ret >= 0) || (cbk->op_errno != ENOTCONN)))
{
fop->answer = cbk;
ec_update_bad(fop, cbk->mask);
}
}
resume = 1;
}
else if ((fop->flags & EC_FLAG_WAITING_WINDS) != 0)
{
resume = 1;
}
}
UNLOCK(&fop->lock);
if (resume)
{
ec_resume(fop, 0);
}
ec_fop_data_release(fop);
}
int32_t ec_child_select(ec_fop_data_t * fop)
{
ec_t * ec = fop->xl->private;
uintptr_t mask = 0;
int32_t first = 0, num = 0;
fop->mask &= ec->node_mask;
mask = ec->xl_up;
if (fop->loc[0].inode != NULL)
{
mask |= ec_inode_good(fop->loc[0].inode, fop->xl);
}
if (fop->loc[1].inode != NULL)
{
mask |= ec_inode_good(fop->loc[1].inode, fop->xl);
}
if (fop->fd != NULL)
{
if (fop->fd->inode != NULL)
{
mask |= ec_inode_good(fop->fd->inode, fop->xl);
}
mask |= ec_fd_good(fop->fd, fop->xl);
}
if ((fop->mask & ~mask) != 0)
{
gf_log(fop->xl->name, GF_LOG_WARNING, "Executing operation with "
"some subvolumes unavailable "
"(%lX)", fop->mask & ~mask);
fop->mask &= mask;
}
switch (fop->minimum)
{
case EC_MINIMUM_ALL:
fop->minimum = ec_bits_count(fop->mask);
if (fop->minimum >= ec->fragments)
{
break;
}
case EC_MINIMUM_MIN:
fop->minimum = ec->fragments;
break;
case EC_MINIMUM_ONE:
fop->minimum = 1;
}
first = ec->idx;
if (++first >= ec->nodes)
{
first = 0;
}
ec->idx = first;
fop->remaining = fop->mask;
ec_trace("SELECT", fop, "");
num = ec_bits_count(fop->mask);
if ((num < fop->minimum) && (num < ec->fragments))
{
gf_log(ec->xl->name, GF_LOG_ERROR, "Insufficient available childs "
"for this request (have %d, need "
"%d)", num, fop->minimum);
return 0;
}
LOCK(&fop->lock);
fop->jobs++;
fop->refs++;
UNLOCK(&fop->lock);
return 1;
}
int32_t ec_dispatch_next(ec_fop_data_t * fop, int32_t idx)
{
ec_t * ec = fop->xl->private;
LOCK(&fop->lock);
idx = ec_child_next(ec, fop, idx);
if (idx >= 0)
{
fop->remaining ^= 1ULL << idx;
ec_trace("EXECUTE", fop, "idx=%d", idx);
fop->winds++;
fop->refs++;
}
UNLOCK(&fop->lock);
if (idx >= 0)
{
fop->wind(ec, fop, idx);
}
return idx;
}
void ec_dispatch_mask(ec_fop_data_t * fop, uintptr_t mask)
{
ec_t * ec = fop->xl->private;
int32_t count, idx;
count = ec_bits_count(mask);
LOCK(&fop->lock);
ec_trace("EXECUTE", fop, "mask=%lX", mask);
fop->remaining ^= mask;
fop->winds += count;
fop->refs += count;
UNLOCK(&fop->lock);
idx = 0;
while (mask != 0)
{
if ((mask & 1) != 0)
{
fop->wind(ec, fop, idx);
}
idx++;
mask >>= 1;
}
}
void ec_dispatch_start(ec_fop_data_t * fop)
{
fop->answer = NULL;
fop->good = 0;
fop->bad = 0;
INIT_LIST_HEAD(&fop->cbk_list);
if (fop->lock_count > 0)
{
ec_owner_copy(fop->frame, &fop->req_frame->root->lk_owner);
}
}
void ec_dispatch_one(ec_fop_data_t * fop)
{
ec_t * ec = fop->xl->private;
ec_dispatch_start(fop);
if (ec_child_select(fop))
{
fop->expected = 1;
fop->first = ec->idx;
ec_dispatch_next(fop, fop->first);
}
}
int32_t ec_dispatch_one_retry(ec_fop_data_t * fop, int32_t idx, int32_t op_ret,
int32_t op_errno)
{
if ((op_ret < 0) && (op_errno == ENOTCONN))
{
return (ec_dispatch_next(fop, idx) >= 0);
}
return 0;
}
void ec_dispatch_inc(ec_fop_data_t * fop)
{
ec_dispatch_start(fop);
if (ec_child_select(fop))
{
fop->expected = ec_bits_count(fop->remaining);
fop->first = 0;
ec_dispatch_next(fop, 0);
}
}
void ec_dispatch_all(ec_fop_data_t * fop)
{
ec_dispatch_start(fop);
if (ec_child_select(fop))
{
fop->expected = ec_bits_count(fop->remaining);
fop->first = 0;
ec_dispatch_mask(fop, fop->remaining);
}
}
void ec_dispatch_min(ec_fop_data_t * fop)
{
ec_t * ec = fop->xl->private;
uintptr_t mask;
int32_t idx, count;
ec_dispatch_start(fop);
if (ec_child_select(fop))
{
fop->expected = count = ec->fragments;
fop->first = ec->idx;
idx = fop->first - 1;
mask = 0;
while (count-- > 0)
{
idx = ec_child_next(ec, fop, idx + 1);
mask |= 1ULL << idx;
}
ec_dispatch_mask(fop, mask);
}
}
ec_lock_t * ec_lock_allocate(xlator_t * xl, int32_t kind, loc_t * loc)
{
ec_t * ec = xl->private;
ec_lock_t * lock;
if ((loc->inode == NULL) ||
(uuid_is_null(loc->gfid) && uuid_is_null(loc->inode->gfid)))
{
gf_log(xl->name, GF_LOG_ERROR, "Trying to lock based on an invalid "
"inode");
return NULL;
}
lock = mem_get0(ec->lock_pool);
if (lock != NULL)
{
lock->kind = kind;
lock->good_mask = -1ULL;
INIT_LIST_HEAD(&lock->waiting);
if (!ec_loc_from_loc(xl, &lock->loc, loc))
{
mem_put(lock);
lock = NULL;
}
}
return lock;
}
void ec_lock_destroy(ec_lock_t * lock)
{
GF_FREE(lock->basename);
loc_wipe(&lock->loc);
mem_put(lock);
}
int32_t ec_lock_compare(ec_lock_t * lock1, ec_lock_t * lock2)
{
int32_t res;
res = uuid_compare(lock1->loc.gfid, lock2->loc.gfid);
if (res != 0)
{
return res;
}
if (lock1->basename == NULL)
{
if (lock2->basename == NULL)
{
return 0;
}
return 1;
}
if (lock2->basename == NULL)
{
return -1;
}
return strcmp(lock1->basename, lock2->basename);
}
void ec_lock_insert(ec_fop_data_t * fop, ec_lock_t * lock)
{
ec_lock_t * tmp;
if ((fop->lock_count > 0) &&
(ec_lock_compare(fop->locks[0].lock, lock) > 0))
{
tmp = fop->locks[0].lock;
fop->locks[0].lock = lock;
lock = tmp;
}
fop->locks[fop->lock_count].lock = lock;
fop->locks[fop->lock_count].fop = fop;
fop->lock_count++;
lock->refs++;
}
void ec_lock_prepare_entry(ec_fop_data_t * fop, loc_t * loc)
{
ec_lock_t * lock = NULL;
ec_inode_t * ctx = NULL;
char * name = NULL;
loc_t tmp;
int32_t error;
if ((fop->parent != NULL) || (fop->error != 0))
{
return;
}
error = ec_loc_parent(fop->xl, loc, &tmp, &name);
if (error != 0)
{
ec_fop_set_error(fop, error);
return;
}
LOCK(&tmp.inode->lock);
ctx = __ec_inode_get(tmp.inode, fop->xl);
if (ctx == NULL)
{
__ec_fop_set_error(fop, EIO);
goto unlock;
}
list_for_each_entry(lock, &ctx->entry_locks, list)
{
if (strcmp(lock->basename, name) == 0)
{
ec_trace("LOCK_ENTRYLK", fop, "lock=%p, inode=%p, path=%s, "
"name=%s. Lock already acquired",
lock, tmp.inode, tmp.path, name);
goto insert;
}
}
lock = ec_lock_allocate(fop->xl, EC_LOCK_ENTRY, &tmp);
if (lock == NULL)
{
__ec_fop_set_error(fop, EIO);
goto unlock;
}
ec_trace("LOCK_CREATE", fop, "lock=%p", lock);
lock->type = ENTRYLK_WRLCK;
lock->basename = name;
name = NULL;
list_add_tail(&lock->list, &ctx->entry_locks);
insert:
ec_lock_insert(fop, lock);
unlock:
UNLOCK(&tmp.inode->lock);
loc_wipe(&tmp);
GF_FREE(name);
}
void ec_lock_prepare_inode(ec_fop_data_t * fop, loc_t * loc)
{
ec_lock_t * lock;
ec_inode_t * ctx;
if ((fop->parent != NULL) || (fop->error != 0) || (loc->inode == NULL))
{
return;
}
LOCK(&loc->inode->lock);
ctx = __ec_inode_get(loc->inode, fop->xl);
if (ctx == NULL)
{
__ec_fop_set_error(fop, EIO);
goto unlock;
}
if (!list_empty(&ctx->inode_locks))
{
lock = list_entry(ctx->inode_locks.next, ec_lock_t, list);
ec_trace("LOCK_INODELK", fop, "lock=%p, inode=%p. Lock already "
"acquired", lock, loc->inode);
goto insert;
}
lock = ec_lock_allocate(fop->xl, EC_LOCK_INODE, loc);
if (lock == NULL)
{
__ec_fop_set_error(fop, EIO);
goto unlock;
}
ec_trace("LOCK_CREATE", fop, "lock=%p", lock);
lock->flock.l_type = F_WRLCK;
lock->flock.l_whence = SEEK_SET;
list_add_tail(&lock->list, &ctx->inode_locks);
insert:
ec_lock_insert(fop, lock);
unlock:
UNLOCK(&loc->inode->lock);
}
void ec_lock_prepare_fd(ec_fop_data_t * fop, fd_t * fd)
{
loc_t loc;
if ((fop->parent != NULL) || (fop->error != 0))
{
return;
}
if (ec_loc_from_fd(fop->xl, &loc, fd))
{
ec_lock_prepare_inode(fop, &loc);
loc_wipe(&loc);
}
else
{
ec_fop_set_error(fop, EIO);
}
}
int32_t ec_locked(call_frame_t * frame, void * cookie, xlator_t * this,
int32_t op_ret, int32_t op_errno, dict_t * xdata)
{
ec_fop_data_t * fop = cookie;
ec_lock_t * lock = NULL;
if (op_ret >= 0)
{
lock = fop->data;
lock->mask = fop->good;
lock->acquired = 1;
fop->parent->mask &= fop->good;
fop->parent->locked++;
ec_trace("LOCKED", fop->parent, "lock=%p", lock);
ec_lock(fop->parent);
}
else
{
gf_log(this->name, GF_LOG_WARNING, "Failed to complete preop lock");
}
return 0;
}
void ec_lock(ec_fop_data_t * fop)
{
ec_lock_t * lock;
while (fop->locked < fop->lock_count)
{
lock = fop->locks[fop->locked].lock;
LOCK(&lock->loc.inode->lock);
if (lock->owner != NULL)
{
ec_trace("LOCK_WAIT", fop, "lock=%p", lock);
list_add_tail(&fop->locks[fop->locked].wait_list, &lock->waiting);
fop->jobs++;
fop->refs++;
UNLOCK(&lock->loc.inode->lock);
break;
}
lock->owner = fop;
UNLOCK(&lock->loc.inode->lock);
if (!lock->acquired)
{
ec_owner_set(fop->frame, lock);
if (lock->kind == EC_LOCK_ENTRY)
{
ec_trace("LOCK_ACQUIRE", fop, "lock=%p, inode=%p, path=%s, "
"name=%s", lock, lock->loc.inode, lock->loc.path,
lock->basename);
ec_entrylk(fop->frame, fop->xl, -1, EC_MINIMUM_ALL, ec_locked,
lock, fop->xl->name, &lock->loc, lock->basename,
ENTRYLK_LOCK, lock->type, NULL);
}
else
{
ec_trace("LOCK_ACQUIRE", fop, "lock=%p, inode=%p", lock,
lock->loc.inode);
ec_inodelk(fop->frame, fop->xl, -1, EC_MINIMUM_ALL, ec_locked,
lock, fop->xl->name, &lock->loc, F_SETLKW,
&lock->flock, NULL);
}
break;
}
ec_trace("LOCK_REUSE", fop, "lock=%p", lock);
if (lock->have_size)
{
fop->pre_size = fop->post_size = lock->size;
fop->have_size = 1;
}
fop->mask &= lock->good_mask;
fop->locked++;
}
}
int32_t ec_unlocked(call_frame_t * frame, void * cookie, xlator_t * this,
int32_t op_ret, int32_t op_errno, dict_t * xdata)
{
ec_fop_data_t * fop = cookie;
if (op_ret < 0)
{
gf_log(this->name, GF_LOG_WARNING, "entry/inode unlocking failed (%s)",
ec_fop_name(fop->parent->id));
}
else
{
ec_trace("UNLOCKED", fop->parent, "lock=%p", fop->data);
}
return 0;
}
void ec_unlock(ec_fop_data_t * fop)
{
ec_lock_t * lock;
int32_t i, refs;
for (i = 0; i < fop->lock_count; i++)
{
lock = fop->locks[i].lock;
LOCK(&lock->loc.inode->lock);
ec_trace("UNLOCK", fop, "lock=%p", lock);
refs = --lock->refs;
if (refs == 0)
{
list_del_init(&lock->list);
}
UNLOCK(&lock->loc.inode->lock);
if (refs == 0)
{
if (lock->mask != 0)
{
ec_owner_set(fop->frame, lock);
switch (lock->kind)
{
case EC_LOCK_ENTRY:
ec_trace("UNLOCK_ENTRYLK", fop, "lock=%p, inode=%p, "
"path=%s, basename=%s",
lock, lock->loc.inode, lock->loc.path,
lock->basename);
ec_entrylk(fop->frame, fop->xl, lock->mask,
EC_MINIMUM_ALL, ec_unlocked, lock,
fop->xl->name, &lock->loc, lock->basename,
ENTRYLK_UNLOCK, lock->type, NULL);
break;
case EC_LOCK_INODE:
lock->flock.l_type = F_UNLCK;
ec_trace("UNLOCK_INODELK", fop, "lock=%p, inode=%p",
lock, lock->loc.inode);
ec_inodelk(fop->frame, fop->xl, lock->mask,
EC_MINIMUM_ALL, ec_unlocked, lock,
fop->xl->name, &lock->loc, F_SETLK,
&lock->flock, NULL);
break;
default:
gf_log(fop->xl->name, GF_LOG_ERROR, "Invalid lock "
"type");
}
}
ec_trace("LOCK_DESTROY", fop, "lock=%p", lock);
ec_lock_destroy(lock);
}
}
}
int32_t ec_get_size_version_set(call_frame_t * frame, void * cookie,
xlator_t * this, int32_t op_ret,
int32_t op_errno, inode_t * inode,
struct iatt * buf, dict_t * xdata,
struct iatt * postparent)
{
ec_t * ec;
ec_fop_data_t * fop = cookie;
ec_inode_t * ctx;
ec_lock_t * lock;
if (op_ret >= 0)
{
if (buf->ia_type == IA_IFREG)
{
if (ec_dict_del_config(xdata, EC_XATTR_CONFIG, &fop->config) < 0)
{
gf_log(this->name, GF_LOG_ERROR, "Failed to get a valid "
"config");
ec_fop_set_error(fop, EIO);
return 0;
}
ec = this->private;
if ((fop->config.version != EC_CONFIG_VERSION) ||
(fop->config.algorithm != EC_CONFIG_ALGORITHM) ||
(fop->config.gf_word_size != EC_GF_BITS) ||
(fop->config.bricks != ec->nodes) ||
(fop->config.redundancy != ec->redundancy) ||
(fop->config.chunk_size != EC_METHOD_CHUNK_SIZE))
{
uint32_t data_bricks;
// This combination of version/algorithm requires the following
// values. Incorrect values for these fields are a sign of
// corruption:
//
// redundancy > 0
// redundancy * 2 < bricks
// gf_word_size must be a power of 2
// chunk_size (in bits) must be a multiple of gf_word_size *
// (bricks - redundancy)
data_bricks = fop->config.bricks - fop->config.redundancy;
if ((fop->config.redundancy < 1) ||
(fop->config.redundancy * 2 >= fop->config.bricks) ||
!ec_is_power_of_2(fop->config.gf_word_size) ||
((fop->config.chunk_size * 8) % (fop->config.gf_word_size *
data_bricks) != 0))
{
gf_log(this->name, GF_LOG_ERROR, "Invalid or corrupted "
"config (V=%u, A=%u, "
"W=%u, N=%u, R=%u, S=%u)",
fop->config.version, fop->config.algorithm,
fop->config.gf_word_size, fop->config.bricks,
fop->config.redundancy, fop->config.chunk_size);
}
else
{
gf_log(this->name, GF_LOG_ERROR, "Unsupported config "
"(V=%u, A=%u, W=%u, "
"N=%u, R=%u, S=%u)",
fop->config.version, fop->config.algorithm,
fop->config.gf_word_size, fop->config.bricks,
fop->config.redundancy, fop->config.chunk_size);
}
ec_fop_set_error(fop, EIO);
return 0;
}
}
LOCK(&inode->lock);
ctx = __ec_inode_get(inode, this);
if ((ctx != NULL) && !list_empty(&ctx->inode_locks))
{
lock = list_entry(ctx->inode_locks.next, ec_lock_t, list);
lock->have_size = 1;
lock->size = buf->ia_size;
lock->version = fop->answer->version;
}
UNLOCK(&inode->lock);
if (lock != NULL)
{
// Only update parent mask if the lookup has been made with
// inode locked.
fop->parent->mask &= fop->good;
}
fop->parent->pre_size = fop->parent->post_size = buf->ia_size;
fop->parent->have_size = 1;
}
else
{
gf_log(this->name, GF_LOG_WARNING, "Failed to get size and version "
"(error %d)", op_errno);
ec_fop_set_error(fop, op_errno);
}
return 0;
}
void ec_get_size_version(ec_fop_data_t * fop)
{
loc_t loc;
dict_t * xdata;
uid_t uid;
gid_t gid;
int32_t error = ENOMEM;
if (fop->have_size)
{
return;
}
if ((fop->parent != NULL) && fop->parent->have_size)
{
fop->pre_size = fop->parent->pre_size;
fop->post_size = fop->parent->post_size;
fop->have_size = 1;
return;
}
memset(&loc, 0, sizeof(loc));
xdata = dict_new();
if (xdata == NULL)
{
goto out;
}
if ((dict_set_uint64(xdata, EC_XATTR_VERSION, 0) != 0) ||
(dict_set_uint64(xdata, EC_XATTR_SIZE, 0) != 0) ||
(dict_set_uint64(xdata, EC_XATTR_CONFIG, 0) != 0))
{
goto out;
}
uid = fop->frame->root->uid;
gid = fop->frame->root->gid;
fop->frame->root->uid = 0;
fop->frame->root->gid = 0;
error = EIO;
if (fop->fd == NULL)
{
if (!ec_loc_from_loc(fop->xl, &loc, &fop->loc[0]))
{
goto out;
}
if (uuid_is_null(loc.pargfid))
{
if (loc.parent != NULL)
{
inode_unref(loc.parent);
loc.parent = NULL;
}
GF_FREE((char *)loc.path);
loc.path = NULL;
loc.name = NULL;
}
}
else if (!ec_loc_from_fd(fop->xl, &loc, fop->fd))
{
goto out;
}
ec_lookup(fop->frame, fop->xl, fop->mask, EC_MINIMUM_MIN,
ec_get_size_version_set, NULL, &loc, xdata);
fop->frame->root->uid = uid;
fop->frame->root->gid = gid;
error = 0;
out:
loc_wipe(&loc);
if (xdata != NULL)
{
dict_unref(xdata);
}
ec_fop_set_error(fop, error);
}
int32_t ec_update_size_version_done(call_frame_t * frame, void * cookie,
xlator_t * this, int32_t op_ret,
int32_t op_errno, dict_t * xattr,
dict_t * xdata)
{
ec_fop_data_t * fop = cookie;
if (op_ret < 0)
{
gf_log(fop->xl->name, GF_LOG_ERROR, "Failed to update version and "
"size (error %d)", op_errno);
}
else
{
fop->parent->mask &= fop->good;
}
return 0;
}
void ec_update_size_version(ec_fop_data_t * fop, uint64_t version,
uint64_t size)
{
dict_t * dict;
uid_t uid;
gid_t gid;
if (fop->parent != NULL)
{
fop->parent->post_size = fop->post_size;
return;
}
ec_trace("UPDATE", fop, "version=%ld, size=%ld", version, size);
dict = dict_new();
if (dict == NULL)
{
goto out;
}
if (ec_dict_set_number(dict, EC_XATTR_VERSION, version) != 0)
{
goto out;
}
if (size != 0)
{
if (ec_dict_set_number(dict, EC_XATTR_SIZE, size) != 0)
{
goto out;
}
}
uid = fop->frame->root->uid;
gid = fop->frame->root->gid;
fop->frame->root->uid = 0;
fop->frame->root->gid = 0;
if (fop->fd == NULL)
{
ec_xattrop(fop->frame, fop->xl, fop->mask, EC_MINIMUM_MIN,
ec_update_size_version_done, NULL, &fop->loc[0],
GF_XATTROP_ADD_ARRAY64, dict, NULL);
}
else
{
ec_fxattrop(fop->frame, fop->xl, fop->mask, EC_MINIMUM_MIN,
ec_update_size_version_done, NULL, fop->fd,
GF_XATTROP_ADD_ARRAY64, dict, NULL);
}
fop->frame->root->uid = uid;
fop->frame->root->gid = gid;
dict_unref(dict);
return;
out:
if (dict != NULL)
{
dict_unref(dict);
}
ec_fop_set_error(fop, EIO);
gf_log(fop->xl->name, GF_LOG_ERROR, "Unable to update version and size");
}
void ec_flush_size_version(ec_fop_data_t * fop)
{
ec_lock_t * lock;
uint64_t version;
size_t delta;
GF_ASSERT(fop->lock_count == 1);
lock = fop->locks[0].lock;
GF_ASSERT(lock->kind == EC_LOCK_INODE);
LOCK(&lock->loc.inode->lock);
GF_ASSERT(lock->owner == fop);
version = lock->version_delta;
delta = lock->size_delta;
lock->version_delta = 0;
lock->size_delta = 0;
UNLOCK(&lock->loc.inode->lock);
if (version > 0)
{
ec_update_size_version(fop, version, delta);
}
}
void ec_lock_reuse(ec_fop_data_t * fop, int32_t update)
{
ec_fop_data_t * wait_fop;
ec_lock_t * lock;
ec_lock_link_t * link;
size_t delta = 0;
uint64_t version = 0;
int32_t refs = 0;
int32_t i;
for (i = 0; i < fop->lock_count; i++)
{
wait_fop = NULL;
lock = fop->locks[i].lock;
LOCK(&lock->loc.inode->lock);
ec_trace("LOCK_DONE", fop, "lock=%p", lock);
GF_ASSERT(lock->owner == fop);
lock->owner = NULL;
if (lock->kind == EC_LOCK_INODE)
{
if (update && (fop->error == 0))
{
lock->version_delta++;
lock->size_delta += fop->post_size - fop->pre_size;
}
version = lock->version_delta;
delta = lock->size_delta;
refs = lock->refs;
if (refs == 1)
{
lock->version_delta = 0;
lock->size_delta = 0;
}
if (fop->have_size)
{
lock->size = fop->post_size;
lock->have_size = 1;
}
}
lock->good_mask &= fop->mask;
if (!list_empty(&lock->waiting))
{
link = list_entry(lock->waiting.next, ec_lock_link_t, wait_list);
list_del_init(&link->wait_list);
wait_fop = link->fop;
if (lock->kind == EC_LOCK_INODE)
{
wait_fop->pre_size = wait_fop->post_size = fop->post_size;
wait_fop->have_size = fop->have_size;
}
wait_fop->mask &= fop->mask;
}
UNLOCK(&lock->loc.inode->lock);
if (wait_fop != NULL)
{
ec_lock(wait_fop);
ec_resume(wait_fop, 0);
}
}
if ((refs == 1) && (version > 0))
{
ec_update_size_version(fop, version, delta);
}
}
void __ec_manager(ec_fop_data_t * fop, int32_t error)
{
do
{
ec_trace("MANAGER", fop, "error=%d", error);
if (fop->state == EC_STATE_END)
{
ec_fop_data_release(fop);
break;
}
if (error != 0)
{
fop->error = error;
fop->state = -fop->state;
}
fop->state = fop->handler(fop, fop->state);
error = ec_check_complete(fop, __ec_manager);
} while (error >= 0);
}
void ec_manager(ec_fop_data_t * fop, int32_t error)
{
GF_ASSERT(fop->jobs == 0);
GF_ASSERT(fop->winds == 0);
GF_ASSERT(fop->error == 0);
if (fop->state == EC_STATE_START)
{
fop->state = EC_STATE_INIT;
}
__ec_manager(fop, error);
}
|