summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/dht/src/dht-helper.c
blob: b81e438af98309a1399ea59569f61d08cb5145b2 (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
/*
  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.h"
#include "xlator.h"
#include "dht-common.h"
#include "dht-lock.h"

static void
dht_free_fd_ctx (dht_fd_ctx_t *fd_ctx)
{
        GF_FREE (fd_ctx);
}


int32_t
dht_fd_ctx_destroy (xlator_t *this, fd_t *fd)
{
        dht_fd_ctx_t *fd_ctx  = NULL;
        uint64_t      value   = 0;
        int32_t       ret     = -1;

        GF_VALIDATE_OR_GOTO ("dht", this, out);
        GF_VALIDATE_OR_GOTO (this->name, fd, out);

        ret = fd_ctx_del (fd, this, &value);
        if (ret) {
                goto out;
        }

        fd_ctx = (dht_fd_ctx_t *)value;
        if (fd_ctx) {
                GF_REF_PUT (fd_ctx);
        }
out:
        return ret;
}


static int
__dht_fd_ctx_set (xlator_t *this, fd_t *fd, xlator_t *dst)
{
        dht_fd_ctx_t *fd_ctx  = NULL;
        uint64_t      value   = 0;
        int           ret     = -1;

        GF_VALIDATE_OR_GOTO ("dht", this, out);
        GF_VALIDATE_OR_GOTO (this->name, fd, out);

        fd_ctx = GF_CALLOC (1, sizeof (*fd_ctx), gf_dht_mt_fd_ctx_t);

        if (!fd_ctx) {
                goto out;
        }

        fd_ctx->opened_on_dst = (uint64_t) dst;
        GF_REF_INIT (fd_ctx, dht_free_fd_ctx);

        value = (uint64_t) fd_ctx;

        ret = __fd_ctx_set (fd, this, value);
        if (ret < 0) {
                gf_msg (this->name, GF_LOG_WARNING, 0,
                        DHT_MSG_FD_CTX_SET_FAILED,
                        "Failed to set fd ctx in fd=0x%p", fd);
                GF_REF_PUT (fd_ctx);
        }
out:
        return ret;
}



int
dht_fd_ctx_set (xlator_t *this, fd_t *fd, xlator_t *dst)
{
        dht_fd_ctx_t *fd_ctx  = NULL;
        uint64_t      value   = 0;
        int           ret     = -1;

        GF_VALIDATE_OR_GOTO ("dht", this, out);
        GF_VALIDATE_OR_GOTO (this->name, fd, out);

        LOCK (&fd->lock);
        {
                ret = __fd_ctx_get (fd, this, &value);
                if (ret && value) {

                        fd_ctx = (dht_fd_ctx_t *) value;
                        if (fd_ctx->opened_on_dst == (uint64_t) dst)  {
                                /* This could happen due to racing
                                 * check_progress tasks*/
                                goto unlock;
                        } else {
                                /* This would be a big problem*/
                                gf_msg (this->name, GF_LOG_WARNING, 0,
                                        DHT_MSG_INVALID_VALUE,
                                        "Different dst found in the fd ctx");

                                /* Overwrite and hope for the best*/
                                fd_ctx->opened_on_dst = (uint64_t)dst;
                                goto unlock;
                        }

                }
                ret = __dht_fd_ctx_set (this, fd, dst);
        }
unlock:
        UNLOCK (&fd->lock);
out:
        return ret;
}



static
dht_fd_ctx_t *
dht_fd_ctx_get (xlator_t *this, fd_t *fd)
{
        dht_fd_ctx_t *fd_ctx  = NULL;
        int           ret     = -1;
        uint64_t      tmp_val = 0;

        GF_VALIDATE_OR_GOTO ("dht", this, out);
        GF_VALIDATE_OR_GOTO (this->name, fd, out);

        LOCK (&fd->lock);
        {
                ret = __fd_ctx_get (fd, this, &tmp_val);
                if ((ret < 0) || (tmp_val == 0)) {
                        UNLOCK (&fd->lock);
                        goto out;
                }

                fd_ctx = (dht_fd_ctx_t *)tmp_val;
                GF_REF_GET (fd_ctx);
        }
        UNLOCK (&fd->lock);

out:
        return fd_ctx;
}

gf_boolean_t
dht_fd_open_on_dst (xlator_t *this, fd_t *fd, xlator_t *dst)
{
        dht_fd_ctx_t  *fd_ctx  = NULL;
        gf_boolean_t   opened  = _gf_false;

        fd_ctx = dht_fd_ctx_get (this, fd);

        if (fd_ctx) {
                if (fd_ctx->opened_on_dst == (uint64_t) dst) {
                        opened = _gf_true;
                }
                GF_REF_PUT (fd_ctx);
        }

        return opened;
}


void
dht_free_mig_info (void *data)
{
        dht_migrate_info_t *miginfo = NULL;

        miginfo = data;
        GF_FREE (miginfo);

        return;
}

static int
dht_inode_ctx_set_mig_info (xlator_t *this, inode_t *inode,
                            xlator_t *src_subvol, xlator_t *dst_subvol)
{
        dht_migrate_info_t *miginfo = NULL;
        uint64_t            value   = 0;
        int                 ret     = -1;

        miginfo = GF_CALLOC (1, sizeof (*miginfo), gf_dht_mt_miginfo_t);
        if (miginfo == NULL)
                goto out;

        miginfo->src_subvol = src_subvol;
        miginfo->dst_subvol = dst_subvol;
        GF_REF_INIT (miginfo, dht_free_mig_info);

        value = (uint64_t) miginfo;

        ret = inode_ctx_set1 (inode, this, &value);
        if (ret < 0) {
                GF_REF_PUT (miginfo);
        }

out:
        return ret;
}


int
dht_inode_ctx_get_mig_info (xlator_t *this, inode_t *inode,
                            xlator_t **src_subvol, xlator_t **dst_subvol)
{
        int                 ret         = -1;
        uint64_t            tmp_miginfo = 0;
        dht_migrate_info_t *miginfo     = NULL;

        LOCK (&inode->lock);
        {
                ret =  __inode_ctx_get1 (inode, this, &tmp_miginfo);
                if ((ret < 0) || (tmp_miginfo == 0)) {
                        UNLOCK (&inode->lock);
                        goto out;
                }

                miginfo = (dht_migrate_info_t *)tmp_miginfo;
                GF_REF_GET (miginfo);
        }
        UNLOCK (&inode->lock);

        if (src_subvol)
                *src_subvol = miginfo->src_subvol;

        if (dst_subvol)
                *dst_subvol = miginfo->dst_subvol;

        GF_REF_PUT (miginfo);

out:
        return ret;
}

gf_boolean_t
dht_mig_info_is_invalid (xlator_t *current, xlator_t *src_subvol,
                      xlator_t *dst_subvol)
{

/* Not set
 */
        if (!src_subvol || !dst_subvol)
                return _gf_true;

/* Invalid scenarios:
 * The src_subvol does not match the subvol on which the current op was sent
 * so the cached subvol has changed between the last mig_info_set and now.
 * src_subvol == dst_subvol. The file was migrated without any FOP detecting
 * a P2 so the old dst is now the current subvol.
 *
 * There is still one scenario where the info could be outdated - if
 * file has undergone multiple migrations and ends up on the same src_subvol
 * on which the mig_info was first set.
 */
        if ((current == dst_subvol) || (current != src_subvol))
                return _gf_true;

        return _gf_false;
}



/* Used to check if fd fops have the fd opened on the cached subvol
 * This is required when:
 * 1. an fd is opened on FILE1 on subvol1
 * 2. the file is migrated to subvol2
 * 3. a lookup updates the cached subvol in the inode_ctx to subvol2
 * 4. a write comes on the fd
 * The write is sent to subvol2 on an fd which has been opened only on fd1
 * Since the migration phase checks don't kick in, the fop fails with EBADF
 *
 */


