2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from nfs_subs.c 8.8 (Berkeley) 5/22/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/namei.h>
54 #include <sys/socket.h>
56 #include <sys/malloc.h>
57 #include <sys/sysent.h>
58 #include <sys/syscall.h>
59 #include <sys/sysproto.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_extern.h>
66 #include <fs/nfs/nfsport.h>
67 #include <fs/nfsclient/nfsnode.h>
68 #include <fs/nfsclient/nfsmount.h>
69 #include <fs/nfsclient/nfs.h>
71 #include <netinet/in.h>
74 * Note that stdarg.h and the ANSI style va_start macro is used for both
75 * ANSI and traditional C compilers.
77 #include <machine/stdarg.h>
79 extern struct mtx ncl_iod_mutex;
80 extern enum nfsiod_state ncl_iodwant[NFS_MAXRAHEAD];
81 extern struct nfsmount *ncl_iodmount[NFS_MAXRAHEAD];
82 extern int ncl_numasync;
83 extern unsigned int ncl_iodmax;
84 extern struct nfsstats newnfsstats;
87 ncl_uninit(struct vfsconf *vfsp)
90 * XXX: Unloading of nfscl module is unsupported.
96 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
97 * any sleeping nfsiods so they check ncl_iodmax and exit.
99 mtx_lock(&ncl_iod_mutex);
101 for (i = 0; i < ncl_numasync; i++)
102 if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
103 wakeup(&ncl_iodwant[i]);
104 /* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
106 msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
107 mtx_unlock(&ncl_iod_mutex);
116 ncl_dircookie_lock(struct nfsnode *np)
118 mtx_lock(&np->n_mtx);
119 while (np->n_flag & NDIRCOOKIELK)
120 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
121 np->n_flag |= NDIRCOOKIELK;
122 mtx_unlock(&np->n_mtx);
126 ncl_dircookie_unlock(struct nfsnode *np)
128 mtx_lock(&np->n_mtx);
129 np->n_flag &= ~NDIRCOOKIELK;
131 mtx_unlock(&np->n_mtx);
135 ncl_upgrade_vnlock(struct vnode *vp)
139 ASSERT_VOP_LOCKED(vp, "ncl_upgrade_vnlock");
140 old_lock = VOP_ISLOCKED(vp);
141 if (old_lock != LK_EXCLUSIVE) {
142 KASSERT(old_lock == LK_SHARED,
143 ("ncl_upgrade_vnlock: wrong old_lock %d", old_lock));
144 /* Upgrade to exclusive lock, this might block */
145 vn_lock(vp, LK_UPGRADE | LK_RETRY);
151 ncl_downgrade_vnlock(struct vnode *vp, int old_lock)
153 if (old_lock != LK_EXCLUSIVE) {
154 KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
155 /* Downgrade from exclusive lock. */
156 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
161 ncl_printf(const char *fmt, ...)
173 #include <sys/sysctl.h>
174 SYSCTL_DECL(_vfs_newnfs);
175 static int nfs_acdebug;
176 SYSCTL_INT(_vfs_newnfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
180 * Check the time stamp
181 * If the cache is valid, copy contents to *vap and return 0
182 * otherwise return an error
185 ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
189 struct nfsmount *nmp;
194 vap = &np->n_vattr.na_vattr;
195 nmp = VFSTONFS(vp->v_mount);
196 mustflush = nfscl_mustflush(vp); /* must be before mtx_lock() */
198 mtx_lock(&Giant); /* ncl_printf() */
200 mtx_lock(&np->n_mtx);
201 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
202 timeo = (time_second - np->n_mtime.tv_sec) / 10;
206 ncl_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
209 if (vap->va_type == VDIR) {
210 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
211 timeo = nmp->nm_acdirmin;
212 else if (timeo > nmp->nm_acdirmax)
213 timeo = nmp->nm_acdirmax;
215 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
216 timeo = nmp->nm_acregmin;
217 else if (timeo > nmp->nm_acregmax)
218 timeo = nmp->nm_acregmax;
223 ncl_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
224 nmp->nm_acregmin, nmp->nm_acregmax,
225 nmp->nm_acdirmin, nmp->nm_acdirmax);
228 ncl_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
229 (time_second - np->n_attrstamp), timeo);
232 if ((time_second - np->n_attrstamp) >= timeo &&
233 (mustflush || np->n_attrstamp == 0)) {
234 newnfsstats.attrcache_misses++;
235 mtx_unlock(&np->n_mtx);
237 mtx_unlock(&Giant); /* ncl_printf() */
241 newnfsstats.attrcache_hits++;
242 if (vap->va_size != np->n_size) {
243 if (vap->va_type == VREG) {
244 if (np->n_flag & NMODIFIED) {
245 if (vap->va_size < np->n_size)
246 vap->va_size = np->n_size;
248 np->n_size = vap->va_size;
250 np->n_size = vap->va_size;
252 vnode_pager_setsize(vp, np->n_size);
254 np->n_size = vap->va_size;
257 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
258 if (np->n_flag & NCHG) {
259 if (np->n_flag & NACC)
260 vaper->va_atime = np->n_atim;
261 if (np->n_flag & NUPD)
262 vaper->va_mtime = np->n_mtim;
264 mtx_unlock(&np->n_mtx);
266 mtx_unlock(&Giant); /* ncl_printf() */
271 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
273 * This function finds the directory cookie that corresponds to the
274 * logical byte offset given.
277 ncl_getcookie(struct nfsnode *np, off_t off, int add)
279 struct nfsdmap *dp, *dp2;
281 nfsuint64 *retval = NULL;
283 pos = (uoff_t)off / NFS_DIRBLKSIZ;
284 if (pos == 0 || off < 0) {
285 KASSERT(!add, ("nfs getcookie add at <= 0"));
286 return (&nfs_nullcookie);
289 dp = LIST_FIRST(&np->n_cookies);
292 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
293 M_NFSDIROFF, M_WAITOK);
294 dp->ndm_eocookie = 0;
295 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
299 while (pos >= NFSNUMCOOKIES) {
300 pos -= NFSNUMCOOKIES;
301 if (LIST_NEXT(dp, ndm_list)) {
302 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
303 pos >= dp->ndm_eocookie)
305 dp = LIST_NEXT(dp, ndm_list);
307 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
308 M_NFSDIROFF, M_WAITOK);
309 dp2->ndm_eocookie = 0;
310 LIST_INSERT_AFTER(dp, dp2, ndm_list);
315 if (pos >= dp->ndm_eocookie) {
317 dp->ndm_eocookie = pos + 1;
321 retval = &dp->ndm_cookies[pos];
327 * Invalidate cached directory information, except for the actual directory
328 * blocks (which are invalidated separately).
329 * Done mainly to avoid the use of stale offset cookies.
332 ncl_invaldir(struct vnode *vp)
334 struct nfsnode *np = VTONFS(vp);
336 KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
337 ncl_dircookie_lock(np);
338 np->n_direofoffset = 0;
339 np->n_cookieverf.nfsuquad[0] = 0;
340 np->n_cookieverf.nfsuquad[1] = 0;
341 if (LIST_FIRST(&np->n_cookies))
342 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
343 ncl_dircookie_unlock(np);
347 * The write verifier has changed (probably due to a server reboot), so all
348 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
349 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
350 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
353 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
354 * writes are not clusterable.
357 ncl_clearcommit(struct mount *mp)
359 struct vnode *vp, *nvp;
360 struct buf *bp, *nbp;
364 MNT_VNODE_FOREACH(vp, mp, nvp) {
367 if (vp->v_iflag & VI_DOOMED) {
375 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
376 if (!BUF_ISLOCKED(bp) &&
377 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
378 == (B_DELWRI | B_NEEDCOMMIT))
379 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
389 * Called once to initialize data structures...
392 ncl_init(struct vfsconf *vfsp)
396 /* Ensure async daemons disabled */
397 for (i = 0; i < NFS_MAXRAHEAD; i++) {
398 ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
399 ncl_iodmount[i] = NULL;
401 ncl_nhinit(); /* Init the nfsnode table */