Skip to contentSkip to conversation

cifs: cache invalidation fixes for cifs with cache=loose

PATCH In review Jeff Layton 2 participants 10 messages Mar 31, 2014 – Apr 25, 2014 7 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover cifs: cache invalidation fixes for cifs with cache=loose

This patchset fixes up some cache invalidation problems that can occur when cache=loose. The main point is to ensure that we revalidate the cache prior to satisfying a read.

At the same time, I added a fix for a potential problem that cropped up with NFS a while back, and to which CIFS is also vulnerable.

This was originally reported by Tetsuo Handa, and as far as I can tell this fixes his testcase, assuming that you also ensure that kernel oplocks are enabled.

Jeff Layton (4):
cifs: convert booleans in cifsInodeInfo to a flags field
cifs: new helper function: cifs_revalidate_mapping
cifs: fix potential races in cifs_revalidate_mapping
cifs: revalidate mapping prior to satisfying aio_read request

 fs/cifs/cifsfs.c   | 21 +++++++++++++---
 fs/cifs/cifsfs.h   |  2 ++
 fs/cifs/cifsglob.h |  6 +++--
 fs/cifs/file.c     | 16 ++++++------
 fs/cifs/inode.c    | 71 ++++++++++++++++++++++++++++++++++++++++++------------
 5 files changed, 86 insertions(+), 30 deletions(-)
Signature · 1 line
1.9.0

A message that is not in the archive (sent off-list or not yet received): 1396288209-27328-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

#2 lore raw

PATCH 1/4 cifs: convert booleans in cifsInodeInfo to a flags field

In later patches, we'll need to have a bitlock, so go ahead and convert these bools to use atomic bitops instead.

---
 fs/cifs/cifsfs.c   |  3 +--
 fs/cifs/cifsglob.h |  5 +++--
 fs/cifs/file.c     |  4 ++--
 fs/cifs/inode.c    | 19 +++++++++++--------
 4 files changed, 17 insertions(+), 14 deletions(-)
fs/cifs/cifsfs.c +1 -2
index 849f6132b327..8a7faf7bd9ee 100644
@@ -251,8 +251,7 @@ cifs_alloc_inode(struct super_block *sb)
 	 * server, can not assume caching of file data or metadata.
 	 */
 	cifs_set_oplock_level(cifs_inode, 0);
-	cifs_inode->delete_pending = false;
-	cifs_inode->invalid_mapping = false;
+	cifs_inode->flags = 0;
 	cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
 	cifs_inode->server_eof = 0;
 	cifs_inode->uniqueid = 0;
fs/cifs/cifsglob.h +3 -2
index c0f3718b77a8..0d4a1039ac27 100644
@@ -1111,8 +1111,9 @@ struct cifsInodeInfo {
 	__u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */
 	unsigned int oplock;		/* oplock/lease level we have */
 	unsigned int epoch;		/* used to track lease state changes */
-	bool delete_pending;		/* DELETE_ON_CLOSE is set */
-	bool invalid_mapping;		/* pagecache is invalid */
+#define CIFS_INO_DELETE_PENDING		(1)
+#define CIFS_INO_INVALID_MAPPING	(2)
+	unsigned long flags;		/* flags field */
 	unsigned long time;		/* jiffies of last update of inode */
 	u64  server_eof;		/* current file size on server -- protected by i_lock */
 	u64  uniqueid;			/* server inode number */
fs/cifs/file.c +2 -2
index 834fce759d80..784b17d510fc 100644
@@ -392,7 +392,7 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
 		 * again and get at least level II oplock.
 		 */
 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
-			CIFS_I(inode)->invalid_mapping = true;
+			set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
 		cifs_set_oplock_level(cifsi, 0);
 	}
 	spin_unlock(&cifs_file_list_lock);
@@ -2562,7 +2562,7 @@ ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov,
 
 	written = cifs_iovec_write(iocb->ki_filp, iov, nr_segs, &pos);
 	if (written > 0) {
-		CIFS_I(inode)->invalid_mapping = true;
+		set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
 		iocb->ki_pos = pos;
 	}
 
fs/cifs/inode.c +11 -8
index a22d667f1069..3e9d8ba21f5c 100644
@@ -117,7 +117,7 @@ cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
 
 	cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
 		 __func__, cifs_i->uniqueid);
-	cifs_i->invalid_mapping = true;
+	set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 }
 
 /*
@@ -177,7 +177,10 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 	else
 		cifs_i->time = jiffies;
 
-	cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING;
+	if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
+		set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
+	else
+		clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
 
 	cifs_i->server_eof = fattr->cf_eof;
 	/*
@@ -1121,7 +1124,7 @@ cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
 	}
 
 	/* try to set DELETE_ON_CLOSE */
