]>
git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_node.c
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
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 $
63 #include <sys/param.h>
64 #include <sys/systm.h>
66 #include <sys/mount.h>
67 #include <sys/namei.h>
68 #include <sys/vnode.h>
69 #include <sys/malloc.h>
71 #include <nfs/rpcv2.h>
72 #include <nfs/nfsproto.h>
74 #include <nfs/nfsnode.h>
75 #include <nfs/nfsmount.h>
77 LIST_HEAD(nfsnodehashhead
, nfsnode
) *nfsnodehashtbl
;
84 * Initialize hash links for nfsnodes
85 * and build nfsnode free list.
90 nfsnodehashtbl
= hashinit(desiredvnodes
, M_NFSNODE
, &nfsnodehash
);
94 * Compute an entry in the NFS hash table structure
98 register nfsfh_t
*fhp
;
101 register u_char
*fhpp
;
102 register u_long fhsum
;
105 fhpp
= &fhp
->fh_bytes
[0];
107 for (i
= 0; i
< fhsize
; i
++)
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.
118 int nfs_node_hash_lock
;
121 nfs_nget(mntp
, fhp
, fhsize
, npp
)
123 register nfsfh_t
*fhp
;
125 struct nfsnode
**npp
;
127 struct proc
*p
= current_proc(); /* XXX */
129 struct nfsnodehashhead
*nhpp
;
130 register struct vnode
*vp
;
135 /* Check for unmount in progress */
136 if (!mntp
|| (mntp
->mnt_kern_flag
& MNTK_UNMOUNT
)) {
138 return (!mntp
? ENXIO
: EPERM
);
141 nhpp
= NFSNOHASH(nfs_hash(fhp
, fhsize
));
143 for (np
= nhpp
->lh_first
; np
!= 0; np
= np
->n_hash
.le_next
) {
144 mp
= (np
->n_flag
& NINIT
) ? np
->n_mount
: NFSTOV(np
)->v_mount
;
145 if (mntp
!= mp
|| np
->n_fhsize
!= fhsize
||
146 bcmp((caddr_t
)fhp
, (caddr_t
)np
->n_fhp
, fhsize
))
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);
155 if (vget(vp
, LK_EXCLUSIVE
, p
))
161 * Obtain a lock to prevent a race condition if the getnewvnode()
162 * or MALLOC() below happens to block.
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);
171 nfs_node_hash_lock
= 1;
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.
178 MALLOC_ZONE(np
, struct nfsnode
*, sizeof *np
, M_NFSNODE
, M_WAITOK
);
179 bzero((caddr_t
)np
, sizeof *np
);
182 lockinit(&np
->n_lock
, PINOD
, "nfsnode", 0, 0);
183 /* lock the new nfsnode */
184 lockmgr(&np
->n_lock
, LK_EXCLUSIVE
, NULL
, p
);
186 /* Insert the nfsnode in the hash queue for its new file handle */
187 if (fhsize
> NFS_SMALLFH
) {
188 MALLOC_ZONE(np
->n_fhp
, nfsfh_t
*,
189 fhsize
, M_NFSBIGFH
, M_WAITOK
);
191 np
->n_fhp
= &np
->n_fh
;
192 bcopy((caddr_t
)fhp
, (caddr_t
)np
->n_fhp
, fhsize
);
193 np
->n_fhsize
= fhsize
;
194 LIST_INSERT_HEAD(nhpp
, np
, n_hash
);
195 np
->n_flag
|= NHASHED
;
197 /* release lock on hash table */
198 if (nfs_node_hash_lock
< 0)
199 wakeup(&nfs_node_hash_lock
);
200 nfs_node_hash_lock
= 0;
202 /* now, attempt to get a new vnode */
203 error
= getnewvnode(VT_NFS
, mntp
, nfsv2_vnodeop_p
, &nvp
);
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
);
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
;
230 struct vop_inactive_args
/* {
235 register struct nfsnode
*np
;
236 register struct sillyrename
*sp
;
237 struct proc
*p
= current_proc(); /* XXX */
238 extern int prtactive
;
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;
248 sp
= (struct sillyrename
*)0;
252 * Remove the silly file that was rename'd earlier
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
);
257 (void) nfs_vinvalbuf(ap
->a_vp
, 0, sp
->s_cred
, p
, 1);
259 ubc_setsize(ap
->a_vp
, (off_t
)0);
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
266 LIST_REMOVE(np
, n_hash
);
267 np
->n_flag
&= ~NHASHED
;
269 if (cred
!= NOCRED
) {
274 FREE_ZONE((caddr_t
)sp
, sizeof (struct sillyrename
), M_NFSREQ
);
276 np
->n_flag
&= (NMODIFIED
| NFLUSHINPROG
| NFLUSHWANT
| NQNFSEVICTED
|
277 NQNFSNONCACHE
| NQNFSWRITE
| NHASHED
);
278 VOP_UNLOCK(ap
->a_vp
, 0, ap
->a_p
);
283 * Reclaim an nfsnode so that it can be used for other purposes.
287 struct vop_reclaim_args
/* {
291 register struct vnode
*vp
= ap
->a_vp
;
292 register struct nfsnode
*np
= VTONFS(vp
);
293 register struct nfsmount
*nmp
;
294 register struct nfsdmap
*dp
, *dp2
;
295 extern int prtactive
;
297 if (prtactive
&& vp
->v_usecount
!= 0)
298 vprint("nfs_reclaim: pushing active", vp
);
300 if (np
->n_flag
& NHASHED
) {
301 LIST_REMOVE(np
, n_hash
);
302 np
->n_flag
&= ~NHASHED
;
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.
312 * For nqnfs, take it off the timer queue as required.
314 nmp
= VFSTONFS(vp
->v_mount
);
315 if (nmp
&& (nmp
->nm_flag
& NFSMNT_NQNFS
) && np
->n_timer
.cqe_next
!= 0) {
316 CIRCLEQ_REMOVE(&nmp
->nm_timerhead
, np
, n_timer
);
320 * Free up any directory cookie structures and
321 * large file handle structures that might be associated with
324 if (vp
->v_type
== VDIR
) {
325 dp
= np
->n_cookies
.lh_first
;
328 dp
= dp
->ndm_list
.le_next
;
329 FREE_ZONE((caddr_t
)dp2
,
330 sizeof (struct nfsdmap
), M_NFSDIROFF
);
333 if (np
->n_fhsize
> NFS_SMALLFH
) {
334 FREE_ZONE((caddr_t
)np
->n_fhp
, np
->n_fhsize
, M_NFSBIGFH
);
337 FREE_ZONE(vp
->v_data
, sizeof (struct nfsnode
), M_NFSNODE
);
338 vp
->v_data
= (void *)0;
347 struct vop_lock_args
/* {
353 register struct vnode
*vp
= ap
->a_vp
;
356 * Ugh, another place where interruptible mounts will get hung.
357 * If you make this call interruptible, then you have to fix all
358 * the VOP_LOCK() calls to expect interruptibility.
360 if (vp
->v_tag
== VT_NON
)
361 return (ENOENT
); /* ??? -- got to check something and error, but what? */
363 return(lockmgr(&VTONFS(vp
)->n_lock
, ap
->a_flags
, &vp
->v_interlock
,
373 struct vop_unlock_args
/* {
379 struct vnode
*vp
= ap
->a_vp
;
381 return (lockmgr(&VTONFS(vp
)->n_lock
, ap
->a_flags
| LK_RELEASE
,
382 &vp
->v_interlock
, ap
->a_p
));
386 * Check for a locked nfsnode
390 struct vop_islocked_args
/* {
394 return (lockstatus(&VTONFS(ap
->a_vp
)->n_lock
));