kernel - Fix endless nfs error 70 during shutdown
[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 $
38 */
39
40/*
41 * Socket operations for use by nfs
42 */
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/proc.h>
47#include <sys/malloc.h>
48#include <sys/mount.h>
49#include <sys/kernel.h>
50#include <sys/mbuf.h>
51#include <sys/vnode.h>
9ba76b73 52#include <sys/fcntl.h>
984263bc 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>
8684e6f9 63
b1b4e5a6 64#include <sys/signal2.h>
8684e6f9 65#include <sys/mutex2.h>
6cef7136 66#include <sys/socketvar2.h>
984263bc
MD
67
68#include <netinet/in.h>
69#include <netinet/tcp.h>
54938b92 70#include <sys/thread2.h>
984263bc 71
1f2de5d4
MD
72#include "rpcv2.h"
73#include "nfsproto.h"
74#include "nfs.h"
75#include "xdr_subs.h"
76#include "nfsm_subs.h"
77#include "nfsmount.h"
78#include "nfsnode.h"
79#include "nfsrtt.h"
984263bc
MD
80
81#define TRUE 1
82#define FALSE 0
83
84/*
f8565b0f
MD
85 * RTT calculations are scaled by 256 (8 bits). A proper fractional
86 * RTT will still be calculated even with a slow NFS timer.
984263bc 87 */
f8565b0f
MD
88#define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum]]
89#define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum]]
90#define NFS_RTT_SCALE_BITS 8 /* bits */
91#define NFS_RTT_SCALE 256 /* value */
984263bc
MD
92
93/*
94 * Defines which timer to use for the procnum.
95 * 0 - default
96 * 1 - getattr
97 * 2 - lookup
98 * 3 - read
99 * 4 - write
100 */
101static int proct[NFS_NPROCS] = {
cc7d050e
MD
102 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, /* 00-09 */
103 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, /* 10-19 */
104 0, 5, 0, 0, 0, 0, /* 20-29 */
105};
106
107static int multt[NFS_NPROCS] = {
108 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-09 */
109 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10-19 */
110 1, 2, 1, 1, 1, 1, /* 20-29 */
984263bc
MD
111};
112
f8565b0f 113static int nfs_backoff[8] = { 2, 3, 5, 8, 13, 21, 34, 55 };
984263bc
MD
114static int nfs_realign_test;
115static int nfs_realign_count;
f8565b0f
MD
116static int nfs_showrtt;
117static int nfs_showrexmit;
cc7d050e 118int nfs_maxasyncbio = NFS_MAXASYNCBIO;
984263bc
MD
119
120SYSCTL_DECL(_vfs_nfs);
121
093e85dc
SG
122SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0,
123 "Number of times mbufs have been tested for bad alignment");
124SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0,
125 "Number of realignments for badly aligned mbuf data");
126SYSCTL_INT(_vfs_nfs, OID_AUTO, showrtt, CTLFLAG_RW, &nfs_showrtt, 0,
127 "Show round trip time output");
128SYSCTL_INT(_vfs_nfs, OID_AUTO, showrexmit, CTLFLAG_RW, &nfs_showrexmit, 0,
129 "Show retransmits info");
130SYSCTL_INT(_vfs_nfs, OID_AUTO, maxasyncbio, CTLFLAG_RW, &nfs_maxasyncbio, 0,
131 "Max number of asynchronous bio's");
984263bc 132
92540a7e
MD
133static int nfs_request_setup(nfsm_info_t info);
134static int nfs_request_auth(struct nfsreq *rep);
135static int nfs_request_try(struct nfsreq *rep);
136static int nfs_request_waitreply(struct nfsreq *rep);
137static int nfs_request_processreply(nfsm_info_t info, int);
984263bc 138
984263bc
MD
139int nfsrtton = 0;
140struct nfsrtt nfsrtt;
f786cc86 141struct callout nfs_timer_handle;
984263bc 142
a6ee311a 143static int nfs_msg (struct thread *,char *,char *);
edb90c22
MD
144static int nfs_rcvlock (struct nfsmount *nmp, struct nfsreq *myreq);
145static void nfs_rcvunlock (struct nfsmount *nmp);
a6ee311a 146static void nfs_realign (struct mbuf **pm, int hsiz);
edb90c22
MD
147static int nfs_receive (struct nfsmount *nmp, struct nfsreq *rep,
148 struct sockaddr **aname, struct mbuf **mp);
f8565b0f
MD
149static void nfs_softterm (struct nfsreq *rep, int islocked);
150static void nfs_hardterm (struct nfsreq *rep, int islocked);
edb90c22 151static int nfs_reconnect (struct nfsmount *nmp, struct nfsreq *rep);
984263bc 152#ifndef NFS_NOSERVER
52553028 153static int nfsrv_getstream (struct nfssvc_sock *, int, int *);
e21aec5b 154static void nfs_timer_req(struct nfsreq *req);
7f3ffbb4 155static void nfs_checkpkt(struct mbuf *m, int len);
984263bc 156
a6ee311a 157int (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
984263bc 158 struct nfssvc_sock *slp,
dadab5e9 159 struct thread *td,
a6ee311a 160 struct mbuf **mreqp) = {
984263bc
MD
161 nfsrv_null,
162 nfsrv_getattr,
163 nfsrv_setattr,
164 nfsrv_lookup,
165 nfsrv3_access,
166 nfsrv_readlink,
167 nfsrv_read,
168 nfsrv_write,
169 nfsrv_create,
170 nfsrv_mkdir,
171 nfsrv_symlink,
172 nfsrv_mknod,
173 nfsrv_remove,
174 nfsrv_rmdir,
175 nfsrv_rename,
176 nfsrv_link,
177 nfsrv_readdir,
178 nfsrv_readdirplus,
179 nfsrv_statfs,
180 nfsrv_fsinfo,
181 nfsrv_pathconf,
182 nfsrv_commit,
e07fef60
MD
183 nfsrv_noop,
184 nfsrv_noop,
984263bc
MD
185 nfsrv_noop,
186 nfsrv_noop
187};
188#endif /* NFS_NOSERVER */
189
190/*
191 * Initialize sockets and congestion for a new NFS connection.
192 * We do not free the sockaddr if error.
193 */
194int
dadab5e9 195nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
984263bc 196{
dadab5e9 197 struct socket *so;
cc7d050e 198 int error;
984263bc
MD
199 struct sockaddr *saddr;
200 struct sockaddr_in *sin;
dadab5e9 201 struct thread *td = &thread0; /* only used for socreate and sobind */
984263bc 202
978c934b 203 nmp->nm_so = so = NULL;
d9adbeaf
MD
204 if (nmp->nm_flag & NFSMNT_FORCE)
205 return (EINVAL);
984263bc 206 saddr = nmp->nm_nam;
978c934b 207 error = socreate(saddr->sa_family, &so, nmp->nm_sotype,
dadab5e9 208 nmp->nm_soproto, td);
984263bc
MD
209 if (error)
210 goto bad;
984263bc
MD
211 nmp->nm_soflags = so->so_proto->pr_flags;
212
213 /*
214 * Some servers require that the client port be a reserved port number.
215 */
216 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
217 struct sockopt sopt;
218 int ip;
219 struct sockaddr_in ssin;
220
221 bzero(&sopt, sizeof sopt);
222 ip = IP_PORTRANGE_LOW;
984263bc
MD
223 sopt.sopt_level = IPPROTO_IP;
224 sopt.sopt_name = IP_PORTRANGE;
225 sopt.sopt_val = (void *)&ip;
226 sopt.sopt_valsize = sizeof(ip);
dadab5e9 227 sopt.sopt_td = NULL;
984263bc
MD
228 error = sosetopt(so, &sopt);
229 if (error)
230 goto bad;
231 bzero(&ssin, sizeof ssin);
232 sin = &ssin;
233 sin->sin_len = sizeof (struct sockaddr_in);
234 sin->sin_family = AF_INET;
235 sin->sin_addr.s_addr = INADDR_ANY;
236 sin->sin_port = htons(0);
dadab5e9 237 error = sobind(so, (struct sockaddr *)sin, td);
984263bc
MD
238 if (error)
239 goto bad;
240 bzero(&sopt, sizeof sopt);
241 ip = IP_PORTRANGE_DEFAULT;
984263bc
MD
242 sopt.sopt_level = IPPROTO_IP;
243 sopt.sopt_name = IP_PORTRANGE;
244 sopt.sopt_val = (void *)&ip;
245 sopt.sopt_valsize = sizeof(ip);
dadab5e9 246 sopt.sopt_td = NULL;
984263bc
MD
247 error = sosetopt(so, &sopt);
248 if (error)
249 goto bad;
250 }
251
252 /*
253 * Protocols that do not require connections may be optionally left
254 * unconnected for servers that reply from a port other than NFS_PORT.
255 */
256 if (nmp->nm_flag & NFSMNT_NOCONN) {
257 if (nmp->nm_soflags & PR_CONNREQUIRED) {
258 error = ENOTCONN;
259 goto bad;
260 }
261 } else {
dadab5e9 262 error = soconnect(so, nmp->nm_nam, td);
984263bc
MD
263 if (error)
264 goto bad;
265
266 /*
267 * Wait for the connection to complete. Cribbed from the
268 * connect system call but with the wait timing out so
269 * that interruptible mounts don't hang here for a long time.
270 */
165dba55 271 crit_enter();
984263bc 272 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
377d4740 273 (void) tsleep((caddr_t)&so->so_timeo, 0,
984263bc
MD
274 "nfscon", 2 * hz);
275 if ((so->so_state & SS_ISCONNECTING) &&
276 so->so_error == 0 && rep &&
dadab5e9 277 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0){
6cef7136 278 soclrstate(so, SS_ISCONNECTING);
165dba55 279 crit_exit();
984263bc
MD
280 goto bad;
281 }
282 }
283 if (so->so_error) {
284 error = so->so_error;
285 so->so_error = 0;
165dba55 286 crit_exit();
984263bc
MD
287 goto bad;
288 }
165dba55 289 crit_exit();
984263bc 290 }
6d49aa6f
MD
291 so->so_rcv.ssb_timeo = (5 * hz);
292 so->so_snd.ssb_timeo = (5 * hz);
984263bc
MD
293
294 /*
295 * Get buffer reservation size from sysctl, but impose reasonable
296 * limits.
297 */
cc7d050e 298 if (nmp->nm_sotype == SOCK_STREAM) {
984263bc
MD
299 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
300 struct sockopt sopt;
301 int val;
302
303 bzero(&sopt, sizeof sopt);
304 sopt.sopt_level = SOL_SOCKET;
305 sopt.sopt_name = SO_KEEPALIVE;
306 sopt.sopt_val = &val;
307 sopt.sopt_valsize = sizeof val;
308 val = 1;
309 sosetopt(so, &sopt);
310 }
311 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
312 struct sockopt sopt;
313 int val;
314
315 bzero(&sopt, sizeof sopt);
316 sopt.sopt_level = IPPROTO_TCP;
317 sopt.sopt_name = TCP_NODELAY;
318 sopt.sopt_val = &val;
319 sopt.sopt_valsize = sizeof val;
320 val = 1;
321 sosetopt(so, &sopt);
8fdd18a4
MD
322
323 bzero(&sopt, sizeof sopt);
324 sopt.sopt_level = IPPROTO_TCP;
325 sopt.sopt_name = TCP_FASTKEEP;
326 sopt.sopt_val = &val;
327 sopt.sopt_valsize = sizeof val;
328 val = 1;
329 sosetopt(so, &sopt);
984263bc 330 }
984263bc 331 }
cc7d050e 332 error = soreserve(so, nfs_soreserve, nfs_soreserve, NULL);
984263bc
MD
333 if (error)
334 goto bad;
14343ad3
MD
335 atomic_set_int(&so->so_rcv.ssb_flags, SSB_NOINTR);
336 atomic_set_int(&so->so_snd.ssb_flags, SSB_NOINTR);
984263bc
MD
337
338 /* Initialize other non-zero congestion variables */
339 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
f8565b0f 340 nmp->nm_srtt[3] = (NFS_TIMEO << NFS_RTT_SCALE_BITS);
984263bc
MD
341 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
342 nmp->nm_sdrtt[3] = 0;
f8565b0f 343 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED;
984263bc 344 nmp->nm_timeouts = 0;
978c934b
MD
345
346 /*
347 * Assign nm_so last. The moment nm_so is assigned the nfs_timer()
348 * can mess with the socket.
349 */
350 nmp->nm_so = so;
984263bc
MD
351 return (0);
352
353bad:
978c934b
MD
354 if (so) {
355 soshutdown(so, SHUT_RDWR);
356 soclose(so, FNONBLOCK);
357 }
984263bc
MD
358 return (error);
359}
360
361/*
362 * Reconnect routine:
363 * Called when a connection is broken on a reliable protocol.
364 * - clean up the old socket
365 * - nfs_connect() again
f8565b0f 366 * - set R_NEEDSXMIT for all outstanding requests on mount point
984263bc
MD
367 * If this fails the mount point is DEAD!
368 * nb: Must be called with the nfs_sndlock() set on the mount point.
369 */
370static int
edb90c22 371nfs_reconnect(struct nfsmount *nmp, struct nfsreq *rep)
984263bc 372{
e21aec5b 373 struct nfsreq *req;
984263bc
MD
374 int error;
375
376 nfs_disconnect(nmp);
a63246d1
MD
377 if (nmp->nm_rxstate >= NFSSVC_STOPPING)
378 return (EINTR);
984263bc
MD
379 while ((error = nfs_connect(nmp, rep)) != 0) {
380 if (error == EINTR || error == ERESTART)
381 return (EINTR);
d9adbeaf
MD
382 if (error == EINVAL)
383 return (error);
a63246d1
MD
384 if (nmp->nm_rxstate >= NFSSVC_STOPPING)
385 return (EINTR);
377d4740 386 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0);
984263bc
MD
387 }
388
389 /*
390 * Loop through outstanding request list and fix up all requests
391 * on old socket.
392 */
91f46891 393 crit_enter();
e21aec5b
MD
394 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
395 KKASSERT(req->r_nmp == nmp);
f8565b0f 396 req->r_flags |= R_NEEDSXMIT;
984263bc 397 }
91f46891 398 crit_exit();
984263bc
MD
399 return (0);
400}
401
402/*
403 * NFS disconnect. Clean up and unlink.
404 */
405void
e851b29e 406nfs_disconnect(struct nfsmount *nmp)
984263bc 407{
40393ded 408 struct socket *so;
984263bc
MD
409
410 if (nmp->nm_so) {
411 so = nmp->nm_so;
60233e58 412 nmp->nm_so = NULL;
e5d03018 413 soshutdown(so, SHUT_RDWR);
9ba76b73 414 soclose(so, FNONBLOCK);
984263bc
MD
415 }
416}
417
418void
e851b29e 419nfs_safedisconnect(struct nfsmount *nmp)
984263bc 420{
edb90c22 421 nfs_rcvlock(nmp, NULL);
984263bc 422 nfs_disconnect(nmp);
edb90c22 423 nfs_rcvunlock(nmp);
984263bc
MD
424}
425
426/*
427 * This is the nfs send routine. For connection based socket types, it
428 * must be called with an nfs_sndlock() on the socket.
429 * "rep == NULL" indicates that it has been called from a server.
430 * For the client side:
431 * - return EINTR if the RPC is terminated, 0 otherwise
f8565b0f 432 * - set R_NEEDSXMIT if the send fails for any reason
984263bc
MD
433 * - do any cleanup required by recoverable socket errors (?)
434 * For the server side:
435 * - return EINTR or ERESTART if interrupted by a signal
436 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
437 * - do any cleanup required by recoverable socket errors (?)
438 */
439int
e851b29e
CP
440nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
441 struct nfsreq *rep)
984263bc
MD
442{
443 struct sockaddr *sendnam;
444 int error, soflags, flags;
445
446 if (rep) {
447 if (rep->r_flags & R_SOFTTERM) {
448 m_freem(top);
449 return (EINTR);
450 }
451 if ((so = rep->r_nmp->nm_so) == NULL) {
f8565b0f 452 rep->r_flags |= R_NEEDSXMIT;
984263bc
MD
453 m_freem(top);
454 return (0);
455 }
f8565b0f 456 rep->r_flags &= ~R_NEEDSXMIT;
984263bc 457 soflags = rep->r_nmp->nm_soflags;
f8565b0f 458 } else {
984263bc 459 soflags = so->so_proto->pr_flags;
f8565b0f 460 }
984263bc 461 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
60233e58 462 sendnam = NULL;
984263bc
MD
463 else
464 sendnam = nam;
465 if (so->so_type == SOCK_SEQPACKET)
466 flags = MSG_EOR;
467 else
468 flags = 0;
469
7f3ffbb4
MD
470 /*
471 * calls pru_sosend -> sosend -> so_pru_send -> netrpc
472 */
6b6e0885 473 error = so_pru_sosend(so, sendnam, NULL, top, NULL, flags,
7f3ffbb4 474 curthread /*XXX*/);
5e6f1ca5 475
984263bc
MD
476 /*
477 * ENOBUFS for dgram sockets is transient and non fatal.
478 * No need to log, and no need to break a soft mount.
479 */
480 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
481 error = 0;
f8565b0f
MD
482 /*
483 * do backoff retransmit on client
484 */
cc7d050e
MD
485 if (rep) {
486 if ((rep->r_nmp->nm_state & NFSSTA_SENDSPACE) == 0) {
487 rep->r_nmp->nm_state |= NFSSTA_SENDSPACE;
488 kprintf("Warning: NFS: Insufficient sendspace "
489 "(%lu),\n"
490 "\t You must increase vfs.nfs.soreserve"
491 "or decrease vfs.nfs.maxasyncbio\n",
492 so->so_snd.ssb_hiwat);
493 }
f8565b0f 494 rep->r_flags |= R_NEEDSXMIT;
cc7d050e 495 }
984263bc
MD
496 }
497
498 if (error) {
499 if (rep) {
500 log(LOG_INFO, "nfs send error %d for server %s\n",error,
501 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
502 /*
503 * Deal with errors for the client side.
504 */
505 if (rep->r_flags & R_SOFTTERM)
506 error = EINTR;
507 else
f8565b0f
MD
508 rep->r_flags |= R_NEEDSXMIT;
509 } else {
984263bc 510 log(LOG_INFO, "nfsd send error %d\n", error);
f8565b0f 511 }
984263bc
MD
512
513 /*
514 * Handle any recoverable (soft) socket errors here. (?)
515 */
516 if (error != EINTR && error != ERESTART &&
517 error != EWOULDBLOCK && error != EPIPE)
518 error = 0;
519 }
520 return (error);
521}
522
523/*
524 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
525 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
526 * Mark and consolidate the data into a new mbuf list.
527 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
528 * small mbufs.
529 * For SOCK_STREAM we must be very careful to read an entire record once
530 * we have read any of it, even if the system call has been interrupted.
531 */
532static int
edb90c22
MD
533nfs_receive(struct nfsmount *nmp, struct nfsreq *rep,
534 struct sockaddr **aname, struct mbuf **mp)
984263bc 535{
40393ded 536 struct socket *so;
6d49aa6f 537 struct sockbuf sio;
984263bc
MD
538 struct uio auio;
539 struct iovec aio;
40393ded 540 struct mbuf *m;
984263bc
MD
541 struct mbuf *control;
542 u_int32_t len;
543 struct sockaddr **getnam;
544 int error, sotype, rcvflg;
dadab5e9 545 struct thread *td = curthread; /* XXX */
984263bc
MD
546
547 /*
548 * Set up arguments for soreceive()
549 */
d8a9a23b
MD
550 *mp = NULL;
551 *aname = NULL;
edb90c22 552 sotype = nmp->nm_sotype;
984263bc
MD
553
554 /*
555 * For reliable protocols, lock against other senders/receivers
556 * in case a reconnect is necessary.
557 * For SOCK_STREAM, first get the Record Mark to find out how much
558 * more there is to get.
559 * We must lock the socket against other receivers
560 * until we have an entire rpc request/reply.
561 */
562 if (sotype != SOCK_DGRAM) {
edb90c22 563 error = nfs_sndlock(nmp, rep);
984263bc
MD
564 if (error)
565 return (error);
566tryagain:
567 /*
568 * Check for fatal errors and resending request.
569 */
570 /*
571 * Ugh: If a reconnect attempt just happened, nm_so
572 * would have changed. NULL indicates a failed
573 * attempt that has essentially shut down this
574 * mount point.
575 */
edb90c22
MD
576 if (rep && (rep->r_mrep || (rep->r_flags & R_SOFTTERM))) {
577 nfs_sndunlock(nmp);
984263bc
MD
578 return (EINTR);
579 }
edb90c22
MD
580 so = nmp->nm_so;
581 if (so == NULL) {
582 error = nfs_reconnect(nmp, rep);
984263bc 583 if (error) {
edb90c22 584 nfs_sndunlock(nmp);
984263bc
MD
585 return (error);
586 }
587 goto tryagain;
588 }
f8565b0f 589 while (rep && (rep->r_flags & R_NEEDSXMIT)) {
74f1caca 590 m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
984263bc
MD
591 nfsstats.rpcretries++;
592 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
593 if (error) {
594 if (error == EINTR || error == ERESTART ||
edb90c22
MD
595 (error = nfs_reconnect(nmp, rep)) != 0) {
596 nfs_sndunlock(nmp);
984263bc
MD
597 return (error);
598 }
599 goto tryagain;
600 }
601 }
edb90c22 602 nfs_sndunlock(nmp);
984263bc 603 if (sotype == SOCK_STREAM) {
d8a9a23b
MD
604 /*
605 * Get the length marker from the stream
606 */
607 aio.iov_base = (caddr_t)&len;
984263bc
MD
608 aio.iov_len = sizeof(u_int32_t);
609 auio.uio_iov = &aio;
610 auio.uio_iovcnt = 1;
611 auio.uio_segflg = UIO_SYSSPACE;
612 auio.uio_rw = UIO_READ;
613 auio.uio_offset = 0;
614 auio.uio_resid = sizeof(u_int32_t);
dadab5e9 615 auio.uio_td = td;
984263bc
MD
616 do {
617 rcvflg = MSG_WAITALL;
6b6e0885 618 error = so_pru_soreceive(so, NULL, &auio, NULL,
d8a9a23b 619 NULL, &rcvflg);
984263bc
MD
620 if (error == EWOULDBLOCK && rep) {
621 if (rep->r_flags & R_SOFTTERM)
622 return (EINTR);
623 }
624 } while (error == EWOULDBLOCK);
d8a9a23b
MD
625
626 if (error == 0 && auio.uio_resid > 0) {
984263bc 627 /*
d8a9a23b 628 * Only log short packets if not EOF
984263bc 629 */
d8a9a23b 630 if (auio.uio_resid != sizeof(u_int32_t))
984263bc
MD
631 log(LOG_INFO,
632 "short receive (%d/%d) from nfs server %s\n",
633 (int)(sizeof(u_int32_t) - auio.uio_resid),
634 (int)sizeof(u_int32_t),
edb90c22 635 nmp->nm_mountp->mnt_stat.f_mntfromname);
984263bc
MD
636 error = EPIPE;
637 }
638 if (error)
639 goto errout;
640 len = ntohl(len) & ~0x80000000;
641 /*
642 * This is SERIOUS! We are out of sync with the sender
643 * and forcing a disconnect/reconnect is all I can do.
644 */
645 if (len > NFS_MAXPACKET) {
646 log(LOG_ERR, "%s (%d) from nfs server %s\n",
647 "impossible packet length",
648 len,
edb90c22 649 nmp->nm_mountp->mnt_stat.f_mntfromname);
984263bc
MD
650 error = EFBIG;
651 goto errout;
652 }
d8a9a23b
MD
653
654 /*
655 * Get the rest of the packet as an mbuf chain
656 */
6d49aa6f 657 sbinit(&sio, len);
984263bc
MD
658 do {
659 rcvflg = MSG_WAITALL;
d8a9a23b
MD
660 error = so_pru_soreceive(so, NULL, NULL, &sio,
661 NULL, &rcvflg);
984263bc
MD
662 } while (error == EWOULDBLOCK || error == EINTR ||
663 error == ERESTART);
6d49aa6f
MD
664 if (error == 0 && sio.sb_cc != len) {
665 if (sio.sb_cc != 0)
984263bc 666 log(LOG_INFO,
bfc09ba0
MD
667 "short receive (%zu/%d) from nfs server %s\n",
668 (size_t)len - auio.uio_resid, len,
edb90c22 669 nmp->nm_mountp->mnt_stat.f_mntfromname);
984263bc
MD
670 error = EPIPE;
671 }
6d49aa6f 672 *mp = sio.sb_mb;
984263bc
MD
673 } else {
674 /*
d8a9a23b
MD
675 * Non-stream, so get the whole packet by not
676 * specifying MSG_WAITALL and by specifying a large
677 * length.
678 *
984263bc
MD
679 * We have no use for control msg., but must grab them
680 * and then throw them away so we know what is going
681 * on.
682 */
6d49aa6f 683 sbinit(&sio, 100000000);
984263bc
MD
684 do {
685 rcvflg = 0;
d8a9a23b
MD
686 error = so_pru_soreceive(so, NULL, NULL, &sio,
687 &control, &rcvflg);
984263bc
MD
688 if (control)
689 m_freem(control);
690 if (error == EWOULDBLOCK && rep) {
d8a9a23b 691 if (rep->r_flags & R_SOFTTERM) {
6d49aa6f 692 m_freem(sio.sb_mb);
984263bc 693 return (EINTR);
d8a9a23b 694 }
984263bc
MD
695 }
696 } while (error == EWOULDBLOCK ||
6d49aa6f 697 (error == 0 && sio.sb_mb == NULL && control));
984263bc 698 if ((rcvflg & MSG_EOR) == 0)
086c1d7e 699 kprintf("Egad!!\n");
6d49aa6f 700 if (error == 0 && sio.sb_mb == NULL)
984263bc 701 error = EPIPE;
6d49aa6f
MD
702 len = sio.sb_cc;
703 *mp = sio.sb_mb;
984263bc
MD
704 }
705errout:
706 if (error && error != EINTR && error != ERESTART) {
707 m_freem(*mp);
d8a9a23b
MD
708 *mp = NULL;
709 if (error != EPIPE) {
984263bc
MD
710 log(LOG_INFO,
711 "receive error %d from nfs server %s\n",
712 error,
edb90c22 713 nmp->nm_mountp->mnt_stat.f_mntfromname);
d8a9a23b 714 }
edb90c22 715 error = nfs_sndlock(nmp, rep);
984263bc 716 if (!error) {
edb90c22 717 error = nfs_reconnect(nmp, rep);
984263bc
MD
718 if (!error)
719 goto tryagain;
720 else
edb90c22 721 nfs_sndunlock(nmp);
984263bc
MD
722 }
723 }
724 } else {
edb90c22 725 if ((so = nmp->nm_so) == NULL)
984263bc
MD
726 return (EACCES);
727 if (so->so_state & SS_ISCONNECTED)
d8a9a23b 728 getnam = NULL;
984263bc
MD
729 else
730 getnam = aname;
6d49aa6f 731 sbinit(&sio, 100000000);
984263bc
MD
732 do {
733 rcvflg = 0;
d8a9a23b
MD
734 error = so_pru_soreceive(so, getnam, NULL, &sio,
735 NULL, &rcvflg);
edb90c22 736 if (error == EWOULDBLOCK && rep &&
d8a9a23b 737 (rep->r_flags & R_SOFTTERM)) {
6d49aa6f 738 m_freem(sio.sb_mb);
984263bc 739 return (EINTR);
d8a9a23b 740 }
984263bc 741 } while (error == EWOULDBLOCK);
524e83aa 742
6d49aa6f
MD
743 len = sio.sb_cc;
744 *mp = sio.sb_mb;
524e83aa
MD
745
746 /*
747 * A shutdown may result in no error and no mbuf.
748 * Convert to EPIPE.
749 */
750 if (*mp == NULL && error == 0)
751 error = EPIPE;
984263bc
MD
752 }
753 if (error) {
754 m_freem(*mp);
d8a9a23b 755 *mp = NULL;
984263bc 756 }
524e83aa 757
984263bc
MD
758 /*
759 * Search for any mbufs that are not a multiple of 4 bytes long
760 * or with m_data not longword aligned.
761 * These could cause pointer alignment problems, so copy them to
762 * well aligned mbufs.
763 */
764 nfs_realign(mp, 5 * NFSX_UNSIGNED);
765 return (error);
766}
767
768/*
769 * Implement receipt of reply on a socket.
edb90c22 770 *
984263bc
MD
771 * We must search through the list of received datagrams matching them
772 * with outstanding requests using the xid, until ours is found.
edb90c22
MD
773 *
774 * If myrep is NULL we process packets on the socket until
775 * interrupted or until nm_reqrxq is non-empty.
984263bc
MD
776 */
777/* ARGSUSED */
778int
edb90c22 779nfs_reply(struct nfsmount *nmp, struct nfsreq *myrep)
984263bc 780{
40393ded 781 struct nfsreq *rep;
984263bc 782 struct sockaddr *nam;
42edf14f
MD
783 u_int32_t rxid;
784 u_int32_t *tl;
984263bc 785 int error;
42edf14f 786 struct nfsm_info info;
984263bc
MD
787
788 /*
789 * Loop around until we get our own reply
790 */
791 for (;;) {
792 /*
793 * Lock against other receivers so that I don't get stuck in
794 * sbwait() after someone else has received my reply for me.
795 * Also necessary for connection based protocols to avoid
796 * race conditions during a reconnect.
8684e6f9 797 *
984263bc
MD
798 * If nfs_rcvlock() returns EALREADY, that means that
799 * the reply has already been recieved by another
800 * process and we can return immediately. In this
801 * case, the lock is not taken to avoid races with
802 * other processes.
803 */
42edf14f
MD
804 info.mrep = NULL;
805
edb90c22 806 error = nfs_rcvlock(nmp, myrep);
984263bc
MD
807 if (error == EALREADY)
808 return (0);
809 if (error)
810 return (error);
edb90c22
MD
811
812 /*
813 * If myrep is NULL we are the receiver helper thread.
814 * Stop waiting for incoming replies if there are
13ddc895
MD
815 * messages sitting on reqrxq that we need to process,
816 * or if a shutdown request is pending.
edb90c22 817 */
13ddc895
MD
818 if (myrep == NULL && (TAILQ_FIRST(&nmp->nm_reqrxq) ||
819 nmp->nm_rxstate > NFSSVC_PENDING)) {
edb90c22
MD
820 nfs_rcvunlock(nmp);
821 return(EWOULDBLOCK);
822 }
823
984263bc
MD
824 /*
825 * Get the next Rpc reply off the socket
f8565b0f
MD
826 *
827 * We cannot release the receive lock until we've
828 * filled in rep->r_mrep, otherwise a waiting
829 * thread may deadlock in soreceive with no incoming
830 * packets expected.
984263bc 831 */
edb90c22 832 error = nfs_receive(nmp, myrep, &nam, &info.mrep);
984263bc 833 if (error) {
984263bc
MD
834 /*
835 * Ignore routing errors on connectionless protocols??
836 */
f8565b0f 837 nfs_rcvunlock(nmp);
984263bc 838 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
edb90c22
MD
839 if (nmp->nm_so == NULL)
840 return (error);
984263bc 841 nmp->nm_so->so_error = 0;
984263bc
MD
842 continue;
843 }
844 return (error);
845 }
846 if (nam)
884717e1 847 kfree(nam, M_SONAME);
984263bc
MD
848
849 /*
850 * Get the xid and check that it is an rpc reply
851 */
42edf14f
MD
852 info.md = info.mrep;
853 info.dpos = mtod(info.md, caddr_t);
854 NULLOUT(tl = nfsm_dissect(&info, 2*NFSX_UNSIGNED));
984263bc
MD
855 rxid = *tl++;
856 if (*tl != rpc_reply) {
984263bc 857 nfsstats.rpcinvalid++;
42edf14f
MD
858 m_freem(info.mrep);
859 info.mrep = NULL;
984263bc 860nfsmout:
f8565b0f 861 nfs_rcvunlock(nmp);
984263bc
MD
862 continue;
863 }
864
865 /*
866 * Loop through the request list to match up the reply
91f46891
MD
867 * Iff no match, just drop the datagram. On match, set
868 * r_mrep atomically to prevent the timer from messing
869 * around with the request after we have exited the critical
870 * section.
984263bc 871 */
91f46891 872 crit_enter();
e21aec5b 873 TAILQ_FOREACH(rep, &nmp->nm_reqq, r_chain) {
8684e6f9 874 if (rep->r_mrep == NULL && rxid == rep->r_xid)
91f46891 875 break;
91f46891 876 }
91f46891
MD
877
878 /*
879 * Fill in the rest of the reply if we found a match.
cc7d050e
MD
880 *
881 * Deal with duplicate responses if there was no match.
91f46891
MD
882 */
883 if (rep) {
42edf14f
MD
884 rep->r_md = info.md;
885 rep->r_dpos = info.dpos;
91f46891
MD
886 if (nfsrtton) {
887 struct rttl *rt;
888
889 rt = &nfsrtt.rttl[nfsrtt.pos];
890 rt->proc = rep->r_procnum;
f8565b0f
MD
891 rt->rto = 0;
892 rt->sent = 0;
893 rt->cwnd = nmp->nm_maxasync_scaled;
91f46891
MD
894 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
895 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
896 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
897 getmicrotime(&rt->tstamp);
898 if (rep->r_flags & R_TIMING)
899 rt->rtt = rep->r_rtt;
900 else
901 rt->rtt = 1000000;
902 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
903 }
f8565b0f 904
91f46891 905 /*
f8565b0f
MD
906 * New congestion control is based only on async
907 * requests.
91f46891 908 */
f8565b0f
MD
909 if (nmp->nm_maxasync_scaled < NFS_MAXASYNC_SCALED)
910 ++nmp->nm_maxasync_scaled;
91f46891
MD
911 if (rep->r_flags & R_SENT) {
912 rep->r_flags &= ~R_SENT;
91f46891 913 }
91f46891
MD
914 /*
915 * Update rtt using a gain of 0.125 on the mean
916 * and a gain of 0.25 on the deviation.
f8565b0f
MD
917 *
918 * NOTE SRTT/SDRTT are only good if R_TIMING is set.
91f46891 919 */
cc7d050e 920 if ((rep->r_flags & R_TIMING) && rep->r_rexmit == 0) {
984263bc 921 /*
91f46891
MD
922 * Since the timer resolution of
923 * NFS_HZ is so course, it can often
924 * result in r_rtt == 0. Since
925 * r_rtt == N means that the actual
926 * rtt is between N+dt and N+2-dt ticks,
927 * add 1.
984263bc 928 */
f8565b0f
MD
929 int n;
930 int d;
edb90c22 931
f8565b0f
MD
932#define NFSRSB NFS_RTT_SCALE_BITS
933 n = ((NFS_SRTT(rep) * 7) +
934 (rep->r_rtt << NFSRSB)) >> 3;
935 d = n - NFS_SRTT(rep);
936 NFS_SRTT(rep) = n;
edb90c22 937
f8565b0f
MD
938 /*
939 * Don't let the jitter calculation decay
940 * too quickly, but we want a fast rampup.
941 */
942 if (d < 0)
943 d = -d;
944 d <<= NFSRSB;
945 if (d < NFS_SDRTT(rep))
946 n = ((NFS_SDRTT(rep) * 15) + d) >> 4;
947 else
948 n = ((NFS_SDRTT(rep) * 3) + d) >> 2;
949 NFS_SDRTT(rep) = n;
950#undef NFSRSB
edb90c22 951 }
f8565b0f
MD
952 nmp->nm_timeouts = 0;
953 rep->r_mrep = info.mrep;
954 nfs_hardterm(rep, 0);
cc7d050e
MD
955 } else {
956 /*
957 * Extract vers, prog, nfsver, procnum. A duplicate
958 * response means we didn't wait long enough so
959 * we increase the SRTT to avoid future spurious
960 * timeouts.
961 */
962 u_int procnum = nmp->nm_lastreprocnum;
963 int n;
964
965 if (procnum < NFS_NPROCS && proct[procnum]) {
966 if (nfs_showrexmit)
967 kprintf("D");
968 n = nmp->nm_srtt[proct[procnum]];
969 n += NFS_ASYSCALE * NFS_HZ;
970 if (n < NFS_ASYSCALE * NFS_HZ * 10)
971 n = NFS_ASYSCALE * NFS_HZ * 10;
972 nmp->nm_srtt[proct[procnum]] = n;
973 }
984263bc 974 }
f8565b0f
MD
975 nfs_rcvunlock(nmp);
976 crit_exit();
977
984263bc
MD
978 /*
979 * If not matched to a request, drop it.
980 * If it's mine, get out.
981 */
91f46891 982 if (rep == NULL) {
984263bc 983 nfsstats.rpcunexpected++;
42edf14f
MD
984 m_freem(info.mrep);
985 info.mrep = NULL;
984263bc
MD
986 } else if (rep == myrep) {
987 if (rep->r_mrep == NULL)
988 panic("nfsreply nil");
989 return (0);
990 }
984263bc
MD
991 }
992}
993
92540a7e
MD
994/*
995 * Run the request state machine until the target state is reached
996 * or a fatal error occurs. The target state is not run. Specifying
997 * a target of NFSM_STATE_DONE runs the state machine until the rpc
998 * is complete.
999 *
1000 * EINPROGRESS is returned for all states other then the DONE state,
1001 * indicating that the rpc is still in progress.
1002 */
8684e6f9 1003int
edb90c22 1004nfs_request(struct nfsm_info *info, nfsm_state_t bstate, nfsm_state_t estate)
8684e6f9 1005{
92540a7e 1006 struct nfsreq *req;
8684e6f9 1007
edb90c22 1008 while (info->state >= bstate && info->state < estate) {
92540a7e
MD
1009 switch(info->state) {
1010 case NFSM_STATE_SETUP:
1011 /*
1012 * Setup the nfsreq. Any error which occurs during
1013 * this state is fatal.
1014 */
1015 info->error = nfs_request_setup(info);
1016 if (info->error) {
1017 info->state = NFSM_STATE_DONE;
1018 return (info->error);
1019 } else {
1020 req = info->req;
1021 req->r_mrp = &info->mrep;
1022 req->r_mdp = &info->md;
1023 req->r_dposp = &info->dpos;
1024 info->state = NFSM_STATE_AUTH;
1025 }
1026 break;
1027 case NFSM_STATE_AUTH:
1028 /*
1029 * Authenticate the nfsreq. Any error which occurs
1030 * during this state is fatal.
1031 */
1032 info->error = nfs_request_auth(info->req);
1033 if (info->error) {
1034 info->state = NFSM_STATE_DONE;
1035 return (info->error);
1036 } else {
1037 info->state = NFSM_STATE_TRY;
1038 }
1039 break;
1040 case NFSM_STATE_TRY:
1041 /*
1042 * Transmit or retransmit attempt. An error in this
1043 * state is ignored and we always move on to the
1044 * next state.
edb90c22
MD
1045 *
1046 * This can trivially race the receiver if the
f8565b0f
MD
1047 * request is asynchronous. nfs_request_try()
1048 * will thus set the state for us and we
1049 * must also return immediately if we are
1050 * running an async state machine, because
1051 * info can become invalid due to races after
1052 * try() returns.
edb90c22 1053 */
f8565b0f
MD
1054 if (info->req->r_flags & R_ASYNC) {
1055 nfs_request_try(info->req);
1056 if (estate == NFSM_STATE_WAITREPLY)
1057 return (EINPROGRESS);
edb90c22 1058 } else {
f8565b0f 1059 nfs_request_try(info->req);
edb90c22
MD
1060 info->state = NFSM_STATE_WAITREPLY;
1061 }
92540a7e
MD
1062 break;
1063 case NFSM_STATE_WAITREPLY:
1064 /*
1065 * Wait for a reply or timeout and move on to the
1066 * next state. The error returned by this state
1067 * is passed to the processing code in the next
1068 * state.
1069 */
1070 info->error = nfs_request_waitreply(info->req);
1071 info->state = NFSM_STATE_PROCESSREPLY;
1072 break;
1073 case NFSM_STATE_PROCESSREPLY:
1074 /*
1075 * Process the reply or timeout. Errors which occur
1076 * in this state may cause the state machine to
1077 * go back to an earlier state, and are fatal
1078 * otherwise.
1079 */
1080 info->error = nfs_request_processreply(info,
1081 info->error);
1082 switch(info->error) {
1083 case ENEEDAUTH:
1084 info->state = NFSM_STATE_AUTH;
1085 break;
1086 case EAGAIN:
1087 info->state = NFSM_STATE_TRY;
1088 break;
1089 default:
1090 /*
1091 * Operation complete, with or without an
1092 * error. We are done.
1093 */
1094 info->req = NULL;
1095 info->state = NFSM_STATE_DONE;
1096 return (info->error);
1097 }
1098 break;
1099 case NFSM_STATE_DONE:
1100 /*
edb90c22 1101 * Shouldn't be reached
92540a7e
MD
1102 */
1103 return (info->error);
1104 /* NOT REACHED */
1105 }
1106 }
1107
1108 /*
edb90c22
MD
1109 * If we are done return the error code (if any).
1110 * Otherwise return EINPROGRESS.
92540a7e 1111 */
edb90c22
MD
1112 if (info->state == NFSM_STATE_DONE)
1113 return (info->error);
92540a7e 1114 return (EINPROGRESS);
8684e6f9
MD
1115}
1116
984263bc
MD
1117/*
1118 * nfs_request - goes something like this
1119 * - fill in request struct
1120 * - links it into list
1121 * - calls nfs_send() for first transmit
1122 * - calls nfs_receive() to get reply
1123 * - break down rpc header and return with nfs reply pointed to
1124 * by mrep or error
1125 * nb: always frees up mreq mbuf list
1126 */
92540a7e
MD
1127static int
1128nfs_request_setup(nfsm_info_t info)
984263bc 1129{
92540a7e 1130 struct nfsreq *req;
984263bc 1131 struct nfsmount *nmp;
8684e6f9
MD
1132 struct mbuf *m;
1133 int i;
984263bc 1134
92540a7e
MD
1135 /*
1136 * Reject requests while attempting a forced unmount.
1137 */
1138 if (info->vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1139 m_freem(info->mreq);
1140 info->mreq = NULL;
83a03e7b 1141 return (EIO);
984263bc 1142 }
92540a7e
MD
1143 nmp = VFSTONFS(info->vp->v_mount);
1144 req = kmalloc(sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1145 req->r_nmp = nmp;
1146 req->r_vp = info->vp;
1147 req->r_td = info->td;
1148 req->r_procnum = info->procnum;
1149 req->r_mreq = NULL;
f8565b0f
MD
1150 req->r_cred = info->cred;
1151
984263bc 1152 i = 0;
92540a7e 1153 m = info->mreq;
984263bc
MD
1154 while (m) {
1155 i += m->m_len;
1156 m = m->m_next;
1157 }
92540a7e
MD
1158 req->r_mrest = info->mreq;
1159 req->r_mrest_len = i;
edb90c22
MD
1160
1161 /*
1162 * The presence of a non-NULL r_info in req indicates
1163 * async completion via our helper threads. See the receiver
1164 * code.
1165 */
f8565b0f
MD
1166 if (info->bio) {
1167 req->r_info = info;
1168 req->r_flags = R_ASYNC;
1169 } else {
1170 req->r_info = NULL;
1171 req->r_flags = 0;
1172 }
92540a7e 1173 info->req = req;
8684e6f9
MD
1174 return(0);
1175}
1176
92540a7e 1177static int
8684e6f9
MD
1178nfs_request_auth(struct nfsreq *rep)
1179{
1180 struct nfsmount *nmp = rep->r_nmp;
1181 struct mbuf *m;
1182 char nickv[RPCX_NICKVERF];
1183 int error = 0, auth_len, auth_type;
1184 int verf_len;
1185 u_int32_t xid;
1186 char *auth_str, *verf_str;
1187 struct ucred *cred;
1188
1189 cred = rep->r_cred;
1190 rep->r_failed_auth = 0;
984263bc
MD
1191
1192 /*
1193 * Get the RPC header with authorization.
1194 */
60233e58 1195 verf_str = auth_str = NULL;
984263bc
MD
1196 if (nmp->nm_flag & NFSMNT_KERB) {
1197 verf_str = nickv;
1198 verf_len = sizeof (nickv);
1199 auth_type = RPCAUTH_KERB4;
8684e6f9
MD
1200 bzero((caddr_t)rep->r_key, sizeof(rep->r_key));
1201 if (rep->r_failed_auth ||
1202 nfs_getnickauth(nmp, cred, &auth_str, &auth_len,
1203 verf_str, verf_len)) {
984263bc 1204 error = nfs_getauth(nmp, rep, cred, &auth_str,
8684e6f9 1205 &auth_len, verf_str, &verf_len, rep->r_key);
984263bc 1206 if (error) {
8684e6f9
MD
1207 m_freem(rep->r_mrest);
1208 rep->r_mrest = NULL;
efda3bd0 1209 kfree((caddr_t)rep, M_NFSREQ);
984263bc
MD
1210 return (error);
1211 }
1212 }
1213 } else {
1214 auth_type = RPCAUTH_UNIX;
1215 if (cred->cr_ngroups < 1)
1216 panic("nfsreq nogrps");
1217 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1218 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1219 5 * NFSX_UNSIGNED;
1220 }
7f3ffbb4
MD
1221 if (rep->r_mrest)
1222 nfs_checkpkt(rep->r_mrest, rep->r_mrest_len);
8684e6f9
MD
1223 m = nfsm_rpchead(cred, nmp->nm_flag, rep->r_procnum, auth_type,
1224 auth_len, auth_str, verf_len, verf_str,
1225 rep->r_mrest, rep->r_mrest_len, &rep->r_mheadend, &xid);
1226 rep->r_mrest = NULL;
984263bc 1227 if (auth_str)
efda3bd0 1228 kfree(auth_str, M_TEMP);
984263bc
MD
1229
1230 /*
1231 * For stream protocols, insert a Sun RPC Record Mark.
1232 */
1233 if (nmp->nm_sotype == SOCK_STREAM) {
74f1caca 1234 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
68db5ea4 1235 if (m == NULL) {
efda3bd0 1236 kfree(rep, M_NFSREQ);
1cee5817 1237 return (ENOBUFS);
68db5ea4 1238 }
984263bc
MD
1239 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1240 (m->m_pkthdr.len - NFSX_UNSIGNED));
1241 }
7f3ffbb4
MD
1242
1243 nfs_checkpkt(m, m->m_pkthdr.len);
1244
984263bc
MD
1245 rep->r_mreq = m;
1246 rep->r_xid = xid;
8684e6f9
MD
1247 return (0);
1248}
1249
92540a7e 1250static int
8684e6f9
MD
1251nfs_request_try(struct nfsreq *rep)
1252{
1253 struct nfsmount *nmp = rep->r_nmp;
1254 struct mbuf *m2;
1255 int error;
1256
f8565b0f
MD
1257 /*
1258 * Request is not on any queue, only the owner has access to it
1259 * so it should not be locked by anyone atm.
1260 *
1261 * Interlock to prevent races. While locked the only remote
1262 * action possible is for r_mrep to be set (once we enqueue it).
1263 */
1264 if (rep->r_flags == 0xdeadc0de) {
7ce2998e 1265 print_backtrace(-1);
f8565b0f
MD
1266 panic("flags nbad\n");
1267 }
1268 KKASSERT((rep->r_flags & (R_LOCKED | R_ONREQQ)) == 0);
984263bc
MD
1269 if (nmp->nm_flag & NFSMNT_SOFT)
1270 rep->r_retry = nmp->nm_retry;
1271 else
1272 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1273 rep->r_rtt = rep->r_rexmit = 0;
8684e6f9 1274 if (proct[rep->r_procnum] > 0)
f8565b0f 1275 rep->r_flags |= R_TIMING | R_LOCKED;
984263bc 1276 else
f8565b0f 1277 rep->r_flags |= R_LOCKED;
984263bc
MD
1278 rep->r_mrep = NULL;
1279
984263bc 1280 nfsstats.rpcrequests++;
54938b92 1281
d9adbeaf
MD
1282 if (nmp->nm_flag & NFSMNT_FORCE) {
1283 rep->r_flags |= R_SOFTTERM;
1284 rep->r_flags &= ~R_LOCKED;
1285 return (0);
1286 }
5e6f1ca5 1287 rep->r_flags |= R_NEEDSXMIT; /* in case send lock races us */
d9adbeaf 1288
984263bc 1289 /*
5e6f1ca5
MD
1290 * Do the client side RPC.
1291 *
984263bc 1292 * Chain request into list of outstanding requests. Be sure
54938b92 1293 * to put it LAST so timer finds oldest requests first. Note
f8565b0f
MD
1294 * that our control of R_LOCKED prevents the request from
1295 * getting ripped out from under us or transmitted by the
1296 * timer code.
1297 *
1298 * For requests with info structures we must atomically set the
1299 * info's state because the structure could become invalid upon
1300 * return due to races (i.e., if async)
984263bc 1301 */
165dba55 1302 crit_enter();
8684e6f9 1303 mtx_link_init(&rep->r_link);
104db2fb 1304 KKASSERT((rep->r_flags & R_ONREQQ) == 0);
f8565b0f
MD
1305 TAILQ_INSERT_TAIL(&nmp->nm_reqq, rep, r_chain);
1306 rep->r_flags |= R_ONREQQ;
edb90c22 1307 ++nmp->nm_reqqlen;
f8565b0f
MD
1308 if (rep->r_flags & R_ASYNC)
1309 rep->r_info->state = NFSM_STATE_WAITREPLY;
1310 crit_exit();
8684e6f9
MD
1311
1312 error = 0;
984263bc 1313
984263bc 1314 /*
f8565b0f
MD
1315 * Send if we can. Congestion control is not handled here any more
1316 * becausing trying to defer the initial send based on the nfs_timer
1317 * requires having a very fast nfs_timer, which is silly.
984263bc 1318 */
f8565b0f 1319 if (nmp->nm_so) {
984263bc 1320 if (nmp->nm_soflags & PR_CONNREQUIRED)
edb90c22 1321 error = nfs_sndlock(nmp, rep);
5e6f1ca5 1322 if (error == 0 && (rep->r_flags & R_NEEDSXMIT)) {
8684e6f9 1323 m2 = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
984263bc 1324 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
f8565b0f
MD
1325 rep->r_flags &= ~R_NEEDSXMIT;
1326 if ((rep->r_flags & R_SENT) == 0) {
1327 rep->r_flags |= R_SENT;
1328 }
5e6f1ca5
MD
1329 if (nmp->nm_soflags & PR_CONNREQUIRED)
1330 nfs_sndunlock(nmp);
984263bc
MD
1331 }
1332 } else {
984263bc
MD
1333 rep->r_rtt = -1;
1334 }
8684e6f9
MD
1335 if (error == EPIPE)
1336 error = 0;
f8565b0f 1337
984263bc 1338 /*
f8565b0f
MD
1339 * Release the lock. The only remote action that may have occurred
1340 * would have been the setting of rep->r_mrep. If this occured
1341 * and the request was async we have to move it to the reader
1342 * thread's queue for action.
1343 *
1344 * For async requests also make sure the reader is woken up so
1345 * it gets on the socket to read responses.
984263bc 1346 */
f8565b0f
MD
1347 crit_enter();
1348 if (rep->r_flags & R_ASYNC) {
1349 if (rep->r_mrep)
1350 nfs_hardterm(rep, 1);
1351 rep->r_flags &= ~R_LOCKED;
1352 nfssvc_iod_reader_wakeup(nmp);
1353 } else {
1354 rep->r_flags &= ~R_LOCKED;
1355 }
1356 if (rep->r_flags & R_WANTED) {
1357 rep->r_flags &= ~R_WANTED;
1358 wakeup(rep);
1359 }
8684e6f9
MD
1360 crit_exit();
1361 return (error);
1362}
1363
f8565b0f
MD
1364/*
1365 * This code is only called for synchronous requests. Completed synchronous
1366 * requests are left on reqq and we remove them before moving on to the
1367 * processing state.
1368 */
92540a7e 1369static int
8684e6f9
MD
1370nfs_request_waitreply(struct nfsreq *rep)
1371{
1372 struct nfsmount *nmp = rep->r_nmp;
1373 int error;
1374
f8565b0f
MD
1375 KKASSERT((rep->r_flags & R_ASYNC) == 0);
1376
1377 /*
1378 * Wait until the request is finished.
1379 */
edb90c22 1380 error = nfs_reply(nmp, rep);
984263bc
MD
1381
1382 /*
91f46891
MD
1383 * RPC done, unlink the request, but don't rip it out from under
1384 * the callout timer.
f8565b0f
MD
1385 *
1386 * Once unlinked no other receiver or the timer will have
1387 * visibility, so we do not have to set R_LOCKED.
984263bc 1388 */
f8565b0f 1389 crit_enter();
91f46891 1390 while (rep->r_flags & R_LOCKED) {
f8565b0f
MD
1391 rep->r_flags |= R_WANTED;
1392 tsleep(rep, 0, "nfstrac", 0);
91f46891 1393 }
f8565b0f 1394 KKASSERT(rep->r_flags & R_ONREQQ);
e21aec5b 1395 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain);
f8565b0f 1396 rep->r_flags &= ~R_ONREQQ;
edb90c22 1397 --nmp->nm_reqqlen;
cc7d050e 1398 if (TAILQ_FIRST(&nmp->nm_bioq) &&
b9a7a2bd 1399 nmp->nm_reqqlen <= nfs_maxasyncbio * 2 / 3) {
cc7d050e
MD
1400 nfssvc_iod_writer_wakeup(nmp);
1401 }
f8565b0f 1402 crit_exit();
984263bc
MD
1403
1404 /*
1405 * Decrement the outstanding request count.
1406 */
1407 if (rep->r_flags & R_SENT) {
54938b92 1408 rep->r_flags &= ~R_SENT;
984263bc 1409 }
8684e6f9
MD
1410 return (error);
1411}
1412
1413/*
1414 * Process reply with error returned from nfs_requet_waitreply().
1415 *
1416 * Returns EAGAIN if it wants us to loop up to nfs_request_try() again.
1417 * Returns ENEEDAUTH if it wants us to loop up to nfs_request_auth() again.
1418 */
92540a7e
MD
1419static int
1420nfs_request_processreply(nfsm_info_t info, int error)
8684e6f9 1421{
92540a7e
MD
1422 struct nfsreq *req = info->req;
1423 struct nfsmount *nmp = req->r_nmp;
8684e6f9 1424 u_int32_t *tl;
8684e6f9 1425 int verf_type;
8684e6f9
MD
1426 int i;
1427
984263bc
MD
1428 /*
1429 * If there was a successful reply and a tprintf msg.
1430 * tprintf a response.
1431 */
92540a7e
MD
1432 if (error == 0 && (req->r_flags & R_TPRINTFMSG)) {
1433 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
984263bc 1434 "is alive again");
92540a7e
MD
1435 }
1436 info->mrep = req->r_mrep;
1437 info->md = req->r_md;
1438 info->dpos = req->r_dpos;
984263bc 1439 if (error) {
92540a7e
MD
1440 m_freem(req->r_mreq);
1441 req->r_mreq = NULL;
1442 kfree(req, M_NFSREQ);
1443 info->req = NULL;
984263bc
MD
1444 return (error);
1445 }
1446
1447 /*
1448 * break down the rpc header and check if ok
1449 */
92540a7e 1450 NULLOUT(tl = nfsm_dissect(info, 3 * NFSX_UNSIGNED));
984263bc 1451 if (*tl++ == rpc_msgdenied) {
8684e6f9 1452 if (*tl == rpc_mismatch) {
984263bc 1453 error = EOPNOTSUPP;
8684e6f9
MD
1454 } else if ((nmp->nm_flag & NFSMNT_KERB) &&
1455 *tl++ == rpc_autherr) {
92540a7e
MD
1456 if (req->r_failed_auth == 0) {
1457 req->r_failed_auth++;
1458 req->r_mheadend->m_next = NULL;
1459 m_freem(info->mrep);
1460 info->mrep = NULL;
1461 m_freem(req->r_mreq);
7f3ffbb4 1462 req->r_mreq = NULL;
8684e6f9
MD
1463 return (ENEEDAUTH);
1464 } else {
984263bc 1465 error = EAUTH;
8684e6f9
MD
1466 }
1467 } else {
984263bc 1468 error = EACCES;
8684e6f9 1469 }
92540a7e
MD
1470 m_freem(info->mrep);
1471 info->mrep = NULL;
1472 m_freem(req->r_mreq);
1473 req->r_mreq = NULL;
1474 kfree(req, M_NFSREQ);
1475 info->req = NULL;
984263bc
MD
1476 return (error);
1477 }
1478
1479 /*
1480 * Grab any Kerberos verifier, otherwise just throw it away.
1481 */
1482 verf_type = fxdr_unsigned(int, *tl++);
1483 i = fxdr_unsigned(int32_t, *tl);
1484 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
92540a7e
MD
1485 error = nfs_savenickauth(nmp, req->r_cred, i, req->r_key,
1486 &info->md, &info->dpos, info->mrep);
984263bc
MD
1487 if (error)
1488 goto nfsmout;
42edf14f 1489 } else if (i > 0) {
92540a7e 1490 ERROROUT(nfsm_adv(info, nfsm_rndup(i)));
42edf14f 1491 }
92540a7e 1492 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
984263bc
MD
1493 /* 0 == ok */
1494 if (*tl == 0) {
92540a7e 1495 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
984263bc
MD
1496 if (*tl != 0) {
1497 error = fxdr_unsigned(int, *tl);
f8565b0f
MD
1498
1499 /*
1500 * Does anyone even implement this? Just impose
1501 * a 1-second delay.
1502 */
984263bc
MD
1503 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1504 error == NFSERR_TRYLATER) {
92540a7e
MD
1505 m_freem(info->mrep);
1506 info->mrep = NULL;
984263bc 1507 error = 0;
f8565b0f
MD
1508
1509 tsleep((caddr_t)&lbolt, 0, "nqnfstry", 0);
8684e6f9 1510 return (EAGAIN); /* goto tryagain */
984263bc
MD
1511 }
1512
1513 /*
1514 * If the File Handle was stale, invalidate the
1515 * lookup cache, just in case.
6739ac6b
MD
1516 *
1517 * To avoid namecache<->vnode deadlocks we must
1518 * release the vnode lock if we hold it.
984263bc 1519 */
dc1be39c 1520 if (error == ESTALE) {
92540a7e 1521 struct vnode *vp = req->r_vp;
6739ac6b
MD
1522 int ltype;
1523
1524 ltype = lockstatus(&vp->v_lock, curthread);
1525 if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED)
1526 lockmgr(&vp->v_lock, LK_RELEASE);
6b008938 1527 cache_inval_vp(vp, CINV_CHILDREN);
6739ac6b
MD
1528 if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED)
1529 lockmgr(&vp->v_lock, ltype);
dc1be39c 1530 }
984263bc 1531 if (nmp->nm_flag & NFSMNT_NFSV3) {
92540a7e
MD
1532 KKASSERT(*req->r_mrp == info->mrep);
1533 KKASSERT(*req->r_mdp == info->md);
1534 KKASSERT(*req->r_dposp == info->dpos);
984263bc 1535 error |= NFSERR_RETERR;
42edf14f 1536 } else {
92540a7e
MD
1537 m_freem(info->mrep);
1538 info->mrep = NULL;
42edf14f 1539 }
92540a7e
MD
1540 m_freem(req->r_mreq);
1541 req->r_mreq = NULL;
1542 kfree(req, M_NFSREQ);
1543 info->req = NULL;
984263bc
MD
1544 return (error);
1545 }
1546
92540a7e
MD
1547 KKASSERT(*req->r_mrp == info->mrep);
1548 KKASSERT(*req->r_mdp == info->md);
1549 KKASSERT(*req->r_dposp == info->dpos);
1550 m_freem(req->r_mreq);
1551 req->r_mreq = NULL;
884717e1 1552 kfree(req, M_NFSREQ);
984263bc
MD
1553 return (0);
1554 }
92540a7e
MD
1555 m_freem(info->mrep);
1556 info->mrep = NULL;
984263bc
MD
1557 error = EPROTONOSUPPORT;
1558nfsmout:
92540a7e
MD
1559 m_freem(req->r_mreq);
1560 req->r_mreq = NULL;
1561 kfree(req, M_NFSREQ);
1562 info->req = NULL;
984263bc
MD
1563 return (error);
1564}
1565
1566#ifndef NFS_NOSERVER
1567/*
1568 * Generate the rpc reply header
1569 * siz arg. is used to decide if adding a cluster is worthwhile
1570 */
1571int
e851b29e 1572nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp,
e07fef60 1573 int err, struct mbuf **mrq, struct mbuf **mbp, caddr_t *bposp)
984263bc 1574{
40393ded 1575 u_int32_t *tl;
42edf14f 1576 struct nfsm_info info;
984263bc 1577
7771277f 1578 siz += RPC_REPLYSIZ;
42edf14f
MD
1579 info.mb = m_getl(max_hdr + siz, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
1580 info.mreq = info.mb;
1581 info.mreq->m_pkthdr.len = 0;
984263bc 1582 /*
7771277f
JH
1583 * If this is not a cluster, try and leave leading space
1584 * for the lower level headers.
984263bc 1585 */
7771277f 1586 if ((max_hdr + siz) < MINCLSIZE)
42edf14f
MD
1587 info.mreq->m_data += max_hdr;
1588 tl = mtod(info.mreq, u_int32_t *);
1589 info.mreq->m_len = 6 * NFSX_UNSIGNED;
1590 info.bpos = ((caddr_t)tl) + info.mreq->m_len;
984263bc
MD
1591 *tl++ = txdr_unsigned(nd->nd_retxid);
1592 *tl++ = rpc_reply;
1593 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1594 *tl++ = rpc_msgdenied;
1595 if (err & NFSERR_AUTHERR) {
1596 *tl++ = rpc_autherr;
1597 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
42edf14f
MD
1598 info.mreq->m_len -= NFSX_UNSIGNED;
1599 info.bpos -= NFSX_UNSIGNED;
984263bc
MD
1600 } else {
1601 *tl++ = rpc_mismatch;
1602 *tl++ = txdr_unsigned(RPC_VER2);
1603 *tl = txdr_unsigned(RPC_VER2);
1604 }
1605 } else {
1606 *tl++ = rpc_msgaccepted;
1607
1608 /*
1609 * For Kerberos authentication, we must send the nickname
1610 * verifier back, otherwise just RPCAUTH_NULL.
1611 */
1612 if (nd->nd_flag & ND_KERBFULL) {
40393ded 1613 struct nfsuid *nuidp;
984263bc
MD
1614 struct timeval ktvin, ktvout;
1615
1616 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1617 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1618 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1619 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1620 &nuidp->nu_haddr, nd->nd_nam2)))
1621 break;
1622 }
1623 if (nuidp) {
1624 ktvin.tv_sec =
1625 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1626 ktvin.tv_usec =
1627 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1628
1629 /*
1630 * Encrypt the timestamp in ecb mode using the
1631 * session key.
1632 */
1633#ifdef NFSKERB
1634 XXX
d557216f
MD
1635#else
1636 ktvout.tv_sec = 0;
1637 ktvout.tv_usec = 0;
984263bc
MD
1638#endif
1639
1640 *tl++ = rpc_auth_kerb;
1641 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1642 *tl = ktvout.tv_sec;
42edf14f 1643 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
984263bc
MD
1644 *tl++ = ktvout.tv_usec;
1645 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1646 } else {
1647 *tl++ = 0;
1648 *tl++ = 0;
1649 }
1650 } else {
1651 *tl++ = 0;
1652 *tl++ = 0;
1653 }
1654 switch (err) {
1655 case EPROGUNAVAIL:
1656 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1657 break;
1658 case EPROGMISMATCH:
1659 *tl = txdr_unsigned(RPC_PROGMISMATCH);
42edf14f 1660 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
e07fef60
MD
1661 *tl++ = txdr_unsigned(2);
1662 *tl = txdr_unsigned(3);
984263bc
MD
1663 break;
1664 case EPROCUNAVAIL:
1665 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1666 break;
1667 case EBADRPC:
1668 *tl = txdr_unsigned(RPC_GARBAGE);
1669 break;
1670 default:
1671 *tl = 0;
1672 if (err != NFSERR_RETVOID) {
42edf14f 1673 tl = nfsm_build(&info, NFSX_UNSIGNED);
984263bc
MD
1674 if (err)
1675 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1676 else
1677 *tl = 0;
1678 }
1679 break;
1680 };
1681 }
1682
984263bc 1683 if (mrq != NULL)
42edf14f
MD
1684 *mrq = info.mreq;
1685 *mbp = info.mb;
1686 *bposp = info.bpos;
984263bc
MD
1687 if (err != 0 && err != NFSERR_RETVOID)
1688 nfsstats.srvrpc_errs++;
1689 return (0);
1690}
1691
1692
1693#endif /* NFS_NOSERVER */
f8565b0f 1694
984263bc 1695/*
f8565b0f
MD
1696 * Nfs timer routine.
1697 *
984263bc
MD
1698 * Scan the nfsreq list and retranmit any requests that have timed out
1699 * To avoid retransmission attempts on STREAM sockets (in the future) make
1700 * sure to set the r_retry field to 0 (implies nm_retry == 0).
f8565b0f
MD
1701 *
1702 * Requests with attached responses, terminated requests, and
1703 * locked requests are ignored. Locked requests will be picked up
1704 * in a later timer call.
984263bc
MD
1705 */
1706void
c6b43e93 1707nfs_timer_callout(void *arg /* never used */)
984263bc 1708{
40393ded 1709 struct nfsmount *nmp;
e21aec5b 1710 struct nfsreq *req;
984263bc 1711#ifndef NFS_NOSERVER
40393ded 1712 struct nfssvc_sock *slp;
984263bc
MD
1713 u_quad_t cur_usec;
1714#endif /* NFS_NOSERVER */
984263bc 1715
c6b43e93 1716 lwkt_gettoken(&nfs_token);
e21aec5b 1717 TAILQ_FOREACH(nmp, &nfs_mountq, nm_entry) {
c6b43e93 1718 lwkt_gettoken(&nmp->nm_token);
e21aec5b
MD
1719 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
1720 KKASSERT(nmp == req->r_nmp);
f8565b0f
MD
1721 if (req->r_mrep)
1722 continue;
1723 if (req->r_flags & (R_SOFTTERM | R_LOCKED))
e21aec5b 1724 continue;
b9ea392b
MD
1725
1726 /*
1727 * Handle timeout/retry. Be sure to process r_mrep
1728 * for async requests that completed while we had
1729 * the request locked or they will hang in the reqq
1730 * forever.
1731 */
e21aec5b
MD
1732 req->r_flags |= R_LOCKED;
1733 if (nfs_sigintr(nmp, req, req->r_td)) {
f8565b0f 1734 nfs_softterm(req, 1);
b9ea392b 1735 req->r_flags &= ~R_LOCKED;
e21aec5b
MD
1736 } else {
1737 nfs_timer_req(req);
b9ea392b
MD
1738 if (req->r_flags & R_ASYNC) {
1739 if (req->r_mrep)
1740 nfs_hardterm(req, 1);
1741 req->r_flags &= ~R_LOCKED;
1742 nfssvc_iod_reader_wakeup(nmp);
1743 } else {
1744 req->r_flags &= ~R_LOCKED;
1745 }
e21aec5b 1746 }
f8565b0f
MD
1747 if (req->r_flags & R_WANTED) {
1748 req->r_flags &= ~R_WANTED;
1749 wakeup(req);
1750 }
984263bc 1751 }
c6b43e93 1752 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
1753 }
1754#ifndef NFS_NOSERVER
984263bc
MD
1755
1756 /*
1757 * Scan the write gathering queues for writes that need to be
1758 * completed now.
1759 */
1760 cur_usec = nfs_curusec();
c6b43e93 1761
ecd80f47 1762 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
95b801b2
MD
1763 /* XXX race against removal */
1764 if (lwkt_trytoken(&slp->ns_token)) {
1765 if (slp->ns_tq.lh_first &&
1766 (slp->ns_tq.lh_first->nd_time <= cur_usec)) {
1767 nfsrv_wakenfsd(slp, 1);
1768 }
1769 lwkt_reltoken(&slp->ns_token);
1770 }
984263bc
MD
1771 }
1772#endif /* NFS_NOSERVER */
c6b43e93
MD
1773
1774 callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer_callout, NULL);
1775 lwkt_reltoken(&nfs_token);
984263bc
MD
1776}
1777
e21aec5b
MD
1778static
1779void
1780nfs_timer_req(struct nfsreq *req)
1781{
1782 struct thread *td = &thread0; /* XXX for creds, will break if sleep */
1783 struct nfsmount *nmp = req->r_nmp;
1784 struct mbuf *m;
1785 struct socket *so;
1786 int timeo;
1787 int error;
1788
f8565b0f
MD
1789 /*
1790 * rtt ticks and timeout calculation. Return if the timeout
1791 * has not been reached yet, unless the packet is flagged
1792 * for an immediate send.
1793 *
1794 * The mean rtt doesn't help when we get random I/Os, we have
1795 * to multiply by fairly large numbers.
1796 */
e21aec5b 1797 if (req->r_rtt >= 0) {
cc7d050e
MD
1798 /*
1799 * Calculate the timeout to test against.
1800 */
e21aec5b 1801 req->r_rtt++;
f8565b0f
MD
1802 if (nmp->nm_flag & NFSMNT_DUMBTIMR) {
1803 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS;
1804 } else if (req->r_flags & R_TIMING) {
1805 timeo = NFS_SRTT(req) + NFS_SDRTT(req);
1806 } else {
1807 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS;
1808 }
cc7d050e 1809 timeo *= multt[req->r_procnum];
f8565b0f
MD
1810 /* timeo is still scaled by SCALE_BITS */
1811
1812#define NFSFS (NFS_RTT_SCALE * NFS_HZ)
1813 if (req->r_flags & R_TIMING) {
1814 static long last_time;
1815 if (nfs_showrtt && last_time != time_second) {
1816 kprintf("rpccmd %d NFS SRTT %d SDRTT %d "
1817 "timeo %d.%03d\n",
1818 proct[req->r_procnum],
1819 NFS_SRTT(req), NFS_SDRTT(req),
1820 timeo / NFSFS,
1821 timeo % NFSFS * 1000 / NFSFS);
1822 last_time = time_second;
1823 }
1824 }
1825#undef NFSFS
1826
1827 /*
1828 * deal with nfs_timer jitter.
1829 */
1830 timeo = (timeo >> NFS_RTT_SCALE_BITS) + 1;
1831 if (timeo < 2)
1832 timeo = 2;
1833
e21aec5b
MD
1834 if (nmp->nm_timeouts > 0)
1835 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
f8565b0f
MD
1836 if (timeo > NFS_MAXTIMEO)
1837 timeo = NFS_MAXTIMEO;
1838 if (req->r_rtt <= timeo) {
1839 if ((req->r_flags & R_NEEDSXMIT) == 0)
1840 return;
1841 } else if (nmp->nm_timeouts < 8) {
e21aec5b 1842 nmp->nm_timeouts++;
f8565b0f 1843 }
e21aec5b 1844 }
f8565b0f 1845
e21aec5b
MD
1846 /*
1847 * Check for server not responding
1848 */
1849 if ((req->r_flags & R_TPRINTFMSG) == 0 &&
1850 req->r_rexmit > nmp->nm_deadthresh) {
f8565b0f
MD
1851 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
1852 "not responding");
e21aec5b
MD
1853 req->r_flags |= R_TPRINTFMSG;
1854 }
1855 if (req->r_rexmit >= req->r_retry) { /* too many */
1856 nfsstats.rpctimeouts++;
f8565b0f 1857 nfs_softterm(req, 1);
e21aec5b
MD
1858 return;
1859 }
f8565b0f
MD
1860
1861 /*
1862 * Generally disable retransmission on reliable sockets,
1863 * unless the request is flagged for immediate send.
1864 */
e21aec5b
MD
1865 if (nmp->nm_sotype != SOCK_DGRAM) {
1866 if (++req->r_rexmit > NFS_MAXREXMIT)
1867 req->r_rexmit = NFS_MAXREXMIT;
f8565b0f
MD
1868 if ((req->r_flags & R_NEEDSXMIT) == 0)
1869 return;
e21aec5b 1870 }
f8565b0f
MD
1871
1872 /*
1873 * Stop here if we do not have a socket!
1874 */
e21aec5b
MD
1875 if ((so = nmp->nm_so) == NULL)
1876 return;
1877
1878 /*
f8565b0f
MD
1879 * If there is enough space and the window allows.. resend it.
1880 *
cc7d050e
MD
1881 * r_rtt is left intact in case we get an answer after the
1882 * retry that was a reply to the original packet.
7f3ffbb4
MD
1883 *
1884 * NOTE: so_pru_send()
e21aec5b 1885 */
e21aec5b 1886 if (ssb_space(&so->so_snd) >= req->r_mreq->m_pkthdr.len &&
f8565b0f 1887 (req->r_flags & (R_SENT | R_NEEDSXMIT)) &&
e21aec5b
MD
1888 (m = m_copym(req->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){
1889 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1890 error = so_pru_send(so, 0, m, NULL, NULL, td);
1891 else
7f3ffbb4 1892 error = so_pru_send(so, 0, m, nmp->nm_nam, NULL, td);
e21aec5b
MD
1893 if (error) {
1894 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1895 so->so_error = 0;
f8565b0f 1896 req->r_flags |= R_NEEDSXMIT;
e21aec5b
MD
1897 } else if (req->r_mrep == NULL) {
1898 /*
1899 * Iff first send, start timing
1900 * else turn timing off, backoff timer
1901 * and divide congestion window by 2.
1902 *
1903 * It is possible for the so_pru_send() to
1904 * block and for us to race a reply so we
1905 * only do this if the reply field has not
1906 * been filled in. R_LOCKED will prevent
1907 * the request from being ripped out from under
1908 * us entirely.
cc7d050e
MD
1909 *
1910 * Record the last resent procnum to aid us
1911 * in duplicate detection on receive.
e21aec5b 1912 */
cc7d050e 1913 if ((req->r_flags & R_NEEDSXMIT) == 0) {
f8565b0f
MD
1914 if (nfs_showrexmit)
1915 kprintf("X");
e21aec5b
MD
1916 if (++req->r_rexmit > NFS_MAXREXMIT)
1917 req->r_rexmit = NFS_MAXREXMIT;
f8565b0f
MD
1918 nmp->nm_maxasync_scaled >>= 1;
1919 if (nmp->nm_maxasync_scaled < NFS_MINASYNC_SCALED)
1920 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED;
e21aec5b 1921 nfsstats.rpcretries++;
cc7d050e 1922 nmp->nm_lastreprocnum = req->r_procnum;
e21aec5b
MD
1923 } else {
1924 req->r_flags |= R_SENT;
cc7d050e 1925 req->r_flags &= ~R_NEEDSXMIT;
e21aec5b 1926 }
e21aec5b
MD
1927 }
1928 }
1929}
1930
984263bc
MD
1931/*
1932 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1933 * wait for all requests to complete. This is used by forced unmounts
1934 * to terminate any outstanding RPCs.
f8565b0f
MD
1935 *
1936 * Locked requests cannot be canceled but will be marked for
1937 * soft-termination.
984263bc
MD
1938 */
1939int
e851b29e 1940nfs_nmcancelreqs(struct nfsmount *nmp)
984263bc
MD
1941{
1942 struct nfsreq *req;
165dba55 1943 int i;
984263bc 1944
165dba55 1945 crit_enter();
e21aec5b 1946 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
f8565b0f 1947 if (req->r_mrep != NULL || (req->r_flags & R_SOFTTERM))
984263bc 1948 continue;
f8565b0f 1949 nfs_softterm(req, 0);
984263bc 1950 }
edb90c22 1951 /* XXX the other two queues as well */
165dba55 1952 crit_exit();
984263bc
MD
1953
1954 for (i = 0; i < 30; i++) {
165dba55 1955 crit_enter();
e21aec5b 1956 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
984263bc
MD
1957 if (nmp == req->r_nmp)
1958 break;
1959 }
165dba55 1960 crit_exit();
984263bc
MD
1961 if (req == NULL)
1962 return (0);
377d4740 1963 tsleep(&lbolt, 0, "nfscancel", 0);
984263bc
MD
1964 }
1965 return (EBUSY);
1966}
1967
f8565b0f
MD
1968/*
1969 * Soft-terminate a request, effectively marking it as failed.
1970 *
1971 * Must be called from within a critical section.
1972 */
edb90c22 1973static void
f8565b0f 1974nfs_softterm(struct nfsreq *rep, int islocked)
edb90c22 1975{
f8565b0f
MD
1976 rep->r_flags |= R_SOFTTERM;
1977 nfs_hardterm(rep, islocked);
edb90c22
MD
1978}
1979
984263bc 1980/*
f8565b0f 1981 * Hard-terminate a request, typically after getting a response.
54938b92 1982 *
f8565b0f
MD
1983 * The state machine can still decide to re-issue it later if necessary.
1984 *
1985 * Must be called from within a critical section.
984263bc 1986 */
984263bc 1987static void
f8565b0f 1988nfs_hardterm(struct nfsreq *rep, int islocked)
984263bc 1989{
edb90c22
MD
1990 struct nfsmount *nmp = rep->r_nmp;
1991
f8565b0f
MD
1992 /*
1993 * The nm_send count is decremented now to avoid deadlocks
1994 * when the process in soreceive() hasn't yet managed to send
1995 * its own request.
1996 */
984263bc 1997 if (rep->r_flags & R_SENT) {
984263bc
MD
1998 rep->r_flags &= ~R_SENT;
1999 }
edb90c22
MD
2000
2001 /*
f8565b0f
MD
2002 * If we locked the request or nobody else has locked the request,
2003 * and the request is async, we can move it to the reader thread's
2004 * queue now and fix up the state.
2005 *
2006 * If we locked the request or nobody else has locked the request,
2007 * we can wake up anyone blocked waiting for a response on the
2008 * request.
edb90c22 2009 */
f8565b0f
MD
2010 if (islocked || (rep->r_flags & R_LOCKED) == 0) {
2011 if ((rep->r_flags & (R_ONREQQ | R_ASYNC)) ==
2012 (R_ONREQQ | R_ASYNC)) {
2013 rep->r_flags &= ~R_ONREQQ;
2014 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain);
2015 --nmp->nm_reqqlen;
2016 TAILQ_INSERT_TAIL(&nmp->nm_reqrxq, rep, r_chain);
2017 KKASSERT(rep->r_info->state == NFSM_STATE_TRY ||
2018 rep->r_info->state == NFSM_STATE_WAITREPLY);
2019 rep->r_info->state = NFSM_STATE_PROCESSREPLY;
2020 nfssvc_iod_reader_wakeup(nmp);
cc7d050e 2021 if (TAILQ_FIRST(&nmp->nm_bioq) &&
b9a7a2bd 2022 nmp->nm_reqqlen <= nfs_maxasyncbio * 2 / 3) {
cc7d050e
MD
2023 nfssvc_iod_writer_wakeup(nmp);
2024 }
f8565b0f
MD
2025 }
2026 mtx_abort_ex_link(&nmp->nm_rxlock, &rep->r_link);
2027 }
984263bc
MD
2028}
2029
2030/*
2031 * Test for a termination condition pending on the process.
2032 * This is used for NFSMNT_INT mounts.
2033 */
2034int
dadab5e9 2035nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
984263bc
MD
2036{
2037 sigset_t tmpset;
dadab5e9 2038 struct proc *p;
08f2f1bb 2039 struct lwp *lp;
984263bc
MD
2040
2041 if (rep && (rep->r_flags & R_SOFTTERM))
2042 return (EINTR);
2043 /* Terminate all requests while attempting a forced unmount. */
2044 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
2045 return (EINTR);
2046 if (!(nmp->nm_flag & NFSMNT_INT))
2047 return (0);
cd990953
MD
2048 /* td might be NULL YYY */
2049 if (td == NULL || (p = td->td_proc) == NULL)
984263bc
MD
2050 return (0);
2051
08f2f1bb 2052 lp = td->td_lwp;
aa6c3de6 2053 tmpset = lwp_sigpend(lp);
08f2f1bb 2054 SIGSETNAND(tmpset, lp->lwp_sigmask);
984263bc 2055 SIGSETNAND(tmpset, p->p_sigignore);
08f2f1bb 2056 if (SIGNOTEMPTY(tmpset) && NFSINT_SIGMASK(tmpset))
984263bc
MD
2057 return (EINTR);
2058
2059 return (0);
2060}
2061
2062/*
2063 * Lock a socket against others.
2064 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
2065 * and also to avoid race conditions between the processes with nfs requests
2066 * in progress when a reconnect is necessary.
2067 */
2068int
edb90c22 2069nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep)
984263bc 2070{
edb90c22 2071 mtx_t mtx = &nmp->nm_txlock;
dadab5e9 2072 struct thread *td;
b2eb81cd
MD
2073 int slptimeo;
2074 int slpflag;
2075 int error;
984263bc 2076
b2eb81cd
MD
2077 slpflag = 0;
2078 slptimeo = 0;
edb90c22
MD
2079 td = rep ? rep->r_td : NULL;
2080 if (nmp->nm_flag & NFSMNT_INT)
984263bc 2081 slpflag = PCATCH;
b2eb81cd 2082
8684e6f9 2083 while ((error = mtx_lock_ex_try(mtx)) != 0) {
edb90c22 2084 if (nfs_sigintr(nmp, rep, td)) {
b2eb81cd
MD
2085 error = EINTR;
2086 break;
2087 }
8684e6f9
MD
2088 error = mtx_lock_ex(mtx, "nfsndlck", slpflag, slptimeo);
2089 if (error == 0)
2090 break;
984263bc
MD
2091 if (slpflag == PCATCH) {
2092 slpflag = 0;
2093 slptimeo = 2 * hz;
2094 }
2095 }
2096 /* Always fail if our request has been cancelled. */
edb90c22 2097 if (rep && (rep->r_flags & R_SOFTTERM)) {
8684e6f9
MD
2098 if (error == 0)
2099 mtx_unlock(mtx);
b2eb81cd 2100 error = EINTR;
8684e6f9 2101 }
b2eb81cd 2102 return (error);
984263bc
MD
2103}
2104
2105/*
2106 * Unlock the stream socket for others.
2107 */
2108void
edb90c22 2109nfs_sndunlock(struct nfsmount *nmp)
984263bc 2110{
edb90c22 2111 mtx_unlock(&nmp->nm_txlock);
984263bc
MD
2112}
2113
edb90c22
MD
2114/*
2115 * Lock the receiver side of the socket.
2116 *
2117 * rep may be NULL.
2118 */
984263bc 2119static int
edb90c22 2120nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
984263bc 2121{
edb90c22 2122 mtx_t mtx = &nmp->nm_rxlock;
b2eb81cd
MD
2123 int slpflag;
2124 int slptimeo;
2125 int error;
984263bc 2126
54938b92
MD
2127 /*
2128 * Unconditionally check for completion in case another nfsiod
2129 * get the packet while the caller was blocked, before the caller
2130 * called us. Packet reception is handled by mainline code which
2131 * is protected by the BGL at the moment.
b2eb81cd
MD
2132 *
2133 * We do not strictly need the second check just before the
2134 * tsleep(), but it's good defensive programming.
54938b92 2135 */
edb90c22 2136 if (rep && rep->r_mrep != NULL)
54938b92
MD
2137 return (EALREADY);
2138
edb90c22 2139 if (nmp->nm_flag & NFSMNT_INT)
984263bc
MD
2140 slpflag = PCATCH;
2141 else
2142 slpflag = 0;
b2eb81cd 2143 slptimeo = 0;
8684e6f9
MD
2144
2145 while ((error = mtx_lock_ex_try(mtx)) != 0) {
edb90c22 2146 if (nfs_sigintr(nmp, rep, (rep ? rep->r_td : NULL))) {
b2eb81cd
MD
2147 error = EINTR;
2148 break;
2149 }
edb90c22 2150 if (rep && rep->r_mrep != NULL) {
b2eb81cd
MD
2151 error = EALREADY;
2152 break;
2153 }
8684e6f9
MD
2154
2155 /*
2156 * NOTE: can return ENOLCK, but in that case rep->r_mrep
2157 * will already be set.
2158 */
edb90c22
MD
2159 if (rep) {
2160 error = mtx_lock_ex_link(mtx, &rep->r_link,
2161 "nfsrcvlk",
2162 slpflag, slptimeo);
2163 } else {
2164 error = mtx_lock_ex(mtx, "nfsrcvlk", slpflag, slptimeo);
2165 }
8684e6f9
MD
2166 if (error == 0)
2167 break;
2168
984263bc
MD
2169 /*
2170 * If our reply was recieved while we were sleeping,
2171 * then just return without taking the lock to avoid a
2172 * situation where a single iod could 'capture' the
2173 * recieve lock.
2174 */
edb90c22 2175 if (rep && rep->r_mrep != NULL) {
b2eb81cd
MD
2176 error = EALREADY;
2177 break;
2178 }
984263bc
MD
2179 if (slpflag == PCATCH) {
2180 slpflag = 0;
2181 slptimeo = 2 * hz;
2182 }
2183 }
b2eb81cd 2184 if (error == 0) {
edb90c22 2185 if (rep && rep->r_mrep != NULL) {
8684e6f9
MD
2186 error = EALREADY;
2187 mtx_unlock(mtx);
2188 }
b2eb81cd 2189 }
b2eb81cd 2190 return (error);
984263bc
MD
2191}
2192
2193/*
2194 * Unlock the stream socket for others.
2195 */
2196static void
edb90c22 2197nfs_rcvunlock(struct nfsmount *nmp)
984263bc 2198{
edb90c22 2199 mtx_unlock(&nmp->nm_rxlock);
984263bc
MD
2200}
2201
2202/*
146c31a9 2203 * nfs_realign:
984263bc 2204 *
146c31a9
MD
2205 * Check for badly aligned mbuf data and realign by copying the unaligned
2206 * portion of the data into a new mbuf chain and freeing the portions
2207 * of the old chain that were replaced.
984263bc 2208 *
146c31a9
MD
2209 * We cannot simply realign the data within the existing mbuf chain
2210 * because the underlying buffers may contain other rpc commands and
2211 * we cannot afford to overwrite them.
984263bc 2212 *
146c31a9
MD
2213 * We would prefer to avoid this situation entirely. The situation does
2214 * not occur with NFS/UDP and is supposed to only occassionally occur
2215 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
2216 *
2217 * NOTE! MB_DONTWAIT cannot be used here. The mbufs must be acquired
2218 * because the rpc request OR reply cannot be thrown away. TCP NFS
2219 * mounts do not retry their RPCs unless the TCP connection itself
2220 * is dropped so throwing away a RPC will basically cause the NFS
2221 * operation to lockup indefinitely.
984263bc
MD
2222 */
2223static void
e851b29e 2224nfs_realign(struct mbuf **pm, int hsiz)
984263bc
MD
2225{
2226 struct mbuf *m;
2227 struct mbuf *n = NULL;
984263bc 2228
3bf6fec3
MD
2229 /*
2230 * Check for misalignemnt
2231 */
984263bc 2232 ++nfs_realign_test;
984263bc 2233 while ((m = *pm) != NULL) {
3bf6fec3 2234 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3))
984263bc 2235 break;
984263bc
MD
2236 pm = &m->m_next;
2237 }
2238
2239 /*
3bf6fec3 2240 * If misalignment found make a completely new copy.
984263bc 2241 */
3bf6fec3 2242 if (m) {
984263bc 2243 ++nfs_realign_count;
3bf6fec3 2244 n = m_dup_data(m, MB_WAIT);
984263bc
MD
2245 m_freem(*pm);
2246 *pm = n;
2247 }
2248}
2249
2250#ifndef NFS_NOSERVER
2251
2252/*
2253 * Parse an RPC request
2254 * - verify it
2255 * - fill in the cred struct.
2256 */
2257int
e851b29e 2258nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
984263bc 2259{
40393ded
RG
2260 int len, i;
2261 u_int32_t *tl;
984263bc
MD
2262 struct uio uio;
2263 struct iovec iov;
42edf14f 2264 caddr_t cp;
984263bc
MD
2265 u_int32_t nfsvers, auth_type;
2266 uid_t nickuid;
e07fef60 2267 int error = 0, ticklen;
40393ded 2268 struct nfsuid *nuidp;
984263bc 2269 struct timeval tvin, tvout;
42edf14f 2270 struct nfsm_info info;
984263bc
MD
2271#if 0 /* until encrypted keys are implemented */
2272 NFSKERBKEYSCHED_T keys; /* stores key schedule */
2273#endif
2274
42edf14f
MD
2275 info.mrep = nd->nd_mrep;
2276 info.md = nd->nd_md;
2277 info.dpos = nd->nd_dpos;
2278
984263bc 2279 if (has_header) {
42edf14f 2280 NULLOUT(tl = nfsm_dissect(&info, 10 * NFSX_UNSIGNED));
984263bc
MD
2281 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
2282 if (*tl++ != rpc_call) {
42edf14f 2283 m_freem(info.mrep);
984263bc
MD
2284 return (EBADRPC);
2285 }
42edf14f
MD
2286 } else {
2287 NULLOUT(tl = nfsm_dissect(&info, 8 * NFSX_UNSIGNED));
2288 }
984263bc
MD
2289 nd->nd_repstat = 0;
2290 nd->nd_flag = 0;
2291 if (*tl++ != rpc_vers) {
2292 nd->nd_repstat = ERPCMISMATCH;
2293 nd->nd_procnum = NFSPROC_NOOP;
2294 return (0);
2295 }
2296 if (*tl != nfs_prog) {
e07fef60
MD
2297 nd->nd_repstat = EPROGUNAVAIL;
2298 nd->nd_procnum = NFSPROC_NOOP;
2299 return (0);
984263bc
MD
2300 }
2301 tl++;
2302 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
e07fef60 2303 if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
984263bc
MD
2304 nd->nd_repstat = EPROGMISMATCH;
2305 nd->nd_procnum = NFSPROC_NOOP;
2306 return (0);
2307 }
e07fef60 2308 if (nfsvers == NFS_VER3)
984263bc
MD
2309 nd->nd_flag = ND_NFSV3;
2310 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
2311 if (nd->nd_procnum == NFSPROC_NULL)
2312 return (0);
2313 if (nd->nd_procnum >= NFS_NPROCS ||
e07fef60 2314 (nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
984263bc
MD
2315 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
2316 nd->nd_repstat = EPROCUNAVAIL;
2317 nd->nd_procnum = NFSPROC_NOOP;
2318 return (0);
2319 }
2320 if ((nd->nd_flag & ND_NFSV3) == 0)
2321 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
2322 auth_type = *tl++;
2323 len = fxdr_unsigned(int, *tl++);
2324 if (len < 0 || len > RPCAUTH_MAXSIZ) {
42edf14f 2325 m_freem(info.mrep);
984263bc
MD
2326 return (EBADRPC);
2327 }
2328
2329 nd->nd_flag &= ~ND_KERBAUTH;
2330 /*
2331 * Handle auth_unix or auth_kerb.
2332 */
2333 if (auth_type == rpc_auth_unix) {
2334 len = fxdr_unsigned(int, *++tl);
2335 if (len < 0 || len > NFS_MAXNAMLEN) {
42edf14f 2336 m_freem(info.mrep);
984263bc
MD
2337 return (EBADRPC);
2338 }
42edf14f
MD
2339 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2340 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
984263bc
MD
2341 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
2342 nd->nd_cr.cr_ref = 1;
2343 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
c83849b3 2344 nd->nd_cr.cr_ruid = nd->nd_cr.cr_svuid = nd->nd_cr.cr_uid;
984263bc 2345 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
c83849b3 2346 nd->nd_cr.cr_rgid = nd->nd_cr.cr_svgid = nd->nd_cr.cr_gid;
984263bc
MD
2347 len = fxdr_unsigned(int, *tl);
2348 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
42edf14f 2349 m_freem(info.mrep);
984263bc
MD
2350 return (EBADRPC);
2351 }
42edf14f 2352 NULLOUT(tl = nfsm_dissect(&info, (len + 2) * NFSX_UNSIGNED));
984263bc
MD
2353 for (i = 1; i <= len; i++)
2354 if (i < NGROUPS)
2355 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
2356 else
2357 tl++;
2358 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
2359 if (nd->nd_cr.cr_ngroups > 1)
2360 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
2361 len = fxdr_unsigned(int, *++tl);
2362 if (len < 0 || len > RPCAUTH_MAXSIZ) {
42edf14f 2363 m_freem(info.mrep);
984263bc
MD
2364 return (EBADRPC);
2365 }
42edf14f
MD
2366 if (len > 0) {
2367 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2368 }
984263bc
MD
2369 } else if (auth_type == rpc_auth_kerb) {
2370 switch (fxdr_unsigned(int, *tl++)) {
2371 case RPCAKN_FULLNAME:
2372 ticklen = fxdr_unsigned(int, *tl);
2373 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
2374 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2375 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2376 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
42edf14f 2377 m_freem(info.mrep);
984263bc
MD
2378 return (EBADRPC);
2379 }
2380 uio.uio_offset = 0;
2381 uio.uio_iov = &iov;
2382 uio.uio_iovcnt = 1;
2383 uio.uio_segflg = UIO_SYSSPACE;
2384 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
2385 iov.iov_len = RPCAUTH_MAXSIZ - 4;
42edf14f
MD
2386 ERROROUT(nfsm_mtouio(&info, &uio, uio.uio_resid));
2387 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
984263bc
MD
2388 if (*tl++ != rpc_auth_kerb ||
2389 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
086c1d7e 2390 kprintf("Bad kerb verifier\n");
984263bc
MD
2391 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2392 nd->nd_procnum = NFSPROC_NOOP;
2393 return (0);
2394 }
42edf14f 2395 NULLOUT(cp = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
984263bc
MD
2396 tl = (u_int32_t *)cp;
2397 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
086c1d7e 2398 kprintf("Not fullname kerb verifier\n");
984263bc
MD
2399 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2400 nd->nd_procnum = NFSPROC_NOOP;
2401 return (0);
2402 }
2403 cp += NFSX_UNSIGNED;
2404 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
2405 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2406 nd->nd_flag |= ND_KERBFULL;
2407 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2408 break;
2409 case RPCAKN_NICKNAME:
2410 if (len != 2 * NFSX_UNSIGNED) {
086c1d7e 2411 kprintf("Kerb nickname short\n");
984263bc
MD
2412 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2413 nd->nd_procnum = NFSPROC_NOOP;
2414 return (0);
2415 }
2416 nickuid = fxdr_unsigned(uid_t, *tl);
42edf14f 2417 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
984263bc
MD
2418 if (*tl++ != rpc_auth_kerb ||
2419 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
086c1d7e 2420 kprintf("Kerb nick verifier bad\n");
984263bc
MD
2421 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2422 nd->nd_procnum = NFSPROC_NOOP;
2423 return (0);
2424 }
42edf14f 2425 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
984263bc
MD
2426 tvin.tv_sec = *tl++;
2427 tvin.tv_usec = *tl;
2428
2429 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
2430 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
2431 if (nuidp->nu_cr.cr_uid == nickuid &&
2432 (!nd->nd_nam2 ||
2433 netaddr_match(NU_NETFAM(nuidp),
2434 &nuidp->nu_haddr, nd->nd_nam2)))
2435 break;
2436 }
2437 if (!nuidp) {
2438 nd->nd_repstat =
2439 (NFSERR_AUTHERR|AUTH_REJECTCRED);
2440 nd->nd_procnum = NFSPROC_NOOP;
2441 return (0);
2442 }
2443
2444 /*
2445 * Now, decrypt the timestamp using the session key
2446 * and validate it.
2447 */
2448#ifdef NFSKERB
2449 XXX
d557216f
MD
2450#else
2451 tvout.tv_sec = 0;
2452 tvout.tv_usec = 0;
984263bc
MD
2453#endif
2454
2455 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2456 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2457 if (nuidp->nu_expire < time_second ||
2458 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2459 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2460 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2461 nuidp->nu_expire = 0;
2462 nd->nd_repstat =
2463 (NFSERR_AUTHERR|AUTH_REJECTVERF);
2464 nd->nd_procnum = NFSPROC_NOOP;
2465 return (0);
2466 }
2467 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
2468 nd->nd_flag |= ND_KERBNICK;
2469 };
2470 } else {
2471 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2472 nd->nd_procnum = NFSPROC_NOOP;
2473 return (0);
2474 }
2475
42edf14f
MD
2476 nd->nd_md = info.md;
2477 nd->nd_dpos = info.dpos;
984263bc
MD
2478 return (0);
2479nfsmout:
2480 return (error);
2481}
2482
2483#endif
2484
b70ddfbf
MD
2485/*
2486 * Send a message to the originating process's terminal. The thread and/or
2487 * process may be NULL. YYY the thread should not be NULL but there may
2488 * still be some uio_td's that are still being passed as NULL through to
2489 * nfsm_request().
2490 */
984263bc 2491static int
dadab5e9 2492nfs_msg(struct thread *td, char *server, char *msg)
984263bc
MD
2493{
2494 tpr_t tpr;
2495
b70ddfbf 2496 if (td && td->td_proc)
dadab5e9 2497 tpr = tprintf_open(td->td_proc);
984263bc
MD
2498 else
2499 tpr = NULL;
2500 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2501 tprintf_close(tpr);
2502 return (0);
2503}
2504
2505#ifndef NFS_NOSERVER
c6b43e93 2506
95b801b2
MD
2507/*
2508 * Socket upcall routine for nfsd sockets. This runs in the protocol
2509 * thread and passes waitflag == MB_DONTWAIT.
2510 */
c6b43e93
MD
2511void
2512nfsrv_rcv_upcall(struct socket *so, void *arg, int waitflag)
2513{
2514 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2515
95b801b2
MD
2516 if (slp->ns_needq_upcall == 0) {
2517 slp->ns_needq_upcall = 1; /* ok to race */
2518 lwkt_gettoken(&nfs_token);
2519 nfsrv_wakenfsd(slp, 1);
2520 lwkt_reltoken(&nfs_token);
2521 }
2522#if 0
c6b43e93 2523 lwkt_gettoken(&slp->ns_token);
95b801b2 2524 slp->ns_flag |= SLP_NEEDQ;
c6b43e93
MD
2525 nfsrv_rcv(so, arg, waitflag);
2526 lwkt_reltoken(&slp->ns_token);
95b801b2 2527#endif
c6b43e93
MD
2528}
2529
984263bc 2530/*
95b801b2
MD
2531 * Process new data on a receive socket. Essentially do as much as we can
2532 * non-blocking, else punt and it will be called with MB_WAIT from an nfsd.
c6b43e93
MD
2533 *
2534 * slp->ns_token is held on call
984263bc
MD
2535 */
2536void
e851b29e 2537nfsrv_rcv(struct socket *so, void *arg, int waitflag)
984263bc 2538{
40393ded
RG
2539 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2540 struct mbuf *m;
984263bc 2541 struct sockaddr *nam;
6d49aa6f 2542 struct sockbuf sio;
984263bc 2543 int flags, error;
52553028 2544 int nparallel_wakeup = 0;
984263bc 2545
c6b43e93
MD
2546 ASSERT_LWKT_TOKEN_HELD(&slp->ns_token);
2547
984263bc
MD
2548 if ((slp->ns_flag & SLP_VALID) == 0)
2549 return;
3b101e2e 2550
984263bc 2551 /*
52553028
MD
2552 * Do not allow an infinite number of completed RPC records to build
2553 * up before we stop reading data from the socket. Otherwise we could
2554 * end up holding onto an unreasonable number of mbufs for requests
2555 * waiting for service.
2556 *
95b801b2
MD
2557 * This should give pretty good feedback to the TCP layer and
2558 * prevents a memory crunch for other protocols.
3b101e2e
MD
2559 *
2560 * Note that the same service socket can be dispatched to several
95b801b2
MD
2561 * nfs servers simultaniously. The tcp protocol callback calls us
2562 * with MB_DONTWAIT. nfsd calls us with MB_WAIT (typically).
984263bc 2563 */
95b801b2
MD
2564 if (NFSRV_RECLIMIT(slp))
2565 return;
3b101e2e
MD
2566
2567 /*
2568 * Handle protocol specifics to parse an RPC request. We always
2569 * pull from the socket using non-blocking I/O.
2570 */
984263bc
MD
2571 if (so->so_type == SOCK_STREAM) {
2572 /*
3b101e2e
MD
2573 * The data has to be read in an orderly fashion from a TCP
2574 * stream, unlike a UDP socket. It is possible for soreceive
2575 * and/or nfsrv_getstream() to block, so make sure only one
2576 * entity is messing around with the TCP stream at any given
2577 * moment. The receive sockbuf's lock in soreceive is not
2578 * sufficient.
984263bc 2579 */
95b801b2
MD
2580 if (slp->ns_flag & SLP_GETSTREAM)
2581 return;
3b101e2e 2582 slp->ns_flag |= SLP_GETSTREAM;
984263bc
MD
2583
2584 /*
d8a9a23b
MD
2585 * Do soreceive(). Pull out as much data as possible without
2586 * blocking.
984263bc 2587 */
6d49aa6f 2588 sbinit(&sio, 1000000000);
984263bc 2589 flags = MSG_DONTWAIT;
d8a9a23b 2590 error = so_pru_soreceive(so, &nam, NULL, &sio, NULL, &flags);
6d49aa6f 2591 if (error || sio.sb_mb == NULL) {
95b801b2 2592 if (error != EWOULDBLOCK)
984263bc 2593 slp->ns_flag |= SLP_DISCONN;
95b801b2
MD
2594 slp->ns_flag &= ~(SLP_GETSTREAM | SLP_NEEDQ);
2595 goto done;
984263bc 2596 }
6d49aa6f 2597 m = sio.sb_mb;
984263bc
MD
2598 if (slp->ns_rawend) {
2599 slp->ns_rawend->m_next = m;
6d49aa6f 2600 slp->ns_cc += sio.sb_cc;
984263bc
MD
2601 } else {
2602 slp->ns_raw = m;
6d49aa6f 2603 slp->ns_cc = sio.sb_cc;
984263bc
MD
2604 }
2605 while (m->m_next)
2606 m = m->m_next;
2607 slp->ns_rawend = m;
2608
2609 /*
3b101e2e 2610 * Now try and parse as many record(s) as we can out of the
95b801b2 2611 * raw stream data. This will set SLP_DOREC.
984263bc 2612 */
52553028 2613 error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup);
95b801b2
MD
2614 if (error && error != EWOULDBLOCK)
2615 slp->ns_flag |= SLP_DISCONN;
3b101e2e 2616 slp->ns_flag &= ~SLP_GETSTREAM;
984263bc 2617 } else {
3b101e2e
MD
2618 /*
2619 * For UDP soreceive typically pulls just one packet, loop
2620 * to get the whole batch.
2621 */
984263bc 2622 do {
6d49aa6f 2623 sbinit(&sio, 1000000000);
984263bc 2624 flags = MSG_DONTWAIT;
d8a9a23b
MD
2625 error = so_pru_soreceive(so, &nam, NULL, &sio,
2626 NULL, &flags);
6d49aa6f 2627 if (sio.sb_mb) {
984263bc 2628 struct nfsrv_rec *rec;
74f1caca 2629 int mf = (waitflag & MB_DONTWAIT) ?
da004c97 2630 M_NOWAIT : M_WAITOK;
77652cad 2631 rec = kmalloc(sizeof(struct nfsrv_rec),
da004c97 2632 M_NFSRVDESC, mf);
984263bc
MD
2633 if (!rec) {
2634 if (nam)
884717e1 2635 kfree(nam, M_SONAME);
6d49aa6f 2636 m_freem(sio.sb_mb);
984263bc
MD
2637 continue;
2638 }
6d49aa6f 2639 nfs_realign(&sio.sb_mb, 10 * NFSX_UNSIGNED);
984263bc 2640 rec->nr_address = nam;
6d49aa6f 2641 rec->nr_packet = sio.sb_mb;
984263bc 2642 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
52553028 2643 ++slp->ns_numrec;
95b801b2 2644 slp->ns_flag |= SLP_DOREC;
52553028 2645 ++nparallel_wakeup;
95b801b2
MD
2646 } else {
2647 slp->ns_flag &= ~SLP_NEEDQ;
984263bc
MD
2648 }
2649 if (error) {
2650 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
95b801b2 2651 && error != EWOULDBLOCK) {
984263bc 2652 slp->ns_flag |= SLP_DISCONN;
95b801b2 2653 break;
984263bc
MD
2654 }
2655 }
95b801b2
MD
2656 if (NFSRV_RECLIMIT(slp))
2657 break;
6d49aa6f 2658 } while (sio.sb_mb);
984263bc
MD
2659 }
2660
2661 /*
3b101e2e
MD
2662 * If we were upcalled from the tcp protocol layer and we have
2663 * fully parsed records ready to go, or there is new data pending,
95b801b2 2664 * or something went wrong, try to wake up a nfsd thread to deal
3b101e2e 2665 * with it.
984263bc 2666 */
95b801b2
MD
2667done:
2668 /* XXX this code is currently not executed (nfsrv_rcv_upcall) */
2669 if (waitflag == MB_DONTWAIT && (slp->ns_flag & SLP_ACTION_MASK)) {
8fdd18a4 2670 lwkt_gettoken(&nfs_token);
52553028 2671 nfsrv_wakenfsd(slp, nparallel_wakeup);
8fdd18a4 2672 lwkt_reltoken(&nfs_token);
3b101e2e 2673 }
984263bc
MD
2674}
2675
2676/*
2677 * Try and extract an RPC request from the mbuf data list received on a
2678 * stream socket. The "waitflag" argument indicates whether or not it
2679 * can sleep.
2680 */
2681static int
52553028 2682nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp)
984263bc 2683{
40393ded
RG
2684 struct mbuf *m, **mpp;
2685 char *cp1, *cp2;
2686 int len;
984263bc
MD
2687 struct mbuf *om, *m2, *recm;
2688 u_int32_t recmark;
2689
984263bc
MD
2690 for (;;) {
2691 if (slp->ns_reclen == 0) {
3b101e2e 2692 if (slp->ns_cc < NFSX_UNSIGNED)
984263bc 2693 return (0);
984263bc
MD
2694 m = slp->ns_raw;
2695 if (m->m_len >= NFSX_UNSIGNED) {
2696 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2697 m->m_data += NFSX_UNSIGNED;
2698 m->m_len -= NFSX_UNSIGNED;
2699 } else {
2700 cp1 = (caddr_t)&recmark;
2701 cp2 = mtod(m, caddr_t);
2702 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2703 while (m->m_len == 0) {
2704 m = m->m_next;
2705 cp2 = mtod(m, caddr_t);
2706 }
2707 *cp1++ = *cp2++;
2708 m->m_data++;
2709 m->m_len--;
2710 }
2711 }
2712 slp->ns_cc -= NFSX_UNSIGNED;
2713 recmark = ntohl(recmark);
2714 slp->ns_reclen = recmark & ~0x80000000;
2715 if (recmark & 0x80000000)
2716 slp->ns_flag |= SLP_LASTFRAG;
2717 else
2718 slp->ns_flag &= ~SLP_LASTFRAG;
43884890 2719 if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) {
63cd2150
MD
2720 log(LOG_ERR, "%s (%d) from nfs client\n",
2721 "impossible packet length",
2722 slp->ns_reclen);
984263bc
MD
2723 return (EPERM);
2724 }
2725 }
2726
2727 /*
2728 * Now get the record part.
2729 *
2730 * Note that slp->ns_reclen may be 0. Linux sometimes
2731 * generates 0-length RPCs
2732 */
2733 recm = NULL;
2734 if (slp->ns_cc == slp->ns_reclen) {
2735 recm = slp->ns_raw;
60233e58 2736 slp->ns_raw = slp->ns_rawend = NULL;
984263bc
MD
2737 slp->ns_cc = slp->ns_reclen = 0;
2738 } else if (slp->ns_cc > slp->ns_reclen) {
2739 len = 0;
2740 m = slp->ns_raw;
60233e58 2741 om = NULL;
984263bc
MD
2742
2743 while (len < slp->ns_reclen) {
2744 if ((len + m->m_len) > slp->ns_reclen) {
2745 m2 = m_copym(m, 0, slp->ns_reclen - len,
2746 waitflag);
2747 if (m2) {
2748 if (om) {
2749 om->m_next = m2;
2750 recm = slp->ns_raw;
2751 } else
2752 recm = m2;
2753 m->m_data += slp->ns_reclen - len;
2754 m->m_len -= slp->ns_reclen - len;
2755 len = slp->ns_reclen;
2756 } else {
984263bc
MD
2757 return (EWOULDBLOCK);
2758 }
2759 } else if ((len + m->m_len) == slp->ns_reclen) {
2760 om = m;
2761 len += m->m_len;
2762 m = m->m_next;
2763 recm = slp->ns_raw;
60233e58 2764 om->m_next = NULL;
984263bc
MD
2765 } else {
2766 om = m;
2767 len += m->m_len;
2768 m = m->m_next;
2769 }
2770 }
2771 slp->ns_raw = m;
2772 slp->ns_cc -= len;
2773 slp->ns_reclen = 0;
2774 } else {
984263bc
MD
2775 return (0);
2776 }
2777
2778 /*
2779 * Accumulate the fragments into a record.
2780 */
2781 mpp = &slp->ns_frag;
2782 while (*mpp)
2783 mpp = &((*mpp)->m_next);
2784 *mpp = recm;
2785 if (slp->ns_flag & SLP_LASTFRAG) {
2786 struct nfsrv_rec *rec;
74f1caca 2787 int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK;
efda3bd0 2788 rec = kmalloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf);
984263bc
MD
2789 if (!rec) {
2790 m_freem(slp->ns_frag);
2791 } else {
2792 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
60233e58 2793 rec->nr_address = NULL;
984263bc
MD
2794 rec->nr_packet = slp->ns_frag;
2795 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
52553028 2796 ++slp->ns_numrec;
95b801b2 2797 slp->ns_flag |= SLP_DOREC;
52553028 2798 ++*countp;
984263bc 2799 }
60233e58 2800 slp->ns_frag = NULL;
984263bc
MD
2801 }
2802 }
2803}
2804
7f3ffbb4
MD
2805#ifdef INVARIANTS
2806
2807/*
2808 * Sanity check our mbuf chain.
2809 */
2810static void
2811nfs_checkpkt(struct mbuf *m, int len)
2812{
2813 int xlen = 0;
2814 while (m) {
2815 xlen += m->m_len;
2816 m = m->m_next;
2817 }
2818 if (xlen != len) {
2819 panic("nfs_checkpkt: len mismatch %d/%d mbuf %p\n",
2820 xlen, len, m);
2821 }
2822}
2823
2824#else
2825
2826static void
2827nfs_checkpkt(struct mbuf *m __unused, int len __unused)
2828{
2829}
2830
2831#endif
2832
984263bc
MD
2833/*
2834 * Parse an RPC header.
95b801b2
MD
2835 *
2836 * If the socket is invalid or no records are pending we return ENOBUFS.
2837 * The caller must deal with NEEDQ races.
984263bc
MD
2838 */
2839int
e851b29e
CP
2840nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
2841 struct nfsrv_descript **ndp)
984263bc
MD
2842{
2843 struct nfsrv_rec *rec;
40393ded 2844 struct mbuf *m;
984263bc 2845 struct sockaddr *nam;
40393ded 2846 struct nfsrv_descript *nd;
984263bc
MD
2847 int error;
2848
2849 *ndp = NULL;
2850 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec))
2851 return (ENOBUFS);
2852 rec = STAILQ_FIRST(&slp->ns_rec);
2853 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
52553028 2854 KKASSERT(slp->ns_numrec > 0);
95b801b2
MD
2855 if (--slp->ns_numrec == 0)
2856 slp->ns_flag &= ~SLP_DOREC;
984263bc
MD
2857 nam = rec->nr_address;
2858 m = rec->nr_packet;
efda3bd0 2859 kfree(rec, M_NFSRVDESC);
884717e1 2860 nd = kmalloc(sizeof(struct nfsrv_descript), M_NFSRVDESC, M_WAITOK);
984263bc
MD
2861 nd->nd_md = nd->nd_mrep = m;
2862 nd->nd_nam2 = nam;
2863 nd->nd_dpos = mtod(m, caddr_t);
2864 error = nfs_getreq(nd, nfsd, TRUE);
2865 if (error) {
2866 if (nam) {
884717e1 2867 kfree(nam, M_SONAME);
984263bc 2868 }
efda3bd0 2869 kfree((caddr_t)nd, M_NFSRVDESC);
984263bc
MD
2870 return (error);
2871 }
2872 *ndp = nd;
2873 nfsd->nfsd_nd = nd;
2874 return (0);
2875}
2876
2877/*
52553028
MD
2878 * Try to assign service sockets to nfsd threads based on the number
2879 * of new rpc requests that have been queued on the service socket.
2880 *
2881 * If no nfsd's are available or additonal requests are pending, set the
2882 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for
2883 * the work in the nfssvc_sock list when it is finished processing its
2884 * current work. This flag is only cleared when an nfsd can not find
2885 * any new work to perform.
984263bc
MD
2886 */
2887void
52553028 2888nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel)
984263bc 2889{
40393ded 2890 struct nfsd *nd;
984263bc
MD
2891
2892 if ((slp->ns_flag & SLP_VALID) == 0)
2893 return;
52553028
MD
2894 if (nparallel <= 1)
2895 nparallel = 1;
ecd80f47 2896 TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) {
984263bc
MD
2897 if (nd->nfsd_flag & NFSD_WAITING) {
2898 nd->nfsd_flag &= ~NFSD_WAITING;
2899 if (nd->nfsd_slp)
2900 panic("nfsd wakeup");
95b801b2 2901 nfsrv_slpref(slp);
984263bc
MD
2902 nd->nfsd_slp = slp;
2903 wakeup((caddr_t)nd);
52553028
MD
2904 if (--nparallel == 0)
2905 break;
984263bc
MD
2906 }
2907 }
95b801b2
MD
2908
2909 /*
2910 * If we couldn't assign slp then the NFSDs are all busy and
2911 * we set a flag indicating that there is pending work.
2912 */
2913 if (nparallel)
52553028 2914 nfsd_head_flag |= NFSD_CHECKSLP;
984263bc
MD
2915}
2916#endif /* NFS_NOSERVER */