-	if (!cifsInode->delete_pending) {
+	if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
 		rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
 					       current->tgid);
 		/*
@@ -1138,7 +1141,7 @@ cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
 			rc = -EBUSY;
 			goto undo_rename;
 		}
-		cifsInode->delete_pending = true;
+		set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
 	}
 
 out_close:
@@ -1761,14 +1764,14 @@ cifs_invalidate_mapping(struct inode *inode)
 	int rc = 0;
 	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
 
-	cifs_i->invalid_mapping = false;
+	clear_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 
 	if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
 		rc = invalidate_inode_pages2(inode->i_mapping);
 		if (rc) {
 			cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
 				 __func__, inode);
-			cifs_i->invalid_mapping = true;
+			set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 		}
 	}
 
@@ -1842,7 +1845,7 @@ int cifs_revalidate_file(struct file *filp)
 	if (rc)
 		return rc;
 
-	if (CIFS_I(inode)->invalid_mapping)
+	if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
 		rc = cifs_invalidate_mapping(inode);
 	return rc;
 }
@@ -1857,7 +1860,7 @@ int cifs_revalidate_dentry(struct dentry *dentry)
 	if (rc)
 		return rc;
 
-	if (CIFS_I(inode)->invalid_mapping)
+	if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
 		rc = cifs_invalidate_mapping(inode);
 	return rc;
 }
Signature · 1 line
1.9.0

A message that is not in the archive (sent off-list or not yet received): 1396288209-27328-2-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

#4 lore raw

Re: [PATCH 1/4] cifs: convert booleans in cifsInodeInfo to a flags field

Looks like you added two spurious semicolons in the middle of if statements?

@@ -1842,7 +1845,7 @@ int cifs_revalidate_file(struct file *filp)
if (rc)
return rc;

- if (CIFS_I(inode)->invalid_mapping)
+ if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags)); rc = cifs_invalidate_mapping(inode);
return rc;
}
@@ -1857,7 +1860,7 @@ int cifs_revalidate_dentry(struct dentry *dentry)
if (rc)
return rc;

- if (CIFS_I(inode)->invalid_mapping)
+ if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags)); rc = cifs_invalidate_mapping(inode);
return rc;
}

Quoted 144 lines · Jeff Layton

In later patches, we'll need to have a bitlock, so go ahead and convert these bools to use atomic bitops instead.

---
 fs/cifs/cifsfs.c   |  3 +--
 fs/cifs/cifsglob.h |  5 +++--
 fs/cifs/file.c     |  4 ++--
 fs/cifs/inode.c    | 19 +++++++++++--------
 4 files changed, 17 insertions(+), 14 deletions(-)
fs/cifs/cifsfs.c +1 -2
index 849f6132b327..8a7faf7bd9ee 100644
@@ -251,8 +251,7 @@ cifs_alloc_inode(struct super_block *sb)
         * server, can not assume caching of file data or metadata.
         */
        cifs_set_oplock_level(cifs_inode, 0);
-       cifs_inode->delete_pending = false;
-       cifs_inode->invalid_mapping = false;
+       cifs_inode->flags = 0;
        cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
        cifs_inode->server_eof = 0;
        cifs_inode->uniqueid = 0;
fs/cifs/cifsglob.h +3 -2
index c0f3718b77a8..0d4a1039ac27 100644
@@ -1111,8 +1111,9 @@ struct cifsInodeInfo {
        __u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */
        unsigned int oplock;            /* oplock/lease level we have */
        unsigned int epoch;             /* used to track lease state changes */
-       bool delete_pending;            /* DELETE_ON_CLOSE is set */
-       bool invalid_mapping;           /* pagecache is invalid */
+#define CIFS_INO_DELETE_PENDING                (1)
+#define CIFS_INO_INVALID_MAPPING       (2)
+       unsigned long flags;            /* flags field */
        unsigned long time;             /* jiffies of last update of inode */
        u64  server_eof;                /* current file size on server -- protected by i_lock */
        u64  uniqueid;                  /* server inode number */
fs/cifs/file.c +2 -2
index 834fce759d80..784b17d510fc 100644
@@ -392,7 +392,7 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
                 * again and get at least level II oplock.
                 */
                if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
-                       CIFS_I(inode)->invalid_mapping = true;
+                       set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
                cifs_set_oplock_level(cifsi, 0);
        }
        spin_unlock(&cifs_file_list_lock);
@@ -2562,7 +2562,7 @@ ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov,
 
        written = cifs_iovec_write(iocb->ki_filp, iov, nr_segs, &pos);
        if (written > 0) {
-               CIFS_I(inode)->invalid_mapping = true;
+               set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
                iocb->ki_pos = pos;
        }
 
fs/cifs/inode.c +11 -9
index a22d667f1069..3e9d8ba21f5c 100644
@@ -117,7 +117,7 @@ cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
 
        cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
                 __func__, cifs_i->uniqueid);
-       cifs_i->invalid_mapping = true;
+       set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 }
 
 /*
@@ -177,7 +177,10 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
        else
                cifs_i->time = jiffies;
 
-       cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING;
+       if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
+               set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
+       else
+               clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
 
        cifs_i->server_eof = fattr->cf_eof;
        /*
@@ -1121,7 +1124,7 @@ cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
        }
 
        /* try to set DELETE_ON_CLOSE */
