summaryrefslogtreecommitdiffstats
path: root/xlators/features/trash/src/trash.c
blob: 3a28815571ea26ba47bbd7e8611f951b42ab4ee9 (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
/*
   Copyright (c) 2006-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.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "trash.h"
#include "trash-mem-types.h"

#define root_gfid        (uuid_t){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
#define trash_gfid       (uuid_t){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}
#define internal_op_gfid (uuid_t){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6}

int32_t
trash_truncate_writev_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                           int32_t op_ret, int32_t op_errno,
                           struct iatt *prebuf, struct iatt *postbuf,
                           dict_t *xdata);

int32_t
trash_truncate_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                          int32_t op_ret, int32_t op_errno, inode_t *inode,
                          struct iatt *stbuf, struct iatt *preparent,
                          struct iatt *postparent, dict_t *xdata);

int32_t
trash_unlink_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, struct iatt *buf,
                         struct iatt *preoldparent, struct iatt *postoldparent,
                         struct iatt *prenewparent, struct iatt *postnewparent,
                         dict_t *xdata);

/* Common routines used in this translator */

/**
 * When a directory/file is created under trash directory, it should have
 * the same permission as before. This function will fetch permission from
 * the existing directory and returns the same
 */
mode_t
get_permission (char *path)
{
        mode_t                  mode                    = 0755;
        struct stat             sbuf                    = {0,};
        struct iatt             ibuf                    = {0,};
        int                     ret                     = 0;

        ret = stat (path, &sbuf);
        if (!ret) {
                iatt_from_stat (&ibuf, &sbuf);
                mode = st_mode_from_ia (ibuf.ia_prot, ibuf.ia_type);
        } else
                gf_log ("trash", GF_LOG_DEBUG, "stat on %s failed"
                                " using default", path);
        return mode;
}

/**
 * For normalization, trash directory name is stored inside priv structure as
 * '/trash_directory/'. As a result the trailing and leading slashes are being
 * striped out for additional usage.
 */
int
extract_trash_directory (char *priv_value, const char **trash_directory)
{
        char                    *tmp                    = NULL;
        int                     ret                     = 0;

        GF_VALIDATE_OR_GOTO("trash", priv_value, out);

        tmp = gf_strdup (priv_value + 1);
        if (!tmp) {
                ret = ENOMEM;
                goto out;
        }
        if (tmp[strlen(tmp)-1] == '/')
                tmp[strlen(tmp)-1] = '\0';
        *trash_directory = gf_strdup (tmp);
        if (!(*trash_directory)) {
                ret = ENOMEM;
                goto out;
        }
out:
        if (tmp)
                GF_FREE (tmp);
        return ret;
}

/**
 * The trash directory path should be append at begining of file path for
 * delete or truncate operations. Normal trashing moves the contents to
 * trash directory and trashing done by internal operations are moved to
 * internal_op directory inside trash.
 */
void
copy_trash_path (const char *priv_value, gf_boolean_t internal, char *path)
{
        char                    trash_path[PATH_MAX]    = {0,};

        strcpy (trash_path, priv_value);
        if (internal)
                strcat (trash_path, "internal_op/");

        strcpy (path, trash_path);
}

/**
 * This function performs the reverse operation of copy_trash_path(). It gives
 * out a pointer, whose starting value will be the path inside trash directory,
 * similar to orginal path.
 */
void
remove_trash_path (const char *path, gf_boolean_t internal, char **rem_path)
{
        if (rem_path == NULL) {
                return;
        }

        *rem_path =  strchr (path + 1, '/');
        if (internal)
                *rem_path =  strchr (*rem_path + 1, '/');
}

/**
 * Check whether the path includes trash directory or internal op directory
 * inside trash. This check is used to make sure that we avoid deletion,
 * rename and creation operations from trash directory.
 */
int
check_whether_trash_directory (const char *path,
                               const char *trash_directory_path)
{
        char                    tmp_path[PATH_MAX]              = {0,};
        char                    internal_op_path[PATH_MAX]      = {0,};
        int                     ret                             = 0;

        if (path[strlen(path)-1] == '/')
                sprintf (tmp_path, "%s", path);
        else
                sprintf (tmp_path, "%s/", path);

        copy_trash_path (trash_directory_path, _gf_true, internal_op_path);
        ret = strcmp (tmp_path, trash_directory_path) &&
              strcmp (tmp_path, internal_op_path);

        return ret;
}

/**
 * Checks whether the given path reside under the specified eliminate path
 */
int
check_whether_eliminate_path (trash_elim_path *trav, const char *path)
{
        int                     match                           = 0;

        while (trav) {
                if (strncmp (path, trav->path, strlen(trav->path)) == 0) {
                        match++;
                        break;
                }
                trav = trav->next;
        }
        return match;
}

/**
 * Stores the eliminate path into internal eliminate path structure
 */
int
store_eliminate_path (char *str, trash_elim_path **eliminate)
{
        trash_elim_path         *trav                   = NULL;
        char                    *component              = NULL;
        char                    elm_path[PATH_MAX]      = {0,};
        int                     ret                     = 0;
        char                    *strtokptr              = NULL;

        if (eliminate == NULL) {
                ret = EINVAL;
                goto out;
        }

        component = strtok_r (str, ",", &strtokptr);
        while (component) {
                trav = GF_CALLOC (1, sizeof (*trav),
                                 gf_trash_mt_trash_elim_path);
                if (!trav) {
                        ret = ENOMEM;
                        goto out;
                }
                if (component[0] == '/')
                        sprintf(elm_path, "%s", component);
                else
                        sprintf(elm_path, "/%s", component);

                if (component[strlen(component)-1] != '/')
                        strcat (elm_path, "/");

                trav->path = gf_strdup(elm_path);
                if (!trav->path) {
                                ret = ENOMEM;
                                gf_log ("trash", GF_LOG_DEBUG, "out of memory");
                                goto out;
                }
                trav->next = *eliminate;
                *eliminate = trav;
                component = strtok_r (NULL, ",", &strtokptr);
        }
out:
        return ret;
}

/**
 * Appends time stamp to given string
 */
void
append_time_stamp (char *name)
{
        int                     i;
        char                    timestr[64]            = {0,};

        gf_time_fmt (timestr, sizeof(timestr), time (NULL),
                     gf_timefmt_F_HMS);

        /* removing white spaces in timestamp */
        for (i = 0; i < strlen (timestr); i++) {
                if (timestr[i] == ' ')
                        timestr[i] = '_';
        }
        strcat (name, "_");
        strcat (name, timestr);
}

/**
 * Wipe the memory used by trash location variable
 */
void
trash_local_wipe (trash_local_t *local)
{
        if (!local)
                goto out;

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

        if (local->fd)
                fd_unref (local->fd);
        if (local->newfd)
                fd_unref (local->newfd);

        mem_put (local);
out:
        return;
}

/**
 * Wipe the memory used by eliminate path through a
 * recursive call
 */
void
wipe_eliminate_path (trash_elim_path **trav)
{
        if (trav == NULL) {
                return;
        }

        if (*trav == NULL) {
                return;
        }

        wipe_eliminate_path (&(*trav)->next);
        GF_FREE ((*trav)->path);
        GF_FREE (*trav);
        *trav = NULL;
}

/**
 * This getxattr calls returns existing trash directory path in
 * the dictionary
 */
int32_t
trash_notify_getxattr_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                      int32_t op_ret, int32_t op_errno, dict_t *dict,
                      dict_t *xdata)
{
        data_t                 *data                  = NULL;
        trash_private_t        *priv                  = NULL;
        int ret                                       = 0;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        data = dict_get (dict, GET_ANCESTRY_PATH_KEY);
        if (!data) {
                gf_log (this->name, GF_LOG_DEBUG,
                                "oldtrash-directory doesnot exists");
                priv->oldtrash_dir = gf_strdup (priv->newtrash_dir);
                if (!priv->oldtrash_dir) {
                        gf_log (this->name, GF_LOG_ERROR, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
        } else {
                priv->oldtrash_dir = gf_strdup (data->data);
                if (!priv->oldtrash_dir) {
                        gf_log (this->name, GF_LOG_ERROR, "out of memory");
                         ret = ENOMEM;
                         goto out;
                }
                gf_log (this->name, GF_LOG_DEBUG, "old trash directory"
                                                  " path is %s", data->data);
        }

out:
          return ret;
}

/**
 * This is a nameless look up for old trash directory
 * The lookup is based on gfid, because trash directory
 * has fixed gfid.
 */
int32_t
trash_notify_lookup_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, inode_t *inode,
                         struct iatt *buf, dict_t *xdata,
                         struct iatt *postparent)
{
        trash_private_t         *priv                  = NULL;
        loc_t                   loc                    = {0,};
        int                     ret                    = 0;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        if (op_ret == 0) {

                gf_log (this->name, GF_LOG_DEBUG, "inode found with gfid %s",
                                   uuid_utoa(buf->ia_gfid));

                uuid_copy (loc.gfid,  trash_gfid);

                /* Find trash inode using available information */
                priv->trash_inode = inode_link (inode, NULL, NULL, buf);

                loc.inode = inode_ref (priv->trash_inode);

                /*Used to find path of old trash directory*/
                STACK_WIND (frame, trash_notify_getxattr_cbk, FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->getxattr, &loc,
                            GET_ANCESTRY_PATH_KEY, xdata);
        }

        /* If there is no old trash directory we set its value to new one,
         * which is the valid condition for trash directory creation
         */
        else {
                priv->oldtrash_dir = gf_strdup (priv->newtrash_dir);
                if (!priv->oldtrash_dir) {
                        gf_log (this->name, GF_LOG_ERROR, "out of memory");
                        goto out;
                }
        }

out:
        loc_wipe (&loc);
        return ret;
}

int32_t
trash_internal_op_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                        int32_t op_ret, int32_t op_errno, inode_t *inode,
                        struct iatt *buf, struct iatt *preparent,
                        struct iatt *postparent, dict_t *xdata)
{
        if (op_ret != 0)
                gf_log (this->name, GF_LOG_ERROR, "mkdir failed for "
                        "internal op directory : %s", strerror (op_errno));
        return op_ret;
}

