1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from nfs_subs.c 8.8 (Berkeley) 5/22/95
35 */
36
37 #include <sys/cdefs.h>
38 /*
39 * These functions support the macros and help fiddle mbuf chains for
40 * the nfs op functions. They do things like create the rpc header and
41 * copy data between mbuf chains and uio lists.
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bio.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/namei.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/stat.h>
56 #include <sys/malloc.h>
57 #include <sys/stdarg.h>
58 #include <sys/syscall.h>
59 #include <sys/sysproto.h>
60 #include <sys/taskqueue.h>
61
62 #include <vm/vm.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
65 #include <vm/uma.h>
66
67 #include <fs/nfs/nfsport.h>
68 #include <fs/nfsclient/nfsnode.h>
69 #include <fs/nfsclient/nfsmount.h>
70 #include <fs/nfsclient/nfs.h>
71 #include <fs/nfsclient/nfs_kdtrace.h>
72
73 #include <netinet/in.h>
74
75 extern struct mtx ncl_iod_mutex;
76 extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
77 extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
78 extern int ncl_numasync;
79 extern unsigned int ncl_iodmax;
80 extern struct nfsstatsv1 nfsstatsv1;
81
82 struct task ncl_nfsiodnew_task;
83
84 int
ncl_uninit(struct vfsconf * vfsp)85 ncl_uninit(struct vfsconf *vfsp)
86 {
87 /*
88 * XXX: Unloading of nfscl module is unsupported.
89 */
90 #if 0
91 int i;
92
93 /*
94 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
95 * any sleeping nfsiods so they check ncl_iodmax and exit.
96 */
97 NFSLOCKIOD();
98 ncl_iodmax = 0;
99 for (i = 0; i < ncl_numasync; i++)
100 if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
101 wakeup(&ncl_iodwant[i]);
102 /* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
103 while (ncl_numasync)
104 msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
105 NFSUNLOCKIOD();
106 ncl_nhuninit();
107 return (0);
108 #else
109 return (EOPNOTSUPP);
110 #endif
111 }
112
113 /* Returns with NFSLOCKNODE() held. */
114 void
ncl_dircookie_lock(struct nfsnode * np)115 ncl_dircookie_lock(struct nfsnode *np)
116 {
117 NFSLOCKNODE(np);
118 while (np->n_flag & NDIRCOOKIELK)
119 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
120 np->n_flag |= NDIRCOOKIELK;
121 }
122
123 void
ncl_dircookie_unlock(struct nfsnode * np)124 ncl_dircookie_unlock(struct nfsnode *np)
125 {
126 NFSLOCKNODE(np);
127 np->n_flag &= ~NDIRCOOKIELK;
128 wakeup(&np->n_flag);
129 NFSUNLOCKNODE(np);
130 }
131
132 bool
ncl_excl_start(struct vnode * vp)133 ncl_excl_start(struct vnode *vp)
134 {
135 struct nfsnode *np;
136 int vn_lk;
137
138 ASSERT_VOP_LOCKED(vp, "ncl_excl_start");
139 vn_lk = NFSVOPISLOCKED(vp);
140 if (vn_lk == LK_EXCLUSIVE)
141 return (false);
142 KASSERT(vn_lk == LK_SHARED,
143 ("ncl_excl_start: wrong vnode lock %d", vn_lk));
144 /* Ensure exclusive access, this might block */
145 np = VTONFS(vp);
146 lockmgr(&np->n_excl, LK_EXCLUSIVE, NULL);
147 return (true);
148 }
149
150 void
ncl_excl_finish(struct vnode * vp,bool old_lock)151 ncl_excl_finish(struct vnode *vp, bool old_lock)
152 {
153 struct nfsnode *np;
154
155 if (!old_lock)
156 return;
157 np = VTONFS(vp);
158 lockmgr(&np->n_excl, LK_RELEASE, NULL);
159 }
160
161 #ifdef NFS_ACDEBUG
162 #include <sys/sysctl.h>
163 SYSCTL_DECL(_vfs_nfs);
164 static int nfs_acdebug;
165 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
166 #endif
167
168 /*
169 * Check the time stamp
170 * If the cache is valid, copy contents to *vap and return 0
171 * otherwise return an error
172 */
173 int
ncl_getattrcache(struct vnode * vp,struct vattr * vaper)174 ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
175 {
176 struct nfsnode *np;
177 struct vattr *vap;
178 struct nfsmount *nmp;
179 int timeo, mustflush;
180 u_quad_t nsize;
181 bool setnsize;
182
183 np = VTONFS(vp);
184 vap = &np->n_vattr.na_vattr;
185 nmp = VFSTONFS(vp->v_mount);
186 mustflush = nfscl_nodeleg(vp, 0); /* must be before mtx_lock() */
187 NFSLOCKNODE(np);
188 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
189 timeo = (time_second - np->n_mtime.tv_sec) / 10;
190
191 #ifdef NFS_ACDEBUG
192 if (nfs_acdebug>1)
193 printf("ncl_getattrcache: initial timeo = %d\n", timeo);
194 #endif
195
196 if (vap->va_type == VDIR) {
197 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
198 timeo = nmp->nm_acdirmin;
199 else if (timeo > nmp->nm_acdirmax)
200 timeo = nmp->nm_acdirmax;
201 } else {
202 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
203 timeo = nmp->nm_acregmin;
204 else if (timeo > nmp->nm_acregmax)
205 timeo = nmp->nm_acregmax;
206 }
207
208 #ifdef NFS_ACDEBUG
209 if (nfs_acdebug > 2)
210 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
211 nmp->nm_acregmin, nmp->nm_acregmax,
212 nmp->nm_acdirmin, nmp->nm_acdirmax);
213
214 if (nfs_acdebug)
215 printf("ncl_getattrcache: age = %d; final timeo = %d\n",
216 (time_second - np->n_attrstamp), timeo);
217 #endif
218
219 if (mustflush != 0 && (np->n_attrstamp == 0 ||
220 time_second - np->n_attrstamp >= timeo)) {
221 nfsstatsv1.attrcache_misses++;
222 NFSUNLOCKNODE(np);
223 KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
224 return( ENOENT);
225 }
226 nfsstatsv1.attrcache_hits++;
227 setnsize = false;
228 if (vap->va_size != np->n_size) {
229 if (vap->va_type == VREG) {
230 if (np->n_flag & NMODIFIED) {
231 if (vap->va_size < np->n_size)
232 vap->va_size = np->n_size;
233 else
234 np->n_size = vap->va_size;
235 } else {
236 np->n_size = vap->va_size;
237 }
238 setnsize = ncl_pager_setsize(vp, &nsize);
239 } else {
240 np->n_size = vap->va_size;
241 }
242 }
243 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
244 if (np->n_flag & NCHG) {
245 if (np->n_flag & NACC)
246 vaper->va_atime = np->n_atim;
247 if (np->n_flag & NUPD)
248 vaper->va_mtime = np->n_mtim;
249 }
250 NFSUNLOCKNODE(np);
251 if (setnsize)
252 vnode_pager_setsize(vp, nsize);
253 KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
254 return (0);
255 }
256
257 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
258 /*
259 * This function finds the directory cookie that corresponds to the
260 * logical byte offset given.
261 */
262 nfsuint64 *
ncl_getcookie(struct nfsnode * np,off_t off,int add)263 ncl_getcookie(struct nfsnode *np, off_t off, int add)
264 {
265 struct nfsdmap *dp, *dp2;
266 u_int pos;
267 nfsuint64 *retval = NULL;
268
269 /*
270 * Limiting "off" to 50Gbytes sets a limit of 100 million directory
271 * entries of maximum filename length. Much more with shorter
272 * file names. This limit ensures "pos" will not be truncated
273 * in the devision below.
274 */
275 if (off > 53687091200ull)
276 goto out;
277 pos = (uoff_t)off / NFS_DIRBLKSIZ;
278 if (pos == 0 || off < 0) {
279 KASSERT(!add, ("nfs getcookie add at <= 0"));
280 return (&nfs_nullcookie);
281 }
282 pos--;
283 dp = LIST_FIRST(&np->n_cookies);
284 if (!dp) {
285 if (add) {
286 dp = malloc(sizeof (struct nfsdmap),
287 M_NFSDIROFF, M_WAITOK);
288 dp->ndm_eocookie = 0;
289 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
290 } else
291 goto out;
292 }
293 while (pos >= NFSNUMCOOKIES) {
294 pos -= NFSNUMCOOKIES;
295 if (LIST_NEXT(dp, ndm_list)) {
296 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
297 pos >= dp->ndm_eocookie)
298 goto out;
299 dp = LIST_NEXT(dp, ndm_list);
300 } else if (add) {
301 dp2 = malloc(sizeof (struct nfsdmap),
302 M_NFSDIROFF, M_WAITOK);
303 dp2->ndm_eocookie = 0;
304 LIST_INSERT_AFTER(dp, dp2, ndm_list);
305 dp = dp2;
306 } else
307 goto out;
308 }
309 if (pos >= dp->ndm_eocookie) {
310 if (add)
311 dp->ndm_eocookie = pos + 1;
312 else
313 goto out;
314 }
315 retval = &dp->ndm_cookies[pos];
316 out:
317 return (retval);
318 }
319
320 /*
321 * Invalidate cached directory information, except for the actual directory
322 * blocks (which are invalidated separately).
323 * Done mainly to avoid the use of stale offset cookies.
324 */
325 void
ncl_invaldir(struct vnode * vp)326 ncl_invaldir(struct vnode *vp)
327 {
328 struct nfsnode *np = VTONFS(vp);
329
330 KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
331 ncl_dircookie_lock(np);
332 np->n_direofoffset = 0;
333 NFSUNLOCKNODE(np);
334 np->n_cookieverf.nfsuquad[0] = 0;
335 np->n_cookieverf.nfsuquad[1] = 0;
336 if (LIST_FIRST(&np->n_cookies))
337 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
338 ncl_dircookie_unlock(np);
339 }
340
341 /*
342 * The write verifier has changed (probably due to a server reboot), so all
343 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
344 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
345 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
346 * mount point.
347 *
348 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
349 * writes are not clusterable.
350 */
351 void
ncl_clearcommit(struct mount * mp)352 ncl_clearcommit(struct mount *mp)
353 {
354 struct vnode *vp, *nvp;
355 struct buf *bp, *nbp;
356 struct bufobj *bo;
357
358 MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
359 bo = &vp->v_bufobj;
360 vholdl(vp);
361 VI_UNLOCK(vp);
362 BO_LOCK(bo);
363 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
364 if (!BUF_ISLOCKED(bp) &&
365 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
366 == (B_DELWRI | B_NEEDCOMMIT))
367 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
368 }
369 BO_UNLOCK(bo);
370 vdrop(vp);
371 }
372 }
373
374 /*
375 * Called once to initialize data structures...
376 */
377 int
ncl_init(struct vfsconf * vfsp)378 ncl_init(struct vfsconf *vfsp)
379 {
380 int i;
381
382 /* Ensure async daemons disabled */
383 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
384 ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
385 ncl_iodmount[i] = NULL;
386 }
387 TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL);
388 ncl_nhinit(); /* Init the nfsnode table */
389
390 return (0);
391 }
392