summaryrefslogtreecommitdiffstats
path: root/xlators/debug/error-gen/src/error-gen.c
blob: 3d271b2ef216d8fe8d43be1ad0a74afe251a6d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
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
/*
   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.
*/
#include <glusterfs/xlator.h>
#include "error-gen.h"
#include <glusterfs/statedump.h>
#include <glusterfs/defaults.h>

/*
 * The user can specify an error probability as a float percentage, but we
 * store it internally as a numerator with this as the denominator.  When it's
 * used, it's like this:
 *
 *    (rand() % FAILURE_GRANULARITY) < error_rate
 *
 * To minimize rounding errors from the modulo operation, it's good for this to
 * be a power of two.
 *
 * (BTW this is just the normal case.  If "random-failure" is set, that does
 * something completely different and this number is irrelevant.  See error_gen
 * for the legacy code.)
 */
#define FAILURE_GRANULARITY (1 << 20)

sys_error_t error_no_list[] = {
    [GF_FOP_LOOKUP] = {.error_no_count = 4,
                       .error_no = {ENOENT, ENOTDIR, ENAMETOOLONG, EAGAIN}},
    [GF_FOP_STAT] = {.error_no_count = 7,
                     .error_no = {EACCES, EBADF, EFAULT, ENAMETOOLONG, ENOENT,
                                  ENOMEM, ENOTDIR}},
    [GF_FOP_READLINK] = {.error_no_count = 8,
                         .error_no = {EACCES, EFAULT, EINVAL, EIO, ENAMETOOLONG,
                                      ENOENT, ENOMEM, ENOTDIR}},
    [GF_FOP_MKNOD] = {.error_no_count = 11,
                      .error_no = {EACCES, EEXIST, EFAULT, EINVAL, ENAMETOOLONG,
                                   ENOENT, ENOMEM, ENOSPC, ENOTDIR, EPERM,
                                   EROFS}},
    [GF_FOP_MKDIR] = {.error_no_count = 10,
                      .error_no = {EACCES, EEXIST, EFAULT, ENAMETOOLONG, ENOENT,
                                   ENOMEM, ENOSPC, ENOTDIR, EPERM, EROFS}},
    [GF_FOP_UNLINK] = {.error_no_count = 10,
                       .error_no = {EACCES, EBUSY, EFAULT, EIO, EISDIR,
                                    ENAMETOOLONG, ENOENT, ENOMEM, ENOTDIR,
                                    EPERM, EROFS}},
    [GF_FOP_RMDIR] = {.error_no_count = 8,
                      .error_no = {EACCES, EBUSY, EFAULT, ENOMEM, ENOTDIR,
                                   ENOTEMPTY, EPERM, EROFS}},
    [GF_FOP_SYMLINK] = {.error_no_count = 11,
                        .error_no = {EACCES, EEXIST, EFAULT, EIO, ENAMETOOLONG,
                                     ENOENT, ENOMEM, ENOSPC, ENOTDIR, EPERM,
                                     EROFS}},
    [GF_FOP_RENAME] = {.error_no_count = 13,
                       .error_no = {EACCES, EBUSY, EFAULT, EINVAL, EISDIR,
                                    EMLINK, ENAMETOOLONG, ENOENT, ENOMEM,
                                    ENOSPC, ENOTDIR, EEXIST, EXDEV}},
    [GF_FOP_LINK] = {.error_no_count = 13,
                     .error_no = {EACCES, EFAULT, EEXIST, EIO, EMLINK,
                                  ENAMETOOLONG, ENOENT, ENOMEM, ENOSPC, ENOTDIR,
                                  EPERM, EROFS, EXDEV}},
    [GF_FOP_TRUNCATE] = {.error_no_count = 10,
                         .error_no = {EACCES, EFAULT, EFBIG, EINTR, EINVAL, EIO,
                                      EISDIR, ENAMETOOLONG, ENOENT, EISDIR}},
    [GF_FOP_CREATE] = {.error_no_count = 10,
                       .error_no = {EACCES, EEXIST, EFAULT, EISDIR, EMFILE,
                                    ENAMETOOLONG, ENFILE, ENODEV, ENOENT,
                                    ENODEV}},
    [GF_FOP_OPEN] = {.error_no_count = 10,
                     .error_no = {EACCES, EEXIST, EFAULT, EISDIR, EMFILE,
                                  ENAMETOOLONG, ENFILE, ENODEV, ENOENT,
                                  ENOMEM}},
    [GF_FOP_READ] = {.error_no_count = 5,
                     .error_no = {EINVAL, EBADF, EFAULT, EISDIR, ENAMETOOLONG}},
    [GF_FOP_WRITE] = {.error_no_count = 7,
                      .error_no = {EINVAL, EBADF, EFAULT, EISDIR, ENAMETOOLONG,
                                   ENOSPC, GF_ERROR_SHORT_WRITE}},
    [GF_FOP_STATFS] = {.error_no_count = 10,
                       .error_no = {EACCES, EBADF, EFAULT, EINTR, EIO,
                                    ENAMETOOLONG, ENOENT, ENOMEM, ENOSYS,
                                    ENOTDIR}},
    [GF_FOP_FLUSH] = {.error_no_count = 5,
                      .error_no = {EACCES, EFAULT, ENAMETOOLONG, ENOSYS,
                                   ENOENT}},
    [GF_FOP_FSYNC] = {.error_no_count = 4,
                      .error_no = {EBADF, EIO, EROFS, EINVAL}},
    [GF_FOP_SETXATTR] = {.error_no_count = 4,
                         .error_no = {EACCES, EBADF, EINTR, ENAMETOOLONG}},
    [GF_FOP_GETXATTR] = {.error_no_count = 4,
                         .error_no = {EACCES, EBADF, ENAMETOOLONG, EINTR}},
    [GF_FOP_REMOVEXATTR] = {.error_no_count = 4,
                            .error_no = {EACCES, EBADF, ENAMETOOLONG, EINTR}},
    [GF_FOP_FSETXATTR] = {.error_no_count = 4,
                          .error_no = {EACCES, EBADF, EINTR, ENAMETOOLONG}},
    [GF_FOP_FGETXATTR] = {.error_no_count = 4,
                          .error_no = {EACCES, EBADF, ENAMETOOLONG, EINTR}},
    [GF_FOP_FREMOVEXATTR] = {.error_no_count = 4,
                             .error_no = {EACCES, EBADF, ENAMETOOLONG, EINTR}},
    [GF_FOP_OPENDIR] = {.error_no_count = 8,
                        .error_no = {EACCES, EEXIST, EFAULT, EISDIR, EMFILE,
                                     ENAMETOOLONG, ENFILE, ENODEV}},
    [GF_FOP_READDIR] = {.error_no_count = 5,
                        .error_no = {EINVAL, EACCES, EBADF, EMFILE, ENOENT}},
    [GF_FOP_READDIRP] = {.error_no_count = 5,
                         .error_no = {EINVAL, EACCES, EBADF, EMFILE, ENOENT}},
    [GF_FOP_FSYNCDIR] = {.error_no_count = 4,
                         .error_no = {EBADF, EIO, EROFS, EINVAL}},
    [GF_FOP_ACCESS] = {.error_no_count = 8,
                       .error_no = {EACCES, ENAMETOOLONG, ENOENT, ENOTDIR,
                                    EROFS, EFAULT, EINVAL, EIO}},
    [GF_FOP_FTRUNCATE] = {.error_no_count = 9,
                          .error_no = {EACCES, EFAULT, EFBIG, EINTR, EINVAL,
                                       EIO, EISDIR, ENAMETOOLONG, ENOENT}},
    [GF_FOP_FSTAT] = {.error_no_count = 7,
                      .error_no = {EACCES, EBADF, EFAULT, ENAMETOOLONG, ENOENT,
                                   ENOMEM, ENOTDIR}},
    [GF_FOP_LK] = {.error_no_count = 4,
                   .error_no = {EACCES, EFAULT, ENOENT, EINTR}},
    [GF_FOP_XATTROP] = {.error_no_count = 5,
                        .error_no = {EACCES, EFAULT, ENAMETOOLONG, ENOSYS,
                                     ENOENT}},
    [GF_FOP_FXATTROP] = {.error_no_count = 4,
                         .error_no = {EBADF, EIO, EROFS, EINVAL}},
    [GF_FOP_INODELK] = {.error_no_count = 4,
                        .error_no = {EACCES, EBADF, EINTR, ENAMETOOLONG}},
    [GF_FOP_FINODELK] = {.error_no_count = 4,
                         .error_no = {EACCES, EBADF, EINTR, ENAMETOOLONG}},
    [GF_FOP_ENTRYLK] = {.error_no_count = 4,
                        .error_no = {EACCES, EBADF, ENAMETOOLONG, EINTR}},
    [GF_FOP_FENTRYLK] = {.error_no_count = 10,
                         .error_no = {EACCES, EEXIST, EFAULT, EISDIR, EMFILE,
                                      ENAMETOOLONG, ENFILE, ENODEV, ENOENT,
                                      ENOMEM}},
    [GF_FOP_SETATTR] = {.error_no_count = 11,
                        .error_no = {EACCES, EFAULT, EIO, ENAMETOOLONG, ENOENT,
                                     ENOMEM, ENOTDIR, EPERM, EROFS, EBADF,
                                     EIO}},
    [GF_FOP_FSETATTR] = {.error_no_count = 11,
                         .error_no = {EACCES, EFAULT, EIO, ENAMETOOLONG, ENOENT,
                                      ENOMEM, ENOTDIR, EPERM, EROFS, EBADF,
                                      EIO}},
    [GF_FOP_GETSPEC] = {.error_no_count = 4,
                        .error_no = {EACCES, EBADF, ENAMETOOLONG, EINTR}}};

