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