int
dht_check_and_open_fd_on_subvol_complete (int ret, call_frame_t *frame,
                                          void *data)
{
        glusterfs_fop_t     fop     = 0;
        dht_local_t        *local   = NULL;
        xlator_t           *subvol  = NULL;
        xlator_t           *this  = NULL;
        fd_t               *fd      = NULL;
        int                 op_errno = -1;

        local = frame->local;
        this = frame->this;
        fop = local->fop;
        subvol = local->cached_subvol;
        fd = local->fd;

        if (ret) {
                op_errno = local->op_errno;
                goto handle_err;
        }

        switch (fop) {

        case GF_FOP_WRITE:

                STACK_WIND_COOKIE (frame, dht_writev_cbk, subvol, subvol,
                                   subvol->fops->writev, fd,
                                   local->rebalance.vector,
                                   local->rebalance.count,
                                   local->rebalance.offset,
                                   local->rebalance.flags,
                                   local->rebalance.iobref, local->xattr_req);
                break;

        case GF_FOP_FLUSH:

                STACK_WIND (frame, dht_flush_cbk, subvol,
                            subvol->fops->flush, fd, local->xattr_req);
                break;

        case GF_FOP_FSETATTR:

                STACK_WIND_COOKIE (frame, dht_file_setattr_cbk, subvol,
                                   subvol, subvol->fops->fsetattr, fd,
                                   &local->rebalance.stbuf,
                                   local->rebalance.flags,
                                   local->xattr_req);
                break;

        case GF_FOP_ZEROFILL:
                STACK_WIND_COOKIE (frame, dht_zerofill_cbk, subvol, subvol,
                                   subvol->fops->zerofill, fd,
                                   local->rebalance.offset,
                                   local->rebalance.size, local->xattr_req);

                break;

        case GF_FOP_DISCARD:
                STACK_WIND_COOKIE (frame, dht_discard_cbk, subvol, subvol,
                                   subvol->fops->discard, local->fd,
                                   local->rebalance.offset,
                                   local->rebalance.size,
                                   local->xattr_req);
                break;

        case GF_FOP_FALLOCATE:
                STACK_WIND_COOKIE (frame, dht_fallocate_cbk, subvol, subvol,
                                   subvol->fops->fallocate, fd,
                                   local->rebalance.flags,
                                   local->rebalance.offset,
                                   local->rebalance.size,
                                   local->xattr_req);
                break;

        case GF_FOP_FTRUNCATE:
                STACK_WIND_COOKIE (frame, dht_truncate_cbk, subvol, subvol,
                                   subvol->fops->ftruncate, fd,
                                   local->rebalance.offset, local->xattr_req);
                break;

        case GF_FOP_FSYNC:
                STACK_WIND_COOKIE (frame, dht_fsync_cbk, subvol, subvol,
                                   subvol->fops->fsync, local->fd,
                                   local->rebalance.flags, local->xattr_req);
                break;

        case GF_FOP_READ:
                STACK_WIND (frame, dht_readv_cbk, subvol, subvol->fops->readv,
                            local->fd, local->rebalance.size,
                            local->rebalance.offset,
                            local->rebalance.flags, local->xattr_req);
                break;

        case GF_FOP_FSTAT:
                STACK_WIND_COOKIE (frame, dht_file_attr_cbk, subvol,
                                   subvol, subvol->fops->fstat, fd,
                                   local->xattr_req);
                break;

        default:
                gf_msg (this->name, GF_LOG_ERROR, 0,
                        DHT_MSG_UNKNOWN_FOP,
                        "Unknown FOP on fd (%p) on file %s @ %s",
                        fd, uuid_utoa (fd->inode->gfid),
                        subvol->name);
                break;

        }

        goto out;

        /* Could not open the fd on the dst. Unwind */

handle_err:

        switch (fop) {

        case GF_FOP_WRITE:
                DHT_STACK_UNWIND (writev, frame, -1,
                                  op_errno, NULL, NULL, NULL);
                break;

        case GF_FOP_FLUSH:
                DHT_STACK_UNWIND (flush, frame, -1, op_errno, NULL);
                break;

        case GF_FOP_FSETATTR:
                DHT_STACK_UNWIND (fsetattr, frame, -1, op_errno,
                                  NULL, NULL, NULL);
                break;

        case GF_FOP_ZEROFILL:
                DHT_STACK_UNWIND (zerofill, frame, -1, op_errno,
                                  NULL, NULL, NULL);
                break;

        case GF_FOP_DISCARD:
                DHT_STACK_UNWIND (discard, frame, -1, op_errno,
                                  NULL, NULL, NULL);
                break;

        case GF_FOP_FALLOCATE:
                DHT_STACK_UNWIND (fallocate, frame, -1, op_errno,
                                  NULL, NULL, NULL);
                break;

        case GF_FOP_FTRUNCATE:
                DHT_STACK_UNWIND (ftruncate, frame, -1, op_errno,
                                  NULL, NULL, NULL);
                break;

        case GF_FOP_FSYNC:
                DHT_STACK_UNWIND (fsync, frame, -1, op_errno, NULL, NULL, NULL);
                break;

        case GF_FOP_READ:
                DHT_STACK_UNWIND (readv, frame, -1, op_errno, NULL,
                                  0, NULL, NULL, NULL);
                break;

        case GF_FOP_FSTAT:
                DHT_STACK_UNWIND (fstat, frame, -1, op_errno, NULL, NULL);
                break;

        default:
                gf_msg (this->name, GF_LOG_ERROR, 0,
                        DHT_MSG_UNKNOWN_FOP,
                        "Unknown FOP on fd (%p) on file %s @ %s",
                        fd, uuid_utoa (fd->inode->gfid),
                        subvol->name);
                break;
        }

out:

        return 0;

}


/* Check once again if the fd has been opened on the cached subvol.
 * If not, open and update the fd_ctx.
 */

int
dht_check_and_open_fd_on_subvol_task (void *data)
{
        loc_t          loc        = {0,};
        int            ret        = -1;
        call_frame_t  *frame      = NULL;
        dht_local_t   *local      = NULL;
        fd_t          *fd         = NULL;
        xlator_t      *this       = NULL;
        xlator_t      *subvol     = NULL;


        frame = data;
        local = frame->local;
        this = THIS;
        fd = local->fd;
        subvol = local->cached_subvol;

        local->fd_checked = _gf_true;

        if (fd_is_anonymous (fd) || dht_fd_open_on_dst (this, fd, subvol)) {
                ret = 0;
                goto out;
        }

        gf_msg_debug (this->name, 0,
                      "Opening fd (%p, flags=0%o) on file %s @ %s",
                      fd, fd->flags, uuid_utoa (fd->inode->gfid),
                      subvol->name);


        loc.inode = inode_ref (fd->inode);
        gf_uuid_copy (loc.gfid, fd->inode->gfid);

        /* Open this on the dst subvol */

        SYNCTASK_SETID(0, 0);

        ret = syncop_open (subvol, &loc,
                           (fd->flags & ~(O_CREAT | O_EXCL | O_TRUNC)),
                           fd, NULL, NULL);

        if (ret < 0) {

                gf_msg (this->name, GF_LOG_ERROR, -ret,
                        DHT_MSG_OPEN_FD_ON_DST_FAILED,
                        "Failed to open the fd"
                        " (%p, flags=0%o) on file %s @ %s",
                        fd, fd->flags, uuid_utoa (fd->inode->gfid),
                        subvol->name);
                /* This can happen if the cached subvol was updated in the
                 * inode_ctx and the fd was opened on the new cached suvol
                 * after this fop was wound on the old cached subvol.
                 * As we do not close the fd on the old subvol (a leak)
                 * don't treat ENOENT as an error and allow the phase1/phase2
                 * checks to handle it.
                 */

                if ((-ret != ENOENT) && (-ret != ESTALE)) {
                        local->op_errno = -ret;
                        ret = -1;
                } else {
                        ret = 0;
                }

                local->op_errno = -ret;
                ret = -1;

        } else {
                dht_fd_ctx_set (this, fd, subvol);
        }

        SYNCTASK_SETID (frame->root->uid, frame->root->gid);
out:
        loc_wipe (&loc);

        return ret;
}


