File: globus_xio_gridftp_driver.c

package info (click to toggle)
globus-xio-gridftp-driver 3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,532 kB
  • sloc: sh: 11,139; ansic: 2,528; perl: 348; makefile: 199
file content (2546 lines) | stat: -rw-r--r-- 85,199 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "globus_xio_driver.h"
#include "globus_xio_gridftp_driver.h"
#include "globus_ftp_client.h"  
#include "version.h"

GlobusDebugDefine(GLOBUS_XIO_GRIDFTP);
GlobusXIODeclareDriver(gridftp);

#define GlobusXIOGridftpDebugPrintf(level, message)                           \
    GlobusDebugPrintf(GLOBUS_XIO_GRIDFTP, level, message)

#define GlobusXIOGridftpDebugEnter()                                          \
    GlobusXIOGridftpDebugPrintf(                                              \
        GLOBUS_L_XIO_GRIDFTP_DEBUG_TRACE,                                     \
        ("[%s] Entering\n", _xio_name))

#define GlobusXIOGridftpDebugExit()                                           \
    GlobusXIOGridftpDebugPrintf(                                              \
        GLOBUS_L_XIO_GRIDFTP_DEBUG_TRACE,                                     \
        ("[%s] Exiting\n", _xio_name))

#define GlobusXIOGridftpDebugExitWithError()                                  \
    GlobusXIOGridftpDebugPrintf(                                              \
        GLOBUS_L_XIO_GRIDFTP_DEBUG_TRACE,                                     \
        ("[%s] Exiting with error\n", _xio_name))

enum globus_l_xio_error_levels
{
    GLOBUS_L_XIO_GRIDFTP_DEBUG_TRACE                = 1,
    GLOBUS_L_XIO_GRIDFTP_DEBUG_INTERNAL_TRACE       = 2
};

#define GLOBUS_XIO_GRIDFTP_REQUESTOR_COUNT 8

typedef enum globus_i_xio_gridftp_state_s
{

    GLOBUS_XIO_GRIDFTP_NONE,
    GLOBUS_XIO_GRIDFTP_OPEN,
    GLOBUS_XIO_GRIDFTP_OPENING,
    GLOBUS_XIO_GRIDFTP_IO_PENDING,
    GLOBUS_XIO_GRIDFTP_IO_DONE,
    GLOBUS_XIO_GRIDFTP_ABORT_PENDING,
    GLOBUS_XIO_GRIDFTP_ABORT_PENDING_IO_PENDING,
    GLOBUS_XIO_GRIDFTP_ABORT_PENDING_CLOSING

} globus_i_xio_gridftp_state_t;

typedef struct
{
    globus_ftp_client_handle_t *        ftp_handle;
    globus_ftp_client_operationattr_t   ftp_operation_attr;
    globus_bool_t                       partial_xfer;
    globus_bool_t			append;
    char *				eret_alg_str;
    char *				esto_alg_str;

} globus_l_xio_gridftp_attr_t;

static globus_l_xio_gridftp_attr_t      globus_l_xio_gridftp_attr_default =
{
    NULL,
    NULL,
    GLOBUS_FALSE,
    GLOBUS_FALSE,
    NULL,
    NULL
};

typedef struct
{
    globus_ftp_client_handle_t *        ftp_handle;
    globus_l_xio_gridftp_attr_t *       attr;
    globus_i_xio_gridftp_state_t        state;  
    globus_memory_t                     requestor_memory;
    globus_fifo_t                       pending_ops_q;
    char *                              url;    

    /* TRUE - read, FALSE - write */
    globus_bool_t                       outstanding_ops_direction; 
    globus_bool_t                       pending_ops_direction; 
    struct globus_i_xio_gridftp_requestor_s *  
                                        partial_requestor;        

    /* this is necessary because xfer_cb might be called before io_cb */
    globus_bool_t                       xfer_done;
    int                                 outstanding_io_count;
    /* No pending_io_count (I use globus_fifo_empty(pending_ops_q) instead) */ 

    globus_off_t                        offset;
    globus_off_t                        end_offset;
    globus_off_t                        size;
    globus_mutex_t                      mutex;
        
} globus_l_xio_gridftp_handle_t;

typedef struct globus_i_xio_gridftp_requestor_s
{
    globus_xio_operation_t              op;
    globus_xio_iovec_t *                iovec;
    int                                 iovec_count;
    globus_l_xio_gridftp_handle_t *     handle;
    globus_off_t                        offset;
    globus_size_t                       length;
    globus_object_t *                   saved_error;
    int                                 finished_count;

} globus_i_xio_gridftp_requestor_t;

typedef struct
{
    globus_xio_operation_t              op;
    globus_result_t                     result;

} globus_i_xio_gridftp_error_info_t;

static
int
globus_l_xio_gridftp_activate(void);

static
int
globus_l_xio_gridftp_deactivate(void);

static
globus_result_t
globus_i_xio_gridftp_register_get(
    globus_i_xio_gridftp_requestor_t *  requestor);

static
globus_result_t
globus_i_xio_gridftp_register_put(
    globus_i_xio_gridftp_requestor_t *  requestor);

static
globus_result_t
globus_i_xio_gridftp_register_read(
    globus_i_xio_gridftp_requestor_t *  requestor);

static
globus_result_t
globus_i_xio_gridftp_register_write(
    globus_i_xio_gridftp_requestor_t *  requestor);

static
void
globus_i_xio_gridftp_abort_io(
    globus_l_xio_gridftp_handle_t *     handle);

static      
globus_result_t
globus_i_xio_gridftp_set_authorization(
    globus_ftp_client_operationattr_t * attr,
    va_list                             ap);

static
globus_result_t
globus_l_xio_gridftp_attr_init(
    void **                             out_attr);

static
globus_result_t
globus_l_xio_gridftp_attr_copy(
    void **                             dst,
    void *                              src);

static
globus_result_t
globus_l_xio_gridftp_attr_destroy(
    void *                              driver_attr);

GlobusXIODefineModule(gridftp) =
{
    "globus_xio_gridftp",
    globus_l_xio_gridftp_activate,
    globus_l_xio_gridftp_deactivate,
    NULL,
    NULL,
    &local_version
};

#define GlobusXIOGridftpErrorAttr(reason)                                   \
    globus_error_put(                                                       \
        globus_error_construct_error(                                       \
            GlobusXIOMyModule(gridftp),                                     \
            NULL,                                                    \
            GLOBUS_XIO_GRIDFTP_ERROR_ATTR,                                  \
            __FILE__,                                                       \
            _xio_name,                                                      \
            __LINE__,                                                       \
            "Attr error: %s", (reason)))

#define GlobusXIOGridftpErrorSeek(reason)                                   \
    globus_error_put(                                                       \
        globus_error_construct_error(                                       \
            GlobusXIOMyModule(gridftp),                                     \
            NULL,                                                    \
            GLOBUS_XIO_GRIDFTP_ERROR_SEEK,                                  \
            __FILE__,                                                       \
            _xio_name,                                                      \
            __LINE__,                                                       \
            "Seek error: %s", (reason)))

#define GlobusXIOGridftpErrorOutstandingRead()                              \
    globus_error_put(                                                       \
        globus_error_construct_error(                                       \
            GlobusXIOMyModule(gridftp),                                     \
            NULL,                                                    \
            GLOBUS_XIO_GRIDFTP_ERROR_OUTSTANDING_READ,                      \
            __FILE__,                                                       \
            _xio_name,                                                      \
            __LINE__,                                                       \
            "Read is outstanding"))

#define GlobusXIOGridftpErrorOutstandingWrite()                             \
    globus_error_put(                                                       \
        globus_error_construct_error(                                       \
            GlobusXIOMyModule(gridftp),                                     \
            NULL,                                                    \
            GLOBUS_XIO_GRIDFTP_ERROR_OUTSTANDING_WRITE,                     \
            __FILE__,                                                       \
            _xio_name,                                                      \
            __LINE__,                                                       \
            "Write is outstanding"))

#define GlobusXIOGridftpErrorPendingRead()                                  \
    globus_error_put(                                                       \
        globus_error_construct_error(                                       \
            GlobusXIOMyModule(gridftp),                                     \
            NULL,                                                    \
            GLOBUS_XIO_GRIDFTP_ERROR_PENDING_READ,                          \
            __FILE__,                                                       \
            _xio_name,                                                      \
            __LINE__,                                                       \
            "Read pending"))

#define GlobusXIOGridftpErrorPendingWrite()                                 \
    globus_error_put(                                                       \
        globus_error_construct_error(                                       \
            GlobusXIOMyModule(gridftp),                                     \
            NULL,                                                    \
            GLOBUS_XIO_GRIDFTP_ERROR_PENDING_WRITE,                         \
            __FILE__,                                                       \
            _xio_name,                                                      \
            __LINE__,                                                       \
            "Write pending"))

#define GlobusXIOGridftpErrorOutstandingPartialXfer()                       \
    globus_error_put(                                                       \
        globus_error_construct_error(                                       \
            GlobusXIOMyModule(gridftp),                                     \
            NULL,                                                    \
            GLOBUS_XIO_GRIDFTP_ERROR_OUTSTANDING_PARTIAL_XFER,              \
            __FILE__,                                                       \
            _xio_name,                                                      \
            __LINE__,                                                       \
            "A Partial Xfer is outstanding"))


