Skip to contentSkip to conversation

relock file capability and locking fixes

PATCH Superseded Pavel Shilovsky 3 participants 20 messages Nov 27, 2012 – Nov 28, 2012 12 messages not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover relock file capability and locking fixes

This patchset is targeted to implement cifs_relock_file as well as to fix some bugs in a lock/oplock codepath. This is suitable for v3.8.

Pavel Shilovsky (5):
CIFS: Separate pushing posix locks and lock_sem handling
CIFS: Separate pushing mandatory locks and lock_sem handling
CIFS: Implement cifs_relock_file
CIFS: Fix lock consistensy bug in cifs_setlk
CIFS: Fix write after setting a read lock for read oplock files

 fs/cifs/file.c     |   87 ++++++++++++++++++++++++++++------------------------
 fs/cifs/smb2file.c |   12 --------
 2 files changed, 47 insertions(+), 52 deletions(-)
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 1/5 CIFS: Separate pushing posix locks and lock_sem handling

In reply to a message not in the archive
---
 fs/cifs/file.c |   32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
fs/cifs/file.c +19 -13
index be0c3ea..54541a6 100644
@@ -1047,9 +1047,8 @@ struct lock_to_push {
 };
 
 static int
-cifs_push_posix_locks(struct cifsFileInfo *cfile)
+cifs_push_posix_locks_locked(struct cifsFileInfo *cfile)
 {
-	struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 	struct file_lock *flock, **before;
 	unsigned int count = 0, i = 0;
@@ -1060,14 +1059,6 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)
 
 	xid = get_xid();
 
-	/* we are going to update can_cache_brlcks here - need a write access */
-	down_write(&cinode->lock_sem);
-	if (!cinode->can_cache_brlcks) {
-		up_write(&cinode->lock_sem);
-		free_xid(xid);
-		return rc;
-	}
-
 	lock_flocks();
 	cifs_for_each_lock(cfile->dentry->d_inode, before) {
 		if ((*before)->fl_flags & FL_POSIX)
@@ -1133,9 +1124,6 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)
 	}
 
 out:
-	cinode->can_cache_brlcks = false;
-	up_write(&cinode->lock_sem);
-
 	free_xid(xid);
 	return rc;
 err_out:
@@ -1147,6 +1135,24 @@ err_out:
 }
 
 static int
