svn commit: r233285 - in stable/9/sys: fs/nfsclient i386/conf kern
nfsclient sys
John Baldwin
jhb at FreeBSD.org
Wed Mar 21 20:50:16 UTC 2012
Author: jhb
Date: Wed Mar 21 20:50:15 2012
New Revision: 233285
URL: http://svn.freebsd.org/changeset/base/233285
Log:
MFC 230394,230441,230489,230552,232420:
Close a race in NFS lookup processing that could result in stale name cache
entries on one client when a directory was renamed on another client. The
root cause for the stale entry being trusted is that each per-vnode nfsnode
structure has a single 'n_ctime' timestamp used to validate positive name
cache entries. However, if there are multiple entries for a single vnode,
they all share a single timestamp. To fix this, extend the name cache
to allow filesystems to optionally store a timestamp value in each name
cache entry. The NFS clients now fetch the timestamp associated with
each name cache entry and use that to validate cache hits instead of the
timestamps previously stored in the nfsnode. Another part of the fix is
that the NFS clients now use timestamps from the post-op attributes of
RPCs when adding name cache entries rather than pulling the timestamps out
of the file's attribute cache. The latter is subject to races with other
lookups updating the attribute cache concurrently.
Modified:
stable/9/sys/fs/nfsclient/nfs_clrpcops.c
stable/9/sys/fs/nfsclient/nfs_clvnops.c
stable/9/sys/fs/nfsclient/nfsnode.h
stable/9/sys/kern/vfs_cache.c
stable/9/sys/nfsclient/nfs_subs.c
stable/9/sys/nfsclient/nfs_vnops.c
stable/9/sys/nfsclient/nfsm_subs.h
stable/9/sys/nfsclient/nfsnode.h
stable/9/sys/sys/vnode.h
Directory Properties:
stable/9/sys/ (props changed)
stable/9/sys/amd64/include/xen/ (props changed)
stable/9/sys/boot/ (props changed)
stable/9/sys/boot/i386/efi/ (props changed)
stable/9/sys/boot/ia64/efi/ (props changed)
stable/9/sys/boot/ia64/ski/ (props changed)
stable/9/sys/boot/powerpc/boot1.chrp/ (props changed)
stable/9/sys/boot/powerpc/ofw/ (props changed)
stable/9/sys/cddl/contrib/opensolaris/ (props changed)
stable/9/sys/conf/ (props changed)
stable/9/sys/contrib/dev/acpica/ (props changed)
stable/9/sys/contrib/octeon-sdk/ (props changed)
stable/9/sys/contrib/pf/ (props changed)
stable/9/sys/contrib/x86emu/ (props changed)
stable/9/sys/fs/ (props changed)
stable/9/sys/fs/ntfs/ (props changed)
stable/9/sys/i386/conf/XENHVM (props changed)
Modified: stable/9/sys/fs/nfsclient/nfs_clrpcops.c
==============================================================================
--- stable/9/sys/fs/nfsclient/nfs_clrpcops.c Wed Mar 21 19:09:52 2012 (r233284)
+++ stable/9/sys/fs/nfsclient/nfs_clrpcops.c Wed Mar 21 20:50:15 2012 (r233285)
@@ -2951,10 +2951,12 @@ nfsrpc_readdirplus(vnode_t vp, struct ui
nfsattrbit_t attrbits, dattrbits;
size_t tresid;
u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr;
+ struct timespec dctime;
KASSERT(uiop->uio_iovcnt == 1 &&
(uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0,
("nfs readdirplusrpc bad uio"));
+ timespecclear(&dctime);
*attrflagp = 0;
if (eofp != NULL)
*eofp = 0;
@@ -2997,6 +2999,7 @@ nfsrpc_readdirplus(vnode_t vp, struct ui
#endif
if (error)
return (error);
+ dctime = nfsva.na_ctime;
dotfileid = nfsva.na_fileid;
NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp);
NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
@@ -3134,6 +3137,8 @@ nfsrpc_readdirplus(vnode_t vp, struct ui
error = nd->nd_repstat;
goto nfsmout;
}
+ if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0)
+ dctime = nap->na_ctime;
NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
NFSLOCKNODE(dnp);
dnp->n_cookieverf.nfsuquad[0] = *tl++;
@@ -3316,9 +3321,14 @@ nfsrpc_readdirplus(vnode_t vp, struct ui
vtonfs_dtype(np->n_vattr.na_type);
ndp->ni_vp = newvp;
NFSCNHASH(cnp, HASHINIT);
- if (cnp->cn_namelen <= NCHNAMLEN) {
- np->n_ctime = np->n_vattr.na_ctime;
- cache_enter(ndp->ni_dvp,ndp->ni_vp,cnp);
+ if (cnp->cn_namelen <= NCHNAMLEN &&
+ (newvp->v_type != VDIR ||
+ dctime.tv_sec != 0)) {
+ cache_enter_time(ndp->ni_dvp,
+ ndp->ni_vp, cnp,
+ &nfsva.na_ctime,
+ newvp->v_type != VDIR ? NULL :
+ &dctime);
}
if (unlocknewvp)
vput(newvp);
Modified: stable/9/sys/fs/nfsclient/nfs_clvnops.c
==============================================================================
--- stable/9/sys/fs/nfsclient/nfs_clvnops.c Wed Mar 21 19:09:52 2012 (r233284)
+++ stable/9/sys/fs/nfsclient/nfs_clvnops.c Wed Mar 21 20:50:15 2012 (r233285)
@@ -1016,12 +1016,12 @@ nfs_lookup(struct vop_lookup_args *ap)
struct vnode *newvp;
struct nfsmount *nmp;
struct nfsnode *np, *newnp;
- int error = 0, attrflag, dattrflag, ltype;
+ int error = 0, attrflag, dattrflag, ltype, ncticks;
struct thread *td = cnp->cn_thread;
struct nfsfh *nfhp;
struct nfsvattr dnfsva, nfsva;
struct vattr vattr;
- struct timespec dmtime;
+ struct timespec nctime;
*vpp = NULLVP;
if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
@@ -1042,11 +1042,24 @@ nfs_lookup(struct vop_lookup_args *ap)
if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
return (error);
- error = cache_lookup(dvp, vpp, cnp);
+ error = cache_lookup_times(dvp, vpp, cnp, &nctime, &ncticks);
if (error > 0 && error != ENOENT)
return (error);
if (error == -1) {
/*
+ * Lookups of "." are special and always return the
+ * current directory. cache_lookup() already handles
+ * associated locking bookkeeping, etc.
+ */
+ if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
+ /* XXX: Is this really correct? */
+ if (cnp->cn_nameiop != LOOKUP &&
+ (flags & ISLASTCN))
+ cnp->cn_flags |= SAVENAME;
+ return (0);
+ }
+
+ /*
* We only accept a positive hit in the cache if the
* change time of the file matches our cached copy.
* Otherwise, we discard the cache entry and fallback
@@ -1073,7 +1086,7 @@ nfs_lookup(struct vop_lookup_args *ap)
}
if (nfscl_nodeleg(newvp, 0) == 0 ||
(VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
- timespeccmp(&vattr.va_ctime, &newnp->n_ctime, ==))) {
+ timespeccmp(&vattr.va_ctime, &nctime, ==))) {
NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
if (cnp->cn_nameiop != LOOKUP &&
(flags & ISLASTCN))
@@ -1092,36 +1105,21 @@ nfs_lookup(struct vop_lookup_args *ap)
/*
* We only accept a negative hit in the cache if the
* modification time of the parent directory matches
- * our cached copy. Otherwise, we discard all of the
- * negative cache entries for this directory. We also
- * only trust -ve cache entries for less than
- * nm_negative_namecache_timeout seconds.
+ * the cached copy in the name cache entry.
+ * Otherwise, we discard all of the negative cache
+ * entries for this directory. We also only trust
+ * negative cache entries for up to nm_negnametimeo
+ * seconds.
*/
- if ((u_int)(ticks - np->n_dmtime_ticks) <
- (nmp->nm_negnametimeo * hz) &&
+ if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
- timespeccmp(&vattr.va_mtime, &np->n_dmtime, ==)) {
+ timespeccmp(&vattr.va_mtime, &nctime, ==)) {
NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
return (ENOENT);
}
cache_purge_negative(dvp);
- mtx_lock(&np->n_mtx);
- timespecclear(&np->n_dmtime);
- mtx_unlock(&np->n_mtx);
}
- /*
- * Cache the modification time of the parent directory in case
- * the lookup fails and results in adding the first negative
- * name cache entry for the directory. Since this is reading
- * a single time_t, don't bother with locking. The
- * modification time may be a bit stale, but it must be read
- * before performing the lookup RPC to prevent a race where
- * another lookup updates the timestamp on the directory after
- * the lookup RPC has been performed on the server but before
- * n_dmtime is set at the end of this function.
- */
- dmtime = np->n_vattr.na_mtime;
error = 0;
newvp = NULLVP;
NFSINCRGLOBAL(newnfsstats.lookupcache_misses);
@@ -1157,30 +1155,22 @@ nfs_lookup(struct vop_lookup_args *ap)
return (EJUSTRETURN);
}
- if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) {
+ if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
+ dattrflag) {
/*
- * Maintain n_dmtime as the modification time
- * of the parent directory when the oldest -ve
- * name cache entry for this directory was
- * added. If a -ve cache entry has already
- * been added with a newer modification time
- * by a concurrent lookup, then don't bother
- * adding a cache entry. The modification
- * time of the directory might have changed
- * due to the file this lookup failed to find
- * being created. In that case a subsequent
- * lookup would incorrectly use the entry
- * added here instead of doing an extra
- * lookup.
+ * Cache the modification time of the parent
+ * directory from the post-op attributes in
+ * the name cache entry. The negative cache
+ * entry will be ignored once the directory
+ * has changed. Don't bother adding the entry
+ * if the directory has already changed.
*/
mtx_lock(&np->n_mtx);
- if (timespeccmp(&np->n_dmtime, &dmtime, <=)) {
- if (!timespecisset(&np->n_dmtime)) {
- np->n_dmtime = dmtime;
- np->n_dmtime_ticks = ticks;
- }
+ if (timespeccmp(&np->n_vattr.na_mtime,
+ &dnfsva.na_mtime, ==)) {
mtx_unlock(&np->n_mtx);
- cache_enter(dvp, NULL, cnp);
+ cache_enter_time(dvp, NULL, cnp,
+ &dnfsva.na_mtime, NULL);
} else
mtx_unlock(&np->n_mtx);
}
@@ -1279,10 +1269,10 @@ nfs_lookup(struct vop_lookup_args *ap)
if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
cnp->cn_flags |= SAVENAME;
if ((cnp->cn_flags & MAKEENTRY) &&
- (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
- np->n_ctime = np->n_vattr.na_vattr.va_ctime;
- cache_enter(dvp, newvp, cnp);
- }
+ (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
+ attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
+ cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
+ newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
*vpp = newvp;
return (0);
}
@@ -1442,8 +1432,6 @@ nfs_mknodrpc(struct vnode *dvp, struct v
}
}
if (!error) {
- if ((cnp->cn_flags & MAKEENTRY))
- cache_enter(dvp, newvp, cnp);
*vpp = newvp;
} else if (NFS_ISV4(dvp)) {
error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
@@ -1601,8 +1589,9 @@ again:
}
}
if (!error) {
- if (cnp->cn_flags & MAKEENTRY)
- cache_enter(dvp, newvp, cnp);
+ if ((cnp->cn_flags & MAKEENTRY) && attrflag)
+ cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
+ NULL);
*ap->a_vpp = newvp;
} else if (NFS_ISV4(dvp)) {
error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
@@ -1977,8 +1966,9 @@ nfs_link(struct vop_link_args *ap)
* must care about lookup caching hit rate, so...
*/
if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
- (cnp->cn_flags & MAKEENTRY))
- cache_enter(tdvp, vp, cnp);
+ (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
+ cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
+ }
if (error && NFS_ISV4(vp))
error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
(gid_t)0);
@@ -2034,15 +2024,6 @@ nfs_symlink(struct vop_symlink_args *ap)
error = nfscl_maperr(cnp->cn_thread, error,
vap->va_uid, vap->va_gid);
} else {
- /*
- * If negative lookup caching is enabled, I might as well
- * add an entry for this node. Not necessary for correctness,
- * but if negative caching is enabled, then the system
- * must care about lookup caching hit rate, so...
- */
- if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
- (cnp->cn_flags & MAKEENTRY))
- cache_enter(dvp, newvp, cnp);
*ap->a_vpp = newvp;
}
@@ -2057,6 +2038,16 @@ nfs_symlink(struct vop_symlink_args *ap)
mtx_unlock(&dnp->n_mtx);
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
}
+ /*
+ * If negative lookup caching is enabled, I might as well
+ * add an entry for this node. Not necessary for correctness,
+ * but if negative caching is enabled, then the system
+ * must care about lookup caching hit rate, so...
+ */
+ if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
+ (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
+ cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
+ }
return (error);
}
@@ -2127,8 +2118,10 @@ nfs_mkdir(struct vop_mkdir_args *ap)
* must care about lookup caching hit rate, so...
*/
if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
- (cnp->cn_flags & MAKEENTRY))
- cache_enter(dvp, newvp, cnp);
+ (cnp->cn_flags & MAKEENTRY) &&
+ attrflag != 0 && dattrflag != 0)
+ cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
+ &dnfsva.na_ctime);
*ap->a_vpp = newvp;
}
return (error);
Modified: stable/9/sys/fs/nfsclient/nfsnode.h
==============================================================================
--- stable/9/sys/fs/nfsclient/nfsnode.h Wed Mar 21 19:09:52 2012 (r233284)
+++ stable/9/sys/fs/nfsclient/nfsnode.h Wed Mar 21 20:50:15 2012 (r233285)
@@ -99,9 +99,9 @@ struct nfsnode {
time_t n_attrstamp; /* Attr. cache timestamp */
struct nfs_accesscache n_accesscache[NFS_ACCESSCACHESIZE];
struct timespec n_mtime; /* Prev modify time. */
- struct timespec n_ctime; /* Prev create time. */
- struct timespec n_dmtime; /* Prev dir modify time. */
- int n_dmtime_ticks; /* Tick of -ve cache entry */
+ struct timespec n_unused0;
+ struct timespec n_unused1;
+ int n_unused2;
struct nfsfh *n_fhp; /* NFS File Handle */
struct vnode *n_vnode; /* associated vnode */
struct vnode *n_dvp; /* parent vnode */
Modified: stable/9/sys/kern/vfs_cache.c
==============================================================================
--- stable/9/sys/kern/vfs_cache.c Wed Mar 21 19:09:52 2012 (r233284)
+++ stable/9/sys/kern/vfs_cache.c Wed Mar 21 20:50:15 2012 (r233285)
@@ -103,6 +103,36 @@ struct namecache {
};
/*
+ * struct namecache_ts repeats struct namecache layout up to the
+ * nc_nlen member.
+ * struct namecache_ts is used in place of struct namecache when time(s) need
+ * to be stored. The nc_dotdottime field is used when a cache entry is mapping
+ * both a non-dotdot directory name plus dotdot for the directory's
+ * parent.
+ */
+struct namecache_ts {
+ LIST_ENTRY(namecache) nc_hash; /* hash chain */
+ LIST_ENTRY(namecache) nc_src; /* source vnode list */
+ TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
+ struct vnode *nc_dvp; /* vnode of parent of name */
+ struct vnode *nc_vp; /* vnode the name refers to */
+ u_char nc_flag; /* flag bits */
+ u_char nc_nlen; /* length of name */
+ struct timespec nc_time; /* timespec provided by fs */
+ struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
+ int nc_ticks; /* ticks value when entry was added */
+ char nc_name[0]; /* segment name + nul */
+};
+
+/*
+ * Flags in namecache.nc_flag
+ */
+#define NCF_WHITE 0x01
+#define NCF_ISDOTDOT 0x02
+#define NCF_TS 0x04
+#define NCF_DTS 0x08
+
+/*
* Name caching works as follows:
*
* Names found by directory scans are retained in a cache
@@ -164,20 +194,71 @@ RW_SYSINIT(vfscache, &cache_lock, "Name
* fit in the small cache.
*/
static uma_zone_t cache_zone_small;
+static uma_zone_t cache_zone_small_ts;
static uma_zone_t cache_zone_large;
+static uma_zone_t cache_zone_large_ts;
#define CACHE_PATH_CUTOFF 35
-#define CACHE_ZONE_SMALL (sizeof(struct namecache) + CACHE_PATH_CUTOFF \
- + 1)
-#define CACHE_ZONE_LARGE (sizeof(struct namecache) + NAME_MAX + 1)
-
-#define cache_alloc(len) uma_zalloc(((len) <= CACHE_PATH_CUTOFF) ? \
- cache_zone_small : cache_zone_large, M_WAITOK)
-#define cache_free(ncp) do { \
- if (ncp != NULL) \
- uma_zfree(((ncp)->nc_nlen <= CACHE_PATH_CUTOFF) ? \
- cache_zone_small : cache_zone_large, (ncp)); \
-} while (0)
+
+static struct namecache *
+cache_alloc(int len, int ts)
+{
+
+ if (len > CACHE_PATH_CUTOFF) {
+ if (ts)
+ return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
+ else
+ return (uma_zalloc(cache_zone_large, M_WAITOK));
+ }
+ if (ts)
+ return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
+ else
+ return (uma_zalloc(cache_zone_small, M_WAITOK));
+}
+
+static void
+cache_free(struct namecache *ncp)
+{
+ int ts;
+
+ if (ncp == NULL)
+ return;
+ ts = ncp->nc_flag & NCF_TS;
+ if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
+ if (ts)
+ uma_zfree(cache_zone_small_ts, ncp);
+ else
+ uma_zfree(cache_zone_small, ncp);
+ } else if (ts)
+ uma_zfree(cache_zone_large_ts, ncp);
+ else
+ uma_zfree(cache_zone_large, ncp);
+}
+
+static char *
+nc_get_name(struct namecache *ncp)
+{
+ struct namecache_ts *ncp_ts;
+
+ if ((ncp->nc_flag & NCF_TS) == 0)
+ return (ncp->nc_name);
+ ncp_ts = (struct namecache_ts *)ncp;
+ return (ncp_ts->nc_name);
+}
+
+static void
+cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
+{
+
+ KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
+ (tsp == NULL && ticksp == NULL),
+ ("No NCF_TS"));
+
+ if (tsp != NULL)
+ *tsp = ((struct namecache_ts *)ncp)->nc_time;
+ if (ticksp != NULL)
+ *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
+}
static int doingcache = 1; /* 1 => enable the cache */
SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
@@ -233,12 +314,6 @@ static int vn_fullpath1(struct thread *t
static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
-/*
- * Flags in namecache.nc_flag
- */
-#define NCF_WHITE 0x01
-#define NCF_ISDOTDOT 0x02
-
#ifdef DIAGNOSTIC
/*
* Grab an atomic snapshot of the name cache hash chain lengths
@@ -343,10 +418,10 @@ cache_zap(ncp)
#ifdef KDTRACE_HOOKS
if (ncp->nc_vp != NULL) {
SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
- ncp->nc_name, ncp->nc_vp, 0, 0);
+ nc_get_name(ncp), ncp->nc_vp, 0, 0);
} else {
SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
- ncp->nc_name, 0, 0, 0);
+ nc_get_name(ncp), 0, 0, 0);
}
#endif
vp = NULL;
@@ -393,10 +468,12 @@ cache_zap(ncp)
*/
int
-cache_lookup(dvp, vpp, cnp)
+cache_lookup_times(dvp, vpp, cnp, tsp, ticksp)
struct vnode *dvp;
struct vnode **vpp;
struct componentname *cnp;
+ struct timespec *tsp;
+ int *ticksp;
{
struct namecache *ncp;
uint32_t hash;
@@ -421,6 +498,10 @@ retry_wlocked:
dothits++;
SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
*vpp, 0, 0);
+ if (tsp != NULL)
+ timespecclear(tsp);
+ if (ticksp != NULL)
+ *ticksp = ticks;
goto success;
}
if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
@@ -439,19 +520,23 @@ retry_wlocked:
CACHE_WUNLOCK();
return (0);
}
- if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
- *vpp = dvp->v_cache_dd->nc_vp;
+ ncp = dvp->v_cache_dd;
+ if (ncp->nc_flag & NCF_ISDOTDOT)
+ *vpp = ncp->nc_vp;
else
- *vpp = dvp->v_cache_dd->nc_dvp;
+ *vpp = ncp->nc_dvp;
/* Return failure if negative entry was found. */
- if (*vpp == NULL) {
- ncp = dvp->v_cache_dd;
+ if (*vpp == NULL)
goto negative_success;
- }
CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
dvp, cnp->cn_nameptr, *vpp);
SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
*vpp, 0, 0);
+ cache_out_ts(ncp, tsp, ticksp);
+ if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
+ NCF_DTS && tsp != NULL)
+ *tsp = ((struct namecache_ts *)ncp)->
+ nc_dotdottime;
goto success;
}
}
@@ -461,7 +546,7 @@ retry_wlocked:
LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
numchecks++;
if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
- !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
+ !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
break;
}
@@ -496,8 +581,9 @@ retry_wlocked:
*vpp = ncp->nc_vp;
CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
dvp, cnp->cn_nameptr, *vpp, ncp);
- SDT_PROBE(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
+ SDT_PROBE(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
*vpp, 0, 0);
+ cache_out_ts(ncp, tsp, ticksp);
goto success;
}
@@ -527,8 +613,9 @@ negative_success:
nchstats.ncs_neghits++;
if (ncp->nc_flag & NCF_WHITE)
cnp->cn_flags |= ISWHITEOUT;
- SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, ncp->nc_name,
+ SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, nc_get_name(ncp),
0, 0, 0);
+ cache_out_ts(ncp, tsp, ticksp);
CACHE_WUNLOCK();
return (ENOENT);
@@ -615,12 +702,15 @@ unlock:
* Add an entry to the cache.
*/
void
-cache_enter(dvp, vp, cnp)
+cache_enter_time(dvp, vp, cnp, tsp, dtsp)
struct vnode *dvp;
struct vnode *vp;
struct componentname *cnp;
+ struct timespec *tsp;
+ struct timespec *dtsp;
{
struct namecache *ncp, *n2;
+ struct namecache_ts *n3;
struct nchashhead *ncpp;
uint32_t hash;
int flag;
@@ -687,13 +777,23 @@ cache_enter(dvp, vp, cnp)
* Calculate the hash key and setup as much of the new
* namecache entry as possible before acquiring the lock.
*/
- ncp = cache_alloc(cnp->cn_namelen);
+ ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
ncp->nc_vp = vp;
ncp->nc_dvp = dvp;
ncp->nc_flag = flag;
+ if (tsp != NULL) {
+ n3 = (struct namecache_ts *)ncp;
+ n3->nc_time = *tsp;
+ n3->nc_ticks = ticks;
+ n3->nc_flag |= NCF_TS;
+ if (dtsp != NULL) {
+ n3->nc_dotdottime = *dtsp;
+ n3->nc_flag |= NCF_DTS;
+ }
+ }
len = ncp->nc_nlen = cnp->cn_namelen;
hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
- strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
+ strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
CACHE_WLOCK();
@@ -706,7 +806,22 @@ cache_enter(dvp, vp, cnp)
LIST_FOREACH(n2, ncpp, nc_hash) {
if (n2->nc_dvp == dvp &&
n2->nc_nlen == cnp->cn_namelen &&
- !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
+ !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
+ if (tsp != NULL) {
+ KASSERT((n2->nc_flag & NCF_TS) != 0,
+ ("no NCF_TS"));
+ n3 = (struct namecache_ts *)n2;
+ n3->nc_time =
+ ((struct namecache_ts *)ncp)->nc_time;
+ n3->nc_ticks =
+ ((struct namecache_ts *)ncp)->nc_ticks;
+ if (dtsp != NULL) {
+ n3->nc_dotdottime =
+ ((struct namecache_ts *)ncp)->
+ nc_dotdottime;
+ n3->nc_flag |= NCF_DTS;
+ }
+ }
CACHE_WUNLOCK();
cache_free(ncp);
return;
@@ -735,6 +850,11 @@ cache_enter(dvp, vp, cnp)
ncp->nc_flag |= NCF_WHITE;
} else if (vp->v_type == VDIR) {
if (flag != NCF_ISDOTDOT) {
+ /*
+ * For this case, the cache entry maps both the
+ * directory name in it and the name ".." for the
+ * directory's parent.
+ */
if ((n2 = vp->v_cache_dd) != NULL &&
(n2->nc_flag & NCF_ISDOTDOT) != 0)
cache_zap(n2);
@@ -764,12 +884,12 @@ cache_enter(dvp, vp, cnp)
*/
if (vp) {
TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
- SDT_PROBE(vfs, namecache, enter, done, dvp, ncp->nc_name, vp,
- 0, 0);
+ SDT_PROBE(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
+ vp, 0, 0);
} else {
TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
- ncp->nc_name, 0, 0, 0);
+ nc_get_name(ncp), 0, 0, 0);
}
if (numneg * ncnegfactor > numcache) {
ncp = TAILQ_FIRST(&ncneg);
@@ -791,10 +911,18 @@ nchinit(void *dummy __unused)
TAILQ_INIT(&ncneg);
- cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL,
- NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
- cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL,
- NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
+ cache_zone_small = uma_zcreate("S VFS Cache",
+ sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
+ NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
+ cache_zone_small_ts = uma_zcreate("STS VFS Cache",
+ sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
+ NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
+ cache_zone_large = uma_zcreate("L VFS Cache",
+ sizeof(struct namecache) + NAME_MAX + 1,
+ NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
+ cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
+ sizeof(struct namecache_ts) + NAME_MAX + 1,
+ NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
}
@@ -1098,9 +1226,9 @@ vn_vptocnp_locked(struct vnode **vp, str
return (error);
}
*buflen -= ncp->nc_nlen;
- memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
+ memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
- ncp->nc_name, vp, 0, 0);
+ nc_get_name(ncp), vp, 0, 0);
dvp = *vp;
*vp = ncp->nc_dvp;
vref(*vp);
@@ -1273,12 +1401,35 @@ vn_commname(struct vnode *vp, char *buf,
return (ENOENT);
}
l = min(ncp->nc_nlen, buflen - 1);
- memcpy(buf, ncp->nc_name, l);
+ memcpy(buf, nc_get_name(ncp), l);
CACHE_RUNLOCK();
buf[l] = '\0';
return (0);
}
+/* ABI compat shims for old kernel modules. */
+#undef cache_enter
+#undef cache_lookup
+
+void cache_enter(struct vnode *dvp, struct vnode *vp,
+ struct componentname *cnp);
+int cache_lookup(struct vnode *dvp, struct vnode **vpp,
+ struct componentname *cnp);
+
+void
+cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
+{
+
+ cache_enter_time(dvp, vp, cnp, NULL, NULL);
+}
+
+int
+cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
+{
+
+ return (cache_lookup_times(dvp, vpp, cnp, NULL, NULL));
+}
+
/*
* This function updates path string to vnode's full global path
* and checks the size of the new path string against the pathlen argument.
Modified: stable/9/sys/nfsclient/nfs_subs.c
==============================================================================
--- stable/9/sys/nfsclient/nfs_subs.c Wed Mar 21 19:09:52 2012 (r233284)
+++ stable/9/sys/nfsclient/nfs_subs.c Wed Mar 21 20:50:15 2012 (r233285)
@@ -978,8 +978,8 @@ nfsm_loadattr_xx(struct vnode **v, struc
}
int
-nfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
- caddr_t *dpos)
+nfsm_postop_attr_xx(struct vnode **v, int *f, struct vattr *va,
+ struct mbuf **md, caddr_t *dpos)
{
u_int32_t *tl;
int t1;
@@ -990,7 +990,7 @@ nfsm_postop_attr_xx(struct vnode **v, in
return EBADRPC;
*f = fxdr_unsigned(int, *tl);
if (*f != 0) {
- t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
+ t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 1);
if (t1 != 0) {
*f = 0;
return t1;
@@ -1020,7 +1020,7 @@ nfsm_wcc_data_xx(struct vnode **v, int *
VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
mtx_unlock(&(VTONFS(*v))->n_mtx);
}
- t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
+ t1 = nfsm_postop_attr_xx(v, &ttattrf, NULL, md, dpos);
if (t1)
return t1;
if (*f)
Modified: stable/9/sys/nfsclient/nfs_vnops.c
==============================================================================
--- stable/9/sys/nfsclient/nfs_vnops.c Wed Mar 21 19:09:52 2012 (r233284)
+++ stable/9/sys/nfsclient/nfs_vnops.c Wed Mar 21 20:50:15 2012 (r233285)
@@ -912,8 +912,8 @@ nfs_lookup(struct vop_lookup_args *ap)
struct vnode *dvp = ap->a_dvp;
struct vnode **vpp = ap->a_vpp;
struct mount *mp = dvp->v_mount;
- struct vattr vattr;
- struct timespec dmtime;
+ struct vattr dvattr, vattr;
+ struct timespec nctime;
int flags = cnp->cn_flags;
struct vnode *newvp;
struct nfsmount *nmp;
@@ -922,7 +922,7 @@ nfs_lookup(struct vop_lookup_args *ap)
long len;
nfsfh_t *fhp;
struct nfsnode *np, *newnp;
- int error = 0, attrflag, fhsize, ltype;
+ int error = 0, attrflag, dattrflag, fhsize, ltype, ncticks;
int v3 = NFS_ISV3(dvp);
struct thread *td = cnp->cn_thread;
@@ -938,11 +938,24 @@ nfs_lookup(struct vop_lookup_args *ap)
*vpp = NULLVP;
return (error);
}
- error = cache_lookup(dvp, vpp, cnp);
+ error = cache_lookup_times(dvp, vpp, cnp, &nctime, &ncticks);
if (error > 0 && error != ENOENT)
return (error);
if (error == -1) {
/*
+ * Lookups of "." are special and always return the
+ * current directory. cache_lookup() already handles
+ * associated locking bookkeeping, etc.
+ */
+ if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
+ /* XXX: Is this really correct? */
+ if (cnp->cn_nameiop != LOOKUP &&
+ (flags & ISLASTCN))
+ cnp->cn_flags |= SAVENAME;
+ return (0);
+ }
+
+ /*
* We only accept a positive hit in the cache if the
* change time of the file matches our cached copy.
* Otherwise, we discard the cache entry and fallback
@@ -968,7 +981,7 @@ nfs_lookup(struct vop_lookup_args *ap)
mtx_unlock(&newnp->n_mtx);
}
if (VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
- timespeccmp(&vattr.va_ctime, &newnp->n_ctime, ==)) {
+ timespeccmp(&vattr.va_ctime, &nctime, ==)) {
nfsstats.lookupcache_hits++;
if (cnp->cn_nameiop != LOOKUP &&
(flags & ISLASTCN))
@@ -987,36 +1000,22 @@ nfs_lookup(struct vop_lookup_args *ap)
/*
* We only accept a negative hit in the cache if the
* modification time of the parent directory matches
- * our cached copy. Otherwise, we discard all of the
- * negative cache entries for this directory. We also
- * only trust -ve cache entries for less than
- * nm_negative_namecache_timeout seconds.
+ * the cached copy in the name cache entry.
+ * Otherwise, we discard all of the negative cache
+ * entries for this directory. We also only trust
+ * negative cache entries for up to nm_negnametimeo
+ * seconds.
*/
- if ((u_int)(ticks - np->n_dmtime_ticks) <
- (nmp->nm_negnametimeo * hz) &&
+ if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
- timespeccmp(&vattr.va_mtime, &np->n_dmtime, ==)) {
+ timespeccmp(&vattr.va_mtime, &nctime, ==)) {
nfsstats.lookupcache_hits++;
return (ENOENT);
}
cache_purge_negative(dvp);
- mtx_lock(&np->n_mtx);
- timespecclear(&np->n_dmtime);
- mtx_unlock(&np->n_mtx);
}
- /*
- * Cache the modification time of the parent directory in case
- * the lookup fails and results in adding the first negative
- * name cache entry for the directory. Since this is reading
- * a single time_t, don't bother with locking. The
- * modification time may be a bit stale, but it must be read
- * before performing the lookup RPC to prevent a race where
- * another lookup updates the timestamp on the directory after
- * the lookup RPC has been performed on the server but before
- * n_dmtime is set at the end of this function.
- */
- dmtime = np->n_vattr.va_mtime;
+ attrflag = dattrflag = 0;
error = 0;
newvp = NULLVP;
nfsstats.lookupcache_misses++;
@@ -1031,7 +1030,7 @@ nfs_lookup(struct vop_lookup_args *ap)
nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
if (error) {
if (v3) {
- nfsm_postop_attr(dvp, attrflag);
+ nfsm_postop_attr_va(dvp, dattrflag, &vattr);
m_freem(mrep);
}
goto nfsmout;
@@ -1127,17 +1126,19 @@ nfs_lookup(struct vop_lookup_args *ap)
}
}
if (v3) {
- nfsm_postop_attr(newvp, attrflag);
- nfsm_postop_attr(dvp, attrflag);
- } else
- nfsm_loadattr(newvp, NULL);
+ nfsm_postop_attr_va(newvp, attrflag, &vattr);
+ nfsm_postop_attr_va(dvp, dattrflag, &dvattr);
+ } else {
+ nfsm_loadattr(newvp, &vattr);
+ attrflag = 1;
+ }
if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
cnp->cn_flags |= SAVENAME;
if ((cnp->cn_flags & MAKEENTRY) &&
- (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
- np->n_ctime = np->n_vattr.va_ctime;
- cache_enter(dvp, newvp, cnp);
- }
+ (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
+ attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
+ cache_enter_time(dvp, newvp, cnp, &vattr.va_ctime,
+ newvp->v_type != VDIR ? NULL : &dvattr.va_ctime);
*vpp = newvp;
m_freem(mrep);
nfsmout:
@@ -1164,30 +1165,22 @@ nfsmout:
return (EJUSTRETURN);
}
- if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) {
+ if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
+ dattrflag) {
/*
- * Maintain n_dmtime as the modification time
- * of the parent directory when the oldest -ve
- * name cache entry for this directory was
- * added. If a -ve cache entry has already
- * been added with a newer modification time
- * by a concurrent lookup, then don't bother
- * adding a cache entry. The modification
- * time of the directory might have changed
- * due to the file this lookup failed to find
- * being created. In that case a subsequent
- * lookup would incorrectly use the entry
- * added here instead of doing an extra
- * lookup.
+ * Cache the modification time of the parent
+ * directory from the post-op attributes in
+ * the name cache entry. The negative cache
+ * entry will be ignored once the directory
+ * has changed. Don't bother adding the entry
+ * if the directory has already changed.
*/
mtx_lock(&np->n_mtx);
- if (timespeccmp(&np->n_dmtime, &dmtime, <=)) {
- if (!timespecisset(&np->n_dmtime)) {
- np->n_dmtime = dmtime;
- np->n_dmtime_ticks = ticks;
- }
+ if (timespeccmp(&np->n_vattr.va_mtime,
+ &vattr.va_mtime, ==)) {
mtx_unlock(&np->n_mtx);
- cache_enter(dvp, NULL, cnp);
+ cache_enter_time(dvp, NULL, cnp,
+ &vattr.va_mtime, NULL);
} else
mtx_unlock(&np->n_mtx);
}
@@ -1538,8 +1531,6 @@ nfsmout:
if (newvp)
vput(newvp);
} else {
- if (cnp->cn_flags & MAKEENTRY)
- cache_enter(dvp, newvp, cnp);
*vpp = newvp;
}
mtx_lock(&(VTONFS(dvp))->n_mtx);
@@ -1678,8 +1669,6 @@ nfsmout:
vput(newvp);
}
if (!error) {
- if (cnp->cn_flags & MAKEENTRY)
- cache_enter(dvp, newvp, cnp);
*ap->a_vpp = newvp;
}
mtx_lock(&(VTONFS(dvp))->n_mtx);
@@ -2473,10 +2462,11 @@ nfs_readdirplusrpc(struct vnode *vp, str
nfsuint64 cookie;
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
struct nfsnode *dnp = VTONFS(vp), *np;
+ struct vattr vattr, dvattr;
nfsfh_t *fhp;
u_quad_t fileno;
int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
- int attrflag, fhsize;
+ int attrflag, dattrflag, fhsize;
#ifndef nolint
dp = NULL;
@@ -2522,7 +2512,7 @@ nfs_readdirplusrpc(struct vnode *vp, str
*tl++ = txdr_unsigned(nmp->nm_readdirsize);
*tl = txdr_unsigned(nmp->nm_rsize);
nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
- nfsm_postop_attr(vp, attrflag);
+ nfsm_postop_attr_va(vp, dattrflag, &dvattr);
if (error) {
m_freem(mrep);
goto nfsmout;
@@ -2653,18 +2643,16 @@ nfs_readdirplusrpc(struct vnode *vp, str
dpos = dpossav1;
mdsav2 = md;
md = mdsav1;
- nfsm_loadattr(newvp, NULL);
+ nfsm_loadattr(newvp, &vattr);
dpos = dpossav2;
md = mdsav2;
- dp->d_type =
- IFTODT(VTTOIF(np->n_vattr.va_type));
+ dp->d_type = IFTODT(VTTOIF(vattr.va_type));
ndp->ni_vp = newvp;
- /*
- * Update n_ctime so subsequent lookup
- * doesn't purge entry.
- */
- np->n_ctime = np->n_vattr.va_ctime;
- cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
+ if (newvp->v_type != VDIR || dattrflag != 0)
+ cache_enter_time(ndp->ni_dvp, ndp->ni_vp,
+ cnp, &vattr.va_ctime,
+ newvp->v_type != VDIR ? NULL :
+ &dvattr.va_ctime);
}
} else {
/* Just skip over the file handle */
Modified: stable/9/sys/nfsclient/nfsm_subs.h
==============================================================================
--- stable/9/sys/nfsclient/nfsm_subs.h Wed Mar 21 19:09:52 2012 (r233284)
+++ stable/9/sys/nfsclient/nfsm_subs.h Wed Mar 21 20:50:15 2012 (r233285)
@@ -152,8 +152,8 @@ int nfsm_getfh_xx(nfsfh_t **f, int *s, i
caddr_t *dpos);
int nfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
caddr_t *dpos);
-int nfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
- caddr_t *dpos);
+int nfsm_postop_attr_xx(struct vnode **v, int *f, struct vattr *va,
+ struct mbuf **md, caddr_t *dpos);
int nfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md,
caddr_t *dpos);
@@ -181,7 +181,14 @@ do { \
#define nfsm_postop_attr(v, f) \
do { \
int32_t t1; \
- t1 = nfsm_postop_attr_xx(&v, &f, &md, &dpos); \
+ t1 = nfsm_postop_attr_xx(&v, &f, NULL, &md, &dpos); \
+ nfsm_dcheck(t1, mrep); \
+} while (0)
+
*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
More information about the svn-src-stable-9
mailing list