int
generate_rand_no(int op_no)
{
    int rand_no = 0;
    int error_no_list_size = 0;

    error_no_list_size = sizeof(error_no_list) / sizeof(error_no_list[0]);

    if (op_no < error_no_list_size)
        /* coverity[DC.WEAK_CRYPTO] */
        rand_no = rand() % error_no_list[op_no].error_no_count;
    return rand_no;
}

int
conv_errno_to_int(char **error_no)
{
    if (!strcmp((*error_no), "ENOENT"))
        return ENOENT;
    else if (!strcmp((*error_no), "ENOTDIR"))
        return ENOTDIR;
    else if (!strcmp((*error_no), "ENAMETOOLONG"))
        return ENAMETOOLONG;
    else if (!strcmp((*error_no), "EACCES"))
        return EACCES;
    else if (!strcmp((*error_no), "EBADF"))
        return EBADF;
    else if (!strcmp((*error_no), "EFAULT"))
        return EFAULT;
    else if (!strcmp((*error_no), "ENOMEM"))
        return ENOMEM;
    else if (!strcmp((*error_no), "EINVAL"))
        return EINVAL;
    else if (!strcmp((*error_no), "EIO"))
        return EIO;
    else if (!strcmp((*error_no), "EEXIST"))
        return EEXIST;
    else if (!strcmp((*error_no), "ENOSPC"))
        return ENOSPC;
    else if (!strcmp((*error_no), "EPERM"))
        return EPERM;
    else if (!strcmp((*error_no), "EROFS"))
        return EROFS;
    else if (!strcmp((*error_no), "EBUSY"))
        return EBUSY;
    else if (!strcmp((*error_no), "EISDIR"))
        return EISDIR;
    else if (!strcmp((*error_no), "ENOTEMPTY"))
        return ENOTEMPTY;
    else if (!strcmp((*error_no), "EMLINK"))
        return EMLINK;
    else if (!strcmp((*error_no), "ENODEV"))
        return ENODEV;
    else if (!strcmp((*error_no), "EXDEV"))
        return EXDEV;
    else if (!strcmp((*error_no), "EMFILE"))
        return EMFILE;
    else if (!strcmp((*error_no), "ENFILE"))
        return ENFILE;
    else if (!strcmp((*error_no), "ENOSYS"))
        return ENOSYS;
    else if (!strcmp((*error_no), "EINTR"))
        return EINTR;
    else if (!strcmp((*error_no), "EFBIG"))
        return EFBIG;
    else if (!strcmp((*error_no), "GF_ERROR_SHORT_WRITE"))
        return GF_ERROR_SHORT_WRITE;
    else
        return EAGAIN;
}

