relock file capability and locking fixes
Subsystems (2)
v2 cover relock file capability and locking fixes
This is version #2.
Changes since #1:
1) add new "CIFS: Do not permit write to a range mandatory locked with a read lock" patch,
2) fix "CIFS: Fix write after setting a read lock for read oplock files" patch with Jeff suggestions,
3) add reviewed tags for patches that got them.
Original description:
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 (6):
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: Do not permit write to a range mandatory locked with a read
lock
CIFS: Fix write after setting a read lock for read oplock files
fs/cifs/cifsproto.h | 2 +- fs/cifs/file.c | 167 +++++++++++++++++++++++++++++---------------------- fs/cifs/smb2file.c | 12 ---- 3 files changed, 95 insertions(+), 86 deletions(-)
Signature · 1 line
1.7.10.4
A message that is not in the archive (sent off-list or not yet received): 1354112625-30315-1-git-send-email-piastry-7qunaywFIewox3rIn2DAYQ@public.gmane.org
PATCH v2 1/6 CIFS: Separate pushing posix locks and lock_sem handling
--- fs/cifs/file.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-)
fs/cifs/file.c +19 -13
@@ -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
PATCH v2 2/6 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
@@ -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
@@ -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
PATCH v2 3/6 CIFS: Implement cifs_relock_file
In reply to a message not in the archivethat 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
@@ -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
PATCH v2 4/6 CIFS: Fix lock consistensy bug in cifs_setlk
In reply to a message not in the archiveIf 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
@@ -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
PATCH v2 5/6 CIFS: Do not permit write to a range mandatory locked with a read lock
In reply to a message not in the archiveWe don't need to permit a write to the area locked with a read lock by any process including the process that issues the write.
--- fs/cifs/cifsproto.h | 2 +- fs/cifs/file.c | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-)
fs/cifs/cifsproto.h +1 -1
@@ -187,7 +187,7 @@ extern void cifs_mark_open_files_invalid(struct cifs_tcon *tcon);extern bool cifs_find_lock_conflict (struct cifsFileInfo *cfile,__u64 offset,__u64 length,__u8 type,struct cifsLockInfo **conf_lock, -bool rw_check); +int rw_check);extern void cifs_add_pending_open (struct cifs_fid *fid,struct tcon_link *tlink,struct cifs_pending_open *open);
fs/cifs/file.c +18 -9
@@ -765,10 +765,15 @@ cifs_del_lock_waiters(struct cifsLockInfo *lock) } } +#define CIFS_LOCK_OP 0 +#define CIFS_READ_OP 1 +#define CIFS_WRITE_OP 2 + +/* @rw_check : 0 - no op, 1 - read, 2 - write */ static bool cifs_find_fid_lock_conflict (struct cifs_fid_locks *fdlocks,__u64 offset,__u64 length,__u8 type,struct cifsFileInfo *cfile, -struct cifsLockInfo **conf_lock,bool rw_check) +struct cifsLockInfo **conf_lock,int rw_check) {struct cifsLockInfo *li;struct cifsFileInfo *cur_cfile = fdlocks->cfile;
@@ -778,9 +783,13 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,if (offset + length <= li->offset || offset >= li->offset + li->length)continue ; -if (rw_check && server->ops->compare_fids (cfile, cur_cfile) && - current->tgid == li->pid) -continue ; +if (rw_check !=CIFS_LOCK_OP && current->tgid == li->pid && + server->ops->compare_fids (cfile, cur_cfile)) { +/* shared lock prevents write op through the same fid */ +if (!(li->type & server->vals->shared_lock_type) || + rw_check !=CIFS_WRITE_OP ) +continue ; + }if ((type & server->vals->shared_lock_type) && ((server->ops->compare_fids (cfile, cur_cfile) && current->tgid == li->pid) || type == li->type))
@@ -795,7 +804,7 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,bool cifs_find_lock_conflict (struct cifsFileInfo *cfile,__u64 offset,__u64 length,__u8 type,struct cifsLockInfo **conf_lock, -bool rw_check) +int rw_check) {bool rc = false;struct cifs_fid_locks *cur;
@@ -831,7 +840,7 @@ cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,down_read (&cinode->lock_sem); exist =cifs_find_lock_conflict (cfile, offset, length, type, - &conf_lock, false); + &conf_lock,CIFS_LOCK_OP );if (exist) { flock->fl_start = conf_lock->offset; flock->fl_end = conf_lock->offset + conf_lock->length -1 ;
@@ -878,7 +887,7 @@ try_again:down_write (&cinode->lock_sem); exist =cifs_find_lock_conflict (cfile, lock->offset, lock->length, - lock->type, &conf_lock, false); + lock->type, &conf_lock,CIFS_LOCK_OP );if (!exist && cinode->can_cache_brlcks) {list_add_tail (&lock->llist, &cfile->llist->locks);up_write (&cinode->lock_sem);
@@ -2472,7 +2481,7 @@ cifs_writev(struct kiocb *iocb, const struct iovec *iov,down_read (&cinode->lock_sem);if (!cifs_find_lock_conflict (cfile, pos,iov_length (iov, nr_segs), server->vals->exclusive_lock_type,NULL , - true)) { +CIFS_WRITE_OP )) {mutex_lock (&inode->i_mutex); rc =__generic_file_aio_write (iocb, iov, nr_segs, &iocb->ki_pos);
@@ -2907,7 +2916,7 @@ cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov,down_read (&cinode->lock_sem);if (!cifs_find_lock_conflict (cfile, pos,iov_length (iov, nr_segs), tcon->ses->server->vals->shared_lock_type, -NULL , true)) +NULL ,CIFS_READ_OP )) rc =generic_file_aio_read (iocb, iov, nr_segs, pos);up_read (&cinode->lock_sem);return rc;
Signature · 1 line
1.7.10.4
A message that is not in the archive (sent off-list or not yet received): 1354112625-30315-6-git-send-email-piastry-7qunaywFIewox3rIn2DAYQ@public.gmane.org
Jeff Layton
Reviewed-byRe: [PATCH v2 5/6] CIFS: Do not permit write to a range mandatory locked with a read lock
Quoted 97 lines · On Wed, 28 Nov 2012 18:23:44 +0400 Pavel Shilovsky…
We don't need to permit a write to the area locked with a read lock by any process including the process that issues the write.
Signed-off-by Pavel Shilovsky <[email protected]>--- fs/cifs/cifsproto.h | 2 +- fs/cifs/file.c | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-)fs/cifs/cifsproto.h +1
-1@@ -187,7 +187,7 @@ extern void cifs_mark_open_files_invalid(struct cifs_tcon *tcon);extern bool cifs_find_lock_conflict (struct cifsFileInfo *cfile,__u64 offset,__u64 length,__u8 type,struct cifsLockInfo **conf_lock, -bool rw_check); +int rw_check);extern void cifs_add_pending_open (struct cifs_fid *fid,struct tcon_link *tlink,struct cifs_pending_open *open);fs/cifs/file.c +17
-8@@ -765,10 +765,15 @@ cifs_del_lock_waiters(struct cifsLockInfo *lock) } } +#define CIFS_LOCK_OP 0 +#define CIFS_READ_OP 1 +#define CIFS_WRITE_OP 2 + +/* @rw_check : 0 - no op, 1 - read, 2 - write */ static bool cifs_find_fid_lock_conflict (struct cifs_fid_locks *fdlocks,__u64 offset,__u64 length,__u8 type,struct cifsFileInfo *cfile, -struct cifsLockInfo **conf_lock,bool rw_check) +struct cifsLockInfo **conf_lock,int rw_check) {struct cifsLockInfo *li;struct cifsFileInfo *cur_cfile = fdlocks->cfile;@@ -778,9 +783,13 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,if (offset + length <= li->offset || offset >= li->offset + li->length)continue ; -if (rw_check && server->ops->compare_fids (cfile, cur_cfile) && - current->tgid == li->pid) -continue ; +if (rw_check !=CIFS_LOCK_OP && current->tgid == li->pid && + server->ops->compare_fids (cfile, cur_cfile)) { +/* shared lock prevents write op through the same fid */ +if (!(li->type & server->vals->shared_lock_type) || + rw_check !=CIFS_WRITE_OP ) +continue ; + }if ((type & server->vals->shared_lock_type) && ((server->ops->compare_fids (cfile, cur_cfile) && current->tgid == li->pid) || type == li->type))@@ -795,7 +804,7 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,bool cifs_find_lock_conflict (struct cifsFileInfo *cfile,__u64 offset,__u64 length,__u8 type,struct cifsLockInfo **conf_lock, -bool rw_check) +int rw_check) {bool rc = false;struct cifs_fid_locks *cur;@@ -831,7 +840,7 @@ cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,down_read (&cinode->lock_sem); exist =cifs_find_lock_conflict (cfile, offset, length, type, - &conf_lock, false); + &conf_lock,CIFS_LOCK_OP );if (exist) { flock->fl_start = conf_lock->offset; flock->fl_end = conf_lock->offset + conf_lock->length -1 ;@@ -878,7 +887,7 @@ try_again:down_write (&cinode->lock_sem); exist =cifs_find_lock_conflict (cfile, lock->offset, lock->length, - lock->type, &conf_lock, false); + lock->type, &conf_lock,CIFS_LOCK_OP );if (!exist && cinode->can_cache_brlcks) {list_add_tail (&lock->llist, &cfile->llist->locks);up_write (&cinode->lock_sem);@@ -2472,7 +2481,7 @@ cifs_writev(struct kiocb *iocb, const struct iovec *iov,down_read (&cinode->lock_sem);if (!cifs_find_lock_conflict (cfile, pos,iov_length (iov, nr_segs), server->vals->exclusive_lock_type,NULL , - true)) { +CIFS_WRITE_OP )) {mutex_lock (&inode->i_mutex); rc =__generic_file_aio_write (iocb, iov, nr_segs, &iocb->ki_pos);
fs/cifs/file.c cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov, · line 2922
Quoted 8 lines
fs/cifs/file.c +1
-1@@ -2907,7 +2916,7 @@ cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov,down_read (&cinode->lock_sem);if (!cifs_find_lock_conflict (cfile, pos,iov_length (iov, nr_segs), tcon->ses->server->vals->shared_lock_type, -NULL , true)) +NULL ,CIFS_READ_OP )) rc =generic_file_aio_read (iocb, iov, nr_segs, pos);up_read (&cinode->lock_sem);return rc;
Code looks ok I guess. I'll have to take your word for it that the windows locking semantics here are correct though...
A message that is not in the archive (sent off-list or not yet received): 20121130135721.5fef027b-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org
Re: [PATCH v2 5/6] CIFS: Do not permit write to a range mandatory locked with a read lock
2012/11/30 Jeff Layton <[email protected]>:
Quoted 112 lines
Quoted 97 lines · On Wed, 28 Nov 2012 18:23:44 +0400 Pavel Shilovsky…
We don't need to permit a write to the area locked with a read lock by any process including the process that issues the write.
Signed-off-by Pavel Shilovsky <[email protected]>--- fs/cifs/cifsproto.h | 2 +- fs/cifs/file.c | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-)fs/cifs/cifsproto.h +1
-1@@ -187,7 +187,7 @@ extern void cifs_mark_open_files_invalid(struct cifs_tcon *tcon);extern bool cifs_find_lock_conflict (struct cifsFileInfo *cfile,__u64 offset,__u64 length,__u8 type,struct cifsLockInfo **conf_lock, -bool rw_check); +int rw_check);extern void cifs_add_pending_open (struct cifs_fid *fid,struct tcon_link *tlink,struct cifs_pending_open *open);fs/cifs/file.c +17
-8@@ -765,10 +765,15 @@ cifs_del_lock_waiters(struct cifsLockInfo *lock) } } +#define CIFS_LOCK_OP 0 +#define CIFS_READ_OP 1 +#define CIFS_WRITE_OP 2 + +/* @rw_check : 0 - no op, 1 - read, 2 - write */ static bool cifs_find_fid_lock_conflict (struct cifs_fid_locks *fdlocks,__u64 offset,__u64 length,__u8 type,struct cifsFileInfo *cfile, -struct cifsLockInfo **conf_lock,bool rw_check) +struct cifsLockInfo **conf_lock,int rw_check) {struct cifsLockInfo *li;struct cifsFileInfo *cur_cfile = fdlocks->cfile;@@ -778,9 +783,13 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,if (offset + length <= li->offset || offset >= li->offset + li->length)continue ; -if (rw_check && server->ops->compare_fids (cfile, cur_cfile) && - current->tgid == li->pid) -continue ; +if (rw_check !=CIFS_LOCK_OP && current->tgid == li->pid && + server->ops->compare_fids (cfile, cur_cfile)) { +/* shared lock prevents write op through the same fid */ +if (!(li->type & server->vals->shared_lock_type) || + rw_check !=CIFS_WRITE_OP ) +continue ; + }if ((type & server->vals->shared_lock_type) && ((server->ops->compare_fids (cfile, cur_cfile) && current->tgid == li->pid) || type == li->type))@@ -795,7 +804,7 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,bool cifs_find_lock_conflict (struct cifsFileInfo *cfile,__u64 offset,__u64 length,__u8 type,struct cifsLockInfo **conf_lock, -bool rw_check) +int rw_check) {bool rc = false;struct cifs_fid_locks *cur;@@ -831,7 +840,7 @@ cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,down_read (&cinode->lock_sem); exist =cifs_find_lock_conflict (cfile, offset, length, type, - &conf_lock, false); + &conf_lock,CIFS_LOCK_OP );if (exist) { flock->fl_start = conf_lock->offset; flock->fl_end = conf_lock->offset + conf_lock->length -1 ;@@ -878,7 +887,7 @@ try_again:down_write (&cinode->lock_sem); exist =cifs_find_lock_conflict (cfile, lock->offset, lock->length, - lock->type, &conf_lock, false); + lock->type, &conf_lock,CIFS_LOCK_OP );if (!exist && cinode->can_cache_brlcks) {list_add_tail (&lock->llist, &cfile->llist->locks);up_write (&cinode->lock_sem);@@ -2472,7 +2481,7 @@ cifs_writev(struct kiocb *iocb, const struct iovec *iov,down_read (&cinode->lock_sem);if (!cifs_find_lock_conflict (cfile, pos,iov_length (iov, nr_segs), server->vals->exclusive_lock_type,NULL , - true)) { +CIFS_WRITE_OP )) {mutex_lock (&inode->i_mutex); rc =__generic_file_aio_write (iocb, iov, nr_segs, &iocb->ki_pos);fs/cifs/file.c cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov, · line 2922
Quoted 8 lines
fs/cifs/file.c +1
-1@@ -2907,7 +2916,7 @@ cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov,down_read (&cinode->lock_sem);if (!cifs_find_lock_conflict (cfile, pos,iov_length (iov, nr_segs), tcon->ses->server->vals->shared_lock_type, -NULL , true)) +NULL ,CIFS_READ_OP )) rc =generic_file_aio_read (iocb, iov, nr_segs, pos);up_read (&cinode->lock_sem);return rc;Code looks ok I guess. I'll have to take your word for it that the windows locking semantics here are correct though...
Quoted 1 line
Reviewed-by Jeff Layton <[email protected]>
It was taken from here and proved with testing:
"Locking a portion of a file for shared access denies all processes
write access to the specified region of the file, including the
process that first locks the region. All processes can read the locked
region." (from http://msdn.microsoft.com/ru-ru/library/windows/desktop/aa365203(v=vs.85).aspx).
Signature · 2 lines
Best regards, Pavel Shilovsky.
A message that is not in the archive (sent off-list or not yet received): CAKywueRtuz=pHWoa7fRkwyrOPsuOSS8MO7rhqVzKtd0SXVRfYQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Re: [PATCH v2 5/6] CIFS: Do not permit write to a range mandatory locked with a read lock
Just to doublecheck semantics ... posix applications (on the Linux kernel client) want to always allow a write where possible - if Wine issues a mandatory lock on a cached file do we want it to apply to posix applications or just applications like Wine? Is the only way we can tell the difference the mount option you suggested?
Quoted 128 lines · Pavel Shilovsky
2012/11/30 Jeff Layton <[email protected]>:
Quoted 112 lines
Quoted 97 lines · On Wed, 28 Nov 2012 18:23:44 +0400 Pavel Shilovsky…
We don't need to permit a write to the area locked with a read lock by any process including the process that issues the write.
Signed-off-by Pavel Shilovsky <[email protected]>--- fs/cifs/cifsproto.h | 2 +- fs/cifs/file.c | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-)fs/cifs/cifsproto.h +1
-1@@ -187,7 +187,7 @@ extern void cifs_mark_open_files_invalid(struct cifs_tcon *tcon);extern bool cifs_find_lock_conflict (struct cifsFileInfo *cfile,__u64 offset,__u64 length,__u8 type,struct cifsLockInfo **conf_lock, -bool rw_check); +int rw_check);extern void cifs_add_pending_open (struct cifs_fid *fid,struct tcon_link *tlink,struct cifs_pending_open *open);fs/cifs/file.c +17
-8@@ -765,10 +765,15 @@ cifs_del_lock_waiters(struct cifsLockInfo *lock) } } +#define CIFS_LOCK_OP 0 +#define CIFS_READ_OP 1 +#define CIFS_WRITE_OP 2 + +/* @rw_check : 0 - no op, 1 - read, 2 - write */ static bool cifs_find_fid_lock_conflict (struct cifs_fid_locks *fdlocks,__u64 offset,__u64 length,__u8 type,struct cifsFileInfo *cfile, -struct cifsLockInfo **conf_lock,bool rw_check) +struct cifsLockInfo **conf_lock,int rw_check) {struct cifsLockInfo *li;struct cifsFileInfo *cur_cfile = fdlocks->cfile;@@ -778,9 +783,13 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,if (offset + length <= li->offset || offset >= li->offset + li->length)continue ; -if (rw_check && server->ops->compare_fids (cfile, cur_cfile) && - current->tgid == li->pid) -continue ; +if (rw_check !=CIFS_LOCK_OP && current->tgid == li->pid && + server->ops->compare_fids (cfile, cur_cfile)) { +/* shared lock prevents write op through the same fid */ +if (!(li->type & server->vals->shared_lock_type) || + rw_check !=CIFS_WRITE_OP ) +continue ; + }if ((type & server->vals->shared_lock_type) && ((server->ops->compare_fids (cfile, cur_cfile) && current->tgid == li->pid) || type == li->type))@@ -795,7 +804,7 @@ cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,bool cifs_find_lock_conflict (struct cifsFileInfo *cfile,__u64 offset,__u64 length,__u8 type,struct cifsLockInfo **conf_lock, -bool rw_check) +int rw_check) {bool rc = false;struct cifs_fid_locks *cur;@@ -831,7 +840,7 @@ cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,down_read (&cinode->lock_sem); exist =cifs_find_lock_conflict (cfile, offset, length, type, - &conf_lock, false); + &conf_lock,CIFS_LOCK_OP );if (exist) { flock->fl_start = conf_lock->offset; flock->fl_end = conf_lock->offset + conf_lock->length -1 ;@@ -878,7 +887,7 @@ try_again:down_write (&cinode->lock_sem); exist =cifs_find_lock_conflict (cfile, lock->offset, lock->length, - lock->type, &conf_lock, false); + lock->type, &conf_lock,CIFS_LOCK_OP );if (!exist && cinode->can_cache_brlcks) {list_add_tail (&lock->llist, &cfile->llist->locks);up_write (&cinode->lock_sem);@@ -2472,7 +2481,7 @@ cifs_writev(struct kiocb *iocb, const struct iovec *iov,down_read (&cinode->lock_sem);if (!cifs_find_lock_conflict (cfile, pos,iov_length (iov, nr_segs), server->vals->exclusive_lock_type,NULL , - true)) { +CIFS_WRITE_OP )) {mutex_lock (&inode->i_mutex); rc =__generic_file_aio_write (iocb, iov, nr_segs, &iocb->ki_pos);fs/cifs/file.c cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov, · line 2922
Quoted 8 lines
fs/cifs/file.c +1
-1@@ -2907,7 +2916,7 @@ cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov,down_read (&cinode->lock_sem);if (!cifs_find_lock_conflict (cfile, pos,iov_length (iov, nr_segs), tcon->ses->server->vals->shared_lock_type, -NULL , true)) +NULL ,CIFS_READ_OP )) rc =generic_file_aio_read (iocb, iov, nr_segs, pos);up_read (&cinode->lock_sem);return rc;Code looks ok I guess. I'll have to take your word for it that the windows locking semantics here are correct though...
Quoted 1 line
Reviewed-by Jeff Layton <[email protected]>It was taken from here and proved with testing:
"Locking a portion of a file for shared access denies all processes
write access to the specified region of the file, including the
process that first locks the region. All processes can read the locked
region." (from http://msdn.microsoft.com/ru-ru/library/windows/desktop/aa365203(v=vs.85).aspx).--
Best regards,
Pavel Shilovsky.
--
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
A message that is not in the archive (sent off-list or not yet received): CAH2r5mut7i4g5qtrt3chf_nO6PEQoHYecoqKpkfwS7cPAaMkFA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Re: [PATCH v2 5/6] CIFS: Do not permit write to a range mandatory locked with a read lock
2012/12/3 Steve French <[email protected]>:
Quoted 6 lines
Just to doublecheck semantics ... posix applications (on the Linux kernel client) want to always allow a write where possible - if Wine issues a mandatory lock on a cached file do we want it to apply to posix applications or just applications like Wine? Is the only way we can tell the difference the mount option you suggested?
This change is not targeted to fix Wine-specific behavior. If we negotiate mandatory locking we need to be sure the server and client are the same: the server denied writes to the range locked with read lock - we should do the same things on the client to make the behavior not depend on the kind of oplock we have now.
As for wine mount option: now, when we have rwpidforward and forcemand mount options, I don't think we need it because all that it should do is rwpidforward + forcemand + strictcache.
Signature · 2 lines
Best regards, Pavel Shilovsky.
PATCH v2 6/6 CIFS: Fix write after setting a read lock for read oplock files
In reply to a message not in the archiveIf 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 checking for brlocks, populating the page cache without marking affected pages dirty and directly writing to the server.
Also remove CONFIG_CIFS_SMB2 ifdefs because it's suitable for both CIFS and SMB2 protocols.
--- fs/cifs/file.c | 63 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 30 deletions(-)
fs/cifs/file.c +33 -30
@@ -2109,7 +2109,14 @@ static int cifs_write_end(struct file *file, struct address_space *mapping, }else { rc = copied; pos += copied; -set_page_dirty (page); +/* +* If we don't have write oplock and use the strict cache mode, +* we can leave pages clean - cifs_strict_writev will send a +* data to the server itself. +*/ +if (CIFS_I (inode)->clientCanCacheAll || + !(cifs_sb->mnt_cifs_flags &CIFS_MOUNT_STRICT_IO )) +set_page_dirty (page); }if (rc >0 ) {
@@ -2460,8 +2467,8 @@ ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov, }static ssize_t -cifs_writev (struct kiocb *iocb,const struct iovec *iov, -unsigned long nr_segs,loff_t pos) +cifs_pagecache_writev (struct kiocb *iocb,const struct iovec *iov, +unsigned long nr_segs,loff_t pos) {struct file *file = iocb->ki_filp;struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
@@ -2511,25 +2518,26 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,struct cifsFileInfo *cfile = (struct cifsFileInfo *) iocb->ki_filp->private_data;struct cifs_tcon *tcon =tlink_tcon (cfile->tlink); +ssize_t written; -#ifdef CONFIG_CIFS_SMB2 -/* -* If we have an oplock for read and want to write a data to the file -* we need to store it in the page cache and then push it to the server -* to be sure the next read will get a valid data. -*/ -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; +/* we have a read oplock - need to store a data in the page cache */ +if (cinode->clientCanCacheRead) { +if (cap_unix (tcon->ses) && + ((cifs_sb->mnt_cifs_flags &CIFS_MOUNT_NOPOSIXBRL ) ==0 ) && + (CIFS_UNIX_FCNTL_CAP &le64_to_cpu ( + tcon->fsUnixInfo.Capability))) + written =generic_file_aio_write (iocb, iov, nr_segs, + pos); +else + written =cifs_pagecache_writev (iocb, iov, nr_segs, + pos); +/* +* Errors occured during writing or we have read+write oplock - +* no need to flush to the server. +*/ +if (written <0 || cinode->clientCanCacheAll) +return written; } -#endif /* * For non-oplocked files in strict cache mode we need to write the data
@@ -2537,16 +2545,11 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,* 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. */ - -if (!cinode->clientCanCacheAll) -return cifs_user_writev (iocb, iov, nr_segs, pos); - -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 generic_file_aio_write (iocb, iov, nr_segs, pos); - -return cifs_writev (iocb, iov, nr_segs, pos); + written =cifs_user_writev (iocb, iov, nr_segs, pos); +/* need to restore pos if errors occured */ +if (written <0 ) + iocb->ki_pos = pos; +return written; } static struct cifs_readdata *
Signature · 1 line
1.7.10.4
A message that is not in the archive (sent off-list or not yet received): 1354112625-30315-7-git-send-email-piastry-7qunaywFIewox3rIn2DAYQ@public.gmane.org
Jeff Layton
Acked-byRe: [PATCH v2 6/6] CIFS: Fix write after setting a read lock for read oplock files
Quoted 101 lines · On Wed, 28 Nov 2012 18:23:45 +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_fdatawritemay fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by checking for brlocks, populating the page cache without marking affected pages dirty and directly writing to the server.Also remove
CONFIG_CIFS_SMB2ifdefs because it's suitable for both CIFS and SMB2 protocols.Signed-off-by Pavel Shilovsky <[email protected]>--- fs/cifs/file.c | 63 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 30 deletions(-)fs/cifs/file.c +28
-28@@ -2109,7 +2109,14 @@ static int cifs_write_end(struct file *file, struct address_space *mapping, }else { rc = copied; pos += copied; -set_page_dirty (page); +/* +* If we don't have write oplock and use the strict cache mode, +* we can leave pages clean - cifs_strict_writev will send a +* data to the server itself. +*/ +if (CIFS_I (inode)->clientCanCacheAll || + !(cifs_sb->mnt_cifs_flags &CIFS_MOUNT_STRICT_IO )) +set_page_dirty (page); }if (rc >0 ) {@@ -2460,8 +2467,8 @@ ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov, }static ssize_t -cifs_writev (struct kiocb *iocb,const struct iovec *iov, -unsigned long nr_segs,loff_t pos) +cifs_pagecache_writev (struct kiocb *iocb,const struct iovec *iov, +unsigned long nr_segs,loff_t pos) {struct file *file = iocb->ki_filp;struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;@@ -2511,25 +2518,26 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,struct cifsFileInfo *cfile = (struct cifsFileInfo *) iocb->ki_filp->private_data;struct cifs_tcon *tcon =tlink_tcon (cfile->tlink); +ssize_t written; -#ifdef CONFIG_CIFS_SMB2 -/* -* If we have an oplock for read and want to write a data to the file -* we need to store it in the page cache and then push it to the server -* to be sure the next read will get a valid data. -*/ -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; +/* we have a read oplock - need to store a data in the page cache */ +if (cinode->clientCanCacheRead) { +if (cap_unix (tcon->ses) && + ((cifs_sb->mnt_cifs_flags &CIFS_MOUNT_NOPOSIXBRL ) ==0 ) && + (CIFS_UNIX_FCNTL_CAP &le64_to_cpu ( + tcon->fsUnixInfo.Capability))) + written =generic_file_aio_write (iocb, iov, nr_segs, + pos); +else + written =cifs_pagecache_writev (iocb, iov, nr_segs, + pos); +/* +* Errors occured during writing or we have read+write oplock - +* no need to flush to the server. +*/ +if (written <0 || cinode->clientCanCacheAll) +return written; } -#endif /* * For non-oplocked files in strict cache mode we need to write the data @@ -2537,16 +2545,11 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,* 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. */ - -if (!cinode->clientCanCacheAll) -return cifs_user_writev (iocb, iov, nr_segs, pos); - -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 generic_file_aio_write (iocb, iov, nr_segs, pos);
fs/cifs/file.c cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, · line 2555
Quoted 10 lines
fs/cifs/file.c +5
-2- -return cifs_writev (iocb, iov, nr_segs, pos); + written =cifs_user_writev (iocb, iov, nr_segs, pos); +/* need to restore pos if errors occured */ +if (written <0 ) + iocb->ki_pos = pos; +return written; } static struct cifs_readdata *
Looks reasonable...
PATCH v3 6/6 CIFS: Fix write after setting a read lock for read oplock files
In reply to a message not in the archiveIf 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 checking for brlocks, populating the page cache without marking affected pages dirty and directly writing to the server.
Also remove CONFIG_CIFS_SMB2 ifdefs because it's suitable for both CIFS and SMB2 protocols.
--- fs/cifs/cifsfs.c | 1 + fs/cifs/cifsglob.h | 1 + fs/cifs/file.c | 82 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 53 insertions(+), 31 deletions(-)
fs/cifs/cifsfs.c +1 -0
@@ -229,6 +229,7 @@ cifs_alloc_inode(struct super_block *sb)cifs_set_oplock_level (cifs_inode,0 ); cifs_inode->delete_pending = false; cifs_inode->invalid_mapping = false; + cifs_inode->leave_pages_clean = false; 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 +1 -0
@@ -1037,6 +1037,7 @@ struct cifsInodeInfo {bool clientCanCacheAll;/* read and writebehind oplock */ bool delete_pending;/* DELETE_ON_CLOSE is set */ bool invalid_mapping;/* pagecache is invalid */ +bool leave_pages_clean;/* protected by i_mutex, not set pages dirty */ 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 +51 -31
115 lines not on this page · raw message
Signature · 1 line
1.7.10.4
A message that is not in the archive (sent off-list or not yet received): 1354536945-9593-1-git-send-email-piastry-7qunaywFIewox3rIn2DAYQ@public.gmane.org
Re: [PATCH v3 6/6] CIFS: Fix write after setting a read lock for read oplock files
2012/12/3 Pavel Shilovsky <[email protected]>:
Quoted 165 lines
If we have a read oplock and set a read lock in it, we can't write to the locked area - so,
filemap_fdatawritemay fail with a no information for a userspace application even if we request a write to non-locked area. Fix this by checking for brlocks, populating the page cache without marking affected pages dirty and directly writing to the server.Also remove
CONFIG_CIFS_SMB2ifdefs because it's suitable for both CIFS and SMB2 protocols.Signed-off-by Pavel Shilovsky <[email protected]>--- fs/cifs/cifsfs.c | 1 + fs/cifs/cifsglob.h | 1 + fs/cifs/file.c | 82 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 53 insertions(+), 31 deletions(-)fs/cifs/cifsfs.c +1
-0@@ -229,6 +229,7 @@ cifs_alloc_inode(struct super_block *sb)cifs_set_oplock_level (cifs_inode,0 ); cifs_inode->delete_pending = false; cifs_inode->invalid_mapping = false; + cifs_inode->leave_pages_clean = false; 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 +1
-0@@ -1037,6 +1037,7 @@ struct cifsInodeInfo {bool clientCanCacheAll;/* read and writebehind oplock */ bool delete_pending;/* DELETE_ON_CLOSE is set */ bool invalid_mapping;/* pagecache is invalid */ +bool leave_pages_clean;/* protected by i_mutex, not set pages dirty */ 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 +51
-32@@ -2109,7 +2109,15 @@ static int cifs_write_end(struct file *file, struct address_space *mapping, }else { rc = copied; pos += copied; -set_page_dirty (page); +/* +* When we use strict cache mode and cifs_strict_writev was run +* with level II oplock (indicated by leave_pages_clean field of +* CIFS_I(inode)), we can leave pages clean and let +* cifs_strict_writev send a new data to the server itself. +*/ +if (!CIFS_I (inode)->leave_pages_clean || + !(cifs_sb->mnt_cifs_flags &CIFS_MOUNT_STRICT_IO )) +set_page_dirty (page); }if (rc >0 ) {@@ -2460,8 +2468,8 @@ ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov, }static ssize_t -cifs_writev (struct kiocb *iocb,const struct iovec *iov, -unsigned long nr_segs,loff_t pos) +cifs_pagecache_writev (struct kiocb *iocb,const struct iovec *iov, +unsigned long nr_segs,loff_t pos,bool cache_all) {struct file *file = iocb->ki_filp;struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;@@ -2483,8 +2491,12 @@ cifs_writev(struct kiocb *iocb, const struct iovec *iov, server->vals->exclusive_lock_type, NULL, CIFS_WRITE_OP)) {mutex_lock (&inode->i_mutex); +if (!cache_all) + cinode->leave_pages_clean = true; rc =__generic_file_aio_write (iocb, iov, nr_segs, - &iocb->ki_pos); + &iocb->ki_pos); +if (!cache_all) + cinode->leave_pages_clean = false;mutex_unlock (&inode->i_mutex); }@@ -2511,25 +2523,38 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,struct cifsFileInfo *cfile = (struct cifsFileInfo *) iocb->ki_filp->private_data;struct cifs_tcon *tcon =tlink_tcon (cfile->tlink); +ssize_t written; -#ifdef CONFIG_CIFS_SMB2 -/* -* If we have an oplock for read and want to write a data to the file -* we need to store it in the page cache and then push it to the server -* to be sure the next read will get a valid data. -*/ -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; +/* we have a read oplock - need to store a data in the page cache */ +if (cinode->clientCanCacheRead) { +/* +* We need to store clientCanCacheAll here to prevent race +* conditions - this value can be changed during an execution +* of generic_file_aio_write. For CIFS it can be changed from +* true to false only, but for SMB2 it can be changed both from +* true to false and vice versa. So, we can end up with a data +* stored in the cache, not marked dirty and not sent to the +* server if this value changes its state from false to true +* after cifs_write_end. +*/ +bool cache_all = cinode->clientCanCacheAll; + +if (cap_unix (tcon->ses) && + ((cifs_sb->mnt_cifs_flags &CIFS_MOUNT_NOPOSIXBRL ) ==0 ) && + (CIFS_UNIX_FCNTL_CAP &le64_to_cpu ( + tcon->fsUnixInfo.Capability))) + written =generic_file_aio_write (iocb, iov, nr_segs, + pos); +else + written =cifs_pagecache_writev (iocb, iov, nr_segs, pos, + cache_all); +/* +* Errors occured during writing or we have read+write oplock - +* no need to flush to the server. +*/ +if (written <0 || cache_all) +return written; } -#endif /* * For non-oplocked files in strict cache mode we need to write the data @@ -2537,16 +2562,11 @@ cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,* 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. */ - -if (!cinode->clientCanCacheAll) -return cifs_user_writev (iocb, iov, nr_segs, pos); - -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 generic_file_aio_write (iocb, iov, nr_segs, pos); - -return cifs_writev (iocb, iov, nr_segs, pos); + written =cifs_user_writev (iocb, iov, nr_segs, pos); +/* need to restore pos if errors occured */ +if (written <0 ) + iocb->ki_pos = pos; +return written; } static struct cifs_readdata * --
Quoted 2 lines
1.7.10.4
This version fixes race conditions when oplock changes its state from level II to exclusive during generic_file_aio_write after cifs_write_end - this end up with a new data stored in the page cache, marked non-dirty and not sent to the server (possible for SMB2 protocol).
Signature · 2 lines
Best regards, Pavel Shilovsky.