Skip to contentSkip to conversation

CIFS: Make read call work with strict cache mode (try #3)

PATCH In review Pavel Shilovsky 3 participants 10 messages Nov 29, 2010 – Dec 8, 2010 6 messages not in the archive

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

PATCH 2/6 CIFS: Make read call work with strict cache mode (try #3)

Add cifs_file_aio_read where we read from the cache if we have at least Level II oplock - otherwise read from the server.

---
 fs/cifs/cifsfs.c |   42 ++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 40 insertions(+), 2 deletions(-)
fs/cifs/cifsfs.c +40 -2
index 9c37897..431c46e 100644
@@ -568,6 +568,44 @@ cifs_do_mount(struct file_system_type *fs_type,
 	return dget(sb->s_root);
 }
 
+static ssize_t cifs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
+				  unsigned long nr_segs, loff_t pos)
+{
+	struct inode *inode;
+	struct cifs_sb_info *cifs_sb;
+	ssize_t read = 0, retval;
+	unsigned long i;
+
+	inode = iocb->ki_filp->f_path.dentry->d_inode;
+	cifs_sb = CIFS_SB(iocb->ki_filp->f_path.dentry->d_sb);
+
+	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) == 0 ||
+	    CIFS_I(inode)->clientCanCacheRead)
+		return generic_file_aio_read(iocb, iov, nr_segs, pos);
+
+	/* In strict cache mode we need to read from the server all the time
+	 * if we don't have level II oplock because the server can delay mtime
+	 * change - so we can't make a decision about inode invalidating.
+	 * And we can also fail with pagereading if there are mandatory locks
+	 * on pages affected by this read but not on the region from pos to
+	 * pos+len-1. */
+
+	for (i = 0; i < nr_segs; i++) {
+		retval = cifs_user_read(iocb->ki_filp, iov[i].iov_base,
+					iov[i].iov_len, &pos);
+		if (retval < 0) {
+			read = read ? read : retval;
+			break;
+		}
+
+		read += retval;
+	}
+
+	iocb->ki_pos = pos;
+
+	return read;
+}
+
 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
 				   unsigned long nr_segs, loff_t pos)
 {
@@ -690,7 +728,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_file_aio_read,
 	.aio_write = cifs_file_aio_write,
 	.open = cifs_open,
 	.release = cifs_close,
@@ -727,7 +765,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_file_aio_read,
 	.aio_write = cifs_file_aio_write,
 	.open = cifs_open,
 	.release = cifs_close,
Signature · 1 line
1.7.3.2

A message that is not in the archive (sent off-list or not yet received): 1291037804-4000-1-git-send-email-piastryyy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH 3/6 CIFS: Make write call work with strict cache mode (try #3)

On strict cache mode if we don't have Exclusive oplock we write a data to the server through cifs_user_write. Also set inode invalid_mapping flag if we have written anything to the server.

---
 fs/cifs/cifsfs.c |   49 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 44 insertions(+), 5 deletions(-)
fs/cifs/cifsfs.c +44 -5
index 431c46e..0732538 100644
@@ -609,12 +609,51 @@ static ssize_t cifs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
 				   unsigned long nr_segs, loff_t pos)
 {
-	struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
-	ssize_t written;
+	struct inode *inode;
+	struct cifs_sb_info *cifs_sb;
+	ssize_t written = 0, retval;
+	unsigned long i;
+
+	inode = iocb->ki_filp->f_path.dentry->d_inode;
+
+	if (CIFS_I(inode)->clientCanCacheAll)
+		return generic_file_aio_write(iocb, iov, nr_segs, pos);
+
+	cifs_sb = CIFS_SB(iocb->ki_filp->f_path.dentry->d_sb);
+
+	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) == 0) {
+		int rc;
+
+		written = generic_file_aio_write(iocb, iov, nr_segs, pos);
+
+		rc = filemap_fdatawrite(inode->i_mapping);
+		if (rc)
+			cFYI(1, "cifs_file_aio_write: %d rc on %p inode",
+			     rc, inode);
+		return written;
+	}
+
+	/* In strict cache mode we need to write the data to the server exactly
+	 * from the pos to pos+len-1 rather than flush all affected pages
+	 * because it may cause a error with mandatory locks on these pages but
+	 * not on the region from pos to ppos+len-1. */
+
+	for (i = 0; i < nr_segs; i++) {
+		retval = cifs_user_write(iocb->ki_filp, iov[i].iov_base,
+					 iov[i].iov_len, &pos);
+		if (retval < 0) {
+			written = written ? written : retval;
+			break;
+		}
+
+		written += retval;
+	}
+
+	iocb->ki_pos = pos;
+
+	if (written > 0)
+		CIFS_I(inode)->invalid_mapping = true;
 
