]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
55e303ae | 2 | * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
e5568f75 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
23 | /* | |
24 | * Copyright (c) 1989, 1993 | |
25 | * The Regents of the University of California. All rights reserved. | |
26 | * | |
27 | * This code is derived from software contributed to Berkeley by | |
28 | * Rick Macklem at The University of Guelph. | |
29 | * | |
30 | * Redistribution and use in source and binary forms, with or without | |
31 | * modification, are permitted provided that the following conditions | |
32 | * are met: | |
33 | * 1. Redistributions of source code must retain the above copyright | |
34 | * notice, this list of conditions and the following disclaimer. | |
35 | * 2. Redistributions in binary form must reproduce the above copyright | |
36 | * notice, this list of conditions and the following disclaimer in the | |
37 | * documentation and/or other materials provided with the distribution. | |
38 | * 3. All advertising materials mentioning features or use of this software | |
39 | * must display the following acknowledgement: | |
40 | * This product includes software developed by the University of | |
41 | * California, Berkeley and its contributors. | |
42 | * 4. Neither the name of the University nor the names of its contributors | |
43 | * may be used to endorse or promote products derived from this software | |
44 | * without specific prior written permission. | |
45 | * | |
46 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
47 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
48 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
49 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
50 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
51 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
52 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
53 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
54 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
55 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
56 | * SUCH DAMAGE. | |
57 | * | |
58 | * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95 | |
59 | * FreeBSD-Id: nfs_node.c,v 1.22 1997/10/28 14:06:20 bde Exp $ | |
60 | */ | |
61 | ||
62 | ||
63 | #include <sys/param.h> | |
64 | #include <sys/systm.h> | |
65 | #include <sys/proc.h> | |
66 | #include <sys/mount.h> | |
67 | #include <sys/namei.h> | |
68 | #include <sys/vnode.h> | |
69 | #include <sys/malloc.h> | |
70 | ||
71 | #include <nfs/rpcv2.h> | |
72 | #include <nfs/nfsproto.h> | |
73 | #include <nfs/nfs.h> | |
74 | #include <nfs/nfsnode.h> | |
75 | #include <nfs/nfsmount.h> | |
76 | ||
1c79356b A |
77 | LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl; |
78 | u_long nfsnodehash; | |
79 | ||
80 | #define TRUE 1 | |
81 | #define FALSE 0 | |
82 | ||
83 | /* | |
84 | * Initialize hash links for nfsnodes | |
85 | * and build nfsnode free list. | |
86 | */ | |
87 | void | |
e5568f75 | 88 | nfs_nhinit(void) |
1c79356b A |
89 | { |
90 | nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash); | |
91 | } | |
92 | ||
93 | /* | |
94 | * Compute an entry in the NFS hash table structure | |
95 | */ | |
96 | u_long | |
97 | nfs_hash(fhp, fhsize) | |
98 | register nfsfh_t *fhp; | |
99 | int fhsize; | |
100 | { | |
101 | register u_char *fhpp; | |
102 | register u_long fhsum; | |
103 | register int i; | |
104 | ||
105 | fhpp = &fhp->fh_bytes[0]; | |
106 | fhsum = 0; | |
107 | for (i = 0; i < fhsize; i++) | |
108 | fhsum += *fhpp++; | |
109 | return (fhsum); | |
110 | } | |
111 | ||
112 | /* | |
113 | * Look up a vnode/nfsnode by file handle. | |
114 | * Callers must check for mount points!! | |
115 | * In all cases, a pointer to a | |
116 | * nfsnode structure is returned. | |
117 | */ | |
118 | int nfs_node_hash_lock; | |
119 | ||
120 | int | |
121 | nfs_nget(mntp, fhp, fhsize, npp) | |
122 | struct mount *mntp; | |
123 | register nfsfh_t *fhp; | |
124 | int fhsize; | |
125 | struct nfsnode **npp; | |
126 | { | |
127 | struct proc *p = current_proc(); /* XXX */ | |
128 | struct nfsnode *np; | |
129 | struct nfsnodehashhead *nhpp; | |
130 | register struct vnode *vp; | |
131 | struct vnode *nvp; | |
132 | int error; | |
55e303ae | 133 | struct mount *mp; |
1c79356b A |
134 | |
135 | /* Check for unmount in progress */ | |
55e303ae | 136 | if (!mntp || (mntp->mnt_kern_flag & MNTK_UNMOUNT)) { |
1c79356b | 137 | *npp = 0; |
55e303ae | 138 | return (!mntp ? ENXIO : EPERM); |
1c79356b A |
139 | } |
140 | ||
141 | nhpp = NFSNOHASH(nfs_hash(fhp, fhsize)); | |
142 | loop: | |
143 | for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) { | |
55e303ae A |
144 | mp = (np->n_flag & NINIT) ? np->n_mount : NFSTOV(np)->v_mount; |
145 | if (mntp != mp || np->n_fhsize != fhsize || | |
1c79356b A |
146 | bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) |
147 | continue; | |
55e303ae A |
148 | /* if the node is still being initialized, sleep on it */ |
149 | if (np->n_flag & NINIT) { | |
150 | np->n_flag |= NWINIT; | |
151 | tsleep(np, PINOD, "nfsngt", 0); | |
152 | goto loop; | |
153 | } | |
1c79356b A |
154 | vp = NFSTOV(np); |
155 | if (vget(vp, LK_EXCLUSIVE, p)) | |
156 | goto loop; | |
157 | *npp = np; | |
158 | return(0); | |
159 | } | |
160 | /* | |
161 | * Obtain a lock to prevent a race condition if the getnewvnode() | |
162 | * or MALLOC() below happens to block. | |
163 | */ | |
164 | if (nfs_node_hash_lock) { | |
165 | while (nfs_node_hash_lock) { | |
166 | nfs_node_hash_lock = -1; | |
167 | tsleep(&nfs_node_hash_lock, PVM, "nfsngt", 0); | |
168 | } | |
169 | goto loop; | |
170 | } | |
171 | nfs_node_hash_lock = 1; | |
172 | ||
173 | /* | |
55e303ae A |
174 | * allocate and initialize nfsnode and stick it in the hash |
175 | * before calling getnewvnode(). Anyone finding it in the | |
176 | * hash before initialization is complete will wait for it. | |
1c79356b A |
177 | */ |
178 | MALLOC_ZONE(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK); | |
1c79356b | 179 | bzero((caddr_t)np, sizeof *np); |
55e303ae A |
180 | np->n_flag |= NINIT; |
181 | np->n_mount = mntp; | |
182 | lockinit(&np->n_lock, PINOD, "nfsnode", 0, 0); | |
183 | /* lock the new nfsnode */ | |
184 | lockmgr(&np->n_lock, LK_EXCLUSIVE, NULL, p); | |
185 | ||
186 | /* Insert the nfsnode in the hash queue for its new file handle */ | |
1c79356b A |
187 | if (fhsize > NFS_SMALLFH) { |
188 | MALLOC_ZONE(np->n_fhp, nfsfh_t *, | |
189 | fhsize, M_NFSBIGFH, M_WAITOK); | |
190 | } else | |
191 | np->n_fhp = &np->n_fh; | |
192 | bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize); | |
193 | np->n_fhsize = fhsize; | |
55e303ae A |
194 | LIST_INSERT_HEAD(nhpp, np, n_hash); |
195 | np->n_flag |= NHASHED; | |
1c79356b | 196 | |
55e303ae | 197 | /* release lock on hash table */ |
1c79356b A |
198 | if (nfs_node_hash_lock < 0) |
199 | wakeup(&nfs_node_hash_lock); | |
200 | nfs_node_hash_lock = 0; | |
201 | ||
55e303ae A |
202 | /* now, attempt to get a new vnode */ |
203 | error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp); | |
204 | if (error) { | |
205 | LIST_REMOVE(np, n_hash); | |
206 | np->n_flag &= ~NHASHED; | |
207 | if (np->n_fhsize > NFS_SMALLFH) | |
208 | FREE_ZONE((caddr_t)np->n_fhp, np->n_fhsize, M_NFSBIGFH); | |
209 | FREE_ZONE(np, sizeof *np, M_NFSNODE); | |
210 | *npp = 0; | |
211 | return (error); | |
212 | } | |
213 | vp = nvp; | |
214 | vp->v_data = np; | |
215 | np->n_vnode = vp; | |
216 | *npp = np; | |
217 | ||
218 | /* node is now initialized, check if anyone's waiting for it */ | |
219 | np->n_flag &= ~NINIT; | |
220 | if (np->n_flag & NWINIT) { | |
221 | np->n_flag &= ~NWINIT; | |
222 | wakeup((caddr_t)np); | |
223 | } | |
1c79356b | 224 | |
fa4905b1 | 225 | return (error); |
1c79356b A |
226 | } |
227 | ||
228 | int | |
229 | nfs_inactive(ap) | |
230 | struct vop_inactive_args /* { | |
231 | struct vnode *a_vp; | |
232 | struct proc *a_p; | |
233 | } */ *ap; | |
234 | { | |
235 | register struct nfsnode *np; | |
236 | register struct sillyrename *sp; | |
237 | struct proc *p = current_proc(); /* XXX */ | |
238 | extern int prtactive; | |
239 | struct ucred *cred; | |
240 | ||
241 | np = VTONFS(ap->a_vp); | |
242 | if (prtactive && ap->a_vp->v_usecount != 0) | |
243 | vprint("nfs_inactive: pushing active", ap->a_vp); | |
244 | if (ap->a_vp->v_type != VDIR) { | |
245 | sp = np->n_sillyrename; | |
246 | np->n_sillyrename = (struct sillyrename *)0; | |
247 | } else | |
248 | sp = (struct sillyrename *)0; | |
249 | ||
250 | if (sp) { | |
251 | /* | |
252 | * Remove the silly file that was rename'd earlier | |
253 | */ | |
254 | #if DIAGNOSTIC | |
255 | kprintf("nfs_inactive removing %s, dvp=%x, a_vp=%x, ap=%x, np=%x, sp=%x\n", &sp->s_name[0], (unsigned)sp->s_dvp, (unsigned)ap->a_vp, (unsigned)ap, (unsigned)np, (unsigned)sp); | |
256 | #endif | |
1c79356b | 257 | (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1); |
fa4905b1 | 258 | np->n_size = 0; |
1c79356b | 259 | ubc_setsize(ap->a_vp, (off_t)0); |
55e303ae A |
260 | nfs_removeit(sp); |
261 | /* | |
262 | * remove nfsnode from hash now so we can't accidentally find it | |
263 | * again if another object gets created with the same filehandle | |
264 | * before this vnode gets reclaimed | |
265 | */ | |
266 | LIST_REMOVE(np, n_hash); | |
267 | np->n_flag &= ~NHASHED; | |
1c79356b A |
268 | cred = sp->s_cred; |
269 | if (cred != NOCRED) { | |
270 | sp->s_cred = NOCRED; | |
271 | crfree(cred); | |
272 | } | |
273 | vrele(sp->s_dvp); | |
274 | FREE_ZONE((caddr_t)sp, sizeof (struct sillyrename), M_NFSREQ); | |
1c79356b A |
275 | } |
276 | np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED | | |
55e303ae | 277 | NQNFSNONCACHE | NQNFSWRITE | NHASHED); |
1c79356b A |
278 | VOP_UNLOCK(ap->a_vp, 0, ap->a_p); |
279 | return (0); | |
280 | } | |
281 | ||
282 | /* | |
283 | * Reclaim an nfsnode so that it can be used for other purposes. | |
284 | */ | |
285 | int | |
286 | nfs_reclaim(ap) | |
287 | struct vop_reclaim_args /* { | |
288 | struct vnode *a_vp; | |
289 | } */ *ap; | |
290 | { | |
291 | register struct vnode *vp = ap->a_vp; | |
292 | register struct nfsnode *np = VTONFS(vp); | |
e5568f75 | 293 | register struct nfsmount *nmp; |
1c79356b A |
294 | register struct nfsdmap *dp, *dp2; |
295 | extern int prtactive; | |
296 | ||
297 | if (prtactive && vp->v_usecount != 0) | |
298 | vprint("nfs_reclaim: pushing active", vp); | |
299 | ||
55e303ae A |
300 | if (np->n_flag & NHASHED) { |
301 | LIST_REMOVE(np, n_hash); | |
302 | np->n_flag &= ~NHASHED; | |
303 | } | |
1c79356b A |
304 | |
305 | /* | |
306 | * In case we block during FREE_ZONEs below, get the entry out | |
307 | * of tbe name cache now so subsequent lookups won't find it. | |
308 | */ | |
309 | cache_purge(vp); | |
310 | ||
311 | /* | |
312 | * For nqnfs, take it off the timer queue as required. | |
313 | */ | |
e5568f75 A |
314 | nmp = VFSTONFS(vp->v_mount); |
315 | if (nmp && (nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) { | |
1c79356b A |
316 | CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer); |
317 | } | |
318 | ||
319 | /* | |
320 | * Free up any directory cookie structures and | |
321 | * large file handle structures that might be associated with | |
322 | * this nfs node. | |
323 | */ | |
324 | if (vp->v_type == VDIR) { | |
325 | dp = np->n_cookies.lh_first; | |
326 | while (dp) { | |
327 | dp2 = dp; | |
328 | dp = dp->ndm_list.le_next; | |
329 | FREE_ZONE((caddr_t)dp2, | |
330 | sizeof (struct nfsdmap), M_NFSDIROFF); | |
331 | } | |
332 | } | |
333 | if (np->n_fhsize > NFS_SMALLFH) { | |
334 | FREE_ZONE((caddr_t)np->n_fhp, np->n_fhsize, M_NFSBIGFH); | |
335 | } | |
336 | ||
337 | FREE_ZONE(vp->v_data, sizeof (struct nfsnode), M_NFSNODE); | |
338 | vp->v_data = (void *)0; | |
339 | return (0); | |
340 | } | |
341 | ||
1c79356b A |
342 | /* |
343 | * Lock an nfsnode | |
344 | */ | |
345 | int | |
346 | nfs_lock(ap) | |
347 | struct vop_lock_args /* { | |
fa4905b1 A |
348 | struct vnode *a_vp; |
349 | int a_flags; | |
350 | struct proc *a_p; | |
1c79356b A |
351 | } */ *ap; |
352 | { | |
353 | register struct vnode *vp = ap->a_vp; | |
354 | ||
355 | /* | |
356 | * Ugh, another place where interruptible mounts will get hung. | |
fa4905b1 | 357 | * If you make this call interruptible, then you have to fix all |
1c79356b A |
358 | * the VOP_LOCK() calls to expect interruptibility. |
359 | */ | |
1c79356b | 360 | if (vp->v_tag == VT_NON) |
fa4905b1 A |
361 | return (ENOENT); /* ??? -- got to check something and error, but what? */ |
362 | ||
363 | return(lockmgr(&VTONFS(vp)->n_lock, ap->a_flags, &vp->v_interlock, | |
364 | ap->a_p)); | |
365 | ||
1c79356b A |
366 | } |
367 | ||
368 | /* | |
369 | * Unlock an nfsnode | |
370 | */ | |
371 | int | |
372 | nfs_unlock(ap) | |
fa4905b1 A |
373 | struct vop_unlock_args /* { |
374 | struct vnode *a_vp; | |
375 | int a_flags; | |
376 | struct proc *a_p; | |
377 | } */ *ap; | |
1c79356b | 378 | { |
fa4905b1 | 379 | struct vnode *vp = ap->a_vp; |
1c79356b | 380 | |
fa4905b1 A |
381 | return (lockmgr(&VTONFS(vp)->n_lock, ap->a_flags | LK_RELEASE, |
382 | &vp->v_interlock, ap->a_p)); | |
1c79356b A |
383 | } |
384 | ||
385 | /* | |
386 | * Check for a locked nfsnode | |
387 | */ | |
388 | int | |
389 | nfs_islocked(ap) | |
390 | struct vop_islocked_args /* { | |
391 | struct vnode *a_vp; | |
392 | } */ *ap; | |
393 | { | |
fa4905b1 A |
394 | return (lockstatus(&VTONFS(ap->a_vp)->n_lock)); |
395 | ||
1c79356b | 396 | } |