1 /* $OpenBSD: nfs_socket.c,v 1.42 2005/04/02 01:00:38 mickey Exp $ */
2 /* $NetBSD: nfs_socket.c,v 1.27 1996/04/15 20:20:00 thorpej Exp $ */
3
4 /*
5 * Copyright (c) 1989, 1991, 1993, 1995
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Rick Macklem at The University of Guelph.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
36 */
37
38 /*
39 * Socket operations for use by nfs
40 */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/mount.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/vnode.h>
49 #include <sys/domain.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/syslog.h>
54 #include <sys/tprintf.h>
55 #include <sys/namei.h>
56
57 #include <netinet/in.h>
58 #include <netinet/tcp.h>
59
60 #include <nfs/rpcv2.h>
61 #include <nfs/nfsproto.h>
62 #include <nfs/nfs.h>
63 #include <nfs/xdr_subs.h>
64 #include <nfs/nfsm_subs.h>
65 #include <nfs/nfsmount.h>
66 #include <nfs/nfsnode.h>
67 #include <nfs/nfsrtt.h>
68 #include <nfs/nfs_var.h>
69
70 #define TRUE 1
71 #define FALSE 0
72
73 /*
74 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
75 * Use the mean and mean deviation of rtt for the appropriate type of rpc
76 * for the frequent rpcs and a default for the others.
77 * The justification for doing "other" this way is that these rpcs
78 * happen so infrequently that timer est. would probably be stale.
79 * Also, since many of these rpcs are
80 * non-idempotent, a conservative timeout is desired.
81 * getattr, lookup - A+2D
82 * read, write - A+4D
83 * other - nm_timeo
84 */
85 #define NFS_RTO(n, t) \
86 ((t) == 0 ? (n)->nm_timeo : \
87 ((t) < 3 ? \
88 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
89 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
90 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
91 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
92 /*
93 * External data, mostly RPC constants in XDR form
94 */
95 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
96 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
97 rpc_auth_kerb;
98 extern u_int32_t nfs_prog;
99 extern struct nfsstats nfsstats;
100 extern int nfsv3_procid[NFS_NPROCS];
101 extern int nfs_ticks;
102
103 /*
104 * Defines which timer to use for the procnum.
105 * 0 - default
106 * 1 - getattr
107 * 2 - lookup
108 * 3 - read
109 * 4 - write
110 */
111 static int proct[NFS_NPROCS] = {
112 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
113 0, 0, 0,
114 };
115
116 /*
117 * There is a congestion window for outstanding rpcs maintained per mount
118 * point. The cwnd size is adjusted in roughly the way that:
119 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
120 * SIGCOMM '88". ACM, August 1988.
121 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
122 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
123 * of rpcs is in progress.
124 * (The sent count and cwnd are scaled for integer arith.)
125 * Variants of "slow start" were tried and were found to be too much of a
126 * performance hit (ave. rtt 3 times larger),
127 * I suspect due to the large rtt that nfs rpcs have.
128 */
129 #define NFS_CWNDSCALE 256
130 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
131 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
132 int nfsrtton = 0;
133 struct nfsrtt nfsrtt;
134
135 void nfs_realign(struct mbuf **, int);
136 unsigned int nfs_realign_test = 0;
137 unsigned int nfs_realign_count = 0;
138
139 struct nfsreqhead nfs_reqq;
140
141 /*
142 * Initialize sockets and congestion for a new NFS connection.
143 * We do not free the sockaddr if error.
144 */
145 int
nfs_connect(nmp,rep)146 nfs_connect(nmp, rep)
147 struct nfsmount *nmp;
148 struct nfsreq *rep;
149 {
150 struct socket *so;
151 int s, error, rcvreserve, sndreserve;
152 struct sockaddr *saddr;
153 struct sockaddr_in *sin;
154 struct mbuf *m;
155
156 nmp->nm_so = (struct socket *)0;
157 saddr = mtod(nmp->nm_nam, struct sockaddr *);
158 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
159 nmp->nm_soproto);
160 if (error)
161 goto bad;
162 so = nmp->nm_so;
163 nmp->nm_soflags = so->so_proto->pr_flags;
164
165 /*
166 * Some servers require that the client port be a reserved port number.
167 * We always allocate a reserved port, as this prevents filehandle
168 * disclosure through UDP port capture.
169 */
170 if (saddr->sa_family == AF_INET) {
171 struct mbuf *mopt;
172 int *ip;
173
174 MGET(mopt, M_WAIT, MT_SOOPTS);
175 mopt->m_len = sizeof(int);
176 ip = mtod(mopt, int *);
177 *ip = IP_PORTRANGE_LOW;
178 error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
179 if (error)
180 goto bad;
181
182 MGET(m, M_WAIT, MT_SONAME);
183 sin = mtod(m, struct sockaddr_in *);
184 sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
185 sin->sin_family = AF_INET;
186 sin->sin_addr.s_addr = INADDR_ANY;
187 sin->sin_port = htons(0);
188 error = sobind(so, m);
189 m_freem(m);
190 if (error)
191 goto bad;
192
193 MGET(mopt, M_WAIT, MT_SOOPTS);
194 mopt->m_len = sizeof(int);
195 ip = mtod(mopt, int *);
196 *ip = IP_PORTRANGE_DEFAULT;
197 error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
198 if (error)
199 goto bad;
200 }
201
202 /*
203 * Protocols that do not require connections may be optionally left
204 * unconnected for servers that reply from a port other than NFS_PORT.
205 */
206 if (nmp->nm_flag & NFSMNT_NOCONN) {
207 if (nmp->nm_soflags & PR_CONNREQUIRED) {
208 error = ENOTCONN;
209 goto bad;
210 }
211 } else {
212 error = soconnect(so, nmp->nm_nam);
213 if (error)
214 goto bad;
215
216 /*
217 * Wait for the connection to complete. Cribbed from the
218 * connect system call but with the wait timing out so
219 * that interruptible mounts don't hang here for a long time.
220 */
221 s = splsoftnet();
222 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
223 (void) tsleep((caddr_t)&so->so_timeo, PSOCK,
224 "nfscon", 2 * hz);
225 if ((so->so_state & SS_ISCONNECTING) &&
226 so->so_error == 0 && rep &&
227 (error = nfs_sigintr(nmp, rep, rep->r_procp)) != 0){
228 so->so_state &= ~SS_ISCONNECTING;
229 splx(s);
230 goto bad;
231 }
232 }
233 if (so->so_error) {
234 error = so->so_error;
235 so->so_error = 0;
236 splx(s);
237 goto bad;
238 }
239 splx(s);
240 }
241 /*
242 * Always set receive timeout to detect server crash and reconnect.
243 * Otherwise, we can get stuck in soreceive forever.
244 */
245 so->so_rcv.sb_timeo = (5 * hz);
246 if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT))
247 so->so_snd.sb_timeo = (5 * hz);
248 else
249 so->so_snd.sb_timeo = 0;
250 if (nmp->nm_sotype == SOCK_DGRAM) {
251 sndreserve = nmp->nm_wsize + NFS_MAXPKTHDR;
252 rcvreserve = max(nmp->nm_rsize, nmp->nm_readdirsize) +
253 NFS_MAXPKTHDR;
254 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
255 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
256 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
257 NFS_MAXPKTHDR) * 2;
258 } else {
259 if (nmp->nm_sotype != SOCK_STREAM)
260 panic("nfscon sotype");
261 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
262 MGET(m, M_WAIT, MT_SOOPTS);
263 *mtod(m, int32_t *) = 1;
264 m->m_len = sizeof(int32_t);
265 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
266 }
267 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
268 MGET(m, M_WAIT, MT_SOOPTS);
269 *mtod(m, int32_t *) = 1;
270 m->m_len = sizeof(int32_t);
271 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
272 }
273 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
274 sizeof (u_int32_t)) * 2;
275 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
276 sizeof (u_int32_t)) * 2;
277 }
278 error = soreserve(so, sndreserve, rcvreserve);
279 if (error)
280 goto bad;
281 so->so_rcv.sb_flags |= SB_NOINTR;
282 so->so_snd.sb_flags |= SB_NOINTR;
283
284 /* Initialize other non-zero congestion variables */
285 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] =
286 nmp->nm_srtt[4] = (NFS_TIMEO << 3);
287 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
288 nmp->nm_sdrtt[3] = nmp->nm_sdrtt[4] = 0;
289 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
290 nmp->nm_sent = 0;
291 nmp->nm_timeouts = 0;
292 return (0);
293
294 bad:
295 nfs_disconnect(nmp);
296 return (error);
297 }
298
299 /*
300 * Reconnect routine:
301 * Called when a connection is broken on a reliable protocol.
302 * - clean up the old socket
303 * - nfs_connect() again
304 * - set R_MUSTRESEND for all outstanding requests on mount point
305 * If this fails the mount point is DEAD!
306 * nb: Must be called with the nfs_sndlock() set on the mount point.
307 */
308 int
nfs_reconnect(rep)309 nfs_reconnect(rep)
310 struct nfsreq *rep;
311 {
312 struct nfsreq *rp;
313 struct nfsmount *nmp = rep->r_nmp;
314 int error;
315
316 nfs_disconnect(nmp);
317 while ((error = nfs_connect(nmp, rep)) != 0) {
318 if (error == EINTR || error == ERESTART)
319 return (EINTR);
320 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
321 }
322
323 /*
324 * Loop through outstanding request list and fix up all requests
325 * on old socket.
326 */
327 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
328 if (rp->r_nmp == nmp) {
329 rp->r_flags |= R_MUSTRESEND;
330 rp->r_rexmit = 0;
331 }
332 }
333 return (0);
334 }
335
336 /*
337 * NFS disconnect. Clean up and unlink.
338 */
339 void
nfs_disconnect(nmp)340 nfs_disconnect(nmp)
341 struct nfsmount *nmp;
342 {
343 struct socket *so;
344
345 if (nmp->nm_so) {
346 so = nmp->nm_so;
347 nmp->nm_so = (struct socket *)0;
348 soshutdown(so, 2);
349 soclose(so);
350 }
351 }
352
353 /*
354 * This is the nfs send routine. For connection based socket types, it
355 * must be called with an nfs_sndlock() on the socket.
356 * "rep == NULL" indicates that it has been called from a server.
357 * For the client side:
358 * - return EINTR if the RPC is terminated, 0 otherwise
359 * - set R_MUSTRESEND if the send fails for any reason
360 * - do any cleanup required by recoverable socket errors (???)
361 * For the server side:
362 * - return EINTR or ERESTART if interrupted by a signal
363 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
364 * - do any cleanup required by recoverable socket errors (???)
365 */
366 int
nfs_send(so,nam,top,rep)367 nfs_send(so, nam, top, rep)
368 struct socket *so;
369 struct mbuf *nam;
370 struct mbuf *top;
371 struct nfsreq *rep;
372 {
373 struct mbuf *sendnam;
374 int error, soflags, flags;
375
376 if (rep) {
377 if (rep->r_flags & R_SOFTTERM) {
378 m_freem(top);
379 return (EINTR);
380 }
381 if ((so = rep->r_nmp->nm_so) == NULL) {
382 rep->r_flags |= R_MUSTRESEND;
383 m_freem(top);
384 return (0);
385 }
386 rep->r_flags &= ~R_MUSTRESEND;
387 soflags = rep->r_nmp->nm_soflags;
388 } else
389 soflags = so->so_proto->pr_flags;
390 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
391 sendnam = (struct mbuf *)0;
392 else
393 sendnam = nam;
394 if (so->so_type == SOCK_SEQPACKET)
395 flags = MSG_EOR;
396 else
397 flags = 0;
398
399 error = sosend(so, sendnam, (struct uio *)0, top,
400 (struct mbuf *)0, flags);
401 if (error) {
402 if (rep) {
403 log(LOG_INFO, "nfs send error %d for server %s\n",error,
404 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
405 /*
406 * Deal with errors for the client side.
407 */
408 if (rep->r_flags & R_SOFTTERM)
409 error = EINTR;
410 else
411 rep->r_flags |= R_MUSTRESEND;
412 } else
413 log(LOG_INFO, "nfsd send error %d\n", error);
414
415 /*
416 * Handle any recoverable (soft) socket errors here. (???)
417 */
418 if (error != EINTR && error != ERESTART &&
419 error != EWOULDBLOCK && error != EPIPE)
420 error = 0;
421 }
422 return (error);
423 }
424
425 #ifdef NFSCLIENT
426 /*
427 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
428 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
429 * Mark and consolidate the data into a new mbuf list.
430 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
431 * small mbufs.
432 * For SOCK_STREAM we must be very careful to read an entire record once
433 * we have read any of it, even if the system call has been interrupted.
434 */
435 int
nfs_receive(rep,aname,mp)436 nfs_receive(rep, aname, mp)
437 struct nfsreq *rep;
438 struct mbuf **aname;
439 struct mbuf **mp;
440 {
441 struct socket *so;
442 struct uio auio;
443 struct iovec aio;
444 struct mbuf *m;
445 struct mbuf *control;
446 u_int32_t len;
447 struct mbuf **getnam;
448 int error, sotype, rcvflg;
449 struct proc *p = curproc; /* XXX */
450
451 /*
452 * Set up arguments for soreceive()
453 */
454 *mp = (struct mbuf *)0;
455 *aname = (struct mbuf *)0;
456 sotype = rep->r_nmp->nm_sotype;
457
458 /*
459 * For reliable protocols, lock against other senders/receivers
460 * in case a reconnect is necessary.
461 * For SOCK_STREAM, first get the Record Mark to find out how much
462 * more there is to get.
463 * We must lock the socket against other receivers
464 * until we have an entire rpc request/reply.
465 */
466 if (sotype != SOCK_DGRAM) {
467 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
468 if (error)
469 return (error);
470 tryagain:
471 /*
472 * Check for fatal errors and resending request.
473 */
474 /*
475 * Ugh: If a reconnect attempt just happened, nm_so
476 * would have changed. NULL indicates a failed
477 * attempt that has essentially shut down this
478 * mount point.
479 */
480 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
481 nfs_sndunlock(&rep->r_nmp->nm_flag);
482 return (EINTR);
483 }
484 so = rep->r_nmp->nm_so;
485 if (!so) {
486 error = nfs_reconnect(rep);
487 if (error) {
488 nfs_sndunlock(&rep->r_nmp->nm_flag);
489 return (error);
490 }
491 goto tryagain;
492 }
493 while (rep->r_flags & R_MUSTRESEND) {
494 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
495 nfsstats.rpcretries++;
496 rep->r_rtt = 0;
497 rep->r_flags &= ~R_TIMING;
498 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
499 if (error) {
500 if (error == EINTR || error == ERESTART ||
501 (error = nfs_reconnect(rep)) != 0) {
502 nfs_sndunlock(&rep->r_nmp->nm_flag);
503 return (error);
504 }
505 goto tryagain;
506 }
507 }
508 nfs_sndunlock(&rep->r_nmp->nm_flag);
509 if (sotype == SOCK_STREAM) {
510 aio.iov_base = (caddr_t) &len;
511 aio.iov_len = sizeof(u_int32_t);
512 auio.uio_iov = &aio;
513 auio.uio_iovcnt = 1;
514 auio.uio_segflg = UIO_SYSSPACE;
515 auio.uio_rw = UIO_READ;
516 auio.uio_offset = 0;
517 auio.uio_resid = sizeof(u_int32_t);
518 auio.uio_procp = p;
519 do {
520 rcvflg = MSG_WAITALL;
521 error = soreceive(so, (struct mbuf **)0, &auio,
522 (struct mbuf **)0, (struct mbuf **)0, &rcvflg);
523 if (error == EWOULDBLOCK && rep) {
524 if (rep->r_flags & R_SOFTTERM)
525 return (EINTR);
526 /*
527 * looks like the server died after it
528 * received the request, make sure
529 * that we will retransmit and we
530 * don't get stuck here forever.
531 */
532 if (rep->r_rexmit >= rep->r_nmp->nm_retry) {
533 nfsstats.rpctimeouts++;
534 error = EPIPE;
535 }
536 }
537 } while (error == EWOULDBLOCK);
538 if (!error && auio.uio_resid > 0) {
539 log(LOG_INFO,
540 "short receive (%ld/%ld) from nfs server %s\n",
541 sizeof(u_int32_t) - auio.uio_resid,
542 sizeof(u_int32_t),
543 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
544 error = EPIPE;
545 }
546 if (error)
547 goto errout;
548
549 len = ntohl(len) & ~0x80000000;
550 /*
551 * This is SERIOUS! We are out of sync with the sender
552 * and forcing a disconnect/reconnect is all I can do.
553 */
554 if (len > NFS_MAXPACKET) {
555 log(LOG_ERR, "%s (%d) from nfs server %s\n",
556 "impossible packet length",
557 len,
558 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
559 error = EFBIG;
560 goto errout;
561 }
562 auio.uio_resid = len;
563 do {
564 rcvflg = MSG_WAITALL;
565 error = soreceive(so, (struct mbuf **)0,
566 &auio, mp, (struct mbuf **)0, &rcvflg);
567 } while (error == EWOULDBLOCK || error == EINTR ||
568 error == ERESTART);
569 if (!error && auio.uio_resid > 0) {
570 log(LOG_INFO,
571 "short receive (%ld/%d) from nfs server %s\n",
572 len - auio.uio_resid, len,
573 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
574 error = EPIPE;
575 }
576 } else {
577 /*
578 * NB: Since uio_resid is big, MSG_WAITALL is ignored
579 * and soreceive() will return when it has either a
580 * control msg or a data msg.
581 * We have no use for control msg., but must grab them
582 * and then throw them away so we know what is going
583 * on.
584 */
585 auio.uio_resid = len = 100000000; /* Anything Big */
586 auio.uio_procp = p;
587 do {
588 rcvflg = 0;
589 error = soreceive(so, (struct mbuf **)0,
590 &auio, mp, &control, &rcvflg);
591 if (control)
592 m_freem(control);
593 if (error == EWOULDBLOCK && rep) {
594 if (rep->r_flags & R_SOFTTERM)
595 return (EINTR);
596 }
597 } while (error == EWOULDBLOCK ||
598 (!error && *mp == NULL && control));
599 if ((rcvflg & MSG_EOR) == 0)
600 printf("Egad!!\n");
601 if (!error && *mp == NULL)
602 error = EPIPE;
603 len -= auio.uio_resid;
604 }
605 errout:
606 if (error && error != EINTR && error != ERESTART) {
607 m_freem(*mp);
608 *mp = (struct mbuf *)0;
609 if (error != EPIPE)
610 log(LOG_INFO,
611 "receive error %d from nfs server %s\n",
612 error,
613 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
614 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
615 if (!error) {
616 error = nfs_reconnect(rep);
617 if (!error)
618 goto tryagain;
619 nfs_sndunlock(&rep->r_nmp->nm_flag);
620 }
621 }
622 } else {
623 if ((so = rep->r_nmp->nm_so) == NULL)
624 return (EACCES);
625 if (so->so_state & SS_ISCONNECTED)
626 getnam = (struct mbuf **)0;
627 else
628 getnam = aname;
629 auio.uio_resid = len = 1000000;
630 auio.uio_procp = p;
631 do {
632 rcvflg = 0;
633 error = soreceive(so, getnam, &auio, mp,
634 (struct mbuf **)0, &rcvflg);
635 if (error == EWOULDBLOCK &&
636 (rep->r_flags & R_SOFTTERM))
637 return (EINTR);
638 } while (error == EWOULDBLOCK);
639 len -= auio.uio_resid;
640 }
641 if (error) {
642 m_freem(*mp);
643 *mp = (struct mbuf *)0;
644 }
645 /*
646 * Search for any mbufs that are not a multiple of 4 bytes long
647 * or with m_data not longword aligned.
648 * These could cause pointer alignment problems, so copy them to
649 * well aligned mbufs.
650 */
651 nfs_realign(mp, 5 * NFSX_UNSIGNED);
652 return (error);
653 }
654
655 /*
656 * Implement receipt of reply on a socket.
657 * We must search through the list of received datagrams matching them
658 * with outstanding requests using the xid, until ours is found.
659 */
660 /* ARGSUSED */
661 int
nfs_reply(myrep)662 nfs_reply(myrep)
663 struct nfsreq *myrep;
664 {
665 struct nfsreq *rep;
666 struct nfsmount *nmp = myrep->r_nmp;
667 int32_t t1;
668 struct mbuf *mrep, *nam, *md;
669 u_int32_t rxid, *tl;
670 caddr_t dpos, cp2;
671 int error;
672
673 /*
674 * Loop around until we get our own reply
675 */
676 for (;;) {
677 /*
678 * Lock against other receivers so that I don't get stuck in
679 * sbwait() after someone else has received my reply for me.
680 * Also necessary for connection based protocols to avoid
681 * race conditions during a reconnect.
682 */
683 error = nfs_rcvlock(myrep);
684 if (error)
685 return (error);
686 /* Already received, bye bye */
687 if (myrep->r_mrep != NULL) {
688 nfs_rcvunlock(&nmp->nm_flag);
689 return (0);
690 }
691 /*
692 * Get the next Rpc reply off the socket
693 */
694 error = nfs_receive(myrep, &nam, &mrep);
695 nfs_rcvunlock(&nmp->nm_flag);
696 if (error) {
697
698 /*
699 * Ignore routing errors on connectionless protocols??
700 */
701 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
702 nmp->nm_so->so_error = 0;
703 if (myrep->r_flags & R_GETONEREP)
704 return (0);
705 continue;
706 }
707 return (error);
708 }
709 if (nam)
710 m_freem(nam);
711
712 /*
713 * Get the xid and check that it is an rpc reply
714 */
715 md = mrep;
716 dpos = mtod(md, caddr_t);
717 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
718 rxid = *tl++;
719 if (*tl != rpc_reply) {
720 nfsstats.rpcinvalid++;
721 m_freem(mrep);
722 nfsmout:
723 if (myrep->r_flags & R_GETONEREP)
724 return (0);
725 continue;
726 }
727
728 /*
729 * Loop through the request list to match up the reply
730 * Iff no match, just drop the datagram
731 */
732 for (rep = TAILQ_FIRST(&nfs_reqq); rep != NULL;
733 rep = TAILQ_NEXT(rep, r_chain)) {
734 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
735 /* Found it.. */
736 rep->r_mrep = mrep;
737 rep->r_md = md;
738 rep->r_dpos = dpos;
739 if (nfsrtton) {
740 struct rttl *rt;
741
742 rt = &nfsrtt.rttl[nfsrtt.pos];
743 rt->proc = rep->r_procnum;
744 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
745 rt->sent = nmp->nm_sent;
746 rt->cwnd = nmp->nm_cwnd;
747 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
748 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
749 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
750 rt->tstamp = time;
751 if (rep->r_flags & R_TIMING)
752 rt->rtt = rep->r_rtt;
753 else
754 rt->rtt = 1000000;
755 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
756 }
757 /*
758 * Update congestion window.
759 * Do the additive increase of
760 * one rpc/rtt.
761 */
762 if (nmp->nm_cwnd <= nmp->nm_sent) {
763 nmp->nm_cwnd +=
764 (NFS_CWNDSCALE * NFS_CWNDSCALE +
765 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
766 if (nmp->nm_cwnd > NFS_MAXCWND)
767 nmp->nm_cwnd = NFS_MAXCWND;
768 }
769 rep->r_flags &= ~R_SENT;
770 nmp->nm_sent -= NFS_CWNDSCALE;
771 /*
772 * Update rtt using a gain of 0.125 on the mean
773 * and a gain of 0.25 on the deviation.
774 */
775 if (rep->r_flags & R_TIMING) {
776 /*
777 * Since the timer resolution of
778 * NFS_HZ is so course, it can often
779 * result in r_rtt == 0. Since
780 * r_rtt == N means that the actual
781 * rtt is between N+dt and N+2-dt ticks,
782 * add 1.
783 */
784 t1 = rep->r_rtt + 1;
785 t1 -= (NFS_SRTT(rep) >> 3);
786 NFS_SRTT(rep) += t1;
787 if (t1 < 0)
788 t1 = -t1;
789 t1 -= (NFS_SDRTT(rep) >> 2);
790 NFS_SDRTT(rep) += t1;
791 }
792 nmp->nm_timeouts = 0;
793 break;
794 }
795 }
796 /*
797 * If not matched to a request, drop it.
798 * If it's mine, get out.
799 */
800 if (rep == 0) {
801 nfsstats.rpcunexpected++;
802 m_freem(mrep);
803 } else if (rep == myrep) {
804 if (rep->r_mrep == NULL)
805 panic("nfsreply nil");
806 return (0);
807 }
808 if (myrep->r_flags & R_GETONEREP)
809 return (0);
810 }
811 }
812
813 /*
814 * nfs_request - goes something like this
815 * - fill in request struct
816 * - links it into list
817 * - calls nfs_send() for first transmit
818 * - calls nfs_receive() to get reply
819 * - break down rpc header and return with nfs reply pointed to
820 * by mrep or error
821 * nb: always frees up mreq mbuf list
822 */
823 int
nfs_request(vp,mrest,procnum,procp,cred,mrp,mdp,dposp)824 nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp)
825 struct vnode *vp;
826 struct mbuf *mrest;
827 int procnum;
828 struct proc *procp;
829 struct ucred *cred;
830 struct mbuf **mrp;
831 struct mbuf **mdp;
832 caddr_t *dposp;
833 {
834 struct mbuf *m, *mrep;
835 struct nfsreq *rep;
836 u_int32_t *tl;
837 int i;
838 struct nfsmount *nmp;
839 struct mbuf *md, *mheadend;
840 char nickv[RPCX_NICKVERF];
841 time_t reqtime, waituntil;
842 caddr_t dpos, cp2;
843 int t1, s, error = 0, mrest_len, auth_len, auth_type;
844 int trylater_delay = 15, trylater_cnt = 0, failed_auth = 0;
845 int verf_len, verf_type;
846 u_int32_t xid;
847 char *auth_str, *verf_str;
848 NFSKERBKEY_T key; /* save session key */
849
850 nmp = VFSTONFS(vp->v_mount);
851 rep = pool_get(&nfsreqpl, PR_WAITOK);
852 rep->r_nmp = nmp;
853 rep->r_vp = vp;
854 rep->r_procp = procp;
855 rep->r_procnum = procnum;
856 i = 0;
857 m = mrest;
858 while (m) {
859 i += m->m_len;
860 m = m->m_next;
861 }
862 mrest_len = i;
863
864 /*
865 * Get the RPC header with authorization.
866 */
867 kerbauth:
868 verf_str = auth_str = (char *)0;
869 if (nmp->nm_flag & NFSMNT_KERB) {
870 verf_str = nickv;
871 verf_len = sizeof (nickv);
872 auth_type = RPCAUTH_KERB4;
873 bzero((caddr_t)key, sizeof (key));
874 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
875 &auth_len, verf_str, verf_len)) {
876 error = nfs_getauth(nmp, rep, cred, &auth_str,
877 &auth_len, verf_str, &verf_len, key);
878 if (error) {
879 pool_put(&nfsreqpl, rep);
880 m_freem(mrest);
881 return (error);
882 }
883 }
884 } else {
885 auth_type = RPCAUTH_UNIX;
886 auth_len = (((cred->cr_ngroups > nmp->nm_numgrps) ?
887 nmp->nm_numgrps : cred->cr_ngroups) << 2) +
888 5 * NFSX_UNSIGNED;
889 }
890 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
891 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
892 if (auth_str)
893 free(auth_str, M_TEMP);
894
895 /*
896 * For stream protocols, insert a Sun RPC Record Mark.
897 */
898 if (nmp->nm_sotype == SOCK_STREAM) {
899 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
900 *mtod(m, u_int32_t *) = htonl(0x80000000 |
901 (m->m_pkthdr.len - NFSX_UNSIGNED));
902 }
903 rep->r_mreq = m;
904 rep->r_xid = xid;
905 tryagain:
906 if (nmp->nm_flag & NFSMNT_SOFT)
907 rep->r_retry = nmp->nm_retry;
908 else
909 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
910 rep->r_rtt = rep->r_rexmit = 0;
911 if (proct[procnum] > 0)
912 rep->r_flags = R_TIMING;
913 else
914 rep->r_flags = 0;
915 rep->r_mrep = NULL;
916
917 /*
918 * Do the client side RPC.
919 */
920 nfsstats.rpcrequests++;
921 /*
922 * Chain request into list of outstanding requests. Be sure
923 * to put it LAST so timer finds oldest requests first.
924 */
925 s = splsoftnet();
926 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
927
928 /* Get send time for nqnfs */
929 reqtime = time.tv_sec;
930
931 /*
932 * If backing off another request or avoiding congestion, don't
933 * send this one now but let timer do it. If not timing a request,
934 * do it now.
935 */
936 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
937 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
938 nmp->nm_sent < nmp->nm_cwnd)) {
939 splx(s);
940 if (nmp->nm_soflags & PR_CONNREQUIRED)
941 error = nfs_sndlock(&nmp->nm_flag, rep);
942 if (!error) {
943 error = nfs_send(nmp->nm_so, nmp->nm_nam,
944 m_copym(m, 0, M_COPYALL, M_WAIT),
945 rep);
946 if (nmp->nm_soflags & PR_CONNREQUIRED)
947 nfs_sndunlock(&nmp->nm_flag);
948 }
949 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
950 nmp->nm_sent += NFS_CWNDSCALE;
951 rep->r_flags |= R_SENT;
952 }
953 } else {
954 splx(s);
955 rep->r_rtt = -1;
956 }
957
958 /*
959 * Wait for the reply from our send or the timer's.
960 */
961 if (!error || error == EPIPE)
962 error = nfs_reply(rep);
963
964 /*
965 * RPC done, unlink the request.
966 */
967 s = splsoftnet();
968 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
969 splx(s);
970
971 /*
972 * Decrement the outstanding request count.
973 */
974 if (rep->r_flags & R_SENT) {
975 rep->r_flags &= ~R_SENT; /* paranoia */
976 nmp->nm_sent -= NFS_CWNDSCALE;
977 }
978
979 /*
980 * If there was a successful reply and a tprintf msg.
981 * tprintf a response.
982 */
983 if (!error && (rep->r_flags & R_TPRINTFMSG))
984 nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
985 "is alive again");
986 mrep = rep->r_mrep;
987 md = rep->r_md;
988 dpos = rep->r_dpos;
989 if (error) {
990 m_freem(rep->r_mreq);
991 pool_put(&nfsreqpl, rep);
992 return (error);
993 }
994
995 /*
996 * break down the rpc header and check if ok
997 */
998 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
999 if (*tl++ == rpc_msgdenied) {
1000 if (*tl == rpc_mismatch)
1001 error = EOPNOTSUPP;
1002 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1003 if (!failed_auth) {
1004 failed_auth++;
1005 mheadend->m_next = (struct mbuf *)0;
1006 m_freem(mrep);
1007 m_freem(rep->r_mreq);
1008 goto kerbauth;
1009 } else
1010 error = EAUTH;
1011 } else
1012 error = EACCES;
1013 m_freem(mrep);
1014 m_freem(rep->r_mreq);
1015 pool_put(&nfsreqpl, rep);
1016 return (error);
1017 }
1018
1019 /*
1020 * Grab any Kerberos verifier, otherwise just throw it away.
1021 */
1022 verf_type = fxdr_unsigned(int, *tl++);
1023 i = fxdr_unsigned(int32_t, *tl);
1024 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1025 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1026 if (error)
1027 goto nfsmout;
1028 } else if (i > 0)
1029 nfsm_adv(nfsm_rndup(i));
1030 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1031 /* 0 == ok */
1032 if (*tl == 0) {
1033 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1034 if (*tl != 0) {
1035 error = fxdr_unsigned(int, *tl);
1036 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1037 error == NFSERR_TRYLATER) {
1038 m_freem(mrep);
1039 error = 0;
1040 waituntil = time.tv_sec + trylater_delay;
1041 while (time.tv_sec < waituntil)
1042 (void) tsleep((caddr_t)&lbolt,
1043 PSOCK, "nqnfstry", 0);
1044 trylater_delay *= nfs_backoff[trylater_cnt];
1045 if (trylater_cnt < 7)
1046 trylater_cnt++;
1047 goto tryagain;
1048 }
1049
1050 /*
1051 * If the File Handle was stale, invalidate the
1052 * lookup cache, just in case.
1053 */
1054 if (error == ESTALE)
1055 cache_purge(vp);
1056 if (nmp->nm_flag & NFSMNT_NFSV3) {
1057 *mrp = mrep;
1058 *mdp = md;
1059 *dposp = dpos;
1060 error |= NFSERR_RETERR;
1061 } else
1062 m_freem(mrep);
1063 m_freem(rep->r_mreq);
1064 pool_put(&nfsreqpl, rep);
1065 return (error);
1066 }
1067
1068 *mrp = mrep;
1069 *mdp = md;
1070 *dposp = dpos;
1071 m_freem(rep->r_mreq);
1072 pool_put(&nfsreqpl, rep);
1073 return (0);
1074 }
1075 m_freem(mrep);
1076 error = EPROTONOSUPPORT;
1077 nfsmout:
1078 m_freem(rep->r_mreq);
1079 pool_put(&nfsreqpl, rep);
1080 return (error);
1081 }
1082 #endif /* NFSCLIENT */
1083
1084 /*
1085 * Generate the rpc reply header
1086 * siz arg. is used to decide if adding a cluster is worthwhile
1087 */
1088 int
nfs_rephead(siz,nd,slp,err,frev,mrq,mbp,bposp)1089 nfs_rephead(siz, nd, slp, err, frev, mrq, mbp, bposp)
1090 int siz;
1091 struct nfsrv_descript *nd;
1092 struct nfssvc_sock *slp;
1093 int err;
1094 u_quad_t *frev;
1095 struct mbuf **mrq;
1096 struct mbuf **mbp;
1097 caddr_t *bposp;
1098 {
1099 u_int32_t *tl;
1100 struct mbuf *mreq;
1101 caddr_t bpos;
1102 struct mbuf *mb, *mb2;
1103
1104 MGETHDR(mreq, M_WAIT, MT_DATA);
1105 mb = mreq;
1106 /*
1107 * If this is a big reply, use a cluster else
1108 * try and leave leading space for the lower level headers.
1109 */
1110 siz += RPC_REPLYSIZ;
1111 if (siz >= max_datalen) {
1112 MCLGET(mreq, M_WAIT);
1113 } else
1114 mreq->m_data += max_hdr;
1115 tl = mtod(mreq, u_int32_t *);
1116 mreq->m_len = 6 * NFSX_UNSIGNED;
1117 bpos = ((caddr_t)tl) + mreq->m_len;
1118 *tl++ = txdr_unsigned(nd->nd_retxid);
1119 *tl++ = rpc_reply;
1120 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1121 *tl++ = rpc_msgdenied;
1122 if (err & NFSERR_AUTHERR) {
1123 *tl++ = rpc_autherr;
1124 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1125 mreq->m_len -= NFSX_UNSIGNED;
1126 bpos -= NFSX_UNSIGNED;
1127 } else {
1128 *tl++ = rpc_mismatch;
1129 *tl++ = txdr_unsigned(RPC_VER2);
1130 *tl = txdr_unsigned(RPC_VER2);
1131 }
1132 } else {
1133 *tl++ = rpc_msgaccepted;
1134
1135 /*
1136 * For Kerberos authentication, we must send the nickname
1137 * verifier back, otherwise just RPCAUTH_NULL.
1138 */
1139 if (nd->nd_flag & ND_KERBFULL) {
1140 struct nfsuid *nuidp;
1141 struct timeval ktvin, ktvout;
1142
1143 LIST_FOREACH(nuidp, NUIDHASH(slp, nd->nd_cr.cr_uid),
1144 nu_hash) {
1145 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1146 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1147 &nuidp->nu_haddr, nd->nd_nam2)))
1148 break;
1149 }
1150 if (nuidp) {
1151 ktvin.tv_sec =
1152 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1153 ktvin.tv_usec =
1154 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1155
1156 *tl++ = rpc_auth_kerb;
1157 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1158 *tl = ktvout.tv_sec;
1159 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1160 *tl++ = ktvout.tv_usec;
1161 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1162 } else {
1163 *tl++ = 0;
1164 *tl++ = 0;
1165 }
1166 } else {
1167 *tl++ = 0;
1168 *tl++ = 0;
1169 }
1170 switch (err) {
1171 case EPROGUNAVAIL:
1172 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1173 break;
1174 case EPROGMISMATCH:
1175 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1176 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1177 *tl++ = txdr_unsigned(2);
1178 *tl = txdr_unsigned(3);
1179 break;
1180 case EPROCUNAVAIL:
1181 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1182 break;
1183 case EBADRPC:
1184 *tl = txdr_unsigned(RPC_GARBAGE);
1185 break;
1186 default:
1187 *tl = 0;
1188 if (err != NFSERR_RETVOID) {
1189 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1190 if (err)
1191 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1192 else
1193 *tl = 0;
1194 }
1195 break;
1196 };
1197 }
1198
1199 *mrq = mreq;
1200 if (mbp != NULL)
1201 *mbp = mb;
1202 *bposp = bpos;
1203 if (err != 0 && err != NFSERR_RETVOID)
1204 nfsstats.srvrpc_errs++;
1205 return (0);
1206 }
1207
1208 /*
1209 * Nfs timer routine
1210 * Scan the nfsreq list and retranmit any requests that have timed out
1211 * To avoid retransmission attempts on STREAM sockets (in the future) make
1212 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1213 */
1214 void
nfs_timer(arg)1215 nfs_timer(arg)
1216 void *arg;
1217 {
1218 struct timeout *to = (struct timeout *)arg;
1219 struct nfsreq *rep;
1220 struct mbuf *m;
1221 struct socket *so;
1222 struct nfsmount *nmp;
1223 int timeo;
1224 int s, error;
1225 #ifdef NFSSERVER
1226 struct nfssvc_sock *slp;
1227 u_quad_t cur_usec;
1228 #endif
1229
1230 s = splsoftnet();
1231 for (rep = TAILQ_FIRST(&nfs_reqq); rep != NULL;
1232 rep = TAILQ_NEXT(rep, r_chain)) {
1233 nmp = rep->r_nmp;
1234 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1235 continue;
1236 if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1237 rep->r_flags |= R_SOFTTERM;
1238 continue;
1239 }
1240 if (rep->r_rtt >= 0) {
1241 rep->r_rtt++;
1242 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1243 timeo = nmp->nm_timeo;
1244 else
1245 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1246 if (nmp->nm_timeouts > 0)
1247 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1248 if (rep->r_rtt <= timeo)
1249 continue;
1250 if (nmp->nm_timeouts < 8)
1251 nmp->nm_timeouts++;
1252 }
1253 /*
1254 * Check for server not responding
1255 */
1256 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1257 rep->r_rexmit > nmp->nm_deadthresh) {
1258 nfs_msg(rep->r_procp,
1259 nmp->nm_mountp->mnt_stat.f_mntfromname,
1260 "not responding");
1261 rep->r_flags |= R_TPRINTFMSG;
1262 }
1263 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1264 nfsstats.rpctimeouts++;
1265 rep->r_flags |= R_SOFTTERM;
1266 continue;
1267 }
1268 if (nmp->nm_sotype != SOCK_DGRAM) {
1269 if (++rep->r_rexmit > NFS_MAXREXMIT)
1270 rep->r_rexmit = NFS_MAXREXMIT;
1271 continue;
1272 }
1273 if ((so = nmp->nm_so) == NULL)
1274 continue;
1275
1276 /*
1277 * If there is enough space and the window allows..
1278 * Resend it
1279 * Set r_rtt to -1 in case we fail to send it now.
1280 */
1281 rep->r_rtt = -1;
1282 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1283 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1284 (rep->r_flags & R_SENT) ||
1285 nmp->nm_sent < nmp->nm_cwnd) &&
1286 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1287 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1288 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1289 (struct mbuf *)0, (struct mbuf *)0);
1290 else
1291 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1292 nmp->nm_nam, (struct mbuf *)0);
1293 if (error) {
1294 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1295 so->so_error = 0;
1296 } else {
1297 /*
1298 * Iff first send, start timing
1299 * else turn timing off, backoff timer
1300 * and divide congestion window by 2.
1301 */
1302 if (rep->r_flags & R_SENT) {
1303 rep->r_flags &= ~R_TIMING;
1304 if (++rep->r_rexmit > NFS_MAXREXMIT)
1305 rep->r_rexmit = NFS_MAXREXMIT;
1306 nmp->nm_cwnd >>= 1;
1307 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1308 nmp->nm_cwnd = NFS_CWNDSCALE;
1309 nfsstats.rpcretries++;
1310 } else {
1311 rep->r_flags |= R_SENT;
1312 nmp->nm_sent += NFS_CWNDSCALE;
1313 }
1314 rep->r_rtt = 0;
1315 }
1316 }
1317 }
1318
1319 #ifdef NFSSERVER
1320 /*
1321 * Scan the write gathering queues for writes that need to be
1322 * completed now.
1323 */
1324 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
1325 for (slp = TAILQ_FIRST(&nfssvc_sockhead); slp != NULL;
1326 slp = TAILQ_NEXT(slp, ns_chain)) {
1327 if (LIST_FIRST(&slp->ns_tq) &&
1328 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
1329 nfsrv_wakenfsd(slp);
1330 }
1331 #endif /* NFSSERVER */
1332 splx(s);
1333 timeout_add(to, nfs_ticks);
1334 }
1335
1336 /*
1337 * Test for a termination condition pending on the process.
1338 * This is used for NFSMNT_INT mounts.
1339 */
1340 int
nfs_sigintr(nmp,rep,p)1341 nfs_sigintr(nmp, rep, p)
1342 struct nfsmount *nmp;
1343 struct nfsreq *rep;
1344 struct proc *p;
1345 {
1346
1347 if (rep && (rep->r_flags & R_SOFTTERM))
1348 return (EINTR);
1349 if (!(nmp->nm_flag & NFSMNT_INT))
1350 return (0);
1351 if (p && p->p_siglist &&
1352 (((p->p_siglist & ~p->p_sigmask) & ~p->p_sigignore) &
1353 NFSINT_SIGMASK))
1354 return (EINTR);
1355 return (0);
1356 }
1357
1358 /*
1359 * Lock a socket against others.
1360 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1361 * and also to avoid race conditions between the processes with nfs requests
1362 * in progress when a reconnect is necessary.
1363 */
1364 int
nfs_sndlock(flagp,rep)1365 nfs_sndlock(flagp, rep)
1366 int *flagp;
1367 struct nfsreq *rep;
1368 {
1369 struct proc *p;
1370 int slpflag = 0, slptimeo = 0;
1371
1372 if (rep) {
1373 p = rep->r_procp;
1374 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1375 slpflag = PCATCH;
1376 } else
1377 p = (struct proc *)0;
1378 while (*flagp & NFSMNT_SNDLOCK) {
1379 if (nfs_sigintr(rep->r_nmp, rep, p))
1380 return (EINTR);
1381 *flagp |= NFSMNT_WANTSND;
1382 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1383 slptimeo);
1384 if (slpflag == PCATCH) {
1385 slpflag = 0;
1386 slptimeo = 2 * hz;
1387 }
1388 }
1389 *flagp |= NFSMNT_SNDLOCK;
1390 return (0);
1391 }
1392
1393 /*
1394 * Unlock the stream socket for others.
1395 */
1396 void
nfs_sndunlock(flagp)1397 nfs_sndunlock(flagp)
1398 int *flagp;
1399 {
1400
1401 if ((*flagp & NFSMNT_SNDLOCK) == 0)
1402 panic("nfs sndunlock");
1403 *flagp &= ~NFSMNT_SNDLOCK;
1404 if (*flagp & NFSMNT_WANTSND) {
1405 *flagp &= ~NFSMNT_WANTSND;
1406 wakeup((caddr_t)flagp);
1407 }
1408 }
1409
1410 int
nfs_rcvlock(rep)1411 nfs_rcvlock(rep)
1412 struct nfsreq *rep;
1413 {
1414 int *flagp = &rep->r_nmp->nm_flag;
1415 int slpflag, slptimeo = 0;
1416
1417 if (*flagp & NFSMNT_INT)
1418 slpflag = PCATCH;
1419 else
1420 slpflag = 0;
1421 while (*flagp & NFSMNT_RCVLOCK) {
1422 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp))
1423 return (EINTR);
1424 *flagp |= NFSMNT_WANTRCV;
1425 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk",
1426 slptimeo);
1427 if (slpflag == PCATCH) {
1428 slpflag = 0;
1429 slptimeo = 2 * hz;
1430 }
1431 }
1432 *flagp |= NFSMNT_RCVLOCK;
1433 return (0);
1434 }
1435
1436 /*
1437 * Unlock the stream socket for others.
1438 */
1439 void
nfs_rcvunlock(flagp)1440 nfs_rcvunlock(flagp)
1441 int *flagp;
1442 {
1443
1444 if ((*flagp & NFSMNT_RCVLOCK) == 0)
1445 panic("nfs rcvunlock");
1446 *flagp &= ~NFSMNT_RCVLOCK;
1447 if (*flagp & NFSMNT_WANTRCV) {
1448 *flagp &= ~NFSMNT_WANTRCV;
1449 wakeup((caddr_t)flagp);
1450 }
1451 }
1452
1453 /*
1454 * NFS parsing code requires 32-bit alignment
1455 */
1456 void
nfs_realign(struct mbuf ** pm,int hsiz)1457 nfs_realign(struct mbuf **pm, int hsiz)
1458 {
1459 struct mbuf *m;
1460 struct mbuf *n = NULL;
1461 int off = 0;
1462
1463 ++nfs_realign_test;
1464 while ((m = *pm) != NULL) {
1465 if ((m->m_len & 0x3) || (mtod(m, long) & 0x3)) {
1466 MGET(n, M_WAIT, MT_DATA);
1467 if (m->m_len >= MINCLSIZE) {
1468 MCLGET(n, M_WAIT);
1469 }
1470 n->m_len = 0;
1471 break;
1472 }
1473 pm = &m->m_next;
1474 }
1475 /*
1476 * If n is non-NULL, loop on m copying data, then replace the
1477 * portion of the chain that had to be realigned.
1478 */
1479 if (n != NULL) {
1480 ++nfs_realign_count;
1481 while (m) {
1482 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1483 off += m->m_len;
1484 m = m->m_next;
1485 }
1486 m_freem(*pm);
1487 *pm = n;
1488 }
1489 }
1490
1491
1492 /*
1493 * Parse an RPC request
1494 * - verify it
1495 * - fill in the cred struct.
1496 */
1497 int
nfs_getreq(nd,nfsd,has_header)1498 nfs_getreq(nd, nfsd, has_header)
1499 struct nfsrv_descript *nd;
1500 struct nfsd *nfsd;
1501 int has_header;
1502 {
1503 int len, i;
1504 u_int32_t *tl;
1505 int32_t t1;
1506 struct uio uio;
1507 struct iovec iov;
1508 caddr_t dpos, cp2, cp;
1509 u_int32_t nfsvers, auth_type;
1510 uid_t nickuid;
1511 int error = 0, ticklen;
1512 struct mbuf *mrep, *md;
1513 struct nfsuid *nuidp;
1514 struct timeval tvin, tvout;
1515
1516 mrep = nd->nd_mrep;
1517 md = nd->nd_md;
1518 dpos = nd->nd_dpos;
1519 if (has_header) {
1520 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1521 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1522 if (*tl++ != rpc_call) {
1523 m_freem(mrep);
1524 return (EBADRPC);
1525 }
1526 } else
1527 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1528 nd->nd_repstat = 0;
1529 nd->nd_flag = 0;
1530 if (*tl++ != rpc_vers) {
1531 nd->nd_repstat = ERPCMISMATCH;
1532 nd->nd_procnum = NFSPROC_NOOP;
1533 return (0);
1534 }
1535 if (*tl != nfs_prog) {
1536 nd->nd_repstat = EPROGUNAVAIL;
1537 nd->nd_procnum = NFSPROC_NOOP;
1538 return (0);
1539 }
1540 tl++;
1541 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1542 if (nfsvers != NFS_VER2 && nfsvers != NFS_VER3) {
1543 nd->nd_repstat = EPROGMISMATCH;
1544 nd->nd_procnum = NFSPROC_NOOP;
1545 return (0);
1546 }
1547 if (nfsvers == NFS_VER3)
1548 nd->nd_flag = ND_NFSV3;
1549 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1550 if (nd->nd_procnum == NFSPROC_NULL)
1551 return (0);
1552 if (nd->nd_procnum >= NFS_NPROCS ||
1553 (nd->nd_procnum > NFSPROC_COMMIT) ||
1554 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1555 nd->nd_repstat = EPROCUNAVAIL;
1556 nd->nd_procnum = NFSPROC_NOOP;
1557 return (0);
1558 }
1559 if ((nd->nd_flag & ND_NFSV3) == 0)
1560 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1561 auth_type = *tl++;
1562 len = fxdr_unsigned(int, *tl++);
1563 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1564 m_freem(mrep);
1565 return (EBADRPC);
1566 }
1567
1568 nd->nd_flag &= ~ND_KERBAUTH;
1569 /*
1570 * Handle auth_unix or auth_kerb.
1571 */
1572 if (auth_type == rpc_auth_unix) {
1573 len = fxdr_unsigned(int, *++tl);
1574 if (len < 0 || len > NFS_MAXNAMLEN) {
1575 m_freem(mrep);
1576 return (EBADRPC);
1577 }
1578 nfsm_adv(nfsm_rndup(len));
1579 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1580 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
1581 nd->nd_cr.cr_ref = 1;
1582 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1583 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1584 len = fxdr_unsigned(int, *tl);
1585 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1586 m_freem(mrep);
1587 return (EBADRPC);
1588 }
1589 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1590 for (i = 0; i < len; i++)
1591 if (i < NGROUPS)
1592 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1593 else
1594 tl++;
1595 nd->nd_cr.cr_ngroups = (len > NGROUPS) ? NGROUPS : len;
1596 if (nd->nd_cr.cr_ngroups > 1)
1597 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
1598 len = fxdr_unsigned(int, *++tl);
1599 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1600 m_freem(mrep);
1601 return (EBADRPC);
1602 }
1603 if (len > 0)
1604 nfsm_adv(nfsm_rndup(len));
1605 } else if (auth_type == rpc_auth_kerb) {
1606 switch (fxdr_unsigned(int, *tl++)) {
1607 case RPCAKN_FULLNAME:
1608 ticklen = fxdr_unsigned(int, *tl);
1609 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1610 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1611 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1612 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1613 m_freem(mrep);
1614 return (EBADRPC);
1615 }
1616 uio.uio_offset = 0;
1617 uio.uio_iov = &iov;
1618 uio.uio_iovcnt = 1;
1619 uio.uio_segflg = UIO_SYSSPACE;
1620 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
1621 iov.iov_len = RPCAUTH_MAXSIZ - 4;
1622 nfsm_mtouio(&uio, uio.uio_resid);
1623 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1624 if (*tl++ != rpc_auth_kerb ||
1625 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1626 printf("Bad kerb verifier\n");
1627 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1628 nd->nd_procnum = NFSPROC_NOOP;
1629 return (0);
1630 }
1631 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
1632 tl = (u_int32_t *)cp;
1633 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1634 printf("Not fullname kerb verifier\n");
1635 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1636 nd->nd_procnum = NFSPROC_NOOP;
1637 return (0);
1638 }
1639 cp += NFSX_UNSIGNED;
1640 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
1641 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1642 nd->nd_flag |= ND_KERBFULL;
1643 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1644 break;
1645 case RPCAKN_NICKNAME:
1646 if (len != 2 * NFSX_UNSIGNED) {
1647 printf("Kerb nickname short\n");
1648 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1649 nd->nd_procnum = NFSPROC_NOOP;
1650 return (0);
1651 }
1652 nickuid = fxdr_unsigned(uid_t, *tl);
1653 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1654 if (*tl++ != rpc_auth_kerb ||
1655 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1656 printf("Kerb nick verifier bad\n");
1657 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1658 nd->nd_procnum = NFSPROC_NOOP;
1659 return (0);
1660 }
1661 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1662 tvin.tv_sec = *tl++;
1663 tvin.tv_usec = *tl;
1664
1665 LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
1666 nu_hash) {
1667 if (nuidp->nu_cr.cr_uid == nickuid &&
1668 (!nd->nd_nam2 ||
1669 netaddr_match(NU_NETFAM(nuidp),
1670 &nuidp->nu_haddr, nd->nd_nam2)))
1671 break;
1672 }
1673 if (!nuidp) {
1674 nd->nd_repstat =
1675 (NFSERR_AUTHERR|AUTH_REJECTCRED);
1676 nd->nd_procnum = NFSPROC_NOOP;
1677 return (0);
1678 }
1679
1680 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1681 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1682 if (nuidp->nu_expire < time.tv_sec ||
1683 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1684 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1685 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1686 nuidp->nu_expire = 0;
1687 nd->nd_repstat =
1688 (NFSERR_AUTHERR|AUTH_REJECTVERF);
1689 nd->nd_procnum = NFSPROC_NOOP;
1690 return (0);
1691 }
1692 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
1693 nd->nd_flag |= ND_KERBNICK;
1694 };
1695 } else {
1696 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1697 nd->nd_procnum = NFSPROC_NOOP;
1698 return (0);
1699 }
1700
1701 nd->nd_md = md;
1702 nd->nd_dpos = dpos;
1703 return (0);
1704 nfsmout:
1705 return (error);
1706 }
1707
1708 int
nfs_msg(p,server,msg)1709 nfs_msg(p, server, msg)
1710 struct proc *p;
1711 char *server, *msg;
1712 {
1713 tpr_t tpr;
1714
1715 if (p)
1716 tpr = tprintf_open(p);
1717 else
1718 tpr = NULL;
1719 tprintf(tpr, "nfs server %s: %s\n", server, msg);
1720 tprintf_close(tpr);
1721 return (0);
1722 }
1723
1724 #ifdef NFSSERVER
1725 int (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *,
1726 struct nfssvc_sock *, struct proc *,
1727 struct mbuf **) = {
1728 nfsrv_null,
1729 nfsrv_getattr,
1730 nfsrv_setattr,
1731 nfsrv_lookup,
1732 nfsrv3_access,
1733 nfsrv_readlink,
1734 nfsrv_read,
1735 nfsrv_write,
1736 nfsrv_create,
1737 nfsrv_mkdir,
1738 nfsrv_symlink,
1739 nfsrv_mknod,
1740 nfsrv_remove,
1741 nfsrv_rmdir,
1742 nfsrv_rename,
1743 nfsrv_link,
1744 nfsrv_readdir,
1745 nfsrv_readdirplus,
1746 nfsrv_statfs,
1747 nfsrv_fsinfo,
1748 nfsrv_pathconf,
1749 nfsrv_commit,
1750 nfsrv_noop,
1751 nfsrv_noop,
1752 nfsrv_noop,
1753 nfsrv_noop
1754 };
1755
1756 /*
1757 * Socket upcall routine for the nfsd sockets.
1758 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
1759 * Essentially do as much as possible non-blocking, else punt and it will
1760 * be called with M_WAIT from an nfsd.
1761 */
1762 void
nfsrv_rcv(so,arg,waitflag)1763 nfsrv_rcv(so, arg, waitflag)
1764 struct socket *so;
1765 caddr_t arg;
1766 int waitflag;
1767 {
1768 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
1769 struct mbuf *m;
1770 struct mbuf *mp, *nam;
1771 struct uio auio;
1772 int flags, error;
1773
1774 if ((slp->ns_flag & SLP_VALID) == 0)
1775 return;
1776 #ifdef notdef
1777 /*
1778 * Define this to test for nfsds handling this under heavy load.
1779 */
1780 if (waitflag == M_DONTWAIT) {
1781 slp->ns_flag |= SLP_NEEDQ; goto dorecs;
1782 }
1783 #endif
1784 auio.uio_procp = NULL;
1785 if (so->so_type == SOCK_STREAM) {
1786 /*
1787 * If there are already records on the queue, defer soreceive()
1788 * to an nfsd so that there is feedback to the TCP layer that
1789 * the nfs servers are heavily loaded.
1790 */
1791 if (slp->ns_rec && waitflag == M_DONTWAIT) {
1792 slp->ns_flag |= SLP_NEEDQ;
1793 goto dorecs;
1794 }
1795
1796 /*
1797 * Do soreceive().
1798 */
1799 auio.uio_resid = 1000000000;
1800 flags = MSG_DONTWAIT;
1801 error = soreceive(so, &nam, &auio, &mp, (struct mbuf **)0, &flags);
1802 if (error || mp == (struct mbuf *)0) {
1803 if (error == EWOULDBLOCK)
1804 slp->ns_flag |= SLP_NEEDQ;
1805 else
1806 slp->ns_flag |= SLP_DISCONN;
1807 goto dorecs;
1808 }
1809 m = mp;
1810 if (slp->ns_rawend) {
1811 slp->ns_rawend->m_next = m;
1812 slp->ns_cc += 1000000000 - auio.uio_resid;
1813 } else {
1814 slp->ns_raw = m;
1815 slp->ns_cc = 1000000000 - auio.uio_resid;
1816 }
1817 while (m->m_next)
1818 m = m->m_next;
1819 slp->ns_rawend = m;
1820
1821 /*
1822 * Now try and parse record(s) out of the raw stream data.
1823 */
1824 error = nfsrv_getstream(slp, waitflag);
1825 if (error) {
1826 if (error == EPERM)
1827 slp->ns_flag |= SLP_DISCONN;
1828 else
1829 slp->ns_flag |= SLP_NEEDQ;
1830 }
1831 } else {
1832 do {
1833 auio.uio_resid = 1000000000;
1834 flags = MSG_DONTWAIT;
1835 error = soreceive(so, &nam, &auio, &mp,
1836 (struct mbuf **)0, &flags);
1837 if (mp) {
1838 if (nam) {
1839 m = nam;
1840 m->m_next = mp;
1841 } else
1842 m = mp;
1843 if (slp->ns_recend)
1844 slp->ns_recend->m_nextpkt = m;
1845 else
1846 slp->ns_rec = m;
1847 slp->ns_recend = m;
1848 m->m_nextpkt = (struct mbuf *)0;
1849 }
1850 if (error) {
1851 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
1852 && error != EWOULDBLOCK) {
1853 slp->ns_flag |= SLP_DISCONN;
1854 goto dorecs;
1855 }
1856 }
1857 } while (mp);
1858 }
1859
1860 /*
1861 * Now try and process the request records, non-blocking.
1862 */
1863 dorecs:
1864 if (waitflag == M_DONTWAIT &&
1865 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
1866 nfsrv_wakenfsd(slp);
1867 }
1868
1869 /*
1870 * Try and extract an RPC request from the mbuf data list received on a
1871 * stream socket. The "waitflag" argument indicates whether or not it
1872 * can sleep.
1873 */
1874 int
nfsrv_getstream(slp,waitflag)1875 nfsrv_getstream(slp, waitflag)
1876 struct nfssvc_sock *slp;
1877 int waitflag;
1878 {
1879 struct mbuf *m, **mpp;
1880 char *cp1, *cp2;
1881 int len;
1882 struct mbuf *om, *m2, *recm;
1883 u_int32_t recmark;
1884
1885 if (slp->ns_flag & SLP_GETSTREAM)
1886 panic("nfs getstream");
1887 slp->ns_flag |= SLP_GETSTREAM;
1888 for (;;) {
1889 if (slp->ns_reclen == 0) {
1890 if (slp->ns_cc < NFSX_UNSIGNED) {
1891 slp->ns_flag &= ~SLP_GETSTREAM;
1892 return (0);
1893 }
1894 m = slp->ns_raw;
1895 if (m->m_len >= NFSX_UNSIGNED) {
1896 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
1897 m->m_data += NFSX_UNSIGNED;
1898 m->m_len -= NFSX_UNSIGNED;
1899 } else {
1900 cp1 = (caddr_t)&recmark;
1901 cp2 = mtod(m, caddr_t);
1902 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
1903 while (m->m_len == 0) {
1904 m = m->m_next;
1905 cp2 = mtod(m, caddr_t);
1906 }
1907 *cp1++ = *cp2++;
1908 m->m_data++;
1909 m->m_len--;
1910 }
1911 }
1912 slp->ns_cc -= NFSX_UNSIGNED;
1913 recmark = ntohl(recmark);
1914 slp->ns_reclen = recmark & ~0x80000000;
1915 if (recmark & 0x80000000)
1916 slp->ns_flag |= SLP_LASTFRAG;
1917 else
1918 slp->ns_flag &= ~SLP_LASTFRAG;
1919 if (slp->ns_reclen > NFS_MAXPACKET) {
1920 slp->ns_flag &= ~SLP_GETSTREAM;
1921 return (EPERM);
1922 }
1923 }
1924
1925 /*
1926 * Now get the record part.
1927 */
1928 recm = NULL;
1929 if (slp->ns_cc == slp->ns_reclen) {
1930 recm = slp->ns_raw;
1931 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
1932 slp->ns_cc = slp->ns_reclen = 0;
1933 } else if (slp->ns_cc > slp->ns_reclen) {
1934 len = 0;
1935 m = slp->ns_raw;
1936 om = (struct mbuf *)0;
1937 while (len < slp->ns_reclen) {
1938 if ((len + m->m_len) > slp->ns_reclen) {
1939 m2 = m_copym(m, 0, slp->ns_reclen - len,
1940 waitflag);
1941 if (m2) {
1942 if (om) {
1943 om->m_next = m2;
1944 recm = slp->ns_raw;
1945 } else
1946 recm = m2;
1947 m->m_data += slp->ns_reclen - len;
1948 m->m_len -= slp->ns_reclen - len;
1949 len = slp->ns_reclen;
1950 } else {
1951 slp->ns_flag &= ~SLP_GETSTREAM;
1952 return (EWOULDBLOCK);
1953 }
1954 } else if ((len + m->m_len) == slp->ns_reclen) {
1955 om = m;
1956 len += m->m_len;
1957 m = m->m_next;
1958 recm = slp->ns_raw;
1959 om->m_next = (struct mbuf *)0;
1960 } else {
1961 om = m;
1962 len += m->m_len;
1963 m = m->m_next;
1964 }
1965 }
1966 slp->ns_raw = m;
1967 slp->ns_cc -= len;
1968 slp->ns_reclen = 0;
1969 } else {
1970 slp->ns_flag &= ~SLP_GETSTREAM;
1971 return (0);
1972 }
1973
1974 /*
1975 * Accumulate the fragments into a record.
1976 */
1977 mpp = &slp->ns_frag;
1978 while (*mpp)
1979 mpp = &((*mpp)->m_next);
1980 *mpp = recm;
1981 if (slp->ns_flag & SLP_LASTFRAG) {
1982 if (slp->ns_recend)
1983 slp->ns_recend->m_nextpkt = slp->ns_frag;
1984 else
1985 slp->ns_rec = slp->ns_frag;
1986 slp->ns_recend = slp->ns_frag;
1987 slp->ns_frag = (struct mbuf *)0;
1988 }
1989 }
1990 }
1991
1992 /*
1993 * Parse an RPC header.
1994 */
1995 int
nfsrv_dorec(slp,nfsd,ndp)1996 nfsrv_dorec(slp, nfsd, ndp)
1997 struct nfssvc_sock *slp;
1998 struct nfsd *nfsd;
1999 struct nfsrv_descript **ndp;
2000 {
2001 struct mbuf *m, *nam;
2002 struct nfsrv_descript *nd;
2003 int error;
2004
2005 *ndp = NULL;
2006 if ((slp->ns_flag & SLP_VALID) == 0 ||
2007 (m = slp->ns_rec) == (struct mbuf *)0)
2008 return (ENOBUFS);
2009 slp->ns_rec = m->m_nextpkt;
2010 if (slp->ns_rec)
2011 m->m_nextpkt = (struct mbuf *)0;
2012 else
2013 slp->ns_recend = (struct mbuf *)0;
2014 if (m->m_type == MT_SONAME) {
2015 nam = m;
2016 m = m->m_next;
2017 nam->m_next = NULL;
2018 } else
2019 nam = NULL;
2020 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
2021 M_NFSRVDESC, M_WAITOK);
2022 nfs_realign(&m, 10 * NFSX_UNSIGNED);
2023 nd->nd_md = nd->nd_mrep = m;
2024 nd->nd_nam2 = nam;
2025 nd->nd_dpos = mtod(m, caddr_t);
2026 error = nfs_getreq(nd, nfsd, TRUE);
2027 if (error) {
2028 m_freem(nam);
2029 free((caddr_t)nd, M_NFSRVDESC);
2030 return (error);
2031 }
2032 *ndp = nd;
2033 nfsd->nfsd_nd = nd;
2034 return (0);
2035 }
2036
2037
2038 /*
2039 * Search for a sleeping nfsd and wake it up.
2040 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2041 * running nfsds will go look for the work in the nfssvc_sock list.
2042 */
2043 void
nfsrv_wakenfsd(slp)2044 nfsrv_wakenfsd(slp)
2045 struct nfssvc_sock *slp;
2046 {
2047 struct nfsd *nd;
2048
2049 if ((slp->ns_flag & SLP_VALID) == 0)
2050 return;
2051 for (nd = TAILQ_FIRST(&nfsd_head); nd != NULL;
2052 nd = TAILQ_NEXT(nd, nfsd_chain)) {
2053 if (nd->nfsd_flag & NFSD_WAITING) {
2054 nd->nfsd_flag &= ~NFSD_WAITING;
2055 if (nd->nfsd_slp)
2056 panic("nfsd wakeup");
2057 slp->ns_sref++;
2058 nd->nfsd_slp = slp;
2059 wakeup((caddr_t)nd);
2060 return;
2061 }
2062 }
2063 slp->ns_flag |= SLP_DOREC;
2064 nfsd_head_flag |= NFSD_CHECKSLP;
2065 }
2066 #endif /* NFSSERVER */
2067