-	written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-	if (!CIFS_I(inode)->clientCanCacheAll)
-		filemap_fdatawrite(inode->i_mapping);
 	return written;
 }
 
Signature · 1 line
1.7.3.2

A message that is not in the archive (sent off-list or not yet received): 1291037804-4000-2-git-send-email-piastryyy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw

Re: [PATCH 3/6] CIFS: Make write call work with strict cache mode (try #3)

2010/11/29 Pavel Shilovsky <[email protected]>:

Quoted 71 lines

On strict cache mode if we don't have Exclusive oplock we write a data to the server through cifs_user_write. Also set inode invalid_mapping flag if we have written anything to the server.

---
 fs/cifs/cifsfs.c |   49 ++++++++++++++++++++++++++++++++++++++++++++-----

 1 files changed, 44 insertions(+), 5 deletions(-)
fs/cifs/cifsfs.c +44 -6
index 431c46e..0732538 100644
@@ -609,12 +609,51 @@ static ssize_t cifs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
                                   unsigned long nr_segs, loff_t pos)
  {
-       struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
-       ssize_t written;
+       struct inode *inode;
+       struct cifs_sb_info *cifs_sb;
+       ssize_t written = 0, retval;
+       unsigned long i;
+
+       inode = iocb->ki_filp->f_path.dentry->d_inode;
+
+       if (CIFS_I(inode)->clientCanCacheAll)
+               return generic_file_aio_write(iocb, iov, nr_segs, pos);
+
+       cifs_sb = CIFS_SB(iocb->ki_filp->f_path.dentry->d_sb);
+
+       if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) == 0) {
+               int rc;
+
+               written = generic_file_aio_write(iocb, iov, nr_segs, pos);
+
+               rc = filemap_fdatawrite(inode->i_mapping);
+               if (rc)
+                       cFYI(1, "cifs_file_aio_write: %d rc on %p inode",
+                            rc, inode);
+               return written;
+       }
+
+       /* In strict cache mode we need to write the data to the server exactly
+        * from the pos to pos+len-1 rather than flush all affected pages
+        * because it may cause a error with mandatory locks on these pages but
+        * not on the region from pos to ppos+len-1. */
+
+       for (i = 0; i < nr_segs; i++) {
+               retval = cifs_user_write(iocb->ki_filp, iov[i].iov_base,
+                                        iov[i].iov_len, &pos);
+               if (retval < 0) {
+                       written = written ? written : retval;
+                       break;
+               }
+
+               written += retval;
+       }
+
+       iocb->ki_pos = pos;
+
+       if (written > 0)
+               CIFS_I(inode)->invalid_mapping = true;
 
-       written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-       if (!CIFS_I(inode)->clientCanCacheAll)
-               filemap_fdatawrite(inode->i_mapping);
        return written;
  }
 
--
Quoted 3 lines

1.7.3.2

See correct one in try #4.

Signature · 2 lines
Best regards,
Pavel Shilovsky.
#5 lore raw