int
error_gen(xlator_t *this, int op_no)
{
    eg_t *egp = NULL;
    int count = 0;
    int error_no_int = 0;
    int rand_no = 0;
    int ret = 0;
    gf_boolean_t should_err = _gf_false;
    int error_no_list_size = 0;

    egp = this->private;

    if (egp->random_failure) {
        /*
         * I honestly don't know why anyone would use this "feature"
         * but I'll try to preserve its functionality anyway.  Without
         * locking twice to update failure_iter_no and egp->op_count
         * separately, then not locking at all to update
         * egp->failure_iter_no.  That's not needed for compatibility,
         * and it's abhorrently wrong.  I have *some* standards.
         */
        LOCK(&egp->lock);
        {
            count = ++(egp->op_count);
            error_no_int = egp->error_no_int;
            if ((count % egp->failure_iter_no) == 0) {
                egp->op_count = 0;
                /* coverity[DC.WEAK_CRYPTO] */
                egp->failure_iter_no = 3 + (rand() % GF_UNIVERSAL_ANSWER);
                should_err = _gf_true;
            }
        }
        UNLOCK(&egp->lock);
    } else {
        /*
         * It turns out that rand() is almost universally implemented
         * as a linear congruential PRNG, which is about as cheap as
         * it gets.  This gets us real random behavior, including
         * phenomena like streaks and dry spells, with controllable
         * long-term probability, cheaply.
         */
        if ((rand() % FAILURE_GRANULARITY) < egp->failure_iter_no) {
            should_err = _gf_true;
        }
    }

    error_no_list_size = sizeof(error_no_list) / sizeof(error_no_list[0]);
    if (should_err) {
        if (error_no_int)
            ret = error_no_int;
        else {
            rand_no = generate_rand_no(op_no);
            if (op_no >= error_no_list_size)
                op_no = 0;
            if (rand_no >= error_no_list[op_no].error_no_count)
                rand_no = 0;
            ret = error_no_list[op_no].error_no[rand_no];
        }
    }

    return ret;
}