-       if (!cifsInode->delete_pending) {
+       if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
                rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
                                               current->tgid);
                /*
@@ -1138,7 +1141,7 @@ cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
                        rc = -EBUSY;
                        goto undo_rename;
                }
-               cifsInode->delete_pending = true;
+               set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
        }
 
 out_close:
@@ -1761,14 +1764,14 @@ cifs_invalidate_mapping(struct inode *inode)
        int rc = 0;
        struct cifsInodeInfo *cifs_i = CIFS_I(inode);
 
-       cifs_i->invalid_mapping = false;
+       clear_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 
        if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
                rc = invalidate_inode_pages2(inode->i_mapping);
                if (rc) {
                        cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
                                 __func__, inode);
-                       cifs_i->invalid_mapping = true;
+                       set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
                }
        }
 
@@ -1842,7 +1845,7 @@ int cifs_revalidate_file(struct file *filp)
        if (rc)
                return rc;
 
-       if (CIFS_I(inode)->invalid_mapping)
+       if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
                rc = cifs_invalidate_mapping(inode);
        return rc;
 }
@@ -1857,7 +1860,7 @@ int cifs_revalidate_dentry(struct dentry *dentry)
        if (rc)
                return rc;
 
-       if (CIFS_I(inode)->invalid_mapping)
+       if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
                rc = cifs_invalidate_mapping(inode);
        return rc;
 }
--

1.9.0

Signature · 3 lines
Thanks,

Steve
#5 lore raw

PATCH 2/4 cifs: new helper function: cifs_revalidate_mapping

In reply to a message not in the archive

Consolidate a bit of code. In a later patch we'll expand this to fix some races.

---
 fs/cifs/cifsfs.h |  1 +
 fs/cifs/inode.c  | 16 ++++++++++------
 2 files changed, 11 insertions(+), 6 deletions(-)
fs/cifs/cifsfs.h +1 -0
index 26a754f49ba1..40d5a4df1e38 100644
@@ -67,6 +67,7 @@ extern int cifs_revalidate_dentry_attr(struct dentry *);
 extern int cifs_revalidate_file(struct file *filp);
 extern int cifs_revalidate_dentry(struct dentry *);
 extern int cifs_invalidate_mapping(struct inode *inode);
+extern int cifs_revalidate_mapping(struct inode *inode);
 extern int cifs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
 extern int cifs_setattr(struct dentry *, struct iattr *);
 
fs/cifs/inode.c +10 -6
index 3e9d8ba21f5c..39b76bac196d 100644
@@ -1779,6 +1779,14 @@ cifs_invalidate_mapping(struct inode *inode)
 	return rc;
 }
 
+int
+cifs_revalidate_mapping(struct inode *inode)
+{
+	if (test_bit(CIFS_INODE_INVALID_MAPPING, &CIFS_I(inode)->flags));
+		return cifs_invalidate_mapping(inode);
+	return 0;
+}
+
 int cifs_revalidate_file_attr(struct file *filp)
 {
 	int rc = 0;
@@ -1845,9 +1853,7 @@ int cifs_revalidate_file(struct file *filp)
 	if (rc)
 		return rc;
 
-	if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
-		rc = cifs_invalidate_mapping(inode);
-	return rc;
+	return cifs_revalidate_mapping(inode);
 }
 
 /* revalidate a dentry's inode attributes */
@@ -1860,9 +1866,7 @@ int cifs_revalidate_dentry(struct dentry *dentry)
 	if (rc)
 		return rc;
 
-	if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
-		rc = cifs_invalidate_mapping(inode);
-	return rc;
+	return cifs_revalidate_mapping(inode);
 }
 
 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
Signature · 1 line
1.9.0

A message that is not in the archive (sent off-list or not yet received): 1396288209-27328-3-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

#7 lore raw

Re: [PATCH 2/4] cifs: new helper function: cifs_revalidate_mapping

The extra semicolon in the following looks wrong

Quoted 65 lines · Jeff Layton

Consolidate a bit of code. In a later patch we'll expand this to fix some races.

---
 fs/cifs/cifsfs.h |  1 +
 fs/cifs/inode.c  | 16 ++++++++++------
 2 files changed, 11 insertions(+), 6 deletions(-)
fs/cifs/cifsfs.h +1 -0
index 26a754f49ba1..40d5a4df1e38 100644
@@ -67,6 +67,7 @@ extern int cifs_revalidate_dentry_attr(struct dentry *);
 extern int cifs_revalidate_file(struct file *filp);
 extern int cifs_revalidate_dentry(struct dentry *);
 extern int cifs_invalidate_mapping(struct inode *inode);
+extern int cifs_revalidate_mapping(struct inode *inode);
 extern int cifs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
 extern int cifs_setattr(struct dentry *, struct iattr *);
 
fs/cifs/inode.c +10 -7
index 3e9d8ba21f5c..39b76bac196d 100644
@@ -1779,6 +1779,14 @@ cifs_invalidate_mapping(struct inode *inode)
        return rc;
 }
 