PATCH 4/6 CIFS: Make cifs_fsync work with strict cache mode (try #3)

In reply to a message not in the archive

Remove filemap_write_and_wait call because it is previously called from vfs_fsync_range and invalidate inode if we don't have at least Level II oplock and strcit cache mode switched on.

---
 fs/cifs/cifsfs.h |    1 +
 fs/cifs/file.c   |   14 +++++++-------
 fs/cifs/inode.c  |    2 +-
 3 files changed, 9 insertions(+), 8 deletions(-)
fs/cifs/cifsfs.h +1 -0
index 897b2b2..b8d783c 100644
@@ -61,6 +61,7 @@ extern int cifs_rename(struct inode *, struct dentry *, struct inode *,
 		       struct dentry *);
 extern int cifs_revalidate_file(struct file *filp);
 extern int cifs_revalidate_dentry(struct dentry *);
+extern void cifs_invalidate_mapping(struct inode *inode);
 extern int cifs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
 extern int cifs_setattr(struct dentry *, struct iattr *);
 
fs/cifs/file.c +7 -7
index f853bf0..d216879 100644
@@ -1595,20 +1595,20 @@ int cifs_fsync(struct file *file, int datasync)
 	struct cifsTconInfo *tcon;
 	struct cifsFileInfo *smbfile = file->private_data;
 	struct inode *inode = file->f_path.dentry->d_inode;
+	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 
 	xid = GetXid();
 
 	cFYI(1, "Sync file - name: %s datasync: 0x%x",
 		file->f_path.dentry->d_name.name, datasync);
 
-	rc = filemap_write_and_wait(inode->i_mapping);
-	if (rc == 0) {
-		struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
+	if (!CIFS_I(inode)->clientCanCacheRead &&
+	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO))
+		cifs_invalidate_mapping(inode);
 
-		tcon = tlink_tcon(smbfile->tlink);
-		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
-			rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
-	}
+	tcon = tlink_tcon(smbfile->tlink);
+	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
+		rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
 
 	FreeXid(xid);
 	return rc;
fs/cifs/inode.c +1 -1
index ff7d299..f90a40a 100644
@@ -1671,7 +1671,7 @@ cifs_inode_needs_reval(struct inode *inode)
 }
 
 /* check invalid_mapping flag and zap the cache if it's set */
-static void
+void
 cifs_invalidate_mapping(struct inode *inode)
 {
 	int rc;
Signature · 1 line
1.7.3.2

A message that is not in the archive (sent off-list or not yet received): 1291037804-4000-3-git-send-email-piastryyy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#7 lore raw

Re: [PATCH 4/6] CIFS: Make cifs_fsync work with strict cache mode (try #3)

Quoted 60 lines · On Mon, 29 Nov 2010 16:36:43 +0300 Pavel Shilovsky…

Remove filemap_write_and_wait call because it is previously called from vfs_fsync_range and invalidate inode if we don't have at least Level II oplock and strcit cache mode switched on.

---
 fs/cifs/cifsfs.h |    1 +
 fs/cifs/file.c   |   14 +++++++-------
 fs/cifs/inode.c  |    2 +-
 3 files changed, 9 insertions(+), 8 deletions(-)
fs/cifs/cifsfs.h +1 -0
index 897b2b2..b8d783c 100644
@@ -61,6 +61,7 @@ extern int cifs_rename(struct inode *, struct dentry *, struct inode *,
 		       struct dentry *);
 extern int cifs_revalidate_file(struct file *filp);
 extern int cifs_revalidate_dentry(struct dentry *);
+extern void cifs_invalidate_mapping(struct inode *inode);
 extern int cifs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
 extern int cifs_setattr(struct dentry *, struct iattr *);
 
fs/cifs/file.c +7 -7
index f853bf0..d216879 100644
@@ -1595,20 +1595,20 @@ int cifs_fsync(struct file *file, int datasync)
 	struct cifsTconInfo *tcon;
 	struct cifsFileInfo *smbfile = file->private_data;
 	struct inode *inode = file->f_path.dentry->d_inode;
+	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 
 	xid = GetXid();
 
 	cFYI(1, "Sync file - name: %s datasync: 0x%x",
 		file->f_path.dentry->d_name.name, datasync);
 
-	rc = filemap_write_and_wait(inode->i_mapping);
-	if (rc == 0) {
-		struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
+	if (!CIFS_I(inode)->clientCanCacheRead &&
+	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO))
+		cifs_invalidate_mapping(inode);
 
-		tcon = tlink_tcon(smbfile->tlink);
-		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
-			rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
-	}
+	tcon = tlink_tcon(smbfile->tlink);
+	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
+		rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
 
 	FreeXid(xid);
 	return rc;
fs/cifs/inode.c +0 -0
index ff7d299..f90a40a 100644

fs/cifs/inode.c cifs_inode_needs_reval(struct inode *inode) · line 1677

Quoted 8 lines
fs/cifs/inode.c +1 -1
@@ -1671,7 +1671,7 @@ cifs_inode_needs_reval(struct inode *inode)
 }
 
 /* check invalid_mapping flag and zap the cache if it's set */
