Home | History | Annotate | Line # | Download | only in nfs
nfs_node.c revision 1.105
      1 /*	$NetBSD: nfs_node.c,v 1.105 2008/10/22 11:36:06 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      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  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: nfs_node.c,v 1.105 2008/10/22 11:36:06 matt Exp $");
     39 
     40 #include "opt_nfs.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/proc.h>
     45 #include <sys/mount.h>
     46 #include <sys/namei.h>
     47 #include <sys/vnode.h>
     48 #include <sys/kernel.h>
     49 #include <sys/pool.h>
     50 #include <sys/lock.h>
     51 #include <sys/hash.h>
     52 #include <sys/kauth.h>
     53 
     54 #include <nfs/rpcv2.h>
     55 #include <nfs/nfsproto.h>
     56 #include <nfs/nfs.h>
     57 #include <nfs/nfsnode.h>
     58 #include <nfs/nfsmount.h>
     59 #include <nfs/nfs_var.h>
     60 
     61 struct pool nfs_node_pool;
     62 struct pool nfs_vattr_pool;
     63 
     64 MALLOC_JUSTDEFINE(M_NFSNODE, "NFS node", "NFS vnode private part");
     65 
     66 extern int prtactive;
     67 
     68 void nfs_gop_size(struct vnode *, off_t, off_t *, int);
     69 int nfs_gop_alloc(struct vnode *, off_t, off_t, int, kauth_cred_t);
     70 int nfs_gop_write(struct vnode *, struct vm_page **, int, int);
     71 
     72 static const struct genfs_ops nfs_genfsops = {
     73 	.gop_size = nfs_gop_size,
     74 	.gop_alloc = nfs_gop_alloc,
     75 	.gop_write = nfs_gop_write,
     76 };
     77 
     78 /*
     79  * Reinitialize inode hash table.
     80  */
     81 void
     82 nfs_node_reinit()
     83 {
     84 	malloc_type_attach(M_NFSNODE);
     85 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
     86 	    &pool_allocator_nointr, IPL_NONE);
     87 	pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
     88 	    &pool_allocator_nointr, IPL_NONE);
     89 }
     90 
     91 /*
     92  * Free resources previously allocated in nfs_node_reinit().
     93  */
     94 void
     95 nfs_node_done()
     96 {
     97 	pool_destroy(&nfs_node_pool);
     98 	pool_destroy(&nfs_vattr_pool);
     99 	malloc_type_detach(M_NFSNODE);
    100 }
    101 
    102 #define	RBTONFSNODE(node) \
    103 	(void *)((uintptr_t)(node) - offsetof(struct nfsnode, n_rbnode))
    104 
    105 struct fh_match {
    106 	nfsfh_t *fhm_fhp;
    107 	size_t fhm_fhsize;
    108 	size_t fhm_fhoffset;
    109 };
    110 
    111 static int
    112 nfs_compare_nodes(const struct rb_node *parent, const struct rb_node *node)
    113 {
    114 	const struct nfsnode * const pnp = RBTONFSNODE(parent);
    115 	const struct nfsnode * const np = RBTONFSNODE(node);
    116 
    117 	if (pnp->n_fhsize != np->n_fhsize)
    118 		return np->n_fhsize - pnp->n_fhsize;
    119 
    120 	return memcmp(np->n_fhp, pnp->n_fhp, np->n_fhsize);
    121 }
    122 
    123 static int
    124 nfs_compare_node_fh(const struct rb_node *b, const void *key)
    125 {
    126 	const struct nfsnode * const pnp = RBTONFSNODE(b);
    127 	const struct fh_match * const fhm = key;
    128 
    129 	if (pnp->n_fhsize != fhm->fhm_fhsize)
    130 		return fhm->fhm_fhsize - pnp->n_fhsize;
    131 
    132 	return memcmp(fhm->fhm_fhp, pnp->n_fhp, pnp->n_fhsize);
    133 }
    134 
    135 static const struct rb_tree_ops nfs_node_rbtree_ops = {
    136 	.rbto_compare_nodes = nfs_compare_nodes,
    137 	.rbto_compare_key = nfs_compare_node_fh,
    138 };
    139 
    140 void
    141 nfs_rbtinit(struct nfsmount *nmp)
    142 {
    143 	rb_tree_init(&nmp->nm_rbtree, &nfs_node_rbtree_ops);
    144 }
    145 
    146 
    147 /*
    148  * Look up a vnode/nfsnode by file handle.
    149  * Callers must check for mount points!!
    150  * In all cases, a pointer to a
    151  * nfsnode structure is returned.
    152  */
    153 int
    154 nfs_nget1(mntp, fhp, fhsize, npp, lkflags)
    155 	struct mount *mntp;
    156 	nfsfh_t *fhp;
    157 	int fhsize;
    158 	struct nfsnode **npp;
    159 	int lkflags;
    160 {
    161 	struct nfsnode *np;
    162 	struct vnode *vp;
    163 	struct nfsmount *nmp = VFSTONFS(mntp);
    164 	int error;
    165 	struct fh_match fhm;
    166 	struct rb_node *node;
    167 
    168 	fhm.fhm_fhp = fhp;
    169 	fhm.fhm_fhsize = fhsize;
    170 
    171 loop:
    172 	rw_enter(&nmp->nm_rbtlock, RW_READER);
    173 	node = rb_tree_find_node(&nmp->nm_rbtree, &fhm);
    174 	if (node != NULL) {
    175 		np = RBTONFSNODE(node);
    176 		vp = NFSTOV(np);
    177 		mutex_enter(&vp->v_interlock);
    178 		rw_exit(&nmp->nm_rbtlock);
    179 		error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | lkflags);
    180 		if (error == EBUSY)
    181 			return error;
    182 		if (error)
    183 			goto loop;
    184 		*npp = np;
    185 		return(0);
    186 	}
    187 	rw_exit(&nmp->nm_rbtlock);
    188 
    189 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &vp);
    190 	if (error) {
    191 		*npp = 0;
    192 		return (error);
    193 	}
    194 	np = pool_get(&nfs_node_pool, PR_WAITOK);
    195 	memset(np, 0, sizeof *np);
    196 	np->n_vnode = vp;
    197 
    198 	/*
    199 	 * Insert the nfsnode in the hash queue for its new file handle
    200 	 */
    201 
    202 	if (fhsize > NFS_SMALLFH) {
    203 		np->n_fhp = kmem_alloc(fhsize, KM_SLEEP);
    204 	} else
    205 		np->n_fhp = &np->n_fh;
    206 	memcpy(np->n_fhp, fhp, fhsize);
    207 	np->n_fhsize = fhsize;
    208 	np->n_accstamp = -1;
    209 	np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
    210 
    211 	rw_enter(&nmp->nm_rbtlock, RW_WRITER);
    212 	if (NULL != rb_tree_find_node(&nmp->nm_rbtree, &fhm)) {
    213 		rw_exit(&nmp->nm_rbtlock);
    214 		if (fhsize > NFS_SMALLFH) {
    215 			kmem_free(np->n_fhp, fhsize);
    216 		}
    217 		pool_put(&nfs_vattr_pool, np->n_vattr);
    218 		pool_put(&nfs_node_pool, np);
    219 		ungetnewvnode(vp);
    220 		goto loop;
    221 	}
    222 	vp->v_data = np;
    223 	genfs_node_init(vp, &nfs_genfsops);
    224 	/*
    225 	 * Initalize read/write creds to useful values. VOP_OPEN will
    226 	 * overwrite these.
    227 	 */
    228 	np->n_rcred = curlwp->l_cred;
    229 	kauth_cred_hold(np->n_rcred);
    230 	np->n_wcred = curlwp->l_cred;
    231 	kauth_cred_hold(np->n_wcred);
    232 	vlockmgr(&vp->v_lock, LK_EXCLUSIVE);
    233 	NFS_INVALIDATE_ATTRCACHE(np);
    234 	uvm_vnp_setsize(vp, 0);
    235 	rb_tree_insert_node(&nmp->nm_rbtree, &np->n_rbnode);
    236 	rw_exit(&nmp->nm_rbtlock);
    237 
    238 	*npp = np;
    239 	return (0);
    240 }
    241 
    242 int
    243 nfs_inactive(v)
    244 	void *v;
    245 {
    246 	struct vop_inactive_args /* {
    247 		struct vnode *a_vp;
    248 		bool *a_recycle;
    249 	} */ *ap = v;
    250 	struct nfsnode *np;
    251 	struct sillyrename *sp;
    252 	struct vnode *vp = ap->a_vp;
    253 
    254 	np = VTONFS(vp);
    255 	if (vp->v_type != VDIR) {
    256 		sp = np->n_sillyrename;
    257 		np->n_sillyrename = (struct sillyrename *)0;
    258 	} else
    259 		sp = NULL;
    260 	if (sp != NULL)
    261 		nfs_vinvalbuf(vp, 0, sp->s_cred, curlwp, 1);
    262 	*ap->a_recycle = (np->n_flag & NREMOVED) != 0;
    263 	np->n_flag &=
    264 	    (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NEOFVALID | NTRUNCDELAYED);
    265 
    266 	if (vp->v_type == VDIR && np->n_dircache)
    267 		nfs_invaldircache(vp,
    268 		    NFS_INVALDIRCACHE_FORCE | NFS_INVALDIRCACHE_KEEPEOF);
    269 
    270 	VOP_UNLOCK(vp, 0);
    271 
    272 	if (sp != NULL) {
    273 		int error;
    274 
    275 		/*
    276 		 * Remove the silly file that was rename'd earlier
    277 		 *
    278 		 * Just in case our thread also has the parent node locked,
    279 		 * we use LK_CANRECURSE.
    280 		 */
    281 
    282 		error = vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_CANRECURSE);
    283 		if (error || sp->s_dvp->v_data == NULL) {
    284 			/* XXX should recover */
    285 			printf("%s: vp=%p error=%d\n",
    286 			    __func__, sp->s_dvp, error);
    287 		} else {
    288 			nfs_removeit(sp);
    289 		}
    290 		kauth_cred_free(sp->s_cred);
    291 		vput(sp->s_dvp);
    292 		kmem_free(sp, sizeof(*sp));
    293 	}
    294 
    295 	return (0);
    296 }
    297 
    298 /*
    299  * Reclaim an nfsnode so that it can be used for other purposes.
    300  */
    301 int
    302 nfs_reclaim(v)
    303 	void *v;
    304 {
    305 	struct vop_reclaim_args /* {
    306 		struct vnode *a_vp;
    307 	} */ *ap = v;
    308 	struct vnode *vp = ap->a_vp;
    309 	struct nfsnode *np = VTONFS(vp);
    310 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    311 
    312 	if (prtactive && vp->v_usecount > 1)
    313 		vprint("nfs_reclaim: pushing active", vp);
    314 
    315 	rw_enter(&nmp->nm_rbtlock, RW_WRITER);
    316 	rb_tree_remove_node(&nmp->nm_rbtree, &np->n_rbnode);
    317 	rw_exit(&nmp->nm_rbtlock);
    318 
    319 	/*
    320 	 * Free up any directory cookie structures and
    321 	 * large file handle structures that might be associated with
    322 	 * this nfs node.
    323 	 */
    324 	if (vp->v_type == VDIR && np->n_dircache != NULL) {
    325 		nfs_invaldircache(vp, NFS_INVALDIRCACHE_FORCE);
    326 		hashdone(np->n_dircache, HASH_LIST, nfsdirhashmask);
    327 	}
    328 	KASSERT(np->n_dirgens == NULL);
    329 
    330 	if (np->n_fhsize > NFS_SMALLFH)
    331 		kmem_free(np->n_fhp, np->n_fhsize);
    332 
    333 	pool_put(&nfs_vattr_pool, np->n_vattr);
    334 	if (np->n_rcred)
    335 		kauth_cred_free(np->n_rcred);
    336 
    337 	if (np->n_wcred)
    338 		kauth_cred_free(np->n_wcred);
    339 
    340 	cache_purge(vp);
    341 	if (vp->v_type == VREG) {
    342 		mutex_destroy(&np->n_commitlock);
    343 	}
    344 	genfs_node_destroy(vp);
    345 	pool_put(&nfs_node_pool, np);
    346 	vp->v_data = NULL;
    347 	return (0);
    348 }
    349 
    350 void
    351 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
    352 {
    353 
    354 	*eobp = MAX(size, vp->v_size);
    355 }
    356 
    357 int
    358 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    359     kauth_cred_t cred)
    360 {
    361 
    362 	return 0;
    363 }
    364 
    365 int
    366 nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
    367 {
    368 	int i;
    369 
    370 	for (i = 0; i < npages; i++) {
    371 		pmap_page_protect(pgs[i], VM_PROT_READ);
    372 	}
    373 	return genfs_gop_write(vp, pgs, npages, flags);
    374 }
    375