int
dht_check_and_open_fd_on_subvol (xlator_t *this, call_frame_t *frame)
{
        int ret            = -1;
        dht_local_t *local = NULL;

/*
        if (dht_fd_open_on_dst (this, fd, subvol))
                goto out;
*/
        local = frame->local;

        ret = synctask_new (this->ctx->env,
                            dht_check_and_open_fd_on_subvol_task,
                            dht_check_and_open_fd_on_subvol_complete,
                            frame, frame);

        if (ret) {
                gf_msg (this->name, GF_LOG_ERROR, 0, 0,
                        "Failed to create synctask"
                        " to check and open fd=%p", local->fd);
        }

        return ret;
}



int
dht_frame_return (call_frame_t *frame)
{
        dht_local_t *local = NULL;
        int          this_call_cnt = -1;

        if (!frame)
                return -1;

        local = frame->local;

        LOCK (&frame->lock);
        {
                this_call_cnt = --local->call_cnt;
        }
        UNLOCK (&frame->lock);

        return this_call_cnt;
}


int
dht_filter_loc_subvol_key (xlator_t *this, loc_t *loc, loc_t *new_loc,
                           xlator_t **subvol)
{
        char          *new_name  = NULL;
        char          *new_path  = NULL;
        xlator_list_t *trav      = NULL;
        char           key[1024] = {0,};
        int            ret       = 0; /* not found */

        /* Why do other tasks if first required 'char' itself is not there */
        if (!new_loc || !loc || !loc->name || !strchr (loc->name, '@')) {
                /* Skip the GF_FREE checks here */
                return ret;
        }

        trav = this->children;
        while (trav) {
                snprintf (key, 1024, "*@%s:%s", this->name, trav->xlator->name);
                if (fnmatch (key, loc->name, FNM_NOESCAPE) == 0) {
                        new_name = GF_CALLOC(strlen (loc->name),
                                             sizeof (char),
                                             gf_common_mt_char);
                        if (!new_name)
                                goto out;
                        if (fnmatch (key, loc->path, FNM_NOESCAPE) == 0) {
                                new_path = GF_CALLOC(strlen (loc->path),
                                                     sizeof (char),
                                                     gf_common_mt_char);
                                if (!new_path)
                                        goto out;
                                strncpy (new_path, loc->path, (strlen (loc->path) -
                                                               strlen (key) + 1));
                        }
                        strncpy (new_name, loc->name, (strlen (loc->name) -
                                                       strlen (key) + 1));

                        if (new_loc) {
                                new_loc->path   = ((new_path) ? new_path:
                                                   gf_strdup (loc->path));
                                new_loc->name   = new_name;
                                new_loc->inode  = inode_ref (loc->inode);
                                new_loc->parent = inode_ref (loc->parent);
                        }
                        *subvol         = trav->xlator;
                        ret = 1;  /* success */
                        goto out;
                }
                trav = trav->next;
        }
out:
        if (!ret) {
                /* !success */
                GF_FREE (new_path);
                GF_FREE (new_name);
        }
        return ret;
}

static xlator_t *
dht_get_subvol_from_id(xlator_t *this, int client_id)
{
        xlator_t   *xl   = NULL;
        dht_conf_t *conf = NULL;
        char       *sid  = NULL;
        int32_t     ret  = -1;

        conf = this->private;

        ret = gf_asprintf(&sid, "%d", client_id);
        if (ret == -1) {
                gf_msg (this->name, GF_LOG_ERROR, 0,
                        DHT_MSG_ASPRINTF_FAILED, "asprintf failed while "
                        "fetching subvol from the id");
                goto out;
        }

        if (dict_get_ptr(conf->leaf_to_subvol, sid, (void **) &xl))
                xl = NULL;

        GF_FREE (sid);

out:
        return xl;

}

int
dht_deitransform (xlator_t *this, uint64_t y, xlator_t **subvol_p)
{
        int         client_id = 0;
        xlator_t   *subvol = 0;
        dht_conf_t *conf = NULL;

        if (!this->private)
                return -1;

        conf = this->private;

        client_id = gf_deitransform(this, y);

        subvol = dht_get_subvol_from_id(this, client_id);

        if (!subvol)
                subvol = conf->subvolumes[0];

        if (subvol_p)
                *subvol_p = subvol;

        return 0;
}

void
dht_local_wipe (xlator_t *this, dht_local_t *local)
{
        int i = 0;

        if (!local)
                return;

        loc_wipe (&local->loc);
        loc_wipe (&local->loc2);

        if (local->xattr)
                dict_unref (local->xattr);

        if (local->inode)
                inode_unref (local->inode);

        if (local->layout) {
                dht_layout_unref (this, local->layout);
                local->layout = NULL;
        }

        loc_wipe (&local->linkfile.loc);

        if (local->linkfile.xattr)
                dict_unref (local->linkfile.xattr);

        if (local->linkfile.inode)
                inode_unref (local->linkfile.inode);

        if (local->fd) {
                fd_unref (local->fd);
                local->fd = NULL;
        }

        if (local->params) {
                dict_unref (local->params);
                local->params = NULL;
        }

        if (local->xattr_req)
                dict_unref (local->xattr_req);

        if (local->selfheal.layout) {
                dht_layout_unref (this, local->selfheal.layout);
                local->selfheal.layout = NULL;
        }

        if (local->selfheal.refreshed_layout) {
                dht_layout_unref (this, local->selfheal.refreshed_layout);
                local->selfheal.refreshed_layout = NULL;
        }

        for (i = 0; i < 2; i++) {
                dht_lock_array_free (local->lock[i].ns.parent_layout.locks,
                                     local->lock[i].ns.parent_layout.lk_count);

                GF_FREE (local->lock[i].ns.parent_layout.locks);

                dht_lock_array_free (local->lock[i].ns.directory_ns.locks,
                                     local->lock[i].ns.directory_ns.lk_count);
                GF_FREE (local->lock[i].ns.directory_ns.locks);
        }

        GF_FREE (local->key);

        if (local->rebalance.xdata)
                dict_unref (local->rebalance.xdata);

        if (local->rebalance.xattr)
                dict_unref (local->rebalance.xattr);

        GF_FREE (local->rebalance.vector);

        if (local->rebalance.iobref)
                iobref_unref (local->rebalance.iobref);

        if (local->stub) {
                call_stub_destroy (local->stub);
                local->stub = NULL;
        }

        if (local->ret_cache)
                GF_FREE (local->ret_cache);

        gf_dirent_free (&local->entries);

        mem_put (local);
}


dht_local_t *
dht_local_init (call_frame_t *frame, loc_t *loc, fd_t *fd, glusterfs_fop_t fop)
{
        dht_local_t *local = NULL;
        inode_t     *inode = NULL;
        int          ret   = 0;

        local = mem_get0 (THIS->local_pool);
        if (!local)
                goto out;

        if (loc) {
                ret = loc_copy (&local->loc, loc);
                if (ret)
                        goto out;

                inode = loc->inode;
        }

        if (fd) {
                local->fd = fd_ref (fd);
                if (!inode)
                        inode = fd->inode;
        }

        local->op_ret   = -1;
        local->op_errno = EUCLEAN;
        local->fop      = fop;

        if (inode) {
                local->layout   = dht_layout_get (frame->this, inode);
                local->cached_subvol = dht_subvol_get_cached (frame->this,
                                                              inode);
        }

        INIT_LIST_HEAD (&local->entries.list);
        frame->local = local;

out:
        if (ret) {
                if (local)
                        mem_put (local);
                local = NULL;
        }
        return local;
}

xlator_t *
dht_first_up_subvol (xlator_t *this)
{
        dht_conf_t *conf = NULL;
        xlator_t   *child = NULL;
        int         i = 0;
        time_t      time = 0;

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

        LOCK (&conf->subvolume_lock);
        {
                for (i = 0; i < conf->subvolume_cnt; i++) {
                        if (conf->subvol_up_time[i]) {
                                if (!time) {
                                        time = conf->subvol_up_time[i];
                                        child = conf->subvolumes[i];
                                } else if (time > conf->subvol_up_time[i]) {
                                        time  = conf->subvol_up_time[i];
                                        child = conf->subvolumes[i];
                                }
                        }
                }
        }
        UNLOCK (&conf->subvolume_lock);

out:
        return child;
}

