nfs_syscalls.c revision 1.160.2.1 1 1.160.2.1 christos /* $NetBSD: nfs_syscalls.c,v 1.160.2.1 2019/06/10 22:09:49 christos 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.70 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd *
34 1.19 fvdl * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
35 1.1 cgd */
36 1.49 lukem
37 1.49 lukem #include <sys/cdefs.h>
38 1.160.2.1 christos __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.160.2.1 2019/06/10 22:09:49 christos Exp $");
39 1.1 cgd
40 1.7 mycroft #include <sys/param.h>
41 1.7 mycroft #include <sys/systm.h>
42 1.7 mycroft #include <sys/kernel.h>
43 1.7 mycroft #include <sys/file.h>
44 1.7 mycroft #include <sys/stat.h>
45 1.7 mycroft #include <sys/vnode.h>
46 1.7 mycroft #include <sys/mount.h>
47 1.7 mycroft #include <sys/proc.h>
48 1.10 mycroft #include <sys/uio.h>
49 1.7 mycroft #include <sys/malloc.h>
50 1.124 yamt #include <sys/kmem.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.39 tsarna #include <sys/signalvar.h>
56 1.7 mycroft #include <sys/domain.h>
57 1.7 mycroft #include <sys/protosw.h>
58 1.10 mycroft #include <sys/namei.h>
59 1.10 mycroft #include <sys/syslog.h>
60 1.18 christos #include <sys/filedesc.h>
61 1.39 tsarna #include <sys/kthread.h>
62 1.92 elad #include <sys/kauth.h>
63 1.13 cgd #include <sys/syscallargs.h>
64 1.159 riastrad #include <sys/cprng.h>
65 1.159 riastrad #include <sys/rbtree.h>
66 1.13 cgd
67 1.7 mycroft #include <netinet/in.h>
68 1.7 mycroft #include <netinet/tcp.h>
69 1.19 fvdl #include <nfs/xdr_subs.h>
70 1.10 mycroft #include <nfs/rpcv2.h>
71 1.19 fvdl #include <nfs/nfsproto.h>
72 1.7 mycroft #include <nfs/nfs.h>
73 1.19 fvdl #include <nfs/nfsm_subs.h>
74 1.7 mycroft #include <nfs/nfsrvcache.h>
75 1.10 mycroft #include <nfs/nfsmount.h>
76 1.10 mycroft #include <nfs/nfsnode.h>
77 1.10 mycroft #include <nfs/nfsrtt.h>
78 1.18 christos #include <nfs/nfs_var.h>
79 1.1 cgd
80 1.144 dsl extern int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *,
81 1.19 fvdl struct nfssvc_sock *,
82 1.144 dsl struct lwp *, struct mbuf **);
83 1.19 fvdl extern int nfsrvw_procrastinate;
84 1.153 christos extern int nuidhash_max;
85 1.51 matt
86 1.10 mycroft static int nfs_numnfsd = 0;
87 1.10 mycroft static struct nfsdrt nfsdrt;
88 1.111 yamt kmutex_t nfsd_lock;
89 1.51 matt struct nfssvc_sockhead nfssvc_sockhead;
90 1.111 yamt kcondvar_t nfsd_initcv;
91 1.66 yamt struct nfssvc_sockhead nfssvc_sockpending;
92 1.66 yamt struct nfsdidlehead nfsd_idle_head;
93 1.51 matt
94 1.159 riastrad static rb_tree_t nfsd_tree;
95 1.159 riastrad static const rb_tree_ops_t nfsd_tree_ops;
96 1.159 riastrad
97 1.51 matt int nfssvc_sockhead_flag;
98 1.51 matt int nfsd_head_flag;
99 1.1 cgd
100 1.142 ad struct nfssvc_sock *nfs_udpsock;
101 1.142 ad struct nfssvc_sock *nfs_udp6sock;
102 1.19 fvdl
103 1.144 dsl static struct nfssvc_sock *nfsrv_sockalloc(void);
104 1.144 dsl static void nfsrv_sockfree(struct nfssvc_sock *);
105 1.144 dsl static void nfsd_rt(int, struct nfsrv_descript *, int);
106 1.156 mrg static int nfssvc_nfsd(struct nfssvc_copy_ops *, struct nfsd_srvargs *, void *,
107 1.156 mrg struct lwp *);
108 1.156 mrg
109 1.159 riastrad static int nfsd_compare_nodes(void *, const void *, const void *);
110 1.159 riastrad static int nfsd_compare_key(void *, const void *, const void *);
111 1.159 riastrad
112 1.159 riastrad static struct nfsd *nfsd_bake_cookie(struct nfsd *);
113 1.159 riastrad static void nfsd_toss_cookie(struct nfsd *);
114 1.159 riastrad static struct nfsd *nfsd_get(struct nfsd *);
115 1.159 riastrad
116 1.156 mrg static int nfssvc_addsock_in(struct nfsd_args *, const void *);
117 1.156 mrg static int nfssvc_setexports_in(struct mountd_exports_list *, const void *);
118 1.156 mrg static int nfssvc_nsd_in(struct nfsd_srvargs *, const void *);
119 1.156 mrg static int nfssvc_nsd_out(void *, const struct nfsd_srvargs *);
120 1.156 mrg static int nfssvc_exp_in(struct export_args *, const void *, size_t);
121 1.156 mrg
122 1.159 riastrad static const rb_tree_ops_t nfsd_tree_ops = {
123 1.159 riastrad .rbto_compare_nodes = nfsd_compare_nodes,
124 1.159 riastrad .rbto_compare_key = nfsd_compare_key,
125 1.159 riastrad .rbto_node_offset = offsetof(struct nfsd, nfsd_node),
126 1.159 riastrad };
127 1.159 riastrad
128 1.159 riastrad static int
129 1.159 riastrad nfsd_compare_nodes(void *cookie, const void *va, const void *vb)
130 1.159 riastrad {
131 1.159 riastrad const struct nfsd *na = va;
132 1.159 riastrad const struct nfsd *nb = vb;
133 1.159 riastrad
134 1.159 riastrad if (na->nfsd_cookie < nb->nfsd_cookie)
135 1.159 riastrad return -1;
136 1.159 riastrad if (na->nfsd_cookie > nb->nfsd_cookie)
137 1.159 riastrad return +1;
138 1.159 riastrad return 0;
139 1.159 riastrad }
140 1.159 riastrad
141 1.159 riastrad static int
142 1.159 riastrad nfsd_compare_key(void *cookie, const void *vn, const void *vk)
143 1.159 riastrad {
144 1.159 riastrad const struct nfsd *n = vn;
145 1.159 riastrad const uint32_t *k = vk;
146 1.159 riastrad
147 1.159 riastrad if (n->nfsd_cookie < *k)
148 1.159 riastrad return -1;
149 1.159 riastrad if (n->nfsd_cookie > *k)
150 1.159 riastrad return +1;
151 1.159 riastrad return 0;
152 1.159 riastrad }
153 1.159 riastrad
154 1.159 riastrad /*
155 1.159 riastrad * nfsd_bake_cookie(nfsd)
156 1.159 riastrad *
157 1.159 riastrad * Bake a cookie for nfsd, hang it on the tree of nfsds, and
158 1.159 riastrad * return a userland-safe pointer nfsdu neatly packed for
159 1.159 riastrad * transport in struct nfsd_srvargs::nsd_nfsd.
160 1.159 riastrad */
161 1.159 riastrad static struct nfsd *
162 1.159 riastrad nfsd_bake_cookie(struct nfsd *nfsd)
163 1.159 riastrad {
164 1.159 riastrad
165 1.159 riastrad KASSERT(mutex_owned(&nfsd_lock));
166 1.159 riastrad
167 1.159 riastrad do {
168 1.159 riastrad nfsd->nfsd_cookie = cprng_fast32();
169 1.159 riastrad } while (nfsd->nfsd_cookie == 0 ||
170 1.159 riastrad rb_tree_insert_node(&nfsd_tree, nfsd) != nfsd);
171 1.159 riastrad
172 1.159 riastrad return (struct nfsd *)(uintptr_t)nfsd->nfsd_cookie;
173 1.159 riastrad }
174 1.159 riastrad
175 1.159 riastrad /*
176 1.159 riastrad * nfsd_toss_cookie(nfsd)
177 1.159 riastrad *
178 1.159 riastrad * Toss nfsd's cookie.
179 1.159 riastrad */
180 1.159 riastrad static void
181 1.159 riastrad nfsd_toss_cookie(struct nfsd *nfsd)
182 1.159 riastrad {
183 1.159 riastrad
184 1.159 riastrad KASSERT(mutex_owned(&nfsd_lock));
185 1.159 riastrad KASSERT(nfsd->nfsd_cookie != 0);
186 1.159 riastrad
187 1.159 riastrad rb_tree_remove_node(&nfsd_tree, nfsd);
188 1.159 riastrad nfsd->nfsd_cookie = 0; /* paranoia */
189 1.159 riastrad }
190 1.159 riastrad
191 1.159 riastrad /*
192 1.159 riastrad * nfsd_get(nfsdu)
193 1.159 riastrad *
194 1.159 riastrad * Return the struct nfsd pointer for the userland nfsdu cookie,
195 1.159 riastrad * as stored in struct nfsd_srvargs::nsd_nfsd, or NULL if nfsdu is
196 1.159 riastrad * not a current valid nfsd cookie.
197 1.159 riastrad *
198 1.159 riastrad * Caller MUST NOT hold nfsd_lock. Caller MUST NOT pass (struct
199 1.159 riastrad * nfsd *)(uintptr_t)0, which is the sentinel value for no nfsd
200 1.159 riastrad * cookie, for which the caller should check first.
201 1.159 riastrad */
202 1.159 riastrad static struct nfsd *
203 1.159 riastrad nfsd_get(struct nfsd *nfsdu)
204 1.159 riastrad {
205 1.159 riastrad uintptr_t cookie = (uintptr_t)nfsdu;
206 1.159 riastrad uint32_t key;
207 1.159 riastrad struct nfsd *nfsd;
208 1.159 riastrad
209 1.159 riastrad KASSERT(cookie != 0);
210 1.159 riastrad if (cookie > UINT32_MAX)
211 1.159 riastrad return NULL;
212 1.159 riastrad key = cookie;
213 1.159 riastrad
214 1.159 riastrad mutex_enter(&nfsd_lock);
215 1.159 riastrad nfsd = rb_tree_find_node(&nfsd_tree, &key);
216 1.159 riastrad mutex_exit(&nfsd_lock);
217 1.159 riastrad
218 1.159 riastrad return nfsd;
219 1.159 riastrad }
220 1.159 riastrad
221 1.156 mrg static int
222 1.156 mrg nfssvc_addsock_in(struct nfsd_args *nfsdarg, const void *argp)
223 1.156 mrg {
224 1.156 mrg
225 1.156 mrg return copyin(argp, nfsdarg, sizeof *nfsdarg);
226 1.156 mrg }
227 1.156 mrg
228 1.156 mrg static int
229 1.156 mrg nfssvc_setexports_in(struct mountd_exports_list *mel, const void *argp)
230 1.156 mrg {
231 1.156 mrg
232 1.156 mrg return copyin(argp, mel, sizeof *mel);
233 1.156 mrg }
234 1.156 mrg
235 1.156 mrg static int
236 1.156 mrg nfssvc_nsd_in(struct nfsd_srvargs *nsd, const void *argp)
237 1.156 mrg {
238 1.156 mrg
239 1.156 mrg return copyin(argp, nsd, sizeof *nsd);
240 1.156 mrg }
241 1.156 mrg
242 1.156 mrg static int
243 1.156 mrg nfssvc_nsd_out(void *argp, const struct nfsd_srvargs *nsd)
244 1.156 mrg {
245 1.156 mrg
246 1.156 mrg return copyout(nsd, argp, sizeof *nsd);
247 1.156 mrg }
248 1.156 mrg
249 1.156 mrg static int
250 1.156 mrg nfssvc_exp_in(struct export_args *exp, const void *argp, size_t nexports)
251 1.156 mrg {
252 1.156 mrg
253 1.156 mrg return copyin(argp, exp, sizeof(*exp) * nexports);
254 1.156 mrg }
255 1.19 fvdl
256 1.1 cgd /*
257 1.1 cgd * NFS server system calls
258 1.1 cgd */
259 1.1 cgd
260 1.156 mrg static struct nfssvc_copy_ops native_ops = {
261 1.156 mrg .addsock_in = nfssvc_addsock_in,
262 1.156 mrg .setexports_in = nfssvc_setexports_in,
263 1.156 mrg .nsd_in = nfssvc_nsd_in,
264 1.156 mrg .nsd_out = nfssvc_nsd_out,
265 1.156 mrg .exp_in = nfssvc_exp_in,
266 1.156 mrg };
267 1.1 cgd
268 1.1 cgd /*
269 1.19 fvdl * Nfs server pseudo system call for the nfsd's
270 1.10 mycroft * Based on the flag value it either:
271 1.10 mycroft * - adds a socket to the selection list
272 1.10 mycroft * - remains in the kernel as an nfsd
273 1.10 mycroft * - remains in the kernel as an nfsiod
274 1.1 cgd */
275 1.156 mrg
276 1.18 christos int
277 1.128 dsl sys_nfssvc(struct lwp *l, const struct sys_nfssvc_args *uap, register_t *retval)
278 1.15 thorpej {
279 1.128 dsl /* {
280 1.13 cgd syscallarg(int) flag;
281 1.107 christos syscallarg(void *) argp;
282 1.128 dsl } */
283 1.156 mrg int flag = SCARG(uap, flag);
284 1.156 mrg void *argp = SCARG(uap, argp);
285 1.156 mrg
286 1.156 mrg return do_nfssvc(&native_ops, l, flag, argp, retval);
287 1.156 mrg }
288 1.156 mrg
289 1.156 mrg int
290 1.156 mrg do_nfssvc(struct nfssvc_copy_ops *ops, struct lwp *l, int flag, void *argp, register_t *retval)
291 1.156 mrg {
292 1.23 thorpej int error;
293 1.132 ad file_t *fp;
294 1.10 mycroft struct mbuf *nam;
295 1.10 mycroft struct nfsd_args nfsdarg;
296 1.10 mycroft struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
297 1.159 riastrad struct nfsd *nfsd = NULL;
298 1.10 mycroft struct nfssvc_sock *slp;
299 1.18 christos struct nfsuid *nuidp;
300 1.1 cgd
301 1.131 elad error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
302 1.131 elad KAUTH_REQ_NETWORK_NFS_SVC, NULL, NULL, NULL);
303 1.71 thorpej if (error)
304 1.1 cgd return (error);
305 1.83 thorpej
306 1.111 yamt mutex_enter(&nfsd_lock);
307 1.12 mycroft while (nfssvc_sockhead_flag & SLP_INIT) {
308 1.111 yamt cv_wait(&nfsd_initcv, &nfsd_lock);
309 1.10 mycroft }
310 1.111 yamt mutex_exit(&nfsd_lock);
311 1.142 ad
312 1.156 mrg if (flag & NFSSVC_BIOD) {
313 1.141 ad /* Dummy implementation of nfsios for 1.4 and earlier. */
314 1.141 ad error = kpause("nfsbiod", true, 0, NULL);
315 1.156 mrg } else if (flag & NFSSVC_MNTD) {
316 1.10 mycroft error = ENOSYS;
317 1.156 mrg } else if (flag & NFSSVC_ADDSOCK) {
318 1.156 mrg error = ops->addsock_in(&nfsdarg, argp);
319 1.18 christos if (error)
320 1.10 mycroft return (error);
321 1.35 thorpej /* getsock() will use the descriptor for us */
322 1.137 ad if ((fp = fd_getfile(nfsdarg.sock)) == NULL)
323 1.137 ad return (EBADF);
324 1.137 ad if (fp->f_type != DTYPE_SOCKET) {
325 1.137 ad fd_putfile(nfsdarg.sock);
326 1.137 ad return (ENOTSOCK);
327 1.137 ad }
328 1.10 mycroft /*
329 1.10 mycroft * Get the client address for connected sockets.
330 1.10 mycroft */
331 1.10 mycroft if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
332 1.10 mycroft nam = (struct mbuf *)0;
333 1.18 christos else {
334 1.18 christos error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
335 1.160 christos UIO_USERSPACE, MT_SONAME);
336 1.35 thorpej if (error) {
337 1.132 ad fd_putfile(nfsdarg.sock);
338 1.18 christos return (error);
339 1.35 thorpej }
340 1.18 christos }
341 1.10 mycroft error = nfssvc_addsock(fp, nam);
342 1.132 ad fd_putfile(nfsdarg.sock);
343 1.156 mrg } else if (flag & NFSSVC_SETEXPORTSLIST) {
344 1.82 jmmv struct export_args *args;
345 1.82 jmmv struct mountd_exports_list mel;
346 1.82 jmmv
347 1.156 mrg error = ops->setexports_in(&mel, argp);
348 1.82 jmmv if (error != 0)
349 1.82 jmmv return error;
350 1.82 jmmv
351 1.82 jmmv args = (struct export_args *)malloc(mel.mel_nexports *
352 1.82 jmmv sizeof(struct export_args), M_TEMP, M_WAITOK);
353 1.156 mrg error = ops->exp_in(args, mel.mel_exports, mel.mel_nexports);
354 1.82 jmmv if (error != 0) {
355 1.82 jmmv free(args, M_TEMP);
356 1.82 jmmv return error;
357 1.82 jmmv }
358 1.82 jmmv mel.mel_exports = args;
359 1.82 jmmv
360 1.149 christos error = mountd_set_exports_list(&mel, l, NULL);
361 1.82 jmmv
362 1.82 jmmv free(args, M_TEMP);
363 1.10 mycroft } else {
364 1.156 mrg error = ops->nsd_in(nsd, argp);
365 1.18 christos if (error)
366 1.10 mycroft return (error);
367 1.159 riastrad if ((uintptr_t)nsd->nsd_nfsd != 0 &&
368 1.159 riastrad (nfsd = nfsd_get(nsd->nsd_nfsd)) == NULL)
369 1.159 riastrad return (EINVAL);
370 1.156 mrg if ((flag & NFSSVC_AUTHIN) &&
371 1.159 riastrad nfsd != NULL &&
372 1.127 yamt (nfsd->nfsd_slp->ns_flags & SLP_VALID)) {
373 1.19 fvdl slp = nfsd->nfsd_slp;
374 1.10 mycroft
375 1.10 mycroft /*
376 1.10 mycroft * First check to see if another nfsd has already
377 1.10 mycroft * added this credential.
378 1.10 mycroft */
379 1.92 elad LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
380 1.57 yamt nu_hash) {
381 1.93 yamt if (kauth_cred_geteuid(nuidp->nu_cr) ==
382 1.93 yamt nsd->nsd_cr.cr_uid &&
383 1.19 fvdl (!nfsd->nfsd_nd->nd_nam2 ||
384 1.19 fvdl netaddr_match(NU_NETFAM(nuidp),
385 1.19 fvdl &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
386 1.10 mycroft break;
387 1.10 mycroft }
388 1.19 fvdl if (nuidp) {
389 1.93 yamt kauth_cred_hold(nuidp->nu_cr);
390 1.93 yamt nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
391 1.19 fvdl nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
392 1.19 fvdl } else {
393 1.10 mycroft /*
394 1.10 mycroft * Nope, so we will.
395 1.10 mycroft */
396 1.10 mycroft if (slp->ns_numuids < nuidhash_max) {
397 1.10 mycroft slp->ns_numuids++;
398 1.130 yamt nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
399 1.10 mycroft } else
400 1.10 mycroft nuidp = (struct nfsuid *)0;
401 1.127 yamt if ((slp->ns_flags & SLP_VALID) == 0) {
402 1.10 mycroft if (nuidp)
403 1.130 yamt kmem_free(nuidp, sizeof(*nuidp));
404 1.10 mycroft } else {
405 1.10 mycroft if (nuidp == (struct nfsuid *)0) {
406 1.57 yamt nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
407 1.12 mycroft LIST_REMOVE(nuidp, nu_hash);
408 1.12 mycroft TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
409 1.12 mycroft nu_lru);
410 1.19 fvdl if (nuidp->nu_flag & NU_NAM)
411 1.19 fvdl m_freem(nuidp->nu_nam);
412 1.10 mycroft }
413 1.19 fvdl nuidp->nu_flag = 0;
414 1.98 pooka kauth_uucred_to_cred(nuidp->nu_cr,
415 1.98 pooka &nsd->nsd_cr);
416 1.19 fvdl nuidp->nu_timestamp = nsd->nsd_timestamp;
417 1.95 kardel nuidp->nu_expire = time_second + nsd->nsd_ttl;
418 1.19 fvdl /*
419 1.19 fvdl * and save the session key in nu_key.
420 1.19 fvdl */
421 1.32 perry memcpy(nuidp->nu_key, nsd->nsd_key,
422 1.32 perry sizeof(nsd->nsd_key));
423 1.19 fvdl if (nfsd->nfsd_nd->nd_nam2) {
424 1.19 fvdl struct sockaddr_in *saddr;
425 1.19 fvdl
426 1.19 fvdl saddr = mtod(nfsd->nfsd_nd->nd_nam2,
427 1.19 fvdl struct sockaddr_in *);
428 1.19 fvdl switch (saddr->sin_family) {
429 1.19 fvdl case AF_INET:
430 1.19 fvdl nuidp->nu_flag |= NU_INETADDR;
431 1.19 fvdl nuidp->nu_inetaddr =
432 1.19 fvdl saddr->sin_addr.s_addr;
433 1.19 fvdl break;
434 1.140 christos case AF_INET6:
435 1.19 fvdl nuidp->nu_flag |= NU_NAM;
436 1.19 fvdl nuidp->nu_nam = m_copym(
437 1.19 fvdl nfsd->nfsd_nd->nd_nam2, 0,
438 1.19 fvdl M_COPYALL, M_WAIT);
439 1.19 fvdl break;
440 1.140 christos default:
441 1.158 maxv kmem_free(nuidp, sizeof(*nuidp));
442 1.140 christos return EAFNOSUPPORT;
443 1.19 fvdl };
444 1.19 fvdl }
445 1.12 mycroft TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
446 1.19 fvdl nu_lru);
447 1.12 mycroft LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
448 1.19 fvdl nuidp, nu_hash);
449 1.93 yamt kauth_cred_hold(nuidp->nu_cr);
450 1.93 yamt nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
451 1.19 fvdl nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
452 1.10 mycroft }
453 1.10 mycroft }
454 1.10 mycroft }
455 1.156 mrg if ((flag & NFSSVC_AUTHINFAIL) &&
456 1.159 riastrad nfsd != NULL)
457 1.19 fvdl nfsd->nfsd_flag |= NFSD_AUTHFAIL;
458 1.156 mrg error = nfssvc_nfsd(ops, nsd, argp, l);
459 1.10 mycroft }
460 1.10 mycroft if (error == EINTR || error == ERESTART)
461 1.10 mycroft error = 0;
462 1.10 mycroft return (error);
463 1.10 mycroft }
464 1.10 mycroft
465 1.65 yamt static struct nfssvc_sock *
466 1.146 cegger nfsrv_sockalloc(void)
467 1.65 yamt {
468 1.65 yamt struct nfssvc_sock *slp;
469 1.78 perry
470 1.130 yamt slp = kmem_alloc(sizeof(*slp), KM_SLEEP);
471 1.65 yamt memset(slp, 0, sizeof (struct nfssvc_sock));
472 1.135 yamt mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
473 1.135 yamt mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET);
474 1.111 yamt cv_init(&slp->ns_cv, "nfsdsock");
475 1.65 yamt TAILQ_INIT(&slp->ns_uidlruhead);
476 1.86 yamt LIST_INIT(&slp->ns_tq);
477 1.86 yamt SIMPLEQ_INIT(&slp->ns_sendq);
478 1.111 yamt mutex_enter(&nfsd_lock);
479 1.65 yamt TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
480 1.111 yamt mutex_exit(&nfsd_lock);
481 1.65 yamt
482 1.65 yamt return slp;
483 1.65 yamt }
484 1.65 yamt
485 1.77 yamt static void
486 1.77 yamt nfsrv_sockfree(struct nfssvc_sock *slp)
487 1.77 yamt {
488 1.77 yamt
489 1.77 yamt KASSERT(slp->ns_so == NULL);
490 1.77 yamt KASSERT(slp->ns_fp == NULL);
491 1.127 yamt KASSERT((slp->ns_flags & SLP_VALID) == 0);
492 1.111 yamt mutex_destroy(&slp->ns_lock);
493 1.127 yamt mutex_destroy(&slp->ns_alock);
494 1.111 yamt cv_destroy(&slp->ns_cv);
495 1.130 yamt kmem_free(slp, sizeof(*slp));
496 1.77 yamt }
497 1.77 yamt
498 1.10 mycroft /*
499 1.10 mycroft * Adds a socket to the list for servicing by nfsds.
500 1.10 mycroft */
501 1.18 christos int
502 1.145 dsl nfssvc_addsock(file_t *fp, struct mbuf *mynam)
503 1.10 mycroft {
504 1.38 augustss int siz;
505 1.38 augustss struct nfssvc_sock *slp;
506 1.38 augustss struct socket *so;
507 1.10 mycroft struct nfssvc_sock *tslp;
508 1.134 ad int error;
509 1.138 plunky int val;
510 1.10 mycroft
511 1.155 matt so = fp->f_socket;
512 1.10 mycroft tslp = (struct nfssvc_sock *)0;
513 1.10 mycroft /*
514 1.10 mycroft * Add it to the list, as required.
515 1.10 mycroft */
516 1.10 mycroft if (so->so_proto->pr_protocol == IPPROTO_UDP) {
517 1.41 fvdl if (so->so_proto->pr_domain->dom_family == AF_INET6)
518 1.41 fvdl tslp = nfs_udp6sock;
519 1.142 ad else {
520 1.142 ad tslp = nfs_udpsock;
521 1.142 ad if (tslp->ns_flags & SLP_VALID) {
522 1.142 ad m_freem(mynam);
523 1.142 ad return (EPERM);
524 1.142 ad }
525 1.10 mycroft }
526 1.10 mycroft }
527 1.10 mycroft if (so->so_type == SOCK_STREAM)
528 1.19 fvdl siz = NFS_MAXPACKET + sizeof (u_long);
529 1.10 mycroft else
530 1.1 cgd siz = NFS_MAXPACKET;
531 1.134 ad solock(so);
532 1.78 perry error = soreserve(so, siz, siz);
533 1.134 ad sounlock(so);
534 1.19 fvdl if (error) {
535 1.10 mycroft m_freem(mynam);
536 1.10 mycroft return (error);
537 1.10 mycroft }
538 1.1 cgd
539 1.1 cgd /*
540 1.1 cgd * Set protocol specific options { for now TCP only } and
541 1.1 cgd * reserve some space. For datagram sockets, this can get called
542 1.1 cgd * repeatedly for the same socket, but that isn't harmful.
543 1.1 cgd */
544 1.10 mycroft if (so->so_type == SOCK_STREAM) {
545 1.138 plunky val = 1;
546 1.138 plunky so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
547 1.138 plunky sizeof(val));
548 1.1 cgd }
549 1.142 ad if ((so->so_proto->pr_domain->dom_family == AF_INET ||
550 1.142 ad so->so_proto->pr_domain->dom_family == AF_INET6) &&
551 1.10 mycroft so->so_proto->pr_protocol == IPPROTO_TCP) {
552 1.138 plunky val = 1;
553 1.138 plunky so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
554 1.138 plunky sizeof(val));
555 1.1 cgd }
556 1.134 ad solock(so);
557 1.1 cgd so->so_rcv.sb_flags &= ~SB_NOINTR;
558 1.1 cgd so->so_rcv.sb_timeo = 0;
559 1.1 cgd so->so_snd.sb_flags &= ~SB_NOINTR;
560 1.1 cgd so->so_snd.sb_timeo = 0;
561 1.134 ad sounlock(so);
562 1.111 yamt if (tslp) {
563 1.10 mycroft slp = tslp;
564 1.111 yamt } else {
565 1.65 yamt slp = nfsrv_sockalloc();
566 1.10 mycroft }
567 1.10 mycroft slp->ns_so = so;
568 1.10 mycroft slp->ns_nam = mynam;
569 1.127 yamt mutex_enter(&fp->f_lock);
570 1.10 mycroft fp->f_count++;
571 1.127 yamt mutex_exit(&fp->f_lock);
572 1.10 mycroft slp->ns_fp = fp;
573 1.127 yamt slp->ns_flags = SLP_VALID;
574 1.127 yamt slp->ns_aflags = SLP_A_NEEDQ;
575 1.127 yamt slp->ns_gflags = 0;
576 1.127 yamt slp->ns_sflags = 0;
577 1.134 ad solock(so);
578 1.107 christos so->so_upcallarg = (void *)slp;
579 1.127 yamt so->so_upcall = nfsrv_soupcall;
580 1.29 matt so->so_rcv.sb_flags |= SB_UPCALL;
581 1.134 ad sounlock(so);
582 1.10 mycroft nfsrv_wakenfsd(slp);
583 1.10 mycroft return (0);
584 1.10 mycroft }
585 1.1 cgd
586 1.10 mycroft /*
587 1.10 mycroft * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
588 1.10 mycroft * until it is killed by a signal.
589 1.10 mycroft */
590 1.156 mrg static int
591 1.156 mrg nfssvc_nfsd(struct nfssvc_copy_ops *ops, struct nfsd_srvargs *nsd,
592 1.156 mrg void *argp, struct lwp *l)
593 1.10 mycroft {
594 1.95 kardel struct timeval tv;
595 1.38 augustss struct mbuf *m;
596 1.38 augustss struct nfssvc_sock *slp;
597 1.159 riastrad struct nfsd *nfsd;
598 1.19 fvdl struct nfsrv_descript *nd = NULL;
599 1.19 fvdl struct mbuf *mreq;
600 1.19 fvdl u_quad_t cur_usec;
601 1.111 yamt int error = 0, cacherep, siz, sotype, writes_todo;
602 1.53 thorpej struct proc *p = l->l_proc;
603 1.127 yamt bool doreinit;
604 1.19 fvdl
605 1.19 fvdl #ifndef nolint
606 1.19 fvdl cacherep = RC_DOIT;
607 1.19 fvdl writes_todo = 0;
608 1.19 fvdl #endif
609 1.159 riastrad /*
610 1.159 riastrad * If userland didn't provide an nfsd cookie, bake a fresh one;
611 1.159 riastrad * if they did provide one, look it up.
612 1.159 riastrad */
613 1.159 riastrad if ((uintptr_t)nsd->nsd_nfsd == 0) {
614 1.159 riastrad nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP);
615 1.111 yamt memset(nfsd, 0, sizeof (struct nfsd));
616 1.111 yamt cv_init(&nfsd->nfsd_cv, "nfsd");
617 1.19 fvdl nfsd->nfsd_procp = p;
618 1.111 yamt mutex_enter(&nfsd_lock);
619 1.127 yamt while ((nfssvc_sockhead_flag & SLP_INIT) != 0) {
620 1.127 yamt KASSERT(nfs_numnfsd == 0);
621 1.127 yamt cv_wait(&nfsd_initcv, &nfsd_lock);
622 1.127 yamt }
623 1.159 riastrad nsd->nsd_nfsd = nfsd_bake_cookie(nfsd);
624 1.10 mycroft nfs_numnfsd++;
625 1.111 yamt mutex_exit(&nfsd_lock);
626 1.159 riastrad } else if ((nfsd = nfsd_get(nsd->nsd_nfsd)) == NULL) {
627 1.159 riastrad return (EINVAL);
628 1.10 mycroft }
629 1.159 riastrad KASSERT(nfsd != NULL);
630 1.159 riastrad KASSERT(nsd->nsd_nfsd != (struct nfsd *)(uintptr_t)0);
631 1.159 riastrad
632 1.1 cgd /*
633 1.10 mycroft * Loop getting rpc requests until SIGKILL.
634 1.1 cgd */
635 1.1 cgd for (;;) {
636 1.127 yamt bool dummy;
637 1.127 yamt
638 1.113 yamt if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
639 1.113 yamt != 0) {
640 1.113 yamt preempt();
641 1.113 yamt }
642 1.85 yamt if (nfsd->nfsd_slp == NULL) {
643 1.111 yamt mutex_enter(&nfsd_lock);
644 1.85 yamt while (nfsd->nfsd_slp == NULL &&
645 1.12 mycroft (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
646 1.66 yamt SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
647 1.66 yamt nfsd_idle);
648 1.111 yamt error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
649 1.66 yamt if (error) {
650 1.66 yamt slp = nfsd->nfsd_slp;
651 1.66 yamt nfsd->nfsd_slp = NULL;
652 1.66 yamt if (!slp)
653 1.66 yamt SLIST_REMOVE(&nfsd_idle_head,
654 1.66 yamt nfsd, nfsd, nfsd_idle);
655 1.111 yamt mutex_exit(&nfsd_lock);
656 1.66 yamt if (slp) {
657 1.66 yamt nfsrv_wakenfsd(slp);
658 1.66 yamt nfsrv_slpderef(slp);
659 1.66 yamt }
660 1.10 mycroft goto done;
661 1.66 yamt }
662 1.10 mycroft }
663 1.85 yamt if (nfsd->nfsd_slp == NULL &&
664 1.12 mycroft (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
665 1.66 yamt slp = TAILQ_FIRST(&nfssvc_sockpending);
666 1.66 yamt if (slp) {
667 1.127 yamt KASSERT((slp->ns_gflags & SLP_G_DOREC)
668 1.121 yamt != 0);
669 1.66 yamt TAILQ_REMOVE(&nfssvc_sockpending, slp,
670 1.66 yamt ns_pending);
671 1.127 yamt slp->ns_gflags &= ~SLP_G_DOREC;
672 1.66 yamt slp->ns_sref++;
673 1.66 yamt nfsd->nfsd_slp = slp;
674 1.66 yamt } else
675 1.12 mycroft nfsd_head_flag &= ~NFSD_CHECKSLP;
676 1.10 mycroft }
677 1.116 yamt KASSERT(nfsd->nfsd_slp == NULL ||
678 1.116 yamt nfsd->nfsd_slp->ns_sref > 0);
679 1.111 yamt mutex_exit(&nfsd_lock);
680 1.85 yamt if ((slp = nfsd->nfsd_slp) == NULL)
681 1.10 mycroft continue;
682 1.127 yamt if (slp->ns_flags & SLP_VALID) {
683 1.127 yamt bool more;
684 1.127 yamt
685 1.127 yamt if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) {
686 1.127 yamt nfsrv_rcv(slp);
687 1.10 mycroft }
688 1.127 yamt if (nfsdsock_testbits(slp, SLP_A_DISCONN)) {
689 1.119 yamt nfsrv_zapsock(slp);
690 1.119 yamt }
691 1.127 yamt error = nfsrv_dorec(slp, nfsd, &nd, &more);
692 1.95 kardel getmicrotime(&tv);
693 1.95 kardel cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
694 1.95 kardel (u_quad_t)tv.tv_usec;
695 1.127 yamt writes_todo = 0;
696 1.127 yamt if (error) {
697 1.127 yamt struct nfsrv_descript *nd2;
698 1.127 yamt
699 1.127 yamt mutex_enter(&nfsd_lock);
700 1.127 yamt nd2 = LIST_FIRST(&slp->ns_tq);
701 1.127 yamt if (nd2 != NULL &&
702 1.127 yamt nd2->nd_time <= cur_usec) {
703 1.127 yamt error = 0;
704 1.127 yamt cacherep = RC_DOIT;
705 1.127 yamt writes_todo = 1;
706 1.127 yamt }
707 1.127 yamt mutex_exit(&nfsd_lock);
708 1.127 yamt }
709 1.127 yamt if (error == 0 && more) {
710 1.85 yamt nfsrv_wakenfsd(slp);
711 1.85 yamt }
712 1.1 cgd }
713 1.10 mycroft } else {
714 1.10 mycroft error = 0;
715 1.19 fvdl slp = nfsd->nfsd_slp;
716 1.10 mycroft }
717 1.116 yamt KASSERT(slp != NULL);
718 1.116 yamt KASSERT(nfsd->nfsd_slp == slp);
719 1.127 yamt if (error || (slp->ns_flags & SLP_VALID) == 0) {
720 1.19 fvdl if (nd) {
721 1.93 yamt nfsdreq_free(nd);
722 1.19 fvdl nd = NULL;
723 1.19 fvdl }
724 1.85 yamt nfsd->nfsd_slp = NULL;
725 1.10 mycroft nfsrv_slpderef(slp);
726 1.1 cgd continue;
727 1.1 cgd }
728 1.86 yamt sotype = slp->ns_so->so_type;
729 1.19 fvdl if (nd) {
730 1.95 kardel getmicrotime(&nd->nd_starttime);
731 1.63 yamt if (nd->nd_nam2)
732 1.63 yamt nd->nd_nam = nd->nd_nam2;
733 1.63 yamt else
734 1.63 yamt nd->nd_nam = slp->ns_nam;
735 1.63 yamt
736 1.63 yamt /*
737 1.63 yamt * Check to see if authorization is needed.
738 1.63 yamt */
739 1.63 yamt if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
740 1.63 yamt nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
741 1.63 yamt nsd->nsd_haddr = mtod(nd->nd_nam,
742 1.63 yamt struct sockaddr_in *)->sin_addr.s_addr;
743 1.63 yamt nsd->nsd_authlen = nfsd->nfsd_authlen;
744 1.63 yamt nsd->nsd_verflen = nfsd->nfsd_verflen;
745 1.63 yamt if (!copyout(nfsd->nfsd_authstr,
746 1.63 yamt nsd->nsd_authstr, nfsd->nfsd_authlen) &&
747 1.63 yamt !copyout(nfsd->nfsd_verfstr,
748 1.63 yamt nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
749 1.156 mrg !ops->nsd_out(argp, nsd)) {
750 1.63 yamt return (ENEEDAUTH);
751 1.63 yamt }
752 1.63 yamt cacherep = RC_DROPIT;
753 1.10 mycroft } else
754 1.63 yamt cacherep = nfsrv_getcache(nd, slp, &mreq);
755 1.63 yamt
756 1.100 yamt if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
757 1.63 yamt nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
758 1.10 mycroft nd->nd_procnum = NFSPROC_NOOP;
759 1.63 yamt nd->nd_repstat =
760 1.63 yamt (NFSERR_AUTHERR | AUTH_TOOWEAK);
761 1.10 mycroft cacherep = RC_DOIT;
762 1.63 yamt }
763 1.10 mycroft }
764 1.10 mycroft
765 1.19 fvdl /*
766 1.19 fvdl * Loop to get all the write rpc relies that have been
767 1.19 fvdl * gathered together.
768 1.19 fvdl */
769 1.19 fvdl do {
770 1.63 yamt switch (cacherep) {
771 1.63 yamt case RC_DOIT:
772 1.80 onoe mreq = NULL;
773 1.88 yamt netexport_rdlock();
774 1.90 christos if (writes_todo || nd == NULL ||
775 1.90 christos (!(nd->nd_flag & ND_NFSV3) &&
776 1.63 yamt nd->nd_procnum == NFSPROC_WRITE &&
777 1.100 yamt nfsrvw_procrastinate > 0))
778 1.63 yamt error = nfsrv_writegather(&nd, slp,
779 1.84 christos l, &mreq);
780 1.63 yamt else
781 1.63 yamt error =
782 1.63 yamt (*(nfsrv3_procs[nd->nd_procnum]))
783 1.84 christos (nd, slp, l, &mreq);
784 1.88 yamt netexport_rdunlock();
785 1.80 onoe if (mreq == NULL) {
786 1.80 onoe if (nd != NULL) {
787 1.80 onoe if (nd->nd_nam2)
788 1.80 onoe m_free(nd->nd_nam2);
789 1.80 onoe }
790 1.63 yamt break;
791 1.80 onoe }
792 1.63 yamt if (error) {
793 1.100 yamt nfsstats.srv_errs++;
794 1.154 christos if (nd) {
795 1.154 christos nfsrv_updatecache(nd, false,
796 1.154 christos mreq);
797 1.154 christos if (nd->nd_nam2)
798 1.154 christos m_freem(nd->nd_nam2);
799 1.154 christos }
800 1.63 yamt break;
801 1.63 yamt }
802 1.154 christos if (nd) {
803 1.154 christos nfsstats.srvrpccnt[nd->nd_procnum]++;
804 1.154 christos nfsrv_updatecache(nd, true, mreq);
805 1.154 christos nd->nd_mrep = NULL;
806 1.154 christos }
807 1.160.2.1 christos /* FALLTHROUGH */
808 1.63 yamt case RC_REPLY:
809 1.63 yamt m = mreq;
810 1.63 yamt siz = 0;
811 1.63 yamt while (m) {
812 1.63 yamt siz += m->m_len;
813 1.63 yamt m = m->m_next;
814 1.63 yamt }
815 1.63 yamt if (siz <= 0 || siz > NFS_MAXPACKET) {
816 1.63 yamt printf("mbuf siz=%d\n",siz);
817 1.63 yamt panic("Bad nfs svc reply");
818 1.63 yamt }
819 1.63 yamt m = mreq;
820 1.63 yamt m->m_pkthdr.len = siz;
821 1.157 ozaki m_reset_rcvif(m);
822 1.63 yamt /*
823 1.63 yamt * For stream protocols, prepend a Sun RPC
824 1.63 yamt * Record Mark.
825 1.63 yamt */
826 1.63 yamt if (sotype == SOCK_STREAM) {
827 1.63 yamt M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
828 1.63 yamt *mtod(m, u_int32_t *) =
829 1.63 yamt htonl(0x80000000 | siz);
830 1.63 yamt }
831 1.154 christos if (nd) {
832 1.154 christos nd->nd_mreq = m;
833 1.154 christos if (nfsrtton) {
834 1.154 christos nfsd_rt(slp->ns_so->so_type, nd,
835 1.154 christos cacherep);
836 1.154 christos }
837 1.154 christos error = nfsdsock_sendreply(slp, nd);
838 1.154 christos nd = NULL;
839 1.63 yamt }
840 1.63 yamt if (error == EPIPE)
841 1.63 yamt nfsrv_zapsock(slp);
842 1.63 yamt if (error == EINTR || error == ERESTART) {
843 1.114 yamt nfsd->nfsd_slp = NULL;
844 1.63 yamt nfsrv_slpderef(slp);
845 1.63 yamt goto done;
846 1.63 yamt }
847 1.19 fvdl break;
848 1.63 yamt case RC_DROPIT:
849 1.154 christos if (nd) {
850 1.154 christos if (nfsrtton)
851 1.154 christos nfsd_rt(sotype, nd, cacherep);
852 1.154 christos m_freem(nd->nd_mrep);
853 1.154 christos m_freem(nd->nd_nam2);
854 1.154 christos }
855 1.1 cgd break;
856 1.1 cgd }
857 1.63 yamt if (nd) {
858 1.93 yamt nfsdreq_free(nd);
859 1.63 yamt nd = NULL;
860 1.63 yamt }
861 1.63 yamt
862 1.1 cgd /*
863 1.63 yamt * Check to see if there are outstanding writes that
864 1.63 yamt * need to be serviced.
865 1.1 cgd */
866 1.95 kardel getmicrotime(&tv);
867 1.95 kardel cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
868 1.95 kardel (u_quad_t)tv.tv_usec;
869 1.147 bouyer mutex_enter(&nfsd_lock);
870 1.63 yamt if (LIST_FIRST(&slp->ns_tq) &&
871 1.63 yamt LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
872 1.63 yamt cacherep = RC_DOIT;
873 1.63 yamt writes_todo = 1;
874 1.63 yamt } else
875 1.63 yamt writes_todo = 0;
876 1.147 bouyer mutex_exit(&nfsd_lock);
877 1.19 fvdl } while (writes_todo);
878 1.127 yamt if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) {
879 1.19 fvdl nfsd->nfsd_slp = NULL;
880 1.10 mycroft nfsrv_slpderef(slp);
881 1.10 mycroft }
882 1.1 cgd }
883 1.10 mycroft done:
884 1.111 yamt mutex_enter(&nfsd_lock);
885 1.159 riastrad nfsd_toss_cookie(nfsd);
886 1.127 yamt doreinit = --nfs_numnfsd == 0;
887 1.127 yamt if (doreinit)
888 1.127 yamt nfssvc_sockhead_flag |= SLP_INIT;
889 1.111 yamt mutex_exit(&nfsd_lock);
890 1.111 yamt cv_destroy(&nfsd->nfsd_cv);
891 1.130 yamt kmem_free(nfsd, sizeof(*nfsd));
892 1.159 riastrad KASSERT(nsd->nsd_nfsd != (struct nfsd *)(uintptr_t)0);
893 1.159 riastrad nsd->nsd_nfsd = (struct nfsd *)(uintptr_t)0;
894 1.127 yamt if (doreinit)
895 1.106 thorpej nfsrv_init(true); /* Reinitialize everything */
896 1.1 cgd return (error);
897 1.1 cgd }
898 1.1 cgd
899 1.10 mycroft /*
900 1.10 mycroft * Shut down a socket associated with an nfssvc_sock structure.
901 1.10 mycroft * Should be called with the send lock set, if required.
902 1.10 mycroft * The trick here is to increment the sref at the start, so that the nfsds
903 1.10 mycroft * will stop using it and clear ns_flag at the end so that it will not be
904 1.10 mycroft * reassigned during cleanup.
905 1.73 yamt *
906 1.73 yamt * called at splsoftnet.
907 1.10 mycroft */
908 1.18 christos void
909 1.145 dsl nfsrv_zapsock(struct nfssvc_sock *slp)
910 1.10 mycroft {
911 1.38 augustss struct nfsuid *nuidp, *nnuidp;
912 1.127 yamt struct nfsrv_descript *nwp;
913 1.10 mycroft struct socket *so;
914 1.104 yamt struct mbuf *m;
915 1.10 mycroft
916 1.85 yamt if (nfsdsock_drain(slp)) {
917 1.74 yamt return;
918 1.74 yamt }
919 1.111 yamt mutex_enter(&nfsd_lock);
920 1.127 yamt if (slp->ns_gflags & SLP_G_DOREC) {
921 1.73 yamt TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
922 1.127 yamt slp->ns_gflags &= ~SLP_G_DOREC;
923 1.73 yamt }
924 1.111 yamt mutex_exit(&nfsd_lock);
925 1.77 yamt
926 1.77 yamt so = slp->ns_so;
927 1.77 yamt KASSERT(so != NULL);
928 1.134 ad solock(so);
929 1.77 yamt so->so_upcall = NULL;
930 1.77 yamt so->so_upcallarg = NULL;
931 1.77 yamt so->so_rcv.sb_flags &= ~SB_UPCALL;
932 1.77 yamt soshutdown(so, SHUT_RDWR);
933 1.134 ad sounlock(so);
934 1.77 yamt
935 1.77 yamt m_freem(slp->ns_raw);
936 1.104 yamt m = slp->ns_rec;
937 1.104 yamt while (m != NULL) {
938 1.104 yamt struct mbuf *n;
939 1.104 yamt
940 1.104 yamt n = m->m_nextpkt;
941 1.104 yamt m_freem(m);
942 1.104 yamt m = n;
943 1.104 yamt }
944 1.147 bouyer /* XXX what about freeing ns_frag ? */
945 1.77 yamt for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
946 1.77 yamt nuidp = nnuidp) {
947 1.77 yamt nnuidp = TAILQ_NEXT(nuidp, nu_lru);
948 1.77 yamt LIST_REMOVE(nuidp, nu_hash);
949 1.77 yamt TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
950 1.77 yamt if (nuidp->nu_flag & NU_NAM)
951 1.77 yamt m_freem(nuidp->nu_nam);
952 1.130 yamt kmem_free(nuidp, sizeof(*nuidp));
953 1.77 yamt }
954 1.127 yamt mutex_enter(&nfsd_lock);
955 1.127 yamt while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) {
956 1.77 yamt LIST_REMOVE(nwp, nd_tq);
957 1.127 yamt mutex_exit(&nfsd_lock);
958 1.93 yamt nfsdreq_free(nwp);
959 1.127 yamt mutex_enter(&nfsd_lock);
960 1.10 mycroft }
961 1.127 yamt mutex_exit(&nfsd_lock);
962 1.10 mycroft }
963 1.10 mycroft
964 1.10 mycroft /*
965 1.10 mycroft * Derefence a server socket structure. If it has no more references and
966 1.10 mycroft * is no longer valid, you can throw it away.
967 1.10 mycroft */
968 1.10 mycroft void
969 1.145 dsl nfsrv_slpderef(struct nfssvc_sock *slp)
970 1.10 mycroft {
971 1.116 yamt uint32_t ref;
972 1.66 yamt
973 1.116 yamt mutex_enter(&nfsd_lock);
974 1.116 yamt KASSERT(slp->ns_sref > 0);
975 1.116 yamt ref = --slp->ns_sref;
976 1.127 yamt if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) {
977 1.132 ad file_t *fp;
978 1.111 yamt
979 1.127 yamt KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0);
980 1.12 mycroft TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
981 1.111 yamt mutex_exit(&nfsd_lock);
982 1.85 yamt
983 1.85 yamt fp = slp->ns_fp;
984 1.85 yamt if (fp != NULL) {
985 1.85 yamt slp->ns_fp = NULL;
986 1.85 yamt KASSERT(fp != NULL);
987 1.155 matt KASSERT(fp->f_socket == slp->ns_so);
988 1.132 ad KASSERT(fp->f_count > 0);
989 1.132 ad closef(fp);
990 1.85 yamt slp->ns_so = NULL;
991 1.85 yamt }
992 1.147 bouyer
993 1.147 bouyer if (slp->ns_nam)
994 1.147 bouyer m_free(slp->ns_nam);
995 1.77 yamt nfsrv_sockfree(slp);
996 1.147 bouyer } else
997 1.147 bouyer mutex_exit(&nfsd_lock);
998 1.10 mycroft }
999 1.10 mycroft
1000 1.10 mycroft /*
1001 1.10 mycroft * Initialize the data structures for the server.
1002 1.10 mycroft * Handshake with any new nfsds starting up to avoid any chance of
1003 1.10 mycroft * corruption.
1004 1.10 mycroft */
1005 1.10 mycroft void
1006 1.145 dsl nfsrv_init(int terminating)
1007 1.10 mycroft {
1008 1.66 yamt struct nfssvc_sock *slp;
1009 1.10 mycroft
1010 1.111 yamt if (!terminating) {
1011 1.135 yamt mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
1012 1.111 yamt cv_init(&nfsd_initcv, "nfsdinit");
1013 1.111 yamt }
1014 1.111 yamt
1015 1.111 yamt mutex_enter(&nfsd_lock);
1016 1.127 yamt if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0)
1017 1.10 mycroft panic("nfsd init");
1018 1.12 mycroft nfssvc_sockhead_flag |= SLP_INIT;
1019 1.66 yamt
1020 1.10 mycroft if (terminating) {
1021 1.114 yamt KASSERT(SLIST_EMPTY(&nfsd_idle_head));
1022 1.159 riastrad KASSERT(RB_TREE_MIN(&nfsd_tree) == NULL);
1023 1.66 yamt while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
1024 1.111 yamt mutex_exit(&nfsd_lock);
1025 1.85 yamt KASSERT(slp->ns_sref == 0);
1026 1.85 yamt slp->ns_sref++;
1027 1.85 yamt nfsrv_zapsock(slp);
1028 1.85 yamt nfsrv_slpderef(slp);
1029 1.111 yamt mutex_enter(&nfsd_lock);
1030 1.10 mycroft }
1031 1.114 yamt KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
1032 1.111 yamt mutex_exit(&nfsd_lock);
1033 1.10 mycroft nfsrv_cleancache(); /* And clear out server cache */
1034 1.66 yamt } else {
1035 1.111 yamt mutex_exit(&nfsd_lock);
1036 1.26 fvdl nfs_pub.np_valid = 0;
1037 1.66 yamt }
1038 1.12 mycroft
1039 1.12 mycroft TAILQ_INIT(&nfssvc_sockhead);
1040 1.66 yamt TAILQ_INIT(&nfssvc_sockpending);
1041 1.12 mycroft
1042 1.159 riastrad rb_tree_init(&nfsd_tree, &nfsd_tree_ops);
1043 1.66 yamt SLIST_INIT(&nfsd_idle_head);
1044 1.12 mycroft nfsd_head_flag &= ~NFSD_CHECKSLP;
1045 1.12 mycroft
1046 1.65 yamt nfs_udpsock = nfsrv_sockalloc();
1047 1.65 yamt nfs_udp6sock = nfsrv_sockalloc();
1048 1.66 yamt
1049 1.111 yamt mutex_enter(&nfsd_lock);
1050 1.111 yamt nfssvc_sockhead_flag &= ~SLP_INIT;
1051 1.111 yamt cv_broadcast(&nfsd_initcv);
1052 1.111 yamt mutex_exit(&nfsd_lock);
1053 1.10 mycroft }
1054 1.10 mycroft
1055 1.142 ad void
1056 1.142 ad nfsrv_fini(void)
1057 1.142 ad {
1058 1.142 ad
1059 1.142 ad nfsrv_init(true);
1060 1.142 ad cv_destroy(&nfsd_initcv);
1061 1.142 ad mutex_destroy(&nfsd_lock);
1062 1.142 ad }
1063 1.142 ad
1064 1.10 mycroft /*
1065 1.10 mycroft * Add entries to the server monitor log.
1066 1.10 mycroft */
1067 1.10 mycroft static void
1068 1.145 dsl nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
1069 1.10 mycroft {
1070 1.95 kardel struct timeval tv;
1071 1.38 augustss struct drt *rt;
1072 1.10 mycroft
1073 1.10 mycroft rt = &nfsdrt.drt[nfsdrt.pos];
1074 1.10 mycroft if (cacherep == RC_DOIT)
1075 1.10 mycroft rt->flag = 0;
1076 1.10 mycroft else if (cacherep == RC_REPLY)
1077 1.10 mycroft rt->flag = DRT_CACHEREPLY;
1078 1.10 mycroft else
1079 1.10 mycroft rt->flag = DRT_CACHEDROP;
1080 1.10 mycroft if (sotype == SOCK_STREAM)
1081 1.10 mycroft rt->flag |= DRT_TCP;
1082 1.102 yamt if (nd->nd_flag & ND_NFSV3)
1083 1.19 fvdl rt->flag |= DRT_NFSV3;
1084 1.10 mycroft rt->proc = nd->nd_procnum;
1085 1.19 fvdl if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
1086 1.19 fvdl rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
1087 1.10 mycroft else
1088 1.19 fvdl rt->ipadr = INADDR_ANY;
1089 1.95 kardel getmicrotime(&tv);
1090 1.95 kardel rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
1091 1.95 kardel (tv.tv_usec - nd->nd_starttime.tv_usec);
1092 1.95 kardel rt->tstamp = tv;
1093 1.10 mycroft nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
1094 1.10 mycroft }
1095