static
int
globus_l_xio_gridftp_activate(void)
{
    int rc;
    GlobusXIOName(globus_l_xio_gridftp_activate);

    GlobusDebugInit(GLOBUS_XIO_GRIDFTP, TRACE);
    GlobusXIOGridftpDebugEnter();
    rc = globus_module_activate(GLOBUS_XIO_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {
        goto error_xio_system_activate;
    }
    rc = globus_module_activate(GLOBUS_FTP_CLIENT_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {
        goto error_ftp_client_activate;
    }
    GlobusXIORegisterDriver(gridftp);
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error_ftp_client_activate:
    globus_module_deactivate(GLOBUS_XIO_MODULE);
error_xio_system_activate:
    GlobusXIOGridftpDebugExitWithError();
    GlobusDebugDestroy(GLOBUS_XIO_GRIDFTP);
    return rc;
}


static
int
globus_l_xio_gridftp_deactivate(void)
{   
    int rc;
    GlobusXIOName(globus_l_xio_gridftp_deactivate);
    
    GlobusXIOGridftpDebugEnter();
    GlobusXIOUnRegisterDriver(gridftp);
    rc = globus_module_deactivate(GLOBUS_FTP_CLIENT_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {   
        goto error_deactivate;
    }
    rc = globus_module_deactivate(GLOBUS_XIO_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {   
        goto error_deactivate;
    }
    GlobusXIOGridftpDebugExit();
    GlobusDebugDestroy(GLOBUS_XIO_GRIDFTP);
    return GLOBUS_SUCCESS;

error_deactivate:
    GlobusXIOGridftpDebugExitWithError();
    GlobusDebugDestroy(GLOBUS_XIO_GRIDFTP);
    return rc;
}


static
void
globus_i_xio_gridftp_contact_info_setup(
    globus_xio_contact_t *              dst_contact_info,
    const globus_xio_contact_t *        src_contact_info) 
{
    GlobusXIOName(globus_i_xio_gridftp_contact_info_setup);     

    GlobusXIOGridftpDebugEnter();
    /* 
     * I use globus_xio_contact_info_to_url to construct the URL to
     * pass to the client library. Since the client lib does not take the 
     * subject string in the URL, it is set to NULL
     */
    memset(dst_contact_info, 0, sizeof(globus_xio_contact_t)); 
    dst_contact_info->resource = src_contact_info->resource;
    dst_contact_info->host = src_contact_info->host;
    dst_contact_info->port = src_contact_info->port;
    dst_contact_info->scheme = src_contact_info->scheme;
    dst_contact_info->user = src_contact_info->user;
    dst_contact_info->pass = src_contact_info->pass;
    GlobusXIOGridftpDebugExit();
}


static
globus_result_t
globus_l_xio_gridftp_handle_destroy(
    globus_l_xio_gridftp_handle_t *     handle)
{
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_gridftp_handle_destroy);

    GlobusXIOGridftpDebugEnter();
    result = globus_ftp_client_handle_flush_url_state(
                                handle->ftp_handle, handle->url);
    if (result != GLOBUS_SUCCESS)
    {
        result = GlobusXIOErrorWrapFailed(
            "globus_ftp_client_handle_flush_url_state", result);
        goto error;
    }
    if (!handle->attr->ftp_handle)
    {
        result = globus_ftp_client_handle_destroy(handle->ftp_handle);
        if (result != GLOBUS_SUCCESS)
        {
            result = GlobusXIOErrorWrapFailed(
                "globus_ftp_client_handle_destroy", result);
            goto error;
        }
        globus_free(handle->ftp_handle);
    }
    result = globus_l_xio_gridftp_attr_destroy(handle->attr);
    if(result != GLOBUS_SUCCESS)
    {
        result = GlobusXIOErrorWrapFailed(
            "globus_l_xio_gridftp_attr_destroy", result);
        goto error;
    }
    globus_free(handle->url);
    globus_fifo_destroy(&handle->pending_ops_q);
    globus_memory_destroy(&handle->requestor_memory);
    globus_mutex_destroy(&handle->mutex);
    globus_free(handle);
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


/* allocate the memory for and initialize an internal handle */
static 
globus_result_t
globus_l_xio_gridftp_handle_create(
    globus_l_xio_gridftp_handle_t **    out_handle,
    globus_l_xio_gridftp_attr_t *       attr,
    const globus_xio_contact_t *        contact_info)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_result_t                     result;
    int                                 node_size;
    int                                 node_count;
    globus_xio_contact_t                contact_info_local;
    GlobusXIOName(globus_l_xio_gridftp_handle_create);

    GlobusXIOGridftpDebugEnter();
    handle = (globus_l_xio_gridftp_handle_t *)
                globus_malloc(sizeof(globus_l_xio_gridftp_handle_t));
    if (handle == NULL)
    {
        result = GlobusXIOErrorMemory("handle");
        goto error_handle;
    }
    if (attr)
    {   
        result = globus_l_xio_gridftp_attr_copy(
                        (void**)&handle->attr, (void*)attr);
    }
    else
    {
        result = globus_l_xio_gridftp_attr_init((void**)&handle->attr);
    }
    if (result != GLOBUS_SUCCESS)
    {
        result = GlobusXIOErrorWrapFailed(
                        "globus_l_xio_gridftp_attr_copy", result);
        goto error_attr;
    }
    if (handle->attr->ftp_handle == NULL)
    {
        handle->ftp_handle = (globus_ftp_client_handle_t *)
                            globus_malloc(sizeof(globus_ftp_client_handle_t));
        result = globus_ftp_client_handle_init(handle->ftp_handle, NULL);
        if (result != GLOBUS_SUCCESS)
        {
            goto error_ftp_handle_init;
        }
    }
    else
    {
        handle->ftp_handle = handle->attr->ftp_handle;
    }
    globus_i_xio_gridftp_contact_info_setup(&contact_info_local, contact_info);
    result = globus_xio_contact_info_to_url(&contact_info_local, &handle->url);
    if (result != GLOBUS_SUCCESS)
    {
        goto error_url;
    }   
    result = globus_ftp_client_handle_cache_url_state(
                                        handle->ftp_handle, handle->url);
    if (result != GLOBUS_SUCCESS)
    {
        goto error_enable_caching;
    }
    result = globus_fifo_init(&handle->pending_ops_q);
    if (result != GLOBUS_SUCCESS)
    {
        goto error_fifo_init;
    }           
    node_size = sizeof(globus_i_xio_gridftp_requestor_t);       
    node_count = GLOBUS_XIO_GRIDFTP_REQUESTOR_COUNT;      
    globus_memory_init(&handle->requestor_memory, node_size, node_count);
    globus_mutex_init(&handle->mutex, NULL);    
    handle->state = GLOBUS_XIO_GRIDFTP_NONE;
    handle->outstanding_io_count = 0;   
    handle->offset = 0; 
    handle->end_offset = -1; 
    *out_handle = handle;
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error_fifo_init:
    globus_ftp_client_handle_flush_url_state(handle->ftp_handle, handle->url);
error_enable_caching:
    globus_free(handle->url);   
error_url:
    if (!handle->attr->ftp_handle)
    {   
        globus_ftp_client_handle_destroy(handle->ftp_handle);
    }
error_ftp_handle_init:
    globus_l_xio_gridftp_attr_destroy(handle->attr);
error_attr:
    globus_free(handle);
error_handle:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
void
globus_l_xio_gridftp_cancel_cb(
    globus_xio_operation_t              op,
    void *                              user_arg,
    globus_xio_error_type_t             reason)
{
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_l_xio_gridftp_handle_t *     handle;
    globus_xio_operation_t              requestor_op = NULL;
    globus_bool_t                       reading;
    GlobusXIOName(globus_l_xio_gridftp_cancel_cb);

    GlobusXIOGridftpDebugEnter();       
    requestor = (globus_i_xio_gridftp_requestor_t *) user_arg;  
    handle = requestor->handle;
    /* no need to finish read or write here, it is done in read/write cb */
    globus_mutex_lock(&handle->mutex);
    switch (handle->state)
    {
        case GLOBUS_XIO_GRIDFTP_NONE:
            break;
        case GLOBUS_XIO_GRIDFTP_OPENING:
            globus_ftp_client_abort(handle->ftp_handle);
            break;
        case GLOBUS_XIO_GRIDFTP_IO_PENDING:
            handle->state = GLOBUS_XIO_GRIDFTP_ABORT_PENDING;
            globus_i_xio_gridftp_abort_io(handle);
            break;
        case GLOBUS_XIO_GRIDFTP_IO_DONE:
            break;
        case GLOBUS_XIO_GRIDFTP_ABORT_PENDING:
            break;
        case GLOBUS_XIO_GRIDFTP_ABORT_PENDING_IO_PENDING:
            requestor = globus_fifo_remove(&handle->pending_ops_q, requestor);
            if (requestor != NULL)
            {
                requestor_op = requestor->op;
                reading = handle->pending_ops_direction;
                globus_memory_push_node(
                    &handle->requestor_memory, (void*)requestor);
            }
            if (globus_fifo_empty(&handle->pending_ops_q))
            {
                handle->state = GLOBUS_XIO_GRIDFTP_ABORT_PENDING;
            }   
            break;
        default:
            /* if it gets here, something is wrong */
            globus_assert(0 && "Unexpected state in cancel callback");  
    }  
    globus_mutex_unlock(&handle->mutex);
    if (requestor_op)
    {
        if (reading)
        {
            globus_xio_driver_finished_read(requestor_op,  
                GlobusXIOErrorCanceled(), 0);
        }
        else
        {
            globus_xio_driver_finished_write(requestor_op,  
                GlobusXIOErrorCanceled(), 0);
        }
    }
    GlobusXIOGridftpDebugExit();        
}


static
void
globus_l_xio_gridftp_open_cb(
    void *                              user_arg,
    globus_ftp_client_handle_t *        ftp_handle,
    globus_object_t *                   error)
{
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_l_xio_gridftp_handle_t *     handle;
    globus_xio_operation_t              requestor_op;
    GlobusXIOName(globus_l_xio_gridftp_open_cb);

    GlobusXIOGridftpDebugEnter();
    requestor = (globus_i_xio_gridftp_requestor_t *) user_arg;
    globus_xio_operation_disable_cancel(requestor->op);
    handle = requestor->handle; 
    requestor_op = requestor->op;
    globus_memory_push_node(&handle->requestor_memory, (void*)requestor);
    /* 
     * error code 550 (file not found) is considered success. If the handle is
     * opened for writing, the file may not be available now and will be 
     * created only the transfer is initiated
     */
    if (error != GLOBUS_SUCCESS &&
        globus_error_ftp_error_get_code(error) != 550)
    {
        globus_result_t result;
        result = GlobusXIOErrorWrapFailed("globus_ftp_client_size", 
                                globus_error_put(globus_object_copy(error)));
        globus_l_xio_gridftp_handle_destroy(handle);
        globus_xio_driver_finished_open(NULL, requestor_op, result);
    }
    else
    {
        globus_mutex_lock(&handle->mutex);
	if (handle->attr->append && error == GLOBUS_SUCCESS) 
	{
	    handle->offset = handle->size;
	}
        handle->state = GLOBUS_XIO_GRIDFTP_OPEN;
        globus_mutex_unlock(&handle->mutex);
        globus_xio_driver_finished_open(
                handle, requestor_op, GLOBUS_SUCCESS);
    }           
    GlobusXIOGridftpDebugExit();
}


static
globus_result_t
globus_l_xio_gridftp_open(
    const globus_xio_contact_t *        contact_info,
    void *                              driver_link,
    void *                              driver_attr,
    globus_xio_operation_t              op)
{
    globus_l_xio_gridftp_handle_t *     handle = NULL;
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_result_t                     result = GLOBUS_SUCCESS;
    globus_result_t                     res;
    GlobusXIOName(globus_l_xio_gridftp_open);

    GlobusXIOGridftpDebugEnter();
    globus_assert(driver_link == NULL);
    if (!contact_info->resource || !contact_info->host || 
        !contact_info->scheme)
    {
        result = GlobusXIOErrorParameter("contact_string");
        goto error_contact_info;
    }
    result = globus_l_xio_gridftp_handle_create(
                &handle, 
                (globus_l_xio_gridftp_attr_t *) driver_attr, 
                contact_info);
    if (result != GLOBUS_SUCCESS)
    {
        goto error_handle;
    }
    if (contact_info->subject || contact_info->user || contact_info->pass)
    {
        /* 
         * if user and/or pass present in contact_info, it would have been
         * put into the URL (client lib allows user and pass be to present
         * in the URL but not subject)
         */
        result = globus_ftp_client_operationattr_set_authorization(
            &handle->attr->ftp_operation_attr,
            GSS_C_NO_CREDENTIAL,
            contact_info->user,
            contact_info->pass,
            NULL,                /* acct */
            contact_info->subject);
        if (result != GLOBUS_SUCCESS)
        {
            goto error_auth;
        }
    }
    requestor = (globus_i_xio_gridftp_requestor_t *)
                        globus_memory_pop_node(&handle->requestor_memory);
    requestor->op = op;
    requestor->handle = handle;
    if (globus_xio_operation_enable_cancel(
        op, globus_l_xio_gridftp_cancel_cb, requestor))
    {
        result = GlobusXIOErrorCanceled();
        goto error_cancel_enable;
    }
    globus_mutex_lock(&handle->mutex);  
    if (globus_xio_operation_is_canceled(op))
    {
        result = GlobusXIOErrorCanceled();
        goto error_operation_canceled;
    }   
    result = globus_ftp_client_size(
        handle->ftp_handle, 
        handle->url, 
        &handle->attr->ftp_operation_attr, 
        &handle->size,
        globus_l_xio_gridftp_open_cb, 
        requestor);
    if (result != GLOBUS_SUCCESS)       
    {
        goto error_size;
    }
    handle->state = GLOBUS_XIO_GRIDFTP_OPENING;
    globus_mutex_unlock(&handle->mutex);        
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error_size:
error_operation_canceled:
    globus_mutex_unlock(&handle->mutex);
    globus_xio_operation_disable_cancel(op);
error_cancel_enable:
    /* 
     * XIO calls the cancel_cb with cancel lock held and disable_cancel waits 
     * for that. So cancel_cb can not be active after I call disable_cancel
     * and thus I can safely push the requestor memory and call handle_destroy
     */ 
    globus_memory_push_node(&handle->requestor_memory, (void*)requestor);
error_auth:
    res = globus_l_xio_gridftp_handle_destroy(handle);  
    globus_assert (res == GLOBUS_SUCCESS);      
error_handle:
error_contact_info:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


/* called locked */
static
globus_result_t
globus_l_xio_gridftp_process_pending_ops(
    globus_l_xio_gridftp_handle_t *     handle,
    globus_list_t **                    error_list)
{
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_result_t                     result;
    globus_i_xio_gridftp_error_info_t * error_info;
    globus_bool_t                       reading;
    GlobusXIOName(globus_l_xio_gridftp_process_pending_ops);

    GlobusXIOGridftpDebugEnter();
    handle->state = GLOBUS_XIO_GRIDFTP_OPEN;
    globus_assert(!globus_fifo_empty(&handle->pending_ops_q));
    requestor = (globus_i_xio_gridftp_requestor_t*)
                      globus_fifo_peek(&handle->pending_ops_q);
    reading = handle->pending_ops_direction;
    if (reading)
    {
        result = globus_i_xio_gridftp_register_get(requestor);
    }
    else
    {
        result = globus_i_xio_gridftp_register_put(requestor);
    }
    if (result != GLOBUS_SUCCESS)
    {
        do
        {
            requestor = (globus_i_xio_gridftp_requestor_t*)
                          globus_fifo_dequeue(&handle->pending_ops_q);
            error_info = (globus_i_xio_gridftp_error_info_t *)
                    globus_malloc(sizeof(globus_i_xio_gridftp_error_info_t));
            error_info->op = requestor->op;
            /* 
             * As I don't use this across cbs, I just store result rather than 
             * error object
             */
            error_info->result = result;
            globus_list_insert(error_list, error_info);
            globus_memory_push_node(
                            &handle->requestor_memory, (void*)requestor);
        } while (!globus_fifo_empty(&handle->pending_ops_q));
        goto error;
    }
    do
    {
        requestor = (globus_i_xio_gridftp_requestor_t*)
                          globus_fifo_dequeue(&handle->pending_ops_q);
        if (reading)
        {
            result = globus_i_xio_gridftp_register_read(requestor);
        }
        else
        {
            result = globus_i_xio_gridftp_register_write(requestor);
        }
        if (result != GLOBUS_SUCCESS)
        {
            error_info = (globus_i_xio_gridftp_error_info_t *)
                globus_malloc(sizeof(globus_i_xio_gridftp_error_info_t));
            error_info->op = requestor->op;
            /* 
             * As I don't use this across cbs, I just store result rather than 
             * error object
             */
            error_info->result = result;
            globus_list_insert(error_list, error_info);
            globus_memory_push_node(
                            &handle->requestor_memory, (void*)requestor);
        }    
        else
        {
            ++handle->outstanding_io_count;
        }
    } while (!globus_fifo_empty(&handle->pending_ops_q));
    if (handle->outstanding_io_count > 0)
    {   
        handle->state = GLOBUS_XIO_GRIDFTP_IO_PENDING;
    }
    if (!globus_list_empty(*error_list))
    {
        /* 
         * The result that I return from this function is not really used in
         * reporting. I use the error information stored in the error_list
         * for reporting it to the user.
         */
        result = error_info->result;
        goto error;
    }
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


/* called locked */
static
globus_result_t
globus_l_xio_gridftp_change_state(
    globus_l_xio_gridftp_handle_t *     handle,
    globus_bool_t *                     close,
    globus_list_t **                    error_list)
{
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_gridftp_change_state);

    GlobusXIOGridftpDebugEnter();
    *close = GLOBUS_FALSE;
    switch (handle->state)
    {
        case GLOBUS_XIO_GRIDFTP_IO_PENDING:             
            if (handle->outstanding_io_count == 0)
            {
                handle->state = GLOBUS_XIO_GRIDFTP_IO_DONE;
            }
            /* fall through */
        case GLOBUS_XIO_GRIDFTP_IO_DONE:
            if (handle->xfer_done == GLOBUS_TRUE)
            {
                handle->state = GLOBUS_XIO_GRIDFTP_OPEN;
            }       
            break;
        case GLOBUS_XIO_GRIDFTP_ABORT_PENDING:
            if ((handle->outstanding_io_count == 0) && 
                (handle->xfer_done == GLOBUS_TRUE))
            {
                handle->state = GLOBUS_XIO_GRIDFTP_OPEN;
            }
            break;
        case GLOBUS_XIO_GRIDFTP_ABORT_PENDING_IO_PENDING:
        {
            if ((handle->outstanding_io_count == 0) && 
                (handle->xfer_done == GLOBUS_TRUE))
            {
                result = globus_l_xio_gridftp_process_pending_ops(
                                                        handle, error_list);
                if (result != GLOBUS_SUCCESS)
                {
                    goto error;
                }
            }           
            break;
        }
        case GLOBUS_XIO_GRIDFTP_ABORT_PENDING_CLOSING:
            if ((handle->outstanding_io_count == 0) && 
                (handle->xfer_done == GLOBUS_TRUE))
            {
                handle->state = GLOBUS_XIO_GRIDFTP_NONE;
                *close = GLOBUS_TRUE;
            }   
            break;
        default:
            /* if it gets here, something is wrong */
            globus_assert(0 && "Unexpected state in gridftp_change_state");
    }   

    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
void
globus_i_xio_gridftp_finish_failed_ops(
    globus_list_t **                    error_list,
    globus_bool_t                       reading)
{
    globus_i_xio_gridftp_error_info_t * error_info;
    GlobusXIOName(globus_i_xio_gridftp_finish_failed_ops);
    GlobusXIOGridftpDebugEnter();
    do 
    {   
        error_info = (globus_i_xio_gridftp_error_info_t *)
                                globus_list_remove(error_list, *error_list);
        if (reading)
        {
            globus_xio_driver_finished_read(
                                error_info->op, error_info->result, 0);
        }
        else
        {    
            globus_xio_driver_finished_write(
                                error_info->op, error_info->result, 0);
        }
        globus_free(error_info);
    } while (!globus_list_empty(*error_list));
    GlobusXIOGridftpDebugExit();
}


static
void
globus_l_xio_gridftp_xfer_cb(
    void *                              user_arg,
    globus_ftp_client_handle_t *        ftp_handle,
    globus_object_t *                   error)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_bool_t                       close = GLOBUS_FALSE;  
    globus_list_t *                     error_list = NULL;
    globus_xio_operation_t              requestor_op = NULL;
    globus_bool_t                       reading;
    globus_size_t                       len = 0;    
    globus_off_t                        offset;
    globus_result_t                     result;        
    GlobusXIOName(globus_l_xio_gridftp_xfer_cb);

    GlobusXIOGridftpDebugEnter();
    /*
     * Here I need to finish read/write if partial_xfer is enabled or if 
     * ftp_client_register_read/write of pending read/write failed while in 
     * ABORT_PENDING_IO_PENDING state. An error returned by 
     * globus_l_xio_gridftp_change_state() indicates pending read/write failed
     */
    handle = (globus_l_xio_gridftp_handle_t *) user_arg;
    globus_mutex_lock(&handle->mutex);
    handle->xfer_done = GLOBUS_TRUE;
    result = globus_l_xio_gridftp_change_state(handle, &close, &error_list);
    if (result != GLOBUS_SUCCESS)
    {
        reading = handle->pending_ops_direction;
        globus_mutex_unlock(&handle->mutex);
        goto error;
    }
    if (close == GLOBUS_TRUE)
    {
        globus_xio_operation_t close_op;
        /*
         * If close was called in 'ABORT_PENDING' state, a close requestor 
         * would have been enqueued in the pending_ops_q
         */
        requestor = (globus_i_xio_gridftp_requestor_t*)
                        globus_fifo_dequeue(&handle->pending_ops_q);
        close_op = requestor->op;
        globus_memory_push_node(
                    &handle->requestor_memory, (void*)requestor);
        globus_mutex_unlock(&handle->mutex);
        result = globus_l_xio_gridftp_handle_destroy(handle);
        globus_assert(result == GLOBUS_SUCCESS);
        globus_xio_driver_finished_close(close_op, result);
    }           
    else if (handle->attr->partial_xfer && 
             handle->state == GLOBUS_XIO_GRIDFTP_OPEN)
    {
        requestor = handle->partial_requestor;
        requestor_op = requestor->op;
        reading = handle->outstanding_ops_direction;
        offset = requestor->offset;
        len = requestor->length;
        if (error == GLOBUS_SUCCESS)      
        {
            result = globus_xio_driver_data_descriptor_cntl(
                        requestor_op,
                        NULL,
                        GLOBUS_XIO_DD_SET_OFFSET,
                        offset);
        }
        else
        {
            result = GlobusXIOErrorWrapFailed("globus_ftp_client_io", 
                            globus_error_put(globus_object_copy(error)));
        }
        if (result == GLOBUS_SUCCESS && 
                            requestor->saved_error != NULL)
        {
            result = globus_error_put(requestor->saved_error);   
        }
        globus_memory_push_node(
                            &handle->requestor_memory, (void*)requestor);
        globus_mutex_unlock(&handle->mutex);
    }
    if (requestor_op)
    {
        if (reading)
        { 
            globus_xio_driver_finished_read(requestor_op, result, len);
        }
        else
        {
            globus_xio_driver_finished_write(requestor_op, result, len);
        }
    }
    GlobusXIOGridftpDebugExit();
    return;

error:
    globus_i_xio_gridftp_finish_failed_ops(&error_list, reading);
    GlobusXIOGridftpDebugExitWithError();
    return;
}


static
void
globus_l_xio_gridftp_write_cb(
    void *                              user_arg,
    globus_ftp_client_handle_t *        ftp_handle,
    globus_object_t *                   error,
    globus_byte_t *                     buffer,
    globus_size_t                       length,
    globus_off_t                        offset,
    globus_bool_t                       eof)
{
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_l_xio_gridftp_handle_t *     handle;
    globus_xio_operation_t              requestor_op;
    globus_list_t *                     error_list = NULL;
    globus_off_t                        requestor_offset;
    globus_size_t                       requestor_length;
    globus_result_t                     requestor_result = GLOBUS_SUCCESS;
    globus_result_t                     result;
    globus_bool_t                       finish = GLOBUS_TRUE;
    globus_bool_t                       finish_pending_op = GLOBUS_FALSE;
    globus_bool_t                       close = GLOBUS_FALSE;
    globus_bool_t                       reading;
    GlobusXIOName(globus_l_xio_gridftp_write_cb);

    GlobusXIOGridftpDebugEnter();
    requestor = (globus_i_xio_gridftp_requestor_t *) user_arg;
    handle = requestor->handle;
    globus_mutex_lock(&handle->mutex);
    if (error != GLOBUS_SUCCESS && requestor->saved_error == NULL)
    {
        requestor->saved_error = globus_object_copy(error);
    }
    if (--requestor->finished_count == 0)
    {
        requestor_op = requestor->op;
        globus_mutex_unlock(&handle->mutex); 
        /* 
         * Unlock the mutex here because I can't call disable_cancel with lock
         * held (lock inversion issues)
         */
        globus_xio_operation_disable_cancel(requestor_op);
        globus_mutex_lock(&handle->mutex);
        handle->outstanding_io_count--;
        result = globus_l_xio_gridftp_change_state(
                                        handle, &close, &error_list);
        /* XIO wouldn't call close while there is an outstanding operation */
        globus_assert(close == GLOBUS_FALSE);
        if (result != GLOBUS_SUCCESS)
        {
            finish_pending_op = GLOBUS_TRUE;
            reading = handle->pending_ops_direction;
        }
        /* 
         * The offset returned in the cb is not used because we might register 
         * multiple ftp_client_writes for a single user write
         */
        requestor_offset = requestor->offset;
        requestor_length = requestor->length;
        if (requestor->saved_error != NULL)
        {
            requestor_result = globus_error_put(requestor->saved_error);
        }
        if (handle->attr->partial_xfer)
        {
            if (handle->state == GLOBUS_XIO_GRIDFTP_OPEN)
            {
                globus_memory_push_node(
                                &handle->requestor_memory, (void*)requestor);
            }
            else
            {
                /* 
                 * put is not done yet. Partial transfers (where each 
                 * read/write is associated with a get/put) are not yet 
                 * finished until get/put is also done 
                 */
                finish = GLOBUS_FALSE;
            }
        }
        else
        {
            globus_memory_push_node(
                                &handle->requestor_memory, (void*)requestor);
        }
    }
    globus_mutex_unlock(&handle->mutex);
    if (finish)
    {
        if (requestor_result == GLOBUS_SUCCESS)
        {
            requestor_result = globus_xio_driver_data_descriptor_cntl(
                                    requestor_op,
                                    NULL,
                                    GLOBUS_XIO_DD_SET_OFFSET,
                                    requestor_offset);
        }
        globus_xio_driver_finished_write(
                        requestor_op, requestor_result, requestor_length);
    }
    if (finish_pending_op)
    {
        globus_i_xio_gridftp_finish_failed_ops(&error_list, reading);
    }
    GlobusXIOGridftpDebugExit();
}


static
void
globus_l_xio_gridftp_read_cb(
    void *                              user_arg,
    globus_ftp_client_handle_t *        ftp_handle, 
    globus_object_t *                   error, 
    globus_byte_t *                     buffer,
    globus_size_t                       length,
    globus_off_t                        offset,
    globus_bool_t                       eof)
{
    globus_result_t                     requestor_result = GLOBUS_SUCCESS;
    globus_result_t                     result;
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_l_xio_gridftp_handle_t *     handle;
    globus_bool_t                       close = GLOBUS_FALSE;
    globus_bool_t                       finish = GLOBUS_TRUE;
    globus_bool_t                       finish_pending_op = GLOBUS_FALSE;
    globus_bool_t                       reading;
    globus_xio_operation_t              requestor_op;
    globus_list_t *                     error_list = NULL;
    GlobusXIOName(globus_l_xio_gridftp_read_cb);

    GlobusXIOGridftpDebugEnter();
    requestor = (globus_i_xio_gridftp_requestor_t *) user_arg;
    handle = requestor->handle; 
    requestor_op = requestor->op;
    globus_xio_operation_disable_cancel(requestor_op);
    globus_mutex_lock(&handle->mutex);
    handle->outstanding_io_count--;
    result = globus_l_xio_gridftp_change_state(handle, &close, &error_list);
    /* XIO wouldn't call close while there is an outstanding operation */
    globus_assert(close == GLOBUS_FALSE);
    if (result != GLOBUS_SUCCESS)
    {
        finish_pending_op = GLOBUS_TRUE;
        reading = handle->pending_ops_direction;
    }
    if (error == GLOBUS_SUCCESS)
    {
        if (offset + length > handle->offset)
        {
            handle->offset = offset + length;
        }
        /* 
         * For the partial reads per buffer, eof returned in this cb 
         * will always be TRUE. I do xio_driver_set_eof_received only when 
         * (eof == TRUE && length < partial_xfer_len. I assume offset 
         * returned in this cb will be same as the one I set in the get/put
         * (thats why I use handle->end_offset - offset to compute the
         * partial_xfer_len)
         */
        if (handle->attr->partial_xfer && eof && 
                    length == handle->end_offset - offset)
        {
            eof = GLOBUS_FALSE;
        }
    }
    else
    {
        requestor_result = GlobusXIOErrorWrapFailed("globus_ftp_client_io",  
                            globus_error_put(globus_object_copy(error)));
    }
    if (handle->attr->partial_xfer)
    {   
        if (handle->state == GLOBUS_XIO_GRIDFTP_OPEN)
        {
            globus_memory_push_node(
                        &handle->requestor_memory, (void*)requestor);
        }
        else
        {
            finish = GLOBUS_FALSE;
            if (eof && requestor_result == GLOBUS_SUCCESS)
            {
                globus_xio_driver_set_eof_received(requestor_op);
                requestor_result = GlobusXIOErrorEOF();
            }
            /* handle->partial_requestor points to this requestor */
            requestor->saved_error = globus_error_get(requestor_result);
            /* read will be finished in xfer_cb, so we need to store offset */
            requestor->offset = offset;
            requestor->length = length;
        }
    }
    else
    {
        globus_memory_push_node(&handle->requestor_memory, (void*)requestor);
    }
    globus_mutex_unlock(&handle->mutex);
    if (finish)
    {
        if (error == GLOBUS_SUCCESS)
        {
            requestor_result = globus_xio_driver_data_descriptor_cntl(
                        requestor_op, NULL, GLOBUS_XIO_DD_SET_OFFSET, offset);
            if (eof && requestor_result == GLOBUS_SUCCESS)
            {
                globus_xio_driver_set_eof_received(requestor_op);
                requestor_result = GlobusXIOErrorEOF();
            }
        }
        globus_xio_driver_finished_read(
                                requestor_op, requestor_result, length);
    }   
    if (finish_pending_op)
    {
        globus_i_xio_gridftp_finish_failed_ops(&error_list, reading);
    }
    GlobusXIOGridftpDebugExit();
    return;
}


static
void
globus_l_xio_gridftp_write_eof_cb(
    void *                              user_arg,
    globus_ftp_client_handle_t *        ftp_handle,
    globus_object_t *                   error,
    globus_byte_t *                     buffer,
    globus_size_t                       length,
    globus_off_t                        offset,
    globus_bool_t                       eof)
{
    GlobusXIOName(globus_l_xio_gridftp_write_eof_cb);
    GlobusXIOGridftpDebugEnter();       
    GlobusXIOGridftpDebugExit();        
}


/* called locked */
static
globus_result_t
globus_i_xio_gridftp_register_get(
    globus_i_xio_gridftp_requestor_t *  requestor)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_result_t                     result;
    GlobusXIOName(globus_i_xio_gridftp_register_get);

    GlobusXIOGridftpDebugEnter();
    handle = requestor->handle;
    handle->outstanding_ops_direction = GLOBUS_TRUE;  
    if (handle->attr->partial_xfer)
    {
        handle->partial_requestor = requestor;
        /* 
         * for reads, user cant specify an offset via dd, so there is no need
         * for requestor->offset here
         */
        handle->end_offset = handle->offset + requestor->iovec[0].iov_len;
    }
    /* if partial xfer is not enabled, handle->end_offset will be -1(eof) */
    handle->xfer_done = GLOBUS_FALSE;
    /* 
     * handle is passed as user_arg to get/put and not requestor because
     * requestor is associated with individual reads/writes; whereas a get/put
     * might span multiple reads/writes
     */
    if (handle->offset > 0 || handle->attr->partial_xfer)
    {
        result = globus_ftp_client_partial_get(
            handle->ftp_handle,
            handle->url,
            &handle->attr->ftp_operation_attr,
            NULL,        /* restart marker */
            handle->offset,
            handle->end_offset,
            globus_l_xio_gridftp_xfer_cb,
            handle);
    }
    else
    {
	if (handle->attr->eret_alg_str)
	{
	    result = globus_ftp_client_extended_get(
		handle->ftp_handle,
		handle->url,
		&handle->attr->ftp_operation_attr,
		NULL,        /* restart_marker */
		(const char *) handle->attr->eret_alg_str,
		globus_l_xio_gridftp_xfer_cb,
		handle);
	}
	else
	{
	    result = globus_ftp_client_get(
		handle->ftp_handle,
		handle->url,
		&handle->attr->ftp_operation_attr,
		NULL,        /* restart_marker */
		globus_l_xio_gridftp_xfer_cb,
		handle);
	}
    }   
    if (result != GLOBUS_SUCCESS)
    {
        goto error;
    }
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


/* called locked */
static
globus_result_t
globus_i_xio_gridftp_register_read(
    globus_i_xio_gridftp_requestor_t *  requestor)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_result_t                     result;
    GlobusXIOName(globus_i_xio_gridftp_register_read);

    GlobusXIOGridftpDebugEnter();
    handle = requestor->handle;
    /* simultaneous read and write not allowed */       
    if (handle->outstanding_ops_direction == GLOBUS_FALSE)
    {
        result = GlobusXIOGridftpErrorOutstandingWrite();
        goto error;
    }           
    result = globus_ftp_client_register_read(
        handle->ftp_handle,
        requestor->iovec[0].iov_base,
        requestor->iovec[0].iov_len,
        globus_l_xio_gridftp_read_cb,
        requestor);
    if (result != GLOBUS_SUCCESS)
    {
        goto error;
    }
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
globus_result_t
globus_l_xio_gridftp_read(
    void *                              driver_specific_handle,
    const globus_xio_iovec_t*           iovec,
    int                                 iovec_count,
    globus_xio_operation_t              op)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_result_t                     result;
    globus_size_t                       wait_for;
    GlobusXIOName(globus_l_xio_gridftp_read);

    GlobusXIOGridftpDebugEnter();
    wait_for = globus_xio_operation_get_wait_for(op);
    if (wait_for != 1)
    {
        result = GlobusXIOErrorParameter("Waitforbytes");
        goto error_wait_for;
    }
    handle = (globus_l_xio_gridftp_handle_t *) driver_specific_handle;
    requestor = (globus_i_xio_gridftp_requestor_t *)
                globus_memory_pop_node(&handle->requestor_memory);
    requestor->op = op;
    requestor->handle = handle;
    requestor->iovec = (globus_xio_iovec_t*)iovec;
    if (globus_xio_operation_enable_cancel(
        op, globus_l_xio_gridftp_cancel_cb, requestor))
    {
        result = GlobusXIOErrorCanceled();
        goto error_cancel_enable;
    }
    /* 
     * I can't call enable_cancel with lock being held because of lock
     * inversion issues 
     */
    globus_mutex_lock(&handle->mutex);
    if (globus_xio_operation_is_canceled(op))
    {
        result = GlobusXIOErrorCanceled();
        goto error_operation_canceled;
    }   
    if (handle->attr->partial_xfer && handle->state != GLOBUS_XIO_GRIDFTP_OPEN)
    {
        result = GlobusXIOGridftpErrorOutstandingPartialXfer();
        goto error_outstanding_partial_xfer;
    }
    if (globus_xio_driver_eof_received(op))
    {
        result = GlobusXIOErrorEOF();
	globus_mutex_unlock(&handle->mutex);
	globus_xio_operation_disable_cancel(op);
	globus_memory_push_node(&handle->requestor_memory, (void*)requestor);
	globus_xio_driver_finished_read(op, result, 0);  
    }
    else
    {
	switch (handle->state)
	{
	    case GLOBUS_XIO_GRIDFTP_OPEN:           
		result = globus_i_xio_gridftp_register_get(requestor);
		if (result != GLOBUS_SUCCESS)
		{   
		    goto error_get;
		}   
		/* fall through */  
	    case GLOBUS_XIO_GRIDFTP_IO_DONE:
		/* fall through */
	    case GLOBUS_XIO_GRIDFTP_IO_PENDING:
		result = globus_i_xio_gridftp_register_read(requestor);
		if (result != GLOBUS_SUCCESS)
		{
		    goto error_register_read;
		}
		++handle->outstanding_io_count;                     
		handle->state = GLOBUS_XIO_GRIDFTP_IO_PENDING;
		break;
	    case GLOBUS_XIO_GRIDFTP_ABORT_PENDING:
		handle->pending_ops_direction = GLOBUS_TRUE;        
		handle->state = GLOBUS_XIO_GRIDFTP_ABORT_PENDING_IO_PENDING;
		/* fall through */  
	    case GLOBUS_XIO_GRIDFTP_ABORT_PENDING_IO_PENDING:
	    {
		/* simultaneous read and write not allowed */       
		if (handle->pending_ops_direction == GLOBUS_FALSE)
		{
		    result = GlobusXIOGridftpErrorPendingWrite();
		    goto error_pending_write;
		}           
		globus_fifo_enqueue(&handle->pending_ops_q, requestor);
		break;
	    }
	    default:
		/* if it gets here, something is wrong */
		globus_assert(0 && "Unexpected state in read");
	}
	globus_mutex_unlock(&handle->mutex);
    }
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error_pending_write:
error_register_read:
error_get:
error_outstanding_partial_xfer:
error_operation_canceled:
    globus_mutex_unlock(&handle->mutex);
    globus_xio_operation_disable_cancel(op);
error_cancel_enable:
    globus_memory_push_node(&handle->requestor_memory, (void*)requestor);
error_wait_for:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


/* called locked */
static
globus_result_t
globus_i_xio_gridftp_register_put(
    globus_i_xio_gridftp_requestor_t *  requestor)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_result_t                     result;
    GlobusXIOName(globus_i_xio_gridftp_register_put);

    GlobusXIOGridftpDebugEnter();
    handle = requestor->handle;
    handle->outstanding_ops_direction = GLOBUS_FALSE;
    if (handle->attr->partial_xfer)
    {
        handle->partial_requestor = requestor;
        handle->end_offset = requestor->offset + requestor->length;
    }
    /* if partial xfer is not enabled, handle->end_offset will be -1(eof) */
    handle->xfer_done = GLOBUS_FALSE;
    /* 
     * handle is passed as user_arg to get/put and not requestor because
     * requestor is associated with individual reads/writes; whereas a get/put
     * might span multiple reads/writes
     */
    if (requestor->offset > 0 || handle->attr->partial_xfer)
    {
        result = globus_ftp_client_partial_put(
            handle->ftp_handle,
            handle->url,
            &handle->attr->ftp_operation_attr,
            NULL,
            requestor->offset,
            handle->end_offset,
            globus_l_xio_gridftp_xfer_cb,
            handle);
    }
    else
    {
	if (handle->attr->esto_alg_str)
	{
	    result = globus_ftp_client_extended_put(
		handle->ftp_handle,
		handle->url,
		&handle->attr->ftp_operation_attr,
		NULL,
		(const char *) handle->attr->esto_alg_str,
		globus_l_xio_gridftp_xfer_cb,
		handle);
	}
	else
	{
	    result = globus_ftp_client_put(
		handle->ftp_handle,
		handle->url,
		&handle->attr->ftp_operation_attr,
		NULL,
		globus_l_xio_gridftp_xfer_cb,
		handle);
	}
    }
    if (result != GLOBUS_SUCCESS)
    {
        goto error;
    }
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


/* called locked */
static
globus_result_t
globus_i_xio_gridftp_register_write(
    globus_i_xio_gridftp_requestor_t *  requestor)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_xio_iovec_t *                iovec;
    globus_result_t                     result;
    globus_bool_t                       eof = GLOBUS_FALSE;
    globus_off_t                        offset;
    int                                 i;
    GlobusXIOName(globus_i_xio_gridftp_register_write);

    GlobusXIOGridftpDebugEnter();
    handle = requestor->handle;
    /* simultaneous read and write not allowed */       
    if (handle->outstanding_ops_direction == GLOBUS_TRUE)
    {
        result = GlobusXIOGridftpErrorOutstandingRead();
        goto error;
    }
    /* This offset is either handle->offset or specified by user via dd */
    offset = requestor->offset;
    iovec = requestor->iovec;
    if (handle->attr->partial_xfer)
    {
        eof = GLOBUS_TRUE;
    }
    for (i = 0; i < requestor->iovec_count; i++)
    {    
        result = globus_ftp_client_register_write(
            handle->ftp_handle,     
            iovec[i].iov_base,
            iovec[i].iov_len,
            offset,
            eof, 
            globus_l_xio_gridftp_write_cb,
            requestor);
        if (result != GLOBUS_SUCCESS)
        {
            if (requestor->finished_count == 0)
            {
                goto error;
            }
            else if (requestor->saved_error == NULL)
            {
                requestor->saved_error = globus_error_get(result);
            }
        }
        ++requestor->finished_count;
        offset = offset + iovec[i].iov_len;
    }
    /* 
     * I do not modify handle->offset if globus_ftp_client_register_write
     * fails because the failure indicates that something is wrong anyways
     */
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
globus_result_t
globus_l_xio_gridftp_write(
    void *                              driver_specific_handle,
    const globus_xio_iovec_t *          iovec,
    int                                 iovec_count,
    globus_xio_operation_t              op)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_i_xio_gridftp_requestor_t *  requestor;
    globus_result_t                     result;
    globus_off_t                        offset;
    int					i;
    GlobusXIOName(globus_l_xio_gridftp_write);

    GlobusXIOGridftpDebugEnter();
    handle = (globus_l_xio_gridftp_handle_t *) driver_specific_handle;
    requestor = (globus_i_xio_gridftp_requestor_t *)
                globus_memory_pop_node(&handle->requestor_memory);
    requestor->op = op;
    requestor->handle = handle;
    requestor->iovec = (globus_xio_iovec_t*)iovec;
    if (globus_xio_operation_enable_cancel(
        op, globus_l_xio_gridftp_cancel_cb, requestor))
    {
        result = GlobusXIOErrorCanceled();
        goto error_cancel_enable;
    }
    /* 
     * I can't call enable_cancel with lock being held because of lock
     * inversion issues 
     */
    globus_mutex_lock(&handle->mutex);
    if (globus_xio_operation_is_canceled(op))
    {
        result = GlobusXIOErrorCanceled();
        goto error_operation_canceled;
    }   
    if (handle->attr->partial_xfer && 
        handle->state != GLOBUS_XIO_GRIDFTP_OPEN)
    {
        result = GlobusXIOGridftpErrorOutstandingPartialXfer();
        goto error_outstanding_partial_xfer;
    }
    result = globus_xio_driver_data_descriptor_cntl(
                op,
                NULL,
                GLOBUS_XIO_DD_GET_OFFSET,
                &offset);
    /* 
     * If offset is not specified, dd_cntl will return offset = -1.
     * In that case offset is set to handle->offset (whose initial value
     * is zero). Basically, the file will be overwritten from the start
     * if offset is not specified.
     */ 
    if (result != GLOBUS_SUCCESS || offset == -1)
    {
        offset = handle->offset;
    }
    requestor->offset = offset;
    GlobusXIOUtilIovTotalLength(requestor->length, iovec, iovec_count);
    requestor->finished_count = 0;
    requestor->iovec_count = iovec_count;
    requestor->saved_error = NULL;
    for (i = 0; i < iovec_count; i++)
    {
	offset += iovec[i].iov_len;
    }
    /*
     * The updated offset might be less than handle->offset if user pass an
     * offset (via dd) that is less than handle->offset - length of buffer.
     * I update the handle->offset here so that it gets updated for the 
     * requests that we put in the pending q as well.
     */
    if (offset > handle->offset)
    {
        handle->offset = offset;
    }
    switch (handle->state)
    {   
        case GLOBUS_XIO_GRIDFTP_OPEN:
            result = globus_i_xio_gridftp_register_put(requestor);
            if (result != GLOBUS_SUCCESS)
            {   
                goto error_put;
            }
            /* fall through */  
        case GLOBUS_XIO_GRIDFTP_IO_DONE:
            /* fall through */
        case GLOBUS_XIO_GRIDFTP_IO_PENDING:
        {
            result = globus_i_xio_gridftp_register_write(requestor);
            if (result != GLOBUS_SUCCESS)
            {
                goto error_register_write;
            }
            ++handle->outstanding_io_count;                     
            handle->state = GLOBUS_XIO_GRIDFTP_IO_PENDING;
            break;
        }
        case GLOBUS_XIO_GRIDFTP_ABORT_PENDING:
            handle->pending_ops_direction = GLOBUS_FALSE;
            handle->state = GLOBUS_XIO_GRIDFTP_ABORT_PENDING_IO_PENDING;
            /* fall through */  
        case GLOBUS_XIO_GRIDFTP_ABORT_PENDING_IO_PENDING:
        {
            /* simultaneous read and write not allowed */       
            if (handle->pending_ops_direction == GLOBUS_TRUE)
            {
                result = GlobusXIOGridftpErrorPendingRead();
                goto error_pending_read;
            }
            globus_fifo_enqueue(&handle->pending_ops_q, requestor);
            break;
        }
        default:
            /* if it gets here, something is wrong */
            globus_assert(0 && "Unexpected state in write");
    }
    globus_mutex_unlock(&handle->mutex);
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error_pending_read:
error_register_write:
error_put:
error_outstanding_partial_xfer:
error_operation_canceled:
    globus_mutex_unlock(&handle->mutex);
    globus_xio_operation_disable_cancel(op);
error_cancel_enable:
    globus_memory_push_node(&handle->requestor_memory, (void*)requestor);
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
void
globus_i_xio_gridftp_abort_io(
    globus_l_xio_gridftp_handle_t *     handle)
{
    globus_byte_t                       buffer;
    GlobusXIOName(globus_l_xio_gridftp_close);

    GlobusXIOGridftpDebugEnter();
    if (handle->outstanding_ops_direction == GLOBUS_TRUE)
    {
        globus_ftp_client_abort(handle->ftp_handle);
    }
    else
    {
        globus_ftp_client_register_write(
            handle->ftp_handle,
            &buffer,
            0,
            handle->offset,
            GLOBUS_TRUE, /* eof */
            globus_l_xio_gridftp_write_eof_cb,
            NULL);
    }
    GlobusXIOGridftpDebugExit();
}


static
globus_result_t
globus_l_xio_gridftp_close(
    void *                              driver_specific_handle,
    void *                              attr,
    globus_xio_operation_t              op)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_result_t                     result;
    globus_bool_t                       finish = GLOBUS_FALSE;
    GlobusXIOName(globus_l_xio_gridftp_close);

    GlobusXIOGridftpDebugEnter();
    handle = (globus_l_xio_gridftp_handle_t *) driver_specific_handle;
    /* 
     * canceling close is not supported. All that is done here abort any 
     * outstanding IO and there is no way to cancel an abort called on 
     * ftp_clinet_lib
     */
    globus_mutex_lock(&handle->mutex);
    switch (handle->state)
    {
        case GLOBUS_XIO_GRIDFTP_OPEN:
            handle->state = GLOBUS_XIO_GRIDFTP_NONE;
            finish = GLOBUS_TRUE;
            break;
        case GLOBUS_XIO_GRIDFTP_IO_DONE:
            globus_i_xio_gridftp_abort_io(handle);
            /* fall through */  
        case GLOBUS_XIO_GRIDFTP_ABORT_PENDING:
        {
            globus_i_xio_gridftp_requestor_t * requestor;
            requestor = (globus_i_xio_gridftp_requestor_t *)
                        globus_memory_pop_node(&handle->requestor_memory);
            requestor->op = op;
            /* 
             * Here, as a special case, close requestor is put in pending_ops_q
             * (pending_ops_q usually contain only read/write requestors). 
             * After transfer is aborted, close will be finished (in xfer_cb)
             */
            globus_fifo_enqueue(&handle->pending_ops_q, requestor);
            handle->state = GLOBUS_XIO_GRIDFTP_ABORT_PENDING_CLOSING;   
            break;
        }
        default:
            /* 
             * I should get close only when I'm in one of the above states
             * otherwise something is wrong (xio wouldn't give me a close
             * when there is an user operation pending)
             */
            globus_assert(0 && "Unexpected state in close");
    }
    globus_mutex_unlock(&handle->mutex);
    if (finish == GLOBUS_TRUE)
    {   
        result = globus_l_xio_gridftp_handle_destroy(handle);
        globus_assert(result == GLOBUS_SUCCESS);
        globus_xio_driver_finished_close(op, GLOBUS_SUCCESS);
    }
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;      
}


static
globus_result_t
globus_l_xio_gridftp_cntl(
    void  *                             driver_specific_handle,
    int                                 cmd,
    va_list                             ap)
{
    globus_l_xio_gridftp_handle_t *     handle;
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_gridftp_cntl);

    GlobusXIOGridftpDebugEnter();
    handle = (globus_l_xio_gridftp_handle_t *) driver_specific_handle;
    globus_mutex_lock(&handle->mutex);      
    switch(cmd)
    {
        case GLOBUS_XIO_GRIDFTP_SEEK:
        {
            /* seek is always from the start of the file */
            globus_off_t seek_offset;   
	    if (handle->attr->append)
	    {
		result = GlobusXIOGridftpErrorSeek(
					"file opened in append mode"); 
		goto error; 
	    }
            seek_offset = va_arg(ap, globus_off_t);
            if (handle->offset != seek_offset)
            {
                switch (handle->state)
                {
                    case GLOBUS_XIO_GRIDFTP_IO_DONE:
                        if (handle->attr->partial_xfer)
                        {
                            result = GlobusXIOGridftpErrorSeek(
						"operation is outstanding"); 
                            goto error; 
                        }
                        globus_i_xio_gridftp_abort_io(handle);
                        handle->state = GLOBUS_XIO_GRIDFTP_ABORT_PENDING;
                        /* fall through */      
                    case GLOBUS_XIO_GRIDFTP_OPEN:
                        /* fall through */      
                    case GLOBUS_XIO_GRIDFTP_ABORT_PENDING:
                        /* fall through */      
                    case GLOBUS_XIO_GRIDFTP_ABORT_PENDING_IO_PENDING:
                        handle->offset = seek_offset;
                        break;
                    default:
                        /* seek not allowed in state's other than above */
                        result = GlobusXIOGridftpErrorSeek(
				"operation is outstanding / invalid state"); 
                        goto error; 
                }
            }   
            break;
        }
        default:
            result = GlobusXIOErrorInvalidCommand(cmd);
            goto error;     
    }   
    globus_mutex_unlock(&handle->mutex);    
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    globus_mutex_unlock(&handle->mutex);    
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
globus_result_t
globus_l_xio_gridftp_attr_init(
    void **                             out_attr)
{
    globus_l_xio_gridftp_attr_t *       attr;
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_gridftp_attr_init);

    GlobusXIOGridftpDebugEnter();
    /*
     *  create a gridftp attr structure and initialize its values
     */
    attr = (globus_l_xio_gridftp_attr_t *) 
                globus_malloc(sizeof(globus_l_xio_gridftp_attr_t));
    if(!attr)
    {
        result = GlobusXIOErrorMemory("attr");
        goto error;
    }
    memcpy(attr, &globus_l_xio_gridftp_attr_default, 
                sizeof(globus_l_xio_gridftp_attr_t));
    result = globus_ftp_client_operationattr_init(
        &attr->ftp_operation_attr);
    if (result != GLOBUS_SUCCESS)
    {
        result = GlobusXIOErrorWrapFailed(
            "globus_ftp_client_operationattr_init", result);
        goto error;
    }
    *out_attr = attr;
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
globus_result_t
globus_i_xio_gridftp_set_authorization(
    globus_ftp_client_operationattr_t * attr,
    va_list                             ap)
{       
    globus_result_t                     result; 
    gss_cred_id_t			credential;
    char *                              user;
    char *                              password;
    char *                              account;
    char *                              subject;
    GlobusXIOName(globus_i_xio_gridftp_set_authorization);

    GlobusXIOGridftpDebugEnter();
    credential = va_arg(ap, gss_cred_id_t);
    user = va_arg(ap, char *);
    password = va_arg(ap, char *);
    account = va_arg(ap, char *);
    subject = va_arg(ap, char *);
    result = globus_ftp_client_operationattr_set_authorization(
        attr,
	credential,
        user,
        password,
        account, 
        subject);
    GlobusXIOGridftpDebugExit();
    return result;
}


static
globus_result_t
globus_i_xio_gridftp_set_parallelism(
    globus_ftp_client_operationattr_t * attr,
    int                                 num_streams)
{
    globus_result_t                     result;
    globus_ftp_control_parallelism_t    parallelism;
    GlobusXIOName(globus_i_xio_gridftp_set_parallelism);

    GlobusXIOGridftpDebugEnter();       
    /*
     * typedef enum globus_ftp_control_parallelism_mode_e
     * {
     *     GLOBUS_FTP_CONTROL_PARALLELISM_NONE,
     *     GLOBUS_FTP_CONTROL_PARALLELISM_FIXED
     * } globus_ftp_control_parallelism_mode_t;
     *
     * typedef struct globus_i_ftp_parallelism_base_s
     * {
     *     globus_ftp_control_parallelism_mode_t       mode;
     *     globus_size_t                               size;
     * } globus_i_ftp_parallelism_base_t;
     *
     * typedef struct globus_ftp_parallelism_fixed_s
     * {
     *     globus_ftp_control_parallelism_mode_t       mode;
     *     globus_size_t                               size;
     * } globus_ftp_parallelism_fixed_t;
     *
     * typedef union globus_ftp_control_parallelism_u
     * {
     *     globus_ftp_control_parallelism_mode_t    mode;
     *     globus_i_ftp_parallelism_base_t          base;
     *     globus_ftp_parallelism_fixed_t           fixed;
     * } globus_ftp_control_parallelism_t; 
     */
    result = globus_ftp_client_operationattr_set_mode(
        attr,
        GLOBUS_FTP_CONTROL_MODE_EXTENDED_BLOCK);
    if (result != GLOBUS_SUCCESS)
    {
        goto error;
    }           
    parallelism.mode = GLOBUS_FTP_CONTROL_PARALLELISM_FIXED;
    parallelism.fixed.size = num_streams;
    result = globus_ftp_client_operationattr_set_parallelism(
        attr,
        &parallelism);
    if (result != GLOBUS_SUCCESS)
    {
        goto error;
    }
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:  
    GlobusXIOGridftpDebugExitWithError();
    return result;
}       


static
globus_result_t
globus_l_xio_gridftp_attr_cntl(
    void *                              driver_attr,
    int                                 cmd,
    va_list                             ap)
{
    globus_result_t                     result;
    globus_l_xio_gridftp_attr_t *       attr;
    GlobusXIOName(globus_l_xio_gridftp_attr_cntl);

    GlobusXIOGridftpDebugEnter();
    attr = (globus_l_xio_gridftp_attr_t *) driver_attr;
    switch(cmd)
    {
        case GLOBUS_XIO_GRIDFTP_SET_HANDLE:
            attr->ftp_handle = va_arg(ap, globus_ftp_client_handle_t*);
            break;
        case GLOBUS_XIO_GRIDFTP_GET_HANDLE:
        {
            globus_ftp_client_handle_t ** ftp_handle_out;
            ftp_handle_out = va_arg(ap, globus_ftp_client_handle_t**);
            *ftp_handle_out = attr->ftp_handle;
            break;
        }
	case GLOBUS_XIO_GRIDFTP_SET_APPEND:
	    attr->append = va_arg(ap, globus_bool_t);
	    break;
	case GLOBUS_XIO_GRIDFTP_GET_APPEND:
	{
	    globus_bool_t * append_out = va_arg(ap, globus_bool_t*);
	    *append_out = attr->append;
	    break;
	}
	case GLOBUS_XIO_GRIDFTP_SET_ERET:
	    if (attr->partial_xfer)
	    {
		result = GlobusXIOGridftpErrorAttr(
			    "Partial transfers can not be used with ERET/ESTO");
		goto error;
	    }
            free(attr->eret_alg_str);
	    attr->eret_alg_str = strdup(va_arg(ap, char*));
	    break;
	case GLOBUS_XIO_GRIDFTP_GET_ERET:
	{
	    char ** eret_alg_str_out = va_arg(ap, char **);
	    *eret_alg_str_out = strdup(attr->eret_alg_str);
	    break;
	}    
	case GLOBUS_XIO_GRIDFTP_SET_ESTO:
	    if (attr->partial_xfer)
	    {
		result = GlobusXIOGridftpErrorAttr(
			    "Partial transfers can not be used with ERET/ESTO");
		goto error;
	    }
            free(attr->esto_alg_str);
	    attr->esto_alg_str = strdup(va_arg(ap, char*));
	    break;
	case GLOBUS_XIO_GRIDFTP_GET_ESTO:
	{
	    char ** esto_alg_str_out = va_arg(ap, char **);
	    *esto_alg_str_out = strdup(attr->esto_alg_str);
	    break;
	}
        /* Each read/write maps to a single partial xfer */ 
        case GLOBUS_XIO_GRIDFTP_SET_PARTIAL_TRANSFER:
        {
            globus_bool_t partial_xfer = va_arg(ap, globus_bool_t);
	    if (attr->eret_alg_str || attr->esto_alg_str)
	    {
		result = GlobusXIOGridftpErrorAttr(
			    "Partial transfers can not be used with ERET/ESTO");
		goto error;
	    }
            attr->partial_xfer = partial_xfer;
            result = globus_ftp_client_operationattr_set_read_all(
                        &attr->ftp_operation_attr, 
                        partial_xfer, 
                        NULL,
                        NULL);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_set_read_all", result);
                goto error;     
            }
            break;
        }
        case GLOBUS_XIO_GRIDFTP_GET_PARTIAL_TRANSFER:
        {
            globus_bool_t * partial_xfer_out;
            partial_xfer_out = va_arg(ap, globus_bool_t*);  
            *partial_xfer_out = attr->partial_xfer;
            break;
        }    
        case GLOBUS_XIO_GRIDFTP_SET_NUM_STREAMS:
        {
            int num_streams;
            num_streams = va_arg(ap, int);
            result = globus_i_xio_gridftp_set_parallelism(
                &attr->ftp_operation_attr, num_streams);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_i_xio_gridftp_set_parallelism", result);
                goto error;     
            }
            break;
        }
        case GLOBUS_XIO_GRIDFTP_GET_NUM_STREAMS:
        {
            globus_ftp_control_parallelism_t parallelism;
            int * num_streams_out = va_arg(ap, int*);
            result = globus_ftp_client_operationattr_get_parallelism(
                &attr->ftp_operation_attr,
                &parallelism);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_get_parallelism", result);
                goto error;     
            }
            *num_streams_out = parallelism.fixed.size;
            break;
        }
        case GLOBUS_XIO_GRIDFTP_SET_TCP_BUFFER:
        {
            /*
             * typedef union globus_ftp_control_tcpbuffer_t
             * {
             *    globus_ftp_control_tcpbuffer_mode_t         mode;
             *    globus_ftp_control_tcpbuffer_default_t      default_tcpbuf;
             *    globus_ftp_control_tcpbuffer_fixed_t        fixed;
             *    globus_ftp_control_tcpbuffer_automatic_t    automatic;
             * } globus_ftp_control_tcpbuffer_t;
             */
            globus_ftp_control_tcpbuffer_t tcp_buffer;
            tcp_buffer.mode = GLOBUS_FTP_CONTROL_TCPBUFFER_FIXED;
            tcp_buffer.fixed.size = va_arg(ap, int);
            result = globus_ftp_client_operationattr_set_tcp_buffer(
                &attr->ftp_operation_attr,
                &tcp_buffer);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_set_tcp_buffer", result);
                goto error;     
            }
            break;
        }
        case GLOBUS_XIO_GRIDFTP_GET_TCP_BUFFER:
        {
            globus_ftp_control_tcpbuffer_t tcp_buffer;
            int * buf_size_out = va_arg(ap, int*);
            result = globus_ftp_client_operationattr_get_tcp_buffer(
                &attr->ftp_operation_attr,
                &tcp_buffer);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_get_tcp_buffer", result);
                goto error;     
            }
            *buf_size_out = tcp_buffer.fixed.size;
            break;
        }
        /* 
         * I force all the xfers to be in type I (binary). So I don't support
         * GLOBUS_XIO_GRIDFTP_SET/GET_TYPE 
         */
        case GLOBUS_XIO_GRIDFTP_SET_MODE:
            result = globus_ftp_client_operationattr_set_mode(
                &attr->ftp_operation_attr,
                va_arg(ap, int));
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_set_mode", result);
                goto error;     
            }
            break;
        case GLOBUS_XIO_GRIDFTP_GET_MODE:
        {
            globus_ftp_control_mode_t ftp_mode;
            int * mode_out;
            mode_out = va_arg(ap, int*);
            result = globus_ftp_client_operationattr_get_mode(
                &attr->ftp_operation_attr,
                &ftp_mode);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_get_mode", result);
                goto error;     
            }
            *mode_out = ftp_mode;
            break;
        }
        case GLOBUS_XIO_GRIDFTP_SET_AUTH:
            result = globus_i_xio_gridftp_set_authorization(
                &attr->ftp_operation_attr, 
                ap);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_i_xio_gridftp_set_authorization", result);
                goto error;     
            }
            break;
        case GLOBUS_XIO_GRIDFTP_GET_AUTH:
        {
            gss_cred_id_t * credential = va_arg(ap, gss_cred_id_t*);
            char ** user_out = va_arg(ap, char**);
            char ** password_out = va_arg(ap, char**);
            char ** account_out = va_arg(ap, char**);
            char ** subject_out = va_arg(ap, char**);
	    
            result = globus_ftp_client_operationattr_get_authorization(
                &attr->ftp_operation_attr,
                credential,
                user_out,
                password_out,
                account_out,
                subject_out);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_get_authorization", 
                    result);
                goto error;     
            }
            break;
        }
        case GLOBUS_XIO_GRIDFTP_SET_DCAU:
        {
            globus_ftp_control_dcau_t dcau;
            dcau.mode = va_arg(ap, int);        
            dcau.subject.subject = va_arg(ap, char*);
            result = globus_ftp_client_operationattr_set_dcau(
                &attr->ftp_operation_attr,
                &dcau);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_set_dcau", result);
                goto error;     
            }
            break;
        }
        case GLOBUS_XIO_GRIDFTP_GET_DCAU:
        {
            globus_ftp_control_dcau_t dcau;
            int * dcau_mode_out = va_arg(ap, int*);
            char ** dcau_subject_out = va_arg(ap, char**);
            result = globus_ftp_client_operationattr_get_dcau(
                &attr->ftp_operation_attr,
                &dcau);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_get_dcau", result);
                goto error;     
            }
            *dcau_mode_out = dcau.mode;
            *dcau_subject_out = dcau.subject.subject;
            break;
        }
        case GLOBUS_XIO_GRIDFTP_SET_DATA_PROTECTION:
            result = globus_ftp_client_operationattr_set_data_protection(
                &attr->ftp_operation_attr,
                va_arg(ap, int));
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_set_data_protection", 
                    result);
                goto error;     
            }
            break;
        case GLOBUS_XIO_GRIDFTP_GET_DATA_PROTECTION:
        {
            globus_ftp_control_protection_t ftp_protection;
            int * protection_out = va_arg(ap, int*);
            result = globus_ftp_client_operationattr_get_data_protection(
                &attr->ftp_operation_attr,
                &ftp_protection);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_get_data_protection", 
                    result);
                goto error;     
            }
            *protection_out = ftp_protection;
            break;
        }
        case GLOBUS_XIO_GRIDFTP_SET_CONTROL_PROTECTION:
            result = globus_ftp_client_operationattr_set_control_protection(
                &attr->ftp_operation_attr,
                va_arg(ap, int));
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_set_control_protection", 
                    result);
                goto error;     
            }
            break;
        case GLOBUS_XIO_GRIDFTP_GET_CONTROL_PROTECTION:
        {
            globus_ftp_control_protection_t ftp_protection;
            int * protection_out = va_arg(ap, int*);
            result = globus_ftp_client_operationattr_get_control_protection(
                &attr->ftp_operation_attr,
                &ftp_protection);
            if (result != GLOBUS_SUCCESS)
            {
                result = GlobusXIOErrorWrapFailed(
                    "globus_ftp_client_operationattr_get_control_protection", 
                    result);
                goto error;     
            }
            *protection_out = ftp_protection;
            break;
        }
        default:
           result = GlobusXIOErrorInvalidCommand(cmd);
           goto error;
    }   
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:  
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
globus_result_t
globus_l_xio_gridftp_attr_copy(
    void **                             dst,
    void *                              src)
{
    globus_l_xio_gridftp_attr_t *       src_attr;
    globus_l_xio_gridftp_attr_t *       dst_attr;
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_gridftp_attr_copy);

    GlobusXIOGridftpDebugEnter();
    dst_attr = (globus_l_xio_gridftp_attr_t *) 
                globus_malloc(sizeof(globus_l_xio_gridftp_attr_t));
    if(!dst_attr)
    {
        result = GlobusXIOErrorMemory("attr");
        goto error_dst_attr;
    }
    src_attr = (globus_l_xio_gridftp_attr_t *) src;
    memcpy(dst_attr, src_attr, sizeof(globus_l_xio_gridftp_attr_t)); 

    result = globus_ftp_client_operationattr_copy(
        &dst_attr->ftp_operation_attr, &src_attr->ftp_operation_attr);
    if (result != GLOBUS_SUCCESS)
    {
        result = GlobusXIOErrorWrapFailed(
            "globus_ftp_client_operationattr_copy", result);
        goto error_operationattr_copy;
    }
    *dst = dst_attr;
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error_operationattr_copy:
    globus_free(dst_attr);
