]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_node.c
xnu-201.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_node.c
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 */
209 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
210
211 return (0);
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);
256 ubc_setsize(ap->a_vp, (off_t)0);
257
258 /* We have a problem. The dvp could have gone away on us
259 * while in the unmount path. Thus it appears as VBAD and we
260 * cannot use it. If we tried locking the parent (future), for silly
261 * rename files, it is unclear where we would lock. The unmount
262 * code just pulls unlocked vnodes as it goes thru its list and
263 * yanks them. Could unmount be smarter to see if a busy reg vnode has
264 * a parent, and not yank it yet? Put in more passes at unmount
265 * time? In the meantime, just check if it went away on us. Could
266 * have gone away during the nfs_vinvalbuf or ubc_setsize which block.
267 * Or perhaps even before nfs_inactive got called.
268 */
269 if ((sp->s_dvp)->v_type != VBAD)
270 nfs_removeit(sp); /* uses the dvp */
271 cred = sp->s_cred;
272 if (cred != NOCRED) {
273 sp->s_cred = NOCRED;
274 crfree(cred);
275 }
276 vrele(sp->s_dvp);
277 FREE_ZONE((caddr_t)sp, sizeof (struct sillyrename), M_NFSREQ);
278 vrele(ap->a_vp);
279 }
280 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
281 NQNFSNONCACHE | NQNFSWRITE);
282 VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
283 return (0);
284 }
285
286 /*
287 * Reclaim an nfsnode so that it can be used for other purposes.
288 */
289 int
290 nfs_reclaim(ap)
291 struct vop_reclaim_args /* {
292 struct vnode *a_vp;
293 } */ *ap;
294 {
295 register struct vnode *vp = ap->a_vp;
296 register struct nfsnode *np = VTONFS(vp);
297 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
298 register struct nfsdmap *dp, *dp2;
299 extern int prtactive;
300
301 if (prtactive && vp->v_usecount != 0)
302 vprint("nfs_reclaim: pushing active", vp);
303
304 LIST_REMOVE(np, n_hash);
305
306 /*
307 * In case we block during FREE_ZONEs below, get the entry out
308 * of tbe name cache now so subsequent lookups won't find it.
309 */
310 cache_purge(vp);
311
312 /*
313 * For nqnfs, take it off the timer queue as required.
314 */
315 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
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
342 #if 0
343 /*
344 * Lock an nfsnode
345 */
346 int
347 nfs_lock(ap)
348 struct vop_lock_args /* {
349 struct vnode *a_vp;
350 } */ *ap;
351 {
352 register struct vnode *vp = ap->a_vp;
353
354 /*
355 * Ugh, another place where interruptible mounts will get hung.
356 * If you make this sleep interruptible, then you have to fix all
357 * the VOP_LOCK() calls to expect interruptibility.
358 */
359 while (vp->v_flag & VXLOCK) {
360 vp->v_flag |= VXWANT;
361 (void) tsleep((caddr_t)vp, PINOD, "nfslck", 0);
362 }
363 if (vp->v_tag == VT_NON)
364 return (ENOENT);
365
366 #if 0
367 /*
368 * Only lock regular files. If a server crashed while we were
369 * holding a directory lock, we could easily end up sleeping
370 * until the server rebooted while holding a lock on the root.
371 * Locks are only needed for protecting critical sections in
372 * VMIO at the moment.
373 * New vnodes will have type VNON but they should be locked
374 * since they may become VREG. This is checked in loadattrcache
375 * and unwanted locks are released there.
376 */
377 if (vp->v_type == VREG || vp->v_type == VNON) {
378 while (np->n_flag & NLOCKED) {
379 np->n_flag |= NWANTED;
380 (void) tsleep((caddr_t) np, PINOD, "nfslck2", 0);
381 /*
382 * If the vnode has transmuted into a VDIR while we
383 * were asleep, then skip the lock.
384 */
385 if (vp->v_type != VREG && vp->v_type != VNON)
386 return (0);
387 }
388 np->n_flag |= NLOCKED;
389 }
390 #endif
391
392 return (0);
393 }
394
395 /*
396 * Unlock an nfsnode
397 */
398 int
399 nfs_unlock(ap)
400 struct vop_unlock_args /* {
401 struct vnode *a_vp;
402 } */ *ap;
403 {
404 #if 0
405 struct vnode* vp = ap->a_vp;
406 struct nfsnode* np = VTONFS(vp);
407
408 if (vp->v_type == VREG || vp->v_type == VNON) {
409 if (!(np->n_flag & NLOCKED))
410 panic("nfs_unlock: nfsnode not locked");
411 np->n_flag &= ~NLOCKED;
412 if (np->n_flag & NWANTED) {
413 np->n_flag &= ~NWANTED;
414 wakeup((caddr_t) np);
415 }
416 }
417 #endif
418
419 return (0);
420 }
421
422 /*
423 * Check for a locked nfsnode
424 */
425 int
426 nfs_islocked(ap)
427 struct vop_islocked_args /* {
428 struct vnode *a_vp;
429 } */ *ap;
430 {
431 return VTONFS(ap->a_vp)->n_flag & NLOCKED ? 1 : 0;
432 }
433 #endif
434
435 /*
436 * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
437 * done. Currently nothing to do.
438 */
439 /* ARGSUSED */
440 int
441 nfs_abortop(ap)
442 struct vop_abortop_args /* {
443 struct vnode *a_dvp;
444 struct componentname *a_cnp;
445 } */ *ap;
446 {
447
448 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
449 FREE_ZONE(ap->a_cnp->cn_pnbuf, ap->a_cnp->cn_pnlen, M_NAMEI);
450 return (0);
451 }