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