+int
+cifs_revalidate_mapping(struct inode *inode)
+{
+       if (test_bit(CIFS_INODE_INVALID_MAPPING, &CIFS_I(inode)->flags));
+               return cifs_invalidate_mapping(inode);
+       return 0;
+}
+
 int cifs_revalidate_file_attr(struct file *filp)
 {
        int rc = 0;
@@ -1845,9 +1853,7 @@ int cifs_revalidate_file(struct file *filp)
        if (rc)
                return rc;
 
-       if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
-               rc = cifs_invalidate_mapping(inode);
-       return rc;
+       return cifs_revalidate_mapping(inode);
 }
 
 /* revalidate a dentry's inode attributes */
@@ -1860,9 +1866,7 @@ int cifs_revalidate_dentry(struct dentry *dentry)
        if (rc)
                return rc;
 
-       if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
-               rc = cifs_invalidate_mapping(inode);
-       return rc;
+       return cifs_revalidate_mapping(inode);
 }
 
 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
--

1.9.0

Signature · 3 lines
Thanks,

Steve

A message that is not in the archive (sent off-list or not yet received): CAH2r5msAfJgSEe-2FFJPCTXn7_YQdURP64w0oSF+yWqb+3y4Mg@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAH2r5msAfJgSEe-2FFJPCTXn7_YQdURP64w0oSF+yWqb+3y4Mg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#10 lore raw

Re: [PATCH 2/4] cifs: new helper function: cifs_revalidate_mapping

fs/cifs/inode.c cifs_invalidate_mapping(struct inode *inode) · line 1786

Quoted 6 lines · On Thu, 24 Apr 2014 22:45:46 -0500 Steve French…

The extra semicolon in the following looks wrong

fs/cifs/inode.c +2 -0
+       if (test_bit(CIFS_INODE_INVALID_MAPPING, &CIFS_I(inode)->flags));
+               return cifs_invalidate_mapping(inode);

Oh my, yes. Good catch. I'll fix and resend -- might be a few days before I can get to it though.

Quoted 71 lines · Jeff Layton
Quoted 68 lines

Consolidate a bit of code. In a later patch we'll expand this to fix some races.

---
 fs/cifs/cifsfs.h |  1 +
 fs/cifs/inode.c  | 16 ++++++++++------
 2 files changed, 11 insertions(+), 6 deletions(-)
fs/cifs/cifsfs.h +1 -0
index 26a754f49ba1..40d5a4df1e38 100644
@@ -67,6 +67,7 @@ extern int cifs_revalidate_dentry_attr(struct dentry *);
 extern int cifs_revalidate_file(struct file *filp);
 extern int cifs_revalidate_dentry(struct dentry *);
 extern int cifs_invalidate_mapping(struct inode *inode);
+extern int cifs_revalidate_mapping(struct inode *inode);
 extern int cifs_getattr(struct vfsmount *, struct dentry *, struct kstat
 *);
 extern int cifs_setattr(struct dentry *, struct iattr *);
fs/cifs/inode.c +10 -7
index 3e9d8ba21f5c..39b76bac196d 100644
@@ -1779,6 +1779,14 @@ cifs_invalidate_mapping(struct inode *inode)
        return rc;
 }
 
+int
+cifs_revalidate_mapping(struct inode *inode)
+{
+       if (test_bit(CIFS_INODE_INVALID_MAPPING, &CIFS_I(inode)->flags));
+               return cifs_invalidate_mapping(inode);
+       return 0;
+}
+
 int cifs_revalidate_file_attr(struct file *filp)
 {
        int rc = 0;
@@ -1845,9 +1853,7 @@ int cifs_revalidate_file(struct file *filp)
        if (rc)
                return rc;
 
-       if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
-               rc = cifs_invalidate_mapping(inode);
-       return rc;
+       return cifs_revalidate_mapping(inode);
 }
 
 /* revalidate a dentry's inode attributes */
@@ -1860,9 +1866,7 @@ int cifs_revalidate_dentry(struct dentry *dentry)
        if (rc)
                return rc;
 
-       if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags));
-               rc = cifs_invalidate_mapping(inode);
-       return rc;
+       return cifs_revalidate_mapping(inode);
 }
 
 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
--

1.9.0

Signature · 1 line
Jeff Layton <[email protected]>
#11 lore raw

PATCH 3/4 cifs: fix potential races in cifs_revalidate_mapping

In reply to a message not in the archive

The handling of the CIFS_INO_INVALID_MAPPING flag is racy. It's possible for two tasks to attempt to revalidate the mapping at the same time. The first sees that CIFS_INO_INVALID_MAPPING is set. It clears the flag and then calls invalidate_inode_pages2 to start shooting down the pagecache.

While that's going on, another task checks the flag and sees that it's clear. It then ends up trusting the pagecache to satisfy a read when it shouldn't.

