summaryrefslogtreecommitdiffstats
path: root/xlators/mgmt/glusterd/src/glusterd-handshake.c
blob: a6cdbcef82c72d238a84da4b77bd4cea210e3d55 (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
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
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
/*
   Copyright (c) 2010-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 <glusterfs/defaults.h>
#include <glusterfs/glusterfs.h>
#include <glusterfs/syscall.h>
#include <glusterfs/compat-errno.h>

#include "glusterd.h"
#include "glusterd-utils.h"
#include "glusterd-op-sm.h"
#include "glusterd-store.h"
#include "glusterd-snapshot-utils.h"
#include "glusterd-svc-mgmt.h"
#include "glusterd-snapd-svc-helper.h"
#include "glusterd-volgen.h"
#include "glusterd-quotad-svc.h"
#include "glusterd-messages.h"
#include "glusterfs3.h"
#include "protocol-common.h"
#include "rpcsvc.h"
#include "rpc-common-xdr.h"
#include "glusterd-gfproxyd-svc-helper.h"
#include "glusterd-shd-svc-helper.h"

extern struct rpc_clnt_program gd_peer_prog;
extern struct rpc_clnt_program gd_mgmt_prog;
extern struct rpc_clnt_program gd_mgmt_v3_prog;

#define TRUSTED_PREFIX "trusted-"
#define GD_PEER_ID_KEY "peer-id"

typedef ssize_t (*gfs_serialize_t)(struct iovec outmsg, void *data);

static int
get_snap_volname_and_volinfo(const char *volpath, char **volname,
                             glusterd_volinfo_t **volinfo)
{
    int ret = -1;
    char *save_ptr = NULL;
    char *str_token = NULL;
    char *snapname = NULL;
    char *volname_token = NULL;
    char *vol = NULL;
    glusterd_snap_t *snap = NULL;
    xlator_t *this = NULL;
    char *tmp_str_token = NULL;
    char *volfile_token = NULL;

    this = THIS;
    GF_ASSERT(this);
    GF_ASSERT(volpath);
    GF_ASSERT(volinfo);

    str_token = gf_strdup(volpath);
    if (NULL == str_token) {
        goto out;
    }

    tmp_str_token = str_token;

    /* Input volname will have below formats:
     * /snaps/<snapname>/<volname>.<hostname>
     * or
     * /snaps/<snapname>/<parent-volname>
     * We need to extract snapname and parent_volname */

    /*split string by "/" */
    strtok_r(str_token, "/", &save_ptr);
    snapname = strtok_r(NULL, "/", &save_ptr);
    if (!snapname) {
        gf_msg(this->name, GF_LOG_ERROR, EINVAL, GD_MSG_INVALID_ENTRY,
               "Invalid path: %s", volpath);
        goto out;
    }

    volname_token = strtok_r(NULL, "/", &save_ptr);
    if (!volname_token) {
        gf_msg(this->name, GF_LOG_ERROR, EINVAL, GD_MSG_INVALID_ENTRY,
               "Invalid path: %s", volpath);
        goto out;
    }

    snap = glusterd_find_snap_by_name(snapname);
    if (!snap) {
        gf_msg(this->name, GF_LOG_ERROR, EINVAL, GD_MSG_SNAP_NOT_FOUND,
               "Failed to "
               "fetch snap %s",
               snapname);
        goto out;
    }

    /* Find if its a parent volume name or snap volume
     * name. This function will succeed if volname_token
     * is a parent volname
     */
    ret = glusterd_volinfo_find(volname_token, volinfo);
    if (ret) {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_VOLINFO_GET_FAIL,
               "failed to get the volinfo for the volume %s", volname_token);

        /* Get the actual volfile name. */
        volfile_token = strtok_r(NULL, "/", &save_ptr);
        *volname = gf_strdup(volfile_token);
        if (NULL == *volname) {
            ret = -1;
            goto out;
        }

        /*
         * Ideally, this should succeed as volname_token now contains
         * the name of the snap volume (i.e. name of the volume that
         * represents the snapshot). But, if for some reason, volinfo
         * for the snap volume is not found, then try to get from the
         * name of the volfile. Name of the volfile is like this.
         * <snap volume name>.<hostname>.<brick path>.vol
         */
        ret = glusterd_snap_volinfo_find(volname_token, snap, volinfo);
        if (ret) {
            /* Split the volume name */
            vol = strtok_r(volfile_token, ".", &save_ptr);
            if (!vol) {
                gf_msg(this->name, GF_LOG_ERROR, EINVAL, GD_MSG_INVALID_ENTRY,
                       "Invalid "
                       "volname (%s)",
                       volfile_token);
                goto out;
            }

            ret = glusterd_snap_volinfo_find(vol, snap, volinfo);
            if (ret) {
                gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAP_INFO_FAIL,
                       "Failed to "
                       "fetch snap volume from volname (%s)",
                       vol);
                goto out;
            }
        }
    } else {
        /*volname_token is parent volname*/
        ret = glusterd_snap_volinfo_find_from_parent_volname(volname_token,
                                                             snap, volinfo);
        if (ret) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAP_INFO_FAIL,
                   "Failed to "
                   "fetch snap volume from parent "
                   "volname (%s)",
                   volname_token);
            goto out;
        }

        /* Since volname_token is a parent volname we should
         * get the snap volname here*/
        *volname = gf_strdup((*volinfo)->volname);
        if (NULL == *volname) {
            ret = -1;
            goto out;
        }
    }

out:
    if (ret && NULL != *volname) {
        GF_FREE(*volname);
        *volname = NULL;
    }

    if (tmp_str_token)
        GF_FREE(tmp_str_token);
    return ret;
}

int32_t
glusterd_get_client_per_brick_volfile(glusterd_volinfo_t *volinfo,
                                      char *filename, char *path, int path_len)
{
    char workdir[PATH_MAX] = {
        0,
    };
    glusterd_conf_t *priv = NULL;
    int32_t ret = -1;

    GF_VALIDATE_OR_GOTO("glusterd", THIS, out);
    priv = THIS->private;
    GF_VALIDATE_OR_GOTO(THIS->name, priv, out);

    GLUSTERD_GET_VOLUME_DIR(workdir, volinfo, priv);

    snprintf(path, path_len, "%s/%s", workdir, filename);

    ret = 0;
out:
    return ret;
}

