Home | History | Annotate | Line # | Download | only in nfs
nfs_node.c revision 1.1.1.3
      1 /*
      2  * Copyright (c) 1989, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Rick Macklem at The University of Guelph.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
     37  */
     38 
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/proc.h>
     43 #include <sys/mount.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/kernel.h>
     47 #include <sys/malloc.h>
     48 
     49 #include <nfs/rpcv2.h>
     50 #include <nfs/nfsproto.h>
     51 #include <nfs/nfs.h>
     52 #include <nfs/nfsnode.h>
     53 #include <nfs/nfsmount.h>
     54 #include <nfs/nqnfs.h>
     55 
     56 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
     57 u_long nfsnodehash;
     58 
     59 #define TRUE	1
     60 #define	FALSE	0
     61 
     62 /*
     63  * Initialize hash links for nfsnodes
     64  * and build nfsnode free list.
     65  */
     66 void
     67 nfs_nhinit()
     68 {
     69 
     70 #ifndef lint
     71 	if ((sizeof(struct nfsnode) - 1) & sizeof(struct nfsnode))
     72 		printf("nfs_nhinit: bad size %d\n", sizeof(struct nfsnode));
     73 #endif /* not lint */
     74 	nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash);
     75 }
     76 
     77 /*
     78  * Compute an entry in the NFS hash table structure
     79  */
     80 u_long
     81 nfs_hash(fhp, fhsize)
     82 	register nfsfh_t *fhp;
     83 	int fhsize;
     84 {
     85 	register u_char *fhpp;
     86 	register u_long fhsum;
     87 	register int i;
     88 
     89 	fhpp = &fhp->fh_bytes[0];
     90 	fhsum = 0;
     91 	for (i = 0; i < fhsize; i++)
     92 		fhsum += *fhpp++;
     93 	return (fhsum);
     94 }
     95 
     96 /*
     97  * Look up a vnode/nfsnode by file handle.
     98  * Callers must check for mount points!!
     99  * In all cases, a pointer to a
    100  * nfsnode structure is returned.
    101  */
    102 int
    103 nfs_nget(mntp, fhp, fhsize, npp)
    104 	struct mount *mntp;
    105 	register nfsfh_t *fhp;
    106 	int fhsize;
    107 	struct nfsnode **npp;
    108 {
    109 	struct proc *p = curproc;	/* XXX */
    110 	struct nfsnode *np;
    111 	struct nfsnodehashhead *nhpp;
    112 	register struct vnode *vp;
    113 	extern int (**nfsv2_vnodeop_p)();
    114 	struct vnode *nvp;
    115 	int error;
    116 
    117 	nhpp = NFSNOHASH(nfs_hash(fhp, fhsize));
    118 loop:
    119 	for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
    120 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
    121 		    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize))
    122 			continue;
    123 		vp = NFSTOV(np);
    124 		if (vget(vp, LK_EXCLUSIVE, p))
    125 			goto loop;
    126 		*npp = np;
    127 		return(0);
    128 	}
    129 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
    130 	if (error) {
    131 		*npp = 0;
    132 		return (error);
    133 	}
    134 	vp = nvp;
    135 	MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
    136 	bzero((caddr_t)np, sizeof *np);
    137 	vp->v_data = np;
    138 	np->n_vnode = vp;
    139 	/*
    140 	 * Insert the nfsnode in the hash queue for its new file handle
    141 	 */
    142 	LIST_INSERT_HEAD(nhpp, np, n_hash);
    143 	if (fhsize > NFS_SMALLFH) {
    144 		MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
    145 	} else
    146 		np->n_fhp = &np->n_fh;
    147 	bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
    148 	np->n_fhsize = fhsize;
    149 	*npp = np;
    150 	return (0);
    151 }
    152 
    153 int
    154 nfs_inactive(ap)
    155 	struct vop_inactive_args /* {
    156 		struct vnode *a_vp;
    157 		struct proc *a_p;
    158 	} */ *ap;
    159 {
    160 	register struct nfsnode *np;
    161 	register struct sillyrename *sp;
    162 	struct proc *p = curproc;	/* XXX */
    163 	extern int prtactive;
    164 
    165 	np = VTONFS(ap->a_vp);
    166 	if (prtactive && ap->a_vp->v_usecount != 0)
    167 		vprint("nfs_inactive: pushing active", ap->a_vp);
    168 	if (ap->a_vp->v_type != VDIR)
    169 		sp = np->n_sillyrename;
    170 	else
    171 		sp = (struct sillyrename *)0;
    172 	np->n_sillyrename = (struct sillyrename *)0;
    173 	if (sp) {
    174 		/*
    175 		 * Remove the silly file that was rename'd earlier
    176 		 */
    177 		(void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
    178 		nfs_removeit(sp);
    179 		crfree(sp->s_cred);
    180 		vrele(sp->s_dvp);
    181 		FREE((caddr_t)sp, M_NFSREQ);
    182 	}
    183 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
    184 		NQNFSNONCACHE | NQNFSWRITE);
    185 	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
    186 	return (0);
    187 }
    188 
    189 /*
    190  * Reclaim an nfsnode so that it can be used for other purposes.
    191  */
    192 int
    193 nfs_reclaim(ap)
    194 	struct vop_reclaim_args /* {
    195 		struct vnode *a_vp;
    196 	} */ *ap;
    197 {
    198 	register struct vnode *vp = ap->a_vp;
    199 	register struct nfsnode *np = VTONFS(vp);
    200 	register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    201 	register struct nfsdmap *dp, *dp2;
    202 	extern int prtactive;
    203 
    204 	if (prtactive && vp->v_usecount != 0)
    205 		vprint("nfs_reclaim: pushing active", vp);
    206 
    207 	LIST_REMOVE(np, n_hash);
    208 
    209 	/*
    210 	 * For nqnfs, take it off the timer queue as required.
    211 	 */
    212 	if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
    213 		CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
    214 	}
    215 
    216 	/*
    217 	 * Free up any directory cookie structures and
    218 	 * large file handle structures that might be associated with
    219 	 * this nfs node.
    220 	 */
    221 	if (vp->v_type == VDIR) {
    222 		dp = np->n_cookies.lh_first;
    223 		while (dp) {
    224 			dp2 = dp;
    225 			dp = dp->ndm_list.le_next;
    226 			FREE((caddr_t)dp2, M_NFSDIROFF);
    227 		}
    228 	}
    229 	if (np->n_fhsize > NFS_SMALLFH) {
    230 		FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
    231 	}
    232 
    233 	cache_purge(vp);
    234 	FREE(vp->v_data, M_NFSNODE);
    235 	vp->v_data = (void *)0;
    236 	return (0);
    237 }
    238 
    239 /*
    240  * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
    241  * done. Currently nothing to do.
    242  */
    243 /* ARGSUSED */
    244 int
    245 nfs_abortop(ap)
    246 	struct vop_abortop_args /* {
    247 		struct vnode *a_dvp;
    248 		struct componentname *a_cnp;
    249 	} */ *ap;
    250 {
    251 
    252 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    253 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
    254 	return (0);
    255 }
    256