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