Skip to content

Commit b2b89eb

Browse files
committed
Merge tag 'locks-v3.20-2' of git://git.samba.org/jlayton/linux
Pull file locking fixes from Jeff Layton: "A small set of patches to fix problems with the recent file locking changes that we discussed earlier this week" " * tag 'locks-v3.20-2' of git://git.samba.org/jlayton/linux: locks: fix list insertion when lock is split in two locks: remove conditional lock release in middle of flock_lock_file locks: only remove leases associated with the file being closed Revert "locks: keep a count of locks on the flctx lists"
2 parents eaa0eda + 2e2f756 commit b2b89eb

4 files changed

Lines changed: 36 additions & 49 deletions

File tree

‎fs/ceph/locks.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,20 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
245245
*/
246246
void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
247247
{
248+
struct file_lock *lock;
248249
struct file_lock_context *ctx;
249250

250251
*fcntl_count = 0;
251252
*flock_count = 0;
252253

253254
ctx = inode->i_flctx;
254255
if (ctx) {
255-
*fcntl_count = ctx->flc_posix_cnt;
256-
*flock_count = ctx->flc_flock_cnt;
256+
spin_lock(&ctx->flc_lock);
257+
list_for_each_entry(lock, &ctx->flc_posix, fl_list)
258+
++(*fcntl_count);
259+
list_for_each_entry(lock, &ctx->flc_flock, fl_list)
260+
++(*flock_count);
261+
spin_unlock(&ctx->flc_lock);
257262
}
258263
dout("counted %d flock locks and %d fcntl locks",
259264
*flock_count, *fcntl_count);

‎fs/cifs/file.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)
11291129
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
11301130
struct file_lock *flock;
11311131
struct file_lock_context *flctx = inode->i_flctx;
1132-
unsigned int i;
1132+
unsigned int count = 0, i;
11331133
int rc = 0, xid, type;
11341134
struct list_head locks_to_send, *el;
11351135
struct lock_to_push *lck, *tmp;
@@ -1140,14 +1140,20 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)
11401140
if (!flctx)
11411141
goto out;
11421142

1143+
spin_lock(&flctx->flc_lock);
1144+
list_for_each(el, &flctx->flc_posix) {
1145+
count++;
1146+
}
1147+
spin_unlock(&flctx->flc_lock);
1148+
11431149
INIT_LIST_HEAD(&locks_to_send);
11441150