Fix this by adding a bitlock to ensure that the clearing of the flag is atomic with respect to the actual cache invalidation. Also, move the other existing users of cifs_invalidate_mapping to use a new cifs_zap_mapping() function that just sets the INVALID_MAPPING bit and then uses the standard codepath to handle the invalidation.

---
 fs/cifs/cifsfs.h   |  1 +
 fs/cifs/cifsglob.h |  1 +
 fs/cifs/file.c     | 12 ++++++------
 fs/cifs/inode.c    | 50 +++++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 49 insertions(+), 15 deletions(-)
fs/cifs/cifsfs.h +1 -0
index 40d5a4df1e38..b7cf9ee7654b 100644
@@ -68,6 +68,7 @@ extern int cifs_revalidate_file(struct file *filp);
 extern int cifs_revalidate_dentry(struct dentry *);
 extern int cifs_invalidate_mapping(struct inode *inode);
 extern int cifs_revalidate_mapping(struct inode *inode);
+extern int cifs_zap_mapping(struct inode *inode);
 extern int cifs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
 extern int cifs_setattr(struct dentry *, struct iattr *);
 
fs/cifs/cifsglob.h +1 -0
index 0d4a1039ac27..84cd4447cc31 100644
@@ -1111,6 +1111,7 @@ struct cifsInodeInfo {
 	__u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */
 	unsigned int oplock;		/* oplock/lease level we have */
 	unsigned int epoch;		/* used to track lease state changes */
+#define CIFS_INO_LOCK			(0)
 #define CIFS_INO_DELETE_PENDING		(1)
 #define CIFS_INO_INVALID_MAPPING	(2)
 	unsigned long flags;		/* flags field */
fs/cifs/file.c +6 -6
index 784b17d510fc..ccca61803f0a 100644
@@ -335,7 +335,7 @@ cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
 	spin_unlock(&cifs_file_list_lock);
 
 	if (fid->purge_cache)
-		cifs_invalidate_mapping(inode);
+		cifs_zap_mapping(inode);
 
 	file->private_data = cfile;
 	return cfile;
@@ -1529,7 +1529,7 @@ cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
 		 */
 		if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
 					CIFS_CACHE_READ(CIFS_I(inode))) {
-			cifs_invalidate_mapping(inode);
+			cifs_zap_mapping(inode);
 			cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
 				 inode);
 			CIFS_I(inode)->oplock = 0;