+cifs_push_posix_locks(struct cifsFileInfo *cfile)
+{
+	struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
+	int rc = 0;
+
+	/* we are going to update can_cache_brlcks here - need a write access */
+	down_write(&cinode->lock_sem);
+	if (!cinode->can_cache_brlcks) {
+		up_write(&cinode->lock_sem);
+		return rc;
+	}
+	rc = cifs_push_posix_locks_locked(cfile);
+	cinode->can_cache_brlcks = false;
+	up_write(&cinode->lock_sem);
+	return rc;
+}
+
+static int
 cifs_push_locks(struct cifsFileInfo *cfile)
 {
 	struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 2/5 CIFS: Separate pushing mandatory locks and lock_sem handling

In reply to a message not in the archive
---
 fs/cifs/file.c     |   39 ++++++++++-----------------------------
 fs/cifs/smb2file.c |   12 ------------
 2 files changed, 10 insertions(+), 41 deletions(-)
fs/cifs/file.c +10 -29
index 54541a6..cf67f8f 100644
@@ -954,7 +954,6 @@ cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
 	int rc = 0, stored_rc;
 	struct cifsLockInfo *li, *tmp;
 	struct cifs_tcon *tcon;
-	struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
 	unsigned int num, max_num, max_buf;
 	LOCKING_ANDX_RANGE *buf, *cur;
 	int types[] = {LOCKING_ANDX_LARGE_FILES,
@@ -964,21 +963,12 @@ cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
 	xid = get_xid();
 	tcon = tlink_tcon(cfile->tlink);
 
-	/* we are going to update can_cache_brlcks here - need a write access */
-	down_write(&cinode->lock_sem);
-	if (!cinode->can_cache_brlcks) {
-		up_write(&cinode->lock_sem);
-		free_xid(xid);
-		return rc;
-	}
-
 	/*
 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
 	 * and check it for zero before using.
 	 */
 	max_buf = tcon->ses->server->maxBuf;
 	if (!max_buf) {
-		up_write(&cinode->lock_sem);
 		free_xid(xid);
 		return -EINVAL;
 	}
@@ -987,7 +977,6 @@ cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
 						sizeof(LOCKING_ANDX_RANGE);
 	buf = kzalloc(max_num * sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
 	if (!buf) {
-		up_write(&cinode->lock_sem);
 		free_xid(xid);
 		return -ENOMEM;
 	}
@@ -1024,9 +1013,6 @@ cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
 		}
 	}
 
-	cinode->can_cache_brlcks = false;
-	up_write(&cinode->lock_sem);
-
 	kfree(buf);
 	free_xid(xid);
 	return rc;
@@ -1047,7 +1033,7 @@ struct lock_to_push {
 };
 
 static int
-cifs_push_posix_locks_locked(struct cifsFileInfo *cfile)
+cifs_push_posix_locks(struct cifsFileInfo *cfile)
 {
 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 	struct file_lock *flock, **before;
@@ -1135,9 +1121,11 @@ err_out:
 }
 
 static int
-cifs_push_posix_locks(struct cifsFileInfo *cfile)
+cifs_push_locks(struct cifsFileInfo *cfile)
 {
+	struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
 	struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
+	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 	int rc = 0;
 
 	/* we are going to update can_cache_brlcks here - need a write access */
@@ -1146,24 +1134,17 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)
 		up_write(&cinode->lock_sem);
 		return rc;
 	}
-	rc = cifs_push_posix_locks_locked(cfile);
-	cinode->can_cache_brlcks = false;
-	up_write(&cinode->lock_sem);
-	return rc;
-}
-
-static int
-cifs_push_locks(struct cifsFileInfo *cfile)
-{
-	struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
-	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 
 	if (cap_unix(tcon->ses) &&
 	    (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
 	    ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
-		return cifs_push_posix_locks(cfile);
+		rc = cifs_push_posix_locks(cfile);
+	else
+		rc = tcon->ses->server->ops->push_mand_locks(cfile);
 
-	return tcon->ses->server->ops->push_mand_locks(cfile);
+	cinode->can_cache_brlcks = false;
+	up_write(&cinode->lock_sem);
+	return rc;
 }
 
 static void
fs/cifs/smb2file.c +0 -12
index bfe22e8..7dfb50c 100644
@@ -260,13 +260,6 @@ smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
 	struct cifs_fid_locks *fdlocks;
 
 	xid = get_xid();
-	/* we are going to update can_cache_brlcks here - need a write access */
-	down_write(&cinode->lock_sem);
-	if (!cinode->can_cache_brlcks) {
-		up_write(&cinode->lock_sem);
-		free_xid(xid);
-		return rc;
-	}
 
 	/*
 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
@@ -274,7 +267,6 @@ smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
 	 */
 	max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
 	if (!max_buf) {
-		up_write(&cinode->lock_sem);
 		free_xid(xid);
 		return -EINVAL;
 	}
@@ -282,7 +274,6 @@ smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
 	max_num = max_buf / sizeof(struct smb2_lock_element);
 	buf = kzalloc(max_num * sizeof(struct smb2_lock_element), GFP_KERNEL);
 	if (!buf) {
-		up_write(&cinode->lock_sem);
 		free_xid(xid);
 		return -ENOMEM;
 	}
@@ -293,10 +284,7 @@ smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
 			rc = stored_rc;
 	}
 
-	cinode->can_cache_brlcks = false;
 	kfree(buf);
-
-	up_write(&cinode->lock_sem);
 	free_xid(xid);
 	return rc;
 }
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH 3/5 CIFS: Implement cifs_relock_file

In reply to a message not in the archive