error_dst_attr:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
globus_result_t
globus_l_xio_gridftp_attr_destroy(
    void *                              driver_attr)
{
    globus_l_xio_gridftp_attr_t *       attr;
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_gridftp_attr_destroy);

    GlobusXIOGridftpDebugEnter();
    attr = (globus_l_xio_gridftp_attr_t *) driver_attr;
    if (attr->ftp_operation_attr)
    {
        result = globus_ftp_client_operationattr_destroy(
                    &attr->ftp_operation_attr);
        if(result != GLOBUS_SUCCESS)
        {
            result = GlobusXIOErrorWrapFailed(
                "globus_ftp_client_operationattr_destroy", result);
            goto error;
        }
    }
    globus_free(attr);
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
globus_result_t
globus_l_xio_gridftp_init(
    globus_xio_driver_t *               out_driver)
{
    globus_xio_driver_t                 driver;
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_gridftp_init);

    GlobusXIOGridftpDebugEnter();
    result = globus_xio_driver_init(&driver, "gridftp", NULL);
    if(result != GLOBUS_SUCCESS)
    {
        result = GlobusXIOErrorWrapFailed(
            "globus_l_xio_driver_init", result);
        goto error_init;
    }
    globus_xio_driver_set_transport(
        driver,
        globus_l_xio_gridftp_open,
        globus_l_xio_gridftp_close,
        globus_l_xio_gridftp_read,
        globus_l_xio_gridftp_write,
        globus_l_xio_gridftp_cntl);
    globus_xio_driver_set_attr(
        driver,
        globus_l_xio_gridftp_attr_init,
        globus_l_xio_gridftp_attr_copy,
        globus_l_xio_gridftp_attr_cntl,
        globus_l_xio_gridftp_attr_destroy);
    *out_driver = driver;
    GlobusXIOGridftpDebugExit();
    return GLOBUS_SUCCESS;

error_init:
    GlobusXIOGridftpDebugExitWithError();
    return result;
}


static
void
globus_l_xio_gridftp_destroy(
    globus_xio_driver_t                 driver)
{
    globus_xio_driver_destroy(driver);
}


GlobusXIODefineDriver(
    gridftp,
    globus_l_xio_gridftp_init,
    globus_l_xio_gridftp_destroy);