Skip to contentSkip to conversation

CIFS: Make cifsFileInfo_put work with strict cache mode (try #4)

PATCH In review Pavel Shilovsky 2 participants 9 messages Dec 13, 2010 3 messages not in the archive

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

PATCH 1/7 CIFS: Make cifsFileInfo_put work with strict cache mode (try #4)

On strict cache mode when we close the last file handle of the inode we should set invalid_mapping flag on this inode to prevent data coherency problem when we open it again but it has been modified by other clients.

---
 fs/cifs/cifs_fs_sb.h |    1 +
 fs/cifs/file.c       |    8 ++++++++
 2 files changed, 9 insertions(+), 0 deletions(-)
fs/cifs/cifs_fs_sb.h +1 -0
index 7852cd6..ac51cd2 100644
@@ -40,6 +40,7 @@
 #define CIFS_MOUNT_FSCACHE	0x8000 /* local caching enabled */
 #define CIFS_MOUNT_MF_SYMLINKS	0x10000 /* Minshall+French Symlinks enabled */
 #define CIFS_MOUNT_MULTIUSER	0x20000 /* multiuser mount */
+#define CIFS_MOUNT_STRICT_IO	0x40000 /* strict cache mode */
 
 struct cifs_sb_info {
 	struct rb_root tlink_tree;
fs/cifs/file.c +8 -0
index 5a28660..60c3f84 100644
@@ -264,6 +264,7 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
 	struct inode *inode = cifs_file->dentry->d_inode;
 	struct cifsTconInfo *tcon = tlink_tcon(cifs_file->tlink);
 	struct cifsInodeInfo *cifsi = CIFS_I(inode);
+	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 	struct cifsLockInfo *li, *tmp;
 
 	spin_lock(&cifs_file_list_lock);
@@ -279,6 +280,13 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
 	if (list_empty(&cifsi->openFileList)) {
 		cFYI(1, "closing last open instance for inode %p",
 			cifs_file->dentry->d_inode);
+
+		/* in strict cache mode we need invalidate mapping on the last
+		   close  because it may cause a error when we open this 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;
+
 		cifs_set_oplock_level(cifsi, 0);
 	}
 	spin_unlock(&cifs_file_list_lock);
Signature · 1 line
1.7.3.2

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

#2 lore raw

PATCH 2/7 CIFS: Add strict cache file operations

Add skeleton read, write, fsync and mmap functions for strict cache mode into new strict file_ops structures.

---
 fs/cifs/cifsfs.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/cifs/cifsfs.h |    8 ++++++--
 fs/cifs/file.c   |   14 ++++++++++++++
 fs/cifs/inode.c  |    6 +++++-
 4 files changed, 79 insertions(+), 3 deletions(-)
fs/cifs/cifsfs.c +54 -0
index 3936aa7..a0e35d3 100644
@@ -572,6 +572,22 @@ cifs_do_mount(struct file_system_type *fs_type,
 	return dget(sb->s_root);
 }
 
+static ssize_t cifs_strict_read(struct kiocb *iocb, const struct iovec *iov,
+				unsigned long nr_segs, loff_t pos)
+{
+	ssize_t read = 0;
+	/* BB */
+	return read;
+}
+
+static ssize_t cifs_strict_write(struct kiocb *iocb, const struct iovec *iov,
+				 unsigned long nr_segs, loff_t pos)
+{
+	ssize_t written = 0;
+	/* BB */
+	return written;
+}
+
 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
 				   unsigned long nr_segs, loff_t pos)
 {
@@ -710,6 +726,25 @@ const struct file_operations cifs_file_ops = {
 	.setlease = cifs_setlease,
 };
 
+const struct file_operations cifs_file_strict_ops = {
+	.read = do_sync_read,
+	.write = do_sync_write,
+	.aio_read = cifs_strict_read,
+	.aio_write = cifs_strict_write,
+	.open = cifs_open,
+	.release = cifs_close,
+	.lock = cifs_lock,
+	.fsync = cifs_strict_fsync,
+	.flush = cifs_flush,
+	.mmap = cifs_file_strict_mmap,
+	.splice_read = generic_file_splice_read,
+	.llseek = cifs_llseek,
+#ifdef CONFIG_CIFS_POSIX
+	.unlocked_ioctl	= cifs_ioctl,
+#endif /* CONFIG_CIFS_POSIX */
+	.setlease = cifs_setlease,
+};
+
 const struct file_operations cifs_file_direct_ops = {
 	/* no aio, no readv -
 	   BB reevaluate whether they can be done with directio, no cache */
@@ -728,6 +763,7 @@ const struct file_operations cifs_file_direct_ops = {
 	.llseek = cifs_llseek,
 	.setlease = cifs_setlease,
 };
+
 const struct file_operations cifs_file_nobrl_ops = {
 	.read = do_sync_read,
 	.write = do_sync_write,
@@ -746,6 +782,24 @@ const struct file_operations cifs_file_nobrl_ops = {
 	.setlease = cifs_setlease,
 };
 
+const struct file_operations cifs_file_strict_nobrl_ops = {
+	.read = do_sync_read,
+	.write = do_sync_write,
+	.aio_read = cifs_strict_read,
+	.aio_write = cifs_strict_write,
+	.open = cifs_open,
+	.release = cifs_close,
+	.fsync = cifs_strict_fsync,
+	.flush = cifs_flush,
+	.mmap = cifs_file_strict_mmap,
+	.splice_read = generic_file_splice_read,
+	.llseek = cifs_llseek,
+#ifdef CONFIG_CIFS_POSIX
+	.unlocked_ioctl	= cifs_ioctl,
+#endif /* CONFIG_CIFS_POSIX */
+	.setlease = cifs_setlease,
+};
+
 const struct file_operations cifs_file_direct_nobrl_ops = {
 	/* no mmap, no aio, no readv -
 	   BB reevaluate whether they can be done with directio, no cache */
fs/cifs/cifsfs.h +6 -2
index 897b2b2..e47c00d 100644
@@ -72,8 +72,10 @@ extern const struct inode_operations cifs_dfs_referral_inode_operations;
 /* Functions related to files and directories */
 extern const struct file_operations cifs_file_ops;
 extern const struct file_operations cifs_file_direct_ops; /* if directio mnt */
-extern const struct file_operations cifs_file_nobrl_ops;
-extern const struct file_operations cifs_file_direct_nobrl_ops; /* no brlocks */
+extern const struct file_operations cifs_file_strict_ops; /* if strictio mnt */
+extern const struct file_operations cifs_file_nobrl_ops; /* no brlocks */
+extern const struct file_operations cifs_file_direct_nobrl_ops;
+extern const struct file_operations cifs_file_strict_nobrl_ops;
 extern int cifs_open(struct inode *inode, struct file *file);
 extern int cifs_close(struct inode *inode, struct file *file);
 extern int cifs_closedir(struct inode *inode, struct file *file);
@@ -83,8 +85,10 @@ extern ssize_t cifs_user_write(struct file *file, const char __user *write_data,
 			 size_t write_size, loff_t *poffset);
 extern int cifs_lock(struct file *, int, struct file_lock *);
 extern int cifs_fsync(struct file *, int);
+extern int cifs_strict_fsync(struct file *, int);
 extern int cifs_flush(struct file *, fl_owner_t id);
 extern int cifs_file_mmap(struct file * , struct vm_area_struct *);
+extern int cifs_file_strict_mmap(struct file * , struct vm_area_struct *);
 extern const struct file_operations cifs_dir_ops;
 extern int cifs_dir_open(struct inode *inode, struct file *file);
 extern int cifs_readdir(struct file *file, void *direntry, filldir_t filldir);
fs/cifs/file.c +14 -0
index 60c3f84..626cfa3 100644
@@ -1586,6 +1586,13 @@ static int cifs_write_end(struct file *file, struct address_space *mapping,
 	return rc;
 }
 
+int cifs_strict_fsync(struct file *file, int datasync)
+{
+	int rc = 0;
+	/* BB */
+	return rc;
+}
+
 int cifs_fsync(struct file *file, int datasync)
 {
 	int xid;
@@ -1807,6 +1814,13 @@ static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
 	return total_read;
 }
 
+int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	int rc = 0;
+	/* BB */
+	return rc;
+}
+
 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	int rc, xid;
fs/cifs/inode.c +5 -1
index 589f3e3..b245cf0 100644
@@ -44,13 +44,17 @@ static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
 				inode->i_fop = &cifs_file_direct_nobrl_ops;
 			else
 				inode->i_fop = &cifs_file_direct_ops;
+		} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
+			if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
+				inode->i_fop = &cifs_file_strict_nobrl_ops;
+			else
+				inode->i_fop = &cifs_file_strict_ops;
 		} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
 			inode->i_fop = &cifs_file_nobrl_ops;
 		else { /* not direct, send byte range locks */
 			inode->i_fop = &cifs_file_ops;
 		}
 
-
 		/* check if server can support readpages */
 		if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
 				PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
Signature · 1 line
1.7.3.2
#3 lore raw

PATCH 3/7 CIFS: Implement cifs_strict_fsync

In reply to a message not in the archive

Invalidate inode if we don't have at least Level II oplock in cifs_strict_fsync. Also remove filemap_write_and_wait call from cifs_fsync because it is previously called from vfs_fsync_range.

---
 fs/cifs/cifsfs.h |    1 +
 fs/cifs/file.c   |   33 +++++++++++++++++++++++----------
 fs/cifs/inode.c  |    2 +-
 3 files changed, 25 insertions(+), 11 deletions(-)
fs/cifs/cifsfs.h +1 -0
index e47c00d..657738f 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 +23 -10
index 626cfa3..938f26a 100644
@@ -1588,8 +1588,26 @@ static int cifs_write_end(struct file *file, struct address_space *mapping,
 
 int cifs_strict_fsync(struct file *file, int datasync)
 {
+	int xid;
 	int rc = 0;
-	/* BB */
+	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);
+
+	if (!CIFS_I(inode)->clientCanCacheRead)
+		cifs_invalidate_mapping(inode);
+
+	tcon = tlink_tcon(smbfile->tlink);
+	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
+		rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
+
+	FreeXid(xid);
 	return rc;
 }
 
@@ -1599,21 +1617,16 @@ int cifs_fsync(struct file *file, int datasync)
 	int rc = 0;
 	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(file->f_path.dentry->d_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);
-
-		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 b245cf0..a8f7cf3 100644
@@ -1683,7 +1683,7 @@ cifs_inode_needs_reval(struct inode *inode)
 /*
  * Zap the cache. Called when invalid_mapping flag is set.
  */
-static void
+void
 cifs_invalidate_mapping(struct inode *inode)
 {
 	int rc;
Signature · 1 line
1.7.3.2
#4 lore raw

PATCH 4/7 CIFS: Implement cifs_file_strict_mmap

In reply to a message not in the archive

Invalidate inode if we don't have at least Level II oplock.

---
 fs/cifs/file.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
fs/cifs/file.c +12 -2
index 938f26a..dd1e59a 100644
@@ -1829,8 +1829,16 @@ static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
 
 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
 {
-	int rc = 0;
-	/* BB */
+	int rc, xid;
+	struct inode *inode = file->f_path.dentry->d_inode;
+
+	xid = GetXid();
+
+	if (!CIFS_I(inode)->clientCanCacheRead)
+		cifs_invalidate_mapping(inode);
+
+	rc = generic_file_mmap(file, vma);
+	FreeXid(xid);
 	return rc;
 }
 
@@ -1839,12 +1847,14 @@ int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
 	int rc, xid;
 
 	xid = GetXid();
+
 	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
#5 lore raw

PATCH 5/7 CIFS: Implement cifs_strict_read

In reply to a message not in the archive

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

---
 fs/cifs/cifsfs.c    |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/cifs/cifsproto.h |    2 ++
 fs/cifs/file.c      |    4 ++--
 3 files changed, 52 insertions(+), 4 deletions(-)
fs/cifs/cifsfs.c +48 -2
index a0e35d3..e3f0024 100644
@@ -575,8 +575,54 @@ cifs_do_mount(struct file_system_type *fs_type,
 static ssize_t cifs_strict_read(struct kiocb *iocb, const struct iovec *iov,
 				unsigned long nr_segs, loff_t pos)
 {
-	ssize_t read = 0;
-	/* BB */
+	struct inode *inode;
+	struct cifs_sb_info *cifs_sb;
+	ssize_t read;
+	unsigned long i, len = 0;
+	char *buf, *offset;
+
+	inode = iocb->ki_filp->f_path.dentry->d_inode;
+	cifs_sb = CIFS_SB(iocb->ki_filp->f_path.dentry->d_sb);
+
+	if (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++)
+		len += iov[i].iov_len;
+
+	buf = kmalloc(len, GFP_KERNEL);
+
+	read = cifs_read(iocb->ki_filp, buf, len, &pos);
+	if (read < 0) {
+		kfree(buf);
+		return read;
+	}
+
+	for (i = 0, offset = buf, len = read; i < nr_segs && len; i++) {
+		unsigned long cur_len = min_t(unsigned long, len,
+					      iov[i].iov_len);
+
+		if (copy_to_user(iov[i].iov_base, offset, cur_len)) {
+			kfree(buf);
+			return -EFAULT;
+		}
+
+		len -= cur_len;
+		offset += cur_len;
+	}
+
+	iocb->ki_pos = pos;
+
+	kfree(buf);
 	return read;
 }
 
fs/cifs/cifsproto.h +2 -0
index e6d1481..05f6446 100644
@@ -79,6 +79,8 @@ extern int checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length);
 extern bool is_valid_oplock_break(struct smb_hdr *smb,
 				  struct TCP_Server_Info *);
 extern bool is_size_safe_to_change(struct cifsInodeInfo *, __u64 eof);
+extern ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
+			 loff_t *poffset);
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
 extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
 extern unsigned int smbCalcSize(struct smb_hdr *ptr);
fs/cifs/file.c +2 -2
index dd1e59a..1f96326 100644
@@ -1758,8 +1758,8 @@ ssize_t cifs_user_read(struct file *file, char __user *read_data,
 }
 
 
-static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
-	loff_t *poffset)
+ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
+		  loff_t *poffset)
 {
 	int rc = -EACCES;
 	unsigned int bytes_read = 0;
Signature · 1 line
1.7.3.2

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

#7 lore raw

Re: [PATCH 5/7] CIFS: Implement cifs_strict_read

Quoted 42 lines · On Mon, 13 Dec 2010 18:00:53 +0300 Pavel Shilovsky…

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

---
 fs/cifs/cifsfs.c    |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/cifs/cifsproto.h |    2 ++
 fs/cifs/file.c      |    4 ++--
 3 files changed, 52 insertions(+), 4 deletions(-)
fs/cifs/cifsfs.c +22 -2
index a0e35d3..e3f0024 100644
@@ -575,8 +575,54 @@ cifs_do_mount(struct file_system_type *fs_type,
 static ssize_t cifs_strict_read(struct kiocb *iocb, const struct iovec *iov,
 				unsigned long nr_segs, loff_t pos)
 {
-	ssize_t read = 0;
-	/* BB */
+	struct inode *inode;
+	struct cifs_sb_info *cifs_sb;
+	ssize_t read;
+	unsigned long i, len = 0;
+	char *buf, *offset;
+
+	inode = iocb->ki_filp->f_path.dentry->d_inode;
+	cifs_sb = CIFS_SB(iocb->ki_filp->f_path.dentry->d_sb);
+
+	if (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++)

fs/cifs/cifsfs.c cifs_do_mount(struct file_system_type *fs_type, · line 609

Quoted 10 lines
fs/cifs/cifsfs.c +10 -0
+		len += iov[i].iov_len;
+
+	buf = kmalloc(len, GFP_KERNEL);
+
+	read = cifs_read(iocb->ki_filp, buf, len, &pos);
+	if (read < 0) {
+		kfree(buf);
+		return read;
+	}
+

It's an improvement, but there sure is a lot of copying going on...

cifsd will call kernel_rcvmsg to copy the data from the receive buffer to the buffer it allocates. CIFSSMBRead will then memcpy from that buffer to your new kmalloc'ed buffer. Finally you'll copy that data from your kmalloced buffer to the buffer in userspace in the for loop below.

You could shortcut one of those copies by adding a CIFSSMBReadVec or something that can issue a read and fill multiple buffers.

Quoted 47 lines
fs/cifs/cifsfs.c +16 -0
+	for (i = 0, offset = buf, len = read; i < nr_segs && len; i++) {
+		unsigned long cur_len = min_t(unsigned long, len,
+					      iov[i].iov_len);
+
+		if (copy_to_user(iov[i].iov_base, offset, cur_len)) {
+			kfree(buf);
+			return -EFAULT;
+		}
+
+		len -= cur_len;
+		offset += cur_len;
+	}
+
+	iocb->ki_pos = pos;
+
+	kfree(buf);
 	return read;
 }
 
fs/cifs/cifsproto.h +2 -0
index e6d1481..05f6446 100644
@@ -79,6 +79,8 @@ extern int checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length);
 extern bool is_valid_oplock_break(struct smb_hdr *smb,
 				  struct TCP_Server_Info *);
 extern bool is_size_safe_to_change(struct cifsInodeInfo *, __u64 eof);
+extern ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
+			 loff_t *poffset);
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
 extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
 extern unsigned int smbCalcSize(struct smb_hdr *ptr);
fs/cifs/file.c +2 -2
index dd1e59a..1f96326 100644
@@ -1758,8 +1758,8 @@ ssize_t cifs_user_read(struct file *file, char __user *read_data,
 }
 
 
-static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
-	loff_t *poffset)
+ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
+		  loff_t *poffset)
 {
 	int rc = -EACCES;
 	unsigned int bytes_read = 0;
Signature · 1 line
Jeff Layton <[email protected]>
#8 lore raw

PATCH 6/7 CIFS: Implement cifs_strict_write

In reply to a message not in the archive

If we don't have Exclusive oplock we write a data to the server through cifs_write. Also set invalidate_mapping flag on the inode if we wrote something to the server.

---
 fs/cifs/cifsfs.c    |   58 ++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/cifs/cifsproto.h |    3 ++
 fs/cifs/file.c      |    5 +--
 3 files changed, 60 insertions(+), 6 deletions(-)
fs/cifs/cifsfs.c +55 -3
index e3f0024..d5a9eb9 100644
@@ -629,8 +629,53 @@ static ssize_t cifs_strict_read(struct kiocb *iocb, const struct iovec *iov,
 static ssize_t cifs_strict_write(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 written = 0;
-	/* BB */
+	unsigned long i, len = 0;
+	char *buf, *offset;
+
+	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);
+
+	/*
+	 * 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++)
+		len += iov[i].iov_len;
+
+	buf = kmalloc(len, GFP_KERNEL);
+
+	for (i = 0, offset = buf; i < nr_segs; i++) {
+		if (copy_from_user(offset, iov[i].iov_base, iov[i].iov_len)) {
+			kfree(buf);
+			return -EFAULT;
+		}
+
+		offset += iov[i].iov_len;
+	}
+
+	written = cifs_write((struct cifsFileInfo *)iocb->ki_filp->private_data,
+			     buf, len, &pos);
+	if (written < 0) {
+		kfree(buf);
+		return written;
+	}
+
+	iocb->ki_pos = pos;
+
+	if (written > 0)
+		CIFS_I(inode)->invalid_mapping = true;
+
+	kfree(buf);
 	return written;
 }
 
@@ -639,10 +684,17 @@ static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
 {
 	struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
 	ssize_t written;
+	int rc;
 
 	written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-	if (!CIFS_I(inode)->clientCanCacheAll)
-		filemap_fdatawrite(inode->i_mapping);
+
+	if (CIFS_I(inode)->clientCanCacheAll)
+		return written;
+
+	rc = filemap_fdatawrite(inode->i_mapping);
+	if (rc)
+		cFYI(1, "cifs_file_aio_write: %d rc on %p inode", rc, inode);
+
 	return written;
 }
 
fs/cifs/cifsproto.h +3 -0
index 05f6446..62d083f 100644
@@ -81,6 +81,9 @@ extern bool is_valid_oplock_break(struct smb_hdr *smb,
 extern bool is_size_safe_to_change(struct cifsInodeInfo *, __u64 eof);
 extern ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
 			 loff_t *poffset);
+extern ssize_t cifs_write(struct cifsFileInfo *open_file,
+			  const char *write_data, size_t write_size,
+			  loff_t *poffset);
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
 extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
 extern unsigned int smbCalcSize(struct smb_hdr *ptr);
fs/cifs/file.c +2 -3
index 1f96326..2f63885 100644
@@ -1026,9 +1026,8 @@ ssize_t cifs_user_write(struct file *file, const char __user *write_data,
 	return total_written;
 }
 
-static ssize_t cifs_write(struct cifsFileInfo *open_file,
-			  const char *write_data, size_t write_size,
-			  loff_t *poffset)
+ssize_t cifs_write(struct cifsFileInfo *open_file, const char *write_data,
+		   size_t write_size, loff_t *poffset)
 {
 	int rc = 0;
 	unsigned int bytes_written = 0;
Signature · 1 line
1.7.3.2

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

#10 lore raw

Re: [PATCH 6/7] CIFS: Implement cifs_strict_write

Quoted 35 lines · On Mon, 13 Dec 2010 18:00:54 +0300 Pavel Shilovsky…

If we don't have Exclusive oplock we write a data to the server through cifs_write. Also set invalidate_mapping flag on the inode if we wrote something to the server.

---
 fs/cifs/cifsfs.c    |   58 ++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/cifs/cifsproto.h |    3 ++
 fs/cifs/file.c      |    5 +--
 3 files changed, 60 insertions(+), 6 deletions(-)
fs/cifs/cifsfs.c +14 -1
index e3f0024..d5a9eb9 100644
@@ -629,8 +629,53 @@ static ssize_t cifs_strict_read(struct kiocb *iocb, const struct iovec *iov,
 static ssize_t cifs_strict_write(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 written = 0;
-	/* BB */
+	unsigned long i, len = 0;
+	char *buf, *offset;
+
+	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);
+
+	/*
+	 * In strict cache mode we need to write the data to the server exactly

fs/cifs/cifsfs.c static ssize_t cifs_strict_read(struct kiocb *iocb, const struct iovec *iov, · line 656

Quoted 10 lines
fs/cifs/cifsfs.c +10 -0
+	 * 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++)
+		len += iov[i].iov_len;
+
+	buf = kmalloc(len, GFP_KERNEL);
+

I'm always a little leery about allocating memory in the write codepath. It's a little less of a concern here since you're presumably not flushing writes in order to free up memory, but it would be better to avoid this...

We have a function called CIFSSMBWrite2 which can take an iovec and issue a write directly from it. It would probably be preferable to use that here. OTOH, there are apparently possible races involving signing -- if the write buffer changes after the signature is calculated then the server may disconnect the socket. I'm not sure what the right thing to do here is...

Anyone else have thoughts?

Quoted 75 lines
fs/cifs/cifsfs.c +31 -2
+	for (i = 0, offset = buf; i < nr_segs; i++) {
+		if (copy_from_user(offset, iov[i].iov_base, iov[i].iov_len)) {
+			kfree(buf);
+			return -EFAULT;
+		}
+
+		offset += iov[i].iov_len;
+	}
+
+	written = cifs_write((struct cifsFileInfo *)iocb->ki_filp->private_data,
+			     buf, len, &pos);
+	if (written < 0) {
+		kfree(buf);
+		return written;
+	}
+
+	iocb->ki_pos = pos;
+
+	if (written > 0)
+		CIFS_I(inode)->invalid_mapping = true;
+
+	kfree(buf);
 	return written;
 }
 
@@ -639,10 +684,17 @@ static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
 {
 	struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
 	ssize_t written;
+	int rc;
 
 	written = generic_file_aio_write(iocb, iov, nr_segs, pos);
-	if (!CIFS_I(inode)->clientCanCacheAll)
-		filemap_fdatawrite(inode->i_mapping);
+
+	if (CIFS_I(inode)->clientCanCacheAll)
+		return written;
+
+	rc = filemap_fdatawrite(inode->i_mapping);
+	if (rc)
+		cFYI(1, "cifs_file_aio_write: %d rc on %p inode", rc, inode);
+
 	return written;
 }
 
fs/cifs/cifsproto.h +3 -0
index 05f6446..62d083f 100644
@@ -81,6 +81,9 @@ extern bool is_valid_oplock_break(struct smb_hdr *smb,
 extern bool is_size_safe_to_change(struct cifsInodeInfo *, __u64 eof);
 extern ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
 			 loff_t *poffset);
+extern ssize_t cifs_write(struct cifsFileInfo *open_file,
+			  const char *write_data, size_t write_size,
+			  loff_t *poffset);
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
 extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
 extern unsigned int smbCalcSize(struct smb_hdr *ptr);
fs/cifs/file.c +2 -3
index 1f96326..2f63885 100644
@@ -1026,9 +1026,8 @@ ssize_t cifs_user_write(struct file *file, const char __user *write_data,
 	return total_written;
 }
 
-static ssize_t cifs_write(struct cifsFileInfo *open_file,
-			  const char *write_data, size_t write_size,
-			  loff_t *poffset)
+ssize_t cifs_write(struct cifsFileInfo *open_file, const char *write_data,
+		   size_t write_size, loff_t *poffset)
 {
 	int rc = 0;
 	unsigned int bytes_written = 0;
Signature · 1 line
Jeff Layton <[email protected]>
#11 lore raw

PATCH 7/7 CIFS: Add strictcache mount option

In reply to a message not in the archive

Use for switching on strict cache mode. In this mode the client reads from the cache all the time it has Oplock Level II, otherwise - read from the server. As for write - the client stores a data in the cache in Exclusive Oplock case, otherwise - write directly to the server.

---
 fs/cifs/README    |    5 +++++
 fs/cifs/connect.c |    5 +++++
 2 files changed, 10 insertions(+), 0 deletions(-)
fs/cifs/README +5 -0
index 46af99a..fe16835 100644
@@ -452,6 +452,11 @@ A partial list of the supported mount options follows:
 		if oplock (caching token) is granted and held. Note that
 		direct allows write operations larger than page size
 		to be sent to the server.
+  strictcache   Use for switching on strict cache mode. In this mode the
+		client read from the cache all the time it has Oplock Level II,
+		otherwise - read from the server. All written data are stored
+		in the cache, but if the client doesn't have Exclusive Oplock,
+		it writes the data to the server.
   acl   	Allow setfacl and getfacl to manage posix ACLs if server
 		supports them.  (default)
   noacl 	Do not allow setfacl and getfacl calls on this mount
fs/cifs/connect.c +5 -0
index cc1a860..877cb9e 100644
@@ -84,6 +84,7 @@ struct smb_vol {
 	bool no_xattr:1;   /* set if xattr (EA) support should be disabled*/
 	bool server_ino:1; /* use inode numbers from server ie UniqueId */
 	bool direct_io:1;
+	bool strict_io:1; /* strict cache behavior */
 	bool remap:1;      /* set to remap seven reserved chars in filenames */
 	bool posix_paths:1; /* unset to not ask for posix pathnames. */
 	bool no_linux_ext:1;
@@ -1357,6 +1358,8 @@ cifs_parse_mount_options(char *options, const char *devname,
 			vol->direct_io = 1;
 		} else if (strnicmp(data, "forcedirectio", 13) == 0) {
 			vol->direct_io = 1;
+		} else if (strnicmp(data, "strictcache", 11) == 0) {
+			vol->strict_io = 1;
 		} else if (strnicmp(data, "noac", 4) == 0) {
 			printk(KERN_WARNING "CIFS: Mount option noac not "
 				"supported. Instead set "
@@ -2614,6 +2617,8 @@ static void setup_cifs_sb(struct smb_vol *pvolume_info,
 	if (pvolume_info->multiuser)
 		cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
 					    CIFS_MOUNT_NO_PERM);
+	if (pvolume_info->strict_io)
+		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
 	if (pvolume_info->direct_io) {
 		cFYI(1, "mounting share using direct i/o");
 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
Signature · 1 line
1.7.3.2