/**
 * This is the call back of mkdir fop initated using STACK_WIND in
 * notify function which is used to create trash directory in the brick
 * when a volume starts.The frame of the mkdir must destroyed from
 * this function itself since it was created by trash xlator
 */
int32_t
trash_notify_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                        int32_t op_ret, int32_t op_errno, inode_t *inode,
                        struct iatt *buf, struct iatt *preparent,
                        struct iatt *postparent, dict_t *xdata)
{
        uuid_t                *gfid_ptr                         = NULL;
        loc_t                 loc                               = {0, };
        int                   ret                               = 0;
        dict_t                *dict                             = NULL;
        char                  internal_op_path[PATH_MAX]        = {0,};
        trash_private_t       *priv       = NULL;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        dict = dict_new ();
        if (!dict) {
                ret = -1;
                goto out;
        }
        if ((op_ret == 0) || (op_ret == -1 && op_errno == EEXIST)) {
                gfid_ptr = GF_CALLOC (1, sizeof(uuid_t),
                                           gf_common_mt_uuid_t);
                if (!gfid_ptr) {
                        ret = ENOMEM;
                        goto out;
                }
                uuid_copy (*gfid_ptr, internal_op_gfid);

                uuid_copy (loc.gfid, internal_op_gfid);
                uuid_copy (loc.pargfid, trash_gfid);
                loc.name = gf_strdup ("internal_op");

                if (!loc.name) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                         "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                sprintf (internal_op_path, "%s%s",
                                priv->newtrash_dir, loc.name);

                loc.path = gf_strdup (internal_op_path);

                if (!loc.path) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                         "out of memory");
                        ret = ENOMEM;
                        goto out;
                }

                loc.inode = inode_new (priv->trash_itable);
                loc.inode->ia_type = IA_IFDIR;
                /* Fixed gfid is set for trash directory with
                 * this function
                 */
                ret = dict_set_dynptr (dict, "gfid-req", gfid_ptr,
                                       sizeof (uuid_t));
                if (ret) {
                        gf_log (this->name, GF_LOG_ERROR,
                                 "setting key gfid-req failed");
                        goto out;
                }

                /* The mkdir call for creating trash directory */
                STACK_WIND (frame, trash_internal_op_mkdir_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->mkdir, &loc, 0755,
                            0022, dict);
                /* After creating we must call other notify functions */
                default_notify (this, GF_EVENT_CHILD_UP, NULL);
        } else {
                gf_log (this->name, GF_LOG_ERROR, "mkdir failed for trash"
                                " directory : %s", strerror (op_errno));
        }

        STACK_DESTROY (frame->root);
out:
        if (ret && gfid_ptr)
                GF_FREE (gfid_ptr);
        if (dict)
                dict_unref (dict);
        return 0;
}

int32_t
trash_notify_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                    int32_t op_ret, int32_t op_errno, struct iatt *buf,
                    struct iatt *preoldparent, struct iatt *postoldparent,
                    struct iatt *prenewparent, struct iatt *postnewparent,
                    dict_t *xdata)
{
        if ((op_ret == 0) || (op_ret == -1 && op_errno == EEXIST)) {
                /* After creating we must call other notify functions */
                default_notify (this, GF_EVENT_CHILD_UP, NULL);
        } else {
                gf_log (this->name, GF_LOG_ERROR, "rename failed: %s",
                        strerror (op_errno));
        }

        STACK_DESTROY (frame->root);
        return op_ret;
}

/**
 * This is the call back of rename fop initated using STACK_WIND in
 * reconfigure function which is used to rename trash directory in
 * the brick when we perform volume set.This frame  must destroyed
 * from this function itself since it was created by trash xlator
 */
int32_t
trash_reconf_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, struct iatt *buf,
                         struct iatt *preoldparent, struct iatt *postoldparent,
                         struct iatt *prenewparent, struct iatt *postnewparent,
                         dict_t *xdata)
{
        if (op_ret == -1 && op_errno == EEXIST) {

                gf_log (this->name, GF_LOG_ERROR, "rename failed: %s",
                        strerror (op_errno));
        }

        STACK_DESTROY (frame->root);

        return op_ret;
}

int32_t
trash_common_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                   int32_t op_ret, int32_t op_errno, inode_t *inode,
                   struct iatt *buf, struct iatt *preparent,
                   struct iatt *postparent, dict_t *xdata)
{
        STACK_UNWIND_STRICT (mkdir, frame, op_ret, op_errno, inode,
                             buf, preparent, postparent, xdata);
        return 0;
}

int32_t
trash_common_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                    int32_t op_ret, int32_t op_errno, struct iatt *buf,
                    struct iatt *preoldparent, struct iatt *postoldparent,
                    struct iatt *prenewparent, struct iatt *postnewparent,
                    dict_t *xdata)
{
        STACK_UNWIND_STRICT (rename, frame, op_ret, op_errno, buf, preoldparent,
                             postoldparent, prenewparent, postnewparent, xdata);
        return 0;
}

int32_t
trash_common_rmdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                   int32_t op_ret, int32_t op_errno, struct iatt *preparent,
                   struct iatt *postparent,
                   dict_t *xdata)
{
        STACK_UNWIND_STRICT (rmdir, frame, op_ret, op_errno, preparent,
                             postparent, xdata);
        return 0;
}

/**
 * move backs from trash translator to unlink call
 */
int32_t
trash_common_unwind_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno,
                         struct iatt *preparent, struct iatt *postparent,
                         dict_t *xdata)
{
        TRASH_STACK_UNWIND (unlink, frame, op_ret, op_errno, preparent,
                            postparent, xdata);
        return 0;
}

/**
 * If the path is not present in the trash directory,it will recursively
 * call this call-back and one by one directories will be created from
 * the starting
 */
int32_t
trash_unlink_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                        int32_t op_ret, int32_t op_errno, inode_t *inode,
                        struct iatt *stbuf, struct iatt *preparent,
                        struct iatt *postparent, dict_t *xdata)
{
        trash_local_t       *local              = NULL;
        char                *tmp_str            = NULL;
        char                *tmp_path           = NULL;
        char                *tmp_dirname        = NULL;
        char                *tmp_stat           = NULL;
        char                real_path[PATH_MAX] = {0,};
        char                *dir_name           = NULL;
        size_t              count               = 0;
        int32_t             loop_count          = 0;
        int                 i                   = 0;
        loc_t               tmp_loc             = {0,};
        trash_private_t     *priv               = NULL;
        int                 ret                 = 0;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        local   = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        tmp_str = gf_strdup (local->newpath);
        if (!tmp_str) {
                gf_log (this->name, GF_LOG_ERROR, "out of memory");
                ret = -1;
                goto out;
        }
        loop_count = local->loop_count;