xlator_t *
dht_last_up_subvol (xlator_t *this)
{
        dht_conf_t *conf = NULL;
        xlator_t   *child = NULL;
        int         i = 0;

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

        LOCK (&conf->subvolume_lock);
        {
                for (i = conf->subvolume_cnt-1; i >= 0; i--) {
                        if (conf->subvolume_status[i]) {
                                child = conf->subvolumes[i];
                                break;
                        }
                }
        }
        UNLOCK (&conf->subvolume_lock);

out:
        return child;
}

xlator_t *
dht_subvol_get_hashed (xlator_t *this, loc_t *loc)
{
        dht_layout_t *layout = NULL;
        xlator_t     *subvol = NULL;
        dht_conf_t *conf = NULL;
        dht_methods_t *methods = NULL;

        GF_VALIDATE_OR_GOTO ("dht", this, out);
        GF_VALIDATE_OR_GOTO (this->name, loc, out);

        conf = this->private;
        GF_VALIDATE_OR_GOTO (this->name, conf, out);

        methods = &(conf->methods);

        if (__is_root_gfid (loc->gfid)) {
                subvol = dht_first_up_subvol (this);
                goto out;
        }

        GF_VALIDATE_OR_GOTO (this->name, loc->parent, out);
        GF_VALIDATE_OR_GOTO (this->name, loc->name, out);

        layout = dht_layout_get (this, loc->parent);

        if (!layout) {
                gf_msg_debug (this->name, 0,
                              "Missing layout. path=%s, parent gfid =%s",
                              loc->path, uuid_utoa (loc->parent->gfid));
                goto out;
        }

        subvol = methods->layout_search (this, layout, loc->name);

        if (!subvol) {
                gf_msg_debug (this->name, 0,
                              "No hashed subvolume for path=%s",
                              loc->path);
                goto out;
        }

out:
        if (layout) {
                dht_layout_unref (this, layout);
        }

        return subvol;
}


xlator_t *
dht_subvol_get_cached (xlator_t *this, inode_t *inode)
{
        dht_layout_t *layout = NULL;
        xlator_t     *subvol = NULL;

        GF_VALIDATE_OR_GOTO (this->name, this, out);
        GF_VALIDATE_OR_GOTO (this->name, inode, out);

        layout = dht_layout_get (this, inode);

        if (!layout) {
                goto out;
        }

        subvol = layout->list[0].xlator;

out:
        if (layout) {
                dht_layout_unref (this, layout);
        }

        return subvol;
}


xlator_t *
dht_subvol_next (xlator_t *this, xlator_t *prev)
{
        dht_conf_t *conf = NULL;
        int         i = 0;
        xlator_t   *next = NULL;

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

        for (i = 0; i < conf->subvolume_cnt; i++) {
                if (conf->subvolumes[i] == prev) {
                        if ((i + 1) < conf->subvolume_cnt)
                                next = conf->subvolumes[i + 1];
                        break;
                }
        }

out:
        return next;
}

/* This func wraps around, if prev is actually the last subvol.
 */
xlator_t *
dht_subvol_next_available (xlator_t *this, xlator_t *prev)
{
        dht_conf_t *conf = NULL;
        int         i = 0;
        xlator_t   *next = NULL;

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

        for (i = 0; i < conf->subvolume_cnt; i++) {
                if (conf->subvolumes[i] == prev) {
                        /* if prev is last in conf->subvolumes, then wrap
                         * around.
                         */
                        if ((i + 1) < conf->subvolume_cnt) {
                                next = conf->subvolumes[i + 1];
                        } else {
                                next = conf->subvolumes[0];
                        }
                        break;
                }
        }

out:
        return next;
}
int
dht_subvol_cnt (xlator_t *this, xlator_t *subvol)
{
        int i = 0;
        int ret = -1;
        dht_conf_t *conf = NULL;

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

        for (i = 0; i < conf->subvolume_cnt; i++) {
                if (subvol == conf->subvolumes[i]) {
                        ret = i;
                        break;
                }
        }

out:
        return ret;
}


#define set_if_greater(a, b) do {               \
                if ((a) < (b))                  \
                        (a) = (b);              \
        } while (0)


#define set_if_greater_time(a, an, b, bn) do {                          \
                if (((a) < (b)) || (((a) == (b)) && ((an) < (bn)))){    \
                        (a) = (b);                                      \
                        (an) = (bn);                                    \
                }                                                       \
        } while (0)                                                     \


int
dht_iatt_merge (xlator_t *this, struct iatt *to,
                struct iatt *from, xlator_t *subvol)
{
        if (!from || !to)
                return 0;

        to->ia_dev      = from->ia_dev;

        gf_uuid_copy (to->ia_gfid, from->ia_gfid);

        to->ia_ino      = from->ia_ino;
        to->ia_prot     = from->ia_prot;
        to->ia_type     = from->ia_type;
        to->ia_nlink    = from->ia_nlink;
        to->ia_rdev     = from->ia_rdev;
        to->ia_size    += from->ia_size;
        to->ia_blksize  = from->ia_blksize;
        to->ia_blocks  += from->ia_blocks;

        if (IA_ISDIR (from->ia_type)) {
                to->ia_blocks = DHT_DIR_STAT_BLOCKS;
                to->ia_size = DHT_DIR_STAT_SIZE;
        }
        set_if_greater (to->ia_uid, from->ia_uid);
        set_if_greater (to->ia_gid, from->ia_gid);

        set_if_greater_time(to->ia_atime, to->ia_atime_nsec,
                            from->ia_atime, from->ia_atime_nsec);
        set_if_greater_time (to->ia_mtime, to->ia_mtime_nsec,
                             from->ia_mtime, from->ia_mtime_nsec);
        set_if_greater_time (to->ia_ctime, to->ia_ctime_nsec,
                             from->ia_ctime, from->ia_ctime_nsec);

        return 0;
}

int
dht_build_child_loc (xlator_t *this, loc_t *child, loc_t *parent, char *name)
{
        if (!child) {
                goto err;
        }

        if (strcmp (parent->path, "/") == 0)
                gf_asprintf ((char **)&child->path, "/%s", name);
        else
                gf_asprintf ((char **)&child->path, "%s/%s", parent->path, name);

        if (!child->path) {
                goto err;
        }

        child->name = strrchr (child->path, '/');
        if (child->name)
                child->name++;

        child->parent = inode_ref (parent->inode);
        child->inode = inode_new (parent->inode->table);

        if (!child->inode) {
                goto err;
        }

        return 0;
err:
        loc_wipe (child);
        return -1;
}

int
dht_init_local_subvolumes (xlator_t *this, dht_conf_t *conf)
{
        xlator_list_t *subvols = NULL;
        int            cnt = 0;

        if (!conf)
                return -1;

        for (subvols = this->children; subvols; subvols = subvols->next)
                cnt++;

        conf->local_subvols = GF_CALLOC (cnt, sizeof (xlator_t *),
                                        gf_dht_mt_xlator_t);

        /* FIX FIX : do this dynamically*/
        conf->local_nodeuuids = GF_CALLOC (cnt, sizeof (subvol_nodeuuid_t),
                                           gf_dht_nodeuuids_t);

        if (!conf->local_subvols || !conf->local_nodeuuids) {
                return -1;
        }

        conf->local_subvols_cnt = 0;

        return 0;
}

