]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
11 | * | |
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 | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
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. | |
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 | ||
77 | #ifdef MALLOC_DEFINE | |
78 | static MALLOC_DEFINE(M_NFSNODE, "NFS node", "NFS vnode private part"); | |
79 | #endif | |
80 | ||
81 | LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl; | |
82 | u_long nfsnodehash; | |
83 | ||
84 | #define TRUE 1 | |
85 | #define FALSE 0 | |
86 | ||
87 | /* | |
88 | * Initialize hash links for nfsnodes | |
89 | * and build nfsnode free list. | |
90 | */ | |
91 | void | |
92 | nfs_nhinit() | |
93 | { | |
94 | nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash); | |
95 | } | |
96 | ||
97 | /* | |
98 | * Compute an entry in the NFS hash table structure | |
99 | */ | |
100 | u_long | |
101 | nfs_hash(fhp, fhsize) | |
102 | register nfsfh_t *fhp; | |
103 | int fhsize; | |
104 | { | |
105 | register u_char *fhpp; | |
106 | register u_long fhsum; | |
107 | register int i; | |
108 | ||
109 | fhpp = &fhp->fh_bytes[0]; | |
110 | fhsum = 0; | |
111 | for (i = 0; i < fhsize; i++) | |
112 | fhsum += *fhpp++; | |
113 | return (fhsum); | |
114 | } | |
115 | ||
116 | /* | |
117 | * Look up a vnode/nfsnode by file handle. | |
118 | * Callers must check for mount points!! | |
119 | * In all cases, a pointer to a | |
120 | * nfsnode structure is returned. | |
121 | */ | |
122 | int nfs_node_hash_lock; | |
123 | ||
124 | int | |
125 | nfs_nget(mntp, fhp, fhsize, npp) | |
126 | struct mount *mntp; | |
127 | register nfsfh_t *fhp; | |
128 | int fhsize; | |
129 | struct nfsnode **npp; | |
130 | { | |
131 | struct proc *p = current_proc(); /* XXX */ | |
132 | struct nfsnode *np; | |
133 | struct nfsnodehashhead *nhpp; | |
134 | register struct vnode *vp; | |
135 | struct vnode *nvp; | |
136 | int error; | |
137 | ||
138 | /* Check for unmount in progress */ | |
139 | if (mntp->mnt_kern_flag & MNTK_UNMOUNT) { | |
140 | *npp = 0; | |
141 | return (EPERM); | |
142 | } | |
143 | ||
144 | nhpp = NFSNOHASH(nfs_hash(fhp, fhsize)); | |
145 | loop: | |
146 | for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) { | |
147 | if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize || | |
148 | bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) | |
149 | continue; | |
150 | vp = NFSTOV(np); | |
151 | if (vget(vp, LK_EXCLUSIVE, p)) | |
152 | goto loop; | |
153 | *npp = np; | |
154 | return(0); | |
155 | } | |
156 | /* | |
157 | * Obtain a lock to prevent a race condition if the getnewvnode() | |
158 | * or MALLOC() below happens to block. | |
159 | */ | |
160 | if (nfs_node_hash_lock) { | |
161 | while (nfs_node_hash_lock) { | |
162 | nfs_node_hash_lock = -1; | |
163 | tsleep(&nfs_node_hash_lock, PVM, "nfsngt", 0); | |
164 | } | |
165 | goto loop; | |
166 | } | |
167 | nfs_node_hash_lock = 1; | |
168 | ||
169 | /* | |
170 | * Do the MALLOC before the getnewvnode since doing so afterward | |
171 | * might cause a bogus v_data pointer to get dereferenced | |
172 | * elsewhere if MALLOC should block. | |
173 | */ | |
174 | MALLOC_ZONE(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK); | |
175 | ||
176 | error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp); | |
177 | if (error) { | |
178 | if (nfs_node_hash_lock < 0) | |
179 | wakeup(&nfs_node_hash_lock); | |
180 | nfs_node_hash_lock = 0; | |
181 | *npp = 0; | |
182 | FREE_ZONE(np, sizeof *np, M_NFSNODE); | |
183 | return (error); | |
184 | } | |
185 | vp = nvp; | |
186 | bzero((caddr_t)np, sizeof *np); | |
187 | vp->v_data = np; | |
188 | np->n_vnode = vp; | |
189 | /* | |
190 | * Insert the nfsnode in the hash queue for its new file handle | |
191 | */ | |
192 | LIST_INSERT_HEAD(nhpp, np, n_hash); | |
193 | if (fhsize > NFS_SMALLFH) { | |
194 | MALLOC_ZONE(np->n_fhp, nfsfh_t *, | |
195 | fhsize, M_NFSBIGFH, M_WAITOK); | |
196 | } else | |
197 | np->n_fhp = &np->n_fh; | |
198 | bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize); | |
199 | np->n_fhsize = fhsize; | |
200 | *npp = np; | |
201 | ||
202 | if (nfs_node_hash_lock < 0) | |
203 | wakeup(&nfs_node_hash_lock); | |
204 | nfs_node_hash_lock = 0; | |
205 | ||
206 | /* | |
207 | * Lock the new nfsnode. | |
208 | */ | |
fa4905b1 | 209 | error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); |
1c79356b | 210 | |
fa4905b1 | 211 | return (error); |
1c79356b A |
212 | } |
213 | ||
214 | int | |
215 | nfs_inactive(ap) | |
216 | struct vop_inactive_args /* { | |
217 | struct vnode *a_vp; | |
218 | struct proc *a_p; | |
219 | } */ *ap; | |
220 | { | |
221 | register struct nfsnode *np; | |
222 | register struct sillyrename *sp; | |
223 | struct proc *p = current_proc(); /* XXX */ | |
224 | extern int prtactive; | |
225 | struct ucred *cred; | |
226 | ||
227 | np = VTONFS(ap->a_vp); | |
228 | if (prtactive && ap->a_vp->v_usecount != 0) | |
229 | vprint("nfs_inactive: pushing active", ap->a_vp); | |
230 | if (ap->a_vp->v_type != VDIR) { | |
231 | sp = np->n_sillyrename; | |
232 | np->n_sillyrename = (struct sillyrename *)0; | |
233 | } else | |
234 | sp = (struct sillyrename *)0; | |
235 | ||
236 | if (sp) { | |
237 | /* | |
238 | * Remove the silly file that was rename'd earlier | |
239 | */ | |
240 | #if DIAGNOSTIC | |
241 | 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); | |
242 | #endif | |
243 | /* | |
244 | * We get a reference (vget) to ensure getnewvnode() | |
245 | * doesn't recycle vp while we're asleep awaiting I/O. | |
246 | * Note we don't need the reference unless usecount is | |
247 | * already zero. In the case of a forcible unmount it | |
248 | * wont be zero and doing a vget would fail because | |
249 | * vclean holds VXLOCK. | |
250 | */ | |
251 | if (ap->a_vp->v_usecount > 0) { | |
252 | VREF(ap->a_vp); | |
253 | } else if (vget(ap->a_vp, 0, ap->a_p)) | |
254 | panic("nfs_inactive: vget failed"); | |
255 | (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1); | |
fa4905b1 | 256 | np->n_size = 0; |
1c79356b A |
257 | ubc_setsize(ap->a_vp, (off_t)0); |
258 | ||
fa4905b1 A |
259 | /* We have a problem. The dvp could have gone away on us while |
260 | * in the unmount path. Thus it appears as VBAD and we cannot | |
261 | * use it. If we tried locking the parent (future), for silly | |
1c79356b A |
262 | * rename files, it is unclear where we would lock. The unmount |
263 | * code just pulls unlocked vnodes as it goes thru its list and | |
264 | * yanks them. Could unmount be smarter to see if a busy reg vnode has | |
265 | * a parent, and not yank it yet? Put in more passes at unmount | |
fa4905b1 A |
266 | * time? In the meantime, just check if it went away on us. |
267 | * Could have gone away during the nfs_vinvalbuf or ubc_setsize | |
268 | * which block. Or perhaps even before nfs_inactive got called. | |
1c79356b A |
269 | */ |
270 | if ((sp->s_dvp)->v_type != VBAD) | |
271 | nfs_removeit(sp); /* uses the dvp */ | |
272 | cred = sp->s_cred; | |
273 | if (cred != NOCRED) { | |
274 | sp->s_cred = NOCRED; | |
275 | crfree(cred); | |
276 | } | |
277 | vrele(sp->s_dvp); | |
278 | FREE_ZONE((caddr_t)sp, sizeof (struct sillyrename), M_NFSREQ); | |
279 | vrele(ap->a_vp); | |
280 | } | |
281 | np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED | | |
282 | NQNFSNONCACHE | NQNFSWRITE); | |
283 | VOP_UNLOCK(ap->a_vp, 0, ap->a_p); | |
284 | return (0); | |
285 | } | |
286 | ||
287 | /* | |
288 | * Reclaim an nfsnode so that it can be used for other purposes. | |
289 | */ | |
290 | int | |
291 | nfs_reclaim(ap) | |
292 | struct vop_reclaim_args /* { | |
293 | struct vnode *a_vp; | |
294 | } */ *ap; | |
295 | { | |
296 | register struct vnode *vp = ap->a_vp; | |
297 | register struct nfsnode *np = VTONFS(vp); | |
298 | register struct nfsmount *nmp = VFSTONFS(vp->v_mount); | |
299 | register struct nfsdmap *dp, *dp2; | |
300 | extern int prtactive; | |
301 | ||
302 | if (prtactive && vp->v_usecount != 0) | |
303 | vprint("nfs_reclaim: pushing active", vp); | |
304 | ||
305 | LIST_REMOVE(np, n_hash); | |
306 | ||
307 | /* | |
308 | * In case we block during FREE_ZONEs below, get the entry out | |
309 | * of tbe name cache now so subsequent lookups won't find it. | |
310 | */ | |
311 | cache_purge(vp); | |
312 | ||
313 | /* | |
314 | * For nqnfs, take it off the timer queue as required. | |
315 | */ | |
316 | if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) { | |
317 | CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer); | |
318 | } | |
319 | ||
320 | /* | |
321 | * Free up any directory cookie structures and | |
322 | * large file handle structures that might be associated with | |
323 | * this nfs node. | |
324 | */ | |
325 | if (vp->v_type == VDIR) { | |
326 | dp = np->n_cookies.lh_first; | |
327 | while (dp) { | |
328 | dp2 = dp; | |
329 | dp = dp->ndm_list.le_next; | |
330 | FREE_ZONE((caddr_t)dp2, | |
331 | sizeof (struct nfsdmap), M_NFSDIROFF); | |
332 | } | |
333 | } | |
334 | if (np->n_fhsize > NFS_SMALLFH) { | |
335 | FREE_ZONE((caddr_t)np->n_fhp, np->n_fhsize, M_NFSBIGFH); | |
336 | } | |
337 | ||
338 | FREE_ZONE(vp->v_data, sizeof (struct nfsnode), M_NFSNODE); | |
339 | vp->v_data = (void *)0; | |
340 | return (0); | |
341 | } | |
342 | ||
1c79356b A |
343 | /* |
344 | * Lock an nfsnode | |
345 | */ | |
346 | int | |
347 | nfs_lock(ap) | |
348 | struct vop_lock_args /* { | |
fa4905b1 A |
349 | struct vnode *a_vp; |
350 | int a_flags; | |
351 | struct proc *a_p; | |
1c79356b A |
352 | } */ *ap; |
353 | { | |
354 | register struct vnode *vp = ap->a_vp; | |
355 | ||
356 | /* | |
357 | * Ugh, another place where interruptible mounts will get hung. | |
fa4905b1 | 358 | * If you make this call interruptible, then you have to fix all |
1c79356b A |
359 | * the VOP_LOCK() calls to expect interruptibility. |
360 | */ | |
1c79356b | 361 | if (vp->v_tag == VT_NON) |
fa4905b1 A |
362 | return (ENOENT); /* ??? -- got to check something and error, but what? */ |
363 | ||
364 | return(lockmgr(&VTONFS(vp)->n_lock, ap->a_flags, &vp->v_interlock, | |
365 | ap->a_p)); | |
366 | ||
1c79356b A |
367 | } |
368 | ||
369 | /* | |
370 | * Unlock an nfsnode | |
371 | */ | |
372 | int | |
373 | nfs_unlock(ap) | |
fa4905b1 A |
374 | struct vop_unlock_args /* { |
375 | struct vnode *a_vp; | |
376 | int a_flags; | |
377 | struct proc *a_p; | |
378 | } */ *ap; | |
1c79356b | 379 | { |
fa4905b1 | 380 | struct vnode *vp = ap->a_vp; |
1c79356b | 381 | |
fa4905b1 A |
382 | return (lockmgr(&VTONFS(vp)->n_lock, ap->a_flags | LK_RELEASE, |
383 | &vp->v_interlock, ap->a_p)); | |
1c79356b A |
384 | } |
385 | ||
386 | /* | |
387 | * Check for a locked nfsnode | |
388 | */ | |
389 | int | |
390 | nfs_islocked(ap) | |
391 | struct vop_islocked_args /* { | |
392 | struct vnode *a_vp; | |
393 | } */ *ap; | |
394 | { | |
fa4905b1 A |
395 | return (lockstatus(&VTONFS(ap->a_vp)->n_lock)); |
396 | ||
1c79356b | 397 | } |
fa4905b1 | 398 | |
1c79356b A |
399 | |
400 | /* | |
401 | * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually | |
402 | * done. Currently nothing to do. | |
403 | */ | |
404 | /* ARGSUSED */ | |
405 | int | |
406 | nfs_abortop(ap) | |
407 | struct vop_abortop_args /* { | |
408 | struct vnode *a_dvp; | |
409 | struct componentname *a_cnp; | |
410 | } */ *ap; | |
411 | { | |
412 | ||
413 | if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) | |
414 | FREE_ZONE(ap->a_cnp->cn_pnbuf, ap->a_cnp->cn_pnlen, M_NAMEI); | |
415 | return (0); | |
416 | } |