        /* The directory is not present , need to create it */
        if ((op_ret == -1) &&  (op_errno == ENOENT)) {
                tmp_dirname = strchr (tmp_str, '/');
                while (tmp_dirname) {
                        count = tmp_dirname - tmp_str;
                        if (count == 0)
                                count = 1;
                        i++;
                        if (i > loop_count)
                                break;
                        tmp_dirname = strchr (tmp_str + count + 1, '/');
                }
                tmp_path = gf_memdup (local->newpath, count + 1);
                if (!tmp_path) {
                        gf_log (this->name, GF_LOG_ERROR, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                tmp_path[count] = '\0';

                loc_copy (&tmp_loc, &local->loc);
                tmp_loc.path = gf_strdup (tmp_path);
                if (!tmp_loc.path) {
                        gf_log (this->name, GF_LOG_ERROR, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }

                /* Stores the the name of directory to be created */
                tmp_loc.name = gf_strdup (strrchr(tmp_path, '/') + 1);
                if (!tmp_loc.name) {
                        gf_log (this->name, GF_LOG_ERROR, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                strcpy (real_path, priv->brick_path);
                remove_trash_path (tmp_path, (frame->root->pid < 0), &tmp_stat);
                if (tmp_stat)
                        strcat (real_path, tmp_stat);
                STACK_WIND_COOKIE (frame, trash_unlink_mkdir_cbk, tmp_path,
                                   FIRST_CHILD(this),
                                   FIRST_CHILD(this)->fops->mkdir,
                                   &tmp_loc, get_permission(real_path),
                                   0022, xdata);
                loc_wipe (&tmp_loc);
                goto out;
        }

        /* Given path is created , comparing to the required path */
        if (op_ret == 0) {
                dir_name = dirname (tmp_str);
                if (strcmp((char *)cookie, dir_name) == 0) {
                        /* File path exists we can rename it*/
                        loc_copy (&tmp_loc, &local->loc);
                        tmp_loc.path = local->newpath;
                        STACK_WIND (frame, trash_unlink_rename_cbk,
                                    FIRST_CHILD(this),
                                    FIRST_CHILD(this)->fops->rename,
                                    &local->loc, &tmp_loc, xdata);
                        goto out;
                }
        }

        LOCK (&frame->lock);
        {
                loop_count = ++local->loop_count;
        }
        UNLOCK (&frame->lock);

        tmp_dirname = strchr (tmp_str, '/');

        /* Path is not completed , need to create remaining path */
        while (tmp_dirname) {
                count = tmp_dirname - tmp_str;
                if (count == 0)
                        count = 1;
                i++;
                if ((i > loop_count) || (count > PATH_MAX))
                        break;
                tmp_dirname = strchr (tmp_str + count + 1, '/');
        }
        tmp_path = gf_memdup (local->newpath, count + 1);
        if (!tmp_path) {
                gf_log (this->name, GF_LOG_ERROR, "out of memory");
                ret = -1;
                goto out;
        }
        tmp_path[count] = '\0';

        loc_copy (&tmp_loc, &local->loc);
        tmp_loc.path = gf_strdup (tmp_path);
        if (!tmp_loc.path) {
                gf_log (this->name, GF_LOG_ERROR, "out of memory");
                ret = -1;
                goto out;
        }

        /* Stores the the name of directory to be created */
        tmp_loc.name = gf_strdup (strrchr(tmp_path, '/') + 1);
        if (!tmp_loc.name) {
                gf_log (this->name, GF_LOG_ERROR, "out of memory");
                ret = -1;
                goto out;
        }

        strcpy (real_path, priv->brick_path);
        remove_trash_path (tmp_path, (frame->root->pid < 0), &tmp_stat);
        if (tmp_stat)
                strcat (real_path, tmp_stat);

        STACK_WIND_COOKIE (frame, trash_unlink_mkdir_cbk, tmp_path,
                           FIRST_CHILD(this),
                           FIRST_CHILD(this)->fops->mkdir, &tmp_loc,
                           get_permission(real_path), 0022, xdata);

out:
        if (tmp_path)
                GF_FREE (tmp_path);
        if (tmp_str)
                GF_FREE (tmp_str);
        return ret;
}

/**
 * The name of unlinking file should be renamed as starting
 * from trash directory as mentioned in the mount point
 */
int32_t
trash_unlink_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, struct iatt *buf,
                         struct iatt *preoldparent, struct iatt *postoldparent,
                         struct iatt *prenewparent, struct iatt *postnewparent,
                         dict_t *xdata)
{
        trash_local_t      *local              = NULL;
        trash_private_t     *priv              = NULL;
        char               *tmp_str            = NULL;
        char               *dir_name           = NULL;
        char               *tmp_cookie         = NULL;
        loc_t              tmp_loc             = {0,};
        char               *tmp_stat           = NULL;
        char               real_path[PATH_MAX] = {0,};
        int                ret                 = 0;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        if ((op_ret == -1) && (op_errno == ENOENT)) {
                /* the file path doesnot exists we want to create path
                 * for the file
                 */
                tmp_str = gf_strdup (local->newpath);
                if (!tmp_str) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                dir_name = dirname (tmp_str); /* stores directory name */

                loc_copy (&tmp_loc, &local->loc);
                tmp_loc.path = gf_strdup (dir_name);
                if (!tmp_loc.path) {
                        gf_log (this->name, GF_LOG_ERROR, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }

                tmp_cookie = gf_strdup (dir_name);
                if (!tmp_cookie) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                strcpy (real_path, priv->brick_path);
                remove_trash_path (tmp_str, (frame->root->pid < 0), &tmp_stat);
                if (tmp_stat)
                        strcat (real_path, tmp_stat);
                /* create the directory with proper permissions */
                STACK_WIND_COOKIE (frame, trash_unlink_mkdir_cbk, tmp_cookie,
                                   FIRST_CHILD(this),
                                   FIRST_CHILD(this)->fops->mkdir,
                                   &tmp_loc, get_permission(real_path),
                                   0022, xdata);
                loc_wipe (&tmp_loc);
                goto out;
        }

        if ((op_ret == -1) && (op_errno == ENOTDIR)) {
                /* if entry is already present in trash directory,
                 * new one is not copied*/
                gf_log (this->name, GF_LOG_DEBUG,
                        "target(%s) exists, cannot keep the copy, deleting",
                        local->newpath);

                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink,
                            &local->loc, 0, xdata);

                goto out;
        }

        if ((op_ret == -1) && (op_errno == EISDIR)) {

                /* if entry is directory,we remove directly */
                gf_log (this->name, GF_LOG_DEBUG,
                        "target(%s) exists as directory, cannot keep copy, "
                        "deleting", local->newpath);

                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink,
                            &local->loc, 0, xdata);
                goto out;
        }

        /* All other cases, unlink should return success */
        TRASH_STACK_UNWIND (unlink, frame, 0, op_errno, &local->preparent,
                            &local->postparent, xdata);
out:
        if (tmp_str)
                GF_FREE (tmp_str);
        if (tmp_cookie)
                GF_FREE (tmp_cookie);

        return ret;
}

/**
 * move backs from trash translator to truncate call
 */
int32_t
trash_common_unwind_buf_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                             int32_t op_ret, int32_t op_errno,
                             struct iatt *prebuf, struct iatt *postbuf,
                             dict_t *xdata)
{
        TRASH_STACK_UNWIND (truncate, frame, op_ret, op_errno, prebuf,
                            postbuf, xdata);
        return 0;
}



int32_t
trash_unlink_stat_cbk (call_frame_t *frame,  void *cookie, xlator_t *this,
                       int32_t op_ret, int32_t op_errno, struct iatt *buf,
                       dict_t *xdata)
{
        trash_private_t     *priv        = NULL;
        trash_local_t       *local       = NULL;
        loc_t               new_loc      = {0,};
        int                 ret          = 0;

        priv  = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        if (op_ret == -1) {
                gf_log (this->name, GF_LOG_DEBUG, "%s: %s",
                        local->loc.path, strerror (op_errno));
                TRASH_STACK_UNWIND (unlink, frame, op_ret, op_errno, buf,
                            NULL, xdata);
                ret = -1;
                goto out;
        }

        /* Only last hardlink will be moved to trash directory */
        if (buf->ia_nlink > 1) {
                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink, &local->loc,
                            0, xdata);
                goto out;
        }

        /* if the file is too big  just unlink it */
        if (buf->ia_size > (priv->max_trash_file_size)) {
                gf_log (this->name, GF_LOG_DEBUG,
                                "%s: file size too big (%"PRId64") to "
                                "move into trash directory",
                                local->loc.path, buf->ia_size);

                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink, &local->loc,
                            0, xdata);
                goto out;
        }

        /* Copies new path for renaming */
        loc_copy (&new_loc, &local->loc);
        new_loc.path = gf_strdup (local->newpath);
        if (!new_loc.path) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                ret = ENOMEM;
                goto out;
        }


        STACK_WIND (frame, trash_unlink_rename_cbk,
                    FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->rename,
                    &local->loc, &new_loc, xdata);