that reacquires byte-range locks when a file is reopened.

---
 fs/cifs/file.c |   26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
fs/cifs/file.c +23 -3
index cf67f8f..a18c9eb 100644
@@ -508,16 +508,36 @@ out:
 	return rc;
 }
 
+static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
+
 /*
  * Try to reacquire byte range locks that were released when session
- * to server was lost
+ * to server was lost.
  */
-static int cifs_relock_file(struct cifsFileInfo *cifsFile)
+static int
+cifs_relock_file(struct cifsFileInfo *cfile)
 {
+	struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
+	struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
+	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 	int rc = 0;
 
-	/* BB list all locks open on this file and relock */
+	/* we are going to update can_cache_brlcks here - need a write access */
+	down_write(&cinode->lock_sem);
+	if (cinode->can_cache_brlcks) {
+		/* can cache locks - no need to push them */
+		up_write(&cinode->lock_sem);
+		return rc;
+	}
+
+	if (cap_unix(tcon->ses) &&
+	    (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
+	    ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
+		rc = cifs_push_posix_locks(cfile);
+	else
+		rc = tcon->ses->server->ops->push_mand_locks(cfile);
 
+	up_write(&cinode->lock_sem);
 	return rc;
 }
 
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 4/5 CIFS: Fix lock consistensy bug in cifs_setlk

In reply to a message not in the archive

If we netogiate mandatory locking style, have a read lock and try to set a write lock we end up with a write lock in vfs cache and no lock in cifs lock cache - that's wrong. Fix it by returning from cifs_setlk immediately if a error occurs during setting a lock.

---
 fs/cifs/file.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/cifs/file.c +5 -3
index a18c9eb..f8fe1bd 100644
@@ -1449,16 +1449,18 @@ cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
 			return -ENOMEM;
 
 		rc = cifs_lock_add_if(cfile, lock, wait_flag);
-		if (rc < 0)
+		if (rc < 0) {
 			kfree(lock);
-		if (rc <= 0)
+			return rc;
+		}
+		if (!rc)
 			goto out;
 
 		rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
 					    type, 1, 0, wait_flag);
 		if (rc) {
 			kfree(lock);
-			goto out;
+			return rc;
 		}
 
 		cifs_lock_add(cfile, lock);
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH 5/5 CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by replacing it with filemap_write_and_wait call and sending non-page write in a error case.

While this may end up with two write requests to the server, we can be sure that our data will be the same at the server and the page cache - the next read on this file gets the valid data.

---
 fs/cifs/file.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/cifs/file.c +4 -6
index f8fe1bd..89efd85 100644
@@ -2511,14 +2511,12 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
 	 */
 	if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) {
 		ssize_t written;
-		int rc;
 
 		written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-		rc = filemap_fdatawrite(inode->i_mapping);
-		if (rc)
-			return (ssize_t)rc;
-
-		return written;
+		/* try page write at first */
+		if (!filemap_write_and_wait(inode->i_mapping))
+			return written;
+		/* page write failed - try from pos to pos+len-1 */
 	}
 #endif
 
Signature · 1 line
1.7.10.4
Acked-by
#8 lore raw

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive
Quoted 28 lines · On Tue, 27 Nov 2012 10:50:28 +0400 Pavel Shilovsky…

If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by replacing it with filemap_write_and_wait call and sending non-page write in a error case.

While this may end up with two write requests to the server, we can be sure that our data will be the same at the server and the page cache - the next read on this file gets the valid data.

---
 fs/cifs/file.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/cifs/file.c +0 -3
index f8fe1bd..89efd85 100644
@@ -2511,14 +2511,12 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
 	 */
 	if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) {
 		ssize_t written;
-		int rc;
 
 		written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-		rc = filemap_fdatawrite(inode->i_mapping);
-		if (rc)

fs/cifs/file.c cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, · line 2522

Quoted 10 lines
fs/cifs/file.c +4 -3
-			return (ssize_t)rc;
-
-		return written;
+		/* try page write at first */
+		if (!filemap_write_and_wait(inode->i_mapping))
+			return written;
+		/* page write failed - try from pos to pos+len-1 */
 	}
 #endif
 

