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