Commit 1c8c601a authored by Jeff Layton's avatar Jeff Layton Committed by Al Viro

locks: protect most of the file_lock handling with i_lock

Having a global lock that protects all of this code is a clear
scalability problem. Instead of doing that, move most of the code to be
protected by the i_lock instead. The exceptions are the global lists
that the ->fl_link sits on, and the ->fl_block list.

->fl_link is what connects these structures to the
global lists, so we must ensure that we hold those locks when iterating
over or updating these lists.

Furthermore, sound deadlock detection requires that we hold the
blocked_list state steady while checking for loops. We also must ensure
that the search and update to the list are atomic.

For the checking and insertion side of the blocked_list, push the
acquisition of the global lock into __posix_lock_file and ensure that
checking and update of the  blocked_list is done without dropping the
lock in between.

On the removal side, when waking up blocked lock waiters, take the
global lock before walking the blocked list and dequeue the waiters from
the global list prior to removal from the fl_block list.

With this, deadlock detection should be race free while we minimize
excessive file_lock_lock thrashing.

Finally, in order to avoid a lock inversion problem when handling
/proc/locks output we must ensure that manipulations of the fl_block
list are also protected by the file_lock_lock.
Signed-off-by: default avatarJeff Layton <jlayton@redhat.com>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 88974691
...@@ -342,7 +342,7 @@ prototypes: ...@@ -342,7 +342,7 @@ prototypes:
locking rules: locking rules:
file_lock_lock may block inode->i_lock may block
fl_copy_lock: yes no fl_copy_lock: yes no
fl_release_private: maybe no fl_release_private: maybe no
...@@ -355,12 +355,19 @@ prototypes: ...@@ -355,12 +355,19 @@ prototypes:
int (*lm_change)(struct file_lock **, int); int (*lm_change)(struct file_lock **, int);
locking rules: locking rules:
file_lock_lock may block
lm_compare_owner: yes no inode->i_lock file_lock_lock may block
lm_notify: yes no lm_compare_owner: yes[1] maybe no
lm_grant: no no lm_notify: yes yes no
lm_break: yes no lm_grant: no no no
lm_change yes no lm_break: yes no no
lm_change yes no no
[1]: ->lm_compare_owner is generally called with *an* inode->i_lock held. It
may not be the i_lock of the inode for either file_lock being compared! This is
the case with deadlock detection, since the code has to chase down the owners
of locks that may be entirely unrelated to the one on which the lock is being
acquired. When doing a search for deadlocks, the file_lock_lock is also held.
--------------------------- buffer_head ----------------------------------- --------------------------- buffer_head -----------------------------------
prototypes: prototypes:
......
...@@ -252,7 +252,8 @@ static void afs_defer_unlock(struct afs_vnode *vnode, struct key *key) ...@@ -252,7 +252,8 @@ static void afs_defer_unlock(struct afs_vnode *vnode, struct key *key)
*/ */
static int afs_do_setlk(struct file *file, struct file_lock *fl) static int afs_do_setlk(struct file *file, struct file_lock *fl)
{ {
struct afs_vnode *vnode = AFS_FS_I(file->f_mapping->host); struct inode *inode = file_inode(file);
struct afs_vnode *vnode = AFS_FS_I(inode);
afs_lock_type_t type; afs_lock_type_t type;
struct key *key = file->private_data; struct key *key = file->private_data;
int ret; int ret;
...@@ -273,7 +274,7 @@ static int afs_do_setlk(struct file *file, struct file_lock *fl) ...@@ -273,7 +274,7 @@ static int afs_do_setlk(struct file *file, struct file_lock *fl)
type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE; type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
lock_flocks(); spin_lock(&inode->i_lock);
/* make sure we've got a callback on this file and that our view of the /* make sure we've got a callback on this file and that our view of the
* data version is up to date */ * data version is up to date */
...@@ -420,7 +421,7 @@ static int afs_do_setlk(struct file *file, struct file_lock *fl) ...@@ -420,7 +421,7 @@ static int afs_do_setlk(struct file *file, struct file_lock *fl)
afs_vnode_fetch_status(vnode, NULL, key); afs_vnode_fetch_status(vnode, NULL, key);
error: error:
unlock_flocks(); spin_unlock(&inode->i_lock);
_leave(" = %d", ret); _leave(" = %d", ret);
return ret; return ret;
......
...@@ -192,7 +192,7 @@ void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count) ...@@ -192,7 +192,7 @@ void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
/** /**
* Encode the flock and fcntl locks for the given inode into the ceph_filelock * Encode the flock and fcntl locks for the given inode into the ceph_filelock
* array. Must be called with lock_flocks() already held. * array. Must be called with inode->i_lock already held.
* If we encounter more of a specific lock type than expected, return -ENOSPC. * If we encounter more of a specific lock type than expected, return -ENOSPC.
*/ */
int ceph_encode_locks_to_buffer(struct inode *inode, int ceph_encode_locks_to_buffer(struct inode *inode,
......
...@@ -2481,20 +2481,20 @@ static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap, ...@@ -2481,20 +2481,20 @@ static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
struct ceph_filelock *flocks; struct ceph_filelock *flocks;
encode_again: encode_again:
lock_flocks(); spin_lock(&inode->i_lock);
ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks); ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
unlock_flocks(); spin_unlock(&inode->i_lock);
flocks = kmalloc((num_fcntl_locks+num_flock_locks) * flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
sizeof(struct ceph_filelock), GFP_NOFS); sizeof(struct ceph_filelock), GFP_NOFS);
if (!flocks) { if (!flocks) {
err = -ENOMEM; err = -ENOMEM;
goto out_free; goto out_free;
} }
lock_flocks(); spin_lock(&inode->i_lock);
err = ceph_encode_locks_to_buffer(inode, flocks, err = ceph_encode_locks_to_buffer(inode, flocks,
num_fcntl_locks, num_fcntl_locks,
num_flock_locks); num_flock_locks);
unlock_flocks(); spin_unlock(&inode->i_lock);
if (err) { if (err) {
kfree(flocks); kfree(flocks);
if (err == -ENOSPC) if (err == -ENOSPC)
......
...@@ -765,7 +765,7 @@ static loff_t cifs_llseek(struct file *file, loff_t offset, int whence) ...@@ -765,7 +765,7 @@ static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
static int cifs_setlease(struct file *file, long arg, struct file_lock **lease) static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
{ {
/* note that this is called by vfs setlease with lock_flocks held /* note that this is called by vfs setlease with i_lock held
to protect *lease from going away */ to protect *lease from going away */
struct inode *inode = file_inode(file); struct inode *inode = file_inode(file);
struct cifsFileInfo *cfile = file->private_data; struct cifsFileInfo *cfile = file->private_data;
......
...@@ -1092,6 +1092,7 @@ struct lock_to_push { ...@@ -1092,6 +1092,7 @@ struct lock_to_push {
static int static int
cifs_push_posix_locks(struct cifsFileInfo *cfile) cifs_push_posix_locks(struct cifsFileInfo *cfile)
{ {
struct inode *inode = cfile->dentry->d_inode;
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
struct file_lock *flock, **before; struct file_lock *flock, **before;
unsigned int count = 0, i = 0; unsigned int count = 0, i = 0;
...@@ -1102,12 +1103,12 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) ...@@ -1102,12 +1103,12 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)
xid = get_xid(); xid = get_xid();
lock_flocks(); spin_lock(&inode->i_lock);
cifs_for_each_lock(cfile->dentry->d_inode, before) { cifs_for_each_lock(inode, before) {
if ((*before)->fl_flags & FL_POSIX) if ((*before)->fl_flags & FL_POSIX)
count++; count++;
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
INIT_LIST_HEAD(&locks_to_send); INIT_LIST_HEAD(&locks_to_send);
...@@ -1126,8 +1127,8 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) ...@@ -1126,8 +1127,8 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)
} }
el = locks_to_send.next; el = locks_to_send.next;
lock_flocks(); spin_lock(&inode->i_lock);
cifs_for_each_lock(cfile->dentry->d_inode, before) { cifs_for_each_lock(inode, before) {
flock = *before; flock = *before;
if ((flock->fl_flags & FL_POSIX) == 0) if ((flock->fl_flags & FL_POSIX) == 0)
continue; continue;
...@@ -1152,7 +1153,7 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) ...@@ -1152,7 +1153,7 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)
lck->offset = flock->fl_start; lck->offset = flock->fl_start;
el = el->next; el = el->next;
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
int stored_rc; int stored_rc;
......
...@@ -889,7 +889,7 @@ static long gfs2_fallocate(struct file *file, int mode, loff_t offset, ...@@ -889,7 +889,7 @@ static long gfs2_fallocate(struct file *file, int mode, loff_t offset,
* cluster; until we do, disable leases (by just returning -EINVAL), * cluster; until we do, disable leases (by just returning -EINVAL),
* unless the administrator has requested purely local locking. * unless the administrator has requested purely local locking.
* *
* Locking: called under lock_flocks * Locking: called under i_lock
* *
* Returns: errno * Returns: errno
*/ */
......
...@@ -169,7 +169,7 @@ nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, ...@@ -169,7 +169,7 @@ nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
again: again:
file->f_locks = 0; file->f_locks = 0;
lock_flocks(); /* protects i_flock list */ spin_lock(&inode->i_lock);
for (fl = inode->i_flock; fl; fl = fl->fl_next) { for (fl = inode->i_flock; fl; fl = fl->fl_next) {
if (fl->fl_lmops != &nlmsvc_lock_operations) if (fl->fl_lmops != &nlmsvc_lock_operations)
continue; continue;
...@@ -181,7 +181,7 @@ nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, ...@@ -181,7 +181,7 @@ nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
if (match(lockhost, host)) { if (match(lockhost, host)) {
struct file_lock lock = *fl; struct file_lock lock = *fl;
unlock_flocks(); spin_unlock(&inode->i_lock);
lock.fl_type = F_UNLCK; lock.fl_type = F_UNLCK;
lock.fl_start = 0; lock.fl_start = 0;
lock.fl_end = OFFSET_MAX; lock.fl_end = OFFSET_MAX;
...@@ -193,7 +193,7 @@ nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, ...@@ -193,7 +193,7 @@ nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
goto again; goto again;
} }
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
return 0; return 0;
} }
...@@ -228,14 +228,14 @@ nlm_file_inuse(struct nlm_file *file) ...@@ -228,14 +228,14 @@ nlm_file_inuse(struct nlm_file *file)
if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares) if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares)
return 1; return 1;
lock_flocks(); spin_lock(&inode->i_lock);
for (fl = inode->i_flock; fl; fl = fl->fl_next) { for (fl = inode->i_flock; fl; fl = fl->fl_next) {
if (fl->fl_lmops == &nlmsvc_lock_operations) { if (fl->fl_lmops == &nlmsvc_lock_operations) {
unlock_flocks(); spin_unlock(&inode->i_lock);
return 1; return 1;
} }
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
file->f_locks = 0; file->f_locks = 0;
return 0; return 0;
} }
......
...@@ -153,27 +153,37 @@ int lease_break_time = 45; ...@@ -153,27 +153,37 @@ int lease_break_time = 45;
#define for_each_lock(inode, lockp) \ #define for_each_lock(inode, lockp) \
for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next) for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
/* The global file_lock_list is only used for displaying /proc/locks. */ /*
* The global file_lock_list is only used for displaying /proc/locks. Protected
* by the file_lock_lock.
*/
static LIST_HEAD(file_lock_list); static LIST_HEAD(file_lock_list);
/* The blocked_list is used to find POSIX lock loops for deadlock detection. */ /*
* The blocked_list is used to find POSIX lock loops for deadlock detection.
* Protected by file_lock_lock.
*/
static LIST_HEAD(blocked_list); static LIST_HEAD(blocked_list);
/* Protects the two list heads above, plus the inode->i_flock list */ /*
* This lock protects the blocked_list, and the file_lock_list. Generally, if
* you're accessing one of those lists, you want to be holding this lock.
*
* In addition, it also protects the fl->fl_block list, and the fl->fl_next
* pointer for file_lock structures that are acting as lock requests (in
* contrast to those that are acting as records of acquired locks).
*
* Note that when we acquire this lock in order to change the above fields,
* we often hold the i_lock as well. In certain cases, when reading the fields
* protected by this lock, we can skip acquiring it iff we already hold the
* i_lock.
*
* In particular, adding an entry to the fl_block list requires that you hold
* both the i_lock and the blocked_lock_lock (acquired in that order). Deleting
* an entry from the list however only requires the file_lock_lock.
*/
static DEFINE_SPINLOCK(file_lock_lock); static DEFINE_SPINLOCK(file_lock_lock);
void lock_flocks(void)
{
spin_lock(&file_lock_lock);
}
EXPORT_SYMBOL_GPL(lock_flocks);
void unlock_flocks(void)
{
spin_unlock(&file_lock_lock);
}
EXPORT_SYMBOL_GPL(unlock_flocks);
static struct kmem_cache *filelock_cache __read_mostly; static struct kmem_cache *filelock_cache __read_mostly;
static void locks_init_lock_heads(struct file_lock *fl) static void locks_init_lock_heads(struct file_lock *fl)
...@@ -489,13 +499,17 @@ static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) ...@@ -489,13 +499,17 @@ static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
static inline void static inline void
locks_insert_global_locks(struct file_lock *fl) locks_insert_global_locks(struct file_lock *fl)
{ {
spin_lock(&file_lock_lock);
list_add_tail(&fl->fl_link, &file_lock_list); list_add_tail(&fl->fl_link, &file_lock_list);
spin_unlock(&file_lock_lock);
} }
static inline void static inline void
locks_delete_global_locks(struct file_lock *fl) locks_delete_global_locks(struct file_lock *fl)
{ {
spin_lock(&file_lock_lock);
list_del_init(&fl->fl_link); list_del_init(&fl->fl_link);
spin_unlock(&file_lock_lock);
} }
static inline void static inline void
...@@ -512,6 +526,8 @@ locks_delete_global_blocked(struct file_lock *waiter) ...@@ -512,6 +526,8 @@ locks_delete_global_blocked(struct file_lock *waiter)
/* Remove waiter from blocker's block list. /* Remove waiter from blocker's block list.
* When blocker ends up pointing to itself then the list is empty. * When blocker ends up pointing to itself then the list is empty.
*
* Must be called with file_lock_lock held.
*/ */
static void __locks_delete_block(struct file_lock *waiter) static void __locks_delete_block(struct file_lock *waiter)
{ {
...@@ -520,37 +536,47 @@ static void __locks_delete_block(struct file_lock *waiter) ...@@ -520,37 +536,47 @@ static void __locks_delete_block(struct file_lock *waiter)
waiter->fl_next = NULL; waiter->fl_next = NULL;
} }
/*
*/
static void locks_delete_block(struct file_lock *waiter) static void locks_delete_block(struct file_lock *waiter)
{ {
lock_flocks(); spin_lock(&file_lock_lock);
__locks_delete_block(waiter); __locks_delete_block(waiter);
unlock_flocks(); spin_unlock(&file_lock_lock);
} }
/* Insert waiter into blocker's block list. /* Insert waiter into blocker's block list.
* We use a circular list so that processes can be easily woken up in * We use a circular list so that processes can be easily woken up in
* the order they blocked. The documentation doesn't require this but * the order they blocked. The documentation doesn't require this but
* it seems like the reasonable thing to do. * it seems like the reasonable thing to do.
*
* Must be called with file_lock_lock held!
*/ */
static void locks_insert_block(struct file_lock *blocker, static void __locks_insert_block(struct file_lock *blocker,
struct file_lock *waiter) struct file_lock *waiter)
{ {
BUG_ON(!list_empty(&waiter->fl_block)); BUG_ON(!list_empty(&waiter->fl_block));
waiter->fl_next = blocker; waiter->fl_next = blocker;
list_add_tail(&waiter->fl_block, &blocker->fl_block); list_add_tail(&waiter->fl_block, &blocker->fl_block);
if (IS_POSIX(blocker)) if (IS_POSIX(blocker))
locks_insert_global_blocked(request); locks_insert_global_blocked(waiter);
}
/* Must be called with i_lock held. */
static void locks_insert_block(struct file_lock *blocker,
struct file_lock *waiter)
{
spin_lock(&file_lock_lock);
__locks_insert_block(blocker, waiter);
spin_unlock(&file_lock_lock);
} }
/* /*
* Wake up processes blocked waiting for blocker. * Wake up processes blocked waiting for blocker.
* *
* Must be called with the file_lock_lock held! * Must be called with the inode->i_lock held!
*/ */
static void locks_wake_up_blocks(struct file_lock *blocker) static void locks_wake_up_blocks(struct file_lock *blocker)
{ {
spin_lock(&file_lock_lock);
while (!list_empty(&blocker->fl_block)) { while (!list_empty(&blocker->fl_block)) {
struct file_lock *waiter; struct file_lock *waiter;
...@@ -562,10 +588,13 @@ static void locks_wake_up_blocks(struct file_lock *blocker) ...@@ -562,10 +588,13 @@ static void locks_wake_up_blocks(struct file_lock *blocker)
else else
wake_up(&waiter->fl_wait); wake_up(&waiter->fl_wait);
} }
spin_unlock(&file_lock_lock);
} }
/* Insert file lock fl into an inode's lock list at the position indicated /* Insert file lock fl into an inode's lock list at the position indicated
* by pos. At the same time add the lock to the global file lock list. * by pos. At the same time add the lock to the global file lock list.
*
* Must be called with the i_lock held!
*/ */
static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl) static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
{ {
...@@ -583,6 +612,8 @@ static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl) ...@@ -583,6 +612,8 @@ static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
* Wake up processes that are blocked waiting for this lock, * Wake up processes that are blocked waiting for this lock,
* notify the FS that the lock has been cleared and * notify the FS that the lock has been cleared and
* finally free the lock. * finally free the lock.
*
* Must be called with the i_lock held!
*/ */
static void locks_delete_lock(struct file_lock **thisfl_p) static void locks_delete_lock(struct file_lock **thisfl_p)
{ {
...@@ -652,8 +683,9 @@ void ...@@ -652,8 +683,9 @@ void
posix_test_lock(struct file *filp, struct file_lock *fl) posix_test_lock(struct file *filp, struct file_lock *fl)
{ {
struct file_lock *cfl; struct file_lock *cfl;
struct inode *inode = file_inode(filp);
lock_flocks(); spin_lock(&inode->i_lock);
for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) { for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) {
if (!IS_POSIX(cfl)) if (!IS_POSIX(cfl))
continue; continue;
...@@ -666,7 +698,7 @@ posix_test_lock(struct file *filp, struct file_lock *fl) ...@@ -666,7 +698,7 @@ posix_test_lock(struct file *filp, struct file_lock *fl)
fl->fl_pid = pid_vnr(cfl->fl_nspid); fl->fl_pid = pid_vnr(cfl->fl_nspid);
} else } else
fl->fl_type = F_UNLCK; fl->fl_type = F_UNLCK;
unlock_flocks(); spin_unlock(&inode->i_lock);
return; return;
} }
EXPORT_SYMBOL(posix_test_lock); EXPORT_SYMBOL(posix_test_lock);
...@@ -710,6 +742,7 @@ static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) ...@@ -710,6 +742,7 @@ static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
return NULL; return NULL;
} }
/* Must be called with the file_lock_lock held! */
static int posix_locks_deadlock(struct file_lock *caller_fl, static int posix_locks_deadlock(struct file_lock *caller_fl,
struct file_lock *block_fl) struct file_lock *block_fl)
{ {
...@@ -745,7 +778,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request) ...@@ -745,7 +778,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
return -ENOMEM; return -ENOMEM;
} }
lock_flocks(); spin_lock(&inode->i_lock);
if (request->fl_flags & FL_ACCESS) if (request->fl_flags & FL_ACCESS)
goto find_conflict; goto find_conflict;
...@@ -775,9 +808,9 @@ static int flock_lock_file(struct file *filp, struct file_lock *request) ...@@ -775,9 +808,9 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
* give it the opportunity to lock the file. * give it the opportunity to lock the file.
*/ */
if (found) { if (found) {
unlock_flocks(); spin_unlock(&inode->i_lock);
cond_resched(); cond_resched();
lock_flocks(); spin_lock(&inode->i_lock);
} }
find_conflict: find_conflict:
...@@ -804,7 +837,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request) ...@@ -804,7 +837,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
error = 0; error = 0;
out: out:
unlock_flocks(); spin_unlock(&inode->i_lock);
if (new_fl) if (new_fl)
locks_free_lock(new_fl); locks_free_lock(new_fl);
return error; return error;
...@@ -834,7 +867,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str ...@@ -834,7 +867,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
new_fl2 = locks_alloc_lock(); new_fl2 = locks_alloc_lock();
} }
lock_flocks(); spin_lock(&inode->i_lock);
/* /*
* New lock request. Walk all POSIX locks and look for conflicts. If * New lock request. Walk all POSIX locks and look for conflicts. If
* there are any, either return error or put the request on the * there are any, either return error or put the request on the
...@@ -852,11 +885,17 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str ...@@ -852,11 +885,17 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
error = -EAGAIN; error = -EAGAIN;
if (!(request->fl_flags & FL_SLEEP)) if (!(request->fl_flags & FL_SLEEP))
goto out; goto out;
/*
* Deadlock detection and insertion into the blocked
* locks list must be done while holding the same lock!
*/
error = -EDEADLK; error = -EDEADLK;
if (posix_locks_deadlock(request, fl)) spin_lock(&file_lock_lock);
goto out; if (likely(!posix_locks_deadlock(request, fl))) {
error = FILE_LOCK_DEFERRED; error = FILE_LOCK_DEFERRED;
locks_insert_block(fl, request); __locks_insert_block(fl, request);
}
spin_unlock(&file_lock_lock);
goto out; goto out;
} }
} }
...@@ -1006,7 +1045,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str ...@@ -1006,7 +1045,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
locks_wake_up_blocks(left); locks_wake_up_blocks(left);
} }
out: out:
unlock_flocks(); spin_unlock(&inode->i_lock);
/* /*
* Free any unused locks. * Free any unused locks.
*/ */
...@@ -1081,14 +1120,14 @@ int locks_mandatory_locked(struct inode *inode) ...@@ -1081,14 +1120,14 @@ int locks_mandatory_locked(struct inode *inode)
/* /*
* Search the lock list for this inode for any POSIX locks. * Search the lock list for this inode for any POSIX locks.
*/ */
lock_flocks(); spin_lock(&inode->i_lock);
for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
if (!IS_POSIX(fl)) if (!IS_POSIX(fl))
continue; continue;
if (fl->fl_owner != owner) if (fl->fl_owner != owner)
break; break;
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
return fl ? -EAGAIN : 0; return fl ? -EAGAIN : 0;
} }
...@@ -1231,7 +1270,7 @@ int __break_lease(struct inode *inode, unsigned int mode) ...@@ -1231,7 +1270,7 @@ int __break_lease(struct inode *inode, unsigned int mode)
if (IS_ERR(new_fl)) if (IS_ERR(new_fl))
return PTR_ERR(new_fl); return PTR_ERR(new_fl);
lock_flocks(); spin_lock(&inode->i_lock);
time_out_leases(inode); time_out_leases(inode);
...@@ -1281,11 +1320,11 @@ int __break_lease(struct inode *inode, unsigned int mode) ...@@ -1281,11 +1320,11 @@ int __break_lease(struct inode *inode, unsigned int mode)
break_time++; break_time++;
} }
locks_insert_block(flock, new_fl); locks_insert_block(flock, new_fl);
unlock_flocks(); spin_unlock(&inode->i_lock);
error = wait_event_interruptible_timeout(new_fl->fl_wait, error = wait_event_interruptible_timeout(new_fl->fl_wait,
!new_fl->fl_next, break_time); !new_fl->fl_next, break_time);
lock_flocks(); spin_lock(&inode->i_lock);
__locks_delete_block(new_fl); locks_delete_block(new_fl);
if (error >= 0) { if (error >= 0) {
if (error == 0) if (error == 0)
time_out_leases(inode); time_out_leases(inode);
...@@ -1302,7 +1341,7 @@ int __break_lease(struct inode *inode, unsigned int mode) ...@@ -1302,7 +1341,7 @@ int __break_lease(struct inode *inode, unsigned int mode)
} }
out: out:
unlock_flocks(); spin_unlock(&inode->i_lock);
locks_free_lock(new_fl); locks_free_lock(new_fl);
return error; return error;
} }
...@@ -1355,9 +1394,10 @@ EXPORT_SYMBOL(lease_get_mtime); ...@@ -1355,9 +1394,10 @@ EXPORT_SYMBOL(lease_get_mtime);
int fcntl_getlease(struct file *filp) int fcntl_getlease(struct file *filp)
{ {
struct file_lock *fl; struct file_lock *fl;
struct inode *inode = file_inode(filp);
int type = F_UNLCK; int type = F_UNLCK;
lock_flocks(); spin_lock(&inode->i_lock);
time_out_leases(file_inode(filp)); time_out_leases(file_inode(filp));
for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl); for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
fl = fl->fl_next) { fl = fl->fl_next) {
...@@ -1366,7 +1406,7 @@ int fcntl_getlease(struct file *filp) ...@@ -1366,7 +1406,7 @@ int fcntl_getlease(struct file *filp)
break; break;
} }
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
return type; return type;
} }
...@@ -1460,7 +1500,7 @@ static int generic_delete_lease(struct file *filp, struct file_lock **flp) ...@@ -1460,7 +1500,7 @@ static int generic_delete_lease(struct file *filp, struct file_lock **flp)
* The (input) flp->fl_lmops->lm_break function is required * The (input) flp->fl_lmops->lm_break function is required
* by break_lease(). * by break_lease().
* *
* Called with file_lock_lock held. * Called with inode->i_lock held.
*/ */
int generic_setlease(struct file *filp, long arg, struct file_lock **flp) int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
{ {
...@@ -1529,11 +1569,12 @@ static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease) ...@@ -1529,11 +1569,12 @@ static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
int vfs_setlease(struct file *filp, long arg, struct file_lock **lease) int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
{ {
struct inode *inode = file_inode(filp);
int error; int error;
lock_flocks(); spin_lock(&inode->i_lock);
error = __vfs_setlease(filp, arg, lease); error = __vfs_setlease(filp, arg, lease);
unlock_flocks(); spin_unlock(&inode->i_lock);
return error; return error;
} }
...@@ -1551,6 +1592,7 @@ static int do_fcntl_delete_lease(struct file *filp) ...@@ -1551,6 +1592,7 @@ static int do_fcntl_delete_lease(struct file *filp)
static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
{ {
struct file_lock *fl, *ret; struct file_lock *fl, *ret;
struct inode *inode = file_inode(filp);
struct fasync_struct *new; struct fasync_struct *new;
int error; int error;
...@@ -1564,10 +1606,10 @@ static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) ...@@ -1564,10 +1606,10 @@ static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
return -ENOMEM; return -ENOMEM;
} }
ret = fl; ret = fl;
lock_flocks(); spin_lock(&inode->i_lock);
error = __vfs_setlease(filp, arg, &ret); error = __vfs_setlease(filp, arg, &ret);
if (error) { if (error) {
unlock_flocks(); spin_unlock(&inode->i_lock);
locks_free_lock(fl); locks_free_lock(fl);
goto out_free_fasync; goto out_free_fasync;
} }
...@@ -1584,7 +1626,7 @@ static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) ...@@ -1584,7 +1626,7 @@ static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
new = NULL; new = NULL;
error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
unlock_flocks(); spin_unlock(&inode->i_lock);
out_free_fasync: out_free_fasync:
if (new) if (new)
...@@ -2108,7 +2150,7 @@ void locks_remove_flock(struct file *filp) ...@@ -2108,7 +2150,7 @@ void locks_remove_flock(struct file *filp)
fl.fl_ops->fl_release_private(&fl); fl.fl_ops->fl_release_private(&fl);
} }
lock_flocks(); spin_lock(&inode->i_lock);
before = &inode->i_flock; before = &inode->i_flock;
while ((fl = *before) != NULL) { while ((fl = *before) != NULL) {
...@@ -2126,7 +2168,7 @@ void locks_remove_flock(struct file *filp) ...@@ -2126,7 +2168,7 @@ void locks_remove_flock(struct file *filp)
} }
before = &fl->fl_next; before = &fl->fl_next;
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
} }
/** /**
...@@ -2140,12 +2182,12 @@ posix_unblock_lock(struct file_lock *waiter) ...@@ -2140,12 +2182,12 @@ posix_unblock_lock(struct file_lock *waiter)
{ {
int status = 0; int status = 0;
lock_flocks(); spin_lock(&file_lock_lock);
if (waiter->fl_next) if (waiter->fl_next)
__locks_delete_block(waiter); __locks_delete_block(waiter);
else else
status = -ENOENT; status = -ENOENT;
unlock_flocks(); spin_unlock(&file_lock_lock);
return status; return status;
} }
EXPORT_SYMBOL(posix_unblock_lock); EXPORT_SYMBOL(posix_unblock_lock);
...@@ -2259,7 +2301,7 @@ static void *locks_start(struct seq_file *f, loff_t *pos) ...@@ -2259,7 +2301,7 @@ static void *locks_start(struct seq_file *f, loff_t *pos)
{ {
loff_t *p = f->private; loff_t *p = f->private;
lock_flocks(); spin_lock(&file_lock_lock);
*p = (*pos + 1); *p = (*pos + 1);
return seq_list_start(&file_lock_list, *pos); return seq_list_start(&file_lock_list, *pos);
} }
...@@ -2273,7 +2315,7 @@ static void *locks_next(struct seq_file *f, void *v, loff_t *pos) ...@@ -2273,7 +2315,7 @@ static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
static void locks_stop(struct seq_file *f, void *v) static void locks_stop(struct seq_file *f, void *v)
{ {
unlock_flocks(); spin_unlock(&file_lock_lock);
} }
static const struct seq_operations locks_seq_operations = { static const struct seq_operations locks_seq_operations = {
...@@ -2320,7 +2362,8 @@ int lock_may_read(struct inode *inode, loff_t start, unsigned long len) ...@@ -2320,7 +2362,8 @@ int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
{ {
struct file_lock *fl; struct file_lock *fl;
int result = 1; int result = 1;
lock_flocks();
spin_lock(&inode->i_lock);
for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
if (IS_POSIX(fl)) { if (IS_POSIX(fl)) {
if (fl->fl_type == F_RDLCK) if (fl->fl_type == F_RDLCK)
...@@ -2337,7 +2380,7 @@ int lock_may_read(struct inode *inode, loff_t start, unsigned long len) ...@@ -2337,7 +2380,7 @@ int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
result = 0; result = 0;
break; break;
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
return result; return result;
} }
...@@ -2360,7 +2403,8 @@ int lock_may_write(struct inode *inode, loff_t start, unsigned long len) ...@@ -2360,7 +2403,8 @@ int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
{ {
struct file_lock *fl; struct file_lock *fl;
int result = 1; int result = 1;
lock_flocks();
spin_lock(&inode->i_lock);
for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
if (IS_POSIX(fl)) { if (IS_POSIX(fl)) {
if ((fl->fl_end < start) || (fl->fl_start > (start + len))) if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
...@@ -2375,7 +2419,7 @@ int lock_may_write(struct inode *inode, loff_t start, unsigned long len) ...@@ -2375,7 +2419,7 @@ int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
result = 0; result = 0;
break; break;
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
return result; return result;
} }
......
...@@ -73,20 +73,20 @@ static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_ ...@@ -73,20 +73,20 @@ static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_
if (inode->i_flock == NULL) if (inode->i_flock == NULL)
goto out; goto out;
/* Protect inode->i_flock using the file locks lock */ /* Protect inode->i_flock using the i_lock */
lock_flocks(); spin_lock(&inode->i_lock);
for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
continue; continue;
if (nfs_file_open_context(fl->fl_file) != ctx) if (nfs_file_open_context(fl->fl_file) != ctx)
continue; continue;
unlock_flocks(); spin_unlock(&inode->i_lock);
status = nfs4_lock_delegation_recall(fl, state, stateid); status = nfs4_lock_delegation_recall(fl, state, stateid);
if (status < 0) if (status < 0)
goto out; goto out;
lock_flocks(); spin_lock(&inode->i_lock);
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
out: out:
return status; return status;
} }
......
...@@ -1373,13 +1373,13 @@ static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_ ...@@ -1373,13 +1373,13 @@ static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_
/* Guard against delegation returns and new lock/unlock calls */ /* Guard against delegation returns and new lock/unlock calls */
down_write(&nfsi->rwsem); down_write(&nfsi->rwsem);
/* Protect inode->i_flock using the BKL */ /* Protect inode->i_flock using the BKL */
lock_flocks(); spin_lock(&inode->i_lock);
for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
continue; continue;
if (nfs_file_open_context(fl->fl_file)->state != state) if (nfs_file_open_context(fl->fl_file)->state != state)
continue; continue;
unlock_flocks(); spin_unlock(&inode->i_lock);
status = ops->recover_lock(state, fl); status = ops->recover_lock(state, fl);
switch (status) { switch (status) {
case 0: case 0:
...@@ -1406,9 +1406,9 @@ static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_ ...@@ -1406,9 +1406,9 @@ static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_
/* kill_proc(fl->fl_pid, SIGLOST, 1); */ /* kill_proc(fl->fl_pid, SIGLOST, 1); */
status = 0; status = 0;
} }
lock_flocks(); spin_lock(&inode->i_lock);
} }
unlock_flocks(); spin_unlock(&inode->i_lock);
out: out:
up_write(&nfsi->rwsem); up_write(&nfsi->rwsem);
return status; return status;
......
...@@ -2645,13 +2645,13 @@ static void nfsd_break_one_deleg(struct nfs4_delegation *dp) ...@@ -2645,13 +2645,13 @@ static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
/* only place dl_time is set. protected by lock_flocks*/ /* Only place dl_time is set; protected by i_lock: */
dp->dl_time = get_seconds(); dp->dl_time = get_seconds();
nfsd4_cb_recall(dp); nfsd4_cb_recall(dp);
} }
/* Called from break_lease() with lock_flocks() held. */ /* Called from break_lease() with i_lock held. */
static void nfsd_break_deleg_cb(struct file_lock *fl) static void nfsd_break_deleg_cb(struct file_lock *fl)
{ {
struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
...@@ -4520,7 +4520,7 @@ check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner) ...@@ -4520,7 +4520,7 @@ check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
struct inode *inode = filp->fi_inode; struct inode *inode = filp->fi_inode;
int status = 0; int status = 0;
lock_flocks(); spin_lock(&inode->i_lock);
for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
if ((*flpp)->fl_owner == (fl_owner_t)lowner) { if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
status = 1; status = 1;
...@@ -4528,7 +4528,7 @@ check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner) ...@@ -4528,7 +4528,7 @@ check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
} }
} }
out: out:
unlock_flocks(); spin_unlock(&inode->i_lock);
return status; return status;
} }
......
...@@ -1024,8 +1024,6 @@ extern int vfs_setlease(struct file *, long, struct file_lock **); ...@@ -1024,8 +1024,6 @@ extern int vfs_setlease(struct file *, long, struct file_lock **);
extern int lease_modify(struct file_lock **, int); extern int lease_modify(struct file_lock **, int);
extern int lock_may_read(struct inode *, loff_t start, unsigned long count); extern int lock_may_read(struct inode *, loff_t start, unsigned long count);
extern int lock_may_write(struct inode *, loff_t start, unsigned long count); extern int lock_may_write(struct inode *, loff_t start, unsigned long count);
extern void lock_flocks(void);
extern void unlock_flocks(void);
#else /* !CONFIG_FILE_LOCKING */ #else /* !CONFIG_FILE_LOCKING */
static inline int fcntl_getlk(struct file *file, struct flock __user *user) static inline int fcntl_getlk(struct file *file, struct flock __user *user)
{ {
...@@ -1166,15 +1164,6 @@ static inline int lock_may_write(struct inode *inode, loff_t start, ...@@ -1166,15 +1164,6 @@ static inline int lock_may_write(struct inode *inode, loff_t start,
{ {
return 1; return 1;
} }
static inline void lock_flocks(void)
{
}
static inline void unlock_flocks(void)
{
}
#endif /* !CONFIG_FILE_LOCKING */ #endif /* !CONFIG_FILE_LOCKING */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment