Home | History | Annotate | Line # | Download | only in puffs
puffs_node.c revision 1.11
      1  1.11  pooka /*	$NetBSD: puffs_node.c,v 1.11 2008/01/28 21:06:36 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4   1.1  pooka  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5   1.1  pooka  *
      6   1.1  pooka  * Development of this software was supported by the
      7   1.1  pooka  * Google Summer of Code program, the Ulla Tuominen Foundation
      8   1.1  pooka  * and the Finnish Cultural Foundation.
      9   1.1  pooka  *
     10   1.1  pooka  * Redistribution and use in source and binary forms, with or without
     11   1.1  pooka  * modification, are permitted provided that the following conditions
     12   1.1  pooka  * are met:
     13   1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     14   1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     15   1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  pooka  *    documentation and/or other materials provided with the distribution.
     18   1.1  pooka  *
     19   1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20   1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21   1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22   1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23   1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25   1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1  pooka  * SUCH DAMAGE.
     30   1.1  pooka  */
     31   1.1  pooka 
     32   1.1  pooka #include <sys/cdefs.h>
     33  1.11  pooka __KERNEL_RCSID(0, "$NetBSD: puffs_node.c,v 1.11 2008/01/28 21:06:36 pooka Exp $");
     34   1.1  pooka 
     35   1.1  pooka #include <sys/param.h>
     36   1.1  pooka #include <sys/hash.h>
     37   1.1  pooka #include <sys/kmem.h>
     38   1.1  pooka #include <sys/malloc.h>
     39   1.1  pooka #include <sys/mount.h>
     40   1.1  pooka #include <sys/namei.h>
     41   1.1  pooka #include <sys/vnode.h>
     42   1.1  pooka 
     43   1.1  pooka #include <fs/puffs/puffs_msgif.h>
     44   1.1  pooka #include <fs/puffs/puffs_sys.h>
     45   1.1  pooka 
     46   1.1  pooka #include <miscfs/genfs/genfs_node.h>
     47   1.1  pooka #include <miscfs/specfs/specdev.h>
     48   1.1  pooka 
     49   1.1  pooka static const struct genfs_ops puffs_genfsops = {
     50   1.1  pooka 	.gop_size = puffs_gop_size,
     51   1.1  pooka 	.gop_write = genfs_gop_write,
     52   1.1  pooka 	.gop_markupdate = puffs_gop_markupdate,
     53   1.1  pooka #if 0
     54   1.1  pooka 	.gop_alloc, should ask userspace
     55   1.1  pooka #endif
     56   1.1  pooka };
     57   1.1  pooka 
     58   1.1  pooka static __inline struct puffs_node_hashlist
     59  1.11  pooka 	*puffs_cookie2hashlist(struct puffs_mount *, puffs_cookie_t);
     60  1.11  pooka static struct puffs_node *puffs_cookie2pnode(struct puffs_mount *,
     61  1.11  pooka 					     puffs_cookie_t);
     62   1.1  pooka 
     63   1.1  pooka struct pool puffs_pnpool;
     64   1.1  pooka 
     65   1.1  pooka /*
     66   1.1  pooka  * Grab a vnode, intialize all the puffs-dependant stuff.
     67   1.1  pooka  */
     68   1.1  pooka int
     69  1.11  pooka puffs_getvnode(struct mount *mp, puffs_cookie_t ck, enum vtype type,
     70   1.1  pooka 	voff_t vsize, dev_t rdev, struct vnode **vpp)
     71   1.1  pooka {
     72   1.1  pooka 	struct puffs_mount *pmp;
     73   1.1  pooka 	struct puffs_newcookie *pnc;
     74  1.10     ad 	struct vnode *vp;
     75   1.1  pooka 	struct puffs_node *pnode;
     76   1.1  pooka 	struct puffs_node_hashlist *plist;
     77   1.1  pooka 	int error;
     78   1.1  pooka 
     79   1.1  pooka 	pmp = MPTOPUFFSMP(mp);
     80   1.1  pooka 
     81   1.1  pooka 	error = EPROTO;
     82   1.3  pooka 	if (type <= VNON || type >= VBAD) {
     83   1.7  pooka 		puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EINVAL,
     84  1.11  pooka 		    "bad node type", ck);
     85   1.3  pooka 		goto bad;
     86   1.3  pooka 	}
     87   1.3  pooka 	if (vsize == VSIZENOTSET) {
     88   1.7  pooka 		puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EINVAL,
     89  1.11  pooka 		    "VSIZENOTSET is not a valid size", ck);
     90   1.1  pooka 		goto bad;
     91   1.1  pooka 	}
     92   1.1  pooka 
     93   1.1  pooka 	/*
     94   1.1  pooka 	 * XXX: there is a deadlock condition between vfs_busy() and
     95   1.1  pooka 	 * vnode locks.  For an unmounting file system the mountpoint
     96   1.1  pooka 	 * is frozen, but in unmount(FORCE) vflush() wants to access all
     97   1.1  pooka 	 * of the vnodes.  If we are here waiting for the mountpoint
     98   1.1  pooka 	 * lock while holding on to a vnode lock, well, we ain't
     99   1.1  pooka 	 * just pining for the fjords anymore.  If we release the
    100   1.1  pooka 	 * vnode lock, we will be in the situation "mount point
    101   1.1  pooka 	 * is dying" and panic() will ensue in insmntque.  So as a
    102   1.1  pooka 	 * temporary workaround, get a vnode without putting it on
    103   1.1  pooka 	 * the mount point list, check if mount point is still alive
    104   1.1  pooka 	 * and kicking and only then add the vnode to the list.
    105   1.1  pooka 	 */
    106   1.1  pooka 	error = getnewvnode(VT_PUFFS, NULL, puffs_vnodeop_p, &vp);
    107   1.1  pooka 	if (error)
    108   1.1  pooka 		goto bad;
    109   1.1  pooka 	vp->v_vnlock = NULL;
    110   1.1  pooka 	vp->v_type = type;
    111   1.1  pooka 
    112   1.1  pooka 	/*
    113   1.1  pooka 	 * Check what mount point isn't going away.  This will work
    114   1.1  pooka 	 * until we decide to remove biglock or make the kernel
    115   1.1  pooka 	 * preemptive.  But hopefully the real problem will be fixed
    116   1.1  pooka 	 * by then.
    117   1.1  pooka 	 *
    118   1.1  pooka 	 * XXX: yes, should call vfs_busy(), but thar be rabbits with
    119   1.1  pooka 	 * vicious streaks a mile wide ...
    120   1.1  pooka 	 *
    121   1.1  pooka 	 * XXX: there is a transient failure here: if someone is unmounting
    122   1.1  pooka 	 * the file system but doesn't succeed (due to it being busy),
    123   1.1  pooka 	 * we incorrectly fail new vnode allocation.  This is *very*
    124   1.1  pooka 	 * hard to fix with the current structure of file system unmounting.
    125   1.1  pooka 	 */
    126   1.1  pooka 	if (mp->mnt_iflag & IMNT_UNMOUNT) {
    127   1.1  pooka 		DPRINTF(("puffs_getvnode: mp %p unmount, unable to create "
    128  1.11  pooka 		    "vnode for cookie %p\n", mp, ck));
    129   1.1  pooka 		ungetnewvnode(vp);
    130   1.1  pooka 		error = ENXIO;
    131   1.1  pooka 		goto bad;
    132   1.1  pooka 	}
    133   1.1  pooka 
    134   1.4  pooka 	/*
    135   1.4  pooka 	 * Creation should not fail after this point.  Or if it does,
    136   1.4  pooka 	 * care must be taken so that VOP_INACTIVE() isn't called.
    137   1.4  pooka 	 */
    138   1.4  pooka 
    139   1.4  pooka 	/* So mp is not dead yet.. good.. inform new vnode of its master */
    140   1.9     ad 	mutex_enter(&mntvnode_lock);
    141   1.1  pooka 	TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
    142   1.1  pooka 	vp->v_mount = mp;
    143   1.9     ad 	mutex_exit(&mntvnode_lock);
    144   1.1  pooka 
    145   1.1  pooka 	/*
    146   1.1  pooka 	 * clerical tasks & footwork
    147   1.1  pooka 	 */
    148   1.1  pooka 
    149   1.1  pooka 	/* default size */
    150   1.1  pooka 	uvm_vnp_setsize(vp, 0);
    151   1.1  pooka 
    152   1.1  pooka 	/* dances based on vnode type. almost ufs_vinit(), but not quite */
    153   1.1  pooka 	switch (type) {
    154   1.1  pooka 	case VCHR:
    155   1.1  pooka 	case VBLK:
    156   1.1  pooka 		/*
    157   1.1  pooka 		 * replace vnode operation vector with the specops vector.
    158   1.1  pooka 		 * our user server has very little control over the node
    159   1.1  pooka 		 * if it decides its a character or block special file
    160   1.1  pooka 		 */
    161   1.1  pooka 		vp->v_op = puffs_specop_p;
    162  1.10     ad 		spec_node_init(vp, rdev);
    163   1.1  pooka 		break;
    164   1.1  pooka 
    165   1.1  pooka 	case VFIFO:
    166   1.1  pooka 		vp->v_op = puffs_fifoop_p;
    167   1.1  pooka 		break;
    168   1.1  pooka 
    169   1.1  pooka 	case VREG:
    170   1.1  pooka 		uvm_vnp_setsize(vp, vsize);
    171   1.1  pooka 		break;
    172   1.1  pooka 
    173   1.1  pooka 	case VDIR:
    174   1.1  pooka 	case VLNK:
    175   1.1  pooka 	case VSOCK:
    176   1.1  pooka 		break;
    177   1.1  pooka 	default:
    178   1.1  pooka 		panic("puffs_getvnode: invalid vtype %d", type);
    179   1.1  pooka 	}
    180   1.1  pooka 
    181   1.1  pooka 	pnode = pool_get(&puffs_pnpool, PR_WAITOK);
    182   1.1  pooka 	memset(pnode, 0, sizeof(struct puffs_node));
    183   1.1  pooka 
    184  1.11  pooka 	pnode->pn_cookie = ck;
    185   1.1  pooka 	pnode->pn_refcount = 1;
    186   1.1  pooka 
    187   1.1  pooka 	/* insert cookie on list, take off of interlock list */
    188   1.1  pooka 	mutex_init(&pnode->pn_mtx, MUTEX_DEFAULT, IPL_NONE);
    189   1.1  pooka 	SLIST_INIT(&pnode->pn_sel.sel_klist);
    190  1.11  pooka 	plist = puffs_cookie2hashlist(pmp, ck);
    191   1.1  pooka 	mutex_enter(&pmp->pmp_lock);
    192   1.1  pooka 	LIST_INSERT_HEAD(plist, pnode, pn_hashent);
    193  1.11  pooka 	if (ck != pmp->pmp_root_cookie) {
    194   1.1  pooka 		LIST_FOREACH(pnc, &pmp->pmp_newcookie, pnc_entries) {
    195  1.11  pooka 			if (pnc->pnc_cookie == ck) {
    196   1.1  pooka 				LIST_REMOVE(pnc, pnc_entries);
    197   1.1  pooka 				kmem_free(pnc, sizeof(struct puffs_newcookie));
    198   1.1  pooka 				break;
    199   1.1  pooka 			}
    200   1.1  pooka 		}
    201   1.1  pooka 		KASSERT(pnc != NULL);
    202   1.1  pooka 	}
    203   1.1  pooka 	mutex_exit(&pmp->pmp_lock);
    204   1.1  pooka 
    205   1.1  pooka 	vp->v_data = pnode;
    206   1.1  pooka 	vp->v_type = type;
    207   1.1  pooka 	pnode->pn_vp = vp;
    208   1.1  pooka 	pnode->pn_serversize = vsize;
    209   1.1  pooka 
    210   1.1  pooka 	genfs_node_init(vp, &puffs_genfsops);
    211   1.1  pooka 	*vpp = vp;
    212   1.1  pooka 
    213   1.1  pooka 	DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
    214   1.1  pooka 	    pnode, pnode->pn_cookie));
    215   1.1  pooka 
    216   1.1  pooka 	return 0;
    217   1.1  pooka 
    218   1.1  pooka  bad:
    219   1.1  pooka 	/* remove staging cookie from list */
    220  1.11  pooka 	if (ck != pmp->pmp_root_cookie) {
    221   1.1  pooka 		mutex_enter(&pmp->pmp_lock);
    222   1.1  pooka 		LIST_FOREACH(pnc, &pmp->pmp_newcookie, pnc_entries) {
    223  1.11  pooka 			if (pnc->pnc_cookie == ck) {
    224   1.1  pooka 				LIST_REMOVE(pnc, pnc_entries);
    225   1.1  pooka 				kmem_free(pnc, sizeof(struct puffs_newcookie));
    226   1.1  pooka 				break;
    227   1.1  pooka 			}
    228   1.1  pooka 		}
    229   1.1  pooka 		KASSERT(pnc != NULL);
    230   1.1  pooka 		mutex_exit(&pmp->pmp_lock);
    231   1.1  pooka 	}
    232   1.1  pooka 
    233   1.1  pooka 	return error;
    234   1.1  pooka }
    235   1.1  pooka 
    236   1.1  pooka /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
    237   1.1  pooka int
    238   1.1  pooka puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
    239  1.11  pooka 	puffs_cookie_t ck, struct componentname *cnp,
    240  1.11  pooka 	enum vtype type, dev_t rdev)
    241   1.1  pooka {
    242   1.1  pooka 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    243   1.1  pooka 	struct puffs_newcookie *pnc;
    244   1.1  pooka 	struct vnode *vp;
    245   1.1  pooka 	int error;
    246   1.1  pooka 
    247   1.1  pooka 	/* userspace probably has this as a NULL op */
    248  1.11  pooka 	if (ck == NULL) {
    249   1.1  pooka 		error = EOPNOTSUPP;
    250   1.1  pooka 		return error;
    251   1.1  pooka 	}
    252   1.1  pooka 
    253   1.1  pooka 	/*
    254   1.1  pooka 	 * Check for previous node with the same designation.
    255   1.1  pooka 	 * Explicitly check the root node cookie, since it might be
    256   1.1  pooka 	 * reclaimed from the kernel when this check is made.
    257   1.1  pooka 	 */
    258   1.1  pooka 	mutex_enter(&pmp->pmp_lock);
    259  1.11  pooka 	if (ck == pmp->pmp_root_cookie
    260  1.11  pooka 	    || puffs_cookie2pnode(pmp, ck) != NULL) {
    261   1.1  pooka 		mutex_exit(&pmp->pmp_lock);
    262   1.7  pooka 		puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EEXIST,
    263  1.11  pooka 		    "cookie exists", ck);
    264   1.1  pooka 		return EPROTO;
    265   1.1  pooka 	}
    266   1.1  pooka 
    267   1.1  pooka 	LIST_FOREACH(pnc, &pmp->pmp_newcookie, pnc_entries) {
    268  1.11  pooka 		if (pnc->pnc_cookie == ck) {
    269   1.1  pooka 			mutex_exit(&pmp->pmp_lock);
    270   1.7  pooka 			puffs_senderr(pmp, PUFFS_ERR_MAKENODE, EEXIST,
    271  1.11  pooka 			    "cookie exists", ck);
    272   1.1  pooka 			return EPROTO;
    273   1.1  pooka 		}
    274   1.1  pooka 	}
    275   1.1  pooka 	pnc = kmem_alloc(sizeof(struct puffs_newcookie), KM_SLEEP);
    276  1.11  pooka 	pnc->pnc_cookie = ck;
    277   1.1  pooka 	LIST_INSERT_HEAD(&pmp->pmp_newcookie, pnc, pnc_entries);
    278   1.1  pooka 	mutex_exit(&pmp->pmp_lock);
    279   1.1  pooka 
    280  1.11  pooka 	error = puffs_getvnode(dvp->v_mount, ck, type, 0, rdev, &vp);
    281   1.1  pooka 	if (error)
    282   1.1  pooka 		return error;
    283   1.1  pooka 
    284   1.1  pooka 	vp->v_type = type;
    285   1.1  pooka 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    286   1.1  pooka 	*vpp = vp;
    287   1.1  pooka 
    288   1.1  pooka 	if ((cnp->cn_flags & MAKEENTRY) && PUFFS_USE_NAMECACHE(pmp))
    289   1.1  pooka 		cache_enter(dvp, vp, cnp);
    290   1.1  pooka 
    291   1.1  pooka 	return 0;
    292   1.1  pooka }
    293   1.1  pooka 
    294   1.1  pooka void
    295   1.1  pooka puffs_putvnode(struct vnode *vp)
    296   1.1  pooka {
    297   1.1  pooka 	struct puffs_mount *pmp;
    298   1.1  pooka 	struct puffs_node *pnode;
    299   1.1  pooka 
    300   1.1  pooka 	pmp = VPTOPUFFSMP(vp);
    301   1.1  pooka 	pnode = VPTOPP(vp);
    302   1.1  pooka 
    303   1.1  pooka #ifdef DIAGNOSTIC
    304   1.1  pooka 	if (vp->v_tag != VT_PUFFS)
    305   1.1  pooka 		panic("puffs_putvnode: %p not a puffs vnode", vp);
    306   1.1  pooka #endif
    307   1.1  pooka 
    308   1.1  pooka 	LIST_REMOVE(pnode, pn_hashent);
    309   1.1  pooka 	genfs_node_destroy(vp);
    310   1.1  pooka 	puffs_releasenode(pnode);
    311   1.1  pooka 	vp->v_data = NULL;
    312   1.1  pooka 
    313   1.1  pooka 	return;
    314   1.1  pooka }
    315   1.1  pooka 
    316   1.1  pooka static __inline struct puffs_node_hashlist *
    317  1.11  pooka puffs_cookie2hashlist(struct puffs_mount *pmp, puffs_cookie_t ck)
    318   1.1  pooka {
    319   1.1  pooka 	uint32_t hash;
    320   1.1  pooka 
    321  1.11  pooka 	hash = hash32_buf(&ck, sizeof(void *), HASH32_BUF_INIT);
    322   1.1  pooka 	return &pmp->pmp_pnodehash[hash % pmp->pmp_npnodehash];
    323   1.1  pooka }
    324   1.1  pooka 
    325   1.1  pooka /*
    326   1.2  pooka  * Translate cookie to puffs_node.  Caller must hold pmp_lock
    327   1.2  pooka  * and it will be held upon return.
    328   1.1  pooka  */
    329   1.1  pooka static struct puffs_node *
    330  1.11  pooka puffs_cookie2pnode(struct puffs_mount *pmp, puffs_cookie_t ck)
    331   1.1  pooka {
    332   1.1  pooka 	struct puffs_node_hashlist *plist;
    333   1.1  pooka 	struct puffs_node *pnode;
    334   1.1  pooka 
    335  1.11  pooka 	plist = puffs_cookie2hashlist(pmp, ck);
    336   1.1  pooka 	LIST_FOREACH(pnode, plist, pn_hashent) {
    337  1.11  pooka 		if (pnode->pn_cookie == ck)
    338   1.1  pooka 			break;
    339   1.1  pooka 	}
    340   1.1  pooka 
    341   1.1  pooka 	return pnode;
    342   1.1  pooka }
    343   1.1  pooka 
    344   1.1  pooka /*
    345   1.1  pooka  * Make sure root vnode exists and reference it.  Does NOT lock.
    346   1.1  pooka  */
    347   1.1  pooka static int
    348   1.1  pooka puffs_makeroot(struct puffs_mount *pmp)
    349   1.1  pooka {
    350   1.1  pooka 	struct vnode *vp;
    351   1.1  pooka 	int rv;
    352   1.1  pooka 
    353   1.1  pooka 	/*
    354   1.1  pooka 	 * pmp_lock must be held if vref()'ing or vrele()'ing the
    355   1.1  pooka 	 * root vnode.  the latter is controlled by puffs_inactive().
    356   1.1  pooka 	 *
    357   1.1  pooka 	 * pmp_root is set here and cleared in puffs_reclaim().
    358   1.1  pooka 	 */
    359   1.1  pooka  retry:
    360   1.1  pooka 	mutex_enter(&pmp->pmp_lock);
    361   1.1  pooka 	vp = pmp->pmp_root;
    362   1.1  pooka 	if (vp) {
    363   1.9     ad 		mutex_enter(&vp->v_interlock);
    364   1.1  pooka 		mutex_exit(&pmp->pmp_lock);
    365   1.1  pooka 		if (vget(vp, LK_INTERLOCK) == 0)
    366   1.1  pooka 			return 0;
    367   1.1  pooka 	} else
    368   1.1  pooka 		mutex_exit(&pmp->pmp_lock);
    369   1.1  pooka 
    370   1.1  pooka 	/*
    371   1.1  pooka 	 * So, didn't have the magic root vnode available.
    372   1.1  pooka 	 * No matter, grab another and stuff it with the cookie.
    373   1.1  pooka 	 */
    374   1.1  pooka 	if ((rv = puffs_getvnode(pmp->pmp_mp, pmp->pmp_root_cookie,
    375   1.1  pooka 	    pmp->pmp_root_vtype, pmp->pmp_root_vsize, pmp->pmp_root_rdev, &vp)))
    376   1.1  pooka 		return rv;
    377   1.1  pooka 
    378   1.1  pooka 	/*
    379   1.1  pooka 	 * Someone magically managed to race us into puffs_getvnode?
    380   1.1  pooka 	 * Put our previous new vnode back and retry.
    381   1.1  pooka 	 */
    382   1.1  pooka 	mutex_enter(&pmp->pmp_lock);
    383   1.1  pooka 	if (pmp->pmp_root) {
    384   1.1  pooka 		mutex_exit(&pmp->pmp_lock);
    385   1.1  pooka 		puffs_putvnode(vp);
    386   1.1  pooka 		goto retry;
    387   1.1  pooka 	}
    388   1.1  pooka 
    389   1.1  pooka 	/* store cache */
    390   1.5     ad 	vp->v_vflag |= VV_ROOT;
    391   1.1  pooka 	pmp->pmp_root = vp;
    392   1.1  pooka 	mutex_exit(&pmp->pmp_lock);
    393   1.1  pooka 
    394   1.1  pooka 	return 0;
    395   1.1  pooka }
    396   1.1  pooka 
    397   1.1  pooka /*
    398   1.1  pooka  * Locate the in-kernel vnode based on the cookie received given
    399   1.1  pooka  * from userspace.  Returns a vnode, if found, NULL otherwise.
    400   1.1  pooka  * The parameter "lock" control whether to lock the possible or
    401   1.1  pooka  * not.  Locking always might cause us to lock against ourselves
    402   1.1  pooka  * in situations where we want the vnode but don't care for the
    403   1.1  pooka  * vnode lock, e.g. file server issued putpages.
    404   1.1  pooka  */
    405   1.1  pooka int
    406  1.11  pooka puffs_cookie2vnode(struct puffs_mount *pmp, puffs_cookie_t ck, int lock,
    407   1.1  pooka 	int willcreate, struct vnode **vpp)
    408   1.1  pooka {
    409   1.1  pooka 	struct puffs_node *pnode;
    410   1.1  pooka 	struct puffs_newcookie *pnc;
    411   1.1  pooka 	struct vnode *vp;
    412   1.1  pooka 	int vgetflags, rv;
    413   1.1  pooka 
    414   1.1  pooka 	/*
    415   1.1  pooka 	 * Handle root in a special manner, since we want to make sure
    416   1.1  pooka 	 * pmp_root is properly set.
    417   1.1  pooka 	 */
    418  1.11  pooka 	if (ck == pmp->pmp_root_cookie) {
    419   1.1  pooka 		if ((rv = puffs_makeroot(pmp)))
    420   1.1  pooka 			return rv;
    421   1.1  pooka 		if (lock)
    422   1.1  pooka 			vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
    423   1.1  pooka 
    424   1.1  pooka 		*vpp = pmp->pmp_root;
    425   1.1  pooka 		return 0;
    426   1.1  pooka 	}
    427   1.1  pooka 
    428   1.1  pooka 	mutex_enter(&pmp->pmp_lock);
    429  1.11  pooka 	pnode = puffs_cookie2pnode(pmp, ck);
    430   1.1  pooka 	if (pnode == NULL) {
    431   1.1  pooka 		if (willcreate) {
    432   1.1  pooka 			pnc = kmem_alloc(sizeof(struct puffs_newcookie),
    433   1.1  pooka 			    KM_SLEEP);
    434  1.11  pooka 			pnc->pnc_cookie = ck;
    435   1.1  pooka 			LIST_INSERT_HEAD(&pmp->pmp_newcookie, pnc, pnc_entries);
    436   1.1  pooka 		}
    437   1.1  pooka 		mutex_exit(&pmp->pmp_lock);
    438   1.1  pooka 		return PUFFS_NOSUCHCOOKIE;
    439   1.1  pooka 	}
    440   1.1  pooka 	vp = pnode->pn_vp;
    441   1.9     ad 	mutex_enter(&vp->v_interlock);
    442   1.1  pooka 	mutex_exit(&pmp->pmp_lock);
    443   1.1  pooka 
    444   1.1  pooka 	vgetflags = LK_INTERLOCK;
    445   1.1  pooka 	if (lock)
    446   1.1  pooka 		vgetflags |= LK_EXCLUSIVE | LK_RETRY;
    447   1.1  pooka 	if ((rv = vget(vp, vgetflags)))
    448   1.1  pooka 		return rv;
    449   1.1  pooka 
    450   1.1  pooka 	*vpp = vp;
    451   1.1  pooka 	return 0;
    452   1.1  pooka }
    453   1.1  pooka 
    454   1.1  pooka void
    455   1.8  pooka puffs_updatenode(struct puffs_node *pn, int flags, voff_t size)
    456   1.1  pooka {
    457   1.1  pooka 	struct timespec ts;
    458   1.1  pooka 
    459   1.1  pooka 	if (flags == 0)
    460   1.1  pooka 		return;
    461   1.1  pooka 
    462   1.1  pooka 	nanotime(&ts);
    463   1.1  pooka 
    464   1.1  pooka 	if (flags & PUFFS_UPDATEATIME) {
    465   1.1  pooka 		pn->pn_mc_atime = ts;
    466   1.1  pooka 		pn->pn_stat |= PNODE_METACACHE_ATIME;
    467   1.1  pooka 	}
    468   1.1  pooka 	if (flags & PUFFS_UPDATECTIME) {
    469   1.1  pooka 		pn->pn_mc_ctime = ts;
    470   1.1  pooka 		pn->pn_stat |= PNODE_METACACHE_CTIME;
    471   1.1  pooka 	}
    472   1.1  pooka 	if (flags & PUFFS_UPDATEMTIME) {
    473   1.1  pooka 		pn->pn_mc_mtime = ts;
    474   1.1  pooka 		pn->pn_stat |= PNODE_METACACHE_MTIME;
    475   1.1  pooka 	}
    476   1.1  pooka 	if (flags & PUFFS_UPDATESIZE) {
    477   1.8  pooka 		pn->pn_mc_size = size;
    478   1.1  pooka 		pn->pn_stat |= PNODE_METACACHE_SIZE;
    479   1.1  pooka 	}
    480   1.1  pooka }
    481   1.1  pooka 
    482   1.1  pooka /*
    483   1.1  pooka  * Add reference to node.
    484   1.1  pooka  *  mutex held on entry and return
    485   1.1  pooka  */
    486   1.1  pooka void
    487   1.1  pooka puffs_referencenode(struct puffs_node *pn)
    488   1.1  pooka {
    489   1.1  pooka 
    490   1.1  pooka 	KASSERT(mutex_owned(&pn->pn_mtx));
    491   1.1  pooka 	pn->pn_refcount++;
    492   1.1  pooka }
    493   1.1  pooka 
    494   1.1  pooka /*
    495   1.1  pooka  * Release pnode structure which dealing with references to the
    496   1.1  pooka  * puffs_node instead of the vnode.  Can't use vref()/vrele() on
    497   1.1  pooka  * the vnode there, since that causes the lovely VOP_INACTIVE(),
    498   1.1  pooka  * which in turn causes the lovely deadlock when called by the one
    499   1.1  pooka  * who is supposed to handle it.
    500   1.1  pooka  */
    501   1.1  pooka void
    502   1.1  pooka puffs_releasenode(struct puffs_node *pn)
    503   1.1  pooka {
    504   1.1  pooka 
    505   1.1  pooka 	mutex_enter(&pn->pn_mtx);
    506   1.1  pooka 	if (--pn->pn_refcount == 0) {
    507   1.1  pooka 		mutex_exit(&pn->pn_mtx);
    508   1.1  pooka 		mutex_destroy(&pn->pn_mtx);
    509   1.1  pooka 		pool_put(&puffs_pnpool, pn);
    510   1.1  pooka 	} else {
    511   1.1  pooka 		mutex_exit(&pn->pn_mtx);
    512   1.1  pooka 	}
    513   1.1  pooka }
    514