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

#ifndef __FS_NFS_NFS_SOCKET_H
#define __FS_NFS_NFS_SOCKET_H

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <sys/socket.h>
#include <queue.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <debug.h>
#include <nuttx/kmalloc.h>

#include "xdr_subs.h"
#include "nfs_proto.h"
#include "rpc.h"
#include "rpc_clnt_private.h"
#include "rpc_v2.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define RPC_RETURN(X) do { nvdbg("returning %d\n", X); return X; } while(0)

/* Estimate rto for an nfs rpc sent via. an unreliable datagram. Use the mean
 * and mean deviation of rtt for the appropriate type of rpc for the frequent
 * rpcs and a default for the others. The justification for doing "other"
 * this way is that these rpcs happen so infrequently that timer est. would
 * probably be stale. Also, since many of these rpcs are non-idempotent, a
 * conservative timeout is desired. getattr, lookup - A+2D read, write     -
 * A+4D other           - nm_timeo
 */

#define RPC_RTO(n, t) \
        ((t) == 0 ? (n)->rc_timeo : \
         ((t) < 3 ? \
          (((((n)->rc_srtt[t-1] + 3) >> 2) + (n)->rc_sdrtt[t-1] + 1) >> 1) : \
          ((((n)->rc_srtt[t-1] + 7) >> 3) + (n)->rc_sdrtt[t-1] + 1)))

#define RPC_SRTT(s,r)   (r)->r_rpcclnt->rc_srtt[rpcclnt_proct((s),\
                                (r)->r_procnum) - 1]

#define RPC_SDRTT(s,r)  (r)->r_rpcclnt->rc_sdrtt[rpcclnt_proct((s),\
                                (r)->r_procnum) - 1]

/* There is a congestion window for outstanding rpcs maintained per mount
 * point. The cwnd size is adjusted in roughly the way that: Van Jacobson,
 * Congestion avoidance and Control, In "Proceedings of SIGCOMM '88". ACM,
 * August 1988. describes for TCP. The cwnd size is chopped in half on a
 * retransmit timeout and incremented by 1/cwnd when each rpc reply is
 * received and a full cwnd of rpcs is in progress. (The sent count and cwnd
 * are scaled for integer arith.) Variants of "slow start" were tried and
 * were found to be too much of a performance hit (ave. rtt 3 times larger),
 * I suspect due to the large rtt that nfs rpcs have.
 */

#define RPC_CWNDSCALE   256
#define RPC_MAXCWND     (RPC_CWNDSCALE * 32)

#define RPC_ERRSTR_ACCEPTED_SIZE 6
#define RPC_ERRSTR_AUTH_SIZE 6

/****************************************************************************
 * Public Data
 ****************************************************************************/

char *rpc_errstr_accepted[RPC_ERRSTR_ACCEPTED_SIZE] =
{
  "",                           /* no good message... */
  "remote server hasn't exported program.",
  "remote server can't support version number.",
  "program can't support procedure.",
  "procedure can't decode params.",
  "remote error.  remote side memory allocation failure?"
};

char *rpc_errstr_denied[2] =
{
  "remote server doesnt support rpc version 2!",
  "remote server authentication error."
};

char *rpc_errstr_auth[RPC_ERRSTR_AUTH_SIZE] =
{
  "",
  "auth error: bad credential (seal broken).",
  "auth error: client must begin new session.",
  "auth error: bad verifier (seal broken).",
  "auth error: verifier expired or replayed.",
  "auth error: rejected for security reasons.",
};

/****************************************************************************
 * Private Data
 ****************************************************************************/

//static int rpcclnt_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };

/* Static data, mostly RPC constants in XDR form */

static uint32_t rpc_reply;
static uint32_t rpc_call;
static uint32_t rpc_vers;
static uint32_t rpc_msgdenied;
static uint32_t rpc_mismatch;
static uint32_t rpc_auth_unix;
static uint32_t rpc_msgaccepted;
static uint32_t rpc_autherr;
static uint32_t rpc_auth_null;

static uint32_t rpcclnt_xid = 0;
static uint32_t rpcclnt_xid_touched = 0;
int rpcclnt_ticks;
struct rpcstats rpcstats;
//struct rpc_call *callmgs;
//struct rpc_reply *replymsg;

/* Queue head for rpctask's */

static dq_queue_t rpctask_q;
//struct callout_handle rpcclnt_timer_handle;

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

static int rpcclnt_send(struct socket *, struct sockaddr *, int, int, void *,
                        struct rpctask *);
static int rpcclnt_receive(struct rpctask *, struct sockaddr *,
                           void *);//, struct rpc_call *);
static int rpcclnt_reply(struct rpctask *, struct rpc_reply_header *, void *);
#ifdef CONFIG_NFS_TCPIP
static int rpcclnt_sndlock(int *, struct rpctask *);
static void rpcclnt_sndunlock(int *);
static int rpcclnt_rcvlock(struct rpctask *);
static void rpcclnt_rcvunlock(int *);
static int  rpcclnt_sigintr(struct rpcclnt *, struct rpctask *, cthread_t *);
#endif
#ifdef COMP
static void rpcclnt_softterm(struct rpctask *task);
static void rpcclnt_timer(void *, struct rpc_call *);
#endif
static uint32_t rpcclnt_proct(struct rpcclnt *, uint32_t);
static int rpcclnt_buildheader(struct rpcclnt *, int, int, int, struct xidr *, FAR const void *,
                               void *);

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/* This is the nfs send routine. For connection based socket types, it must
 * be called with an nfs_sndlock() on the socket. "rep == NULL" indicates
 * that it has been called from a server. For the client side: - return EINTR
 * if the RPC is terminated, 0 otherwise - set TASK_MUSTRESEND if the send fails
 * for any reason - do any cleanup required by recoverable socket errors
 * (???) For the server side: - return EINTR or ERESTART if interrupted by a
 * signal - return EPIPE if a connection is lost for connection based sockets
 * (TCP...) - do any cleanup required by recoverable socket errors (???)
 */

