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