int
dht_init_subvolumes (xlator_t *this, dht_conf_t *conf)
{
        xlator_list_t *subvols = NULL;
        int            cnt = 0;

        if (!conf)
                return -1;

        for (subvols = this->children; subvols; subvols = subvols->next)
                cnt++;

        conf->subvolumes = GF_CALLOC (cnt, sizeof (xlator_t *),
                                      gf_dht_mt_xlator_t);
        if (!conf->subvolumes) {
                return -1;
        }
        conf->subvolume_cnt = cnt;

        conf->local_subvols_cnt = 0;

        dht_set_subvol_range(this);

        cnt = 0;
        for (subvols = this->children; subvols; subvols = subvols->next)
                conf->subvolumes[cnt++] = subvols->xlator;

        conf->subvolume_status = GF_CALLOC (cnt, sizeof (char),
                                            gf_dht_mt_char);
        if (!conf->subvolume_status) {
                return -1;
        }

        conf->last_event = GF_CALLOC (cnt, sizeof (int),
                                      gf_dht_mt_char);
        if (!conf->last_event) {
                return -1;
        }

        conf->subvol_up_time = GF_CALLOC (cnt, sizeof (time_t),
                                          gf_dht_mt_subvol_time);
        if (!conf->subvol_up_time) {
                return -1;
        }

        conf->du_stats = GF_CALLOC (conf->subvolume_cnt, sizeof (dht_du_t),
                                    gf_dht_mt_dht_du_t);
        if (!conf->du_stats) {
                return -1;
        }

        conf->decommissioned_bricks = GF_CALLOC (cnt, sizeof (xlator_t *),
                                                 gf_dht_mt_xlator_t);
        if (!conf->decommissioned_bricks) {
                return -1;
        }

        return 0;
}


/*
 op_ret values :
  0 : Success.
 -1 : Failure.
  1 : File is being migrated but not by this DHT layer.
*/

static int
dht_migration_complete_check_done (int op_ret, call_frame_t *frame, void *data)
{
        dht_local_t *local  = NULL;
        xlator_t    *subvol = NULL;

        local = frame->local;

        if (op_ret != 0)
                goto out;

        if (local->cached_subvol == NULL) {
                local->op_errno = EINVAL;
                goto out;
        }

        subvol = local->cached_subvol;

out:
        local->rebalance.target_op_fn (THIS, subvol, frame, op_ret);

        return 0;
}


int
dht_migration_complete_check_task (void *data)
{
        int                 ret         = -1;
        xlator_t           *src_node    = NULL;
        xlator_t           *dst_node    = NULL, *linkto_target = NULL;
        dht_local_t        *local       = NULL;
        dict_t             *dict        = NULL;
        struct iatt         stbuf       = {0,};
        xlator_t           *this        = NULL;
        call_frame_t       *frame       = NULL;
        loc_t               tmp_loc     = {0,};
        char               *path        = NULL;
        dht_conf_t         *conf        = NULL;
        inode_t            *inode       = NULL;
        fd_t               *iter_fd     = NULL;
        fd_t               *tmp         = NULL;
        uint64_t            tmp_miginfo = 0;
        dht_migrate_info_t *miginfo     = NULL;
        int                 open_failed = 0;

        this  = THIS;
        frame = data;
        local = frame->local;
        conf = this->private;

        src_node = local->cached_subvol;

        if (!local->loc.inode && !local->fd) {
                local->op_errno = EINVAL;
                goto out;
        }

        inode = (!local->fd) ? local->loc.inode : local->fd->inode;

        /* getxattr on cached_subvol for 'linkto' value. Do path based getxattr
         * as root:root. If a fd is already open, access check wont be done*/

        if (!local->loc.inode) {
                ret = syncop_fgetxattr (src_node, local->fd, &dict,
                                        conf->link_xattr_name, NULL, NULL);
        } else {
                SYNCTASK_SETID (0, 0);
                ret = syncop_getxattr (src_node, &local->loc, &dict,
                                       conf->link_xattr_name, NULL, NULL);
                SYNCTASK_SETID (frame->root->uid, frame->root->gid);
        }


        /*
         * Each DHT xlator layer has its own name for the linkto xattr.
         * If the file mode bits indicate the the file is being migrated but
         * this layer's linkto xattr is not set, it means that another
         * DHT layer is migrating the file. In this case, return 1 so
         * the mode bits can be passed on to the higher layer for appropriate
         * action.
         */
        if (-ret == ENODATA) {
                /* This DHT translator is not migrating this file */

                ret = inode_ctx_reset1 (inode, this, &tmp_miginfo);
                if (tmp_miginfo) {

                        /* This can be a problem if the file was
                         * migrated by two different layers. Raise
                         * a warning here.
                         */
                        gf_msg (this->name, GF_LOG_WARNING, 0,
                                DHT_MSG_HAS_MIGINFO,
                                "%s: Found miginfo in the inode ctx",
                                tmp_loc.path ? tmp_loc.path :
                                uuid_utoa (tmp_loc.gfid));

                        miginfo = (void *)tmp_miginfo;
                        GF_REF_PUT (miginfo);
                }
                ret = 1;
                goto out;
        }

        if (!ret)
                linkto_target = dht_linkfile_subvol (this, NULL, NULL, dict);

        if (local->loc.inode) {
                loc_copy (&tmp_loc, &local->loc);
        } else {
                tmp_loc.inode = inode_ref (inode);
                gf_uuid_copy (tmp_loc.gfid, inode->gfid);
        }

        ret = syncop_lookup (this, &tmp_loc, &stbuf, 0, 0, 0);
        if (ret) {
                gf_msg (this->name, GF_LOG_ERROR, -ret,
                        DHT_MSG_FILE_LOOKUP_FAILED,
                        "%s: failed to lookup the file on %s",
                        tmp_loc.path ? tmp_loc.path : uuid_utoa (tmp_loc.gfid),
                        this->name);
                local->op_errno = -ret;
                ret = -1;
                goto out;
        }

        dst_node = dht_subvol_get_cached (this, tmp_loc.inode);
        if (linkto_target && dst_node != linkto_target) {
                gf_msg (this->name, GF_LOG_WARNING, 0,
                        DHT_MSG_INVALID_LINKFILE,
                        "linkto target (%s) is "
                        "different from cached-subvol (%s). Treating %s as "
                        "destination subvol", linkto_target->name,
                        dst_node->name, dst_node->name);
        }

        if (gf_uuid_compare (stbuf.ia_gfid, tmp_loc.inode->gfid)) {
                        gf_msg (this->name, GF_LOG_ERROR, 0,
                                DHT_MSG_GFID_MISMATCH,
                                "%s: gfid different on the target file on %s",
                                tmp_loc.path ? tmp_loc.path :
                                uuid_utoa (tmp_loc.gfid), dst_node->name);
                ret = -1;
                local->op_errno = EIO;
                goto out;
        }

        /* update local. A layout is set in inode-ctx in lookup already */

        dht_layout_unref (this, local->layout);

        local->layout   = dht_layout_get (frame->this, inode);
        local->cached_subvol = dst_node;

        ret = 0;

        /* once we detect the migration complete, the inode-ctx2 is no more
           required.. delete the ctx and also, it means, open() already
           done on all the fd of inode */
        ret = inode_ctx_reset1 (inode, this, &tmp_miginfo);
        if (tmp_miginfo) {
                miginfo = (void *)tmp_miginfo;
                GF_REF_PUT (miginfo);
                goto out;
        }

        /* perform 'open()' on all the fd's present on the inode */
        if (tmp_loc.path == NULL) {
                inode_path (inode, NULL, &path);
                if (path)
                        tmp_loc.path = path;
        }

        LOCK(&inode->lock);

        if (list_empty (&inode->fd_list))
                goto unlock;

        /* perform open as root:root. There is window between linkfile
         * creation(root:root) and setattr with the correct uid/gid
         */
        SYNCTASK_SETID(0, 0);

        /* It's possible that we are the last user of iter_fd after each
         * iteration. In this case the fd_unref() of iter_fd at the end of
         * the loop will cause the destruction of the fd. So we need to
         * iterate the list safely because iter_fd cannot be trusted.
         */
        list_for_each_entry_safe (iter_fd, tmp, &inode->fd_list, inode_list) {

                if (fd_is_anonymous (iter_fd))
                        continue;

                if (dht_fd_open_on_dst (this, iter_fd, dst_node))
                        continue;

                /* We need to release the inode->lock before calling
                 * syncop_open() to avoid possible deadlocks. However this
                 * can cause the iter_fd to be released by other threads.
                 * To avoid this, we take a reference before releasing the
                 * lock.
                 */
                __fd_ref(iter_fd);

                UNLOCK(&inode->lock);

                /* flags for open are stripped down to allow following the
                 * new location of the file, otherwise we can get EEXIST or
                 * truncate the file again as rebalance is moving the data */
                ret = syncop_open (dst_node, &tmp_loc,
                                   (iter_fd->flags &
                                   ~(O_CREAT | O_EXCL | O_TRUNC)),
                                   iter_fd, NULL, NULL);
                if (ret < 0) {
                        gf_msg (this->name, GF_LOG_ERROR, -ret,
                                DHT_MSG_OPEN_FD_ON_DST_FAILED, "failed"
                                " to open the fd"
                                " (%p, flags=0%o) on file %s @ %s",
                                iter_fd, iter_fd->flags, path,
                                dst_node->name);

                        open_failed = 1;
                        local->op_errno = -ret;
                        ret = -1;
                } else {
                        dht_fd_ctx_set (this, iter_fd, dst_node);
                }

                fd_unref(iter_fd);

                LOCK(&inode->lock);
        }

        SYNCTASK_SETID (frame->root->uid, frame->root->gid);

        if (open_failed) {
                ret = -1;
                goto unlock;
        }
        ret = 0;

unlock:
        UNLOCK(&inode->lock);

out:
        if (dict) {
                dict_unref (dict);
        }

        loc_wipe (&tmp_loc);

        return ret;
}