out:
        loc_wipe (&new_loc);

        return ret;

}

/**
 * Unlink is called internally by rm system call and also
 * by internal operations of gluster such as self-heal
 */
int32_t
trash_unlink (call_frame_t *frame, xlator_t *this, loc_t *loc, int xflags,
              dict_t *xdata)
{
        trash_private_t         *priv           = NULL;
        trash_local_t           *local          = NULL;/* files inside trash */
        int32_t                 match           = 0;
        char                    *pathbuf        = NULL;
        int                     ret             = 0;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        /* If trash is not active or not enabled through cli, then
         *  we bypass and wind back
         */
        if (!priv->state) {
                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink, loc, 0,
                            xdata);
                goto out;
        }

        /* The files removed by gluster internal operations such as self-heal,
         * should moved to trash directory , but files by client should not
         * moved
         */
        if ((frame->root->pid < 0) && !priv->internal) {
                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink, loc, 0,
                            xdata);
                goto out;
        }
        /* loc need some gfid which will be present in inode */
        uuid_copy (loc->gfid, loc->inode->gfid);

        /* Checking for valid location */
        if (uuid_is_null (loc->gfid) && uuid_is_null (loc->inode->gfid)) {
                gf_log (this->name, GF_LOG_DEBUG, "Bad address");
                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink, loc, 0,
                            xdata);
                ret = EFAULT;
                goto out;
        }

        /* This will be more accurate */
        inode_path (loc->inode, NULL, &pathbuf);
        /* Check whether the file is present under eliminate paths or
         * inside trash directory. In both cases we don't need to move the
         * file to trash directory. Instead delete it permanently
         */
        match = check_whether_eliminate_path (priv->eliminate, pathbuf);
        if ((strncmp (pathbuf, priv->newtrash_dir,
                      strlen (priv->newtrash_dir)) == 0) || (match)) {
                if (match) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "%s is a file comes under an eliminate path, "
                                "so it is not moved to trash", loc->name);
                }

                /* Trying to unlink from the trash-dir. So do the
                 * actual unlink without moving to trash-dir.
                 */
                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink, loc, 0,
                            xdata);
                goto out;
        }

        local = mem_get0 (this->local_pool);
        if (!local) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                TRASH_STACK_UNWIND (unlink, frame, -1, ENOMEM, NULL, NULL,
                                    xdata);
                ret = ENOMEM;
                goto out;
        }
        frame->local = local;
        loc_copy (&local->loc, loc);

        /* rename new location of file as starting from trash directory */
        strcpy (local->origpath, pathbuf);
        copy_trash_path (priv->newtrash_dir, (frame->root->pid < 0),
                                                        local->newpath);
        strcat (local->newpath, pathbuf);

        /* append timestamp to file name so that we can avoid
         * name collisions inside trash
         */
        append_time_stamp (local->newpath);

        LOCK_INIT (&frame->lock);

        STACK_WIND (frame, trash_unlink_stat_cbk,
                    FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->stat, loc, xdata);
out:
        return ret;
}

/**
 * Use this when a failure occurs, and delete the newly created file
 */
int32_t
trash_truncate_unlink_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                           int32_t op_ret, int32_t op_errno,
                           struct iatt *preparent, struct iatt *postparent,
                           dict_t *xdata)
{
        trash_local_t     *local  = NULL;

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        if (op_ret == -1) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "deleting the newly created file: %s",
                        strerror (op_errno));
        }

        STACK_WIND (frame, trash_common_unwind_buf_cbk,
                    FIRST_CHILD(this), FIRST_CHILD(this)->fops->truncate,
                    &local->loc, local->fop_offset, xdata);
out:
        return 0;
}

/**
 * Read from source file
 */
int32_t
trash_truncate_readv_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                          int32_t op_ret, int32_t op_errno,
                          struct iovec *vector, int32_t count,
                          struct iatt *stbuf, struct iobref *iobuf,
                          dict_t *xdata)
{

        trash_local_t    *local  = NULL;

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        if (op_ret == -1) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "readv on the existing file failed: %s",
                        strerror (op_errno));

                STACK_WIND (frame, trash_truncate_unlink_cbk,
                            FIRST_CHILD(this), FIRST_CHILD(this)->fops->unlink,
                            &local->newloc, 0, xdata);
                goto out;
        }

        local->fsize = stbuf->ia_size;
        STACK_WIND (frame, trash_truncate_writev_cbk, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->writev,
                    local->newfd, vector, count, local->cur_offset, 0, iobuf,
                    xdata);

out:
        return 0;

}

/**
 * Write to file created in trash directory
 */
int32_t
trash_truncate_writev_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                           int32_t op_ret, int32_t op_errno,
                           struct iatt *prebuf, struct iatt *postbuf,
                           dict_t *xdata)
{
        trash_local_t    *local  = NULL;

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        if (op_ret == -1) {
                /* Let truncate work, but previous copy is not preserved. */
                gf_log (this->name, GF_LOG_DEBUG,
                        "writev on the existing file failed: %s",
                        strerror (op_errno));

                STACK_WIND (frame, trash_truncate_unlink_cbk, FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink, &local->newloc, 0,
                            xdata);
                goto out;
        }

        if (local->cur_offset < local->fsize) {
                local->cur_offset += GF_BLOCK_READV_SIZE;
                /* Loop back and Read the contents again. */
                STACK_WIND (frame, trash_truncate_readv_cbk,
                            FIRST_CHILD(this), FIRST_CHILD(this)->fops->readv,
                            local->fd, (size_t)GF_BLOCK_READV_SIZE,
                            local->cur_offset, 0, xdata);
                goto out;
        }


        /* OOFH.....Finally calling Truncate. */
        STACK_WIND (frame, trash_common_unwind_buf_cbk, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->truncate, &local->loc,
                    local->fop_offset, xdata);

out:
        return 0;
}

/**
 * The source file is opened for reading and writing
 */
int32_t
trash_truncate_open_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, fd_t *fd,
                         dict_t *xdata)
{
        trash_local_t    *local  = NULL;

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        if (op_ret == -1) {
                /* Let truncate work, but previous copy is not preserved. */
                gf_log (this->name, GF_LOG_DEBUG,
                        "open on the existing file failed: %s",
                        strerror (op_errno));

                STACK_WIND (frame, trash_truncate_unlink_cbk,
                            FIRST_CHILD(this), FIRST_CHILD(this)->fops->unlink,
                            &local->newloc, 0, xdata);
                goto out;
        }

        local->cur_offset = 0;

        STACK_WIND (frame, trash_truncate_readv_cbk,
                    FIRST_CHILD (this), FIRST_CHILD (this)->fops->readv,
                    local->fd, (size_t)GF_BLOCK_READV_SIZE, local->cur_offset,
                    0, xdata);

out:
        return 0;
}

/**
 * Creates new file descriptor for read and write operations,
 * if the path is present in trash directory
 */
int32_t
trash_truncate_create_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                           int32_t op_ret, int32_t op_errno, fd_t *fd,
                           inode_t *inode, struct iatt *buf,
                           struct iatt *preparent, struct iatt *postparent,
                           dict_t *xdata)
{
        trash_local_t        *local                  = NULL;
        char                 *tmp_str                = NULL;
        char                 *dir_name               = NULL;
        char                 *tmp_path               = NULL;
        int32_t              flags                   = 0;
        loc_t                tmp_loc                 = {0,};
        char                 *tmp_stat               = NULL;
        char                 real_path[PATH_MAX]     = {0,};
        trash_private_t     *priv               = NULL;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        /* Checks whether path is present in trash directory or not */

        if ((op_ret == -1) && (op_errno == ENOENT)) {
                /* Creating the directory structure here. */
                tmp_str = gf_strdup (local->newpath);
                if (!tmp_str) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        goto out;
                }
                dir_name = dirname (tmp_str);

                tmp_path = gf_strdup (dir_name);
                if (!tmp_path) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        goto out;
                }
                loc_copy (&tmp_loc, &local->newloc);
                tmp_loc.path = gf_strdup (tmp_path);
                if (!tmp_loc.path) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        goto out;
                }
                strcpy (real_path, priv->brick_path);
                remove_trash_path (tmp_path, (frame->root->pid < 0), &tmp_stat);
                if (tmp_stat)
                        strcat (real_path, tmp_stat);
                /* create the directory with proper permissions */
                STACK_WIND_COOKIE (frame, trash_truncate_mkdir_cbk,
                                   tmp_path, FIRST_CHILD(this),
                                   FIRST_CHILD(this)->fops->mkdir,
                                   &tmp_loc, get_permission(real_path),
                                   0022, xdata);
                loc_wipe (&tmp_loc);
                goto out;
        }

        if (op_ret == -1) {
                /* Let truncate work, but previous copy is not preserved.
                 * Deleting the newly created copy.
                 */
                gf_log (this->name, GF_LOG_DEBUG,
                        "creation of new file in trash-dir failed, "
                        "when truncate was called: %s", strerror (op_errno));

                STACK_WIND (frame, trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->truncate, &local->loc,
                            local->fop_offset, xdata);
                goto out;
        }

        flags = O_RDONLY;

        /* fd which represents source file for reading and writing from it */

        local->fd = fd_create (local->loc.inode, frame->root->pid);

        STACK_WIND (frame, trash_truncate_open_cbk,  FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->open, &local->loc, flags,
                    local->fd, 0);
