nfs_node.c revision 1.14 1 /* $NetBSD: nfs_node.c,v 1.14 1995/12/19 23:07:27 cgd 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 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash);
73 }
74
75 /*
76 * Compute an entry in the NFS hash table structure
77 */
78 struct nfsnodehashhead *
79 nfs_hash(fhp)
80 register nfsv2fh_t *fhp;
81 {
82 register u_char *fhpp;
83 register u_long fhsum;
84 int i;
85
86 fhpp = &fhp->fh_bytes[0];
87 fhsum = 0;
88 for (i = 0; i < NFSX_FH; i++)
89 fhsum += *fhpp++;
90 return (NFSNOHASH(fhsum));
91 }
92
93 /*
94 * Look up a vnode/nfsnode by file handle.
95 * Callers must check for mount points!!
96 * In all cases, a pointer to a
97 * nfsnode structure is returned.
98 */
99 nfs_nget(mntp, fhp, npp)
100 struct mount *mntp;
101 register nfsv2fh_t *fhp;
102 struct nfsnode **npp;
103 {
104 register struct nfsnode *np;
105 struct nfsnodehashhead *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->lh_first; np != 0; np = np->n_hash.le_next) {
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 LIST_INSERT_HEAD(nhpp, np, n_hash);
136 bcopy((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH);
137 np->n_attrstamp = 0;
138 np->n_direofoffset = 0;
139 np->n_sillyrename = (struct sillyrename *)0;
140 np->n_size = 0;
141 np->n_mtime = 0;
142 np->n_lockf = 0;
143 if (VFSTONFS(mntp)->nm_flag & NFSMNT_NQNFS) {
144 np->n_brev = 0;
145 np->n_lrev = 0;
146 np->n_expiry = (time_t)0;
147 np->n_timer.cqe_next = (struct nfsnode *)0;
148 }
149 *npp = np;
150 return (0);
151 }
152
153 nfs_inactive(ap)
154 struct vop_inactive_args /* {
155 struct vnode *a_vp;
156 } */ *ap;
157 {
158 register struct nfsnode *np;
159 register struct sillyrename *sp;
160 struct proc *p = curproc; /* XXX */
161 extern int prtactive;
162
163 np = VTONFS(ap->a_vp);
164 if (prtactive && ap->a_vp->v_usecount != 0)
165 vprint("nfs_inactive: pushing active", ap->a_vp);
166 sp = np->n_sillyrename;
167 np->n_sillyrename = (struct sillyrename *)0;
168 if (sp) {
169 /*
170 * Remove the silly file that was rename'd earlier
171 */
172 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
173 nfs_removeit(sp);
174 crfree(sp->s_cred);
175 vrele(sp->s_dvp);
176 #ifdef SILLYSEPARATE
177 free((caddr_t)sp, M_NFSREQ);
178 #endif
179 }
180 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
181 NQNFSNONCACHE | NQNFSWRITE);
182 return (0);
183 }
184
185 /*
186 * Reclaim an nfsnode so that it can be used for other purposes.
187 */
188 nfs_reclaim(ap)
189 struct vop_reclaim_args /* {
190 struct vnode *a_vp;
191 } */ *ap;
192 {
193 register struct vnode *vp = ap->a_vp;
194 register struct nfsnode *np = VTONFS(vp);
195 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
196 extern int prtactive;
197
198 if (prtactive && vp->v_usecount != 0)
199 vprint("nfs_reclaim: pushing active", vp);
200 LIST_REMOVE(np, n_hash);
201
202 /*
203 * For nqnfs, take it off the timer queue as required.
204 */
205 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
206 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
207 }
208 cache_purge(vp);
209 FREE(vp->v_data, M_NFSNODE);
210 vp->v_data = (void *)0;
211 return (0);
212 }
213
214 /*
215 * Lock an nfsnode
216 */
217 nfs_lock(ap)
218 struct vop_lock_args /* {
219 struct vnode *a_vp;
220 } */ *ap;
221 {
222 register struct vnode *vp = ap->a_vp;
223
224 /*
225 * Ugh, another place where interruptible mounts will get hung.
226 * If you make this sleep interruptible, then you have to fix all
227 * the VOP_LOCK() calls to expect interruptibility.
228 */
229 while (vp->v_flag & VXLOCK) {
230 vp->v_flag |= VXWANT;
231 sleep((caddr_t)vp, PINOD);
232 }
233 if (vp->v_tag == VT_NON)
234 return (ENOENT);
235 return (0);
236 }
237
238 /*
239 * Unlock an nfsnode
240 */
241 nfs_unlock(ap)
242 struct vop_unlock_args /* {
243 struct vnode *a_vp;
244 } */ *ap;
245 {
246
247 return (0);
248 }
249
250 /*
251 * Check for a locked nfsnode
252 */
253 nfs_islocked(ap)
254 struct vop_islocked_args /* {
255 struct vnode *a_vp;
256 } */ *ap;
257 {
258
259 return (0);
260 }
261
262 /*
263 * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
264 * done. Currently nothing to do.
265 */
266 /* ARGSUSED */
267 int
268 nfs_abortop(ap)
269 struct vop_abortop_args /* {
270 struct vnode *a_dvp;
271 struct componentname *a_cnp;
272 } */ *ap;
273 {
274
275 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
276 FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
277 return (0);
278 }
279