int
error_gen_lookup(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_LOOKUP];

    if (enable)
        op_errno = error_gen(this, GF_FOP_LOOKUP);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(lookup, frame, -1, op_errno, NULL, NULL, NULL,
                            NULL);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->lookup,
                    loc, xdata);
    return 0;
}

int
error_gen_stat(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_STAT];

    if (enable)
        op_errno = error_gen(this, GF_FOP_STAT);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(stat, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->stat,
                    loc, xdata);
    return 0;
}

int
error_gen_setattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
                  struct iatt *stbuf, int32_t valid, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_SETATTR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_SETATTR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(setattr, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->setattr,
                    loc, stbuf, valid, xdata);
    return 0;
}

int
error_gen_fsetattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
                   struct iatt *stbuf, int32_t valid, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FSETATTR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FSETATTR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fsetattr, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->fsetattr,
                    fd, stbuf, valid, xdata);
    return 0;
}

int
error_gen_truncate(call_frame_t *frame, xlator_t *this, loc_t *loc,
                   off_t offset, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_TRUNCATE];

    if (enable)
        op_errno = error_gen(this, GF_FOP_TRUNCATE);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(truncate, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->truncate,
                    loc, offset, xdata);
    return 0;
}

int
error_gen_ftruncate(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
                    dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FTRUNCATE];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FTRUNCATE);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(ftruncate, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->ftruncate, fd, offset, xdata);
    return 0;
}

int
error_gen_access(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t mask,
                 dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_ACCESS];

    if (enable)
        op_errno = error_gen(this, GF_FOP_ACCESS);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(access, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->access,
                    loc, mask, xdata);
    return 0;
}

int
error_gen_readlink(call_frame_t *frame, xlator_t *this, loc_t *loc, size_t size,
                   dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_READLINK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_READLINK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(readlink, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->readlink,
                    loc, size, xdata);
    return 0;
}

int
error_gen_mknod(call_frame_t *frame, xlator_t *this, loc_t *loc, mode_t mode,
                dev_t rdev, mode_t umask, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_MKNOD];

    if (enable)
        op_errno = error_gen(this, GF_FOP_MKNOD);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(mknod, frame, -1, op_errno, NULL, NULL, NULL, NULL,
                            xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->mknod,
                    loc, mode, rdev, umask, xdata);
    return 0;
}

int
error_gen_mkdir(call_frame_t *frame, xlator_t *this, loc_t *loc, mode_t mode,
                mode_t umask, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_MKDIR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_MKDIR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(mkdir, frame, -1, op_errno, NULL, NULL, NULL, NULL,
                            xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->mkdir,
                    loc, mode, umask, xdata);
    return 0;
}

int
error_gen_unlink(call_frame_t *frame, xlator_t *this, loc_t *loc, int xflag,
                 dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_UNLINK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_UNLINK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(unlink, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->unlink,
                    loc, xflag, xdata);
    return 0;
}

int
error_gen_rmdir(call_frame_t *frame, xlator_t *this, loc_t *loc, int flags,
                dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_RMDIR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_RMDIR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(rmdir, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->rmdir,
                    loc, flags, xdata);
    return 0;
}