out:
        if (tmp_str)
                GF_FREE (tmp_str);
        if (tmp_path)
                GF_FREE (tmp_path);

        return 0;
}

/**
 * If the path is not present in the trash directory,it will recursively call
 * this call-back and one by one directories will be created from the
 * beginning
 */
int32_t
trash_truncate_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                          int32_t op_ret, int32_t op_errno, inode_t *inode,
                          struct iatt *stbuf, struct iatt *preparent,
                          struct iatt *postparent, dict_t *xdata)
{
        trash_local_t        *local              = NULL;
        trash_private_t      *priv               = NULL;
        char                 *tmp_str            = NULL;
        char                 *tmp_path           = NULL;
        char                 *tmp_dirname        = NULL;
        char                 *dir_name           = NULL;
        char                 *tmp_stat           = NULL;
        char                 real_path[PATH_MAX] = {0,};
        size_t               count               = 0;
        int32_t              flags               = 0;
        int32_t              loop_count          = 0;
        int                  i                   = 0;
        loc_t                tmp_loc             = {0,};
        int                  ret                 = 0;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        loop_count = local->loop_count;

        tmp_str = gf_strdup (local->newpath);
        if (!tmp_str) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                ret = ENOMEM;
                goto out;
        }

        if ((op_ret == -1) && (op_errno == ENOENT)) {
                tmp_dirname = strchr (tmp_str, '/');
                while (tmp_dirname) {
                        count = tmp_dirname - tmp_str;
                        if (count == 0)
                                count = 1;
                        i++;
                        if (i > loop_count)
                                break;
                        tmp_dirname = strchr (tmp_str + count + 1, '/');
                }
                tmp_path = gf_memdup (local->newpath, count + 1);
                if (!tmp_path) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                tmp_path[count] = '\0';

                loc_copy (&tmp_loc, &local->newloc);
                tmp_loc.path = gf_strdup (tmp_path);
                if (!tmp_loc.path) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }

                /* Stores the the name of directory to be created */
                tmp_loc.name = gf_strdup (strrchr(tmp_path, '/') + 1);
                if (!tmp_loc.name) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                strcpy (real_path, priv->brick_path);
                remove_trash_path (tmp_path, (frame->root->pid < 0), &tmp_stat);
                if (tmp_stat)
                        strcat (real_path, tmp_stat);
                STACK_WIND_COOKIE (frame, trash_truncate_mkdir_cbk,
                                   tmp_path, FIRST_CHILD(this),
                                   FIRST_CHILD(this)->fops->mkdir,
                                   &tmp_loc, get_permission(real_path),
                                   0022, xdata);
                loc_wipe (&tmp_loc);
                goto out;
        }

        if (op_ret == 0) {
                dir_name = dirname (tmp_str);
                if (strcmp ((char*)cookie, dir_name) == 0) {
                        flags = O_CREAT|O_EXCL|O_WRONLY;
                        strcpy (real_path, priv->brick_path);
                        strcat (real_path, local->origpath);
                        /* Call create again once directory structure
                           is created. */
                        STACK_WIND (frame, trash_truncate_create_cbk,
                                    FIRST_CHILD(this),
                                    FIRST_CHILD(this)->fops->create,
                                    &local->newloc, flags,
                                    get_permission (real_path),
                                    0022, local->newfd, xdata);
                        goto out;
                }
        }

        LOCK (&frame->lock);
        {
                loop_count = ++local->loop_count;
        }
        UNLOCK (&frame->lock);

        tmp_dirname = strchr (tmp_str, '/');
        while (tmp_dirname) {
                count = tmp_dirname - tmp_str;
                if (count == 0)
                        count = 1;
                i++;
                if ((i > loop_count) || (count > PATH_MAX))
                        break;
                tmp_dirname = strchr (tmp_str + count + 1, '/');
        }
        tmp_path = gf_memdup (local->newpath, count + 1);
        if (!tmp_path) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                ret = ENOMEM;
                goto out;
        }
        tmp_path[count] = '\0';

        loc_copy (&tmp_loc, &local->newloc);
        tmp_loc.path = gf_strdup (tmp_path);
        if (!tmp_loc.path) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                ret = ENOMEM;
                goto out;
        }

        /* Stores the the name of directory to be created */
        tmp_loc.name = gf_strdup (strrchr(tmp_path, '/') + 1);
        if (!tmp_loc.name) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                goto out;
        }

        strcpy (real_path, priv->brick_path);
        remove_trash_path (tmp_path, (frame->root->pid < 0), &tmp_stat);
        if (tmp_stat)
                strcat (real_path, tmp_stat);

        STACK_WIND_COOKIE (frame, trash_truncate_mkdir_cbk, tmp_path,
                           FIRST_CHILD(this),
                           FIRST_CHILD(this)->fops->mkdir, &tmp_loc,
                           get_permission(real_path),
                           0022, xdata);

out:
        if (tmp_str)
                GF_FREE (tmp_str);
        if (tmp_path)
                GF_FREE (tmp_path);

        return ret;
}


int32_t
trash_truncate_stat_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, struct iatt *buf,
                         dict_t *xdata)
{
        trash_private_t       *priv                   = NULL;
        trash_local_t         *local                  = NULL;
        char                  loc_newname[PATH_MAX]   = {0,};
        int32_t               flags                   = 0;
        dentry_t              *dir_entry              = NULL;
        inode_table_t         *table                  = NULL;
        int                   ret                     = 0;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        local = frame->local;
        GF_VALIDATE_OR_GOTO ("trash", local, out);

        table = local->loc.inode->table;

        pthread_mutex_lock (&table->lock);
        {
                dir_entry = __dentry_search_arbit (local->loc.inode);
        }
        pthread_mutex_unlock (&table->lock);

        if (op_ret == -1) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "fstat on the file failed: %s",
                        strerror (op_errno));

                TRASH_STACK_UNWIND (truncate, frame, op_ret, op_errno, buf,
                                NULL, xdata);
                goto out;
        }

        /* Only last hardlink will be moved to trash directory */
        if (buf->ia_nlink > 1) {
                STACK_WIND (frame, trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->truncate,
                            &local->loc, local->fop_offset, xdata);
                goto out;
        }

        /**
         * If the file is too big or if it is extended truncate,
         * just don't move it to trash directory.
         */
        if (buf->ia_size > (priv->max_trash_file_size) ||
                                buf->ia_size <= local->fop_offset) {
                gf_log (this->name, GF_LOG_DEBUG, "%s: not moving to trash , "
                           "having inappropiate file size", local->loc.path);

                STACK_WIND (frame,  trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->truncate,
                            &local->loc, local->fop_offset, xdata);
                goto out;
        }

        /* Retrives the name of file from path */
        local->loc.name = gf_strdup (strrchr (local->loc.path, '/'));
        if (!local->loc.name) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                goto out;
        }

        /* Stores new path for source file */
        copy_trash_path (priv->newtrash_dir, (frame->root->pid < 0),
                                                        local->newpath);
        strcat (local->newpath, local->loc.path);

        /* append timestamp to file name so that we can avoid
           name collisions inside trash */
        append_time_stamp (local->newpath);

        strcpy (loc_newname, local->loc.name);
        append_time_stamp (loc_newname);
        /* local->newloc represents old file(file inside trash),
           where as local->loc represents truncated file. We need
           to create new inode and fd for new file*/
        local->newloc.name = gf_strdup (loc_newname);
        if (!local->newloc.name) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                ret = ENOMEM;
                goto out;
        }
        local->newloc.path = gf_strdup (local->newpath);
        if (!local->newloc.path) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                ret = ENOMEM;
                goto out;
        }
        local->newloc.inode = inode_new (local->loc.inode->table);
        local->newfd = fd_create (local->newloc.inode, frame->root->pid);

        /* Creating vaild parent and pargfids for both files */

        local->loc.parent = inode_ref (dir_entry->parent);
        uuid_copy (local->loc.pargfid, dir_entry->parent->gfid);

        local->newloc.parent = inode_ref (dir_entry->parent);
        uuid_copy (local->newloc.pargfid, dir_entry->parent->gfid);

        flags = O_CREAT|O_EXCL|O_WRONLY;

        STACK_WIND (frame, trash_truncate_create_cbk,
                    FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->create,
                    &local->newloc, flags,
                    st_mode_from_ia (buf->ia_prot, local->loc.inode->ia_type),
                    0022, local->newfd, xdata);

