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
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
|
/*
Copyright (c) 2015 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.
*/
#include <math.h>
#include <ctype.h>
#include <sys/uio.h>
#include "glusterfs.h"
#include "logging.h"
#include "common-utils.h"
#include "bit-rot-scrub.h"
#include <pthread.h>
#include "bit-rot-bitd-messages.h"
#include "bit-rot-scrub-status.h"
struct br_scrubbers {
pthread_t scrubthread;
struct list_head list;
};
struct br_fsscan_entry {
void *data;
loc_t parent;
gf_dirent_t *entry;
struct br_scanfs *fsscan; /* backpointer to subvolume scanner */
struct list_head list;
};
/**
* fetch signature extended attribute from an object's fd.
* NOTE: On success @xattr is not unref'd as @sign points
* to the dictionary value.
*/
static int32_t
bitd_fetch_signature (xlator_t *this, br_child_t *child,
fd_t *fd, dict_t **xattr, br_isignature_out_t **sign)
{
int32_t ret = -1;
ret = syncop_fgetxattr (child->xl, fd, xattr,
GLUSTERFS_GET_OBJECT_SIGNATURE, NULL, NULL);
if (ret < 0) {
br_log_object (this, "fgetxattr", fd->inode->gfid, -ret);
goto out;
}
ret = dict_get_ptr
(*xattr, GLUSTERFS_GET_OBJECT_SIGNATURE, (void **) sign);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_GET_SIGN_FAILED,
"failed to extract signature info [GFID: %s]",
uuid_utoa (fd->inode->gfid));
goto unref_dict;
}
return 0;
unref_dict:
dict_unref (*xattr);
out:
return -1;
}
/**
* POST COMPUTE CHECK
*
* Checks to be performed before verifying calculated signature
* Object is skipped if:
* - has stale signature
* - mismatches versions caches in pre-compute check
*/
int32_t
bitd_scrub_post_compute_check (xlator_t *this,
br_child_t *child,
fd_t *fd, unsigned long version,
br_isignature_out_t **signature,
br_scrub_stats_t *scrub_stat,
gf_boolean_t skip_stat)
{
int32_t ret = 0;
size_t signlen = 0;
dict_t *xattr = NULL;
br_isignature_out_t *signptr = NULL;
ret = bitd_fetch_signature (this, child, fd, &xattr, &signptr);
if (ret < 0) {
if (!skip_stat)
br_inc_unsigned_file_count (scrub_stat);
goto out;
}
/**
* Either the object got dirtied during the time the signature was
* calculated OR the version we saved during pre-compute check does
* not match now, implying that the object got dirtied and signed in
* between scrubs pre & post compute checks (checksum window).
*
* The log entry looks pretty ugly, but helps in debugging..
*/
if (signptr->stale || (signptr->version != version)) {
if (!skip_stat)
br_inc_unsigned_file_count (scrub_stat);
gf_msg_debug (this->name, 0, "<STAGE: POST> Object [GFID: %s] "
"either has a stale signature OR underwent "
"signing during checksumming {Stale: %d | "
"Version: %lu,%lu}", uuid_utoa (fd->inode->gfid),
(signptr->stale) ? 1 : 0, version,
signptr->version);
ret = -1;
goto unref_dict;
}
signlen = signptr->signaturelen;
*signature = GF_CALLOC (1, sizeof (br_isignature_out_t) + signlen,
gf_common_mt_char);
(void) memcpy (*signature, signptr,
sizeof (br_isignature_out_t) + signlen);
unref_dict:
dict_unref (xattr);
out:
return ret;
}
static int32_t
bitd_signature_staleness (xlator_t *this,
br_child_t *child, fd_t *fd,
int *stale, unsigned long *version,
br_scrub_stats_t *scrub_stat, gf_boolean_t skip_stat)
{
int32_t ret = -1;
dict_t *xattr = NULL;
br_isignature_out_t *signptr = NULL;
ret = bitd_fetch_signature (this, child, fd, &xattr, &signptr);
if (ret < 0) {
if (!skip_stat)
br_inc_unsigned_file_count (scrub_stat);
goto out;
}
/**
* save verison for validation in post compute stage
* c.f. bitd_scrub_post_compute_check()
*/
*stale = signptr->stale ? 1 : 0;
*version = signptr->version;
dict_unref (xattr);
out:
return ret;
}
/**
* PRE COMPUTE CHECK
*
* Checks to be performed before initiating object signature calculation.
* An object is skipped if:
* - it's already marked corrupted
* - has stale signature
*/
int32_t
bitd_scrub_pre_compute_check (xlator_t *this, br_child_t *child,
fd_t *fd, unsigned long *version,
br_scrub_stats_t *scrub_stat,
gf_boolean_t skip_stat)
{
int stale = 0;
int32_t ret = -1;
if (bitd_is_bad_file (this, child, NULL, fd)) {
gf_msg (this->name, GF_LOG_WARNING, 0, BRB_MSG_SKIP_OBJECT,
"Object [GFID: %s] is marked corrupted, skipping..",
uuid_utoa (fd->inode->gfid));
goto out;
}
ret = bitd_signature_staleness (this, child, fd, &stale, version,
scrub_stat, skip_stat);
if (!ret && stale) {
if (!skip_stat)
br_inc_unsigned_file_count (scrub_stat);
gf_msg_debug (this->name, 0, "<STAGE: PRE> Object [GFID: %s] "
"has stale signature",
uuid_utoa (fd->inode->gfid));
ret = -1;
}
out:
return ret;
}
/* static int */
int
bitd_compare_ckum (xlator_t *this,
br_isignature_out_t *sign,
unsigned char *md, inode_t *linked_inode,
gf_dirent_t *entry, fd_t *fd, br_child_t *child, loc_t *loc)
{
int ret = -1;
dict_t *xattr = NULL;
GF_VALIDATE_OR_GOTO ("bit-rot", this, out);
GF_VALIDATE_OR_GOTO (this->name, sign, out);
GF_VALIDATE_OR_GOTO (this->name, fd, out);
GF_VALIDATE_OR_GOTO (this->name, child, out);
GF_VALIDATE_OR_GOTO (this->name, linked_inode, out);
GF_VALIDATE_OR_GOTO (this->name, md, out);
GF_VALIDATE_OR_GOTO (this->name, entry, out);
if (strncmp
(sign->signature, (char *) md, strlen (sign->signature)) == 0) {
gf_msg_debug (this->name, 0, "%s [GFID: %s | Brick: %s] "
"matches calculated checksum", loc->path,
uuid_utoa (linked_inode->gfid),
child->brick_path);
return 0;
}
gf_msg (this->name, GF_LOG_DEBUG, 0, BRB_MSG_CHECKSUM_MISMATCH,
"Object checksum mismatch: %s [GFID: %s | Brick: %s]",
loc->path, uuid_utoa (linked_inode->gfid), child->brick_path);
gf_msg (this->name, GF_LOG_ALERT, 0, BRB_MSG_CHECKSUM_MISMATCH,
"CORRUPTION DETECTED: Object %s {Brick: %s | GFID: %s}",
loc->path, child->brick_path, uuid_utoa (linked_inode->gfid));
/* Perform bad-file marking */
xattr = dict_new ();
if (!xattr) {
ret = -1;
goto out;
}
ret = dict_set_int32 (xattr, BITROT_OBJECT_BAD_KEY, _gf_true);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_MARK_BAD_FILE,
"Error setting bad-file marker for %s [GFID: %s | "
"Brick: %s]", loc->path, uuid_utoa (linked_inode->gfid),
child->brick_path);
goto dictfree;
}
gf_msg (this->name, GF_LOG_ALERT, 0, BRB_MSG_MARK_CORRUPTED, "Marking"
" %s [GFID: %s | Brick: %s] as corrupted..", loc->path,
uuid_utoa (linked_inode->gfid), child->brick_path);
gf_event (EVENT_BITROT_BAD_FILE, "gfid=%s;path=%s;brick=%s",
uuid_utoa (linked_inode->gfid), loc->path, child->brick_path);
ret = syncop_fsetxattr (child->xl, fd, xattr, 0, NULL, NULL);
if (ret)
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_MARK_BAD_FILE,
"Error marking object %s [GFID: %s] as corrupted",
loc->path, uuid_utoa (linked_inode->gfid));
dictfree:
dict_unref (xattr);
out:
return ret;
}
/**
* "The Scrubber"
*
* Perform signature validation for a given object with the assumption
* that the signature is SHA256 (because signer as of now _always_
* signs with SHA256).
*/
int
br_scrubber_scrub_begin (xlator_t *this, struct br_fsscan_entry *fsentry)
{
int32_t ret = -1;
fd_t *fd = NULL;
loc_t loc = {0, };
struct iatt iatt = {0, };
struct iatt parent_buf = {0, };
pid_t pid = 0;
br_child_t *child = NULL;
unsigned char *md = NULL;
inode_t *linked_inode = NULL;
br_isignature_out_t *sign = NULL;
unsigned long signedversion = 0;
gf_dirent_t *entry = NULL;
br_private_t *priv = NULL;
loc_t *parent = NULL;
gf_boolean_t skip_stat = _gf_false;
uuid_t shard_root_gfid = {0,};
GF_VALIDATE_OR_GOTO ("bit-rot", fsentry, out);
entry = fsentry->entry;
parent = &fsentry->parent;
child = fsentry->data;
priv = this->private;
GF_VALIDATE_OR_GOTO ("bit-rot", entry, out);
GF_VALIDATE_OR_GOTO ("bit-rot", parent, out);
GF_VALIDATE_OR_GOTO ("bit-rot", child, out);
GF_VALIDATE_OR_GOTO ("bit-rot", priv, out);
pid = GF_CLIENT_PID_SCRUB;
ret = br_prepare_loc (this, child, parent, entry, &loc);
if (!ret)
goto out;
syncopctx_setfspid (&pid);
ret = syncop_lookup (child->xl, &loc, &iatt, &parent_buf, NULL, NULL);
if (ret) {
br_log_object_path (this, "lookup", loc.path, -ret);
goto out;
}
linked_inode = inode_link (loc.inode, parent->inode, loc.name, &iatt);
if (linked_inode)
inode_lookup (linked_inode);
gf_msg_debug (this->name, 0, "Scrubbing object %s [GFID: %s]",
entry->d_name, uuid_utoa (linked_inode->gfid));
if (iatt.ia_type != IA_IFREG) {
gf_msg_debug (this->name, 0, "%s is not a regular file",
entry->d_name);
ret = 0;
goto unref_inode;
}
if (IS_DHT_LINKFILE_MODE ((&iatt))) {
gf_msg_debug (this->name, 0, "%s is a dht sticky bit file",
entry->d_name);
ret = 0;
goto unref_inode;
}
/* skip updating scrub statistics for shard entries */
gf_uuid_parse (SHARD_ROOT_GFID, shard_root_gfid);
if (gf_uuid_compare (loc.pargfid, shard_root_gfid) == 0)
skip_stat = _gf_true;
/**
* open() an fd for subsequent opertaions
*/
fd = fd_create (linked_inode, 0);
if (!fd) {
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_FD_CREATE_FAILED,
"failed to create fd for inode %s",
uuid_utoa (linked_inode->gfid));
goto unref_inode;
}
ret = syncop_open (child->xl, &loc, O_RDWR, fd, NULL, NULL);
if (ret) {
br_log_object (this, "open", linked_inode->gfid, -ret);
ret = -1;
goto unrefd;
}
fd_bind (fd);
/**
* perform pre compute checks before initiating checksum
* computation
* - presence of bad object
* - signature staleness
*/
ret = bitd_scrub_pre_compute_check (this, child, fd, &signedversion,
&priv->scrub_stat, skip_stat);
if (ret)
goto unrefd; /* skip this object */
/* if all's good, proceed to calculate the hash */
md = GF_CALLOC (SHA256_DIGEST_LENGTH, sizeof (*md),
gf_common_mt_char);
if (!md)
goto unrefd;
ret = br_calculate_obj_checksum (md, child, fd, &iatt);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_CALC_ERROR,
"error calculating hash for object [GFID: %s]",
uuid_utoa (fd->inode->gfid));
ret = -1;
goto free_md;
}
/**
* perform post compute checks as an object's signature may have
* become stale while scrubber calculated checksum.
*/
ret = bitd_scrub_post_compute_check (this, child, fd, signedversion,
&sign, &priv->scrub_stat,
skip_stat);
if (ret)
goto free_md;
ret = bitd_compare_ckum (this, sign, md,
linked_inode, entry, fd, child, &loc);
if (!skip_stat)
br_inc_scrubbed_file (&priv->scrub_stat);
GF_FREE (sign); /* alloced on post-compute */
/** fd_unref() takes care of closing fd.. like syncop_close() */
free_md:
GF_FREE (md);
unrefd:
fd_unref (fd);
unref_inode:
inode_unref (linked_inode);
out:
loc_wipe (&loc);
return ret;
}
static void
_br_lock_cleaner (void *arg)
{
pthread_mutex_t *mutex = arg;
pthread_mutex_unlock (mutex);
}
static void
wait_for_scrubbing (xlator_t *this, struct br_scanfs *fsscan)
{
br_private_t *priv = NULL;
struct br_scrubber *fsscrub = NULL;
priv = this->private;
fsscrub = &priv->fsscrub;
pthread_cleanup_push (_br_lock_cleaner, &fsscan->waitlock);
pthread_mutex_lock (&fsscan->waitlock);
{
pthread_cleanup_push (_br_lock_cleaner, &fsscrub->mutex);
pthread_mutex_lock (&fsscrub->mutex);
{
list_replace_init (&fsscan->queued, &fsscan->ready);
/* wake up scrubbers */
pthread_cond_broadcast (&fsscrub->cond);
}
pthread_mutex_unlock (&fsscrub->mutex);
pthread_cleanup_pop (0);
while (fsscan->entries != 0)
pthread_cond_wait
(&fsscan->waitcond, &fsscan->waitlock);
}
pthread_mutex_unlock (&fsscan->waitlock);
pthread_cleanup_pop (0);
}
static void
_br_fsscan_inc_entry_count (struct br_scanfs *fsscan)
{
fsscan->entries++;
}
static void
_br_fsscan_dec_entry_count (struct br_scanfs *fsscan)
{
if (--fsscan->entries == 0) {
pthread_mutex_lock (&fsscan->waitlock);
{
pthread_cond_signal (&fsscan->waitcond);
}
pthread_mutex_unlock (&fsscan->waitlock);
}
}
static void
_br_fsscan_collect_entry (struct br_scanfs *fsscan,
struct br_fsscan_entry *fsentry)
{
list_add_tail (&fsentry->list, &fsscan->queued);
_br_fsscan_inc_entry_count (fsscan);
}
#define NR_ENTRIES (1<<7) /* ..bulk scrubbing */
int
br_fsscanner_handle_entry (xlator_t *subvol,
gf_dirent_t *entry, loc_t *parent, void *data)
{
int32_t ret = -1;
int scrub = 0;
br_child_t *child = NULL;
xlator_t *this = NULL;
struct br_scanfs *fsscan = NULL;
struct br_fsscan_entry *fsentry = NULL;
GF_VALIDATE_OR_GOTO ("bit-rot", subvol, error_return);
GF_VALIDATE_OR_GOTO ("bit-rot", data, error_return);
child = data;
this = child->this;
fsscan = &child->fsscan;
_mask_cancellation ();
fsentry = GF_CALLOC (1, sizeof (*fsentry), gf_br_mt_br_fsscan_entry_t);
if (!fsentry)
goto error_return;
{
fsentry->data = data;
fsentry->fsscan = &child->fsscan;
/* copy parent loc */
ret = loc_copy (&fsentry->parent, parent);
if (ret)
goto dealloc;
/* copy child entry */
fsentry->entry = entry_copy (entry);
if (!fsentry->entry)
goto locwipe;
INIT_LIST_HEAD (&fsentry->list);
}
LOCK (&fsscan->entrylock);
{
_br_fsscan_collect_entry (fsscan, fsentry);
/**
* need not be a equality check as entries may be pushed
* back onto the scanned queue when thread(s) are cleaned.
*/
if (fsscan->entries >= NR_ENTRIES)
scrub = 1;
}
UNLOCK (&fsscan->entrylock);
_unmask_cancellation ();
if (scrub)
wait_for_scrubbing (this, fsscan);
return 0;
locwipe:
loc_wipe (&fsentry->parent);
dealloc:
GF_FREE (fsentry);
error_return:
return -1;
}
int32_t
br_fsscan_deactivate (xlator_t *this)
{
int ret = 0;
br_private_t *priv = NULL;
br_scrub_state_t nstate = 0;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
scrub_monitor = &priv->scrub_monitor;
ret = gf_tw_del_timer (priv->timer_wheel, scrub_monitor->timer);
if (ret == 0) {
nstate = BR_SCRUB_STATE_STALLED;
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_INFO,
"Volume is under active scrubbing. Pausing scrub..");
} else {
nstate = BR_SCRUB_STATE_PAUSED;
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_INFO,
"Scrubber paused");
}
_br_monitor_set_scrub_state (scrub_monitor, nstate);
return 0;
}
static void
br_scrubber_log_time (xlator_t *this, const char *sfx)
{
char timestr[1024] = {0,};
struct timeval tv = {0,};
br_private_t *priv = NULL;
priv = this->private;
gettimeofday (&tv, NULL);
gf_time_fmt (timestr, sizeof (timestr), tv.tv_sec, gf_timefmt_FT);
if (strcasecmp (sfx, "started") == 0) {
br_update_scrub_start_time (&priv->scrub_stat, &tv);
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_START,
"Scrubbing %s at %s", sfx, timestr);
} else {
br_update_scrub_finish_time (&priv->scrub_stat, timestr, &tv);
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_FINISH,
"Scrubbing %s at %s", sfx, timestr);
}
}
static void
br_fsscanner_log_time (xlator_t *this, br_child_t *child, const char *sfx)
{
char timestr[1024] = {0,};
struct timeval tv = {0,};
gettimeofday (&tv, NULL);
gf_time_fmt (timestr, sizeof (timestr), tv.tv_sec, gf_timefmt_FT);
if (strcasecmp (sfx, "started") == 0) {
gf_msg_debug (this->name, 0, "Scrubbing \"%s\" %s at %s",
child->brick_path, sfx, timestr);
} else {
gf_msg_debug (this->name, 0, "Scrubbing \"%s\" %s at %s",
child->brick_path, sfx, timestr);
}
}
void
br_child_set_scrub_state (br_child_t *child, gf_boolean_t state)
{
child->active_scrubbing = state;
}
static void
br_fsscanner_wait_until_kicked (xlator_t *this, br_child_t *child)
{
br_private_t *priv = NULL;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
scrub_monitor = &priv->scrub_monitor;
pthread_cleanup_push (_br_lock_cleaner, &scrub_monitor->wakelock);
pthread_mutex_lock (&scrub_monitor->wakelock);
{
while (!scrub_monitor->kick)
pthread_cond_wait (&scrub_monitor->wakecond,
&scrub_monitor->wakelock);
/* Child lock is to synchronize with disconnect events */
pthread_cleanup_push (_br_lock_cleaner, &child->lock);
pthread_mutex_lock (&child->lock);
{
scrub_monitor->active_child_count++;
br_child_set_scrub_state (child, _gf_true);
}
pthread_mutex_unlock (&child->lock);
pthread_cleanup_pop (0);
}
pthread_mutex_unlock (&scrub_monitor->wakelock);
pthread_cleanup_pop (0);
}
static void
br_scrubber_entry_control (xlator_t *this)
{
br_private_t *priv = NULL;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
scrub_monitor = &priv->scrub_monitor;
LOCK (&scrub_monitor->lock);
{
/* Move the state to BR_SCRUB_STATE_ACTIVE */
if (scrub_monitor->state == BR_SCRUB_STATE_PENDING)
scrub_monitor->state = BR_SCRUB_STATE_ACTIVE;
br_scrubber_log_time (this, "started");
priv->scrub_stat.scrub_running = 1;
}
UNLOCK (&scrub_monitor->lock);
}
static void
br_scrubber_exit_control (xlator_t *this)
{
br_private_t *priv = NULL;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
scrub_monitor = &priv->scrub_monitor;
LOCK (&scrub_monitor->lock);
{
br_scrubber_log_time (this, "finished");
priv->scrub_stat.scrub_running = 0;
if (scrub_monitor->state == BR_SCRUB_STATE_ACTIVE) {
(void) br_fsscan_activate (this);
} else {
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_INFO,
"Volume waiting to get rescheduled..");
}
}
UNLOCK (&scrub_monitor->lock);
}
static void
br_fsscanner_entry_control (xlator_t *this, br_child_t *child)
{
br_fsscanner_log_time (this, child, "started");
}
static void
br_fsscanner_exit_control (xlator_t *this, br_child_t *child)
{
br_private_t *priv = NULL;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
scrub_monitor = &priv->scrub_monitor;
if (!_br_is_child_connected (child)) {
gf_msg (this->name, GF_LOG_WARNING, 0, BRB_MSG_SCRUB_INFO,
"Brick [%s] disconnected while scrubbing. Scrubbing "
"might be incomplete", child->brick_path);
}
br_fsscanner_log_time (this, child, "finished");
pthread_cleanup_push (_br_lock_cleaner, &scrub_monitor->wakelock);
pthread_mutex_lock (&scrub_monitor->wakelock);
{
scrub_monitor->active_child_count--;
pthread_cleanup_push (_br_lock_cleaner, &child->lock);
pthread_mutex_lock (&child->lock);
{
br_child_set_scrub_state (child, _gf_false);
}
pthread_mutex_unlock (&child->lock);
pthread_cleanup_pop (0);
if (scrub_monitor->active_child_count == 0) {
/* The last child has finished scrubbing.
* Set the kick to false and wake up other
* children who are waiting for the last
* child to complete scrubbing.
*/
scrub_monitor->kick = _gf_false;
pthread_cond_broadcast (&scrub_monitor->wakecond);
/* Signal monitor thread waiting for the all
* the children to finish scrubbing.
*/
pthread_cleanup_push (_br_lock_cleaner,
&scrub_monitor->donelock);
pthread_mutex_lock (&scrub_monitor->donelock);
{
scrub_monitor->done = _gf_true;
pthread_cond_signal (&scrub_monitor->donecond);
}
pthread_mutex_unlock (&scrub_monitor->donelock);
pthread_cleanup_pop (0);
} else {
while (scrub_monitor->active_child_count)
pthread_cond_wait (&scrub_monitor->wakecond,
&scrub_monitor->wakelock);
}
}
pthread_mutex_unlock (&scrub_monitor->wakelock);
pthread_cleanup_pop (0);
}
void *
br_fsscanner (void *arg)
{
loc_t loc = {0,};
br_child_t *child = NULL;
xlator_t *this = NULL;
struct br_scanfs *fsscan = NULL;
child = arg;
this = child->this;
fsscan = &child->fsscan;
THIS = this;
loc.inode = child->table->root;
while (1) {
br_fsscanner_wait_until_kicked (this, child);
{
/* precursor for scrub */
br_fsscanner_entry_control (this, child);
/* scrub */
(void) syncop_ftw (child->xl,
&loc, GF_CLIENT_PID_SCRUB,
child, br_fsscanner_handle_entry);
if (!list_empty (&fsscan->queued))
wait_for_scrubbing (this, fsscan);
/* scrub exit criteria */
br_fsscanner_exit_control (this, child);
}
}
return NULL;
}
/**
* Keep this routine extremely simple and do not ever try to acquire
* child->lock here: it may lead to deadlock. Scrubber state is
* modified in br_fsscanner(). An intermediate state change to pause
* changes the scrub state to the _correct_ state by identifying a
* non-pending timer.
*/
void
br_kickstart_scanner (struct gf_tw_timer_list *timer,
void *data, unsigned long calltime)
{
xlator_t *this = NULL;
struct br_monitor *scrub_monitor = data;
br_private_t *priv = NULL;
THIS = this = scrub_monitor->this;
priv = this->private;
/* Reset scrub statistics */
priv->scrub_stat.scrubbed_files = 0;
priv->scrub_stat.unsigned_files = 0;
/* Moves state from PENDING to ACTIVE */
(void) br_scrubber_entry_control (this);
/* kickstart scanning.. */
pthread_mutex_lock (&scrub_monitor->wakelock);
{
scrub_monitor->kick = _gf_true;
GF_ASSERT (scrub_monitor->active_child_count == 0);
pthread_cond_broadcast (&scrub_monitor->wakecond);
}
pthread_mutex_unlock (&scrub_monitor->wakelock);
return;
}
static uint32_t
br_fsscan_calculate_delta (uint32_t times)
{
return times;
}
#define BR_SCRUB_MINUTE (60)
#define BR_SCRUB_HOURLY (60 * 60)
#define BR_SCRUB_DAILY (1 * 24 * 60 * 60)
#define BR_SCRUB_WEEKLY (7 * 24 * 60 * 60)
#define BR_SCRUB_BIWEEKLY (14 * 24 * 60 * 60)
#define BR_SCRUB_MONTHLY (30 * 24 * 60 * 60)
static unsigned int
br_fsscan_calculate_timeout (scrub_freq_t freq)
{
uint32_t timo = 0;
switch (freq) {
case BR_FSSCRUB_FREQ_MINUTE:
timo = br_fsscan_calculate_delta (BR_SCRUB_MINUTE);
break;
case BR_FSSCRUB_FREQ_HOURLY:
timo = br_fsscan_calculate_delta (BR_SCRUB_HOURLY);
break;
case BR_FSSCRUB_FREQ_DAILY:
timo = br_fsscan_calculate_delta (BR_SCRUB_DAILY);
break;
case BR_FSSCRUB_FREQ_WEEKLY:
timo = br_fsscan_calculate_delta (BR_SCRUB_WEEKLY);
break;
case BR_FSSCRUB_FREQ_BIWEEKLY:
timo = br_fsscan_calculate_delta (BR_SCRUB_BIWEEKLY);
break;
case BR_FSSCRUB_FREQ_MONTHLY:
timo = br_fsscan_calculate_delta (BR_SCRUB_MONTHLY);
break;
default:
timo = 0;
}
return timo;
}
int32_t
br_fsscan_schedule (xlator_t *this)
{
uint32_t timo = 0;
br_private_t *priv = NULL;
struct timeval tv = {0,};
char timestr[1024] = {0,};
struct br_scrubber *fsscrub = NULL;
struct gf_tw_timer_list *timer = NULL;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
fsscrub = &priv->fsscrub;
scrub_monitor = &priv->scrub_monitor;
(void) gettimeofday (&tv, NULL);
scrub_monitor->boot = tv.tv_sec;
timo = br_fsscan_calculate_timeout (fsscrub->frequency);
if (timo == 0) {
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_ZERO_TIMEOUT_BUG,
"BUG: Zero schedule timeout");
goto error_return;
}
scrub_monitor->timer = GF_CALLOC (1, sizeof (*scrub_monitor->timer),
gf_br_stub_mt_br_scanner_freq_t);
if (!scrub_monitor->timer)
goto error_return;
timer = scrub_monitor->timer;
INIT_LIST_HEAD (&timer->entry);
timer->data = scrub_monitor;
timer->expires = timo;
timer->function = br_kickstart_scanner;
gf_tw_add_timer (priv->timer_wheel, timer);
_br_monitor_set_scrub_state (scrub_monitor, BR_SCRUB_STATE_PENDING);
gf_time_fmt (timestr, sizeof (timestr),
(scrub_monitor->boot + timo), gf_timefmt_FT);
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_INFO, "Scrubbing is "
"scheduled to run at %s", timestr);
return 0;
error_return:
return -1;
}
int32_t
br_fsscan_activate (xlator_t *this)
{
uint32_t timo = 0;
char timestr[1024] = {0,};
struct timeval now = {0,};
br_private_t *priv = NULL;
struct br_scrubber *fsscrub = NULL;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
fsscrub = &priv->fsscrub;
scrub_monitor = &priv->scrub_monitor;
(void) gettimeofday (&now, NULL);
timo = br_fsscan_calculate_timeout (fsscrub->frequency);
if (timo == 0) {
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_ZERO_TIMEOUT_BUG,
"BUG: Zero schedule timeout");
return -1;
}
pthread_mutex_lock (&scrub_monitor->donelock);
{
scrub_monitor->done = _gf_false;
}
pthread_mutex_unlock (&scrub_monitor->donelock);
gf_time_fmt (timestr, sizeof (timestr),
(now.tv_sec + timo), gf_timefmt_FT);
(void) gf_tw_mod_timer (priv->timer_wheel, scrub_monitor->timer, timo);
_br_monitor_set_scrub_state (scrub_monitor, BR_SCRUB_STATE_PENDING);
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_INFO, "Scrubbing is "
"rescheduled to run at %s", timestr);
return 0;
}
int32_t
br_fsscan_reschedule (xlator_t *this)
{
int32_t ret = 0;
uint32_t timo = 0;
char timestr[1024] = {0,};
struct timeval now = {0,};
br_private_t *priv = NULL;
struct br_scrubber *fsscrub = NULL;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
fsscrub = &priv->fsscrub;
scrub_monitor = &priv->scrub_monitor;
if (!fsscrub->frequency_reconf)
return 0;
(void) gettimeofday (&now, NULL);
timo = br_fsscan_calculate_timeout (fsscrub->frequency);
if (timo == 0) {
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_ZERO_TIMEOUT_BUG,
"BUG: Zero schedule timeout");
return -1;
}
gf_time_fmt (timestr, sizeof (timestr),
(now.tv_sec + timo), gf_timefmt_FT);
pthread_mutex_lock (&scrub_monitor->donelock);
{
scrub_monitor->done = _gf_false;
}
pthread_mutex_unlock (&scrub_monitor->donelock);
ret = gf_tw_mod_timer_pending (priv->timer_wheel, scrub_monitor->timer, timo);
if (ret == 0)
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_INFO,
"Scrubber is currently running and would be "
"rescheduled after completion");
else {
_br_monitor_set_scrub_state (scrub_monitor, BR_SCRUB_STATE_PENDING);
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_INFO,
"Scrubbing rescheduled to run at %s", timestr);
}
return 0;
}
#define BR_SCRUB_THREAD_SCALE_LAZY 0
#define BR_SCRUB_THREAD_SCALE_NORMAL 0.4
#define BR_SCRUB_THREAD_SCALE_AGGRESSIVE 1.0
#ifndef M_E
#define M_E 2.718
#endif
/**
* This is just a simple exponential scale to a fixed value selected
* per throttle config. We probably need to be more smart and select
* the scale based on the number of processor cores too.
*/
static unsigned int
br_scrubber_calc_scale (xlator_t *this,
br_private_t *priv, scrub_throttle_t throttle)
{
unsigned int scale = 0;
switch (throttle) {
case BR_SCRUB_THROTTLE_VOID:
case BR_SCRUB_THROTTLE_STALLED:
scale = 0;
break;
case BR_SCRUB_THROTTLE_LAZY:
scale = priv->child_count *
pow (M_E, BR_SCRUB_THREAD_SCALE_LAZY);
break;
case BR_SCRUB_THROTTLE_NORMAL:
scale = priv->child_count *
pow (M_E, BR_SCRUB_THREAD_SCALE_NORMAL);
break;
case BR_SCRUB_THROTTLE_AGGRESSIVE:
scale = priv->child_count *
pow (M_E, BR_SCRUB_THREAD_SCALE_AGGRESSIVE);
break;
default:
gf_msg (this->name, GF_LOG_ERROR, 0, BRB_MSG_UNKNOWN_THROTTLE,
"Unknown throttle %d", throttle);
}
return scale;
}
static br_child_t *
_br_scrubber_get_next_child (struct br_scrubber *fsscrub)
{
br_child_t *child = NULL;
child = list_first_entry (&fsscrub->scrublist, br_child_t, list);
list_rotate_left (&fsscrub->scrublist);
return child;
}
static void
_br_scrubber_get_entry (br_child_t *child, struct br_fsscan_entry **fsentry)
{
struct br_scanfs *fsscan = &child->fsscan;
if (list_empty (&fsscan->ready))
return;
*fsentry = list_first_entry
(&fsscan->ready, struct br_fsscan_entry, list);
list_del_init (&(*fsentry)->list);
}
static void
_br_scrubber_find_scrubbable_entry (struct br_scrubber *fsscrub,
struct br_fsscan_entry **fsentry)
{
br_child_t *child = NULL;
br_child_t *firstchild = NULL;
while (1) {
while (list_empty (&fsscrub->scrublist))
pthread_cond_wait (&fsscrub->cond, &fsscrub->mutex);
firstchild = NULL;
for (child = _br_scrubber_get_next_child (fsscrub);
child != firstchild;
child = _br_scrubber_get_next_child (fsscrub)) {
if (!firstchild)
firstchild = child;
_br_scrubber_get_entry (child, fsentry);
if (*fsentry)
break;
}
if (*fsentry)
break;
/* nothing to work on.. wait till available */
pthread_cond_wait (&fsscrub->cond, &fsscrub->mutex);
}
}
static void
br_scrubber_pick_entry (struct br_scrubber *fsscrub,
struct br_fsscan_entry **fsentry)
{
pthread_cleanup_push (_br_lock_cleaner, &fsscrub->mutex);
pthread_mutex_lock (&fsscrub->mutex);
{
*fsentry = NULL;
_br_scrubber_find_scrubbable_entry (fsscrub, fsentry);
}
pthread_mutex_unlock (&fsscrub->mutex);
pthread_cleanup_pop (0);
}
struct br_scrub_entry {
gf_boolean_t scrubbed;
struct br_fsscan_entry *fsentry;
};
/**
* We need to be a bit careful here. These thread(s) are prone to cancellations
* when threads are scaled down (depending on the thottling value configured)
* and pausing scrub. A thread can get cancelled while it's waiting for entries
* in the ->pending queue or when an object is undergoing scrubbing.
*/
static void
br_scrubber_entry_handle (void *arg)
{
struct br_scanfs *fsscan = NULL;
struct br_scrub_entry *sentry = NULL;
struct br_fsscan_entry *fsentry = NULL;
sentry = arg;
fsentry = sentry->fsentry;
fsscan = fsentry->fsscan;
LOCK (&fsscan->entrylock);
{
if (sentry->scrubbed) {
_br_fsscan_dec_entry_count (fsscan);
/* cleanup ->entry */
fsentry->data = NULL;
fsentry->fsscan = NULL;
loc_wipe (&fsentry->parent);
gf_dirent_entry_free (fsentry->entry);
GF_FREE (sentry->fsentry);
} else {
/* (re)queue the entry again for scrub */
_br_fsscan_collect_entry (fsscan, sentry->fsentry);
}
}
UNLOCK (&fsscan->entrylock);
}
static void
br_scrubber_scrub_entry (xlator_t *this, struct br_fsscan_entry *fsentry)
{
struct br_scrub_entry sentry = {0, };
sentry.scrubbed = 0;
sentry.fsentry = fsentry;
pthread_cleanup_push (br_scrubber_entry_handle, &sentry);
{
(void) br_scrubber_scrub_begin (this, fsentry);
sentry.scrubbed = 1;
}
pthread_cleanup_pop (1);
}
void *br_scrubber_proc (void *arg)
{
xlator_t *this = NULL;
struct br_scrubber *fsscrub = NULL;
struct br_fsscan_entry *fsentry = NULL;
fsscrub = arg;
THIS = this = fsscrub->this;
while (1) {
br_scrubber_pick_entry (fsscrub, &fsentry);
br_scrubber_scrub_entry (this, fsentry);
sleep (1);
}
return NULL;
}
static int32_t
br_scrubber_scale_up (xlator_t *this,
struct br_scrubber *fsscrub,
unsigned int v1, unsigned int v2)
{
int i = 0;
int32_t ret = -1;
int diff = 0;
struct br_scrubbers *scrub = NULL;
diff = (int)(v2 - v1);
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCALING_UP_SCRUBBER,
"Scaling up scrubbers [%d => %d]", v1, v2);
for (i = 0; i < diff; i++) {
scrub = GF_CALLOC (diff, sizeof (*scrub),
gf_br_mt_br_scrubber_t);
if (!scrub)
break;
INIT_LIST_HEAD (&scrub->list);
ret = gf_thread_create (&scrub->scrubthread,
NULL, br_scrubber_proc, fsscrub);
if (ret)
break;
fsscrub->nr_scrubbers++;
list_add_tail (&scrub->list, &fsscrub->scrubbers);
}
if ((i != diff) && !scrub)
goto error_return;
if (i != diff) /* degraded scaling.. */
gf_msg (this->name, GF_LOG_WARNING, 0, BRB_MSG_SCALE_UP_FAILED,
"Could not fully scale up to %d scrubber(s). Spawned "
"%d/%d [total scrubber(s): %d]", v2, i, diff, (v1 + i));
return 0;
error_return:
return -1;
}
static int32_t
br_scrubber_scale_down (xlator_t *this,
struct br_scrubber *fsscrub,
unsigned int v1, unsigned int v2)
{
int i = 0;
int diff = 0;
int32_t ret = -1;
struct br_scrubbers *scrub = NULL;
diff = (int)(v1 - v2);
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCALE_DOWN_SCRUBBER,
"Scaling down scrubbers [%d => %d]", v1, v2);
for (i = 0 ; i < diff; i++) {
scrub = list_first_entry
(&fsscrub->scrubbers, struct br_scrubbers, list);
list_del_init (&scrub->list);
ret = gf_thread_cleanup_xint (scrub->scrubthread);
if (ret)
break;
GF_FREE (scrub);
fsscrub->nr_scrubbers--;
}
if (ret) {
gf_msg (this->name, GF_LOG_WARNING, 0,
BRB_MSG_SCALE_DOWN_FAILED, "Could not fully scale down "
"to %d scrubber(s). Terminated %d/%d [total "
"scrubber(s): %d]", v1, i, diff, (v2 - i));
ret = 0;
}
return ret;
}
static int32_t
br_scrubber_configure (xlator_t *this, br_private_t *priv,
struct br_scrubber *fsscrub, scrub_throttle_t nthrottle)
{
int32_t ret = 0;
unsigned int v1 = 0;
unsigned int v2 = 0;
v1 = fsscrub->nr_scrubbers;
v2 = br_scrubber_calc_scale (this, priv, nthrottle);
if (v1 == v2)
return 0;
if (v1 > v2)
ret = br_scrubber_scale_down (this, fsscrub, v1, v2);
else
ret = br_scrubber_scale_up (this, fsscrub, v1, v2);
return ret;
}
static int32_t
br_scrubber_fetch_option (xlator_t *this,
char *opt, dict_t *options, char **value)
{
if (options)
GF_OPTION_RECONF (opt, *value, options, str, error_return);
else
GF_OPTION_INIT (opt, *value, str, error_return);
return 0;
error_return:
return -1;
}
/* internal "throttle" override */
#define BR_SCRUB_STALLED "STALLED"
/* TODO: token buket spec */
static int32_t
br_scrubber_handle_throttle (xlator_t *this, br_private_t *priv,
dict_t *options, gf_boolean_t scrubstall)
{
int32_t ret = 0;
char *tmp = NULL;
struct br_scrubber *fsscrub = NULL;
scrub_throttle_t nthrottle = BR_SCRUB_THROTTLE_VOID;
fsscrub = &priv->fsscrub;
fsscrub->throttle_reconf = _gf_false;
ret = br_scrubber_fetch_option (this, "scrub-throttle", options, &tmp);
if (ret)
goto error_return;
if (scrubstall)
tmp = BR_SCRUB_STALLED;
if (strcasecmp (tmp, "lazy") == 0)
nthrottle = BR_SCRUB_THROTTLE_LAZY;
else if (strcasecmp (tmp, "normal") == 0)
nthrottle = BR_SCRUB_THROTTLE_NORMAL;
else if (strcasecmp (tmp, "aggressive") == 0)
nthrottle = BR_SCRUB_THROTTLE_AGGRESSIVE;
else if (strcasecmp (tmp, BR_SCRUB_STALLED) == 0)
nthrottle = BR_SCRUB_THROTTLE_STALLED;
else
goto error_return;
/* on failure old throttling value is preserved */
ret = br_scrubber_configure (this, priv, fsscrub, nthrottle);
if (ret)
goto error_return;
if (fsscrub->throttle != nthrottle)
fsscrub->throttle_reconf = _gf_true;
fsscrub->throttle = nthrottle;
return 0;
error_return:
return -1;
}
static int32_t
br_scrubber_handle_stall (xlator_t *this, br_private_t *priv,
dict_t *options, gf_boolean_t *scrubstall)
{
int32_t ret = 0;
char *tmp = NULL;
ret = br_scrubber_fetch_option (this, "scrub-state", options, &tmp);
if (ret)
goto error_return;
if (strcasecmp (tmp, "pause") == 0) /* anything else is active */
*scrubstall = _gf_true;
return 0;
error_return:
return -1;
}
static int32_t
br_scrubber_handle_freq (xlator_t *this, br_private_t *priv,
dict_t *options, gf_boolean_t scrubstall)
{
int32_t ret = -1;
char *tmp = NULL;
scrub_freq_t frequency = BR_FSSCRUB_FREQ_HOURLY;
struct br_scrubber *fsscrub = NULL;
fsscrub = &priv->fsscrub;
fsscrub->frequency_reconf = _gf_true;
ret = br_scrubber_fetch_option (this, "scrub-freq", options, &tmp);
if (ret)
goto error_return;
if (scrubstall)
tmp = BR_SCRUB_STALLED;
if (strcasecmp (tmp, "hourly") == 0) {
frequency = BR_FSSCRUB_FREQ_HOURLY;
} else if (strcasecmp (tmp, "daily") == 0) {
frequency = BR_FSSCRUB_FREQ_DAILY;
} else if (strcasecmp (tmp, "weekly") == 0) {
frequency = BR_FSSCRUB_FREQ_WEEKLY;
} else if (strcasecmp (tmp, "biweekly") == 0) {
frequency = BR_FSSCRUB_FREQ_BIWEEKLY;
} else if (strcasecmp (tmp, "monthly") == 0) {
frequency = BR_FSSCRUB_FREQ_MONTHLY;
} else if (strcasecmp (tmp, "minute") == 0) {
frequency = BR_FSSCRUB_FREQ_MINUTE;
} else if (strcasecmp (tmp, BR_SCRUB_STALLED) == 0) {
frequency = BR_FSSCRUB_FREQ_STALLED;
} else
goto error_return;
if (fsscrub->frequency == frequency)
fsscrub->frequency_reconf = _gf_false;
else
fsscrub->frequency = frequency;
return 0;
error_return:
return -1;
}
static void br_scrubber_log_option (xlator_t *this,
br_private_t *priv, gf_boolean_t scrubstall)
{
struct br_scrubber *fsscrub = &priv->fsscrub;
char *scrub_throttle_str[] = {
[BR_SCRUB_THROTTLE_LAZY] = "lazy",
[BR_SCRUB_THROTTLE_NORMAL] = "normal",
[BR_SCRUB_THROTTLE_AGGRESSIVE] = "aggressive",
};
char *scrub_freq_str[] = {
[BR_FSSCRUB_FREQ_HOURLY] = "hourly",
[BR_FSSCRUB_FREQ_DAILY] = "daily",
[BR_FSSCRUB_FREQ_WEEKLY] = "weekly",
[BR_FSSCRUB_FREQ_BIWEEKLY] = "biweekly",
[BR_FSSCRUB_FREQ_MONTHLY] = "monthly (30 days)",
[BR_FSSCRUB_FREQ_MINUTE] = "every minute",
};
if (scrubstall)
return; /* logged as pause */
if (fsscrub->frequency_reconf || fsscrub->throttle_reconf) {
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_TUNABLE,
"SCRUB TUNABLES:: [Frequency: %s, Throttle: %s]",
scrub_freq_str[fsscrub->frequency],
scrub_throttle_str[fsscrub->throttle]);
}
}
int32_t
br_scrubber_handle_options (xlator_t *this, br_private_t *priv, dict_t *options)
{
int32_t ret = 0;
gf_boolean_t scrubstall = _gf_false; /* not as dangerous as it sounds */
ret = br_scrubber_handle_stall (this, priv, options, &scrubstall);
if (ret)
goto error_return;
ret = br_scrubber_handle_throttle (this, priv, options, scrubstall);
if (ret)
goto error_return;
ret = br_scrubber_handle_freq (this, priv, options, scrubstall);
if (ret)
goto error_return;
br_scrubber_log_option (this, priv, scrubstall);
return 0;
error_return:
return -1;
}
inode_t *
br_lookup_bad_obj_dir (xlator_t *this, br_child_t *child, uuid_t gfid)
{
struct iatt statbuf = {0, };
inode_table_t *table = NULL;
int32_t ret = -1;
loc_t loc = {0, };
inode_t *linked_inode = NULL;
int32_t op_errno = 0;
GF_VALIDATE_OR_GOTO ("bit-rot-scrubber", this, out);
GF_VALIDATE_OR_GOTO (this->name, this->private, out);
GF_VALIDATE_OR_GOTO (this->name, child, out);
table = child->table;
loc.inode = inode_new (table);
if (!loc.inode) {
gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
BRB_MSG_NO_MEMORY, "failed to allocate a new inode for"
"bad object directory");
goto out;
}
gf_uuid_copy (loc.gfid, gfid);
ret = syncop_lookup (child->xl, &loc, &statbuf, NULL, NULL, NULL);
if (ret < 0) {
op_errno = -ret;
gf_msg (this->name, GF_LOG_ERROR, 0,
BRB_MSG_LOOKUP_FAILED, "failed to lookup the bad "
"objects directory (gfid: %s (%s))", uuid_utoa (gfid),
strerror (op_errno));
goto out;
}
linked_inode = inode_link (loc.inode, NULL, NULL, &statbuf);
if (linked_inode)
inode_lookup (linked_inode);
out:
loc_wipe (&loc);
return linked_inode;
}
int32_t
br_read_bad_object_dir (xlator_t *this, br_child_t *child, fd_t *fd,
dict_t *dict)
{
gf_dirent_t entries;
gf_dirent_t *entry = NULL;
int32_t ret = -1;
off_t offset = 0;
int32_t count = 0;
char key[PATH_MAX] = {0, };
INIT_LIST_HEAD (&entries.list);
while ((ret = syncop_readdir (child->xl, fd, 131072, offset, &entries,
NULL, NULL))) {
if (ret < 0)
goto out;
if (ret == 0)
break;
list_for_each_entry (entry, &entries.list, list) {
offset = entry->d_off;
snprintf (key, sizeof (key), "quarantine-%d", count);
/*
* ignore the dict_set errors for now. The intention is
* to get as many bad objects as possible instead of
* erroring out at the first failure.
*/
ret = dict_set_dynstr_with_alloc (dict, key,
entry->d_name);
if (!ret)
count++;
}
gf_dirent_free (&entries);
}
ret = count;
ret = dict_set_int32 (dict, "count", count);
out:
return ret;
}
int32_t
br_get_bad_objects_from_child (xlator_t *this, dict_t *dict, br_child_t *child)
{
inode_t *inode = NULL;
inode_table_t *table = NULL;
fd_t *fd = NULL;
int32_t ret = -1;
loc_t loc = {0, };
int32_t op_errno = 0;
GF_VALIDATE_OR_GOTO ("bit-rot-scrubber", this, out);
GF_VALIDATE_OR_GOTO (this->name, this->private, out);
GF_VALIDATE_OR_GOTO (this->name, child, out);
GF_VALIDATE_OR_GOTO (this->name, dict, out);
table = child->table;
inode = inode_find (table, BR_BAD_OBJ_CONTAINER);
if (!inode) {
inode = br_lookup_bad_obj_dir (this, child,
BR_BAD_OBJ_CONTAINER);
if (!inode)
goto out;
}
fd = fd_create (inode, 0);
if (!fd) {
gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
BRB_MSG_FD_CREATE_FAILED, "fd creation for the bad "
"objects directory failed (gfid: %s)",
uuid_utoa (BR_BAD_OBJ_CONTAINER));
goto out;
}
loc.inode = inode;
gf_uuid_copy (loc.gfid, inode->gfid);
ret = syncop_opendir (child->xl, &loc, fd, NULL, NULL);
if (ret < 0) {
op_errno = -ret;
fd_unref (fd);
fd = NULL;
gf_msg (this->name, GF_LOG_ERROR, op_errno,
BRB_MSG_FD_CREATE_FAILED, "failed to open the bad "
"objects directory %s",
uuid_utoa (BR_BAD_OBJ_CONTAINER));
goto out;
}
fd_bind (fd);
ret = br_read_bad_object_dir (this, child, fd, dict);
if (ret < 0) {
gf_msg (this->name, GF_LOG_ERROR, 0,
BRB_MSG_BAD_OBJ_READDIR_FAIL, "readdir of the bad "
"objects directory (%s) failed ",
uuid_utoa (BR_BAD_OBJ_CONTAINER));
goto out;
}
ret = 0;
out:
loc_wipe (&loc);
if (fd)
fd_unref (fd);
return ret;
}
int32_t
br_collect_bad_objects_of_child (xlator_t *this, br_child_t *child,
dict_t *dict, dict_t *child_dict,
int32_t total_count)
{
int32_t ret = -1;
int32_t count = 0;
char key[PATH_MAX] = {0, };
char main_key[PATH_MAX] = {0, };
int32_t j = 0;
int32_t tmp_count = 0;
char *entry = NULL;
ret = dict_get_int32 (child_dict, "count", &count);
if (ret)
goto out;
tmp_count = total_count;
for (j = 0; j < count; j++) {
snprintf (key, PATH_MAX, "quarantine-%d", j);
ret = dict_get_str (child_dict, key, &entry);
if (ret)
continue;
snprintf (main_key, PATH_MAX, "quarantine-%d",
tmp_count);
ret = dict_set_dynstr_with_alloc (dict, main_key, entry);
if (!ret)
tmp_count++;
}
ret = tmp_count;
out:
return ret;
}
int32_t
br_collect_bad_objects_from_children (xlator_t *this, dict_t *dict)
{
int32_t ret = -1;
dict_t *child_dict = NULL;
int32_t i = 0;
int32_t total_count = 0;
br_child_t *child = NULL;
br_private_t *priv = NULL;
dict_t *tmp_dict = NULL;
priv = this->private;
tmp_dict = dict;
for (i = 0; i < priv->child_count; i++) {
child = &priv->children[i];
GF_ASSERT (child);
if (!_br_is_child_connected (child))
continue;
child_dict = dict_new ();
if (!child_dict) {
gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
BRB_MSG_NO_MEMORY, "failed to allocate dict");
continue;
}
ret = br_get_bad_objects_from_child (this, child_dict, child);
/*
* Continue asking the remaining children for the list of
* bad objects even though getting the list from one of them
* fails.
*/
if (ret) {
dict_unref (child_dict);
continue;
}
ret = br_collect_bad_objects_of_child (this, child, tmp_dict,
child_dict, total_count);
if (ret < 0) {
dict_unref (child_dict);
continue;
}
total_count = ret;
dict_unref (child_dict);
child_dict = NULL;
}
ret = dict_set_int32 (tmp_dict, "total-count", total_count);
return ret;
}
int32_t
br_get_bad_objects_list (xlator_t *this, dict_t **dict)
{
int32_t ret = -1;
dict_t *tmp_dict = NULL;
GF_VALIDATE_OR_GOTO ("bir-rot-scrubber", this, out);
GF_VALIDATE_OR_GOTO (this->name, dict, out);
tmp_dict = *dict;
if (!tmp_dict) {
tmp_dict = dict_new ();
if (!tmp_dict) {
gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
BRB_MSG_NO_MEMORY, "failed to allocate dict");
goto out;
}
*dict = tmp_dict;
}
ret = br_collect_bad_objects_from_children (this, tmp_dict);
out:
return ret;
}
static int
wait_for_scrub_to_finish (xlator_t *this)
{
int ret = -1;
br_private_t *priv = NULL;
struct br_monitor *scrub_monitor = NULL;
priv = this->private;
scrub_monitor = &priv->scrub_monitor;
GF_VALIDATE_OR_GOTO ("bit-rot", scrub_monitor, out);
GF_VALIDATE_OR_GOTO ("bit-rot", this, out);
gf_msg (this->name, GF_LOG_INFO, 0, BRB_MSG_SCRUB_INFO,
"Waiting for all children to start and finish scrub");
pthread_mutex_lock (&scrub_monitor->donelock);
{
while (!scrub_monitor->done)
pthread_cond_wait (&scrub_monitor->donecond,
&scrub_monitor->donelock);
}
pthread_mutex_unlock (&scrub_monitor->donelock);
ret = 0;
out:
return ret;
}
/**
* This function is executed in a separate thread. This is scrubber monitor
* thread that takes care of state machine.
*/
void *
br_monitor_thread (void *arg)
{
int32_t ret = 0;
xlator_t *this = NULL;
br_private_t *priv = NULL;
struct br_monitor *scrub_monitor = NULL;
this = arg;
priv = this->private;
/*
* Since, this is the topmost xlator, THIS has to be set by bit-rot
* xlator itself (STACK_WIND wont help in this case). Also it has
* to be done for each thread that gets spawned. Otherwise, a new
* thread will get global_xlator's pointer when it does "THIS".
*/
THIS = this;
scrub_monitor = &priv->scrub_monitor;
pthread_mutex_lock (&scrub_monitor->mutex);
{
while (!scrub_monitor->inited)
pthread_cond_wait (&scrub_monitor->cond,
&scrub_monitor->mutex);
}
pthread_mutex_unlock (&scrub_monitor->mutex);
/* this needs to be serialized with reconfigure() */
pthread_mutex_lock (&priv->lock);
{
ret = br_scrub_state_machine (this);
}
pthread_mutex_unlock (&priv->lock);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, -ret,
BRB_MSG_SSM_FAILED,
"Scrub state machine failed");
goto out;
}
while (1) {
/* Wait for all children to finish scrubbing */
ret = wait_for_scrub_to_finish (this);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, -ret,
BRB_MSG_SCRUB_WAIT_FAILED,
"Scrub wait failed");
goto out;
}
/* scrub exit criteria: Move the state to PENDING */
br_scrubber_exit_control (this);
}
out:
return NULL;
}
static void
br_set_scrub_state (struct br_monitor *scrub_monitor, br_scrub_state_t state)
{
LOCK (&scrub_monitor->lock);
{
_br_monitor_set_scrub_state (scrub_monitor, state);
}
UNLOCK (&scrub_monitor->lock);
}
int32_t
br_scrubber_monitor_init (xlator_t *this, br_private_t *priv)
{
struct br_monitor *scrub_monitor = NULL;
int ret = 0;
scrub_monitor = &priv->scrub_monitor;
LOCK_INIT (&scrub_monitor->lock);
scrub_monitor->this = this;
scrub_monitor->inited = _gf_false;
pthread_mutex_init (&scrub_monitor->mutex, NULL);
pthread_cond_init (&scrub_monitor->cond, NULL);
scrub_monitor->kick = _gf_false;
scrub_monitor->active_child_count = 0;
pthread_mutex_init (&scrub_monitor->wakelock, NULL);
pthread_cond_init (&scrub_monitor->wakecond, NULL);
scrub_monitor->done = _gf_false;
pthread_mutex_init (&scrub_monitor->donelock, NULL);
pthread_cond_init (&scrub_monitor->donecond, NULL);
/* Set the state to INACTIVE */
br_set_scrub_state (&priv->scrub_monitor, BR_SCRUB_STATE_INACTIVE);
/* Start the monitor thread */
ret = gf_thread_create (&scrub_monitor->thread, NULL, br_monitor_thread, this);
if (ret != 0) {
gf_msg (this->name, GF_LOG_ERROR, -ret,
BRB_MSG_SPAWN_FAILED, "monitor thread creation failed");
ret = -1;
goto err;
}
return 0;
err:
pthread_mutex_destroy (&scrub_monitor->mutex);
pthread_cond_destroy (&scrub_monitor->cond);
pthread_mutex_destroy (&scrub_monitor->wakelock);
pthread_cond_destroy (&scrub_monitor->wakecond);
pthread_mutex_destroy (&scrub_monitor->donelock);
pthread_cond_destroy (&scrub_monitor->donecond);
LOCK_DESTROY (&scrub_monitor->lock);
return ret;
}
int32_t
br_scrubber_init (xlator_t *this, br_private_t *priv)
{
struct br_scrubber *fsscrub = NULL;
int ret = 0;
priv->tbf = tbf_init (NULL, 0);
if (!priv->tbf)
return -1;
ret = br_scrubber_monitor_init (this, priv);
if (ret)
return -1;
fsscrub = &priv->fsscrub;
fsscrub->this = this;
fsscrub->throttle = BR_SCRUB_THROTTLE_VOID;
pthread_mutex_init (&fsscrub->mutex, NULL);
pthread_cond_init (&fsscrub->cond, NULL);
fsscrub->nr_scrubbers = 0;
INIT_LIST_HEAD (&fsscrub->scrubbers);
INIT_LIST_HEAD (&fsscrub->scrublist);
return 0;
}
|