int
error_gen_symlink(call_frame_t *frame, xlator_t *this, const char *linkpath,
                  loc_t *loc, mode_t umask, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_SYMLINK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_SYMLINK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(symlink, frame, -1, op_errno, NULL, NULL, NULL,
                            NULL, NULL); /* pre & post parent attr */
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->symlink,
                    linkpath, loc, umask, xdata);
    return 0;
}

int
error_gen_rename(call_frame_t *frame, xlator_t *this, loc_t *oldloc,
                 loc_t *newloc, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_RENAME];

    if (enable)
        op_errno = error_gen(this, GF_FOP_RENAME);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(rename, frame, -1, op_errno, NULL, NULL, NULL, NULL,
                            NULL, NULL);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->rename,
                    oldloc, newloc, xdata);
    return 0;
}

int
error_gen_link(call_frame_t *frame, xlator_t *this, loc_t *oldloc,
               loc_t *newloc, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_LINK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_LINK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(link, frame, -1, op_errno, NULL, NULL, NULL, NULL,
                            NULL);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->link,
                    oldloc, newloc, xdata);
    return 0;
}

int
error_gen_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
                 mode_t mode, mode_t umask, fd_t *fd, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_CREATE];

    if (enable)
        op_errno = error_gen(this, GF_FOP_CREATE);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(create, frame, -1, op_errno, NULL, NULL, NULL, NULL,
                            NULL, NULL);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->create,
                    loc, flags, mode, umask, fd, xdata);
    return 0;
}

int
error_gen_open(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
               fd_t *fd, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_OPEN];

    if (enable)
        op_errno = error_gen(this, GF_FOP_OPEN);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(open, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->open,
                    loc, flags, fd, xdata);
    return 0;
}

int
error_gen_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
                off_t offset, uint32_t flags, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_READ];

    if (enable)
        op_errno = error_gen(this, GF_FOP_READ);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(readv, frame, -1, op_errno, NULL, 0, NULL, NULL,
                            xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->readv,
                    fd, size, offset, flags, xdata);
    return 0;
}

int
error_gen_writev(call_frame_t *frame, xlator_t *this, fd_t *fd,
                 struct iovec *vector, int32_t count, off_t off, uint32_t flags,
                 struct iobref *iobref, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;
    struct iovec *shortvec = NULL;

    egp = this->private;
    enable = egp->enable[GF_FOP_WRITE];

    if (enable)
        op_errno = error_gen(this, GF_FOP_WRITE);

    if (op_errno == GF_ERROR_SHORT_WRITE) {
        /*
         * A short write error returns some value less than what was
         * requested from a write. To simulate this, replace the vector
         * with one half the size;
         */
        shortvec = iov_dup(vector, 1);
        shortvec->iov_len /= 2;
        goto wind;
    } else if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(writev, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }
wind:
    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->writev,
                    fd, shortvec ? shortvec : vector, count, off, flags, iobref,
                    xdata);

    if (shortvec)
        GF_FREE(shortvec);
    return 0;
}

int
error_gen_flush(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FLUSH];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FLUSH);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(flush, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->flush,
                    fd, xdata);
    return 0;
}

int
error_gen_fsync(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
                dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FSYNC];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FSYNC);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fsync, frame, -1, op_errno, NULL, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->fsync,
                    fd, flags, xdata);
    return 0;
}

int
error_gen_fstat(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FSTAT];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FSTAT);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fstat, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->fstat,
                    fd, xdata);
    return 0;
}

int
error_gen_opendir(call_frame_t *frame, xlator_t *this, loc_t *loc, fd_t *fd,
                  dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_OPENDIR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_OPENDIR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(opendir, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->opendir,
                    loc, fd, xdata);
    return 0;
}

int
error_gen_fsyncdir(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
                   dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FSYNCDIR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FSYNCDIR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fsyncdir, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->fsyncdir,
                    fd, flags, xdata);
    return 0;
}

int
error_gen_statfs(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_STATFS];

    if (enable)
        op_errno = error_gen(this, GF_FOP_STATFS);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(statfs, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->statfs,
                    loc, xdata);
    return 0;
}

int
error_gen_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
                   dict_t *dict, int32_t flags, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_SETXATTR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_SETXATTR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(setxattr, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->setxattr,
                    loc, dict, flags, xdata);
    return 0;
}

int
error_gen_getxattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
                   const char *name, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_GETXATTR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_GETXATTR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(getxattr, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->getxattr,
                    loc, name, xdata);
    return 0;
}

int
error_gen_fsetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *dict,
                    int32_t flags, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FSETXATTR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FSETXATTR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fsetxattr, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->fsetxattr, fd, dict, flags, xdata);
    return 0;
}