out:
        return ret;
}

/**
 * Truncate can be explicitly called or implicitly by some other applications
 * like text editors etc..
 */
int32_t
trash_truncate (call_frame_t *frame, xlator_t *this, loc_t *loc,
                off_t offset, dict_t *xdata)
{
        trash_private_t        *priv           = NULL;
        trash_local_t          *local          = NULL;
        int32_t                match           = 0;
        char                   *pathbuf        = NULL;
        int                    ret             = 0;

        priv  = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);
        /* If trash is not active or not enabled through cli, then
         * we bypass and wind back
         */
        if (!priv->state) {
                STACK_WIND (frame, trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->truncate, loc,
                            offset, xdata);
                goto out;
        }

        /* The files removed by gluster operations such as self-heal,
           should moved to trash directory, but files by client should
           not moved */
        if ((frame->root->pid < 0) && !priv->internal) {
                STACK_WIND (frame, trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->truncate, loc,
                            offset, xdata);
                goto out;
        }
        /* This will be more accurate */
        inode_path(loc->inode, NULL, &pathbuf);

        /* Checks whether file is in trash directory or eliminate path.
         * In all such cases it does not move to trash directory,
         * truncate will be performed
         */
        match = check_whether_eliminate_path (priv->eliminate, pathbuf);

        if ((strncmp (pathbuf, priv->newtrash_dir,
                      strlen (priv->newtrash_dir)) == 0) || (match)) {
                if (match) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "%s: file not moved to trash as per option "
                                "'eliminate path'", loc->path);
                }

                /* Trying to truncate from the trash-dir. So do the
                 * actual truncate without moving to trash-dir.
                 */
                STACK_WIND (frame, trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->truncate, loc, offset,
                            xdata);
                goto out;
        }

        LOCK_INIT (&frame->lock);

        local = mem_get0 (this->local_pool);
        if (!local) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                TRASH_STACK_UNWIND (truncate, frame, -1, ENOMEM, NULL, NULL,
                                    xdata);
                ret = ENOMEM;
                goto out;
        }

        loc_copy (&local->loc, loc);
        local->loc.path = pathbuf;
        local->fop_offset = offset;

        frame->local = local;

        STACK_WIND (frame, trash_truncate_stat_cbk,
                    FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->stat, loc,
                    xdata);

out:
        return ret;
}

/**
 * When we call truncate from terminal it comes to ftruncate of trash-xlator.
 * Since truncate internally calls ftruncate and we receive fd of the file,
 * other than that it also called by Rebalance operation
 */
int32_t
trash_ftruncate (call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
                 dict_t *xdata)
{
        trash_private_t      *priv           = NULL;
        trash_local_t        *local          = NULL;/* file inside trash */
        char                 *pathbuf        = NULL;/* path of file from fd */
        int32_t              retval          = 0;
        int32_t              match           = 0;
        int                  ret             = 0;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);
        /* If trash is not active or not enabled through cli, then
         * we bypass and wind back
         */
        if (!priv->state) {
                STACK_WIND (frame, trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->ftruncate, fd,
                            offset, xdata);
                goto out;
        }

        /* The files removed by gluster operations such as self-heal,
         * should moved to trash directory, but files by client
         * should not moved
         */
        if ((frame->root->pid < 0) && !priv->internal) {
                STACK_WIND (frame, trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->ftruncate, fd,
                            offset, xdata);
                goto out;
        }
        /* This will be more accurate */
        retval = inode_path (fd->inode, NULL, &pathbuf);

        /* Checking  the eliminate path */

        /* Checks whether file is trash directory or eliminate path or
         * invalid fd. In all such cases it does not move to trash directory,
         * ftruncate will be performed
         */
        match = check_whether_eliminate_path (priv->eliminate, pathbuf);
        if ((strncmp (pathbuf, priv->newtrash_dir,
                      strlen (priv->newtrash_dir)) == 0) || match ||
                      !retval) {

                if (match) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "%s: file matches eliminate path, "
                                "not moved to trash", pathbuf);
                }

                /* Trying to ftruncate from the trash-dir. So do the
                 * actual ftruncate without moving to trash-dir
                 */
                STACK_WIND (frame, trash_common_unwind_buf_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->ftruncate,
                            fd, offset, xdata);
                goto out;
        }

        local = mem_get0 (this->local_pool);
        if (!local) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                TRASH_STACK_UNWIND (ftruncate, frame, -1, ENOMEM, NULL,
                                    NULL, xdata);
                ret     = -1;
                goto out;
        }

        /* To convert fd to location */
        frame->local=local;

        local->loc.path  = pathbuf;
        local->loc.inode = inode_ref (fd->inode);
        uuid_copy (local->loc.gfid, local->loc.inode->gfid);

        local->fop_offset = offset;

        /* Else remains same to truncate code, so from here flow goes
         * to truncate_stat
         */
        STACK_WIND (frame, trash_truncate_stat_cbk, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->fstat, fd, xdata);
out:
        return ret;
}

/**
 * The mkdir call is intercepted to avoid creation of
 * trash directory in the mount by the user
 */
int32_t
trash_mkdir (call_frame_t *frame, xlator_t *this,
             loc_t *loc, mode_t mode, mode_t umask, dict_t *xdata)
{
        int32_t               op_ret      = 0;
        int32_t               op_errno    = 0;
        trash_private_t       *priv       = NULL;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        if (!check_whether_trash_directory (loc->path, priv->newtrash_dir)) {
                gf_log (this->name, GF_LOG_WARNING,
                        "mkdir issued on %s, which is not permitted",
                        priv->newtrash_dir);
                op_errno = EPERM;
                op_ret = -1;

                STACK_UNWIND_STRICT (mkdir, frame, op_ret, op_errno,
                             NULL, NULL, NULL, NULL, xdata);
        } else {
                STACK_WIND (frame, trash_common_mkdir_cbk, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->mkdir, loc, mode, umask, xdata);
        }
out:
        return 0;
}

/**
 * The rename call is intercepted to avoid renaming
 * of trash directory in the mount by the user
 */
int
trash_rename (call_frame_t *frame, xlator_t *this,
              loc_t *oldloc, loc_t *newloc, dict_t *xdata)
{
        int32_t               op_ret      = 0;
        int32_t               op_errno    = 0;
        trash_private_t       *priv       = NULL;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        if (!check_whether_trash_directory (oldloc->path, priv->newtrash_dir)) {
                gf_log (this->name, GF_LOG_WARNING,
                        "rename issued on %s, which is not permitted",
                        priv->newtrash_dir);
                op_errno = EPERM;
                op_ret = -1;

                STACK_UNWIND_STRICT (rename, frame, op_ret, op_errno, NULL,
                                     NULL, NULL, NULL, NULL, xdata);
        } else {
                STACK_WIND (frame, trash_common_rename_cbk, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->rename, oldloc, newloc, xdata);
        }
out:
        return 0;
}

/**
 * The rmdir call is intercepted to avoid deletion of
 * trash directory in the mount by the user
 */
int32_t
trash_rmdir (call_frame_t *frame, xlator_t *this,
             loc_t *loc, int flags, dict_t *xdata)
{
        int32_t               op_ret      = 0;
        int32_t               op_errno    = 0;
        trash_private_t       *priv       = NULL;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        if (!check_whether_trash_directory (loc->path, priv->newtrash_dir)) {
                gf_log (this->name, GF_LOG_WARNING,
                        "rmdir issued on %s, which is not permitted",
                        priv->newtrash_dir);
                op_errno = EPERM;
                op_ret = -1;

                STACK_UNWIND_STRICT (rmdir, frame, op_ret, op_errno,
                             NULL, NULL, xdata);
        } else {
                STACK_WIND (frame, trash_common_rmdir_cbk, FIRST_CHILD(this),
                    FIRST_CHILD(this)->fops->rmdir, loc, flags, xdata);
        }
out:
        return 0;
}

/**
 * Volume set option is handled by the reconfigure funtion.
 * Here we checks whether each option is set or not ,if it
 * sets then corresponding modifciations will be made
 */
