Home | History | Annotate | Line # | Download | only in nfs
nfs_node.c revision 1.7
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * 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	7.34 (Berkeley) 5/15/91
     37  *	$Id: nfs_node.c,v 1.7 1994/04/21 23:23:29 cgd 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/nfsv2.h>
     50 #include <nfs/nfs.h>
     51 #include <nfs/nfsnode.h>
     52 #include <nfs/nfsmount.h>
     53 
     54 /* The request list head */
     55 extern struct nfsreq nfsreqh;
     56 
     57 #define	NFSNOHSZ	512
     58 #if	((NFSNOHSZ&(NFSNOHSZ-1)) == 0)
     59 #define	NFSNOHASH(fhsum)	((fhsum)&(NFSNOHSZ-1))
     60 #else
     61 #define	NFSNOHASH(fhsum)	(((unsigned)(fhsum))%NFSNOHSZ)
     62 #endif
     63 
     64 union nhead {
     65 	union  nhead *nh_head[2];
     66 	struct nfsnode *nh_chain[2];
     67 } nhead[NFSNOHSZ];
     68 
     69 #define TRUE	1
     70 #define	FALSE	0
     71 
     72 /*
     73  * Initialize hash links for nfsnodes
     74  * and build nfsnode free list.
     75  */
     76 nfs_nhinit()
     77 {
     78 	register int i;
     79 	register union  nhead *nh = nhead;
     80 
     81 #ifndef lint
     82 	if (VN_MAXPRIVATE < sizeof(struct nfsnode))
     83 		panic("nfs_nhinit: too small");
     84 #endif /* not lint */
     85 	for (i = NFSNOHSZ; --i >= 0; nh++) {
     86 		nh->nh_head[0] = nh;
     87 		nh->nh_head[1] = nh;
     88 	}
     89 }
     90 
     91 /*
     92  * Compute an entry in the NFS hash table structure
     93  */
     94 union nhead *
     95 nfs_hash(fhp)
     96 	register nfsv2fh_t *fhp;
     97 {
     98 	register u_char *fhpp;
     99 	register u_long fhsum;
    100 	int i;
    101 
    102 	fhpp = &fhp->fh_bytes[0];
    103 	fhsum = 0;
    104 	for (i = 0; i < NFSX_FH; i++)
    105 		fhsum += *fhpp++;
    106 	return (&nhead[NFSNOHASH(fhsum)]);
    107 }
    108 
    109 /*
    110  * Look up a vnode/nfsnode by file handle.
    111  * Callers must check for mount points!!
    112  * In all cases, a pointer to a
    113  * nfsnode structure is returned.
    114  */
    115 nfs_nget(mntp, fhp, npp)
    116 	struct mount *mntp;
    117 	register nfsv2fh_t *fhp;
    118 	struct nfsnode **npp;
    119 {
    120 	register struct nfsnode *np;
    121 	register struct vnode *vp;
    122 	extern struct vnodeops nfsv2_vnodeops;
    123 	struct vnode *nvp;
    124 	union nhead *nh;
    125 	int error;
    126 
    127 	nh = nfs_hash(fhp);
    128 loop:
    129 	for (np = nh->nh_chain[0]; np != (struct nfsnode *)nh; np = np->n_forw) {
    130 		if (mntp != NFSTOV(np)->v_mount ||
    131 		    bcmp((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH))
    132 			continue;
    133 		vp = NFSTOV(np);
    134 		if (vget(vp))
    135 			goto loop;
    136 		*npp = np;
    137 		return(0);
    138 	}
    139 	if (error = getnewvnode(VT_NFS, mntp, &nfsv2_vnodeops, &nvp)) {
    140 		*npp = 0;
    141 		return (error);
    142 	}
    143 	vp = nvp;
    144 	np = VTONFS(vp);
    145 	np->n_vnode = vp;
    146 	/*
    147 	 * Insert the nfsnode in the hash queue for its new file handle
    148 	 */
    149 	np->n_flag = 0;
    150 	insque(np, nh);
    151 	nfs_lock(vp);
    152 	bcopy((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH);
    153 	np->n_attrstamp = 0;
    154 	np->n_direofoffset = 0;
    155 	np->n_sillyrename = (struct sillyrename *)0;
    156 	np->n_size = 0;
    157 	np->n_mtime = 0;
    158 	np->n_lockf = 0;
    159 	np->n_delayed_mtime.tv_sec = VNOVAL;
    160 	np->n_delayed_atime.tv_sec = VNOVAL;
    161 	*npp = np;
    162 	return (0);
    163 }
    164 
    165 nfs_inactive(vp, p)
    166 	struct vnode *vp;
    167 	struct proc *p;
    168 {
    169 	register struct nfsnode *np;
    170 	register struct sillyrename *sp;
    171 	struct nfsnode *dnp;
    172 	extern int prtactive;
    173 
    174 	np = VTONFS(vp);
    175 	if (prtactive && vp->v_usecount != 0)
    176 		vprint("nfs_inactive: pushing active", vp);
    177 	nfs_lock(vp);
    178 	sp = np->n_sillyrename;
    179 	np->n_sillyrename = (struct sillyrename *)0;
    180 	if (sp) {
    181 		/*
    182 		 * Remove the silly file that was rename'd earlier
    183 		 */
    184 		if (!nfs_nget(vp->v_mount, &sp->s_fh, &dnp)) {
    185 			sp->s_dvp = NFSTOV(dnp);
    186 			nfs_removeit(sp, p);
    187 			nfs_nput(sp->s_dvp);
    188 		}
    189 		crfree(sp->s_cred);
    190 		vrele(sp->s_dvp);
    191 		free((caddr_t)sp, M_NFSREQ);
    192 	}
    193 	nfs_unlock(vp);
    194 	np->n_flag &= NMODIFIED;
    195 #ifdef notdef
    196 	/*
    197 	 * Scan the request list for any requests left hanging about
    198 	 */
    199 	s = splnet();
    200 	rep = nfsreqh.r_next;
    201 	while (rep && rep != &nfsreqh) {
    202 		if (rep->r_vp == vp) {
    203 			rep->r_prev->r_next = rep2 = rep->r_next;
    204 			rep->r_next->r_prev = rep->r_prev;
    205 			m_freem(rep->r_mreq);
    206 			if (rep->r_mrep != NULL)
    207 				m_freem(rep->r_mrep);
    208 			free((caddr_t)rep, M_NFSREQ);
    209 			rep = rep2;
    210 		} else
    211 			rep = rep->r_next;
    212 	}
    213 	splx(s);
    214 #endif
    215 	return (0);
    216 }
    217 
    218 /*
    219  * Reclaim an nfsnode so that it can be used for other purposes.
    220  */
    221 nfs_reclaim(vp)
    222 	register struct vnode *vp;
    223 {
    224 	register struct nfsnode *np = VTONFS(vp);
    225 	extern int prtactive;
    226 
    227 	if (prtactive && vp->v_usecount != 0)
    228 		vprint("nfs_reclaim: pushing active", vp);
    229 	/*
    230 	 * Remove the nfsnode from its hash chain.
    231 	 */
    232 	remque(np);
    233 	np->n_forw = np;
    234 	np->n_back = np;
    235 	cache_purge(vp);
    236 	np->n_flag = 0;
    237 	np->n_direofoffset = 0;
    238 	return (0);
    239 }
    240 
    241 /*
    242  * Lock an nfsnode
    243  */
    244 
    245 nfs_lock(vp)
    246 	struct vnode *vp;
    247 {
    248 
    249 	return (0);
    250 }
    251 
    252 /*
    253  * Unlock an nfsnode
    254  */
    255 nfs_unlock(vp)
    256 	struct vnode *vp;
    257 {
    258 
    259 	return (0);
    260 }
    261 
    262 /*
    263  * Check for a locked nfsnode
    264  */
    265 nfs_islocked(vp)
    266 	struct vnode *vp;
    267 {
    268 
    269 	return (0);
    270 }
    271 
    272 /*
    273  * Unlock (i.e. do nothing) and vrele()
    274  */
    275 nfs_nput(vp)
    276 	struct vnode *vp;
    277 {
    278 	register struct nfsnode *np = VTONFS(vp);
    279 
    280 	vrele(vp);
    281 }
    282 
    283 /*
    284  * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
    285  * done. Currently nothing to do.
    286  */
    287 /* ARGSUSED */
    288 nfs_abortop(ndp)
    289 	struct nameidata *ndp;
    290 {
    291 
    292 	if ((ndp->ni_nameiop & (HASBUF | SAVESTART)) == HASBUF)
    293 		FREE(ndp->ni_pnbuf, M_NAMEI);
    294 	return (0);
    295 }
    296