File:  [DragonFly] / src / sys / vfs / nullfs / Attic / null_subr.c
Revision 1.7: download - view: text, annotated - select for diffs
Mon Mar 1 06:33:22 2004 UTC (10 years, 4 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
Newtoken commit.  Change the token implementation as follows:  (1) Obtaining
a token no longer enters a critical section.  (2) tokens can be held through
schedular switches and blocking conditions and are effectively released and
reacquired on resume.  Thus tokens serialize access only while the thread
is actually running.  Serialization is not broken by preemptive interrupts.
That is, interrupt threads which preempt do no release the preempted thread's
tokens.  (3) Unlike spl's, tokens will interlock w/ interrupt threads on
the same or on a different cpu.

The vnode interlock code has been rewritten and the API has changed.  The
mountlist vnode scanning code has been consolidated and all known races have
been fixed.  The vnode interlock is now a pool token.

The code that frees unreferenced vnodes whos last VM page has been freed has
been moved out of the low level vm_page_free() code and moved to the
periodic filesystem sycer code in vfs_msycn().

The SMP startup code and the IPI code has been cleaned up considerably.
Certain early token interactions on AP cpus have been moved to the BSP.

The LWKT rwlock API has been cleaned up and turned on.

Major testing by: David Rhodus

    1: /*
    2:  * Copyright (c) 1992, 1993
    3:  *	The Regents of the University of California.  All rights reserved.
    4:  *
    5:  * This code is derived from software donated to Berkeley by
    6:  * Jan-Simon Pendry.
    7:  *
    8:  * Redistribution and use in source and binary forms, with or without
    9:  * modification, are permitted provided that the following conditions
   10:  * are met:
   11:  * 1. Redistributions of source code must retain the above copyright
   12:  *    notice, this list of conditions and the following disclaimer.
   13:  * 2. Redistributions in binary form must reproduce the above copyright
   14:  *    notice, this list of conditions and the following disclaimer in the
   15:  *    documentation and/or other materials provided with the distribution.
   16:  * 3. All advertising materials mentioning features or use of this software
   17:  *    must display the following acknowledgement:
   18:  *	This product includes software developed by the University of
   19:  *	California, Berkeley and its contributors.
   20:  * 4. Neither the name of the University nor the names of its contributors
   21:  *    may be used to endorse or promote products derived from this software
   22:  *    without specific prior written permission.
   23:  *
   24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34:  * SUCH DAMAGE.
   35:  *
   36:  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
   37:  *
   38:  * $FreeBSD: src/sys/miscfs/nullfs/null_subr.c,v 1.21.2.4 2001/06/26 04:20:09 bp Exp $
   39:  * $DragonFly: src/sys/vfs/nullfs/null_subr.c,v 1.7 2004/03/01 06:33:22 dillon Exp $
   40:  */
   41: 
   42: #include <sys/param.h>
   43: #include <sys/systm.h>
   44: #include <sys/kernel.h>
   45: #include <sys/proc.h>
   46: #include <sys/vnode.h>
   47: #include <sys/mount.h>
   48: #include <sys/malloc.h>
   49: #include "null.h"
   50: 
   51: #define LOG2_SIZEVNODE 7		/* log2(sizeof struct vnode) */
   52: #define	NNULLNODECACHE 16
   53: 
   54: /*
   55:  * Null layer cache:
   56:  * Each cache entry holds a reference to the lower vnode
   57:  * along with a pointer to the alias vnode.  When an
   58:  * entry is added the lower vnode is VREF'd.  When the
   59:  * alias is removed the lower vnode is vrele'd.
   60:  */
   61: 
   62: #define	NULL_NHASH(vp) \
   63: 	(&null_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & null_node_hash])
   64: 
   65: static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
   66: static u_long null_node_hash;
   67: struct lock null_hashlock;
   68: 
   69: static MALLOC_DEFINE(M_NULLFSHASH, "NULLFS hash", "NULLFS hash table");
   70: MALLOC_DEFINE(M_NULLFSNODE, "NULLFS node", "NULLFS vnode private part");
   71: 
   72: static int	null_node_alloc(struct mount *mp, struct vnode *lowervp,
   73: 				     struct vnode **vpp);
   74: static struct vnode *
   75: 		null_node_find(struct mount *mp, struct vnode *lowervp);
   76: 
   77: /*
   78:  * Initialise cache headers
   79:  */
   80: int
   81: nullfs_init(vfsp)
   82: 	struct vfsconf *vfsp;
   83: {
   84: 
   85: 	NULLFSDEBUG("nullfs_init\n");		/* printed during system boot */
   86: 	null_node_hashtbl = hashinit(NNULLNODECACHE, M_NULLFSHASH, &null_node_hash);
   87: 	lockinit(&null_hashlock, 0, "nullhs", 0, 0);
   88: 	return (0);
   89: }
   90: 
   91: int
   92: nullfs_uninit(vfsp)
   93: 	struct vfsconf *vfsp;
   94: {
   95: 
   96:         if (null_node_hashtbl) {
   97: 		free(null_node_hashtbl, M_NULLFSHASH);
   98: 	}
   99: 	return (0);
  100: }
  101: 
  102: /*
  103:  * Return a VREF'ed alias for lower vnode if already exists, else 0.
  104:  * Lower vnode should be locked on entry and will be left locked on exit.
  105:  */
  106: static struct vnode *
  107: null_node_find(mp, lowervp)
  108: 	struct mount *mp;
  109: 	struct vnode *lowervp;
  110: {
  111: 	struct thread *td = curthread;	/* XXX */
  112: 	struct null_node_hashhead *hd;
  113: 	struct null_node *a;
  114: 	struct vnode *vp;
  115: 
  116: 	/*
  117: 	 * Find hash base, and then search the (two-way) linked
  118: 	 * list looking for a null_node structure which is referencing
  119: 	 * the lower vnode.  If found, the increment the null_node
  120: 	 * reference count (but NOT the lower vnode's VREF counter).
  121: 	 */
  122: 	hd = NULL_NHASH(lowervp);
  123: loop:
  124: 	lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, td);
  125: 	LIST_FOREACH(a, hd, null_hash) {
  126: 		if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
  127: 			vp = NULLTOV(a);
  128: 			lockmgr(&null_hashlock, LK_RELEASE, NULL, td);
  129: 			/*
  130: 			 * We need vget for the VXLOCK
  131: 			 * stuff, but we don't want to lock
  132: 			 * the lower node.
  133: 			 */
  134: 			if (vget(vp, NULL, LK_EXCLUSIVE | LK_CANRECURSE, td)) {
  135: 				printf ("null_node_find: vget failed.\n");
  136: 				goto loop;
  137: 			}
  138: 			VOP_UNLOCK(lowervp, NULL, 0, td);
  139: 			return (vp);
  140: 		}
  141: 	}
  142: 	lockmgr(&null_hashlock, LK_RELEASE, NULL, td);
  143: 
  144: 	return NULLVP;
  145: }
  146: 
  147: 
  148: /*
  149:  * Make a new null_node node.
  150:  * Vp is the alias vnode, lofsvp is the lower vnode.
  151:  * Maintain a reference to (lowervp).
  152:  */
  153: static int
  154: null_node_alloc(mp, lowervp, vpp)
  155: 	struct mount *mp;
  156: 	struct vnode *lowervp;
  157: 	struct vnode **vpp;
  158: {
  159: 	struct thread *td = curthread;	/* XXX */
  160: 	struct null_node_hashhead *hd;
  161: 	struct null_node *xp;
  162: 	struct vnode *othervp, *vp;
  163: 	int error;
  164: 
  165: 	/*
  166: 	 * Do the MALLOC before the getnewvnode since doing so afterward
  167: 	 * might cause a bogus v_data pointer to get dereferenced
  168: 	 * elsewhere if MALLOC should block.
  169: 	 */
  170: 	MALLOC(xp, struct null_node *, sizeof(struct null_node),
  171: 	    M_NULLFSNODE, M_WAITOK);
  172: 
  173: 	error = getnewvnode(VT_NULL, mp, null_vnodeop_p, vpp);
  174: 	if (error) {
  175: 		FREE(xp, M_NULLFSNODE);
  176: 		return (error);
  177: 	}
  178: 	vp = *vpp;
  179: 
  180: 	vp->v_type = lowervp->v_type;
  181: 
  182: 	/*
  183: 	 * XXX:
  184: 	 * When nullfs encounters sockets or device nodes, it
  185: 	 * has a hard time working with the normal vp union.
  186: 	 * This still needs to be investigated.
  187: 	 */
  188: 	if (vp->v_type == VCHR || vp->v_type == VBLK)
  189: 		addalias(vp, lowervp->v_un.vu_spec.vu_specinfo);
  190: 	else
  191: 		vp->v_un = lowervp->v_un;
  192: 	lockinit(&xp->null_lock, 0, "nullnode", 0, LK_CANRECURSE);
  193: 	xp->null_vnode = vp;
  194: 	vp->v_data = xp;
  195: 	xp->null_lowervp = lowervp;
  196: 	/*
  197: 	 * Before we insert our new node onto the hash chains,
  198: 	 * check to see if someone else has beaten us to it.
  199: 	 * (We could have slept in MALLOC.)
  200: 	 */
  201: 	othervp = null_node_find(mp, lowervp);
  202: 	if (othervp) {
  203: 		vp->v_data = NULL;
  204: 		FREE(xp, M_NULLFSNODE);
  205: 		vp->v_type = VBAD;	/* node is discarded */
  206: 		vrele(vp);
  207: 		*vpp = othervp;
  208: 		return 0;
  209: 	}
  210: 
  211: 	/*
  212: 	 * From NetBSD:
  213: 	 * Now lock the new node. We rely on the fact that we were passed
  214: 	 * a locked vnode. If the lower node is exporting a struct lock
  215: 	 * (v_vnlock != NULL) then we just set the upper v_vnlock to the
  216: 	 * lower one, and both are now locked. If the lower node is exporting
  217: 	 * NULL, then we copy that up and manually lock the new vnode.
  218: 	 */
  219: 
  220: 	lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, td);
  221: 	vp->v_vnlock = lowervp->v_vnlock;
  222: 	error = VOP_LOCK(vp, NULL, LK_EXCLUSIVE | LK_THISLAYER, td);
  223: 	if (error)
  224: 		panic("null_node_alloc: can't lock new vnode\n");
  225: 
  226: 	VREF(lowervp);
  227: 	hd = NULL_NHASH(lowervp);
  228: 	LIST_INSERT_HEAD(hd, xp, null_hash);
  229: 	lockmgr(&null_hashlock, LK_RELEASE, NULL, td);
  230: 	return 0;
  231: }
  232: 
  233: 
  234: /*
  235:  * Try to find an existing null_node vnode refering to the given underlying
  236:  * vnode (which should be locked). If no vnode found, create a new null_node
  237:  * vnode which contains a reference to the lower vnode.
  238:  */
  239: int
  240: null_node_create(mp, lowervp, newvpp)
  241: 	struct mount *mp;
  242: 	struct vnode *lowervp;
  243: 	struct vnode **newvpp;
  244: {
  245: 	struct vnode *aliasvp;
  246: 
  247: 	aliasvp = null_node_find(mp, lowervp);
  248: 	if (aliasvp) {
  249: 		/*
  250: 		 * null_node_find has taken another reference
  251: 		 * to the alias vnode.
  252: 		 */
  253: 		vrele(lowervp);
  254: #ifdef NULLFS_DEBUG
  255: 		vprint("null_node_create: exists", aliasvp);
  256: #endif
  257: 	} else {
  258: 		int error;
  259: 
  260: 		/*
  261: 		 * Get new vnode.
  262: 		 */
  263: 		NULLFSDEBUG("null_node_create: create new alias vnode\n");
  264: 
  265: 		/*
  266: 		 * Make new vnode reference the null_node.
  267: 		 */
  268: 		error = null_node_alloc(mp, lowervp, &aliasvp);
  269: 		if (error)
  270: 			return error;
  271: 
  272: 		/*
  273: 		 * aliasvp is already VREF'd by getnewvnode()
  274: 		 */
  275: 	}
  276: 
  277: #ifdef DIAGNOSTIC
  278: 	if (lowervp->v_usecount < 1) {
  279: 		/* Should never happen... */
  280: 		vprint ("null_node_create: alias ", aliasvp);
  281: 		vprint ("null_node_create: lower ", lowervp);
  282: 		panic ("null_node_create: lower has 0 usecount.");
  283: 	};
  284: #endif
  285: 
  286: #ifdef NULLFS_DEBUG
  287: 	vprint("null_node_create: alias", aliasvp);
  288: 	vprint("null_node_create: lower", lowervp);
  289: #endif
  290: 
  291: 	*newvpp = aliasvp;
  292: 	return (0);
  293: }
  294: 
  295: #ifdef DIAGNOSTIC
  296: #include "opt_ddb.h"
  297: 
  298: #ifdef DDB
  299: #define	null_checkvp_barrier	1
  300: #else
  301: #define	null_checkvp_barrier	0
  302: #endif
  303: 
  304: struct vnode *
  305: null_checkvp(vp, fil, lno)
  306: 	struct vnode *vp;
  307: 	char *fil;
  308: 	int lno;
  309: {
  310: 	struct null_node *a = VTONULL(vp);
  311: #ifdef notyet
  312: 	/*
  313: 	 * Can't do this check because vop_reclaim runs
  314: 	 * with a funny vop vector.
  315: 	 */
  316: 	if (vp->v_op != null_vnodeop_p) {
  317: 		printf ("null_checkvp: on non-null-node\n");
  318: 		while (null_checkvp_barrier) /*WAIT*/ ;
  319: 		panic("null_checkvp");
  320: 	};
  321: #endif
  322: 	if (a->null_lowervp == NULLVP) {
  323: 		/* Should never happen */
  324: 		int i; u_long *p;
  325: 		printf("vp = %p, ZERO ptr\n", (void *)vp);
  326: 		for (p = (u_long *) a, i = 0; i < 8; i++)
  327: 			printf(" %lx", p[i]);
  328: 		printf("\n");
  329: 		/* wait for debugger */
  330: 		while (null_checkvp_barrier) /*WAIT*/ ;
  331: 		panic("null_checkvp");
  332: 	}
  333: 	if (a->null_lowervp->v_usecount < 1) {
  334: 		int i; u_long *p;
  335: 		printf("vp = %p, unref'ed lowervp\n", (void *)vp);
  336: 		for (p = (u_long *) a, i = 0; i < 8; i++)
  337: 			printf(" %lx", p[i]);
  338: 		printf("\n");
  339: 		/* wait for debugger */
  340: 		while (null_checkvp_barrier) /*WAIT*/ ;
  341: 		panic ("null with unref'ed lowervp");
  342: 	};
  343: #ifdef notyet
  344: 	printf("null %x/%d -> %x/%d [%s, %d]\n",
  345: 	        NULLTOV(a), NULLTOV(a)->v_usecount,
  346: 		a->null_lowervp, a->null_lowervp->v_usecount,
  347: 		fil, lno);
  348: #endif
  349: 	return a->null_lowervp;
  350: }
  351: #endif