Bleh -- nasty. I guess this will work though...

Wonder if there's some way to populate the cache and then just mark the pages clean without sending out writes? That would be a better solution IMO, but I guess we can live with this for now...

Reviewed-by
#31 lore raw
In reply to a message not in the archive
Quoted 9 lines · On Tue, 27 Nov 2012 10:50:23 +0400 Pavel Shilovsky…

This patchset is targeted to implement cifs_relock_file as well as to fix some bugs in a lock/oplock codepath. This is suitable for v3.8.

Pavel Shilovsky (5):
CIFS: Separate pushing posix locks and lock_sem handling
CIFS: Separate pushing mandatory locks and lock_sem handling
CIFS: Implement cifs_relock_file
CIFS: Fix lock consistensy bug in cifs_setlk
CIFS: Fix write after setting a read lock for read oplock files

Quoted 4 lines
 fs/cifs/file.c     |   87 ++++++++++++++++++++++++++++------------------------
 fs/cifs/smb2file.c |   12 --------
 2 files changed, 47 insertions(+), 52 deletions(-)

Set looks good to me...

#10 lore raw

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

2012/11/27 Jeff Layton <[email protected]>:

Quoted 44 lines
Quoted 28 lines · On Tue, 27 Nov 2012 10:50:28 +0400 Pavel Shilovsky…

If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by replacing it with filemap_write_and_wait call and sending non-page write in a error case.

While this may end up with two write requests to the server, we can be sure that our data will be the same at the server and the page cache - the next read on this file gets the valid data.

---
 fs/cifs/file.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/cifs/file.c +0 -3
index f8fe1bd..89efd85 100644
@@ -2511,14 +2511,12 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
       */
      if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) {
              ssize_t written;
-             int rc;
 
              written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-             rc = filemap_fdatawrite(inode->i_mapping);
-             if (rc)

fs/cifs/file.c cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, · line 2522

Quoted 10 lines
fs/cifs/file.c +4 -3
-                     return (ssize_t)rc;
-
-             return written;
+             /* try page write at first */
+             if (!filemap_write_and_wait(inode->i_mapping))
+                     return written;
+             /* page write failed - try from pos to pos+len-1 */
      }
 #endif
 

Bleh -- nasty. I guess this will work though...

Quoted 5 lines

Wonder if there's some way to populate the cache and then just mark the pages clean without sending out writes? That would be a better solution IMO, but I guess we can live with this for now...

Thanks for reviewing the patchset.

As for this patch I have the followon patch:

http://git.altlinux.org/people/piastry/public/?p=cifs-2.6.git;a=commitdiff;h=11673376e6840c686f0c6e51892b16ad1945887e

that allows us to populate the page cache without making pages dirty. I didn't have enough time to test it well (that's why I haven't posted it yet) - going to do it tomorrow. But you are welcome to comment the approach.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

Am curious how you noticed this problem? Did you have a test case?

Quoted 45 lines · Pavel Shilovsky

If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by replacing it with filemap_write_and_wait call and sending non-page write in a error case.

While this may end up with two write requests to the server, we can be sure that our data will be the same at the server and the page cache - the next read on this file gets the valid data.

---
 fs/cifs/file.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/cifs/file.c +4 -7
index f8fe1bd..89efd85 100644
@@ -2511,14 +2511,12 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
         */
        if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) {
                ssize_t written;
-               int rc;
 
                written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-               rc = filemap_fdatawrite(inode->i_mapping);
-               if (rc)
-                       return (ssize_t)rc;
-
-               return written;
+               /* try page write at first */
+               if (!filemap_write_and_wait(inode->i_mapping))
+                       return written;
+               /* page write failed - try from pos to pos+len-1 */
        }
 #endif
 
--

1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
Thanks,

Steve
#30 lore raw

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

2012/11/27 Steve French <[email protected]>:

