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