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