int
error_gen_fgetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
                    const char *name, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FGETXATTR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FGETXATTR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fgetxattr, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->fgetxattr, fd, name, xdata);
    return 0;
}

int
error_gen_xattrop(call_frame_t *frame, xlator_t *this, loc_t *loc,
                  gf_xattrop_flags_t flags, dict_t *dict, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_XATTROP];

    if (enable)
        op_errno = error_gen(this, GF_FOP_XATTROP);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(xattrop, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->xattrop,
                    loc, flags, dict, xdata);
    return 0;
}

int
error_gen_fxattrop(call_frame_t *frame, xlator_t *this, fd_t *fd,
                   gf_xattrop_flags_t flags, dict_t *dict, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FXATTROP];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FXATTROP);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fxattrop, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->fxattrop,
                    fd, flags, dict, xdata);
    return 0;
}

int
error_gen_removexattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
                      const char *name, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_REMOVEXATTR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_REMOVEXATTR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(removexattr, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->removexattr, loc, name, xdata);
    return 0;
}

int
error_gen_fremovexattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
                       const char *name, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FREMOVEXATTR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FREMOVEXATTR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fremovexattr, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->fremovexattr, fd, name, xdata);
    return 0;
}

int
error_gen_lk(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t cmd,
             struct gf_flock *lock, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_LK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_LK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(lk, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->lk, fd,
                    cmd, lock, xdata);
    return 0;
}

int
error_gen_inodelk(call_frame_t *frame, xlator_t *this, const char *volume,
                  loc_t *loc, int32_t cmd, struct gf_flock *lock, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_INODELK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_INODELK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(inodelk, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->inodelk,
                    volume, loc, cmd, lock, xdata);
    return 0;
}

int
error_gen_finodelk(call_frame_t *frame, xlator_t *this, const char *volume,
                   fd_t *fd, int32_t cmd, struct gf_flock *lock, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FINODELK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FINODELK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(finodelk, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->finodelk,
                    volume, fd, cmd, lock, xdata);
    return 0;
}

int
error_gen_entrylk(call_frame_t *frame, xlator_t *this, const char *volume,
                  loc_t *loc, const char *basename, entrylk_cmd cmd,
                  entrylk_type type, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_ENTRYLK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_ENTRYLK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(entrylk, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->entrylk,
                    volume, loc, basename, cmd, type, xdata);
    return 0;
}

int
error_gen_fentrylk(call_frame_t *frame, xlator_t *this, const char *volume,
                   fd_t *fd, const char *basename, entrylk_cmd cmd,
                   entrylk_type type, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_FENTRYLK];

    if (enable)
        op_errno = error_gen(this, GF_FOP_FENTRYLK);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(fentrylk, frame, -1, op_errno, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->fentrylk,
                    volume, fd, basename, cmd, type, xdata);
    return 0;
}

int
error_gen_getspec(call_frame_t *frame, xlator_t *this, const char *key,
                  int32_t flags)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_GETSPEC];

    if (enable)
        op_errno = error_gen(this, GF_FOP_GETSPEC);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(getspec, frame, -1, op_errno, NULL);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->getspec,
                    key, flags);
    return 0;
}

int
error_gen_readdir(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
                  off_t off, dict_t *xdata)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_READDIR];

    if (enable)
        op_errno = error_gen(this, GF_FOP_READDIR);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(readdir, frame, -1, op_errno, NULL, xdata);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->readdir,
                    fd, size, off, xdata);
    return 0;
}

int
error_gen_readdirp(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
                   off_t off, dict_t *dict)
{
    int op_errno = 0;
    eg_t *egp = NULL;
    int enable = 1;

    egp = this->private;
    enable = egp->enable[GF_FOP_READDIRP];

    if (enable)
        op_errno = error_gen(this, GF_FOP_READDIRP);

    if (op_errno) {
        GF_ERROR(this, "unwind(-1, %s)", strerror(op_errno));
        STACK_UNWIND_STRICT(readdirp, frame, -1, op_errno, NULL, NULL);
        return 0;
    }

    STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->readdirp,
                    fd, size, off, dict);
    return 0;
}

static void
error_gen_set_failure(eg_t *pvt, double percent)
{
    double ppm;

    GF_ASSERT(pvt);

    ppm = (percent / 100.0) * (double)FAILURE_GRANULARITY;
    pvt->failure_iter_no = (int)ppm;
}