int
reconfigure (xlator_t *this, dict_t *options)
{
        uint64_t              max_fsize           = 0;
        int                   ret                 = 0;
        char                  *tmp                = NULL;
        char                  *tmp_str            = NULL;
        trash_private_t       *priv               = NULL;
        loc_t                 old_loc             = {0, };
        loc_t                 new_loc             = {0, };
        call_frame_t          *frame              = NULL;
        char                  trash_dir[PATH_MAX] = {0,};

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        GF_OPTION_RECONF ("trash", priv->state, options, bool, out);

        GF_OPTION_RECONF ("trash-dir", tmp, options, str, out);
        if (tmp) {
                sprintf(trash_dir, "/%s/", tmp);
                if (strcmp(priv->newtrash_dir, trash_dir) != 0) {

                        /* When user set a new name for trash directory, trash
                        * xlator will perform a rename operation on old trash
                        * directory to the new one using a STACK_WIND from here.
                        * This option can be configured only when volume is in
                        * started state
                        */

                        GF_FREE (priv->newtrash_dir);

                        priv->newtrash_dir = gf_strdup (trash_dir);
                        if (!priv->newtrash_dir) {
                                ret = ENOMEM;
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }
                        gf_log (this->name, GF_LOG_DEBUG,
                                "Renaming %s -> %s from reconfigure",
                                priv->oldtrash_dir, priv->newtrash_dir);

                        if (!priv->newtrash_dir) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                ret = ENOMEM;
                                goto out;
                        }
                        frame = create_frame (this, this->ctx->pool);

                        /* assign new location values to new_loc members */
                        uuid_copy (new_loc.gfid, trash_gfid);
                        uuid_copy (new_loc.pargfid, root_gfid);
                        ret = extract_trash_directory (priv->newtrash_dir,
                                                                &new_loc.name);
                        if (ret) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }
                        new_loc.path = gf_strdup (priv->newtrash_dir);
                        if (!new_loc.path) {
                                ret = ENOMEM;
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }

                        /* assign old location values to old_loc members */
                        uuid_copy (old_loc.gfid, trash_gfid);
                        uuid_copy (old_loc.pargfid, root_gfid);
                        ret = extract_trash_directory (priv->oldtrash_dir,
                                                                &old_loc.name);
                        if (ret) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }
                        old_loc.path = gf_strdup (priv->oldtrash_dir);
                        if (!old_loc.path) {
                                ret = ENOMEM;
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }

                        old_loc.inode = inode_ref (priv->trash_inode);
                        uuid_copy(old_loc.inode->gfid, old_loc.gfid);

                        STACK_WIND (frame, trash_reconf_rename_cbk,
                                    FIRST_CHILD(this),
                                    FIRST_CHILD(this)->fops->rename,
                                    &old_loc, &new_loc, options);
                        if (priv->oldtrash_dir)
                                GF_FREE (priv->oldtrash_dir);

                        priv->oldtrash_dir = gf_strdup(priv->newtrash_dir);
                        if (!priv->oldtrash_dir) {
                                ret = ENOMEM;
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }
                }
        }
        tmp = NULL;

        GF_OPTION_RECONF ("trash-internal-op", priv->internal, options,
                                               bool, out);

        GF_OPTION_RECONF ("trash-max-filesize", max_fsize, options,
                                                size_uint64, out);
        if (max_fsize) {
                if (max_fsize > GF_ALLOWED_MAX_FILE_SIZE) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "Size specified for max-size(in MB) is too "
                                "large so using 1GB as max-size (NOT IDEAL)");
                        priv->max_trash_file_size = GF_ALLOWED_MAX_FILE_SIZE;
                } else
                        priv->max_trash_file_size = max_fsize;
                gf_log (this->name, GF_LOG_DEBUG, "%"GF_PRI_SIZET" max-size",
                        priv->max_trash_file_size);
        }
        GF_OPTION_RECONF ("trash-eliminate-path", tmp, options, str, out);
        if (!tmp) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "no option specified for 'eliminate', using NULL");
        } else {
                if (priv->eliminate)
                        wipe_eliminate_path (&priv->eliminate);

                tmp_str = gf_strdup (tmp);
                if (!tmp_str) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                ret = store_eliminate_path (tmp_str, &priv->eliminate);

        }

out:
        if (tmp_str)
                GF_FREE (tmp_str);
        loc_wipe (&new_loc);
        loc_wipe (&old_loc);

        return ret;
}

/**
 * Notify is used to create the trash directory with fixed gfid
 * using STACK_WIND only when posix xlator is up
 */
int
notify (xlator_t *this, int event, void *data, ...)
{
        trash_private_t       *priv      = NULL;
        dict_t                *dict      = NULL;
        int                   ret        = 0;
        uuid_t                *tgfid_ptr = NULL;
        loc_t                 loc        = {0, };
        loc_t                 old_loc    = {0, };
        call_frame_t          *frame     = NULL;

        priv = this->private;
        GF_VALIDATE_OR_GOTO ("trash", priv, out);

        /* Check whether posix is up not */
        if (event == GF_EVENT_CHILD_UP) {
                frame = create_frame(this, this->ctx->pool);
                dict = dict_new ();
                if (!dict) {
                        ret = ENOMEM;
                        goto out;
                }
                priv->trash_itable = inode_table_new (0, this);

                /* Here there is two possiblities ,if trash directory already
                 * exist ,then we need to perform a rename operation on the
                 * old one. Otherwise, we need to create the trash directory
                 * For both, we need to pass location variable, gfid of parent
                 * and a frame for calling STACK_WIND.The location variable
                 * requires name,path,gfid and inode
                 */
                if (!priv->oldtrash_dir) {
                        loc.inode = inode_new (priv->trash_itable);
                        uuid_copy (loc.gfid, trash_gfid);

                        gf_log (this->name, GF_LOG_DEBUG, "nameless lookup for"
                                           "old trash directory");
                        STACK_WIND (frame, trash_notify_lookup_cbk,
                                    FIRST_CHILD(this),
                                    FIRST_CHILD(this)->fops->lookup,
                                    &loc, dict);
                        gf_log (this->name, GF_LOG_DEBUG, "old_trash_dir %s",
                                        priv->oldtrash_dir);
                        loc_wipe (&loc);
                }

                if (strcmp (priv->oldtrash_dir, priv->newtrash_dir) == 0) {
                        gf_log (this->name, GF_LOG_DEBUG, "Creating trash "
                                           "directory %s from notify",
                                           priv->newtrash_dir);

                        tgfid_ptr = GF_CALLOC (1, sizeof(uuid_t),
                                                  gf_common_mt_uuid_t);
                        if (!tgfid_ptr) {
                                ret = ENOMEM;
                                goto out;
                        }
                        uuid_copy (*tgfid_ptr, trash_gfid);

                        uuid_copy (loc.gfid, trash_gfid);
                        uuid_copy (loc.pargfid, root_gfid);
                        ret = extract_trash_directory (priv->newtrash_dir,
                                                                &loc.name);
                        if (ret) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }
                        loc.path = gf_strdup (priv->newtrash_dir);
                        if (!loc.path) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                ret = ENOMEM;
                                goto out;
                        }

                        priv->trash_inode = inode_new (priv->trash_itable);
                        priv->trash_inode->ia_type = IA_IFDIR;
                        loc.inode = inode_ref (priv->trash_inode);

                        /* Fixed gfid is set for trash directory with
                         * this function
                         */
                        ret = dict_set_dynptr (dict, "gfid-req", tgfid_ptr,
                                              sizeof (uuid_t));
                        if (ret) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "setting key gfid-req failed");
                                goto out;
                        }

                        /* The mkdir call for creating trash directory */
                        STACK_WIND (frame, trash_notify_mkdir_cbk,
                                    FIRST_CHILD(this),
                                    FIRST_CHILD(this)->fops->mkdir, &loc, 0755,
                                    0022, dict);
                } else {
                        /* assign new location values to new_loc members */
                        gf_log (this->name, GF_LOG_DEBUG, "Renaming %s -> %s"
                                        " from notify", priv->oldtrash_dir,
                                        priv->newtrash_dir);
                        uuid_copy (loc.gfid, trash_gfid);
                        uuid_copy (loc.pargfid, root_gfid);
                        ret = extract_trash_directory (priv->newtrash_dir,
                                                                &loc.name);
                        if (ret) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }
                        loc.path = gf_strdup (priv->newtrash_dir);
                        if (!loc.path) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                ret = ENOMEM;
                                goto out;
                        }
                        /* assign old location values to old_loc members */
                        uuid_copy (old_loc.gfid, trash_gfid);
                        uuid_copy (old_loc.pargfid, root_gfid);
                        ret = extract_trash_directory (priv->oldtrash_dir,
                                                                &old_loc.name);
                        if (ret) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                goto out;
                        }
                        old_loc.path = gf_strdup (priv->oldtrash_dir);
                        if (!old_loc.path) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                ret = ENOMEM;
                                goto out;
                        }

                        old_loc.inode = inode_ref (priv->trash_inode);
                        uuid_copy(old_loc.inode->gfid, old_loc.gfid);

                        STACK_WIND (frame, trash_notify_rename_cbk,
                                    FIRST_CHILD(this),
                                    FIRST_CHILD(this)->fops->rename,
                                    &old_loc, &loc, dict);
                        if (priv->oldtrash_dir)
                                GF_FREE (priv->oldtrash_dir);

                        priv->oldtrash_dir = gf_strdup(priv->newtrash_dir);
                        if (!priv->oldtrash_dir) {
                                gf_log (this->name, GF_LOG_DEBUG,
                                                "out of memory");
                                ret = ENOMEM;
                                goto out;
                        }
                }
        } else {
                ret = default_notify (this, event, data);
                if (ret)
                        gf_log (this->name, GF_LOG_INFO,
                                "default notify event failed");
        }

