summaryrefslogtreecommitdiff
path: root/nfs/rpc.c
blob: cbc5a198d3b1fad37105fd5b335f30a4fc8d6590 (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
/* SunRPC management for NFS client
   Copyright (C) 1994, 1995, 1996 Free Software Foundation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "nfs.h"

/* Needed in order to get the RPC header files to include correctly */
#undef TRUE
#undef FALSE
#define malloc spoiufasdf	/* Avoid bogus definition in rpc/types.h */

#include <rpc/types.h>
#include <rpc/xdr.h>
#include <rpc/auth.h>
#include <rpc/rpc_msg.h>
#include <rpc/auth_unix.h>

#undef malloc			/* Get rid protection.  */

#include <netinet/in.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>

/* One of these exists for each pending RPC. */
struct rpc_list
{
  struct rpc_list *next, **prevp;
  void *reply;
};

/* A list of all the pending RPCs. */
static struct rpc_list *outstanding_rpcs;

/* This lock must be held around any modifications to the list
   structure of outstanding_rpcs. */
static spin_lock_t rpc_list_lock = SPIN_LOCK_INITIALIZER;

/* Wake up this condition when an outstanding RPC has received a reply
   or we should check for timeouts. */
static struct condition rpc_wakeup = CONDITION_INITIALIZER;

/* This lock must be held around modifications of the REPLY members of 
   records on outstanding_rpcs and around uses of rpc_wakeup. */
static struct mutex outstanding_lock = MUTEX_INITIALIZER;



/* Generate and return a new transaction ID. */
static int
generate_xid ()
{
  static int nextxid;
  
  if (nextxid == 0)
    nextxid = mapped_time->seconds;
  
  return nextxid++;
}

/* Set up an RPC for procdeure RPC_PROC, for talking to the server
   PROGRAM of version VERSION.  Allocate storage with malloc and point
   *BUF at it; caller must free this when done.  Allocate at least LEN
   bytes more than the usual amount for an RPC.  Initialize the RPC
   credential structure with UID, GID, and SECOND_GID.  (Any of those
   may be -1 to indicate that it does not apply; exactly or two of UID
   and GID must be -1, however.) */
int *
initialize_rpc (int program, int version, int rpc_proc, 
		size_t len, void **bufp, 
		uid_t uid, gid_t gid, gid_t second_gid)
{
  void *buf = malloc (len + 1024);
  int *p, *lenaddr;
  struct rpc_list *hdr;

  /* First the struct rpc_list bit. */
  hdr = buf;
  hdr->reply = 0;
  
  p = buf + sizeof (struct rpc_list);

  /* RPC header */
  *p++ = htonl (generate_xid ());
  *p++ = htonl (CALL);
  *p++ = htonl (RPC_MSG_VERSION);
  *p++ = htonl (program);
  *p++ = htonl (version);
  *p++ = htonl (rpc_proc);
  
  assert ((uid == -1) == (gid == -1));

  if (uid != -1)
    {
      *p++ = htonl (AUTH_UNIX);
      lenaddr = p++;
      *p++ = htonl (mapped_time->seconds);
      p = xdr_encode_string (p, hostname);
      *p++ = htonl (uid);
      *p++ = htonl (gid);
      if (second_gid != -1)
	{
	  *p++ = htonl (1);
	  *p++ = htonl (second_gid);
	}
      else
	*p++ = 0;
      *lenaddr = htonl ((p - (lenaddr + 1)) * sizeof (int));
    }
  else
    {
      *p++ = htonl (AUTH_NULL);
      *p++ = 0;
    }
        
  /* VERF field */
  *p++ = htonl (AUTH_NULL);
  *p++ = 0;
  
  *bufp = buf;
  return p;
}

/* Remove HDR from the list of pending RPC's. */
static void
unlink_rpc (struct rpc_list *hdr)
{
  spin_lock (&rpc_list_lock);
  *hdr->prevp = hdr->next;
  if (hdr->next)
    hdr->next->prevp = hdr->prevp;
  spin_unlock (&rpc_list_lock);
}

/* Send the specified RPC message.  *RPCBUF is the initialized buffer
   from a previous initialize_rpc call; *PP points past the filled
   in args.  Set *PP to the address of the reply contents themselves.
   The user will be expected to free *RPCBUF (which will have changed)
   when done with the reply contents.  The old value of *RPCBUF will
   be freed by this routine. */
error_t
conduct_rpc (void **rpcbuf, int **pp)
{
  struct rpc_list *hdr = *rpcbuf;
  error_t err;
  size_t cc, nc;
  int timeout = initial_transmit_timeout;
  time_t lasttrans;
  int ntransmit = 0;
  int *p;
  int xid;
  int n;
  int cancel;
  
  /* Link it in */
  spin_lock (&rpc_list_lock);
  hdr->next = outstanding_rpcs;
  if (hdr->next)
    hdr->next->prevp = &hdr->next;
  hdr->prevp = &outstanding_rpcs;
  outstanding_rpcs = hdr;
  spin_unlock (&rpc_list_lock);

  xid = * (int *) (*rpcbuf + sizeof (struct rpc_list));

  do
    {
      /* If we've sent enough, give up */
      if (mounted_soft && ntransmit == soft_retries)
	{
	  unlink_rpc (hdr);
	  return ETIMEDOUT;
	}

      /* Issue the RPC */
      mutex_lock (&outstanding_lock);
      lasttrans = mapped_time->seconds;
      ntransmit++;
      nc = (void *) *pp - *rpcbuf - sizeof (struct rpc_list);
      cc = write (main_udp_socket, *rpcbuf + sizeof (struct rpc_list), nc);
      if (cc == -1)
	{
	  mutex_unlock (&outstanding_lock);
	  unlink_rpc (hdr);
	  return errno;
	}
      else 
	assert (cc == nc);
      
      /* Wait for reply */
      cancel = 0;
      while (!hdr->reply
	     && (mapped_time->seconds - lasttrans < timeout)
	     && !cancel)
	cancel = hurd_condition_wait (&rpc_wakeup, &outstanding_lock);
      mutex_unlock (&outstanding_lock);
  
      if (cancel)
	{
	  unlink_rpc (hdr);
	  return EINTR;
	}

      if (!hdr->reply)
	{
	  timeout *=2;
	  if (timeout > max_transmit_timeout)
	    timeout = max_transmit_timeout;
	}
    }
  while (!hdr->reply);

  /* Switch to the reply buffer. */
  *rpcbuf = hdr->reply;
  free (hdr);

  /* Process the reply, dissecting errors.  When we're done, set *PP to
     the rpc return contents, if there is no error. */
  p = (int *) *rpcbuf;
  
  assert (*p == xid);
  p++;
  
  switch (ntohl (*p++))
    {
    default:
      err = EBADRPC;
      break;
      
    case REPLY:
      switch (ntohl (*p++))
	{
	default:
	  err = EBADRPC;
	  break;
	  
	case MSG_DENIED:
	  switch (ntohl (*p++))
	    {
	    default:
	      err = EBADRPC;
	      break;
	      
	    case RPC_MISMATCH:
	      err = ERPCMISMATCH;
	      break;
	      	      
	    case AUTH_ERROR:
	      switch (ntohl (*p++))
		{
		case AUTH_BADCRED:
		case AUTH_REJECTEDCRED:
		  err = EAUTH;
		  break;

		case AUTH_TOOWEAK:
		  err = ENEEDAUTH;
		  break;
		  		  
		case AUTH_BADVERF:
		case AUTH_REJECTEDVERF:
		default:
		  err = EBADRPC;
		  break;
		}
	      break;
	    }
	  break;
	  
	case MSG_ACCEPTED:

	  /* Process VERF field. */
	  p++;			/* skip verf type */
	  n = ntohl (*p++);	/* size of verf */
	  p += INTSIZE (n);	/* skip verf itself */

	  switch (ntohl (*p++))
	    {
	    default:
	    case GARBAGE_ARGS:
	      err = EBADRPC;
	      break;

	    case PROG_UNAVAIL:
	      err = EPROGUNAVAIL;
	      break;
	      
	    case PROG_MISMATCH:
	      err = EPROGMISMATCH;
	      break;
	      
	    case PROC_UNAVAIL:
	      err = EPROCUNAVAIL;
	      break;

	    case SUCCESS:
	      *pp = p;
	      err = 0;
	      break;
	    }
	  break;
	}
      break;
    }
  return err;
}

/* Dedicated thread to wakeup rpc_wakeup once a second. */
void
timeout_service_thread ()
{
  while (1)
    {
      sleep (1);
      mutex_lock (&outstanding_lock);
      condition_broadcast (&rpc_wakeup);
      mutex_unlock (&outstanding_lock);
    }
}

/* Dedicate thread to receive RPC replies, register them on the queue
   of pending wakeups, and deal appropriately. */
void
rpc_receive_thread ()
{
  int cc;
  void *buf;
  struct rpc_list *r;
  int xid;

  while (1)
    {
      buf = malloc (1024 + read_size);

      do
	{
	  cc = read (main_udp_socket, buf, 1024 + read_size);
	  if (cc == -1)
	    {
	      perror ("nfs read");
	      r = 0;
	    }
	  else
	    {
	      xid = *(int *)buf;
	      spin_lock (&rpc_list_lock);
	      for (r = outstanding_rpcs; r; r = r->next)
		{
		  if (* (int *) &r[1] == xid)
		    {
		      /* Remove it from the list */
		      *r->prevp = r->next;
		      if (r->next)
			r->next->prevp = r->prevp;
		      spin_unlock (&rpc_list_lock);

		      mutex_lock (&outstanding_lock);
		      r->reply = buf;
		      condition_broadcast (&rpc_wakeup);
		      mutex_unlock (&outstanding_lock);
		      break;
		    }
		}
	      if (!r)
		{
		  spin_unlock (&rpc_list_lock);
		  fprintf (stderr, "NFS dropping reply xid %d\n", xid);
		}
	    }
	}
      while (!r);
    }
}