Mon May 4 10:48:39 2009 UTC ()
fix a merge botch.


(yamt)
diff -r1.170.2.3 -r1.170.2.4 src/sys/nfs/nfs_socket.c

cvs diff -r1.170.2.3 -r1.170.2.4 src/sys/nfs/nfs_socket.c (switch to unified diff)

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