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