Skip to content

Commit

Permalink
locks: new helpers - flock_lock_inode_wait and posix_lock_inode_wait
Browse files Browse the repository at this point in the history
Allow callers to pass in an inode instead of a filp.

Signed-off-by: Jeff Layton <jeff.layton@primarydata.com>
Reviewed-by: "J. Bruce Fields" <bfields@fieldses.org>
Tested-by: "J. Bruce Fields" <bfields@fieldses.org>
  • Loading branch information
Jeff Layton committed Jul 13, 2015
1 parent bcd7f78 commit 29d01b2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
50 changes: 38 additions & 12 deletions fs/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1163,20 +1163,19 @@ int posix_lock_file(struct file *filp, struct file_lock *fl,
EXPORT_SYMBOL(posix_lock_file);

/**
* posix_lock_file_wait - Apply a POSIX-style lock to a file
* @filp: The file to apply the lock to
* posix_lock_inode_wait - Apply a POSIX-style lock to a file
* @inode: inode of file to which lock request should be applied
* @fl: The lock to be applied
*
* Add a POSIX style lock to a file.
* We merge adjacent & overlapping locks whenever possible.
* POSIX locks are sorted by owner task, then by starting address
* Variant of posix_lock_file_wait that does not take a filp, and so can be
* used after the filp has already been torn down.
*/
int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
{
int error;
might_sleep ();
for (;;) {
error = posix_lock_file(filp, fl, NULL);
error = __posix_lock_file(inode, fl, NULL);
if (error != FILE_LOCK_DEFERRED)
break;
error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
Expand All @@ -1188,6 +1187,21 @@ int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
}
return error;
}
EXPORT_SYMBOL(posix_lock_inode_wait);

/**
* posix_lock_file_wait - Apply a POSIX-style lock to a file
* @filp: The file to apply the lock to
* @fl: The lock to be applied
*
* Add a POSIX style lock to a file.
* We merge adjacent & overlapping locks whenever possible.
* POSIX locks are sorted by owner task, then by starting address
*/
int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
{
return posix_lock_inode_wait(file_inode(filp), fl);
}
EXPORT_SYMBOL(posix_lock_file_wait);

/**
Expand Down Expand Up @@ -1850,18 +1864,18 @@ int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
}

/**
* flock_lock_file_wait - Apply a FLOCK-style lock to a file
* @filp: The file to apply the lock to
* flock_lock_inode_wait - Apply a FLOCK-style lock to a file
* @inode: inode of the file to apply to
* @fl: The lock to be applied
*
* Add a FLOCK style lock to a file.
* Apply a FLOCK style lock request to an inode.
*/
int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
{
int error;
might_sleep();
for (;;) {
error = flock_lock_inode(file_inode(filp), fl);
error = flock_lock_inode(inode, fl);
if (error != FILE_LOCK_DEFERRED)
break;
error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
Expand All @@ -1873,7 +1887,19 @@ int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
}
return error;
}
EXPORT_SYMBOL(flock_lock_inode_wait);

/**
* flock_lock_file_wait - Apply a FLOCK-style lock to a file
* @filp: The file to apply the lock to
* @fl: The lock to be applied
*
* Add a FLOCK style lock to a file.
*/
int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
{
return flock_lock_inode_wait(file_inode(filp), fl);
}
EXPORT_SYMBOL(flock_lock_file_wait);

/**
Expand Down
14 changes: 14 additions & 0 deletions include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,13 @@ extern void locks_remove_file(struct file *);
extern void locks_release_private(struct file_lock *);
extern void posix_test_lock(struct file *, struct file_lock *);
extern int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
extern int posix_lock_inode_wait(struct inode *, struct file_lock *);
extern int posix_lock_file_wait(struct file *, struct file_lock *);
extern int posix_unblock_lock(struct file_lock *);
extern int vfs_test_lock(struct file *, struct file_lock *);
extern int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
extern int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
extern int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl);
extern int flock_lock_file_wait(struct file *filp, struct file_lock *fl);
extern int __break_lease(struct inode *inode, unsigned int flags, unsigned int type);
extern void lease_get_mtime(struct inode *, struct timespec *time);
Expand Down Expand Up @@ -1137,6 +1139,12 @@ static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
return -ENOLCK;
}

static inline int posix_lock_inode_wait(struct inode *inode,
struct file_lock *fl)
{
return -ENOLCK;
}

static inline int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
{
return -ENOLCK;
Expand All @@ -1163,6 +1171,12 @@ static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
return 0;
}

static inline int flock_lock_inode_wait(struct inode *inode,
struct file_lock *request)
{
return -ENOLCK;
}

static inline int flock_lock_file_wait(struct file *filp,
struct file_lock *request)
{
Expand Down

0 comments on commit 29d01b2

Please sign in to comment.