Quoted 1 line

Am curious how you noticed this problem? Did you have a test case?

The test case that reproduces the problem:
1) client1 opens "filename" (file that already has enough data ~~ 4096 bytes is ok)
2) client1 sets a read lock a file from pos=1 to pos=2
3) client2 opens "filename" - client1 gets oplock break to level II and push lock from #2 to the server.
4) client1 writes 'a' to a file (from pos 0) - got status OK, but the server reject the write (due to the client1 writes the whole page from pos=0 to pos=4096 and conflicts with the read lock from pos=1 to pos=2) - ERROR.
5) client2 reads 1 byte and got the old data (not 'a') because the server returns it's version of the data

It is suitable for both CIFS and SMB2.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive
Quoted 53 lines · On Tue, 27 Nov 2012 18:57:22 +0400 Pavel Shilovsky…

2012/11/27 Jeff Layton <[email protected]>:

Quoted 44 lines
Quoted 28 lines · On Tue, 27 Nov 2012 10:50:28 +0400 Pavel Shilovsky…

If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by replacing it with filemap_write_and_wait call and sending non-page write in a error case.

While this may end up with two write requests to the server, we can be sure that our data will be the same at the server and the page cache - the next read on this file gets the valid data.

---
 fs/cifs/file.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/cifs/file.c +0 -3
index f8fe1bd..89efd85 100644
@@ -2511,14 +2511,12 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
       */
      if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) {
              ssize_t written;
-             int rc;
 
              written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-             rc = filemap_fdatawrite(inode->i_mapping);
-             if (rc)

fs/cifs/file.c cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, · line 2522

Quoted 10 lines
fs/cifs/file.c +4 -3
-                     return (ssize_t)rc;
-
-             return written;
+             /* try page write at first */
+             if (!filemap_write_and_wait(inode->i_mapping))
+                     return written;
+             /* page write failed - try from pos to pos+len-1 */
      }
 #endif
 

Bleh -- nasty. I guess this will work though...

Quoted 5 lines

Wonder if there's some way to populate the cache and then just mark the pages clean without sending out writes? That would be a better solution IMO, but I guess we can live with this for now...

Thanks for reviewing the patchset.

Quoted 9 lines

As for this patch I have the followon patch:

http://git.altlinux.org/people/piastry/public/?p=cifs-2.6.git;a=commitdiff;h=11673376e6840c686f0c6e51892b16ad1945887e

that allows us to populate the page cache without making pages dirty. I didn't have enough time to test it well (that's why I haven't posted it yet) - going to do it tomorrow. But you are welcome to comment the approach.

The only thing that makes me wary here is that you're setting this flag on the inode. Could there ever be a situation where another task might be writing to this inode at the same time and needs to set them dirty?

If not, then I'm not sure I see the need for a new bool in the inode. It might be simpler to just check what sort of oplock you have in write_end instead.

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

2012/11/28 Jeff Layton <[email protected]>:

Quoted 70 lines
Quoted 53 lines · On Tue, 27 Nov 2012 18:57:22 +0400 Pavel Shilovsky…

2012/11/27 Jeff Layton <[email protected]>:

Quoted 44 lines
Quoted 28 lines · On Tue, 27 Nov 2012 10:50:28 +0400 Pavel Shilovsky…

If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by replacing it with filemap_write_and_wait call and sending non-page write in a error case.

While this may end up with two write requests to the server, we can be sure that our data will be the same at the server and the page cache - the next read on this file gets the valid data.

---
 fs/cifs/file.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/cifs/file.c +0 -3
index f8fe1bd..89efd85 100644
@@ -2511,14 +2511,12 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
       */
      if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) {
              ssize_t written;
-             int rc;
 
              written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-             rc = filemap_fdatawrite(inode->i_mapping);
-             if (rc)

fs/cifs/file.c cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, · line 2522

Quoted 10 lines
fs/cifs/file.c +4 -3
-                     return (ssize_t)rc;
-
-             return written;
+             /* try page write at first */
+             if (!filemap_write_and_wait(inode->i_mapping))
+                     return written;
+             /* page write failed - try from pos to pos+len-1 */
      }
 #endif
 

