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