size_t
build_volfile_path(char *volume_id, char *path, size_t path_len,
                   char *trusted_str, dict_t *dict)
{
    struct stat stbuf = {
        0,
    };
    int32_t ret = -1;
    char *vol = NULL;
    char *dup_volname = NULL;
    char *save_ptr = NULL;
    char *free_ptr = NULL;
    char *volname = NULL;
    char *volid_ptr = NULL;
    char dup_volid[PATH_MAX] = {
        0,
    };
    char path_prefix[PATH_MAX] = {
        0,
    };
    xlator_t *this = NULL;
    glusterd_volinfo_t *volinfo = NULL;
    glusterd_conf_t *priv = NULL;
    int32_t len = 0;

    this = THIS;
    GF_ASSERT(this);
    priv = this->private;
    GF_ASSERT(priv);
    GF_ASSERT(volume_id);
    GF_ASSERT(path);

    volid_ptr = strstr(volume_id, "snapd/");
    if (volid_ptr) {
        volid_ptr = strchr(volid_ptr, '/');
        if (!volid_ptr) {
            ret = -1;
            goto out;
        }
        volid_ptr++;

        ret = glusterd_volinfo_find(volid_ptr, &volinfo);
        if (ret == -1) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLINFO_GET_FAIL,
                   "Couldn't find volinfo");
            goto out;
        }
        glusterd_svc_build_snapd_volfile(volinfo, path, path_len);
        ret = 0;
        goto out;
    }

    volid_ptr = strstr(volume_id, "gluster/");
    if (volid_ptr) {
        volid_ptr = strchr(volid_ptr, '/');
        if (!volid_ptr) {
            ret = -1;
            goto out;
        }
        volid_ptr++;

        glusterd_svc_build_volfile_path(volid_ptr, priv->workdir, path,
                                        path_len);
        ret = 0;
        goto out;
    }

    volid_ptr = strstr(volume_id, "gfproxy-client/");
    if (volid_ptr) {
        volid_ptr = strchr(volid_ptr, '/');
        if (!volid_ptr) {
            ret = -1;
            goto out;
        }
        volid_ptr++;

        ret = glusterd_volinfo_find(volid_ptr, &volinfo);
        if (ret == -1) {
            gf_log(this->name, GF_LOG_ERROR, "Couldn't find volinfo");
            goto out;
        }

        glusterd_get_gfproxy_client_volfile(volinfo, path, path_len);

        ret = 0;
        goto out;
    }

    volid_ptr = strstr(volume_id, "gfproxyd/");
    if (volid_ptr) {
        volid_ptr = strchr(volid_ptr, '/');
        if (!volid_ptr) {
            ret = -1;
            goto out;
        }
        volid_ptr++;

        ret = glusterd_volinfo_find(volid_ptr, &volinfo);
        if (ret == -1) {
            gf_log(this->name, GF_LOG_ERROR, "Couldn't find volinfo");
            goto out;
        }

        glusterd_svc_build_gfproxyd_volfile_path(volinfo, path, path_len);
        ret = 0;
        goto out;
    }

    volid_ptr = strstr(volume_id, "shd/");
    if (volid_ptr) {
        volid_ptr = strchr(volid_ptr, '/');
        if (!volid_ptr) {
            ret = -1;
            goto out;
        }
        volid_ptr++;

        ret = glusterd_volinfo_find(volid_ptr, &volinfo);
        if (ret == -1) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLINFO_GET_FAIL,
                   "Couldn't find volinfo for volid=%s", volid_ptr);
            goto out;
        }

        glusterd_svc_build_shd_volfile_path(volinfo, path, path_len);

        ret = glusterd_svc_set_shd_pidfile(volinfo, dict);
        if (ret == -1) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_SET_FAILED,
                   "Couldn't set pidfile in dict for volid=%s", volid_ptr);
            goto out;
        }
        ret = 0;
        goto out;
    }

    volid_ptr = strstr(volume_id, "/snaps/");
    if (volid_ptr) {
        ret = get_snap_volname_and_volinfo(volid_ptr, &volname, &volinfo);
        if (ret) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAP_INFO_FAIL,
                   "Failed to get snap"
                   " volinfo from path (%s)",
                   volume_id);
            ret = -1;
            goto out;
        }

        len = snprintf(path_prefix, sizeof(path_prefix), "%s/snaps/%s",
                       priv->workdir, volinfo->snapshot->snapname);
        volid_ptr = volname;
        /* this is to ensure that volname recvd from
           get_snap_volname_and_volinfo is free'd */
        free_ptr = volname;
        if ((len < 0) || (len >= sizeof(path_prefix))) {
            ret = -1;
            goto out;
        }

        goto gotvolinfo;
    }

    volid_ptr = strstr(volume_id, "rebalance/");
    if (volid_ptr) {
        volid_ptr = strchr(volid_ptr, '/');
        if (!volid_ptr) {
            ret = -1;
            goto out;
        }
        volid_ptr++;

        ret = glusterd_volinfo_find(volid_ptr, &volinfo);
        if (ret == -1) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLINFO_GET_FAIL,
                   "Couldn't find volinfo");
            goto out;
        }
        glusterd_get_rebalance_volfile(volinfo, path, path_len);
        ret = 0;
        goto out;
    }

    volid_ptr = strstr(volume_id, "client_per_brick/");
    if (volid_ptr) {
        volid_ptr = strchr(volid_ptr, '/');
        if (!volid_ptr) {
            ret = -1;
            goto out;
        }
        volid_ptr++;

        dup_volname = gf_strdup(volid_ptr);
        if (!dup_volname) {
            gf_msg(this->name, GF_LOG_ERROR, ENOMEM, GD_MSG_NO_MEMORY,
                   "strdup failed");
            ret = -1;
            goto out;
        }

        /* Split the volume name */
        vol = strtok_r(dup_volname, ".", &save_ptr);
        if (!vol) {
            ret = -1;
            goto out;
        }
        ret = glusterd_volinfo_find(vol, &volinfo);
        if (ret == -1) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLINFO_GET_FAIL,
                   "Couldn't find volinfo");
            goto out;
        }
        ret = glusterd_get_client_per_brick_volfile(volinfo, volid_ptr, path,
                                                    path_len);
        if (ret < 0) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_NO_MEMORY,
                   "failed to get volinfo path");
            goto out;
        }

        ret = sys_access(path, F_OK);
        goto out;
    }

    if (volume_id[0] == '/') {
        /* Normal behavior */
        volid_ptr = volume_id;
        volid_ptr++;

    } else {
        /* Bringing in NFS like behavior for mount command, */
        /* With this, one can mount a volume with below cmd */
        /* bash# mount -t glusterfs server:/volume /mnt/pnt */
        volid_ptr = volume_id;
    }

    len = snprintf(path_prefix, sizeof(path_prefix), "%s/vols", priv->workdir);
    if ((len < 0) || (len >= sizeof(path_prefix))) {
        ret = -1;
        goto out;
    }

    ret = glusterd_volinfo_find(volid_ptr, &volinfo);

    if (ret) {
        dup_volname = gf_strdup(volid_ptr);
        if (!dup_volname) {
            ret = -1;
            goto out;
        }
        /* Split the volume name */
        vol = strtok_r(dup_volname, ".", &save_ptr);
        if (!vol) {
            ret = -1;
            goto out;
        }
        ret = glusterd_volinfo_find(vol, &volinfo);
        if (ret)
            goto out;
    }

gotvolinfo:
    if (!glusterd_auth_get_username(volinfo))
        trusted_str = NULL;

    ret = snprintf(path, path_len, "%s/%s/%s.vol", path_prefix,
                   volinfo->volname, volid_ptr);
    if (ret == -1)
        goto out;

    ret = sys_stat(path, &stbuf);

    if ((ret == -1) && (errno == ENOENT)) {
        if (snprintf(dup_volid, PATH_MAX, "%s", volid_ptr) >= PATH_MAX)
            goto out;
        if (!strchr(dup_volid, '.')) {
            switch (volinfo->transport_type) {
                case GF_TRANSPORT_TCP:
                    strcat(dup_volid, ".tcp");
                    break;
                case GF_TRANSPORT_RDMA:
                    strcat(dup_volid, ".rdma");
                    break;
                case GF_TRANSPORT_BOTH_TCP_RDMA:
                    strcat(dup_volid, ".tcp");
                    break;
                default:
                    break;
            }
        }
        snprintf(path, path_len, "%s/%s/%s%s-fuse.vol", path_prefix,
                 volinfo->volname, (trusted_str ? trusted_str : ""), dup_volid);
        ret = sys_stat(path, &stbuf);
    }
out:
    if (dup_volname)
        GF_FREE(dup_volname);
    if (free_ptr)
        GF_FREE(free_ptr);
    return ret;
}

/* Get and store op-versions of the clients sending the getspec request
 * Clients of versions <= 3.3, don't send op-versions, their op-versions are
 * defaulted to 1. Also fetch brick_name.
 */
int32_t
glusterd_get_args_from_dict(gf_getspec_req *args, peer_info_t *peerinfo,
                            char **brick_name)
{
    dict_t *dict = NULL;
    int client_max_op_version = 1;
    int client_min_op_version = 1;
    int32_t ret = -1;
    xlator_t *this = NULL;
    char *name = NULL;

    this = THIS;
    GF_ASSERT(this);
    GF_ASSERT(args);
    GF_ASSERT(peerinfo);

    if (!args->xdata.xdata_len) {
        ret = 0;
        goto out;
    }

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

    ret = dict_unserialize(args->xdata.xdata_val, args->xdata.xdata_len, &dict);
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_UNSERIALIZE_FAIL,
               "Failed to unserialize request dictionary");
        goto out;
    }

    ret = dict_get_int32(dict, "min-op-version", &client_min_op_version);
    if (ret) {
        gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_DICT_GET_FAILED,
               "Failed to get client-min-op-version");
        goto out;
    }

    ret = dict_get_int32(dict, "max-op-version", &client_max_op_version);
    if (ret) {
        gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_DICT_GET_FAILED,
               "Failed to get client-max-op-version");
        goto out;
    }

    ret = dict_get_str(dict, "brick_name", &name);
    if (ret) {
        gf_msg_debug(this->name, 0, "No brick name present");
        ret = 0;
        goto out;
    }
    *brick_name = gf_strdup(name);
    if (*brick_name == NULL) {
        ret = -1;
        goto out;
    }

    gf_msg_debug(this->name, 0, "brick_name = %s", *brick_name);
out:
    peerinfo->max_op_version = client_max_op_version;
    peerinfo->min_op_version = client_min_op_version;

    if (dict)
        dict_unref(dict);

    return ret;
}

/* Given the missed_snapinfo and snap_opinfo take the
 * missed lvm snapshot
 */