Bleh -- nasty. I guess this will work though...

Quoted 5 lines

Wonder if there's some way to populate the cache and then just mark the pages clean without sending out writes? That would be a better solution IMO, but I guess we can live with this for now...

Thanks for reviewing the patchset.

Quoted 9 lines

As for this patch I have the followon patch:

http://git.altlinux.org/people/piastry/public/?p=cifs-2.6.git;a=commitdiff;h=11673376e6840c686f0c6e51892b16ad1945887e

that allows us to populate the page cache without making pages dirty. I didn't have enough time to test it well (that's why I haven't posted it yet) - going to do it tomorrow. But you are welcome to comment the approach.

The only thing that makes me wary here is that you're setting this flag on the inode. Could there ever be a situation where another task might be writing to this inode at the same time and needs to set them dirty?

Quoted 8 lines

If not, then I'm not sure I see the need for a new bool in the inode. It might be simpler to just check what sort of oplock you have in write_end instead.

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

There are some places where VFS code uses write_end call (through pagecache_write_end) but I didn't find any place where cifs code can hit it. So, I think we can assume now that cifs_write_end is called only from generic_file_aio_write codepath. If so, we can be sure that only on process may want to set pages dirty through cifs_write_end due to i_mutex lock.

It seems your are right and we can use clientCanCacheAll value directly from cifs_write_end - will make changes. Also, I think I can merge these two patches into one.

Thanks.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive
Quoted 87 lines · On Wed, 28 Nov 2012 16:12:56 +0400 Pavel Shilovsky…

2012/11/28 Jeff Layton <[email protected]>:

Quoted 70 lines
Quoted 53 lines · On Tue, 27 Nov 2012 18:57:22 +0400 Pavel Shilovsky…

2012/11/27 Jeff Layton <[email protected]>:

Quoted 44 lines
Quoted 28 lines · On Tue, 27 Nov 2012 10:50:28 +0400 Pavel Shilovsky…

If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by replacing it with filemap_write_and_wait call and sending non-page write in a error case.

While this may end up with two write requests to the server, we can be sure that our data will be the same at the server and the page cache - the next read on this file gets the valid data.

---
 fs/cifs/file.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/cifs/file.c +0 -3
index f8fe1bd..89efd85 100644
@@ -2511,14 +2511,12 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
       */
      if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) {
              ssize_t written;
-             int rc;
 
              written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-             rc = filemap_fdatawrite(inode->i_mapping);
-             if (rc)

fs/cifs/file.c cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, · line 2522

Quoted 10 lines
fs/cifs/file.c +4 -3
-                     return (ssize_t)rc;
-
-             return written;
+             /* try page write at first */
+             if (!filemap_write_and_wait(inode->i_mapping))
+                     return written;
+             /* page write failed - try from pos to pos+len-1 */
      }
 #endif
 

Bleh -- nasty. I guess this will work though...

Quoted 5 lines

Wonder if there's some way to populate the cache and then just mark the pages clean without sending out writes? That would be a better solution IMO, but I guess we can live with this for now...

Thanks for reviewing the patchset.

Quoted 9 lines

As for this patch I have the followon patch:

http://git.altlinux.org/people/piastry/public/?p=cifs-2.6.git;a=commitdiff;h=11673376e6840c686f0c6e51892b16ad1945887e

that allows us to populate the page cache without making pages dirty. I didn't have enough time to test it well (that's why I haven't posted it yet) - going to do it tomorrow. But you are welcome to comment the approach.

The only thing that makes me wary here is that you're setting this flag on the inode. Could there ever be a situation where another task might be writing to this inode at the same time and needs to set them dirty?

Quoted 8 lines

If not, then I'm not sure I see the need for a new bool in the inode. It might be simpler to just check what sort of oplock you have in write_end instead.

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

