summaryrefslogtreecommitdiff
path: root/nuttx/fs/nfs/nfs_socket.c
blob: d659a097e5246f2eec6eef2fe5c4c9c3d61a7333 (plain) (blame)
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
/****************************************************************************
 * fs/nfs/nfs_socket.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
 * 
 * permission is granted to use, copy, create derivative works and redistribute
 * this software and such derivative works for any purpose, so long as the name
 * of the university of michigan is not used in any advertising or publicity
 * pertaining to the use or distribution of this software without specific,
 * written prior authorization.  if the above copyright notice or any other
 * identification of the university of michigan is included in any copy of any
 * portion of this software, then the disclaimer below must also be included.
 * 
 * this software is provided as is, without representation from the university
 * of michigan as to its fitness for any purpose, and without warranty by the
 * university of michigan of any kind, either express or implied, including
 * without limitation the implied warranties of merchantability and fitness for
 * a particular purpose. the regents of the university of michigan shall not be
 * liable for any damages, including special, indirect, incidental, or
 * consequential damages, with respect to any claim arising out of or in
 * connection with the use of the software, even if it has been or is hereafter
 * advised of the possibility of such damages.
 *
 ****************************************************************************/

/****************************************************************************
 * 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/fs/nfs.h>

#include "nfs.h"
#include "rpc.h"
#include "rpc_v2.h"
#include "nfs_proto.h"
#include "xdr_subs.h"
#include "nfs_mount.h"
#include "nfs_socket.h"

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

/* Flag translations */

#define nfsmnt_to_rpcclnt(nf, rf, name) do \
  {                                        \
    if (nf & NFSMNT_##name)                \
      {                                    \
        rf |= RPCCLNT_##name;              \
      }                                    \
  } while(0)

/****************************************************************************
 * Private Variables
 ****************************************************************************/

static struct rpc_program nfs3_program =
{
  NFS_PROG, NFS_VER3, "NFSv3"
};

/****************************************************************************
 * Public Variables
 ****************************************************************************/

uint32_t nfs_true;
uint32_t nfs_false;
uint32_t nfs_xdrneg1;
int nfs_ticks;
struct nfsstats nfsstats;

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

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

void nfs_init(void)
{
   nfs_true = txdr_unsigned(TRUE);
   nfs_false = txdr_unsigned(FALSE);
   nfs_xdrneg1 = txdr_unsigned(-1);

   nfs_ticks = (CLOCKS_PER_SEC * NFS_TICKINTVL + 500) / 1000;
   if (nfs_ticks < 1)
    {
      nfs_ticks = 1;
    }

   rpcclnt_init();
}

int nfs_connect(struct nfsmount *nmp)
{
  struct rpcclnt *rpc;
  int error = 0;

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

  rpc = nmp->nm_rpcclnt;

  rpc->rc_prog = &nfs3_program;

  nvdbg("nfsxconnect!\n");

  /* translate nfsmnt flags -> rpcclnt flags */

  rpc->rc_flag = 0;
  nfsmnt_to_rpcclnt(nmp->nm_flag, rpc->rc_flag, SOFT);
  nfsmnt_to_rpcclnt(nmp->nm_flag, rpc->rc_flag, INT);
  nfsmnt_to_rpcclnt(nmp->nm_flag, rpc->rc_flag, NOCONN);
  nfsmnt_to_rpcclnt(nmp->nm_flag, rpc->rc_flag, DUMBTIMR);

  //rpc->rc_flag |= RPCCLNT_REDIRECT;     /* Make this a mount option. */

  rpc->rc_authtype = RPCAUTH_NULL;      /* for now */
  //rpc->rc_servername = nmp->nm_mountp->mnt_stat.f_mntfromname;
  rpc->rc_name = nmp->nm_nam;

  rpc->rc_sotype = nmp->nm_sotype;
  rpc->rc_soproto = nmp->nm_soproto;
  rpc->rc_rsize = (nmp->nm_rsize > nmp->nm_readdirsize) ?
    nmp->nm_rsize : nmp->nm_readdirsize;
  rpc->rc_wsize = nmp->nm_wsize;
  rpc->rc_deadthresh = nmp->nm_deadthresh;
  rpc->rc_timeo = nmp->nm_timeo;
  rpc->rc_retry = nmp->nm_retry;

  /* XXX v2,3 need to use this */

  rpc->rc_proctlen = 0;
  rpc->rc_proct = NULL;

  if (error)
    {
      return error;
    }

  return rpcclnt_connect(rpc);
}

/* NFS disconnect. Clean up and unlink. */

void nfs_disconnect(struct nfsmount *nmp)
{
  rpcclnt_disconnect(nmp->nm_rpcclnt);
}

#ifdef CONFIG_NFS_TCPIP
void nfs_safedisconnect(struct nfsmount *nmp)
{
  rpcclnt_safedisconnect(nmp->nm_rpcclnt);
}
#endif

int nfs_request(struct nfsmount *nmp, int procnum, void *datain, void *dataout)
{
  int error;
  struct rpcclnt *clnt;
  struct rpc_reply *reply = NULL;
  int trylater_delay;

  clnt = nmp->nm_rpcclnt;

tryagain:

  memset(reply, 0, sizeof(struct rpc_reply));

  if ((error = rpcclnt_request(clnt, procnum, reply, datain)) != 0)
    {
      goto out;
    }

  dataout = reply->stat.where;

  if (reply->rpc_verfi.authtype != 0)
    {
      error = fxdr_unsigned(int, reply->rpc_verfi.authtype);

      if ((nmp->nm_flag & NFSMNT_NFSV3) && error == NFSERR_TRYLATER)
        {
          error = 0;
          trylater_delay *= NFS_TIMEOUTMUL;
          if (trylater_delay > NFS_MAXTIMEO)
            {
              trylater_delay = NFS_MAXTIMEO;
            }
          goto tryagain;
        }

      /* If the File Handle was stale, invalidate the
       * lookup cache, just in case.
       */

      if (error == ESTALE)
        {
          ndbg("%s: ESTALE on mount from server \n",
               nmp->nm_rpcclnt->rc_prog->prog_name);
        }
      else
        {
          ndbg("%s: unknown error %d from server \n",
               nmp->nm_rpcclnt->rc_prog->prog_name, error);
        }

      goto out;
    }
  return 0;
  
out:
  return error;
}

/* terminate any outstanding RPCs. */

int nfs_nmcancelreqs(struct nfsmount *nmp)
{
  return rpcclnt_cancelreqs(nmp->nm_rpcclnt);
}