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