#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/fcntl.h>
#include "hfs_cnode.h"
#include "hfs_lockf.h"
int hfsmaxlockdepth = MAXDEPTH;
#ifdef LOCKF_DEBUG
#include <vm/vm.h>
#include <sys/sysctl.h>
int lockf_debug = 0;
struct ctldebug debug4 = { "lockf_debug", &lockf_debug };
#endif
#define NOLOCKF (struct hfslockf *)0
#define SELF 0x1
#define OTHERS 0x2
int
hfs_setlock(lock)
register struct hfslockf *lock;
{
register struct hfslockf *block;
struct filefork *fork = lock->lf_fork;
struct hfslockf **prev, *overlap, *ltmp;
static char lockstr[] = "hfslockf";
int ovcase, priority, needtolink, error;
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
hfs_lprint("hfs_setlock", lock);
#endif
priority = PLOCK;
if (lock->lf_type == F_WRLCK)
priority += 4;
priority |= PCATCH;
while ((block = hfs_getblock(lock))) {
if ((lock->lf_flags & F_WAIT) == 0) {
FREE(lock, M_LOCKF);
return (EAGAIN);
}
if ((lock->lf_flags & F_POSIX) &&
(block->lf_flags & F_POSIX)) {
register struct proc *wproc;
register struct hfslockf *waitblock;
int i = 0;
wproc = (struct proc *)block->lf_id;
while (wproc->p_wchan &&
(wproc->p_wmesg == lockstr) &&
(i++ < hfsmaxlockdepth)) {
waitblock = (struct hfslockf *)wproc->p_wchan;
waitblock = waitblock->lf_next;
if ((waitblock->lf_flags & F_POSIX) == 0)
break;
wproc = (struct proc *)waitblock->lf_id;
if (wproc == (struct proc *)lock->lf_id) {
_FREE(lock, M_LOCKF);
return (EDEADLK);
}
}
}
if ((lock->lf_flags & F_FLOCK) &&
lock->lf_type == F_WRLCK) {
lock->lf_type = F_UNLCK;
(void) hfs_clearlock(lock);
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) {
hfs_lprint("hfs_setlock: blocking on", block);
hfs_lprintlist("hfs_setlock", block);
}
#endif
if ((error = tsleep((caddr_t)lock, priority, lockstr, 0))) {
if (lock->lf_next)
TAILQ_REMOVE(&lock->lf_next->lf_blkhd, lock,
lf_block);
_FREE(lock, M_LOCKF);
return (error);
}
}
prev = &fork->ff_lockf;
block = fork->ff_lockf;
needtolink = 1;
for (;;) {
if ((ovcase = hfs_findoverlap(block, lock, SELF, &prev, &overlap)))
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)
hfs_wakelock(overlap);
overlap->lf_type = lock->lf_type;
FREE(lock, M_LOCKF);
lock = overlap;
break;
case 2:
if (overlap->lf_type == lock->lf_type) {
_FREE(lock, M_LOCKF);
lock = overlap;
break;
}
if (overlap->lf_start == lock->lf_start) {
*prev = lock;
lock->lf_next = overlap;
overlap->lf_start = lock->lf_end + 1;
} else
hfs_split(overlap, lock);
hfs_wakelock(overlap);
break;
case 3:
if (lock->lf_type == F_RDLCK &&
overlap->lf_type == F_WRLCK) {
hfs_wakelock(overlap);
} else {
while ((ltmp = overlap->lf_blkhd.tqh_first)) {
TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
lf_block);
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;
_FREE(overlap, M_LOCKF);
continue;
case 4:
lock->lf_next = overlap->lf_next;
overlap->lf_next = lock;
overlap->lf_end = lock->lf_start - 1;
prev = &lock->lf_next;
hfs_wakelock(overlap);
needtolink = 0;
continue;
case 5:
if (needtolink) {
*prev = lock;
lock->lf_next = overlap;
}
overlap->lf_start = lock->lf_end + 1;
hfs_wakelock(overlap);
break;
}
break;
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 1) {
hfs_lprint("hfs_setlock: got the lock", lock);
hfs_lprintlist("hfs_setlock", lock);
}
#endif
return (0);
}
int
hfs_clearlock(unlock)
register struct hfslockf *unlock;
{
struct filefork *fork = unlock->lf_fork;
register struct hfslockf *lf = fork->ff_lockf;
struct hfslockf *overlap, **prev;
int ovcase;
if (lf == NOLOCKF)
return (0);
#ifdef LOCKF_DEBUG
if (unlock->lf_type != F_UNLCK)
panic("hfs_clearlock: bad type");
if (lockf_debug & 1)
hfs_lprint("hfs_clearlock", unlock);
#endif
prev = &fork->ff_lockf;
while ((ovcase = hfs_findoverlap(lf, unlock, SELF, &prev, &overlap))) {
hfs_wakelock(overlap);
switch (ovcase) {
case 1:
*prev = overlap->lf_next;
FREE(overlap, M_LOCKF);
break;
case 2:
if (overlap->lf_start == unlock->lf_start) {
overlap->lf_start = unlock->lf_end + 1;
break;
}
hfs_split(overlap, unlock);
overlap->lf_next = unlock->lf_next;
break;
case 3:
*prev = overlap->lf_next;
lf = overlap->lf_next;
_FREE(overlap, M_LOCKF);
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)
hfs_lprintlist("hfs_clearlock", unlock);
#endif
return (0);
}
int
hfs_getlock(lock, fl)
register struct hfslockf *lock;
register struct flock *fl;
{
register struct hfslockf *block;
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
hfs_lprint("hfs_getlock", lock);
#endif
if ((block = hfs_getblock(lock))) {
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);
}
struct hfslockf *
hfs_getblock(lock)
register struct hfslockf *lock;
{
struct hfslockf **prev, *overlap, *lf = lock->lf_fork->ff_lockf;
int ovcase;
prev = &lock->lf_fork->ff_lockf;
while ((ovcase = hfs_findoverlap(lf, lock, OTHERS, &prev, &overlap))) {
if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
return (overlap);
lf = overlap->lf_next;
}
return (NOLOCKF);
}
int
hfs_findoverlap(lf, lock, type, prev, overlap)
register struct hfslockf *lf;
struct hfslockf *lock;
int type;
struct hfslockf ***prev;
struct hfslockf **overlap;
{
off_t start, end;
*overlap = lf;
if (lf == NOLOCKF)
return (0);
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
hfs_lprint("hfs_findoverlap: looking for overlap in", lock);
#endif
start = lock->lf_start;
end = lock->lf_end;
while (lf != NOLOCKF) {
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)
hfs_lprint("\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("hfs_findoverlap: default");
}
return (0);
}
void
hfs_split(lock1, lock2)
register struct hfslockf *lock1;
register struct hfslockf *lock2;
{
register struct hfslockf *splitlock;
#ifdef LOCKF_DEBUG
if (lockf_debug & 2) {
hfs_lprint("hfs_split", lock1);
hfs_lprint("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;
}
MALLOC(splitlock, struct hfslockf *, sizeof *splitlock, M_LOCKF, M_WAITOK);
bcopy((caddr_t)lock1, (caddr_t)splitlock, sizeof *splitlock);
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;
}
void
hfs_wakelock(listhead)
struct hfslockf *listhead;
{
register struct hfslockf *wakelock;
while ((wakelock = listhead->lf_blkhd.tqh_first)) {
TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
wakelock->lf_next = NOLOCKF;
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
hfs_lprint("hfs_wakelock: awakening", wakelock);
#endif
wakeup((caddr_t)wakelock);
}
}
#ifdef LOCKF_DEBUG
hfs_lprint(tag, lock)
char *tag;
register struct hfslockf *lock;
{
printf("%s: lock 0x%lx for ", tag, lock);
if (lock->lf_flags & F_POSIX)
printf("proc %d", ((struct proc *)(lock->lf_id))->p_pid);
else
printf("id 0x%x", lock->lf_id);
printf(" in ino %d on dev <%d, %d>, %s, start %d, end %d",
FTOC(lock->lf_fork)->c_fileid,
major(FTOC(lock->lf_fork)->c_dev),
minor(FTOC(lock->lf_fork)->c_dev),
lock->lf_type == F_RDLCK ? "shared" :
lock->lf_type == F_WRLCK ? "exclusive" :
lock->lf_type == F_UNLCK ? "unlock" :
"unknown", lock->lf_start, lock->lf_end);
if (lock->lf_blkhd.tqh_first)
printf(" block 0x%x\n", lock->lf_blkhd.tqh_first);
else
printf("\n");
}
hfs_lprintlist(tag, lock)
char *tag;
struct hfslockf *lock;
{
register struct hfslockf *lf, *blk;
printf("%s: Lock list for ino %d on dev <%d, %d>:\n",
tag, FTOC(lock->lf_fork)->i_number,
major(FTOC(lock->lf_fork)->c_dev),
minor(FTOC(lock->lf_fork)->c_dev));
for (lf = lock->lf_fork->ff_lockf; lf; lf = lf->lf_next) {
printf("\tlock 0x%lx for ", lf);
if (lf->lf_flags & F_POSIX)
printf("proc %d", ((struct proc *)(lf->lf_id))->p_pid);
else
printf("id 0x%x", lf->lf_id);
printf(", %s, start %d, end %d",
lf->lf_type == F_RDLCK ? "shared" :
lf->lf_type == F_WRLCK ? "exclusive" :
lf->lf_type == F_UNLCK ? "unlock" :
"unknown", lf->lf_start, lf->lf_end);
for (blk = lf->lf_blkhd.tqh_first; blk;
blk = blk->lf_block.tqe_next) {
printf("\n\t\tlock request 0x%lx for ", blk);
if (blk->lf_flags & F_POSIX)
printf("proc %d",
((struct proc *)(blk->lf_id))->p_pid);
else
printf("id 0x%x", blk->lf_id);
printf(", %s, start %d, end %d",
blk->lf_type == F_RDLCK ? "shared" :
blk->lf_type == F_WRLCK ? "exclusive" :
blk->lf_type == F_UNLCK ? "unlock" :
"unknown", blk->lf_start, blk->lf_end);
if (blk->lf_blkhd.tqh_first)
panic("hfs_lprintlist: bad list");
}
printf("\n");
}
}
#endif