-static void
+void
 cifs_invalidate_mapping(struct inode *inode)
 {
 	int rc;

Looks reasonable, but I'll withhold the reviewed-by until the previous patches are fixed.

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

PATCH 5/6 CIFS: Make cifs_file_map work with strict cache mode (try #3)

In reply to a message not in the archive

Invalidate inode if we don't have at least Level II oplock and strict cache mode switched on.

---
 fs/cifs/file.c |   20 +++++++++++++++-----
 1 files changed, 15 insertions(+), 5 deletions(-)
fs/cifs/file.c +15 -5
index d216879..e9f2fae 100644
@@ -1812,14 +1812,24 @@ static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	int rc, xid;
+	struct inode *inode = file->f_path.dentry->d_inode;
+	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 
 	xid = GetXid();
-	rc = cifs_revalidate_file(file);
-	if (rc) {
-		cFYI(1, "Validation prior to mmap failed, error=%d", rc);
-		FreeXid(xid);
-		return rc;
+
+	if (!CIFS_I(inode)->clientCanCacheRead &&
+	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO))
+		cifs_invalidate_mapping(inode);
+	else {
+		rc = cifs_revalidate_file(file);
+		if (rc) {
+			cFYI(1, "Validation prior to mmap failed, error=%d",
+			     rc);
+			FreeXid(xid);
+			return rc;
+		}
 	}
+
 	rc = generic_file_mmap(file, vma);
 	FreeXid(xid);
 	return rc;
Signature · 1 line
1.7.3.2

A message that is not in the archive (sent off-list or not yet received): 1291037804-4000-4-git-send-email-piastryyy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#10 lore raw

Re: [PATCH 5/6] CIFS: Make cifs_file_map work with strict cache mode (try #3)

Quoted 32 lines · On Mon, 29 Nov 2010 16:36:44 +0300 Pavel Shilovsky…

Invalidate inode if we don't have at least Level II oplock and strict cache mode switched on.

---
 fs/cifs/file.c |   20 +++++++++++++++-----
 1 files changed, 15 insertions(+), 5 deletions(-)
fs/cifs/file.c +9 -5
index d216879..e9f2fae 100644
@@ -1812,14 +1812,24 @@ static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	int rc, xid;
+	struct inode *inode = file->f_path.dentry->d_inode;
+	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 
 	xid = GetXid();
-	rc = cifs_revalidate_file(file);
-	if (rc) {
-		cFYI(1, "Validation prior to mmap failed, error=%d", rc);
-		FreeXid(xid);
-		return rc;
+
+	if (!CIFS_I(inode)->clientCanCacheRead &&
+	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO))
+		cifs_invalidate_mapping(inode);
+	else {
+		rc = cifs_revalidate_file(file);
+		if (rc) {

fs/cifs/file.c static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size, · line 1835

Quoted 10 lines
fs/cifs/file.c +6 -0
+			cFYI(1, "Validation prior to mmap failed, error=%d",
+			     rc);
+			FreeXid(xid);
+			return rc;
+		}
 	}
+
 	rc = generic_file_mmap(file, vma);
 	FreeXid(xid);
 	return rc;

Looks reasonable, though all of this special-casing sure is gross...

Might it be better to have a set of cifs_strict_file/inode_ops and use those rather than all of these if/else conditions?

Signature · 1 line
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): 20101207075635.6f9c1c5f-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org

#12 lore raw

Re: [PATCH 5/6] CIFS: Make cifs_file_map work with strict cache mode (try #3)

2010/12/7 Jeff Layton <[email protected]>:

Quoted 5 lines

Looks reasonable, though all of this special-casing sure is gross...

Might it be better to have a set of cifs_strict_file/inode_ops and use those rather than all of these if/else conditions?

Good idea. So, do you mean to split all these aio_read, aio_write, fsync and mmap into strict and non-strict calls as we have in direct i/o case?

Signature · 2 lines
Best regards,
Pavel Shilovsky.

A message that is not in the archive (sent off-list or not yet received): AANLkTineJVVgK6aihGBLWJfEnEOuz67CcHncwBUpdvt0-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#14 lore raw

Re: [PATCH 5/6] CIFS: Make cifs_file_map work with strict cache mode (try #3)

Quoted 11 lines · On Wed, 8 Dec 2010 23:53:12 +0300 Pavel Shilovsky…

2010/12/7 Jeff Layton <[email protected]>:

Quoted 5 lines

Looks reasonable, though all of this special-casing sure is gross...

Might it be better to have a set of cifs_strict_file/inode_ops and use those rather than all of these if/else conditions?

Good idea. So, do you mean to split all these aio_read, aio_write, fsync and mmap into strict and non-strict calls as we have in direct i/o case?

Right. Once a mount is declared "strictcache" it won't ever change, so you might as well set the ops appropriately.

Signature · 1 line
Jeff Layton <[email protected]>
#15 lore raw
In reply to a message not in the archive

2010/11/29 Pavel Shilovsky <[email protected]>:

Quoted 76 lines

Add cifs_file_aio_read where we read from the cache if we have at least Level II oplock - otherwise read from the server.

---
 fs/cifs/cifsfs.c |   42 ++++++++++++++++++++++++++++++++++++++++--

 1 files changed, 40 insertions(+), 2 deletions(-)
fs/cifs/cifsfs.c +40 -3
index 9c37897..431c46e 100644
@@ -568,6 +568,44 @@ cifs_do_mount(struct file_system_type *fs_type,
        return dget(sb->s_root);
  }
 
+static ssize_t cifs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
+                                 unsigned long nr_segs, loff_t pos)
+{
+       struct inode *inode;
+       struct cifs_sb_info *cifs_sb;
+       ssize_t read = 0, retval;
+       unsigned long i;
+
+       inode = iocb->ki_filp->f_path.dentry->d_inode;
+       cifs_sb = CIFS_SB(iocb->ki_filp->f_path.dentry->d_sb);
+
+       if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) == 0 ||
+           CIFS_I(inode)->clientCanCacheRead)
+               return generic_file_aio_read(iocb, iov, nr_segs, pos);
+
+       /* In strict cache mode we need to read from the server all the time
+        * if we don't have level II oplock because the server can delay mtime
+        * change - so we can't make a decision about inode invalidating.
+        * And we can also fail with pagereading if there are mandatory locks
+        * on pages affected by this read but not on the region from pos to
+        * pos+len-1. */
+
+       for (i = 0; i < nr_segs; i++) {
+               retval = cifs_user_read(iocb->ki_filp, iov[i].iov_base,
+                                       iov[i].iov_len, &pos);
+               if (retval < 0) {
+                       read = read ? read : retval;
+                       break;
+               }
+
+               read += retval;
+       }
+
+       iocb->ki_pos = pos;
+
+       return read;
+}
+
  static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
                                   unsigned long nr_segs, loff_t pos)
  {
@@ -690,7 +728,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_file_aio_read,
        .aio_write = cifs_file_aio_write,
        .open = cifs_open,
        .release = cifs_close,
@@ -727,7 +765,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_file_aio_read,
        .aio_write = cifs_file_aio_write,
        .open = cifs_open,
        .release = cifs_close,
--
Quoted 3 lines

1.7.3.2

Just found out that comment style don't match CodingStyle again. Let's drop with version. The same for write patch.

Signature · 2 lines
Best regards,
Pavel Shilovsky.