nfs_commonport.c revision 1.2 1 1.2 pgoyette /* $NetBSD: nfs_commonport.c,v 1.2 2016/12/13 22:31:51 pgoyette Exp $ */
2 1.1 dholland /*-
3 1.1 dholland * Copyright (c) 1989, 1993
4 1.1 dholland * The Regents of the University of California. All rights reserved.
5 1.1 dholland *
6 1.1 dholland * This code is derived from software contributed to Berkeley by
7 1.1 dholland * Rick Macklem at The University of Guelph.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland * 4. Neither the name of the University nor the names of its contributors
18 1.1 dholland * may be used to endorse or promote products derived from this software
19 1.1 dholland * without specific prior written permission.
20 1.1 dholland *
21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 dholland * SUCH DAMAGE.
32 1.1 dholland *
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland #include <sys/cdefs.h>
36 1.2 pgoyette /* __FBSDID("FreeBSD: head/sys/fs/nfs/nfs_commonport.c 304026 2016-08-12 22:44:59Z rmacklem "); */
37 1.2 pgoyette __RCSID("$NetBSD: nfs_commonport.c,v 1.2 2016/12/13 22:31:51 pgoyette Exp $");
38 1.1 dholland
39 1.1 dholland /*
40 1.1 dholland * Functions that need to be different for different versions of BSD
41 1.1 dholland * kernel should be kept here, along with any global storage specific
42 1.1 dholland * to this BSD variant.
43 1.1 dholland */
44 1.2 pgoyette #include <fs/nfs/common/nfsport.h>
45 1.1 dholland #include <sys/sysctl.h>
46 1.1 dholland #include <vm/vm.h>
47 1.1 dholland #include <vm/vm_object.h>
48 1.1 dholland #include <vm/vm_page.h>
49 1.1 dholland #include <vm/vm_param.h>
50 1.1 dholland #include <vm/vm_map.h>
51 1.1 dholland #include <vm/vm_kern.h>
52 1.1 dholland #include <vm/vm_extern.h>
53 1.1 dholland #include <vm/uma.h>
54 1.1 dholland
55 1.1 dholland extern int nfscl_ticks;
56 1.1 dholland extern int nfsrv_nfsuserd;
57 1.1 dholland extern struct nfssockreq nfsrv_nfsuserdsock;
58 1.1 dholland extern void (*nfsd_call_recall)(struct vnode *, int, struct ucred *,
59 1.1 dholland struct thread *);
60 1.1 dholland extern int nfsrv_useacl;
61 1.1 dholland struct mount nfsv4root_mnt;
62 1.1 dholland int newnfs_numnfsd = 0;
63 1.2 pgoyette struct nfsstatsv1 nfsstatsv1;
64 1.1 dholland int nfs_numnfscbd = 0;
65 1.1 dholland int nfscl_debuglevel = 0;
66 1.1 dholland char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
67 1.1 dholland struct callout newnfsd_callout;
68 1.2 pgoyette int nfsrv_lughashsize = 100;
69 1.1 dholland void (*nfsd_call_servertimer)(void) = NULL;
70 1.1 dholland void (*ncl_call_invalcaches)(struct vnode *) = NULL;
71 1.1 dholland
72 1.1 dholland static int nfs_realign_test;
73 1.1 dholland static int nfs_realign_count;
74 1.2 pgoyette static struct ext_nfsstats oldnfsstats;
75 1.1 dholland
76 1.2 pgoyette SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
77 1.1 dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test,
78 1.1 dholland 0, "Number of realign tests done");
79 1.1 dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count,
80 1.1 dholland 0, "Number of mbuf realignments done");
81 1.1 dholland SYSCTL_STRING(_vfs_nfs, OID_AUTO, callback_addr, CTLFLAG_RW,
82 1.1 dholland nfsv4_callbackaddr, sizeof(nfsv4_callbackaddr),
83 1.1 dholland "NFSv4 callback addr for server to use");
84 1.1 dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nfscl_debuglevel,
85 1.2 pgoyette 0, "Debug level for NFS client");
86 1.2 pgoyette SYSCTL_INT(_vfs_nfs, OID_AUTO, userhashsize, CTLFLAG_RDTUN, &nfsrv_lughashsize,
87 1.2 pgoyette 0, "Size of hash tables for uid/name mapping");
88 1.1 dholland
89 1.1 dholland /*
90 1.1 dholland * Defines for malloc
91 1.1 dholland * (Here for FreeBSD, since they allocate storage.)
92 1.1 dholland */
93 1.1 dholland MALLOC_DEFINE(M_NEWNFSRVCACHE, "NFSD srvcache", "NFSD Server Request Cache");
94 1.1 dholland MALLOC_DEFINE(M_NEWNFSDCLIENT, "NFSD V4client", "NFSD V4 Client Id");
95 1.1 dholland MALLOC_DEFINE(M_NEWNFSDSTATE, "NFSD V4state",
96 1.1 dholland "NFSD V4 State (Openowner, Open, Lockowner, Delegation");
97 1.1 dholland MALLOC_DEFINE(M_NEWNFSDLOCK, "NFSD V4lock", "NFSD V4 byte range lock");
98 1.1 dholland MALLOC_DEFINE(M_NEWNFSDLOCKFILE, "NFSD lckfile", "NFSD Open/Lock file");
99 1.1 dholland MALLOC_DEFINE(M_NEWNFSSTRING, "NFSD string", "NFSD V4 long string");
100 1.1 dholland MALLOC_DEFINE(M_NEWNFSUSERGROUP, "NFSD usrgroup", "NFSD V4 User/group map");
101 1.1 dholland MALLOC_DEFINE(M_NEWNFSDREQ, "NFS req", "NFS request header");
102 1.1 dholland MALLOC_DEFINE(M_NEWNFSFH, "NFS fh", "NFS file handle");
103 1.1 dholland MALLOC_DEFINE(M_NEWNFSCLOWNER, "NFSCL owner", "NFSCL Open Owner");
104 1.1 dholland MALLOC_DEFINE(M_NEWNFSCLOPEN, "NFSCL open", "NFSCL Open");
105 1.1 dholland MALLOC_DEFINE(M_NEWNFSCLDELEG, "NFSCL deleg", "NFSCL Delegation");
106 1.1 dholland MALLOC_DEFINE(M_NEWNFSCLCLIENT, "NFSCL client", "NFSCL Client");
107 1.1 dholland MALLOC_DEFINE(M_NEWNFSCLLOCKOWNER, "NFSCL lckown", "NFSCL Lock Owner");
108 1.1 dholland MALLOC_DEFINE(M_NEWNFSCLLOCK, "NFSCL lck", "NFSCL Lock");
109 1.2 pgoyette MALLOC_DEFINE(M_NEWNFSV4NODE, "NEWNFSnode", "NFS vnode");
110 1.2 pgoyette MALLOC_DEFINE(M_NEWNFSDIRECTIO, "NEWdirectio", "NFS Direct IO buffer");
111 1.1 dholland MALLOC_DEFINE(M_NEWNFSDIROFF, "NFSCL diroffdiroff",
112 1.2 pgoyette "NFS directory offset data");
113 1.1 dholland MALLOC_DEFINE(M_NEWNFSDROLLBACK, "NFSD rollback",
114 1.2 pgoyette "NFS local lock rollback");
115 1.1 dholland MALLOC_DEFINE(M_NEWNFSLAYOUT, "NFSCL layout", "NFSv4.1 Layout");
116 1.1 dholland MALLOC_DEFINE(M_NEWNFSFLAYOUT, "NFSCL flayout", "NFSv4.1 File Layout");
117 1.1 dholland MALLOC_DEFINE(M_NEWNFSDEVINFO, "NFSCL devinfo", "NFSv4.1 Device Info");
118 1.1 dholland MALLOC_DEFINE(M_NEWNFSSOCKREQ, "NFSCL sockreq", "NFS Sock Req");
119 1.1 dholland MALLOC_DEFINE(M_NEWNFSCLDS, "NFSCL session", "NFSv4.1 Session");
120 1.1 dholland MALLOC_DEFINE(M_NEWNFSLAYRECALL, "NFSCL layrecall", "NFSv4.1 Layout Recall");
121 1.2 pgoyette MALLOC_DEFINE(M_NEWNFSDSESSION, "NFSD session", "NFSD Session for a client");
122 1.1 dholland
123 1.1 dholland /*
124 1.1 dholland * Definition of mutex locks.
125 1.1 dholland * newnfsd_mtx is used in nfsrvd_nfsd() to protect the nfs socket list
126 1.1 dholland * and assorted other nfsd structures.
127 1.1 dholland */
128 1.1 dholland struct mtx newnfsd_mtx;
129 1.1 dholland struct mtx nfs_sockl_mutex;
130 1.1 dholland struct mtx nfs_state_mutex;
131 1.1 dholland struct mtx nfs_nameid_mutex;
132 1.1 dholland struct mtx nfs_req_mutex;
133 1.1 dholland struct mtx nfs_slock_mutex;
134 1.1 dholland
135 1.1 dholland /* local functions */
136 1.1 dholland static int nfssvc_call(struct thread *, struct nfssvc_args *, struct ucred *);
137 1.1 dholland
138 1.1 dholland #ifdef __NO_STRICT_ALIGNMENT
139 1.1 dholland /*
140 1.1 dholland * These architectures don't need re-alignment, so just return.
141 1.1 dholland */
142 1.1 dholland int
143 1.1 dholland newnfs_realign(struct mbuf **pm, int how)
144 1.1 dholland {
145 1.1 dholland
146 1.1 dholland return (0);
147 1.1 dholland }
148 1.1 dholland #else /* !__NO_STRICT_ALIGNMENT */
149 1.1 dholland /*
150 1.1 dholland * newnfs_realign:
151 1.1 dholland *
152 1.1 dholland * Check for badly aligned mbuf data and realign by copying the unaligned
153 1.1 dholland * portion of the data into a new mbuf chain and freeing the portions
154 1.1 dholland * of the old chain that were replaced.
155 1.1 dholland *
156 1.1 dholland * We cannot simply realign the data within the existing mbuf chain
157 1.1 dholland * because the underlying buffers may contain other rpc commands and
158 1.1 dholland * we cannot afford to overwrite them.
159 1.1 dholland *
160 1.1 dholland * We would prefer to avoid this situation entirely. The situation does
161 1.2 pgoyette * not occur with NFS/UDP and is supposed to only occasionally occur
162 1.1 dholland * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
163 1.1 dholland *
164 1.1 dholland */
165 1.1 dholland int
166 1.1 dholland newnfs_realign(struct mbuf **pm, int how)
167 1.1 dholland {
168 1.1 dholland struct mbuf *m, *n;
169 1.1 dholland int off, space;
170 1.1 dholland
171 1.1 dholland ++nfs_realign_test;
172 1.1 dholland while ((m = *pm) != NULL) {
173 1.1 dholland if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
174 1.1 dholland /*
175 1.1 dholland * NB: we can't depend on m_pkthdr.len to help us
176 1.1 dholland * decide what to do here. May not be worth doing
177 1.1 dholland * the m_length calculation as m_copyback will
178 1.1 dholland * expand the mbuf chain below as needed.
179 1.1 dholland */
180 1.1 dholland space = m_length(m, NULL);
181 1.1 dholland if (space >= MINCLSIZE) {
182 1.1 dholland /* NB: m_copyback handles space > MCLBYTES */
183 1.1 dholland n = m_getcl(how, MT_DATA, 0);
184 1.1 dholland } else
185 1.1 dholland n = m_get(how, MT_DATA);
186 1.1 dholland if (n == NULL)
187 1.1 dholland return (ENOMEM);
188 1.1 dholland /*
189 1.1 dholland * Align the remainder of the mbuf chain.
190 1.1 dholland */
191 1.1 dholland n->m_len = 0;
192 1.1 dholland off = 0;
193 1.1 dholland while (m != NULL) {
194 1.1 dholland m_copyback(n, off, m->m_len, mtod(m, caddr_t));
195 1.1 dholland off += m->m_len;
196 1.1 dholland m = m->m_next;
197 1.1 dholland }
198 1.1 dholland m_freem(*pm);
199 1.1 dholland *pm = n;
200 1.1 dholland ++nfs_realign_count;
201 1.1 dholland break;
202 1.1 dholland }
203 1.1 dholland pm = &m->m_next;
204 1.1 dholland }
205 1.1 dholland
206 1.1 dholland return (0);
207 1.1 dholland }
208 1.1 dholland #endif /* __NO_STRICT_ALIGNMENT */
209 1.1 dholland
210 1.1 dholland #ifdef notdef
211 1.1 dholland static void
212 1.1 dholland nfsrv_object_create(struct vnode *vp, struct thread *td)
213 1.1 dholland {
214 1.1 dholland
215 1.1 dholland if (vp == NULL || vp->v_type != VREG)
216 1.1 dholland return;
217 1.1 dholland (void) vfs_object_create(vp, td, td->td_ucred);
218 1.1 dholland }
219 1.1 dholland #endif
220 1.1 dholland
221 1.1 dholland /*
222 1.1 dholland * Look up a file name. Basically just initialize stuff and call namei().
223 1.1 dholland */
224 1.1 dholland int
225 1.1 dholland nfsrv_lookupfilename(struct nameidata *ndp, char *fname, NFSPROC_T *p)
226 1.1 dholland {
227 1.1 dholland int error;
228 1.1 dholland
229 1.1 dholland NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, fname,
230 1.1 dholland p);
231 1.1 dholland error = namei(ndp);
232 1.1 dholland if (!error) {
233 1.1 dholland NDFREE(ndp, NDF_ONLY_PNBUF);
234 1.1 dholland }
235 1.1 dholland return (error);
236 1.1 dholland }
237 1.1 dholland
238 1.1 dholland /*
239 1.1 dholland * Copy NFS uid, gids to the cred structure.
240 1.1 dholland */
241 1.1 dholland void
242 1.1 dholland newnfs_copycred(struct nfscred *nfscr, struct ucred *cr)
243 1.1 dholland {
244 1.1 dholland
245 1.1 dholland KASSERT(nfscr->nfsc_ngroups >= 0,
246 1.1 dholland ("newnfs_copycred: negative nfsc_ngroups"));
247 1.1 dholland cr->cr_uid = nfscr->nfsc_uid;
248 1.1 dholland crsetgroups(cr, nfscr->nfsc_ngroups, nfscr->nfsc_groups);
249 1.1 dholland }
250 1.1 dholland
251 1.1 dholland /*
252 1.1 dholland * Map args from nfsmsleep() to msleep().
253 1.1 dholland */
254 1.1 dholland int
255 1.1 dholland nfsmsleep(void *chan, void *mutex, int prio, const char *wmesg,
256 1.1 dholland struct timespec *ts)
257 1.1 dholland {
258 1.1 dholland u_int64_t nsecval;
259 1.1 dholland int error, timeo;
260 1.1 dholland
261 1.1 dholland if (ts) {
262 1.1 dholland timeo = hz * ts->tv_sec;
263 1.1 dholland nsecval = (u_int64_t)ts->tv_nsec;
264 1.1 dholland nsecval = ((nsecval * ((u_int64_t)hz)) + 500000000) /
265 1.1 dholland 1000000000;
266 1.1 dholland timeo += (int)nsecval;
267 1.1 dholland } else {
268 1.1 dholland timeo = 0;
269 1.1 dholland }
270 1.1 dholland error = msleep(chan, (struct mtx *)mutex, prio, wmesg, timeo);
271 1.1 dholland return (error);
272 1.1 dholland }
273 1.1 dholland
274 1.1 dholland /*
275 1.1 dholland * Get the file system info for the server. For now, just assume FFS.
276 1.1 dholland */
277 1.1 dholland void
278 1.1 dholland nfsvno_getfs(struct nfsfsinfo *sip, int isdgram)
279 1.1 dholland {
280 1.1 dholland int pref;
281 1.1 dholland
282 1.1 dholland /*
283 1.1 dholland * XXX
284 1.1 dholland * There should be file system VFS OP(s) to get this information.
285 1.1 dholland * For now, assume ufs.
286 1.1 dholland */
287 1.1 dholland if (isdgram)
288 1.1 dholland pref = NFS_MAXDGRAMDATA;
289 1.1 dholland else
290 1.2 pgoyette pref = NFS_SRVMAXIO;
291 1.2 pgoyette sip->fs_rtmax = NFS_SRVMAXIO;
292 1.1 dholland sip->fs_rtpref = pref;
293 1.1 dholland sip->fs_rtmult = NFS_FABLKSIZE;
294 1.2 pgoyette sip->fs_wtmax = NFS_SRVMAXIO;
295 1.1 dholland sip->fs_wtpref = pref;
296 1.1 dholland sip->fs_wtmult = NFS_FABLKSIZE;
297 1.1 dholland sip->fs_dtpref = pref;
298 1.1 dholland sip->fs_maxfilesize = 0xffffffffffffffffull;
299 1.1 dholland sip->fs_timedelta.tv_sec = 0;
300 1.1 dholland sip->fs_timedelta.tv_nsec = 1;
301 1.1 dholland sip->fs_properties = (NFSV3FSINFO_LINK |
302 1.1 dholland NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
303 1.1 dholland NFSV3FSINFO_CANSETTIME);
304 1.1 dholland }
305 1.1 dholland
306 1.1 dholland /*
307 1.1 dholland * Do the pathconf vnode op.
308 1.1 dholland */
309 1.1 dholland int
310 1.1 dholland nfsvno_pathconf(struct vnode *vp, int flag, register_t *retf,
311 1.1 dholland struct ucred *cred, struct thread *p)
312 1.1 dholland {
313 1.1 dholland int error;
314 1.1 dholland
315 1.1 dholland error = VOP_PATHCONF(vp, flag, retf);
316 1.1 dholland if (error == EOPNOTSUPP || error == EINVAL) {
317 1.1 dholland /*
318 1.1 dholland * Some file systems return EINVAL for name arguments not
319 1.1 dholland * supported and some return EOPNOTSUPP for this case.
320 1.1 dholland * So the NFSv3 Pathconf RPC doesn't fail for these cases,
321 1.1 dholland * just fake them.
322 1.1 dholland */
323 1.1 dholland switch (flag) {
324 1.1 dholland case _PC_LINK_MAX:
325 1.1 dholland *retf = LINK_MAX;
326 1.1 dholland break;
327 1.1 dholland case _PC_NAME_MAX:
328 1.1 dholland *retf = NAME_MAX;
329 1.1 dholland break;
330 1.1 dholland case _PC_CHOWN_RESTRICTED:
331 1.1 dholland *retf = 1;
332 1.1 dholland break;
333 1.1 dholland case _PC_NO_TRUNC:
334 1.1 dholland *retf = 1;
335 1.1 dholland break;
336 1.1 dholland default:
337 1.1 dholland /*
338 1.1 dholland * Only happens if a _PC_xxx is added to the server,
339 1.1 dholland * but this isn't updated.
340 1.1 dholland */
341 1.1 dholland *retf = 0;
342 1.1 dholland printf("nfsrvd pathconf flag=%d not supp\n", flag);
343 1.2 pgoyette }
344 1.1 dholland error = 0;
345 1.1 dholland }
346 1.1 dholland NFSEXITCODE(error);
347 1.1 dholland return (error);
348 1.1 dholland }
349 1.1 dholland
350 1.1 dholland /* Fake nfsrv_atroot. Just return 0 */
351 1.1 dholland int
352 1.1 dholland nfsrv_atroot(struct vnode *vp, long *retp)
353 1.1 dholland {
354 1.1 dholland
355 1.1 dholland return (0);
356 1.1 dholland }
357 1.1 dholland
358 1.1 dholland /*
359 1.1 dholland * Set the credentials to refer to root.
360 1.1 dholland * If only the various BSDen could agree on whether cr_gid is a separate
361 1.1 dholland * field or cr_groups[0]...
362 1.1 dholland */
363 1.1 dholland void
364 1.1 dholland newnfs_setroot(struct ucred *cred)
365 1.1 dholland {
366 1.1 dholland
367 1.1 dholland cred->cr_uid = 0;
368 1.1 dholland cred->cr_groups[0] = 0;
369 1.1 dholland cred->cr_ngroups = 1;
370 1.1 dholland }
371 1.1 dholland
372 1.1 dholland /*
373 1.1 dholland * Get the client credential. Used for Renew and recovery.
374 1.1 dholland */
375 1.1 dholland struct ucred *
376 1.1 dholland newnfs_getcred(void)
377 1.1 dholland {
378 1.1 dholland struct ucred *cred;
379 1.1 dholland struct thread *td = curthread;
380 1.1 dholland
381 1.1 dholland cred = crdup(td->td_ucred);
382 1.1 dholland newnfs_setroot(cred);
383 1.1 dholland return (cred);
384 1.1 dholland }
385 1.1 dholland
386 1.1 dholland /*
387 1.1 dholland * Nfs timer routine
388 1.1 dholland * Call the nfsd's timer function once/sec.
389 1.1 dholland */
390 1.1 dholland void
391 1.1 dholland newnfs_timer(void *arg)
392 1.1 dholland {
393 1.1 dholland static time_t lasttime = 0;
394 1.1 dholland /*
395 1.1 dholland * Call the server timer, if set up.
396 1.1 dholland * The argument indicates if it is the next second and therefore
397 1.1 dholland * leases should be checked.
398 1.1 dholland */
399 1.1 dholland if (lasttime != NFSD_MONOSEC) {
400 1.1 dholland lasttime = NFSD_MONOSEC;
401 1.1 dholland if (nfsd_call_servertimer != NULL)
402 1.1 dholland (*nfsd_call_servertimer)();
403 1.1 dholland }
404 1.1 dholland callout_reset(&newnfsd_callout, nfscl_ticks, newnfs_timer, NULL);
405 1.1 dholland }
406 1.1 dholland
407 1.1 dholland
408 1.1 dholland /*
409 1.1 dholland * Sleep for a short period of time unless errval == NFSERR_GRACE, where
410 1.1 dholland * the sleep should be for 5 seconds.
411 1.1 dholland * Since lbolt doesn't exist in FreeBSD-CURRENT, just use a timeout on
412 1.1 dholland * an event that never gets a wakeup. Only return EINTR or 0.
413 1.1 dholland */
414 1.1 dholland int
415 1.1 dholland nfs_catnap(int prio, int errval, const char *wmesg)
416 1.1 dholland {
417 1.1 dholland static int non_event;
418 1.1 dholland int ret;
419 1.1 dholland
420 1.1 dholland if (errval == NFSERR_GRACE)
421 1.1 dholland ret = tsleep(&non_event, prio, wmesg, 5 * hz);
422 1.1 dholland else
423 1.1 dholland ret = tsleep(&non_event, prio, wmesg, 1);
424 1.1 dholland if (ret != EINTR)
425 1.1 dholland ret = 0;
426 1.1 dholland return (ret);
427 1.1 dholland }
428 1.1 dholland
429 1.1 dholland /*
430 1.1 dholland * Get referral. For now, just fail.
431 1.1 dholland */
432 1.1 dholland struct nfsreferral *
433 1.1 dholland nfsv4root_getreferral(struct vnode *vp, struct vnode *dvp, u_int32_t fileno)
434 1.1 dholland {
435 1.1 dholland
436 1.1 dholland return (NULL);
437 1.1 dholland }
438 1.1 dholland
439 1.1 dholland static int
440 1.1 dholland nfssvc_nfscommon(struct thread *td, struct nfssvc_args *uap)
441 1.1 dholland {
442 1.1 dholland int error;
443 1.1 dholland
444 1.1 dholland error = nfssvc_call(td, uap, td->td_ucred);
445 1.1 dholland NFSEXITCODE(error);
446 1.1 dholland return (error);
447 1.1 dholland }
448 1.1 dholland
449 1.1 dholland static int
450 1.1 dholland nfssvc_call(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
451 1.1 dholland {
452 1.2 pgoyette int error = EINVAL, i, j;
453 1.1 dholland struct nfsd_idargs nid;
454 1.2 pgoyette struct nfsd_oidargs onid;
455 1.2 pgoyette struct {
456 1.2 pgoyette int vers; /* Just the first field of nfsstats. */
457 1.2 pgoyette } nfsstatver;
458 1.1 dholland
459 1.1 dholland if (uap->flag & NFSSVC_IDNAME) {
460 1.2 pgoyette if ((uap->flag & NFSSVC_NEWSTRUCT) != 0)
461 1.2 pgoyette error = copyin(uap->argp, &nid, sizeof(nid));
462 1.2 pgoyette else {
463 1.2 pgoyette error = copyin(uap->argp, &onid, sizeof(onid));
464 1.2 pgoyette if (error == 0) {
465 1.2 pgoyette nid.nid_flag = onid.nid_flag;
466 1.2 pgoyette nid.nid_uid = onid.nid_uid;
467 1.2 pgoyette nid.nid_gid = onid.nid_gid;
468 1.2 pgoyette nid.nid_usermax = onid.nid_usermax;
469 1.2 pgoyette nid.nid_usertimeout = onid.nid_usertimeout;
470 1.2 pgoyette nid.nid_name = onid.nid_name;
471 1.2 pgoyette nid.nid_namelen = onid.nid_namelen;
472 1.2 pgoyette nid.nid_ngroup = 0;
473 1.2 pgoyette nid.nid_grps = NULL;
474 1.2 pgoyette }
475 1.2 pgoyette }
476 1.1 dholland if (error)
477 1.1 dholland goto out;
478 1.1 dholland error = nfssvc_idname(&nid);
479 1.1 dholland goto out;
480 1.1 dholland } else if (uap->flag & NFSSVC_GETSTATS) {
481 1.2 pgoyette if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
482 1.2 pgoyette /* Copy fields to the old ext_nfsstat structure. */
483 1.2 pgoyette oldnfsstats.attrcache_hits =
484 1.2 pgoyette nfsstatsv1.attrcache_hits;
485 1.2 pgoyette oldnfsstats.attrcache_misses =
486 1.2 pgoyette nfsstatsv1.attrcache_misses;
487 1.2 pgoyette oldnfsstats.lookupcache_hits =
488 1.2 pgoyette nfsstatsv1.lookupcache_hits;
489 1.2 pgoyette oldnfsstats.lookupcache_misses =
490 1.2 pgoyette nfsstatsv1.lookupcache_misses;
491 1.2 pgoyette oldnfsstats.direofcache_hits =
492 1.2 pgoyette nfsstatsv1.direofcache_hits;
493 1.2 pgoyette oldnfsstats.direofcache_misses =
494 1.2 pgoyette nfsstatsv1.direofcache_misses;
495 1.2 pgoyette oldnfsstats.accesscache_hits =
496 1.2 pgoyette nfsstatsv1.accesscache_hits;
497 1.2 pgoyette oldnfsstats.accesscache_misses =
498 1.2 pgoyette nfsstatsv1.accesscache_misses;
499 1.2 pgoyette oldnfsstats.biocache_reads =
500 1.2 pgoyette nfsstatsv1.biocache_reads;
501 1.2 pgoyette oldnfsstats.read_bios =
502 1.2 pgoyette nfsstatsv1.read_bios;
503 1.2 pgoyette oldnfsstats.read_physios =
504 1.2 pgoyette nfsstatsv1.read_physios;
505 1.2 pgoyette oldnfsstats.biocache_writes =
506 1.2 pgoyette nfsstatsv1.biocache_writes;
507 1.2 pgoyette oldnfsstats.write_bios =
508 1.2 pgoyette nfsstatsv1.write_bios;
509 1.2 pgoyette oldnfsstats.write_physios =
510 1.2 pgoyette nfsstatsv1.write_physios;
511 1.2 pgoyette oldnfsstats.biocache_readlinks =
512 1.2 pgoyette nfsstatsv1.biocache_readlinks;
513 1.2 pgoyette oldnfsstats.readlink_bios =
514 1.2 pgoyette nfsstatsv1.readlink_bios;
515 1.2 pgoyette oldnfsstats.biocache_readdirs =
516 1.2 pgoyette nfsstatsv1.biocache_readdirs;
517 1.2 pgoyette oldnfsstats.readdir_bios =
518 1.2 pgoyette nfsstatsv1.readdir_bios;
519 1.2 pgoyette for (i = 0; i < NFSV4_NPROCS; i++)
520 1.2 pgoyette oldnfsstats.rpccnt[i] = nfsstatsv1.rpccnt[i];
521 1.2 pgoyette oldnfsstats.rpcretries = nfsstatsv1.rpcretries;
522 1.2 pgoyette for (i = 0; i < NFSV4OP_NOPS; i++)
523 1.2 pgoyette oldnfsstats.srvrpccnt[i] =
524 1.2 pgoyette nfsstatsv1.srvrpccnt[i];
525 1.2 pgoyette for (i = NFSV42_NOPS, j = NFSV4OP_NOPS;
526 1.2 pgoyette i < NFSV42_NOPS + NFSV4OP_FAKENOPS; i++, j++)
527 1.2 pgoyette oldnfsstats.srvrpccnt[j] =
528 1.2 pgoyette nfsstatsv1.srvrpccnt[i];
529 1.2 pgoyette oldnfsstats.srvrpc_errs = nfsstatsv1.srvrpc_errs;
530 1.2 pgoyette oldnfsstats.srv_errs = nfsstatsv1.srv_errs;
531 1.2 pgoyette oldnfsstats.rpcrequests = nfsstatsv1.rpcrequests;
532 1.2 pgoyette oldnfsstats.rpctimeouts = nfsstatsv1.rpctimeouts;
533 1.2 pgoyette oldnfsstats.rpcunexpected = nfsstatsv1.rpcunexpected;
534 1.2 pgoyette oldnfsstats.rpcinvalid = nfsstatsv1.rpcinvalid;
535 1.2 pgoyette oldnfsstats.srvcache_inproghits =
536 1.2 pgoyette nfsstatsv1.srvcache_inproghits;
537 1.2 pgoyette oldnfsstats.srvcache_idemdonehits =
538 1.2 pgoyette nfsstatsv1.srvcache_idemdonehits;
539 1.2 pgoyette oldnfsstats.srvcache_nonidemdonehits =
540 1.2 pgoyette nfsstatsv1.srvcache_nonidemdonehits;
541 1.2 pgoyette oldnfsstats.srvcache_misses =
542 1.2 pgoyette nfsstatsv1.srvcache_misses;
543 1.2 pgoyette oldnfsstats.srvcache_tcppeak =
544 1.2 pgoyette nfsstatsv1.srvcache_tcppeak;
545 1.2 pgoyette oldnfsstats.srvcache_size = nfsstatsv1.srvcache_size;
546 1.2 pgoyette oldnfsstats.srvclients = nfsstatsv1.srvclients;
547 1.2 pgoyette oldnfsstats.srvopenowners = nfsstatsv1.srvopenowners;
548 1.2 pgoyette oldnfsstats.srvopens = nfsstatsv1.srvopens;
549 1.2 pgoyette oldnfsstats.srvlockowners = nfsstatsv1.srvlockowners;
550 1.2 pgoyette oldnfsstats.srvlocks = nfsstatsv1.srvlocks;
551 1.2 pgoyette oldnfsstats.srvdelegates = nfsstatsv1.srvdelegates;
552 1.2 pgoyette for (i = 0; i < NFSV4OP_CBNOPS; i++)
553 1.2 pgoyette oldnfsstats.cbrpccnt[i] =
554 1.2 pgoyette nfsstatsv1.cbrpccnt[i];
555 1.2 pgoyette oldnfsstats.clopenowners = nfsstatsv1.clopenowners;
556 1.2 pgoyette oldnfsstats.clopens = nfsstatsv1.clopens;
557 1.2 pgoyette oldnfsstats.cllockowners = nfsstatsv1.cllockowners;
558 1.2 pgoyette oldnfsstats.cllocks = nfsstatsv1.cllocks;
559 1.2 pgoyette oldnfsstats.cldelegates = nfsstatsv1.cldelegates;
560 1.2 pgoyette oldnfsstats.cllocalopenowners =
561 1.2 pgoyette nfsstatsv1.cllocalopenowners;
562 1.2 pgoyette oldnfsstats.cllocalopens = nfsstatsv1.cllocalopens;
563 1.2 pgoyette oldnfsstats.cllocallockowners =
564 1.2 pgoyette nfsstatsv1.cllocallockowners;
565 1.2 pgoyette oldnfsstats.cllocallocks = nfsstatsv1.cllocallocks;
566 1.2 pgoyette error = copyout(&oldnfsstats, uap->argp,
567 1.2 pgoyette sizeof (oldnfsstats));
568 1.2 pgoyette } else {
569 1.2 pgoyette error = copyin(uap->argp, &nfsstatver,
570 1.2 pgoyette sizeof(nfsstatver));
571 1.2 pgoyette if (error == 0 && nfsstatver.vers != NFSSTATS_V1)
572 1.2 pgoyette error = EPERM;
573 1.2 pgoyette if (error == 0)
574 1.2 pgoyette error = copyout(&nfsstatsv1, uap->argp,
575 1.2 pgoyette sizeof (nfsstatsv1));
576 1.2 pgoyette }
577 1.1 dholland if (error == 0) {
578 1.1 dholland if ((uap->flag & NFSSVC_ZEROCLTSTATS) != 0) {
579 1.2 pgoyette nfsstatsv1.attrcache_hits = 0;
580 1.2 pgoyette nfsstatsv1.attrcache_misses = 0;
581 1.2 pgoyette nfsstatsv1.lookupcache_hits = 0;
582 1.2 pgoyette nfsstatsv1.lookupcache_misses = 0;
583 1.2 pgoyette nfsstatsv1.direofcache_hits = 0;
584 1.2 pgoyette nfsstatsv1.direofcache_misses = 0;
585 1.2 pgoyette nfsstatsv1.accesscache_hits = 0;
586 1.2 pgoyette nfsstatsv1.accesscache_misses = 0;
587 1.2 pgoyette nfsstatsv1.biocache_reads = 0;
588 1.2 pgoyette nfsstatsv1.read_bios = 0;
589 1.2 pgoyette nfsstatsv1.read_physios = 0;
590 1.2 pgoyette nfsstatsv1.biocache_writes = 0;
591 1.2 pgoyette nfsstatsv1.write_bios = 0;
592 1.2 pgoyette nfsstatsv1.write_physios = 0;
593 1.2 pgoyette nfsstatsv1.biocache_readlinks = 0;
594 1.2 pgoyette nfsstatsv1.readlink_bios = 0;
595 1.2 pgoyette nfsstatsv1.biocache_readdirs = 0;
596 1.2 pgoyette nfsstatsv1.readdir_bios = 0;
597 1.2 pgoyette nfsstatsv1.rpcretries = 0;
598 1.2 pgoyette nfsstatsv1.rpcrequests = 0;
599 1.2 pgoyette nfsstatsv1.rpctimeouts = 0;
600 1.2 pgoyette nfsstatsv1.rpcunexpected = 0;
601 1.2 pgoyette nfsstatsv1.rpcinvalid = 0;
602 1.2 pgoyette bzero(nfsstatsv1.rpccnt,
603 1.2 pgoyette sizeof(nfsstatsv1.rpccnt));
604 1.1 dholland }
605 1.1 dholland if ((uap->flag & NFSSVC_ZEROSRVSTATS) != 0) {
606 1.2 pgoyette nfsstatsv1.srvrpc_errs = 0;
607 1.2 pgoyette nfsstatsv1.srv_errs = 0;
608 1.2 pgoyette nfsstatsv1.srvcache_inproghits = 0;
609 1.2 pgoyette nfsstatsv1.srvcache_idemdonehits = 0;
610 1.2 pgoyette nfsstatsv1.srvcache_nonidemdonehits = 0;
611 1.2 pgoyette nfsstatsv1.srvcache_misses = 0;
612 1.2 pgoyette nfsstatsv1.srvcache_tcppeak = 0;
613 1.2 pgoyette nfsstatsv1.srvclients = 0;
614 1.2 pgoyette nfsstatsv1.srvopenowners = 0;
615 1.2 pgoyette nfsstatsv1.srvopens = 0;
616 1.2 pgoyette nfsstatsv1.srvlockowners = 0;
617 1.2 pgoyette nfsstatsv1.srvlocks = 0;
618 1.2 pgoyette nfsstatsv1.srvdelegates = 0;
619 1.2 pgoyette nfsstatsv1.clopenowners = 0;
620 1.2 pgoyette nfsstatsv1.clopens = 0;
621 1.2 pgoyette nfsstatsv1.cllockowners = 0;
622 1.2 pgoyette nfsstatsv1.cllocks = 0;
623 1.2 pgoyette nfsstatsv1.cldelegates = 0;
624 1.2 pgoyette nfsstatsv1.cllocalopenowners = 0;
625 1.2 pgoyette nfsstatsv1.cllocalopens = 0;
626 1.2 pgoyette nfsstatsv1.cllocallockowners = 0;
627 1.2 pgoyette nfsstatsv1.cllocallocks = 0;
628 1.2 pgoyette bzero(nfsstatsv1.srvrpccnt,
629 1.2 pgoyette sizeof(nfsstatsv1.srvrpccnt));
630 1.2 pgoyette bzero(nfsstatsv1.cbrpccnt,
631 1.2 pgoyette sizeof(nfsstatsv1.cbrpccnt));
632 1.1 dholland }
633 1.1 dholland }
634 1.1 dholland goto out;
635 1.1 dholland } else if (uap->flag & NFSSVC_NFSUSERDPORT) {
636 1.1 dholland u_short sockport;
637 1.1 dholland
638 1.1 dholland error = copyin(uap->argp, (caddr_t)&sockport,
639 1.1 dholland sizeof (u_short));
640 1.1 dholland if (!error)
641 1.1 dholland error = nfsrv_nfsuserdport(sockport, p);
642 1.1 dholland } else if (uap->flag & NFSSVC_NFSUSERDDELPORT) {
643 1.1 dholland nfsrv_nfsuserddelport();
644 1.1 dholland error = 0;
645 1.1 dholland }
646 1.1 dholland
647 1.1 dholland out:
648 1.1 dholland NFSEXITCODE(error);
649 1.1 dholland return (error);
650 1.1 dholland }
651 1.1 dholland
652 1.1 dholland /*
653 1.1 dholland * called by all three modevent routines, so that it gets things
654 1.1 dholland * initialized soon enough.
655 1.1 dholland */
656 1.1 dholland void
657 1.1 dholland newnfs_portinit(void)
658 1.1 dholland {
659 1.1 dholland static int inited = 0;
660 1.1 dholland
661 1.1 dholland if (inited)
662 1.1 dholland return;
663 1.1 dholland inited = 1;
664 1.1 dholland /* Initialize SMP locks used by both client and server. */
665 1.1 dholland mtx_init(&newnfsd_mtx, "newnfsd_mtx", NULL, MTX_DEF);
666 1.1 dholland mtx_init(&nfs_state_mutex, "nfs_state_mutex", NULL, MTX_DEF);
667 1.1 dholland }
668 1.1 dholland
669 1.1 dholland /*
670 1.1 dholland * Determine if the file system supports NFSv4 ACLs.
671 1.1 dholland * Return 1 if it does, 0 otherwise.
672 1.1 dholland */
673 1.1 dholland int
674 1.1 dholland nfs_supportsnfsv4acls(struct vnode *vp)
675 1.1 dholland {
676 1.1 dholland int error;
677 1.1 dholland register_t retval;
678 1.1 dholland
679 1.1 dholland ASSERT_VOP_LOCKED(vp, "nfs supports nfsv4acls");
680 1.1 dholland
681 1.1 dholland if (nfsrv_useacl == 0)
682 1.1 dholland return (0);
683 1.1 dholland error = VOP_PATHCONF(vp, _PC_ACL_NFS4, &retval);
684 1.1 dholland if (error == 0 && retval != 0)
685 1.1 dholland return (1);
686 1.1 dholland return (0);
687 1.1 dholland }
688 1.1 dholland
689 1.1 dholland extern int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *);
690 1.1 dholland
691 1.1 dholland /*
692 1.1 dholland * Called once to initialize data structures...
693 1.1 dholland */
694 1.1 dholland static int
695 1.1 dholland nfscommon_modevent(module_t mod, int type, void *data)
696 1.1 dholland {
697 1.1 dholland int error = 0;
698 1.1 dholland static int loaded = 0;
699 1.1 dholland
700 1.1 dholland switch (type) {
701 1.1 dholland case MOD_LOAD:
702 1.1 dholland if (loaded)
703 1.1 dholland goto out;
704 1.1 dholland newnfs_portinit();
705 1.1 dholland mtx_init(&nfs_nameid_mutex, "nfs_nameid_mutex", NULL, MTX_DEF);
706 1.1 dholland mtx_init(&nfs_sockl_mutex, "nfs_sockl_mutex", NULL, MTX_DEF);
707 1.1 dholland mtx_init(&nfs_slock_mutex, "nfs_slock_mutex", NULL, MTX_DEF);
708 1.1 dholland mtx_init(&nfs_req_mutex, "nfs_req_mutex", NULL, MTX_DEF);
709 1.1 dholland mtx_init(&nfsrv_nfsuserdsock.nr_mtx, "nfsuserd", NULL,
710 1.1 dholland MTX_DEF);
711 1.2 pgoyette callout_init(&newnfsd_callout, 1);
712 1.1 dholland newnfs_init();
713 1.1 dholland nfsd_call_nfscommon = nfssvc_nfscommon;
714 1.1 dholland loaded = 1;
715 1.1 dholland break;
716 1.1 dholland
717 1.1 dholland case MOD_UNLOAD:
718 1.1 dholland if (newnfs_numnfsd != 0 || nfsrv_nfsuserd != 0 ||
719 1.1 dholland nfs_numnfscbd != 0) {
720 1.1 dholland error = EBUSY;
721 1.1 dholland break;
722 1.1 dholland }
723 1.1 dholland
724 1.1 dholland nfsd_call_nfscommon = NULL;
725 1.1 dholland callout_drain(&newnfsd_callout);
726 1.2 pgoyette /* Clean out the name<-->id cache. */
727 1.2 pgoyette nfsrv_cleanusergroup();
728 1.1 dholland /* and get rid of the mutexes */
729 1.1 dholland mtx_destroy(&nfs_nameid_mutex);
730 1.1 dholland mtx_destroy(&newnfsd_mtx);
731 1.1 dholland mtx_destroy(&nfs_state_mutex);
732 1.1 dholland mtx_destroy(&nfs_sockl_mutex);
733 1.1 dholland mtx_destroy(&nfs_slock_mutex);
734 1.1 dholland mtx_destroy(&nfs_req_mutex);
735 1.1 dholland mtx_destroy(&nfsrv_nfsuserdsock.nr_mtx);
736 1.1 dholland loaded = 0;
737 1.1 dholland break;
738 1.1 dholland default:
739 1.1 dholland error = EOPNOTSUPP;
740 1.1 dholland break;
741 1.1 dholland }
742 1.1 dholland
743 1.1 dholland out:
744 1.1 dholland NFSEXITCODE(error);
745 1.1 dholland return error;
746 1.1 dholland }
747 1.1 dholland static moduledata_t nfscommon_mod = {
748 1.1 dholland "nfscommon",
749 1.1 dholland nfscommon_modevent,
750 1.1 dholland NULL,
751 1.1 dholland };
752 1.1 dholland DECLARE_MODULE(nfscommon, nfscommon_mod, SI_SUB_VFS, SI_ORDER_ANY);
753 1.1 dholland
754 1.1 dholland /* So that loader and kldload(2) can find us, wherever we are.. */
755 1.1 dholland MODULE_VERSION(nfscommon, 1);
756 1.1 dholland MODULE_DEPEND(nfscommon, nfssvc, 1, 1, 1);
757 1.1 dholland MODULE_DEPEND(nfscommon, krpc, 1, 1, 1);
758 1.1 dholland
759