Commit 75cabec0 authored by Jeff Layton's avatar Jeff Layton Committed by Christian Brauner

filelock: add some new helper functions

In later patches we're going to embed some common fields into a new
structure inside struct file_lock. Smooth the transition by adding some
new helper functions, and converting the core file locking code to use
them.
Signed-off-by: default avatarJeff Layton <jlayton@kernel.org>
Link: https://lore.kernel.org/r/20240131-flsplit-v3-4-c6129007ee8d@kernel.orgReviewed-by: default avatarNeilBrown <neilb@suse.de>
Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent 6021d62c
...@@ -674,7 +674,7 @@ static void __locks_wake_up_blocks(struct file_lock *blocker) ...@@ -674,7 +674,7 @@ static void __locks_wake_up_blocks(struct file_lock *blocker)
if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
waiter->fl_lmops->lm_notify(waiter); waiter->fl_lmops->lm_notify(waiter);
else else
wake_up(&waiter->fl_wait); locks_wake_up(waiter);
/* /*
* The setting of fl_blocker to NULL marks the "done" * The setting of fl_blocker to NULL marks the "done"
...@@ -841,9 +841,9 @@ locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose) ...@@ -841,9 +841,9 @@ locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
static bool locks_conflict(struct file_lock *caller_fl, static bool locks_conflict(struct file_lock *caller_fl,
struct file_lock *sys_fl) struct file_lock *sys_fl)
{ {
if (sys_fl->fl_type == F_WRLCK) if (lock_is_write(sys_fl))
return true; return true;
if (caller_fl->fl_type == F_WRLCK) if (lock_is_write(caller_fl))
return true; return true;
return false; return false;
} }
...@@ -874,7 +874,7 @@ static bool posix_test_locks_conflict(struct file_lock *caller_fl, ...@@ -874,7 +874,7 @@ static bool posix_test_locks_conflict(struct file_lock *caller_fl,
struct file_lock *sys_fl) struct file_lock *sys_fl)
{ {
/* F_UNLCK checks any locks on the same fd. */ /* F_UNLCK checks any locks on the same fd. */
if (caller_fl->fl_type == F_UNLCK) { if (lock_is_unlock(caller_fl)) {
if (!posix_same_owner(caller_fl, sys_fl)) if (!posix_same_owner(caller_fl, sys_fl))
return false; return false;
return locks_overlap(caller_fl, sys_fl); return locks_overlap(caller_fl, sys_fl);
...@@ -1055,7 +1055,7 @@ static int flock_lock_inode(struct inode *inode, struct file_lock *request) ...@@ -1055,7 +1055,7 @@ static int flock_lock_inode(struct inode *inode, struct file_lock *request)
break; break;
} }
if (request->fl_type == F_UNLCK) { if (lock_is_unlock(request)) {
if ((request->fl_flags & FL_EXISTS) && !found) if ((request->fl_flags & FL_EXISTS) && !found)
error = -ENOENT; error = -ENOENT;
goto out; goto out;
...@@ -1107,7 +1107,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request, ...@@ -1107,7 +1107,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
ctx = locks_get_lock_context(inode, request->fl_type); ctx = locks_get_lock_context(inode, request->fl_type);
if (!ctx) if (!ctx)
return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM; return lock_is_unlock(request) ? 0 : -ENOMEM;
/* /*
* We may need two file_lock structures for this operation, * We may need two file_lock structures for this operation,
...@@ -1228,7 +1228,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request, ...@@ -1228,7 +1228,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
continue; continue;
if (fl->fl_start > request->fl_end) if (fl->fl_start > request->fl_end)
break; break;
if (request->fl_type == F_UNLCK) if (lock_is_unlock(request))
added = true; added = true;
if (fl->fl_start < request->fl_start) if (fl->fl_start < request->fl_start)
left = fl; left = fl;
...@@ -1279,7 +1279,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request, ...@@ -1279,7 +1279,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
error = 0; error = 0;
if (!added) { if (!added) {
if (request->fl_type == F_UNLCK) { if (lock_is_unlock(request)) {
if (request->fl_flags & FL_EXISTS) if (request->fl_flags & FL_EXISTS)
error = -ENOENT; error = -ENOENT;
goto out; goto out;
...@@ -1608,7 +1608,7 @@ void lease_get_mtime(struct inode *inode, struct timespec64 *time) ...@@ -1608,7 +1608,7 @@ void lease_get_mtime(struct inode *inode, struct timespec64 *time)
spin_lock(&ctx->flc_lock); spin_lock(&ctx->flc_lock);
fl = list_first_entry_or_null(&ctx->flc_lease, fl = list_first_entry_or_null(&ctx->flc_lease,
struct file_lock, fl_list); struct file_lock, fl_list);
if (fl && (fl->fl_type == F_WRLCK)) if (fl && lock_is_write(fl))
has_lease = true; has_lease = true;
spin_unlock(&ctx->flc_lock); spin_unlock(&ctx->flc_lock);
} }
......
...@@ -147,6 +147,29 @@ int fcntl_setlk64(unsigned int, struct file *, unsigned int, ...@@ -147,6 +147,29 @@ int fcntl_setlk64(unsigned int, struct file *, unsigned int,
int fcntl_setlease(unsigned int fd, struct file *filp, int arg); int fcntl_setlease(unsigned int fd, struct file *filp, int arg);
int fcntl_getlease(struct file *filp); int fcntl_getlease(struct file *filp);
static inline bool lock_is_unlock(struct file_lock *fl)
{
return fl->fl_type == F_UNLCK;
}
static inline bool lock_is_read(struct file_lock *fl)
{
return fl->fl_type == F_RDLCK;
}
static inline bool lock_is_write(struct file_lock *fl)
{
return fl->fl_type == F_WRLCK;
}
static inline void locks_wake_up(struct file_lock *fl)
{
wake_up(&fl->fl_wait);
}
/* for walking lists of file_locks linked by fl_list */
#define for_each_file_lock(_fl, _head) list_for_each_entry(_fl, _head, fl_list)
/* fs/locks.c */ /* fs/locks.c */
void locks_free_lock_context(struct inode *inode); void locks_free_lock_context(struct inode *inode);
void locks_free_lock(struct file_lock *fl); void locks_free_lock(struct file_lock *fl);
...@@ -223,6 +246,27 @@ static inline int fcntl_getlease(struct file *filp) ...@@ -223,6 +246,27 @@ static inline int fcntl_getlease(struct file *filp)
return F_UNLCK; return F_UNLCK;
} }
static inline bool lock_is_unlock(struct file_lock *fl)
{
return false;
}
static inline bool lock_is_read(struct file_lock *fl)
{
return false;
}
static inline bool lock_is_write(struct file_lock *fl)
{
return false;
}
static inline void locks_wake_up(struct file_lock *fl)
{
}
#define for_each_file_lock(_fl, _head) while(false)
static inline void static inline void
locks_free_lock_context(struct inode *inode) locks_free_lock_context(struct inode *inode)
{ {
......
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