@@ -2218,7 +2218,7 @@ int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
 		 file->f_path.dentry->d_name.name, datasync);
 
 	if (!CIFS_CACHE_READ(CIFS_I(inode))) {
-		rc = cifs_invalidate_mapping(inode);
+		rc = cifs_zap_mapping(inode);
 		if (rc) {
 			cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
 			rc = 0; /* don't care about it in fsync */
@@ -2628,7 +2628,7 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
 		 * request comes - break it on the client to prevent reading
 		 * an old data.
 		 */
-		cifs_invalidate_mapping(inode);
+		cifs_zap_mapping(inode);
 		cifs_dbg(FYI, "Set no oplock for inode=%p after a write operation\n",
 			 inode);
 		cinode->oplock = 0;
@@ -3125,7 +3125,7 @@ int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
 	xid = get_xid();
 
 	if (!CIFS_CACHE_READ(CIFS_I(inode))) {
-		rc = cifs_invalidate_mapping(inode);
+		rc = cifs_zap_mapping(inode);
 		if (rc)
 			return rc;
 	}
@@ -3669,7 +3669,7 @@ void cifs_oplock_break(struct work_struct *work)
 		if (!CIFS_CACHE_READ(cinode)) {
 			rc = filemap_fdatawait(inode->i_mapping);
 			mapping_set_error(inode->i_mapping, rc);
-			cifs_invalidate_mapping(inode);
+			cifs_zap_mapping(inode);
 		}
 		cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
 	}
fs/cifs/inode.c +41 -9

76 lines not on this page · raw message

Signature · 1 line
1.9.0

A message that is not in the archive (sent off-list or not yet received): 1396288209-27328-4-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

#13 lore raw

Re: [PATCH 3/4] cifs: fix potential races in cifs_revalidate_mapping

Various minor whitespace errors on this - do you want to respin it or want me to fix it up?

Quoted 190 lines · Jeff Layton

The handling of the CIFS_INO_INVALID_MAPPING flag is racy. It's possible for two tasks to attempt to revalidate the mapping at the same time. The first sees that CIFS_INO_INVALID_MAPPING is set. It clears the flag and then calls invalidate_inode_pages2 to start shooting down the pagecache.

While that's going on, another task checks the flag and sees that it's clear. It then ends up trusting the pagecache to satisfy a read when it shouldn't.

Fix this by adding a bitlock to ensure that the clearing of the flag is atomic with respect to the actual cache invalidation. Also, move the other existing users of cifs_invalidate_mapping to use a new cifs_zap_mapping() function that just sets the INVALID_MAPPING bit and then uses the standard codepath to handle the invalidation.

---
 fs/cifs/cifsfs.h   |  1 +
 fs/cifs/cifsglob.h |  1 +
 fs/cifs/file.c     | 12 ++++++------
 fs/cifs/inode.c    | 50 +++++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 49 insertions(+), 15 deletions(-)
fs/cifs/cifsfs.h +1 -0
index 40d5a4df1e38..b7cf9ee7654b 100644
@@ -68,6 +68,7 @@ extern int cifs_revalidate_file(struct file *filp);
 extern int cifs_revalidate_dentry(struct dentry *);
 extern int cifs_invalidate_mapping(struct inode *inode);
 extern int cifs_revalidate_mapping(struct inode *inode);
+extern int cifs_zap_mapping(struct inode *inode);
 extern int cifs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
 extern int cifs_setattr(struct dentry *, struct iattr *);
 
fs/cifs/cifsglob.h +1 -0
index 0d4a1039ac27..84cd4447cc31 100644
@@ -1111,6 +1111,7 @@ struct cifsInodeInfo {
        __u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */
        unsigned int oplock;            /* oplock/lease level we have */
        unsigned int epoch;             /* used to track lease state changes */
+#define CIFS_INO_LOCK                  (0)
 #define CIFS_INO_DELETE_PENDING                (1)
 #define CIFS_INO_INVALID_MAPPING       (2)
        unsigned long flags;            /* flags field */
fs/cifs/file.c +6 -6
index 784b17d510fc..ccca61803f0a 100644
@@ -335,7 +335,7 @@ cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
        spin_unlock(&cifs_file_list_lock);
 
        if (fid->purge_cache)
-               cifs_invalidate_mapping(inode);
+               cifs_zap_mapping(inode);
 
        file->private_data = cfile;
        return cfile;
@@ -1529,7 +1529,7 @@ cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
                 */
                if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
                                        CIFS_CACHE_READ(CIFS_I(inode))) {
-                       cifs_invalidate_mapping(inode);
+                       cifs_zap_mapping(inode);
                        cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
                                 inode);
                        CIFS_I(inode)->oplock = 0;
@@ -2218,7 +2218,7 @@ int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
                 file->f_path.dentry->d_name.name, datasync);
 
        if (!CIFS_CACHE_READ(CIFS_I(inode))) {
-               rc = cifs_invalidate_mapping(inode);
+               rc = cifs_zap_mapping(inode);
                if (rc) {
                        cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
                        rc = 0; /* don't care about it in fsync */
@@ -2628,7 +2628,7 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
                 * request comes - break it on the client to prevent reading
                 * an old data.
                 */
-               cifs_invalidate_mapping(inode);
+               cifs_zap_mapping(inode);
                cifs_dbg(FYI, "Set no oplock for inode=%p after a write operation\n",
                         inode);
                cinode->oplock = 0;
@@ -3125,7 +3125,7 @@ int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
        xid = get_xid();
 
        if (!CIFS_CACHE_READ(CIFS_I(inode))) {
-               rc = cifs_invalidate_mapping(inode);
+               rc = cifs_zap_mapping(inode);
                if (rc)
                        return rc;
        }
@@ -3669,7 +3669,7 @@ void cifs_oplock_break(struct work_struct *work)
                if (!CIFS_CACHE_READ(cinode)) {
                        rc = filemap_fdatawait(inode->i_mapping);
                        mapping_set_error(inode->i_mapping, rc);
-                       cifs_invalidate_mapping(inode);
+                       cifs_zap_mapping(inode);
                }
                cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
        }
fs/cifs/inode.c +41 -10
index 39b76bac196d..b840930f6271 100644
@@ -22,6 +22,7 @@
 #include <linux/stat.h>
 #include <linux/slab.h>
 #include <linux/pagemap.h>
+#include <linux/freezer.h>
 #include <asm/div64.h>
 #include "cifsfs.h"
 #include "cifspdu.h"
@@ -1762,29 +1763,60 @@ int
 cifs_invalidate_mapping(struct inode *inode)
 {
        int rc = 0;
-       struct cifsInodeInfo *cifs_i = CIFS_I(inode);
-
-       clear_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 
        if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
                rc = invalidate_inode_pages2(inode->i_mapping);
-               if (rc) {
+               if (rc)
                        cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
                                 __func__, inode);
-                       set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
-               }
        }
 
        cifs_fscache_reset_inode_cookie(inode);
        return rc;
 }
 
