2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * from nfs_subs.c 8.8 (Berkeley) 5/22/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
56 #include <sys/socket.h>
58 #include <sys/malloc.h>
59 #include <sys/sysent.h>
60 #include <sys/syscall.h>
61 #include <sys/sysproto.h>
62 #include <sys/taskqueue.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
69 #include <fs/nfs/nfsport.h>
70 #include <fs/nfsclient/nfsnode.h>
71 #include <fs/nfsclient/nfsmount.h>
72 #include <fs/nfsclient/nfs.h>
73 #include <fs/nfsclient/nfs_kdtrace.h>
75 #include <netinet/in.h>
78 * Note that stdarg.h and the ANSI style va_start macro is used for both
79 * ANSI and traditional C compilers.
81 #include <machine/stdarg.h>
83 extern struct mtx ncl_iod_mutex;
84 extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
85 extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
86 extern int ncl_numasync;
87 extern unsigned int ncl_iodmax;
88 extern struct nfsstatsv1 nfsstatsv1;
90 struct task ncl_nfsiodnew_task;
93 ncl_uninit(struct vfsconf *vfsp)
96 * XXX: Unloading of nfscl module is unsupported.
102 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
103 * any sleeping nfsiods so they check ncl_iodmax and exit.
107 for (i = 0; i < ncl_numasync; i++)
108 if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
109 wakeup(&ncl_iodwant[i]);
110 /* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
112 msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
122 ncl_dircookie_lock(struct nfsnode *np)
125 while (np->n_flag & NDIRCOOKIELK)
126 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
127 np->n_flag |= NDIRCOOKIELK;
132 ncl_dircookie_unlock(struct nfsnode *np)
135 np->n_flag &= ~NDIRCOOKIELK;
141 ncl_excl_start(struct vnode *vp)
146 ASSERT_VOP_LOCKED(vp, "ncl_excl_start");
147 vn_lk = NFSVOPISLOCKED(vp);
148 if (vn_lk == LK_EXCLUSIVE)
150 KASSERT(vn_lk == LK_SHARED,
151 ("ncl_excl_start: wrong vnode lock %d", vn_lk));
152 /* Ensure exclusive access, this might block */
154 lockmgr(&np->n_excl, LK_EXCLUSIVE, NULL);
159 ncl_excl_finish(struct vnode *vp, bool old_lock)
166 lockmgr(&np->n_excl, LK_RELEASE, NULL);
170 #include <sys/sysctl.h>
171 SYSCTL_DECL(_vfs_nfs);
172 static int nfs_acdebug;
173 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
177 * Check the time stamp
178 * If the cache is valid, copy contents to *vap and return 0
179 * otherwise return an error
182 ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
186 struct nfsmount *nmp;
187 int timeo, mustflush;
192 vap = &np->n_vattr.na_vattr;
193 nmp = VFSTONFS(vp->v_mount);
194 mustflush = nfscl_mustflush(vp); /* must be before mtx_lock() */
196 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
197 timeo = (time_second - np->n_mtime.tv_sec) / 10;
201 printf("ncl_getattrcache: initial timeo = %d\n", timeo);
204 if (vap->va_type == VDIR) {
205 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
206 timeo = nmp->nm_acdirmin;
207 else if (timeo > nmp->nm_acdirmax)
208 timeo = nmp->nm_acdirmax;
210 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
211 timeo = nmp->nm_acregmin;
212 else if (timeo > nmp->nm_acregmax)
213 timeo = nmp->nm_acregmax;
218 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
219 nmp->nm_acregmin, nmp->nm_acregmax,
220 nmp->nm_acdirmin, nmp->nm_acdirmax);
223 printf("ncl_getattrcache: age = %d; final timeo = %d\n",
224 (time_second - np->n_attrstamp), timeo);
227 if ((time_second - np->n_attrstamp) >= timeo &&
228 (mustflush != 0 || np->n_attrstamp == 0)) {
229 nfsstatsv1.attrcache_misses++;
231 KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
234 nfsstatsv1.attrcache_hits++;
236 if (vap->va_size != np->n_size) {
237 if (vap->va_type == VREG) {
238 if (np->n_flag & NMODIFIED) {
239 if (vap->va_size < np->n_size)
240 vap->va_size = np->n_size;
242 np->n_size = vap->va_size;
244 np->n_size = vap->va_size;
246 setnsize = ncl_pager_setsize(vp, &nsize);
248 np->n_size = vap->va_size;
251 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
252 if (np->n_flag & NCHG) {
253 if (np->n_flag & NACC)
254 vaper->va_atime = np->n_atim;
255 if (np->n_flag & NUPD)
256 vaper->va_mtime = np->n_mtim;
260 vnode_pager_setsize(vp, nsize);
261 KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
265 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
267 * This function finds the directory cookie that corresponds to the
268 * logical byte offset given.
271 ncl_getcookie(struct nfsnode *np, off_t off, int add)
273 struct nfsdmap *dp, *dp2;
275 nfsuint64 *retval = NULL;
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);
283 dp = LIST_FIRST(&np->n_cookies);
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);
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)
299 dp = LIST_NEXT(dp, ndm_list);
301 dp2 = malloc(sizeof (struct nfsdmap),
302 M_NFSDIROFF, M_WAITOK);
303 dp2->ndm_eocookie = 0;
304 LIST_INSERT_AFTER(dp, dp2, ndm_list);
309 if (pos >= dp->ndm_eocookie) {
311 dp->ndm_eocookie = pos + 1;
315 retval = &dp->ndm_cookies[pos];
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.
326 ncl_invaldir(struct vnode *vp)
328 struct nfsnode *np = VTONFS(vp);
330 KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
331 ncl_dircookie_lock(np);
332 np->n_direofoffset = 0;
333 np->n_cookieverf.nfsuquad[0] = 0;
334 np->n_cookieverf.nfsuquad[1] = 0;
335 if (LIST_FIRST(&np->n_cookies))
336 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
337 ncl_dircookie_unlock(np);
341 * The write verifier has changed (probably due to a server reboot), so all
342 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
343 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
344 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
347 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
348 * writes are not clusterable.
351 ncl_clearcommit(struct mount *mp)
353 struct vnode *vp, *nvp;
354 struct buf *bp, *nbp;
357 MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
362 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
363 if (!BUF_ISLOCKED(bp) &&
364 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
365 == (B_DELWRI | B_NEEDCOMMIT))
366 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
374 * Called once to initialize data structures...
377 ncl_init(struct vfsconf *vfsp)
381 /* Ensure async daemons disabled */
382 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
383 ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
384 ncl_iodmount[i] = NULL;
386 TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL);
387 ncl_nhinit(); /* Init the nfsnode table */