int32_t
glusterd_create_missed_snap(glusterd_missed_snap_info *missed_snapinfo,
                            glusterd_snap_op_t *snap_opinfo)
{
    char *device = NULL;
    glusterd_conf_t *priv = NULL;
    glusterd_snap_t *snap = NULL;
    glusterd_volinfo_t *snap_vol = NULL;
    glusterd_volinfo_t *volinfo = NULL;
    glusterd_brickinfo_t *brickinfo = NULL;
    int32_t ret = -1;
    int32_t i = 0;
    uuid_t snap_uuid = {
        0,
    };
    xlator_t *this = NULL;
    char *mnt_device = NULL;

    this = THIS;
    GF_ASSERT(this);
    priv = this->private;
    GF_ASSERT(priv);
    GF_ASSERT(missed_snapinfo);
    GF_ASSERT(snap_opinfo);

    gf_uuid_parse(missed_snapinfo->snap_uuid, snap_uuid);

    /* Find the snap-object */
    snap = glusterd_find_snap_by_id(snap_uuid);
    if (!snap) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAP_NOT_FOUND,
               "Unable to find the snap with snap_uuid %s",
               missed_snapinfo->snap_uuid);
        ret = -1;
        goto out;
    }

    /* Find the snap_vol */
    cds_list_for_each_entry(volinfo, &snap->volumes, vol_list)
    {
        if (!strcmp(volinfo->volname, snap_opinfo->snap_vol_id)) {
            snap_vol = volinfo;
            break;
        }
    }

    if (!snap_vol) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOL_NOT_FOUND,
               "Unable to find the snap_vol(%s) "
               "for snap(%s)",
               snap_opinfo->snap_vol_id, snap->snapname);
        ret = -1;
        goto out;
    }

    /* Find the missed brick in the snap volume */
    cds_list_for_each_entry(brickinfo, &snap_vol->bricks, brick_list)
    {
        i++;
        if (i == snap_opinfo->brick_num)
            break;
    }

    if (brickinfo->snap_status != -1) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAP_STATUS_NOT_PENDING,
               "The snap status of the missed "
               "brick(%s) is not pending",
               brickinfo->path);
        goto out;
    }

    /* Fetch the device path */
    mnt_device = glusterd_get_brick_mount_device(snap_opinfo->brick_path);
    if (!mnt_device) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_BRICK_GET_INFO_FAIL,
               "Getting device name for the"
               "brick %s:%s failed",
               brickinfo->hostname, snap_opinfo->brick_path);
        ret = -1;
        goto out;
    }

    device = glusterd_build_snap_device_path(mnt_device, snap_vol->volname,
                                             snap_opinfo->brick_num - 1);
    if (!device) {
        gf_msg(this->name, GF_LOG_ERROR, ENXIO,
               GD_MSG_SNAP_DEVICE_NAME_GET_FAIL,
               "cannot copy the snapshot "
               "device name (volname: %s, snapname: %s)",
               snap_vol->volname, snap->snapname);
        ret = -1;
        goto out;
    }
    if (snprintf(brickinfo->device_path, sizeof(brickinfo->device_path), "%s",
                 device) >= sizeof(brickinfo->device_path)) {
        gf_msg(this->name, GF_LOG_ERROR, ENXIO,
               GD_MSG_SNAP_DEVICE_NAME_GET_FAIL,
               "cannot copy the device_path "
               "(device_path: %s)",
               brickinfo->device_path);
        ret = -1;
        goto out;
    }

    /* Update the backend file-system type of snap brick in
     * snap volinfo. */
    ret = glusterd_update_mntopts(snap_opinfo->brick_path, brickinfo);
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_BRK_MOUNTOPTS_FAIL,
               "Failed to update "
               "mount options for %s brick",
               brickinfo->path);
        /* We should not fail snapshot operation if we fail to get
         * the file-system type */
    }

    ret = glusterd_take_lvm_snapshot(brickinfo, snap_opinfo->brick_path);
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_SNAPSHOT_OP_FAILED,
               "Failed to take snapshot of %s", snap_opinfo->brick_path);
        goto out;
    }

    /* After the snapshot both the origin brick (LVM brick) and
     * the snapshot brick will have the same file-system label. This
     * will cause lot of problems at mount time. Therefore we must
     * generate a new label for the snapshot brick
     */
    ret = glusterd_update_fs_label(brickinfo);
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_BRICK_SET_INFO_FAIL,
               "Failed to update "
               "file-system label for %s brick",
               brickinfo->path);
        /* Failing to update label should not cause snapshot failure.
         * Currently label is updated only for XFS and ext2/ext3/ext4
         * file-system.
         */
    }

    /* Create and mount the snap brick */
    ret = glusterd_snap_brick_create(snap_vol, brickinfo,
                                     snap_opinfo->brick_num - 1, 0);
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_BRICK_CREATION_FAIL,
               "Failed to "
               " create and mount the brick(%s) for the snap %s",
               snap_opinfo->brick_path, snap_vol->snapshot->snapname);
        goto out;
    }

    brickinfo->snap_status = 0;
    ret = glusterd_brick_start(snap_vol, brickinfo, _gf_false, _gf_false);
    if (ret) {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_BRICK_DISCONNECTED,
               "starting the "
               "brick %s:%s for the snap %s failed",
               brickinfo->hostname, brickinfo->path, snap->snapname);
        goto out;
    }
    ret = glusterd_store_volinfo(snap_vol, GLUSTERD_VOLINFO_VER_AC_NONE);
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLINFO_STORE_FAIL,
               "Failed to store snapshot "
               "volinfo (%s) for snap %s",
               snap_vol->volname, snap->snapname);
        goto out;
    }

out:
    if (mnt_device)
        GF_FREE(mnt_device);
    if (device)
        GF_FREE(device);

    return ret;
}

/* Look into missed_snap_list, to see it the given brick_name,
 * has any missed snap creates for the local node */
int32_t
glusterd_take_missing_brick_snapshots(char *brick_name)
{
    char *my_node_uuid = NULL;
    glusterd_conf_t *priv = NULL;
    glusterd_missed_snap_info *missed_snapinfo = NULL;
    glusterd_snap_op_t *snap_opinfo = NULL;
    int32_t ret = -1;
    gf_boolean_t update_list = _gf_false;
    xlator_t *this = NULL;

    this = THIS;
    GF_ASSERT(this);
    priv = this->private;
    GF_ASSERT(priv);
    GF_ASSERT(brick_name);

    my_node_uuid = uuid_utoa(MY_UUID);

    cds_list_for_each_entry(missed_snapinfo, &priv->missed_snaps_list,
                            missed_snaps)
    {
        /* If the missed snap op is not for the local node
         * then continue
         */
        if (strcmp(my_node_uuid, missed_snapinfo->node_uuid))
            continue;

        cds_list_for_each_entry(snap_opinfo, &missed_snapinfo->snap_ops,
                                snap_ops_list)
        {
            /* Check if the missed snap's op is a create for
             * the brick name in question
             */
            if ((snap_opinfo->op == GF_SNAP_OPTION_TYPE_CREATE) &&
                (!strcmp(brick_name, snap_opinfo->brick_path))) {
                /* Perform a snap create if the
                 * op is still pending
                 */
                if (snap_opinfo->status == GD_MISSED_SNAP_PENDING) {
                    ret = glusterd_create_missed_snap(missed_snapinfo,
                                                      snap_opinfo);
                    if (ret) {
                        gf_msg(this->name, GF_LOG_ERROR, 0,
                               GD_MSG_MISSED_SNAP_CREATE_FAIL,
                               "Failed to create "
                               "missed snap for %s",
                               brick_name);
                        /* At this stage, we will mark
                         * the entry as done. Because
                         * of the failure other
                         * snapshots will not be
                         * affected, and neither the
                         * brick. Only the current snap
                         * brick will always remain as
                         * pending.
                         */
                    }
                    snap_opinfo->status = GD_MISSED_SNAP_DONE;
                    update_list = _gf_true;
                }
                /* One snap-id won't have more than one missed
                 * create for the same brick path. Hence
                 * breaking in search of another missed create
                 * for the same brick path in the local node
                 */
                break;
            }
        }
    }

    if (update_list == _gf_true) {
        ret = glusterd_store_update_missed_snaps();
        if (ret) {
            gf_msg(this->name, GF_LOG_ERROR, 0,
                   GD_MSG_MISSED_SNAP_LIST_STORE_FAIL,
                   "Failed to update missed_snaps_list");
            goto out;
        }
    }

    ret = 0;
out:
    return ret;
}

/* Checks if the client supports the volume, ie. client can understand all the
 * options in the volfile
 */
static gf_boolean_t
_client_supports_volume(peer_info_t *peerinfo, int32_t *op_errno)
{
    gf_boolean_t ret = _gf_true;
    glusterd_volinfo_t *volinfo = NULL;

    GF_ASSERT(peerinfo);
    GF_ASSERT(op_errno);

    /* Only check when the volfile being requested is a volume. Not finding
     * a volinfo implies that the volfile requested for is not of a gluster
     * volume. A non volume volfile is requested by the local gluster
     * services like shd and nfs-server. These need not be checked as they
     * will be running at the same op-version as glusterd and will be able
     * to support all the features
     */
    if ((glusterd_volinfo_find(peerinfo->volname, &volinfo) == 0) &&
        ((peerinfo->min_op_version > volinfo->client_op_version) ||
         (peerinfo->max_op_version < volinfo->client_op_version))) {
        ret = _gf_false;
        *op_errno = ENOTSUP;
        gf_msg("glusterd", GF_LOG_INFO, ENOTSUP, GD_MSG_UNSUPPORTED_VERSION,
               "Client %s (%d -> %d) doesn't support required "
               "op-version (%d). Rejecting volfile request.",
               peerinfo->identifier, peerinfo->min_op_version,
               peerinfo->max_op_version, volinfo->client_op_version);
    }

    return ret;
}

