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>
60 #include <sys/taskqueue.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
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>
73 #include <netinet/in.h>
76 * Note that stdarg.h and the ANSI style va_start macro is used for both
77 * ANSI and traditional C compilers.
79 #include <machine/stdarg.h>
81 extern struct mtx ncl_iod_mutex;
82 extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
83 extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
84 extern int ncl_numasync;
85 extern unsigned int ncl_iodmax;
86 extern struct nfsstats newnfsstats;
88 struct task ncl_nfsiodnew_task;
91 ncl_uninit(struct vfsconf *vfsp)
94 * XXX: Unloading of nfscl module is unsupported.
100 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
101 * any sleeping nfsiods so they check ncl_iodmax and exit.
103 mtx_lock(&ncl_iod_mutex);
105 for (i = 0; i < ncl_numasync; i++)
106 if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
107 wakeup(&ncl_iodwant[i]);
108 /* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
110 msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
111 mtx_unlock(&ncl_iod_mutex);
120 ncl_dircookie_lock(struct nfsnode *np)
122 mtx_lock(&np->n_mtx);
123 while (np->n_flag & NDIRCOOKIELK)
124 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
125 np->n_flag |= NDIRCOOKIELK;
126 mtx_unlock(&np->n_mtx);
130 ncl_dircookie_unlock(struct nfsnode *np)
132 mtx_lock(&np->n_mtx);
133 np->n_flag &= ~NDIRCOOKIELK;
135 mtx_unlock(&np->n_mtx);
139 ncl_upgrade_vnlock(struct vnode *vp)
143 ASSERT_VOP_LOCKED(vp, "ncl_upgrade_vnlock");
144 old_lock = NFSVOPISLOCKED(vp);
145 if (old_lock != LK_EXCLUSIVE) {
146 KASSERT(old_lock == LK_SHARED,
147 ("ncl_upgrade_vnlock: wrong old_lock %d", old_lock));
148 /* Upgrade to exclusive lock, this might block */
149 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
155 ncl_downgrade_vnlock(struct vnode *vp, int old_lock)
157 if (old_lock != LK_EXCLUSIVE) {
158 KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
159 /* Downgrade from exclusive lock. */
160 NFSVOPLOCK(vp, LK_DOWNGRADE | LK_RETRY);
165 ncl_printf(const char *fmt, ...)
177 #include <sys/sysctl.h>
178 SYSCTL_DECL(_vfs_nfs);
179 static int nfs_acdebug;
180 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
184 * Check the time stamp
185 * If the cache is valid, copy contents to *vap and return 0
186 * otherwise return an error
189 ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
193 struct nfsmount *nmp;
194 int timeo, mustflush;
197 vap = &np->n_vattr.na_vattr;
198 nmp = VFSTONFS(vp->v_mount);
199 mustflush = nfscl_mustflush(vp); /* must be before mtx_lock() */
201 mtx_lock(&Giant); /* ncl_printf() */
203 mtx_lock(&np->n_mtx);
204 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
205 timeo = (time_second - np->n_mtime.tv_sec) / 10;
209 ncl_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
212 if (vap->va_type == VDIR) {
213 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
214 timeo = nmp->nm_acdirmin;
215 else if (timeo > nmp->nm_acdirmax)
216 timeo = nmp->nm_acdirmax;
218 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
219 timeo = nmp->nm_acregmin;
220 else if (timeo > nmp->nm_acregmax)
221 timeo = nmp->nm_acregmax;
226 ncl_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
227 nmp->nm_acregmin, nmp->nm_acregmax,
228 nmp->nm_acdirmin, nmp->nm_acdirmax);
231 ncl_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
232 (time_second - np->n_attrstamp), timeo);
235 if ((time_second - np->n_attrstamp) >= timeo &&
236 (mustflush != 0 || np->n_attrstamp == 0)) {
237 newnfsstats.attrcache_misses++;
238 mtx_unlock(&np->n_mtx);
240 mtx_unlock(&Giant); /* ncl_printf() */
242 KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
245 newnfsstats.attrcache_hits++;
246 if (vap->va_size != np->n_size) {
247 if (vap->va_type == VREG) {
248 if (np->n_flag & NMODIFIED) {
249 if (vap->va_size < np->n_size)
250 vap->va_size = np->n_size;
252 np->n_size = vap->va_size;
254 np->n_size = vap->va_size;
256 vnode_pager_setsize(vp, np->n_size);
258 np->n_size = vap->va_size;
261 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
262 if (np->n_flag & NCHG) {
263 if (np->n_flag & NACC)
264 vaper->va_atime = np->n_atim;
265 if (np->n_flag & NUPD)
266 vaper->va_mtime = np->n_mtim;
268 mtx_unlock(&np->n_mtx);
270 mtx_unlock(&Giant); /* ncl_printf() */
272 KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
276 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
278 * This function finds the directory cookie that corresponds to the
279 * logical byte offset given.
282 ncl_getcookie(struct nfsnode *np, off_t off, int add)
284 struct nfsdmap *dp, *dp2;
286 nfsuint64 *retval = NULL;
288 pos = (uoff_t)off / NFS_DIRBLKSIZ;
289 if (pos == 0 || off < 0) {
290 KASSERT(!add, ("nfs getcookie add at <= 0"));
291 return (&nfs_nullcookie);
294 dp = LIST_FIRST(&np->n_cookies);
297 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
298 M_NFSDIROFF, M_WAITOK);
299 dp->ndm_eocookie = 0;
300 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
304 while (pos >= NFSNUMCOOKIES) {
305 pos -= NFSNUMCOOKIES;
306 if (LIST_NEXT(dp, ndm_list)) {
307 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
308 pos >= dp->ndm_eocookie)
310 dp = LIST_NEXT(dp, ndm_list);
312 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
313 M_NFSDIROFF, M_WAITOK);
314 dp2->ndm_eocookie = 0;
315 LIST_INSERT_AFTER(dp, dp2, ndm_list);
320 if (pos >= dp->ndm_eocookie) {
322 dp->ndm_eocookie = pos + 1;
326 retval = &dp->ndm_cookies[pos];
332 * Invalidate cached directory information, except for the actual directory
333 * blocks (which are invalidated separately).
334 * Done mainly to avoid the use of stale offset cookies.
337 ncl_invaldir(struct vnode *vp)
339 struct nfsnode *np = VTONFS(vp);
341 KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
342 ncl_dircookie_lock(np);
343 np->n_direofoffset = 0;
344 np->n_cookieverf.nfsuquad[0] = 0;
345 np->n_cookieverf.nfsuquad[1] = 0;
346 if (LIST_FIRST(&np->n_cookies))
347 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
348 ncl_dircookie_unlock(np);
352 * The write verifier has changed (probably due to a server reboot), so all
353 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
354 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
355 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
358 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
359 * writes are not clusterable.
362 ncl_clearcommit(struct mount *mp)
364 struct vnode *vp, *nvp;
365 struct buf *bp, *nbp;
368 MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
373 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
374 if (!BUF_ISLOCKED(bp) &&
375 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
376 == (B_DELWRI | B_NEEDCOMMIT))
377 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
385 * Called once to initialize data structures...
388 ncl_init(struct vfsconf *vfsp)
392 /* Ensure async daemons disabled */
393 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
394 ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
395 ncl_iodmount[i] = NULL;
397 TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL);
398 ncl_nhinit(); /* Init the nfsnode table */