Skip to content

Commit c4b3ffb

Browse files
committed
Merge series 'filelock: split file leases out of struct file_lock' of https://lore.kernel.org/r/20240131-flsplit-v3-0-c6129007ee8d@kernel.org
Pull file locking series from Jeff Layton: Long ago, file locks used to hang off of a singly-linked list in struct inode. Because of this, when leases were added, they were added to the same list and so they had to be tracked using the same sort of structure. Several years ago, we added struct file_lock_context, which allowed us to use separate lists to track different types of file locks. Given that, leases no longer need to be tracked using struct file_lock. That said, a lot of the underlying infrastructure _is_ the same between file leases and locks, so we can't completely separate everything. This patchset first splits a group of fields used by both file locks and leases into a new struct file_lock_core, that is then embedded in struct file_lock. Coccinelle was then used to convert a lot of the callers to deal with the move, with the remaining 25% or so converted by hand. It then converts several internal functions in fs/locks.c to work with struct file_lock_core. Lastly, struct file_lock is split into struct file_lock and file_lease, and the lease-related APIs converted to take struct file_lease. I also added a few small helpers and converted several users over to them. That reduces the size of the per-fs conversion patches later in the series. I played with some others too, but they were too awkward or not frequently used enough to make it worthwhile. * series 'filelock: split file leases out of struct file_lock' of https://lore.kernel.org/r/20240131-flsplit-v3-0-c6129007ee8d@kernel.org: (47 commits) filelock: split leases out of struct file_lock filelock: remove temporary compatibility macros smb/server: adapt to breakup of struct file_lock smb/client: adapt to breakup of struct file_lock ocfs2: adapt to breakup of struct file_lock nfsd: adapt to breakup of struct file_lock nfs: adapt to breakup of struct file_lock lockd: adapt to breakup of struct file_lock fuse: adapt to breakup of struct file_lock gfs2: adapt to breakup of struct file_lock dlm: adapt to breakup of struct file_lock ceph: adapt to breakup of struct file_lock afs: adapt to breakup of struct file_lock 9p: adapt to breakup of struct file_lock filelock: convert seqfile handling to use file_lock_core filelock: convert locks_translate_pid to take file_lock_core filelock: convert locks_insert_lock_ctx and locks_delete_lock_ctx filelock: convert locks_wake_up_blocks to take a file_lock_core pointer filelock: make assign_type helper take a file_lock_core pointer filelock: reorganize locks_delete_block and __locks_insert_block ... Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents 6613476 + c69ff40 commit c4b3ffb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1085
-933
lines changed

fs/9p/vfs_file.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
107107

108108
p9_debug(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
109109

110-
if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
110+
if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->c.flc_type != F_UNLCK) {
111111
filemap_write_and_wait(inode->i_mapping);
112112
invalidate_mapping_pages(&inode->i_data, 0, -1);
113113
}
@@ -121,13 +121,12 @@ static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
121121
struct p9_fid *fid;
122122
uint8_t status = P9_LOCK_ERROR;
123123
int res = 0;
124-
unsigned char fl_type;
125124
struct v9fs_session_info *v9ses;
126125

127126
fid = filp->private_data;
128127
BUG_ON(fid == NULL);
129128

130-
BUG_ON((fl->fl_flags & FL_POSIX) != FL_POSIX);
129+
BUG_ON((fl->c.flc_flags & FL_POSIX) != FL_POSIX);
131130