int
dht_rebalance_complete_check (xlator_t *this, call_frame_t *frame)
{
        int         ret     = -1;

        ret = synctask_new (this->ctx->env, dht_migration_complete_check_task,
                            dht_migration_complete_check_done,
                            frame, frame);
        return ret;

}

/* During 'in-progress' state, both nodes should have the file */
/*
 op_ret values :
  0 : Success
 -1 : Failure.
  1 : File is being migrated but not by this DHT layer.
*/
static int
dht_inprogress_check_done (int op_ret, call_frame_t *frame, void *data)
{
        dht_local_t *local      = NULL;
        xlator_t    *dst_subvol = NULL, *src_subvol = NULL;
        inode_t     *inode      = NULL;

        local = frame->local;

        if (op_ret != 0)
                goto out;

        inode = local->loc.inode ? local->loc.inode : local->fd->inode;

        dht_inode_ctx_get_mig_info (THIS, inode, &src_subvol, &dst_subvol);
        if (dht_mig_info_is_invalid (local->cached_subvol,
                                     src_subvol, dst_subvol)) {
                dst_subvol = dht_subvol_get_cached (THIS, inode);
                if (!dst_subvol) {
                        local->op_errno = EINVAL;
                        goto out;
                }
        }

out:
        local->rebalance.target_op_fn (THIS, dst_subvol, frame, op_ret);

        return 0;
}

static int
dht_rebalance_inprogress_task (void *data)
{
        int           ret             = -1;
        xlator_t     *src_node        = NULL;
        xlator_t     *dst_node        = NULL;
        dht_local_t  *local           = NULL;
        dict_t       *dict            = NULL;
        call_frame_t *frame           = NULL;
        xlator_t     *this            = NULL;
        char         *path            = NULL;
        struct iatt   stbuf           = {0,};
        loc_t         tmp_loc         = {0,};
        dht_conf_t   *conf            = NULL;
        inode_t      *inode           = NULL;
        fd_t         *iter_fd         = NULL;
        fd_t         *tmp             = NULL;
        int           open_failed     = 0;
        uint64_t      tmp_miginfo     = 0;
        dht_migrate_info_t *miginfo   = NULL;


        this  = THIS;
        frame = data;
        local = frame->local;
        conf = this->private;

        src_node = local->cached_subvol;

        if (!local->loc.inode && !local->fd)
                goto out;

        inode = (!local->fd) ? local->loc.inode : local->fd->inode;

        /* getxattr on cached_subvol for 'linkto' value. Do path based getxattr
         * as root:root. If a fd is already open, access check wont be done*/
        if (local->loc.inode) {
                SYNCTASK_SETID (0, 0);
                ret = syncop_getxattr (src_node, &local->loc, &dict,
                                       conf->link_xattr_name, NULL, NULL);
                SYNCTASK_SETID (frame->root->uid, frame->root->gid);
        } else {
                ret = syncop_fgetxattr (src_node, local->fd, &dict,
                                        conf->link_xattr_name, NULL, NULL);
        }

        /*
         * Each DHT xlator layer has its own name for the linkto xattr.
         * If the file mode bits indicate the the file is being migrated but
         * this layer's linkto xattr is not present, it means that another
         * DHT layer is migrating the file. In this case, return 1 so
         * the mode bits can be passed on to the higher layer for appropriate
         * action.
         */

        if (-ret == ENODATA) {
                /* This DHT layer is not migrating this file */
                ret = inode_ctx_reset1 (inode, this, &tmp_miginfo);
                if (tmp_miginfo) {
                        /* This can be a problem if the file was
                         * migrated by two different layers. Raise
                         * a warning here.
                         */
                        gf_msg (this->name, GF_LOG_WARNING, 0,
                                DHT_MSG_HAS_MIGINFO,
                                "%s: Found miginfo in the inode ctx",
                                tmp_loc.path ? tmp_loc.path :
                                uuid_utoa (tmp_loc.gfid));
                        miginfo = (void *)tmp_miginfo;
                        GF_REF_PUT (miginfo);
                }
                ret = 1;
                goto out;
        }

        if (ret < 0) {
                gf_msg (this->name, GF_LOG_ERROR, -ret,
                        DHT_MSG_GET_XATTR_FAILED,
                        "%s: failed to get the 'linkto' xattr",
                        local->loc.path);
                ret = -1;
                goto out;
        }

        dst_node = dht_linkfile_subvol (this, NULL, NULL, dict);
        if (!dst_node) {
                gf_msg (this->name, GF_LOG_ERROR, 0,
                        DHT_MSG_SUBVOL_NOT_FOUND,
                        "%s: failed to get the 'linkto' xattr from dict",
                        local->loc.path);
                ret = -1;
                goto out;
        }

        local->rebalance.target_node = dst_node;

        if (local->loc.inode) {
                loc_copy (&tmp_loc, &local->loc);
        } else {
                tmp_loc.inode = inode_ref (inode);
                gf_uuid_copy (tmp_loc.gfid, inode->gfid);
        }

        /* lookup on dst */
        ret = syncop_lookup (dst_node, &tmp_loc, &stbuf, NULL,
                             NULL, NULL);
        if (ret) {
                gf_msg (this->name, GF_LOG_ERROR, -ret,
                        DHT_MSG_FILE_LOOKUP_ON_DST_FAILED,
                        "%s: failed to lookup the file on %s",
                        tmp_loc.path ? tmp_loc.path : uuid_utoa (tmp_loc.gfid),
                        dst_node->name);
                ret = -1;
                goto out;
        }

        if (gf_uuid_compare (stbuf.ia_gfid, tmp_loc.inode->gfid)) {
                gf_msg (this->name, GF_LOG_ERROR, 0,
                        DHT_MSG_GFID_MISMATCH,
                        "%s: gfid different on the target file on %s",
                        tmp_loc.path ? tmp_loc.path : uuid_utoa (tmp_loc.gfid),
                        dst_node->name);
                ret = -1;
                goto out;
        }
        ret = 0;

        if (tmp_loc.path == NULL) {
                inode_path (inode, NULL, &path);
                if (path)
                        tmp_loc.path = path;
        }

        LOCK(&inode->lock);

        if (list_empty (&inode->fd_list))
                goto unlock;

        /* perform open as root:root. There is window between linkfile
         * creation(root:root) and setattr with the correct uid/gid
         */
        SYNCTASK_SETID (0, 0);

        /* It's possible that we are the last user of iter_fd after each
         * iteration. In this case the fd_unref() of iter_fd at the end of
         * the loop will cause the destruction of the fd. So we need to
         * iterate the list safely because iter_fd cannot be trusted.
         */
        list_for_each_entry_safe (iter_fd, tmp, &inode->fd_list, inode_list) {
                if (fd_is_anonymous (iter_fd))
                        continue;

                if (dht_fd_open_on_dst (this, iter_fd, dst_node))
                        continue;

                /* We need to release the inode->lock before calling
                 * syncop_open() to avoid possible deadlocks. However this
                 * can cause the iter_fd to be released by other threads.
                 * To avoid this, we take a reference before releasing the
                 * lock.
                 */
                __fd_ref(iter_fd);

                UNLOCK(&inode->lock);

                /* flags for open are stripped down to allow following the
                 * new location of the file, otherwise we can get EEXIST or
                 * truncate the file again as rebalance is moving the data */
                ret = syncop_open (dst_node, &tmp_loc,
                                  (iter_fd->flags &
                                   ~(O_CREAT | O_EXCL | O_TRUNC)),
                                   iter_fd, NULL, NULL);
                if (ret < 0) {
                        gf_msg (this->name, GF_LOG_ERROR, -ret,
                                DHT_MSG_OPEN_FD_ON_DST_FAILED,
                                "failed to send open "
                                "the fd (%p, flags=0%o) on file %s @ %s",
                                iter_fd, iter_fd->flags, path,
                                dst_node->name);
                        ret = -1;
                        open_failed = 1;
                } else {
                        /* Potential fd leak if this fails here as it will be
                           reopened at the next Phase1/2 check */
                        dht_fd_ctx_set (this, iter_fd, dst_node);
                }

                fd_unref(iter_fd);

                LOCK(&inode->lock);
        }

        SYNCTASK_SETID (frame->root->uid, frame->root->gid);

unlock:
        UNLOCK(&inode->lock);

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

        ret = dht_inode_ctx_set_mig_info (this, inode, src_node, dst_node);
        if (ret) {
                gf_msg (this->name, GF_LOG_ERROR, 0,
                        DHT_MSG_SET_INODE_CTX_FAILED,
                        "%s: failed to set inode-ctx target file at %s",
                        local->loc.path, dst_node->name);
                goto out;
        }

        ret = 0;
out:
        if (dict) {
                dict_unref (dict);
        }

        loc_wipe (&tmp_loc);
        return ret;
}