int
__server_getspec(rpcsvc_request_t *req)
{
    int32_t ret = -1;
    int32_t op_ret = -1;
    int32_t op_errno = 0;
    int32_t spec_fd = -1;
    size_t file_len = 0;
    char filename[PATH_MAX] = {
        0,
    };
    struct stat stbuf = {
        0,
    };
    char *brick_name = NULL;
    char *volume = NULL;
    char *tmp = NULL;
    rpc_transport_t *trans = NULL;
    gf_getspec_req args = {
        0,
    };
    gf_getspec_rsp rsp = {
        0,
    };
    char addrstr[RPCSVC_PEER_STRLEN] = {0};
    peer_info_t *peerinfo = NULL;
    xlator_t *this = NULL;
    dict_t *dict = NULL;

    this = THIS;
    GF_ASSERT(this);

    ret = xdr_to_generic(req->msg[0], &args, (xdrproc_t)xdr_gf_getspec_req);
    if (ret < 0) {
        // failed to decode msg;
        req->rpc_err = GARBAGE_ARGS;
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_REQ_DECODE_FAIL,
               "Failed to decode the message");
        goto fail;
    }

    peerinfo = &req->trans->peerinfo;

    volume = args.key;

    if (strlen(volume) >= (NAME_MAX)) {
        op_errno = EINVAL;
        gf_msg(this->name, GF_LOG_ERROR, EINVAL, GD_MSG_NAME_TOO_LONG,
               "volume name too long (%s)", volume);
        goto fail;
    }

    gf_msg(this->name, GF_LOG_INFO, 0, GD_MSG_MOUNT_REQ_RCVD,
           "Received mount request for volume %s", volume);

    /* Need to strip leading '/' from volnames. This was introduced to
     * support nfs style mount parameters for native gluster mount
     */
    if (volume[0] == '/')
        ret = snprintf(peerinfo->volname, sizeof(peerinfo->volname), "%s",
                       &volume[1]);
    else
        ret = snprintf(peerinfo->volname, sizeof(peerinfo->volname), "%s",
                       volume);
    if (ret < 0 || ret >= sizeof(peerinfo->volname)) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VOLINFO_GET_FAIL,
               "peerinfo->volname %s truncated or error occurred: "
               "(ret: %d)",
               peerinfo->volname, ret);
        ret = -1;
        goto fail;
    }

    ret = glusterd_get_args_from_dict(&args, peerinfo, &brick_name);
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_GET_FAILED,
               "Failed to get args from dict");
        goto fail;
    }

    if (!_client_supports_volume(peerinfo, &op_errno)) {
        ret = -1;
        goto fail;
    }

    dict = dict_new();
    if (!dict) {
        ret = -ENOMEM;
        goto fail;
    }

    trans = req->trans;
    /* addrstr will be empty for cli socket connections */
    ret = rpcsvc_transport_peername(trans, (char *)&addrstr, sizeof(addrstr));
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0,
               GD_MSG_RPC_TRANSPORT_GET_PEERNAME_FAIL,
               "Failed to get the peername");
        goto fail;
    }

    tmp = strrchr(addrstr, ':');
    if (tmp)
        *tmp = '\0';

    /* The trusted volfiles are given to the glusterd owned process like NFS
     * server, self-heal daemon etc., so that they are not inadvertently
     * blocked by a auth.{allow,reject} setting. The trusted volfile is not
     * meant for external users.
     * For unix domain socket, address will be empty.
     */
    if (strlen(addrstr) == 0 || gf_is_local_addr(addrstr)) {
        ret = build_volfile_path(volume, filename, sizeof(filename),
                                 TRUSTED_PREFIX, dict);
    } else {
        ret = build_volfile_path(volume, filename, sizeof(filename), NULL,
                                 dict);
    }

    if (ret == 0) {
        if (dict->count > 0) {
            ret = dict_allocate_and_serialize(dict, &rsp.xdata.xdata_val,
                                              &rsp.xdata.xdata_len);
            if (ret) {
                gf_msg(this->name, GF_LOG_ERROR, 0,
                       GD_MSG_DICT_SERL_LENGTH_GET_FAIL,
                       "Failed to serialize dict "
                       "to request buffer");
                goto fail;
            }
            dict->extra_free = rsp.xdata.xdata_val;
        }

        /* to allocate the proper buffer to hold the file data */
        ret = sys_stat(filename, &stbuf);
        if (ret < 0) {
            gf_msg("glusterd", GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED,
                   "Unable to stat %s (%s)", filename, strerror(errno));
            goto fail;
        }

        spec_fd = open(filename, O_RDONLY);
        if (spec_fd < 0) {
            gf_msg("glusterd", GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED,
                   "Unable to open %s (%s)", filename, strerror(errno));
            goto fail;
        }
        ret = file_len = stbuf.st_size;
    } else {
        op_errno = ENOENT;
        goto fail;
    }

    if (file_len) {
        rsp.spec = CALLOC(file_len + 1, sizeof(char));
        if (!rsp.spec) {
            ret = -1;
            op_errno = ENOMEM;
            goto fail;
        }
        ret = sys_read(spec_fd, rsp.spec, file_len);
    }

    if (brick_name) {
        gf_msg_debug(this->name, 0, "Look for missing snap creates for %s",
                     brick_name);
        op_ret = glusterd_take_missing_brick_snapshots(brick_name);
        if (op_ret) {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_MISSED_SNAP_CREATE_FAIL,
                   "Failed to take missing brick snapshots");
            ret = -1;
            goto fail;
        }
    }
    /* convert to XDR */
fail:
    if (spec_fd >= 0)
        sys_close(spec_fd);

    GF_FREE(brick_name);

    rsp.op_ret = ret;
    if (rsp.op_ret < 0)
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_MOUNT_REQ_FAIL,
               "Failed to mount the volume");

    if (op_errno)
        rsp.op_errno = gf_errno_to_error(op_errno);

    if (!rsp.spec)
        rsp.spec = strdup("");

    glusterd_submit_reply(req, &rsp, NULL, 0, NULL,
                          (xdrproc_t)xdr_gf_getspec_rsp);
    free(args.key);  // malloced by xdr
    free(rsp.spec);

    if (dict)
        dict_unref(dict);

    if (args.xdata.xdata_val)
        free(args.xdata.xdata_val);

    return 0;
}

int
server_getspec(rpcsvc_request_t *req)
{
    return glusterd_big_locked_handler(req, __server_getspec);
}

int32_t
__server_event_notify(rpcsvc_request_t *req)
{
    int32_t ret = -1;
    gf_event_notify_req args = {
        0,
    };
    gf_event_notify_rsp rsp = {
        0,
    };
    dict_t *dict = NULL;
    gf_boolean_t need_rsp = _gf_true;

    ret = xdr_to_generic(req->msg[0], &args,
                         (xdrproc_t)xdr_gf_event_notify_req);
    if (ret < 0) {
        req->rpc_err = GARBAGE_ARGS;
        goto fail;
    }

    if (args.dict.dict_len) {
        dict = dict_new();
        if (!dict)
            return ret;
        ret = dict_unserialize(args.dict.dict_val, args.dict.dict_len, &dict);
        if (ret) {
            gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_DICT_UNSERIALIZE_FAIL,
                   "Failed to unserialize req");
            goto fail;
        }
    }

    switch (args.op) {
        case GF_EN_DEFRAG_STATUS:
            gf_msg("glusterd", GF_LOG_INFO, 0, GD_MSG_DEFRAG_STATUS_UPDATED,
                   "received defrag status updated");
            if (dict) {
                glusterd_defrag_event_notify_handle(dict);
                need_rsp = _gf_false;
            }
            break;
        default:
            gf_msg("glusterd", GF_LOG_ERROR, EINVAL, GD_MSG_OP_UNSUPPORTED,
                   "Unknown op received in event "
                   "notify");
            gf_event(EVENT_NOTIFY_UNKNOWN_OP, "op=%d", args.op);
            ret = -1;
            break;
    }

fail:
    rsp.op_ret = ret;

    if (need_rsp)
        glusterd_submit_reply(req, &rsp, NULL, 0, NULL,
                              (xdrproc_t)xdr_gf_event_notify_rsp);
    if (dict)
        dict_unref(dict);
    free(args.dict.dict_val);  // malloced by xdr

    return 0;
}

int32_t
server_event_notify(rpcsvc_request_t *req)
{
    return glusterd_big_locked_handler(req, __server_event_notify);
}

int
gd_validate_cluster_op_version(xlator_t *this, int cluster_op_version,
                               char *peerid)
{
    int ret = -1;
    glusterd_conf_t *conf = NULL;

    conf = this->private;

    if (cluster_op_version > GD_OP_VERSION_MAX) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_OP_VERSION_MISMATCH,
               "operating version %d is more than the maximum "
               "supported (%d) on the machine (as per peer request "
               "from %s)",
               cluster_op_version, GD_OP_VERSION_MAX, peerid);
        goto out;
    }

    /* The peer can only reduce its op-version when it doesn't have any
     * volumes. Reducing op-version when it already contains volumes can
     * lead to inconsistencies in the cluster
     */
    if ((cluster_op_version < conf->op_version) &&
        !cds_list_empty(&conf->volumes)) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_OP_VERS_ADJUST_FAIL,
               "cannot reduce operating version to %d from current "
               "version %d as volumes exist (as per peer request from "
               "%s)",
               cluster_op_version, conf->op_version, peerid);
        goto out;
    }

    ret = 0;
out:
    return ret;
}

/* Validate if glusterd can serve the management handshake request
 *
 * Requests are allowed if,
 *  - glusterd has no peers & no volumes, or
 *  - the request came from a known peer
 * A known peer is identified using the following steps
 *  - the dict is checked for a peer uuid, which if present is matched with the
 *  peer list, else
 *  - the incoming request address is matched with the peer list
 */
gf_boolean_t
gd_validate_mgmt_hndsk_req(rpcsvc_request_t *req, dict_t *dict)
{
    int ret = -1;
    char hostname[UNIX_PATH_MAX + 1] = {
        0,
    };
    glusterd_peerinfo_t *peer = NULL;
    xlator_t *this = NULL;
    char *uuid_str = NULL;
    uuid_t peer_uuid = {
        0,
    };

    this = THIS;
    GF_ASSERT(this);

    if (!glusterd_have_peers() && !glusterd_have_volumes())
        return _gf_true;

    ret = dict_get_str(dict, GD_PEER_ID_KEY, &uuid_str);
    /* Try to match uuid only if available, don't fail as older peers will
     * not send a uuid
     */
    if (!ret) {
        gf_uuid_parse(uuid_str, peer_uuid);
        RCU_READ_LOCK;
        ret = (glusterd_peerinfo_find(peer_uuid, NULL) != NULL);
        RCU_READ_UNLOCK;
        if (ret)
            return _gf_true;
    }

    /* If you cannot get the hostname, you cannot authenticate */
    ret = glusterd_remote_hostname_get(req, hostname, sizeof(hostname));
    if (ret)
        return _gf_false;

    /* If peer object is not found it indicates that request is from an
     * unknown peer, if its found, validate whether its uuid is also
     * available in the peerinfo list. There could be a case where hostname
     * is available in the peerinfo list but the uuid has changed of the
     * node due to a reinstall, in that case the validation should fail!
     */
    RCU_READ_LOCK;
    if (!uuid_str) {
        ret = (glusterd_peerinfo_find(NULL, hostname) == NULL);
    } else {
        peer = glusterd_peerinfo_find(NULL, hostname);
        if (!peer) {
            ret = -1;
        } else if (peer && glusterd_peerinfo_find(peer_uuid, NULL) != NULL) {
            ret = 0;
        } else {
            gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_HANDSHAKE_REQ_REJECTED,
                   "Request from "
                   "peer %s has an entry in peerinfo, but uuid "
                   "does not match",
                   req->trans->peerinfo.identifier);
            ret = -1;
        }
    }
    RCU_READ_UNLOCK;
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_HANDSHAKE_REQ_REJECTED,
               "Rejecting management "
               "handshake request from unknown peer %s",
               req->trans->peerinfo.identifier);
        gf_event(EVENT_PEER_REJECT, "peer=%s", req->trans->peerinfo.identifier);
        return _gf_false;
    }

    return _gf_true;
}