+/**
+ * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
+ * @word: long word containing the bit lock
+ */
+static int
+cifs_wait_bit_killable(void *word)
+{
+       if (fatal_signal_pending(current))
+               return -ERESTARTSYS;
+       freezable_schedule_unsafe();
+       return 0;
+}
+
 int
 cifs_revalidate_mapping(struct inode *inode)
 {
-       if (test_bit(CIFS_INODE_INVALID_MAPPING, &CIFS_I(inode)->flags));
-               return cifs_invalidate_mapping(inode);
-       return 0;
+       int rc;
+       unsigned long *flags = &CIFS_I(inode)->flags;
+
+       rc = wait_on_bit_lock(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
+                               TASK_KILLABLE);
+       if (rc)
+               return rc;
+
+       if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
+               rc = cifs_invalidate_mapping(inode);
+               if (rc)
+                       set_bit(CIFS_INO_INVALID_MAPPING, flags);
+       }
+
+        clear_bit_unlock(CIFS_INO_LOCK, flags);
+        smp_mb__after_clear_bit();
+        wake_up_bit(flags, CIFS_INO_LOCK);
+
+       return rc;
+}
+
+int
+cifs_zap_mapping(struct inode *inode)
+{
+       set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
+       return cifs_revalidate_mapping(inode);
 }
 
 int cifs_revalidate_file_attr(struct file *filp)
--

1.9.0

Signature · 3 lines
Thanks,

Steve

A message that is not in the archive (sent off-list or not yet received): CAH2r5mtOfVvofBD3HC4YpS-0h=gBny2DgBwEKxPCP6-=A4jp3g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#15 lore raw

Re: [PATCH 3/4] cifs: fix potential races in cifs_revalidate_mapping

Quoted 3 lines · On Thu, 24 Apr 2014 22:48:47 -0500 Steve French…

Various minor whitespace errors on this - do you want to respin it or want me to fix it up?

I'll respin and resend the whole set. It needs some merge help anyway to account for some of Sachin's changes. It'll probably be a few days before I can get to it though. Probably best that we punt this set out to v3.16.

Thanks,
Jeff

Quoted 194 lines · Jeff Layton
Quoted 190 lines

The handling of the CIFS_INO_INVALID_MAPPING flag is racy. It's possible for two tasks to attempt to revalidate the mapping at the same time. The first sees that CIFS_INO_INVALID_MAPPING is set. It clears the flag and then calls invalidate_inode_pages2 to start shooting down the pagecache.

While that's going on, another task checks the flag and sees that it's clear. It then ends up trusting the pagecache to satisfy a read when it shouldn't.

Fix this by adding a bitlock to ensure that the clearing of the flag is atomic with respect to the actual cache invalidation. Also, move the other existing users of cifs_invalidate_mapping to use a new cifs_zap_mapping() function that just sets the INVALID_MAPPING bit and then uses the standard codepath to handle the invalidation.

---
 fs/cifs/cifsfs.h   |  1 +
 fs/cifs/cifsglob.h |  1 +
 fs/cifs/file.c     | 12 ++++++------
 fs/cifs/inode.c    | 50 +++++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 49 insertions(+), 15 deletions(-)
fs/cifs/cifsfs.h +1 -0
index 40d5a4df1e38..b7cf9ee7654b 100644
@@ -68,6 +68,7 @@ extern int cifs_revalidate_file(struct file *filp);
 extern int cifs_revalidate_dentry(struct dentry *);
 extern int cifs_invalidate_mapping(struct inode *inode);
 extern int cifs_revalidate_mapping(struct inode *inode);
+extern int cifs_zap_mapping(struct inode *inode);
 extern int cifs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
 extern int cifs_setattr(struct dentry *, struct iattr *);
 
fs/cifs/cifsglob.h +1 -0
index 0d4a1039ac27..84cd4447cc31 100644
@@ -1111,6 +1111,7 @@ struct cifsInodeInfo {
        __u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */
        unsigned int oplock;            /* oplock/lease level we have */
        unsigned int epoch;             /* used to track lease state changes */
+#define CIFS_INO_LOCK                  (0)
 #define CIFS_INO_DELETE_PENDING                (1)
 #define CIFS_INO_INVALID_MAPPING       (2)
        unsigned long flags;            /* flags field */
fs/cifs/file.c +6 -6
index 784b17d510fc..ccca61803f0a 100644
@@ -335,7 +335,7 @@ cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
        spin_unlock(&cifs_file_list_lock);
 
        if (fid->purge_cache)
-               cifs_invalidate_mapping(inode);
+               cifs_zap_mapping(inode);
 
        file->private_data = cfile;
        return cfile;
@@ -1529,7 +1529,7 @@ cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
                 */
                if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
                                        CIFS_CACHE_READ(CIFS_I(inode))) {
-                       cifs_invalidate_mapping(inode);
+                       cifs_zap_mapping(inode);
                        cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
                                 inode);
                        CIFS_I(inode)->oplock = 0;
