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