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