Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.21
      1 /*	$NetBSD: kern_lock.c,v 1.21 1999/07/27 21:29:16 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * This code is derived from software contributed to The NetBSD Foundation
     12  * by Ross Harvey.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed by the NetBSD
     25  *	Foundation, Inc. and its contributors.
     26  * 4. Neither the name of The NetBSD Foundation nor the names of its
     27  *    contributors may be used to endorse or promote products derived
     28  *    from this software without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40  * POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 
     43 /*
     44  * Copyright (c) 1995
     45  *	The Regents of the University of California.  All rights reserved.
     46  *
     47  * This code contains ideas from software contributed to Berkeley by
     48  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
     49  * System project at Carnegie-Mellon University.
     50  *
     51  * Redistribution and use in source and binary forms, with or without
     52  * modification, are permitted provided that the following conditions
     53  * are met:
     54  * 1. Redistributions of source code must retain the above copyright
     55  *    notice, this list of conditions and the following disclaimer.
     56  * 2. Redistributions in binary form must reproduce the above copyright
     57  *    notice, this list of conditions and the following disclaimer in the
     58  *    documentation and/or other materials provided with the distribution.
     59  * 3. All advertising materials mentioning features or use of this software
     60  *    must display the following acknowledgement:
     61  *	This product includes software developed by the University of
     62  *	California, Berkeley and its contributors.
     63  * 4. Neither the name of the University nor the names of its contributors
     64  *    may be used to endorse or promote products derived from this software
     65  *    without specific prior written permission.
     66  *
     67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77  * SUCH DAMAGE.
     78  *
     79  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
     80  */
     81 
     82 #include "opt_multiprocessor.h"
     83 #include "opt_lockdebug.h"
     84 #include "opt_ddb.h"
     85 
     86 #include <sys/param.h>
     87 #include <sys/proc.h>
     88 #include <sys/lock.h>
     89 #include <sys/systm.h>
     90 #include <machine/cpu.h>
     91 
     92 /*
     93  * Locking primitives implementation.
     94  * Locks provide shared/exclusive sychronization.
     95  */
     96 
     97 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
     98 #if defined(MULTIPROCESSOR) /* { */
     99 #define	COUNT_CPU(cpu_id, x)						\
    100 	/* atomic_add_ulong(&curcpu().ci_spin_locks, (x)) */
    101 #else
    102 u_long	spin_locks;
    103 #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    104 #endif /* MULTIPROCESSOR */ /* } */
    105 
    106 #define	COUNT(lkp, p, cpu_id, x)					\
    107 do {									\
    108 	if ((lkp)->lk_flags & LK_SPIN)					\
    109 		COUNT_CPU((cpu_id), (x));				\
    110 	else								\
    111 		(p)->p_locks += (x);					\
    112 } while (0)
    113 #else
    114 #define COUNT(p, x)
    115 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    116 
    117 /*
    118  * Acquire a resource.
    119  */
    120 #define ACQUIRE(lkp, error, extflags, wanted)				\
    121 	if ((extflags) & LK_SPIN) {					\
    122 		int interlocked;					\
    123 									\
    124 		for (interlocked = 1;;) {				\
    125 			if (wanted) {					\
    126 				if (interlocked) {			\
    127 					simple_unlock(&(lkp)->lk_interlock); \
    128 					interlocked = 0;		\
    129 				}					\
    130 			} else if (interlocked) {			\
    131 				break;					\
    132 			} else {					\
    133 				simple_lock(&(lkp)->lk_interlock);	\
    134 				interlocked = 1;			\
    135 			}						\
    136 		}							\
    137 		KASSERT((wanted) == 0);					\
    138 		error = 0;	/* sanity */				\
    139 	} else {							\
    140 		for (error = 0; wanted; ) {				\
    141 			(lkp)->lk_waitcount++;				\
    142 			simple_unlock(&(lkp)->lk_interlock);		\
    143 			error = tsleep((void *)lkp, (lkp)->lk_prio,	\
    144 			    (lkp)->lk_wmesg, (lkp)->lk_timo);		\
    145 			simple_lock(&(lkp)->lk_interlock);		\
    146 			(lkp)->lk_waitcount--;				\
    147 			if (error)					\
    148 				break;					\
    149 			if ((extflags) & LK_SLEEPFAIL) {		\
    150 				error = ENOLCK;				\
    151 				break;					\
    152 			}						\
    153 		}							\
    154 	}
    155 
    156 #define	SETHOLDER(lkp, pid, cpu_id)					\
    157 do {									\
    158 	if ((lkp)->lk_flags & LK_SPIN)					\
    159 		(lkp)->lk_cpu = cpu_id;					\
    160 	else								\
    161 		(lkp)->lk_lockholder = pid;				\
    162 } while (0)
    163 
    164 #define	WEHOLDIT(lkp, pid, cpu_id)					\
    165 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    166 	 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
    167 
    168 #if defined(LOCKDEBUG) /* { */
    169 #if defined(MULTIPROCESSOR) /* { */
    170 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    171 
    172 #define	SPINLOCK_LIST_LOCK()	cpu_simple_lock(&spinlock_list_slock)
    173 
    174 #define	SPINLOCK_LIST_UNLOCK()	cpu_simple_unlock(&spinlock_list_slock)
    175 #else
    176 #define	SPINLOCK_LIST_LOCK()	/* nothing */
    177 
    178 #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    179 #endif /* MULTIPROCESSOR */ /* } */
    180 
    181 TAILQ_HEAD(, lock) spinlock_list =
    182     TAILQ_HEAD_INITIALIZER(spinlock_list);
    183 
    184 #define	HAVEIT(lkp)							\
    185 do {									\
    186 	if ((lkp)->lk_flags & LK_SPIN) {				\
    187 		int s = splhigh();					\
    188 		SPINLOCK_LIST_LOCK();					\
    189 		/* XXX Cast away volatile. */				\
    190 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
    191 		    lk_list);						\
    192 		SPINLOCK_LIST_UNLOCK();					\
    193 		splx(s);						\
    194 	}								\
    195 } while (0)
    196 
    197 #define	DONTHAVEIT(lkp)							\
    198 do {									\
    199 	if ((lkp)->lk_flags & LK_SPIN) {				\
    200 		int s = splhigh();					\
    201 		SPINLOCK_LIST_LOCK();					\
    202 		/* XXX Cast away volatile. */				\
    203 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
    204 		    lk_list);						\
    205 		SPINLOCK_LIST_UNLOCK();					\
    206 		splx(s);						\
    207 	}								\
    208 } while (0)
    209 #else
    210 #define	HAVEIT(lkp)		/* nothing */
    211 
    212 #define	DONTHAVEIT(lkp)		/* nothing */
    213 #endif /* LOCKDEBUG */ /* } */
    214 
    215 /*
    216  * Initialize a lock; required before use.
    217  */
    218 void
    219 lockinit(lkp, prio, wmesg, timo, flags)
    220 	struct lock *lkp;
    221 	int prio;
    222 	const char *wmesg;
    223 	int timo;
    224 	int flags;
    225 {
    226 
    227 	memset(lkp, 0, sizeof(struct lock));
    228 	simple_lock_init(&lkp->lk_interlock);
    229 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    230 	if (flags & LK_SPIN)
    231 		lkp->lk_cpu = LK_NOCPU;
    232 	else {
    233 		lkp->lk_lockholder = LK_NOPROC;
    234 		lkp->lk_prio = prio;
    235 		lkp->lk_timo = timo;
    236 	}
    237 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    238 }
    239 
    240 /*
    241  * Determine the status of a lock.
    242  */
    243 int
    244 lockstatus(lkp)
    245 	struct lock *lkp;
    246 {
    247 	int lock_type = 0;
    248 
    249 	simple_lock(&lkp->lk_interlock);
    250 	if (lkp->lk_exclusivecount != 0)
    251 		lock_type = LK_EXCLUSIVE;
    252 	else if (lkp->lk_sharecount != 0)
    253 		lock_type = LK_SHARED;
    254 	simple_unlock(&lkp->lk_interlock);
    255 	return (lock_type);
    256 }
    257 
    258 /*
    259  * Set, change, or release a lock.
    260  *
    261  * Shared requests increment the shared count. Exclusive requests set the
    262  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    263  * accepted shared locks and shared-to-exclusive upgrades to go away.
    264  */
    265 int
    266 lockmgr(lkp, flags, interlkp)
    267 	__volatile struct lock *lkp;
    268 	u_int flags;
    269 	struct simplelock *interlkp;
    270 {
    271 	int error;
    272 	pid_t pid;
    273 	int extflags;
    274 	u_long cpu_id;
    275 	struct proc *p = curproc;
    276 
    277 	error = 0;
    278 
    279 	simple_lock(&lkp->lk_interlock);
    280 	if (flags & LK_INTERLOCK)
    281 		simple_unlock(interlkp);
    282 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    283 
    284 #ifdef DIAGNOSTIC /* { */
    285 	/*
    286 	 * Don't allow spins on sleep locks and don't allow sleeps
    287 	 * on spin locks.
    288 	 */
    289 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    290 		panic("lockmgr: sleep/spin mismatch\n");
    291 #endif /* } */
    292 
    293 	if (extflags & LK_SPIN)
    294 		pid = LK_KERNPROC;
    295 	else {
    296 #ifdef DIAGNOSTIC /* { */
    297 		if (p == NULL)
    298 			panic("lockmgr: no context");
    299 #endif /* } */
    300 		pid = p->p_pid;
    301 	}
    302 	cpu_id = 0;			/* XXX cpu_number() XXX */
    303 
    304 #ifdef DIAGNOSTIC /* { */
    305 	/*
    306 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    307 	 * exclusive lock is returned. The only valid operation thereafter
    308 	 * is a single release of that exclusive lock. This final release
    309 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    310 	 * further requests of any sort will result in a panic. The bits
    311 	 * selected for these two flags are chosen so that they will be set
    312 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    313 	 * The final release is permitted to give a new lease on life to
    314 	 * the lock by specifying LK_REENABLE.
    315 	 */
    316 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    317 		if (lkp->lk_flags & LK_DRAINED)
    318 			panic("lockmgr: using decommissioned lock");
    319 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    320 		    WEHOLDIT(lkp, pid, cpu_id) == 0)
    321 			panic("lockmgr: non-release on draining lock: %d\n",
    322 			    flags & LK_TYPE_MASK);
    323 		lkp->lk_flags &= ~LK_DRAINING;
    324 		if ((flags & LK_REENABLE) == 0)
    325 			lkp->lk_flags |= LK_DRAINED;
    326 	}
    327 #endif /* DIAGNOSTIC */ /* } */
    328 
    329 	switch (flags & LK_TYPE_MASK) {
    330 
    331 	case LK_SHARED:
    332 		if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    333 			/*
    334 			 * If just polling, check to see if we will block.
    335 			 */
    336 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    337 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    338 				error = EBUSY;
    339 				break;
    340 			}
    341 			/*
    342 			 * Wait for exclusive locks and upgrades to clear.
    343 			 */
    344 			ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    345 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    346 			if (error)
    347 				break;
    348 			lkp->lk_sharecount++;
    349 			COUNT(lkp, p, cpu_id, 1);
    350 			break;
    351 		}
    352 		/*
    353 		 * We hold an exclusive lock, so downgrade it to shared.
    354 		 * An alternative would be to fail with EDEADLK.
    355 		 */
    356 		lkp->lk_sharecount++;
    357 		COUNT(lkp, p, cpu_id, 1);
    358 		/* fall into downgrade */
    359 
    360 	case LK_DOWNGRADE:
    361 		if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
    362 		    lkp->lk_exclusivecount == 0)
    363 			panic("lockmgr: not holding exclusive lock");
    364 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    365 		lkp->lk_exclusivecount = 0;
    366 		lkp->lk_recurselevel = 0;
    367 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    368 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    369 		DONTHAVEIT(lkp);
    370 		if (lkp->lk_waitcount)
    371 			wakeup_one((void *)lkp);
    372 		break;
    373 
    374 	case LK_EXCLUPGRADE:
    375 		/*
    376 		 * If another process is ahead of us to get an upgrade,
    377 		 * then we want to fail rather than have an intervening
    378 		 * exclusive access.
    379 		 */
    380 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    381 			lkp->lk_sharecount--;
    382 			COUNT(lkp, p, cpu_id, -1);
    383 			error = EBUSY;
    384 			break;
    385 		}
    386 		/* fall into normal upgrade */
    387 
    388 	case LK_UPGRADE:
    389 		/*
    390 		 * Upgrade a shared lock to an exclusive one. If another
    391 		 * shared lock has already requested an upgrade to an
    392 		 * exclusive lock, our shared lock is released and an
    393 		 * exclusive lock is requested (which will be granted
    394 		 * after the upgrade). If we return an error, the file
    395 		 * will always be unlocked.
    396 		 */
    397 		if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
    398 			panic("lockmgr: upgrade exclusive lock");
    399 		lkp->lk_sharecount--;
    400 		COUNT(lkp, p, cpu_id, -1);
    401 		/*
    402 		 * If we are just polling, check to see if we will block.
    403 		 */
    404 		if ((extflags & LK_NOWAIT) &&
    405 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    406 		     lkp->lk_sharecount > 1)) {
    407 			error = EBUSY;
    408 			break;
    409 		}
    410 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    411 			/*
    412 			 * We are first shared lock to request an upgrade, so
    413 			 * request upgrade and wait for the shared count to
    414 			 * drop to zero, then take exclusive lock.
    415 			 */
    416 			lkp->lk_flags |= LK_WANT_UPGRADE;
    417 			ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
    418 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    419 			if (error)
    420 				break;
    421 			lkp->lk_flags |= LK_HAVE_EXCL;
    422 			SETHOLDER(lkp, pid, cpu_id);
    423 			HAVEIT(lkp);
    424 			if (lkp->lk_exclusivecount != 0)
    425 				panic("lockmgr: non-zero exclusive count");
    426 			lkp->lk_exclusivecount = 1;
    427 			if (extflags & LK_SETRECURSE)
    428 				lkp->lk_recurselevel = 1;
    429 			COUNT(lkp, p, cpu_id, 1);
    430 			break;
    431 		}
    432 		/*
    433 		 * Someone else has requested upgrade. Release our shared
    434 		 * lock, awaken upgrade requestor if we are the last shared
    435 		 * lock, then request an exclusive lock.
    436 		 */
    437 		if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
    438 			wakeup_one((void *)lkp);
    439 		/* fall into exclusive request */
    440 
    441 	case LK_EXCLUSIVE:
    442 		if (WEHOLDIT(lkp, pid, cpu_id)) {
    443 			/*
    444 			 * Recursive lock.
    445 			 */
    446 			if ((extflags & LK_CANRECURSE) == 0 &&
    447 			     lkp->lk_recurselevel == 0) {
    448 				if (extflags & LK_RECURSEFAIL) {
    449 					error = EDEADLK;
    450 					break;
    451 				} else
    452 					panic("lockmgr: locking against myself");
    453 			}
    454 			lkp->lk_exclusivecount++;
    455 			if (extflags & LK_SETRECURSE &&
    456 			    lkp->lk_recurselevel == 0)
    457 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    458 			COUNT(lkp, p, cpu_id, 1);
    459 			break;
    460 		}
    461 		/*
    462 		 * If we are just polling, check to see if we will sleep.
    463 		 */
    464 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    465 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    466 		     lkp->lk_sharecount != 0)) {
    467 			error = EBUSY;
    468 			break;
    469 		}
    470 		/*
    471 		 * Try to acquire the want_exclusive flag.
    472 		 */
    473 		ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    474 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    475 		if (error)
    476 			break;
    477 		lkp->lk_flags |= LK_WANT_EXCL;
    478 		/*
    479 		 * Wait for shared locks and upgrades to finish.
    480 		 */
    481 		ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
    482 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    483 		lkp->lk_flags &= ~LK_WANT_EXCL;
    484 		if (error)
    485 			break;
    486 		lkp->lk_flags |= LK_HAVE_EXCL;
    487 		SETHOLDER(lkp, pid, cpu_id);
    488 		HAVEIT(lkp);
    489 		if (lkp->lk_exclusivecount != 0)
    490 			panic("lockmgr: non-zero exclusive count");
    491 		lkp->lk_exclusivecount = 1;
    492 		if (extflags & LK_SETRECURSE)
    493 			lkp->lk_recurselevel = 1;
    494 		COUNT(lkp, p, cpu_id, 1);
    495 		break;
    496 
    497 	case LK_RELEASE:
    498 		if (lkp->lk_exclusivecount != 0) {
    499 			if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    500 				if (lkp->lk_flags & LK_SPIN) {
    501 					panic("lockmgr: processor %lu, not "
    502 					    "exclusive lock holder %lu "
    503 					    "unlocking", cpu_id, lkp->lk_cpu);
    504 				} else {
    505 					panic("lockmgr: pid %d, not "
    506 					    "exclusive lock holder %d "
    507 					    "unlocking", pid,
    508 					    lkp->lk_lockholder);
    509 				}
    510 			}
    511 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    512 				lkp->lk_recurselevel = 0;
    513 			lkp->lk_exclusivecount--;
    514 			COUNT(lkp, p, cpu_id, -1);
    515 			if (lkp->lk_exclusivecount == 0) {
    516 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    517 				SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    518 				DONTHAVEIT(lkp);
    519 			}
    520 		} else if (lkp->lk_sharecount != 0) {
    521 			lkp->lk_sharecount--;
    522 			COUNT(lkp, p, cpu_id, -1);
    523 		}
    524 		if (lkp->lk_waitcount)
    525 			wakeup_one((void *)lkp);
    526 		break;
    527 
    528 	case LK_DRAIN:
    529 		/*
    530 		 * Check that we do not already hold the lock, as it can
    531 		 * never drain if we do. Unfortunately, we have no way to
    532 		 * check for holding a shared lock, but at least we can
    533 		 * check for an exclusive one.
    534 		 */
    535 		if (WEHOLDIT(lkp, pid, cpu_id))
    536 			panic("lockmgr: draining against myself");
    537 		/*
    538 		 * If we are just polling, check to see if we will sleep.
    539 		 */
    540 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    541 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    542 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    543 			error = EBUSY;
    544 			break;
    545 		}
    546 		if (lkp->lk_flags & LK_SPIN) {
    547 			ACQUIRE(lkp, error, extflags,
    548 			    ((lkp->lk_flags &
    549 			     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    550 			     lkp->lk_sharecount != 0 ||
    551 			     lkp->lk_waitcount != 0));
    552 		} else {
    553 			/*
    554 			 * This is just a special cause of the sleep case
    555 			 * in ACQUIRE().  We set WANTDRAIN instead of
    556 			 * incrementing waitcount.
    557 			 */
    558 			for (error = 0; ((lkp->lk_flags &
    559 			     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    560 			     lkp->lk_sharecount != 0 ||
    561 			     lkp->lk_waitcount != 0); ) {
    562 				lkp->lk_flags |= LK_WAITDRAIN;
    563 				simple_unlock(&lkp->lk_interlock);
    564 				if ((error = tsleep((void *)&lkp->lk_flags,
    565 				    lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo)))
    566 					return (error);
    567 				if ((extflags) & LK_SLEEPFAIL)
    568 					return (ENOLCK);
    569 				simple_lock(&lkp->lk_interlock);
    570 			}
    571 		}
    572 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    573 		SETHOLDER(lkp, pid, cpu_id);
    574 		HAVEIT(lkp);
    575 		lkp->lk_exclusivecount = 1;
    576 		/* XXX unlikely that we'd want this */
    577 		if (extflags & LK_SETRECURSE)
    578 			lkp->lk_recurselevel = 1;
    579 		COUNT(lkp, p, cpu_id, 1);
    580 		break;
    581 
    582 	default:
    583 		simple_unlock(&lkp->lk_interlock);
    584 		panic("lockmgr: unknown locktype request %d",
    585 		    flags & LK_TYPE_MASK);
    586 		/* NOTREACHED */
    587 	}
    588 	if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
    589 	     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    590 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    591 		lkp->lk_flags &= ~LK_WAITDRAIN;
    592 		wakeup_one((void *)&lkp->lk_flags);
    593 	}
    594 	simple_unlock(&lkp->lk_interlock);
    595 	return (error);
    596 }
    597 
    598 /*
    599  * Print out information about state of a lock. Used by VOP_PRINT
    600  * routines to display ststus about contained locks.
    601  */
    602 void
    603 lockmgr_printinfo(lkp)
    604 	__volatile struct lock *lkp;
    605 {
    606 
    607 	if (lkp->lk_sharecount)
    608 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    609 		    lkp->lk_sharecount);
    610 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    611 		printf(" lock type %s: EXCL (count %d) by ",
    612 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    613 		if (lkp->lk_flags & LK_SPIN)
    614 			printf("processor %lu", lkp->lk_cpu);
    615 		else
    616 			printf("pid %d", lkp->lk_lockholder);
    617 	} else
    618 		printf(" not locked");
    619 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    620 		printf(" with %d pending", lkp->lk_waitcount);
    621 }
    622 
    623 #if defined(LOCKDEBUG) /* { */
    624 TAILQ_HEAD(, simplelock) simplelock_list =
    625     TAILQ_HEAD_INITIALIZER(simplelock_list);
    626 
    627 #if defined(MULTIPROCESSOR) /* { */
    628 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    629 
    630 #define	SLOCK_LIST_LOCK()						\
    631 	cpu_simple_lock(&simplelock_list_slock)
    632 
    633 #define	SLOCK_LIST_UNLOCK()						\
    634 	cpu_simple_unlock(&simplelock_list_slock)
    635 
    636 #define	SLOCK_COUNT(x)							\
    637 	/* atomic_add_ulong(&curcpu()->ci_simple_locks, (x)) */
    638 #else
    639 u_long simple_locks;
    640 
    641 #define	SLOCK_LIST_LOCK()	/* nothing */
    642 
    643 #define	SLOCK_LIST_UNLOCK()	/* nothing */
    644 
    645 #define	SLOCK_COUNT(x)		simple_locks += (x)
    646 #endif /* MULTIPROCESSOR */ /* } */
    647 
    648 #ifdef DDB /* { */
    649 int simple_lock_debugger = 0;
    650 #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    651 #else
    652 #define	SLOCK_DEBUGGER()	/* nothing */
    653 #endif /* } */
    654 
    655 #define	SLOCK_WHERE(str, alp, id, l)					\
    656 do {									\
    657 	printf(str);							\
    658 	printf("currently at: %s:%d\n", (id), (l));			\
    659 	if ((alp)->lock_file != NULL)					\
    660 		printf("last locked: %s:%d\n", (alp)->lock_file,	\
    661 		    (alp)->lock_line);					\
    662 	if ((alp)->unlock_file != NULL)					\
    663 		printf("last unlocked: %s:%d\n", (alp)->unlock_file,	\
    664 		    (alp)->unlock_line);				\
    665 	SLOCK_DEBUGGER();						\
    666 } while (0)
    667 
    668 /*
    669  * Simple lock functions so that the debugger can see from whence
    670  * they are being called.
    671  */
    672 void
    673 simple_lock_init(alp)
    674 	struct simplelock *alp;
    675 {
    676 
    677 #if defined(MULTIPROCESSOR) /* { */
    678 	cpu_simple_lock_init(alp);
    679 #else
    680 	alp->lock_data = SIMPLELOCK_UNLOCKED;
    681 #endif /* } */
    682 	alp->lock_file = NULL;
    683 	alp->lock_line = 0;
    684 	alp->unlock_file = NULL;
    685 	alp->unlock_line = 0;
    686 	alp->lock_holder = 0;
    687 }
    688 
    689 void
    690 _simple_lock(alp, id, l)
    691 	__volatile struct simplelock *alp;
    692 	const char *id;
    693 	int l;
    694 {
    695 	u_long cpu_id = 0 /* XXX cpu_number() XXX */;
    696 	int s;
    697 
    698 	s = splhigh();
    699 
    700 	/*
    701 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    702 	 * don't take any action, and just fall into the normal spin case.
    703 	 */
    704 	if (alp->lock_data == SIMPLELOCK_LOCKED) {
    705 #if defined(MULTIPROCESSOR) /* { */
    706 		if (alp->lock_holder == cpu_id) {
    707 			SLOCK_WHERE("simple_lock: locking against myself\n",
    708 			    alp, id, l);
    709 			goto out;
    710 		}
    711 #else
    712 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
    713 		goto out;
    714 #endif /* MULTIPROCESSOR */ /* } */
    715 	}
    716 
    717 #if defined(MULTIPROCESSOR) /* { */
    718 	/* Acquire the lock before modifying any fields. */
    719 	cpu_simple_lock(alp);
    720 #else
    721 	alp->lock_data = SIMPLELOCK_LOCKED;
    722 #endif /* } */
    723 
    724 	alp->lock_file = id;
    725 	alp->lock_line = l;
    726 	alp->lock_holder = cpu_id;
    727 
    728 	SLOCK_LIST_LOCK();
    729 	/* XXX Cast away volatile */
    730 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    731 	SLOCK_LIST_UNLOCK();
    732 
    733 	SLOCK_COUNT(1);
    734 
    735  out:
    736 	splx(s);
    737 }
    738 
    739 int
    740 _simple_lock_try(alp, id, l)
    741 	__volatile struct simplelock *alp;
    742 	const char *id;
    743 	int l;
    744 {
    745 	u_long cpu_id = 0 /* XXX cpu_number() XXX */;
    746 	int s, rv = 0;
    747 
    748 	s = splhigh();
    749 
    750 	/*
    751 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    752 	 * don't take any action.
    753 	 */
    754 #if defined(MULTIPROCESSOR) /* { */
    755 	if ((rv = cpu_simple_lock_try(alp)) == 0) {
    756 		if (alp->lock_holder == cpu_id)
    757 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
    758 			    alp, id l);
    759 		goto out;
    760 	}
    761 #else
    762 	if (alp->lock_data == SIMPLELOCK_LOCKED) {
    763 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
    764 		goto out;
    765 	}
    766 	alp->lock_data = SIMPLELOCK_LOCKED;
    767 #endif /* MULTIPROCESSOR */ /* } */
    768 
    769 	/*
    770 	 * At this point, we have acquired the lock.
    771 	 */
    772 
    773 	rv = 1;
    774 
    775 	alp->lock_file = id;
    776 	alp->lock_line = l;
    777 	alp->lock_holder = cpu_id;
    778 
    779 	SLOCK_LIST_LOCK();
    780 	/* XXX Cast away volatile. */
    781 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    782 	SLOCK_LIST_UNLOCK();
    783 
    784 	SLOCK_COUNT(1);
    785 
    786  out:
    787 	splx(s);
    788 	return (rv);
    789 }
    790 
    791 void
    792 _simple_unlock(alp, id, l)
    793 	__volatile struct simplelock *alp;
    794 	const char *id;
    795 	int l;
    796 {
    797 	int s;
    798 
    799 	s = splhigh();
    800 
    801 	/*
    802 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
    803 	 * the lock, and if we don't, we don't take any action.
    804 	 */
    805 	if (alp->lock_data == SIMPLELOCK_UNLOCKED) {
    806 		SLOCK_WHERE("simple_unlock: lock not held\n",
    807 		    alp, id, l);
    808 		goto out;
    809 	}
    810 
    811 	SLOCK_LIST_LOCK();
    812 	TAILQ_REMOVE(&simplelock_list, alp, list);
    813 	SLOCK_LIST_UNLOCK();
    814 
    815 	SLOCK_COUNT(-1);
    816 
    817 	alp->list.tqe_next = NULL;	/* sanity */
    818 	alp->list.tqe_prev = NULL;	/* sanity */
    819 
    820 	alp->unlock_file = id;
    821 	alp->unlock_line = l;
    822 
    823 #if defined(MULTIPROCESSOR) /* { */
    824 	/* Now that we've modified all fields, release the lock. */
    825 	cpu_simple_unlock(alp);
    826 #else
    827 	alp->lock_data = SIMPLELOCK_UNLOCKED;
    828 #endif /* } */
    829 
    830  out:
    831 	splx(s);
    832 }
    833 
    834 void
    835 simple_lock_dump()
    836 {
    837 	struct simplelock *alp;
    838 	int s;
    839 
    840 	s = splhigh();
    841 	SLOCK_LIST_LOCK();
    842 	printf("all simple locks:\n");
    843 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    844 	     alp = TAILQ_NEXT(alp, list)) {
    845 		printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
    846 		    alp->lock_file, alp->lock_line);
    847 	}
    848 	SLOCK_LIST_UNLOCK();
    849 	splx(s);
    850 }
    851 
    852 void
    853 simple_lock_freecheck(start, end)
    854 	void *start, *end;
    855 {
    856 	struct simplelock *alp;
    857 	int s;
    858 
    859 	s = splhigh();
    860 	SLOCK_LIST_LOCK();
    861 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    862 	     alp = TAILQ_NEXT(alp, list)) {
    863 		if ((void *)alp >= start && (void *)alp < end) {
    864 			printf("freeing simple_lock %p CPU %lu %s:%d\n",
    865 			    alp, alp->lock_holder, alp->lock_file,
    866 			    alp->lock_line);
    867 			SLOCK_DEBUGGER();
    868 		}
    869 	}
    870 	SLOCK_LIST_UNLOCK();
    871 	splx(s);
    872 }
    873 #endif /* LOCKDEBUG */ /* } */
    874