132131
res = locks_lock_file_wait(filp, fl);
133132
if (res < 0)
@@ -136,7 +135,7 @@ static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
136135
/* convert posix lock to p9 tlock args */
137136
memset(&flock, 0, sizeof(flock));
138137
/* map the lock type */
139-
switch (fl->fl_type) {
138+
switch (fl->c.flc_type) {
140139
case F_RDLCK:
141140
flock.type = P9_LOCK_TYPE_RDLCK;
142141
break;
@@ -152,7 +151,7 @@ static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
152151
flock.length = 0;
153152
else
154153
flock.length = fl->fl_end - fl->fl_start + 1;
155-
flock.proc_id = fl->fl_pid;
154+
flock.proc_id = fl->c.flc_pid;
156155
flock.client_id = fid->clnt->name;
157156
if (IS_SETLKW(cmd))
158157
flock.flags = P9_LOCK_FLAGS_BLOCK;
@@ -207,12 +206,13 @@ static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
207206
* incase server returned error for lock request, revert
208207
* it locally
209208
*/
210-
if (res < 0 && fl->fl_type != F_UNLCK) {
211-
fl_type = fl->fl_type;
212-
fl->fl_type = F_UNLCK;
209+
if (res < 0 && fl->c.flc_type != F_UNLCK) {
210+
unsigned char type = fl->c.flc_type;
211+
212+
fl->c.flc_type = F_UNLCK;
213213
/* Even if this fails we want to return the remote error */
214214
locks_lock_file_wait(filp, fl);
215-
fl->fl_type = fl_type;
215+
fl->c.flc_type = type;
216216
}
217217
if (flock.client_id != fid->clnt->name)
218218
kfree(flock.client_id);
@@ -234,7 +234,7 @@ static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
234234
* if we have a conflicting lock locally, no need to validate
235235
* with server
236236
*/
237-
if (fl->fl_type != F_UNLCK)
237+
if (fl->c.flc_type != F_UNLCK)
238238
return res;
239239

240240
/* convert posix lock to p9 tgetlock args */
@@ -245,7 +245,7 @@ static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
245245
glock.length = 0;
246246
else
247247
glock.length = fl->fl_end - fl->fl_start + 1;
248-
glock.proc_id = fl->fl_pid;
248+
glock.proc_id = fl->c.flc_pid;
249249
glock.client_id = fid->clnt->name;
250250

251251
res = p9_client_getlock_dotl(fid, &glock);
@@ -254,13 +254,13 @@ static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
254254
/* map 9p lock type to os lock type */
255255
switch (glock.type) {
256256
case P9_LOCK_TYPE_RDLCK:
257-
fl->fl_type = F_RDLCK;
257+
fl->c.flc_type = F_RDLCK;
258258
break;
259259
case P9_LOCK_TYPE_WRLCK:
260-
fl->fl_type = F_WRLCK;
260+
fl->c.flc_type = F_WRLCK;
261261
break;
262262
case P9_LOCK_TYPE_UNLCK:
263-
fl->fl_type = F_UNLCK;
263+
fl->c.flc_type = F_UNLCK;
264264
break;
265265
}
266266
if (glock.type != P9_LOCK_TYPE_UNLCK) {
@@ -269,7 +269,7 @@ static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
269269
fl->fl_end = OFFSET_MAX;
270270
else
271271
fl->fl_end = glock.start + glock.length - 1;
272-
fl->fl_pid = -glock.proc_id;
272+
fl->c.flc_pid = -glock.proc_id;
273273
}
274274
out:
275275
if (glock.client_id != fid->clnt->name)
@@ -293,7 +293,7 @@ static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
293293
p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
294294
filp, cmd, fl, filp);
295295

296-
if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
296+
if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->c.flc_type != F_UNLCK) {
297297
filemap_write_and_wait(inode->i_mapping);
298298
invalidate_mapping_pages(&inode->i_data, 0, -1);
299299
}
@@ -324,16 +324,16 @@ static int v9fs_file_flock_dotl(struct file *filp, int cmd,
324324
p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
325325
filp, cmd, fl, filp);
326326

327-
if (!(fl->fl_flags & FL_FLOCK))
327+
if (!(fl->c.flc_flags & FL_FLOCK))
328328
goto out_err;
329329

330-
if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
330+
if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->c.flc_type != F_UNLCK) {
331331
filemap_write_and_wait(inode->i_mapping);
332332
invalidate_mapping_pages(&inode->i_data, 0, -1);
333333
}
334334
/* Convert flock to posix lock */
335-
fl->fl_flags |= FL_POSIX;
336-
fl->fl_flags ^= FL_FLOCK;
335+
fl->c.flc_flags |= FL_POSIX;
336+
fl->c.flc_flags ^= FL_FLOCK;
337337

338338
if (IS_SETLK(cmd) | IS_SETLKW(cmd))
339339
ret = v9fs_file_do_lock(filp, cmd, fl);

fs/afs/flock.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ static void afs_grant_locks(struct afs_vnode *vnode)
9393
bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE);
9494

9595
list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
96-
if (!exclusive && p->fl_type == F_WRLCK)
96+
if (!exclusive && lock_is_write(p))
9797
continue;
9898

9999
list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks);
100100
p->fl_u.afs.state = AFS_LOCK_GRANTED;
101101
trace_afs_flock_op(vnode, p, afs_flock_op_grant);
102-
wake_up(&p->fl_wait);
102+
locks_wake_up(p);
103103
}
104104
}
105105

