xref: /netbsd-src/sys/nfs/nfs_node.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
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.11 1994/06/13 15:39:13 mycroft 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 /*
64  * Initialize hash links for nfsnodes
65  * and build nfsnode free list.
66  */
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 	nheadhashtbl = hashinit(desiredvnodes, M_NFSNODE, &nheadhash);
75 }
76 
77 /*
78  * Compute an entry in the NFS hash table structure
79  */
80 struct nfsnode **
81 nfs_hash(fhp)
82 	register nfsv2fh_t *fhp;
83 {
84 	register u_char *fhpp;
85 	register u_long fhsum;
86 	int i;
87 
88 	fhpp = &fhp->fh_bytes[0];
89 	fhsum = 0;
90 	for (i = 0; i < NFSX_FH; i++)
91 		fhsum += *fhpp++;
92 	return (&nheadhashtbl[NFSNOHASH(fhsum)]);
93 }
94 
95 /*
96  * Look up a vnode/nfsnode by file handle.
97  * Callers must check for mount points!!
98  * In all cases, a pointer to a
99  * nfsnode structure is returned.
100  */
101 nfs_nget(mntp, fhp, npp)
102 	struct mount *mntp;
103 	register nfsv2fh_t *fhp;
104 	struct nfsnode **npp;
105 {
106 	register struct nfsnode *np, *nq, **nhpp;
107 	register struct vnode *vp;
108 	extern int (**nfsv2_vnodeop_p)();
109 	struct vnode *nvp;
110 	int error;
111 
112 	nhpp = nfs_hash(fhp);
113 loop:
114 	for (np = *nhpp; np; np = np->n_forw) {
115 		if (mntp != NFSTOV(np)->v_mount ||
116 		    bcmp((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH))
117 			continue;
118 		vp = NFSTOV(np);
119 		if (vget(vp, 1))
120 			goto loop;
121 		*npp = np;
122 		return(0);
123 	}
124 	if (error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp)) {
125 		*npp = 0;
126 		return (error);
127 	}
128 	vp = nvp;
129 	MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
130 	vp->v_data = np;
131 	np->n_vnode = vp;
132 	/*
133 	 * Insert the nfsnode in the hash queue for its new file handle
134 	 */
135 	np->n_flag = 0;
136 	if (nq = *nhpp)
137 		nq->n_back = &np->n_forw;
138 	np->n_forw = nq;
139 	np->n_back = nhpp;
140 	*nhpp = np;
141 	bcopy((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH);
142 	np->n_attrstamp = 0;
143 	np->n_direofoffset = 0;
144 	np->n_sillyrename = (struct sillyrename *)0;
145 	np->n_size = 0;
146 	np->n_mtime = 0;
147 	np->n_lockf = 0;
148 	if (VFSTONFS(mntp)->nm_flag & NFSMNT_NQNFS) {
149 		np->n_brev = 0;
150 		np->n_lrev = 0;
151 		np->n_expiry = (time_t)0;
152 		np->n_tnext = (struct nfsnode *)0;
153 	}
154 	*npp = np;
155 	return (0);
156 }
157 
158 nfs_inactive(ap)
159 	struct vop_inactive_args /* {
160 		struct vnode *a_vp;
161 	} */ *ap;
162 {
163 	register struct nfsnode *np;
164 	register struct sillyrename *sp;
165 	struct proc *p = curproc;	/* XXX */
166 	extern int prtactive;
167 
168 	np = VTONFS(ap->a_vp);
169 	if (prtactive && ap->a_vp->v_usecount != 0)
170 		vprint("nfs_inactive: pushing active", ap->a_vp);
171 	sp = np->n_sillyrename;
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 #ifdef SILLYSEPARATE
182 		free((caddr_t)sp, M_NFSREQ);
183 #endif
184 	}
185 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
186 		NQNFSNONCACHE | NQNFSWRITE);
187 	return (0);
188 }
189 
190 /*
191  * Reclaim an nfsnode so that it can be used for other purposes.
192  */
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 nfsnode *nq;
202 	extern int prtactive;
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