#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vfs_lockf.c,v 1.83 2024/12/07 02:27:38 riastradh Exp $");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/atomic.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/lockf.h>
#include <sys/proc.h>
#include <sys/sdt.h>
#include <sys/systm.h>
#include <sys/uidinfo.h>
#include <sys/vnode.h>
TAILQ_HEAD(locklist, lockf);
struct lockf {
kcondvar_t lf_cv;
short lf_flags;
short lf_type;
off_t lf_start;
off_t lf_end;
void *lf_id;
struct lockf **lf_head;
struct lockf *lf_next;
struct locklist lf_blkhd;
TAILQ_ENTRY(lockf) lf_block;
struct uidinfo *lf_uip;
};
#define MAXDEPTH 50
static kmutex_t lockf_lock __cacheline_aligned;
static char lockstr[] = "lockf";
int maxlockdepth = MAXDEPTH;
#ifdef LOCKF_DEBUG
int lockf_debug = 0;
#endif
#define SELF 0x1
#define OTHERS 0x2
#define MAXLOCKSPERUID (2 * maxfiles)
#ifdef LOCKF_DEBUG
static void
lf_print(const char *tag, struct lockf *lock)
{
printf("%s: lock %p for ", tag, lock);
if (lock->lf_flags & F_POSIX)
printf("proc %d", ((struct proc *)lock->lf_id)->p_pid);
else
printf("file %p", (struct file *)lock->lf_id);
printf(" %s, start %jd, end %jd",
lock->lf_type == F_RDLCK ? "shared" :
lock->lf_type == F_WRLCK ? "exclusive" :
lock->lf_type == F_UNLCK ? "unlock" :
"unknown", (intmax_t)lock->lf_start, (intmax_t)lock->lf_end);
if (TAILQ_FIRST(&lock->lf_blkhd))
printf(" block %p\n", TAILQ_FIRST(&lock->lf_blkhd));
else
printf("\n");
}
static void
lf_printlist(const char *tag, struct lockf *lock)
{
struct lockf *lf, *blk;
printf("%s: Lock list:\n", tag);
for (lf = *lock->lf_head; lf; lf = lf->lf_next) {
printf("\tlock %p for ", lf);
if (lf->lf_flags & F_POSIX)
printf("proc %d", ((struct proc *)lf->lf_id)->p_pid);
else
printf("file %p", (struct file *)lf->lf_id);
printf(", %s, start %jd, end %jd",
lf->lf_type == F_RDLCK ? "shared" :
lf->lf_type == F_WRLCK ? "exclusive" :
lf->lf_type == F_UNLCK ? "unlock" :
"unknown", (intmax_t)lf->lf_start, (intmax_t)lf->lf_end);
TAILQ_FOREACH(blk, &lf->lf_blkhd, lf_block) {
if (blk->lf_flags & F_POSIX)
printf("; proc %d",
((struct proc *)blk->lf_id)->p_pid);
else
printf("; file %p", (struct file *)blk->lf_id);
printf(", %s, start %jd, end %jd",
blk->lf_type == F_RDLCK ? "shared" :
blk->lf_type == F_WRLCK ? "exclusive" :
blk->lf_type == F_UNLCK ? "unlock" :
"unknown",
(intmax_t)blk->lf_start, (intmax_t)blk->lf_end);
if (TAILQ_FIRST(&blk->lf_blkhd))
panic("lf_printlist: bad list");
}
printf("\n");
}
}
#endif
static struct lockf *
lf_alloc(int allowfail)
{
struct uidinfo *uip;
struct lockf *lock;
u_long lcnt;
const uid_t uid = kauth_cred_geteuid(kauth_cred_get());
uip = uid_find(uid);
lcnt = atomic_inc_ulong_nv(&uip->ui_lockcnt);
if (uid && allowfail && lcnt >
(allowfail == 1 ? MAXLOCKSPERUID : (MAXLOCKSPERUID * 2))) {
atomic_dec_ulong(&uip->ui_lockcnt);
return NULL;
}
lock = kmem_alloc(sizeof(*lock), KM_SLEEP);
lock->lf_uip = uip;
cv_init(&lock->lf_cv, lockstr);
return lock;
}
static void
lf_free(struct lockf *lock)
{
atomic_dec_ulong(&lock->lf_uip->ui_lockcnt);
cv_destroy(&lock->lf_cv);
kmem_free(lock, sizeof(*lock));
}
static int
lf_findoverlap(struct lockf *lf, struct lockf *lock, int type,
struct lockf ***prev, struct lockf **overlap)
{
off_t start, end;
*overlap = lf;
if (lf == NULL)
return 0;
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
lf_print("lf_findoverlap: looking for overlap in", lock);
#endif
start = lock->lf_start;
end = lock->lf_end;
while (lf != NULL) {
if (((type == SELF) && lf->lf_id != lock->lf_id) ||
((type == OTHERS) && lf->lf_id == lock->lf_id)) {
*prev = &lf->lf_next;
*overlap = lf = lf->lf_next;
continue;
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
lf_print("\tchecking", lf);
#endif
if ((lf->lf_end != -1 && start > lf->lf_end) ||
(end != -1 && lf->lf_start > end)) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("no overlap\n");
#endif
if ((type & SELF) && end != -1 && lf->lf_start > end)
return 0;
*prev = &lf->lf_next;
*overlap = lf = lf->lf_next;
continue;
}
if ((lf->lf_start == start) && (lf->lf_end == end)) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("overlap == lock\n");
#endif
return 1;
}
if ((lf->lf_start <= start) &&
(end != -1) &&
((lf->lf_end >= end) || (lf->lf_end == -1))) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("overlap contains lock\n");
#endif
return 2;
}
if (start <= lf->lf_start &&
(end == -1 ||
(lf->lf_end != -1 && end >= lf->lf_end))) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("lock contains overlap\n");
#endif
return 3;
}
if ((lf->lf_start < start) &&
((lf->lf_end >= start) || (lf->lf_end == -1))) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("overlap starts before lock\n");
#endif
return 4;
}
if ((lf->lf_start > start) &&
(end != -1) &&
((lf->lf_end > end) || (lf->lf_end == -1))) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("overlap ends after lock\n");
#endif
return 5;
}
panic("lf_findoverlap: default");
}
return 0;
}
static void
lf_split(struct lockf *lock1, struct lockf *lock2, struct lockf **sparelock)
{
struct lockf *splitlock;
#ifdef LOCKF_DEBUG
if (lockf_debug & 2) {
lf_print("lf_split", lock1);
lf_print("splitting from", lock2);
}
#endif
if (lock1->lf_start == lock2->lf_start) {
lock1->lf_start = lock2->lf_end + 1;
lock2->lf_next = lock1;
return;
}
if (lock1->lf_end == lock2->lf_end) {
lock1->lf_end = lock2->lf_start - 1;
lock2->lf_next = lock1->lf_next;
lock1->lf_next = lock2;
return;
}
splitlock = *sparelock;
*sparelock = NULL;
cv_destroy(&splitlock->lf_cv);
memcpy(splitlock, lock1, sizeof(*splitlock));
cv_init(&splitlock->lf_cv, lockstr);
splitlock->lf_start = lock2->lf_end + 1;
TAILQ_INIT(&splitlock->lf_blkhd);
lock1->lf_end = lock2->lf_start - 1;
splitlock->lf_next = lock1->lf_next;
lock2->lf_next = splitlock;
lock1->lf_next = lock2;
}
static void
lf_wakelock(struct lockf *listhead)
{
struct lockf *wakelock;
while ((wakelock = TAILQ_FIRST(&listhead->lf_blkhd))) {
KASSERT(wakelock->lf_next == listhead);
TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
wakelock->lf_next = NULL;
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
lf_print("lf_wakelock: awakening", wakelock);
#endif
cv_broadcast(&wakelock->lf_cv);
}
}
static int
lf_clearlock(struct lockf *unlock, struct lockf **sparelock)
{
struct lockf **head = unlock->lf_head;
struct lockf *lf = *head;
struct lockf *overlap, **prev;
int ovcase;
if (lf == NULL)
return 0;
#ifdef LOCKF_DEBUG
if (unlock->lf_type != F_UNLCK)
panic("lf_clearlock: bad type");
if (lockf_debug & 1)
lf_print("lf_clearlock", unlock);
#endif
prev = head;
while ((ovcase = lf_findoverlap(lf, unlock, SELF,
&prev, &overlap)) != 0) {
lf_wakelock(overlap);
switch (ovcase) {
case 1:
*prev = overlap->lf_next;
lf_free(overlap);
break;
case 2:
if (overlap->lf_start == unlock->lf_start) {
overlap->lf_start = unlock->lf_end + 1;
break;
}
lf_split(overlap, unlock, sparelock);
overlap->lf_next = unlock->lf_next;
break;
case 3:
*prev = overlap->lf_next;
lf = overlap->lf_next;
lf_free(overlap);
continue;
case 4:
overlap->lf_end = unlock->lf_start - 1;
prev = &overlap->lf_next;
lf = overlap->lf_next;
continue;
case 5:
overlap->lf_start = unlock->lf_end + 1;
break;
}
break;
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
lf_printlist("lf_clearlock", unlock);
#endif
return 0;
}
static struct lockf *
lf_getblock(struct lockf *lock)
{
struct lockf **prev, *overlap, *lf = *(lock->lf_head);
prev = lock->lf_head;
while (lf_findoverlap(lf, lock, OTHERS, &prev, &overlap) != 0) {
if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
return overlap;
lf = overlap->lf_next;
}
return NULL;
}
static int
lf_setlock(struct lockf *lock, struct lockf **sparelock,
kmutex_t *interlock)
{
struct lockf *block;
struct lockf **head = lock->lf_head;
struct lockf **prev, *overlap, *ltmp;
int ovcase, needtolink, error;
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
lf_print("lf_setlock", lock);
#endif
while ((block = lf_getblock(lock)) != NULL) {
if ((lock->lf_flags & F_WAIT) == 0) {
lf_free(lock);
return SET_ERROR(EAGAIN);
}
if ((lock->lf_flags & F_POSIX) &&
(block->lf_flags & F_POSIX)) {
struct lwp *wlwp;
volatile const struct lockf *waitblock;
int i = 0;
struct proc *p;
p = (struct proc *)block->lf_id;
KASSERT(p != NULL);
while (i++ < maxlockdepth) {
mutex_enter(p->p_lock);
if (p->p_nlwps > 1) {
mutex_exit(p->p_lock);
break;
}
wlwp = LIST_FIRST(&p->p_lwps);
lwp_lock(wlwp);
if (wlwp->l_wchan == NULL ||
wlwp->l_wmesg != lockstr) {
lwp_unlock(wlwp);
mutex_exit(p->p_lock);
break;
}
waitblock = wlwp->l_wchan;
lwp_unlock(wlwp);
mutex_exit(p->p_lock);
waitblock = waitblock->lf_next;
if ((waitblock->lf_flags & F_POSIX) == 0)
break;
p = (struct proc *)waitblock->lf_id;
if (p == curproc) {
lf_free(lock);
return SET_ERROR(EDEADLK);
}
}
if (i >= maxlockdepth) {
lf_free(lock);
return SET_ERROR(EDEADLK);
}
}
if ((lock->lf_flags & F_FLOCK) &&
lock->lf_type == F_WRLCK) {
lock->lf_type = F_UNLCK;
(void) lf_clearlock(lock, NULL);
lock->lf_type = F_WRLCK;
}
lock->lf_next = block;
TAILQ_INSERT_TAIL(&block->lf_blkhd, lock, lf_block);
#ifdef LOCKF_DEBUG
if (lockf_debug & 1) {
lf_print("lf_setlock: blocking on", block);
lf_printlist("lf_setlock", block);
}
#endif
error = cv_wait_sig(&lock->lf_cv, interlock);
if (lock->lf_next != NULL) {
TAILQ_REMOVE(&lock->lf_next->lf_blkhd, lock, lf_block);
lock->lf_next = NULL;
}
if (error) {
lf_free(lock);
return error;
}
}
prev = head;
block = *head;
needtolink = 1;
for (;;) {
ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap);
if (ovcase)
block = overlap->lf_next;
switch (ovcase) {
case 0:
if (needtolink) {
*prev = lock;
lock->lf_next = overlap;
}
break;
case 1:
if (lock->lf_type == F_RDLCK &&
overlap->lf_type == F_WRLCK)
lf_wakelock(overlap);
overlap->lf_type = lock->lf_type;
lf_free(lock);
lock = overlap;
break;
case 2:
if (overlap->lf_type == lock->lf_type) {
lf_free(lock);
lock = overlap;
break;
}
if (overlap->lf_start == lock->lf_start) {
*prev = lock;
lock->lf_next = overlap;
overlap->lf_start = lock->lf_end + 1;
} else
lf_split(overlap, lock, sparelock);
lf_wakelock(overlap);
break;
case 3:
if (lock->lf_type == F_RDLCK &&
overlap->lf_type == F_WRLCK) {
lf_wakelock(overlap);
} else {
while ((ltmp =
TAILQ_FIRST(&overlap->lf_blkhd))
!= NULL) {
KASSERT(ltmp->lf_next == overlap);
TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
lf_block);
ltmp->lf_next = lock;
TAILQ_INSERT_TAIL(&lock->lf_blkhd,
ltmp, lf_block);
}
}
if (needtolink) {
*prev = lock;
lock->lf_next = overlap->lf_next;
prev = &lock->lf_next;
needtolink = 0;
} else
*prev = overlap->lf_next;
lf_free(overlap);
continue;
case 4:
lock->lf_next = overlap->lf_next;
overlap->lf_next = lock;
overlap->lf_end = lock->lf_start - 1;
prev = &lock->lf_next;
lf_wakelock(overlap);
needtolink = 0;
continue;
case 5:
if (needtolink) {
*prev = lock;
lock->lf_next = overlap;
}
overlap->lf_start = lock->lf_end + 1;
lf_wakelock(overlap);
break;
}
break;
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 1) {
lf_print("lf_setlock: got the lock", lock);
lf_printlist("lf_setlock", lock);
}
#endif
return 0;
}
static int
lf_getlock(struct lockf *lock, struct flock *fl)
{
struct lockf *block;
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
lf_print("lf_getlock", lock);
#endif
if ((block = lf_getblock(lock)) != NULL) {
fl->l_type = block->lf_type;
fl->l_whence = SEEK_SET;
fl->l_start = block->lf_start;
if (block->lf_end == -1)
fl->l_len = 0;
else
fl->l_len = block->lf_end - block->lf_start + 1;
if (block->lf_flags & F_POSIX)
fl->l_pid = ((struct proc *)block->lf_id)->p_pid;
else
fl->l_pid = -1;
} else {
fl->l_type = F_UNLCK;
}
return 0;
}
int
lf_advlock(struct vop_advlock_args *ap, struct lockf **head, off_t size)
{
struct flock *fl = ap->a_fl;
struct lockf *lock = NULL;
struct lockf *sparelock;
kmutex_t *interlock = &lockf_lock;
off_t start, end;
int error = 0;
KASSERTMSG(size >= 0, "size=%jd", (intmax_t)size);
switch (fl->l_whence) {
case SEEK_SET:
case SEEK_CUR:
start = fl->l_start;
break;
case SEEK_END:
if (fl->l_start > __type_max(off_t) - size)
return SET_ERROR(EINVAL);
start = size + fl->l_start;
break;
default:
return SET_ERROR(EINVAL);
}
if (fl->l_len == 0)
end = -1;
else {
if (fl->l_len >= 0) {
if (start >= 0 &&
fl->l_len - 1 > __type_max(off_t) - start)
return SET_ERROR(EINVAL);
end = start + (fl->l_len - 1);
} else {
if (start < 0)
return SET_ERROR(EINVAL);
end = start - 1;
start += fl->l_len;
}
}
if (start < 0)
return SET_ERROR(EINVAL);
switch (ap->a_op) {
case F_SETLK:
case F_UNLCK:
if ((ap->a_flags & F_FLOCK) == 0) {
sparelock = lf_alloc(0);
if (sparelock == NULL) {
error = SET_ERROR(ENOMEM);
goto quit;
}
break;
}
case F_GETLK:
sparelock = NULL;
break;
default:
return SET_ERROR(EINVAL);
}
switch (ap->a_op) {
case F_SETLK:
lock = lf_alloc(1);
break;
case F_UNLCK:
if (start == 0 || end == -1) {
lock = lf_alloc(0);
} else {
lock = lf_alloc(2);
}
break;
case F_GETLK:
lock = lf_alloc(0);
break;
}
if (lock == NULL) {
error = SET_ERROR(ENOMEM);
goto quit;
}
mutex_enter(interlock);
if (*head == (struct lockf *)0) {
if (ap->a_op != F_SETLK) {
fl->l_type = F_UNLCK;
error = 0;
goto quit_unlock;
}
}
lock->lf_start = start;
lock->lf_end = end;
lock->lf_head = head;
lock->lf_type = fl->l_type;
lock->lf_next = (struct lockf *)0;
TAILQ_INIT(&lock->lf_blkhd);
lock->lf_flags = ap->a_flags;
if (lock->lf_flags & F_POSIX) {
KASSERT(curproc == (struct proc *)ap->a_id);
}
lock->lf_id = ap->a_id;
switch (ap->a_op) {
case F_SETLK:
error = lf_setlock(lock, &sparelock, interlock);
lock = NULL;
break;
case F_UNLCK:
error = lf_clearlock(lock, &sparelock);
break;
case F_GETLK:
error = lf_getlock(lock, fl);
break;
default:
break;
}
quit_unlock:
mutex_exit(interlock);
quit:
if (lock)
lf_free(lock);
if (sparelock)
lf_free(sparelock);
return error;
}
void
lf_init(void)
{
mutex_init(&lockf_lock, MUTEX_DEFAULT, IPL_NONE);
}