@@ -112,25 +112,24 @@ static void afs_next_locker(struct afs_vnode *vnode, int error)
112112
{
113113
struct file_lock *p, *_p, *next = NULL;
114114
struct key *key = vnode->lock_key;
115-
unsigned int fl_type = F_RDLCK;
115+
unsigned int type = F_RDLCK;
116116

117117
_enter("");
118118

119119
if (vnode->lock_type == AFS_LOCK_WRITE)
120-
fl_type = F_WRLCK;
120+
type = F_WRLCK;
121121

122122
list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
123123
if (error &&
124-
p->fl_type == fl_type &&
125-
afs_file_key(p->fl_file) == key) {
124+
p->c.flc_type == type &&
125+
afs_file_key(p->c.flc_file) == key) {
126126
list_del_init(&p->fl_u.afs.link);
127127
p->fl_u.afs.state = error;
128-
wake_up(&p->fl_wait);
128+
locks_wake_up(p);
129129
}
130130

131131
/* Select the next locker to hand off to. */
132-
if (next &&
133-
(next->fl_type == F_WRLCK || p->fl_type == F_RDLCK))
132+
if (next && (lock_is_write(next) || lock_is_read(p)))
134133
continue;
135134
next = p;
136135
}
@@ -142,7 +141,7 @@ static void afs_next_locker(struct afs_vnode *vnode, int error)
142141
afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
143142
next->fl_u.afs.state = AFS_LOCK_YOUR_TRY;
144143
trace_afs_flock_op(vnode, next, afs_flock_op_wake);
145-
wake_up(&next->fl_wait);
144+
locks_wake_up(next);
146145
} else {
147146
afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE);
148147
trace_afs_flock_ev(vnode, NULL, afs_flock_no_lockers, 0);
@@ -166,7 +165,7 @@ static void afs_kill_lockers_enoent(struct afs_vnode *vnode)
166165
struct file_lock, fl_u.afs.link);
167166
list_del_init(&p->fl_u.afs.link);
168167
p->fl_u.afs.state = -ENOENT;
169-
wake_up(&p->fl_wait);
168+
locks_wake_up(p);
170169
}
171170

172171
key_put(vnode->lock_key);
@@ -464,14 +463,14 @@ static int afs_do_setlk(struct file *file, struct file_lock *fl)
464463

465464
_enter("{%llx:%llu},%llu-%llu,%u,%u",
466465
vnode->fid.vid, vnode->fid.vnode,
467-
fl->fl_start, fl->fl_end, fl->fl_type, mode);
466+
fl->fl_start, fl->fl_end, fl->c.flc_type, mode);
468467

469468
fl->fl_ops = &afs_lock_ops;
470469
INIT_LIST_HEAD(&fl->fl_u.afs.link);
471470
fl->fl_u.afs.state = AFS_LOCK_PENDING;
472471

473472
partial = (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX);
474-
type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
473+
type = lock_is_read(fl) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
475474
if (mode == afs_flock_mode_write && partial)
476475
type = AFS_LOCK_WRITE;
477476

@@ -524,7 +523,7 @@ static int afs_do_setlk(struct file *file, struct file_lock *fl)
524523
}
525524

