]>
git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_node.c
8a9c0835b70db04aea713cd0725a8b8168689bb6
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * Rick Macklem at The University of Guelph.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
62 * FreeBSD-Id: nfs_node.c,v 1.22 1997/10/28 14:06:20 bde Exp $
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/mount.h>
70 #include <sys/namei.h>
71 #include <sys/vnode.h>
72 #include <sys/malloc.h>
74 #include <nfs/rpcv2.h>
75 #include <nfs/nfsproto.h>
77 #include <nfs/nfsnode.h>
78 #include <nfs/nfsmount.h>
81 static MALLOC_DEFINE(M_NFSNODE
, "NFS node", "NFS vnode private part");
84 LIST_HEAD(nfsnodehashhead
, nfsnode
) *nfsnodehashtbl
;
91 * Initialize hash links for nfsnodes
92 * and build nfsnode free list.
97 nfsnodehashtbl
= hashinit(desiredvnodes
, M_NFSNODE
, &nfsnodehash
);
101 * Compute an entry in the NFS hash table structure
104 nfs_hash(fhp
, fhsize
)
105 register nfsfh_t
*fhp
;
108 register u_char
*fhpp
;
109 register u_long fhsum
;
112 fhpp
= &fhp
->fh_bytes
[0];
114 for (i
= 0; i
< fhsize
; i
++)
120 * Look up a vnode/nfsnode by file handle.
121 * Callers must check for mount points!!
122 * In all cases, a pointer to a
123 * nfsnode structure is returned.
125 int nfs_node_hash_lock
;
128 nfs_nget(mntp
, fhp
, fhsize
, npp
)
130 register nfsfh_t
*fhp
;
132 struct nfsnode
**npp
;
134 struct proc
*p
= current_proc(); /* XXX */
136 struct nfsnodehashhead
*nhpp
;
137 register struct vnode
*vp
;
141 /* Check for unmount in progress */
142 if (mntp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
147 nhpp
= NFSNOHASH(nfs_hash(fhp
, fhsize
));
149 for (np
= nhpp
->lh_first
; np
!= 0; np
= np
->n_hash
.le_next
) {
150 if (mntp
!= NFSTOV(np
)->v_mount
|| np
->n_fhsize
!= fhsize
||
151 bcmp((caddr_t
)fhp
, (caddr_t
)np
->n_fhp
, fhsize
))
154 if (vget(vp
, LK_EXCLUSIVE
, p
))
160 * Obtain a lock to prevent a race condition if the getnewvnode()
161 * or MALLOC() below happens to block.
163 if (nfs_node_hash_lock
) {
164 while (nfs_node_hash_lock
) {
165 nfs_node_hash_lock
= -1;
166 tsleep(&nfs_node_hash_lock
, PVM
, "nfsngt", 0);
170 nfs_node_hash_lock
= 1;
173 * Do the MALLOC before the getnewvnode since doing so afterward
174 * might cause a bogus v_data pointer to get dereferenced
175 * elsewhere if MALLOC should block.
177 MALLOC_ZONE(np
, struct nfsnode
*, sizeof *np
, M_NFSNODE
, M_WAITOK
);
179 error
= getnewvnode(VT_NFS
, mntp
, nfsv2_vnodeop_p
, &nvp
);
181 if (nfs_node_hash_lock
< 0)
182 wakeup(&nfs_node_hash_lock
);
183 nfs_node_hash_lock
= 0;
185 FREE_ZONE(np
, sizeof *np
, M_NFSNODE
);
189 bzero((caddr_t
)np
, sizeof *np
);
193 * Insert the nfsnode in the hash queue for its new file handle
195 LIST_INSERT_HEAD(nhpp
, np
, n_hash
);
196 if (fhsize
> NFS_SMALLFH
) {
197 MALLOC_ZONE(np
->n_fhp
, nfsfh_t
*,
198 fhsize
, M_NFSBIGFH
, M_WAITOK
);
200 np
->n_fhp
= &np
->n_fh
;
201 bcopy((caddr_t
)fhp
, (caddr_t
)np
->n_fhp
, fhsize
);
202 np
->n_fhsize
= fhsize
;
205 if (nfs_node_hash_lock
< 0)
206 wakeup(&nfs_node_hash_lock
);
207 nfs_node_hash_lock
= 0;
210 * Lock the new nfsnode.
212 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
219 struct vop_inactive_args
/* {
224 register struct nfsnode
*np
;
225 register struct sillyrename
*sp
;
226 struct proc
*p
= current_proc(); /* XXX */
227 extern int prtactive
;
230 np
= VTONFS(ap
->a_vp
);
231 if (prtactive
&& ap
->a_vp
->v_usecount
!= 0)
232 vprint("nfs_inactive: pushing active", ap
->a_vp
);
233 if (ap
->a_vp
->v_type
!= VDIR
) {
234 sp
= np
->n_sillyrename
;
235 np
->n_sillyrename
= (struct sillyrename
*)0;
237 sp
= (struct sillyrename
*)0;
241 * Remove the silly file that was rename'd earlier
244 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
);
247 * We get a reference (vget) to ensure getnewvnode()
248 * doesn't recycle vp while we're asleep awaiting I/O.
249 * Note we don't need the reference unless usecount is
250 * already zero. In the case of a forcible unmount it
251 * wont be zero and doing a vget would fail because
252 * vclean holds VXLOCK.
254 if (ap
->a_vp
->v_usecount
> 0) {
256 } else if (vget(ap
->a_vp
, 0, ap
->a_p
))
257 panic("nfs_inactive: vget failed");
258 (void) nfs_vinvalbuf(ap
->a_vp
, 0, sp
->s_cred
, p
, 1);
260 ubc_setsize(ap
->a_vp
, (off_t
)0);
262 /* We have a problem. The dvp could have gone away on us while
263 * in the unmount path. Thus it appears as VBAD and we cannot
264 * use it. If we tried locking the parent (future), for silly
265 * rename files, it is unclear where we would lock. The unmount
266 * code just pulls unlocked vnodes as it goes thru its list and
267 * yanks them. Could unmount be smarter to see if a busy reg vnode has
268 * a parent, and not yank it yet? Put in more passes at unmount
269 * time? In the meantime, just check if it went away on us.
270 * Could have gone away during the nfs_vinvalbuf or ubc_setsize
271 * which block. Or perhaps even before nfs_inactive got called.
273 if ((sp
->s_dvp
)->v_type
!= VBAD
)
274 nfs_removeit(sp
); /* uses the dvp */
276 if (cred
!= NOCRED
) {
281 FREE_ZONE((caddr_t
)sp
, sizeof (struct sillyrename
), M_NFSREQ
);
284 np
->n_flag
&= (NMODIFIED
| NFLUSHINPROG
| NFLUSHWANT
| NQNFSEVICTED
|
285 NQNFSNONCACHE
| NQNFSWRITE
);
286 VOP_UNLOCK(ap
->a_vp
, 0, ap
->a_p
);
291 * Reclaim an nfsnode so that it can be used for other purposes.
295 struct vop_reclaim_args
/* {
299 register struct vnode
*vp
= ap
->a_vp
;
300 register struct nfsnode
*np
= VTONFS(vp
);
301 register struct nfsmount
*nmp
= VFSTONFS(vp
->v_mount
);
302 register struct nfsdmap
*dp
, *dp2
;
303 extern int prtactive
;
305 if (prtactive
&& vp
->v_usecount
!= 0)
306 vprint("nfs_reclaim: pushing active", vp
);
308 LIST_REMOVE(np
, n_hash
);
311 * In case we block during FREE_ZONEs below, get the entry out
312 * of tbe name cache now so subsequent lookups won't find it.
317 * For nqnfs, take it off the timer queue as required.
319 if ((nmp
->nm_flag
& NFSMNT_NQNFS
) && np
->n_timer
.cqe_next
!= 0) {
320 CIRCLEQ_REMOVE(&nmp
->nm_timerhead
, np
, n_timer
);
324 * Free up any directory cookie structures and
325 * large file handle structures that might be associated with
328 if (vp
->v_type
== VDIR
) {
329 dp
= np
->n_cookies
.lh_first
;
332 dp
= dp
->ndm_list
.le_next
;
333 FREE_ZONE((caddr_t
)dp2
,
334 sizeof (struct nfsdmap
), M_NFSDIROFF
);
337 if (np
->n_fhsize
> NFS_SMALLFH
) {
338 FREE_ZONE((caddr_t
)np
->n_fhp
, np
->n_fhsize
, M_NFSBIGFH
);
341 FREE_ZONE(vp
->v_data
, sizeof (struct nfsnode
), M_NFSNODE
);
342 vp
->v_data
= (void *)0;
351 struct vop_lock_args
/* {
357 register struct vnode
*vp
= ap
->a_vp
;
360 * Ugh, another place where interruptible mounts will get hung.
361 * If you make this call interruptible, then you have to fix all
362 * the VOP_LOCK() calls to expect interruptibility.
364 if (vp
->v_tag
== VT_NON
)
365 return (ENOENT
); /* ??? -- got to check something and error, but what? */
367 return(lockmgr(&VTONFS(vp
)->n_lock
, ap
->a_flags
, &vp
->v_interlock
,
377 struct vop_unlock_args
/* {
383 struct vnode
*vp
= ap
->a_vp
;
385 return (lockmgr(&VTONFS(vp
)->n_lock
, ap
->a_flags
| LK_RELEASE
,
386 &vp
->v_interlock
, ap
->a_p
));
390 * Check for a locked nfsnode
394 struct vop_islocked_args
/* {
398 return (lockstatus(&VTONFS(ap
->a_vp
)->n_lock
));
404 * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
405 * done. Currently nothing to do.
410 struct vop_abortop_args
/* {
412 struct componentname *a_cnp;
416 if ((ap
->a_cnp
->cn_flags
& (HASBUF
| SAVESTART
)) == HASBUF
)
417 FREE_ZONE(ap
->a_cnp
->cn_pnbuf
, ap
->a_cnp
->cn_pnlen
, M_NAMEI
);