Home | History | Annotate | Line # | Download | only in nfs
nfs_node.c revision 1.1.1.2
      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.2 (Berkeley) 12/30/93
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/mount.h>
     43 #include <sys/namei.h>
     44 #include <sys/vnode.h>
     45 #include <sys/kernel.h>
     46 #include <sys/malloc.h>
     47 
     48 #include <nfs/rpcv2.h>
     49 #include <nfs/nfsv2.h>
     50 #include <nfs/nfs.h>
     51 #include <nfs/nfsnode.h>
     52 #include <nfs/nfsmount.h>
     53 #include <nfs/nqnfs.h>
     54 
     55 struct nfsnode **nheadhashtbl;
     56 u_long nheadhash;
     57 #define	NFSNOHASH(fhsum)	((fhsum)&nheadhash)
     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 nfs_nhinit()
     67 {
     68 
     69 #ifndef lint
     70 	if ((sizeof(struct nfsnode) - 1) & sizeof(struct nfsnode))
     71 		printf("nfs_nhinit: bad size %d\n", sizeof(struct nfsnode));
     72 #endif /* not lint */
     73 	nheadhashtbl = hashinit(desiredvnodes, M_NFSNODE, &nheadhash);
     74 }
     75 
     76 /*
     77  * Compute an entry in the NFS hash table structure
     78  */
     79 struct nfsnode **
     80 nfs_hash(fhp)
     81 	register nfsv2fh_t *fhp;
     82 {
     83 	register u_char *fhpp;
     84 	register u_long fhsum;
     85 	int i;
     86 
     87 	fhpp = &fhp->fh_bytes[0];
     88 	fhsum = 0;
     89 	for (i = 0; i < NFSX_FH; i++)
     90 		fhsum += *fhpp++;
     91 	return (&nheadhashtbl[NFSNOHASH(fhsum)]);
     92 }
     93 
     94 /*
     95  * Look up a vnode/nfsnode by file handle.
     96  * Callers must check for mount points!!
     97  * In all cases, a pointer to a
     98  * nfsnode structure is returned.
     99  */
    100 nfs_nget(mntp, fhp, npp)
    101 	struct mount *mntp;
    102 	register nfsv2fh_t *fhp;
    103 	struct nfsnode **npp;
    104 {
    105 	register struct nfsnode *np, *nq, **nhpp;
    106 	register struct vnode *vp;
    107 	extern int (**nfsv2_vnodeop_p)();
    108 	struct vnode *nvp;
    109 	int error;
    110 
    111 	nhpp = nfs_hash(fhp);
    112 loop:
    113 	for (np = *nhpp; np; np = np->n_forw) {
    114 		if (mntp != NFSTOV(np)->v_mount ||
    115 		    bcmp((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH))
    116 			continue;
    117 		vp = NFSTOV(np);
    118 		if (vget(vp, 1))
    119 			goto loop;
    120 		*npp = np;
    121 		return(0);
    122 	}
    123 	if (error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp)) {
    124 		*npp = 0;
    125 		return (error);
    126 	}
    127 	vp = nvp;
    128 	MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
    129 	vp->v_data = np;
    130 	np->n_vnode = vp;
    131 	/*
    132 	 * Insert the nfsnode in the hash queue for its new file handle
    133 	 */
    134 	np->n_flag = 0;
    135 	if (nq = *nhpp)
    136 		nq->n_back = &np->n_forw;
    137 	np->n_forw = nq;
    138 	np->n_back = nhpp;
    139 	*nhpp = np;
    140 	bcopy((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH);
    141 	np->n_attrstamp = 0;
    142 	np->n_direofoffset = 0;
    143 	np->n_sillyrename = (struct sillyrename *)0;
    144 	np->n_size = 0;
    145 	np->n_mtime = 0;
    146 	if (VFSTONFS(mntp)->nm_flag & NFSMNT_NQNFS) {
    147 		np->n_brev = 0;
    148 		np->n_lrev = 0;
    149 		np->n_expiry = (time_t)0;
    150 		np->n_tnext = (struct nfsnode *)0;
    151 	}
    152 	*npp = np;
    153 	return (0);
    154 }
    155 
    156 nfs_inactive(ap)
    157 	struct vop_inactive_args /* {
    158 		struct vnode *a_vp;
    159 	} */ *ap;
    160 {
    161 	register struct nfsnode *np;
    162 	register struct sillyrename *sp;
    163 	struct proc *p = curproc;	/* XXX */
    164 	extern int prtactive;
    165 
    166 	np = VTONFS(ap->a_vp);
    167 	if (prtactive && ap->a_vp->v_usecount != 0)
    168 		vprint("nfs_inactive: pushing active", ap->a_vp);
    169 	sp = np->n_sillyrename;
    170 	np->n_sillyrename = (struct sillyrename *)0;
    171 	if (sp) {
    172 		/*
    173 		 * Remove the silly file that was rename'd earlier
    174 		 */
    175 		(void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
    176 		nfs_removeit(sp);
    177 		crfree(sp->s_cred);
    178 		vrele(sp->s_dvp);
    179 #ifdef SILLYSEPARATE
    180 		free((caddr_t)sp, M_NFSREQ);
    181 #endif
    182 	}
    183 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
    184 		NQNFSNONCACHE | NQNFSWRITE);
    185 	return (0);
    186 }
    187 
    188 /*
    189  * Reclaim an nfsnode so that it can be used for other purposes.
    190  */
    191 nfs_reclaim(ap)
    192 	struct vop_reclaim_args /* {
    193 		struct vnode *a_vp;
    194 	} */ *ap;
    195 {
    196 	register struct vnode *vp = ap->a_vp;
    197 	register struct nfsnode *np = VTONFS(vp);
    198 	register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    199 	register struct nfsnode *nq;
    200 	extern int prtactive;
    201 
    202 	if (prtactive && vp->v_usecount != 0)
    203 		vprint("nfs_reclaim: pushing active", vp);
    204 	/*
    205 	 * Remove the nfsnode from its hash chain.
    206 	 */
    207 	if (nq = np->n_forw)
    208 		nq->n_back = np->n_back;
    209 	*np->n_back = nq;
    210 
    211 	/*
    212 	 * For nqnfs, take it off the timer queue as required.
    213 	 */
    214 	if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_tnext) {
    215 		if (np->n_tnext == (struct nfsnode *)nmp)
    216 			nmp->nm_tprev = np->n_tprev;
    217 		else
    218 			np->n_tnext->n_tprev = np->n_tprev;
    219 		if (np->n_tprev == (struct nfsnode *)nmp)
    220 			nmp->nm_tnext = np->n_tnext;
    221 		else
    222 			np->n_tprev->n_tnext = np->n_tnext;
    223 	}
    224 	cache_purge(vp);
    225 	FREE(vp->v_data, M_NFSNODE);
    226 	vp->v_data = (void *)0;
    227 	return (0);
    228 }
    229 
    230 /*
    231  * Lock an nfsnode
    232  */
    233 nfs_lock(ap)
    234 	struct vop_lock_args /* {
    235 		struct vnode *a_vp;
    236 	} */ *ap;
    237 {
    238 	register struct vnode *vp = ap->a_vp;
    239 
    240 	/*
    241 	 * Ugh, another place where interruptible mounts will get hung.
    242 	 * If you make this sleep interruptible, then you have to fix all
    243 	 * the VOP_LOCK() calls to expect interruptibility.
    244 	 */
    245 	while (vp->v_flag & VXLOCK) {
    246 		vp->v_flag |= VXWANT;
    247 		sleep((caddr_t)vp, PINOD);
    248 	}
    249 	if (vp->v_tag == VT_NON)
    250 		return (ENOENT);
    251 	return (0);
    252 }
    253 
    254 /*
    255  * Unlock an nfsnode
    256  */
    257 nfs_unlock(ap)
    258 	struct vop_unlock_args /* {
    259 		struct vnode *a_vp;
    260 	} */ *ap;
    261 {
    262 
    263 	return (0);
    264 }
    265 
    266 /*
    267  * Check for a locked nfsnode
    268  */
    269 nfs_islocked(ap)
    270 	struct vop_islocked_args /* {
    271 		struct vnode *a_vp;
    272 	} */ *ap;
    273 {
    274 
    275 	return (0);
    276 }
    277 
    278 /*
    279  * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
    280  * done. Currently nothing to do.
    281  */
    282 /* ARGSUSED */
    283 int
    284 nfs_abortop(ap)
    285 	struct vop_abortop_args /* {
    286 		struct vnode *a_dvp;
    287 		struct componentname *a_cnp;
    288 	} */ *ap;
    289 {
    290 
    291 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    292 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
    293 	return (0);
    294 }
    295