out:
        if (ret && tgfid_ptr)
                GF_FREE (tgfid_ptr);
        if (dict)
                dict_unref (dict);
        loc_wipe (&loc);
        loc_wipe (&old_loc);

        return ret;
}

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

        GF_VALIDATE_OR_GOTO ("trash", this, out);

        ret = xlator_mem_acct_init (this, gf_trash_mt_end + 1);
        if (ret != 0) {
                gf_log(this->name, GF_LOG_ERROR, "Memory accounting init"
                       "failed");
                return ret;
        }
out:
        return ret;
}

/**
 * trash_init
 */
int32_t
init (xlator_t *this)
{
        trash_private_t        *priv                   = NULL;
        int                    ret                     = -1;
        char                   *tmp                    = NULL;
        char                   *tmp_str                = NULL;
        char                   trash_dir[PATH_MAX]     = {0,};
        uint64_t               max_trash_file_size64   = 0;
        data_t                *data                    = NULL;

        GF_VALIDATE_OR_GOTO ("trash", this, out);

        if (!this->children || this->children->next) {
                gf_log (this->name, GF_LOG_ERROR,
                        "not configured with exactly one child. exiting");
                ret = -1;
                goto out;
        }

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

        priv = GF_CALLOC (1, sizeof (*priv), gf_trash_mt_trash_private_t);
        if (!priv) {
                gf_log (this->name, GF_LOG_ERROR, "out of memory");
                ret = ENOMEM;
                goto out;
        }

        /* Trash priv data members are initialized through the following
         * set of statements
         */
        GF_OPTION_INIT ("trash", priv->state, bool, out);

        GF_OPTION_INIT ("trash-dir", tmp, str, out);

        /* We store trash dir value as path for easier manipulation*/
        if (!tmp) {
                gf_log (this->name, GF_LOG_INFO,
                        "no option specified for 'trash-dir', "
                        "using \"/.trashcan/\"");
                priv->newtrash_dir = gf_strdup ("/.trashcan/");
                if (!priv->newtrash_dir) {
                        ret = ENOMEM;
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        goto out;
                }
        } else {
                sprintf(trash_dir, "/%s/", tmp);
                priv->newtrash_dir = gf_strdup (trash_dir);
                if (!priv->newtrash_dir) {
                        ret = ENOMEM;
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        goto out;
                }
        }
        tmp = NULL;

        GF_OPTION_INIT ("trash-eliminate-path", tmp, str, out);
        if (!tmp) {
                gf_log (this->name, GF_LOG_INFO,
                        "no option specified for 'eliminate', using NULL");
        } else {
                tmp_str = gf_strdup (tmp);
                if (!tmp_str) {
                        gf_log (this->name, GF_LOG_ERROR,
                                        "out of memory");
                        ret = ENOMEM;
                        goto out;
                }
                ret = store_eliminate_path (tmp_str, &priv->eliminate);

        }
        tmp = NULL;

        GF_OPTION_INIT ("trash-max-filesize", max_trash_file_size64,
                        size_uint64, out);
        if (!max_trash_file_size64) {
                gf_log (this->name, GF_LOG_ERROR,
                        "no option specified for 'max-trashable-file-size', "
                        "using default = %lld MB",
                        GF_DEFAULT_MAX_FILE_SIZE / GF_UNIT_MB);
                priv->max_trash_file_size = GF_DEFAULT_MAX_FILE_SIZE;
        } else {
                if( max_trash_file_size64 > GF_ALLOWED_MAX_FILE_SIZE ) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "Size specified for max-size(in MB) is too "
                                "large so using 1GB as max-size (NOT IDEAL)");
                        priv->max_trash_file_size = GF_ALLOWED_MAX_FILE_SIZE;
                } else
                        priv->max_trash_file_size = max_trash_file_size64;
                gf_log (this->name, GF_LOG_DEBUG, "%"GF_PRI_SIZET" max-size",
                        priv->max_trash_file_size);
        }

        GF_OPTION_INIT ("trash-internal-op", priv->internal, bool, out);

        this->local_pool = mem_pool_new (trash_local_t, 64);
        if (!this->local_pool) {
                gf_log (this->name, GF_LOG_ERROR,
                        "failed to create local_t's memory pool");
                ret = ENOMEM;
                goto out;
        }

        /* For creating directories inside trash with proper permissions,
         * we need to perform stat on that directories, for this we use
         * brick path
         */
        data = dict_get (this->options, "brick-path");
        if (!data) {
                gf_log (this->name, GF_LOG_ERROR,
                        "no option specified for 'brick-path'");
                ret = ENOMEM;
                goto out;
        }
        priv->brick_path = gf_strdup (data->data);
        if (!priv->brick_path) {
                ret = ENOMEM;
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                goto out;
        }

        gf_log (this->name, GF_LOG_DEBUG, "brick path is%s", priv->brick_path);

        this->private = (void *)priv;
        ret = 0;

out:
        if (tmp_str)
                GF_FREE (tmp_str);
        if (ret) {
                if (priv) {
                        if (priv->newtrash_dir)
                                GF_FREE (priv->newtrash_dir);
                        if (priv->oldtrash_dir)
                                GF_FREE (priv->oldtrash_dir);
                        if (priv->brick_path)
                                GF_FREE (priv->brick_path);
                        if (priv->eliminate)
                                wipe_eliminate_path (&priv->eliminate);
                        GF_FREE (priv);
                }
                mem_pool_destroy (this->local_pool);
        }
        return ret;
}

/**
 * trash_fini
 */
void
fini (xlator_t *this)
{
        trash_private_t *priv = NULL;

        GF_VALIDATE_OR_GOTO ("trash", this, out);
        priv = this->private;

        if (priv) {
                if (priv->newtrash_dir)
                        GF_FREE (priv->newtrash_dir);
                if (priv->oldtrash_dir)
                        GF_FREE (priv->oldtrash_dir);
                if (priv->brick_path)
                        GF_FREE (priv->brick_path);
                if (priv->eliminate)
                        wipe_eliminate_path (&priv->eliminate);
                GF_FREE (priv);
        }
        mem_pool_destroy (this->local_pool);
        this->private = NULL;
out:
        return;
}

struct xlator_fops fops = {
        .unlink          = trash_unlink,
        .truncate        = trash_truncate,
        .ftruncate       = trash_ftruncate,
        .rmdir           = trash_rmdir,
        .mkdir           = trash_mkdir,
        .rename          = trash_rename,
};

struct xlator_cbks cbks = {
};

struct volume_options options[] = {
        { .key           = { "trash" },
          .type          = GF_OPTION_TYPE_BOOL,
          .default_value = "off",
          .description   = "Enable/disable trash translator",
        },
        { .key           = { "trash-dir" },
          .type          = GF_OPTION_TYPE_STR,
          .default_value = ".trashcan",
          .description   = "Directory for trash files",
        },
        { .key           = { "trash-eliminate-path" },
          .type          = GF_OPTION_TYPE_STR,
          .description   = "Eliminate paths to be excluded "
                           "from trashing",
        },
        { .key           = { "trash-max-filesize" },
          .type          = GF_OPTION_TYPE_SIZET,
          .default_value = "5MB",
          .description   = "Maximum size of file that can be "
                           "moved to trash",
        },
        { .key           = { "trash-internal-op" },
          .type          = GF_OPTION_TYPE_BOOL,
          .default_value = "off",
          .description   = "Enable/disable trash translator for "
                           "internal operations",
        },
        { .key           = {NULL} },
};