There are some places where VFS code uses write_end call (through pagecache_write_end) but I didn't find any place where cifs code can hit it. So, I think we can assume now that cifs_write_end is called only from generic_file_aio_write codepath. If so, we can be sure that only on process may want to set pages dirty through cifs_write_end due to i_mutex lock.

Quoted 6 lines

It seems your are right and we can use clientCanCacheAll value directly from cifs_write_end - will make changes. Also, I think I can merge these two patches into one.

Thanks.

Note though, that it matters what sort of cache= option is in force too. If cache=loose then you *do* want to set the page dirty.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

2012/11/28 Jeff Layton <[email protected]>:

Quoted 94 lines
Quoted 91 lines · On Wed, 28 Nov 2012 16:12:56 +0400 Pavel Shilovsky…

2012/11/28 Jeff Layton <[email protected]>:

Quoted 70 lines
Quoted 53 lines · On Tue, 27 Nov 2012 18:57:22 +0400 Pavel Shilovsky…

2012/11/27 Jeff Layton <[email protected]>:

Quoted 44 lines
Quoted 28 lines · On Tue, 27 Nov 2012 10:50:28 +0400 Pavel Shilovsky…

If we have a read oplock and set a read lock in it, we can't write to the locked area - so, filemap_fdatawrite may fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by replacing it with filemap_write_and_wait call and sending non-page write in a error case.

While this may end up with two write requests to the server, we can be sure that our data will be the same at the server and the page cache - the next read on this file gets the valid data.

---
 fs/cifs/file.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/cifs/file.c +0 -3
index f8fe1bd..89efd85 100644
@@ -2511,14 +2511,12 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
       */
      if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead) {
              ssize_t written;
-             int rc;
 
              written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-             rc = filemap_fdatawrite(inode->i_mapping);
-             if (rc)

fs/cifs/file.c cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, · line 2522

Quoted 10 lines
fs/cifs/file.c +4 -3
-                     return (ssize_t)rc;
-
-             return written;
+             /* try page write at first */
+             if (!filemap_write_and_wait(inode->i_mapping))
+                     return written;
+             /* page write failed - try from pos to pos+len-1 */
      }
 #endif
 

Bleh -- nasty. I guess this will work though...

Quoted 5 lines

Wonder if there's some way to populate the cache and then just mark the pages clean without sending out writes? That would be a better solution IMO, but I guess we can live with this for now...

Thanks for reviewing the patchset.

Quoted 9 lines

As for this patch I have the followon patch:

http://git.altlinux.org/people/piastry/public/?p=cifs-2.6.git;a=commitdiff;h=11673376e6840c686f0c6e51892b16ad1945887e

that allows us to populate the page cache without making pages dirty. I didn't have enough time to test it well (that's why I haven't posted it yet) - going to do it tomorrow. But you are welcome to comment the approach.

The only thing that makes me wary here is that you're setting this flag on the inode. Could there ever be a situation where another task might be writing to this inode at the same time and needs to set them dirty?

Quoted 8 lines

If not, then I'm not sure I see the need for a new bool in the inode. It might be simpler to just check what sort of oplock you have in write_end instead.

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

There are some places where VFS code uses write_end call (through pagecache_write_end) but I didn't find any place where cifs code can hit it. So, I think we can assume now that cifs_write_end is called only from generic_file_aio_write codepath. If so, we can be sure that only on process may want to set pages dirty through cifs_write_end due to i_mutex lock.

It seems your are right and we can use clientCanCacheAll value directly from cifs_write_end - will make changes. Also, I think I can merge these two patches into one.

Quoted 5 lines
Quoted 2 lines

Thanks.

Note though, that it matters what sort of cache= option is in force too. If cache=loose then you *do* want to set the page dirty.

This makes sense, thanks.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

2012/11/28 Jeff Layton <[email protected]>:

Quoted 3 lines

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

