nfs_socket.c revision 1.173.4.6 1 /* $NetBSD: nfs_socket.c,v 1.173.4.6 2011/03/29 19:36:50 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.6 2011/03/29 19:36:50 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 if (error != EMSGSIZE)
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 != EMSGSIZE)
562 error = 0;
563 }
564 return (error);
565 }
566
567 #ifdef NFS
568 /*
569 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
570 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
571 * Mark and consolidate the data into a new mbuf list.
572 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
573 * small mbufs.
574 * For SOCK_STREAM we must be very careful to read an entire record once
575 * we have read any of it, even if the system call has been interrupted.
576 */
577 static int
578 nfs_receive(struct nfsreq *rep, struct mbuf **aname, struct mbuf **mp,
579 struct lwp *l)
580 {
581 struct socket *so;
582 struct uio auio;
583 struct iovec aio;
584 struct mbuf *m;
585 struct mbuf *control;
586 u_int32_t len;
587 struct mbuf **getnam;
588 int error, sotype, rcvflg;
589
590 /*
591 * Set up arguments for soreceive()
592 */
593 *mp = (struct mbuf *)0;
594 *aname = (struct mbuf *)0;
595 sotype = rep->r_nmp->nm_sotype;
596
597 /*
598 * For reliable protocols, lock against other senders/receivers
599 * in case a reconnect is necessary.
600 * For SOCK_STREAM, first get the Record Mark to find out how much
601 * more there is to get.
602 * We must lock the socket against other receivers
603 * until we have an entire rpc request/reply.
604 */
605 if (sotype != SOCK_DGRAM) {
606 error = nfs_sndlock(rep->r_nmp, rep);
607 if (error)
608 return (error);
609 tryagain:
610 /*
611 * Check for fatal errors and resending request.
612 */
613 /*
614 * Ugh: If a reconnect attempt just happened, nm_so
615 * would have changed. NULL indicates a failed
616 * attempt that has essentially shut down this
617 * mount point.
618 */
619 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
620 nfs_sndunlock(rep->r_nmp);
621 return (EINTR);
622 }
623 so = rep->r_nmp->nm_so;
624 if (!so) {
625 error = nfs_reconnect(rep);
626 if (error) {
627 nfs_sndunlock(rep->r_nmp);
628 return (error);
629 }
630 goto tryagain;
631 }
632 while (rep->r_flags & R_MUSTRESEND) {
633 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
634 nfsstats.rpcretries++;
635 rep->r_rtt = 0;
636 rep->r_flags &= ~R_TIMING;
637 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep, l);
638 if (error) {
639 if (error == EINTR || error == ERESTART ||
640 (error = nfs_reconnect(rep)) != 0) {
641 nfs_sndunlock(rep->r_nmp);
642 return (error);
643 }
644 goto tryagain;
645 }
646 }
647 nfs_sndunlock(rep->r_nmp);
648 if (sotype == SOCK_STREAM) {
649 aio.iov_base = (void *) &len;
650 aio.iov_len = sizeof(u_int32_t);
651 auio.uio_iov = &aio;
652 auio.uio_iovcnt = 1;
653 auio.uio_rw = UIO_READ;
654 auio.uio_offset = 0;
655 auio.uio_resid = sizeof(u_int32_t);
656 UIO_SETUP_SYSSPACE(&auio);
657 do {
658 rcvflg = MSG_WAITALL;
659 error = (*so->so_receive)(so, (struct mbuf **)0, &auio,
660 (struct mbuf **)0, (struct mbuf **)0, &rcvflg);
661 if (error == EWOULDBLOCK && rep) {
662 if (rep->r_flags & R_SOFTTERM)
663 return (EINTR);
664 /*
665 * if it seems that the server died after it
666 * received our request, set EPIPE so that
667 * we'll reconnect and retransmit requests.
668 */
669 if (rep->r_rexmit >= rep->r_nmp->nm_retry) {
670 nfsstats.rpctimeouts++;
671 error = EPIPE;
672 }
673 }
674 } while (error == EWOULDBLOCK);
675 if (!error && auio.uio_resid > 0) {
676 /*
677 * Don't log a 0 byte receive; it means
678 * that the socket has been closed, and
679 * can happen during normal operation
680 * (forcible unmount or Solaris server).
681 */
682 if (auio.uio_resid != sizeof (u_int32_t))
683 log(LOG_INFO,
684 "short receive (%lu/%lu) from nfs server %s\n",
685 (u_long)sizeof(u_int32_t) - auio.uio_resid,
686 (u_long)sizeof(u_int32_t),
687 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
688 error = EPIPE;
689 }
690 if (error)
691 goto errout;
692 len = ntohl(len) & ~0x80000000;
693 /*
694 * This is SERIOUS! We are out of sync with the sender
695 * and forcing a disconnect/reconnect is all I can do.
696 */
697 if (len > NFS_MAXPACKET) {
698 log(LOG_ERR, "%s (%d) from nfs server %s\n",
699 "impossible packet length",
700 len,
701 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
702 error = EFBIG;
703 goto errout;
704 }
705 auio.uio_resid = len;
706 do {
707 rcvflg = MSG_WAITALL;
708 error = (*so->so_receive)(so, (struct mbuf **)0,
709 &auio, mp, (struct mbuf **)0, &rcvflg);
710 } while (error == EWOULDBLOCK || error == EINTR ||
711 error == ERESTART);
712 if (!error && auio.uio_resid > 0) {
713 if (len != auio.uio_resid)
714 log(LOG_INFO,
715 "short receive (%lu/%d) from nfs server %s\n",
716 (u_long)len - auio.uio_resid, len,
717 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
718 error = EPIPE;
719 }
720 } else {
721 /*
722 * NB: Since uio_resid is big, MSG_WAITALL is ignored
723 * and soreceive() will return when it has either a
724 * control msg or a data msg.
725 * We have no use for control msg., but must grab them
726 * and then throw them away so we know what is going
727 * on.
728 */
729 auio.uio_resid = len = 100000000; /* Anything Big */
730 /* not need to setup uio_vmspace */
731 do {
732 rcvflg = 0;
733 error = (*so->so_receive)(so, (struct mbuf **)0,
734 &auio, mp, &control, &rcvflg);
735 if (control)
736 m_freem(control);
737 if (error == EWOULDBLOCK && rep) {
738 if (rep->r_flags & R_SOFTTERM)
739 return (EINTR);
740 }
741 } while (error == EWOULDBLOCK ||
742 (!error && *mp == NULL && control));
743 if ((rcvflg & MSG_EOR) == 0)
744 printf("Egad!!\n");
745 if (!error && *mp == NULL)
746 error = EPIPE;
747 len -= auio.uio_resid;
748 }
749 errout:
750 if (error && error != EINTR && error != ERESTART) {
751 m_freem(*mp);
752 *mp = (struct mbuf *)0;
753 if (error != EPIPE)
754 log(LOG_INFO,
755 "receive error %d from nfs server %s\n",
756 error,
757 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
758 error = nfs_sndlock(rep->r_nmp, rep);
759 if (!error)
760 error = nfs_reconnect(rep);
761 if (!error)
762 goto tryagain;
763 else
764 nfs_sndunlock(rep->r_nmp);
765 }
766 } else {
767 if ((so = rep->r_nmp->nm_so) == NULL)
768 return (EACCES);
769 if (so->so_state & SS_ISCONNECTED)
770 getnam = (struct mbuf **)0;
771 else
772 getnam = aname;
773 auio.uio_resid = len = 1000000;
774 /* not need to setup uio_vmspace */
775 do {
776 rcvflg = 0;
777 error = (*so->so_receive)(so, getnam, &auio, mp,
778 (struct mbuf **)0, &rcvflg);
779 if (error == EWOULDBLOCK &&
780 (rep->r_flags & R_SOFTTERM))
781 return (EINTR);
782 } while (error == EWOULDBLOCK);
783 len -= auio.uio_resid;
784 if (!error && *mp == NULL)
785 error = EPIPE;
786 }
787 if (error) {
788 m_freem(*mp);
789 *mp = (struct mbuf *)0;
790 }
791 return (error);
792 }
793
794 /*
795 * Implement receipt of reply on a socket.
796 * We must search through the list of received datagrams matching them
797 * with outstanding requests using the xid, until ours is found.
798 */
799 /* ARGSUSED */
800 static int
801 nfs_reply(struct nfsreq *myrep, struct lwp *lwp)
802 {
803 struct nfsreq *rep;
804 struct nfsmount *nmp = myrep->r_nmp;
805 int32_t t1;
806 struct mbuf *mrep, *nam, *md;
807 u_int32_t rxid, *tl;
808 char *dpos, *cp2;
809 int error;
810
811 /*
812 * Loop around until we get our own reply
813 */
814 for (;;) {
815 /*
816 * Lock against other receivers so that I don't get stuck in
817 * sbwait() after someone else has received my reply for me.
818 * Also necessary for connection based protocols to avoid
819 * race conditions during a reconnect.
820 */
821 error = nfs_rcvlock(nmp, myrep);
822 if (error == EALREADY)
823 return (0);
824 if (error)
825 return (error);
826 /*
827 * Get the next Rpc reply off the socket
828 */
829
830 mutex_enter(&nmp->nm_lock);
831 nmp->nm_waiters++;
832 mutex_exit(&nmp->nm_lock);
833
834 error = nfs_receive(myrep, &nam, &mrep, lwp);
835
836 mutex_enter(&nmp->nm_lock);
837 nmp->nm_waiters--;
838 cv_signal(&nmp->nm_disconcv);
839 mutex_exit(&nmp->nm_lock);
840
841 if (error) {
842 nfs_rcvunlock(nmp);
843
844 if (nmp->nm_iflag & NFSMNT_DISMNT) {
845 /*
846 * Oops, we're going away now..
847 */
848 return error;
849 }
850 /*
851 * Ignore routing errors on connectionless protocols? ?
852 */
853 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
854 nmp->nm_so->so_error = 0;
855 #ifdef DEBUG
856 if (ratecheck(&nfs_reply_last_err_time,
857 &nfs_err_interval))
858 printf("%s: ignoring error %d\n",
859 __func__, error);
860 #endif
861 continue;
862 }
863 return (error);
864 }
865 if (nam)
866 m_freem(nam);
867
868 /*
869 * Get the xid and check that it is an rpc reply
870 */
871 md = mrep;
872 dpos = mtod(md, void *);
873 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
874 rxid = *tl++;
875 if (*tl != rpc_reply) {
876 nfsstats.rpcinvalid++;
877 m_freem(mrep);
878 nfsmout:
879 nfs_rcvunlock(nmp);
880 continue;
881 }
882
883 /*
884 * Loop through the request list to match up the reply
885 * Iff no match, just drop the datagram
886 */
887 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
888 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
889 /* Found it.. */
890 rep->r_mrep = mrep;
891 rep->r_md = md;
892 rep->r_dpos = dpos;
893 if (nfsrtton) {
894 struct rttl *rt;
895
896 rt = &nfsrtt.rttl[nfsrtt.pos];
897 rt->proc = rep->r_procnum;
898 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
899 rt->sent = nmp->nm_sent;
900 rt->cwnd = nmp->nm_cwnd;
901 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
902 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
903 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsidx;
904 getmicrotime(&rt->tstamp);
905 if (rep->r_flags & R_TIMING)
906 rt->rtt = rep->r_rtt;
907 else
908 rt->rtt = 1000000;
909 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
910 }
911 /*
912 * Update congestion window.
913 * Do the additive increase of
914 * one rpc/rtt.
915 */
916 if (nmp->nm_cwnd <= nmp->nm_sent) {
917 nmp->nm_cwnd +=
918 (NFS_CWNDSCALE * NFS_CWNDSCALE +
919 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
920 if (nmp->nm_cwnd > NFS_MAXCWND)
921 nmp->nm_cwnd = NFS_MAXCWND;
922 }
923 rep->r_flags &= ~R_SENT;
924 nmp->nm_sent -= NFS_CWNDSCALE;
925 /*
926 * Update rtt using a gain of 0.125 on the mean
927 * and a gain of 0.25 on the deviation.
928 */
929 if (rep->r_flags & R_TIMING) {
930 /*
931 * Since the timer resolution of
932 * NFS_HZ is so course, it can often
933 * result in r_rtt == 0. Since
934 * r_rtt == N means that the actual
935 * rtt is between N+dt and N+2-dt ticks,
936 * add 1.
937 */
938 t1 = rep->r_rtt + 1;
939 t1 -= (NFS_SRTT(rep) >> 3);
940 NFS_SRTT(rep) += t1;
941 if (t1 < 0)
942 t1 = -t1;
943 t1 -= (NFS_SDRTT(rep) >> 2);
944 NFS_SDRTT(rep) += t1;
945 }
946 nmp->nm_timeouts = 0;
947 break;
948 }
949 }
950 nfs_rcvunlock(nmp);
951 /*
952 * If not matched to a request, drop it.
953 * If it's mine, get out.
954 */
955 if (rep == 0) {
956 nfsstats.rpcunexpected++;
957 m_freem(mrep);
958 } else if (rep == myrep) {
959 if (rep->r_mrep == NULL)
960 panic("nfsreply nil");
961 return (0);
962 }
963 }
964 }
965
966 /*
967 * nfs_request - goes something like this
968 * - fill in request struct
969 * - links it into list
970 * - calls nfs_send() for first transmit
971 * - calls nfs_receive() to get reply
972 * - break down rpc header and return with nfs reply pointed to
973 * by mrep or error
974 * nb: always frees up mreq mbuf list
975 */
976 int
977 nfs_request(np, mrest, procnum, lwp, cred, mrp, mdp, dposp, rexmitp)
978 struct nfsnode *np;
979 struct mbuf *mrest;
980 int procnum;
981 struct lwp *lwp;
982 kauth_cred_t cred;
983 struct mbuf **mrp;
984 struct mbuf **mdp;
985 char **dposp;
986 int *rexmitp;
987 {
988 struct mbuf *m, *mrep;
989 struct nfsreq *rep;
990 u_int32_t *tl;
991 int i;
992 struct nfsmount *nmp = VFSTONFS(np->n_vnode->v_mount);
993 struct mbuf *md, *mheadend;
994 char nickv[RPCX_NICKVERF];
995 time_t waituntil;
996 char *dpos, *cp2;
997 int t1, s, error = 0, mrest_len, auth_len, auth_type;
998 int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0;
999 int verf_len, verf_type;
1000 u_int32_t xid;
1001 char *auth_str, *verf_str;
1002 NFSKERBKEY_T key; /* save session key */
1003 kauth_cred_t acred;
1004 struct mbuf *mrest_backup = NULL;
1005 kauth_cred_t origcred = NULL; /* XXX: gcc */
1006 bool retry_cred = true;
1007 bool use_opencred = (np->n_flag & NUSEOPENCRED) != 0;
1008
1009 if (rexmitp != NULL)
1010 *rexmitp = 0;
1011
1012 acred = kauth_cred_alloc();
1013
1014 tryagain_cred:
1015 KASSERT(cred != NULL);
1016 rep = kmem_alloc(sizeof(*rep), KM_SLEEP);
1017 rep->r_nmp = nmp;
1018 KASSERT(lwp == NULL || lwp == curlwp);
1019 rep->r_lwp = lwp;
1020 rep->r_procnum = procnum;
1021 i = 0;
1022 m = mrest;
1023 while (m) {
1024 i += m->m_len;
1025 m = m->m_next;
1026 }
1027 mrest_len = i;
1028
1029 /*
1030 * Get the RPC header with authorization.
1031 */
1032 kerbauth:
1033 verf_str = auth_str = (char *)0;
1034 if (nmp->nm_flag & NFSMNT_KERB) {
1035 verf_str = nickv;
1036 verf_len = sizeof (nickv);
1037 auth_type = RPCAUTH_KERB4;
1038 memset((void *)key, 0, sizeof (key));
1039 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
1040 &auth_len, verf_str, verf_len)) {
1041 error = nfs_getauth(nmp, rep, cred, &auth_str,
1042 &auth_len, verf_str, &verf_len, key);
1043 if (error) {
1044 kmem_free(rep, sizeof(*rep));
1045 m_freem(mrest);
1046 KASSERT(kauth_cred_getrefcnt(acred) == 1);
1047 kauth_cred_free(acred);
1048 return (error);
1049 }
1050 }
1051 retry_cred = false;
1052 } else {
1053 /* AUTH_UNIX */
1054 uid_t uid;
1055 gid_t gid;
1056
1057 /*
1058 * on the most unix filesystems, permission checks are
1059 * done when the file is open(2)'ed.
1060 * ie. once a file is successfully open'ed,
1061 * following i/o operations never fail with EACCES.
1062 * we try to follow the semantics as far as possible.
1063 *
1064 * note that we expect that the nfs server always grant
1065 * accesses by the file's owner.
1066 */
1067 origcred = cred;
1068 switch (procnum) {
1069 case NFSPROC_READ:
1070 case NFSPROC_WRITE:
1071 case NFSPROC_COMMIT:
1072 uid = np->n_vattr->va_uid;
1073 gid = np->n_vattr->va_gid;
1074 if (kauth_cred_geteuid(cred) == uid &&
1075 kauth_cred_getegid(cred) == gid) {
1076 retry_cred = false;
1077 break;
1078 }
1079 if (use_opencred)
1080 break;
1081 kauth_cred_setuid(acred, uid);
1082 kauth_cred_seteuid(acred, uid);
1083 kauth_cred_setsvuid(acred, uid);
1084 kauth_cred_setgid(acred, gid);
1085 kauth_cred_setegid(acred, gid);
1086 kauth_cred_setsvgid(acred, gid);
1087 cred = acred;
1088 break;
1089 default:
1090 retry_cred = false;
1091 break;
1092 }
1093 /*
1094 * backup mbuf chain if we can need it later to retry.
1095 *
1096 * XXX maybe we can keep a direct reference to
1097 * mrest without doing m_copym, but it's ...ugly.
1098 */
1099 if (retry_cred)
1100 mrest_backup = m_copym(mrest, 0, M_COPYALL, M_WAIT);
1101 auth_type = RPCAUTH_UNIX;
1102 /* XXX elad - ngroups */
1103 auth_len = (((kauth_cred_ngroups(cred) > nmp->nm_numgrps) ?
1104 nmp->nm_numgrps : kauth_cred_ngroups(cred)) << 2) +
1105 5 * NFSX_UNSIGNED;
1106 }
1107 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1108 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
1109 if (auth_str)
1110 free(auth_str, M_TEMP);
1111
1112 /*
1113 * For stream protocols, insert a Sun RPC Record Mark.
1114 */
1115 if (nmp->nm_sotype == SOCK_STREAM) {
1116 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
1117 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1118 (m->m_pkthdr.len - NFSX_UNSIGNED));
1119 }
1120 rep->r_mreq = m;
1121 rep->r_xid = xid;
1122 tryagain:
1123 if (nmp->nm_flag & NFSMNT_SOFT)
1124 rep->r_retry = nmp->nm_retry;
1125 else
1126 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1127 rep->r_rtt = rep->r_rexmit = 0;
1128 if (proct[procnum] > 0)
1129 rep->r_flags = R_TIMING;
1130 else
1131 rep->r_flags = 0;
1132 rep->r_mrep = NULL;
1133
1134 /*
1135 * Do the client side RPC.
1136 */
1137 nfsstats.rpcrequests++;
1138 /*
1139 * Chain request into list of outstanding requests. Be sure
1140 * to put it LAST so timer finds oldest requests first.
1141 */
1142 s = splsoftnet();
1143 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1144 nfs_timer_start();
1145
1146 /*
1147 * If backing off another request or avoiding congestion, don't
1148 * send this one now but let timer do it. If not timing a request,
1149 * do it now.
1150 */
1151 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1152 (nmp->nm_flag & NFSMNT_DUMBTIMR) || nmp->nm_sent < nmp->nm_cwnd)) {
1153 splx(s);
1154 if (nmp->nm_soflags & PR_CONNREQUIRED)
1155 error = nfs_sndlock(nmp, rep);
1156 if (!error) {
1157 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
1158 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep, lwp);
1159 if (nmp->nm_soflags & PR_CONNREQUIRED)
1160 nfs_sndunlock(nmp);
1161 }
1162 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
1163 nmp->nm_sent += NFS_CWNDSCALE;
1164 rep->r_flags |= R_SENT;
1165 }
1166 } else {
1167 splx(s);
1168 rep->r_rtt = -1;
1169 }
1170
1171 /*
1172 * Wait for the reply from our send or the timer's.
1173 */
1174 if (!error || error == EPIPE || error == EWOULDBLOCK)
1175 error = nfs_reply(rep, lwp);
1176
1177 /*
1178 * RPC done, unlink the request.
1179 */
1180 s = splsoftnet();
1181 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1182 splx(s);
1183
1184 /*
1185 * Decrement the outstanding request count.
1186 */
1187 if (rep->r_flags & R_SENT) {
1188 rep->r_flags &= ~R_SENT; /* paranoia */
1189 nmp->nm_sent -= NFS_CWNDSCALE;
1190 }
1191
1192 if (rexmitp != NULL) {
1193 int rexmit;
1194
1195 if (nmp->nm_sotype != SOCK_DGRAM)
1196 rexmit = (rep->r_flags & R_REXMITTED) != 0;
1197 else
1198 rexmit = rep->r_rexmit;
1199 *rexmitp = rexmit;
1200 }
1201
1202 /*
1203 * If there was a successful reply and a tprintf msg.
1204 * tprintf a response.
1205 */
1206 if (!error && (rep->r_flags & R_TPRINTFMSG))
1207 nfs_msg(rep->r_lwp, nmp->nm_mountp->mnt_stat.f_mntfromname,
1208 "is alive again");
1209 mrep = rep->r_mrep;
1210 md = rep->r_md;
1211 dpos = rep->r_dpos;
1212 if (error)
1213 goto nfsmout;
1214
1215 /*
1216 * break down the rpc header and check if ok
1217 */
1218 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1219 if (*tl++ == rpc_msgdenied) {
1220 if (*tl == rpc_mismatch)
1221 error = EOPNOTSUPP;
1222 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1223 if (!failed_auth) {
1224 failed_auth++;
1225 mheadend->m_next = (struct mbuf *)0;
1226 m_freem(mrep);
1227 m_freem(rep->r_mreq);
1228 goto kerbauth;
1229 } else
1230 error = EAUTH;
1231 } else
1232 error = EACCES;
1233 m_freem(mrep);
1234 goto nfsmout;
1235 }
1236
1237 /*
1238 * Grab any Kerberos verifier, otherwise just throw it away.
1239 */
1240 verf_type = fxdr_unsigned(int, *tl++);
1241 i = fxdr_unsigned(int32_t, *tl);
1242 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1243 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1244 if (error)
1245 goto nfsmout;
1246 } else if (i > 0)
1247 nfsm_adv(nfsm_rndup(i));
1248 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1249 /* 0 == ok */
1250 if (*tl == 0) {
1251 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1252 if (*tl != 0) {
1253 error = fxdr_unsigned(int, *tl);
1254 switch (error) {
1255 case NFSERR_PERM:
1256 error = EPERM;
1257 break;
1258
1259 case NFSERR_NOENT:
1260 error = ENOENT;
1261 break;
1262
1263 case NFSERR_IO:
1264 error = EIO;
1265 break;
1266
1267 case NFSERR_NXIO:
1268 error = ENXIO;
1269 break;
1270
1271 case NFSERR_ACCES:
1272 error = EACCES;
1273 if (!retry_cred)
1274 break;
1275 m_freem(mrep);
1276 m_freem(rep->r_mreq);
1277 kmem_free(rep, sizeof(*rep));
1278 use_opencred = !use_opencred;
1279 if (mrest_backup == NULL) {
1280 /* m_copym failure */
1281 KASSERT(
1282 kauth_cred_getrefcnt(acred) == 1);
1283 kauth_cred_free(acred);
1284 return ENOMEM;
1285 }
1286 mrest = mrest_backup;
1287 mrest_backup = NULL;
1288 cred = origcred;
1289 error = 0;
1290 retry_cred = false;
1291 goto tryagain_cred;
1292
1293 case NFSERR_EXIST:
1294 error = EEXIST;
1295 break;
1296
1297 case NFSERR_XDEV:
1298 error = EXDEV;
1299 break;
1300
1301 case NFSERR_NODEV:
1302 error = ENODEV;
1303 break;
1304
1305 case NFSERR_NOTDIR:
1306 error = ENOTDIR;
1307 break;
1308
1309 case NFSERR_ISDIR:
1310 error = EISDIR;
1311 break;
1312
1313 case NFSERR_INVAL:
1314 error = EINVAL;
1315 break;
1316
1317 case NFSERR_FBIG:
1318 error = EFBIG;
1319 break;
1320
1321 case NFSERR_NOSPC:
1322 error = ENOSPC;
1323 break;
1324
1325 case NFSERR_ROFS:
1326 error = EROFS;
1327 break;
1328
1329 case NFSERR_MLINK:
1330 error = EMLINK;
1331 break;
1332
1333 case NFSERR_TIMEDOUT:
1334 error = ETIMEDOUT;
1335 break;
1336
1337 case NFSERR_NAMETOL:
1338 error = ENAMETOOLONG;
1339 break;
1340
1341 case NFSERR_NOTEMPTY:
1342 error = ENOTEMPTY;
1343 break;
1344
1345 case NFSERR_DQUOT:
1346 error = EDQUOT;
1347 break;
1348
1349 case NFSERR_STALE:
1350 /*
1351 * If the File Handle was stale, invalidate the
1352 * lookup cache, just in case.
1353 */
1354 error = ESTALE;
1355 cache_purge(NFSTOV(np));
1356 break;
1357
1358 case NFSERR_REMOTE:
1359 error = EREMOTE;
1360 break;
1361
1362 case NFSERR_WFLUSH:
1363 case NFSERR_BADHANDLE:
1364 case NFSERR_NOT_SYNC:
1365 case NFSERR_BAD_COOKIE:
1366 error = EINVAL;
1367 break;
1368
1369 case NFSERR_NOTSUPP:
1370 error = ENOTSUP;
1371 break;
1372
1373 case NFSERR_TOOSMALL:
1374 case NFSERR_SERVERFAULT:
1375 case NFSERR_BADTYPE:
1376 error = EINVAL;
1377 break;
1378
1379 case NFSERR_TRYLATER:
1380 if ((nmp->nm_flag & NFSMNT_NFSV3) == 0)
1381 break;
1382 m_freem(mrep);
1383 error = 0;
1384 waituntil = time_second + trylater_delay;
1385 while (time_second < waituntil) {
1386 kpause("nfstrylater", false, hz, NULL);
1387 }
1388 trylater_delay *= NFS_TRYLATERDELMUL;
1389 if (trylater_delay > NFS_TRYLATERDELMAX)
1390 trylater_delay = NFS_TRYLATERDELMAX;
1391 /*
1392 * RFC1813:
1393 * The client should wait and then try
1394 * the request with a new RPC transaction ID.
1395 */
1396 nfs_renewxid(rep);
1397 goto tryagain;
1398
1399 default:
1400 #ifdef DIAGNOSTIC
1401 printf("Invalid rpc error code %d\n", error);
1402 #endif
1403 error = EINVAL;
1404 break;
1405 }
1406
1407 if (nmp->nm_flag & NFSMNT_NFSV3) {
1408 *mrp = mrep;
1409 *mdp = md;
1410 *dposp = dpos;
1411 error |= NFSERR_RETERR;
1412 } else
1413 m_freem(mrep);
1414 goto nfsmout;
1415 }
1416
1417 /*
1418 * note which credential worked to minimize number of retries.
1419 */
1420 if (use_opencred)
1421 np->n_flag |= NUSEOPENCRED;
1422 else
1423 np->n_flag &= ~NUSEOPENCRED;
1424
1425 *mrp = mrep;
1426 *mdp = md;
1427 *dposp = dpos;
1428
1429 KASSERT(error == 0);
1430 goto nfsmout;
1431 }
1432 m_freem(mrep);
1433 error = EPROTONOSUPPORT;
1434 nfsmout:
1435 KASSERT(kauth_cred_getrefcnt(acred) == 1);
1436 kauth_cred_free(acred);
1437 m_freem(rep->r_mreq);
1438 kmem_free(rep, sizeof(*rep));
1439 m_freem(mrest_backup);
1440 return (error);
1441 }
1442 #endif /* NFS */
1443
1444 /*
1445 * Generate the rpc reply header
1446 * siz arg. is used to decide if adding a cluster is worthwhile
1447 */
1448 int
1449 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1450 int siz;
1451 struct nfsrv_descript *nd;
1452 struct nfssvc_sock *slp;
1453 int err;
1454 int cache;
1455 u_quad_t *frev;
1456 struct mbuf **mrq;
1457 struct mbuf **mbp;
1458 char **bposp;
1459 {
1460 u_int32_t *tl;
1461 struct mbuf *mreq;
1462 char *bpos;
1463 struct mbuf *mb;
1464
1465 mreq = m_gethdr(M_WAIT, MT_DATA);
1466 MCLAIM(mreq, &nfs_mowner);
1467 mb = mreq;
1468 /*
1469 * If this is a big reply, use a cluster else
1470 * try and leave leading space for the lower level headers.
1471 */
1472 siz += RPC_REPLYSIZ;
1473 if (siz >= max_datalen) {
1474 m_clget(mreq, M_WAIT);
1475 } else
1476 mreq->m_data += max_hdr;
1477 tl = mtod(mreq, u_int32_t *);
1478 mreq->m_len = 6 * NFSX_UNSIGNED;
1479 bpos = ((char *)tl) + mreq->m_len;
1480 *tl++ = txdr_unsigned(nd->nd_retxid);
1481 *tl++ = rpc_reply;
1482 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1483 *tl++ = rpc_msgdenied;
1484 if (err & NFSERR_AUTHERR) {
1485 *tl++ = rpc_autherr;
1486 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1487 mreq->m_len -= NFSX_UNSIGNED;
1488 bpos -= NFSX_UNSIGNED;
1489 } else {
1490 *tl++ = rpc_mismatch;
1491 *tl++ = txdr_unsigned(RPC_VER2);
1492 *tl = txdr_unsigned(RPC_VER2);
1493 }
1494 } else {
1495 *tl++ = rpc_msgaccepted;
1496
1497 /*
1498 * For Kerberos authentication, we must send the nickname
1499 * verifier back, otherwise just RPCAUTH_NULL.
1500 */
1501 if (nd->nd_flag & ND_KERBFULL) {
1502 struct nfsuid *nuidp;
1503 struct timeval ktvin, ktvout;
1504
1505 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1506
1507 LIST_FOREACH(nuidp,
1508 NUIDHASH(slp, kauth_cred_geteuid(nd->nd_cr)),
1509 nu_hash) {
1510 if (kauth_cred_geteuid(nuidp->nu_cr) ==
1511 kauth_cred_geteuid(nd->nd_cr) &&
1512 (!nd->nd_nam2 || netaddr_match(
1513 NU_NETFAM(nuidp), &nuidp->nu_haddr,
1514 nd->nd_nam2)))
1515 break;
1516 }
1517 if (nuidp) {
1518 ktvin.tv_sec =
1519 txdr_unsigned(nuidp->nu_timestamp.tv_sec
1520 - 1);
1521 ktvin.tv_usec =
1522 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1523
1524 /*
1525 * Encrypt the timestamp in ecb mode using the
1526 * session key.
1527 */
1528 #ifdef NFSKERB
1529 XXX
1530 #endif
1531
1532 *tl++ = rpc_auth_kerb;
1533 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1534 *tl = ktvout.tv_sec;
1535 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1536 *tl++ = ktvout.tv_usec;
1537 *tl++ = txdr_unsigned(
1538 kauth_cred_geteuid(nuidp->nu_cr));
1539 } else {
1540 *tl++ = 0;
1541 *tl++ = 0;
1542 }
1543 } else {
1544 *tl++ = 0;
1545 *tl++ = 0;
1546 }
1547 switch (err) {
1548 case EPROGUNAVAIL:
1549 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1550 break;
1551 case EPROGMISMATCH:
1552 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1553 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1554 *tl++ = txdr_unsigned(2);
1555 *tl = txdr_unsigned(3);
1556 break;
1557 case EPROCUNAVAIL:
1558 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1559 break;
1560 case EBADRPC:
1561 *tl = txdr_unsigned(RPC_GARBAGE);
1562 break;
1563 default:
1564 *tl = 0;
1565 if (err != NFSERR_RETVOID) {
1566 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1567 if (err)
1568 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1569 else
1570 *tl = 0;
1571 }
1572 break;
1573 };
1574 }
1575
1576 if (mrq != NULL)
1577 *mrq = mreq;
1578 *mbp = mb;
1579 *bposp = bpos;
1580 if (err != 0 && err != NFSERR_RETVOID)
1581 nfsstats.srvrpc_errs++;
1582 return (0);
1583 }
1584
1585 static void
1586 nfs_timer_schedule(void)
1587 {
1588
1589 callout_schedule(&nfs_timer_ch, nfs_ticks);
1590 }
1591
1592 void
1593 nfs_timer_start(void)
1594 {
1595
1596 if (callout_pending(&nfs_timer_ch))
1597 return;
1598
1599 nfs_timer_start_ev.ev_count++;
1600 nfs_timer_schedule();
1601 }
1602
1603 void
1604 nfs_timer_init(void)
1605 {
1606
1607 callout_init(&nfs_timer_ch, 0);
1608 callout_setfunc(&nfs_timer_ch, nfs_timer, NULL);
1609 evcnt_attach_dynamic(&nfs_timer_ev, EVCNT_TYPE_MISC, NULL,
1610 "nfs", "timer");
1611 evcnt_attach_dynamic(&nfs_timer_start_ev, EVCNT_TYPE_MISC, NULL,
1612 "nfs", "timer start");
1613 evcnt_attach_dynamic(&nfs_timer_stop_ev, EVCNT_TYPE_MISC, NULL,
1614 "nfs", "timer stop");
1615 }
1616
1617 /*
1618 * Nfs timer routine
1619 * Scan the nfsreq list and retranmit any requests that have timed out
1620 * To avoid retransmission attempts on STREAM sockets (in the future) make
1621 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1622 */
1623 void
1624 nfs_timer(void *arg)
1625 {
1626 struct nfsreq *rep;
1627 struct mbuf *m;
1628 struct socket *so;
1629 struct nfsmount *nmp;
1630 int timeo;
1631 int error;
1632 bool more = false;
1633 #ifdef NFSSERVER
1634 struct timeval tv;
1635 struct nfssvc_sock *slp;
1636 u_quad_t cur_usec;
1637 #endif
1638
1639 nfs_timer_ev.ev_count++;
1640
1641 mutex_enter(softnet_lock); /* XXX PR 40491 */
1642 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1643 more = true;
1644 nmp = rep->r_nmp;
1645 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1646 continue;
1647 if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
1648 rep->r_flags |= R_SOFTTERM;
1649 continue;
1650 }
1651 if (rep->r_rtt >= 0) {
1652 rep->r_rtt++;
1653 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1654 timeo = nmp->nm_timeo;
1655 else
1656 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1657 if (nmp->nm_timeouts > 0)
1658 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1659 if (timeo > NFS_MAXTIMEO)
1660 timeo = NFS_MAXTIMEO;
1661 if (rep->r_rtt <= timeo)
1662 continue;
1663 if (nmp->nm_timeouts <
1664 (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
1665 nmp->nm_timeouts++;
1666 }
1667 /*
1668 * Check for server not responding
1669 */
1670 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1671 rep->r_rexmit > nmp->nm_deadthresh) {
1672 nfs_msg(rep->r_lwp,
1673 nmp->nm_mountp->mnt_stat.f_mntfromname,
1674 "not responding");
1675 rep->r_flags |= R_TPRINTFMSG;
1676 }
1677 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1678 nfsstats.rpctimeouts++;
1679 rep->r_flags |= R_SOFTTERM;
1680 continue;
1681 }
1682 if (nmp->nm_sotype != SOCK_DGRAM) {
1683 if (++rep->r_rexmit > NFS_MAXREXMIT)
1684 rep->r_rexmit = NFS_MAXREXMIT;
1685 continue;
1686 }
1687 if ((so = nmp->nm_so) == NULL)
1688 continue;
1689
1690 /*
1691 * If there is enough space and the window allows..
1692 * Resend it
1693 * Set r_rtt to -1 in case we fail to send it now.
1694 */
1695 /* solock(so); XXX PR 40491 */
1696 rep->r_rtt = -1;
1697 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1698 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1699 (rep->r_flags & R_SENT) ||
1700 nmp->nm_sent < nmp->nm_cwnd) &&
1701 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1702 if (so->so_state & SS_ISCONNECTED)
1703 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1704 (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1705 else
1706 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1707 nmp->nm_nam, (struct mbuf *)0, (struct lwp *)0);
1708 if (error) {
1709 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
1710 #ifdef DEBUG
1711 if (ratecheck(&nfs_timer_last_err_time,
1712 &nfs_err_interval))
1713 printf("%s: ignoring error "
1714 "%d\n", __func__, error);
1715 #endif
1716 so->so_error = 0;
1717 }
1718 } else {
1719 /*
1720 * Iff first send, start timing
1721 * else turn timing off, backoff timer
1722 * and divide congestion window by 2.
1723 */
1724 if (rep->r_flags & R_SENT) {
1725 rep->r_flags &= ~R_TIMING;
1726 if (++rep->r_rexmit > NFS_MAXREXMIT)
1727 rep->r_rexmit = NFS_MAXREXMIT;
1728 nmp->nm_cwnd >>= 1;
1729 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1730 nmp->nm_cwnd = NFS_CWNDSCALE;
1731 nfsstats.rpcretries++;
1732 } else {
1733 rep->r_flags |= R_SENT;
1734 nmp->nm_sent += NFS_CWNDSCALE;
1735 }
1736 rep->r_rtt = 0;
1737 }
1738 }
1739 /* sounlock(so); XXX PR 40491 */
1740 }
1741 mutex_exit(softnet_lock); /* XXX PR 40491 */
1742
1743 #ifdef NFSSERVER
1744 /*
1745 * Scan the write gathering queues for writes that need to be
1746 * completed now.
1747 */
1748 getmicrotime(&tv);
1749 cur_usec = (u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec;
1750 mutex_enter(&nfsd_lock);
1751 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1752 struct nfsrv_descript *nd;
1753
1754 nd = LIST_FIRST(&slp->ns_tq);
1755 if (nd != NULL) {
1756 if (nd->nd_time <= cur_usec) {
1757 nfsrv_wakenfsd_locked(slp);
1758 }
1759 more = true;
1760 }
1761 }
1762 mutex_exit(&nfsd_lock);
1763 #endif /* NFSSERVER */
1764 if (more) {
1765 nfs_timer_schedule();
1766 } else {
1767 nfs_timer_stop_ev.ev_count++;
1768 }
1769 }
1770
1771 /*
1772 * Test for a termination condition pending on the process.
1773 * This is used for NFSMNT_INT mounts.
1774 */
1775 int
1776 nfs_sigintr(nmp, rep, l)
1777 struct nfsmount *nmp;
1778 struct nfsreq *rep;
1779 struct lwp *l;
1780 {
1781 sigset_t ss;
1782
1783 if (rep && (rep->r_flags & R_SOFTTERM))
1784 return (EINTR);
1785 if (!(nmp->nm_flag & NFSMNT_INT))
1786 return (0);
1787 if (l) {
1788 sigpending1(l, &ss);
1789 #if 0
1790 sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
1791 #endif
1792 if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
1793 sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
1794 sigismember(&ss, SIGQUIT))
1795 return (EINTR);
1796 }
1797 return (0);
1798 }
1799
1800 #ifdef NFS
1801 /*
1802 * Lock a socket against others.
1803 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1804 * and also to avoid race conditions between the processes with nfs requests
1805 * in progress when a reconnect is necessary.
1806 */
1807 static int
1808 nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep)
1809 {
1810 struct lwp *l;
1811 int timeo = 0;
1812 bool catch = false;
1813 int error = 0;
1814
1815 if (rep) {
1816 l = rep->r_lwp;
1817 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1818 catch = true;
1819 } else
1820 l = NULL;
1821 mutex_enter(&nmp->nm_lock);
1822 while ((nmp->nm_iflag & NFSMNT_SNDLOCK) != 0) {
1823 if (rep && nfs_sigintr(rep->r_nmp, rep, l)) {
1824 error = EINTR;
1825 goto quit;
1826 }
1827 if (catch) {
1828 cv_timedwait_sig(&nmp->nm_sndcv, &nmp->nm_lock, timeo);
1829 } else {
1830 cv_timedwait(&nmp->nm_sndcv, &nmp->nm_lock, timeo);
1831 }
1832 if (catch) {
1833 catch = false;
1834 timeo = 2 * hz;
1835 }
1836 }
1837 nmp->nm_iflag |= NFSMNT_SNDLOCK;
1838 quit:
1839 mutex_exit(&nmp->nm_lock);
1840 return error;
1841 }
1842
1843 /*
1844 * Unlock the stream socket for others.
1845 */
1846 static void
1847 nfs_sndunlock(struct nfsmount *nmp)
1848 {
1849
1850 mutex_enter(&nmp->nm_lock);
1851 if ((nmp->nm_iflag & NFSMNT_SNDLOCK) == 0)
1852 panic("nfs sndunlock");
1853 nmp->nm_iflag &= ~NFSMNT_SNDLOCK;
1854 cv_signal(&nmp->nm_sndcv);
1855 mutex_exit(&nmp->nm_lock);
1856 }
1857 #endif /* NFS */
1858
1859 static int
1860 nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
1861 {
1862 int *flagp = &nmp->nm_iflag;
1863 int slptimeo = 0;
1864 bool catch;
1865 int error = 0;
1866
1867 KASSERT(nmp == rep->r_nmp);
1868
1869 catch = (nmp->nm_flag & NFSMNT_INT) != 0;
1870 mutex_enter(&nmp->nm_lock);
1871 while (/* CONSTCOND */ true) {
1872 if (*flagp & NFSMNT_DISMNT) {
1873 cv_signal(&nmp->nm_disconcv);
1874 error = EIO;
1875 break;
1876 }
1877 /* If our reply was received while we were sleeping,
1878 * then just return without taking the lock to avoid a
1879 * situation where a single iod could 'capture' the
1880 * receive lock.
1881 */
1882 if (rep->r_mrep != NULL) {
1883 error = EALREADY;
1884 break;
1885 }
1886 if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
1887 error = EINTR;
1888 break;
1889 }
1890 if ((*flagp & NFSMNT_RCVLOCK) == 0) {
1891 *flagp |= NFSMNT_RCVLOCK;
1892 break;
1893 }
1894 if (catch) {
1895 cv_timedwait_sig(&nmp->nm_rcvcv, &nmp->nm_lock,
1896 slptimeo);
1897 } else {
1898 cv_timedwait(&nmp->nm_rcvcv, &nmp->nm_lock,
1899 slptimeo);
1900 }
1901 if (catch) {
1902 catch = false;
1903 slptimeo = 2 * hz;
1904 }
1905 }
1906 mutex_exit(&nmp->nm_lock);
1907 return error;
1908 }
1909
1910 /*
1911 * Unlock the stream socket for others.
1912 */
1913 static void
1914 nfs_rcvunlock(struct nfsmount *nmp)
1915 {
1916
1917 mutex_enter(&nmp->nm_lock);
1918 if ((nmp->nm_iflag & NFSMNT_RCVLOCK) == 0)
1919 panic("nfs rcvunlock");
1920 nmp->nm_iflag &= ~NFSMNT_RCVLOCK;
1921 cv_broadcast(&nmp->nm_rcvcv);
1922 mutex_exit(&nmp->nm_lock);
1923 }
1924
1925 /*
1926 * Parse an RPC request
1927 * - verify it
1928 * - allocate and fill in the cred.
1929 */
1930 int
1931 nfs_getreq(nd, nfsd, has_header)
1932 struct nfsrv_descript *nd;
1933 struct nfsd *nfsd;
1934 int has_header;
1935 {
1936 int len, i;
1937 u_int32_t *tl;
1938 int32_t t1;
1939 struct uio uio;
1940 struct iovec iov;
1941 char *dpos, *cp2, *cp;
1942 u_int32_t nfsvers, auth_type;
1943 uid_t nickuid;
1944 int error = 0, ticklen;
1945 struct mbuf *mrep, *md;
1946 struct nfsuid *nuidp;
1947 struct timeval tvin, tvout;
1948
1949 memset(&tvout, 0, sizeof tvout); /* XXX gcc */
1950
1951 KASSERT(nd->nd_cr == NULL);
1952 mrep = nd->nd_mrep;
1953 md = nd->nd_md;
1954 dpos = nd->nd_dpos;
1955 if (has_header) {
1956 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1957 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1958 if (*tl++ != rpc_call) {
1959 m_freem(mrep);
1960 return (EBADRPC);
1961 }
1962 } else
1963 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1964 nd->nd_repstat = 0;
1965 nd->nd_flag = 0;
1966 if (*tl++ != rpc_vers) {
1967 nd->nd_repstat = ERPCMISMATCH;
1968 nd->nd_procnum = NFSPROC_NOOP;
1969 return (0);
1970 }
1971 if (*tl != nfs_prog) {
1972 nd->nd_repstat = EPROGUNAVAIL;
1973 nd->nd_procnum = NFSPROC_NOOP;
1974 return (0);
1975 }
1976 tl++;
1977 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1978 if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
1979 nd->nd_repstat = EPROGMISMATCH;
1980 nd->nd_procnum = NFSPROC_NOOP;
1981 return (0);
1982 }
1983 if (nfsvers == NFS_VER3)
1984 nd->nd_flag = ND_NFSV3;
1985 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1986 if (nd->nd_procnum == NFSPROC_NULL)
1987 return (0);
1988 if (nd->nd_procnum > NFSPROC_COMMIT ||
1989 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1990 nd->nd_repstat = EPROCUNAVAIL;
1991 nd->nd_procnum = NFSPROC_NOOP;
1992 return (0);
1993 }
1994 if ((nd->nd_flag & ND_NFSV3) == 0)
1995 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1996 auth_type = *tl++;
1997 len = fxdr_unsigned(int, *tl++);
1998 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1999 m_freem(mrep);
2000 return (EBADRPC);
2001 }
2002
2003 nd->nd_flag &= ~ND_KERBAUTH;
2004 /*
2005 * Handle auth_unix or auth_kerb.
2006 */
2007 if (auth_type == rpc_auth_unix) {
2008 uid_t uid;
2009 gid_t gid;
2010
2011 nd->nd_cr = kauth_cred_alloc();
2012 len = fxdr_unsigned(int, *++tl);
2013 if (len < 0 || len > NFS_MAXNAMLEN) {
2014 m_freem(mrep);
2015 error = EBADRPC;
2016 goto errout;
2017 }
2018 nfsm_adv(nfsm_rndup(len));
2019 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2020
2021 uid = fxdr_unsigned(uid_t, *tl++);
2022 gid = fxdr_unsigned(gid_t, *tl++);
2023 kauth_cred_setuid(nd->nd_cr, uid);
2024 kauth_cred_seteuid(nd->nd_cr, uid);
2025 kauth_cred_setsvuid(nd->nd_cr, uid);
2026 kauth_cred_setgid(nd->nd_cr, gid);
2027 kauth_cred_setegid(nd->nd_cr, gid);
2028 kauth_cred_setsvgid(nd->nd_cr, gid);
2029
2030 len = fxdr_unsigned(int, *tl);
2031 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
2032 m_freem(mrep);
2033 error = EBADRPC;
2034 goto errout;
2035 }
2036 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
2037
2038 if (len > 0) {
2039 size_t grbuf_size = min(len, NGROUPS) * sizeof(gid_t);
2040 gid_t *grbuf = kmem_alloc(grbuf_size, KM_SLEEP);
2041
2042 for (i = 0; i < len; i++) {
2043 if (i < NGROUPS) /* XXX elad */
2044 grbuf[i] = fxdr_unsigned(gid_t, *tl++);
2045 else
2046 tl++;
2047 }
2048 kauth_cred_setgroups(nd->nd_cr, grbuf,
2049 min(len, NGROUPS), -1, UIO_SYSSPACE);
2050 kmem_free(grbuf, grbuf_size);
2051 }
2052
2053 len = fxdr_unsigned(int, *++tl);
2054 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2055 m_freem(mrep);
2056 error = EBADRPC;
2057 goto errout;
2058 }
2059 if (len > 0)
2060 nfsm_adv(nfsm_rndup(len));
2061 } else if (auth_type == rpc_auth_kerb) {
2062 switch (fxdr_unsigned(int, *tl++)) {
2063 case RPCAKN_FULLNAME:
2064 ticklen = fxdr_unsigned(int, *tl);
2065 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
2066 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2067 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2068 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
2069 m_freem(mrep);
2070 error = EBADRPC;
2071 goto errout;
2072 }
2073 uio.uio_offset = 0;
2074 uio.uio_iov = &iov;
2075 uio.uio_iovcnt = 1;
2076 UIO_SETUP_SYSSPACE(&uio);
2077 iov.iov_base = (void *)&nfsd->nfsd_authstr[4];
2078 iov.iov_len = RPCAUTH_MAXSIZ - 4;
2079 nfsm_mtouio(&uio, uio.uio_resid);
2080 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2081 if (*tl++ != rpc_auth_kerb ||
2082 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
2083 printf("Bad kerb verifier\n");
2084 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2085 nd->nd_procnum = NFSPROC_NOOP;
2086 return (0);
2087 }
2088 nfsm_dissect(cp, void *, 4 * NFSX_UNSIGNED);
2089 tl = (u_int32_t *)cp;
2090 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
2091 printf("Not fullname kerb verifier\n");
2092 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2093 nd->nd_procnum = NFSPROC_NOOP;
2094 return (0);
2095 }
2096 cp += NFSX_UNSIGNED;
2097 memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
2098 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2099 nd->nd_flag |= ND_KERBFULL;
2100 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2101 break;
2102 case RPCAKN_NICKNAME:
2103 if (len != 2 * NFSX_UNSIGNED) {
2104 printf("Kerb nickname short\n");
2105 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2106 nd->nd_procnum = NFSPROC_NOOP;
2107 return (0);
2108 }
2109 nickuid = fxdr_unsigned(uid_t, *tl);
2110 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2111 if (*tl++ != rpc_auth_kerb ||
2112 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
2113 printf("Kerb nick verifier bad\n");
2114 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2115 nd->nd_procnum = NFSPROC_NOOP;
2116 return (0);
2117 }
2118 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2119 tvin.tv_sec = *tl++;
2120 tvin.tv_usec = *tl;
2121
2122 LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
2123 nu_hash) {
2124 if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid &&
2125 (!nd->nd_nam2 ||
2126 netaddr_match(NU_NETFAM(nuidp),
2127 &nuidp->nu_haddr, nd->nd_nam2)))
2128 break;
2129 }
2130 if (!nuidp) {
2131 nd->nd_repstat =
2132 (NFSERR_AUTHERR|AUTH_REJECTCRED);
2133 nd->nd_procnum = NFSPROC_NOOP;
2134 return (0);
2135 }
2136
2137 /*
2138 * Now, decrypt the timestamp using the session key
2139 * and validate it.
2140 */
2141 #ifdef NFSKERB
2142 XXX
2143 #endif
2144
2145 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2146 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2147 if (nuidp->nu_expire < time_second ||
2148 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2149 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2150 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2151 nuidp->nu_expire = 0;
2152 nd->nd_repstat =
2153 (NFSERR_AUTHERR|AUTH_REJECTVERF);
2154 nd->nd_procnum = NFSPROC_NOOP;
2155 return (0);
2156 }
2157 kauth_cred_hold(nuidp->nu_cr);
2158 nd->nd_cr = nuidp->nu_cr;
2159 nd->nd_flag |= ND_KERBNICK;
2160 }
2161 } else {
2162 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2163 nd->nd_procnum = NFSPROC_NOOP;
2164 return (0);
2165 }
2166
2167 nd->nd_md = md;
2168 nd->nd_dpos = dpos;
2169 KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0)
2170 || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0));
2171 return (0);
2172 nfsmout:
2173 errout:
2174 KASSERT(error != 0);
2175 if (nd->nd_cr != NULL) {
2176 kauth_cred_free(nd->nd_cr);
2177 nd->nd_cr = NULL;
2178 }
2179 return (error);
2180 }
2181
2182 int
2183 nfs_msg(l, server, msg)
2184 struct lwp *l;
2185 const char *server, *msg;
2186 {
2187 tpr_t tpr;
2188
2189 if (l)
2190 tpr = tprintf_open(l->l_proc);
2191 else
2192 tpr = NULL;
2193 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2194 tprintf_close(tpr);
2195 return (0);
2196 }
2197
2198 #ifdef NFSSERVER
2199 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
2200 struct nfssvc_sock *, struct lwp *,
2201 struct mbuf **)) = {
2202 nfsrv_null,
2203 nfsrv_getattr,
2204 nfsrv_setattr,
2205 nfsrv_lookup,
2206 nfsrv3_access,
2207 nfsrv_readlink,
2208 nfsrv_read,
2209 nfsrv_write,
2210 nfsrv_create,
2211 nfsrv_mkdir,
2212 nfsrv_symlink,
2213 nfsrv_mknod,
2214 nfsrv_remove,
2215 nfsrv_rmdir,
2216 nfsrv_rename,
2217 nfsrv_link,
2218 nfsrv_readdir,
2219 nfsrv_readdirplus,
2220 nfsrv_statfs,
2221 nfsrv_fsinfo,
2222 nfsrv_pathconf,
2223 nfsrv_commit,
2224 nfsrv_noop
2225 };
2226
2227 /*
2228 * Socket upcall routine for the nfsd sockets.
2229 * The void *arg is a pointer to the "struct nfssvc_sock".
2230 */
2231 void
2232 nfsrv_soupcall(struct socket *so, void *arg, int waitflag)
2233 {
2234 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2235
2236 nfsdsock_setbits(slp, SLP_A_NEEDQ);
2237 nfsrv_wakenfsd(slp);
2238 }
2239
2240 void
2241 nfsrv_rcv(struct nfssvc_sock *slp)
2242 {
2243 struct socket *so;
2244 struct mbuf *m;
2245 struct mbuf *mp, *nam;
2246 struct uio auio;
2247 int flags;
2248 int error;
2249 int setflags = 0;
2250
2251 error = nfsdsock_lock(slp, true);
2252 if (error) {
2253 setflags |= SLP_A_NEEDQ;
2254 goto dorecs_unlocked;
2255 }
2256
2257 nfsdsock_clearbits(slp, SLP_A_NEEDQ);
2258
2259 so = slp->ns_so;
2260 if (so->so_type == SOCK_STREAM) {
2261 /*
2262 * Do soreceive().
2263 */
2264 auio.uio_resid = 1000000000;
2265 /* not need to setup uio_vmspace */
2266 flags = MSG_DONTWAIT;
2267 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, &flags);
2268 if (error || mp == NULL) {
2269 if (error == EWOULDBLOCK)
2270 setflags |= SLP_A_NEEDQ;
2271 else
2272 setflags |= SLP_A_DISCONN;
2273 goto dorecs;
2274 }
2275 m = mp;
2276 m_claimm(m, &nfs_mowner);
2277 if (slp->ns_rawend) {
2278 slp->ns_rawend->m_next = m;
2279 slp->ns_cc += 1000000000 - auio.uio_resid;
2280 } else {
2281 slp->ns_raw = m;
2282 slp->ns_cc = 1000000000 - auio.uio_resid;
2283 }
2284 while (m->m_next)
2285 m = m->m_next;
2286 slp->ns_rawend = m;
2287
2288 /*
2289 * Now try and parse record(s) out of the raw stream data.
2290 */
2291 error = nfsrv_getstream(slp, M_WAIT);
2292 if (error) {
2293 if (error == EPERM)
2294 setflags |= SLP_A_DISCONN;
2295 else
2296 setflags |= SLP_A_NEEDQ;
2297 }
2298 } else {
2299 do {
2300 auio.uio_resid = 1000000000;
2301 /* not need to setup uio_vmspace */
2302 flags = MSG_DONTWAIT;
2303 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL,
2304 &flags);
2305 if (mp) {
2306 if (nam) {
2307 m = nam;
2308 m->m_next = mp;
2309 } else
2310 m = mp;
2311 m_claimm(m, &nfs_mowner);
2312 if (slp->ns_recend)
2313 slp->ns_recend->m_nextpkt = m;
2314 else
2315 slp->ns_rec = m;
2316 slp->ns_recend = m;
2317 m->m_nextpkt = (struct mbuf *)0;
2318 }
2319 if (error) {
2320 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2321 && error != EWOULDBLOCK) {
2322 setflags |= SLP_A_DISCONN;
2323 goto dorecs;
2324 }
2325 }
2326 } while (mp);
2327 }
2328 dorecs:
2329 nfsdsock_unlock(slp);
2330
2331 dorecs_unlocked:
2332 if (setflags) {
2333 nfsdsock_setbits(slp, setflags);
2334 }
2335 }
2336
2337 int
2338 nfsdsock_lock(struct nfssvc_sock *slp, bool waitok)
2339 {
2340
2341 mutex_enter(&slp->ns_lock);
2342 while ((~slp->ns_flags & (SLP_BUSY|SLP_VALID)) == 0) {
2343 if (!waitok) {
2344 mutex_exit(&slp->ns_lock);
2345 return EWOULDBLOCK;
2346 }
2347 cv_wait(&slp->ns_cv, &slp->ns_lock);
2348 }
2349 if ((slp->ns_flags & SLP_VALID) == 0) {
2350 mutex_exit(&slp->ns_lock);
2351 return EINVAL;
2352 }
2353 KASSERT((slp->ns_flags & SLP_BUSY) == 0);
2354 slp->ns_flags |= SLP_BUSY;
2355 mutex_exit(&slp->ns_lock);
2356
2357 return 0;
2358 }
2359
2360 void
2361 nfsdsock_unlock(struct nfssvc_sock *slp)
2362 {
2363
2364 mutex_enter(&slp->ns_lock);
2365 KASSERT((slp->ns_flags & SLP_BUSY) != 0);
2366 cv_broadcast(&slp->ns_cv);
2367 slp->ns_flags &= ~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_flags & SLP_VALID) == 0) {
2378 error = EINVAL;
2379 goto done;
2380 }
2381 slp->ns_flags &= ~SLP_VALID;
2382 while ((slp->ns_flags & 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_flags & 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_sflags |= SLP_S_LASTFRAG;
2420 else
2421 slp->ns_sflags &= ~SLP_S_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_sflags & SLP_S_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 = NULL;
2469 }
2470 }
2471
2472 return error;
2473 }
2474
2475 /*
2476 * Parse an RPC header.
2477 */
2478 int
2479 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
2480 struct nfsrv_descript **ndp, bool *more)
2481 {
2482 struct mbuf *m, *nam;
2483 struct nfsrv_descript *nd;
2484 int error;
2485
2486 *ndp = NULL;
2487 *more = false;
2488
2489 if (nfsdsock_lock(slp, true)) {
2490 return ENOBUFS;
2491 }
2492 m = slp->ns_rec;
2493 if (m == NULL) {
2494 nfsdsock_unlock(slp);
2495 return ENOBUFS;
2496 }
2497 slp->ns_rec = m->m_nextpkt;
2498 if (slp->ns_rec) {
2499 m->m_nextpkt = NULL;
2500 *more = true;
2501 } else {
2502 slp->ns_recend = NULL;
2503 }
2504 nfsdsock_unlock(slp);
2505
2506 if (m->m_type == MT_SONAME) {
2507 nam = m;
2508 m = m->m_next;
2509 nam->m_next = NULL;
2510 } else
2511 nam = NULL;
2512 nd = nfsdreq_alloc();
2513 nd->nd_md = nd->nd_mrep = m;
2514 nd->nd_nam2 = nam;
2515 nd->nd_dpos = mtod(m, void *);
2516 error = nfs_getreq(nd, nfsd, true);
2517 if (error) {
2518 m_freem(nam);
2519 nfsdreq_free(nd);
2520 return (error);
2521 }
2522 *ndp = nd;
2523 nfsd->nfsd_nd = nd;
2524 return (0);
2525 }
2526
2527 /*
2528 * Search for a sleeping nfsd and wake it up.
2529 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2530 * running nfsds will go look for the work in the nfssvc_sock list.
2531 */
2532 static void
2533 nfsrv_wakenfsd_locked(struct nfssvc_sock *slp)
2534 {
2535 struct nfsd *nd;
2536
2537 KASSERT(mutex_owned(&nfsd_lock));
2538
2539 if ((slp->ns_flags & SLP_VALID) == 0)
2540 return;
2541 if (slp->ns_gflags & SLP_G_DOREC)
2542 return;
2543 nd = SLIST_FIRST(&nfsd_idle_head);
2544 if (nd) {
2545 SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle);
2546 if (nd->nfsd_slp)
2547 panic("nfsd wakeup");
2548 slp->ns_sref++;
2549 KASSERT(slp->ns_sref > 0);
2550 nd->nfsd_slp = slp;
2551 cv_signal(&nd->nfsd_cv);
2552 } else {
2553 slp->ns_gflags |= SLP_G_DOREC;
2554 nfsd_head_flag |= NFSD_CHECKSLP;
2555 TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending);
2556 }
2557 }
2558
2559 void
2560 nfsrv_wakenfsd(struct nfssvc_sock *slp)
2561 {
2562
2563 mutex_enter(&nfsd_lock);
2564 nfsrv_wakenfsd_locked(slp);
2565 mutex_exit(&nfsd_lock);
2566 }
2567
2568 int
2569 nfsdsock_sendreply(struct nfssvc_sock *slp, struct nfsrv_descript *nd)
2570 {
2571 int error;
2572
2573 if (nd->nd_mrep != NULL) {
2574 m_freem(nd->nd_mrep);
2575 nd->nd_mrep = NULL;
2576 }
2577
2578 mutex_enter(&slp->ns_lock);
2579 if ((slp->ns_flags & SLP_SENDING) != 0) {
2580 SIMPLEQ_INSERT_TAIL(&slp->ns_sendq, nd, nd_sendq);
2581 mutex_exit(&slp->ns_lock);
2582 return 0;
2583 }
2584 KASSERT(SIMPLEQ_EMPTY(&slp->ns_sendq));
2585 slp->ns_flags |= SLP_SENDING;
2586 mutex_exit(&slp->ns_lock);
2587
2588 again:
2589 error = nfs_send(slp->ns_so, nd->nd_nam2, nd->nd_mreq, NULL, curlwp);
2590 if (nd->nd_nam2) {
2591 m_free(nd->nd_nam2);
2592 }
2593 nfsdreq_free(nd);
2594
2595 mutex_enter(&slp->ns_lock);
2596 KASSERT((slp->ns_flags & SLP_SENDING) != 0);
2597 nd = SIMPLEQ_FIRST(&slp->ns_sendq);
2598 if (nd != NULL) {
2599 SIMPLEQ_REMOVE_HEAD(&slp->ns_sendq, nd_sendq);
2600 mutex_exit(&slp->ns_lock);
2601 goto again;
2602 }
2603 slp->ns_flags &= ~SLP_SENDING;
2604 mutex_exit(&slp->ns_lock);
2605
2606 return error;
2607 }
2608
2609 void
2610 nfsdsock_setbits(struct nfssvc_sock *slp, int bits)
2611 {
2612
2613 mutex_enter(&slp->ns_alock);
2614 slp->ns_aflags |= bits;
2615 mutex_exit(&slp->ns_alock);
2616 }
2617
2618 void
2619 nfsdsock_clearbits(struct nfssvc_sock *slp, int bits)
2620 {
2621
2622 mutex_enter(&slp->ns_alock);
2623 slp->ns_aflags &= ~bits;
2624 mutex_exit(&slp->ns_alock);
2625 }
2626
2627 bool
2628 nfsdsock_testbits(struct nfssvc_sock *slp, int bits)
2629 {
2630
2631 return (slp->ns_aflags & bits);
2632 }
2633 #endif /* NFSSERVER */
2634
2635 #if defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY))
2636 static struct pool nfs_srvdesc_pool;
2637
2638 void
2639 nfsdreq_init(void)
2640 {
2641
2642 pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
2643 0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr, IPL_NONE);
2644 }
2645
2646 struct nfsrv_descript *
2647 nfsdreq_alloc(void)
2648 {
2649 struct nfsrv_descript *nd;
2650
2651 nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
2652 nd->nd_cr = NULL;
2653 return nd;
2654 }
2655
2656 void
2657 nfsdreq_free(struct nfsrv_descript *nd)
2658 {
2659 kauth_cred_t cr;
2660
2661 cr = nd->nd_cr;
2662 if (cr != NULL) {
2663 kauth_cred_free(cr);
2664 }
2665 pool_put(&nfs_srvdesc_pool, nd);
2666 }
2667 #endif /* defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY)) */
2668