static void
error_gen_parse_fill_fops(eg_t *pvt, char *enable_fops)
{
    char *op_no_str = NULL;
    int op_no = -1;
    int i = 0;
    xlator_t *this = THIS;
    char *saveptr = NULL;

    GF_ASSERT(pvt);
    GF_ASSERT(this);

    for (i = 0; i < GF_FOP_MAXVALUE; i++)
        pvt->enable[i] = 0;

    if (!enable_fops) {
        gf_log(this->name, GF_LOG_WARNING, "All fops are enabled.");
        for (i = 0; i < GF_FOP_MAXVALUE; i++)
            pvt->enable[i] = 1;
    } else {
        op_no_str = strtok_r(enable_fops, ",", &saveptr);
        while (op_no_str) {
            op_no = gf_fop_int(op_no_str);
            if (op_no == -1) {
                gf_log(this->name, GF_LOG_WARNING, "Wrong option value %s",
                       op_no_str);
            } else
                pvt->enable[op_no] = 1;

            op_no_str = strtok_r(NULL, ",", &saveptr);
        }
    }
}

int32_t
error_gen_priv_dump(xlator_t *this)
{
    char key_prefix[GF_DUMP_MAX_BUF_LEN];
    int ret = -1;
    eg_t *conf = NULL;

    if (!this)
        goto out;

    conf = this->private;
    if (!conf)
        goto out;

    ret = TRY_LOCK(&conf->lock);
    if (ret != 0) {
        return ret;
    }

    gf_proc_dump_add_section("xlator.debug.error-gen.%s.priv", this->name);
    gf_proc_dump_build_key(key_prefix, "xlator.debug.error-gen", "%s.priv",
                           this->name);

    gf_proc_dump_write("op_count", "%d", conf->op_count);
    gf_proc_dump_write("failure_iter_no", "%d", conf->failure_iter_no);
    gf_proc_dump_write("error_no", "%s", conf->error_no);
    gf_proc_dump_write("random_failure", "%d", conf->random_failure);

    UNLOCK(&conf->lock);
out:
    return ret;
}

int32_t
mem_acct_init(xlator_t *this)
{
    int ret = -1;

    if (!this)
        return ret;

    ret = xlator_mem_acct_init(this, gf_error_gen_mt_end + 1);

    if (ret != 0) {
        gf_log(this->name, GF_LOG_ERROR,
               "Memory accounting init"
               " failed");
        return ret;
    }

    return ret;
}

int
reconfigure(xlator_t *this, dict_t *options)
{
    eg_t *pvt = NULL;
    int32_t ret = 0;
    char *error_enable_fops = NULL;
    double failure_percent_dbl = 0.0;

    if (!this || !this->private)
        goto out;

    pvt = this->private;

    ret = -1;

    GF_OPTION_RECONF("error-no", pvt->error_no, options, str, out);

    if (pvt->error_no)
        pvt->error_no_int = conv_errno_to_int(&pvt->error_no);

    GF_OPTION_RECONF("failure", failure_percent_dbl, options, percent, out);

    GF_OPTION_RECONF("enable", error_enable_fops, options, str, out);

    GF_OPTION_RECONF("random-failure", pvt->random_failure, options, bool, out);

    error_gen_parse_fill_fops(pvt, error_enable_fops);
    error_gen_set_failure(pvt, failure_percent_dbl);

    ret = 0;
out:
    gf_log(this ? this->name : "error-gen", GF_LOG_DEBUG,
           "reconfigure returning %d", ret);
    return ret;
}

int
init(xlator_t *this)
{
    eg_t *pvt = NULL;
    int32_t ret = 0;
    char *error_enable_fops = NULL;
    double failure_percent_dbl = 0.0;

    if (!this->children || this->children->next) {
        gf_log(this->name, GF_LOG_ERROR,
               "error-gen not configured with one subvolume");
        ret = -1;
        goto out;
    }

    if (!this->parents) {
        gf_log(this->name, GF_LOG_WARNING, "dangling volume. check volfile ");
    }

    pvt = GF_CALLOC(1, sizeof(eg_t), gf_error_gen_mt_eg_t);

    if (!pvt) {
        ret = -1;
        goto out;
    }

    LOCK_INIT(&pvt->lock);

    ret = -1;

    GF_OPTION_INIT("error-no", pvt->error_no, str, out);

    if (pvt->error_no)
        pvt->error_no_int = conv_errno_to_int(&pvt->error_no);

    GF_OPTION_INIT("failure", failure_percent_dbl, percent, out);

    GF_OPTION_INIT("enable", error_enable_fops, str, out);

    GF_OPTION_INIT("random-failure", pvt->random_failure, bool, out);

    error_gen_parse_fill_fops(pvt, error_enable_fops);
    error_gen_set_failure(pvt, failure_percent_dbl);

    this->private = pvt;

    /* Give some seed value here */
    srand(time(NULL));

    ret = 0;
out:
    if (ret)
        GF_FREE(pvt);
    return ret;
}