int
__glusterd_mgmt_hndsk_versions(rpcsvc_request_t *req)
{
    dict_t *dict = NULL;
    xlator_t *this = NULL;
    glusterd_conf_t *conf = NULL;
    int ret = -1;
    int op_errno = EINVAL;
    gf_mgmt_hndsk_req args = {
        {
            0,
        },
    };
    gf_mgmt_hndsk_rsp rsp = {
        0,
    };
    dict_t *args_dict = NULL;

    this = THIS;
    conf = this->private;

    ret = xdr_to_generic(req->msg[0], &args, (xdrproc_t)xdr_gf_mgmt_hndsk_req);
    if (ret < 0) {
        // failed to decode msg;
        req->rpc_err = GARBAGE_ARGS;
        goto out;
    }

    GF_PROTOCOL_DICT_UNSERIALIZE(this, args_dict, args.hndsk.hndsk_val,
                                 (args.hndsk.hndsk_len), ret, op_errno, out);

    /* Check if we can service the request */
    if (!gd_validate_mgmt_hndsk_req(req, args_dict)) {
        ret = -1;
        goto out;
    }

    dict = dict_new();
    if (!dict)
        goto out;

    ret = dict_set_int32(dict, GD_OP_VERSION_KEY, conf->op_version);
    if (ret) {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_DICT_SET_FAILED,
               "failed to set operating version");
        rsp.op_ret = ret;
        goto out;
    }

    ret = dict_set_int32(dict, GD_MIN_OP_VERSION_KEY, GD_OP_VERSION_MIN);
    if (ret) {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_DICT_SET_FAILED,
               "failed to set %s", GD_MIN_OP_VERSION_KEY);
        rsp.op_ret = ret;
        goto out;
    }

    ret = dict_set_int32(dict, GD_MAX_OP_VERSION_KEY, GD_OP_VERSION_MAX);
    if (ret) {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_DICT_SET_FAILED,
               "failed to set %s", GD_MAX_OP_VERSION_KEY);
        rsp.op_ret = ret;
        goto out;
    }

    ret = 0;

    GF_PROTOCOL_DICT_SERIALIZE(this, dict, (&rsp.hndsk.hndsk_val),
                               rsp.hndsk.hndsk_len, op_errno, out);
out:

    rsp.op_ret = ret;
    rsp.op_errno = op_errno;

    glusterd_submit_reply(req, &rsp, NULL, 0, NULL,
                          (xdrproc_t)xdr_gf_mgmt_hndsk_rsp);

    ret = 0;

    if (dict)
        dict_unref(dict);

    if (args.hndsk.hndsk_val)
        free(args.hndsk.hndsk_val);

    if (rsp.hndsk.hndsk_val)
        GF_FREE(rsp.hndsk.hndsk_val);

    if (args_dict)
        dict_unref(args_dict);

    return ret;
}

int
glusterd_mgmt_hndsk_versions(rpcsvc_request_t *req)
{
    return glusterd_big_locked_handler(req, __glusterd_mgmt_hndsk_versions);
}

int
__glusterd_mgmt_hndsk_versions_ack(rpcsvc_request_t *req)
{
    dict_t *clnt_dict = NULL;
    xlator_t *this = NULL;
    glusterd_conf_t *conf = NULL;
    int ret = -1;
    int op_errno = EINVAL;
    int peer_op_version = 0;
    gf_mgmt_hndsk_req args = {
        {
            0,
        },
    };
    gf_mgmt_hndsk_rsp rsp = {
        0,
    };

    this = THIS;
    conf = this->private;

    ret = xdr_to_generic(req->msg[0], &args, (xdrproc_t)xdr_gf_mgmt_hndsk_req);
    if (ret < 0) {
        // failed to decode msg;
        req->rpc_err = GARBAGE_ARGS;
        goto out;
    }

    GF_PROTOCOL_DICT_UNSERIALIZE(this, clnt_dict, args.hndsk.hndsk_val,
                                 (args.hndsk.hndsk_len), ret, op_errno, out);

    ret = dict_get_int32(clnt_dict, GD_OP_VERSION_KEY, &peer_op_version);
    if (ret) {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_DICT_GET_FAILED,
               "failed to get the op-version key peer=%s",
               req->trans->peerinfo.identifier);
        goto out;
    }

    ret = gd_validate_cluster_op_version(this, peer_op_version,
                                         req->trans->peerinfo.identifier);
    if (ret)
        goto out;

    /* As this is ACK from the Cluster for the versions supported,
       can set the op-version of 'this' glusterd to the one
       received. */
    gf_msg(this->name, GF_LOG_INFO, 0, GD_MSG_VERS_INFO,
           "using the op-version %d", peer_op_version);
    conf->op_version = peer_op_version;
    ret = glusterd_store_global_info(this);
    if (ret)
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_GLOBAL_OP_VERSION_SET_FAIL,
               "Failed to store op-version");

out:
    rsp.op_ret = ret;
    rsp.op_errno = op_errno;

    glusterd_submit_reply(req, &rsp, NULL, 0, NULL,
                          (xdrproc_t)xdr_gf_mgmt_hndsk_rsp);

    ret = 0;

    if (clnt_dict)
        dict_unref(clnt_dict);

    if (args.hndsk.hndsk_val)
        free(args.hndsk.hndsk_val);

    return ret;
}

int
glusterd_mgmt_hndsk_versions_ack(rpcsvc_request_t *req)
{
    return glusterd_big_locked_handler(req, __glusterd_mgmt_hndsk_versions_ack);
}

int
__server_get_volume_info(rpcsvc_request_t *req)
{
    int ret = -1;
    int32_t op_errno = ENOENT;
    gf_get_volume_info_req vol_info_req = {{
        0,
    }};
    gf_get_volume_info_rsp vol_info_rsp = {
        0,
    };
    char *volname = NULL;
    glusterd_volinfo_t *volinfo = NULL;
    dict_t *dict = NULL;
    dict_t *dict_rsp = NULL;
    char *volume_id_str = NULL;
    int32_t flags = 0;

    ret = xdr_to_generic(req->msg[0], &vol_info_req,
                         (xdrproc_t)xdr_gf_get_volume_info_req);
    if (ret < 0) {
        /* failed to decode msg */
        req->rpc_err = GARBAGE_ARGS;
        goto out;
    }
    gf_msg("glusterd", GF_LOG_INFO, 0, GD_MSG_VOL_INFO_REQ_RECVD,
           "Received get volume info req");

    if (vol_info_req.dict.dict_len) {
        /* Unserialize the dictionary */
        dict = dict_new();
        if (!dict) {
            gf_msg("glusterd", GF_LOG_WARNING, ENOMEM, GD_MSG_NO_MEMORY,
                   "Out of Memory");
            op_errno = ENOMEM;
            ret = -1;
            goto out;
        }

        ret = dict_unserialize(vol_info_req.dict.dict_val,
                               vol_info_req.dict.dict_len, &dict);
        if (ret < 0) {
            gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_DICT_UNSERIALIZE_FAIL,
                   "failed to "
                   "unserialize req-buffer to dictionary");
            op_errno = -ret;
            ret = -1;
            goto out;
        } else {
            dict->extra_stdfree = vol_info_req.dict.dict_val;
        }
    }

    ret = dict_get_int32(dict, "flags", &flags);
    if (ret) {
        gf_msg(THIS->name, GF_LOG_ERROR, -ret, GD_MSG_DICT_GET_FAILED,
               "failed to get flags");
        op_errno = -ret;
        ret = -1;
        goto out;
    }

    if (!flags) {
        /* Nothing to query about. Just return success */
        gf_msg(THIS->name, GF_LOG_ERROR, 0, GD_MSG_NO_FLAG_SET, "No flags set");
        ret = 0;
        goto out;
    }

    ret = dict_get_str(dict, "volname", &volname);
    if (ret) {
        op_errno = EINVAL;
        ret = -1;
        goto out;
    }

    ret = glusterd_volinfo_find(volname, &volinfo);
    if (ret) {
        op_errno = EINVAL;
        ret = -1;
        goto out;
    }

    if (flags & (int32_t)GF_GET_VOLUME_UUID) {
        volume_id_str = gf_strdup(uuid_utoa(volinfo->volume_id));
        if (!volume_id_str) {
            op_errno = ENOMEM;
            ret = -1;
            goto out;
        }

        dict_rsp = dict_new();
        if (!dict_rsp) {
            gf_msg("glusterd", GF_LOG_WARNING, ENOMEM, GD_MSG_NO_MEMORY,
                   "Out of Memory");
            op_errno = ENOMEM;
            GF_FREE(volume_id_str);
            ret = -1;
            goto out;
        }
        ret = dict_set_dynstr(dict_rsp, "volume_id", volume_id_str);
        if (ret) {
            op_errno = -ret;
            ret = -1;
            goto out;
        }
    }
    ret = dict_allocate_and_serialize(dict_rsp, &vol_info_rsp.dict.dict_val,
                                      &vol_info_rsp.dict.dict_len);
    if (ret) {
        op_errno = -ret;
        ret = -1;
        goto out;
    }

