Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.1
      1  1.1  fvdl /*
      2  1.1  fvdl  * Copyright (c) 1995
      3  1.1  fvdl  *	The Regents of the University of California.  All rights reserved.
      4  1.1  fvdl  *
      5  1.1  fvdl  * This code contains ideas from software contributed to Berkeley by
      6  1.1  fvdl  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
      7  1.1  fvdl  * System project at Carnegie-Mellon University.
      8  1.1  fvdl  *
      9  1.1  fvdl  * Redistribution and use in source and binary forms, with or without
     10  1.1  fvdl  * modification, are permitted provided that the following conditions
     11  1.1  fvdl  * are met:
     12  1.1  fvdl  * 1. Redistributions of source code must retain the above copyright
     13  1.1  fvdl  *    notice, this list of conditions and the following disclaimer.
     14  1.1  fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  fvdl  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  fvdl  *    documentation and/or other materials provided with the distribution.
     17  1.1  fvdl  * 3. All advertising materials mentioning features or use of this software
     18  1.1  fvdl  *    must display the following acknowledgement:
     19  1.1  fvdl  *	This product includes software developed by the University of
     20  1.1  fvdl  *	California, Berkeley and its contributors.
     21  1.1  fvdl  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  fvdl  *    may be used to endorse or promote products derived from this software
     23  1.1  fvdl  *    without specific prior written permission.
     24  1.1  fvdl  *
     25  1.1  fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  fvdl  * SUCH DAMAGE.
     36  1.1  fvdl  *
     37  1.1  fvdl  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
     38  1.1  fvdl  */
     39  1.1  fvdl 
     40  1.1  fvdl #include <sys/param.h>
     41  1.1  fvdl #include <sys/proc.h>
     42  1.1  fvdl #include <sys/lock.h>
     43  1.1  fvdl #include <machine/cpu.h>
     44  1.1  fvdl 
     45  1.1  fvdl /*
     46  1.1  fvdl  * Locking primitives implementation.
     47  1.1  fvdl  * Locks provide shared/exclusive sychronization.
     48  1.1  fvdl  */
     49  1.1  fvdl 
     50  1.1  fvdl #ifdef DEBUG
     51  1.1  fvdl #define COUNT(p, x) if (p) (p)->p_locks += (x)
     52  1.1  fvdl #else
     53  1.1  fvdl #define COUNT(p, x)
     54  1.1  fvdl #endif
     55  1.1  fvdl 
     56  1.1  fvdl #if NCPUS > 1
     57  1.1  fvdl 
     58  1.1  fvdl /*
     59  1.1  fvdl  * For multiprocessor system, try spin lock first.
     60  1.1  fvdl  *
     61  1.1  fvdl  * This should be inline expanded below, but we cannot have #if
     62  1.1  fvdl  * inside a multiline define.
     63  1.1  fvdl  */
     64  1.1  fvdl int lock_wait_time = 100;
     65  1.1  fvdl #define PAUSE(lkp, wanted)						\
     66  1.1  fvdl 		if (lock_wait_time > 0) {				\
     67  1.1  fvdl 			int i;						\
     68  1.1  fvdl 									\
     69  1.1  fvdl 			simple_unlock(&lkp->lk_interlock);		\
     70  1.1  fvdl 			for (i = lock_wait_time; i > 0; i--)		\
     71  1.1  fvdl 				if (!(wanted))				\
     72  1.1  fvdl 					break;				\
     73  1.1  fvdl 			simple_lock(&lkp->lk_interlock);		\
     74  1.1  fvdl 		}							\
     75  1.1  fvdl 		if (!(wanted))						\
     76  1.1  fvdl 			break;
     77  1.1  fvdl 
     78  1.1  fvdl #else /* NCPUS == 1 */
     79  1.1  fvdl 
     80  1.1  fvdl /*
     81  1.1  fvdl  * It is an error to spin on a uniprocessor as nothing will ever cause
     82  1.1  fvdl  * the simple lock to clear while we are executing.
     83  1.1  fvdl  */
     84  1.1  fvdl #define PAUSE(lkp, wanted)
     85  1.1  fvdl 
     86  1.1  fvdl #endif /* NCPUS == 1 */
     87  1.1  fvdl 
     88  1.1  fvdl /*
     89  1.1  fvdl  * Acquire a resource.
     90  1.1  fvdl  */
     91  1.1  fvdl #define ACQUIRE(lkp, error, extflags, wanted)				\
     92  1.1  fvdl 	PAUSE(lkp, wanted);						\
     93  1.1  fvdl 	for (error = 0; wanted; ) {					\
     94  1.1  fvdl 		(lkp)->lk_waitcount++;					\
     95  1.1  fvdl 		simple_unlock(&(lkp)->lk_interlock);			\
     96  1.1  fvdl 		error = tsleep((void *)lkp, (lkp)->lk_prio,		\
     97  1.1  fvdl 		    (lkp)->lk_wmesg, (lkp)->lk_timo);			\
     98  1.1  fvdl 		simple_lock(&(lkp)->lk_interlock);			\
     99  1.1  fvdl 		(lkp)->lk_waitcount--;					\
    100  1.1  fvdl 		if (error)						\
    101  1.1  fvdl 			break;						\
    102  1.1  fvdl 		if ((extflags) & LK_SLEEPFAIL) {			\
    103  1.1  fvdl 			error = ENOLCK;					\
    104  1.1  fvdl 			break;						\
    105  1.1  fvdl 		}							\
    106  1.1  fvdl 	}
    107  1.1  fvdl 
    108  1.1  fvdl /*
    109  1.1  fvdl  * Initialize a lock; required before use.
    110  1.1  fvdl  */
    111  1.1  fvdl void
    112  1.1  fvdl lockinit(lkp, prio, wmesg, timo, flags)
    113  1.1  fvdl 	struct lock *lkp;
    114  1.1  fvdl 	int prio;
    115  1.1  fvdl 	char *wmesg;
    116  1.1  fvdl 	int timo;
    117  1.1  fvdl 	int flags;
    118  1.1  fvdl {
    119  1.1  fvdl 
    120  1.1  fvdl 	bzero(lkp, sizeof(struct lock));
    121  1.1  fvdl 	simple_lock_init(&lkp->lk_interlock);
    122  1.1  fvdl 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    123  1.1  fvdl 	lkp->lk_prio = prio;
    124  1.1  fvdl 	lkp->lk_timo = timo;
    125  1.1  fvdl 	lkp->lk_wmesg = wmesg;
    126  1.1  fvdl 	lkp->lk_lockholder = LK_NOPROC;
    127  1.1  fvdl }
    128  1.1  fvdl 
    129  1.1  fvdl /*
    130  1.1  fvdl  * Determine the status of a lock.
    131  1.1  fvdl  */
    132  1.1  fvdl int
    133  1.1  fvdl lockstatus(lkp)
    134  1.1  fvdl 	struct lock *lkp;
    135  1.1  fvdl {
    136  1.1  fvdl 	int lock_type = 0;
    137  1.1  fvdl 
    138  1.1  fvdl 	simple_lock(&lkp->lk_interlock);
    139  1.1  fvdl 	if (lkp->lk_exclusivecount != 0)
    140  1.1  fvdl 		lock_type = LK_EXCLUSIVE;
    141  1.1  fvdl 	else if (lkp->lk_sharecount != 0)
    142  1.1  fvdl 		lock_type = LK_SHARED;
    143  1.1  fvdl 	simple_unlock(&lkp->lk_interlock);
    144  1.1  fvdl 	return (lock_type);
    145  1.1  fvdl }
    146  1.1  fvdl 
    147  1.1  fvdl /*
    148  1.1  fvdl  * Set, change, or release a lock.
    149  1.1  fvdl  *
    150  1.1  fvdl  * Shared requests increment the shared count. Exclusive requests set the
    151  1.1  fvdl  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    152  1.1  fvdl  * accepted shared locks and shared-to-exclusive upgrades to go away.
    153  1.1  fvdl  */
    154  1.1  fvdl int
    155  1.1  fvdl lockmgr(lkp, flags, interlkp, p)
    156  1.1  fvdl 	__volatile struct lock *lkp;
    157  1.1  fvdl 	u_int flags;
    158  1.1  fvdl 	struct simplelock *interlkp;
    159  1.1  fvdl 	struct proc *p;
    160  1.1  fvdl {
    161  1.1  fvdl 	int error;
    162  1.1  fvdl 	pid_t pid;
    163  1.1  fvdl 	int extflags;
    164  1.1  fvdl 
    165  1.1  fvdl 	error = 0;
    166  1.1  fvdl 	if (p)
    167  1.1  fvdl 		pid = p->p_pid;
    168  1.1  fvdl 	else
    169  1.1  fvdl 		pid = LK_KERNPROC;
    170  1.1  fvdl 	simple_lock(&lkp->lk_interlock);
    171  1.1  fvdl 	if (flags & LK_INTERLOCK)
    172  1.1  fvdl 		simple_unlock(interlkp);
    173  1.1  fvdl 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    174  1.1  fvdl #ifdef DIAGNOSTIC
    175  1.1  fvdl 	/*
    176  1.1  fvdl 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    177  1.1  fvdl 	 * exclusive lock is returned. The only valid operation thereafter
    178  1.1  fvdl 	 * is a single release of that exclusive lock. This final release
    179  1.1  fvdl 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    180  1.1  fvdl 	 * further requests of any sort will result in a panic. The bits
    181  1.1  fvdl 	 * selected for these two flags are chosen so that they will be set
    182  1.1  fvdl 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    183  1.1  fvdl 	 * The final release is permitted to give a new lease on life to
    184  1.1  fvdl 	 * the lock by specifying LK_REENABLE.
    185  1.1  fvdl 	 */
    186  1.1  fvdl 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    187  1.1  fvdl 		if (lkp->lk_flags & LK_DRAINED)
    188  1.1  fvdl 			panic("lockmgr: using decommissioned lock");
    189  1.1  fvdl 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    190  1.1  fvdl 		    lkp->lk_lockholder != pid)
    191  1.1  fvdl 			panic("lockmgr: non-release on draining lock: %d\n",
    192  1.1  fvdl 			    flags & LK_TYPE_MASK);
    193  1.1  fvdl 		lkp->lk_flags &= ~LK_DRAINING;
    194  1.1  fvdl 		if ((flags & LK_REENABLE) == 0)
    195  1.1  fvdl 			lkp->lk_flags |= LK_DRAINED;
    196  1.1  fvdl 	}
    197  1.1  fvdl #endif DIAGNOSTIC
    198  1.1  fvdl 
    199  1.1  fvdl 	switch (flags & LK_TYPE_MASK) {
    200  1.1  fvdl 
    201  1.1  fvdl 	case LK_SHARED:
    202  1.1  fvdl 		if (lkp->lk_lockholder != pid) {
    203  1.1  fvdl 			/*
    204  1.1  fvdl 			 * If just polling, check to see if we will block.
    205  1.1  fvdl 			 */
    206  1.1  fvdl 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    207  1.1  fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    208  1.1  fvdl 				error = EBUSY;
    209  1.1  fvdl 				break;
    210  1.1  fvdl 			}
    211  1.1  fvdl 			/*
    212  1.1  fvdl 			 * Wait for exclusive locks and upgrades to clear.
    213  1.1  fvdl 			 */
    214  1.1  fvdl 			ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    215  1.1  fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    216  1.1  fvdl 			if (error)
    217  1.1  fvdl 				break;
    218  1.1  fvdl 			lkp->lk_sharecount++;
    219  1.1  fvdl 			COUNT(p, 1);
    220  1.1  fvdl 			break;
    221  1.1  fvdl 		}
    222  1.1  fvdl 		/*
    223  1.1  fvdl 		 * We hold an exclusive lock, so downgrade it to shared.
    224  1.1  fvdl 		 * An alternative would be to fail with EDEADLK.
    225  1.1  fvdl 		 */
    226  1.1  fvdl 		lkp->lk_sharecount++;
    227  1.1  fvdl 		COUNT(p, 1);
    228  1.1  fvdl 		/* fall into downgrade */
    229  1.1  fvdl 
    230  1.1  fvdl 	case LK_DOWNGRADE:
    231  1.1  fvdl 		if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0)
    232  1.1  fvdl 			panic("lockmgr: not holding exclusive lock");
    233  1.1  fvdl 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    234  1.1  fvdl 		lkp->lk_exclusivecount = 0;
    235  1.1  fvdl 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    236  1.1  fvdl 		lkp->lk_lockholder = LK_NOPROC;
    237  1.1  fvdl 		if (lkp->lk_waitcount)
    238  1.1  fvdl 			wakeup((void *)lkp);
    239  1.1  fvdl 		break;
    240  1.1  fvdl 
    241  1.1  fvdl 	case LK_EXCLUPGRADE:
    242  1.1  fvdl 		/*
    243  1.1  fvdl 		 * If another process is ahead of us to get an upgrade,
    244  1.1  fvdl 		 * then we want to fail rather than have an intervening
    245  1.1  fvdl 		 * exclusive access.
    246  1.1  fvdl 		 */
    247  1.1  fvdl 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    248  1.1  fvdl 			lkp->lk_sharecount--;
    249  1.1  fvdl 			COUNT(p, -1);
    250  1.1  fvdl 			error = EBUSY;
    251  1.1  fvdl 			break;
    252  1.1  fvdl 		}
    253  1.1  fvdl 		/* fall into normal upgrade */
    254  1.1  fvdl 
    255  1.1  fvdl 	case LK_UPGRADE:
    256  1.1  fvdl 		/*
    257  1.1  fvdl 		 * Upgrade a shared lock to an exclusive one. If another
    258  1.1  fvdl 		 * shared lock has already requested an upgrade to an
    259  1.1  fvdl 		 * exclusive lock, our shared lock is released and an
    260  1.1  fvdl 		 * exclusive lock is requested (which will be granted
    261  1.1  fvdl 		 * after the upgrade). If we return an error, the file
    262  1.1  fvdl 		 * will always be unlocked.
    263  1.1  fvdl 		 */
    264  1.1  fvdl 		if (lkp->lk_lockholder == pid || lkp->lk_sharecount <= 0)
    265  1.1  fvdl 			panic("lockmgr: upgrade exclusive lock");
    266  1.1  fvdl 		lkp->lk_sharecount--;
    267  1.1  fvdl 		COUNT(p, -1);
    268  1.1  fvdl 		/*
    269  1.1  fvdl 		 * If we are just polling, check to see if we will block.
    270  1.1  fvdl 		 */
    271  1.1  fvdl 		if ((extflags & LK_NOWAIT) &&
    272  1.1  fvdl 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    273  1.1  fvdl 		     lkp->lk_sharecount > 1)) {
    274  1.1  fvdl 			error = EBUSY;
    275  1.1  fvdl 			break;
    276  1.1  fvdl 		}
    277  1.1  fvdl 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    278  1.1  fvdl 			/*
    279  1.1  fvdl 			 * We are first shared lock to request an upgrade, so
    280  1.1  fvdl 			 * request upgrade and wait for the shared count to
    281  1.1  fvdl 			 * drop to zero, then take exclusive lock.
    282  1.1  fvdl 			 */
    283  1.1  fvdl 			lkp->lk_flags |= LK_WANT_UPGRADE;
    284  1.1  fvdl 			ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
    285  1.1  fvdl 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    286  1.1  fvdl 			if (error)
    287  1.1  fvdl 				break;
    288  1.1  fvdl 			lkp->lk_flags |= LK_HAVE_EXCL;
    289  1.1  fvdl 			lkp->lk_lockholder = pid;
    290  1.1  fvdl 			if (lkp->lk_exclusivecount != 0)
    291  1.1  fvdl 				panic("lockmgr: non-zero exclusive count");
    292  1.1  fvdl 			lkp->lk_exclusivecount = 1;
    293  1.1  fvdl 			COUNT(p, 1);
    294  1.1  fvdl 			break;
    295  1.1  fvdl 		}
    296  1.1  fvdl 		/*
    297  1.1  fvdl 		 * Someone else has requested upgrade. Release our shared
    298  1.1  fvdl 		 * lock, awaken upgrade requestor if we are the last shared
    299  1.1  fvdl 		 * lock, then request an exclusive lock.
    300  1.1  fvdl 		 */
    301  1.1  fvdl 		if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
    302  1.1  fvdl 			wakeup((void *)lkp);
    303  1.1  fvdl 		/* fall into exclusive request */
    304  1.1  fvdl 
    305  1.1  fvdl 	case LK_EXCLUSIVE:
    306  1.1  fvdl 		if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) {
    307  1.1  fvdl 			/*
    308  1.1  fvdl 			 *	Recursive lock.
    309  1.1  fvdl 			 */
    310  1.1  fvdl 			if ((extflags & LK_CANRECURSE) == 0)
    311  1.1  fvdl 				panic("lockmgr: locking against myself");
    312  1.1  fvdl 			lkp->lk_exclusivecount++;
    313  1.1  fvdl 			COUNT(p, 1);
    314  1.1  fvdl 			break;
    315  1.1  fvdl 		}
    316  1.1  fvdl 		/*
    317  1.1  fvdl 		 * If we are just polling, check to see if we will sleep.
    318  1.1  fvdl 		 */
    319  1.1  fvdl 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    320  1.1  fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    321  1.1  fvdl 		     lkp->lk_sharecount != 0)) {
    322  1.1  fvdl 			error = EBUSY;
    323  1.1  fvdl 			break;
    324  1.1  fvdl 		}
    325  1.1  fvdl 		/*
    326  1.1  fvdl 		 * Try to acquire the want_exclusive flag.
    327  1.1  fvdl 		 */
    328  1.1  fvdl 		ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    329  1.1  fvdl 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    330  1.1  fvdl 		if (error)
    331  1.1  fvdl 			break;
    332  1.1  fvdl 		lkp->lk_flags |= LK_WANT_EXCL;
    333  1.1  fvdl 		/*
    334  1.1  fvdl 		 * Wait for shared locks and upgrades to finish.
    335  1.1  fvdl 		 */
    336  1.1  fvdl 		ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
    337  1.1  fvdl 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    338  1.1  fvdl 		lkp->lk_flags &= ~LK_WANT_EXCL;
    339  1.1  fvdl 		if (error)
    340  1.1  fvdl 			break;
    341  1.1  fvdl 		lkp->lk_flags |= LK_HAVE_EXCL;
    342  1.1  fvdl 		lkp->lk_lockholder = pid;
    343  1.1  fvdl 		if (lkp->lk_exclusivecount != 0)
    344  1.1  fvdl 			panic("lockmgr: non-zero exclusive count");
    345  1.1  fvdl 		lkp->lk_exclusivecount = 1;
    346  1.1  fvdl 		COUNT(p, 1);
    347  1.1  fvdl 		break;
    348  1.1  fvdl 
    349  1.1  fvdl 	case LK_RELEASE:
    350  1.1  fvdl 		if (lkp->lk_exclusivecount != 0) {
    351  1.1  fvdl 			if (pid != lkp->lk_lockholder)
    352  1.1  fvdl 				panic("lockmgr: pid %d, not %s %d unlocking",
    353  1.1  fvdl 				    pid, "exclusive lock holder",
    354  1.1  fvdl 				    lkp->lk_lockholder);
    355  1.1  fvdl 			lkp->lk_exclusivecount--;
    356  1.1  fvdl 			COUNT(p, -1);
    357  1.1  fvdl 			if (lkp->lk_exclusivecount == 0) {
    358  1.1  fvdl 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    359  1.1  fvdl 				lkp->lk_lockholder = LK_NOPROC;
    360  1.1  fvdl 			}
    361  1.1  fvdl 		} else if (lkp->lk_sharecount != 0) {
    362  1.1  fvdl 			lkp->lk_sharecount--;
    363  1.1  fvdl 			COUNT(p, -1);
    364  1.1  fvdl 		}
    365  1.1  fvdl 		if (lkp->lk_waitcount)
    366  1.1  fvdl 			wakeup((void *)lkp);
    367  1.1  fvdl 		break;
    368  1.1  fvdl 
    369  1.1  fvdl 	case LK_DRAIN:
    370  1.1  fvdl 		/*
    371  1.1  fvdl 		 * Check that we do not already hold the lock, as it can
    372  1.1  fvdl 		 * never drain if we do. Unfortunately, we have no way to
    373  1.1  fvdl 		 * check for holding a shared lock, but at least we can
    374  1.1  fvdl 		 * check for an exclusive one.
    375  1.1  fvdl 		 */
    376  1.1  fvdl 		if (lkp->lk_lockholder == pid)
    377  1.1  fvdl 			panic("lockmgr: draining against myself");
    378  1.1  fvdl 		/*
    379  1.1  fvdl 		 * If we are just polling, check to see if we will sleep.
    380  1.1  fvdl 		 */
    381  1.1  fvdl 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    382  1.1  fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    383  1.1  fvdl 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    384  1.1  fvdl 			error = EBUSY;
    385  1.1  fvdl 			break;
    386  1.1  fvdl 		}
    387  1.1  fvdl 		PAUSE(lkp, ((lkp->lk_flags &
    388  1.1  fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    389  1.1  fvdl 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0));
    390  1.1  fvdl 		for (error = 0; ((lkp->lk_flags &
    391  1.1  fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    392  1.1  fvdl 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0); ) {
    393  1.1  fvdl 			lkp->lk_flags |= LK_WAITDRAIN;
    394  1.1  fvdl 			simple_unlock(&lkp->lk_interlock);
    395  1.1  fvdl 			if (error = tsleep((void *)&lkp->lk_flags, lkp->lk_prio,
    396  1.1  fvdl 			    lkp->lk_wmesg, lkp->lk_timo))
    397  1.1  fvdl 				return (error);
    398  1.1  fvdl 			if ((extflags) & LK_SLEEPFAIL)
    399  1.1  fvdl 				return (ENOLCK);
    400  1.1  fvdl 			simple_lock(&lkp->lk_interlock);
    401  1.1  fvdl 		}
    402  1.1  fvdl 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    403  1.1  fvdl 		lkp->lk_lockholder = pid;
    404  1.1  fvdl 		lkp->lk_exclusivecount = 1;
    405  1.1  fvdl 		COUNT(p, 1);
    406  1.1  fvdl 		break;
    407  1.1  fvdl 
    408  1.1  fvdl 	default:
    409  1.1  fvdl 		simple_unlock(&lkp->lk_interlock);
    410  1.1  fvdl 		panic("lockmgr: unknown locktype request %d",
    411  1.1  fvdl 		    flags & LK_TYPE_MASK);
    412  1.1  fvdl 		/* NOTREACHED */
    413  1.1  fvdl 	}
    414  1.1  fvdl 	if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
    415  1.1  fvdl 	     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    416  1.1  fvdl 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    417  1.1  fvdl 		lkp->lk_flags &= ~LK_WAITDRAIN;
    418  1.1  fvdl 		wakeup((void *)&lkp->lk_flags);
    419  1.1  fvdl 	}
    420  1.1  fvdl 	simple_unlock(&lkp->lk_interlock);
    421  1.1  fvdl 	return (error);
    422  1.1  fvdl }
    423  1.1  fvdl 
    424  1.1  fvdl /*
    425  1.1  fvdl  * Print out information about state of a lock. Used by VOP_PRINT
    426  1.1  fvdl  * routines to display ststus about contained locks.
    427  1.1  fvdl  */
    428  1.1  fvdl lockmgr_printinfo(lkp)
    429  1.1  fvdl 	struct lock *lkp;
    430  1.1  fvdl {
    431  1.1  fvdl 
    432  1.1  fvdl 	if (lkp->lk_sharecount)
    433  1.1  fvdl 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    434  1.1  fvdl 		    lkp->lk_sharecount);
    435  1.1  fvdl 	else if (lkp->lk_flags & LK_HAVE_EXCL)
    436  1.1  fvdl 		printf(" lock type %s: EXCL (count %d) by pid %d",
    437  1.1  fvdl 		    lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
    438  1.1  fvdl 	if (lkp->lk_waitcount > 0)
    439  1.1  fvdl 		printf(" with %d pending", lkp->lk_waitcount);
    440  1.1  fvdl }
    441  1.1  fvdl 
    442  1.1  fvdl #if defined(DEBUG) && NCPUS == 1
    443  1.1  fvdl #include <sys/kernel.h>
    444  1.1  fvdl #include <vm/vm.h>
    445  1.1  fvdl #include <sys/sysctl.h>
    446  1.1  fvdl int lockpausetime = 0;
    447  1.1  fvdl struct ctldebug debug2 = { "lockpausetime", &lockpausetime };
    448  1.1  fvdl int simplelockrecurse;
    449  1.1  fvdl /*
    450  1.1  fvdl  * Simple lock functions so that the debugger can see from whence
    451  1.1  fvdl  * they are being called.
    452  1.1  fvdl  */
    453  1.1  fvdl void
    454  1.1  fvdl simple_lock_init(alp)
    455  1.1  fvdl 	struct simplelock *alp;
    456  1.1  fvdl {
    457  1.1  fvdl 
    458  1.1  fvdl 	alp->lock_data = 0;
    459  1.1  fvdl }
    460  1.1  fvdl 
    461  1.1  fvdl void
    462  1.1  fvdl _simple_lock(alp, id, l)
    463  1.1  fvdl 	__volatile struct simplelock *alp;
    464  1.1  fvdl 	const char *id;
    465  1.1  fvdl 	int l;
    466  1.1  fvdl {
    467  1.1  fvdl 
    468  1.1  fvdl 	if (simplelockrecurse)
    469  1.1  fvdl 		return;
    470  1.1  fvdl 	if (alp->lock_data == 1) {
    471  1.1  fvdl 		if (lockpausetime == -1)
    472  1.1  fvdl 			panic("%s:%d: simple_lock: lock held", id, l);
    473  1.1  fvdl 		printf("%s:%d: simple_lock: lock held\n", id, l);
    474  1.1  fvdl 		if (lockpausetime == 1) {
    475  1.1  fvdl 			BACKTRACE(curproc);
    476  1.1  fvdl 		} else if (lockpausetime > 1) {
    477  1.1  fvdl 			printf("%s:%d: simple_lock: lock held...", id, l);
    478  1.1  fvdl 			tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
    479  1.1  fvdl 			    lockpausetime * hz);
    480  1.1  fvdl 			printf(" continuing\n");
    481  1.1  fvdl 		}
    482  1.1  fvdl 	}
    483  1.1  fvdl 	alp->lock_data = 1;
    484  1.1  fvdl 	if (curproc)
    485  1.1  fvdl 		curproc->p_simple_locks++;
    486  1.1  fvdl }
    487  1.1  fvdl 
    488  1.1  fvdl int
    489  1.1  fvdl _simple_lock_try(alp, id, l)
    490  1.1  fvdl 	__volatile struct simplelock *alp;
    491  1.1  fvdl 	const char *id;
    492  1.1  fvdl 	int l;
    493  1.1  fvdl {
    494  1.1  fvdl 
    495  1.1  fvdl 	if (alp->lock_data)
    496  1.1  fvdl 		return (0);
    497  1.1  fvdl 	if (simplelockrecurse)
    498  1.1  fvdl 		return (1);
    499  1.1  fvdl 	alp->lock_data = 1;
    500  1.1  fvdl 	if (curproc)
    501  1.1  fvdl 		curproc->p_simple_locks++;
    502  1.1  fvdl 	return (1);
    503  1.1  fvdl }
    504  1.1  fvdl 
    505  1.1  fvdl void
    506  1.1  fvdl _simple_unlock(alp, id, l)
    507  1.1  fvdl 	__volatile struct simplelock *alp;
    508  1.1  fvdl 	const char *id;
    509  1.1  fvdl 	int l;
    510  1.1  fvdl {
    511  1.1  fvdl 
    512  1.1  fvdl 	if (simplelockrecurse)
    513  1.1  fvdl 		return;
    514  1.1  fvdl 	if (alp->lock_data == 0) {
    515  1.1  fvdl 		if (lockpausetime == -1)
    516  1.1  fvdl 			panic("%s:%d: simple_unlock: lock not held", id, l);
    517  1.1  fvdl 		printf("%s:%d: simple_unlock: lock not held\n", id, l);
    518  1.1  fvdl 		if (lockpausetime == 1) {
    519  1.1  fvdl 			BACKTRACE(curproc);
    520  1.1  fvdl 		} else if (lockpausetime > 1) {
    521  1.1  fvdl 			printf("%s:%d: simple_unlock: lock not held...", id, l);
    522  1.1  fvdl 			tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock",
    523  1.1  fvdl 			    lockpausetime * hz);
    524  1.1  fvdl 			printf(" continuing\n");
    525  1.1  fvdl 		}
    526  1.1  fvdl 	}
    527  1.1  fvdl 	alp->lock_data = 0;
    528  1.1  fvdl 	if (curproc)
    529  1.1  fvdl 		curproc->p_simple_locks--;
    530  1.1  fvdl }
    531  1.1  fvdl #endif /* DEBUG && NCPUS == 1 */
    532