Home | History | Annotate | Line # | Download | only in nfs
nfs_node.c revision 1.41.2.5
      1 /*	$NetBSD: nfs_node.c,v 1.41.2.5 2001/11/14 19:18:43 nathanw 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: nfs_node.c,v 1.41.2.5 2001/11/14 19:18:43 nathanw Exp $");
     43 
     44 #include "opt_nfs.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/proc.h>
     49 #include <sys/lwp.h>
     50 #include <sys/mount.h>
     51 #include <sys/namei.h>
     52 #include <sys/vnode.h>
     53 #include <sys/kernel.h>
     54 #include <sys/malloc.h>
     55 #include <sys/pool.h>
     56 #include <sys/lock.h>
     57 
     58 #include <nfs/rpcv2.h>
     59 #include <nfs/nfsproto.h>
     60 #include <nfs/nfs.h>
     61 #include <nfs/nfsnode.h>
     62 #include <nfs/nfsmount.h>
     63 #include <nfs/nqnfs.h>
     64 #include <nfs/nfs_var.h>
     65 
     66 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
     67 u_long nfsnodehash;
     68 struct lock nfs_hashlock;
     69 
     70 struct pool nfs_node_pool;		/* memory pool for nfs nodes */
     71 struct pool nfs_vattr_pool;		/* memory pool for nfs vattrs */
     72 
     73 extern int prtactive;
     74 
     75 #define TRUE	1
     76 #define	FALSE	0
     77 
     78 void nfs_gop_size(struct vnode *, off_t, off_t *);
     79 int nfs_gop_alloc(struct vnode *, off_t, off_t, int, struct ucred *);
     80 
     81 struct genfs_ops nfs_genfsops = {
     82 	nfs_gop_size,
     83 	nfs_gop_alloc,
     84 	nfs_gop_write,
     85 };
     86 
     87 /*
     88  * Initialize hash links for nfsnodes
     89  * and build nfsnode free list.
     90  */
     91 void
     92 nfs_nhinit()
     93 {
     94 
     95 	nfsnodehashtbl = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE,
     96 	    M_WAITOK, &nfsnodehash);
     97 	lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
     98 
     99 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
    100 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
    101 	pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
    102 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
    103 }
    104 
    105 /*
    106  * Reinitialize inode hash table.
    107  */
    108 
    109 void
    110 nfs_nhreinit()
    111 {
    112 	struct nfsnode *np;
    113 	struct nfsnodehashhead *oldhash, *hash;
    114 	u_long oldmask, mask, val;
    115 	int i;
    116 
    117 	hash = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE, M_WAITOK,
    118 	    &mask);
    119 
    120 	lockmgr(&nfs_hashlock, LK_EXCLUSIVE, NULL);
    121 	oldhash = nfsnodehashtbl;
    122 	oldmask = nfsnodehash;
    123 	nfsnodehashtbl = hash;
    124 	nfsnodehash = mask;
    125 	for (i = 0; i <= oldmask; i++) {
    126 		while ((np = LIST_FIRST(&oldhash[i])) != NULL) {
    127 			LIST_REMOVE(np, n_hash);
    128 			val = NFSNOHASH(nfs_hash(np->n_fhp, np->n_fhsize));
    129 			LIST_INSERT_HEAD(&hash[val], np, n_hash);
    130 		}
    131 	}
    132 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
    133 	hashdone(oldhash, M_NFSNODE);
    134 }
    135 
    136 /*
    137  * Free resources previoslu allocated in nfs_nhinit().
    138  */
    139 void
    140 nfs_nhdone()
    141 {
    142 	hashdone(nfsnodehashtbl, M_NFSNODE);
    143 	pool_destroy(&nfs_node_pool);
    144 	pool_destroy(&nfs_vattr_pool);
    145 }
    146 
    147 /*
    148  * Compute an entry in the NFS hash table structure
    149  */
    150 u_long
    151 nfs_hash(fhp, fhsize)
    152 	nfsfh_t *fhp;
    153 	int fhsize;
    154 {
    155 	u_char *fhpp;
    156 	u_long fhsum;
    157 	int i;
    158 
    159 	fhpp = &fhp->fh_bytes[0];
    160 	fhsum = 0;
    161 	for (i = 0; i < fhsize; i++)
    162 		fhsum += *fhpp++;
    163 	return (fhsum);
    164 }
    165 
    166 /*
    167  * Look up a vnode/nfsnode by file handle.
    168  * Callers must check for mount points!!
    169  * In all cases, a pointer to a
    170  * nfsnode structure is returned.
    171  */
    172 int
    173 nfs_nget(mntp, fhp, fhsize, npp)
    174 	struct mount *mntp;
    175 	nfsfh_t *fhp;
    176 	int fhsize;
    177 	struct nfsnode **npp;
    178 {
    179 	struct nfsnode *np;
    180 	struct nfsnodehashhead *nhpp;
    181 	struct vnode *vp;
    182 	struct vnode *nvp;
    183 	int error;
    184 
    185 	nhpp = &nfsnodehashtbl[NFSNOHASH(nfs_hash(fhp, fhsize))];
    186 loop:
    187 	LIST_FOREACH(np, nhpp, n_hash) {
    188 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
    189 		    memcmp(fhp, np->n_fhp, fhsize))
    190 			continue;
    191 		vp = NFSTOV(np);
    192 		if (vget(vp, LK_EXCLUSIVE))
    193 			goto loop;
    194 		*npp = np;
    195 		return(0);
    196 	}
    197 	if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
    198 		goto loop;
    199 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
    200 	if (error) {
    201 		*npp = 0;
    202 		lockmgr(&nfs_hashlock, LK_RELEASE, 0);
    203 		return (error);
    204 	}
    205 	vp = nvp;
    206 	np = pool_get(&nfs_node_pool, PR_WAITOK);
    207 	memset(np, 0, sizeof *np);
    208 	lockinit(&np->n_commitlock, PINOD, "nfsclock", 0, 0);
    209 	vp->v_data = np;
    210 	np->n_vnode = vp;
    211 	genfs_node_init(vp, &nfs_genfsops);
    212 
    213 	/*
    214 	 * Insert the nfsnode in the hash queue for its new file handle
    215 	 */
    216 
    217 	LIST_INSERT_HEAD(nhpp, np, n_hash);
    218 	if (fhsize > NFS_SMALLFH) {
    219 		np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
    220 	} else
    221 		np->n_fhp = &np->n_fh;
    222 	memcpy(np->n_fhp, fhp, fhsize);
    223 	np->n_fhsize = fhsize;
    224 	np->n_accstamp = -1;
    225 	np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
    226 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
    227 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
    228 	error = VOP_GETATTR(vp, np->n_vattr, curproc->l_proc->p_ucred,
    229 	    curproc->l_proc);
    230 	if (error) {
    231 		vgone(vp);
    232 		return error;
    233 	}
    234 	uvm_vnp_setsize(vp, np->n_vattr->va_size);
    235 	*npp = np;
    236 	return (0);
    237 }
    238 
    239 int
    240 nfs_inactive(v)
    241 	void *v;
    242 {
    243 	struct vop_inactive_args /* {
    244 		struct vnode *a_vp;
    245 		struct proc *a_p;
    246 	} */ *ap = v;
    247 	struct nfsnode *np;
    248 	struct sillyrename *sp;
    249 	struct proc *p = ap->a_p;
    250 	struct vnode *vp = ap->a_vp;
    251 
    252 	np = VTONFS(vp);
    253 	if (prtactive && vp->v_usecount != 0)
    254 		vprint("nfs_inactive: pushing active", 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, p, 1);
    262 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
    263 		NQNFSNONCACHE | NQNFSWRITE);
    264 	VOP_UNLOCK(vp, 0);
    265 	if (sp != NULL) {
    266 
    267 		/*
    268 		 * Remove the silly file that was rename'd earlier
    269 		 */
    270 
    271 		vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_RETRY);
    272 		nfs_removeit(sp);
    273 		crfree(sp->s_cred);
    274 		vput(sp->s_dvp);
    275 		FREE(sp, M_NFSREQ);
    276 	}
    277 	return (0);
    278 }
    279 
    280 /*
    281  * Reclaim an nfsnode so that it can be used for other purposes.
    282  */
    283 int
    284 nfs_reclaim(v)
    285 	void *v;
    286 {
    287 	struct vop_reclaim_args /* {
    288 		struct vnode *a_vp;
    289 	} */ *ap = v;
    290 	struct vnode *vp = ap->a_vp;
    291 	struct nfsnode *np = VTONFS(vp);
    292 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    293 
    294 	if (prtactive && vp->v_usecount != 0)
    295 		vprint("nfs_reclaim: pushing active", vp);
    296 
    297 	LIST_REMOVE(np, n_hash);
    298 
    299 	/*
    300 	 * For nqnfs, take it off the timer queue as required.
    301 	 */
    302 	if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
    303 		CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
    304 	}
    305 
    306 	/*
    307 	 * Free up any directory cookie structures and
    308 	 * large file handle structures that might be associated with
    309 	 * this nfs node.
    310 	 */
    311 	if (vp->v_type == VDIR && np->n_dircache) {
    312 		nfs_invaldircache(vp, 1);
    313 		FREE(np->n_dircache, M_NFSDIROFF);
    314 	}
    315 	if (np->n_fhsize > NFS_SMALLFH) {
    316 		free(np->n_fhp, M_NFSBIGFH);
    317 	}
    318 
    319 	pool_put(&nfs_vattr_pool, np->n_vattr);
    320 	if (np->n_rcred) {
    321 		crfree(np->n_rcred);
    322 	}
    323 	if (np->n_wcred) {
    324 		crfree(np->n_wcred);
    325 	}
    326 	cache_purge(vp);
    327 	pool_put(&nfs_node_pool, vp->v_data);
    328 	vp->v_data = NULL;
    329 	return (0);
    330 }
    331 
    332 void
    333 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp)
    334 {
    335 	*eobp = MAX(size, vp->v_size);
    336 }
    337 
    338 int
    339 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    340     struct ucred *cred)
    341 {
    342 	return 0;
    343 }
    344