1994-08-08 17:31:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Scooter Morris at Genentech Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)ufs_lockf.c 8.3 (Berkeley) 1/6/94
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-01-31 07:23:16 +00:00
|
|
|
#include "opt_debug_lockf.h"
|
|
|
|
|
1994-08-08 17:31:01 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1998-11-10 09:16:29 +00:00
|
|
|
#include <sys/kernel.h>
|
2003-04-29 13:36:06 +00:00
|
|
|
#include <sys/limits.h>
|
1997-12-05 19:55:52 +00:00
|
|
|
#include <sys/lock.h>
|
2002-08-13 10:07:05 +00:00
|
|
|
#include <sys/mount.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/mutex.h>
|
1994-08-08 17:31:01 +00:00
|
|
|
#include <sys/proc.h>
|
1996-09-03 14:25:27 +00:00
|
|
|
#include <sys/unistd.h>
|
1994-08-08 17:31:01 +00:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/lockf.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This variable controls the maximum number of processes that will
|
|
|
|
* be checked in doing deadlock detection.
|
|
|
|
*/
|
1997-02-10 02:22:35 +00:00
|
|
|
static int maxlockdepth = MAXDEPTH;
|
1994-08-08 17:31:01 +00:00
|
|
|
|
|
|
|
#ifdef LOCKF_DEBUG
|
1997-02-10 02:22:35 +00:00
|
|
|
#include <sys/sysctl.h>
|
1997-02-18 14:37:26 +00:00
|
|
|
|
|
|
|
#include <ufs/ufs/quota.h>
|
|
|
|
#include <ufs/ufs/inode.h>
|
|
|
|
|
1997-10-11 18:31:40 +00:00
|
|
|
|
1997-04-01 10:30:06 +00:00
|
|
|
static int lockf_debug = 0;
|
|
|
|
SYSCTL_INT(_debug, OID_AUTO, lockf_debug, CTLFLAG_RW, &lockf_debug, 0, "");
|
1994-08-08 17:31:01 +00:00
|
|
|
#endif
|
|
|
|
|
2001-04-17 20:45:23 +00:00
|
|
|
MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
|
1997-10-11 18:31:40 +00:00
|
|
|
|
1994-08-08 17:31:01 +00:00
|
|
|
#define NOLOCKF (struct lockf *)0
|
|
|
|
#define SELF 0x1
|
|
|
|
#define OTHERS 0x2
|
2002-03-19 21:25:46 +00:00
|
|
|
static int lf_clearlock(struct lockf *);
|
|
|
|
static int lf_findoverlap(struct lockf *,
|
|
|
|
struct lockf *, int, struct lockf ***, struct lockf **);
|
1995-12-14 08:32:45 +00:00
|
|
|
static struct lockf *
|
2002-03-19 21:25:46 +00:00
|
|
|
lf_getblock(struct lockf *);
|
|
|
|
static int lf_getlock(struct lockf *, struct flock *);
|
|
|
|
static int lf_setlock(struct lockf *);
|
|
|
|
static void lf_split(struct lockf *, struct lockf *);
|
|
|
|
static void lf_wakelock(struct lockf *);
|
1994-08-08 17:31:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Advisory record locking support
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
lf_advlock(ap, head, size)
|
|
|
|
struct vop_advlock_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
caddr_t a_id;
|
|
|
|
int a_op;
|
|
|
|
struct flock *a_fl;
|
|
|
|
int a_flags;
|
|
|
|
} */ *ap;
|
|
|
|
struct lockf **head;
|
|
|
|
u_quad_t size;
|
|
|
|
{
|
|
|
|
register struct flock *fl = ap->a_fl;
|
|
|
|
register struct lockf *lock;
|
2001-08-26 10:23:49 +00:00
|
|
|
off_t start, end, oadd;
|
1994-08-08 17:31:01 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert the flock structure into a start and end.
|
|
|
|
*/
|
|
|
|
switch (fl->l_whence) {
|
|
|
|
|
|
|
|
case SEEK_SET:
|
|
|
|
case SEEK_CUR:
|
|
|
|
/*
|
|
|
|
* Caller is responsible for adding any necessary offset
|
|
|
|
* when SEEK_CUR is used.
|
|
|
|
*/
|
|
|
|
start = fl->l_start;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
2001-08-29 18:53:53 +00:00
|
|
|
if (size > OFF_MAX ||
|
|
|
|
(fl->l_start > 0 && size > OFF_MAX - fl->l_start))
|
2001-08-23 07:42:40 +00:00
|
|
|
return (EOVERFLOW);
|
1994-08-08 17:31:01 +00:00
|
|
|
start = size + fl->l_start;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
if (start < 0)
|
|
|
|
return (EINVAL);
|
2001-08-23 15:40:30 +00:00
|
|
|
if (fl->l_len < 0) {
|
2001-08-23 16:00:27 +00:00
|
|
|
if (start == 0)
|
2001-08-23 15:40:30 +00:00
|
|
|
return (EINVAL);
|
|
|
|
end = start - 1;
|
2001-08-23 16:00:27 +00:00
|
|
|
start += fl->l_len;
|
|
|
|
if (start < 0)
|
|
|
|
return (EINVAL);
|
2001-08-23 15:40:30 +00:00
|
|
|
} else if (fl->l_len == 0)
|
1994-08-08 17:31:01 +00:00
|
|
|
end = -1;
|
1996-12-19 13:22:30 +00:00
|
|
|
else {
|
2001-08-26 10:23:49 +00:00
|
|
|
oadd = fl->l_len - 1;
|
2001-08-23 07:42:40 +00:00
|
|
|
if (oadd > OFF_MAX - start)
|
|
|
|
return (EOVERFLOW);
|
|
|
|
end = start + oadd;
|
1996-12-19 13:22:30 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Avoid the common case of unlocking when inode has no locks.
|
|
|
|
*/
|
|
|
|
if (*head == (struct lockf *)0) {
|
|
|
|
if (ap->a_op != F_SETLK) {
|
|
|
|
fl->l_type = F_UNLCK;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
1994-08-08 17:31:01 +00:00
|
|
|
/*
|
|
|
|
* Create the lockf structure
|
|
|
|
*/
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
|
1994-08-08 17:31:01 +00:00
|
|
|
lock->lf_start = start;
|
|
|
|
lock->lf_end = end;
|
|
|
|
lock->lf_id = ap->a_id;
|
2001-12-02 12:47:25 +00:00
|
|
|
/*
|
|
|
|
* XXX The problem is that VTOI is ufs specific, so it will
|
|
|
|
* break LOCKF_DEBUG for all other FS's other than UFS because
|
|
|
|
* it casts the vnode->data ptr to struct inode *.
|
|
|
|
*/
|
|
|
|
/* lock->lf_inode = VTOI(ap->a_vp); */
|
|
|
|
lock->lf_inode = (struct inode *)0;
|
1994-08-08 17:31:01 +00:00
|
|
|
lock->lf_type = fl->l_type;
|
1997-02-10 02:22:35 +00:00
|
|
|
lock->lf_head = head;
|
1994-08-08 17:31:01 +00:00
|
|
|
lock->lf_next = (struct lockf *)0;
|
1997-02-10 02:22:35 +00:00
|
|
|
TAILQ_INIT(&lock->lf_blkhd);
|
1994-08-08 17:31:01 +00:00
|
|
|
lock->lf_flags = ap->a_flags;
|
|
|
|
/*
|
|
|
|
* Do the requested operation.
|
|
|
|
*/
|
|
|
|
switch(ap->a_op) {
|
|
|
|
case F_SETLK:
|
|
|
|
return (lf_setlock(lock));
|
|
|
|
|
|
|
|
case F_UNLCK:
|
|
|
|
error = lf_clearlock(lock);
|
|
|
|
FREE(lock, M_LOCKF);
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
case F_GETLK:
|
|
|
|
error = lf_getlock(lock, fl);
|
|
|
|
FREE(lock, M_LOCKF);
|
|
|
|
return (error);
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-08-08 17:31:01 +00:00
|
|
|
default:
|
|
|
|
free(lock, M_LOCKF);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a byte-range lock.
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static int
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_setlock(lock)
|
|
|
|
register struct lockf *lock;
|
|
|
|
{
|
|
|
|
register struct lockf *block;
|
|
|
|
struct lockf **head = lock->lf_head;
|
|
|
|
struct lockf **prev, *overlap, *ltmp;
|
|
|
|
static char lockstr[] = "lockf";
|
|
|
|
int ovcase, priority, needtolink, error;
|
|
|
|
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 1)
|
|
|
|
lf_print("lf_setlock", lock);
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the priority
|
|
|
|
*/
|
|
|
|
priority = PLOCK;
|
|
|
|
if (lock->lf_type == F_WRLCK)
|
|
|
|
priority += 4;
|
|
|
|
priority |= PCATCH;
|
|
|
|
/*
|
|
|
|
* Scan lock list for this file looking for locks that would block us.
|
|
|
|
*/
|
1994-09-25 19:34:02 +00:00
|
|
|
while ((block = lf_getblock(lock))) {
|
1994-08-08 17:31:01 +00:00
|
|
|
/*
|
|
|
|
* Free the structure and return if nonblocking.
|
|
|
|
*/
|
|
|
|
if ((lock->lf_flags & F_WAIT) == 0) {
|
|
|
|
FREE(lock, M_LOCKF);
|
|
|
|
return (EAGAIN);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We are blocked. Since flock style locks cover
|
|
|
|
* the whole file, there is no chance for deadlock.
|
|
|
|
* For byte-range locks we must check for deadlock.
|
|
|
|
*
|
|
|
|
* Deadlock detection is done by looking through the
|
|
|
|
* wait channels to see if there are any cycles that
|
|
|
|
* involve us. MAXDEPTH is set just to make sure we
|
|
|
|
* do not go off into neverland.
|
|
|
|
*/
|
|
|
|
if ((lock->lf_flags & F_POSIX) &&
|
|
|
|
(block->lf_flags & F_POSIX)) {
|
|
|
|
register struct proc *wproc;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
1994-08-08 17:31:01 +00:00
|
|
|
register struct lockf *waitblock;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* The block is waiting on something */
|
2001-09-12 08:38:13 +00:00
|
|
|
/* XXXKSE this is not complete under threads */
|
1994-08-08 17:31:01 +00:00
|
|
|
wproc = (struct proc *)block->lf_id;
|
2001-03-24 03:57:44 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2001-09-12 08:38:13 +00:00
|
|
|
FOREACH_THREAD_IN_PROC(wproc, td) {
|
|
|
|
while (td->td_wchan &&
|
|
|
|
(td->td_wmesg == lockstr) &&
|
|
|
|
(i++ < maxlockdepth)) {
|
|
|
|
waitblock = (struct lockf *)td->td_wchan;
|
|
|
|
/* Get the owner of the blocking lock */
|
|
|
|
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) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
free(lock, M_LOCKF);
|
|
|
|
return (EDEADLK);
|
|
|
|
}
|
1994-08-08 17:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
2001-03-24 03:57:44 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1994-08-08 17:31:01 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* For flock type locks, we must first remove
|
|
|
|
* any shared locks that we hold before we sleep
|
|
|
|
* waiting for an exclusive lock.
|
|
|
|
*/
|
|
|
|
if ((lock->lf_flags & F_FLOCK) &&
|
|
|
|
lock->lf_type == F_WRLCK) {
|
|
|
|
lock->lf_type = F_UNLCK;
|
|
|
|
(void) lf_clearlock(lock);
|
|
|
|
lock->lf_type = F_WRLCK;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Add our lock to the blocked list and sleep until we're free.
|
|
|
|
* Remember who blocked us (for deadlock detection).
|
|
|
|
*/
|
|
|
|
lock->lf_next = block;
|
1997-02-10 02:22:35 +00:00
|
|
|
TAILQ_INSERT_TAIL(&block->lf_blkhd, lock, lf_block);
|
1994-08-08 17:31:01 +00:00
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 1) {
|
|
|
|
lf_print("lf_setlock: blocking on", block);
|
|
|
|
lf_printlist("lf_setlock", block);
|
|
|
|
}
|
|
|
|
#endif /* LOCKF_DEBUG */
|
2002-06-29 00:29:12 +00:00
|
|
|
error = tsleep(lock, priority, lockstr, 0);
|
1999-07-04 14:43:01 +00:00
|
|
|
/*
|
|
|
|
* We may have been awakened by a signal and/or by a
|
|
|
|
* debugger continuing us (in which cases we must remove
|
|
|
|
* ourselves from the blocked list) and/or by another
|
|
|
|
* process releasing a lock (in which case we have
|
|
|
|
* already been removed from the blocked list and our
|
|
|
|
* lf_next field set to NOLOCKF).
|
|
|
|
*/
|
|
|
|
if (lock->lf_next) {
|
|
|
|
TAILQ_REMOVE(&lock->lf_next->lf_blkhd, lock, lf_block);
|
|
|
|
lock->lf_next = NOLOCKF;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
free(lock, M_LOCKF);
|
|
|
|
return (error);
|
1994-08-08 17:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* No blocks!! Add the lock. Note that we will
|
|
|
|
* downgrade or upgrade any overlapping locks this
|
|
|
|
* process already owns.
|
|
|
|
*
|
|
|
|
* Skip over locks owned by other processes.
|
|
|
|
* Handle any locks that overlap and are owned by ourselves.
|
|
|
|
*/
|
|
|
|
prev = head;
|
|
|
|
block = *head;
|
|
|
|
needtolink = 1;
|
|
|
|
for (;;) {
|
1994-09-25 19:34:02 +00:00
|
|
|
ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap);
|
|
|
|
if (ovcase)
|
1994-08-08 17:31:01 +00:00
|
|
|
block = overlap->lf_next;
|
|
|
|
/*
|
|
|
|
* Six cases:
|
|
|
|
* 0) no overlap
|
|
|
|
* 1) overlap == lock
|
|
|
|
* 2) overlap contains lock
|
|
|
|
* 3) lock contains overlap
|
|
|
|
* 4) overlap starts before lock
|
|
|
|
* 5) overlap ends after lock
|
|
|
|
*/
|
|
|
|
switch (ovcase) {
|
|
|
|
case 0: /* no overlap */
|
|
|
|
if (needtolink) {
|
|
|
|
*prev = lock;
|
|
|
|
lock->lf_next = overlap;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* overlap == lock */
|
|
|
|
/*
|
|
|
|
* If downgrading lock, others may be
|
|
|
|
* able to acquire it.
|
|
|
|
*/
|
|
|
|
if (lock->lf_type == F_RDLCK &&
|
|
|
|
overlap->lf_type == F_WRLCK)
|
|
|
|
lf_wakelock(overlap);
|
|
|
|
overlap->lf_type = lock->lf_type;
|
|
|
|
FREE(lock, M_LOCKF);
|
|
|
|
lock = overlap; /* for debug output below */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* overlap contains lock */
|
|
|
|
/*
|
|
|
|
* Check for common starting point and different types.
|
|
|
|
*/
|
|
|
|
if (overlap->lf_type == lock->lf_type) {
|
|
|
|
free(lock, M_LOCKF);
|
|
|
|
lock = overlap; /* for debug output below */
|
|
|
|
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);
|
|
|
|
lf_wakelock(overlap);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* lock contains overlap */
|
|
|
|
/*
|
|
|
|
* If downgrading lock, others may be able to
|
|
|
|
* acquire it, otherwise take the list.
|
|
|
|
*/
|
|
|
|
if (lock->lf_type == F_RDLCK &&
|
|
|
|
overlap->lf_type == F_WRLCK) {
|
|
|
|
lf_wakelock(overlap);
|
|
|
|
} else {
|
1999-11-16 16:28:58 +00:00
|
|
|
while (!TAILQ_EMPTY(&overlap->lf_blkhd)) {
|
|
|
|
ltmp = TAILQ_FIRST(&overlap->lf_blkhd);
|
1997-02-10 02:22:35 +00:00
|
|
|
TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
|
|
|
|
lf_block);
|
|
|
|
TAILQ_INSERT_TAIL(&lock->lf_blkhd,
|
|
|
|
ltmp, lf_block);
|
1999-05-08 22:46:46 +00:00
|
|
|
ltmp->lf_next = lock;
|
1997-02-10 02:22:35 +00:00
|
|
|
}
|
1994-08-08 17:31:01 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Add the new lock if necessary and delete the overlap.
|
|
|
|
*/
|
|
|
|
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: /* overlap starts before lock */
|
|
|
|
/*
|
|
|
|
* Add lock after overlap on the list.
|
|
|
|
*/
|
|
|
|
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: /* overlap ends after lock */
|
|
|
|
/*
|
|
|
|
* Add the new lock before overlap.
|
|
|
|
*/
|
|
|
|
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 /* LOCKF_DEBUG */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a byte-range lock on an inode.
|
|
|
|
*
|
|
|
|
* Generally, find the lock (or an overlap to that lock)
|
|
|
|
* and remove it (or shrink it), then wakeup anyone we can.
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static int
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_clearlock(unlock)
|
|
|
|
register struct lockf *unlock;
|
|
|
|
{
|
|
|
|
struct lockf **head = unlock->lf_head;
|
|
|
|
register struct lockf *lf = *head;
|
|
|
|
struct lockf *overlap, **prev;
|
|
|
|
int ovcase;
|
|
|
|
|
|
|
|
if (lf == NOLOCKF)
|
|
|
|
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 /* LOCKF_DEBUG */
|
|
|
|
prev = head;
|
1994-09-25 19:34:02 +00:00
|
|
|
while ((ovcase = lf_findoverlap(lf, unlock, SELF, &prev, &overlap))) {
|
1994-08-08 17:31:01 +00:00
|
|
|
/*
|
|
|
|
* Wakeup the list of locks to be retried.
|
|
|
|
*/
|
|
|
|
lf_wakelock(overlap);
|
|
|
|
|
|
|
|
switch (ovcase) {
|
|
|
|
|
|
|
|
case 1: /* overlap == lock */
|
|
|
|
*prev = overlap->lf_next;
|
|
|
|
FREE(overlap, M_LOCKF);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* overlap contains lock: split it */
|
|
|
|
if (overlap->lf_start == unlock->lf_start) {
|
|
|
|
overlap->lf_start = unlock->lf_end + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lf_split(overlap, unlock);
|
|
|
|
overlap->lf_next = unlock->lf_next;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* lock contains overlap */
|
|
|
|
*prev = overlap->lf_next;
|
|
|
|
lf = overlap->lf_next;
|
|
|
|
free(overlap, M_LOCKF);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 4: /* overlap starts before lock */
|
|
|
|
overlap->lf_end = unlock->lf_start - 1;
|
|
|
|
prev = &overlap->lf_next;
|
|
|
|
lf = overlap->lf_next;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 5: /* overlap ends after lock */
|
|
|
|
overlap->lf_start = unlock->lf_end + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 1)
|
|
|
|
lf_printlist("lf_clearlock", unlock);
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether there is a blocking lock,
|
|
|
|
* and if so return its process identifier.
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static int
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_getlock(lock, fl)
|
|
|
|
register struct lockf *lock;
|
|
|
|
register struct flock *fl;
|
|
|
|
{
|
|
|
|
register struct lockf *block;
|
|
|
|
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 1)
|
|
|
|
lf_print("lf_getlock", lock);
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
|
1994-09-25 19:34:02 +00:00
|
|
|
if ((block = lf_getblock(lock))) {
|
1994-08-08 17:31:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk the list of locks for an inode and
|
|
|
|
* return the first blocking lock.
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static struct lockf *
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_getblock(lock)
|
|
|
|
register struct lockf *lock;
|
|
|
|
{
|
|
|
|
struct lockf **prev, *overlap, *lf = *(lock->lf_head);
|
|
|
|
int ovcase;
|
|
|
|
|
|
|
|
prev = lock->lf_head;
|
1994-09-25 19:34:02 +00:00
|
|
|
while ((ovcase = lf_findoverlap(lf, lock, OTHERS, &prev, &overlap))) {
|
1994-08-08 17:31:01 +00:00
|
|
|
/*
|
|
|
|
* We've found an overlap, see if it blocks us
|
|
|
|
*/
|
|
|
|
if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
|
|
|
|
return (overlap);
|
|
|
|
/*
|
|
|
|
* Nope, point to the next one on the list and
|
|
|
|
* see if it blocks us
|
|
|
|
*/
|
|
|
|
lf = overlap->lf_next;
|
|
|
|
}
|
|
|
|
return (NOLOCKF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk the list of locks for an inode to
|
|
|
|
* find an overlapping lock (if any).
|
|
|
|
*
|
|
|
|
* NOTE: this returns only the FIRST overlapping lock. There
|
|
|
|
* may be more than one.
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static int
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_findoverlap(lf, lock, type, prev, overlap)
|
|
|
|
register struct lockf *lf;
|
|
|
|
struct lockf *lock;
|
|
|
|
int type;
|
|
|
|
struct lockf ***prev;
|
|
|
|
struct lockf **overlap;
|
|
|
|
{
|
|
|
|
off_t start, end;
|
|
|
|
|
|
|
|
*overlap = lf;
|
|
|
|
if (lf == NOLOCKF)
|
|
|
|
return (0);
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2)
|
|
|
|
lf_print("lf_findoverlap: looking for overlap in", lock);
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
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)
|
|
|
|
lf_print("\tchecking", lf);
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
/*
|
|
|
|
* OK, check for overlap
|
|
|
|
*
|
|
|
|
* Six cases:
|
|
|
|
* 0) no overlap
|
|
|
|
* 1) overlap == lock
|
|
|
|
* 2) overlap contains lock
|
|
|
|
* 3) lock contains overlap
|
|
|
|
* 4) overlap starts before lock
|
|
|
|
* 5) overlap ends after lock
|
|
|
|
*/
|
|
|
|
if ((lf->lf_end != -1 && start > lf->lf_end) ||
|
|
|
|
(end != -1 && lf->lf_start > end)) {
|
|
|
|
/* Case 0 */
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2)
|
|
|
|
printf("no overlap\n");
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
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)) {
|
|
|
|
/* Case 1 */
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2)
|
|
|
|
printf("overlap == lock\n");
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if ((lf->lf_start <= start) &&
|
|
|
|
(end != -1) &&
|
|
|
|
((lf->lf_end >= end) || (lf->lf_end == -1))) {
|
|
|
|
/* Case 2 */
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2)
|
|
|
|
printf("overlap contains lock\n");
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
return (2);
|
|
|
|
}
|
|
|
|
if (start <= lf->lf_start &&
|
|
|
|
(end == -1 ||
|
|
|
|
(lf->lf_end != -1 && end >= lf->lf_end))) {
|
|
|
|
/* Case 3 */
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2)
|
|
|
|
printf("lock contains overlap\n");
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
return (3);
|
|
|
|
}
|
|
|
|
if ((lf->lf_start < start) &&
|
|
|
|
((lf->lf_end >= start) || (lf->lf_end == -1))) {
|
|
|
|
/* Case 4 */
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2)
|
|
|
|
printf("overlap starts before lock\n");
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
return (4);
|
|
|
|
}
|
|
|
|
if ((lf->lf_start > start) &&
|
|
|
|
(end != -1) &&
|
|
|
|
((lf->lf_end > end) || (lf->lf_end == -1))) {
|
|
|
|
/* Case 5 */
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2)
|
|
|
|
printf("overlap ends after lock\n");
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
return (5);
|
|
|
|
}
|
|
|
|
panic("lf_findoverlap: default");
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Split a lock and a contained region into
|
|
|
|
* two or three locks as necessary.
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static void
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_split(lock1, lock2)
|
|
|
|
register struct lockf *lock1;
|
|
|
|
register struct lockf *lock2;
|
|
|
|
{
|
|
|
|
register struct lockf *splitlock;
|
|
|
|
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2) {
|
|
|
|
lf_print("lf_split", lock1);
|
|
|
|
lf_print("splitting from", lock2);
|
|
|
|
}
|
|
|
|
#endif /* LOCKF_DEBUG */
|
|
|
|
/*
|
|
|
|
* Check to see if spliting into only two pieces.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Make a new lock consisting of the last part of
|
|
|
|
* the encompassing lock
|
|
|
|
*/
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(splitlock, struct lockf *, sizeof *splitlock, M_LOCKF, M_WAITOK);
|
2002-06-29 00:29:12 +00:00
|
|
|
bcopy(lock1, splitlock, sizeof *splitlock);
|
1994-08-08 17:31:01 +00:00
|
|
|
splitlock->lf_start = lock2->lf_end + 1;
|
1997-02-10 02:22:35 +00:00
|
|
|
TAILQ_INIT(&splitlock->lf_blkhd);
|
1994-08-08 17:31:01 +00:00
|
|
|
lock1->lf_end = lock2->lf_start - 1;
|
|
|
|
/*
|
|
|
|
* OK, now link it in
|
|
|
|
*/
|
|
|
|
splitlock->lf_next = lock1->lf_next;
|
|
|
|
lock2->lf_next = splitlock;
|
|
|
|
lock1->lf_next = lock2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wakeup a blocklist
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static void
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_wakelock(listhead)
|
|
|
|
struct lockf *listhead;
|
|
|
|
{
|
1997-02-10 02:22:35 +00:00
|
|
|
register struct lockf *wakelock;
|
|
|
|
|
1999-11-16 16:28:58 +00:00
|
|
|
while (!TAILQ_EMPTY(&listhead->lf_blkhd)) {
|
|
|
|
wakelock = TAILQ_FIRST(&listhead->lf_blkhd);
|
1997-02-10 02:22:35 +00:00
|
|
|
TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
|
1994-08-08 17:31:01 +00:00
|
|
|
wakelock->lf_next = NOLOCKF;
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
if (lockf_debug & 2)
|
|
|
|
lf_print("lf_wakelock: awakening", wakelock);
|
|
|
|
#endif /* LOCKF_DEBUG */
|
2002-06-29 00:29:12 +00:00
|
|
|
wakeup(wakelock);
|
1997-02-10 02:22:35 +00:00
|
|
|
}
|
1994-08-08 17:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LOCKF_DEBUG
|
|
|
|
/*
|
|
|
|
* Print out a lock.
|
|
|
|
*/
|
1997-02-12 14:00:00 +00:00
|
|
|
void
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_print(tag, lock)
|
|
|
|
char *tag;
|
|
|
|
register struct lockf *lock;
|
|
|
|
{
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("%s: lock %p for ", tag, (void *)lock);
|
1994-08-08 17:31:01 +00:00
|
|
|
if (lock->lf_flags & F_POSIX)
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("proc %ld", (long)((struct proc *)lock->lf_id)->p_pid);
|
1994-08-08 17:31:01 +00:00
|
|
|
else
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("id %p", (void *)lock->lf_id);
|
2001-12-02 12:47:25 +00:00
|
|
|
if (lock->lf_inode != (struct inode *)0)
|
2002-11-07 21:56:05 +00:00
|
|
|
printf(" in ino %ju on dev <%d, %d>, %s, start %jd, end %jd",
|
|
|
|
(uintmax_t)lock->lf_inode->i_number,
|
2001-12-02 12:47:25 +00:00
|
|
|
major(lock->lf_inode->i_dev),
|
|
|
|
minor(lock->lf_inode->i_dev),
|
|
|
|
lock->lf_type == F_RDLCK ? "shared" :
|
|
|
|
lock->lf_type == F_WRLCK ? "exclusive" :
|
2002-11-07 21:56:05 +00:00
|
|
|
lock->lf_type == F_UNLCK ? "unlock" : "unknown",
|
|
|
|
(intmax_t)lock->lf_start, (intmax_t)lock->lf_end);
|
2001-12-02 12:47:25 +00:00
|
|
|
else
|
2002-11-07 21:56:05 +00:00
|
|
|
printf(" %s, start %jd, end %jd",
|
2001-12-02 12:47:25 +00:00
|
|
|
lock->lf_type == F_RDLCK ? "shared" :
|
|
|
|
lock->lf_type == F_WRLCK ? "exclusive" :
|
2002-11-07 21:56:05 +00:00
|
|
|
lock->lf_type == F_UNLCK ? "unlock" : "unknown",
|
|
|
|
(intmax_t)lock->lf_start, (intmax_t)lock->lf_end);
|
1999-11-16 16:28:58 +00:00
|
|
|
if (!TAILQ_EMPTY(&lock->lf_blkhd))
|
|
|
|
printf(" block %p\n", (void *)TAILQ_FIRST(&lock->lf_blkhd));
|
1994-08-08 17:31:01 +00:00
|
|
|
else
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
1997-02-12 14:00:00 +00:00
|
|
|
void
|
1994-08-08 17:31:01 +00:00
|
|
|
lf_printlist(tag, lock)
|
|
|
|
char *tag;
|
|
|
|
struct lockf *lock;
|
|
|
|
{
|
1997-02-10 02:22:35 +00:00
|
|
|
register struct lockf *lf, *blk;
|
1994-08-08 17:31:01 +00:00
|
|
|
|
2001-12-02 12:47:25 +00:00
|
|
|
if (lock->lf_inode == (struct inode *)0)
|
|
|
|
return;
|
|
|
|
|
2002-11-07 21:56:05 +00:00
|
|
|
printf("%s: Lock list for ino %ju on dev <%d, %d>:\n",
|
|
|
|
tag, (uintmax_t)lock->lf_inode->i_number,
|
1998-07-29 17:38:14 +00:00
|
|
|
major(lock->lf_inode->i_dev),
|
|
|
|
minor(lock->lf_inode->i_dev));
|
1994-08-08 17:31:01 +00:00
|
|
|
for (lf = lock->lf_inode->i_lockf; lf; lf = lf->lf_next) {
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("\tlock %p for ",(void *)lf);
|
1994-08-08 17:31:01 +00:00
|
|
|
if (lf->lf_flags & F_POSIX)
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("proc %ld",
|
|
|
|
(long)((struct proc *)lf->lf_id)->p_pid);
|
1994-08-08 17:31:01 +00:00
|
|
|
else
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("id %p", (void *)lf->lf_id);
|
2002-11-07 21:56:05 +00:00
|
|
|
printf(", %s, start %jd, end %jd",
|
1998-07-29 17:38:14 +00:00
|
|
|
lf->lf_type == F_RDLCK ? "shared" :
|
|
|
|
lf->lf_type == F_WRLCK ? "exclusive" :
|
|
|
|
lf->lf_type == F_UNLCK ? "unlock" :
|
2002-11-07 21:56:05 +00:00
|
|
|
"unknown", (intmax_t)lf->lf_start, (intmax_t)lf->lf_end);
|
1999-11-16 16:28:58 +00:00
|
|
|
TAILQ_FOREACH(blk, &lf->lf_blkhd, lf_block) {
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("\n\t\tlock request %p for ", (void *)blk);
|
1997-02-10 02:22:35 +00:00
|
|
|
if (blk->lf_flags & F_POSIX)
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("proc %ld",
|
|
|
|
(long)((struct proc *)blk->lf_id)->p_pid);
|
1997-02-10 02:22:35 +00:00
|
|
|
else
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("id %p", (void *)blk->lf_id);
|
2002-11-07 21:56:05 +00:00
|
|
|
printf(", %s, start %jd, end %jd",
|
1998-07-29 17:38:14 +00:00
|
|
|
blk->lf_type == F_RDLCK ? "shared" :
|
|
|
|
blk->lf_type == F_WRLCK ? "exclusive" :
|
|
|
|
blk->lf_type == F_UNLCK ? "unlock" :
|
2002-11-07 21:56:05 +00:00
|
|
|
"unknown", (intmax_t)blk->lf_start,
|
|
|
|
(intmax_t)blk->lf_end);
|
1999-11-16 16:28:58 +00:00
|
|
|
if (!TAILQ_EMPTY(&blk->lf_blkhd))
|
1997-02-10 02:22:35 +00:00
|
|
|
panic("lf_printlist: bad list");
|
|
|
|
}
|
|
|
|
printf("\n");
|
1994-08-08 17:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LOCKF_DEBUG */
|