nfs_socket.c revision 1.135 1 /* $NetBSD: nfs_socket.c,v 1.135 2006/06/07 22:34:17 kardel 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.135 2006/06/07 22:34:17 kardel 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 getmicrotime(&rt->tstamp);
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
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 KASSERT(kauth_cred_getrefcnt(acred) == 1);
1037 kauth_cred_free(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_second;
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 KASSERT(
1275 kauth_cred_getrefcnt(acred) == 1);
1276 kauth_cred_free(acred);
1277 return ENOMEM;
1278 }
1279 mrest = mrest_backup;
1280 mrest_backup = NULL;
1281 cred = origcred;
1282 error = 0;
1283 retry_cred = FALSE;
1284 goto tryagain_cred;
1285
1286 case NFSERR_EXIST:
1287 error = EEXIST;
1288 break;
1289
1290 case NFSERR_XDEV:
1291 error = EXDEV;
1292 break;
1293
1294 case NFSERR_NODEV:
1295 error = ENODEV;
1296 break;
1297
1298 case NFSERR_NOTDIR:
1299 error = ENOTDIR;
1300 break;
1301
1302 case NFSERR_ISDIR:
1303 error = EISDIR;
1304 break;
1305
1306 case NFSERR_INVAL:
1307 error = EINVAL;
1308 break;
1309
1310 case NFSERR_FBIG:
1311 error = EFBIG;
1312 break;
1313
1314 case NFSERR_NOSPC:
1315 error = ENOSPC;
1316 break;
1317
1318 case NFSERR_ROFS:
1319 error = EROFS;
1320 break;
1321
1322 case NFSERR_MLINK:
1323 error = EMLINK;
1324 break;
1325
1326 case NFSERR_TIMEDOUT:
1327 error = ETIMEDOUT;
1328 break;
1329
1330 case NFSERR_NAMETOL:
1331 error = ENAMETOOLONG;
1332 break;
1333
1334 case NFSERR_NOTEMPTY:
1335 error = ENOTEMPTY;
1336 break;
1337
1338 case NFSERR_DQUOT:
1339 error = EDQUOT;
1340 break;
1341
1342 case NFSERR_STALE:
1343 /*
1344 * If the File Handle was stale, invalidate the
1345 * lookup cache, just in case.
1346 */
1347 error = ESTALE;
1348 cache_purge(NFSTOV(np));
1349 break;
1350
1351 case NFSERR_REMOTE:
1352 error = EREMOTE;
1353 break;
1354
1355 case NFSERR_WFLUSH:
1356 case NFSERR_BADHANDLE:
1357 case NFSERR_NOT_SYNC:
1358 case NFSERR_BAD_COOKIE:
1359 error = EINVAL;
1360 break;
1361
1362 case NFSERR_NOTSUPP:
1363 error = ENOTSUP;
1364 break;
1365
1366 case NFSERR_TOOSMALL:
1367 case NFSERR_SERVERFAULT:
1368 case NFSERR_BADTYPE:
1369 error = EINVAL;
1370 break;
1371
1372 case NFSERR_TRYLATER:
1373 if ((nmp->nm_flag & NFSMNT_NFSV3) == 0)
1374 break;
1375 m_freem(mrep);
1376 error = 0;
1377 waituntil = time_second + trylater_delay;
1378 while (time_second < waituntil)
1379 (void) tsleep((caddr_t)&lbolt,
1380 PSOCK, "nqnfstry", 0);
1381 trylater_delay *= NFS_TRYLATERDELMUL;
1382 if (trylater_delay > NFS_TRYLATERDELMAX)
1383 trylater_delay = NFS_TRYLATERDELMAX;
1384 /*
1385 * RFC1813:
1386 * The client should wait and then try
1387 * the request with a new RPC transaction ID.
1388 */
1389 nfs_renewxid(rep);
1390 goto tryagain;
1391
1392 case NFSERR_STALEWRITEVERF:
1393 error = EINVAL;
1394 break;
1395
1396 default:
1397 #ifdef DIAGNOSTIC
1398 printf("Invalid rpc error code %d\n", error);
1399 #endif
1400 error = EINVAL;
1401 break;
1402 }
1403
1404 if (nmp->nm_flag & NFSMNT_NFSV3) {
1405 *mrp = mrep;
1406 *mdp = md;
1407 *dposp = dpos;
1408 error |= NFSERR_RETERR;
1409 } else
1410 m_freem(mrep);
1411 goto nfsmout;
1412 }
1413
1414 /*
1415 * note which credential worked to minimize number of retries.
1416 */
1417 if (use_opencred)
1418 np->n_flag |= NUSEOPENCRED;
1419 else
1420 np->n_flag &= ~NUSEOPENCRED;
1421
1422 #ifndef NFS_V2_ONLY
1423 /*
1424 * For nqnfs, get any lease in reply
1425 */
1426 if (nmp->nm_flag & NFSMNT_NQNFS) {
1427 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1428 if (*tl) {
1429 nqlflag = fxdr_unsigned(int, *tl);
1430 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
1431 cachable = fxdr_unsigned(int, *tl++);
1432 reqtime += fxdr_unsigned(int, *tl++);
1433 if (reqtime > time_second) {
1434 frev = fxdr_hyper(tl);
1435 nqnfs_clientlease(nmp, np, nqlflag,
1436 cachable, reqtime, frev);
1437 }
1438 }
1439 }
1440 #endif
1441 *mrp = mrep;
1442 *mdp = md;
1443 *dposp = dpos;
1444
1445 KASSERT(error == 0);
1446 goto nfsmout;
1447 }
1448 m_freem(mrep);
1449 error = EPROTONOSUPPORT;
1450 nfsmout:
1451 KASSERT(kauth_cred_getrefcnt(acred) == 1);
1452 kauth_cred_free(acred);
1453 m_freem(rep->r_mreq);
1454 free((caddr_t)rep, M_NFSREQ);
1455 m_freem(mrest_backup);
1456 return (error);
1457 }
1458 #endif /* NFS */
1459
1460 /*
1461 * Generate the rpc reply header
1462 * siz arg. is used to decide if adding a cluster is worthwhile
1463 */
1464 int
1465 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1466 int siz;
1467 struct nfsrv_descript *nd;
1468 struct nfssvc_sock *slp;
1469 int err;
1470 int cache;
1471 u_quad_t *frev;
1472 struct mbuf **mrq;
1473 struct mbuf **mbp;
1474 caddr_t *bposp;
1475 {
1476 u_int32_t *tl;
1477 struct mbuf *mreq;
1478 caddr_t bpos;
1479 struct mbuf *mb;
1480
1481 mreq = m_gethdr(M_WAIT, MT_DATA);
1482 MCLAIM(mreq, &nfs_mowner);
1483 mb = mreq;
1484 /*
1485 * If this is a big reply, use a cluster else
1486 * try and leave leading space for the lower level headers.
1487 */
1488 siz += RPC_REPLYSIZ;
1489 if (siz >= max_datalen) {
1490 m_clget(mreq, M_WAIT);
1491 } else
1492 mreq->m_data += max_hdr;
1493 tl = mtod(mreq, u_int32_t *);
1494 mreq->m_len = 6 * NFSX_UNSIGNED;
1495 bpos = ((caddr_t)tl) + mreq->m_len;
1496 *tl++ = txdr_unsigned(nd->nd_retxid);
1497 *tl++ = rpc_reply;
1498 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1499 *tl++ = rpc_msgdenied;
1500 if (err & NFSERR_AUTHERR) {
1501 *tl++ = rpc_autherr;
1502 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1503 mreq->m_len -= NFSX_UNSIGNED;
1504 bpos -= NFSX_UNSIGNED;
1505 } else {
1506 *tl++ = rpc_mismatch;
1507 *tl++ = txdr_unsigned(RPC_VER2);
1508 *tl = txdr_unsigned(RPC_VER2);
1509 }
1510 } else {
1511 *tl++ = rpc_msgaccepted;
1512
1513 /*
1514 * For Kerberos authentication, we must send the nickname
1515 * verifier back, otherwise just RPCAUTH_NULL.
1516 */
1517 if (nd->nd_flag & ND_KERBFULL) {
1518 struct nfsuid *nuidp;
1519 struct timeval ktvin, ktvout;
1520
1521 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1522
1523 LIST_FOREACH(nuidp,
1524 NUIDHASH(slp, kauth_cred_geteuid(nd->nd_cr)),
1525 nu_hash) {
1526 if (kauth_cred_geteuid(nuidp->nu_cr) ==
1527 kauth_cred_geteuid(nd->nd_cr) &&
1528 (!nd->nd_nam2 || netaddr_match(
1529 NU_NETFAM(nuidp), &nuidp->nu_haddr,
1530 nd->nd_nam2)))
1531 break;
1532 }
1533 if (nuidp) {
1534 ktvin.tv_sec =
1535 txdr_unsigned(nuidp->nu_timestamp.tv_sec
1536 - 1);
1537 ktvin.tv_usec =
1538 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1539
1540 /*
1541 * Encrypt the timestamp in ecb mode using the
1542 * session key.
1543 */
1544 #ifdef NFSKERB
1545 XXX
1546 #endif
1547
1548 *tl++ = rpc_auth_kerb;
1549 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1550 *tl = ktvout.tv_sec;
1551 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1552 *tl++ = ktvout.tv_usec;
1553 *tl++ = txdr_unsigned(
1554 kauth_cred_geteuid(nuidp->nu_cr));
1555 } else {
1556 *tl++ = 0;
1557 *tl++ = 0;
1558 }
1559 } else {
1560 *tl++ = 0;
1561 *tl++ = 0;
1562 }
1563 switch (err) {
1564 case EPROGUNAVAIL:
1565 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1566 break;
1567 case EPROGMISMATCH:
1568 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1569 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1570 if (nd->nd_flag & ND_NQNFS) {
1571 *tl++ = txdr_unsigned(3);
1572 *tl = txdr_unsigned(3);
1573 } else {
1574 *tl++ = txdr_unsigned(2);
1575 *tl = txdr_unsigned(3);
1576 }
1577 break;
1578 case EPROCUNAVAIL:
1579 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1580 break;
1581 case EBADRPC:
1582 *tl = txdr_unsigned(RPC_GARBAGE);
1583 break;
1584 default:
1585 *tl = 0;
1586 if (err != NFSERR_RETVOID) {
1587 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1588 if (err)
1589 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1590 else
1591 *tl = 0;
1592 }
1593 break;
1594 };
1595 }
1596
1597 /*
1598 * For nqnfs, piggyback lease as requested.
1599 */
1600 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1601 if (nd->nd_flag & ND_LEASE) {
1602 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1603 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1604 *tl++ = txdr_unsigned(cache);
1605 *tl++ = txdr_unsigned(nd->nd_duration);
1606 txdr_hyper(*frev, tl);
1607 } else {
1608 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1609 *tl = 0;
1610 }
1611 }
1612 if (mrq != NULL)
1613 *mrq = mreq;
1614 *mbp = mb;
1615 *bposp = bpos;
1616 if (err != 0 && err != NFSERR_RETVOID)
1617 nfsstats.srvrpc_errs++;
1618 return (0);
1619 }
1620
1621 /*
1622 * Nfs timer routine
1623 * Scan the nfsreq list and retranmit any requests that have timed out
1624 * To avoid retransmission attempts on STREAM sockets (in the future) make
1625 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1626 */
1627 void
1628 nfs_timer(arg)
1629 void *arg; /* never used */
1630 {
1631 struct nfsreq *rep;
1632 struct mbuf *m;
1633 struct socket *so;
1634 struct nfsmount *nmp;
1635 int timeo;
1636 int s, error;
1637 #ifdef NFSSERVER
1638 struct timeval tv;
1639 struct nfssvc_sock *slp;
1640 static long lasttime = 0;
1641 u_quad_t cur_usec;
1642 #endif
1643
1644 s = splsoftnet();
1645 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1646 nmp = rep->r_nmp;
1647 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1648 continue;
1649 if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
1650 rep->r_flags |= R_SOFTTERM;
1651 continue;
1652 }
1653 if (rep->r_rtt >= 0) {
1654 rep->r_rtt++;
1655 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1656 timeo = nmp->nm_timeo;
1657 else
1658 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1659 if (nmp->nm_timeouts > 0)
1660 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1661 if (rep->r_rtt <= timeo)
1662 continue;
1663 if (nmp->nm_timeouts <
1664 (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
1665 nmp->nm_timeouts++;
1666 }
1667 /*
1668 * Check for server not responding
1669 */
1670 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1671 rep->r_rexmit > nmp->nm_deadthresh) {
1672 nfs_msg(rep->r_lwp,
1673 nmp->nm_mountp->mnt_stat.f_mntfromname,
1674 "not responding");
1675 rep->r_flags |= R_TPRINTFMSG;
1676 }
1677 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1678 nfsstats.rpctimeouts++;
1679 rep->r_flags |= R_SOFTTERM;
1680 continue;
1681 }
1682 if (nmp->nm_sotype != SOCK_DGRAM) {
1683 if (++rep->r_rexmit > NFS_MAXREXMIT)
1684 rep->r_rexmit = NFS_MAXREXMIT;
1685 continue;
1686 }
1687 if ((so = nmp->nm_so) == NULL)
1688 continue;
1689
1690 /*
1691 * If there is enough space and the window allows..
1692 * Resend it
1693 * Set r_rtt to -1 in case we fail to send it now.
1694 */
1695 rep->r_rtt = -1;
1696 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1697 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1698 (rep->r_flags & R_SENT) ||
1699 nmp->nm_sent < nmp->nm_cwnd) &&
1700 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1701 if (so->so_state & SS_ISCONNECTED)
1702 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1703 (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1704 else
1705 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1706 nmp->nm_nam, (struct mbuf *)0, (struct lwp *)0);
1707 if (error) {
1708 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
1709 #ifdef DEBUG
1710 printf("nfs_timer: ignoring error %d\n",
1711 error);
1712 #endif
1713 so->so_error = 0;
1714 }
1715 } else {
1716 /*
1717 * Iff first send, start timing
1718 * else turn timing off, backoff timer
1719 * and divide congestion window by 2.
1720 */
1721 if (rep->r_flags & R_SENT) {
1722 rep->r_flags &= ~R_TIMING;
1723 if (++rep->r_rexmit > NFS_MAXREXMIT)
1724 rep->r_rexmit = NFS_MAXREXMIT;
1725 nmp->nm_cwnd >>= 1;
1726 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1727 nmp->nm_cwnd = NFS_CWNDSCALE;
1728 nfsstats.rpcretries++;
1729 } else {
1730 rep->r_flags |= R_SENT;
1731 nmp->nm_sent += NFS_CWNDSCALE;
1732 }
1733 rep->r_rtt = 0;
1734 }
1735 }
1736 }
1737
1738 #ifdef NFSSERVER
1739 /*
1740 * Call the nqnfs server timer once a second to handle leases.
1741 */
1742 if (lasttime != time_second) {
1743 lasttime = time_second;
1744 nqnfs_serverd();
1745 }
1746
1747 /*
1748 * Scan the write gathering queues for writes that need to be
1749 * completed now.
1750 */
1751 getmicrotime(&tv);
1752 cur_usec = (u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec;
1753 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1754 if (LIST_FIRST(&slp->ns_tq) &&
1755 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
1756 nfsrv_wakenfsd(slp);
1757 }
1758 #endif /* NFSSERVER */
1759 splx(s);
1760 callout_schedule(&nfs_timer_ch, nfs_ticks);
1761 }
1762
1763 /*ARGSUSED*/
1764 void
1765 nfs_exit(p, v)
1766 struct proc *p;
1767 void *v;
1768 {
1769 struct nfsreq *rp;
1770 int s = splsoftnet();
1771
1772 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
1773 if (rp->r_lwp && rp->r_lwp->l_proc == p)
1774 TAILQ_REMOVE(&nfs_reqq, rp, r_chain);
1775 }
1776 splx(s);
1777 }
1778
1779 /*
1780 * Test for a termination condition pending on the process.
1781 * This is used for NFSMNT_INT mounts.
1782 */
1783 int
1784 nfs_sigintr(nmp, rep, l)
1785 struct nfsmount *nmp;
1786 struct nfsreq *rep;
1787 struct lwp *l;
1788 {
1789 sigset_t ss;
1790
1791 if (rep && (rep->r_flags & R_SOFTTERM))
1792 return (EINTR);
1793 if (!(nmp->nm_flag & NFSMNT_INT))
1794 return (0);
1795 if (l) {
1796 sigpending1(l->l_proc, &ss);
1797 #if 0
1798 sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
1799 #endif
1800 if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
1801 sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
1802 sigismember(&ss, SIGQUIT))
1803 return (EINTR);
1804 }
1805 return (0);
1806 }
1807
1808 /*
1809 * Lock a socket against others.
1810 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1811 * and also to avoid race conditions between the processes with nfs requests
1812 * in progress when a reconnect is necessary.
1813 */
1814 int
1815 nfs_sndlock(flagp, rep)
1816 int *flagp;
1817 struct nfsreq *rep;
1818 {
1819 struct lwp *l;
1820 int slpflag = 0, slptimeo = 0;
1821
1822 if (rep) {
1823 l = rep->r_lwp;
1824 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1825 slpflag = PCATCH;
1826 } else
1827 l = (struct lwp *)0;
1828 while (*flagp & NFSMNT_SNDLOCK) {
1829 if (rep && nfs_sigintr(rep->r_nmp, rep, l))
1830 return (EINTR);
1831 *flagp |= NFSMNT_WANTSND;
1832 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1833 slptimeo);
1834 if (slpflag == PCATCH) {
1835 slpflag = 0;
1836 slptimeo = 2 * hz;
1837 }
1838 }
1839 *flagp |= NFSMNT_SNDLOCK;
1840 return (0);
1841 }
1842
1843 /*
1844 * Unlock the stream socket for others.
1845 */
1846 void
1847 nfs_sndunlock(flagp)
1848 int *flagp;
1849 {
1850
1851 if ((*flagp & NFSMNT_SNDLOCK) == 0)
1852 panic("nfs sndunlock");
1853 *flagp &= ~NFSMNT_SNDLOCK;
1854 if (*flagp & NFSMNT_WANTSND) {
1855 *flagp &= ~NFSMNT_WANTSND;
1856 wakeup((caddr_t)flagp);
1857 }
1858 }
1859
1860 int
1861 nfs_rcvlock(rep)
1862 struct nfsreq *rep;
1863 {
1864 struct nfsmount *nmp = rep->r_nmp;
1865 int *flagp = &nmp->nm_iflag;
1866 int slpflag, slptimeo = 0;
1867 int error = 0;
1868
1869 if (*flagp & NFSMNT_DISMNT)
1870 return EIO;
1871
1872 if (*flagp & NFSMNT_INT)
1873 slpflag = PCATCH;
1874 else
1875 slpflag = 0;
1876 simple_lock(&nmp->nm_slock);
1877 while (*flagp & NFSMNT_RCVLOCK) {
1878 if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
1879 error = EINTR;
1880 goto quit;
1881 }
1882 *flagp |= NFSMNT_WANTRCV;
1883 nmp->nm_waiters++;
1884 (void) ltsleep(flagp, slpflag | (PZERO - 1), "nfsrcvlk",
1885 slptimeo, &nmp->nm_slock);
1886 nmp->nm_waiters--;
1887 if (*flagp & NFSMNT_DISMNT) {
1888 wakeup(&nmp->nm_waiters);
1889 error = EIO;
1890 goto quit;
1891 }
1892 /* If our reply was received while we were sleeping,
1893 * then just return without taking the lock to avoid a
1894 * situation where a single iod could 'capture' the
1895 * receive lock.
1896 */
1897 if (rep->r_mrep != NULL) {
1898 error = EALREADY;
1899 goto quit;
1900 }
1901 if (slpflag == PCATCH) {
1902 slpflag = 0;
1903 slptimeo = 2 * hz;
1904 }
1905 }
1906 *flagp |= NFSMNT_RCVLOCK;
1907 quit:
1908 simple_unlock(&nmp->nm_slock);
1909 return error;
1910 }
1911
1912 /*
1913 * Unlock the stream socket for others.
1914 */
1915 void
1916 nfs_rcvunlock(nmp)
1917 struct nfsmount *nmp;
1918 {
1919 int *flagp = &nmp->nm_iflag;
1920
1921 simple_lock(&nmp->nm_slock);
1922 if ((*flagp & NFSMNT_RCVLOCK) == 0)
1923 panic("nfs rcvunlock");
1924 *flagp &= ~NFSMNT_RCVLOCK;
1925 if (*flagp & NFSMNT_WANTRCV) {
1926 *flagp &= ~NFSMNT_WANTRCV;
1927 wakeup((caddr_t)flagp);
1928 }
1929 simple_unlock(&nmp->nm_slock);
1930 }
1931
1932 /*
1933 * Parse an RPC request
1934 * - verify it
1935 * - allocate and fill in the cred.
1936 */
1937 int
1938 nfs_getreq(nd, nfsd, has_header)
1939 struct nfsrv_descript *nd;
1940 struct nfsd *nfsd;
1941 int has_header;
1942 {
1943 int len, i;
1944 u_int32_t *tl;
1945 int32_t t1;
1946 struct uio uio;
1947 struct iovec iov;
1948 caddr_t dpos, cp2, cp;
1949 u_int32_t nfsvers, auth_type;
1950 uid_t nickuid;
1951 int error = 0, nqnfs = 0, ticklen;
1952 struct mbuf *mrep, *md;
1953 struct nfsuid *nuidp;
1954 struct timeval tvin, tvout;
1955
1956 memset(&tvout, 0, sizeof tvout); /* XXX gcc */
1957
1958 KASSERT(nd->nd_cr == NULL);
1959 mrep = nd->nd_mrep;
1960 md = nd->nd_md;
1961 dpos = nd->nd_dpos;
1962 if (has_header) {
1963 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1964 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1965 if (*tl++ != rpc_call) {
1966 m_freem(mrep);
1967 return (EBADRPC);
1968 }
1969 } else
1970 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1971 nd->nd_repstat = 0;
1972 nd->nd_flag = 0;
1973 if (*tl++ != rpc_vers) {
1974 nd->nd_repstat = ERPCMISMATCH;
1975 nd->nd_procnum = NFSPROC_NOOP;
1976 return (0);
1977 }
1978 if (*tl != nfs_prog) {
1979 if (*tl == nqnfs_prog)
1980 nqnfs++;
1981 else {
1982 nd->nd_repstat = EPROGUNAVAIL;
1983 nd->nd_procnum = NFSPROC_NOOP;
1984 return (0);
1985 }
1986 }
1987 tl++;
1988 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1989 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
1990 (nfsvers != NQNFS_VER3 && nqnfs)) {
1991 nd->nd_repstat = EPROGMISMATCH;
1992 nd->nd_procnum = NFSPROC_NOOP;
1993 return (0);
1994 }
1995 if (nqnfs)
1996 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
1997 else if (nfsvers == NFS_VER3)
1998 nd->nd_flag = ND_NFSV3;
1999 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
2000 if (nd->nd_procnum == NFSPROC_NULL)
2001 return (0);
2002 if (nd->nd_procnum >= NFS_NPROCS ||
2003 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
2004 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
2005 nd->nd_repstat = EPROCUNAVAIL;
2006 nd->nd_procnum = NFSPROC_NOOP;
2007 return (0);
2008 }
2009 if ((nd->nd_flag & ND_NFSV3) == 0)
2010 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
2011 auth_type = *tl++;
2012 len = fxdr_unsigned(int, *tl++);
2013 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2014 m_freem(mrep);
2015 return (EBADRPC);
2016 }
2017
2018 nd->nd_flag &= ~ND_KERBAUTH;
2019 /*
2020 * Handle auth_unix or auth_kerb.
2021 */
2022 if (auth_type == rpc_auth_unix) {
2023 uid_t uid;
2024 gid_t gid, *grbuf;
2025
2026 nd->nd_cr = kauth_cred_alloc();
2027 len = fxdr_unsigned(int, *++tl);
2028 if (len < 0 || len > NFS_MAXNAMLEN) {
2029 m_freem(mrep);
2030 error = EBADRPC;
2031 goto errout;
2032 }
2033 nfsm_adv(nfsm_rndup(len));
2034 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2035
2036 uid = fxdr_unsigned(uid_t, *tl++);
2037 gid = fxdr_unsigned(gid_t, *tl++);
2038 kauth_cred_setuid(nd->nd_cr, uid);
2039 kauth_cred_setgid(nd->nd_cr, gid);
2040 kauth_cred_seteuid(nd->nd_cr, uid);
2041 kauth_cred_setsvuid(nd->nd_cr, gid);
2042 kauth_cred_setegid(nd->nd_cr, uid);
2043 kauth_cred_setsvgid(nd->nd_cr, gid);
2044
2045 len = fxdr_unsigned(int, *tl);
2046 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
2047 m_freem(mrep);
2048 error = EBADRPC;
2049 goto errout;
2050 }
2051 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
2052
2053 grbuf = malloc(len * sizeof(gid_t), M_TEMP, M_WAITOK);
2054 for (i = 0; i < len; i++) {
2055 if (i < NGROUPS) /* XXX elad */
2056 grbuf[i] = fxdr_unsigned(gid_t, *tl++);
2057 else
2058 tl++;
2059 }
2060 kauth_cred_setgroups(nd->nd_cr, grbuf, min(len, NGROUPS), -1);
2061 free(grbuf, M_TEMP);
2062
2063 len = fxdr_unsigned(int, *++tl);
2064 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2065 m_freem(mrep);
2066 error = EBADRPC;
2067 goto errout;
2068 }
2069 if (len > 0)
2070 nfsm_adv(nfsm_rndup(len));
2071 } else if (auth_type == rpc_auth_kerb) {
2072 switch (fxdr_unsigned(int, *tl++)) {
2073 case RPCAKN_FULLNAME:
2074 ticklen = fxdr_unsigned(int, *tl);
2075 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
2076 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2077 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2078 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
2079 m_freem(mrep);
2080 error = EBADRPC;
2081 goto errout;
2082 }
2083 uio.uio_offset = 0;
2084 uio.uio_iov = &iov;
2085 uio.uio_iovcnt = 1;
2086 UIO_SETUP_SYSSPACE(&uio);
2087 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
2088 iov.iov_len = RPCAUTH_MAXSIZ - 4;
2089 nfsm_mtouio(&uio, uio.uio_resid);
2090 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2091 if (*tl++ != rpc_auth_kerb ||
2092 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
2093 printf("Bad kerb verifier\n");
2094 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2095 nd->nd_procnum = NFSPROC_NOOP;
2096 return (0);
2097 }
2098 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
2099 tl = (u_int32_t *)cp;
2100 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
2101 printf("Not fullname kerb verifier\n");
2102 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2103 nd->nd_procnum = NFSPROC_NOOP;
2104 return (0);
2105 }
2106 cp += NFSX_UNSIGNED;
2107 memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
2108 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2109 nd->nd_flag |= ND_KERBFULL;
2110 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2111 break;
2112 case RPCAKN_NICKNAME:
2113 if (len != 2 * NFSX_UNSIGNED) {
2114 printf("Kerb nickname short\n");
2115 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2116 nd->nd_procnum = NFSPROC_NOOP;
2117 return (0);
2118 }
2119 nickuid = fxdr_unsigned(uid_t, *tl);
2120 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2121 if (*tl++ != rpc_auth_kerb ||
2122 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
2123 printf("Kerb nick verifier bad\n");
2124 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2125 nd->nd_procnum = NFSPROC_NOOP;
2126 return (0);
2127 }
2128 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2129 tvin.tv_sec = *tl++;
2130 tvin.tv_usec = *tl;
2131
2132 LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
2133 nu_hash) {
2134 if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid &&
2135 (!nd->nd_nam2 ||
2136 netaddr_match(NU_NETFAM(nuidp),
2137 &nuidp->nu_haddr, nd->nd_nam2)))
2138 break;
2139 }
2140 if (!nuidp) {
2141 nd->nd_repstat =
2142 (NFSERR_AUTHERR|AUTH_REJECTCRED);
2143 nd->nd_procnum = NFSPROC_NOOP;
2144 return (0);
2145 }
2146
2147 /*
2148 * Now, decrypt the timestamp using the session key
2149 * and validate it.
2150 */
2151 #ifdef NFSKERB
2152 XXX
2153 #endif
2154
2155 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2156 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2157 if (nuidp->nu_expire < time_second ||
2158 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2159 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2160 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2161 nuidp->nu_expire = 0;
2162 nd->nd_repstat =
2163 (NFSERR_AUTHERR|AUTH_REJECTVERF);
2164 nd->nd_procnum = NFSPROC_NOOP;
2165 return (0);
2166 }
2167 kauth_cred_hold(nuidp->nu_cr);
2168 nd->nd_cr = nuidp->nu_cr;
2169 nd->nd_flag |= ND_KERBNICK;
2170 }
2171 } else {
2172 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2173 nd->nd_procnum = NFSPROC_NOOP;
2174 return (0);
2175 }
2176
2177 /*
2178 * For nqnfs, get piggybacked lease request.
2179 */
2180 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
2181 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2182 nd->nd_flag |= fxdr_unsigned(int, *tl);
2183 if (nd->nd_flag & ND_LEASE) {
2184 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2185 nd->nd_duration = fxdr_unsigned(u_int32_t, *tl);
2186 } else
2187 nd->nd_duration = NQ_MINLEASE;
2188 } else
2189 nd->nd_duration = NQ_MINLEASE;
2190 nd->nd_md = md;
2191 nd->nd_dpos = dpos;
2192 KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0)
2193 || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0));
2194 return (0);
2195 nfsmout:
2196 errout:
2197 KASSERT(error != 0);
2198 if (nd->nd_cr != NULL) {
2199 kauth_cred_free(nd->nd_cr);
2200 nd->nd_cr = NULL;
2201 }
2202 return (error);
2203 }
2204
2205 int
2206 nfs_msg(l, server, msg)
2207 struct lwp *l;
2208 const char *server, *msg;
2209 {
2210 tpr_t tpr;
2211
2212 if (l)
2213 tpr = tprintf_open(l->l_proc);
2214 else
2215 tpr = NULL;
2216 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2217 tprintf_close(tpr);
2218 return (0);
2219 }
2220
2221 #ifdef NFSSERVER
2222 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
2223 struct nfssvc_sock *, struct lwp *,
2224 struct mbuf **)) = {
2225 nfsrv_null,
2226 nfsrv_getattr,
2227 nfsrv_setattr,
2228 nfsrv_lookup,
2229 nfsrv3_access,
2230 nfsrv_readlink,
2231 nfsrv_read,
2232 nfsrv_write,
2233 nfsrv_create,
2234 nfsrv_mkdir,
2235 nfsrv_symlink,
2236 nfsrv_mknod,
2237 nfsrv_remove,
2238 nfsrv_rmdir,
2239 nfsrv_rename,
2240 nfsrv_link,
2241 nfsrv_readdir,
2242 nfsrv_readdirplus,
2243 nfsrv_statfs,
2244 nfsrv_fsinfo,
2245 nfsrv_pathconf,
2246 nfsrv_commit,
2247 nqnfsrv_getlease,
2248 nqnfsrv_vacated,
2249 nfsrv_noop,
2250 nfsrv_noop
2251 };
2252
2253 /*
2254 * Socket upcall routine for the nfsd sockets.
2255 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2256 * Essentially do as much as possible non-blocking, else punt and it will
2257 * be called with M_WAIT from an nfsd.
2258 */
2259 void
2260 nfsrv_rcv(so, arg, waitflag)
2261 struct socket *so;
2262 caddr_t arg;
2263 int waitflag;
2264 {
2265 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2266 struct mbuf *m;
2267 struct mbuf *mp, *nam;
2268 struct uio auio;
2269 int flags, error;
2270 int setflags = 0;
2271
2272 error = nfsdsock_lock(slp, (waitflag != M_DONTWAIT));
2273 if (error) {
2274 setflags |= SLP_NEEDQ;
2275 goto dorecs_unlocked;
2276 }
2277
2278 KASSERT(so == slp->ns_so);
2279 #define NFS_TEST_HEAVY
2280 #ifdef NFS_TEST_HEAVY
2281 /*
2282 * Define this to test for nfsds handling this under heavy load.
2283 *
2284 * XXX it isn't safe to call so_receive from so_upcall context.
2285 */
2286 if (waitflag == M_DONTWAIT) {
2287 setflags |= SLP_NEEDQ;
2288 goto dorecs;
2289 }
2290 #endif
2291 simple_lock(&slp->ns_lock);
2292 slp->ns_flag &= ~SLP_NEEDQ;
2293 simple_unlock(&slp->ns_lock);
2294 if (so->so_type == SOCK_STREAM) {
2295 #ifndef NFS_TEST_HEAVY
2296 /*
2297 * If there are already records on the queue, defer soreceive()
2298 * to an nfsd so that there is feedback to the TCP layer that
2299 * the nfs servers are heavily loaded.
2300 */
2301 if (slp->ns_rec && waitflag == M_DONTWAIT) {
2302 setflags |= SLP_NEEDQ;
2303 goto dorecs;
2304 }
2305 #endif
2306
2307 /*
2308 * Do soreceive().
2309 */
2310 auio.uio_resid = 1000000000;
2311 /* not need to setup uio_vmspace */
2312 flags = MSG_DONTWAIT;
2313 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, &flags);
2314 if (error || mp == NULL) {
2315 if (error == EWOULDBLOCK)
2316 setflags |= SLP_NEEDQ;
2317 else
2318 setflags |= SLP_DISCONN;
2319 goto dorecs;
2320 }
2321 m = mp;
2322 if (slp->ns_rawend) {
2323 slp->ns_rawend->m_next = m;
2324 slp->ns_cc += 1000000000 - auio.uio_resid;
2325 } else {
2326 slp->ns_raw = m;
2327 slp->ns_cc = 1000000000 - auio.uio_resid;
2328 }
2329 while (m->m_next)
2330 m = m->m_next;
2331 slp->ns_rawend = m;
2332
2333 /*
2334 * Now try and parse record(s) out of the raw stream data.
2335 */
2336 error = nfsrv_getstream(slp, waitflag);
2337 if (error) {
2338 if (error == EPERM)
2339 setflags |= SLP_DISCONN;
2340 else
2341 setflags |= SLP_NEEDQ;
2342 }
2343 } else {
2344 do {
2345 auio.uio_resid = 1000000000;
2346 /* not need to setup uio_vmspace */
2347 flags = MSG_DONTWAIT;
2348 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL,
2349 &flags);
2350 if (mp) {
2351 if (nam) {
2352 m = nam;
2353 m->m_next = mp;
2354 } else
2355 m = mp;
2356 if (slp->ns_recend)
2357 slp->ns_recend->m_nextpkt = m;
2358 else
2359 slp->ns_rec = m;
2360 slp->ns_recend = m;
2361 m->m_nextpkt = (struct mbuf *)0;
2362 }
2363 if (error) {
2364 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2365 && error != EWOULDBLOCK) {
2366 setflags |= SLP_DISCONN;
2367 goto dorecs;
2368 }
2369 }
2370 } while (mp);
2371 }
2372 dorecs:
2373 nfsdsock_unlock(slp);
2374
2375 dorecs_unlocked:
2376 /*
2377 * Now try and process the request records, non-blocking.
2378 */
2379 if (setflags) {
2380 simple_lock(&slp->ns_lock);
2381 slp->ns_flag |= setflags;
2382 simple_unlock(&slp->ns_lock);
2383 }
2384 if (waitflag == M_DONTWAIT &&
2385 (slp->ns_rec || (slp->ns_flag & (SLP_DISCONN | SLP_NEEDQ)) != 0)) {
2386 nfsrv_wakenfsd(slp);
2387 }
2388 }
2389
2390 int
2391 nfsdsock_lock(struct nfssvc_sock *slp, boolean_t waitok)
2392 {
2393
2394 simple_lock(&slp->ns_lock);
2395 while ((slp->ns_flag & (SLP_BUSY|SLP_VALID)) == SLP_BUSY) {
2396 if (!waitok) {
2397 simple_unlock(&slp->ns_lock);
2398 return EWOULDBLOCK;
2399 }
2400 slp->ns_flag |= SLP_WANT;
2401 ltsleep(&slp->ns_flag, PSOCK, "nslock", 0, &slp->ns_lock);
2402 }
2403 if ((slp->ns_flag & SLP_VALID) == 0) {
2404 simple_unlock(&slp->ns_lock);
2405 return EINVAL;
2406 }
2407 slp->ns_flag |= SLP_BUSY;
2408 simple_unlock(&slp->ns_lock);
2409
2410 return 0;
2411 }
2412
2413 void
2414 nfsdsock_unlock(struct nfssvc_sock *slp)
2415 {
2416
2417 KASSERT((slp->ns_flag & SLP_BUSY) != 0);
2418
2419 simple_lock(&slp->ns_lock);
2420 if ((slp->ns_flag & SLP_WANT) != 0) {
2421 wakeup(&slp->ns_flag);
2422 }
2423 slp->ns_flag &= ~(SLP_BUSY|SLP_WANT);
2424 simple_unlock(&slp->ns_lock);
2425 }
2426
2427 int
2428 nfsdsock_drain(struct nfssvc_sock *slp)
2429 {
2430 int error = 0;
2431
2432 simple_lock(&slp->ns_lock);
2433 if ((slp->ns_flag & SLP_VALID) == 0) {
2434 error = EINVAL;
2435 goto done;
2436 }
2437 slp->ns_flag &= ~SLP_VALID;
2438 while ((slp->ns_flag & SLP_BUSY) != 0) {
2439 slp->ns_flag |= SLP_WANT;
2440 ltsleep(&slp->ns_flag, PSOCK, "nsdrain", 0, &slp->ns_lock);
2441 }
2442 done:
2443 simple_unlock(&slp->ns_lock);
2444
2445 return error;
2446 }
2447
2448 /*
2449 * Try and extract an RPC request from the mbuf data list received on a
2450 * stream socket. The "waitflag" argument indicates whether or not it
2451 * can sleep.
2452 */
2453 int
2454 nfsrv_getstream(slp, waitflag)
2455 struct nfssvc_sock *slp;
2456 int waitflag;
2457 {
2458 struct mbuf *m, **mpp;
2459 struct mbuf *recm;
2460 u_int32_t recmark;
2461 int error = 0;
2462
2463 for (;;) {
2464 if (slp->ns_reclen == 0) {
2465 if (slp->ns_cc < NFSX_UNSIGNED) {
2466 break;
2467 }
2468 m = slp->ns_raw;
2469 m_copydata(m, 0, NFSX_UNSIGNED, (caddr_t)&recmark);
2470 m_adj(m, NFSX_UNSIGNED);
2471 slp->ns_cc -= NFSX_UNSIGNED;
2472 recmark = ntohl(recmark);
2473 slp->ns_reclen = recmark & ~0x80000000;
2474 if (recmark & 0x80000000)
2475 slp->ns_flag |= SLP_LASTFRAG;
2476 else
2477 slp->ns_flag &= ~SLP_LASTFRAG;
2478 if (slp->ns_reclen > NFS_MAXPACKET) {
2479 error = EPERM;
2480 break;
2481 }
2482 }
2483
2484 /*
2485 * Now get the record part.
2486 *
2487 * Note that slp->ns_reclen may be 0. Linux sometimes
2488 * generates 0-length records.
2489 */
2490 if (slp->ns_cc == slp->ns_reclen) {
2491 recm = slp->ns_raw;
2492 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2493 slp->ns_cc = slp->ns_reclen = 0;
2494 } else if (slp->ns_cc > slp->ns_reclen) {
2495 recm = slp->ns_raw;
2496 m = m_split(recm, slp->ns_reclen, waitflag);
2497 if (m == NULL) {
2498 error = EWOULDBLOCK;
2499 break;
2500 }
2501 m_claimm(recm, &nfs_mowner);
2502 slp->ns_raw = m;
2503 if (m->m_next == NULL)
2504 slp->ns_rawend = m;
2505 slp->ns_cc -= slp->ns_reclen;
2506 slp->ns_reclen = 0;
2507 } else {
2508 break;
2509 }
2510
2511 /*
2512 * Accumulate the fragments into a record.
2513 */
2514 mpp = &slp->ns_frag;
2515 while (*mpp)
2516 mpp = &((*mpp)->m_next);
2517 *mpp = recm;
2518 if (slp->ns_flag & SLP_LASTFRAG) {
2519 if (slp->ns_recend)
2520 slp->ns_recend->m_nextpkt = slp->ns_frag;
2521 else
2522 slp->ns_rec = slp->ns_frag;
2523 slp->ns_recend = slp->ns_frag;
2524 slp->ns_frag = (struct mbuf *)0;
2525 }
2526 }
2527
2528 return error;
2529 }
2530
2531 /*
2532 * Parse an RPC header.
2533 */
2534 int
2535 nfsrv_dorec(slp, nfsd, ndp)
2536 struct nfssvc_sock *slp;
2537 struct nfsd *nfsd;
2538 struct nfsrv_descript **ndp;
2539 {
2540 struct mbuf *m, *nam;
2541 struct nfsrv_descript *nd;
2542 int error;
2543
2544 *ndp = NULL;
2545
2546 if (nfsdsock_lock(slp, TRUE)) {
2547 return ENOBUFS;
2548 }
2549 m = slp->ns_rec;
2550 if (m == NULL) {
2551 nfsdsock_unlock(slp);
2552 return ENOBUFS;
2553 }
2554 slp->ns_rec = m->m_nextpkt;
2555 if (slp->ns_rec)
2556 m->m_nextpkt = NULL;
2557 else
2558 slp->ns_recend = NULL;
2559 nfsdsock_unlock(slp);
2560
2561 if (m->m_type == MT_SONAME) {
2562 nam = m;
2563 m = m->m_next;
2564 nam->m_next = NULL;
2565 } else
2566 nam = NULL;
2567 nd = nfsdreq_alloc();
2568 nd->nd_md = nd->nd_mrep = m;
2569 nd->nd_nam2 = nam;
2570 nd->nd_dpos = mtod(m, caddr_t);
2571 error = nfs_getreq(nd, nfsd, TRUE);
2572 if (error) {
2573 m_freem(nam);
2574 nfsdreq_free(nd);
2575 return (error);
2576 }
2577 *ndp = nd;
2578 nfsd->nfsd_nd = nd;
2579 return (0);
2580 }
2581
2582 /*
2583 * Search for a sleeping nfsd and wake it up.
2584 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2585 * running nfsds will go look for the work in the nfssvc_sock list.
2586 */
2587 void
2588 nfsrv_wakenfsd(slp)
2589 struct nfssvc_sock *slp;
2590 {
2591 struct nfsd *nd;
2592
2593 if ((slp->ns_flag & SLP_VALID) == 0)
2594 return;
2595 simple_lock(&nfsd_slock);
2596 if (slp->ns_flag & SLP_DOREC) {
2597 simple_unlock(&nfsd_slock);
2598 return;
2599 }
2600 nd = SLIST_FIRST(&nfsd_idle_head);
2601 if (nd) {
2602 SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle);
2603 simple_unlock(&nfsd_slock);
2604
2605 if (nd->nfsd_slp)
2606 panic("nfsd wakeup");
2607 slp->ns_sref++;
2608 nd->nfsd_slp = slp;
2609 wakeup(nd);
2610 return;
2611 }
2612 slp->ns_flag |= SLP_DOREC;
2613 nfsd_head_flag |= NFSD_CHECKSLP;
2614 TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending);
2615 simple_unlock(&nfsd_slock);
2616 }
2617
2618 int
2619 nfsdsock_sendreply(struct nfssvc_sock *slp, struct nfsrv_descript *nd)
2620 {
2621 int error;
2622
2623 if (nd->nd_mrep != NULL) {
2624 m_freem(nd->nd_mrep);
2625 nd->nd_mrep = NULL;
2626 }
2627
2628 simple_lock(&slp->ns_lock);
2629 if ((slp->ns_flag & SLP_SENDING) != 0) {
2630 SIMPLEQ_INSERT_TAIL(&slp->ns_sendq, nd, nd_sendq);
2631 simple_unlock(&slp->ns_lock);
2632 return 0;
2633 }
2634 KASSERT(SIMPLEQ_EMPTY(&slp->ns_sendq));
2635 slp->ns_flag |= SLP_SENDING;
2636 simple_unlock(&slp->ns_lock);
2637
2638 again:
2639 error = nfs_send(slp->ns_so, nd->nd_nam2, nd->nd_mreq, NULL, curlwp);
2640 if (nd->nd_nam2) {
2641 m_free(nd->nd_nam2);
2642 }
2643 nfsdreq_free(nd);
2644
2645 simple_lock(&slp->ns_lock);
2646 KASSERT((slp->ns_flag & SLP_SENDING) != 0);
2647 nd = SIMPLEQ_FIRST(&slp->ns_sendq);
2648 if (nd != NULL) {
2649 SIMPLEQ_REMOVE_HEAD(&slp->ns_sendq, nd_sendq);
2650 simple_unlock(&slp->ns_lock);
2651 goto again;
2652 }
2653 slp->ns_flag &= ~SLP_SENDING;
2654 simple_unlock(&slp->ns_lock);
2655
2656 return error;
2657 }
2658 #endif /* NFSSERVER */
2659
2660 #if defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY))
2661 static struct pool nfs_srvdesc_pool;
2662
2663 void
2664 nfsdreq_init(void)
2665 {
2666
2667 pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
2668 0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr);
2669 }
2670
2671 struct nfsrv_descript *
2672 nfsdreq_alloc(void)
2673 {
2674 struct nfsrv_descript *nd;
2675
2676 nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
2677 nd->nd_cr = NULL;
2678 return nd;
2679 }
2680
2681 void
2682 nfsdreq_free(struct nfsrv_descript *nd)
2683 {
2684 kauth_cred_t cr;
2685
2686 cr = nd->nd_cr;
2687 if (cr != NULL) {
2688 KASSERT(kauth_cred_getrefcnt(cr) == 1);
2689 kauth_cred_free(cr);
2690 }
2691 pool_put(&nfs_srvdesc_pool, nd);
2692 }
2693 #endif /* defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY)) */
2694