526525
if (vnode->lock_state == AFS_VNODE_LOCK_NONE &&
527-
!(fl->fl_flags & FL_SLEEP)) {
526+
!(fl->c.flc_flags & FL_SLEEP)) {
528527
ret = -EAGAIN;
529528
if (type == AFS_LOCK_READ) {
530529
if (vnode->status.lock_count == -1)
@@ -621,7 +620,7 @@ static int afs_do_setlk(struct file *file, struct file_lock *fl)
621620
return 0;
622621

623622
lock_is_contended:
624-
if (!(fl->fl_flags & FL_SLEEP)) {
623+
if (!(fl->c.flc_flags & FL_SLEEP)) {
625624
list_del_init(&fl->fl_u.afs.link);
626625
afs_next_locker(vnode, 0);
627626
ret = -EAGAIN;
@@ -641,7 +640,7 @@ static int afs_do_setlk(struct file *file, struct file_lock *fl)
641640
spin_unlock(&vnode->lock);
642641

643642
trace_afs_flock_ev(vnode, fl, afs_flock_waiting, 0);
644-
ret = wait_event_interruptible(fl->fl_wait,
643+
ret = wait_event_interruptible(fl->c.flc_wait,
645644
fl->fl_u.afs.state != AFS_LOCK_PENDING);
646645
trace_afs_flock_ev(vnode, fl, afs_flock_waited, ret);
647646

@@ -704,7 +703,8 @@ static int afs_do_unlk(struct file *file, struct file_lock *fl)
704703
struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
705704
int ret;
706705

707-
_enter("{%llx:%llu},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
706+
_enter("{%llx:%llu},%u", vnode->fid.vid, vnode->fid.vnode,
707+
fl->c.flc_type);
708708

709709
trace_afs_flock_op(vnode, fl, afs_flock_op_unlock);
710710

@@ -730,11 +730,11 @@ static int afs_do_getlk(struct file *file, struct file_lock *fl)
730730
if (vnode->lock_state == AFS_VNODE_LOCK_DELETED)
731731
return -ENOENT;
732732

733-
fl->fl_type = F_UNLCK;
733+
fl->c.flc_type = F_UNLCK;
734734

735735
/* check local lock records first */
736736
posix_test_lock(file, fl);
737-
if (fl->fl_type == F_UNLCK) {
737+
if (lock_is_unlock(fl)) {
738738
/* no local locks; consult the server */
739739
ret = afs_fetch_status(vnode, key, false, NULL);
740740
if (ret < 0)
@@ -743,18 +743,18 @@ static int afs_do_getlk(struct file *file, struct file_lock *fl)
743743
lock_count = READ_ONCE(vnode->status.lock_count);
744744
if (lock_count != 0) {
745745
if (lock_count > 0)
746-
fl->fl_type = F_RDLCK;
746+
fl->c.flc_type = F_RDLCK;
747747
else
748-
fl->fl_type = F_WRLCK;
748+
fl->c.flc_type = F_WRLCK;
749749
fl->fl_start = 0;
750750
fl->fl_end = OFFSET_MAX;
751-
fl->fl_pid = 0;
751+
fl->c.flc_pid = 0;
752752
}
753753
}
754754

755755
ret = 0;
756756
error:
757-
_leave(" = %d [%hd]", ret, fl->fl_type);
757+
_leave(" = %d [%hd]", ret, fl->c.flc_type);
758758
return ret;
759759
}
760760

@@ -769,7 +769,7 @@ int afs_lock(struct file *file, int cmd, struct file_lock *fl)
769769

770770
_enter("{%llx:%llu},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
771771
vnode->fid.vid, vnode->fid.vnode, cmd,
772-
fl->fl_type, fl->fl_flags,
772+
fl->c.flc_type, fl->c.flc_flags,
773773
(long long) fl->fl_start, (long long) fl->fl_end);
774774

775775
if (IS_GETLK(cmd))
@@ -778,7 +778,7 @@ int afs_lock(struct file *file, int cmd, struct file_lock *fl)
778778
fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
779779
trace_afs_flock_op(vnode, fl, afs_flock_op_lock);
780780

781-
if (fl->fl_type == F_UNLCK)
781+
if (lock_is_unlock(fl))
782782
ret = afs_do_unlk(file, fl);
783783
else
784784
ret = afs_do_setlk(file, fl);
@@ -804,7 +804,7 @@ int afs_flock(struct file *file, int cmd, struct file_lock *fl)
804804

805805
_enter("{%llx:%llu},%d,{t=%x,fl=%x}",
806806
vnode->fid.vid, vnode->fid.vnode, cmd,
807-
fl->fl_type, fl->fl_flags);
807+
fl->c.flc_type, fl->c.flc_flags);
808808

809809
/*
810810
* No BSD flocks over NFS allowed.
@@ -813,14 +813,14 @@ int afs_flock(struct file *file, int cmd, struct file_lock *fl)
813813
* Not sure whether that would be unique, though, or whether
814814
* that would break in other places.
815815
*/
816-
if (!(fl->fl_flags & FL_FLOCK))
816+
if (!(fl->c.flc_flags & FL_FLOCK))
817817
return -ENOLCK;
818818

819819
fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
820820
trace_afs_flock_op(vnode, fl, afs_flock_op_flock);
821821

822822
/* we're simulating flock() locks using posix locks on the server */
823-
if (fl->fl_type == F_UNLCK)
823+
if (lock_is_unlock(fl))
824824
ret = afs_do_unlk(file, fl);
825825
else
826826
ret = afs_do_setlk(file, fl);
@@ -843,7 +843,7 @@ int afs_flock(struct file *file, int cmd, struct file_lock *fl)
843843
*/
844844
static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
845845
{
846-
struct afs_vnode *vnode = AFS_FS_I(file_inode(fl->fl_file));
846+
struct afs_vnode *vnode = AFS_FS_I(file_inode(fl->c.flc_file));
847847

848848
_enter("");
849849

@@ -861,7 +861,7 @@ static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
861861
*/
862862
static void afs_fl_release_private(struct file_lock *fl)
863863
{
864-
struct afs_vnode *vnode = AFS_FS_I(file_inode(fl->fl_file));
864+
struct afs_vnode *vnode = AFS_FS_I(file_inode(fl->c.flc_file));
865865

866866
_enter("");
867867

0 commit comments

Comments
 (0)