nfs_socket.c revision 1.150 1 1.150 yamt /* $NetBSD: nfs_socket.c,v 1.150 2007/04/29 10:30:18 yamt 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.96 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd *
34 1.24 fvdl * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd /*
38 1.1 cgd * Socket operations for use by nfs
39 1.1 cgd */
40 1.69 lukem
41 1.69 lukem #include <sys/cdefs.h>
42 1.150 yamt __KERNEL_RCSID(0, "$NetBSD: nfs_socket.c,v 1.150 2007/04/29 10:30:18 yamt Exp $");
43 1.42 thorpej
44 1.42 thorpej #include "fs_nfs.h"
45 1.59 bjh21 #include "opt_nfs.h"
46 1.44 thorpej #include "opt_nfsserver.h"
47 1.89 martin #include "opt_mbuftrace.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.130 elad #include <sys/kauth.h>
68 1.1 cgd
69 1.9 mycroft #include <netinet/in.h>
70 1.9 mycroft #include <netinet/tcp.h>
71 1.24 fvdl
72 1.9 mycroft #include <nfs/rpcv2.h>
73 1.24 fvdl #include <nfs/nfsproto.h>
74 1.9 mycroft #include <nfs/nfs.h>
75 1.9 mycroft #include <nfs/xdr_subs.h>
76 1.9 mycroft #include <nfs/nfsm_subs.h>
77 1.9 mycroft #include <nfs/nfsmount.h>
78 1.14 mycroft #include <nfs/nfsnode.h>
79 1.14 mycroft #include <nfs/nfsrtt.h>
80 1.23 christos #include <nfs/nfs_var.h>
81 1.78 thorpej
82 1.78 thorpej MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
83 1.79 matt #ifdef MBUFTRACE
84 1.139 dogcow struct mowner nfs_mowner = MOWNER_INIT("nfs","");
85 1.79 matt #endif
86 1.1 cgd
87 1.1 cgd /*
88 1.14 mycroft * Estimate rto for an nfs rpc sent via. an unreliable datagram.
89 1.14 mycroft * Use the mean and mean deviation of rtt for the appropriate type of rpc
90 1.14 mycroft * for the frequent rpcs and a default for the others.
91 1.14 mycroft * The justification for doing "other" this way is that these rpcs
92 1.14 mycroft * happen so infrequently that timer est. would probably be stale.
93 1.14 mycroft * Also, since many of these rpcs are
94 1.14 mycroft * non-idempotent, a conservative timeout is desired.
95 1.14 mycroft * getattr, lookup - A+2D
96 1.14 mycroft * read, write - A+4D
97 1.14 mycroft * other - nm_timeo
98 1.14 mycroft */
99 1.14 mycroft #define NFS_RTO(n, t) \
100 1.14 mycroft ((t) == 0 ? (n)->nm_timeo : \
101 1.14 mycroft ((t) < 3 ? \
102 1.14 mycroft (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
103 1.14 mycroft ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
104 1.14 mycroft #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
105 1.14 mycroft #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
106 1.14 mycroft /*
107 1.1 cgd * External data, mostly RPC constants in XDR form
108 1.1 cgd */
109 1.22 cgd extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
110 1.24 fvdl rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
111 1.14 mycroft rpc_auth_kerb;
112 1.144 yamt extern u_int32_t nfs_prog;
113 1.77 matt extern const 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.118 yamt [NFSPROC_NULL] = 0,
126 1.118 yamt [NFSPROC_GETATTR] = 1,
127 1.118 yamt [NFSPROC_SETATTR] = 0,
128 1.118 yamt [NFSPROC_LOOKUP] = 2,
129 1.118 yamt [NFSPROC_ACCESS] = 1,
130 1.118 yamt [NFSPROC_READLINK] = 3,
131 1.118 yamt [NFSPROC_READ] = 3,
132 1.118 yamt [NFSPROC_WRITE] = 4,
133 1.118 yamt [NFSPROC_CREATE] = 0,
134 1.118 yamt [NFSPROC_MKDIR] = 0,
135 1.118 yamt [NFSPROC_SYMLINK] = 0,
136 1.118 yamt [NFSPROC_MKNOD] = 0,
137 1.118 yamt [NFSPROC_REMOVE] = 0,
138 1.118 yamt [NFSPROC_RMDIR] = 0,
139 1.118 yamt [NFSPROC_RENAME] = 0,
140 1.118 yamt [NFSPROC_LINK] = 0,
141 1.118 yamt [NFSPROC_READDIR] = 3,
142 1.118 yamt [NFSPROC_READDIRPLUS] = 3,
143 1.118 yamt [NFSPROC_FSSTAT] = 0,
144 1.118 yamt [NFSPROC_FSINFO] = 0,
145 1.118 yamt [NFSPROC_PATHCONF] = 0,
146 1.118 yamt [NFSPROC_COMMIT] = 0,
147 1.118 yamt [NFSPROC_NOOP] = 0,
148 1.1 cgd };
149 1.14 mycroft
150 1.14 mycroft /*
151 1.14 mycroft * There is a congestion window for outstanding rpcs maintained per mount
152 1.14 mycroft * point. The cwnd size is adjusted in roughly the way that:
153 1.14 mycroft * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
154 1.14 mycroft * SIGCOMM '88". ACM, August 1988.
155 1.14 mycroft * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
156 1.14 mycroft * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
157 1.14 mycroft * of rpcs is in progress.
158 1.14 mycroft * (The sent count and cwnd are scaled for integer arith.)
159 1.14 mycroft * Variants of "slow start" were tried and were found to be too much of a
160 1.14 mycroft * performance hit (ave. rtt 3 times larger),
161 1.14 mycroft * I suspect due to the large rtt that nfs rpcs have.
162 1.14 mycroft */
163 1.14 mycroft #define NFS_CWNDSCALE 256
164 1.14 mycroft #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
165 1.66 jdolecek static const int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
166 1.14 mycroft int nfsrtton = 0;
167 1.14 mycroft struct nfsrtt nfsrtt;
168 1.74 matt struct nfsreqhead nfs_reqq;
169 1.1 cgd
170 1.99 yamt struct callout nfs_timer_ch = CALLOUT_INITIALIZER_SETFUNC(nfs_timer, NULL);
171 1.54 thorpej
172 1.150 yamt static int nfs_sndlock(struct nfsmount *, struct nfsreq *);
173 1.150 yamt static void nfs_sndunlock(struct nfsmount *);
174 1.150 yamt static int nfs_rcvlock(struct nfsmount *, struct nfsreq *);
175 1.150 yamt static void nfs_rcvunlock(struct nfsmount *);
176 1.150 yamt
177 1.1 cgd /*
178 1.1 cgd * Initialize sockets and congestion for a new NFS connection.
179 1.1 cgd * We do not free the sockaddr if error.
180 1.1 cgd */
181 1.23 christos int
182 1.119 christos nfs_connect(nmp, rep, l)
183 1.55 augustss struct nfsmount *nmp;
184 1.14 mycroft struct nfsreq *rep;
185 1.119 christos struct lwp *l;
186 1.1 cgd {
187 1.55 augustss struct socket *so;
188 1.14 mycroft int s, error, rcvreserve, sndreserve;
189 1.11 cgd struct sockaddr *saddr;
190 1.14 mycroft struct sockaddr_in *sin;
191 1.57 fvdl #ifdef INET6
192 1.57 fvdl struct sockaddr_in6 *sin6;
193 1.57 fvdl #endif
194 1.1 cgd struct mbuf *m;
195 1.1 cgd
196 1.1 cgd nmp->nm_so = (struct socket *)0;
197 1.11 cgd saddr = mtod(nmp->nm_nam, struct sockaddr *);
198 1.105 jonathan error = socreate(saddr->sa_family, &nmp->nm_so,
199 1.119 christos nmp->nm_sotype, nmp->nm_soproto, l);
200 1.23 christos if (error)
201 1.1 cgd goto bad;
202 1.1 cgd so = nmp->nm_so;
203 1.79 matt #ifdef MBUFTRACE
204 1.79 matt so->so_mowner = &nfs_mowner;
205 1.79 matt so->so_rcv.sb_mowner = &nfs_mowner;
206 1.79 matt so->so_snd.sb_mowner = &nfs_mowner;
207 1.79 matt #endif
208 1.1 cgd nmp->nm_soflags = so->so_proto->pr_flags;
209 1.1 cgd
210 1.2 cgd /*
211 1.2 cgd * Some servers require that the client port be a reserved port number.
212 1.2 cgd */
213 1.14 mycroft if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
214 1.79 matt m = m_get(M_WAIT, MT_SOOPTS);
215 1.79 matt MCLAIM(m, so->so_mowner);
216 1.72 lukem *mtod(m, int32_t *) = IP_PORTRANGE_LOW;
217 1.72 lukem m->m_len = sizeof(int32_t);
218 1.72 lukem if ((error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, m)))
219 1.72 lukem goto bad;
220 1.79 matt m = m_get(M_WAIT, MT_SONAME);
221 1.79 matt MCLAIM(m, so->so_mowner);
222 1.2 cgd sin = mtod(m, struct sockaddr_in *);
223 1.2 cgd sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
224 1.2 cgd sin->sin_family = AF_INET;
225 1.2 cgd sin->sin_addr.s_addr = INADDR_ANY;
226 1.72 lukem sin->sin_port = 0;
227 1.119 christos error = sobind(so, m, &lwp0);
228 1.2 cgd m_freem(m);
229 1.14 mycroft if (error)
230 1.14 mycroft goto bad;
231 1.2 cgd }
232 1.57 fvdl #ifdef INET6
233 1.57 fvdl if (saddr->sa_family == AF_INET6 && (nmp->nm_flag & NFSMNT_RESVPORT)) {
234 1.79 matt m = m_get(M_WAIT, MT_SOOPTS);
235 1.79 matt MCLAIM(m, so->so_mowner);
236 1.72 lukem *mtod(m, int32_t *) = IPV6_PORTRANGE_LOW;
237 1.72 lukem m->m_len = sizeof(int32_t);
238 1.72 lukem if ((error = sosetopt(so, IPPROTO_IPV6, IPV6_PORTRANGE, m)))
239 1.72 lukem goto bad;
240 1.79 matt m = m_get(M_WAIT, MT_SONAME);
241 1.79 matt MCLAIM(m, so->so_mowner);
242 1.57 fvdl sin6 = mtod(m, struct sockaddr_in6 *);
243 1.57 fvdl sin6->sin6_len = m->m_len = sizeof (struct sockaddr_in6);
244 1.57 fvdl sin6->sin6_family = AF_INET6;
245 1.57 fvdl sin6->sin6_addr = in6addr_any;
246 1.72 lukem sin6->sin6_port = 0;
247 1.119 christos error = sobind(so, m, &lwp0);
248 1.57 fvdl m_freem(m);
249 1.57 fvdl if (error)
250 1.57 fvdl goto bad;
251 1.57 fvdl }
252 1.57 fvdl #endif
253 1.2 cgd
254 1.1 cgd /*
255 1.1 cgd * Protocols that do not require connections may be optionally left
256 1.1 cgd * unconnected for servers that reply from a port other than NFS_PORT.
257 1.1 cgd */
258 1.1 cgd if (nmp->nm_flag & NFSMNT_NOCONN) {
259 1.1 cgd if (nmp->nm_soflags & PR_CONNREQUIRED) {
260 1.1 cgd error = ENOTCONN;
261 1.1 cgd goto bad;
262 1.1 cgd }
263 1.1 cgd } else {
264 1.119 christos error = soconnect(so, nmp->nm_nam, l);
265 1.24 fvdl if (error)
266 1.1 cgd goto bad;
267 1.1 cgd
268 1.1 cgd /*
269 1.1 cgd * Wait for the connection to complete. Cribbed from the
270 1.14 mycroft * connect system call but with the wait timing out so
271 1.14 mycroft * that interruptible mounts don't hang here for a long time.
272 1.1 cgd */
273 1.21 mycroft s = splsoftnet();
274 1.14 mycroft while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
275 1.148 christos (void) tsleep((void *)&so->so_timeo, PSOCK,
276 1.48 fvdl "nfscn1", 2 * hz);
277 1.14 mycroft if ((so->so_state & SS_ISCONNECTING) &&
278 1.14 mycroft so->so_error == 0 && rep &&
279 1.119 christos (error = nfs_sigintr(nmp, rep, rep->r_lwp)) != 0){
280 1.14 mycroft so->so_state &= ~SS_ISCONNECTING;
281 1.14 mycroft splx(s);
282 1.14 mycroft goto bad;
283 1.14 mycroft }
284 1.14 mycroft }
285 1.1 cgd if (so->so_error) {
286 1.1 cgd error = so->so_error;
287 1.14 mycroft so->so_error = 0;
288 1.14 mycroft splx(s);
289 1.1 cgd goto bad;
290 1.1 cgd }
291 1.14 mycroft splx(s);
292 1.14 mycroft }
293 1.14 mycroft if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
294 1.14 mycroft so->so_rcv.sb_timeo = (5 * hz);
295 1.14 mycroft so->so_snd.sb_timeo = (5 * hz);
296 1.14 mycroft } else {
297 1.106 yamt /*
298 1.106 yamt * enable receive timeout to detect server crash and reconnect.
299 1.106 yamt * otherwise, we can be stuck in soreceive forever.
300 1.106 yamt */
301 1.106 yamt so->so_rcv.sb_timeo = (5 * hz);
302 1.14 mycroft so->so_snd.sb_timeo = 0;
303 1.1 cgd }
304 1.1 cgd if (nmp->nm_sotype == SOCK_DGRAM) {
305 1.37 fvdl sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
306 1.37 fvdl rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
307 1.37 fvdl NFS_MAXPKTHDR) * 2;
308 1.14 mycroft } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
309 1.26 fvdl sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
310 1.26 fvdl rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
311 1.25 fvdl NFS_MAXPKTHDR) * 2;
312 1.1 cgd } else {
313 1.14 mycroft if (nmp->nm_sotype != SOCK_STREAM)
314 1.14 mycroft panic("nfscon sotype");
315 1.1 cgd if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
316 1.79 matt m = m_get(M_WAIT, MT_SOOPTS);
317 1.79 matt MCLAIM(m, so->so_mowner);
318 1.22 cgd *mtod(m, int32_t *) = 1;
319 1.22 cgd m->m_len = sizeof(int32_t);
320 1.1 cgd sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
321 1.1 cgd }
322 1.14 mycroft if (so->so_proto->pr_protocol == IPPROTO_TCP) {
323 1.79 matt m = m_get(M_WAIT, MT_SOOPTS);
324 1.79 matt MCLAIM(m, so->so_mowner);
325 1.22 cgd *mtod(m, int32_t *) = 1;
326 1.22 cgd m->m_len = sizeof(int32_t);
327 1.1 cgd sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
328 1.1 cgd }
329 1.22 cgd sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
330 1.22 cgd sizeof (u_int32_t)) * 2;
331 1.22 cgd rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
332 1.22 cgd sizeof (u_int32_t)) * 2;
333 1.1 cgd }
334 1.24 fvdl error = soreserve(so, sndreserve, rcvreserve);
335 1.24 fvdl if (error)
336 1.14 mycroft goto bad;
337 1.1 cgd so->so_rcv.sb_flags |= SB_NOINTR;
338 1.1 cgd so->so_snd.sb_flags |= SB_NOINTR;
339 1.1 cgd
340 1.1 cgd /* Initialize other non-zero congestion variables */
341 1.14 mycroft nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] =
342 1.68 simonb NFS_TIMEO << 3;
343 1.14 mycroft nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
344 1.68 simonb nmp->nm_sdrtt[3] = 0;
345 1.14 mycroft nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
346 1.1 cgd nmp->nm_sent = 0;
347 1.14 mycroft nmp->nm_timeouts = 0;
348 1.1 cgd return (0);
349 1.1 cgd
350 1.1 cgd bad:
351 1.1 cgd nfs_disconnect(nmp);
352 1.1 cgd return (error);
353 1.1 cgd }
354 1.1 cgd
355 1.1 cgd /*
356 1.1 cgd * Reconnect routine:
357 1.1 cgd * Called when a connection is broken on a reliable protocol.
358 1.1 cgd * - clean up the old socket
359 1.1 cgd * - nfs_connect() again
360 1.1 cgd * - set R_MUSTRESEND for all outstanding requests on mount point
361 1.1 cgd * If this fails the mount point is DEAD!
362 1.14 mycroft * nb: Must be called with the nfs_sndlock() set on the mount point.
363 1.1 cgd */
364 1.23 christos int
365 1.119 christos nfs_reconnect(rep, l)
366 1.55 augustss struct nfsreq *rep;
367 1.119 christos struct lwp *l;
368 1.1 cgd {
369 1.55 augustss struct nfsreq *rp;
370 1.55 augustss struct nfsmount *nmp = rep->r_nmp;
371 1.1 cgd int error;
372 1.1 cgd
373 1.14 mycroft nfs_disconnect(nmp);
374 1.119 christos while ((error = nfs_connect(nmp, rep, l)) != 0) {
375 1.14 mycroft if (error == EINTR || error == ERESTART)
376 1.1 cgd return (EINTR);
377 1.148 christos (void) tsleep((void *)&lbolt, PSOCK, "nfscn2", 0);
378 1.1 cgd }
379 1.1 cgd
380 1.1 cgd /*
381 1.1 cgd * Loop through outstanding request list and fix up all requests
382 1.1 cgd * on old socket.
383 1.1 cgd */
384 1.73 christos TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
385 1.106 yamt if (rp->r_nmp == nmp) {
386 1.106 yamt if ((rp->r_flags & R_MUSTRESEND) == 0)
387 1.106 yamt rp->r_flags |= R_MUSTRESEND | R_REXMITTED;
388 1.106 yamt rp->r_rexmit = 0;
389 1.106 yamt }
390 1.1 cgd }
391 1.1 cgd return (0);
392 1.1 cgd }
393 1.1 cgd
394 1.1 cgd /*
395 1.1 cgd * NFS disconnect. Clean up and unlink.
396 1.1 cgd */
397 1.1 cgd void
398 1.1 cgd nfs_disconnect(nmp)
399 1.55 augustss struct nfsmount *nmp;
400 1.1 cgd {
401 1.55 augustss struct socket *so;
402 1.53 sommerfe int drain = 0;
403 1.112 perry
404 1.1 cgd if (nmp->nm_so) {
405 1.1 cgd so = nmp->nm_so;
406 1.1 cgd nmp->nm_so = (struct socket *)0;
407 1.143 yamt soshutdown(so, SHUT_RDWR);
408 1.53 sommerfe drain = (nmp->nm_iflag & NFSMNT_DISMNT) != 0;
409 1.53 sommerfe if (drain) {
410 1.51 sommerfe /*
411 1.51 sommerfe * soshutdown() above should wake up the current
412 1.51 sommerfe * listener.
413 1.71 minoura * Now wake up those waiting for the receive lock, and
414 1.51 sommerfe * wait for them to go away unhappy, to prevent *nmp
415 1.51 sommerfe * from evaporating while they're sleeping.
416 1.51 sommerfe */
417 1.51 sommerfe while (nmp->nm_waiters > 0) {
418 1.150 yamt mutex_enter(&nmp->nm_lock);
419 1.150 yamt cv_broadcast(&nmp->nm_rcvcv);
420 1.150 yamt cv_broadcast(&nmp->nm_sndcv);
421 1.150 yamt mutex_exit(&nmp->nm_lock);
422 1.56 thorpej (void) tsleep(&nmp->nm_waiters, PVFS,
423 1.56 thorpej "nfsdis", 0);
424 1.51 sommerfe }
425 1.51 sommerfe }
426 1.1 cgd soclose(so);
427 1.41 fvdl }
428 1.51 sommerfe #ifdef DIAGNOSTIC
429 1.53 sommerfe if (drain && (nmp->nm_waiters > 0))
430 1.76 provos panic("nfs_disconnect: waiters left after drain?");
431 1.51 sommerfe #endif
432 1.41 fvdl }
433 1.40 fvdl
434 1.41 fvdl void
435 1.41 fvdl nfs_safedisconnect(nmp)
436 1.41 fvdl struct nfsmount *nmp;
437 1.41 fvdl {
438 1.41 fvdl struct nfsreq dummyreq;
439 1.41 fvdl
440 1.46 perry memset(&dummyreq, 0, sizeof(dummyreq));
441 1.41 fvdl dummyreq.r_nmp = nmp;
442 1.150 yamt nfs_rcvlock(nmp, &dummyreq); /* XXX ignored error return */
443 1.41 fvdl nfs_disconnect(nmp);
444 1.87 yamt nfs_rcvunlock(nmp);
445 1.1 cgd }
446 1.1 cgd
447 1.1 cgd /*
448 1.1 cgd * This is the nfs send routine. For connection based socket types, it
449 1.14 mycroft * must be called with an nfs_sndlock() on the socket.
450 1.1 cgd * "rep == NULL" indicates that it has been called from a server.
451 1.14 mycroft * For the client side:
452 1.14 mycroft * - return EINTR if the RPC is terminated, 0 otherwise
453 1.14 mycroft * - set R_MUSTRESEND if the send fails for any reason
454 1.58 mrg * - do any cleanup required by recoverable socket errors (? ? ?)
455 1.14 mycroft * For the server side:
456 1.14 mycroft * - return EINTR or ERESTART if interrupted by a signal
457 1.14 mycroft * - return EPIPE if a connection is lost for connection based sockets (TCP...)
458 1.58 mrg * - do any cleanup required by recoverable socket errors (? ? ?)
459 1.1 cgd */
460 1.23 christos int
461 1.119 christos nfs_send(so, nam, top, rep, l)
462 1.55 augustss struct socket *so;
463 1.1 cgd struct mbuf *nam;
464 1.55 augustss struct mbuf *top;
465 1.1 cgd struct nfsreq *rep;
466 1.119 christos struct lwp *l;
467 1.1 cgd {
468 1.1 cgd struct mbuf *sendnam;
469 1.14 mycroft int error, soflags, flags;
470 1.1 cgd
471 1.119 christos /* XXX nfs_doio()/nfs_request() calls with rep->r_lwp == NULL */
472 1.119 christos if (l == NULL && rep->r_lwp == NULL)
473 1.119 christos l = curlwp;
474 1.105 jonathan
475 1.1 cgd if (rep) {
476 1.1 cgd if (rep->r_flags & R_SOFTTERM) {
477 1.1 cgd m_freem(top);
478 1.1 cgd return (EINTR);
479 1.1 cgd }
480 1.14 mycroft if ((so = rep->r_nmp->nm_so) == NULL) {
481 1.14 mycroft rep->r_flags |= R_MUSTRESEND;
482 1.14 mycroft m_freem(top);
483 1.14 mycroft return (0);
484 1.14 mycroft }
485 1.1 cgd rep->r_flags &= ~R_MUSTRESEND;
486 1.1 cgd soflags = rep->r_nmp->nm_soflags;
487 1.1 cgd } else
488 1.1 cgd soflags = so->so_proto->pr_flags;
489 1.1 cgd if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
490 1.1 cgd sendnam = (struct mbuf *)0;
491 1.1 cgd else
492 1.1 cgd sendnam = nam;
493 1.14 mycroft if (so->so_type == SOCK_SEQPACKET)
494 1.14 mycroft flags = MSG_EOR;
495 1.14 mycroft else
496 1.14 mycroft flags = 0;
497 1.1 cgd
498 1.43 matt error = (*so->so_send)(so, sendnam, (struct uio *)0, top,
499 1.119 christos (struct mbuf *)0, flags, l);
500 1.14 mycroft if (error) {
501 1.14 mycroft if (rep) {
502 1.60 fvdl if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
503 1.60 fvdl /*
504 1.60 fvdl * We're too fast for the network/driver,
505 1.60 fvdl * and UDP isn't flowcontrolled.
506 1.60 fvdl * We need to resend. This is not fatal,
507 1.60 fvdl * just try again.
508 1.60 fvdl *
509 1.60 fvdl * Could be smarter here by doing some sort
510 1.60 fvdl * of a backoff, but this is rare.
511 1.60 fvdl */
512 1.14 mycroft rep->r_flags |= R_MUSTRESEND;
513 1.60 fvdl } else {
514 1.101 matt if (error != EPIPE)
515 1.101 matt log(LOG_INFO,
516 1.101 matt "nfs send error %d for %s\n",
517 1.101 matt error,
518 1.101 matt rep->r_nmp->nm_mountp->
519 1.101 matt mnt_stat.f_mntfromname);
520 1.60 fvdl /*
521 1.60 fvdl * Deal with errors for the client side.
522 1.60 fvdl */
523 1.60 fvdl if (rep->r_flags & R_SOFTTERM)
524 1.60 fvdl error = EINTR;
525 1.60 fvdl else
526 1.60 fvdl rep->r_flags |= R_MUSTRESEND;
527 1.60 fvdl }
528 1.67 fvdl } else {
529 1.67 fvdl /*
530 1.67 fvdl * See above. This error can happen under normal
531 1.67 fvdl * circumstances and the log is too noisy.
532 1.67 fvdl * The error will still show up in nfsstat.
533 1.67 fvdl */
534 1.67 fvdl if (error != ENOBUFS || so->so_type != SOCK_DGRAM)
535 1.67 fvdl log(LOG_INFO, "nfsd send error %d\n", error);
536 1.67 fvdl }
537 1.14 mycroft
538 1.14 mycroft /*
539 1.58 mrg * Handle any recoverable (soft) socket errors here. (? ? ?)
540 1.14 mycroft */
541 1.14 mycroft if (error != EINTR && error != ERESTART &&
542 1.14 mycroft error != EWOULDBLOCK && error != EPIPE)
543 1.1 cgd error = 0;
544 1.1 cgd }
545 1.1 cgd return (error);
546 1.1 cgd }
547 1.1 cgd
548 1.32 thorpej #ifdef NFS
549 1.1 cgd /*
550 1.1 cgd * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
551 1.1 cgd * done by soreceive(), but for SOCK_STREAM we must deal with the Record
552 1.1 cgd * Mark and consolidate the data into a new mbuf list.
553 1.1 cgd * nb: Sometimes TCP passes the data up to soreceive() in long lists of
554 1.1 cgd * small mbufs.
555 1.1 cgd * For SOCK_STREAM we must be very careful to read an entire record once
556 1.1 cgd * we have read any of it, even if the system call has been interrupted.
557 1.1 cgd */
558 1.23 christos int
559 1.119 christos nfs_receive(rep, aname, mp, l)
560 1.55 augustss struct nfsreq *rep;
561 1.1 cgd struct mbuf **aname;
562 1.1 cgd struct mbuf **mp;
563 1.119 christos struct lwp *l;
564 1.119 christos {
565 1.55 augustss struct socket *so;
566 1.1 cgd struct uio auio;
567 1.1 cgd struct iovec aio;
568 1.55 augustss struct mbuf *m;
569 1.14 mycroft struct mbuf *control;
570 1.22 cgd u_int32_t len;
571 1.1 cgd struct mbuf **getnam;
572 1.14 mycroft int error, sotype, rcvflg;
573 1.1 cgd
574 1.1 cgd /*
575 1.1 cgd * Set up arguments for soreceive()
576 1.1 cgd */
577 1.1 cgd *mp = (struct mbuf *)0;
578 1.1 cgd *aname = (struct mbuf *)0;
579 1.14 mycroft sotype = rep->r_nmp->nm_sotype;
580 1.1 cgd
581 1.1 cgd /*
582 1.1 cgd * For reliable protocols, lock against other senders/receivers
583 1.1 cgd * in case a reconnect is necessary.
584 1.1 cgd * For SOCK_STREAM, first get the Record Mark to find out how much
585 1.1 cgd * more there is to get.
586 1.1 cgd * We must lock the socket against other receivers
587 1.1 cgd * until we have an entire rpc request/reply.
588 1.1 cgd */
589 1.14 mycroft if (sotype != SOCK_DGRAM) {
590 1.150 yamt error = nfs_sndlock(rep->r_nmp, rep);
591 1.24 fvdl if (error)
592 1.14 mycroft return (error);
593 1.1 cgd tryagain:
594 1.1 cgd /*
595 1.1 cgd * Check for fatal errors and resending request.
596 1.1 cgd */
597 1.14 mycroft /*
598 1.14 mycroft * Ugh: If a reconnect attempt just happened, nm_so
599 1.14 mycroft * would have changed. NULL indicates a failed
600 1.14 mycroft * attempt that has essentially shut down this
601 1.14 mycroft * mount point.
602 1.14 mycroft */
603 1.14 mycroft if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
604 1.150 yamt nfs_sndunlock(rep->r_nmp);
605 1.14 mycroft return (EINTR);
606 1.14 mycroft }
607 1.24 fvdl so = rep->r_nmp->nm_so;
608 1.24 fvdl if (!so) {
609 1.119 christos error = nfs_reconnect(rep, l);
610 1.24 fvdl if (error) {
611 1.150 yamt nfs_sndunlock(rep->r_nmp);
612 1.14 mycroft return (error);
613 1.14 mycroft }
614 1.14 mycroft goto tryagain;
615 1.14 mycroft }
616 1.14 mycroft while (rep->r_flags & R_MUSTRESEND) {
617 1.14 mycroft m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
618 1.14 mycroft nfsstats.rpcretries++;
619 1.106 yamt rep->r_rtt = 0;
620 1.106 yamt rep->r_flags &= ~R_TIMING;
621 1.119 christos error = nfs_send(so, rep->r_nmp->nm_nam, m, rep, l);
622 1.23 christos if (error) {
623 1.14 mycroft if (error == EINTR || error == ERESTART ||
624 1.119 christos (error = nfs_reconnect(rep, l)) != 0) {
625 1.150 yamt nfs_sndunlock(rep->r_nmp);
626 1.14 mycroft return (error);
627 1.14 mycroft }
628 1.14 mycroft goto tryagain;
629 1.1 cgd }
630 1.1 cgd }
631 1.150 yamt nfs_sndunlock(rep->r_nmp);
632 1.14 mycroft if (sotype == SOCK_STREAM) {
633 1.148 christos aio.iov_base = (void *) &len;
634 1.22 cgd aio.iov_len = sizeof(u_int32_t);
635 1.1 cgd auio.uio_iov = &aio;
636 1.1 cgd auio.uio_iovcnt = 1;
637 1.1 cgd auio.uio_rw = UIO_READ;
638 1.1 cgd auio.uio_offset = 0;
639 1.22 cgd auio.uio_resid = sizeof(u_int32_t);
640 1.123 yamt UIO_SETUP_SYSSPACE(&auio);
641 1.1 cgd do {
642 1.14 mycroft rcvflg = MSG_WAITALL;
643 1.43 matt error = (*so->so_receive)(so, (struct mbuf **)0, &auio,
644 1.1 cgd (struct mbuf **)0, (struct mbuf **)0, &rcvflg);
645 1.14 mycroft if (error == EWOULDBLOCK && rep) {
646 1.1 cgd if (rep->r_flags & R_SOFTTERM)
647 1.1 cgd return (EINTR);
648 1.106 yamt /*
649 1.106 yamt * if it seems that the server died after it
650 1.106 yamt * received our request, set EPIPE so that
651 1.106 yamt * we'll reconnect and retransmit requests.
652 1.106 yamt */
653 1.106 yamt if (rep->r_rexmit >= rep->r_nmp->nm_retry) {
654 1.106 yamt nfsstats.rpctimeouts++;
655 1.106 yamt error = EPIPE;
656 1.106 yamt }
657 1.14 mycroft }
658 1.1 cgd } while (error == EWOULDBLOCK);
659 1.1 cgd if (!error && auio.uio_resid > 0) {
660 1.41 fvdl /*
661 1.41 fvdl * Don't log a 0 byte receive; it means
662 1.41 fvdl * that the socket has been closed, and
663 1.41 fvdl * can happen during normal operation
664 1.41 fvdl * (forcible unmount or Solaris server).
665 1.41 fvdl */
666 1.41 fvdl if (auio.uio_resid != sizeof (u_int32_t))
667 1.41 fvdl log(LOG_INFO,
668 1.49 thorpej "short receive (%lu/%lu) from nfs server %s\n",
669 1.49 thorpej (u_long)sizeof(u_int32_t) - auio.uio_resid,
670 1.49 thorpej (u_long)sizeof(u_int32_t),
671 1.1 cgd rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
672 1.1 cgd error = EPIPE;
673 1.1 cgd }
674 1.1 cgd if (error)
675 1.1 cgd goto errout;
676 1.1 cgd len = ntohl(len) & ~0x80000000;
677 1.1 cgd /*
678 1.1 cgd * This is SERIOUS! We are out of sync with the sender
679 1.1 cgd * and forcing a disconnect/reconnect is all I can do.
680 1.1 cgd */
681 1.1 cgd if (len > NFS_MAXPACKET) {
682 1.14 mycroft log(LOG_ERR, "%s (%d) from nfs server %s\n",
683 1.14 mycroft "impossible packet length",
684 1.14 mycroft len,
685 1.14 mycroft rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
686 1.1 cgd error = EFBIG;
687 1.1 cgd goto errout;
688 1.1 cgd }
689 1.1 cgd auio.uio_resid = len;
690 1.1 cgd do {
691 1.1 cgd rcvflg = MSG_WAITALL;
692 1.43 matt error = (*so->so_receive)(so, (struct mbuf **)0,
693 1.1 cgd &auio, mp, (struct mbuf **)0, &rcvflg);
694 1.1 cgd } while (error == EWOULDBLOCK || error == EINTR ||
695 1.1 cgd error == ERESTART);
696 1.1 cgd if (!error && auio.uio_resid > 0) {
697 1.41 fvdl if (len != auio.uio_resid)
698 1.41 fvdl log(LOG_INFO,
699 1.49 thorpej "short receive (%lu/%d) from nfs server %s\n",
700 1.49 thorpej (u_long)len - auio.uio_resid, len,
701 1.14 mycroft rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
702 1.1 cgd error = EPIPE;
703 1.1 cgd }
704 1.1 cgd } else {
705 1.14 mycroft /*
706 1.14 mycroft * NB: Since uio_resid is big, MSG_WAITALL is ignored
707 1.14 mycroft * and soreceive() will return when it has either a
708 1.14 mycroft * control msg or a data msg.
709 1.14 mycroft * We have no use for control msg., but must grab them
710 1.14 mycroft * and then throw them away so we know what is going
711 1.14 mycroft * on.
712 1.14 mycroft */
713 1.14 mycroft auio.uio_resid = len = 100000000; /* Anything Big */
714 1.123 yamt /* not need to setup uio_vmspace */
715 1.1 cgd do {
716 1.1 cgd rcvflg = 0;
717 1.43 matt error = (*so->so_receive)(so, (struct mbuf **)0,
718 1.14 mycroft &auio, mp, &control, &rcvflg);
719 1.14 mycroft if (control)
720 1.14 mycroft m_freem(control);
721 1.1 cgd if (error == EWOULDBLOCK && rep) {
722 1.1 cgd if (rep->r_flags & R_SOFTTERM)
723 1.1 cgd return (EINTR);
724 1.1 cgd }
725 1.14 mycroft } while (error == EWOULDBLOCK ||
726 1.14 mycroft (!error && *mp == NULL && control));
727 1.14 mycroft if ((rcvflg & MSG_EOR) == 0)
728 1.31 christos printf("Egad!!\n");
729 1.1 cgd if (!error && *mp == NULL)
730 1.1 cgd error = EPIPE;
731 1.1 cgd len -= auio.uio_resid;
732 1.1 cgd }
733 1.1 cgd errout:
734 1.14 mycroft if (error && error != EINTR && error != ERESTART) {
735 1.1 cgd m_freem(*mp);
736 1.1 cgd *mp = (struct mbuf *)0;
737 1.14 mycroft if (error != EPIPE)
738 1.1 cgd log(LOG_INFO,
739 1.1 cgd "receive error %d from nfs server %s\n",
740 1.1 cgd error,
741 1.1 cgd rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
742 1.150 yamt error = nfs_sndlock(rep->r_nmp, rep);
743 1.14 mycroft if (!error)
744 1.119 christos error = nfs_reconnect(rep, l);
745 1.1 cgd if (!error)
746 1.1 cgd goto tryagain;
747 1.37 fvdl else
748 1.150 yamt nfs_sndunlock(rep->r_nmp);
749 1.1 cgd }
750 1.1 cgd } else {
751 1.14 mycroft if ((so = rep->r_nmp->nm_so) == NULL)
752 1.14 mycroft return (EACCES);
753 1.1 cgd if (so->so_state & SS_ISCONNECTED)
754 1.1 cgd getnam = (struct mbuf **)0;
755 1.1 cgd else
756 1.1 cgd getnam = aname;
757 1.1 cgd auio.uio_resid = len = 1000000;
758 1.123 yamt /* not need to setup uio_vmspace */
759 1.1 cgd do {
760 1.1 cgd rcvflg = 0;
761 1.43 matt error = (*so->so_receive)(so, getnam, &auio, mp,
762 1.1 cgd (struct mbuf **)0, &rcvflg);
763 1.14 mycroft if (error == EWOULDBLOCK &&
764 1.1 cgd (rep->r_flags & R_SOFTTERM))
765 1.1 cgd return (EINTR);
766 1.1 cgd } while (error == EWOULDBLOCK);
767 1.1 cgd len -= auio.uio_resid;
768 1.51 sommerfe if (!error && *mp == NULL)
769 1.51 sommerfe error = EPIPE;
770 1.1 cgd }
771 1.1 cgd if (error) {
772 1.1 cgd m_freem(*mp);
773 1.1 cgd *mp = (struct mbuf *)0;
774 1.1 cgd }
775 1.1 cgd return (error);
776 1.1 cgd }
777 1.1 cgd
778 1.1 cgd /*
779 1.1 cgd * Implement receipt of reply on a socket.
780 1.1 cgd * We must search through the list of received datagrams matching them
781 1.1 cgd * with outstanding requests using the xid, until ours is found.
782 1.1 cgd */
783 1.1 cgd /* ARGSUSED */
784 1.23 christos int
785 1.119 christos nfs_reply(myrep, lwp)
786 1.1 cgd struct nfsreq *myrep;
787 1.119 christos struct lwp *lwp;
788 1.1 cgd {
789 1.55 augustss struct nfsreq *rep;
790 1.55 augustss struct nfsmount *nmp = myrep->r_nmp;
791 1.55 augustss int32_t t1;
792 1.14 mycroft struct mbuf *mrep, *nam, *md;
793 1.22 cgd u_int32_t rxid, *tl;
794 1.148 christos char *dpos, *cp2;
795 1.14 mycroft int error;
796 1.1 cgd
797 1.1 cgd /*
798 1.1 cgd * Loop around until we get our own reply
799 1.1 cgd */
800 1.1 cgd for (;;) {
801 1.1 cgd /*
802 1.1 cgd * Lock against other receivers so that I don't get stuck in
803 1.1 cgd * sbwait() after someone else has received my reply for me.
804 1.1 cgd * Also necessary for connection based protocols to avoid
805 1.1 cgd * race conditions during a reconnect.
806 1.1 cgd */
807 1.150 yamt error = nfs_rcvlock(nmp, myrep);
808 1.36 fvdl if (error == EALREADY)
809 1.36 fvdl return (0);
810 1.24 fvdl if (error)
811 1.14 mycroft return (error);
812 1.1 cgd /*
813 1.1 cgd * Get the next Rpc reply off the socket
814 1.1 cgd */
815 1.51 sommerfe nmp->nm_waiters++;
816 1.119 christos error = nfs_receive(myrep, &nam, &mrep, lwp);
817 1.87 yamt nfs_rcvunlock(nmp);
818 1.14 mycroft if (error) {
819 1.1 cgd
820 1.51 sommerfe if (nmp->nm_iflag & NFSMNT_DISMNT) {
821 1.51 sommerfe /*
822 1.51 sommerfe * Oops, we're going away now..
823 1.51 sommerfe */
824 1.51 sommerfe nmp->nm_waiters--;
825 1.51 sommerfe wakeup (&nmp->nm_waiters);
826 1.51 sommerfe return error;
827 1.51 sommerfe }
828 1.51 sommerfe nmp->nm_waiters--;
829 1.1 cgd /*
830 1.58 mrg * Ignore routing errors on connectionless protocols? ?
831 1.1 cgd */
832 1.1 cgd if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
833 1.1 cgd nmp->nm_so->so_error = 0;
834 1.37 fvdl #ifdef DEBUG
835 1.33 fvdl printf("nfs_reply: ignoring error %d\n", error);
836 1.37 fvdl #endif
837 1.14 mycroft if (myrep->r_flags & R_GETONEREP)
838 1.14 mycroft return (0);
839 1.1 cgd continue;
840 1.1 cgd }
841 1.1 cgd return (error);
842 1.1 cgd }
843 1.112 perry nmp->nm_waiters--;
844 1.14 mycroft if (nam)
845 1.14 mycroft m_freem(nam);
846 1.112 perry
847 1.1 cgd /*
848 1.1 cgd * Get the xid and check that it is an rpc reply
849 1.1 cgd */
850 1.14 mycroft md = mrep;
851 1.148 christos dpos = mtod(md, void *);
852 1.22 cgd nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
853 1.14 mycroft rxid = *tl++;
854 1.14 mycroft if (*tl != rpc_reply) {
855 1.144 yamt nfsstats.rpcinvalid++;
856 1.144 yamt m_freem(mrep);
857 1.14 mycroft nfsmout:
858 1.14 mycroft if (myrep->r_flags & R_GETONEREP)
859 1.14 mycroft return (0);
860 1.1 cgd continue;
861 1.1 cgd }
862 1.14 mycroft
863 1.1 cgd /*
864 1.1 cgd * Loop through the request list to match up the reply
865 1.1 cgd * Iff no match, just drop the datagram
866 1.1 cgd */
867 1.73 christos TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
868 1.1 cgd if (rep->r_mrep == NULL && rxid == rep->r_xid) {
869 1.1 cgd /* Found it.. */
870 1.14 mycroft rep->r_mrep = mrep;
871 1.14 mycroft rep->r_md = md;
872 1.14 mycroft rep->r_dpos = dpos;
873 1.14 mycroft if (nfsrtton) {
874 1.14 mycroft struct rttl *rt;
875 1.14 mycroft
876 1.14 mycroft rt = &nfsrtt.rttl[nfsrtt.pos];
877 1.14 mycroft rt->proc = rep->r_procnum;
878 1.14 mycroft rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
879 1.14 mycroft rt->sent = nmp->nm_sent;
880 1.14 mycroft rt->cwnd = nmp->nm_cwnd;
881 1.14 mycroft rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
882 1.14 mycroft rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
883 1.103 christos rt->fsid = nmp->nm_mountp->mnt_stat.f_fsidx;
884 1.135 kardel getmicrotime(&rt->tstamp);
885 1.14 mycroft if (rep->r_flags & R_TIMING)
886 1.14 mycroft rt->rtt = rep->r_rtt;
887 1.14 mycroft else
888 1.14 mycroft rt->rtt = 1000000;
889 1.14 mycroft nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
890 1.14 mycroft }
891 1.1 cgd /*
892 1.14 mycroft * Update congestion window.
893 1.14 mycroft * Do the additive increase of
894 1.14 mycroft * one rpc/rtt.
895 1.14 mycroft */
896 1.14 mycroft if (nmp->nm_cwnd <= nmp->nm_sent) {
897 1.14 mycroft nmp->nm_cwnd +=
898 1.14 mycroft (NFS_CWNDSCALE * NFS_CWNDSCALE +
899 1.14 mycroft (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
900 1.14 mycroft if (nmp->nm_cwnd > NFS_MAXCWND)
901 1.14 mycroft nmp->nm_cwnd = NFS_MAXCWND;
902 1.14 mycroft }
903 1.14 mycroft rep->r_flags &= ~R_SENT;
904 1.14 mycroft nmp->nm_sent -= NFS_CWNDSCALE;
905 1.14 mycroft /*
906 1.14 mycroft * Update rtt using a gain of 0.125 on the mean
907 1.14 mycroft * and a gain of 0.25 on the deviation.
908 1.1 cgd */
909 1.1 cgd if (rep->r_flags & R_TIMING) {
910 1.14 mycroft /*
911 1.14 mycroft * Since the timer resolution of
912 1.14 mycroft * NFS_HZ is so course, it can often
913 1.14 mycroft * result in r_rtt == 0. Since
914 1.14 mycroft * r_rtt == N means that the actual
915 1.14 mycroft * rtt is between N+dt and N+2-dt ticks,
916 1.14 mycroft * add 1.
917 1.14 mycroft */
918 1.14 mycroft t1 = rep->r_rtt + 1;
919 1.14 mycroft t1 -= (NFS_SRTT(rep) >> 3);
920 1.14 mycroft NFS_SRTT(rep) += t1;
921 1.14 mycroft if (t1 < 0)
922 1.14 mycroft t1 = -t1;
923 1.14 mycroft t1 -= (NFS_SDRTT(rep) >> 2);
924 1.14 mycroft NFS_SDRTT(rep) += t1;
925 1.1 cgd }
926 1.14 mycroft nmp->nm_timeouts = 0;
927 1.1 cgd break;
928 1.1 cgd }
929 1.1 cgd }
930 1.1 cgd /*
931 1.1 cgd * If not matched to a request, drop it.
932 1.1 cgd * If it's mine, get out.
933 1.1 cgd */
934 1.16 mycroft if (rep == 0) {
935 1.1 cgd nfsstats.rpcunexpected++;
936 1.14 mycroft m_freem(mrep);
937 1.14 mycroft } else if (rep == myrep) {
938 1.14 mycroft if (rep->r_mrep == NULL)
939 1.14 mycroft panic("nfsreply nil");
940 1.14 mycroft return (0);
941 1.14 mycroft }
942 1.14 mycroft if (myrep->r_flags & R_GETONEREP)
943 1.1 cgd return (0);
944 1.1 cgd }
945 1.1 cgd }
946 1.1 cgd
947 1.1 cgd /*
948 1.1 cgd * nfs_request - goes something like this
949 1.1 cgd * - fill in request struct
950 1.1 cgd * - links it into list
951 1.1 cgd * - calls nfs_send() for first transmit
952 1.1 cgd * - calls nfs_receive() to get reply
953 1.1 cgd * - break down rpc header and return with nfs reply pointed to
954 1.1 cgd * by mrep or error
955 1.1 cgd * nb: always frees up mreq mbuf list
956 1.1 cgd */
957 1.23 christos int
958 1.119 christos nfs_request(np, mrest, procnum, lwp, cred, mrp, mdp, dposp, rexmitp)
959 1.83 drochner struct nfsnode *np;
960 1.14 mycroft struct mbuf *mrest;
961 1.1 cgd int procnum;
962 1.119 christos struct lwp *lwp;
963 1.130 elad kauth_cred_t cred;
964 1.1 cgd struct mbuf **mrp;
965 1.1 cgd struct mbuf **mdp;
966 1.148 christos char **dposp;
967 1.104 yamt int *rexmitp;
968 1.1 cgd {
969 1.55 augustss struct mbuf *m, *mrep;
970 1.55 augustss struct nfsreq *rep;
971 1.55 augustss u_int32_t *tl;
972 1.55 augustss int i;
973 1.110 yamt struct nfsmount *nmp = VFSTONFS(np->n_vnode->v_mount);
974 1.14 mycroft struct mbuf *md, *mheadend;
975 1.24 fvdl char nickv[RPCX_NICKVERF];
976 1.144 yamt time_t waituntil;
977 1.148 christos char *dpos, *cp2;
978 1.62 fvdl int t1, s, error = 0, mrest_len, auth_len, auth_type;
979 1.97 yamt int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0;
980 1.24 fvdl int verf_len, verf_type;
981 1.22 cgd u_int32_t xid;
982 1.24 fvdl char *auth_str, *verf_str;
983 1.24 fvdl NFSKERBKEY_T key; /* save session key */
984 1.130 elad kauth_cred_t acred;
985 1.110 yamt struct mbuf *mrest_backup = NULL;
986 1.130 elad kauth_cred_t origcred = NULL; /* XXX: gcc */
987 1.147 thorpej bool retry_cred = true;
988 1.146 thorpej bool use_opencred = (np->n_flag & NUSEOPENCRED) != 0;
989 1.1 cgd
990 1.104 yamt if (rexmitp != NULL)
991 1.104 yamt *rexmitp = 0;
992 1.104 yamt
993 1.130 elad acred = kauth_cred_alloc();
994 1.130 elad
995 1.110 yamt tryagain_cred:
996 1.64 bjh21 KASSERT(cred != NULL);
997 1.1 cgd MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
998 1.1 cgd rep->r_nmp = nmp;
999 1.119 christos rep->r_lwp = lwp;
1000 1.14 mycroft rep->r_procnum = procnum;
1001 1.14 mycroft i = 0;
1002 1.14 mycroft m = mrest;
1003 1.1 cgd while (m) {
1004 1.14 mycroft i += m->m_len;
1005 1.1 cgd m = m->m_next;
1006 1.1 cgd }
1007 1.14 mycroft mrest_len = i;
1008 1.14 mycroft
1009 1.14 mycroft /*
1010 1.14 mycroft * Get the RPC header with authorization.
1011 1.14 mycroft */
1012 1.14 mycroft kerbauth:
1013 1.24 fvdl verf_str = auth_str = (char *)0;
1014 1.14 mycroft if (nmp->nm_flag & NFSMNT_KERB) {
1015 1.24 fvdl verf_str = nickv;
1016 1.24 fvdl verf_len = sizeof (nickv);
1017 1.24 fvdl auth_type = RPCAUTH_KERB4;
1018 1.148 christos memset((void *)key, 0, sizeof (key));
1019 1.24 fvdl if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
1020 1.24 fvdl &auth_len, verf_str, verf_len)) {
1021 1.24 fvdl error = nfs_getauth(nmp, rep, cred, &auth_str,
1022 1.24 fvdl &auth_len, verf_str, &verf_len, key);
1023 1.14 mycroft if (error) {
1024 1.148 christos free((void *)rep, M_NFSREQ);
1025 1.14 mycroft m_freem(mrest);
1026 1.133 yamt KASSERT(kauth_cred_getrefcnt(acred) == 1);
1027 1.133 yamt kauth_cred_free(acred);
1028 1.14 mycroft return (error);
1029 1.14 mycroft }
1030 1.1 cgd }
1031 1.147 thorpej retry_cred = false;
1032 1.14 mycroft } else {
1033 1.110 yamt /* AUTH_UNIX */
1034 1.110 yamt uid_t uid;
1035 1.110 yamt gid_t gid;
1036 1.110 yamt
1037 1.110 yamt /*
1038 1.110 yamt * on the most unix filesystems, permission checks are
1039 1.110 yamt * done when the file is open(2)'ed.
1040 1.110 yamt * ie. once a file is successfully open'ed,
1041 1.110 yamt * following i/o operations never fail with EACCES.
1042 1.110 yamt * we try to follow the semantics as far as possible.
1043 1.110 yamt *
1044 1.110 yamt * note that we expect that the nfs server always grant
1045 1.110 yamt * accesses by the file's owner.
1046 1.110 yamt */
1047 1.110 yamt origcred = cred;
1048 1.100 fvdl switch (procnum) {
1049 1.100 fvdl case NFSPROC_READ:
1050 1.100 fvdl case NFSPROC_WRITE:
1051 1.100 fvdl case NFSPROC_COMMIT:
1052 1.110 yamt uid = np->n_vattr->va_uid;
1053 1.110 yamt gid = np->n_vattr->va_gid;
1054 1.130 elad if (kauth_cred_geteuid(cred) == uid &&
1055 1.130 elad kauth_cred_getegid(cred) == gid) {
1056 1.147 thorpej retry_cred = false;
1057 1.110 yamt break;
1058 1.110 yamt }
1059 1.110 yamt if (use_opencred)
1060 1.110 yamt break;
1061 1.130 elad kauth_cred_setuid(acred, uid);
1062 1.130 elad kauth_cred_seteuid(acred, uid);
1063 1.130 elad kauth_cred_setsvuid(acred, uid);
1064 1.130 elad kauth_cred_setgid(acred, gid);
1065 1.130 elad kauth_cred_setegid(acred, gid);
1066 1.130 elad kauth_cred_setsvgid(acred, gid);
1067 1.130 elad cred = acred;
1068 1.100 fvdl break;
1069 1.110 yamt default:
1070 1.147 thorpej retry_cred = false;
1071 1.110 yamt break;
1072 1.100 fvdl }
1073 1.110 yamt /*
1074 1.110 yamt * backup mbuf chain if we can need it later to retry.
1075 1.110 yamt *
1076 1.110 yamt * XXX maybe we can keep a direct reference to
1077 1.110 yamt * mrest without doing m_copym, but it's ...ugly.
1078 1.110 yamt */
1079 1.110 yamt if (retry_cred)
1080 1.110 yamt mrest_backup = m_copym(mrest, 0, M_COPYALL, M_WAIT);
1081 1.14 mycroft auth_type = RPCAUTH_UNIX;
1082 1.130 elad /* XXX elad - ngroups */
1083 1.130 elad auth_len = (((kauth_cred_ngroups(cred) > nmp->nm_numgrps) ?
1084 1.130 elad nmp->nm_numgrps : kauth_cred_ngroups(cred)) << 2) +
1085 1.14 mycroft 5 * NFSX_UNSIGNED;
1086 1.14 mycroft }
1087 1.24 fvdl m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1088 1.24 fvdl auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
1089 1.14 mycroft if (auth_str)
1090 1.14 mycroft free(auth_str, M_TEMP);
1091 1.14 mycroft
1092 1.1 cgd /*
1093 1.14 mycroft * For stream protocols, insert a Sun RPC Record Mark.
1094 1.1 cgd */
1095 1.14 mycroft if (nmp->nm_sotype == SOCK_STREAM) {
1096 1.14 mycroft M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
1097 1.22 cgd *mtod(m, u_int32_t *) = htonl(0x80000000 |
1098 1.14 mycroft (m->m_pkthdr.len - NFSX_UNSIGNED));
1099 1.1 cgd }
1100 1.14 mycroft rep->r_mreq = m;
1101 1.14 mycroft rep->r_xid = xid;
1102 1.14 mycroft tryagain:
1103 1.14 mycroft if (nmp->nm_flag & NFSMNT_SOFT)
1104 1.14 mycroft rep->r_retry = nmp->nm_retry;
1105 1.14 mycroft else
1106 1.14 mycroft rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1107 1.14 mycroft rep->r_rtt = rep->r_rexmit = 0;
1108 1.14 mycroft if (proct[procnum] > 0)
1109 1.14 mycroft rep->r_flags = R_TIMING;
1110 1.14 mycroft else
1111 1.14 mycroft rep->r_flags = 0;
1112 1.14 mycroft rep->r_mrep = NULL;
1113 1.1 cgd
1114 1.1 cgd /*
1115 1.1 cgd * Do the client side RPC.
1116 1.1 cgd */
1117 1.1 cgd nfsstats.rpcrequests++;
1118 1.1 cgd /*
1119 1.1 cgd * Chain request into list of outstanding requests. Be sure
1120 1.1 cgd * to put it LAST so timer finds oldest requests first.
1121 1.1 cgd */
1122 1.35 fvdl s = splsoftnet();
1123 1.16 mycroft TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1124 1.14 mycroft
1125 1.1 cgd /*
1126 1.1 cgd * If backing off another request or avoiding congestion, don't
1127 1.1 cgd * send this one now but let timer do it. If not timing a request,
1128 1.1 cgd * do it now.
1129 1.1 cgd */
1130 1.14 mycroft if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1131 1.14 mycroft (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1132 1.14 mycroft nmp->nm_sent < nmp->nm_cwnd)) {
1133 1.1 cgd splx(s);
1134 1.1 cgd if (nmp->nm_soflags & PR_CONNREQUIRED)
1135 1.150 yamt error = nfs_sndlock(nmp, rep);
1136 1.14 mycroft if (!error) {
1137 1.52 fvdl m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
1138 1.119 christos error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep, lwp);
1139 1.14 mycroft if (nmp->nm_soflags & PR_CONNREQUIRED)
1140 1.150 yamt nfs_sndunlock(nmp);
1141 1.14 mycroft }
1142 1.14 mycroft if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
1143 1.14 mycroft nmp->nm_sent += NFS_CWNDSCALE;
1144 1.14 mycroft rep->r_flags |= R_SENT;
1145 1.14 mycroft }
1146 1.14 mycroft } else {
1147 1.1 cgd splx(s);
1148 1.14 mycroft rep->r_rtt = -1;
1149 1.14 mycroft }
1150 1.1 cgd
1151 1.1 cgd /*
1152 1.1 cgd * Wait for the reply from our send or the timer's.
1153 1.1 cgd */
1154 1.14 mycroft if (!error || error == EPIPE)
1155 1.119 christos error = nfs_reply(rep, lwp);
1156 1.1 cgd
1157 1.1 cgd /*
1158 1.1 cgd * RPC done, unlink the request.
1159 1.1 cgd */
1160 1.35 fvdl s = splsoftnet();
1161 1.16 mycroft TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1162 1.1 cgd splx(s);
1163 1.1 cgd
1164 1.1 cgd /*
1165 1.14 mycroft * Decrement the outstanding request count.
1166 1.14 mycroft */
1167 1.14 mycroft if (rep->r_flags & R_SENT) {
1168 1.14 mycroft rep->r_flags &= ~R_SENT; /* paranoia */
1169 1.14 mycroft nmp->nm_sent -= NFS_CWNDSCALE;
1170 1.14 mycroft }
1171 1.14 mycroft
1172 1.104 yamt if (rexmitp != NULL) {
1173 1.104 yamt int rexmit;
1174 1.104 yamt
1175 1.104 yamt if (nmp->nm_sotype != SOCK_DGRAM)
1176 1.104 yamt rexmit = (rep->r_flags & R_REXMITTED) != 0;
1177 1.104 yamt else
1178 1.104 yamt rexmit = rep->r_rexmit;
1179 1.104 yamt *rexmitp = rexmit;
1180 1.104 yamt }
1181 1.104 yamt
1182 1.14 mycroft /*
1183 1.1 cgd * If there was a successful reply and a tprintf msg.
1184 1.1 cgd * tprintf a response.
1185 1.1 cgd */
1186 1.1 cgd if (!error && (rep->r_flags & R_TPRINTFMSG))
1187 1.119 christos nfs_msg(rep->r_lwp, nmp->nm_mountp->mnt_stat.f_mntfromname,
1188 1.1 cgd "is alive again");
1189 1.1 cgd mrep = rep->r_mrep;
1190 1.14 mycroft md = rep->r_md;
1191 1.14 mycroft dpos = rep->r_dpos;
1192 1.110 yamt if (error)
1193 1.110 yamt goto nfsmout;
1194 1.1 cgd
1195 1.1 cgd /*
1196 1.1 cgd * break down the rpc header and check if ok
1197 1.1 cgd */
1198 1.24 fvdl nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1199 1.1 cgd if (*tl++ == rpc_msgdenied) {
1200 1.1 cgd if (*tl == rpc_mismatch)
1201 1.1 cgd error = EOPNOTSUPP;
1202 1.14 mycroft else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1203 1.24 fvdl if (!failed_auth) {
1204 1.14 mycroft failed_auth++;
1205 1.14 mycroft mheadend->m_next = (struct mbuf *)0;
1206 1.14 mycroft m_freem(mrep);
1207 1.14 mycroft m_freem(rep->r_mreq);
1208 1.14 mycroft goto kerbauth;
1209 1.14 mycroft } else
1210 1.14 mycroft error = EAUTH;
1211 1.14 mycroft } else
1212 1.1 cgd error = EACCES;
1213 1.1 cgd m_freem(mrep);
1214 1.110 yamt goto nfsmout;
1215 1.1 cgd }
1216 1.14 mycroft
1217 1.1 cgd /*
1218 1.24 fvdl * Grab any Kerberos verifier, otherwise just throw it away.
1219 1.1 cgd */
1220 1.24 fvdl verf_type = fxdr_unsigned(int, *tl++);
1221 1.24 fvdl i = fxdr_unsigned(int32_t, *tl);
1222 1.24 fvdl if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1223 1.24 fvdl error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1224 1.24 fvdl if (error)
1225 1.24 fvdl goto nfsmout;
1226 1.24 fvdl } else if (i > 0)
1227 1.24 fvdl nfsm_adv(nfsm_rndup(i));
1228 1.22 cgd nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1229 1.1 cgd /* 0 == ok */
1230 1.1 cgd if (*tl == 0) {
1231 1.22 cgd nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1232 1.1 cgd if (*tl != 0) {
1233 1.1 cgd error = fxdr_unsigned(int, *tl);
1234 1.115 christos switch (error) {
1235 1.115 christos case NFSERR_PERM:
1236 1.115 christos error = EPERM;
1237 1.115 christos break;
1238 1.115 christos
1239 1.115 christos case NFSERR_NOENT:
1240 1.115 christos error = ENOENT;
1241 1.115 christos break;
1242 1.115 christos
1243 1.116 christos case NFSERR_IO:
1244 1.117 tron error = EIO;
1245 1.116 christos break;
1246 1.116 christos
1247 1.115 christos case NFSERR_NXIO:
1248 1.115 christos error = ENXIO;
1249 1.115 christos break;
1250 1.115 christos
1251 1.115 christos case NFSERR_ACCES:
1252 1.115 christos error = EACCES;
1253 1.115 christos if (!retry_cred)
1254 1.115 christos break;
1255 1.110 yamt m_freem(mrep);
1256 1.110 yamt m_freem(rep->r_mreq);
1257 1.110 yamt FREE(rep, M_NFSREQ);
1258 1.110 yamt use_opencred = !use_opencred;
1259 1.130 elad if (mrest_backup == NULL) {
1260 1.115 christos /* m_copym failure */
1261 1.133 yamt KASSERT(
1262 1.133 yamt kauth_cred_getrefcnt(acred) == 1);
1263 1.133 yamt kauth_cred_free(acred);
1264 1.115 christos return ENOMEM;
1265 1.130 elad }
1266 1.110 yamt mrest = mrest_backup;
1267 1.110 yamt mrest_backup = NULL;
1268 1.110 yamt cred = origcred;
1269 1.110 yamt error = 0;
1270 1.147 thorpej retry_cred = false;
1271 1.110 yamt goto tryagain_cred;
1272 1.115 christos
1273 1.115 christos case NFSERR_EXIST:
1274 1.115 christos error = EEXIST;
1275 1.115 christos break;
1276 1.115 christos
1277 1.115 christos case NFSERR_XDEV:
1278 1.115 christos error = EXDEV;
1279 1.115 christos break;
1280 1.115 christos
1281 1.115 christos case NFSERR_NODEV:
1282 1.115 christos error = ENODEV;
1283 1.115 christos break;
1284 1.115 christos
1285 1.115 christos case NFSERR_NOTDIR:
1286 1.115 christos error = ENOTDIR;
1287 1.115 christos break;
1288 1.115 christos
1289 1.115 christos case NFSERR_ISDIR:
1290 1.115 christos error = EISDIR;
1291 1.115 christos break;
1292 1.115 christos
1293 1.115 christos case NFSERR_INVAL:
1294 1.115 christos error = EINVAL;
1295 1.115 christos break;
1296 1.115 christos
1297 1.115 christos case NFSERR_FBIG:
1298 1.115 christos error = EFBIG;
1299 1.115 christos break;
1300 1.115 christos
1301 1.115 christos case NFSERR_NOSPC:
1302 1.115 christos error = ENOSPC;
1303 1.115 christos break;
1304 1.115 christos
1305 1.115 christos case NFSERR_ROFS:
1306 1.115 christos error = EROFS;
1307 1.115 christos break;
1308 1.115 christos
1309 1.115 christos case NFSERR_MLINK:
1310 1.115 christos error = EMLINK;
1311 1.115 christos break;
1312 1.115 christos
1313 1.116 christos case NFSERR_TIMEDOUT:
1314 1.116 christos error = ETIMEDOUT;
1315 1.116 christos break;
1316 1.116 christos
1317 1.115 christos case NFSERR_NAMETOL:
1318 1.115 christos error = ENAMETOOLONG;
1319 1.115 christos break;
1320 1.115 christos
1321 1.115 christos case NFSERR_NOTEMPTY:
1322 1.115 christos error = ENOTEMPTY;
1323 1.115 christos break;
1324 1.115 christos
1325 1.115 christos case NFSERR_DQUOT:
1326 1.115 christos error = EDQUOT;
1327 1.115 christos break;
1328 1.115 christos
1329 1.115 christos case NFSERR_STALE:
1330 1.115 christos /*
1331 1.115 christos * If the File Handle was stale, invalidate the
1332 1.115 christos * lookup cache, just in case.
1333 1.115 christos */
1334 1.115 christos error = ESTALE;
1335 1.115 christos cache_purge(NFSTOV(np));
1336 1.115 christos break;
1337 1.115 christos
1338 1.115 christos case NFSERR_REMOTE:
1339 1.115 christos error = EREMOTE;
1340 1.115 christos break;
1341 1.115 christos
1342 1.115 christos case NFSERR_WFLUSH:
1343 1.115 christos case NFSERR_BADHANDLE:
1344 1.115 christos case NFSERR_NOT_SYNC:
1345 1.115 christos case NFSERR_BAD_COOKIE:
1346 1.115 christos error = EINVAL;
1347 1.115 christos break;
1348 1.115 christos
1349 1.115 christos case NFSERR_NOTSUPP:
1350 1.115 christos error = ENOTSUP;
1351 1.115 christos break;
1352 1.115 christos
1353 1.115 christos case NFSERR_TOOSMALL:
1354 1.115 christos case NFSERR_SERVERFAULT:
1355 1.115 christos case NFSERR_BADTYPE:
1356 1.115 christos error = EINVAL;
1357 1.115 christos break;
1358 1.115 christos
1359 1.115 christos case NFSERR_TRYLATER:
1360 1.115 christos if ((nmp->nm_flag & NFSMNT_NFSV3) == 0)
1361 1.115 christos break;
1362 1.24 fvdl m_freem(mrep);
1363 1.14 mycroft error = 0;
1364 1.135 kardel waituntil = time_second + trylater_delay;
1365 1.144 yamt while (time_second < waituntil) {
1366 1.144 yamt tsleep(&lbolt, PSOCK, "nfstrylater", 0);
1367 1.144 yamt }
1368 1.97 yamt trylater_delay *= NFS_TRYLATERDELMUL;
1369 1.97 yamt if (trylater_delay > NFS_TRYLATERDELMAX)
1370 1.97 yamt trylater_delay = NFS_TRYLATERDELMAX;
1371 1.95 yamt /*
1372 1.95 yamt * RFC1813:
1373 1.95 yamt * The client should wait and then try
1374 1.95 yamt * the request with a new RPC transaction ID.
1375 1.95 yamt */
1376 1.95 yamt nfs_renewxid(rep);
1377 1.14 mycroft goto tryagain;
1378 1.115 christos
1379 1.115 christos default:
1380 1.115 christos #ifdef DIAGNOSTIC
1381 1.115 christos printf("Invalid rpc error code %d\n", error);
1382 1.115 christos #endif
1383 1.115 christos error = EINVAL;
1384 1.115 christos break;
1385 1.14 mycroft }
1386 1.14 mycroft
1387 1.24 fvdl if (nmp->nm_flag & NFSMNT_NFSV3) {
1388 1.24 fvdl *mrp = mrep;
1389 1.24 fvdl *mdp = md;
1390 1.24 fvdl *dposp = dpos;
1391 1.24 fvdl error |= NFSERR_RETERR;
1392 1.24 fvdl } else
1393 1.24 fvdl m_freem(mrep);
1394 1.110 yamt goto nfsmout;
1395 1.1 cgd }
1396 1.14 mycroft
1397 1.110 yamt /*
1398 1.110 yamt * note which credential worked to minimize number of retries.
1399 1.110 yamt */
1400 1.110 yamt if (use_opencred)
1401 1.110 yamt np->n_flag |= NUSEOPENCRED;
1402 1.110 yamt else
1403 1.110 yamt np->n_flag &= ~NUSEOPENCRED;
1404 1.110 yamt
1405 1.1 cgd *mrp = mrep;
1406 1.1 cgd *mdp = md;
1407 1.1 cgd *dposp = dpos;
1408 1.110 yamt
1409 1.110 yamt KASSERT(error == 0);
1410 1.110 yamt goto nfsmout;
1411 1.1 cgd }
1412 1.1 cgd m_freem(mrep);
1413 1.24 fvdl error = EPROTONOSUPPORT;
1414 1.24 fvdl nfsmout:
1415 1.133 yamt KASSERT(kauth_cred_getrefcnt(acred) == 1);
1416 1.133 yamt kauth_cred_free(acred);
1417 1.14 mycroft m_freem(rep->r_mreq);
1418 1.148 christos free((void *)rep, M_NFSREQ);
1419 1.110 yamt m_freem(mrest_backup);
1420 1.1 cgd return (error);
1421 1.1 cgd }
1422 1.32 thorpej #endif /* NFS */
1423 1.1 cgd
1424 1.1 cgd /*
1425 1.1 cgd * Generate the rpc reply header
1426 1.1 cgd * siz arg. is used to decide if adding a cluster is worthwhile
1427 1.1 cgd */
1428 1.23 christos int
1429 1.24 fvdl nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1430 1.1 cgd int siz;
1431 1.24 fvdl struct nfsrv_descript *nd;
1432 1.24 fvdl struct nfssvc_sock *slp;
1433 1.1 cgd int err;
1434 1.14 mycroft int cache;
1435 1.14 mycroft u_quad_t *frev;
1436 1.1 cgd struct mbuf **mrq;
1437 1.1 cgd struct mbuf **mbp;
1438 1.148 christos char **bposp;
1439 1.1 cgd {
1440 1.55 augustss u_int32_t *tl;
1441 1.55 augustss struct mbuf *mreq;
1442 1.148 christos char *bpos;
1443 1.79 matt struct mbuf *mb;
1444 1.1 cgd
1445 1.79 matt mreq = m_gethdr(M_WAIT, MT_DATA);
1446 1.79 matt MCLAIM(mreq, &nfs_mowner);
1447 1.1 cgd mb = mreq;
1448 1.14 mycroft /*
1449 1.14 mycroft * If this is a big reply, use a cluster else
1450 1.14 mycroft * try and leave leading space for the lower level headers.
1451 1.14 mycroft */
1452 1.14 mycroft siz += RPC_REPLYSIZ;
1453 1.45 fvdl if (siz >= max_datalen) {
1454 1.79 matt m_clget(mreq, M_WAIT);
1455 1.14 mycroft } else
1456 1.14 mycroft mreq->m_data += max_hdr;
1457 1.22 cgd tl = mtod(mreq, u_int32_t *);
1458 1.24 fvdl mreq->m_len = 6 * NFSX_UNSIGNED;
1459 1.148 christos bpos = ((char *)tl) + mreq->m_len;
1460 1.14 mycroft *tl++ = txdr_unsigned(nd->nd_retxid);
1461 1.1 cgd *tl++ = rpc_reply;
1462 1.24 fvdl if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1463 1.1 cgd *tl++ = rpc_msgdenied;
1464 1.24 fvdl if (err & NFSERR_AUTHERR) {
1465 1.14 mycroft *tl++ = rpc_autherr;
1466 1.24 fvdl *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1467 1.14 mycroft mreq->m_len -= NFSX_UNSIGNED;
1468 1.14 mycroft bpos -= NFSX_UNSIGNED;
1469 1.14 mycroft } else {
1470 1.14 mycroft *tl++ = rpc_mismatch;
1471 1.24 fvdl *tl++ = txdr_unsigned(RPC_VER2);
1472 1.24 fvdl *tl = txdr_unsigned(RPC_VER2);
1473 1.14 mycroft }
1474 1.1 cgd } else {
1475 1.1 cgd *tl++ = rpc_msgaccepted;
1476 1.24 fvdl
1477 1.24 fvdl /*
1478 1.24 fvdl * For Kerberos authentication, we must send the nickname
1479 1.24 fvdl * verifier back, otherwise just RPCAUTH_NULL.
1480 1.24 fvdl */
1481 1.24 fvdl if (nd->nd_flag & ND_KERBFULL) {
1482 1.84 yamt struct nfsuid *nuidp;
1483 1.84 yamt struct timeval ktvin, ktvout;
1484 1.24 fvdl
1485 1.129 mrg memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1486 1.129 mrg
1487 1.131 yamt LIST_FOREACH(nuidp,
1488 1.131 yamt NUIDHASH(slp, kauth_cred_geteuid(nd->nd_cr)),
1489 1.84 yamt nu_hash) {
1490 1.131 yamt if (kauth_cred_geteuid(nuidp->nu_cr) ==
1491 1.131 yamt kauth_cred_geteuid(nd->nd_cr) &&
1492 1.84 yamt (!nd->nd_nam2 || netaddr_match(
1493 1.84 yamt NU_NETFAM(nuidp), &nuidp->nu_haddr,
1494 1.84 yamt nd->nd_nam2)))
1495 1.84 yamt break;
1496 1.84 yamt }
1497 1.84 yamt if (nuidp) {
1498 1.84 yamt ktvin.tv_sec =
1499 1.84 yamt txdr_unsigned(nuidp->nu_timestamp.tv_sec
1500 1.84 yamt - 1);
1501 1.84 yamt ktvin.tv_usec =
1502 1.84 yamt txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1503 1.24 fvdl
1504 1.84 yamt /*
1505 1.84 yamt * Encrypt the timestamp in ecb mode using the
1506 1.84 yamt * session key.
1507 1.84 yamt */
1508 1.24 fvdl #ifdef NFSKERB
1509 1.84 yamt XXX
1510 1.24 fvdl #endif
1511 1.24 fvdl
1512 1.84 yamt *tl++ = rpc_auth_kerb;
1513 1.84 yamt *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1514 1.84 yamt *tl = ktvout.tv_sec;
1515 1.84 yamt nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1516 1.84 yamt *tl++ = ktvout.tv_usec;
1517 1.131 yamt *tl++ = txdr_unsigned(
1518 1.131 yamt kauth_cred_geteuid(nuidp->nu_cr));
1519 1.84 yamt } else {
1520 1.84 yamt *tl++ = 0;
1521 1.84 yamt *tl++ = 0;
1522 1.84 yamt }
1523 1.24 fvdl } else {
1524 1.24 fvdl *tl++ = 0;
1525 1.24 fvdl *tl++ = 0;
1526 1.24 fvdl }
1527 1.1 cgd switch (err) {
1528 1.1 cgd case EPROGUNAVAIL:
1529 1.1 cgd *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1530 1.1 cgd break;
1531 1.1 cgd case EPROGMISMATCH:
1532 1.1 cgd *tl = txdr_unsigned(RPC_PROGMISMATCH);
1533 1.24 fvdl nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1534 1.144 yamt *tl++ = txdr_unsigned(2);
1535 1.144 yamt *tl = txdr_unsigned(3);
1536 1.1 cgd break;
1537 1.1 cgd case EPROCUNAVAIL:
1538 1.1 cgd *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1539 1.1 cgd break;
1540 1.24 fvdl case EBADRPC:
1541 1.24 fvdl *tl = txdr_unsigned(RPC_GARBAGE);
1542 1.24 fvdl break;
1543 1.1 cgd default:
1544 1.1 cgd *tl = 0;
1545 1.24 fvdl if (err != NFSERR_RETVOID) {
1546 1.22 cgd nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1547 1.14 mycroft if (err)
1548 1.24 fvdl *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1549 1.14 mycroft else
1550 1.24 fvdl *tl = 0;
1551 1.1 cgd }
1552 1.1 cgd break;
1553 1.1 cgd };
1554 1.1 cgd }
1555 1.14 mycroft
1556 1.34 fvdl if (mrq != NULL)
1557 1.34 fvdl *mrq = mreq;
1558 1.1 cgd *mbp = mb;
1559 1.1 cgd *bposp = bpos;
1560 1.24 fvdl if (err != 0 && err != NFSERR_RETVOID)
1561 1.1 cgd nfsstats.srvrpc_errs++;
1562 1.1 cgd return (0);
1563 1.1 cgd }
1564 1.1 cgd
1565 1.1 cgd /*
1566 1.1 cgd * Nfs timer routine
1567 1.1 cgd * Scan the nfsreq list and retranmit any requests that have timed out
1568 1.1 cgd * To avoid retransmission attempts on STREAM sockets (in the future) make
1569 1.1 cgd * sure to set the r_retry field to 0 (implies nm_retry == 0).
1570 1.1 cgd */
1571 1.7 mycroft void
1572 1.141 yamt nfs_timer(void *arg)
1573 1.1 cgd {
1574 1.55 augustss struct nfsreq *rep;
1575 1.55 augustss struct mbuf *m;
1576 1.55 augustss struct socket *so;
1577 1.55 augustss struct nfsmount *nmp;
1578 1.55 augustss int timeo;
1579 1.27 thorpej int s, error;
1580 1.27 thorpej #ifdef NFSSERVER
1581 1.135 kardel struct timeval tv;
1582 1.55 augustss struct nfssvc_sock *slp;
1583 1.27 thorpej u_quad_t cur_usec;
1584 1.23 christos #endif
1585 1.1 cgd
1586 1.21 mycroft s = splsoftnet();
1587 1.73 christos TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1588 1.1 cgd nmp = rep->r_nmp;
1589 1.14 mycroft if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1590 1.1 cgd continue;
1591 1.119 christos if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
1592 1.1 cgd rep->r_flags |= R_SOFTTERM;
1593 1.1 cgd continue;
1594 1.1 cgd }
1595 1.14 mycroft if (rep->r_rtt >= 0) {
1596 1.14 mycroft rep->r_rtt++;
1597 1.14 mycroft if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1598 1.14 mycroft timeo = nmp->nm_timeo;
1599 1.14 mycroft else
1600 1.14 mycroft timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1601 1.14 mycroft if (nmp->nm_timeouts > 0)
1602 1.14 mycroft timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1603 1.14 mycroft if (rep->r_rtt <= timeo)
1604 1.14 mycroft continue;
1605 1.98 yamt if (nmp->nm_timeouts <
1606 1.98 yamt (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
1607 1.14 mycroft nmp->nm_timeouts++;
1608 1.1 cgd }
1609 1.1 cgd /*
1610 1.1 cgd * Check for server not responding
1611 1.1 cgd */
1612 1.1 cgd if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1613 1.14 mycroft rep->r_rexmit > nmp->nm_deadthresh) {
1614 1.119 christos nfs_msg(rep->r_lwp,
1615 1.1 cgd nmp->nm_mountp->mnt_stat.f_mntfromname,
1616 1.1 cgd "not responding");
1617 1.1 cgd rep->r_flags |= R_TPRINTFMSG;
1618 1.1 cgd }
1619 1.1 cgd if (rep->r_rexmit >= rep->r_retry) { /* too many */
1620 1.1 cgd nfsstats.rpctimeouts++;
1621 1.1 cgd rep->r_flags |= R_SOFTTERM;
1622 1.1 cgd continue;
1623 1.1 cgd }
1624 1.14 mycroft if (nmp->nm_sotype != SOCK_DGRAM) {
1625 1.14 mycroft if (++rep->r_rexmit > NFS_MAXREXMIT)
1626 1.14 mycroft rep->r_rexmit = NFS_MAXREXMIT;
1627 1.14 mycroft continue;
1628 1.14 mycroft }
1629 1.14 mycroft if ((so = nmp->nm_so) == NULL)
1630 1.1 cgd continue;
1631 1.1 cgd
1632 1.1 cgd /*
1633 1.1 cgd * If there is enough space and the window allows..
1634 1.1 cgd * Resend it
1635 1.14 mycroft * Set r_rtt to -1 in case we fail to send it now.
1636 1.1 cgd */
1637 1.14 mycroft rep->r_rtt = -1;
1638 1.1 cgd if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1639 1.14 mycroft ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1640 1.14 mycroft (rep->r_flags & R_SENT) ||
1641 1.14 mycroft nmp->nm_sent < nmp->nm_cwnd) &&
1642 1.14 mycroft (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1643 1.40 fvdl if (so->so_state & SS_ISCONNECTED)
1644 1.1 cgd error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1645 1.119 christos (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1646 1.1 cgd else
1647 1.1 cgd error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1648 1.119 christos nmp->nm_nam, (struct mbuf *)0, (struct lwp *)0);
1649 1.1 cgd if (error) {
1650 1.33 fvdl if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
1651 1.37 fvdl #ifdef DEBUG
1652 1.33 fvdl printf("nfs_timer: ignoring error %d\n",
1653 1.33 fvdl error);
1654 1.37 fvdl #endif
1655 1.1 cgd so->so_error = 0;
1656 1.33 fvdl }
1657 1.1 cgd } else {
1658 1.1 cgd /*
1659 1.14 mycroft * Iff first send, start timing
1660 1.14 mycroft * else turn timing off, backoff timer
1661 1.14 mycroft * and divide congestion window by 2.
1662 1.1 cgd */
1663 1.14 mycroft if (rep->r_flags & R_SENT) {
1664 1.14 mycroft rep->r_flags &= ~R_TIMING;
1665 1.14 mycroft if (++rep->r_rexmit > NFS_MAXREXMIT)
1666 1.14 mycroft rep->r_rexmit = NFS_MAXREXMIT;
1667 1.14 mycroft nmp->nm_cwnd >>= 1;
1668 1.14 mycroft if (nmp->nm_cwnd < NFS_CWNDSCALE)
1669 1.14 mycroft nmp->nm_cwnd = NFS_CWNDSCALE;
1670 1.14 mycroft nfsstats.rpcretries++;
1671 1.14 mycroft } else {
1672 1.14 mycroft rep->r_flags |= R_SENT;
1673 1.14 mycroft nmp->nm_sent += NFS_CWNDSCALE;
1674 1.14 mycroft }
1675 1.14 mycroft rep->r_rtt = 0;
1676 1.1 cgd }
1677 1.1 cgd }
1678 1.1 cgd }
1679 1.14 mycroft
1680 1.14 mycroft #ifdef NFSSERVER
1681 1.14 mycroft /*
1682 1.24 fvdl * Scan the write gathering queues for writes that need to be
1683 1.24 fvdl * completed now.
1684 1.24 fvdl */
1685 1.135 kardel getmicrotime(&tv);
1686 1.135 kardel cur_usec = (u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec;
1687 1.73 christos TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1688 1.80 yamt if (LIST_FIRST(&slp->ns_tq) &&
1689 1.80 yamt LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
1690 1.24 fvdl nfsrv_wakenfsd(slp);
1691 1.24 fvdl }
1692 1.14 mycroft #endif /* NFSSERVER */
1693 1.1 cgd splx(s);
1694 1.99 yamt callout_schedule(&nfs_timer_ch, nfs_ticks);
1695 1.1 cgd }
1696 1.1 cgd
1697 1.73 christos /*ARGSUSED*/
1698 1.73 christos void
1699 1.141 yamt nfs_exit(struct proc *p, void *v)
1700 1.73 christos {
1701 1.73 christos struct nfsreq *rp;
1702 1.73 christos int s = splsoftnet();
1703 1.73 christos
1704 1.73 christos TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
1705 1.119 christos if (rp->r_lwp && rp->r_lwp->l_proc == p)
1706 1.73 christos TAILQ_REMOVE(&nfs_reqq, rp, r_chain);
1707 1.73 christos }
1708 1.73 christos splx(s);
1709 1.73 christos }
1710 1.73 christos
1711 1.1 cgd /*
1712 1.14 mycroft * Test for a termination condition pending on the process.
1713 1.14 mycroft * This is used for NFSMNT_INT mounts.
1714 1.1 cgd */
1715 1.23 christos int
1716 1.119 christos nfs_sigintr(nmp, rep, l)
1717 1.14 mycroft struct nfsmount *nmp;
1718 1.14 mycroft struct nfsreq *rep;
1719 1.119 christos struct lwp *l;
1720 1.14 mycroft {
1721 1.47 mycroft sigset_t ss;
1722 1.14 mycroft
1723 1.14 mycroft if (rep && (rep->r_flags & R_SOFTTERM))
1724 1.14 mycroft return (EINTR);
1725 1.14 mycroft if (!(nmp->nm_flag & NFSMNT_INT))
1726 1.14 mycroft return (0);
1727 1.119 christos if (l) {
1728 1.145 ad sigpending1(l, &ss);
1729 1.47 mycroft #if 0
1730 1.119 christos sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
1731 1.47 mycroft #endif
1732 1.47 mycroft if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
1733 1.47 mycroft sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
1734 1.47 mycroft sigismember(&ss, SIGQUIT))
1735 1.47 mycroft return (EINTR);
1736 1.47 mycroft }
1737 1.14 mycroft return (0);
1738 1.14 mycroft }
1739 1.1 cgd
1740 1.1 cgd /*
1741 1.14 mycroft * Lock a socket against others.
1742 1.14 mycroft * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1743 1.14 mycroft * and also to avoid race conditions between the processes with nfs requests
1744 1.14 mycroft * in progress when a reconnect is necessary.
1745 1.1 cgd */
1746 1.150 yamt static int
1747 1.150 yamt nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep)
1748 1.14 mycroft {
1749 1.119 christos struct lwp *l;
1750 1.150 yamt int timeo = 0;
1751 1.150 yamt bool catch = false;
1752 1.150 yamt int error = 0;
1753 1.14 mycroft
1754 1.14 mycroft if (rep) {
1755 1.119 christos l = rep->r_lwp;
1756 1.14 mycroft if (rep->r_nmp->nm_flag & NFSMNT_INT)
1757 1.150 yamt catch = true;
1758 1.14 mycroft } else
1759 1.150 yamt l = NULL;
1760 1.150 yamt mutex_enter(&nmp->nm_lock);
1761 1.150 yamt while ((nmp->nm_iflag & NFSMNT_SNDLOCK) != 0) {
1762 1.150 yamt if (rep && nfs_sigintr(rep->r_nmp, rep, l)) {
1763 1.150 yamt error = EINTR;
1764 1.150 yamt goto quit;
1765 1.150 yamt }
1766 1.150 yamt if (catch) {
1767 1.150 yamt cv_timedwait_sig(&nmp->nm_sndcv, &nmp->nm_lock, timeo);
1768 1.150 yamt } else {
1769 1.150 yamt cv_timedwait(&nmp->nm_sndcv, &nmp->nm_lock, timeo);
1770 1.150 yamt }
1771 1.150 yamt if (catch) {
1772 1.150 yamt catch = false;
1773 1.150 yamt timeo = 2 * hz;
1774 1.14 mycroft }
1775 1.14 mycroft }
1776 1.150 yamt nmp->nm_iflag |= NFSMNT_SNDLOCK;
1777 1.150 yamt quit:
1778 1.150 yamt mutex_exit(&nmp->nm_lock);
1779 1.150 yamt return error;
1780 1.14 mycroft }
1781 1.1 cgd
1782 1.14 mycroft /*
1783 1.14 mycroft * Unlock the stream socket for others.
1784 1.14 mycroft */
1785 1.150 yamt static void
1786 1.150 yamt nfs_sndunlock(struct nfsmount *nmp)
1787 1.1 cgd {
1788 1.1 cgd
1789 1.150 yamt mutex_enter(&nmp->nm_lock);
1790 1.150 yamt if ((nmp->nm_iflag & NFSMNT_SNDLOCK) == 0)
1791 1.14 mycroft panic("nfs sndunlock");
1792 1.150 yamt nmp->nm_iflag &= ~NFSMNT_SNDLOCK;
1793 1.150 yamt cv_signal(&nmp->nm_sndcv);
1794 1.150 yamt mutex_exit(&nmp->nm_lock);
1795 1.14 mycroft }
1796 1.14 mycroft
1797 1.150 yamt static int
1798 1.150 yamt nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
1799 1.14 mycroft {
1800 1.55 augustss int *flagp = &nmp->nm_iflag;
1801 1.150 yamt int slptimeo = 0;
1802 1.150 yamt bool catch;
1803 1.87 yamt int error = 0;
1804 1.14 mycroft
1805 1.150 yamt KASSERT(nmp == rep->r_nmp);
1806 1.150 yamt
1807 1.51 sommerfe if (*flagp & NFSMNT_DISMNT)
1808 1.51 sommerfe return EIO;
1809 1.112 perry
1810 1.14 mycroft if (*flagp & NFSMNT_INT)
1811 1.150 yamt catch = true;
1812 1.14 mycroft else
1813 1.150 yamt catch = false;
1814 1.150 yamt mutex_enter(&nmp->nm_lock);
1815 1.14 mycroft while (*flagp & NFSMNT_RCVLOCK) {
1816 1.119 christos if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
1817 1.87 yamt error = EINTR;
1818 1.87 yamt goto quit;
1819 1.87 yamt }
1820 1.150 yamt if (catch) {
1821 1.150 yamt cv_timedwait_sig(&nmp->nm_rcvcv, &nmp->nm_lock,
1822 1.150 yamt slptimeo);
1823 1.150 yamt } else {
1824 1.150 yamt cv_timedwait(&nmp->nm_rcvcv, &nmp->nm_lock,
1825 1.150 yamt slptimeo);
1826 1.150 yamt }
1827 1.51 sommerfe if (*flagp & NFSMNT_DISMNT) {
1828 1.51 sommerfe wakeup(&nmp->nm_waiters);
1829 1.87 yamt error = EIO;
1830 1.87 yamt goto quit;
1831 1.51 sommerfe }
1832 1.36 fvdl /* If our reply was received while we were sleeping,
1833 1.36 fvdl * then just return without taking the lock to avoid a
1834 1.36 fvdl * situation where a single iod could 'capture' the
1835 1.36 fvdl * receive lock.
1836 1.36 fvdl */
1837 1.87 yamt if (rep->r_mrep != NULL) {
1838 1.150 yamt cv_signal(&nmp->nm_rcvcv);
1839 1.87 yamt error = EALREADY;
1840 1.87 yamt goto quit;
1841 1.87 yamt }
1842 1.150 yamt if (catch) {
1843 1.150 yamt catch = false;
1844 1.14 mycroft slptimeo = 2 * hz;
1845 1.1 cgd }
1846 1.1 cgd }
1847 1.14 mycroft *flagp |= NFSMNT_RCVLOCK;
1848 1.87 yamt quit:
1849 1.150 yamt mutex_exit(&nmp->nm_lock);
1850 1.87 yamt return error;
1851 1.14 mycroft }
1852 1.14 mycroft
1853 1.14 mycroft /*
1854 1.14 mycroft * Unlock the stream socket for others.
1855 1.14 mycroft */
1856 1.150 yamt static void
1857 1.150 yamt nfs_rcvunlock(struct nfsmount *nmp)
1858 1.14 mycroft {
1859 1.14 mycroft
1860 1.150 yamt mutex_enter(&nmp->nm_lock);
1861 1.150 yamt if ((nmp->nm_iflag & NFSMNT_RCVLOCK) == 0)
1862 1.14 mycroft panic("nfs rcvunlock");
1863 1.150 yamt nmp->nm_iflag &= ~NFSMNT_RCVLOCK;
1864 1.150 yamt cv_signal(&nmp->nm_rcvcv);
1865 1.150 yamt mutex_exit(&nmp->nm_lock);
1866 1.1 cgd }
1867 1.1 cgd
1868 1.14 mycroft /*
1869 1.14 mycroft * Parse an RPC request
1870 1.14 mycroft * - verify it
1871 1.131 yamt * - allocate and fill in the cred.
1872 1.1 cgd */
1873 1.23 christos int
1874 1.24 fvdl nfs_getreq(nd, nfsd, has_header)
1875 1.55 augustss struct nfsrv_descript *nd;
1876 1.24 fvdl struct nfsd *nfsd;
1877 1.14 mycroft int has_header;
1878 1.1 cgd {
1879 1.55 augustss int len, i;
1880 1.55 augustss u_int32_t *tl;
1881 1.55 augustss int32_t t1;
1882 1.14 mycroft struct uio uio;
1883 1.14 mycroft struct iovec iov;
1884 1.148 christos char *dpos, *cp2, *cp;
1885 1.22 cgd u_int32_t nfsvers, auth_type;
1886 1.24 fvdl uid_t nickuid;
1887 1.144 yamt int error = 0, ticklen;
1888 1.14 mycroft struct mbuf *mrep, *md;
1889 1.55 augustss struct nfsuid *nuidp;
1890 1.24 fvdl struct timeval tvin, tvout;
1891 1.14 mycroft
1892 1.129 mrg memset(&tvout, 0, sizeof tvout); /* XXX gcc */
1893 1.129 mrg
1894 1.131 yamt KASSERT(nd->nd_cr == NULL);
1895 1.14 mycroft mrep = nd->nd_mrep;
1896 1.14 mycroft md = nd->nd_md;
1897 1.14 mycroft dpos = nd->nd_dpos;
1898 1.14 mycroft if (has_header) {
1899 1.24 fvdl nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1900 1.24 fvdl nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1901 1.14 mycroft if (*tl++ != rpc_call) {
1902 1.14 mycroft m_freem(mrep);
1903 1.14 mycroft return (EBADRPC);
1904 1.14 mycroft }
1905 1.24 fvdl } else
1906 1.24 fvdl nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1907 1.14 mycroft nd->nd_repstat = 0;
1908 1.24 fvdl nd->nd_flag = 0;
1909 1.14 mycroft if (*tl++ != rpc_vers) {
1910 1.14 mycroft nd->nd_repstat = ERPCMISMATCH;
1911 1.14 mycroft nd->nd_procnum = NFSPROC_NOOP;
1912 1.14 mycroft return (0);
1913 1.14 mycroft }
1914 1.14 mycroft if (*tl != nfs_prog) {
1915 1.144 yamt nd->nd_repstat = EPROGUNAVAIL;
1916 1.144 yamt nd->nd_procnum = NFSPROC_NOOP;
1917 1.144 yamt return (0);
1918 1.14 mycroft }
1919 1.14 mycroft tl++;
1920 1.24 fvdl nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1921 1.144 yamt if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
1922 1.14 mycroft nd->nd_repstat = EPROGMISMATCH;
1923 1.14 mycroft nd->nd_procnum = NFSPROC_NOOP;
1924 1.14 mycroft return (0);
1925 1.14 mycroft }
1926 1.144 yamt if (nfsvers == NFS_VER3)
1927 1.24 fvdl nd->nd_flag = ND_NFSV3;
1928 1.24 fvdl nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1929 1.14 mycroft if (nd->nd_procnum == NFSPROC_NULL)
1930 1.14 mycroft return (0);
1931 1.144 yamt if (nd->nd_procnum > NFSPROC_COMMIT ||
1932 1.144 yamt (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1933 1.14 mycroft nd->nd_repstat = EPROCUNAVAIL;
1934 1.14 mycroft nd->nd_procnum = NFSPROC_NOOP;
1935 1.1 cgd return (0);
1936 1.14 mycroft }
1937 1.24 fvdl if ((nd->nd_flag & ND_NFSV3) == 0)
1938 1.24 fvdl nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1939 1.14 mycroft auth_type = *tl++;
1940 1.14 mycroft len = fxdr_unsigned(int, *tl++);
1941 1.14 mycroft if (len < 0 || len > RPCAUTH_MAXSIZ) {
1942 1.14 mycroft m_freem(mrep);
1943 1.14 mycroft return (EBADRPC);
1944 1.14 mycroft }
1945 1.14 mycroft
1946 1.24 fvdl nd->nd_flag &= ~ND_KERBAUTH;
1947 1.14 mycroft /*
1948 1.14 mycroft * Handle auth_unix or auth_kerb.
1949 1.14 mycroft */
1950 1.14 mycroft if (auth_type == rpc_auth_unix) {
1951 1.130 elad uid_t uid;
1952 1.130 elad gid_t gid, *grbuf;
1953 1.130 elad
1954 1.131 yamt nd->nd_cr = kauth_cred_alloc();
1955 1.14 mycroft len = fxdr_unsigned(int, *++tl);
1956 1.14 mycroft if (len < 0 || len > NFS_MAXNAMLEN) {
1957 1.14 mycroft m_freem(mrep);
1958 1.131 yamt error = EBADRPC;
1959 1.131 yamt goto errout;
1960 1.14 mycroft }
1961 1.14 mycroft nfsm_adv(nfsm_rndup(len));
1962 1.24 fvdl nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1963 1.130 elad
1964 1.130 elad uid = fxdr_unsigned(uid_t, *tl++);
1965 1.130 elad gid = fxdr_unsigned(gid_t, *tl++);
1966 1.130 elad kauth_cred_setuid(nd->nd_cr, uid);
1967 1.137 yamt kauth_cred_seteuid(nd->nd_cr, uid);
1968 1.137 yamt kauth_cred_setsvuid(nd->nd_cr, uid);
1969 1.130 elad kauth_cred_setgid(nd->nd_cr, gid);
1970 1.137 yamt kauth_cred_setegid(nd->nd_cr, gid);
1971 1.130 elad kauth_cred_setsvgid(nd->nd_cr, gid);
1972 1.130 elad
1973 1.14 mycroft len = fxdr_unsigned(int, *tl);
1974 1.14 mycroft if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1975 1.14 mycroft m_freem(mrep);
1976 1.131 yamt error = EBADRPC;
1977 1.131 yamt goto errout;
1978 1.14 mycroft }
1979 1.24 fvdl nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1980 1.130 elad
1981 1.130 elad grbuf = malloc(len * sizeof(gid_t), M_TEMP, M_WAITOK);
1982 1.130 elad for (i = 0; i < len; i++) {
1983 1.130 elad if (i < NGROUPS) /* XXX elad */
1984 1.130 elad grbuf[i] = fxdr_unsigned(gid_t, *tl++);
1985 1.130 elad else
1986 1.130 elad tl++;
1987 1.130 elad }
1988 1.130 elad kauth_cred_setgroups(nd->nd_cr, grbuf, min(len, NGROUPS), -1);
1989 1.130 elad free(grbuf, M_TEMP);
1990 1.130 elad
1991 1.24 fvdl len = fxdr_unsigned(int, *++tl);
1992 1.24 fvdl if (len < 0 || len > RPCAUTH_MAXSIZ) {
1993 1.14 mycroft m_freem(mrep);
1994 1.131 yamt error = EBADRPC;
1995 1.131 yamt goto errout;
1996 1.14 mycroft }
1997 1.24 fvdl if (len > 0)
1998 1.24 fvdl nfsm_adv(nfsm_rndup(len));
1999 1.24 fvdl } else if (auth_type == rpc_auth_kerb) {
2000 1.24 fvdl switch (fxdr_unsigned(int, *tl++)) {
2001 1.24 fvdl case RPCAKN_FULLNAME:
2002 1.24 fvdl ticklen = fxdr_unsigned(int, *tl);
2003 1.24 fvdl *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
2004 1.24 fvdl uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2005 1.24 fvdl nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2006 1.24 fvdl if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
2007 1.24 fvdl m_freem(mrep);
2008 1.131 yamt error = EBADRPC;
2009 1.131 yamt goto errout;
2010 1.24 fvdl }
2011 1.24 fvdl uio.uio_offset = 0;
2012 1.24 fvdl uio.uio_iov = &iov;
2013 1.24 fvdl uio.uio_iovcnt = 1;
2014 1.123 yamt UIO_SETUP_SYSSPACE(&uio);
2015 1.148 christos iov.iov_base = (void *)&nfsd->nfsd_authstr[4];
2016 1.24 fvdl iov.iov_len = RPCAUTH_MAXSIZ - 4;
2017 1.24 fvdl nfsm_mtouio(&uio, uio.uio_resid);
2018 1.24 fvdl nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2019 1.24 fvdl if (*tl++ != rpc_auth_kerb ||
2020 1.24 fvdl fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
2021 1.31 christos printf("Bad kerb verifier\n");
2022 1.24 fvdl nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2023 1.24 fvdl nd->nd_procnum = NFSPROC_NOOP;
2024 1.24 fvdl return (0);
2025 1.24 fvdl }
2026 1.148 christos nfsm_dissect(cp, void *, 4 * NFSX_UNSIGNED);
2027 1.24 fvdl tl = (u_int32_t *)cp;
2028 1.24 fvdl if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
2029 1.31 christos printf("Not fullname kerb verifier\n");
2030 1.24 fvdl nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2031 1.24 fvdl nd->nd_procnum = NFSPROC_NOOP;
2032 1.24 fvdl return (0);
2033 1.24 fvdl }
2034 1.24 fvdl cp += NFSX_UNSIGNED;
2035 1.46 perry memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
2036 1.24 fvdl nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2037 1.24 fvdl nd->nd_flag |= ND_KERBFULL;
2038 1.24 fvdl nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2039 1.24 fvdl break;
2040 1.24 fvdl case RPCAKN_NICKNAME:
2041 1.24 fvdl if (len != 2 * NFSX_UNSIGNED) {
2042 1.31 christos printf("Kerb nickname short\n");
2043 1.24 fvdl nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2044 1.24 fvdl nd->nd_procnum = NFSPROC_NOOP;
2045 1.24 fvdl return (0);
2046 1.24 fvdl }
2047 1.24 fvdl nickuid = fxdr_unsigned(uid_t, *tl);
2048 1.24 fvdl nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2049 1.24 fvdl if (*tl++ != rpc_auth_kerb ||
2050 1.24 fvdl fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
2051 1.31 christos printf("Kerb nick verifier bad\n");
2052 1.24 fvdl nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2053 1.24 fvdl nd->nd_procnum = NFSPROC_NOOP;
2054 1.24 fvdl return (0);
2055 1.24 fvdl }
2056 1.24 fvdl nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2057 1.24 fvdl tvin.tv_sec = *tl++;
2058 1.24 fvdl tvin.tv_usec = *tl;
2059 1.24 fvdl
2060 1.80 yamt LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
2061 1.80 yamt nu_hash) {
2062 1.130 elad if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid &&
2063 1.24 fvdl (!nd->nd_nam2 ||
2064 1.24 fvdl netaddr_match(NU_NETFAM(nuidp),
2065 1.24 fvdl &nuidp->nu_haddr, nd->nd_nam2)))
2066 1.24 fvdl break;
2067 1.24 fvdl }
2068 1.24 fvdl if (!nuidp) {
2069 1.24 fvdl nd->nd_repstat =
2070 1.24 fvdl (NFSERR_AUTHERR|AUTH_REJECTCRED);
2071 1.24 fvdl nd->nd_procnum = NFSPROC_NOOP;
2072 1.24 fvdl return (0);
2073 1.24 fvdl }
2074 1.24 fvdl
2075 1.24 fvdl /*
2076 1.24 fvdl * Now, decrypt the timestamp using the session key
2077 1.24 fvdl * and validate it.
2078 1.24 fvdl */
2079 1.24 fvdl #ifdef NFSKERB
2080 1.24 fvdl XXX
2081 1.24 fvdl #endif
2082 1.14 mycroft
2083 1.24 fvdl tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2084 1.24 fvdl tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2085 1.135 kardel if (nuidp->nu_expire < time_second ||
2086 1.24 fvdl nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2087 1.24 fvdl (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2088 1.24 fvdl nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2089 1.24 fvdl nuidp->nu_expire = 0;
2090 1.24 fvdl nd->nd_repstat =
2091 1.24 fvdl (NFSERR_AUTHERR|AUTH_REJECTVERF);
2092 1.24 fvdl nd->nd_procnum = NFSPROC_NOOP;
2093 1.24 fvdl return (0);
2094 1.24 fvdl }
2095 1.131 yamt kauth_cred_hold(nuidp->nu_cr);
2096 1.131 yamt nd->nd_cr = nuidp->nu_cr;
2097 1.24 fvdl nd->nd_flag |= ND_KERBNICK;
2098 1.131 yamt }
2099 1.24 fvdl } else {
2100 1.24 fvdl nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2101 1.24 fvdl nd->nd_procnum = NFSPROC_NOOP;
2102 1.24 fvdl return (0);
2103 1.14 mycroft }
2104 1.14 mycroft
2105 1.14 mycroft nd->nd_md = md;
2106 1.14 mycroft nd->nd_dpos = dpos;
2107 1.131 yamt KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0)
2108 1.131 yamt || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0));
2109 1.14 mycroft return (0);
2110 1.14 mycroft nfsmout:
2111 1.131 yamt errout:
2112 1.131 yamt KASSERT(error != 0);
2113 1.131 yamt if (nd->nd_cr != NULL) {
2114 1.131 yamt kauth_cred_free(nd->nd_cr);
2115 1.131 yamt nd->nd_cr = NULL;
2116 1.131 yamt }
2117 1.14 mycroft return (error);
2118 1.1 cgd }
2119 1.1 cgd
2120 1.24 fvdl int
2121 1.119 christos nfs_msg(l, server, msg)
2122 1.119 christos struct lwp *l;
2123 1.114 christos const char *server, *msg;
2124 1.1 cgd {
2125 1.1 cgd tpr_t tpr;
2126 1.1 cgd
2127 1.119 christos if (l)
2128 1.119 christos tpr = tprintf_open(l->l_proc);
2129 1.1 cgd else
2130 1.1 cgd tpr = NULL;
2131 1.1 cgd tprintf(tpr, "nfs server %s: %s\n", server, msg);
2132 1.1 cgd tprintf_close(tpr);
2133 1.24 fvdl return (0);
2134 1.1 cgd }
2135 1.1 cgd
2136 1.14 mycroft #ifdef NFSSERVER
2137 1.24 fvdl int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
2138 1.119 christos struct nfssvc_sock *, struct lwp *,
2139 1.23 christos struct mbuf **)) = {
2140 1.14 mycroft nfsrv_null,
2141 1.14 mycroft nfsrv_getattr,
2142 1.14 mycroft nfsrv_setattr,
2143 1.14 mycroft nfsrv_lookup,
2144 1.24 fvdl nfsrv3_access,
2145 1.14 mycroft nfsrv_readlink,
2146 1.14 mycroft nfsrv_read,
2147 1.14 mycroft nfsrv_write,
2148 1.14 mycroft nfsrv_create,
2149 1.24 fvdl nfsrv_mkdir,
2150 1.24 fvdl nfsrv_symlink,
2151 1.24 fvdl nfsrv_mknod,
2152 1.14 mycroft nfsrv_remove,
2153 1.24 fvdl nfsrv_rmdir,
2154 1.14 mycroft nfsrv_rename,
2155 1.14 mycroft nfsrv_link,
2156 1.14 mycroft nfsrv_readdir,
2157 1.24 fvdl nfsrv_readdirplus,
2158 1.14 mycroft nfsrv_statfs,
2159 1.24 fvdl nfsrv_fsinfo,
2160 1.24 fvdl nfsrv_pathconf,
2161 1.24 fvdl nfsrv_commit,
2162 1.24 fvdl nfsrv_noop
2163 1.14 mycroft };
2164 1.14 mycroft
2165 1.1 cgd /*
2166 1.14 mycroft * Socket upcall routine for the nfsd sockets.
2167 1.148 christos * The void *arg is a pointer to the "struct nfssvc_sock".
2168 1.14 mycroft * Essentially do as much as possible non-blocking, else punt and it will
2169 1.14 mycroft * be called with M_WAIT from an nfsd.
2170 1.1 cgd */
2171 1.14 mycroft void
2172 1.14 mycroft nfsrv_rcv(so, arg, waitflag)
2173 1.14 mycroft struct socket *so;
2174 1.148 christos void *arg;
2175 1.14 mycroft int waitflag;
2176 1.1 cgd {
2177 1.55 augustss struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2178 1.55 augustss struct mbuf *m;
2179 1.14 mycroft struct mbuf *mp, *nam;
2180 1.14 mycroft struct uio auio;
2181 1.14 mycroft int flags, error;
2182 1.121 yamt int setflags = 0;
2183 1.1 cgd
2184 1.121 yamt error = nfsdsock_lock(slp, (waitflag != M_DONTWAIT));
2185 1.121 yamt if (error) {
2186 1.121 yamt setflags |= SLP_NEEDQ;
2187 1.121 yamt goto dorecs_unlocked;
2188 1.121 yamt }
2189 1.121 yamt
2190 1.121 yamt KASSERT(so == slp->ns_so);
2191 1.127 christos #define NFS_TEST_HEAVY
2192 1.128 dogcow #ifdef NFS_TEST_HEAVY
2193 1.14 mycroft /*
2194 1.14 mycroft * Define this to test for nfsds handling this under heavy load.
2195 1.121 yamt *
2196 1.121 yamt * XXX it isn't safe to call so_receive from so_upcall context.
2197 1.14 mycroft */
2198 1.14 mycroft if (waitflag == M_DONTWAIT) {
2199 1.121 yamt setflags |= SLP_NEEDQ;
2200 1.121 yamt goto dorecs;
2201 1.1 cgd }
2202 1.14 mycroft #endif
2203 1.121 yamt simple_lock(&slp->ns_lock);
2204 1.121 yamt slp->ns_flag &= ~SLP_NEEDQ;
2205 1.121 yamt simple_unlock(&slp->ns_lock);
2206 1.14 mycroft if (so->so_type == SOCK_STREAM) {
2207 1.127 christos #ifndef NFS_TEST_HEAVY
2208 1.14 mycroft /*
2209 1.14 mycroft * If there are already records on the queue, defer soreceive()
2210 1.14 mycroft * to an nfsd so that there is feedback to the TCP layer that
2211 1.14 mycroft * the nfs servers are heavily loaded.
2212 1.14 mycroft */
2213 1.14 mycroft if (slp->ns_rec && waitflag == M_DONTWAIT) {
2214 1.121 yamt setflags |= SLP_NEEDQ;
2215 1.14 mycroft goto dorecs;
2216 1.14 mycroft }
2217 1.127 christos #endif
2218 1.14 mycroft
2219 1.14 mycroft /*
2220 1.14 mycroft * Do soreceive().
2221 1.14 mycroft */
2222 1.14 mycroft auio.uio_resid = 1000000000;
2223 1.123 yamt /* not need to setup uio_vmspace */
2224 1.14 mycroft flags = MSG_DONTWAIT;
2225 1.121 yamt error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, &flags);
2226 1.121 yamt if (error || mp == NULL) {
2227 1.14 mycroft if (error == EWOULDBLOCK)
2228 1.121 yamt setflags |= SLP_NEEDQ;
2229 1.14 mycroft else
2230 1.121 yamt setflags |= SLP_DISCONN;
2231 1.14 mycroft goto dorecs;
2232 1.14 mycroft }
2233 1.14 mycroft m = mp;
2234 1.142 yamt m_claimm(m, &nfs_mowner);
2235 1.14 mycroft if (slp->ns_rawend) {
2236 1.14 mycroft slp->ns_rawend->m_next = m;
2237 1.14 mycroft slp->ns_cc += 1000000000 - auio.uio_resid;
2238 1.14 mycroft } else {
2239 1.14 mycroft slp->ns_raw = m;
2240 1.14 mycroft slp->ns_cc = 1000000000 - auio.uio_resid;
2241 1.14 mycroft }
2242 1.14 mycroft while (m->m_next)
2243 1.14 mycroft m = m->m_next;
2244 1.14 mycroft slp->ns_rawend = m;
2245 1.14 mycroft
2246 1.14 mycroft /*
2247 1.14 mycroft * Now try and parse record(s) out of the raw stream data.
2248 1.14 mycroft */
2249 1.24 fvdl error = nfsrv_getstream(slp, waitflag);
2250 1.24 fvdl if (error) {
2251 1.14 mycroft if (error == EPERM)
2252 1.121 yamt setflags |= SLP_DISCONN;
2253 1.14 mycroft else
2254 1.121 yamt setflags |= SLP_NEEDQ;
2255 1.14 mycroft }
2256 1.14 mycroft } else {
2257 1.14 mycroft do {
2258 1.14 mycroft auio.uio_resid = 1000000000;
2259 1.123 yamt /* not need to setup uio_vmspace */
2260 1.14 mycroft flags = MSG_DONTWAIT;
2261 1.121 yamt error = (*so->so_receive)(so, &nam, &auio, &mp, NULL,
2262 1.121 yamt &flags);
2263 1.14 mycroft if (mp) {
2264 1.14 mycroft if (nam) {
2265 1.14 mycroft m = nam;
2266 1.14 mycroft m->m_next = mp;
2267 1.14 mycroft } else
2268 1.14 mycroft m = mp;
2269 1.142 yamt m_claimm(m, &nfs_mowner);
2270 1.14 mycroft if (slp->ns_recend)
2271 1.14 mycroft slp->ns_recend->m_nextpkt = m;
2272 1.14 mycroft else
2273 1.14 mycroft slp->ns_rec = m;
2274 1.14 mycroft slp->ns_recend = m;
2275 1.14 mycroft m->m_nextpkt = (struct mbuf *)0;
2276 1.14 mycroft }
2277 1.14 mycroft if (error) {
2278 1.14 mycroft if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2279 1.121 yamt && error != EWOULDBLOCK) {
2280 1.121 yamt setflags |= SLP_DISCONN;
2281 1.14 mycroft goto dorecs;
2282 1.14 mycroft }
2283 1.14 mycroft }
2284 1.14 mycroft } while (mp);
2285 1.14 mycroft }
2286 1.121 yamt dorecs:
2287 1.121 yamt nfsdsock_unlock(slp);
2288 1.14 mycroft
2289 1.121 yamt dorecs_unlocked:
2290 1.14 mycroft /*
2291 1.14 mycroft * Now try and process the request records, non-blocking.
2292 1.14 mycroft */
2293 1.121 yamt if (setflags) {
2294 1.121 yamt simple_lock(&slp->ns_lock);
2295 1.121 yamt slp->ns_flag |= setflags;
2296 1.121 yamt simple_unlock(&slp->ns_lock);
2297 1.121 yamt }
2298 1.14 mycroft if (waitflag == M_DONTWAIT &&
2299 1.121 yamt (slp->ns_rec || (slp->ns_flag & (SLP_DISCONN | SLP_NEEDQ)) != 0)) {
2300 1.14 mycroft nfsrv_wakenfsd(slp);
2301 1.121 yamt }
2302 1.121 yamt }
2303 1.121 yamt
2304 1.121 yamt int
2305 1.146 thorpej nfsdsock_lock(struct nfssvc_sock *slp, bool waitok)
2306 1.121 yamt {
2307 1.121 yamt
2308 1.121 yamt simple_lock(&slp->ns_lock);
2309 1.121 yamt while ((slp->ns_flag & (SLP_BUSY|SLP_VALID)) == SLP_BUSY) {
2310 1.121 yamt if (!waitok) {
2311 1.121 yamt simple_unlock(&slp->ns_lock);
2312 1.121 yamt return EWOULDBLOCK;
2313 1.121 yamt }
2314 1.121 yamt slp->ns_flag |= SLP_WANT;
2315 1.121 yamt ltsleep(&slp->ns_flag, PSOCK, "nslock", 0, &slp->ns_lock);
2316 1.121 yamt }
2317 1.121 yamt if ((slp->ns_flag & SLP_VALID) == 0) {
2318 1.121 yamt simple_unlock(&slp->ns_lock);
2319 1.121 yamt return EINVAL;
2320 1.121 yamt }
2321 1.121 yamt slp->ns_flag |= SLP_BUSY;
2322 1.121 yamt simple_unlock(&slp->ns_lock);
2323 1.121 yamt
2324 1.121 yamt return 0;
2325 1.121 yamt }
2326 1.121 yamt
2327 1.121 yamt void
2328 1.121 yamt nfsdsock_unlock(struct nfssvc_sock *slp)
2329 1.121 yamt {
2330 1.121 yamt
2331 1.121 yamt KASSERT((slp->ns_flag & SLP_BUSY) != 0);
2332 1.121 yamt
2333 1.121 yamt simple_lock(&slp->ns_lock);
2334 1.121 yamt if ((slp->ns_flag & SLP_WANT) != 0) {
2335 1.121 yamt wakeup(&slp->ns_flag);
2336 1.121 yamt }
2337 1.121 yamt slp->ns_flag &= ~(SLP_BUSY|SLP_WANT);
2338 1.121 yamt simple_unlock(&slp->ns_lock);
2339 1.121 yamt }
2340 1.121 yamt
2341 1.121 yamt int
2342 1.121 yamt nfsdsock_drain(struct nfssvc_sock *slp)
2343 1.121 yamt {
2344 1.121 yamt int error = 0;
2345 1.121 yamt
2346 1.121 yamt simple_lock(&slp->ns_lock);
2347 1.121 yamt if ((slp->ns_flag & SLP_VALID) == 0) {
2348 1.121 yamt error = EINVAL;
2349 1.121 yamt goto done;
2350 1.121 yamt }
2351 1.121 yamt slp->ns_flag &= ~SLP_VALID;
2352 1.121 yamt while ((slp->ns_flag & SLP_BUSY) != 0) {
2353 1.121 yamt slp->ns_flag |= SLP_WANT;
2354 1.121 yamt ltsleep(&slp->ns_flag, PSOCK, "nsdrain", 0, &slp->ns_lock);
2355 1.121 yamt }
2356 1.121 yamt done:
2357 1.121 yamt simple_unlock(&slp->ns_lock);
2358 1.121 yamt
2359 1.121 yamt return error;
2360 1.1 cgd }
2361 1.1 cgd
2362 1.1 cgd /*
2363 1.14 mycroft * Try and extract an RPC request from the mbuf data list received on a
2364 1.14 mycroft * stream socket. The "waitflag" argument indicates whether or not it
2365 1.14 mycroft * can sleep.
2366 1.14 mycroft */
2367 1.23 christos int
2368 1.14 mycroft nfsrv_getstream(slp, waitflag)
2369 1.55 augustss struct nfssvc_sock *slp;
2370 1.14 mycroft int waitflag;
2371 1.1 cgd {
2372 1.55 augustss struct mbuf *m, **mpp;
2373 1.81 yamt struct mbuf *recm;
2374 1.24 fvdl u_int32_t recmark;
2375 1.121 yamt int error = 0;
2376 1.1 cgd
2377 1.14 mycroft for (;;) {
2378 1.82 yamt if (slp->ns_reclen == 0) {
2379 1.82 yamt if (slp->ns_cc < NFSX_UNSIGNED) {
2380 1.121 yamt break;
2381 1.82 yamt }
2382 1.82 yamt m = slp->ns_raw;
2383 1.148 christos m_copydata(m, 0, NFSX_UNSIGNED, (void *)&recmark);
2384 1.82 yamt m_adj(m, NFSX_UNSIGNED);
2385 1.82 yamt slp->ns_cc -= NFSX_UNSIGNED;
2386 1.82 yamt recmark = ntohl(recmark);
2387 1.82 yamt slp->ns_reclen = recmark & ~0x80000000;
2388 1.82 yamt if (recmark & 0x80000000)
2389 1.82 yamt slp->ns_flag |= SLP_LASTFRAG;
2390 1.82 yamt else
2391 1.82 yamt slp->ns_flag &= ~SLP_LASTFRAG;
2392 1.126 rpaulo if (slp->ns_reclen > NFS_MAXPACKET) {
2393 1.121 yamt error = EPERM;
2394 1.121 yamt break;
2395 1.82 yamt }
2396 1.82 yamt }
2397 1.82 yamt
2398 1.82 yamt /*
2399 1.82 yamt * Now get the record part.
2400 1.82 yamt *
2401 1.82 yamt * Note that slp->ns_reclen may be 0. Linux sometimes
2402 1.82 yamt * generates 0-length records.
2403 1.82 yamt */
2404 1.82 yamt if (slp->ns_cc == slp->ns_reclen) {
2405 1.82 yamt recm = slp->ns_raw;
2406 1.82 yamt slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2407 1.82 yamt slp->ns_cc = slp->ns_reclen = 0;
2408 1.82 yamt } else if (slp->ns_cc > slp->ns_reclen) {
2409 1.82 yamt recm = slp->ns_raw;
2410 1.82 yamt m = m_split(recm, slp->ns_reclen, waitflag);
2411 1.82 yamt if (m == NULL) {
2412 1.121 yamt error = EWOULDBLOCK;
2413 1.121 yamt break;
2414 1.82 yamt }
2415 1.108 jonathan m_claimm(recm, &nfs_mowner);
2416 1.82 yamt slp->ns_raw = m;
2417 1.82 yamt if (m->m_next == NULL)
2418 1.82 yamt slp->ns_rawend = m;
2419 1.82 yamt slp->ns_cc -= slp->ns_reclen;
2420 1.82 yamt slp->ns_reclen = 0;
2421 1.82 yamt } else {
2422 1.121 yamt break;
2423 1.14 mycroft }
2424 1.14 mycroft
2425 1.82 yamt /*
2426 1.82 yamt * Accumulate the fragments into a record.
2427 1.82 yamt */
2428 1.82 yamt mpp = &slp->ns_frag;
2429 1.82 yamt while (*mpp)
2430 1.82 yamt mpp = &((*mpp)->m_next);
2431 1.82 yamt *mpp = recm;
2432 1.82 yamt if (slp->ns_flag & SLP_LASTFRAG) {
2433 1.82 yamt if (slp->ns_recend)
2434 1.82 yamt slp->ns_recend->m_nextpkt = slp->ns_frag;
2435 1.82 yamt else
2436 1.82 yamt slp->ns_rec = slp->ns_frag;
2437 1.82 yamt slp->ns_recend = slp->ns_frag;
2438 1.82 yamt slp->ns_frag = (struct mbuf *)0;
2439 1.14 mycroft }
2440 1.1 cgd }
2441 1.121 yamt
2442 1.121 yamt return error;
2443 1.1 cgd }
2444 1.1 cgd
2445 1.1 cgd /*
2446 1.14 mycroft * Parse an RPC header.
2447 1.14 mycroft */
2448 1.23 christos int
2449 1.24 fvdl nfsrv_dorec(slp, nfsd, ndp)
2450 1.55 augustss struct nfssvc_sock *slp;
2451 1.24 fvdl struct nfsd *nfsd;
2452 1.24 fvdl struct nfsrv_descript **ndp;
2453 1.14 mycroft {
2454 1.55 augustss struct mbuf *m, *nam;
2455 1.55 augustss struct nfsrv_descript *nd;
2456 1.14 mycroft int error;
2457 1.1 cgd
2458 1.24 fvdl *ndp = NULL;
2459 1.121 yamt
2460 1.147 thorpej if (nfsdsock_lock(slp, true)) {
2461 1.121 yamt return ENOBUFS;
2462 1.121 yamt }
2463 1.121 yamt m = slp->ns_rec;
2464 1.121 yamt if (m == NULL) {
2465 1.121 yamt nfsdsock_unlock(slp);
2466 1.121 yamt return ENOBUFS;
2467 1.121 yamt }
2468 1.24 fvdl slp->ns_rec = m->m_nextpkt;
2469 1.24 fvdl if (slp->ns_rec)
2470 1.121 yamt m->m_nextpkt = NULL;
2471 1.14 mycroft else
2472 1.121 yamt slp->ns_recend = NULL;
2473 1.121 yamt nfsdsock_unlock(slp);
2474 1.121 yamt
2475 1.14 mycroft if (m->m_type == MT_SONAME) {
2476 1.24 fvdl nam = m;
2477 1.24 fvdl m = m->m_next;
2478 1.24 fvdl nam->m_next = NULL;
2479 1.24 fvdl } else
2480 1.24 fvdl nam = NULL;
2481 1.131 yamt nd = nfsdreq_alloc();
2482 1.24 fvdl nd->nd_md = nd->nd_mrep = m;
2483 1.24 fvdl nd->nd_nam2 = nam;
2484 1.148 christos nd->nd_dpos = mtod(m, void *);
2485 1.147 thorpej error = nfs_getreq(nd, nfsd, true);
2486 1.24 fvdl if (error) {
2487 1.24 fvdl m_freem(nam);
2488 1.131 yamt nfsdreq_free(nd);
2489 1.14 mycroft return (error);
2490 1.14 mycroft }
2491 1.24 fvdl *ndp = nd;
2492 1.24 fvdl nfsd->nfsd_nd = nd;
2493 1.1 cgd return (0);
2494 1.1 cgd }
2495 1.1 cgd
2496 1.1 cgd /*
2497 1.14 mycroft * Search for a sleeping nfsd and wake it up.
2498 1.14 mycroft * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2499 1.14 mycroft * running nfsds will go look for the work in the nfssvc_sock list.
2500 1.14 mycroft */
2501 1.14 mycroft void
2502 1.14 mycroft nfsrv_wakenfsd(slp)
2503 1.14 mycroft struct nfssvc_sock *slp;
2504 1.14 mycroft {
2505 1.55 augustss struct nfsd *nd;
2506 1.14 mycroft
2507 1.14 mycroft if ((slp->ns_flag & SLP_VALID) == 0)
2508 1.14 mycroft return;
2509 1.90 yamt simple_lock(&nfsd_slock);
2510 1.90 yamt if (slp->ns_flag & SLP_DOREC) {
2511 1.90 yamt simple_unlock(&nfsd_slock);
2512 1.90 yamt return;
2513 1.90 yamt }
2514 1.90 yamt nd = SLIST_FIRST(&nfsd_idle_head);
2515 1.90 yamt if (nd) {
2516 1.90 yamt SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle);
2517 1.90 yamt simple_unlock(&nfsd_slock);
2518 1.90 yamt
2519 1.90 yamt if (nd->nfsd_slp)
2520 1.90 yamt panic("nfsd wakeup");
2521 1.90 yamt slp->ns_sref++;
2522 1.90 yamt nd->nfsd_slp = slp;
2523 1.90 yamt wakeup(nd);
2524 1.90 yamt return;
2525 1.14 mycroft }
2526 1.14 mycroft slp->ns_flag |= SLP_DOREC;
2527 1.17 mycroft nfsd_head_flag |= NFSD_CHECKSLP;
2528 1.90 yamt TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending);
2529 1.90 yamt simple_unlock(&nfsd_slock);
2530 1.1 cgd }
2531 1.122 yamt
2532 1.122 yamt int
2533 1.122 yamt nfsdsock_sendreply(struct nfssvc_sock *slp, struct nfsrv_descript *nd)
2534 1.122 yamt {
2535 1.122 yamt int error;
2536 1.122 yamt
2537 1.122 yamt if (nd->nd_mrep != NULL) {
2538 1.122 yamt m_freem(nd->nd_mrep);
2539 1.122 yamt nd->nd_mrep = NULL;
2540 1.122 yamt }
2541 1.122 yamt
2542 1.122 yamt simple_lock(&slp->ns_lock);
2543 1.122 yamt if ((slp->ns_flag & SLP_SENDING) != 0) {
2544 1.122 yamt SIMPLEQ_INSERT_TAIL(&slp->ns_sendq, nd, nd_sendq);
2545 1.122 yamt simple_unlock(&slp->ns_lock);
2546 1.122 yamt return 0;
2547 1.122 yamt }
2548 1.122 yamt KASSERT(SIMPLEQ_EMPTY(&slp->ns_sendq));
2549 1.122 yamt slp->ns_flag |= SLP_SENDING;
2550 1.122 yamt simple_unlock(&slp->ns_lock);
2551 1.122 yamt
2552 1.122 yamt again:
2553 1.122 yamt error = nfs_send(slp->ns_so, nd->nd_nam2, nd->nd_mreq, NULL, curlwp);
2554 1.122 yamt if (nd->nd_nam2) {
2555 1.122 yamt m_free(nd->nd_nam2);
2556 1.122 yamt }
2557 1.131 yamt nfsdreq_free(nd);
2558 1.122 yamt
2559 1.122 yamt simple_lock(&slp->ns_lock);
2560 1.134 blymn KASSERT((slp->ns_flag & SLP_SENDING) != 0);
2561 1.122 yamt nd = SIMPLEQ_FIRST(&slp->ns_sendq);
2562 1.122 yamt if (nd != NULL) {
2563 1.122 yamt SIMPLEQ_REMOVE_HEAD(&slp->ns_sendq, nd_sendq);
2564 1.122 yamt simple_unlock(&slp->ns_lock);
2565 1.122 yamt goto again;
2566 1.122 yamt }
2567 1.134 blymn slp->ns_flag &= ~SLP_SENDING;
2568 1.122 yamt simple_unlock(&slp->ns_lock);
2569 1.122 yamt
2570 1.122 yamt return error;
2571 1.122 yamt }
2572 1.14 mycroft #endif /* NFSSERVER */
2573 1.132 yamt
2574 1.132 yamt #if defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY))
2575 1.132 yamt static struct pool nfs_srvdesc_pool;
2576 1.132 yamt
2577 1.132 yamt void
2578 1.132 yamt nfsdreq_init(void)
2579 1.132 yamt {
2580 1.132 yamt
2581 1.132 yamt pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
2582 1.149 ad 0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr, IPL_NONE);
2583 1.132 yamt }
2584 1.132 yamt
2585 1.132 yamt struct nfsrv_descript *
2586 1.132 yamt nfsdreq_alloc(void)
2587 1.132 yamt {
2588 1.132 yamt struct nfsrv_descript *nd;
2589 1.132 yamt
2590 1.132 yamt nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
2591 1.132 yamt nd->nd_cr = NULL;
2592 1.132 yamt return nd;
2593 1.132 yamt }
2594 1.132 yamt
2595 1.132 yamt void
2596 1.132 yamt nfsdreq_free(struct nfsrv_descript *nd)
2597 1.132 yamt {
2598 1.132 yamt kauth_cred_t cr;
2599 1.132 yamt
2600 1.132 yamt cr = nd->nd_cr;
2601 1.132 yamt if (cr != NULL) {
2602 1.132 yamt kauth_cred_free(cr);
2603 1.132 yamt }
2604 1.132 yamt pool_put(&nfs_srvdesc_pool, nd);
2605 1.132 yamt }
2606 1.132 yamt #endif /* defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY)) */
2607