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