Home | History | Annotate | Line # | Download | only in nfs
nfs_node.c revision 1.47
      1 /*	$NetBSD: nfs_node.c,v 1.47 2001/11/10 10:59:09 lukem 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.47 2001/11/10 10:59:09 lukem 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/mount.h>
     50 #include <sys/namei.h>
     51 #include <sys/vnode.h>
     52 #include <sys/kernel.h>
     53 #include <sys/malloc.h>
     54 #include <sys/pool.h>
     55 #include <sys/lock.h>
     56 
     57 #include <nfs/rpcv2.h>
     58 #include <nfs/nfsproto.h>
     59 #include <nfs/nfs.h>
     60 #include <nfs/nfsnode.h>
     61 #include <nfs/nfsmount.h>
     62 #include <nfs/nqnfs.h>
     63 #include <nfs/nfs_var.h>
     64 
     65 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
     66 u_long nfsnodehash;
     67 struct lock nfs_hashlock;
     68 
     69 struct pool nfs_node_pool;		/* memory pool for nfs nodes */
     70 struct pool nfs_vattr_pool;		/* memory pool for nfs vattrs */
     71 
     72 extern int prtactive;
     73 
     74 #define TRUE	1
     75 #define	FALSE	0
     76 
     77 void nfs_gop_size(struct vnode *, off_t, off_t *);
     78 int nfs_gop_alloc(struct vnode *, off_t, off_t, int, struct ucred *);
     79 
     80 struct genfs_ops nfs_genfsops = {
     81 	nfs_gop_size,
     82 	nfs_gop_alloc,
     83 	nfs_gop_write,
     84 };
     85 
     86 /*
     87  * Initialize hash links for nfsnodes
     88  * and build nfsnode free list.
     89  */
     90 void
     91 nfs_nhinit()
     92 {
     93 
     94 	nfsnodehashtbl = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE,
     95 	    M_WAITOK, &nfsnodehash);
     96 	lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
     97 
     98 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
     99 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
    100 	pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
    101 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
    102 }
    103 
    104 /*
    105  * Reinitialize inode hash table.
    106  */
    107 
    108 void
    109 nfs_nhreinit()
    110 {
    111 	struct nfsnode *np;
    112 	struct nfsnodehashhead *oldhash, *hash;
    113 	u_long oldmask, mask, val;
    114 	int i;
    115 
    116 	hash = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE, M_WAITOK,
    117 	    &mask);
    118 
    119 	lockmgr(&nfs_hashlock, LK_EXCLUSIVE, NULL);
    120 	oldhash = nfsnodehashtbl;
    121 	oldmask = nfsnodehash;
    122 	nfsnodehashtbl = hash;
    123 	nfsnodehash = mask;
    124 	for (i = 0; i <= oldmask; i++) {
    125 		while ((np = LIST_FIRST(&oldhash[i])) != NULL) {
    126 			LIST_REMOVE(np, n_hash);
    127 			val = NFSNOHASH(nfs_hash(np->n_fhp, np->n_fhsize));
    128 			LIST_INSERT_HEAD(&hash[val], np, n_hash);
    129 		}
    130 	}
    131 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
    132 	hashdone(oldhash, M_NFSNODE);
    133 }
    134 
    135 /*
    136  * Free resources previoslu allocated in nfs_nhinit().
    137  */
    138 void
    139 nfs_nhdone()
    140 {
    141 	hashdone(nfsnodehashtbl, M_NFSNODE);
    142 	pool_destroy(&nfs_node_pool);
    143 	pool_destroy(&nfs_vattr_pool);
    144 }
    145 
    146 /*
    147  * Compute an entry in the NFS hash table structure
    148  */
    149 u_long
    150 nfs_hash(fhp, fhsize)
    151 	nfsfh_t *fhp;
    152 	int fhsize;
    153 {
    154 	u_char *fhpp;
    155 	u_long fhsum;
    156 	int i;
    157 
    158 	fhpp = &fhp->fh_bytes[0];
    159 	fhsum = 0;
    160 	for (i = 0; i < fhsize; i++)
    161 		fhsum += *fhpp++;
    162 	return (fhsum);
    163 }
    164 
    165 /*
    166  * Look up a vnode/nfsnode by file handle.
    167  * Callers must check for mount points!!
    168  * In all cases, a pointer to a
    169  * nfsnode structure is returned.
    170  */
    171 int
    172 nfs_nget(mntp, fhp, fhsize, npp)
    173 	struct mount *mntp;
    174 	nfsfh_t *fhp;
    175 	int fhsize;
    176 	struct nfsnode **npp;
    177 {
    178 	struct nfsnode *np;
    179 	struct nfsnodehashhead *nhpp;
    180 	struct vnode *vp;
    181 	struct vnode *nvp;
    182 	int error;
    183 
    184 	nhpp = &nfsnodehashtbl[NFSNOHASH(nfs_hash(fhp, fhsize))];
    185 loop:
    186 	LIST_FOREACH(np, nhpp, n_hash) {
    187 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
    188 		    memcmp(fhp, np->n_fhp, fhsize))
    189 			continue;
    190 		vp = NFSTOV(np);
    191 		if (vget(vp, LK_EXCLUSIVE))
    192 			goto loop;
    193 		*npp = np;
    194 		return(0);
    195 	}
    196 	if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
    197 		goto loop;
    198 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
    199 	if (error) {
    200 		*npp = 0;
    201 		lockmgr(&nfs_hashlock, LK_RELEASE, 0);
    202 		return (error);
    203 	}
    204 	vp = nvp;
    205 	np = pool_get(&nfs_node_pool, PR_WAITOK);
    206 	memset(np, 0, sizeof *np);
    207 	lockinit(&np->n_commitlock, PINOD, "nfsclock", 0, 0);
    208 	vp->v_data = np;
    209 	np->n_vnode = vp;
    210 	genfs_node_init(vp, &nfs_genfsops);
    211 
    212 	/*
    213 	 * Insert the nfsnode in the hash queue for its new file handle
    214 	 */
    215 
    216 	LIST_INSERT_HEAD(nhpp, np, n_hash);
    217 	if (fhsize > NFS_SMALLFH) {
    218 		np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
    219 	} else
    220 		np->n_fhp = &np->n_fh;
    221 	memcpy(np->n_fhp, fhp, fhsize);
    222 	np->n_fhsize = fhsize;
    223 	np->n_accstamp = -1;
    224 	np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
    225 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
    226 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
    227 	error = VOP_GETATTR(vp, np->n_vattr, curproc->p_ucred, curproc);
    228 	if (error) {
    229 		vgone(vp);
    230 		return error;
    231 	}
    232 	uvm_vnp_setsize(vp, np->n_vattr->va_size);
    233 	*npp = np;
    234 	return (0);
    235 }
    236 
    237 int
    238 nfs_inactive(v)
    239 	void *v;
    240 {
    241 	struct vop_inactive_args /* {
    242 		struct vnode *a_vp;
    243 		struct proc *a_p;
    244 	} */ *ap = v;
    245 	struct nfsnode *np;
    246 	struct sillyrename *sp;
    247 	struct proc *p = ap->a_p;
    248 	struct vnode *vp = ap->a_vp;
    249 
    250 	np = VTONFS(vp);
    251 	if (prtactive && vp->v_usecount != 0)
    252 		vprint("nfs_inactive: pushing active", vp);
    253 	if (vp->v_type != VDIR) {
    254 		sp = np->n_sillyrename;
    255 		np->n_sillyrename = (struct sillyrename *)0;
    256 	} else
    257 		sp = NULL;
    258 	if (sp != NULL)
    259 		nfs_vinvalbuf(vp, 0, sp->s_cred, p, 1);
    260 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
    261 		NQNFSNONCACHE | NQNFSWRITE);
    262 	VOP_UNLOCK(vp, 0);
    263 	if (sp != NULL) {
    264 
    265 		/*
    266 		 * Remove the silly file that was rename'd earlier
    267 		 */
    268 
    269 		vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_RETRY);
    270 		nfs_removeit(sp);
    271 		crfree(sp->s_cred);
    272 		vput(sp->s_dvp);
    273 		FREE(sp, M_NFSREQ);
    274 	}
    275 	return (0);
    276 }
    277 
    278 /*
    279  * Reclaim an nfsnode so that it can be used for other purposes.
    280  */
    281 int
    282 nfs_reclaim(v)
    283 	void *v;
    284 {
    285 	struct vop_reclaim_args /* {
    286 		struct vnode *a_vp;
    287 	} */ *ap = v;
    288 	struct vnode *vp = ap->a_vp;
    289 	struct nfsnode *np = VTONFS(vp);
    290 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    291 
    292 	if (prtactive && vp->v_usecount != 0)
    293 		vprint("nfs_reclaim: pushing active", vp);
    294 
    295 	LIST_REMOVE(np, n_hash);
    296 
    297 	/*
    298 	 * For nqnfs, take it off the timer queue as required.
    299 	 */
    300 	if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
    301 		CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
    302 	}
    303 
    304 	/*
    305 	 * Free up any directory cookie structures and
    306 	 * large file handle structures that might be associated with
    307 	 * this nfs node.
    308 	 */
    309 	if (vp->v_type == VDIR && np->n_dircache) {
    310 		nfs_invaldircache(vp, 1);
    311 		FREE(np->n_dircache, M_NFSDIROFF);
    312 	}
    313 	if (np->n_fhsize > NFS_SMALLFH) {
    314 		free(np->n_fhp, M_NFSBIGFH);
    315 	}
    316 
    317 	pool_put(&nfs_vattr_pool, np->n_vattr);
    318 	if (np->n_rcred) {
    319 		crfree(np->n_rcred);
    320 	}
    321 	if (np->n_wcred) {
    322 		crfree(np->n_wcred);
    323 	}
    324 	cache_purge(vp);
    325 	pool_put(&nfs_node_pool, vp->v_data);
    326 	vp->v_data = NULL;
    327 	return (0);
    328 }
    329 
    330 void
    331 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp)
    332 {
    333 	*eobp = MAX(size, vp->v_size);
    334 }
    335 
    336 int
    337 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    338     struct ucred *cred)
    339 {
    340 	return 0;
    341 }
    342