2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
65 * FreeBSD-Id: nfs_node.c,v 1.22 1997/10/28 14:06:20 bde Exp $
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/kauth.h>
73 #include <sys/mount_internal.h>
74 #include <sys/vnode.h>
76 #include <sys/malloc.h>
78 #include <nfs/rpcv2.h>
79 #include <nfs/nfsproto.h>
81 #include <nfs/nfsnode.h>
82 #include <nfs/nfsmount.h>
84 LIST_HEAD(nfsnodehashhead
, nfsnode
) *nfsnodehashtbl
;
87 lck_grp_t
* nfs_node_hash_lck_grp
;
88 lck_grp_attr_t
* nfs_node_hash_lck_grp_attr
;
89 lck_attr_t
* nfs_node_hash_lck_attr
;
90 lck_mtx_t
*nfs_node_hash_mutex
;
93 * Initialize hash links for nfsnodes
94 * and build nfsnode free list.
99 nfsnodehashtbl
= hashinit(desiredvnodes
, M_NFSNODE
, &nfsnodehash
);
101 nfs_node_hash_lck_grp_attr
= lck_grp_attr_alloc_init();
102 nfs_node_hash_lck_grp
= lck_grp_alloc_init("nfs_node_hash", nfs_node_hash_lck_grp_attr
);
104 nfs_node_hash_lck_attr
= lck_attr_alloc_init();
106 nfs_node_hash_mutex
= lck_mtx_alloc_init(nfs_node_hash_lck_grp
, nfs_node_hash_lck_attr
);
110 * Compute an entry in the NFS hash table structure
113 nfs_hash(u_char
*fhp
, int fhsize
)
119 for (i
= 0; i
< fhsize
; i
++)
125 * Look up a vnode/nfsnode by file handle.
126 * Callers must check for mount points!!
127 * In all cases, a pointer to a
128 * nfsnode structure is returned.
134 struct componentname
*cnp
,
137 struct nfs_vattr
*nvap
,
140 struct nfsnode
**npp
)
143 struct nfsnodehashhead
*nhpp
;
147 struct vnode_fsparam vfsp
;
150 /* Check for unmount in progress */
151 if (!mntp
|| (mntp
->mnt_kern_flag
& MNTK_UNMOUNT
)) {
153 return (!mntp
? ENXIO
: EPERM
);
156 nhpp
= NFSNOHASH(nfs_hash(fhp
, fhsize
));
158 lck_mtx_lock(nfs_node_hash_mutex
);
159 for (np
= nhpp
->lh_first
; np
!= 0; np
= np
->n_hash
.le_next
) {
160 mp
= (np
->n_flag
& NINIT
) ? np
->n_mount
: vnode_mount(NFSTOV(np
));
161 if (mntp
!= mp
|| np
->n_fhsize
!= fhsize
||
162 bcmp(fhp
, np
->n_fhp
, fhsize
))
164 /* if the node is still being initialized, sleep on it */
165 if (np
->n_flag
& NINIT
) {
166 np
->n_flag
|= NWINIT
;
167 msleep(np
, nfs_node_hash_mutex
, PDROP
| PINOD
, "nfs_nget", 0);
172 lck_mtx_unlock(nfs_node_hash_mutex
);
173 if ((error
= vnode_getwithvid(vp
, vid
))) {
175 * If vnode is being reclaimed or has already
176 * changed identity, no need to wait.
180 /* update attributes */
181 error
= nfs_loadattrcache(np
, nvap
, xidp
, 0);
185 if (dvp
&& cnp
&& (flags
& NG_MAKEENTRY
))
186 cache_enter(dvp
, vp
, cnp
);
193 * allocate and initialize nfsnode and stick it in the hash
194 * before calling getnewvnode(). Anyone finding it in the
195 * hash before initialization is complete will wait for it.
197 MALLOC_ZONE(np
, struct nfsnode
*, sizeof *np
, M_NFSNODE
, M_WAITOK
);
199 lck_mtx_unlock(nfs_node_hash_mutex
);
203 bzero((caddr_t
)np
, sizeof *np
);
207 /* setup node's file handle */
208 if (fhsize
> NFS_SMALLFH
) {
209 MALLOC_ZONE(np
->n_fhp
, u_char
*,
210 fhsize
, M_NFSBIGFH
, M_WAITOK
);
212 lck_mtx_unlock(nfs_node_hash_mutex
);
213 FREE_ZONE(np
, sizeof *np
, M_NFSNODE
);
218 np
->n_fhp
= &np
->n_fh
[0];
220 bcopy(fhp
, np
->n_fhp
, fhsize
);
221 np
->n_fhsize
= fhsize
;
223 /* Insert the nfsnode in the hash queue for its new file handle */
224 np
->n_flag
|= NHASHED
;
225 LIST_INSERT_HEAD(nhpp
, np
, n_hash
);
227 /* release lock on hash table */
228 lck_mtx_unlock(nfs_node_hash_mutex
);
230 /* do initial loading of attributes */
231 error
= nfs_loadattrcache(np
, nvap
, xidp
, 1);
233 lck_mtx_lock(nfs_node_hash_mutex
);
234 LIST_REMOVE(np
, n_hash
);
235 np
->n_flag
&= ~(NHASHED
|NINIT
);
236 if (np
->n_flag
& NWINIT
) {
237 np
->n_flag
&= ~NWINIT
;
240 lck_mtx_unlock(nfs_node_hash_mutex
);
241 if (np
->n_fhsize
> NFS_SMALLFH
)
242 FREE_ZONE(np
->n_fhp
, np
->n_fhsize
, M_NFSBIGFH
);
243 FREE_ZONE(np
, sizeof *np
, M_NFSNODE
);
247 np
->n_mtime
= nvap
->nva_mtime
;
248 if (nvap
->nva_type
== VDIR
)
249 np
->n_ncmtime
= nvap
->nva_mtime
;
252 /* now, attempt to get a new vnode */
254 vfsp
.vnfs_vtype
= nvap
->nva_type
;
255 vfsp
.vnfs_str
= "nfs";
257 vfsp
.vnfs_fsnode
= np
;
258 if (nvap
->nva_type
== VFIFO
)
259 vfsp
.vnfs_vops
= fifo_nfsv2nodeop_p
;
260 else if (nvap
->nva_type
== VBLK
|| nvap
->nva_type
== VCHR
)
261 vfsp
.vnfs_vops
= spec_nfsv2nodeop_p
;
263 vfsp
.vnfs_vops
= nfsv2_vnodeop_p
;
264 vfsp
.vnfs_markroot
= (flags
& NG_MARKROOT
) ? 1 : 0;
265 vfsp
.vnfs_marksystem
= 0;
267 vfsp
.vnfs_filesize
= nvap
->nva_size
;
269 if (dvp
&& cnp
&& (flags
& NG_MAKEENTRY
))
272 vfsp
.vnfs_flags
= VNFS_NOCACHE
;
273 error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &nvp
);
275 lck_mtx_lock(nfs_node_hash_mutex
);
276 LIST_REMOVE(np
, n_hash
);
277 np
->n_flag
&= ~(NHASHED
|NINIT
);
278 if (np
->n_flag
& NWINIT
) {
279 np
->n_flag
&= ~NWINIT
;
282 lck_mtx_unlock(nfs_node_hash_mutex
);
283 if (np
->n_fhsize
> NFS_SMALLFH
)
284 FREE_ZONE(np
->n_fhp
, np
->n_fhsize
, M_NFSBIGFH
);
285 FREE_ZONE(np
, sizeof *np
, M_NFSNODE
);
292 vnode_settag(vp
, VT_NFS
); // XXX shouldn't this be a vnode_create() parameter?
294 /* node is now initialized */
296 /* check if anyone's waiting on this node */
297 lck_mtx_lock(nfs_node_hash_mutex
);
298 np
->n_flag
&= ~NINIT
;
299 if (np
->n_flag
& NWINIT
) {
300 np
->n_flag
&= ~NWINIT
;
303 lck_mtx_unlock(nfs_node_hash_mutex
);
311 struct vnop_inactive_args
/* {
312 struct vnodeop_desc *a_desc;
314 vfs_context_t a_context;
317 register struct nfsnode
*np
;
318 register struct sillyrename
*sp
;
320 np
= VTONFS(ap
->a_vp
);
321 if (vnode_vtype(ap
->a_vp
) != VDIR
) {
322 sp
= np
->n_sillyrename
;
323 np
->n_sillyrename
= (struct sillyrename
*)0;
325 sp
= (struct sillyrename
*)0;
329 * Remove the silly file that was rename'd earlier
332 kprintf("nfs_inactive removing %s, dvp=%x, a_vp=%x, ap=%x, np=%x, sp=%x\n",
333 &sp
->s_name
[0], (unsigned)sp
->s_dvp
, (unsigned)ap
->a_vp
, (unsigned)ap
,
334 (unsigned)np
, (unsigned)sp
);
336 nfs_vinvalbuf(ap
->a_vp
, 0, sp
->s_cred
, vfs_context_proc(ap
->a_context
), 1);
338 ubc_setsize(ap
->a_vp
, (off_t
)0);
341 * remove nfsnode from hash now so we can't accidentally find it
342 * again if another object gets created with the same filehandle
343 * before this vnode gets reclaimed
345 lck_mtx_lock(nfs_node_hash_mutex
);
346 LIST_REMOVE(np
, n_hash
);
347 np
->n_flag
&= ~NHASHED
;
348 lck_mtx_unlock(nfs_node_hash_mutex
);
349 if (IS_VALID_CRED(sp
->s_cred
)) {
350 kauth_cred_unref(&sp
->s_cred
);
352 vnode_rele(sp
->s_dvp
);
353 FREE_ZONE((caddr_t
)sp
, sizeof (struct sillyrename
), M_NFSREQ
);
354 vnode_recycle(ap
->a_vp
);
356 /* clear all flags other than these */
357 np
->n_flag
&= (NMODIFIED
| NFLUSHINPROG
| NFLUSHWANT
| NHASHED
);
362 * Reclaim an nfsnode so that it can be used for other purposes.
366 struct vnop_reclaim_args
/* {
367 struct vnodeop_desc *a_desc;
369 vfs_context_t a_context;
372 vnode_t vp
= ap
->a_vp
;
373 struct nfsnode
*np
= VTONFS(vp
);
374 struct nfsdmap
*dp
, *dp2
;
376 vnode_removefsref(vp
);
378 if (np
->n_flag
& NHASHED
) {
379 lck_mtx_lock(nfs_node_hash_mutex
);
380 LIST_REMOVE(np
, n_hash
);
381 np
->n_flag
&= ~NHASHED
;
382 lck_mtx_unlock(nfs_node_hash_mutex
);
386 * Free up any directory cookie structures and
387 * large file handle structures that might be associated with
390 if (vnode_vtype(vp
) == VDIR
) {
391 dp
= np
->n_cookies
.lh_first
;
394 dp
= dp
->ndm_list
.le_next
;
395 FREE_ZONE((caddr_t
)dp2
,
396 sizeof (struct nfsdmap
), M_NFSDIROFF
);
399 if (np
->n_fhsize
> NFS_SMALLFH
) {
400 FREE_ZONE(np
->n_fhp
, np
->n_fhsize
, M_NFSBIGFH
);
402 vnode_clearfsnode(vp
);
404 FREE_ZONE(np
, sizeof(struct nfsnode
), M_NFSNODE
);