Skip to content

Commit b5efb97

Browse files
piastrySteve French
authored andcommitted
CIFS: Fix VFS lock usage for oplocked files
We can deadlock if we have a write oplock and two processes use the same file handle. In this case the first process can't unlock its lock if another process blocked on the lock in the same time. Fix this by removing lock_mutex protection from waiting on a blocked lock and protect only posix_lock_file call. Cc: stable@kernel.org Signed-off-by: Pavel Shilovsky <piastry@etersoft.ru> Acked-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: Steve French <sfrench@us.ibm.com>
1 parent fa2a451 commit b5efb97

1 file changed

Lines changed: 52 additions & 4 deletions

File tree

‎fs/cifs/file.c‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,21 @@ cifs_del_lock_waiters(struct cifsLockInfo *lock)
671671
}
672672
}
673673

674+
/*
675+
* Copied from fs/locks.c with small changes.
676+
* Remove waiter from blocker's block list.
677+
* When blocker ends up pointing to itself then the list is empty.
678+
*/
679+
static void
680+
cifs_locks_delete_block(struct file_lock *waiter)
681+
{
682+
lock_flocks();
683+
list_del_init(&waiter->fl_block);
684+
list_del_init(&waiter->fl_link);
685+
waiter->fl_next = NULL;
686+
unlock_flocks();
687+
}
688+
674689
static bool
675690
__cifs_find_lock_conflict(struct cifsInodeInfo *cinode, __u64 offset,
676691
__u64 length, __u8 type, __u16 netfid,
@@ -820,6 +835,39 @@ cifs_posix_lock_test(struct file *file, struct file_lock *flock)
820835
return rc;
821836
}
822837

838+
/* Called with locked lock_mutex, return with unlocked. */
839+
static int
840+
cifs_posix_lock_file_wait_locked(struct file *file, struct file_lock *flock)
841+
{
842+
struct cifsInodeInfo *cinode = CIFS_I(file->f_path.dentry->d_inode);
843+
int rc;
844+
845+
while (true) {
846+
rc = posix_lock_file(file, flock, NULL);
847+
mutex_unlock(&cinode->lock_mutex);
848+
if (rc != FILE_LOCK_DEFERRED)
849+
break;
850+
rc = wait_event_interruptible(flock->fl_wait, !flock->fl_next);
851+
if (!rc) {
852+
mutex_lock(&cinode->lock_mutex);
853+
continue;
854+
}
855+
cifs_locks_delete_block(flock);
856+
break;
857+
}
858+
return rc;
859+
}
860+
861+
static int
862+
cifs_posix_lock_file_wait(struct file *file, struct file_lock *flock)
863+
{
864+
struct cifsInodeInfo *cinode = CIFS_I(file->f_path.dentry->d_inode);
865+
866+
mutex_lock(&cinode->lock_mutex);
867+
/* lock_mutex will be released by the function below */
868+
return cifs_posix_lock_file_wait_locked(file, flock);
869+
}
870+
823871
/*
824872
* Set the byte-range lock (posix style). Returns:
825873
* 1) 0, if we set the lock and don't need to request to the server;
@@ -840,9 +888,9 @@ cifs_posix_lock_set(struct file *file, struct file_lock *flock)
840888
mutex_unlock(&cinode->lock_mutex);
841889
return rc;
842890
}
843-
rc = posix_lock_file_wait(file, flock);
844-
mutex_unlock(&cinode->lock_mutex);
845-
return rc;
891+
892+
/* lock_mutex will be released by the function below */
893+
return cifs_posix_lock_file_wait_locked(file, flock);
846894
}
847895

848896
static int
@@ -1338,7 +1386,7 @@ cifs_setlk(struct file *file, struct file_lock *flock, __u8 type,
13381386

13391387
out:
13401388
if (flock->fl_flags & FL_POSIX)
1341-
posix_lock_file_wait(file, flock);
1389+
cifs_posix_lock_file_wait(file, flock);
13421390
return rc;
13431391
}
13441392

0 commit comments

Comments
 (0)