void
fini(xlator_t *this)
{
    eg_t *pvt = NULL;

    if (!this)
        return;
    pvt = this->private;

    if (pvt) {
        LOCK_DESTROY(&pvt->lock);
        GF_FREE(pvt);
        gf_log(this->name, GF_LOG_DEBUG, "fini called");
    }
    return;
}

struct xlator_dumpops dumpops = {
    .priv = error_gen_priv_dump,
};

struct xlator_cbks cbks;

struct xlator_fops fops = {
    .lookup = error_gen_lookup,
    .stat = error_gen_stat,
    .readlink = error_gen_readlink,
    .mknod = error_gen_mknod,
    .mkdir = error_gen_mkdir,
    .unlink = error_gen_unlink,
    .rmdir = error_gen_rmdir,
    .symlink = error_gen_symlink,
    .rename = error_gen_rename,
    .link = error_gen_link,
    .truncate = error_gen_truncate,
    .create = error_gen_create,
    .open = error_gen_open,
    .readv = error_gen_readv,
    .writev = error_gen_writev,
    .statfs = error_gen_statfs,
    .flush = error_gen_flush,
    .fsync = error_gen_fsync,
    .setxattr = error_gen_setxattr,
    .getxattr = error_gen_getxattr,
    .removexattr = error_gen_removexattr,
    .fsetxattr = error_gen_fsetxattr,
    .fgetxattr = error_gen_fgetxattr,
    .fremovexattr = error_gen_fremovexattr,
    .opendir = error_gen_opendir,
    .readdir = error_gen_readdir,
    .readdirp = error_gen_readdirp,
    .fsyncdir = error_gen_fsyncdir,
    .access = error_gen_access,
    .ftruncate = error_gen_ftruncate,
    .fstat = error_gen_fstat,
    .lk = error_gen_lk,
    .xattrop = error_gen_xattrop,
    .fxattrop = error_gen_fxattrop,
    .inodelk = error_gen_inodelk,
    .finodelk = error_gen_finodelk,
    .entrylk = error_gen_entrylk,
    .fentrylk = error_gen_fentrylk,
    .setattr = error_gen_setattr,
    .fsetattr = error_gen_fsetattr,
    .getspec = error_gen_getspec,
};

struct volume_options options[] = {
    {
        .key = {"failure"},
        .type = GF_OPTION_TYPE_PERCENT,
        .description = "Percentage failure of operations when enabled.",
    },

    {
        .key = {"error-no"},
        .value = {"ENOENT",
                  "ENOTDIR",
                  "ENAMETOOLONG",
                  "EACCES",
                  "EBADF",
                  "EFAULT",
                  "ENOMEM",
                  "EINVAL",
                  "EIO",
                  "EEXIST",
                  "ENOSPC",
                  "EPERM",
                  "EROFS",
                  "EBUSY",
                  "EISDIR",
                  "ENOTEMPTY",
                  "EMLINK"
                  "ENODEV",
                  "EXDEV",
                  "EMFILE",
                  "ENFILE",
                  "ENOSYS",
                  "EINTR",
                  "EFBIG",
                  "EAGAIN",
                  "GF_ERROR_SHORT_WRITE"},
        .type = GF_OPTION_TYPE_STR,
        .op_version = {3},
        .tags = {"error-gen"},
        .flags = OPT_FLAG_SETTABLE,

    },

    {
        .key = {"random-failure"},
        .type = GF_OPTION_TYPE_BOOL,
        .default_value = "off",
        .op_version = {3},
        .tags = {"error-gen"},
        .flags = OPT_FLAG_SETTABLE,
    },

    {
        .key = {"enable", "error-fops"},
        .type = GF_OPTION_TYPE_STR,
        .description = "Accepts a string which takes ',' separated fop "
                       "strings to denote which fops are enabled for error",
        .op_version = {3},
        .tags = {"error-gen"},
        .flags = OPT_FLAG_SETTABLE,
    },

    {.key = {NULL}}};