@@ -2218,7 +2218,7 @@ int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
                 file->f_path.dentry->d_name.name, datasync);
 
        if (!CIFS_CACHE_READ(CIFS_I(inode))) {
-               rc = cifs_invalidate_mapping(inode);
+               rc = cifs_zap_mapping(inode);
                if (rc) {
                        cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
                        rc = 0; /* don't care about it in fsync */
@@ -2628,7 +2628,7 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
                 * request comes - break it on the client to prevent reading
                 * an old data.
                 */
-               cifs_invalidate_mapping(inode);
+               cifs_zap_mapping(inode);
                cifs_dbg(FYI, "Set no oplock for inode=%p after a write operation\n",
                         inode);
                cinode->oplock = 0;
@@ -3125,7 +3125,7 @@ int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
        xid = get_xid();
 
        if (!CIFS_CACHE_READ(CIFS_I(inode))) {
-               rc = cifs_invalidate_mapping(inode);
+               rc = cifs_zap_mapping(inode);
                if (rc)
                        return rc;
        }
@@ -3669,7 +3669,7 @@ void cifs_oplock_break(struct work_struct *work)
                if (!CIFS_CACHE_READ(cinode)) {
                        rc = filemap_fdatawait(inode->i_mapping);
                        mapping_set_error(inode->i_mapping, rc);
-                       cifs_invalidate_mapping(inode);
+                       cifs_zap_mapping(inode);
                }
                cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
        }
fs/cifs/inode.c +41 -10
index 39b76bac196d..b840930f6271 100644
@@ -22,6 +22,7 @@
 #include <linux/stat.h>
 #include <linux/slab.h>
 #include <linux/pagemap.h>
+#include <linux/freezer.h>
 #include <asm/div64.h>
 #include "cifsfs.h"
 #include "cifspdu.h"
@@ -1762,29 +1763,60 @@ int
 cifs_invalidate_mapping(struct inode *inode)
 {
        int rc = 0;
-       struct cifsInodeInfo *cifs_i = CIFS_I(inode);
-
-       clear_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 
        if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
                rc = invalidate_inode_pages2(inode->i_mapping);
-               if (rc) {
+               if (rc)
                        cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
                                 __func__, inode);
-                       set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
-               }
        }
 
        cifs_fscache_reset_inode_cookie(inode);
        return rc;
 }
 
+/**
+ * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
+ * @word: long word containing the bit lock
+ */
+static int
+cifs_wait_bit_killable(void *word)
+{
+       if (fatal_signal_pending(current))
+               return -ERESTARTSYS;
+       freezable_schedule_unsafe();
+       return 0;
+}
+
 int
 cifs_revalidate_mapping(struct inode *inode)
 {
-       if (test_bit(CIFS_INODE_INVALID_MAPPING, &CIFS_I(inode)->flags));
-               return cifs_invalidate_mapping(inode);
-       return 0;
+       int rc;
+       unsigned long *flags = &CIFS_I(inode)->flags;
+
+       rc = wait_on_bit_lock(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
+                               TASK_KILLABLE);
+       if (rc)
+               return rc;
+
+       if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
+               rc = cifs_invalidate_mapping(inode);
+               if (rc)
+                       set_bit(CIFS_INO_INVALID_MAPPING, flags);
+       }
+
+        clear_bit_unlock(CIFS_INO_LOCK, flags);
+        smp_mb__after_clear_bit();
+        wake_up_bit(flags, CIFS_INO_LOCK);
+
+       return rc;
+}
+
+int
+cifs_zap_mapping(struct inode *inode)
+{
+       set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
+       return cifs_revalidate_mapping(inode);
 }
 
 int cifs_revalidate_file_attr(struct file *filp)
--

1.9.0

Signature · 1 line
Jeff Layton <[email protected]>
#16 lore raw

PATCH 4/4 cifs: revalidate mapping prior to satisfying aio_read request

In reply to a message not in the archive

Before satisfying a read with cache=loose, we should always check that the pagecache is actually valid before using it.

---
 fs/cifs/cifsfs.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
fs/cifs/cifsfs.c +16 -2
index 8a7faf7bd9ee..799e37262832 100644
@@ -726,6 +726,20 @@ out_nls:
 	goto out;
 }
 
+static ssize_t
+cifs_loose_aio_read(struct kiocb *iocb, const struct iovec *iov,
+		   unsigned long nr_segs, loff_t pos)
+{
+	int rc;
+	struct inode *inode = file_inode(iocb->ki_filp);
+
+	rc = cifs_revalidate_mapping(inode);
+	if (rc)
+		return rc;
+
+	return generic_file_aio_read(iocb, iov, nr_segs, pos);
+}
+
 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
 				   unsigned long nr_segs, loff_t pos)
 {
@@ -877,7 +891,7 @@ const struct inode_operations cifs_symlink_inode_ops = {
 const struct file_operations cifs_file_ops = {
 	.read = do_sync_read,
 	.write = do_sync_write,
-	.aio_read = generic_file_aio_read,
+	.aio_read = cifs_loose_aio_read,
 	.aio_write = cifs_file_aio_write,
 	.open = cifs_open,
 	.release = cifs_close,
@@ -935,7 +949,7 @@ const struct file_operations cifs_file_direct_ops = {
 const struct file_operations cifs_file_nobrl_ops = {
 	.read = do_sync_read,
 	.write = do_sync_write,
-	.aio_read = generic_file_aio_read,
+	.aio_read = cifs_loose_aio_read,
 	.aio_write = cifs_file_aio_write,
 	.open = cifs_open,
 	.release = cifs_close,
Signature · 1 line
1.9.0