int
dht_rebalance_in_progress_check (xlator_t *this, call_frame_t *frame)
{

        int         ret     = -1;

        ret = synctask_new (this->ctx->env, dht_rebalance_inprogress_task,
                            dht_inprogress_check_done,
                            frame, frame);
        return ret;
}

int
dht_inode_ctx_layout_set (inode_t *inode, xlator_t *this,
                          dht_layout_t *layout_int)
{
        dht_inode_ctx_t         *ctx            = NULL;
        int                      ret            = -1;

        ret = dht_inode_ctx_get (inode, this, &ctx);
        if (!ret && ctx) {
                ctx->layout = layout_int;
        } else {
                ctx = GF_CALLOC (1, sizeof (*ctx), gf_dht_mt_inode_ctx_t);
                if (!ctx)
                        return ret;
                ctx->layout = layout_int;
        }

        ret = dht_inode_ctx_set (inode, this, ctx);

        return ret;
}


void
dht_inode_ctx_time_set (inode_t *inode, xlator_t *this, struct iatt *stat)
{
        dht_inode_ctx_t         *ctx            = NULL;
        dht_stat_time_t         *time           = 0;
        int                      ret            = -1;

        ret = dht_inode_ctx_get (inode, this, &ctx);

        if (ret)
		return;

        time = &ctx->time;

	time->mtime      = stat->ia_mtime;
	time->mtime_nsec = stat->ia_mtime_nsec;

	time->ctime      = stat->ia_ctime;
	time->ctime_nsec = stat->ia_ctime_nsec;

	time->atime      = stat->ia_atime;
	time->atime_nsec = stat->ia_atime_nsec;

	return;
}


int
dht_inode_ctx_time_update (inode_t *inode, xlator_t *this, struct iatt *stat,
                           int32_t post)
{
        dht_inode_ctx_t         *ctx            = NULL;
        dht_stat_time_t         *time           = 0;
        int                      ret            = -1;

        GF_VALIDATE_OR_GOTO (this->name, stat, out);
        GF_VALIDATE_OR_GOTO (this->name, inode, out);

        ret = dht_inode_ctx_get (inode, this, &ctx);

        if (ret) {
                ctx = GF_CALLOC (1, sizeof (*ctx), gf_dht_mt_inode_ctx_t);
                if (!ctx)
                        return -1;
        }

        time = &ctx->time;

        DHT_UPDATE_TIME(time->mtime, time->mtime_nsec,
                        stat->ia_mtime, stat->ia_mtime_nsec, inode, post);
        DHT_UPDATE_TIME(time->ctime, time->ctime_nsec,
                        stat->ia_ctime, stat->ia_ctime_nsec, inode, post);
        DHT_UPDATE_TIME(time->atime, time->atime_nsec,
                        stat->ia_atime, stat->ia_atime_nsec, inode, post);

        ret = dht_inode_ctx_set (inode, this, ctx);
out:
        return 0;
}

int
dht_inode_ctx_get (inode_t *inode, xlator_t *this, dht_inode_ctx_t **ctx)
{
        int             ret     = -1;
        uint64_t        ctx_int = 0;

        GF_VALIDATE_OR_GOTO ("dht", this, out);
        GF_VALIDATE_OR_GOTO (this->name, inode, out);

        ret = inode_ctx_get (inode, this, &ctx_int);

        if (ret)
                return ret;

        if (ctx)
                *ctx = (dht_inode_ctx_t *) ctx_int;
out:
        return ret;
}

int dht_inode_ctx_set (inode_t *inode, xlator_t *this, dht_inode_ctx_t *ctx)
{
        int             ret = -1;
        uint64_t        ctx_int = 0;

        GF_VALIDATE_OR_GOTO ("dht", this, out);
        GF_VALIDATE_OR_GOTO (this->name, inode, out);
        GF_VALIDATE_OR_GOTO (this->name, ctx, out);

        ctx_int = (long)ctx;
        ret = inode_ctx_set (inode, this, &ctx_int);
out:
        return ret;
}

int
dht_subvol_status (dht_conf_t *conf, xlator_t *subvol)
{
        int i;

        for (i=0 ; i < conf->subvolume_cnt; i++) {
                if (conf->subvolumes[i] == subvol) {
                        return conf->subvolume_status[i];
                }
        }
        return 0;
}

inode_t*
dht_heal_path (xlator_t *this, char *path, inode_table_t *itable)
{
        int             ret             = -1;
        struct iatt     iatt            = {0, };
        inode_t        *linked_inode    = NULL;
        loc_t           loc             = {0, };
        char           *bname           = NULL;
        char           *save_ptr        = NULL;
        uuid_t          gfid            = {0, };
        char           *tmp_path        = NULL;


        tmp_path = gf_strdup (path);
        if (!tmp_path) {
                goto out;
        }

        memset (gfid, 0, 16);
        gfid[15] = 1;

        gf_uuid_copy (loc.pargfid, gfid);
        loc.parent = inode_ref (itable->root);

        bname = strtok_r (tmp_path, "/",  &save_ptr);

        /* sending a lookup on parent directory,
         * Eg:  if  path is like /a/b/c/d/e/f/g/
         * then we will send a lookup on a first and then b,c,d,etc
         */

        while (bname) {
                linked_inode = NULL;
                loc.inode = inode_grep (itable, loc.parent, bname);
                if (loc.inode == NULL) {
                        loc.inode = inode_new (itable);
                        if (loc.inode == NULL) {
                                ret = -ENOMEM;
                                goto out;
                        }
                } else {
                        /*
                         * Inode is already populated in the inode table.
                         * Which means we already looked up the inde and
                         * linked with a dentry. So that we will skip
                         * lookup on this entry, and proceed to next.
                         */
                        bname = strtok_r (NULL, "/",  &save_ptr);
                        inode_unref (loc.parent);
                        loc.parent = loc.inode;
                        gf_uuid_copy (loc.pargfid, loc.inode->gfid);
                        loc.inode = NULL;
                        continue;
                }

                loc.name = bname;
                ret = loc_path (&loc, bname);

                ret = syncop_lookup (this, &loc, &iatt, NULL, NULL, NULL);
                if (ret) {
                        gf_msg (this->name, GF_LOG_INFO, -ret,
                                DHT_MSG_DIR_SELFHEAL_FAILED,
                                "Healing of path %s failed on subvolume %s for "
                                "directory %s", path, this->name, bname);
                        goto out;
                }

                linked_inode = inode_link (loc.inode, loc.parent, bname, &iatt);
                if (!linked_inode)
                        goto out;

                loc_wipe (&loc);
                gf_uuid_copy (loc.pargfid, linked_inode->gfid);
                loc.inode = NULL;
                loc.parent = linked_inode;

                bname = strtok_r (NULL, "/",  &save_ptr);
        }
out:
        inode_ref (linked_inode);
        loc_wipe (&loc);
        GF_FREE (tmp_path);

        return linked_inode;
}


