// SPDX-License-Identifier: GPL-2.0-or-later
/* AFS file locking support
*
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#include "internal.h"
#define AFS_LOCK_GRANTED 0
#define AFS_LOCK_PENDING 1
#define AFS_LOCK_YOUR_TRY 2
struct workqueue_struct *afs_lock_manager;
static void afs_next_locker(struct afs_vnode *vnode, int error);
static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
static void afs_fl_release_private(struct file_lock *fl);
static const struct file_lock_operations afs_lock_ops = {
.fl_copy_lock = afs_fl_copy_lock,
.fl_release_private = afs_fl_release_private,
};
static inline void afs_set_lock_state(struct afs_vnode *vnode, enum afs_lock_state state)
{
_debug("STATE %u -> %u", vnode->lock_state, state);
vnode->lock_state = state;
}
static atomic_t afs_file_lock_debug_id;
/*
* if the callback is broken on this vnode, then the lock may now be available
*/
void afs_lock_may_be_available(struct afs_vnode *vnode)
{
_enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
spin_lock(&vnode->lock);
if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
afs_next_locker(vnode, 0);
trace_afs_flock_ev(vnode, NULL, afs_flock_callback_break, 0);
spin_unlock(&vnode->lock);
}
/*
* the lock will time out in 5 minutes unless we extend it, so schedule
* extension in a bit less than that time
*/
static void afs_schedule_lock_extension(struct afs_vnode *vnode)
{
ktime_t expires_at, now, duration;
u64 duration_j;
expires_at = ktime_add_ms(vnode->locked_at, AFS_LOCKWAIT * 1000 / 2);
now = ktime_get_real();
duration = ktime_sub(expires_at, now);
if (duration <= 0)
duration_j = 0;
else
duration_j = nsecs_to_jiffies(ktime_to_ns(duration));
queue_delayed_work(afs_lock_manager, &vnode->lock_work, duration_j);
}
/*
* In the case of successful completion of a lock operation, record the time
* the reply appeared and start the lock extension timer.
*/
void afs_lock_op_done(struct afs_call *call)
{
struct afs_vnode *vnode = call->lvnode;
if (call->error == 0) {
spin_lock(&vnode->lock);
trace_afs_flock_ev(vnode, NULL, afs_flock_timestamp, 0);
vnode->locked_at = call->reply_time;
afs_schedule_lock_extension(vnode);
spin_unlock(&vnode->lock);
}
}
/*
* grant one or more locks (readlocks are allowed to jump the queue if the
* first lock in the queue is itself a readlock)
* - the caller must hold the vnode lock
*/
static void afs_grant_locks(struct afs_vnode *vnode)
{
struct file_lock *p, *_p;
bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE);
list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
if (!exclusive && p->fl_type == F_WRLCK)
continue;
list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks);
p->fl_u.afs.state = AFS_LOCK_GRANTED;
trace_afs_flock_op(vnode, p, afs_flock_op_grant);
wake_up(&p->fl_wait);
}
}
/*
* If an error is specified, reject every pending lock that matches the
* authentication and type of the lock we failed to get. If there are any
* remaining lockers, try to wake up one of them to have a go.
*/
static void afs_next_locker(struct afs_vnode *vnode, int error)
{
struct file_lock *p, *_p, *next = NULL;
struct key *key = vnode->lock_key;
unsigned int fl_type = F_RDLCK;
_enter("");
if (vnode->lock_type == AFS_LOCK_WRITE)
fl_type = F_WRLCK;
list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
if (error &&
p->fl_type == fl_type &&
afs_file_key(p->fl_file) == key) {
list_del_init(&p->fl_u.afs.link);
p->fl_u.afs.state = error;
wake_up(&p->fl_wait);
}
/* Select the next locker to hand off to. */
if (next &&
(next->fl_type == F_WRLCK || p->fl_type == F_RDLCK))
continue;
next = p;
}
vnode->lock_key = NULL;
key_put(key);
if (next) {
afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
next->fl_u.afs.state = AFS_LOCK_YOUR_TRY;
trace_afs_flock_op(vnode, next, afs_flock_op_wake);
wake_up(&next->fl_wait);
} else {
afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE);
trace_afs_flock_ev(vnode, NULL, afs_flock_no_lockers, 0);
}
_leave("");
}
/*
* Kill off all waiters in the the pending lock queue due to the vnode being
* deleted.
*/
static void afs_kill_lockers_enoent(struct afs_vnode *vnode)
{
struct file_lock *p;
afs_set_lock_state(vnode, AFS_VNODE_LO