xref: /openbsd/sys/nfs/nfs_node.c (revision df930be7)
1 /*	$NetBSD: nfs_node.c,v 1.13 1994/08/18 22:47:46 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)nfs_node.c	8.2 (Berkeley) 12/30/93
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 
50 #include <nfs/rpcv2.h>
51 #include <nfs/nfsv2.h>
52 #include <nfs/nfs.h>
53 #include <nfs/nfsnode.h>
54 #include <nfs/nfsmount.h>
55 #include <nfs/nqnfs.h>
56 
57 #define	NFSNOHASH(fhsum) \
58 	(&nfsnodehashtbl[(fhsum) & nfsnodehash])
59 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
60 u_long nfsnodehash;
61 
62 #define TRUE	1
63 #define	FALSE	0
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 	nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash);
77 }
78 
79 /*
80  * Compute an entry in the NFS hash table structure
81  */
82 struct nfsnodehashhead *
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 (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;
109 	struct nfsnodehashhead *nhpp;
110 	register struct vnode *vp;
111 	extern int (**nfsv2_vnodeop_p)();
112 	struct vnode *nvp;
113 	int error;
114 
115 	nhpp = nfs_hash(fhp);
116 loop:
117 	for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
118 		if (mntp != NFSTOV(np)->v_mount ||
119 		    bcmp((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH))
120 			continue;
121 		vp = NFSTOV(np);
122 		if (vget(vp, 1))
123 			goto loop;
124 		*npp = np;
125 		return(0);
126 	}
127 	if (error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp)) {
128 		*npp = 0;
129 		return (error);
130 	}
131 	vp = nvp;
132 	MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
133 	vp->v_data = np;
134 	np->n_vnode = vp;
135 	/*
136 	 * Insert the nfsnode in the hash queue for its new file handle
137 	 */
138 	np->n_flag = 0;
139 	LIST_INSERT_HEAD(nhpp, np, n_hash);
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 	np->n_lockf = 0;
147 	if (VFSTONFS(mntp)->nm_flag & NFSMNT_NQNFS) {
148 		np->n_brev = 0;
149 		np->n_lrev = 0;
150 		np->n_expiry = (time_t)0;
151 		np->n_timer.cqe_next = (struct nfsnode *)0;
152 	}
153 	*npp = np;
154 	return (0);
155 }
156 
157 nfs_inactive(ap)
158 	struct vop_inactive_args /* {
159 		struct vnode *a_vp;
160 	} */ *ap;
161 {
162 	register struct nfsnode *np;
163 	register struct sillyrename *sp;
164 	struct proc *p = curproc;	/* XXX */
165 	extern int prtactive;
166 
167 	np = VTONFS(ap->a_vp);
168 	if (prtactive && ap->a_vp->v_usecount != 0)
169 		vprint("nfs_inactive: pushing active", ap->a_vp);
170 	sp = np->n_sillyrename;
171 	np->n_sillyrename = (struct sillyrename *)0;
172 	if (sp) {
173 		/*
174 		 * Remove the silly file that was rename'd earlier
175 		 */
176 		(void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
177 		nfs_removeit(sp);
178 		crfree(sp->s_cred);
179 		vrele(sp->s_dvp);
180 #ifdef SILLYSEPARATE
181 		free((caddr_t)sp, M_NFSREQ);
182 #endif
183 	}
184 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
185 		NQNFSNONCACHE | NQNFSWRITE);
186 	return (0);
187 }
188 
189 /*
190  * Reclaim an nfsnode so that it can be used for other purposes.
191  */
192 nfs_reclaim(ap)
193 	struct vop_reclaim_args /* {
194 		struct vnode *a_vp;
195 	} */ *ap;
196 {
197 	register struct vnode *vp = ap->a_vp;
198 	register struct nfsnode *np = VTONFS(vp);
199 	register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
200 	extern int prtactive;
201 
202 	if (prtactive && vp->v_usecount != 0)
203 		vprint("nfs_reclaim: pushing active", vp);
204 	LIST_REMOVE(np, n_hash);
205 
206 	/*
207 	 * For nqnfs, take it off the timer queue as required.
208 	 */
209 	if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
210 		CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
211 	}
212 	cache_purge(vp);
213 	FREE(vp->v_data, M_NFSNODE);
214 	vp->v_data = (void *)0;
215 	return (0);
216 }
217 
218 /*
219  * Lock an nfsnode
220  */
221 nfs_lock(ap)
222 	struct vop_lock_args /* {
223 		struct vnode *a_vp;
224 	} */ *ap;
225 {
226 	register struct vnode *vp = ap->a_vp;
227 
228 	/*
229 	 * Ugh, another place where interruptible mounts will get hung.
230 	 * If you make this sleep interruptible, then you have to fix all
231 	 * the VOP_LOCK() calls to expect interruptibility.
232 	 */
233 	while (vp->v_flag & VXLOCK) {
234 		vp->v_flag |= VXWANT;
235 		sleep((caddr_t)vp, PINOD);
236 	}
237 	if (vp->v_tag == VT_NON)
238 		return (ENOENT);
239 	return (0);
240 }
241 
242 /*
243  * Unlock an nfsnode
244  */
245 nfs_unlock(ap)
246 	struct vop_unlock_args /* {
247 		struct vnode *a_vp;
248 	} */ *ap;
249 {
250 
251 	return (0);
252 }
253 
254 /*
255  * Check for a locked nfsnode
256  */
257 nfs_islocked(ap)
258 	struct vop_islocked_args /* {
259 		struct vnode *a_vp;
260 	} */ *ap;
261 {
262 
263 	return (0);
264 }
265 
266 /*
267  * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
268  * done. Currently nothing to do.
269  */
270 /* ARGSUSED */
271 int
272 nfs_abortop(ap)
273 	struct vop_abortop_args /* {
274 		struct vnode *a_dvp;
275 		struct componentname *a_cnp;
276 	} */ *ap;
277 {
278 
279 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
280 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
281 	return (0);
282 }
283