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