out:
    vol_info_rsp.op_ret = ret;
    vol_info_rsp.op_errno = op_errno;
    vol_info_rsp.op_errstr = "";
    glusterd_submit_reply(req, &vol_info_rsp, NULL, 0, NULL,
                          (xdrproc_t)xdr_gf_get_volume_info_rsp);
    ret = 0;

    if (dict) {
        dict_unref(dict);
    }

    if (dict_rsp) {
        dict_unref(dict_rsp);
    }

    if (vol_info_rsp.dict.dict_val) {
        GF_FREE(vol_info_rsp.dict.dict_val);
    }
    return ret;
}

int
server_get_volume_info(rpcsvc_request_t *req)
{
    return glusterd_big_locked_handler(req, __server_get_volume_info);
}

/*
 * glusterd function to get the list of snapshot names and uuids
 */
int
__server_get_snap_info(rpcsvc_request_t *req)
{
    int ret = -1;
    int op_errno = ENOENT;
    gf_getsnap_name_uuid_req snap_info_req = {{
        0,
    }};
    gf_getsnap_name_uuid_rsp snap_info_rsp = {
        0,
    };
    dict_t *dict = NULL;
    dict_t *dict_rsp = NULL;
    char *volname = NULL;

    GF_ASSERT(req);

    ret = xdr_to_generic(req->msg[0], &snap_info_req,
                         (xdrproc_t)xdr_gf_getsnap_name_uuid_req);
    if (ret < 0) {
        req->rpc_err = GARBAGE_ARGS;
        gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_REQ_DECODE_FAIL,
               "Failed to decode management handshake response");
        goto out;
    }

    if (snap_info_req.dict.dict_len) {
        dict = dict_new();
        if (!dict) {
            op_errno = ENOMEM;
            ret = -1;
            goto out;
        }

        ret = dict_unserialize(snap_info_req.dict.dict_val,
                               snap_info_req.dict.dict_len, &dict);
        if (ret < 0) {
            gf_msg("glusterd", GF_LOG_ERROR, EINVAL,
                   GD_MSG_DICT_UNSERIALIZE_FAIL,
                   "Failed to unserialize dictionary");
            op_errno = EINVAL;
            ret = -1;
            goto out;
        } else {
            dict->extra_stdfree = snap_info_req.dict.dict_val;
        }
    }

    ret = dict_get_str(dict, "volname", &volname);
    if (ret) {
        op_errno = EINVAL;
        gf_msg("glusterd", GF_LOG_ERROR, EINVAL, GD_MSG_DICT_GET_FAILED,
               "Failed to retrieve volname");
        ret = -1;
        goto out;
    }

    dict_rsp = dict_new();
    if (!dict_rsp) {
        op_errno = ENOMEM;
        ret = -1;
        goto out;
    }

    ret = glusterd_snapshot_get_volnames_uuids(dict_rsp, volname,
                                               &snap_info_rsp);

    if (ret) {
        gf_msg("glusterd", GF_LOG_ERROR, EINVAL, GD_MSG_VOL_NOT_FOUND,
               "Error getting snapshot volume names and uuids : %s", volname);
        op_errno = EINVAL;
    }

out:
    snap_info_rsp.op_ret = ret;
    snap_info_rsp.op_errno = op_errno;
    snap_info_rsp.op_errstr = "";
    glusterd_submit_reply(req, &snap_info_rsp, NULL, 0, NULL,
                          (xdrproc_t)xdr_gf_getsnap_name_uuid_rsp);

    if (dict) {
        dict_unref(dict);
    }

    if (dict_rsp) {
        dict_unref(dict_rsp);
    }

    if (snap_info_rsp.dict.dict_val) {
        GF_FREE(snap_info_rsp.dict.dict_val);
    }

    return 0;
}

int
server_get_snap_info(rpcsvc_request_t *req)
{
    return glusterd_big_locked_handler(req, __server_get_snap_info);
}

rpcsvc_actor_t gluster_handshake_actors[GF_HNDSK_MAXVALUE] = {
    [GF_HNDSK_NULL] = {"NULL", GF_HNDSK_NULL, NULL, NULL, 0, DRC_NA},
    [GF_HNDSK_GETSPEC] = {"GETSPEC", GF_HNDSK_GETSPEC, server_getspec, NULL, 0,
                          DRC_NA},
    [GF_HNDSK_EVENT_NOTIFY] = {"EVENTNOTIFY", GF_HNDSK_EVENT_NOTIFY,
                               server_event_notify, NULL, 0, DRC_NA},
    [GF_HNDSK_GET_VOLUME_INFO] = {"GETVOLUMEINFO", GF_HNDSK_GET_VOLUME_INFO,
                                  server_get_volume_info, NULL, 0, DRC_NA},
    [GF_HNDSK_GET_SNAPSHOT_INFO] = {"GETSNAPINFO", GF_HNDSK_GET_SNAPSHOT_INFO,
                                    server_get_snap_info, NULL, 0, DRC_NA},
};

struct rpcsvc_program gluster_handshake_prog = {
    .progname = "Gluster Handshake",
    .prognum = GLUSTER_HNDSK_PROGRAM,
    .progver = GLUSTER_HNDSK_VERSION,
    .actors = gluster_handshake_actors,
    .numactors = GF_HNDSK_MAXVALUE,
};

/* A minimal RPC program just for the cli getspec command */
rpcsvc_actor_t gluster_cli_getspec_actors[GF_HNDSK_MAXVALUE] = {
    [GF_HNDSK_GETSPEC] = {"GETSPEC", GF_HNDSK_GETSPEC, server_getspec, NULL, 0,
                          DRC_NA},
};

struct rpcsvc_program gluster_cli_getspec_prog = {
    .progname = "Gluster Handshake (CLI Getspec)",
    .prognum = GLUSTER_HNDSK_PROGRAM,
    .progver = GLUSTER_HNDSK_VERSION,
    .actors = gluster_cli_getspec_actors,
    .numactors = GF_HNDSK_MAXVALUE,
};

char *glusterd_dump_proc[GF_DUMP_MAXVALUE] = {
    [GF_DUMP_NULL] = "NULL",
    [GF_DUMP_DUMP] = "DUMP",
    [GF_DUMP_PING] = "PING",
};

rpc_clnt_prog_t glusterd_dump_prog = {
    .progname = "GLUSTERD-DUMP",
    .prognum = GLUSTER_DUMP_PROGRAM,
    .progver = GLUSTER_DUMP_VERSION,
    .procnames = glusterd_dump_proc,
};

rpcsvc_actor_t glusterd_mgmt_hndsk_actors[GD_MGMT_HNDSK_MAXVALUE] = {
    [GD_MGMT_HNDSK_NULL] = {"NULL", GD_MGMT_HNDSK_NULL, NULL, NULL, 0, DRC_NA},
    [GD_MGMT_HNDSK_VERSIONS] = {"MGMT-VERS", GD_MGMT_HNDSK_VERSIONS,
                                glusterd_mgmt_hndsk_versions, NULL, 0, DRC_NA},
    [GD_MGMT_HNDSK_VERSIONS_ACK] = {"MGMT-VERS-ACK", GD_MGMT_HNDSK_VERSIONS_ACK,
                                    glusterd_mgmt_hndsk_versions_ack, NULL, 0,
                                    DRC_NA},
};

struct rpcsvc_program glusterd_mgmt_hndsk_prog = {
    .progname = "Gluster MGMT Handshake",
    .prognum = GD_MGMT_HNDSK_PROGRAM,
    .progver = GD_MGMT_HNDSK_VERSION,
    .actors = glusterd_mgmt_hndsk_actors,
    .numactors = GD_MGMT_HNDSK_MAXVALUE,
};

char *glusterd_mgmt_hndsk_proc[GD_MGMT_HNDSK_MAXVALUE] = {
    [GD_MGMT_HNDSK_NULL] = "NULL",
    [GD_MGMT_HNDSK_VERSIONS] = "MGMT-VERS",
    [GD_MGMT_HNDSK_VERSIONS_ACK] = "MGMT-VERS-ACK",
};

rpc_clnt_prog_t gd_clnt_mgmt_hndsk_prog = {
    .progname = "Gluster MGMT Handshake",
    .prognum = GD_MGMT_HNDSK_PROGRAM,
    .progver = GD_MGMT_HNDSK_VERSION,
    .procnames = glusterd_mgmt_hndsk_proc,
};

static int
glusterd_event_connected_inject(glusterd_peerctx_t *peerctx)
{
    GF_ASSERT(peerctx);

    glusterd_friend_sm_event_t *event = NULL;
    glusterd_probe_ctx_t *ctx = NULL;
    int ret = -1;
    glusterd_peerinfo_t *peerinfo = NULL;

    ret = glusterd_friend_sm_new_event(GD_FRIEND_EVENT_CONNECTED, &event);

    if (ret) {
        gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_EVENT_NEW_GET_FAIL,
               "Unable to get new event");
        goto out;
    }

    ctx = GF_CALLOC(1, sizeof(*ctx), gf_gld_mt_probe_ctx_t);

    if (!ctx) {
        ret = -1;
        gf_msg("glusterd", GF_LOG_ERROR, ENOMEM, GD_MSG_NO_MEMORY,
               "Memory not available");
        goto out;
    }

    RCU_READ_LOCK;

    peerinfo = glusterd_peerinfo_find_by_generation(peerctx->peerinfo_gen);
    if (!peerinfo) {
        RCU_READ_UNLOCK;
        ret = -1;
        gf_msg(THIS->name, GF_LOG_ERROR, 0, GD_MSG_PEER_NOT_FOUND,
               "Could not find peer %s(%s)", peerctx->peername,
               uuid_utoa(peerctx->peerid));
        GF_FREE(ctx);
        goto out;
    }
    ctx->hostname = gf_strdup(peerinfo->hostname);
    ctx->port = peerinfo->port;
    ctx->req = peerctx->args.req;
    ctx->dict = peerctx->args.dict;

    event->peername = gf_strdup(peerinfo->hostname);
    gf_uuid_copy(event->peerid, peerinfo->uuid);
    event->ctx = ctx;

    ret = glusterd_friend_sm_inject_event(event);

    RCU_READ_UNLOCK;

    if (ret)
        gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_EVENT_INJECT_FAIL,
               "Unable to inject "
               "EVENT_CONNECTED ret = %d",
               ret);

