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