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