nfs_syscalls.c revision 1.41 1 1.41 fvdl /* $NetBSD: nfs_syscalls.c,v 1.41 2000/06/09 00:00:18 fvdl Exp $ */
2 1.11 cgd
3 1.1 cgd /*
4 1.10 mycroft * Copyright (c) 1989, 1993
5 1.10 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.19 fvdl * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
39 1.1 cgd */
40 1.28 thorpej
41 1.28 thorpej #include "fs_nfs.h"
42 1.30 thorpej #include "opt_nfsserver.h"
43 1.31 jonathan #include "opt_iso.h"
44 1.41 fvdl #include "opt_inet.h"
45 1.39 tsarna #include "opt_compat_netbsd.h"
46 1.1 cgd
47 1.7 mycroft #include <sys/param.h>
48 1.7 mycroft #include <sys/systm.h>
49 1.7 mycroft #include <sys/kernel.h>
50 1.7 mycroft #include <sys/file.h>
51 1.7 mycroft #include <sys/stat.h>
52 1.7 mycroft #include <sys/vnode.h>
53 1.7 mycroft #include <sys/mount.h>
54 1.7 mycroft #include <sys/proc.h>
55 1.10 mycroft #include <sys/uio.h>
56 1.7 mycroft #include <sys/malloc.h>
57 1.7 mycroft #include <sys/buf.h>
58 1.7 mycroft #include <sys/mbuf.h>
59 1.7 mycroft #include <sys/socket.h>
60 1.7 mycroft #include <sys/socketvar.h>
61 1.39 tsarna #include <sys/signalvar.h>
62 1.7 mycroft #include <sys/domain.h>
63 1.7 mycroft #include <sys/protosw.h>
64 1.10 mycroft #include <sys/namei.h>
65 1.10 mycroft #include <sys/syslog.h>
66 1.18 christos #include <sys/filedesc.h>
67 1.39 tsarna #include <sys/kthread.h>
68 1.1 cgd
69 1.13 cgd #include <sys/syscallargs.h>
70 1.13 cgd
71 1.7 mycroft #include <netinet/in.h>
72 1.7 mycroft #include <netinet/tcp.h>
73 1.10 mycroft #ifdef ISO
74 1.10 mycroft #include <netiso/iso.h>
75 1.10 mycroft #endif
76 1.19 fvdl #include <nfs/xdr_subs.h>
77 1.10 mycroft #include <nfs/rpcv2.h>
78 1.19 fvdl #include <nfs/nfsproto.h>
79 1.7 mycroft #include <nfs/nfs.h>
80 1.19 fvdl #include <nfs/nfsm_subs.h>
81 1.7 mycroft #include <nfs/nfsrvcache.h>
82 1.10 mycroft #include <nfs/nfsmount.h>
83 1.10 mycroft #include <nfs/nfsnode.h>
84 1.10 mycroft #include <nfs/nqnfs.h>
85 1.10 mycroft #include <nfs/nfsrtt.h>
86 1.18 christos #include <nfs/nfs_var.h>
87 1.1 cgd
88 1.1 cgd /* Global defs. */
89 1.19 fvdl extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
90 1.19 fvdl struct nfssvc_sock *,
91 1.19 fvdl struct proc *, struct mbuf **));
92 1.1 cgd extern int nfs_numasync;
93 1.10 mycroft extern time_t nqnfsstarttime;
94 1.10 mycroft extern int nqsrv_writeslack;
95 1.10 mycroft extern int nfsrtton;
96 1.19 fvdl extern struct nfsstats nfsstats;
97 1.19 fvdl extern int nfsrvw_procrastinate;
98 1.10 mycroft struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock;
99 1.41 fvdl #ifdef INET6
100 1.41 fvdl struct nfssvc_sock *nfs_udp6sock;
101 1.41 fvdl #endif
102 1.10 mycroft int nuidhash_max = NFS_MAXUIDHASH;
103 1.18 christos int nfsd_waiting = 0;
104 1.18 christos #ifdef NFSSERVER
105 1.10 mycroft static int nfs_numnfsd = 0;
106 1.10 mycroft static int notstarted = 1;
107 1.10 mycroft static int modify_flag = 0;
108 1.10 mycroft static struct nfsdrt nfsdrt;
109 1.18 christos #endif
110 1.1 cgd
111 1.1 cgd #define TRUE 1
112 1.1 cgd #define FALSE 0
113 1.1 cgd
114 1.24 thorpej #ifdef NFS
115 1.39 tsarna static struct proc *nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
116 1.40 tsarna int nfs_niothreads = -1; /* == "0, and has never been set" */
117 1.23 thorpej #endif
118 1.19 fvdl
119 1.18 christos #ifdef NFSSERVER
120 1.19 fvdl static void nfsd_rt __P((int, struct nfsrv_descript *, int));
121 1.18 christos #endif
122 1.19 fvdl
123 1.1 cgd /*
124 1.1 cgd * NFS server system calls
125 1.1 cgd */
126 1.1 cgd
127 1.1 cgd
128 1.1 cgd /*
129 1.19 fvdl * Nfs server pseudo system call for the nfsd's
130 1.10 mycroft * Based on the flag value it either:
131 1.10 mycroft * - adds a socket to the selection list
132 1.10 mycroft * - remains in the kernel as an nfsd
133 1.10 mycroft * - remains in the kernel as an nfsiod
134 1.1 cgd */
135 1.18 christos int
136 1.16 mycroft sys_nfssvc(p, v, retval)
137 1.1 cgd struct proc *p;
138 1.15 thorpej void *v;
139 1.15 thorpej register_t *retval;
140 1.15 thorpej {
141 1.38 augustss struct sys_nfssvc_args /* {
142 1.13 cgd syscallarg(int) flag;
143 1.13 cgd syscallarg(caddr_t) argp;
144 1.15 thorpej } */ *uap = v;
145 1.23 thorpej int error;
146 1.24 thorpej #ifdef NFS
147 1.10 mycroft struct nameidata nd;
148 1.18 christos struct nfsmount *nmp;
149 1.19 fvdl struct nfsd_cargs ncd;
150 1.19 fvdl #endif
151 1.18 christos #ifdef NFSSERVER
152 1.1 cgd struct file *fp;
153 1.10 mycroft struct mbuf *nam;
154 1.10 mycroft struct nfsd_args nfsdarg;
155 1.10 mycroft struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
156 1.10 mycroft struct nfsd *nfsd;
157 1.10 mycroft struct nfssvc_sock *slp;
158 1.18 christos struct nfsuid *nuidp;
159 1.18 christos #endif
160 1.1 cgd
161 1.1 cgd /*
162 1.1 cgd * Must be super user
163 1.1 cgd */
164 1.19 fvdl error = suser(p->p_ucred, &p->p_acflag);
165 1.19 fvdl if(error)
166 1.1 cgd return (error);
167 1.12 mycroft while (nfssvc_sockhead_flag & SLP_INIT) {
168 1.19 fvdl nfssvc_sockhead_flag |= SLP_WANTINIT;
169 1.10 mycroft (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
170 1.10 mycroft }
171 1.13 cgd if (SCARG(uap, flag) & NFSSVC_BIOD) {
172 1.39 tsarna #if defined(NFS) && defined(COMPAT_14)
173 1.19 fvdl error = nfssvc_iod(p);
174 1.19 fvdl #else
175 1.10 mycroft error = ENOSYS;
176 1.19 fvdl #endif
177 1.13 cgd } else if (SCARG(uap, flag) & NFSSVC_MNTD) {
178 1.24 thorpej #ifndef NFS
179 1.10 mycroft error = ENOSYS;
180 1.19 fvdl #else
181 1.18 christos error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd));
182 1.18 christos if (error)
183 1.10 mycroft return (error);
184 1.10 mycroft NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
185 1.10 mycroft ncd.ncd_dirp, p);
186 1.19 fvdl error = namei(&nd);
187 1.19 fvdl if (error)
188 1.10 mycroft return (error);
189 1.10 mycroft if ((nd.ni_vp->v_flag & VROOT) == 0)
190 1.10 mycroft error = EINVAL;
191 1.10 mycroft nmp = VFSTONFS(nd.ni_vp->v_mount);
192 1.10 mycroft vput(nd.ni_vp);
193 1.10 mycroft if (error)
194 1.10 mycroft return (error);
195 1.27 fvdl if ((nmp->nm_iflag & NFSMNT_MNTD) &&
196 1.19 fvdl (SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0)
197 1.10 mycroft return (0);
198 1.27 fvdl nmp->nm_iflag |= NFSMNT_MNTD;
199 1.13 cgd error = nqnfs_clientd(nmp, p->p_ucred, &ncd, SCARG(uap, flag),
200 1.19 fvdl SCARG(uap, argp), p);
201 1.24 thorpej #endif /* NFS */
202 1.13 cgd } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
203 1.10 mycroft #ifndef NFSSERVER
204 1.10 mycroft error = ENOSYS;
205 1.19 fvdl #else
206 1.18 christos error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg,
207 1.19 fvdl sizeof(nfsdarg));
208 1.18 christos if (error)
209 1.10 mycroft return (error);
210 1.35 thorpej /* getsock() will use the descriptor for us */
211 1.19 fvdl error = getsock(p->p_fd, nfsdarg.sock, &fp);
212 1.19 fvdl if (error)
213 1.10 mycroft return (error);
214 1.10 mycroft /*
215 1.10 mycroft * Get the client address for connected sockets.
216 1.10 mycroft */
217 1.10 mycroft if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
218 1.10 mycroft nam = (struct mbuf *)0;
219 1.18 christos else {
220 1.18 christos error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
221 1.19 fvdl MT_SONAME);
222 1.35 thorpej if (error) {
223 1.35 thorpej FILE_UNUSE(fp, NULL);
224 1.18 christos return (error);
225 1.35 thorpej }
226 1.18 christos }
227 1.10 mycroft error = nfssvc_addsock(fp, nam);
228 1.35 thorpej FILE_UNUSE(fp, NULL);
229 1.10 mycroft #endif /* !NFSSERVER */
230 1.10 mycroft } else {
231 1.10 mycroft #ifndef NFSSERVER
232 1.10 mycroft error = ENOSYS;
233 1.19 fvdl #else
234 1.18 christos error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd));
235 1.18 christos if (error)
236 1.10 mycroft return (error);
237 1.13 cgd if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
238 1.19 fvdl ((nfsd = nsd->nsd_nfsd)) != NULL &&
239 1.19 fvdl (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
240 1.19 fvdl slp = nfsd->nfsd_slp;
241 1.10 mycroft
242 1.10 mycroft /*
243 1.10 mycroft * First check to see if another nfsd has already
244 1.10 mycroft * added this credential.
245 1.10 mycroft */
246 1.19 fvdl for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
247 1.12 mycroft nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
248 1.19 fvdl if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
249 1.19 fvdl (!nfsd->nfsd_nd->nd_nam2 ||
250 1.19 fvdl netaddr_match(NU_NETFAM(nuidp),
251 1.19 fvdl &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
252 1.10 mycroft break;
253 1.10 mycroft }
254 1.19 fvdl if (nuidp) {
255 1.19 fvdl nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
256 1.19 fvdl nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
257 1.19 fvdl } else {
258 1.10 mycroft /*
259 1.10 mycroft * Nope, so we will.
260 1.10 mycroft */
261 1.10 mycroft if (slp->ns_numuids < nuidhash_max) {
262 1.10 mycroft slp->ns_numuids++;
263 1.10 mycroft nuidp = (struct nfsuid *)
264 1.10 mycroft malloc(sizeof (struct nfsuid), M_NFSUID,
265 1.10 mycroft M_WAITOK);
266 1.10 mycroft } else
267 1.10 mycroft nuidp = (struct nfsuid *)0;
268 1.10 mycroft if ((slp->ns_flag & SLP_VALID) == 0) {
269 1.10 mycroft if (nuidp)
270 1.10 mycroft free((caddr_t)nuidp, M_NFSUID);
271 1.10 mycroft } else {
272 1.10 mycroft if (nuidp == (struct nfsuid *)0) {
273 1.12 mycroft nuidp = slp->ns_uidlruhead.tqh_first;
274 1.12 mycroft LIST_REMOVE(nuidp, nu_hash);
275 1.12 mycroft TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
276 1.12 mycroft nu_lru);
277 1.19 fvdl if (nuidp->nu_flag & NU_NAM)
278 1.19 fvdl m_freem(nuidp->nu_nam);
279 1.10 mycroft }
280 1.19 fvdl nuidp->nu_flag = 0;
281 1.10 mycroft nuidp->nu_cr = nsd->nsd_cr;
282 1.10 mycroft if (nuidp->nu_cr.cr_ngroups > NGROUPS)
283 1.19 fvdl nuidp->nu_cr.cr_ngroups = NGROUPS;
284 1.10 mycroft nuidp->nu_cr.cr_ref = 1;
285 1.19 fvdl nuidp->nu_timestamp = nsd->nsd_timestamp;
286 1.19 fvdl nuidp->nu_expire = time.tv_sec + nsd->nsd_ttl;
287 1.19 fvdl /*
288 1.19 fvdl * and save the session key in nu_key.
289 1.19 fvdl */
290 1.32 perry memcpy(nuidp->nu_key, nsd->nsd_key,
291 1.32 perry sizeof(nsd->nsd_key));
292 1.19 fvdl if (nfsd->nfsd_nd->nd_nam2) {
293 1.19 fvdl struct sockaddr_in *saddr;
294 1.19 fvdl
295 1.19 fvdl saddr = mtod(nfsd->nfsd_nd->nd_nam2,
296 1.19 fvdl struct sockaddr_in *);
297 1.19 fvdl switch (saddr->sin_family) {
298 1.19 fvdl case AF_INET:
299 1.19 fvdl nuidp->nu_flag |= NU_INETADDR;
300 1.19 fvdl nuidp->nu_inetaddr =
301 1.19 fvdl saddr->sin_addr.s_addr;
302 1.19 fvdl break;
303 1.19 fvdl case AF_ISO:
304 1.19 fvdl default:
305 1.19 fvdl nuidp->nu_flag |= NU_NAM;
306 1.19 fvdl nuidp->nu_nam = m_copym(
307 1.19 fvdl nfsd->nfsd_nd->nd_nam2, 0,
308 1.19 fvdl M_COPYALL, M_WAIT);
309 1.19 fvdl break;
310 1.19 fvdl };
311 1.19 fvdl }
312 1.12 mycroft TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
313 1.19 fvdl nu_lru);
314 1.12 mycroft LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
315 1.19 fvdl nuidp, nu_hash);
316 1.19 fvdl nfsrv_setcred(&nuidp->nu_cr,
317 1.19 fvdl &nfsd->nfsd_nd->nd_cr);
318 1.19 fvdl nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
319 1.10 mycroft }
320 1.10 mycroft }
321 1.10 mycroft }
322 1.13 cgd if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
323 1.13 cgd (nfsd = nsd->nsd_nfsd))
324 1.19 fvdl nfsd->nfsd_flag |= NFSD_AUTHFAIL;
325 1.13 cgd error = nfssvc_nfsd(nsd, SCARG(uap, argp), p);
326 1.10 mycroft #endif /* !NFSSERVER */
327 1.10 mycroft }
328 1.10 mycroft if (error == EINTR || error == ERESTART)
329 1.10 mycroft error = 0;
330 1.10 mycroft return (error);
331 1.10 mycroft }
332 1.10 mycroft
333 1.10 mycroft #ifdef NFSSERVER
334 1.10 mycroft /*
335 1.10 mycroft * Adds a socket to the list for servicing by nfsds.
336 1.10 mycroft */
337 1.18 christos int
338 1.10 mycroft nfssvc_addsock(fp, mynam)
339 1.10 mycroft struct file *fp;
340 1.10 mycroft struct mbuf *mynam;
341 1.10 mycroft {
342 1.38 augustss struct mbuf *m;
343 1.38 augustss int siz;
344 1.38 augustss struct nfssvc_sock *slp;
345 1.38 augustss struct socket *so;
346 1.10 mycroft struct nfssvc_sock *tslp;
347 1.10 mycroft int error, s;
348 1.10 mycroft
349 1.1 cgd so = (struct socket *)fp->f_data;
350 1.10 mycroft tslp = (struct nfssvc_sock *)0;
351 1.10 mycroft /*
352 1.10 mycroft * Add it to the list, as required.
353 1.10 mycroft */
354 1.10 mycroft if (so->so_proto->pr_protocol == IPPROTO_UDP) {
355 1.41 fvdl #ifdef INET6
356 1.41 fvdl if (so->so_proto->pr_domain->dom_family == AF_INET6)
357 1.41 fvdl tslp = nfs_udp6sock;
358 1.41 fvdl else
359 1.41 fvdl #endif
360 1.10 mycroft tslp = nfs_udpsock;
361 1.10 mycroft if (tslp->ns_flag & SLP_VALID) {
362 1.10 mycroft m_freem(mynam);
363 1.10 mycroft return (EPERM);
364 1.10 mycroft }
365 1.10 mycroft #ifdef ISO
366 1.10 mycroft } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
367 1.10 mycroft tslp = nfs_cltpsock;
368 1.10 mycroft if (tslp->ns_flag & SLP_VALID) {
369 1.10 mycroft m_freem(mynam);
370 1.10 mycroft return (EPERM);
371 1.10 mycroft }
372 1.10 mycroft #endif /* ISO */
373 1.10 mycroft }
374 1.10 mycroft if (so->so_type == SOCK_STREAM)
375 1.19 fvdl siz = NFS_MAXPACKET + sizeof (u_long);
376 1.10 mycroft else
377 1.1 cgd siz = NFS_MAXPACKET;
378 1.19 fvdl error = soreserve(so, siz, siz);
379 1.19 fvdl if (error) {
380 1.10 mycroft m_freem(mynam);
381 1.10 mycroft return (error);
382 1.10 mycroft }
383 1.1 cgd
384 1.1 cgd /*
385 1.1 cgd * Set protocol specific options { for now TCP only } and
386 1.1 cgd * reserve some space. For datagram sockets, this can get called
387 1.1 cgd * repeatedly for the same socket, but that isn't harmful.
388 1.1 cgd */
389 1.10 mycroft if (so->so_type == SOCK_STREAM) {
390 1.1 cgd MGET(m, M_WAIT, MT_SOOPTS);
391 1.19 fvdl *mtod(m, int32_t *) = 1;
392 1.19 fvdl m->m_len = sizeof(int32_t);
393 1.1 cgd sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
394 1.1 cgd }
395 1.41 fvdl if ((so->so_proto->pr_domain->dom_family == AF_INET
396 1.41 fvdl #ifdef INET6
397 1.41 fvdl || so->so_proto->pr_domain->dom_family == AF_INET6
398 1.41 fvdl #endif
399 1.41 fvdl ) &&
400 1.10 mycroft so->so_proto->pr_protocol == IPPROTO_TCP) {
401 1.1 cgd MGET(m, M_WAIT, MT_SOOPTS);
402 1.19 fvdl *mtod(m, int32_t *) = 1;
403 1.19 fvdl m->m_len = sizeof(int32_t);
404 1.1 cgd sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
405 1.1 cgd }
406 1.1 cgd so->so_rcv.sb_flags &= ~SB_NOINTR;
407 1.1 cgd so->so_rcv.sb_timeo = 0;
408 1.1 cgd so->so_snd.sb_flags &= ~SB_NOINTR;
409 1.1 cgd so->so_snd.sb_timeo = 0;
410 1.10 mycroft if (tslp)
411 1.10 mycroft slp = tslp;
412 1.10 mycroft else {
413 1.10 mycroft slp = (struct nfssvc_sock *)
414 1.10 mycroft malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
415 1.32 perry memset((caddr_t)slp, 0, sizeof (struct nfssvc_sock));
416 1.12 mycroft TAILQ_INIT(&slp->ns_uidlruhead);
417 1.12 mycroft TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
418 1.10 mycroft }
419 1.10 mycroft slp->ns_so = so;
420 1.10 mycroft slp->ns_nam = mynam;
421 1.10 mycroft fp->f_count++;
422 1.10 mycroft slp->ns_fp = fp;
423 1.14 mycroft s = splsoftnet();
424 1.10 mycroft so->so_upcallarg = (caddr_t)slp;
425 1.10 mycroft so->so_upcall = nfsrv_rcv;
426 1.29 matt so->so_rcv.sb_flags |= SB_UPCALL;
427 1.10 mycroft slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
428 1.10 mycroft nfsrv_wakenfsd(slp);
429 1.10 mycroft splx(s);
430 1.10 mycroft return (0);
431 1.10 mycroft }
432 1.1 cgd
433 1.10 mycroft /*
434 1.10 mycroft * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
435 1.10 mycroft * until it is killed by a signal.
436 1.10 mycroft */
437 1.18 christos int
438 1.10 mycroft nfssvc_nfsd(nsd, argp, p)
439 1.10 mycroft struct nfsd_srvargs *nsd;
440 1.10 mycroft caddr_t argp;
441 1.10 mycroft struct proc *p;
442 1.10 mycroft {
443 1.38 augustss struct mbuf *m;
444 1.38 augustss int siz;
445 1.38 augustss struct nfssvc_sock *slp;
446 1.38 augustss struct socket *so;
447 1.38 augustss int *solockp;
448 1.19 fvdl struct nfsd *nfsd = nsd->nsd_nfsd;
449 1.19 fvdl struct nfsrv_descript *nd = NULL;
450 1.19 fvdl struct mbuf *mreq;
451 1.19 fvdl int error = 0, cacherep, s, sotype, writes_todo;
452 1.19 fvdl u_quad_t cur_usec;
453 1.19 fvdl
454 1.19 fvdl #ifndef nolint
455 1.19 fvdl cacherep = RC_DOIT;
456 1.19 fvdl writes_todo = 0;
457 1.19 fvdl #endif
458 1.14 mycroft s = splsoftnet();
459 1.19 fvdl if (nfsd == (struct nfsd *)0) {
460 1.19 fvdl nsd->nsd_nfsd = nfsd = (struct nfsd *)
461 1.10 mycroft malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
462 1.32 perry memset((caddr_t)nfsd, 0, sizeof (struct nfsd));
463 1.19 fvdl nfsd->nfsd_procp = p;
464 1.19 fvdl TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
465 1.10 mycroft nfs_numnfsd++;
466 1.10 mycroft }
467 1.33 mycroft p->p_holdcnt++;
468 1.1 cgd /*
469 1.10 mycroft * Loop getting rpc requests until SIGKILL.
470 1.1 cgd */
471 1.1 cgd for (;;) {
472 1.19 fvdl if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
473 1.19 fvdl while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
474 1.12 mycroft (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
475 1.19 fvdl nfsd->nfsd_flag |= NFSD_WAITING;
476 1.10 mycroft nfsd_waiting++;
477 1.19 fvdl error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
478 1.19 fvdl "nfsd", 0);
479 1.10 mycroft nfsd_waiting--;
480 1.10 mycroft if (error)
481 1.10 mycroft goto done;
482 1.10 mycroft }
483 1.19 fvdl if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
484 1.12 mycroft (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
485 1.12 mycroft for (slp = nfssvc_sockhead.tqh_first; slp != 0;
486 1.12 mycroft slp = slp->ns_chain.tqe_next) {
487 1.10 mycroft if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
488 1.10 mycroft == (SLP_VALID | SLP_DOREC)) {
489 1.10 mycroft slp->ns_flag &= ~SLP_DOREC;
490 1.10 mycroft slp->ns_sref++;
491 1.19 fvdl nfsd->nfsd_slp = slp;
492 1.10 mycroft break;
493 1.10 mycroft }
494 1.10 mycroft }
495 1.12 mycroft if (slp == 0)
496 1.12 mycroft nfsd_head_flag &= ~NFSD_CHECKSLP;
497 1.10 mycroft }
498 1.19 fvdl if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
499 1.10 mycroft continue;
500 1.10 mycroft if (slp->ns_flag & SLP_VALID) {
501 1.10 mycroft if (slp->ns_flag & SLP_DISCONN)
502 1.10 mycroft nfsrv_zapsock(slp);
503 1.10 mycroft else if (slp->ns_flag & SLP_NEEDQ) {
504 1.10 mycroft slp->ns_flag &= ~SLP_NEEDQ;
505 1.10 mycroft (void) nfs_sndlock(&slp->ns_solock,
506 1.10 mycroft (struct nfsreq *)0);
507 1.10 mycroft nfsrv_rcv(slp->ns_so, (caddr_t)slp,
508 1.10 mycroft M_WAIT);
509 1.10 mycroft nfs_sndunlock(&slp->ns_solock);
510 1.10 mycroft }
511 1.19 fvdl error = nfsrv_dorec(slp, nfsd, &nd);
512 1.19 fvdl cur_usec = (u_quad_t)time.tv_sec * 1000000 +
513 1.19 fvdl (u_quad_t)time.tv_usec;
514 1.19 fvdl if (error && slp->ns_tq.lh_first &&
515 1.19 fvdl slp->ns_tq.lh_first->nd_time <= cur_usec) {
516 1.19 fvdl error = 0;
517 1.19 fvdl cacherep = RC_DOIT;
518 1.19 fvdl writes_todo = 1;
519 1.19 fvdl } else
520 1.19 fvdl writes_todo = 0;
521 1.19 fvdl nfsd->nfsd_flag |= NFSD_REQINPROG;
522 1.1 cgd }
523 1.10 mycroft } else {
524 1.10 mycroft error = 0;
525 1.19 fvdl slp = nfsd->nfsd_slp;
526 1.10 mycroft }
527 1.10 mycroft if (error || (slp->ns_flag & SLP_VALID) == 0) {
528 1.19 fvdl if (nd) {
529 1.19 fvdl free((caddr_t)nd, M_NFSRVDESC);
530 1.19 fvdl nd = NULL;
531 1.19 fvdl }
532 1.19 fvdl nfsd->nfsd_slp = (struct nfssvc_sock *)0;
533 1.19 fvdl nfsd->nfsd_flag &= ~NFSD_REQINPROG;
534 1.10 mycroft nfsrv_slpderef(slp);
535 1.1 cgd continue;
536 1.1 cgd }
537 1.10 mycroft splx(s);
538 1.10 mycroft so = slp->ns_so;
539 1.10 mycroft sotype = so->so_type;
540 1.10 mycroft if (so->so_proto->pr_flags & PR_CONNREQUIRED)
541 1.10 mycroft solockp = &slp->ns_solock;
542 1.10 mycroft else
543 1.10 mycroft solockp = (int *)0;
544 1.19 fvdl if (nd) {
545 1.19 fvdl nd->nd_starttime = time;
546 1.19 fvdl if (nd->nd_nam2)
547 1.19 fvdl nd->nd_nam = nd->nd_nam2;
548 1.19 fvdl else
549 1.19 fvdl nd->nd_nam = slp->ns_nam;
550 1.19 fvdl
551 1.19 fvdl /*
552 1.19 fvdl * Check to see if authorization is needed.
553 1.19 fvdl */
554 1.19 fvdl if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
555 1.19 fvdl nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
556 1.19 fvdl nsd->nsd_haddr = mtod(nd->nd_nam,
557 1.19 fvdl struct sockaddr_in *)->sin_addr.s_addr;
558 1.19 fvdl nsd->nsd_authlen = nfsd->nfsd_authlen;
559 1.19 fvdl nsd->nsd_verflen = nfsd->nfsd_verflen;
560 1.19 fvdl if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
561 1.19 fvdl nfsd->nfsd_authlen) &&
562 1.19 fvdl !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
563 1.19 fvdl nfsd->nfsd_verflen) &&
564 1.33 mycroft !copyout((caddr_t)nsd, argp, sizeof (*nsd))) {
565 1.33 mycroft p->p_holdcnt--;
566 1.19 fvdl return (ENEEDAUTH);
567 1.33 mycroft }
568 1.19 fvdl cacherep = RC_DROPIT;
569 1.19 fvdl } else
570 1.19 fvdl cacherep = nfsrv_getcache(nd, slp, &mreq);
571 1.19 fvdl
572 1.19 fvdl /*
573 1.19 fvdl * Check for just starting up for NQNFS and send
574 1.19 fvdl * fake "try again later" replies to the NQNFS clients.
575 1.19 fvdl */
576 1.19 fvdl if (notstarted && nqnfsstarttime <= time.tv_sec) {
577 1.10 mycroft if (modify_flag) {
578 1.10 mycroft nqnfsstarttime = time.tv_sec + nqsrv_writeslack;
579 1.10 mycroft modify_flag = 0;
580 1.10 mycroft } else
581 1.10 mycroft notstarted = 0;
582 1.19 fvdl }
583 1.19 fvdl if (notstarted) {
584 1.19 fvdl if ((nd->nd_flag & ND_NQNFS) == 0)
585 1.10 mycroft cacherep = RC_DROPIT;
586 1.10 mycroft else if (nd->nd_procnum != NFSPROC_WRITE) {
587 1.10 mycroft nd->nd_procnum = NFSPROC_NOOP;
588 1.10 mycroft nd->nd_repstat = NQNFS_TRYLATER;
589 1.10 mycroft cacherep = RC_DOIT;
590 1.10 mycroft } else
591 1.10 mycroft modify_flag = 1;
592 1.19 fvdl } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
593 1.19 fvdl nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
594 1.10 mycroft nd->nd_procnum = NFSPROC_NOOP;
595 1.19 fvdl nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
596 1.1 cgd cacherep = RC_DOIT;
597 1.19 fvdl }
598 1.10 mycroft }
599 1.10 mycroft
600 1.19 fvdl /*
601 1.19 fvdl * Loop to get all the write rpc relies that have been
602 1.19 fvdl * gathered together.
603 1.19 fvdl */
604 1.19 fvdl do {
605 1.34 sommerfe #ifdef DIAGNOSTIC
606 1.34 sommerfe int lockcount;
607 1.34 sommerfe #endif
608 1.19 fvdl switch (cacherep) {
609 1.19 fvdl case RC_DOIT:
610 1.34 sommerfe #ifdef DIAGNOSTIC
611 1.34 sommerfe /*
612 1.34 sommerfe * NFS server procs should neither release
613 1.34 sommerfe * locks already held, nor leave things
614 1.34 sommerfe * locked. Catch this sooner, rather than
615 1.34 sommerfe * later (when we try to relock something we
616 1.34 sommerfe * already have locked). Careful inspection
617 1.34 sommerfe * of the failing routine usually turns up the
618 1.34 sommerfe * lock leak.. once we know what it is..
619 1.34 sommerfe */
620 1.34 sommerfe lockcount = p->p_locks;
621 1.34 sommerfe #endif
622 1.19 fvdl if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
623 1.19 fvdl nfsrvw_procrastinate > 0 && !notstarted))
624 1.19 fvdl error = nfsrv_writegather(&nd, slp,
625 1.19 fvdl nfsd->nfsd_procp, &mreq);
626 1.19 fvdl else
627 1.19 fvdl error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
628 1.19 fvdl slp, nfsd->nfsd_procp, &mreq);
629 1.34 sommerfe #ifdef DIAGNOSTIC
630 1.34 sommerfe if (p->p_locks != lockcount) {
631 1.34 sommerfe /*
632 1.34 sommerfe * If you see this panic, audit
633 1.34 sommerfe * nfsrv3_procs[nd->nd_procnum] for vnode
634 1.34 sommerfe * locking errors (usually, it's due to
635 1.34 sommerfe * forgetting to vput() something).
636 1.34 sommerfe */
637 1.34 sommerfe panic("nfsd: locking botch in op %d",
638 1.34 sommerfe nd ? nd->nd_procnum : -1);
639 1.34 sommerfe }
640 1.34 sommerfe #endif
641 1.19 fvdl if (mreq == NULL)
642 1.19 fvdl break;
643 1.10 mycroft if (error) {
644 1.10 mycroft if (nd->nd_procnum != NQNFSPROC_VACATED)
645 1.10 mycroft nfsstats.srv_errs++;
646 1.19 fvdl nfsrv_updatecache(nd, FALSE, mreq);
647 1.19 fvdl if (nd->nd_nam2)
648 1.19 fvdl m_freem(nd->nd_nam2);
649 1.1 cgd break;
650 1.1 cgd }
651 1.10 mycroft nfsstats.srvrpccnt[nd->nd_procnum]++;
652 1.19 fvdl nfsrv_updatecache(nd, TRUE, mreq);
653 1.10 mycroft nd->nd_mrep = (struct mbuf *)0;
654 1.19 fvdl case RC_REPLY:
655 1.1 cgd m = mreq;
656 1.1 cgd siz = 0;
657 1.1 cgd while (m) {
658 1.1 cgd siz += m->m_len;
659 1.1 cgd m = m->m_next;
660 1.1 cgd }
661 1.1 cgd if (siz <= 0 || siz > NFS_MAXPACKET) {
662 1.21 christos printf("mbuf siz=%d\n",siz);
663 1.1 cgd panic("Bad nfs svc reply");
664 1.1 cgd }
665 1.10 mycroft m = mreq;
666 1.10 mycroft m->m_pkthdr.len = siz;
667 1.10 mycroft m->m_pkthdr.rcvif = (struct ifnet *)0;
668 1.1 cgd /*
669 1.10 mycroft * For stream protocols, prepend a Sun RPC
670 1.1 cgd * Record Mark.
671 1.1 cgd */
672 1.10 mycroft if (sotype == SOCK_STREAM) {
673 1.10 mycroft M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
674 1.17 cgd *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
675 1.10 mycroft }
676 1.10 mycroft if (solockp)
677 1.10 mycroft (void) nfs_sndlock(solockp, (struct nfsreq *)0);
678 1.10 mycroft if (slp->ns_flag & SLP_VALID)
679 1.19 fvdl error = nfs_send(so, nd->nd_nam2, m, NULL);
680 1.10 mycroft else {
681 1.10 mycroft error = EPIPE;
682 1.10 mycroft m_freem(m);
683 1.10 mycroft }
684 1.10 mycroft if (nfsrtton)
685 1.19 fvdl nfsd_rt(sotype, nd, cacherep);
686 1.19 fvdl if (nd->nd_nam2)
687 1.19 fvdl MFREE(nd->nd_nam2, m);
688 1.10 mycroft if (nd->nd_mrep)
689 1.10 mycroft m_freem(nd->nd_mrep);
690 1.10 mycroft if (error == EPIPE)
691 1.10 mycroft nfsrv_zapsock(slp);
692 1.10 mycroft if (solockp)
693 1.10 mycroft nfs_sndunlock(solockp);
694 1.10 mycroft if (error == EINTR || error == ERESTART) {
695 1.19 fvdl free((caddr_t)nd, M_NFSRVDESC);
696 1.10 mycroft nfsrv_slpderef(slp);
697 1.14 mycroft s = splsoftnet();
698 1.10 mycroft goto done;
699 1.1 cgd }
700 1.1 cgd break;
701 1.19 fvdl case RC_DROPIT:
702 1.10 mycroft if (nfsrtton)
703 1.19 fvdl nfsd_rt(sotype, nd, cacherep);
704 1.10 mycroft m_freem(nd->nd_mrep);
705 1.19 fvdl m_freem(nd->nd_nam2);
706 1.1 cgd break;
707 1.19 fvdl };
708 1.19 fvdl if (nd) {
709 1.19 fvdl FREE((caddr_t)nd, M_NFSRVDESC);
710 1.19 fvdl nd = NULL;
711 1.19 fvdl }
712 1.19 fvdl
713 1.19 fvdl /*
714 1.19 fvdl * Check to see if there are outstanding writes that
715 1.19 fvdl * need to be serviced.
716 1.19 fvdl */
717 1.19 fvdl cur_usec = (u_quad_t)time.tv_sec * 1000000 +
718 1.19 fvdl (u_quad_t)time.tv_usec;
719 1.19 fvdl s = splsoftclock();
720 1.19 fvdl if (slp->ns_tq.lh_first &&
721 1.19 fvdl slp->ns_tq.lh_first->nd_time <= cur_usec) {
722 1.19 fvdl cacherep = RC_DOIT;
723 1.19 fvdl writes_todo = 1;
724 1.19 fvdl } else
725 1.19 fvdl writes_todo = 0;
726 1.19 fvdl splx(s);
727 1.19 fvdl } while (writes_todo);
728 1.14 mycroft s = splsoftnet();
729 1.19 fvdl if (nfsrv_dorec(slp, nfsd, &nd)) {
730 1.19 fvdl nfsd->nfsd_flag &= ~NFSD_REQINPROG;
731 1.19 fvdl nfsd->nfsd_slp = NULL;
732 1.10 mycroft nfsrv_slpderef(slp);
733 1.10 mycroft }
734 1.1 cgd }
735 1.10 mycroft done:
736 1.33 mycroft p->p_holdcnt--;
737 1.19 fvdl TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
738 1.10 mycroft splx(s);
739 1.19 fvdl free((caddr_t)nfsd, M_NFSD);
740 1.10 mycroft nsd->nsd_nfsd = (struct nfsd *)0;
741 1.10 mycroft if (--nfs_numnfsd == 0)
742 1.10 mycroft nfsrv_init(TRUE); /* Reinitialize everything */
743 1.1 cgd return (error);
744 1.1 cgd }
745 1.1 cgd
746 1.10 mycroft /*
747 1.10 mycroft * Shut down a socket associated with an nfssvc_sock structure.
748 1.10 mycroft * Should be called with the send lock set, if required.
749 1.10 mycroft * The trick here is to increment the sref at the start, so that the nfsds
750 1.10 mycroft * will stop using it and clear ns_flag at the end so that it will not be
751 1.10 mycroft * reassigned during cleanup.
752 1.10 mycroft */
753 1.18 christos void
754 1.10 mycroft nfsrv_zapsock(slp)
755 1.38 augustss struct nfssvc_sock *slp;
756 1.10 mycroft {
757 1.38 augustss struct nfsuid *nuidp, *nnuidp;
758 1.38 augustss struct nfsrv_descript *nwp, *nnwp;
759 1.10 mycroft struct socket *so;
760 1.10 mycroft struct file *fp;
761 1.10 mycroft struct mbuf *m;
762 1.19 fvdl int s;
763 1.10 mycroft
764 1.10 mycroft slp->ns_flag &= ~SLP_ALLFLAGS;
765 1.19 fvdl fp = slp->ns_fp;
766 1.19 fvdl if (fp) {
767 1.35 thorpej FILE_USE(fp);
768 1.10 mycroft slp->ns_fp = (struct file *)0;
769 1.10 mycroft so = slp->ns_so;
770 1.10 mycroft so->so_upcall = NULL;
771 1.29 matt so->so_upcallarg = NULL;
772 1.29 matt so->so_rcv.sb_flags &= ~SB_UPCALL;
773 1.10 mycroft soshutdown(so, 2);
774 1.10 mycroft closef(fp, (struct proc *)0);
775 1.10 mycroft if (slp->ns_nam)
776 1.10 mycroft MFREE(slp->ns_nam, m);
777 1.10 mycroft m_freem(slp->ns_raw);
778 1.10 mycroft m_freem(slp->ns_rec);
779 1.12 mycroft for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
780 1.12 mycroft nuidp = nnuidp) {
781 1.12 mycroft nnuidp = nuidp->nu_lru.tqe_next;
782 1.12 mycroft LIST_REMOVE(nuidp, nu_hash);
783 1.12 mycroft TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
784 1.19 fvdl if (nuidp->nu_flag & NU_NAM)
785 1.19 fvdl m_freem(nuidp->nu_nam);
786 1.12 mycroft free((caddr_t)nuidp, M_NFSUID);
787 1.12 mycroft }
788 1.19 fvdl s = splsoftclock();
789 1.19 fvdl for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
790 1.19 fvdl nnwp = nwp->nd_tq.le_next;
791 1.19 fvdl LIST_REMOVE(nwp, nd_tq);
792 1.19 fvdl free((caddr_t)nwp, M_NFSRVDESC);
793 1.19 fvdl }
794 1.19 fvdl LIST_INIT(&slp->ns_tq);
795 1.19 fvdl splx(s);
796 1.10 mycroft }
797 1.10 mycroft }
798 1.10 mycroft
799 1.10 mycroft /*
800 1.10 mycroft * Derefence a server socket structure. If it has no more references and
801 1.10 mycroft * is no longer valid, you can throw it away.
802 1.10 mycroft */
803 1.10 mycroft void
804 1.10 mycroft nfsrv_slpderef(slp)
805 1.38 augustss struct nfssvc_sock *slp;
806 1.10 mycroft {
807 1.10 mycroft if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
808 1.12 mycroft TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
809 1.10 mycroft free((caddr_t)slp, M_NFSSVC);
810 1.10 mycroft }
811 1.10 mycroft }
812 1.10 mycroft
813 1.10 mycroft /*
814 1.10 mycroft * Initialize the data structures for the server.
815 1.10 mycroft * Handshake with any new nfsds starting up to avoid any chance of
816 1.10 mycroft * corruption.
817 1.10 mycroft */
818 1.10 mycroft void
819 1.10 mycroft nfsrv_init(terminating)
820 1.10 mycroft int terminating;
821 1.10 mycroft {
822 1.38 augustss struct nfssvc_sock *slp, *nslp;
823 1.10 mycroft
824 1.12 mycroft if (nfssvc_sockhead_flag & SLP_INIT)
825 1.10 mycroft panic("nfsd init");
826 1.12 mycroft nfssvc_sockhead_flag |= SLP_INIT;
827 1.10 mycroft if (terminating) {
828 1.12 mycroft for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
829 1.12 mycroft nslp = slp->ns_chain.tqe_next;
830 1.10 mycroft if (slp->ns_flag & SLP_VALID)
831 1.10 mycroft nfsrv_zapsock(slp);
832 1.12 mycroft TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
833 1.12 mycroft free((caddr_t)slp, M_NFSSVC);
834 1.10 mycroft }
835 1.10 mycroft nfsrv_cleancache(); /* And clear out server cache */
836 1.26 fvdl } else
837 1.26 fvdl nfs_pub.np_valid = 0;
838 1.12 mycroft
839 1.12 mycroft TAILQ_INIT(&nfssvc_sockhead);
840 1.12 mycroft nfssvc_sockhead_flag &= ~SLP_INIT;
841 1.12 mycroft if (nfssvc_sockhead_flag & SLP_WANTINIT) {
842 1.12 mycroft nfssvc_sockhead_flag &= ~SLP_WANTINIT;
843 1.12 mycroft wakeup((caddr_t)&nfssvc_sockhead);
844 1.12 mycroft }
845 1.12 mycroft
846 1.12 mycroft TAILQ_INIT(&nfsd_head);
847 1.12 mycroft nfsd_head_flag &= ~NFSD_CHECKSLP;
848 1.12 mycroft
849 1.10 mycroft nfs_udpsock = (struct nfssvc_sock *)
850 1.10 mycroft malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
851 1.32 perry memset((caddr_t)nfs_udpsock, 0, sizeof (struct nfssvc_sock));
852 1.12 mycroft TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
853 1.12 mycroft TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
854 1.41 fvdl
855 1.41 fvdl #ifdef INET6
856 1.41 fvdl nfs_udp6sock = (struct nfssvc_sock *)
857 1.41 fvdl malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
858 1.41 fvdl memset((caddr_t)nfs_udp6sock, 0, sizeof (struct nfssvc_sock));
859 1.41 fvdl TAILQ_INIT(&nfs_udp6sock->ns_uidlruhead);
860 1.41 fvdl TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_udp6sock, ns_chain);
861 1.41 fvdl #endif
862 1.12 mycroft
863 1.10 mycroft nfs_cltpsock = (struct nfssvc_sock *)
864 1.10 mycroft malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
865 1.32 perry memset((caddr_t)nfs_cltpsock, 0, sizeof (struct nfssvc_sock));
866 1.12 mycroft TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
867 1.12 mycroft TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
868 1.10 mycroft }
869 1.10 mycroft
870 1.10 mycroft /*
871 1.10 mycroft * Add entries to the server monitor log.
872 1.10 mycroft */
873 1.10 mycroft static void
874 1.19 fvdl nfsd_rt(sotype, nd, cacherep)
875 1.10 mycroft int sotype;
876 1.38 augustss struct nfsrv_descript *nd;
877 1.10 mycroft int cacherep;
878 1.10 mycroft {
879 1.38 augustss struct drt *rt;
880 1.10 mycroft
881 1.10 mycroft rt = &nfsdrt.drt[nfsdrt.pos];
882 1.10 mycroft if (cacherep == RC_DOIT)
883 1.10 mycroft rt->flag = 0;
884 1.10 mycroft else if (cacherep == RC_REPLY)
885 1.10 mycroft rt->flag = DRT_CACHEREPLY;
886 1.10 mycroft else
887 1.10 mycroft rt->flag = DRT_CACHEDROP;
888 1.10 mycroft if (sotype == SOCK_STREAM)
889 1.10 mycroft rt->flag |= DRT_TCP;
890 1.19 fvdl if (nd->nd_flag & ND_NQNFS)
891 1.10 mycroft rt->flag |= DRT_NQNFS;
892 1.19 fvdl else if (nd->nd_flag & ND_NFSV3)
893 1.19 fvdl rt->flag |= DRT_NFSV3;
894 1.10 mycroft rt->proc = nd->nd_procnum;
895 1.19 fvdl if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
896 1.19 fvdl rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
897 1.10 mycroft else
898 1.19 fvdl rt->ipadr = INADDR_ANY;
899 1.19 fvdl rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
900 1.19 fvdl (time.tv_usec - nd->nd_starttime.tv_usec);
901 1.10 mycroft rt->tstamp = time;
902 1.10 mycroft nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
903 1.10 mycroft }
904 1.3 glass #endif /* NFSSERVER */
905 1.3 glass
906 1.24 thorpej #ifdef NFS
907 1.22 thorpej
908 1.22 thorpej int nfs_defect = 0;
909 1.1 cgd /*
910 1.39 tsarna * Asynchronous I/O threads for client nfs.
911 1.10 mycroft * They do read-ahead and write-behind operations on the block I/O cache.
912 1.10 mycroft * Never returns unless it fails or gets killed.
913 1.1 cgd */
914 1.39 tsarna
915 1.18 christos int
916 1.10 mycroft nfssvc_iod(p)
917 1.1 cgd struct proc *p;
918 1.1 cgd {
919 1.38 augustss struct buf *bp;
920 1.38 augustss int i, myiod;
921 1.22 thorpej struct nfsmount *nmp;
922 1.22 thorpej int error = 0;
923 1.1 cgd
924 1.1 cgd /*
925 1.1 cgd * Assign my position or return error if too many already running
926 1.1 cgd */
927 1.1 cgd myiod = -1;
928 1.1 cgd for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
929 1.39 tsarna if (nfs_asyncdaemon[i] == NULL) {
930 1.1 cgd myiod = i;
931 1.1 cgd break;
932 1.1 cgd }
933 1.1 cgd if (myiod == -1)
934 1.1 cgd return (EBUSY);
935 1.39 tsarna nfs_asyncdaemon[myiod] = p;
936 1.1 cgd nfs_numasync++;
937 1.33 mycroft p->p_holdcnt++;
938 1.1 cgd /*
939 1.1 cgd * Just loop around doin our stuff until SIGKILL
940 1.1 cgd */
941 1.1 cgd for (;;) {
942 1.22 thorpej while (((nmp = nfs_iodmount[myiod]) == NULL
943 1.22 thorpej || nmp->nm_bufq.tqh_first == NULL)
944 1.22 thorpej && error == 0) {
945 1.22 thorpej if (nmp)
946 1.22 thorpej nmp->nm_bufqiods--;
947 1.19 fvdl nfs_iodwant[myiod] = p;
948 1.22 thorpej nfs_iodmount[myiod] = NULL;
949 1.19 fvdl error = tsleep((caddr_t)&nfs_iodwant[myiod],
950 1.19 fvdl PWAIT | PCATCH, "nfsidl", 0);
951 1.19 fvdl }
952 1.22 thorpej if (error) {
953 1.22 thorpej if (nmp)
954 1.22 thorpej nmp->nm_bufqiods--;
955 1.22 thorpej nfs_iodmount[myiod] = NULL;
956 1.33 mycroft break;
957 1.22 thorpej }
958 1.22 thorpej while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
959 1.19 fvdl /* Take one off the front of the list */
960 1.22 thorpej TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
961 1.22 thorpej nmp->nm_bufqlen--;
962 1.22 thorpej if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) {
963 1.22 thorpej nmp->nm_bufqwant = FALSE;
964 1.22 thorpej wakeup(&nmp->nm_bufq);
965 1.22 thorpej }
966 1.19 fvdl if (bp->b_flags & B_READ)
967 1.19 fvdl (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0);
968 1.22 thorpej else
969 1.19 fvdl (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0);
970 1.22 thorpej /*
971 1.22 thorpej * If there are more than one iod on this mount, then defect
972 1.22 thorpej * so that the iods can be shared out fairly between the mounts
973 1.22 thorpej */
974 1.22 thorpej if (nfs_defect && nmp->nm_bufqiods > 1) {
975 1.22 thorpej nfs_iodmount[myiod] = NULL;
976 1.22 thorpej nmp->nm_bufqiods--;
977 1.22 thorpej break;
978 1.22 thorpej }
979 1.19 fvdl }
980 1.1 cgd }
981 1.33 mycroft p->p_holdcnt--;
982 1.39 tsarna nfs_asyncdaemon[myiod] = NULL;
983 1.33 mycroft nfs_numasync--;
984 1.39 tsarna
985 1.33 mycroft return (error);
986 1.1 cgd }
987 1.3 glass
988 1.39 tsarna void
989 1.39 tsarna start_nfsio(arg)
990 1.39 tsarna void *arg;
991 1.39 tsarna {
992 1.39 tsarna nfssvc_iod(curproc);
993 1.39 tsarna
994 1.39 tsarna kthread_exit(0);
995 1.39 tsarna }
996 1.39 tsarna
997 1.39 tsarna void
998 1.39 tsarna nfs_getset_niothreads(set)
999 1.39 tsarna int set;
1000 1.39 tsarna {
1001 1.39 tsarna int i, have, start;
1002 1.39 tsarna
1003 1.39 tsarna for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
1004 1.39 tsarna if (nfs_asyncdaemon[i] != NULL)
1005 1.39 tsarna have++;
1006 1.39 tsarna
1007 1.39 tsarna if (set) {
1008 1.39 tsarna /* clamp to sane range */
1009 1.39 tsarna nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
1010 1.39 tsarna
1011 1.39 tsarna start = nfs_niothreads - have;
1012 1.39 tsarna
1013 1.39 tsarna while (start > 0) {
1014 1.39 tsarna kthread_create1(start_nfsio, NULL, NULL, "nfsio");
1015 1.39 tsarna start--;
1016 1.39 tsarna }
1017 1.39 tsarna
1018 1.39 tsarna for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
1019 1.39 tsarna if (nfs_asyncdaemon[i] != NULL) {
1020 1.39 tsarna psignal(nfs_asyncdaemon[i], SIGKILL);
1021 1.39 tsarna start++;
1022 1.39 tsarna }
1023 1.39 tsarna } else {
1024 1.40 tsarna if (nfs_niothreads >= 0)
1025 1.40 tsarna nfs_niothreads = have;
1026 1.39 tsarna }
1027 1.39 tsarna }
1028 1.19 fvdl
1029 1.10 mycroft /*
1030 1.10 mycroft * Get an authorization string for the uid by having the mount_nfs sitting
1031 1.10 mycroft * on this mount point porpous out of the kernel and do it.
1032 1.10 mycroft */
1033 1.18 christos int
1034 1.19 fvdl nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
1035 1.38 augustss struct nfsmount *nmp;
1036 1.10 mycroft struct nfsreq *rep;
1037 1.10 mycroft struct ucred *cred;
1038 1.10 mycroft char **auth_str;
1039 1.10 mycroft int *auth_len;
1040 1.19 fvdl char *verf_str;
1041 1.19 fvdl int *verf_len;
1042 1.19 fvdl NFSKERBKEY_T key; /* return session key */
1043 1.10 mycroft {
1044 1.10 mycroft int error = 0;
1045 1.10 mycroft
1046 1.27 fvdl while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
1047 1.27 fvdl nmp->nm_iflag |= NFSMNT_WANTAUTH;
1048 1.10 mycroft (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
1049 1.10 mycroft "nfsauth1", 2 * hz);
1050 1.18 christos error = nfs_sigintr(nmp, rep, rep->r_procp);
1051 1.18 christos if (error) {
1052 1.27 fvdl nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1053 1.10 mycroft return (error);
1054 1.10 mycroft }
1055 1.10 mycroft }
1056 1.27 fvdl nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
1057 1.10 mycroft nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1058 1.19 fvdl nmp->nm_authlen = RPCAUTH_MAXSIZ;
1059 1.19 fvdl nmp->nm_verfstr = verf_str;
1060 1.19 fvdl nmp->nm_verflen = *verf_len;
1061 1.10 mycroft nmp->nm_authuid = cred->cr_uid;
1062 1.10 mycroft wakeup((caddr_t)&nmp->nm_authstr);
1063 1.3 glass
1064 1.10 mycroft /*
1065 1.10 mycroft * And wait for mount_nfs to do its stuff.
1066 1.10 mycroft */
1067 1.27 fvdl while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
1068 1.10 mycroft (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
1069 1.10 mycroft "nfsauth2", 2 * hz);
1070 1.10 mycroft error = nfs_sigintr(nmp, rep, rep->r_procp);
1071 1.10 mycroft }
1072 1.27 fvdl if (nmp->nm_iflag & NFSMNT_AUTHERR) {
1073 1.27 fvdl nmp->nm_iflag &= ~NFSMNT_AUTHERR;
1074 1.10 mycroft error = EAUTH;
1075 1.10 mycroft }
1076 1.10 mycroft if (error)
1077 1.10 mycroft free((caddr_t)*auth_str, M_TEMP);
1078 1.10 mycroft else {
1079 1.10 mycroft *auth_len = nmp->nm_authlen;
1080 1.19 fvdl *verf_len = nmp->nm_verflen;
1081 1.32 perry memcpy((caddr_t)key, (caddr_t)nmp->nm_key, sizeof (key));
1082 1.10 mycroft }
1083 1.27 fvdl nmp->nm_iflag &= ~NFSMNT_HASAUTH;
1084 1.27 fvdl nmp->nm_iflag |= NFSMNT_WAITAUTH;
1085 1.27 fvdl if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
1086 1.27 fvdl nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1087 1.10 mycroft wakeup((caddr_t)&nmp->nm_authtype);
1088 1.10 mycroft }
1089 1.19 fvdl return (error);
1090 1.19 fvdl }
1091 1.19 fvdl
1092 1.19 fvdl /*
1093 1.19 fvdl * Get a nickname authenticator and verifier.
1094 1.19 fvdl */
1095 1.19 fvdl int
1096 1.19 fvdl nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1097 1.19 fvdl struct nfsmount *nmp;
1098 1.19 fvdl struct ucred *cred;
1099 1.19 fvdl char **auth_str;
1100 1.19 fvdl int *auth_len;
1101 1.19 fvdl char *verf_str;
1102 1.19 fvdl int verf_len;
1103 1.19 fvdl {
1104 1.38 augustss struct nfsuid *nuidp;
1105 1.38 augustss u_int32_t *nickp, *verfp;
1106 1.19 fvdl struct timeval ktvin, ktvout;
1107 1.19 fvdl
1108 1.19 fvdl #ifdef DIAGNOSTIC
1109 1.19 fvdl if (verf_len < (4 * NFSX_UNSIGNED))
1110 1.19 fvdl panic("nfs_getnickauth verf too small");
1111 1.19 fvdl #endif
1112 1.19 fvdl for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1113 1.19 fvdl nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1114 1.19 fvdl if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1115 1.19 fvdl break;
1116 1.19 fvdl }
1117 1.19 fvdl if (!nuidp || nuidp->nu_expire < time.tv_sec)
1118 1.19 fvdl return (EACCES);
1119 1.19 fvdl
1120 1.19 fvdl /*
1121 1.19 fvdl * Move to the end of the lru list (end of lru == most recently used).
1122 1.19 fvdl */
1123 1.19 fvdl TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1124 1.19 fvdl TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1125 1.19 fvdl
1126 1.19 fvdl nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1127 1.19 fvdl *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1128 1.19 fvdl *nickp = txdr_unsigned(nuidp->nu_nickname);
1129 1.19 fvdl *auth_str = (char *)nickp;
1130 1.19 fvdl *auth_len = 2 * NFSX_UNSIGNED;
1131 1.19 fvdl
1132 1.19 fvdl /*
1133 1.19 fvdl * Now we must encrypt the verifier and package it up.
1134 1.19 fvdl */
1135 1.19 fvdl verfp = (u_int32_t *)verf_str;
1136 1.19 fvdl *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1137 1.19 fvdl if (time.tv_sec > nuidp->nu_timestamp.tv_sec ||
1138 1.19 fvdl (time.tv_sec == nuidp->nu_timestamp.tv_sec &&
1139 1.19 fvdl time.tv_usec > nuidp->nu_timestamp.tv_usec))
1140 1.19 fvdl nuidp->nu_timestamp = time;
1141 1.19 fvdl else
1142 1.19 fvdl nuidp->nu_timestamp.tv_usec++;
1143 1.19 fvdl ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1144 1.19 fvdl ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1145 1.19 fvdl
1146 1.19 fvdl /*
1147 1.19 fvdl * Now encrypt the timestamp verifier in ecb mode using the session
1148 1.19 fvdl * key.
1149 1.19 fvdl */
1150 1.19 fvdl #ifdef NFSKERB
1151 1.19 fvdl XXX
1152 1.19 fvdl #endif
1153 1.19 fvdl
1154 1.19 fvdl *verfp++ = ktvout.tv_sec;
1155 1.19 fvdl *verfp++ = ktvout.tv_usec;
1156 1.19 fvdl *verfp = 0;
1157 1.19 fvdl return (0);
1158 1.19 fvdl }
1159 1.19 fvdl
1160 1.19 fvdl /*
1161 1.19 fvdl * Save the current nickname in a hash list entry on the mount point.
1162 1.19 fvdl */
1163 1.19 fvdl int
1164 1.19 fvdl nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1165 1.38 augustss struct nfsmount *nmp;
1166 1.19 fvdl struct ucred *cred;
1167 1.19 fvdl int len;
1168 1.19 fvdl NFSKERBKEY_T key;
1169 1.19 fvdl struct mbuf **mdp;
1170 1.19 fvdl char **dposp;
1171 1.19 fvdl struct mbuf *mrep;
1172 1.19 fvdl {
1173 1.38 augustss struct nfsuid *nuidp;
1174 1.38 augustss u_int32_t *tl;
1175 1.38 augustss int32_t t1;
1176 1.19 fvdl struct mbuf *md = *mdp;
1177 1.19 fvdl struct timeval ktvin, ktvout;
1178 1.19 fvdl u_int32_t nick;
1179 1.19 fvdl char *dpos = *dposp, *cp2;
1180 1.19 fvdl int deltasec, error = 0;
1181 1.19 fvdl
1182 1.19 fvdl if (len == (3 * NFSX_UNSIGNED)) {
1183 1.19 fvdl nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1184 1.19 fvdl ktvin.tv_sec = *tl++;
1185 1.19 fvdl ktvin.tv_usec = *tl++;
1186 1.19 fvdl nick = fxdr_unsigned(u_int32_t, *tl);
1187 1.19 fvdl
1188 1.19 fvdl /*
1189 1.19 fvdl * Decrypt the timestamp in ecb mode.
1190 1.19 fvdl */
1191 1.19 fvdl #ifdef NFSKERB
1192 1.19 fvdl XXX
1193 1.19 fvdl #endif
1194 1.19 fvdl ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1195 1.19 fvdl ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1196 1.19 fvdl deltasec = time.tv_sec - ktvout.tv_sec;
1197 1.19 fvdl if (deltasec < 0)
1198 1.19 fvdl deltasec = -deltasec;
1199 1.19 fvdl /*
1200 1.19 fvdl * If ok, add it to the hash list for the mount point.
1201 1.19 fvdl */
1202 1.19 fvdl if (deltasec <= NFS_KERBCLOCKSKEW) {
1203 1.19 fvdl if (nmp->nm_numuids < nuidhash_max) {
1204 1.19 fvdl nmp->nm_numuids++;
1205 1.19 fvdl nuidp = (struct nfsuid *)
1206 1.19 fvdl malloc(sizeof (struct nfsuid), M_NFSUID,
1207 1.19 fvdl M_WAITOK);
1208 1.19 fvdl } else {
1209 1.19 fvdl nuidp = nmp->nm_uidlruhead.tqh_first;
1210 1.19 fvdl LIST_REMOVE(nuidp, nu_hash);
1211 1.19 fvdl TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1212 1.19 fvdl nu_lru);
1213 1.19 fvdl }
1214 1.19 fvdl nuidp->nu_flag = 0;
1215 1.19 fvdl nuidp->nu_cr.cr_uid = cred->cr_uid;
1216 1.19 fvdl nuidp->nu_expire = time.tv_sec + NFS_KERBTTL;
1217 1.19 fvdl nuidp->nu_timestamp = ktvout;
1218 1.19 fvdl nuidp->nu_nickname = nick;
1219 1.32 perry memcpy(nuidp->nu_key, key, sizeof (key));
1220 1.19 fvdl TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1221 1.19 fvdl nu_lru);
1222 1.19 fvdl LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1223 1.19 fvdl nuidp, nu_hash);
1224 1.19 fvdl }
1225 1.19 fvdl } else
1226 1.19 fvdl nfsm_adv(nfsm_rndup(len));
1227 1.19 fvdl nfsmout:
1228 1.19 fvdl *mdp = md;
1229 1.19 fvdl *dposp = dpos;
1230 1.10 mycroft return (error);
1231 1.10 mycroft }
1232 1.24 thorpej #endif /* NFS */
1233