out:
    gf_msg_debug("glusterd", 0, "returning %d", ret);
    return ret;
}

int
gd_validate_peer_op_version(xlator_t *this, glusterd_peerinfo_t *peerinfo,
                            dict_t *dict, char **errstr)
{
    int ret = -1;
    glusterd_conf_t *conf = NULL;
    int32_t peer_op_version = 0;
    int32_t peer_min_op_version = 0;
    int32_t peer_max_op_version = 0;

    if (!dict || !this || !peerinfo)
        goto out;

    conf = this->private;

    ret = dict_get_int32(dict, GD_OP_VERSION_KEY, &peer_op_version);
    if (ret)
        goto out;

    ret = dict_get_int32(dict, GD_MAX_OP_VERSION_KEY, &peer_max_op_version);
    if (ret)
        goto out;

    ret = dict_get_int32(dict, GD_MIN_OP_VERSION_KEY, &peer_min_op_version);
    if (ret)
        goto out;

    ret = -1;
    /* Check if peer can support our op_version */
    if ((peer_max_op_version < conf->op_version) ||
        (peer_min_op_version > conf->op_version)) {
        ret = gf_asprintf(errstr,
                          "Peer %s does not support required "
                          "op-version",
                          peerinfo->hostname);
        ret = -1;
        goto out;
    }

    ret = 0;
out:
    if (peerinfo)
        gf_msg_debug((this ? this->name : "glusterd"), 0, "Peer %s %s",
                     peerinfo->hostname, ((ret < 0) ? "rejected" : "accepted"));
    return ret;
}

int
__glusterd_mgmt_hndsk_version_ack_cbk(struct rpc_req *req, struct iovec *iov,
                                      int count, void *myframe)
{
    int ret = -1;
    gf_mgmt_hndsk_rsp rsp = {
        0,
    };
    xlator_t *this = NULL;
    call_frame_t *frame = NULL;
    glusterd_peerinfo_t *peerinfo = NULL;
    glusterd_peerctx_t *peerctx = NULL;
    char msg[64] = {
        0,
    };

    this = THIS;
    frame = myframe;
    peerctx = frame->local;

    RCU_READ_LOCK;
    peerinfo = glusterd_peerinfo_find_by_generation(peerctx->peerinfo_gen);
    if (!peerinfo) {
        gf_msg_debug(this->name, 0, "Could not find peer %s(%s)",
                     peerctx->peername, uuid_utoa(peerctx->peerid));
        ret = -1;
        goto out;
    }

    if (-1 == req->rpc_status) {
        snprintf(msg, sizeof(msg),
                 "Error through RPC layer, retry again later");
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_RPC_LAYER_ERROR, "%s", msg);
        peerctx->errstr = gf_strdup(msg);
        goto out;
    }

    ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_mgmt_hndsk_rsp);
    if (ret < 0) {
        snprintf(msg, sizeof(msg), "Failed to decode XDR");
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_REQ_DECODE_FAIL, "%s", msg);
        peerctx->errstr = gf_strdup(msg);
        goto out;
    }

    if (-1 == rsp.op_ret) {
        ret = -1;
        snprintf(msg, sizeof(msg),
                 "Failed to get handshake ack from remote server");
        gf_msg(frame->this->name, GF_LOG_ERROR, 0, GD_MSG_NO_HANDSHAKE_ACK,
               "%s", msg);
        peerctx->errstr = gf_strdup(msg);
        goto out;
    }

    /* TODO: this is hardcoded as of now, but I don't forsee any problems
     * with this as long as we are properly handshaking operating versions
     */
    peerinfo->mgmt = &gd_mgmt_prog;
    peerinfo->peer = &gd_peer_prog;
    peerinfo->mgmt_v3 = &gd_mgmt_v3_prog;

    ret = default_notify(this, GF_EVENT_CHILD_UP, NULL);

    if (GD_MODE_ON == peerctx->args.mode) {
        (void)glusterd_event_connected_inject(peerctx);
        peerctx->args.req = NULL;
    } else if (GD_MODE_SWITCH_ON == peerctx->args.mode) {
        peerctx->args.mode = GD_MODE_ON;
    } else {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_UNKNOWN_MODE,
               "unknown mode %d", peerctx->args.mode);
    }

    ret = 0;
out:

    if (ret != 0 && peerinfo)
        rpc_transport_disconnect(peerinfo->rpc->conn.trans, _gf_false);

    RCU_READ_UNLOCK;

    frame->local = NULL;
    STACK_DESTROY(frame->root);

    if (rsp.hndsk.hndsk_val)
        free(rsp.hndsk.hndsk_val);

    glusterd_friend_sm();

    return 0;
}

int
glusterd_mgmt_hndsk_version_ack_cbk(struct rpc_req *req, struct iovec *iov,
                                    int count, void *myframe)
{
    return glusterd_big_locked_cbk(req, iov, count, myframe,
                                   __glusterd_mgmt_hndsk_version_ack_cbk);
}

int
__glusterd_mgmt_hndsk_version_cbk(struct rpc_req *req, struct iovec *iov,
                                  int count, void *myframe)
{
    int ret = -1;
    int op_errno = EINVAL;
    gf_mgmt_hndsk_rsp rsp = {
        0,
    };
    gf_mgmt_hndsk_req arg = {{
        0,
    }};
    xlator_t *this = NULL;
    call_frame_t *frame = NULL;
    glusterd_peerinfo_t *peerinfo = NULL;
    glusterd_peerctx_t *peerctx = NULL;
    dict_t *dict = NULL;
    dict_t *rsp_dict = NULL;
    glusterd_conf_t *conf = NULL;
    char msg[64] = {
        0,
    };

    this = THIS;
    conf = this->private;
    frame = myframe;
    peerctx = frame->local;

    RCU_READ_LOCK;

    peerinfo = glusterd_peerinfo_find_by_generation(peerctx->peerinfo_gen);
    if (!peerinfo) {
        ret = -1;
        gf_msg_debug(this->name, 0, "Could not find peer %s(%s)",
                     peerctx->peername, uuid_utoa(peerctx->peerid));
        goto out;
    }

    if (-1 == req->rpc_status) {
        ret = -1;
        snprintf(msg, sizeof(msg),
                 "Error through RPC layer, retry again later");
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_RPC_LAYER_ERROR, "%s", msg);
        peerctx->errstr = gf_strdup(msg);
        goto out;
    }

    ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_mgmt_hndsk_rsp);
    if (ret < 0) {
        snprintf(msg, sizeof(msg),
                 "Failed to decode management "
                 "handshake response");
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_REQ_DECODE_FAIL, "%s", msg);
        peerctx->errstr = gf_strdup(msg);
        goto out;
    }

    GF_PROTOCOL_DICT_UNSERIALIZE(this, dict, rsp.hndsk.hndsk_val,
                                 rsp.hndsk.hndsk_len, ret, op_errno, out);

    op_errno = rsp.op_errno;
    if (-1 == rsp.op_ret) {
        gf_msg(this->name, GF_LOG_ERROR, op_errno, GD_MSG_VERS_GET_FAIL,
               "failed to get the 'versions' from peer (%s)",
               req->conn->trans->peerinfo.identifier);
        goto out;
    }

    /* Check if peer can be part of cluster */
    ret = gd_validate_peer_op_version(this, peerinfo, dict, &peerctx->errstr);
    if (ret < 0) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_OP_VERSION_MISMATCH,
               "failed to validate the operating version of peer (%s)",
               peerinfo->hostname);
        goto out;
    }

    rsp_dict = dict_new();
    if (!rsp_dict)
        goto out;

    ret = dict_set_int32(rsp_dict, GD_OP_VERSION_KEY, conf->op_version);
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_SET_FAILED,
               "failed to set operating version in dict");
        goto out;
    }

    GF_PROTOCOL_DICT_SERIALIZE(this, rsp_dict, (&arg.hndsk.hndsk_val),
                               arg.hndsk.hndsk_len, op_errno, out);

    ret = glusterd_submit_request(
        peerinfo->rpc, &arg, frame, &gd_clnt_mgmt_hndsk_prog,
        GD_MGMT_HNDSK_VERSIONS_ACK, NULL, this,
        glusterd_mgmt_hndsk_version_ack_cbk, (xdrproc_t)xdr_gf_mgmt_hndsk_req);

out:
    if (ret) {
        frame->local = NULL;
        STACK_DESTROY(frame->root);
        if (peerinfo)
            rpc_transport_disconnect(peerinfo->rpc->conn.trans, _gf_false);
    }

    RCU_READ_UNLOCK;

    if (rsp.hndsk.hndsk_val)
        free(rsp.hndsk.hndsk_val);

    if (arg.hndsk.hndsk_val)
        GF_FREE(arg.hndsk.hndsk_val);

    if (dict)
        dict_unref(dict);

    if (rsp_dict)
        dict_unref(rsp_dict);

    return 0;
}

int
glusterd_mgmt_hndsk_version_cbk(struct rpc_req *req, struct iovec *iov,
                                int count, void *myframe)
{
    return glusterd_big_locked_cbk(req, iov, count, myframe,
                                   __glusterd_mgmt_hndsk_version_cbk);
}