int
dht_heal_full_path (void *data)
{
        call_frame_t            *heal_frame     = data;
        dht_local_t             *local          = NULL;
        loc_t                    loc            = {0, };
        dict_t                  *dict           = NULL;
        char                    *path           = NULL;
        int                      ret            = -1;
        xlator_t                *source         = NULL;
        xlator_t                *this           = NULL;
        inode_table_t           *itable         = NULL;
        inode_t                 *inode          = NULL;
        inode_t                 *tmp_inode      = NULL;

        GF_VALIDATE_OR_GOTO ("DHT", heal_frame, out);

        local = heal_frame->local;
        this = heal_frame->this;
        source = heal_frame->cookie;
        heal_frame->cookie = NULL;
        gf_uuid_copy (loc.gfid, local->gfid);

        if (local->loc.inode)
                loc.inode = inode_ref (local->loc.inode);
        else
                goto out;

        itable = loc.inode->table;
        ret = syncop_getxattr (source, &loc, &dict,
                       GET_ANCESTRY_PATH_KEY, NULL, NULL);
        if (ret) {
                gf_msg (this->name, GF_LOG_INFO, -ret,
                        DHT_MSG_DIR_SELFHEAL_FAILED,
                        "Failed to get path from subvol %s. Aborting "
                        "directory healing.", source->name);
                goto out;
        }

        ret = dict_get_str (dict, GET_ANCESTRY_PATH_KEY, &path);
        if (path) {
                inode = dht_heal_path (this, path, itable);
                if (inode && inode != local->inode) {
                        /*
                         * if inode returned by heal function is different
                         * from what we passed, which means a racing thread
                         * already linked a different inode for dentry.
                         * So we will update our local->inode, so that we can
                         * retrurn proper inode.
                         */
                        tmp_inode = local->inode;
                        local->inode = inode;
                        inode_unref (tmp_inode);
                        tmp_inode = NULL;
                } else {
                        inode_unref (inode);
                }
        }

out:
        loc_wipe (&loc);
        if (dict)
                dict_unref (dict);
        return 0;
}

int
dht_heal_full_path_done (int op_ret, call_frame_t *heal_frame, void *data)
{

        call_frame_t            *main_frame       = NULL;
        dht_local_t             *local            = NULL;

        local = heal_frame->local;
        main_frame = local->main_frame;
        local->main_frame = NULL;

        dht_set_fixed_dir_stat (&local->postparent);

        DHT_STACK_UNWIND (lookup, main_frame, 0, 0,
                          local->inode, &local->stbuf, local->xattr,
                          &local->postparent);

        DHT_STACK_DESTROY (heal_frame);
        return 0;
}

/* This function must be called inside an inode lock */
int
__dht_lock_subvol_set (inode_t *inode, xlator_t *this,
                       xlator_t *lock_subvol)
{
        dht_inode_ctx_t         *ctx            = NULL;
        int                      ret            = -1;
        uint64_t                 value          = 0;

        GF_VALIDATE_OR_GOTO (this->name, inode, out);

        ret = __inode_ctx_get0 (inode, this, &value);
        if (ret || !value) {
                return -1;
        }

        ctx = (dht_inode_ctx_t *) value;
        ctx->lock_subvol = lock_subvol;
out:
        return ret;
}

xlator_t*
dht_get_lock_subvolume (xlator_t *this, struct gf_flock *lock,
                        dht_local_t *local)
{
        xlator_t                *subvol                  = NULL;
        inode_t                 *inode                   = NULL;
        int32_t                  ret                     = -1;
        uint64_t                 value                   = 0;
        xlator_t                *cached_subvol           = NULL;
        dht_inode_ctx_t         *ctx                     = NULL;
        char                     gfid[GF_UUID_BUF_SIZE]  = {0};

        GF_VALIDATE_OR_GOTO (this->name, lock, out);
        GF_VALIDATE_OR_GOTO (this->name, local, out);

        cached_subvol = local->cached_subvol;

        if (local->loc.inode || local->fd) {
                inode = local->loc.inode ? local->loc.inode : local->fd->inode;
        }

        if (!inode)
                goto out;

        if (!(IA_ISDIR (inode->ia_type) || IA_ISINVAL (inode->ia_type))) {
                /*
                 * We may get non-linked inode for directories as part
                 * of the selfheal code path. So checking  for IA_INVAL
                 * type also. This will only happen for directory.
                 */
                subvol = local->cached_subvol;
                goto out;
        }

        if (lock->l_type != F_UNLCK) {
                /*
                 * inode purging might happen on NFS between a lk
                 * and unlk. Due to this lk and unlk might be sent
                 * to different subvols.
                 * So during a lock request, taking a ref on inode
                 * to prevent inode purging. inode unref will happen
                 * in unlock cbk code path.
                 */
                inode_ref (inode);
        }

        LOCK (&inode->lock);
                ret = __inode_ctx_get0 (inode, this, &value);
                if (!ret && value) {
                        ctx = (dht_inode_ctx_t *) value;
                        subvol = ctx->lock_subvol;
                }
                if (!subvol && lock->l_type != F_UNLCK && cached_subvol) {
                        ret = __dht_lock_subvol_set (inode, this,
                                                     cached_subvol);
                        if (ret) {
                                gf_uuid_unparse(inode->gfid, gfid);
                                gf_msg (this->name, GF_LOG_WARNING, 0,
                                        DHT_MSG_SET_INODE_CTX_FAILED,
                                        "Failed to set lock_subvol in "
                                        "inode ctx for gfid %s",
                                        gfid);
                                goto unlock;
                        }
                        subvol = cached_subvol;
                }
unlock:
        UNLOCK (&inode->lock);
        if (!subvol && inode && lock->l_type != F_UNLCK) {
                inode_unref (inode);
        }
out:
        return subvol;
}

int
dht_lk_inode_unref (call_frame_t *frame, int32_t op_ret)
{
        int                     ret                     = -1;
        dht_local_t            *local                   = NULL;
        inode_t                *inode                   = NULL;
        xlator_t               *this                    = NULL;
        char                    gfid[GF_UUID_BUF_SIZE]  = {0};

        local = frame->local;
        this = frame->this;

        if (local->loc.inode || local->fd) {
                inode = local->loc.inode ? local->loc.inode : local->fd->inode;
        }
        if (!inode) {
                gf_msg (this->name, GF_LOG_WARNING, 0,
                        DHT_MSG_LOCK_INODE_UNREF_FAILED,
                        "Found a NULL inode. Failed to unref the inode");
                goto out;
        }

        if (!(IA_ISDIR (inode->ia_type) || IA_ISINVAL (inode->ia_type))) {
                ret = 0;
                goto out;
        }

        switch (local->lock_type) {
        case F_RDLCK:
        case F_WRLCK:
                if (op_ret) {
                        gf_uuid_unparse(inode->gfid, gfid);
                        gf_msg_debug (this->name, 0,
                                "lock request failed for gfid %s", gfid);
                        inode_unref (inode);
                        goto out;
                }
                break;

        case F_UNLCK:
                if (!op_ret) {
                        inode_unref (inode);
                } else {
                        gf_uuid_unparse(inode->gfid, gfid);
                        gf_msg (this->name, GF_LOG_WARNING, 0,
                                DHT_MSG_LOCK_INODE_UNREF_FAILED,
                                "Unlock request failed for gfid %s."
                                "Failed to unref the inode", gfid);
                        goto out;
                }
        default:
                break;
        }
        ret = 0;
out:
        return ret;
}