11451151
/*
1146-
* Allocating flc_posix_cnt locks is enough because no FL_POSIX locks
1147-
* can be added to the list while we are holding cinode->lock_sem that
1152+
* Allocating count locks is enough because no FL_POSIX locks can be
1153+
* added to the list while we are holding cinode->lock_sem that
11481154
* protects locking operations of this inode.
11491155
*/
1150-
for (i = 0; i < flctx->flc_posix_cnt; i++) {
1156+
for (i = 0; i < count; i++) {
11511157
lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
11521158
if (!lck) {
11531159
rc = -ENOMEM;

‎fs/locks.c‎

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -681,21 +681,18 @@ static void locks_wake_up_blocks(struct file_lock *blocker)
681681
}
682682

683683
static void
684-
locks_insert_lock_ctx(struct file_lock *fl, int *counter,
685-
struct list_head *before)
684+
locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
686685
{
687686
fl->fl_nspid = get_pid(task_tgid(current));
688687
list_add_tail(&fl->fl_list, before);
689-
++*counter;
690688
locks_insert_global_locks(fl);
691689
}
692690

693691
static void
694-
locks_unlink_lock_ctx(struct file_lock *fl, int *counter)
692+
locks_unlink_lock_ctx(struct file_lock *fl)
695693
{
696694
locks_delete_global_locks(fl);
697695
list_del_init(&fl->fl_list);
698-
--*counter;
699696
if (fl->fl_nspid) {
700697
put_pid(fl->fl_nspid);
701698
fl->fl_nspid = NULL;
@@ -704,10 +701,9 @@ locks_unlink_lock_ctx(struct file_lock *fl, int *counter)
704701
}
705702

706703
static void
707-
locks_delete_lock_ctx(struct file_lock *fl, int *counter,
708-
struct list_head *dispose)
704+
locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
709705
{
710-
locks_unlink_lock_ctx(fl, counter);
706+
locks_unlink_lock_ctx(fl);
711707
if (dispose)
712708
list_add(&fl->fl_list, dispose);
713709
else
@@ -895,7 +891,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
895891
if (request->fl_type == fl->fl_type)
896892
goto out;
897893
found = true;
898-
locks_delete_lock_ctx(fl, &ctx->flc_flock_cnt, &dispose);
894+
locks_delete_lock_ctx(fl, &dispose);
899895
break;
900896
}
901897

@@ -905,16 +901,6 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
905901
goto out;
906902
}
907903

908-
/*
909-
* If a higher-priority process was blocked on the old file lock,
910-
* give it the opportunity to lock the file.
911-
*/
912-
if (found) {
913-
spin_unlock(&ctx->flc_lock);
914-
cond_resched();
915-
spin_lock(&ctx->flc_lock);
916-
}
917-
918904
find_conflict:
919905
list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
920906
if (!flock_locks_conflict(request, fl))
@@ -929,7 +915,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
929915
if (request->fl_flags & FL_ACCESS)
930916
goto out;
931917
locks_copy_lock(new_fl, request);
932-
locks_insert_lock_ctx(new_fl, &ctx->flc_flock_cnt, &ctx->flc_flock);
918+
locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
933919
new_fl = NULL;
934920
error = 0;
935921

@@ -1046,8 +1032,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
10461032
else
10471033
request->fl_end = fl->fl_end;
10481034
if (added) {
1049-
locks_delete_lock_ctx(fl, &ctx->flc_posix_cnt,
1050-
&dispose);
1035+
locks_delete_lock_ctx(fl, &dispose);
10511036
continue;
10521037
}
10531038
request = fl;
@@ -1076,8 +1061,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
10761061
* one (This may happen several times).
10771062
*/
10781063
if (added) {
1079-
locks_delete_lock_ctx(fl,
1080-
&ctx->flc_posix_cnt, &dispose);
1064+
locks_delete_lock_ctx(fl, &dispose);
10811065
continue;
10821066
}
10831067
/*
@@ -1093,10 +1077,8 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
10931077
locks_copy_lock(new_fl, request);
10941078
request = new_fl;
10951079
new_fl = NULL;
1096-
locks_insert_lock_ctx(request,
1097-
&ctx->flc_posix_cnt, &fl->fl_list);
1098-
locks_delete_lock_ctx(fl,
1099-
&ctx->flc_posix_cnt, &dispose);
1080+
locks_insert_lock_ctx(request, &fl->fl_list);
1081+
locks_delete_lock_ctx(fl, &dispose);
11001082
added = true;
11011083
}
11021084
}
@@ -1124,8 +1106,8 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
11241106
goto out;
11251107
}
11261108
locks_copy_lock(new_fl, request);
1127-
locks_insert_lock_ctx(new_fl, &ctx->flc_posix_cnt,
1128-
&fl->fl_list);
1109+
locks_insert_lock_ctx(new_fl, &fl->fl_list);
1110+
fl = new_fl;
11291111
new_fl = NULL;
11301112
}
11311113
if (right) {
@@ -1136,8 +1118,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
11361118
left = new_fl2;
11371119
new_fl2 = NULL;
11381120
locks_copy_lock(left, right);
1139-
locks_insert_lock_ctx(left, &ctx->flc_posix_cnt,
1140-
&fl->fl_list);
1121+
locks_insert_lock_ctx(left, &fl->fl_list);
11411122
}
11421123
right->fl_start = request->fl_end + 1;
11431124
locks_wake_up_blocks(right);
@@ -1321,7 +1302,6 @@ static void lease_clear_pending(struct file_lock *fl, int arg)
13211302
/* We already had a lease on this file; just change its type */
13221303
int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
13231304
{
1324-
struct file_lock_context *flctx;
13251305
int error = assign_type(fl, arg);
13261306

13271307
if (error)
@@ -1331,15 +1311,14 @@ int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
13311311
if (arg == F_UNLCK) {
13321312
struct file *filp = fl->fl_file;
13331313

1334-
flctx = file_inode(filp)->i_flctx;
13351314
f_delown(filp);
13361315
filp->f_owner.signum = 0;
13371316
fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
13381317
if (fl->fl_fasync != NULL) {
13391318
printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
13401319
fl->fl_fasync = NULL;
13411320
}
1342-
locks_delete_lock_ctx(fl, &flctx->flc_lease_cnt, dispose);
1321+
locks_delete_lock_ctx(fl, dispose);
13431322
}
13441323
return 0;
13451324
}
@@ -1456,8 +1435,7 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
14561435
fl->fl_downgrade_time = break_time;
14571436
}
14581437
if (fl->fl_lmops->lm_break(fl))
1459-
locks_delete_lock_ctx(fl, &ctx->flc_lease_cnt,
1460-
&dispose);
1438+
locks_delete_lock_ctx(fl, &dispose);
14611439
}
14621440

14631441
if (list_empty(&ctx->flc_lease))
@@ -1697,7 +1675,7 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
16971675
if (!leases_enable)
16981676
goto out;
16991677

1700-
locks_insert_lock_ctx(lease, &ctx->flc_lease_cnt, &ctx->flc_lease);
1678+
locks_insert_lock_ctx(lease, &ctx->flc_lease);
17011679
/*
17021680
* The check in break_lease() is lockless. It's possible for another
17031681
* open to race in after we did the earlier check for a conflicting
@@ -1710,7 +1688,7 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
17101688
smp_mb();
17111689
error = check_conflicting_open(dentry, arg, lease->fl_flags);
17121690
if (error) {
1713-
locks_unlink_lock_ctx(lease, &ctx->flc_lease_cnt);
1691+
locks_unlink_lock_ctx(lease);
17141692
goto out;
17151693
}
17161694

@@ -2448,7 +2426,8 @@ locks_remove_lease(struct file *filp)
24482426

24492427
spin_lock(&ctx->flc_lock);
24502428
list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
2451-
lease_modify(fl, F_UNLCK, &dispose);
2429+
if (filp == fl->fl_file)
2430+
lease_modify(fl, F_UNLCK, &dispose);
24522431
spin_unlock(&ctx->flc_lock);
24532432
locks_dispose_list(&dispose);
24542433
}

‎include/linux/fs.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,9 +968,6 @@ struct file_lock_context {
968968
struct list_head flc_flock;
969969
struct list_head flc_posix;
970970
struct list_head flc_lease;
971-
int flc_flock_cnt;
972-
int flc_posix_cnt;
973-
int flc_lease_cnt;
974971
};
975972

976973
/* The following constant reflects the upper bound of the file/locking space */

0 commit comments

Comments
 (0)