int
glusterd_mgmt_handshake(xlator_t *this, glusterd_peerctx_t *peerctx)
{
    call_frame_t *frame = NULL;
    gf_mgmt_hndsk_req req = {
        {
            0,
        },
    };
    glusterd_peerinfo_t *peerinfo = NULL;
    dict_t *req_dict = NULL;
    int ret = -1;

    frame = create_frame(this, this->ctx->pool);
    if (!frame)
        goto out;

    frame->local = peerctx;

    req_dict = dict_new();
    if (!req_dict)
        goto out;

    ret = dict_set_dynstr(req_dict, GD_PEER_ID_KEY,
                          gf_strdup(uuid_utoa(MY_UUID)));
    if (ret) {
        gf_msg(this->name, GF_LOG_ERROR, errno, GD_MSG_DICT_SET_FAILED,
               "failed to set peer ID in dict");
        goto out;
    }

    GF_PROTOCOL_DICT_SERIALIZE(this, req_dict, (&req.hndsk.hndsk_val),
                               req.hndsk.hndsk_len, ret, out);

    RCU_READ_LOCK;

    peerinfo = glusterd_peerinfo_find_by_generation(peerctx->peerinfo_gen);
    if (!peerinfo) {
        RCU_READ_UNLOCK;
        gf_msg_debug(THIS->name, 0, "Could not find peer %s(%s)",
                     peerctx->peername, uuid_utoa(peerctx->peerid));
        goto out;
    }

    ret = glusterd_submit_request(
        peerinfo->rpc, &req, frame, &gd_clnt_mgmt_hndsk_prog,
        GD_MGMT_HNDSK_VERSIONS, NULL, this, glusterd_mgmt_hndsk_version_cbk,
        (xdrproc_t)xdr_gf_mgmt_hndsk_req);

    RCU_READ_UNLOCK;

    ret = 0;

out:
    if (req_dict)
        dict_unref(req_dict);

    if (ret && frame)
        STACK_DESTROY(frame->root);

    return ret;
}

int
glusterd_set_clnt_mgmt_program(glusterd_peerinfo_t *peerinfo,
                               gf_prog_detail *prog)
{
    gf_prog_detail *trav = NULL;
    int ret = -1;

    if (!peerinfo || !prog)
        goto out;

    trav = prog;

    while (trav) {
        ret = -1;
        if ((gd_mgmt_prog.prognum == trav->prognum) &&
            (gd_mgmt_prog.progver == trav->progver)) {
            peerinfo->mgmt = &gd_mgmt_prog;
            ret = 0;
        }

        if ((gd_peer_prog.prognum == trav->prognum) &&
            (gd_peer_prog.progver == trav->progver)) {
            peerinfo->peer = &gd_peer_prog;
            ret = 0;
        }

        if (ret) {
            gf_msg_debug("glusterd", 0,
                         "%s (%" PRId64 ":%" PRId64 ") not supported",
                         trav->progname, trav->prognum, trav->progver);
        }

        trav = trav->next;
    }

    if (peerinfo->mgmt) {
        gf_msg("glusterd", GF_LOG_INFO, 0, GD_MSG_VERS_INFO,
               "Using Program %s, Num (%d), Version (%d)",
               peerinfo->mgmt->progname, peerinfo->mgmt->prognum,
               peerinfo->mgmt->progver);
    }

    if (peerinfo->peer) {
        gf_msg("glusterd", GF_LOG_INFO, 0, GD_MSG_VERS_INFO,
               "Using Program %s, Num (%d), Version (%d)",
               peerinfo->peer->progname, peerinfo->peer->prognum,
               peerinfo->peer->progver);
    }

    if (peerinfo->mgmt_v3) {
        gf_msg("glusterd", GF_LOG_INFO, 0, GD_MSG_VERS_INFO,
               "Using Program %s, Num (%d), Version (%d)",
               peerinfo->mgmt_v3->progname, peerinfo->mgmt_v3->prognum,
               peerinfo->mgmt_v3->progver);
    }

    ret = 0;
out:
    return ret;
}

static gf_boolean_t
_mgmt_hndsk_prog_present(gf_prog_detail *prog)
{
    gf_boolean_t ret = _gf_false;
    gf_prog_detail *trav = NULL;

    GF_ASSERT(prog);

    trav = prog;

    while (trav) {
        if ((trav->prognum == GD_MGMT_HNDSK_PROGRAM) &&
            (trav->progver == GD_MGMT_HNDSK_VERSION)) {
            ret = _gf_true;
            goto out;
        }
        trav = trav->next;
    }
out:
    return ret;
}

int
__glusterd_peer_dump_version_cbk(struct rpc_req *req, struct iovec *iov,
                                 int count, void *myframe)
{
    int ret = -1;
    gf_dump_rsp rsp = {
        0,
    };
    xlator_t *this = NULL;
    gf_prog_detail *trav = NULL;
    gf_prog_detail *next = NULL;
    call_frame_t *frame = NULL;
    glusterd_peerinfo_t *peerinfo = NULL;
    glusterd_peerctx_t *peerctx = NULL;
    glusterd_conf_t *conf = NULL;
    char msg[1024] = {
        0,
    };

    this = THIS;
    conf = this->private;
    frame = myframe;
    peerctx = frame->local;

    RCU_READ_LOCK;

    peerinfo = glusterd_peerinfo_find_by_generation(peerctx->peerinfo_gen);
    if (!peerinfo) {
        gf_msg_debug(this->name, 0, "Couldn't find peer %s(%s)",
                     peerctx->peername, uuid_utoa(peerctx->peerid));
        goto out;
    }

    if (-1 == req->rpc_status) {
        snprintf(msg, sizeof(msg),
                 "Error through RPC layer, retry again later");
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_RPC_LAYER_ERROR, "%s", msg);
        peerctx->errstr = gf_strdup(msg);
        goto out;
    }

    ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_dump_rsp);
    if (ret < 0) {
        snprintf(msg, sizeof(msg), "Failed to decode XDR");
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_REQ_DECODE_FAIL, "%s", msg);
        peerctx->errstr = gf_strdup(msg);
        goto out;
    }
    if (-1 == rsp.op_ret) {
        snprintf(msg, sizeof(msg),
                 "Failed to get the 'versions' from remote server");
        gf_msg(frame->this->name, GF_LOG_ERROR, 0, GD_MSG_VERS_GET_FAIL, "%s",
               msg);
        peerctx->errstr = gf_strdup(msg);
        goto out;
    }

    if (_mgmt_hndsk_prog_present(rsp.prog)) {
        gf_msg_debug(this->name, 0,
                     "Proceeding to op-version handshake with peer %s",
                     peerinfo->hostname);
        ret = glusterd_mgmt_handshake(this, peerctx);
        goto out;
    } else if (conf->op_version > 1) {
        ret = -1;
        snprintf(msg, sizeof(msg),
                 "Peer %s does not support required op-version",
                 peerinfo->hostname);
        peerctx->errstr = gf_strdup(msg);
        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_VERSION_UNSUPPORTED, "%s",
               msg);
        goto out;
    }

    /* Make sure we assign the proper program to peer */
    ret = glusterd_set_clnt_mgmt_program(peerinfo, rsp.prog);
    if (ret) {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_MGMT_PGM_SET_FAIL,
               "failed to set the mgmt program");
        goto out;
    }

    ret = default_notify(this, GF_EVENT_CHILD_UP, NULL);

    if (GD_MODE_ON == peerctx->args.mode) {
        (void)glusterd_event_connected_inject(peerctx);
        peerctx->args.req = NULL;
    } else if (GD_MODE_SWITCH_ON == peerctx->args.mode) {
        peerctx->args.mode = GD_MODE_ON;
    } else {
        gf_msg(this->name, GF_LOG_WARNING, 0, GD_MSG_UNKNOWN_MODE,
               "unknown mode %d", peerctx->args.mode);
    }

    ret = 0;

out:
    if (ret != 0 && peerinfo)
        rpc_transport_disconnect(peerinfo->rpc->conn.trans, _gf_false);

    RCU_READ_UNLOCK;

    glusterd_friend_sm();
    glusterd_op_sm();

    /* don't use GF_FREE, buffer was allocated by libc */
    if (rsp.prog) {
        trav = rsp.prog;
        while (trav) {
            next = trav->next;
            free(trav->progname);
            free(trav);
            trav = next;
        }
    }

    frame->local = NULL;
    STACK_DESTROY(frame->root);

    return 0;
}

int
glusterd_peer_dump_version_cbk(struct rpc_req *req, struct iovec *iov,
                               int count, void *myframe)
{
    return glusterd_big_locked_cbk(req, iov, count, myframe,
                                   __glusterd_peer_dump_version_cbk);
}

int
glusterd_peer_dump_version(xlator_t *this, struct rpc_clnt *rpc,
                           glusterd_peerctx_t *peerctx)
{
    call_frame_t *frame = NULL;
    gf_dump_req req = {
        0,
    };
    glusterd_peerinfo_t *peerinfo = NULL;
    int ret = -1;

    frame = create_frame(this, this->ctx->pool);
    if (!frame)
        goto out;

    frame->local = peerctx;
    if (!peerctx)
        goto out;

    RCU_READ_LOCK;

    peerinfo = glusterd_peerinfo_find_by_generation(peerctx->peerinfo_gen);
    if (!peerinfo) {
        RCU_READ_UNLOCK;
        gf_msg_debug(this->name, 0, "Couldn't find peer %s(%s)",
                     peerctx->peername, uuid_utoa(peerctx->peerid));
        goto out;
    }

    req.gfs_id = 0xcafe;

    ret = glusterd_submit_request(
        peerinfo->rpc, &req, frame, &glusterd_dump_prog, GF_DUMP_DUMP, NULL,
        this, glusterd_peer_dump_version_cbk, (xdrproc_t)xdr_gf_dump_req);

    RCU_READ_UNLOCK;
out:
    if (ret && frame)
        STACK_DESTROY(frame->root);

    return ret;
}