static int
rpcclnt_send(struct socket *so, struct sockaddr *nam, int procid, int prog,
             void *call, struct rpctask *rep)
{
  struct sockaddr *sendnam;
  int error = ESRCH;
#ifdef CONFIG_NFS_TCPIP
  int soflags;
#endif
  int flags;

  if (rep != NULL)
    {
      if (rep->r_flags & TASK_SOFTTERM)
        {
          RPC_RETURN(EINTR);
        }

      if ((so = rep->r_rpcclnt->rc_so) == NULL)
        {
          rep->r_flags |= TASK_MUSTRESEND;
          RPC_RETURN(0);
        }

      rep->r_flags &= ~TASK_MUSTRESEND;
#ifdef CONFIG_NFS_TCPIP
      soflags = rep->r_rpcclnt->rc_soflags;
#endif
    }
#ifdef CONFIG_NFS_TCPIP
  else
    {
      soflags = so->s_flags;
    }

  if ((soflags & PR_CONNREQUIRED))
    {
      sendnam = NULL;
    {
  else
#endif
    {
      sendnam = nam;
    }

  if (so->s_type == SOCK_SEQPACKET)
    {
      flags = MSG_EOR;
    }
  else
    {
      flags = 0;
    }

  if (prog == PMAPPROG)
    {
      if (procid == PMAPPROC_GETPORT)
        {
          struct rpc_call_pmap *callmsg = (struct rpc_call_pmap *)call;
          error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                               sendnam, sizeof(*sendnam));
        }
      else if (procid == PMAPPROC_UNSET)
        {
          struct rpc_call_pmap *callmsg = (struct rpc_call_pmap *)call;
          error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                               sendnam, sizeof(*sendnam));
        }
    }

  else if (prog == RPCPROG_MNT)
    {
      if (procid == RPCMNT_UMOUNT)
        {
          struct rpc_call_mount *callmsg = (struct rpc_call_mount *)call;
          error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                               sendnam, sizeof(*sendnam));
        }
      else if (procid == RPCMNT_MOUNT)
        {
          struct rpc_call_mount *callmsg = (struct rpc_call_mount *)call;
          error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                               sendnam, sizeof(*sendnam));
        }
    }

  else if (prog == NFS_PROG)
    {
      switch (procid)
        {
        case NFSPROC_CREATE:
          {
            struct rpc_call_create *callmsg = (struct rpc_call_create *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                 sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_READ:
          {
            struct rpc_call_read *callmsg = (struct rpc_call_read *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                 sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_WRITE:
          {
            struct rpc_call_write *callmsg = (struct rpc_call_write *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                 sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_READDIR:
          {
            struct rpc_call_readdir *callmsg = (struct rpc_call_readdir *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                 sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_FSSTAT:
          {
            struct rpc_call_fs *callmsg = (struct rpc_call_fs *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                 sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_REMOVE:
          {
            struct rpc_call_remove *callmsg  = (struct rpc_call_remove *)call;
                  error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                           sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_MKDIR:
          {
            struct rpc_call_mkdir *callmsg = (struct rpc_call_mkdir *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                 sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_RMDIR:
          {
            struct rpc_call_rmdir *callmsg  = (struct rpc_call_rmdir *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_RENAME:
          {
            struct rpc_call_rename *callmsg  = (struct rpc_call_rename *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                 sendnam, sizeof(*sendnam));
          }
          break;

        case NFSPROC_FSINFO:
          {
            struct rpc_call_fs *callmsg = (struct rpc_call_fs *)call;
            error = psock_sendto(so, callmsg, sizeof(*callmsg), flags,
                                 sendnam, sizeof(*sendnam));
          }
          break;

        default:
          break;
        }
    }

  if (error < 0)
    {
      if (rep != NULL)
        {
          ndbg("rpc send error %d for service %s\n", error,
               rep->r_rpcclnt->rc_prog->prog_name);

          /* Deal with errors for the client side. */

          if (rep->r_flags & TASK_SOFTTERM)
            {
              error = EINTR;
            }
          else
            {
              rep->r_flags |= TASK_MUSTRESEND;
            }
        }
      else
        {
          ndbg("rpc service send error %d\n", error);
        }

      RPC_RETURN(error);

    }
  else
    {
      RPC_RETURN(0);
    }
}

/* Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
 * done by soreceive().For SOCK_STREAM, first get the
 * Record Mark to find out how much more there is to get. We must
 * lock the socket against other receivers until we have an entire
 * rpc request/reply.
 */

static int rpcclnt_receive(struct rpctask *rep, struct sockaddr *aname,
                           void *reply)//, struct rpc_call *call)
{
  struct socket *so;
#ifdef CONFIG_NFS_TCPIP
  uint32_t len;
  int sotype;
#endif
  int error = 0;
  int rcvflg;

#ifdef CONFIG_NFS_TCPIP
  /* Set up arguments for soreceive() */

  sotype = rep->r_rpcclnt->rc_sotype;

  /* For reliable protocols, lock against other senders/receivers in
   * case a reconnect is necessary. For SOCK_STREAM, first get the
   * Record Mark to find out how much more there is to get. We must
   * lock the socket against other receivers until we have an entire
   * rpc request/reply.
   */

  if (sotype != SOCK_DGRAM)
    {
      error = rpcclnt_sndlock(&rep->r_rpcclnt->rc_flag, rep);
      if (error != 0)
        {
          return error;
        }

    tryagain:

      /* Check for fatal errors and resending request.
       *
       * Ugh: If a reconnect attempt just happened, rc_so would
       * have changed. NULL indicates a failed attempt that has
       * essentially shut down this mount point.
       */

      if (rep->r_flags & TASK_SOFTTERM)
        {
          rpcclnt_sndunlock(&rep->r_rpcclnt->rc_flag);
          return EINTR;
        }

      so = rep->r_rpcclnt->rc_so;
      if (so == NULL)
        {
          error = rpcclnt_reconnect(rep);
          if (error)
            {
              rpcclnt_sndunlock(&rep->r_rpcclnt->rc_flag);
              return error;
            }

          goto tryagain;
        }
      while (rep->r_flags & TASK_MUSTRESEND)
        {
          rpcstats.rpcretries++;
          error = rpcclnt_send(so, rep->r_rpcclnt->rc_name, call, rep);
          if (error)
            {
              if (error == EINTR || error == ERESTART ||
                  (error = rpcclnt_reconnect(rep)) != 0)
                {
                  rpcclnt_sndunlock(&rep->r_rpcclnt->rc_flag);
                  return error;
                }

              goto tryagain;
            }
        }

      rpcclnt_sndunlock(&rep->r_rpcclnt->rc_flag);
      if (sotype == SOCK_STREAM)
        {
          do
            {
              socklen_t fromlen = sizeof(*rep->r_rpcclnt->rc_name)
              rcvflg = MSG_WAITALL;
              error = psock_recvfrom(so, reply, sizeof(*reply),
                                     &rcvflg, rep->r_rpcclnt->rc_name,
                                     &fromlen);
              if (error == EWOULDBLOCK && rep && (rep->r_flags & TASK_SOFTTERM))
                {
                  RPC_RETURN(EINTR);
                }
            }
          while (error == EWOULDBLOCK);

          if (error == 0)
            {
              ndbg("short receive from rpc server %s\n",
                   rep->r_rpcclnt->rc_prog->prog_name);
              error = EPIPE;
            }

          len = ntohl(len) & ~0x80000000;

          /* This is SERIOUS! We are out of sync with the
           * sender and forcing a disconnect/reconnect is all I
           * can do.
           */

          if (len > RPC_MAXPACKET)
            {
              ndbg("%s (%d) from rpc server %s\n",
                   "impossible packet length",
                   len, rep->r_rpcclnt->rc_prog->prog_name);
              error = EFBIG;
              goto errout;
            }
          do
            {
              socklen_t fromlen = sizeof(*rep->r_rpcclnt->rc_name);
              rcvflg = MSG_WAITALL;
              error = psock_recvfrom(so, reply, sizeof(*reply),
                                     &rcvflg, rep->r_rpcclnt->rc_name,
                                     &fromlen);
            }
          while (error == EWOULDBLOCK || error == EINTR || error == ERESTART);

          if (error == 0)
            {
              ndbg("short receive from rpc server %s\n",
                   rep->r_rpcclnt->rc_prog->prog_name);
              error = EPIPE;
            }

          if (error != 0)
            {
              goto errout;
            }
        }
      else
        {
          /* NB: Since uio_resid is big, MSG_WAITALL is ignored
           * and soreceive() will return when it has either a
           * control msg or a data msg. We have no use for
           * control msg., but must grab them and then throw
           * them away so we know what is going on.
           */

          do
            {
              socklen_t fromlen = sizeof(*rep->r_rpcclnt->rc_name);
              rcvflg = 0;
              error = psock_recvfrom(so, reply, sizeof(*reply), &rcvflg,
                                     rep->r_rpcclnt->rc_name, &fromlen);
              if (error == EWOULDBLOCK && rep)
                {
                  if (rep->r_flags & TASK_SOFTTERM)
                    {
                      return EINTR;
                    }
                }
            }
          while (error == EWOULDBLOCK || (!error));

          if ((rcvflg & MSG_EOR) == 0)
            {
              ndbg("Egad!!\n");
            }

          if (error == 0)
            {
              error = EPIPE;
            }
        }

    errout:
      if (error != 0 && error != EINTR && error != ERESTART)
        {
          if (error != EPIPE)
            {
              ndbg("receive error %d from rpc server %s\n",
                   error, rep->r_rpcclnt->rc_prog->prog_name);
            }

          error = rpcclnt_sndlock(&rep->r_rpcclnt->rc_flag, rep);
          if (error == 0)
            {
              error = rpcclnt_reconnect(rep);
            }

          if (error == 0)
            {
              goto tryagain;
            }
        }
    }
  else
    {
#endif
      so = rep->r_rpcclnt->rc_so;
      if (so == NULL)
        {
          RPC_RETURN(EACCES);
        }

      socklen_t fromlen = sizeof(*aname);
      rcvflg = 0;
      error = psock_recvfrom(so, reply, sizeof(*reply), rcvflg,
                             aname, &fromlen);
      nvdbg("psock_recvfrom returns %d\n", error);
      if (error > 0)
        {
          RPC_RETURN(0);
        }

#ifdef CONFIG_NFS_TCPIP
    }
#endif
  RPC_RETURN(ENONET);
}

/* Implement receipt of reply on a socket. We must search through the list of
 * received datagrams matching them with outstanding requests using the xid,
 * until ours is found.
 */

static int
rpcclnt_reply(struct rpctask *myrep, struct rpc_reply_header *replyheader,  //Here we need to make changes for debugging
              void *reply)
{
  struct rpctask *rep;
  struct rpcclnt *rpc = myrep->r_rpcclnt;
  int32_t t1;
  uint32_t rxid;
  int error;
  int count;

  /* Loop around until we get our own reply */

  for (count = 0; count < 9; count++)
    {
      /* Lock against other receivers so that I don't get stuck in
       * sbwait() after someone else has received my reply for me.
       * Also necessary for connection based protocols to avoid
       * race conditions during a reconnect.
       */

#ifdef CONFIG_NFS_TCPIP
      error = rpcclnt_rcvlock(myrep);
      if (error)
        {
          return error;
        }
#endif
      /* Get the next Rpc reply off the socket */

      error = rpcclnt_receive(myrep, rpc->rc_name, reply);//, call);
#ifdef CONFIG_NFS_TCPIP
      rpcclnt_rcvunlock(&rpc->rc_flag);
#endif

      if (error != 0)
        {
          /* Ignore routing errors on connectionless
           * protocols??
           */

          if (RPCIGNORE_SOERROR(rpc->rc_soflags, error))
            {
              if (myrep->r_flags & TASK_GETONEREP)
                {
                  RPC_RETURN(0);
                }

              ndbg("ignoring routing error on connectionless protocol.");
              continue;
            }
          RPC_RETURN(error);
        }

      bcopy(reply, replyheader, sizeof(struct rpc_reply_header));

      /* Get the xid and check that it is an rpc replysvr */

      rxid = replyheader->rp_xid;

      if (replyheader->rp_direction != rpc_reply)
        {
          rpcstats.rpcinvalid++;
          if (myrep->r_flags & TASK_GETONEREP)
            {
              RPC_RETURN(0);
            }

          continue;
        }

      /* Loop through the request list to match up the reply Iff no
       * match, just drop the datagram
       */

      for (rep = (struct rpctask *)rpctask_q.head; rep != NULL;
           rep = (struct rpctask *)rep->r_chain.flink)
        {
          if (rxid == rep->r_xid)
            {
              /* Update congestion window. Do the additive
               * increase of one rpc/rtt.
               */

              if (rpc->rc_cwnd <= rpc->rc_sent)
                {
                  rpc->rc_cwnd +=
                    (RPC_CWNDSCALE * RPC_CWNDSCALE +
                     (rpc->rc_cwnd >> 1)) / rpc->rc_cwnd;

                  if (rpc->rc_cwnd > RPC_MAXCWND)
                    {
                      rpc->rc_cwnd = RPC_MAXCWND;
                    }
                }

              rep->r_flags &= ~TASK_SENT;
              rpc->rc_sent -= RPC_CWNDSCALE;

              /* Update rtt using a gain of 0.125 on the
               * mean and a gain of 0.25 on the deviation.
               */

              if (rep->r_flags & TASK_TIMING)
                {
                  /* Since the timer resolution of
                   * NFS_HZ is so course, it can often
                   * result in r_rtt == 0. Since r_rtt
                   * == N means that the actual rtt is
                   * between N+dt and N+2-dt ticks, add
                   * 1.
                   */

                  t1 = rep->r_rtt + 1;
                  t1 -= (RPC_SRTT(rpc, rep) >> 3);
                  RPC_SRTT(rpc, rep) += t1;
                  if (t1 < 0)
                    {
                      t1 = -t1;
                    }

                  t1 -= (RPC_SDRTT(rpc, rep) >> 2);
                  RPC_SDRTT(rpc, rep) += t1;
                }

              rpc->rc_timeouts = 0;
              break;
            }
        }

      /* If not matched to a request, drop it. If it's mine, get
       * out.
       */

      if (rep == 0)
        {
          ndbg("rpc reply not matched\n");
          rpcstats.rpcunexpected++;
          RPC_RETURN(ENOMSG);
        }
      else if (rep == myrep)
        {
          RPC_RETURN(0);
        }

      if (myrep->r_flags & TASK_GETONEREP)
        {
          RPC_RETURN(0);
        }
    }

  RPC_RETURN(ENONET);
}

#ifdef CONFIG_NFS_TCPIP
static int
rpcclnt_sigintr( struct rpcclnt *rpc, struct rpctask *task, cthread_t *td)
{
  struct proc *p;
  sigset_t     tmpset;

  if (rpc == NULL)
    {
      return EFAULT;
    }

  if (ISSET(rpc->rc_flag, RPCCLNT_REDIRECT))
    {
      return 0;
    }

  if (task && ISSET(task->r_flags, TASK_SOFTTERM))
    {
      RPC_RETURN(EINTR);
    }

  if (!ISSET(rpc->rc_flag, RPCCLNT_INT))
    {
      RPC_RETURN(0);
    }

  if (td == NULL)
    {
      return 0;
    }

  p = cthread_proc(td);

  PROC_LOCK(p);
  tmpset = p->p_siglist;
  SIGSETNAND(tmpset, td->td_sigmask);
  mtx_lock(&p->p_sigacts->ps_mtx);
  SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
  mtx_unlock(&p->p_sigacts->ps_mtx);

  if (SIGNOTEMPTY(p->p_siglist) && RPCCLNTINT_SIGMASK(tmpset))
    {
      PROC_UNLOCK(p);
      RPC_RETURN(EINTR);
    }

  PROC_UNLOCK(p);
  RPC_RETURN(0);
}

/* Lock a socket against others. Necessary for STREAM sockets to ensure you
 * get an entire rpc request/reply and also to avoid race conditions between
 * the processes with nfs requests in progress when a reconnect is necessary.
 */

static int rpcclnt_sndlock(int *flagp, struct rpctask *task)
{
  int slpflag = 0, slptimeo = 0;

  if (task)
    {
      if (task->r_rpcclnt->rc_flag & RPCCLNT_INT)
        slpflag = PCATCH;
    }

  while (*flagp & RPCCLNT_SNDLOCK)
    {
      if (rpcclnt_sigintr(task->r_rpcclnt, task, p))
        {
          return EINTR;
        }

      *flagp |= RPCCLNT_WANTSND;
      if (slpflag == PCATCH)
        {
          slpflag = 0;
          slptimeo = 2 * CLOCKS_PER_SEC;
        }
    }

  *flagp |= RPCCLNT_SNDLOCK;
  return 0;
}

/* Unlock the stream socket for others. */

static void rpcclnt_sndunlock(int *flagp)
{
  if ((*flagp & RPCCLNT_SNDLOCK) == 0)
    {
      panic("rpc sndunlock");
    }

  *flagp &= ~RPCCLNT_SNDLOCK;
  if (*flagp & RPCCLNT_WANTSND)
    {
      *flagp &= ~RPCCLNT_WANTSND;
    }
}

static int rpcclnt_rcvlock(struct rpctask *task)
{
  int *flagp = &task->r_rpcclnt->rc_flag;
  int slpflag, slptimeo = 0;

  if (*flagp & RPCCLNT_INT)
    {
      slpflag = PCATCH;
    }
  else
    {
      slpflag = 0;
    }

  while (*flagp & RPCCLNT_RCVLOCK)
    {
      if (rpcclnt_sigintr(task->r_rpcclnt, task, task->r_td))
        {
          return EINTR;
        }

      *flagp |= RPCCLNT_WANTRCV;
      tsleep((caddr_t) flagp, slpflag | (PZERO - 1), "rpcrcvlk", slptimeo);
      if (slpflag == PCATCH)
        {
          slpflag = 0;
          slptimeo = 2 * CLOCKS_PER_SEC;
        }
    }

  *flagp |= RPCCLNT_RCVLOCK;
  return 0;
}

/* Unlock the stream socket for others. */

static void rpcclnt_rcvunlock(int *flagp)
{
  if ((*flagp & RPCCLNT_RCVLOCK) == 0)
    {
      panic("nfs rcvunlock");
    }

  *flagp &= ~RPCCLNT_RCVLOCK;
  if (*flagp & RPCCLNT_WANTRCV)
    {
      *flagp &= ~RPCCLNT_WANTRCV;
      wakeup((caddr_t) flagp);
    }
}
#endif

static uint32_t rpcclnt_proct(struct rpcclnt *rpc, uint32_t procid)
{
  if (rpc->rc_proctlen != 0 && rpc->rc_proct != NULL &&
      procid < rpc->rc_proctlen)
    {
      return rpc->rc_proct[procid];
    }

  return 0;
}

#ifdef COMP
static void rpcclnt_softterm(struct rpctask *task)
{
  task->r_flags |= TASK_SOFTTERM;
  if (task->r_flags & TASK_SENT)
    {
      task->r_rpcclnt->rc_sent -= RPC_CWNDSCALE;
      task->r_flags &= ~TASK_SENT;
    }
}
#endif

/****************************************************************************
 * Public Functions
 ****************************************************************************/

void rpcclnt_init(void)
{
  rpcclnt_ticks = (CLOCKS_PER_SEC * RPC_TICKINTVL + 500) / 1000;
  if (rpcclnt_ticks < 1)
    {
      rpcclnt_ticks = 1;
    }

  rpcstats.rpcretries = 0;
  rpcstats.rpcrequests = 0;
  rpcstats.rpctimeouts = 0;
  rpcstats.rpcunexpected = 0;
  rpcstats.rpcinvalid = 0;

  /* RPC constants how about actually using more than one of these! */

  rpc_reply = txdr_unsigned(RPC_REPLY);
  rpc_vers = txdr_unsigned(RPC_VER2);
  rpc_call = txdr_unsigned(RPC_CALL);
  rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
  rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
  rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
  rpc_autherr = txdr_unsigned(RPC_AUTHERR);
  rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
  rpc_auth_null = txdr_unsigned(RPCAUTH_NULL);

  /* Initialize rpctask queue */

  dq_init(&rpctask_q);

  //rpcclnt_timer(NULL, callmgs);

  nvdbg("rpc initialized");
  return;
}

/*
void
rpcclnt_uninit(void)
{
  nvdbg("uninit");
  untimeout(rpcclnt_timer, (void *)NULL, rpcclnt_timer_handle);
}
*/

/* Initialize sockets and congestion for a new RPC connection. We do not free
 * the sockaddr if error.
 */

int rpcclnt_connect(struct rpcclnt *rpc)
{
  struct socket *so;
  int error;
  struct sockaddr *saddr;
  struct sockaddr_in sin;
  struct sockaddr_in *sa;
  struct call_args_pmap sdata;
  struct call_args_mount mountd;
  struct rpc_reply_pmap *rdata;
  struct rpc_reply_mount *mdata;
  void *reply = NULL;
  struct timeval tv;
  uint16_t tport;

  /* Create the socket */

  saddr = rpc->rc_name;
  memset(&sin, 0, sizeof(sin));

  /* Create an instance of the socket state structure */

  so = (struct socket *)kzalloc(sizeof(struct socket));
  if (!so)
    {
      fdbg("Failed to allocate socket structure\n");
      return -ENOMEM;
    }

  error =
    psock_socket(saddr->sa_family, rpc->rc_sotype, rpc->rc_soproto, so);

  if (error != 0)
    {
      ndbg("error %d in psock_socket()", error);
      RPC_RETURN(error);
    }

  so->s_crefs = 1;
  rpc->rc_so = so;
  rpc->rc_soflags = so->s_flags;

   /* Always set receive timeout to detect server crash and reconnect.
    * Otherwise, we can get stuck in psock_receive forever.
    */

  tv.tv_sec = 1;
  tv.tv_usec = 0;

  error = psock_setsockopt(rpc->rc_so, SOL_SOCKET, SO_RCVTIMEO,
                          (const void *)&tv, sizeof(tv));
  if (error != 0)
    {
      goto bad;
    }

  /* Some servers require that the client port be a reserved port
   * number. We always allocate a reserved port, as this prevents
   * filehandle disclosure through UDP port capture.
   */

  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  tport = 1024;

  do
    {
      tport--;
      sin.sin_port = htons(tport);
      error = psock_bind(rpc->rc_so, (struct sockaddr *)&sin, sizeof(sin));
    }
  while (error == EADDRINUSE && tport > 1024 / 2);

  if (error)
    {
      ndbg("bind failed\n");
      goto bad;
    }

  /* Protocols that do not require connections may be optionally left
   * unconnected for servers that reply from a port other than
   * NFS_PORT.
   */

#ifdef CONFIG_NFS_TCPIP
  if (rpc->rc_soflags == PR_CONNREQUIRED)
    {
      error = ENOTCONN;
      goto bad;
    }
  else
#endif
    {
      error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));

      if (error)
        {
          ndbg("psock_connect to ppmap port returns %d", error);
          goto bad;
        }

      /* Do the RPC to get a dynamic bounding with the server using ppmap.
       * Get port number for MOUNTD.
       */

      memset(&sdata, 0, sizeof(sdata));
      sdata.prog = txdr_unsigned(RPCPROG_MNT);
      sdata.vers = txdr_unsigned(RPCMNT_VER1);
      sdata.proc = txdr_unsigned(IPPROTO_UDP);
      sdata.port = 0;

      error = rpcclnt_request(rpc, PMAPPROC_GETPORT, PMAPPROG, PMAPVERS,
                              reply, (FAR const void *)&sdata);
      if (error != 0)
        {
          goto bad;
        }

      rdata = (struct rpc_reply_pmap *)reply;
      nvdbg("Port from the server %d\n", rdata->pmap.port);
      sa = (FAR struct sockaddr_in *)saddr;
      sa->sin_port = rdata->pmap.port;
      nvdbg("Port in socket %d\n", sa->sin_port);

      error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));
      if (error)
        {
          ndbg("psock_connect NFS port returns %d\n", error);
          goto bad;
        }

      /* Do RPC to mountd. */

      //memset(&reply, 0, sizeof(reply));
      nvdbg("remote path %s\n", rpc->rc_path);
      memset(&mountd, 0, sizeof(mountd));
      mountd.rpath = rpc->rc_path;
      mountd.len =  sizeof(mountd.rpath);

      nvdbg("path %s\n", mountd.rpath );
      nvdbg("len %d\n", mountd.len);
      error = rpcclnt_request(rpc, RPCMNT_MOUNT, RPCPROG_MNT, RPCMNT_VER1,
                              reply, (FAR const void *)&mountd);
      if (error != 0)
        {
          goto bad;
        }

      mdata = (struct rpc_reply_mount *)reply;
      rpc->rc_fh = mdata->mount.fhandle;

      if (mdata->mount.problem)
        {
          ndbg("error mounting with the server %d\n", error);
          goto bad;
        }

      /* NFS port in the socket*/

      sa->sin_port = htons(NFS_PORT);
      error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));
      if (error)
        {
          ndbg("psock_connect NFS port returns %d\n", error);
          goto bad;
        }
    }

  /* Initialize other non-zero congestion variables */

  rpc->rc_srtt[0] = rpc->rc_srtt[1] = rpc->rc_srtt[2] = rpc->rc_srtt[3] = (RPC_TIMEO << 3);
  rpc->rc_sdrtt[0] = rpc->rc_sdrtt[1] = rpc->rc_sdrtt[2] = rpc->rc_sdrtt[3] = 0;
  rpc->rc_cwnd = RPC_MAXCWND / 2;       /* Initial send window */
  rpc->rc_sent = 0;
  rpc->rc_timeouts = 0;

  RPC_RETURN(0);

bad:
  rpcclnt_disconnect(rpc);
  RPC_RETURN(error);
}

/* Reconnect routine: Called when a connection is broken on a reliable
 * protocol. - clean up the old socket - nfs_connect() again - set
 * TASK_MUSTRESEND for all outstanding requests on mount point If this
 * fails the mount point is DEAD! nb: Must be called with the
 * nfs_sndlock() set on the mount point.
 */

#ifdef CONFIG_NFS_TCPIP
int rpcclnt_reconnect(struct rpctask *rep)
{
  struct rpctask *rp;
  struct rpcclnt *rpc = rep->r_rpcclnt;
  int error;

  rpcclnt_disconnect(rpc);
  while ((error = rpcclnt_connect(rpc)) != 0)
    {
      if (error == EINTR || error == ERESTART)
        {
          return EINTR;
        }
    }

  /* Loop through outstanding request list and fix up all
   * requests on old socket.
   */

  for (rp = (struct rpctask *)rpctask_q->head; rp != NULL;
       rp = (struct rpctask *)rp->r_chain.blink)
    {
      if (rp->r_rpcclnt == rpc)
        {
          rp->r_flags |= TASK_MUSTRESEND;
        }
    }

  return 0;
}
#endif

void rpcclnt_disconnect(struct rpcclnt *rpc)
{
  struct socket *so;

  if (rpc->rc_so != NULL)
    {
      so = rpc->rc_so;
      (void)psock_close(so);
    }
}

int rpcclnt_umount(struct rpcclnt *rpc)
{
  struct sockaddr *saddr;
  struct sockaddr_in *sa;
  void *reply;
  struct call_args_pmap sdata;
  struct rpc_reply_pmap *rdata;
  int error;

  saddr = rpc->rc_name;

  /* Do the RPC to get a dynamic bounding with the server using ppmap*/
  /* Get port number for MOUNTD. */

  sdata.prog = txdr_unsigned(RPCPROG_MNT);
  sdata.vers = txdr_unsigned(RPCMNT_VER1);
  sdata.proc = txdr_unsigned(IPPROTO_UDP);
  sdata.port = 0;

  memset(&reply, 0, sizeof(reply));

  error = rpcclnt_request(rpc, PMAPPROC_GETPORT, PMAPPROG, PMAPVERS,
                          &reply, &sdata);
  if (error != 0)
    {
      goto bad;
    }

  rdata = (struct rpc_reply_pmap *)reply;

  sa = (FAR struct sockaddr_in *)saddr;
  sa->sin_port = rdata->pmap.port;

  error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));

  if (error)
    {
      ndbg("psock_connect umount port returns %d\n", error);
      goto bad;
    }

  /* Do RPC to umountd. */

  //memset(&reply, 0, sizeof(reply));

  error = rpcclnt_request(rpc, RPCMNT_UMOUNT, RPCPROG_MNT, RPCMNT_VER1,
                          reply, &rpc->rc_path);
  if (error != 0)
    {
      goto bad;
    }

  RPC_RETURN(0);

bad:
  rpcclnt_disconnect(rpc);
  RPC_RETURN(error);
}

#ifdef CONFIG_NFS_TCPIP
void rpcclnt_safedisconnect(struct rpcclnt *rpc)
{
  struct rpctask dummytask;

  memset((void *)dummytask, 0, sizeof(*call));
  dummytask.r_rpcclnt = rpc;
  rpcclnt_rcvlock(&dummytask);
  rpcclnt_disconnect(rpc);
  rpcclnt_rcvunlock(&rpc->rc_flag);
}
#endif

/* Code from nfs_request - goes something like this - fill in task struct -
 * links task into list - calls nfs_send() for first transmit - calls
 * nfs_receive() to get reply - fills in reply (which should be initialized
 * prior to calling), which is valid when 0.
 *
 * note that reply->result_* are invalid unless reply->type ==
 * RPC_MSGACCEPTED and reply->status == RPC_SUCCESS and that reply->verf_*
 * are invalid unless reply->type == RPC_MSGACCEPTED
 */

int rpcclnt_request(struct rpcclnt *rpc, int procnum, int prog, int version,
                    void *dataout, FAR const void *datain)
{
  struct rpc_reply_header *replymgs;
  struct rpc_reply_header replyheader;
  struct rpctask *task = NULL;
  struct xidr value;
  struct rpc_call_pmap pmap;
  struct rpc_call_mount mountd;
  struct rpc_call_create create;
  struct rpc_call_write write;
  struct rpc_call_read read;
  struct rpc_call_remove removef;
  struct rpc_call_rename renamef;
  struct rpc_call_mkdir mkdir;
  struct rpc_call_rmdir rmdir;
  struct rpc_call_readdir readdir;
  struct rpc_call_fs fs;
  void *msgcall = NULL;
  int error = 0;

  memset(&replyheader, 0, sizeof(replyheader));

  if (prog == PMAPPROG)
    {
      if (procnum == PMAPPROC_GETPORT)
        {
          memset(&pmap, 0, sizeof(struct rpc_call_pmap));
          msgcall = &pmap;
        }
      else if (procnum == PMAPPROC_UNSET)
        {
          memset(&pmap, 0, sizeof(struct rpc_call_pmap));
          msgcall = &pmap;
        }
    }
  else if (prog == RPCPROG_MNT)
    {
      if (procnum == RPCMNT_UMOUNT)
        {
          memset(&mountd, 0, sizeof(struct rpc_call_mount));
          msgcall = &mountd;
        }
      else if (procnum == RPCMNT_MOUNT)
        {
          memset(&mountd, 0, sizeof(struct rpc_call_mount));
          msgcall = &mountd;
        }
    }
  else if (prog == NFS_PROG)
    {
      switch (procnum)
        {
        case NFSPROC_CREATE:
          {
            memset(&create, 0, sizeof(struct rpc_call_create));
            msgcall = &create;
          }
          break;

        case NFSPROC_READ:
          {
            memset(&read, 0, sizeof(struct rpc_call_read));
            msgcall = &read;
          }
          break;

        case NFSPROC_WRITE:
          {
            memset(&write, 0, sizeof(struct rpc_call_write));
            msgcall = &write;
          }
          break;

        case NFSPROC_RENAME:
          {
            memset(&renamef, 0, sizeof(struct rpc_call_rename));
            msgcall = &renamef;
          }
          break;

        case NFSPROC_REMOVE:
          {
            memset(&removef, 0, sizeof(struct rpc_call_remove));
            msgcall = &removef;
          }
          break;

        case NFSPROC_MKDIR:
          {
            memset(&mkdir, 0, sizeof(struct rpc_call_mkdir));
            msgcall = &mkdir;
          }
          break;

        case NFSPROC_RMDIR:
          {
            memset(&rmdir, 0, sizeof(struct rpc_call_rmdir));
            msgcall = &rmdir;
          }
          break;

        case NFSPROC_READDIR:
          {
            memset(&readdir, 0, sizeof(struct rpc_call_readdir));
            msgcall = &readdir;
          }
          break;

        case NFSPROC_FSSTAT:
          {
            memset(&fs, 0, sizeof(struct rpc_call_fs));
            msgcall = &fs;
          }
          break;

        case NFSPROC_FSINFO:
          {
            memset(&fs, 0, sizeof(struct rpc_call_fs));
            msgcall = &fs;
          }
          break;

        default:
          {
            error = ESRCH;
            goto rpcmout;
          }
        }
    }

  /* Create an instance of the task state structure */

  task = (struct rpctask *)kzalloc(sizeof(struct rpctask));
  if (!task)
    {
      fdbg("Failed to allocate reply msg structure\n");
      return -ENOMEM;
    }

  error = rpcclnt_buildheader(rpc, procnum, prog, version, &value, datain, msgcall);
  if (error)
    {
      ndbg("building call header error");
      goto rpcmout;
    }

  task->r_rpcclnt = rpc;
  task->r_xid = value.xid;
  task->r_procnum = procnum;

  if (rpc->rc_flag & RPCCLNT_SOFT)
    {
      task->r_retry = rpc->rc_retry;
    }
  else
    {
      task->r_retry = RPC_MAXREXMIT + 1;  /* past clip limit */
    }

  task->r_rtt = task->r_rexmit = 0;

  if (rpcclnt_proct(rpc, procnum) > 0)
    {
      task->r_flags = TASK_TIMING;
    }
  else
    {
      task->r_flags = 0;
    }

  /* Do the client side RPC. */

  rpcstats.rpcrequests++;

  /* Chain request into list of outstanding requests. Be sure to put it
   * LAST so timer finds oldest requests first.
   */

  dq_addlast(&task->r_chain, &rpctask_q);

  /* If backing off another request or avoiding congestion, don't send
   * this one now but let timer do it. If not timing a request, do it
   * now.
   */

  if (rpc->rc_so && (rpc->rc_sotype == SOCK_DGRAM ||
                     (rpc->rc_flag & RPCCLNT_DUMBTIMR) ||
                     rpc->rc_sent < rpc->rc_cwnd))
    {
#ifdef CONFIG_NFS_TCPIP
      if (rpc->rc_soflags & PR_CONNREQUIRED)
        {
          error = rpcclnt_sndlock(&rpc->rc_flag, task);
        }
#endif
      if (error == 0)
        {
          error = rpcclnt_send(rpc->rc_so, rpc->rc_name, procnum, prog,
                               msgcall, task);

#ifdef CONFIG_NFS_TCPIP
          if (rpc->rc_soflags & PR_CONNREQUIRED)
            {
              rpcclnt_sndunlock(&rpc->rc_flag);
            }
#endif
        }

      if (error == 0 && (task->r_flags & TASK_MUSTRESEND) == 0)
        {
          rpc->rc_sent += RPC_CWNDSCALE;
          task->r_flags |= TASK_SENT;
        }
    }
  else
    {
      task->r_rtt = -1;
    }

  /* Wait for the reply from our send. */

  if (error == 0 || error == EPIPE)
    {
      error = rpcclnt_reply(task, &replyheader, dataout);
    }
  nvdbg("out for reply %d\n", error);

  /* RPC done, unlink the request. */

  dq_rem(&task->r_chain, &rpctask_q);

  /* Decrement the outstanding request count. */

  if (task->r_flags & TASK_SENT)
    {
      task->r_flags &= ~TASK_SENT;      /* paranoia */
      rpc->rc_sent -= RPC_CWNDSCALE;
    }

  if (error != 0)
    {
      goto rpcmout;
    }

  /* Break down the rpc header and check if ok */

  nvdbg("Breakdown msg \n");
  memset(&replymgs, 0, sizeof(replymgs));
  replymgs->stat.type = fxdr_unsigned(uint32_t, replyheader.stat.type);
  if (replymgs->stat.type == RPC_MSGDENIED)
    {
      replymgs->stat.status = fxdr_unsigned(uint32_t, replyheader.stat.status);
      switch (replymgs->stat.status)
        {
        case RPC_MISMATCH:
          replymgs->stat.mismatch_info.low =
            fxdr_unsigned(uint32_t, replyheader.stat.mismatch_info.low);
          replymgs->stat.mismatch_info.high =
            fxdr_unsigned(uint32_t, replyheader.stat.mismatch_info.high);
          ndbg("RPC_MSGDENIED: RPC_MISMATCH error");
          error = EOPNOTSUPP;
          break;

        case RPC_AUTHERR:
          replymgs->stat.autherr = fxdr_unsigned(uint32_t, replyheader.stat.autherr);
          ndbg("RPC_MSGDENIED: RPC_AUTHERR error");
          error = EACCES;
          break;

        default:
          error = EOPNOTSUPP;
          break;
        }
      goto rpcmout;
    }
  else if (replymgs->stat.type != RPC_MSGACCEPTED)
    {
      error = EOPNOTSUPP;
      goto rpcmout;
    }

  /* Verifier */

  replymgs->rpc_verfi.authtype =
    fxdr_unsigned(enum auth_flavor, replyheader.rpc_verfi.authtype);
  replymgs->rpc_verfi.authlen =
    fxdr_unsigned(uint32_t, replyheader.rpc_verfi.authlen);

  if (replymgs->stat.status == RPC_SUCCESS)
    {
      nvdbg("RPC_SUCCESS");
    }
  else if (replymgs->stat.status == RPC_PROGMISMATCH)
    {
      replymgs->stat.mismatch_info.low =
        fxdr_unsigned(uint32_t, replyheader.stat.mismatch_info.low);
      replymgs->stat.mismatch_info.high =
        fxdr_unsigned(uint32_t, replyheader.stat.mismatch_info.high);

      ndbg("RPC_MSGACCEPTED: RPC_PROGMISMATCH error");
      error = EOPNOTSUPP;
    }
  else if (replymgs->stat.status > 5)
    {
      error = EOPNOTSUPP;
      goto rpcmout;
    }

rpcmout:
  kfree(task);
  RPC_RETURN(error);
}

#undef COMP
#ifdef COMP
/* Nfs timer routine Scan the nfsreq list and retranmit any requests that
 * have timed out To avoid retransmission attempts on STREAM sockets (in the
 * future) make sure to set the r_retry field to 0 (implies nm_retry == 0).
 */

void rpcclnt_timer(void *arg, struct rpc_call *call)
{
  struct rpctask *rep;
  struct socket *so;
  struct rpcclnt *rpc;
  int timeo, error;

  for (rep = (struct rpctask *)rpctask_q.head; rep != NULL;
       rep = (struct rpctask *)rep->r_chain.flink)
    {
      rpc = rep->r_rpcclnt;
      if (rep->r_flags & TASK_SOFTTERM)
        {
          continue;
        }

      if (rep->r_rtt >= 0)
        {
          rep->r_rtt++;
          if (rpc->rc_flag & RPCCLNT_DUMBTIMR)
            {
              timeo = rpc->rc_timeo;
            }
          else
            {
              timeo = RPC_RTO(rpc, rpcclnt_proct(rep->r_rpcclnt, rep->r_procnum));
            }

          if (rpc->rc_timeouts > 0)
            {
              timeo *= rpcclnt_backoff[rpc->rc_timeouts - 1];
            }

          if (rep->r_rtt <= timeo)
            {
              continue;
            }

          if (rpc->rc_timeouts < 8)
            {
              rpc->rc_timeouts++;
            }
        }

      /* Check for server not responding */

      if ((rep->r_flags & TASK_TPRINTFMSG) == 0 &&
          rep->r_rexmit > rpc->rc_deadthresh)
        {
          ndbg("Server is not responding");
          rep->r_flags |= TASK_TPRINTFMSG;
        }

      if (rep->r_rexmit >= rep->r_retry)
        {                       /* too many */
          rpcstats.rpctimeouts++;
          rep->r_flags |= TASK_SOFTTERM;
          continue;
        }

      if (rpc->rc_sotype != SOCK_DGRAM)
        {
          if (++rep->r_rexmit > RPC_MAXREXMIT)
            {
              rep->r_rexmit = RPC_MAXREXMIT;
            }
          continue;
        }

      if ((so = rpc->rc_so) == NULL)
        {
          continue;
        }

      /* If there is enough space and the window allows.. Resend it
       * Set r_rtt to -1 in case we fail to send it now.
       */

      rep->r_rtt = -1;
      if ((rpc->rc_flag & RPCCLNT_DUMBTIMR) || (rep->r_flags & TASK_SENT) ||
          rpc->rc_sent < rpc->rc_cwnd)
        {

          if ((rpc->rc_flag & RPCCLNT_NOCONN) == 0)
            {
              error = psock_sendto(so, call, sizeof(*call), 0, NULL, 0);
            }
          else
            {
              error = psock_sendto(so, call, sizeof(*call), 0, rpc->rc_name,
                                   sizeof(*rpc->rc_name));
            }

          if (!error)
            {
              /* Iff first send, start timing else turn
               * timing off, backoff timer and divide
               * congestion window by 2.
               */

              if (rep->r_flags & TASK_SENT)
                {
                  rep->r_flags &= ~TASK_TIMING;
                  if (++rep->r_rexmit > RPC_MAXREXMIT)
                    {
                      rep->r_rexmit = RPC_MAXREXMIT;
                    }

                  rpc->rc_cwnd >>= 1;
                  if (rpc->rc_cwnd < RPC_CWNDSCALE)
                    {
                      rpc->rc_cwnd = RPC_CWNDSCALE;
                    }

                  rpcstats.rpcretries++;
                }
              else
                {
                  rep->r_flags |= TASK_SENT;
                  rpc->rc_sent += RPC_CWNDSCALE;
                }

              rep->r_rtt = 0;
            }
        }
    }

  // rpcclnt_timer_handle = timeout(rpcclnt_timer, NULL, rpcclnt_ticks);
}
#endif

/* Build the RPC header and fill in the authorization info. */

int rpcclnt_buildheader(struct rpcclnt *rpc, int procid, int prog, int vers,
                        struct xidr *value, FAR const void *datain,
                        void *dataout)
{
  srand(time(NULL));
  int xidp = 0;

  /* The RPC header.*/

  /* Get a new (non-zero) xid */

  if ((rpcclnt_xid == 0) && (rpcclnt_xid_touched == 0))
    {
      rpcclnt_xid = rand();
      rpcclnt_xid_touched = 1;
    }
  else
    {
      do
        {
          xidp = rand();
        }
      while ((xidp % 256) == 0);

      rpcclnt_xid += xidp;
    }

  /* Perform the binding depending on the protocol type */

  if (prog == PMAPPROG)
    {
      if (procid == PMAPPROC_GETPORT)
        {
          struct rpc_call_pmap *callmsg = (struct rpc_call_pmap *)dataout;
          bcopy(datain, &callmsg->pmap, sizeof(struct call_args_pmap));

          callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
          value->xid = callmsg->ch.rp_xid;
          callmsg->ch.rp_direction = rpc_call;
          callmsg->ch.rp_rpcvers = rpc_vers;
          callmsg->ch.rp_prog = txdr_unsigned(prog);
          callmsg->ch.rp_vers = txdr_unsigned(vers);
          callmsg->ch.rp_proc = txdr_unsigned(procid);

          /* rpc_auth part (auth_unix as root) */

          callmsg->ch.rpc_auth.authtype = rpc_auth_null;
        //call->rpc_auth.authlen = 0;
#ifdef CONFIG_NFS_UNIX_AUTH
          callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
          callmsg->ch.rpc_unix.hostname = 0;
          callmsg->ch.rpc_unix.uid = setuid;
          callmsg->ch.rpc_unix.gid = setgid;
          callmsg->ch.rpc_unix.gidlist = 0;
#endif
          /* rpc_verf part (auth_null) */

          callmsg->ch.rpc_verf.authtype = rpc_auth_null;
        //call->rpc_verf.authlen = 0;
          return 0;
        }
      else if (procid == PMAPPROC_UNSET)
        {
          struct rpc_call_pmap *callmsg = (struct rpc_call_pmap *)dataout;;
          bcopy(datain, &callmsg->pmap, sizeof(struct call_args_pmap));
          callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
          value->xid = callmsg->ch.rp_xid;
          callmsg->ch.rp_direction = rpc_call;
          callmsg->ch.rp_rpcvers = rpc_vers;
          callmsg->ch.rp_prog = txdr_unsigned(prog);
          callmsg->ch.rp_vers = txdr_unsigned(vers);
          callmsg->ch.rp_proc = txdr_unsigned(procid);

          /* rpc_auth part (auth_unix as root) */

          callmsg->ch.rpc_auth.authtype = rpc_auth_null;
        //call->rpc_auth.authlen = 0;
#ifdef CONFIG_NFS_UNIX_AUTH
          callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
          callmsg->ch.rpc_unix.hostname = 0;
          callmsg->ch.rpc_unix.uid = setuid;
          callmsg->ch.rpc_unix.gid = setgid;
          callmsg->ch.rpc_unix.gidlist = 0;
#endif
          /* rpc_verf part (auth_null) */

          callmsg->ch.rpc_verf.authtype = rpc_auth_null;
        //call->rpc_verf.authlen = 0;
          return 0;
        }
    }
  else if (prog == RPCPROG_MNT)
    {
      if (procid == RPCMNT_UMOUNT)
        {
          struct rpc_call_mount *callmsg = (struct rpc_call_mount *)dataout;
          bcopy(datain, &callmsg->mount, sizeof(struct call_args_mount));
          callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
          value->xid = callmsg->ch.rp_xid;
          callmsg->ch.rp_direction = rpc_call;
          callmsg->ch.rp_rpcvers = rpc_vers;
          callmsg->ch.rp_prog = txdr_unsigned(prog);
          callmsg->ch.rp_vers = txdr_unsigned(vers);
          callmsg->ch.rp_proc = txdr_unsigned(procid);

          /* rpc_auth part (auth_unix as root) */

          callmsg->ch.rpc_auth.authtype = rpc_auth_null;
        //call->rpc_auth.authlen = 0;
#ifdef CONFIG_NFS_UNIX_AUTH
          callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
          callmsg->ch.rpc_unix.hostname = 0;
          callmsg->ch.rpc_unix.uid = setuid;
          callmsg->ch.rpc_unix.gid = setgid;
          callmsg->ch.rpc_unix.gidlist = 0;
#endif
          /* rpc_verf part (auth_null) */

          callmsg->ch.rpc_verf.authtype = rpc_auth_null;
        //call->rpc_verf.authlen = 0;
          return 0;
        }
      else if (procid == RPCMNT_MOUNT)
        {
          struct rpc_call_mount *callmsg = (struct rpc_call_mount *)dataout;
          bcopy(datain, &callmsg->mount, sizeof(struct call_args_mount));
          callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
          value->xid = callmsg->ch.rp_xid;
          callmsg->ch.rp_direction = rpc_call;
          callmsg->ch.rp_rpcvers = rpc_vers;
          callmsg->ch.rp_prog = txdr_unsigned(prog);
          callmsg->ch.rp_vers = txdr_unsigned(vers);
          callmsg->ch.rp_proc = txdr_unsigned(procid);

          /* rpc_auth part (auth_unix as root) */

          callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;
#ifdef CONFIG_NFS_UNIX_AUTH
          callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
          callmsg->ch.rpc_unix.hostname = 0;
          callmsg->ch.rpc_unix.uid = setuid;
          callmsg->ch.rpc_unix.gid = setgid;
          callmsg->ch.rpc_unix.gidlist = 0;
#endif
          /* rpc_verf part (auth_null) */

          callmsg->ch.rpc_verf.authtype = rpc_auth_null;
        //call->rpc_verf.authlen = 0;
          return 0;
        }
    }
  else if (prog == NFS_PROG)
    {
      switch (procid)
        {
        case NFSPROC_CREATE:
          {
            struct rpc_call_create *callmsg = (struct rpc_call_create *)dataout;
            bcopy(datain, &callmsg->create, sizeof(struct CREATE3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;
#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_READ:
          {
            struct rpc_call_read *callmsg = (struct rpc_call_read *)dataout;
            bcopy(datain, &callmsg->read, sizeof(struct READ3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_WRITE:
          {
            struct rpc_call_write *callmsg = (struct rpc_call_write *)dataout;
            bcopy(datain, &callmsg->write, sizeof(struct WRITE3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_READDIR:
          {
            struct rpc_call_readdir *callmsg = (struct rpc_call_readdir *)dataout;
            bcopy(datain, &callmsg->readdir, sizeof(struct READDIR3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_FSSTAT:
          {
            struct rpc_call_fs *callmsg = (struct rpc_call_fs *)dataout;
            bcopy(datain, &callmsg->fs, sizeof(struct FS3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_REMOVE:
          {
            struct rpc_call_remove *callmsg  = (struct rpc_call_remove *)dataout;
            bcopy(datain, &callmsg->remove, sizeof(struct REMOVE3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_MKDIR:
          {
            struct rpc_call_mkdir *callmsg = (struct rpc_call_mkdir *)dataout;
            bcopy(datain, &callmsg->mkdir, sizeof(struct MKDIR3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_RMDIR:
          {
            struct rpc_call_rmdir *callmsg  = (struct rpc_call_rmdir *)dataout;
            bcopy(datain, &callmsg->rmdir, sizeof(struct RMDIR3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_RENAME:
          {
            struct rpc_call_rename *callmsg  = (struct rpc_call_rename *)dataout;
            bcopy(datain, &callmsg->rename, sizeof(struct RENAME3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
            //call->rpc_verf.authlen = 0;
            return 0;
          }

        case NFSPROC_FSINFO:
          {
            struct rpc_call_fs *callmsg = (struct rpc_call_fs *)dataout;
            bcopy(datain, &callmsg->fs, sizeof(struct FS3args));
            callmsg->ch.rp_xid = txdr_unsigned(rpcclnt_xid);
            value->xid = callmsg->ch.rp_xid;
            callmsg->ch.rp_direction = rpc_call;
            callmsg->ch.rp_rpcvers = rpc_vers;
            callmsg->ch.rp_prog = txdr_unsigned(prog);
            callmsg->ch.rp_vers = txdr_unsigned(vers);
            callmsg->ch.rp_proc = txdr_unsigned(procid);

            /* rpc_auth part (auth_unix as root) */

            callmsg->ch.rpc_auth.authtype = rpc_auth_null;
          //call->rpc_auth.authlen = 0;

#ifdef CONFIG_NFS_UNIX_AUTH
            callmsg->ch.rpc_unix.stamp = txdr_unsigned(1);
            callmsg->ch.rpc_unix.hostname = 0;
            callmsg->ch.rpc_unix.uid = setuid;
            callmsg->ch.rpc_unix.gid = setgid;
            callmsg->ch.rpc_unix.gidlist = 0;
#endif
            /* rpc_verf part (auth_null) */

            callmsg->ch.rpc_verf.authtype = rpc_auth_null;
          //call->rpc_verf.authlen = 0;
            return 0;
          }

        default:
          break;
        }
    }

  return ESRCH;
}

#ifdef COMP
int rpcclnt_cancelreqs(struct rpcclnt *rpc)
{
  struct rpctask *task;
  int i;

  for (task = (struct rpctask *)rpctask_q.head; task;
       task = (struct rpctask *)task->r_chain.flink)
    {
      if (rpc != task->r_rpcclnt || (task->r_flags & TASK_SOFTTERM))
        {
          continue;
        }

      rpcclnt_softterm(task);
    }

  for (i = 0; i < 30; i++)
    {
      for (task = (struct rpctask *)&rpctask_q.head; task;
           task = (struct rpctask *)task->r_chain.flink)
        {
          if (rpc == task->r_rpcclnt)
            {
              break;
            }
        }

      if (task == NULL)
        {
          return 0;
        }
    }

  return EBUSY;
}
#endif
#endif