We can use cifs_writev for both POSIX and mandatory variants but I divided them to make POSIX variant work faster (no need to check hold a semaphore, walk through a lock list, etc).

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive
Quoted 9 lines · On Wed, 28 Nov 2012 17:55:41 +0400 Pavel Shilovsky…

2012/11/28 Jeff Layton <[email protected]>:

Quoted 3 lines

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

We can use cifs_writev for both POSIX and mandatory variants but I divided them to make POSIX variant work faster (no need to check hold a semaphore, walk through a lock list, etc).

Refresh my memory -- why do we need to handle writes differently when POSIX vs. non-POSIX locking is in force? It seems to me that that shouldn't matter and the behavior should be solely a function of what sort of oplock you have.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

2012/11/28 Jeff Layton <[email protected]>:

Quoted 13 lines
Quoted 9 lines · On Wed, 28 Nov 2012 17:55:41 +0400 Pavel Shilovsky…

2012/11/28 Jeff Layton <[email protected]>:

Quoted 3 lines

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

We can use cifs_writev for both POSIX and mandatory variants but I divided them to make POSIX variant work faster (no need to check hold a semaphore, walk through a lock list, etc).

Quoted 4 lines

Refresh my memory -- why do we need to handle writes differently when POSIX vs. non-POSIX locking is in force? It seems to me that that shouldn't matter and the behavior should be solely a function of what sort of oplock you have.

A write request can have conflict with mandatory locks set on a file. That's why we need to check for lock conflicts before issue the write/read.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive
Quoted 14 lines · On Wed, 28 Nov 2012 18:07:46 +0400 Pavel Shilovsky…

2012/11/28 Jeff Layton <[email protected]>:

Quoted 13 lines
Quoted 9 lines · On Wed, 28 Nov 2012 17:55:41 +0400 Pavel Shilovsky…

2012/11/28 Jeff Layton <[email protected]>:

Quoted 3 lines

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

We can use cifs_writev for both POSIX and mandatory variants but I divided them to make POSIX variant work faster (no need to check hold a semaphore, walk through a lock list, etc).

Quoted 9 lines
Quoted 4 lines

Refresh my memory -- why do we need to handle writes differently when POSIX vs. non-POSIX locking is in force? It seems to me that that shouldn't matter and the behavior should be solely a function of what sort of oplock you have.

A write request can have conflict with mandatory locks set on a file. That's why we need to check for lock conflicts before issue the write/read.

Hmmm...and the server might not know about the lock if it's cached. Fair enough.

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

Re: [PATCH 5/5] CIFS: Fix write after setting a read lock for read oplock files

In reply to a message not in the archive

2012/11/28 Jeff Layton <[email protected]>:

Quoted 22 lines
Quoted 19 lines · On Wed, 28 Nov 2012 18:07:46 +0400 Pavel Shilovsky…

2012/11/28 Jeff Layton <[email protected]>:

Quoted 17 lines
Quoted 9 lines · On Wed, 28 Nov 2012 17:55:41 +0400 Pavel Shilovsky…

2012/11/28 Jeff Layton <[email protected]>:

Quoted 3 lines

There's also a lot of logic around what sort of locking you're doing here too. I think we ought to do the same sort of I/O regardless of whether POSIX locks are being used or not.

We can use cifs_writev for both POSIX and mandatory variants but I divided them to make POSIX variant work faster (no need to check hold a semaphore, walk through a lock list, etc).

Refresh my memory -- why do we need to handle writes differently when POSIX vs. non-POSIX locking is in force? It seems to me that that shouldn't matter and the behavior should be solely a function of what sort of oplock you have.

Quoted 7 lines
Quoted 4 lines

A write request can have conflict with mandatory locks set on a file. That's why we need to check for lock conflicts before issue the write/read.

Hmmm...and the server might not know about the lock if it's cached. Fair enough.

Yes. Also there may be a situation when we have oplock level II and brlocks sent to the server (we can only cache brlock in exclusive oplock case): we can check locks locally too and don't send a write to the server if conflicts exist - save performance.

Signature · 2 lines
Best regards,
Pavel Shilovsky.