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