More gracefully handle stale file handles and attributes when opening a

file via NFS.  Specifically, to satisfy close-to-open-consistency, the NFS
client always performs at least one RPC on a file during an open(2) to see
if the file has changed.  Normally this RPC is an ACCESS or GETATTR RPC
that is forced by flushing a file's attribute cache during nfs_open() and
then requesting new attributes.  However, if the file is noticed to be
stale during nfs_open(), the only recourse is to fail the open(2) call
with ESTALE.  On the other hand, if the ACCESS or GETATTR RPC is sent
during nfs_lookup(), then the NFS client can fall back to a LOOKUP RPC to
obtain the new file handle in the case that a file has been replaced.

This change causes the NFS client to flush the attribute cache during
nfs_lookup() when validating a name cache hit if the attributes fetched
during nfs_lookup() can be reused in nfs_open().  This allows the client
to open a replaced file via the new file handle the first time that it
notices a replaced file rather than failing with ESTALE in some cases.

Reviewed by:	rmacklem, bde
Reviewed by:	mohans (older version)
MFC after:	1 week
This commit is contained in:
John Baldwin 2010-05-27 18:07:20 +00:00
parent 1e4bf1e35f
commit b367632ec2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=208603

View File

@ -932,7 +932,7 @@ nfs_lookup(struct vop_lookup_args *ap)
struct mbuf *mreq, *mrep, *md, *mb;
long len;
nfsfh_t *fhp;
struct nfsnode *np;
struct nfsnode *np, *newnp;
int error = 0, attrflag, fhsize, ltype;
int v3 = NFS_ISV3(dvp);
struct thread *td = cnp->cn_thread;
@ -958,10 +958,27 @@ nfs_lookup(struct vop_lookup_args *ap)
* change time of the file matches our cached copy.
* Otherwise, we discard the cache entry and fallback
* to doing a lookup RPC.
*
* To better handle stale file handles and attributes,
* clear the attribute cache of this node if it is a
* leaf component, part of an open() call, and not
* locally modified before fetching the attributes.
* This should allow stale file handles to be detected
* here where we can fall back to a LOOKUP RPC to
* recover rather than having nfs_open() detect the
* stale file handle and failing open(2) with ESTALE.
*/
newvp = *vpp;
if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred)
&& vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
newnp = VTONFS(newvp);
if ((cnp->cn_flags & (ISLASTCN | ISOPEN)) ==
(ISLASTCN | ISOPEN) && !(newnp->n_flag & NMODIFIED)) {
mtx_lock(&newnp->n_mtx);
newnp->n_attrstamp = 0;
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
mtx_unlock(&newnp->n_mtx);
}
if (VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
vattr.va_ctime.tv_sec == newnp->n_ctime) {
nfsstats.lookupcache_hits++;
if (cnp->cn_nameiop != LOOKUP &&
(flags & ISLASTCN))