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