Don't depend on POSIX namespace pollution with u_char from sys/types.h.
[dragonfly.git] / sys / vfs / nfs / nfs_socket.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1989, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
37 * $FreeBSD: src/sys/nfs/nfs_socket.c,v 1.60.2.6 2003/03/26 01:44:46 alfred Exp $
6b008938 38 * $DragonFly: src/sys/vfs/nfs/nfs_socket.c,v 1.34 2006/04/25 22:11:31 dillon Exp $
984263bc
MD
39 */
40
41/*
42 * Socket operations for use by nfs
43 */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/proc.h>
48#include <sys/malloc.h>
49#include <sys/mount.h>
50#include <sys/kernel.h>
51#include <sys/mbuf.h>
52#include <sys/vnode.h>
53#include <sys/protosw.h>
e4700d00 54#include <sys/resourcevar.h>
984263bc
MD
55#include <sys/socket.h>
56#include <sys/socketvar.h>
6b6e0885 57#include <sys/socketops.h>
984263bc 58#include <sys/syslog.h>
e4700d00 59#include <sys/thread.h>
984263bc
MD
60#include <sys/tprintf.h>
61#include <sys/sysctl.h>
62#include <sys/signalvar.h>
63
64#include <netinet/in.h>
65#include <netinet/tcp.h>
54938b92 66#include <sys/thread2.h>
984263bc 67
1f2de5d4
MD
68#include "rpcv2.h"
69#include "nfsproto.h"
70#include "nfs.h"
71#include "xdr_subs.h"
72#include "nfsm_subs.h"
73#include "nfsmount.h"
74#include "nfsnode.h"
75#include "nfsrtt.h"
984263bc
MD
76
77#define TRUE 1
78#define FALSE 0
79
80/*
81 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
82 * Use the mean and mean deviation of rtt for the appropriate type of rpc
83 * for the frequent rpcs and a default for the others.
84 * The justification for doing "other" this way is that these rpcs
85 * happen so infrequently that timer est. would probably be stale.
86 * Also, since many of these rpcs are
87 * non-idempotent, a conservative timeout is desired.
88 * getattr, lookup - A+2D
89 * read, write - A+4D
90 * other - nm_timeo
91 */
92#define NFS_RTO(n, t) \
93 ((t) == 0 ? (n)->nm_timeo : \
94 ((t) < 3 ? \
95 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
96 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
97#define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
98#define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
99/*
100 * External data, mostly RPC constants in XDR form
101 */
102extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
103 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
104 rpc_auth_kerb;
e07fef60 105extern u_int32_t nfs_prog;
984263bc
MD
106extern struct nfsstats nfsstats;
107extern int nfsv3_procid[NFS_NPROCS];
108extern int nfs_ticks;
109
110/*
111 * Defines which timer to use for the procnum.
112 * 0 - default
113 * 1 - getattr
114 * 2 - lookup
115 * 3 - read
116 * 4 - write
117 */
118static int proct[NFS_NPROCS] = {
119 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
120 0, 0, 0,
121};
122
123static int nfs_realign_test;
124static int nfs_realign_count;
125static int nfs_bufpackets = 4;
91f46891 126static int nfs_timer_raced;
984263bc
MD
127
128SYSCTL_DECL(_vfs_nfs);
129
130SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
131SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
132SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
133
134
135/*
136 * There is a congestion window for outstanding rpcs maintained per mount
137 * point. The cwnd size is adjusted in roughly the way that:
138 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
139 * SIGCOMM '88". ACM, August 1988.
140 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
141 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
142 * of rpcs is in progress.
143 * (The sent count and cwnd are scaled for integer arith.)
144 * Variants of "slow start" were tried and were found to be too much of a
145 * performance hit (ave. rtt 3 times larger),
146 * I suspect due to the large rtt that nfs rpcs have.
147 */
148#define NFS_CWNDSCALE 256
149#define NFS_MAXCWND (NFS_CWNDSCALE * 32)
150static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
151int nfsrtton = 0;
152struct nfsrtt nfsrtt;
f786cc86 153struct callout nfs_timer_handle;
984263bc 154
a6ee311a
RG
155static int nfs_msg (struct thread *,char *,char *);
156static int nfs_rcvlock (struct nfsreq *);
157static void nfs_rcvunlock (struct nfsreq *);
158static void nfs_realign (struct mbuf **pm, int hsiz);
159static int nfs_receive (struct nfsreq *rep, struct sockaddr **aname,
160 struct mbuf **mp);
161static void nfs_softterm (struct nfsreq *rep);
162static int nfs_reconnect (struct nfsreq *rep);
984263bc 163#ifndef NFS_NOSERVER
52553028 164static int nfsrv_getstream (struct nfssvc_sock *, int, int *);
984263bc 165
a6ee311a 166int (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
984263bc 167 struct nfssvc_sock *slp,
dadab5e9 168 struct thread *td,
a6ee311a 169 struct mbuf **mreqp) = {
984263bc
MD
170 nfsrv_null,
171 nfsrv_getattr,
172 nfsrv_setattr,
173 nfsrv_lookup,
174 nfsrv3_access,
175 nfsrv_readlink,
176 nfsrv_read,
177 nfsrv_write,
178 nfsrv_create,
179 nfsrv_mkdir,
180 nfsrv_symlink,
181 nfsrv_mknod,
182 nfsrv_remove,
183 nfsrv_rmdir,
184 nfsrv_rename,
185 nfsrv_link,
186 nfsrv_readdir,
187 nfsrv_readdirplus,
188 nfsrv_statfs,
189 nfsrv_fsinfo,
190 nfsrv_pathconf,
191 nfsrv_commit,
e07fef60
MD
192 nfsrv_noop,
193 nfsrv_noop,
984263bc
MD
194 nfsrv_noop,
195 nfsrv_noop
196};
197#endif /* NFS_NOSERVER */
198
199/*
200 * Initialize sockets and congestion for a new NFS connection.
201 * We do not free the sockaddr if error.
202 */
203int
dadab5e9 204nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
984263bc 205{
dadab5e9 206 struct socket *so;
165dba55 207 int error, rcvreserve, sndreserve;
984263bc
MD
208 int pktscale;
209 struct sockaddr *saddr;
210 struct sockaddr_in *sin;
dadab5e9 211 struct thread *td = &thread0; /* only used for socreate and sobind */
984263bc
MD
212
213 nmp->nm_so = (struct socket *)0;
214 saddr = nmp->nm_nam;
215 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
dadab5e9 216 nmp->nm_soproto, td);
984263bc
MD
217 if (error)
218 goto bad;
219 so = nmp->nm_so;
220 nmp->nm_soflags = so->so_proto->pr_flags;
221
222 /*
223 * Some servers require that the client port be a reserved port number.
224 */
225 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
226 struct sockopt sopt;
227 int ip;
228 struct sockaddr_in ssin;
229
230 bzero(&sopt, sizeof sopt);
231 ip = IP_PORTRANGE_LOW;
984263bc
MD
232 sopt.sopt_level = IPPROTO_IP;
233 sopt.sopt_name = IP_PORTRANGE;
234 sopt.sopt_val = (void *)&ip;
235 sopt.sopt_valsize = sizeof(ip);
dadab5e9 236 sopt.sopt_td = NULL;
984263bc
MD
237 error = sosetopt(so, &sopt);
238 if (error)
239 goto bad;
240 bzero(&ssin, sizeof ssin);
241 sin = &ssin;
242 sin->sin_len = sizeof (struct sockaddr_in);
243 sin->sin_family = AF_INET;
244 sin->sin_addr.s_addr = INADDR_ANY;
245 sin->sin_port = htons(0);
dadab5e9 246 error = sobind(so, (struct sockaddr *)sin, td);
984263bc
MD
247 if (error)
248 goto bad;
249 bzero(&sopt, sizeof sopt);
250 ip = IP_PORTRANGE_DEFAULT;
984263bc
MD
251 sopt.sopt_level = IPPROTO_IP;
252 sopt.sopt_name = IP_PORTRANGE;
253 sopt.sopt_val = (void *)&ip;
254 sopt.sopt_valsize = sizeof(ip);
dadab5e9 255 sopt.sopt_td = NULL;
984263bc
MD
256 error = sosetopt(so, &sopt);
257 if (error)
258 goto bad;
259 }
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 {
dadab5e9 271 error = soconnect(so, nmp->nm_nam, td);
984263bc
MD
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 */
165dba55 280 crit_enter();
984263bc 281 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
377d4740 282 (void) tsleep((caddr_t)&so->so_timeo, 0,
984263bc
MD
283 "nfscon", 2 * hz);
284 if ((so->so_state & SS_ISCONNECTING) &&
285 so->so_error == 0 && rep &&
dadab5e9 286 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0){
984263bc 287 so->so_state &= ~SS_ISCONNECTING;
165dba55 288 crit_exit();
984263bc
MD
289 goto bad;
290 }
291 }
292 if (so->so_error) {
293 error = so->so_error;
294 so->so_error = 0;
165dba55 295 crit_exit();
984263bc
MD
296 goto bad;
297 }
165dba55 298 crit_exit();
984263bc
MD
299 }
300 so->so_rcv.sb_timeo = (5 * hz);
301 so->so_snd.sb_timeo = (5 * hz);
302
303 /*
304 * Get buffer reservation size from sysctl, but impose reasonable
305 * limits.
306 */
307 pktscale = nfs_bufpackets;
308 if (pktscale < 2)
309 pktscale = 2;
310 if (pktscale > 64)
311 pktscale = 64;
312
313 if (nmp->nm_sotype == SOCK_DGRAM) {
314 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
315 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
316 NFS_MAXPKTHDR) * pktscale;
317 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
318 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
319 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
320 NFS_MAXPKTHDR) * pktscale;
321 } else {
322 if (nmp->nm_sotype != SOCK_STREAM)
323 panic("nfscon sotype");
324 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
325 struct sockopt sopt;
326 int val;
327
328 bzero(&sopt, sizeof sopt);
329 sopt.sopt_level = SOL_SOCKET;
330 sopt.sopt_name = SO_KEEPALIVE;
331 sopt.sopt_val = &val;
332 sopt.sopt_valsize = sizeof val;
333 val = 1;
334 sosetopt(so, &sopt);
335 }
336 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
337 struct sockopt sopt;
338 int val;
339
340 bzero(&sopt, sizeof sopt);
341 sopt.sopt_level = IPPROTO_TCP;
342 sopt.sopt_name = TCP_NODELAY;
343 sopt.sopt_val = &val;
344 sopt.sopt_valsize = sizeof val;
345 val = 1;
346 sosetopt(so, &sopt);
347 }
348 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
349 sizeof (u_int32_t)) * pktscale;
350 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
351 sizeof (u_int32_t)) * pktscale;
352 }
e4700d00
JH
353 error = soreserve(so, sndreserve, rcvreserve,
354 &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
984263bc
MD
355 if (error)
356 goto bad;
357 so->so_rcv.sb_flags |= SB_NOINTR;
358 so->so_snd.sb_flags |= SB_NOINTR;
359
360 /* Initialize other non-zero congestion variables */
361 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
362 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
363 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
364 nmp->nm_sdrtt[3] = 0;
365 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
366 nmp->nm_sent = 0;
367 nmp->nm_timeouts = 0;
368 return (0);
369
370bad:
371 nfs_disconnect(nmp);
372 return (error);
373}
374
375/*
376 * Reconnect routine:
377 * Called when a connection is broken on a reliable protocol.
378 * - clean up the old socket
379 * - nfs_connect() again
380 * - set R_MUSTRESEND for all outstanding requests on mount point
381 * If this fails the mount point is DEAD!
382 * nb: Must be called with the nfs_sndlock() set on the mount point.
383 */
384static int
e851b29e 385nfs_reconnect(struct nfsreq *rep)
984263bc 386{
40393ded
RG
387 struct nfsreq *rp;
388 struct nfsmount *nmp = rep->r_nmp;
984263bc
MD
389 int error;
390
391 nfs_disconnect(nmp);
392 while ((error = nfs_connect(nmp, rep)) != 0) {
393 if (error == EINTR || error == ERESTART)
394 return (EINTR);
377d4740 395 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0);
984263bc
MD
396 }
397
398 /*
399 * Loop through outstanding request list and fix up all requests
400 * on old socket.
401 */
91f46891 402 crit_enter();
ecd80f47 403 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
984263bc
MD
404 if (rp->r_nmp == nmp)
405 rp->r_flags |= R_MUSTRESEND;
406 }
91f46891 407 crit_exit();
984263bc
MD
408 return (0);
409}
410
411/*
412 * NFS disconnect. Clean up and unlink.
413 */
414void
e851b29e 415nfs_disconnect(struct nfsmount *nmp)
984263bc 416{
40393ded 417 struct socket *so;
984263bc
MD
418
419 if (nmp->nm_so) {
420 so = nmp->nm_so;
421 nmp->nm_so = (struct socket *)0;
422 soshutdown(so, 2);
423 soclose(so);
424 }
425}
426
427void
e851b29e 428nfs_safedisconnect(struct nfsmount *nmp)
984263bc
MD
429{
430 struct nfsreq dummyreq;
431
432 bzero(&dummyreq, sizeof(dummyreq));
433 dummyreq.r_nmp = nmp;
cd990953 434 dummyreq.r_td = NULL;
984263bc
MD
435 nfs_rcvlock(&dummyreq);
436 nfs_disconnect(nmp);
437 nfs_rcvunlock(&dummyreq);
438}
439
440/*
441 * This is the nfs send routine. For connection based socket types, it
442 * must be called with an nfs_sndlock() on the socket.
443 * "rep == NULL" indicates that it has been called from a server.
444 * For the client side:
445 * - return EINTR if the RPC is terminated, 0 otherwise
446 * - set R_MUSTRESEND if the send fails for any reason
447 * - do any cleanup required by recoverable socket errors (?)
448 * For the server side:
449 * - return EINTR or ERESTART if interrupted by a signal
450 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
451 * - do any cleanup required by recoverable socket errors (?)
452 */
453int
e851b29e
CP
454nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
455 struct nfsreq *rep)
984263bc
MD
456{
457 struct sockaddr *sendnam;
458 int error, soflags, flags;
459
460 if (rep) {
461 if (rep->r_flags & R_SOFTTERM) {
462 m_freem(top);
463 return (EINTR);
464 }
465 if ((so = rep->r_nmp->nm_so) == NULL) {
466 rep->r_flags |= R_MUSTRESEND;
467 m_freem(top);
468 return (0);
469 }
470 rep->r_flags &= ~R_MUSTRESEND;
471 soflags = rep->r_nmp->nm_soflags;
472 } else
473 soflags = so->so_proto->pr_flags;
474 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
475 sendnam = (struct sockaddr *)0;
476 else
477 sendnam = nam;
478 if (so->so_type == SOCK_SEQPACKET)
479 flags = MSG_EOR;
480 else
481 flags = 0;
482
6b6e0885
JH
483 error = so_pru_sosend(so, sendnam, NULL, top, NULL, flags,
484 curthread /*XXX*/);
984263bc
MD
485 /*
486 * ENOBUFS for dgram sockets is transient and non fatal.
487 * No need to log, and no need to break a soft mount.
488 */
489 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
490 error = 0;
491 if (rep) /* do backoff retransmit on client */
492 rep->r_flags |= R_MUSTRESEND;
493 }
494
495 if (error) {
496 if (rep) {
497 log(LOG_INFO, "nfs send error %d for server %s\n",error,
498 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
499 /*
500 * Deal with errors for the client side.
501 */
502 if (rep->r_flags & R_SOFTTERM)
503 error = EINTR;
504 else
505 rep->r_flags |= R_MUSTRESEND;
506 } else
507 log(LOG_INFO, "nfsd send error %d\n", error);
508
509 /*
510 * Handle any recoverable (soft) socket errors here. (?)
511 */
512 if (error != EINTR && error != ERESTART &&
513 error != EWOULDBLOCK && error != EPIPE)
514 error = 0;
515 }
516 return (error);
517}
518
519/*
520 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
521 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
522 * Mark and consolidate the data into a new mbuf list.
523 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
524 * small mbufs.
525 * For SOCK_STREAM we must be very careful to read an entire record once
526 * we have read any of it, even if the system call has been interrupted.
527 */
528static int
dadab5e9 529nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
984263bc 530{
40393ded 531 struct socket *so;
984263bc
MD
532 struct uio auio;
533 struct iovec aio;
40393ded 534 struct mbuf *m;
984263bc
MD
535 struct mbuf *control;
536 u_int32_t len;
537 struct sockaddr **getnam;
538 int error, sotype, rcvflg;
dadab5e9 539 struct thread *td = curthread; /* XXX */
984263bc
MD
540
541 /*
542 * Set up arguments for soreceive()
543 */
544 *mp = (struct mbuf *)0;
545 *aname = (struct sockaddr *)0;
546 sotype = rep->r_nmp->nm_sotype;
547
548 /*
549 * For reliable protocols, lock against other senders/receivers
550 * in case a reconnect is necessary.
551 * For SOCK_STREAM, first get the Record Mark to find out how much
552 * more there is to get.
553 * We must lock the socket against other receivers
554 * until we have an entire rpc request/reply.
555 */
556 if (sotype != SOCK_DGRAM) {
557 error = nfs_sndlock(rep);
558 if (error)
559 return (error);
560tryagain:
561 /*
562 * Check for fatal errors and resending request.
563 */
564 /*
565 * Ugh: If a reconnect attempt just happened, nm_so
566 * would have changed. NULL indicates a failed
567 * attempt that has essentially shut down this
568 * mount point.
569 */
570 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
571 nfs_sndunlock(rep);
572 return (EINTR);
573 }
574 so = rep->r_nmp->nm_so;
575 if (!so) {
576 error = nfs_reconnect(rep);
577 if (error) {
578 nfs_sndunlock(rep);
579 return (error);
580 }
581 goto tryagain;
582 }
583 while (rep->r_flags & R_MUSTRESEND) {
74f1caca 584 m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
984263bc
MD
585 nfsstats.rpcretries++;
586 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
587 if (error) {
588 if (error == EINTR || error == ERESTART ||
589 (error = nfs_reconnect(rep)) != 0) {
590 nfs_sndunlock(rep);
591 return (error);
592 }
593 goto tryagain;
594 }
595 }
596 nfs_sndunlock(rep);
597 if (sotype == SOCK_STREAM) {
598 aio.iov_base = (caddr_t) &len;
599 aio.iov_len = sizeof(u_int32_t);
600 auio.uio_iov = &aio;
601 auio.uio_iovcnt = 1;
602 auio.uio_segflg = UIO_SYSSPACE;
603 auio.uio_rw = UIO_READ;
604 auio.uio_offset = 0;
605 auio.uio_resid = sizeof(u_int32_t);
dadab5e9 606 auio.uio_td = td;
984263bc
MD
607 do {
608 rcvflg = MSG_WAITALL;
6b6e0885
JH
609 error = so_pru_soreceive(so, NULL, &auio, NULL,
610 NULL, &rcvflg);
984263bc
MD
611 if (error == EWOULDBLOCK && rep) {
612 if (rep->r_flags & R_SOFTTERM)
613 return (EINTR);
614 }
615 } while (error == EWOULDBLOCK);
616 if (!error && auio.uio_resid > 0) {
617 /*
618 * Don't log a 0 byte receive; it means
619 * that the socket has been closed, and
620 * can happen during normal operation
621 * (forcible unmount or Solaris server).
622 */
623 if (auio.uio_resid != sizeof (u_int32_t))
624 log(LOG_INFO,
625 "short receive (%d/%d) from nfs server %s\n",
626 (int)(sizeof(u_int32_t) - auio.uio_resid),
627 (int)sizeof(u_int32_t),
628 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
629 error = EPIPE;
630 }
631 if (error)
632 goto errout;
633 len = ntohl(len) & ~0x80000000;
634 /*
635 * This is SERIOUS! We are out of sync with the sender
636 * and forcing a disconnect/reconnect is all I can do.
637 */
638 if (len > NFS_MAXPACKET) {
639 log(LOG_ERR, "%s (%d) from nfs server %s\n",
640 "impossible packet length",
641 len,
642 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
643 error = EFBIG;
644 goto errout;
645 }
646 auio.uio_resid = len;
647 do {
648 rcvflg = MSG_WAITALL;
6b6e0885
JH
649 error = so_pru_soreceive(so, NULL, &auio, mp,
650 NULL, &rcvflg);
984263bc
MD
651 } while (error == EWOULDBLOCK || error == EINTR ||
652 error == ERESTART);
653 if (!error && auio.uio_resid > 0) {
654 if (len != auio.uio_resid)
655 log(LOG_INFO,
656 "short receive (%d/%d) from nfs server %s\n",
657 len - auio.uio_resid, len,
658 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
659 error = EPIPE;
660 }
661 } else {
662 /*
663 * NB: Since uio_resid is big, MSG_WAITALL is ignored
664 * and soreceive() will return when it has either a
665 * control msg or a data msg.
666 * We have no use for control msg., but must grab them
667 * and then throw them away so we know what is going
668 * on.
669 */
670 auio.uio_resid = len = 100000000; /* Anything Big */
dadab5e9 671 auio.uio_td = td;
984263bc
MD
672 do {
673 rcvflg = 0;
6b6e0885
JH
674 error = so_pru_soreceive(so, NULL, &auio, mp,
675 &control, &rcvflg);
984263bc
MD
676 if (control)
677 m_freem(control);
678 if (error == EWOULDBLOCK && rep) {
679 if (rep->r_flags & R_SOFTTERM)
680 return (EINTR);
681 }
682 } while (error == EWOULDBLOCK ||
683 (!error && *mp == NULL && control));
684 if ((rcvflg & MSG_EOR) == 0)
685 printf("Egad!!\n");
686 if (!error && *mp == NULL)
687 error = EPIPE;
688 len -= auio.uio_resid;
689 }
690errout:
691 if (error && error != EINTR && error != ERESTART) {
692 m_freem(*mp);
693 *mp = (struct mbuf *)0;
694 if (error != EPIPE)
695 log(LOG_INFO,
696 "receive error %d from nfs server %s\n",
697 error,
698 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
699 error = nfs_sndlock(rep);
700 if (!error) {
701 error = nfs_reconnect(rep);
702 if (!error)
703 goto tryagain;
704 else
705 nfs_sndunlock(rep);
706 }
707 }
708 } else {
709 if ((so = rep->r_nmp->nm_so) == NULL)
710 return (EACCES);
711 if (so->so_state & SS_ISCONNECTED)
712 getnam = (struct sockaddr **)0;
713 else
714 getnam = aname;
715 auio.uio_resid = len = 1000000;
dadab5e9 716 auio.uio_td = td;
984263bc
MD
717 do {
718 rcvflg = 0;
6b6e0885
JH
719 error = so_pru_soreceive(so, getnam, &auio, mp, NULL,
720 &rcvflg);
984263bc
MD
721 if (error == EWOULDBLOCK &&
722 (rep->r_flags & R_SOFTTERM))
723 return (EINTR);
724 } while (error == EWOULDBLOCK);
725 len -= auio.uio_resid;
726 }
727 if (error) {
728 m_freem(*mp);
729 *mp = (struct mbuf *)0;
730 }
731 /*
732 * Search for any mbufs that are not a multiple of 4 bytes long
733 * or with m_data not longword aligned.
734 * These could cause pointer alignment problems, so copy them to
735 * well aligned mbufs.
736 */
737 nfs_realign(mp, 5 * NFSX_UNSIGNED);
738 return (error);
739}
740
741/*
742 * Implement receipt of reply on a socket.
743 * We must search through the list of received datagrams matching them
744 * with outstanding requests using the xid, until ours is found.
745 */
746/* ARGSUSED */
747int
e851b29e 748nfs_reply(struct nfsreq *myrep)
984263bc 749{
40393ded
RG
750 struct nfsreq *rep;
751 struct nfsmount *nmp = myrep->r_nmp;
752 int32_t t1;
984263bc
MD
753 struct mbuf *mrep, *md;
754 struct sockaddr *nam;
755 u_int32_t rxid, *tl;
756 caddr_t dpos, cp2;
757 int error;
758
759 /*
760 * Loop around until we get our own reply
761 */
762 for (;;) {
763 /*
764 * Lock against other receivers so that I don't get stuck in
765 * sbwait() after someone else has received my reply for me.
766 * Also necessary for connection based protocols to avoid
767 * race conditions during a reconnect.
768 * If nfs_rcvlock() returns EALREADY, that means that
769 * the reply has already been recieved by another
770 * process and we can return immediately. In this
771 * case, the lock is not taken to avoid races with
772 * other processes.
773 */
774 error = nfs_rcvlock(myrep);
775 if (error == EALREADY)
776 return (0);
777 if (error)
778 return (error);
779 /*
780 * Get the next Rpc reply off the socket
781 */
782 error = nfs_receive(myrep, &nam, &mrep);
783 nfs_rcvunlock(myrep);
784 if (error) {
984263bc
MD
785 /*
786 * Ignore routing errors on connectionless protocols??
787 */
788 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
789 nmp->nm_so->so_error = 0;
790 if (myrep->r_flags & R_GETONEREP)
791 return (0);
792 continue;
793 }
794 return (error);
795 }
796 if (nam)
797 FREE(nam, M_SONAME);
798
799 /*
800 * Get the xid and check that it is an rpc reply
801 */
802 md = mrep;
803 dpos = mtod(md, caddr_t);
804 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
805 rxid = *tl++;
806 if (*tl != rpc_reply) {
984263bc
MD
807 nfsstats.rpcinvalid++;
808 m_freem(mrep);
984263bc
MD
809nfsmout:
810 if (myrep->r_flags & R_GETONEREP)
811 return (0);
812 continue;
813 }
814
815 /*
816 * Loop through the request list to match up the reply
91f46891
MD
817 * Iff no match, just drop the datagram. On match, set
818 * r_mrep atomically to prevent the timer from messing
819 * around with the request after we have exited the critical
820 * section.
984263bc 821 */
91f46891 822 crit_enter();
ecd80f47 823 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
984263bc 824 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
984263bc 825 rep->r_mrep = mrep;
91f46891
MD
826 break;
827 }
828 }
829 crit_exit();
830
831 /*
832 * Fill in the rest of the reply if we found a match.
833 */
834 if (rep) {
835 rep->r_md = md;
836 rep->r_dpos = dpos;
837 if (nfsrtton) {
838 struct rttl *rt;
839
840 rt = &nfsrtt.rttl[nfsrtt.pos];
841 rt->proc = rep->r_procnum;
842 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
843 rt->sent = nmp->nm_sent;
844 rt->cwnd = nmp->nm_cwnd;
845 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
846 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
847 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
848 getmicrotime(&rt->tstamp);
849 if (rep->r_flags & R_TIMING)
850 rt->rtt = rep->r_rtt;
851 else
852 rt->rtt = 1000000;
853 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
854 }
855 /*
856 * Update congestion window.
857 * Do the additive increase of
858 * one rpc/rtt.
859 */
860 if (nmp->nm_cwnd <= nmp->nm_sent) {
861 nmp->nm_cwnd +=
862 (NFS_CWNDSCALE * NFS_CWNDSCALE +
863 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
864 if (nmp->nm_cwnd > NFS_MAXCWND)
865 nmp->nm_cwnd = NFS_MAXCWND;
866 }
867 crit_enter(); /* nfs_timer interlock for nm_sent */
868 if (rep->r_flags & R_SENT) {
869 rep->r_flags &= ~R_SENT;
870 nmp->nm_sent -= NFS_CWNDSCALE;
871 }
872 crit_exit();
873 /*
874 * Update rtt using a gain of 0.125 on the mean
875 * and a gain of 0.25 on the deviation.
876 */
877 if (rep->r_flags & R_TIMING) {
984263bc 878 /*
91f46891
MD
879 * Since the timer resolution of
880 * NFS_HZ is so course, it can often
881 * result in r_rtt == 0. Since
882 * r_rtt == N means that the actual
883 * rtt is between N+dt and N+2-dt ticks,
884 * add 1.
984263bc 885 */
91f46891
MD
886 t1 = rep->r_rtt + 1;
887 t1 -= (NFS_SRTT(rep) >> 3);
888 NFS_SRTT(rep) += t1;
889 if (t1 < 0)
890 t1 = -t1;
891 t1 -= (NFS_SDRTT(rep) >> 2);
892 NFS_SDRTT(rep) += t1;
984263bc 893 }
91f46891 894 nmp->nm_timeouts = 0;
984263bc
MD
895 }
896 /*
897 * If not matched to a request, drop it.
898 * If it's mine, get out.
899 */
91f46891 900 if (rep == NULL) {
984263bc
MD
901 nfsstats.rpcunexpected++;
902 m_freem(mrep);
903 } else if (rep == myrep) {
904 if (rep->r_mrep == NULL)
905 panic("nfsreply nil");
906 return (0);
907 }
908 if (myrep->r_flags & R_GETONEREP)
909 return (0);
910 }
911}
912
913/*
914 * nfs_request - goes something like this
915 * - fill in request struct
916 * - links it into list
917 * - calls nfs_send() for first transmit
918 * - calls nfs_receive() to get reply
919 * - break down rpc header and return with nfs reply pointed to
920 * by mrep or error
921 * nb: always frees up mreq mbuf list
922 */
923int
e851b29e
CP
924nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
925 struct thread *td, struct ucred *cred, struct mbuf **mrp,
926 struct mbuf **mdp, caddr_t *dposp)
984263bc 927{
40393ded
RG
928 struct mbuf *mrep, *m2;
929 struct nfsreq *rep;
930 u_int32_t *tl;
931 int i;
984263bc
MD
932 struct nfsmount *nmp;
933 struct mbuf *m, *md, *mheadend;
984263bc 934 char nickv[RPCX_NICKVERF];
e07fef60 935 time_t waituntil;
984263bc 936 caddr_t dpos, cp2;
e07fef60
MD
937 int t1, error = 0, mrest_len, auth_len, auth_type;
938 int trylater_delay = 15, trylater_cnt = 0, failed_auth = 0;
984263bc
MD
939 int verf_len, verf_type;
940 u_int32_t xid;
984263bc
MD
941 char *auth_str, *verf_str;
942 NFSKERBKEY_T key; /* save session key */
943
944 /* Reject requests while attempting a forced unmount. */
945 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
946 m_freem(mrest);
947 return (ESTALE);
948 }
949 nmp = VFSTONFS(vp->v_mount);
950 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
951 rep->r_nmp = nmp;
952 rep->r_vp = vp;
dadab5e9 953 rep->r_td = td;
984263bc 954 rep->r_procnum = procnum;
91f46891 955 rep->r_mreq = NULL;
984263bc
MD
956 i = 0;
957 m = mrest;
958 while (m) {
959 i += m->m_len;
960 m = m->m_next;
961 }
962 mrest_len = i;
963
964 /*
965 * Get the RPC header with authorization.
966 */
967kerbauth:
968 verf_str = auth_str = (char *)0;
969 if (nmp->nm_flag & NFSMNT_KERB) {
970 verf_str = nickv;
971 verf_len = sizeof (nickv);
972 auth_type = RPCAUTH_KERB4;
973 bzero((caddr_t)key, sizeof (key));
974 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
975 &auth_len, verf_str, verf_len)) {
976 error = nfs_getauth(nmp, rep, cred, &auth_str,
977 &auth_len, verf_str, &verf_len, key);
978 if (error) {
979 free((caddr_t)rep, M_NFSREQ);
980 m_freem(mrest);
981 return (error);
982 }
983 }
984 } else {
985 auth_type = RPCAUTH_UNIX;
986 if (cred->cr_ngroups < 1)
987 panic("nfsreq nogrps");
988 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
989 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
990 5 * NFSX_UNSIGNED;
991 }
992 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
993 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
994 if (auth_str)
995 free(auth_str, M_TEMP);
996
997 /*
998 * For stream protocols, insert a Sun RPC Record Mark.
999 */
1000 if (nmp->nm_sotype == SOCK_STREAM) {
74f1caca 1001 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
68db5ea4
MD
1002 if (m == NULL) {
1003 free(rep, M_NFSREQ);
1cee5817 1004 return (ENOBUFS);
68db5ea4 1005 }
984263bc
MD
1006 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1007 (m->m_pkthdr.len - NFSX_UNSIGNED));
1008 }
1009 rep->r_mreq = m;
1010 rep->r_xid = xid;
1011tryagain:
1012 if (nmp->nm_flag & NFSMNT_SOFT)
1013 rep->r_retry = nmp->nm_retry;
1014 else
1015 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1016 rep->r_rtt = rep->r_rexmit = 0;
1017 if (proct[procnum] > 0)
54938b92 1018 rep->r_flags = R_TIMING | R_MASKTIMER;
984263bc 1019 else
54938b92 1020 rep->r_flags = R_MASKTIMER;
984263bc
MD
1021 rep->r_mrep = NULL;
1022
1023 /*
1024 * Do the client side RPC.
1025 */
1026 nfsstats.rpcrequests++;
54938b92 1027
984263bc
MD
1028 /*
1029 * Chain request into list of outstanding requests. Be sure
54938b92
MD
1030 * to put it LAST so timer finds oldest requests first. Note
1031 * that R_MASKTIMER is set at the moment to prevent any timer
1032 * action on this request while we are still doing processing on
1033 * it below. splsoftclock() primarily protects nm_sent. Note
1034 * that we may block in this code so there is no atomicy guarentee.
984263bc 1035 */
165dba55 1036 crit_enter();
984263bc
MD
1037 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1038
984263bc
MD
1039 /*
1040 * If backing off another request or avoiding congestion, don't
91f46891
MD
1041 * send this one now but let timer do it. If not timing a request,
1042 * do it now.
1043 *
1044 * Even though the timer will not mess with our request there is
1045 * still the possibility that we will race a reply (which clears
1046 * R_SENT), especially on localhost connections, so be very careful
1047 * when setting R_SENT. We could set R_SENT prior to calling
1048 * nfs_send() but why bother if the response occurs that quickly?
984263bc
MD
1049 */
1050 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
91f46891
MD
1051 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1052 nmp->nm_sent < nmp->nm_cwnd)) {
984263bc
MD
1053 if (nmp->nm_soflags & PR_CONNREQUIRED)
1054 error = nfs_sndlock(rep);
1055 if (!error) {
74f1caca 1056 m2 = m_copym(m, 0, M_COPYALL, MB_WAIT);
984263bc
MD
1057 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1058 if (nmp->nm_soflags & PR_CONNREQUIRED)
1059 nfs_sndunlock(rep);
1060 }
91f46891
MD
1061 if (!error && (rep->r_flags & R_MUSTRESEND) == 0 &&
1062 rep->r_mrep == NULL) {
1063 KASSERT((rep->r_flags & R_SENT) == 0,
1064 ("R_SENT ASSERT %p", rep));
984263bc
MD
1065 nmp->nm_sent += NFS_CWNDSCALE;
1066 rep->r_flags |= R_SENT;
1067 }
1068 } else {
984263bc
MD
1069 rep->r_rtt = -1;
1070 }
1071
1072 /*
54938b92
MD
1073 * Let the timer do what it will with the request, then
1074 * wait for the reply from our send or the timer's.
984263bc 1075 */
91f46891
MD
1076 if (!error || error == EPIPE) {
1077 rep->r_flags &= ~R_MASKTIMER;
1078 crit_exit();
984263bc 1079 error = nfs_reply(rep);
91f46891
MD
1080 crit_enter();
1081 }
984263bc
MD
1082
1083 /*
91f46891
MD
1084 * RPC done, unlink the request, but don't rip it out from under
1085 * the callout timer.
984263bc 1086 */
91f46891
MD
1087 while (rep->r_flags & R_LOCKED) {
1088 nfs_timer_raced = 1;
1089 tsleep(&nfs_timer_raced, 0, "nfstrac", 0);
1090 }
984263bc 1091 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
984263bc
MD
1092
1093 /*
1094 * Decrement the outstanding request count.
1095 */
1096 if (rep->r_flags & R_SENT) {
54938b92 1097 rep->r_flags &= ~R_SENT;
984263bc
MD
1098 nmp->nm_sent -= NFS_CWNDSCALE;
1099 }
165dba55 1100 crit_exit();
984263bc
MD
1101
1102 /*
1103 * If there was a successful reply and a tprintf msg.
1104 * tprintf a response.
1105 */
1106 if (!error && (rep->r_flags & R_TPRINTFMSG))
dadab5e9 1107 nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
984263bc
MD
1108 "is alive again");
1109 mrep = rep->r_mrep;
1110 md = rep->r_md;
1111 dpos = rep->r_dpos;
1112 if (error) {
1113 m_freem(rep->r_mreq);
1114 free((caddr_t)rep, M_NFSREQ);
1115 return (error);
1116 }
1117
1118 /*
1119 * break down the rpc header and check if ok
1120 */
1121 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1122 if (*tl++ == rpc_msgdenied) {
1123 if (*tl == rpc_mismatch)
1124 error = EOPNOTSUPP;
1125 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1126 if (!failed_auth) {
1127 failed_auth++;
1128 mheadend->m_next = (struct mbuf *)0;
1129 m_freem(mrep);
1130 m_freem(rep->r_mreq);
1131 goto kerbauth;
1132 } else
1133 error = EAUTH;
1134 } else
1135 error = EACCES;
1136 m_freem(mrep);
1137 m_freem(rep->r_mreq);
1138 free((caddr_t)rep, M_NFSREQ);
1139 return (error);
1140 }
1141
1142 /*
1143 * Grab any Kerberos verifier, otherwise just throw it away.
1144 */
1145 verf_type = fxdr_unsigned(int, *tl++);
1146 i = fxdr_unsigned(int32_t, *tl);
1147 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1148 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1149 if (error)
1150 goto nfsmout;
1151 } else if (i > 0)
1152 nfsm_adv(nfsm_rndup(i));
1153 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1154 /* 0 == ok */
1155 if (*tl == 0) {
1156 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1157 if (*tl != 0) {
1158 error = fxdr_unsigned(int, *tl);
1159 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1160 error == NFSERR_TRYLATER) {
1161 m_freem(mrep);
1162 error = 0;
1163 waituntil = time_second + trylater_delay;
1164 while (time_second < waituntil)
1165 (void) tsleep((caddr_t)&lbolt,
377d4740 1166 0, "nqnfstry", 0);
984263bc
MD
1167 trylater_delay *= nfs_backoff[trylater_cnt];
1168 if (trylater_cnt < 7)
1169 trylater_cnt++;
1170 goto tryagain;
1171 }
1172
1173 /*
1174 * If the File Handle was stale, invalidate the
1175 * lookup cache, just in case.
1176 */
dc1be39c 1177 if (error == ESTALE) {
6b008938 1178 cache_inval_vp(vp, CINV_CHILDREN);
dc1be39c 1179 }
984263bc
MD
1180 if (nmp->nm_flag & NFSMNT_NFSV3) {
1181 *mrp = mrep;
1182 *mdp = md;
1183 *dposp = dpos;
1184 error |= NFSERR_RETERR;
1185 } else
1186 m_freem(mrep);
1187 m_freem(rep->r_mreq);
1188 free((caddr_t)rep, M_NFSREQ);
1189 return (error);
1190 }
1191
984263bc
MD
1192 *mrp = mrep;
1193 *mdp = md;
1194 *dposp = dpos;
1195 m_freem(rep->r_mreq);
1196 FREE((caddr_t)rep, M_NFSREQ);
1197 return (0);
1198 }
1199 m_freem(mrep);
1200 error = EPROTONOSUPPORT;
1201nfsmout:
1202 m_freem(rep->r_mreq);
1203 free((caddr_t)rep, M_NFSREQ);
1204 return (error);
1205}
1206
1207#ifndef NFS_NOSERVER
1208/*
1209 * Generate the rpc reply header
1210 * siz arg. is used to decide if adding a cluster is worthwhile
1211 */
1212int
e851b29e 1213nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp,
e07fef60 1214 int err, struct mbuf **mrq, struct mbuf **mbp, caddr_t *bposp)
984263bc 1215{
40393ded
RG
1216 u_int32_t *tl;
1217 struct mbuf *mreq;
984263bc
MD
1218 caddr_t bpos;
1219 struct mbuf *mb, *mb2;
1220
7771277f
JH
1221 siz += RPC_REPLYSIZ;
1222 mb = mreq = m_getl(max_hdr + siz, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
1223 mreq->m_pkthdr.len = 0;
984263bc 1224 /*
7771277f
JH
1225 * If this is not a cluster, try and leave leading space
1226 * for the lower level headers.
984263bc 1227 */
7771277f 1228 if ((max_hdr + siz) < MINCLSIZE)
984263bc
MD
1229 mreq->m_data += max_hdr;
1230 tl = mtod(mreq, u_int32_t *);
1231 mreq->m_len = 6 * NFSX_UNSIGNED;
1232 bpos = ((caddr_t)tl) + mreq->m_len;
1233 *tl++ = txdr_unsigned(nd->nd_retxid);
1234 *tl++ = rpc_reply;
1235 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1236 *tl++ = rpc_msgdenied;
1237 if (err & NFSERR_AUTHERR) {
1238 *tl++ = rpc_autherr;
1239 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1240 mreq->m_len -= NFSX_UNSIGNED;
1241 bpos -= NFSX_UNSIGNED;
1242 } else {
1243 *tl++ = rpc_mismatch;
1244 *tl++ = txdr_unsigned(RPC_VER2);
1245 *tl = txdr_unsigned(RPC_VER2);
1246 }
1247 } else {
1248 *tl++ = rpc_msgaccepted;
1249
1250 /*
1251 * For Kerberos authentication, we must send the nickname
1252 * verifier back, otherwise just RPCAUTH_NULL.
1253 */
1254 if (nd->nd_flag & ND_KERBFULL) {
40393ded 1255 struct nfsuid *nuidp;
984263bc
MD
1256 struct timeval ktvin, ktvout;
1257
1258 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1259 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1260 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1261 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1262 &nuidp->nu_haddr, nd->nd_nam2)))
1263 break;
1264 }
1265 if (nuidp) {
1266 ktvin.tv_sec =
1267 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1268 ktvin.tv_usec =
1269 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1270
1271 /*
1272 * Encrypt the timestamp in ecb mode using the
1273 * session key.
1274 */
1275#ifdef NFSKERB
1276 XXX
1277#endif
1278
1279 *tl++ = rpc_auth_kerb;
1280 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1281 *tl = ktvout.tv_sec;
1282 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1283 *tl++ = ktvout.tv_usec;
1284 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1285 } else {
1286 *tl++ = 0;
1287 *tl++ = 0;
1288 }
1289 } else {
1290 *tl++ = 0;
1291 *tl++ = 0;
1292 }
1293 switch (err) {
1294 case EPROGUNAVAIL:
1295 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1296 break;
1297 case EPROGMISMATCH:
1298 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1299 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
e07fef60
MD
1300 *tl++ = txdr_unsigned(2);
1301 *tl = txdr_unsigned(3);
984263bc
MD
1302 break;
1303 case EPROCUNAVAIL:
1304 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1305 break;
1306 case EBADRPC:
1307 *tl = txdr_unsigned(RPC_GARBAGE);
1308 break;
1309 default:
1310 *tl = 0;
1311 if (err != NFSERR_RETVOID) {
1312 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1313 if (err)
1314 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1315 else
1316 *tl = 0;
1317 }
1318 break;
1319 };
1320 }
1321
984263bc
MD
1322 if (mrq != NULL)
1323 *mrq = mreq;
1324 *mbp = mb;
1325 *bposp = bpos;
1326 if (err != 0 && err != NFSERR_RETVOID)
1327 nfsstats.srvrpc_errs++;
1328 return (0);
1329}
1330
1331
1332#endif /* NFS_NOSERVER */
1333/*
1334 * Nfs timer routine
1335 * Scan the nfsreq list and retranmit any requests that have timed out
1336 * To avoid retransmission attempts on STREAM sockets (in the future) make
1337 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1338 */
1339void
e851b29e 1340nfs_timer(void *arg /* never used */)
984263bc 1341{
40393ded
RG
1342 struct nfsreq *rep;
1343 struct mbuf *m;
1344 struct socket *so;
1345 struct nfsmount *nmp;
1346 int timeo;
165dba55 1347 int error;
984263bc 1348#ifndef NFS_NOSERVER
40393ded 1349 struct nfssvc_sock *slp;
984263bc
MD
1350 u_quad_t cur_usec;
1351#endif /* NFS_NOSERVER */
dadab5e9 1352 struct thread *td = &thread0; /* XXX for credentials, will break if sleep */
984263bc 1353
165dba55 1354 crit_enter();
ecd80f47 1355 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
984263bc 1356 nmp = rep->r_nmp;
54938b92 1357 if (rep->r_mrep || (rep->r_flags & (R_SOFTTERM|R_MASKTIMER)))
984263bc 1358 continue;
91f46891 1359 rep->r_flags |= R_LOCKED;
dadab5e9 1360 if (nfs_sigintr(nmp, rep, rep->r_td)) {
984263bc 1361 nfs_softterm(rep);
91f46891 1362 goto skip;
984263bc
MD
1363 }
1364 if (rep->r_rtt >= 0) {
1365 rep->r_rtt++;
1366 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1367 timeo = nmp->nm_timeo;
1368 else
1369 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1370 if (nmp->nm_timeouts > 0)
1371 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1372 if (rep->r_rtt <= timeo)
91f46891 1373 goto skip;
984263bc
MD
1374 if (nmp->nm_timeouts < 8)
1375 nmp->nm_timeouts++;
1376 }
1377 /*
1378 * Check for server not responding
1379 */
1380 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1381 rep->r_rexmit > nmp->nm_deadthresh) {
dadab5e9 1382 nfs_msg(rep->r_td,
984263bc
MD
1383 nmp->nm_mountp->mnt_stat.f_mntfromname,
1384 "not responding");
1385 rep->r_flags |= R_TPRINTFMSG;
1386 }
1387 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1388 nfsstats.rpctimeouts++;
1389 nfs_softterm(rep);
91f46891 1390 goto skip;
984263bc
MD
1391 }
1392 if (nmp->nm_sotype != SOCK_DGRAM) {
1393 if (++rep->r_rexmit > NFS_MAXREXMIT)
1394 rep->r_rexmit = NFS_MAXREXMIT;
91f46891 1395 goto skip;
984263bc
MD
1396 }
1397 if ((so = nmp->nm_so) == NULL)
91f46891 1398 goto skip;
984263bc
MD
1399
1400 /*
1401 * If there is enough space and the window allows..
1402 * Resend it
1403 * Set r_rtt to -1 in case we fail to send it now.
1404 */
1405 rep->r_rtt = -1;
1406 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1407 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1408 (rep->r_flags & R_SENT) ||
1409 nmp->nm_sent < nmp->nm_cwnd) &&
74f1caca 1410 (m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){
984263bc 1411 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
6b6e0885 1412 error = so_pru_send(so, 0, m, (struct sockaddr *)0,
dadab5e9 1413 (struct mbuf *)0, td);
984263bc 1414 else
6b6e0885
JH
1415 error = so_pru_send(so, 0, m, nmp->nm_nam,
1416 (struct mbuf *)0, td);
984263bc
MD
1417 if (error) {
1418 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1419 so->so_error = 0;
91f46891 1420 } else if (rep->r_mrep == NULL) {
984263bc
MD
1421 /*
1422 * Iff first send, start timing
1423 * else turn timing off, backoff timer
1424 * and divide congestion window by 2.
91f46891
MD
1425 *
1426 * It is possible for the so_pru_send() to
1427 * block and for us to race a reply so we
1428 * only do this if the reply field has not
1429 * been filled in. R_LOCKED will prevent
1430 * the request from being ripped out from under
1431 * us entirely.
984263bc
MD
1432 */
1433 if (rep->r_flags & R_SENT) {
1434 rep->r_flags &= ~R_TIMING;
1435 if (++rep->r_rexmit > NFS_MAXREXMIT)
1436 rep->r_rexmit = NFS_MAXREXMIT;
1437 nmp->nm_cwnd >>= 1;
1438 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1439 nmp->nm_cwnd = NFS_CWNDSCALE;
1440 nfsstats.rpcretries++;
1441 } else {
1442 rep->r_flags |= R_SENT;
1443 nmp->nm_sent += NFS_CWNDSCALE;
1444 }
1445 rep->r_rtt = 0;
1446 }
1447 }
91f46891
MD
1448skip:
1449 rep->r_flags &= ~R_LOCKED;
984263bc
MD
1450 }
1451#ifndef NFS_NOSERVER
984263bc
MD
1452
1453 /*
1454 * Scan the write gathering queues for writes that need to be
1455 * completed now.
1456 */
1457 cur_usec = nfs_curusec();
ecd80f47 1458 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
984263bc 1459 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
52553028 1460 nfsrv_wakenfsd(slp, 1);
984263bc
MD
1461 }
1462#endif /* NFS_NOSERVER */
91f46891
MD
1463
1464 /*
1465 * Due to possible blocking, a client operation may be waiting for
1466 * us to finish processing this request so it can remove it.
1467 */
1468 if (nfs_timer_raced) {
1469 nfs_timer_raced = 0;
1470 wakeup(&nfs_timer_raced);
1471 }
165dba55 1472 crit_exit();
f786cc86 1473 callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer, NULL);
984263bc
MD
1474}
1475
1476/*
1477 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1478 * wait for all requests to complete. This is used by forced unmounts
1479 * to terminate any outstanding RPCs.
1480 */
1481int
e851b29e 1482nfs_nmcancelreqs(struct nfsmount *nmp)
984263bc
MD
1483{
1484 struct nfsreq *req;
165dba55 1485 int i;
984263bc 1486
165dba55 1487 crit_enter();
984263bc
MD
1488 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1489 if (nmp != req->r_nmp || req->r_mrep != NULL ||
91f46891 1490 (req->r_flags & R_SOFTTERM)) {
984263bc 1491 continue;
91f46891 1492 }
984263bc
MD
1493 nfs_softterm(req);
1494 }
165dba55 1495 crit_exit();
984263bc
MD
1496
1497 for (i = 0; i < 30; i++) {
165dba55 1498 crit_enter();
984263bc
MD
1499 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1500 if (nmp == req->r_nmp)
1501 break;
1502 }
165dba55 1503 crit_exit();
984263bc
MD
1504 if (req == NULL)
1505 return (0);
377d4740 1506 tsleep(&lbolt, 0, "nfscancel", 0);
984263bc
MD
1507 }
1508 return (EBUSY);
1509}
1510
1511/*
1512 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1513 * The nm_send count is decremented now to avoid deadlocks when the process in
1514 * soreceive() hasn't yet managed to send its own request.
54938b92
MD
1515 *
1516 * This routine must be called at splsoftclock() to protect r_flags and
1517 * nm_sent.
984263bc
MD
1518 */
1519
1520static void
e851b29e 1521nfs_softterm(struct nfsreq *rep)
984263bc
MD
1522{
1523 rep->r_flags |= R_SOFTTERM;
1524
1525 if (rep->r_flags & R_SENT) {
1526 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1527 rep->r_flags &= ~R_SENT;
1528 }
1529}
1530
1531/*
1532 * Test for a termination condition pending on the process.
1533 * This is used for NFSMNT_INT mounts.
1534 */
1535int
dadab5e9 1536nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
984263bc
MD
1537{
1538 sigset_t tmpset;
dadab5e9 1539 struct proc *p;
984263bc
MD
1540
1541 if (rep && (rep->r_flags & R_SOFTTERM))
1542 return (EINTR);
1543 /* Terminate all requests while attempting a forced unmount. */
1544 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1545 return (EINTR);
1546 if (!(nmp->nm_flag & NFSMNT_INT))
1547 return (0);
cd990953
MD
1548 /* td might be NULL YYY */
1549 if (td == NULL || (p = td->td_proc) == NULL)
984263bc
MD
1550 return (0);
1551
1552 tmpset = p->p_siglist;
1553 SIGSETNAND(tmpset, p->p_sigmask);
1554 SIGSETNAND(tmpset, p->p_sigignore);
1555 if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset))
1556 return (EINTR);
1557
1558 return (0);
1559}
1560
1561/*
1562 * Lock a socket against others.
1563 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1564 * and also to avoid race conditions between the processes with nfs requests
1565 * in progress when a reconnect is necessary.
1566 */
1567int
dadab5e9 1568nfs_sndlock(struct nfsreq *rep)
984263bc 1569{
dadab5e9
MD
1570 int *statep = &rep->r_nmp->nm_state;
1571 struct thread *td;
b2eb81cd
MD
1572 int slptimeo;
1573 int slpflag;
1574 int error;
984263bc 1575
b2eb81cd
MD
1576 slpflag = 0;
1577 slptimeo = 0;
dadab5e9 1578 td = rep->r_td;
984263bc
MD
1579 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1580 slpflag = PCATCH;
b2eb81cd
MD
1581
1582 error = 0;
1583 crit_enter();
984263bc 1584 while (*statep & NFSSTA_SNDLOCK) {
54938b92 1585 *statep |= NFSSTA_WANTSND;
b2eb81cd
MD
1586 if (nfs_sigintr(rep->r_nmp, rep, td)) {
1587 error = EINTR;
1588 break;
1589 }
1590 tsleep((caddr_t)statep, slpflag, "nfsndlck", slptimeo);
984263bc
MD
1591 if (slpflag == PCATCH) {
1592 slpflag = 0;
1593 slptimeo = 2 * hz;
1594 }
1595 }
1596 /* Always fail if our request has been cancelled. */
1597 if ((rep->r_flags & R_SOFTTERM))
b2eb81cd
MD
1598 error = EINTR;
1599 if (error == 0)
1600 *statep |= NFSSTA_SNDLOCK;
1601 crit_exit();
1602 return (error);
984263bc
MD
1603}
1604
1605/*
1606 * Unlock the stream socket for others.
1607 */
1608void
e851b29e 1609nfs_sndunlock(struct nfsreq *rep)
984263bc 1610{
40393ded 1611 int *statep = &rep->r_nmp->nm_state;
984263bc
MD
1612
1613 if ((*statep & NFSSTA_SNDLOCK) == 0)
1614 panic("nfs sndunlock");
b2eb81cd 1615 crit_enter();
984263bc
MD
1616 *statep &= ~NFSSTA_SNDLOCK;
1617 if (*statep & NFSSTA_WANTSND) {
1618 *statep &= ~NFSSTA_WANTSND;
1619 wakeup((caddr_t)statep);
1620 }
b2eb81cd 1621 crit_exit();
984263bc
MD
1622}
1623
1624static int
e851b29e 1625nfs_rcvlock(struct nfsreq *rep)
984263bc 1626{
40393ded 1627 int *statep = &rep->r_nmp->nm_state;
b2eb81cd
MD
1628 int slpflag;
1629 int slptimeo;
1630 int error;
984263bc 1631
54938b92
MD
1632 /*
1633 * Unconditionally check for completion in case another nfsiod
1634 * get the packet while the caller was blocked, before the caller
1635 * called us. Packet reception is handled by mainline code which
1636 * is protected by the BGL at the moment.
b2eb81cd
MD
1637 *
1638 * We do not strictly need the second check just before the
1639 * tsleep(), but it's good defensive programming.
54938b92
MD
1640 */
1641 if (rep->r_mrep != NULL)
1642 return (EALREADY);
1643
984263bc
MD
1644 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1645 slpflag = PCATCH;
1646 else
1647 slpflag = 0;
b2eb81cd
MD
1648 slptimeo = 0;
1649 error = 0;
1650 crit_enter();
984263bc 1651 while (*statep & NFSSTA_RCVLOCK) {
b2eb81cd
MD
1652 if (nfs_sigintr(rep->r_nmp, rep, rep->r_td)) {
1653 error = EINTR;
1654 break;
1655 }
1656 if (rep->r_mrep != NULL) {
1657 error = EALREADY;
1658 break;
1659 }
984263bc 1660 *statep |= NFSSTA_WANTRCV;
b2eb81cd 1661 tsleep((caddr_t)statep, slpflag, "nfsrcvlk", slptimeo);
984263bc
MD
1662 /*
1663 * If our reply was recieved while we were sleeping,
1664 * then just return without taking the lock to avoid a
1665 * situation where a single iod could 'capture' the
1666 * recieve lock.
1667 */
b2eb81cd
MD
1668 if (rep->r_mrep != NULL) {
1669 error = EALREADY;
1670 break;
1671 }
984263bc
MD
1672 if (slpflag == PCATCH) {
1673 slpflag = 0;
1674 slptimeo = 2 * hz;
1675 }
1676 }
b2eb81cd
MD
1677 if (error == 0) {
1678 *statep |= NFSSTA_RCVLOCK;
1679 rep->r_nmp->nm_rcvlock_td = curthread; /* DEBUGGING */
1680 }
1681 crit_exit();
1682 return (error);
984263bc
MD
1683}
1684
1685/*
1686 * Unlock the stream socket for others.
1687 */
1688static void
e851b29e 1689nfs_rcvunlock(struct nfsreq *rep)
984263bc 1690{
40393ded 1691 int *statep = &rep->r_nmp->nm_state;
984263bc
MD
1692
1693 if ((*statep & NFSSTA_RCVLOCK) == 0)
1694 panic("nfs rcvunlock");
b2eb81cd
MD
1695 crit_enter();
1696 rep->r_nmp->nm_rcvlock_td = (void *)-1; /* DEBUGGING */
984263bc
MD
1697 *statep &= ~NFSSTA_RCVLOCK;
1698 if (*statep & NFSSTA_WANTRCV) {
1699 *statep &= ~NFSSTA_WANTRCV;
1700 wakeup((caddr_t)statep);
1701 }
b2eb81cd 1702 crit_exit();
984263bc
MD
1703}
1704
1705/*
1706 * nfs_realign:
1707 *
1708 * Check for badly aligned mbuf data and realign by copying the unaligned
1709 * portion of the data into a new mbuf chain and freeing the portions
1710 * of the old chain that were replaced.
1711 *
1712 * We cannot simply realign the data within the existing mbuf chain
1713 * because the underlying buffers may contain other rpc commands and
1714 * we cannot afford to overwrite them.
1715 *
1716 * We would prefer to avoid this situation entirely. The situation does
1717 * not occur with NFS/UDP and is supposed to only occassionally occur
1718 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1719 */
1720static void
e851b29e 1721nfs_realign(struct mbuf **pm, int hsiz)
984263bc
MD
1722{
1723 struct mbuf *m;
1724 struct mbuf *n = NULL;
1725 int off = 0;
1726
1727 ++nfs_realign_test;
1728
1729 while ((m = *pm) != NULL) {
1730 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
50503f0f 1731 n = m_getl(m->m_len, MB_WAIT, MT_DATA, 0, NULL);
984263bc
MD
1732 n->m_len = 0;
1733 break;
1734 }
1735 pm = &m->m_next;
1736 }
1737
1738 /*
1739 * If n is non-NULL, loop on m copying data, then replace the
1740 * portion of the chain that had to be realigned.
1741 */
1742 if (n != NULL) {
1743 ++nfs_realign_count;
1744 while (m) {
1745 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1746 off += m->m_len;
1747 m = m->m_next;
1748 }
1749 m_freem(*pm);
1750 *pm = n;
1751 }
1752}
1753
1754#ifndef NFS_NOSERVER
1755
1756/*
1757 * Parse an RPC request
1758 * - verify it
1759 * - fill in the cred struct.
1760 */
1761int
e851b29e 1762nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
984263bc 1763{
40393ded
RG
1764 int len, i;
1765 u_int32_t *tl;
1766 int32_t t1;
984263bc
MD
1767 struct uio uio;
1768 struct iovec iov;
1769 caddr_t dpos, cp2, cp;
1770 u_int32_t nfsvers, auth_type;
1771 uid_t nickuid;
e07fef60 1772 int error = 0, ticklen;
984263bc 1773 struct mbuf *mrep, *md;
40393ded 1774 struct nfsuid *nuidp;
984263bc
MD
1775 struct timeval tvin, tvout;
1776#if 0 /* until encrypted keys are implemented */
1777 NFSKERBKEYSCHED_T keys; /* stores key schedule */
1778#endif
1779
1780 mrep = nd->nd_mrep;
1781 md = nd->nd_md;
1782 dpos = nd->nd_dpos;
1783 if (has_header) {
1784 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1785 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1786 if (*tl++ != rpc_call) {
1787 m_freem(mrep);
1788 return (EBADRPC);
1789 }
1790 } else
1791 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1792 nd->nd_repstat = 0;
1793 nd->nd_flag = 0;
1794 if (*tl++ != rpc_vers) {
1795 nd->nd_repstat = ERPCMISMATCH;
1796 nd->nd_procnum = NFSPROC_NOOP;
1797 return (0);
1798 }
1799 if (*tl != nfs_prog) {
e07fef60
MD
1800 nd->nd_repstat = EPROGUNAVAIL;
1801 nd->nd_procnum = NFSPROC_NOOP;
1802 return (0);
984263bc
MD
1803 }
1804 tl++;
1805 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
e07fef60 1806 if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
984263bc
MD
1807 nd->nd_repstat = EPROGMISMATCH;
1808 nd->nd_procnum = NFSPROC_NOOP;
1809 return (0);
1810 }
e07fef60 1811 if (nfsvers == NFS_VER3)
984263bc
MD
1812 nd->nd_flag = ND_NFSV3;
1813 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1814 if (nd->nd_procnum == NFSPROC_NULL)
1815 return (0);
1816 if (nd->nd_procnum >= NFS_NPROCS ||
e07fef60 1817 (nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
984263bc
MD
1818 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1819 nd->nd_repstat = EPROCUNAVAIL;
1820 nd->nd_procnum = NFSPROC_NOOP;
1821 return (0);
1822 }
1823 if ((nd->nd_flag & ND_NFSV3) == 0)
1824 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1825 auth_type = *tl++;
1826 len = fxdr_unsigned(int, *tl++);
1827 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1828 m_freem(mrep);
1829 return (EBADRPC);
1830 }
1831
1832 nd->nd_flag &= ~ND_KERBAUTH;
1833 /*
1834 * Handle auth_unix or auth_kerb.
1835 */
1836 if (auth_type == rpc_auth_unix) {
1837 len = fxdr_unsigned(int, *++tl);
1838 if (len < 0 || len > NFS_MAXNAMLEN) {
1839 m_freem(mrep);
1840 return (EBADRPC);
1841 }
1842 nfsm_adv(nfsm_rndup(len));
1843 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1844 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
1845 nd->nd_cr.cr_ref = 1;
1846 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1847 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1848 len = fxdr_unsigned(int, *tl);
1849 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1850 m_freem(mrep);
1851 return (EBADRPC);
1852 }
1853 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1854 for (i = 1; i <= len; i++)
1855 if (i < NGROUPS)
1856 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1857 else
1858 tl++;
1859 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
1860 if (nd->nd_cr.cr_ngroups > 1)
1861 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
1862 len = fxdr_unsigned(int, *++tl);
1863 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1864 m_freem(mrep);
1865 return (EBADRPC);
1866 }
1867 if (len > 0)
1868 nfsm_adv(nfsm_rndup(len));
1869 } else if (auth_type == rpc_auth_kerb) {
1870 switch (fxdr_unsigned(int, *tl++)) {
1871 case RPCAKN_FULLNAME:
1872 ticklen = fxdr_unsigned(int, *tl);
1873 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1874 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1875 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1876 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1877 m_freem(mrep);
1878 return (EBADRPC);
1879 }
1880 uio.uio_offset = 0;
1881 uio.uio_iov = &iov;
1882 uio.uio_iovcnt = 1;
1883 uio.uio_segflg = UIO_SYSSPACE;
1884 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
1885 iov.iov_len = RPCAUTH_MAXSIZ - 4;
1886 nfsm_mtouio(&uio, uio.uio_resid);
1887 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1888 if (*tl++ != rpc_auth_kerb ||
1889 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1890 printf("Bad kerb verifier\n");
1891 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1892 nd->nd_procnum = NFSPROC_NOOP;
1893 return (0);
1894 }
1895 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
1896 tl = (u_int32_t *)cp;
1897 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1898 printf("Not fullname kerb verifier\n");
1899 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1900 nd->nd_procnum = NFSPROC_NOOP;
1901 return (0);
1902 }
1903 cp += NFSX_UNSIGNED;
1904 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
1905 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1906 nd->nd_flag |= ND_KERBFULL;
1907 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1908 break;
1909 case RPCAKN_NICKNAME:
1910 if (len != 2 * NFSX_UNSIGNED) {
1911 printf("Kerb nickname short\n");
1912 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1913 nd->nd_procnum = NFSPROC_NOOP;
1914 return (0);
1915 }
1916 nickuid = fxdr_unsigned(uid_t, *tl);
1917 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1918 if (*tl++ != rpc_auth_kerb ||
1919 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1920 printf("Kerb nick verifier bad\n");
1921 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1922 nd->nd_procnum = NFSPROC_NOOP;
1923 return (0);
1924 }
1925 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1926 tvin.tv_sec = *tl++;
1927 tvin.tv_usec = *tl;
1928
1929 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
1930 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1931 if (nuidp->nu_cr.cr_uid == nickuid &&
1932 (!nd->nd_nam2 ||
1933 netaddr_match(NU_NETFAM(nuidp),
1934 &nuidp->nu_haddr, nd->nd_nam2)))
1935 break;
1936 }
1937 if (!nuidp) {
1938 nd->nd_repstat =
1939 (NFSERR_AUTHERR|AUTH_REJECTCRED);
1940 nd->nd_procnum = NFSPROC_NOOP;
1941 return (0);
1942 }
1943
1944 /*
1945 * Now, decrypt the timestamp using the session key
1946 * and validate it.
1947 */
1948#ifdef NFSKERB
1949 XXX
1950#endif
1951
1952 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1953 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1954 if (nuidp->nu_expire < time_second ||
1955 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1956 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1957 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1958 nuidp->nu_expire = 0;
1959 nd->nd_repstat =
1960 (NFSERR_AUTHERR|AUTH_REJECTVERF);
1961 nd->nd_procnum = NFSPROC_NOOP;
1962 return (0);
1963 }
1964 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
1965 nd->nd_flag |= ND_KERBNICK;
1966 };
1967 } else {
1968 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1969 nd->nd_procnum = NFSPROC_NOOP;
1970 return (0);
1971 }
1972
984263bc
MD
1973 nd->nd_md = md;
1974 nd->nd_dpos = dpos;
1975 return (0);
1976nfsmout:
1977 return (error);
1978}
1979
1980#endif
1981
b70ddfbf
MD
1982/*
1983 * Send a message to the originating process's terminal. The thread and/or
1984 * process may be NULL. YYY the thread should not be NULL but there may
1985 * still be some uio_td's that are still being passed as NULL through to
1986 * nfsm_request().
1987 */
984263bc 1988static int
dadab5e9 1989nfs_msg(struct thread *td, char *server, char *msg)
984263bc
MD
1990{
1991 tpr_t tpr;
1992
b70ddfbf 1993 if (td && td->td_proc)
dadab5e9 1994 tpr = tprintf_open(td->td_proc);
984263bc
MD
1995 else
1996 tpr = NULL;
1997 tprintf(tpr, "nfs server %s: %s\n", server, msg);
1998 tprintf_close(tpr);
1999 return (0);
2000}
2001
2002#ifndef NFS_NOSERVER
2003/*
2004 * Socket upcall routine for the nfsd sockets.
2005 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2006 * Essentially do as much as possible non-blocking, else punt and it will
74f1caca 2007 * be called with MB_WAIT from an nfsd.
984263bc
MD
2008 */
2009void
e851b29e 2010nfsrv_rcv(struct socket *so, void *arg, int waitflag)
984263bc 2011{
40393ded
RG
2012 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2013 struct mbuf *m;
984263bc
MD
2014 struct mbuf *mp;
2015 struct sockaddr *nam;
2016 struct uio auio;
2017 int flags, error;
52553028 2018 int nparallel_wakeup = 0;
984263bc
MD
2019
2020 if ((slp->ns_flag & SLP_VALID) == 0)
2021 return;
3b101e2e 2022
984263bc 2023 /*
52553028
MD
2024 * Do not allow an infinite number of completed RPC records to build
2025 * up before we stop reading data from the socket. Otherwise we could
2026 * end up holding onto an unreasonable number of mbufs for requests
2027 * waiting for service.
2028 *
2029 * This should give pretty good feedback to the TCP
3b101e2e
MD
2030 * layer and prevents a memory crunch for other protocols.
2031 *
2032 * Note that the same service socket can be dispatched to several
2033 * nfs servers simultaniously.
2034 *
2035 * the tcp protocol callback calls us with MB_DONTWAIT.
2036 * nfsd calls us with MB_WAIT (typically).
984263bc 2037 */
52553028 2038 if (waitflag == MB_DONTWAIT && slp->ns_numrec >= nfsd_waiting / 2 + 1) {
3b101e2e
MD
2039 slp->ns_flag |= SLP_NEEDQ;
2040 goto dorecs;
984263bc 2041 }
3b101e2e
MD
2042
2043 /*
2044 * Handle protocol specifics to parse an RPC request. We always
2045 * pull from the socket using non-blocking I/O.
2046 */
dadab5e9 2047 auio.uio_td = NULL;
984263bc
MD
2048 if (so->so_type == SOCK_STREAM) {
2049 /*
3b101e2e
MD
2050 * The data has to be read in an orderly fashion from a TCP
2051 * stream, unlike a UDP socket. It is possible for soreceive
2052 * and/or nfsrv_getstream() to block, so make sure only one
2053 * entity is messing around with the TCP stream at any given
2054 * moment. The receive sockbuf's lock in soreceive is not
2055 * sufficient.
2056 *
2057 * Note that this procedure can be called from any number of
2058 * NFS severs *OR* can be upcalled directly from a TCP
2059 * protocol thread.
984263bc 2060 */
3b101e2e 2061 if (slp->ns_flag & SLP_GETSTREAM) {
984263bc
MD
2062 slp->ns_flag |= SLP_NEEDQ;
2063 goto dorecs;
2064 }
3b101e2e 2065 slp->ns_flag |= SLP_GETSTREAM;
984263bc
MD
2066
2067 /*
2068 * Do soreceive().
2069 */
2070 auio.uio_resid = 1000000000;
2071 flags = MSG_DONTWAIT;
6b6e0885 2072 error = so_pru_soreceive(so, &nam, &auio, &mp, NULL, &flags);
984263bc
MD
2073 if (error || mp == (struct mbuf *)0) {
2074 if (error == EWOULDBLOCK)
2075 slp->ns_flag |= SLP_NEEDQ;
2076 else
2077 slp->ns_flag |= SLP_DISCONN;
3b101e2e 2078 slp->ns_flag &= ~SLP_GETSTREAM;
984263bc
MD
2079 goto dorecs;
2080 }
2081 m = mp;
2082 if (slp->ns_rawend) {
2083 slp->ns_rawend->m_next = m;
2084 slp->ns_cc += 1000000000 - auio.uio_resid;
2085 } else {
2086 slp->ns_raw = m;
2087 slp->ns_cc = 1000000000 - auio.uio_resid;
2088 }
2089 while (m->m_next)
2090 m = m->m_next;
2091 slp->ns_rawend = m;
2092
2093 /*
3b101e2e
MD
2094 * Now try and parse as many record(s) as we can out of the
2095 * raw stream data.
984263bc 2096 */
52553028 2097 error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup);
984263bc
MD
2098 if (error) {
2099 if (error == EPERM)
2100 slp->ns_flag |= SLP_DISCONN;
2101 else
2102 slp->ns_flag |= SLP_NEEDQ;
2103 }
3b101e2e 2104 slp->ns_flag &= ~SLP_GETSTREAM;
984263bc 2105 } else {
3b101e2e
MD
2106 /*
2107 * For UDP soreceive typically pulls just one packet, loop
2108 * to get the whole batch.
2109 */
984263bc
MD
2110 do {
2111 auio.uio_resid = 1000000000;
2112 flags = MSG_DONTWAIT;
6b6e0885
JH
2113 error = so_pru_soreceive(so, &nam, &auio, &mp, NULL,
2114 &flags);
984263bc
MD
2115 if (mp) {
2116 struct nfsrv_rec *rec;
74f1caca 2117 int mf = (waitflag & MB_DONTWAIT) ?
da004c97 2118 M_NOWAIT : M_WAITOK;
984263bc 2119 rec = malloc(sizeof(struct nfsrv_rec),
da004c97 2120 M_NFSRVDESC, mf);
984263bc
MD
2121 if (!rec) {
2122 if (nam)
2123 FREE(nam, M_SONAME);
2124 m_freem(mp);
2125 continue;
2126 }
2127 nfs_realign(&mp, 10 * NFSX_UNSIGNED);
2128 rec->nr_address = nam;
2129 rec->nr_packet = mp;
2130 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
52553028
MD
2131 ++slp->ns_numrec;
2132 ++nparallel_wakeup;
984263bc
MD
2133 }
2134 if (error) {
2135 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2136 && error != EWOULDBLOCK) {
2137 slp->ns_flag |= SLP_DISCONN;
2138 goto dorecs;
2139 }
2140 }
2141 } while (mp);
2142 }
2143
2144 /*
3b101e2e
MD
2145 * If we were upcalled from the tcp protocol layer and we have
2146 * fully parsed records ready to go, or there is new data pending,
2147 * or something went wrong, try to wake up an nfsd thread to deal
2148 * with it.
984263bc
MD
2149 */
2150dorecs:
52553028 2151 if (waitflag == MB_DONTWAIT && (slp->ns_numrec > 0
3b101e2e 2152 || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) {
52553028 2153 nfsrv_wakenfsd(slp, nparallel_wakeup);
3b101e2e 2154 }
984263bc
MD
2155}
2156
2157/*
2158 * Try and extract an RPC request from the mbuf data list received on a
2159 * stream socket. The "waitflag" argument indicates whether or not it
2160 * can sleep.
2161 */
2162static int
52553028 2163nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp)
984263bc 2164{
40393ded
RG
2165 struct mbuf *m, **mpp;
2166 char *cp1, *cp2;
2167 int len;
984263bc
MD
2168 struct mbuf *om, *m2, *recm;
2169 u_int32_t recmark;
2170
984263bc
MD
2171 for (;;) {
2172 if (slp->ns_reclen == 0) {
3b101e2e 2173 if (slp->ns_cc < NFSX_UNSIGNED)
984263bc 2174 return (0);
984263bc
MD
2175 m = slp->ns_raw;
2176 if (m->m_len >= NFSX_UNSIGNED) {
2177 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2178 m->m_data += NFSX_UNSIGNED;
2179 m->m_len -= NFSX_UNSIGNED;
2180 } else {
2181 cp1 = (caddr_t)&recmark;
2182 cp2 = mtod(m, caddr_t);
2183 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2184 while (m->m_len == 0) {
2185 m = m->m_next;
2186 cp2 = mtod(m, caddr_t);
2187 }
2188 *cp1++ = *cp2++;
2189 m->m_data++;
2190 m->m_len--;
2191 }
2192 }
2193 slp->ns_cc -= NFSX_UNSIGNED;
2194 recmark = ntohl(recmark);
2195 slp->ns_reclen = recmark & ~0x80000000;
2196 if (recmark & 0x80000000)
2197 slp->ns_flag |= SLP_LASTFRAG;
2198 else
2199 slp->ns_flag &= ~SLP_LASTFRAG;
43884890 2200 if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) {
63cd2150
MD
2201 log(LOG_ERR, "%s (%d) from nfs client\n",
2202 "impossible packet length",
2203 slp->ns_reclen);
984263bc
MD
2204 return (EPERM);
2205 }
2206 }
2207
2208 /*
2209 * Now get the record part.
2210 *
2211 * Note that slp->ns_reclen may be 0. Linux sometimes
2212 * generates 0-length RPCs
2213 */
2214 recm = NULL;
2215 if (slp->ns_cc == slp->ns_reclen) {
2216 recm = slp->ns_raw;
2217 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2218 slp->ns_cc = slp->ns_reclen = 0;
2219 } else if (slp->ns_cc > slp->ns_reclen) {
2220 len = 0;
2221 m = slp->ns_raw;
2222 om = (struct mbuf *)0;
2223
2224 while (len < slp->ns_reclen) {
2225 if ((len + m->m_len) > slp->ns_reclen) {
2226 m2 = m_copym(m, 0, slp->ns_reclen - len,
2227 waitflag);
2228 if (m2) {
2229 if (om) {
2230 om->m_next = m2;
2231 recm = slp->ns_raw;
2232 } else
2233 recm = m2;
2234 m->m_data += slp->ns_reclen - len;
2235 m->m_len -= slp->ns_reclen - len;
2236 len = slp->ns_reclen;
2237 } else {
984263bc
MD
2238 return (EWOULDBLOCK);
2239 }
2240 } else if ((len + m->m_len) == slp->ns_reclen) {
2241 om = m;
2242 len += m->m_len;
2243 m = m->m_next;
2244 recm = slp->ns_raw;
2245 om->m_next = (struct mbuf *)0;
2246 } else {
2247 om = m;
2248 len += m->m_len;
2249 m = m->m_next;
2250 }
2251 }
2252 slp->ns_raw = m;
2253 slp->ns_cc -= len;
2254 slp->ns_reclen = 0;
2255 } else {
984263bc
MD
2256 return (0);
2257 }
2258
2259 /*
2260 * Accumulate the fragments into a record.
2261 */
2262 mpp = &slp->ns_frag;
2263 while (*mpp)
2264 mpp = &((*mpp)->m_next);
2265 *mpp = recm;
2266 if (slp->ns_flag & SLP_LASTFRAG) {
2267 struct nfsrv_rec *rec;
74f1caca 2268 int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK;
da004c97 2269 rec = malloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf);
984263bc
MD
2270 if (!rec) {
2271 m_freem(slp->ns_frag);
2272 } else {
2273 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
2274 rec->nr_address = (struct sockaddr *)0;
2275 rec->nr_packet = slp->ns_frag;
2276 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
52553028
MD
2277 ++slp->ns_numrec;
2278 ++*countp;
984263bc
MD
2279 }
2280 slp->ns_frag = (struct mbuf *)0;
2281 }
2282 }
2283}
2284
2285/*
2286 * Parse an RPC header.
2287 */
2288int
e851b29e
CP
2289nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
2290 struct nfsrv_descript **ndp)
984263bc
MD
2291{
2292 struct nfsrv_rec *rec;
40393ded 2293 struct mbuf *m;
984263bc 2294 struct sockaddr *nam;
40393ded 2295 struct nfsrv_descript *nd;
984263bc
MD
2296 int error;
2297
2298 *ndp = NULL;
2299 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec))
2300 return (ENOBUFS);
2301 rec = STAILQ_FIRST(&slp->ns_rec);
2302 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
52553028
MD
2303 KKASSERT(slp->ns_numrec > 0);
2304 --slp->ns_numrec;
984263bc
MD
2305 nam = rec->nr_address;
2306 m = rec->nr_packet;
2307 free(rec, M_NFSRVDESC);
2308 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
2309 M_NFSRVDESC, M_WAITOK);
2310 nd->nd_md = nd->nd_mrep = m;
2311 nd->nd_nam2 = nam;
2312 nd->nd_dpos = mtod(m, caddr_t);
2313 error = nfs_getreq(nd, nfsd, TRUE);
2314 if (error) {
2315 if (nam) {
2316 FREE(nam, M_SONAME);
2317 }
2318 free((caddr_t)nd, M_NFSRVDESC);
2319 return (error);
2320 }
2321 *ndp = nd;
2322 nfsd->nfsd_nd = nd;
2323 return (0);
2324}
2325
2326/*
52553028
MD
2327 * Try to assign service sockets to nfsd threads based on the number
2328 * of new rpc requests that have been queued on the service socket.
2329 *
2330 * If no nfsd's are available or additonal requests are pending, set the
2331 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for
2332 * the work in the nfssvc_sock list when it is finished processing its
2333 * current work. This flag is only cleared when an nfsd can not find
2334 * any new work to perform.
984263bc
MD
2335 */
2336void
52553028 2337nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel)
984263bc 2338{
40393ded 2339 struct nfsd *nd;
984263bc
MD
2340
2341 if ((slp->ns_flag & SLP_VALID) == 0)
2342 return;
52553028
MD
2343 if (nparallel <= 1)
2344 nparallel = 1;
ecd80f47 2345 TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) {
984263bc
MD
2346 if (nd->nfsd_flag & NFSD_WAITING) {
2347 nd->nfsd_flag &= ~NFSD_WAITING;
2348 if (nd->nfsd_slp)
2349 panic("nfsd wakeup");
2350 slp->ns_sref++;
2351 nd->nfsd_slp = slp;
2352 wakeup((caddr_t)nd);
52553028
MD
2353 if (--nparallel == 0)
2354 break;
984263bc
MD
2355 }
2356 }
52553028
MD
2357 if (nparallel) {
2358 slp->ns_flag |= SLP_DOREC;
2359 nfsd_head_flag |= NFSD_CHECKSLP;
2360 }
984263bc
MD
2361}
2362#endif /* NFS_NOSERVER */