2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
29 * Rick Macklem at The University of Guelph.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
60 * FreeBSD-Id: nfs_node.c,v 1.22 1997/10/28 14:06:20 bde Exp $
64 #include <sys/param.h>
65 #include <sys/systm.h>
67 #include <sys/kauth.h>
68 #include <sys/mount_internal.h>
69 #include <sys/vnode.h>
71 #include <sys/malloc.h>
73 #include <nfs/rpcv2.h>
74 #include <nfs/nfsproto.h>
76 #include <nfs/nfsnode.h>
77 #include <nfs/nfsmount.h>
79 LIST_HEAD(nfsnodehashhead
, nfsnode
) *nfsnodehashtbl
;
82 lck_grp_t
* nfs_node_hash_lck_grp
;
83 lck_grp_attr_t
* nfs_node_hash_lck_grp_attr
;
84 lck_attr_t
* nfs_node_hash_lck_attr
;
85 lck_mtx_t
*nfs_node_hash_mutex
;
88 * Initialize hash links for nfsnodes
89 * and build nfsnode free list.
94 nfsnodehashtbl
= hashinit(desiredvnodes
, M_NFSNODE
, &nfsnodehash
);
96 nfs_node_hash_lck_grp_attr
= lck_grp_attr_alloc_init();
97 lck_grp_attr_setstat(nfs_node_hash_lck_grp_attr
);
98 nfs_node_hash_lck_grp
= lck_grp_alloc_init("nfs_node_hash", nfs_node_hash_lck_grp_attr
);
100 nfs_node_hash_lck_attr
= lck_attr_alloc_init();
102 nfs_node_hash_mutex
= lck_mtx_alloc_init(nfs_node_hash_lck_grp
, nfs_node_hash_lck_attr
);
106 * Compute an entry in the NFS hash table structure
109 nfs_hash(u_char
*fhp
, int fhsize
)
115 for (i
= 0; i
< fhsize
; i
++)
121 * Look up a vnode/nfsnode by file handle.
122 * Callers must check for mount points!!
123 * In all cases, a pointer to a
124 * nfsnode structure is returned.
130 struct componentname
*cnp
,
133 struct nfs_vattr
*nvap
,
136 struct nfsnode
**npp
)
139 struct nfsnodehashhead
*nhpp
;
143 struct vnode_fsparam vfsp
;
146 /* Check for unmount in progress */
147 if (!mntp
|| (mntp
->mnt_kern_flag
& MNTK_UNMOUNT
)) {
149 return (!mntp
? ENXIO
: EPERM
);
152 nhpp
= NFSNOHASH(nfs_hash(fhp
, fhsize
));
154 lck_mtx_lock(nfs_node_hash_mutex
);
155 for (np
= nhpp
->lh_first
; np
!= 0; np
= np
->n_hash
.le_next
) {
156 mp
= (np
->n_flag
& NINIT
) ? np
->n_mount
: vnode_mount(NFSTOV(np
));
157 if (mntp
!= mp
|| np
->n_fhsize
!= fhsize
||
158 bcmp(fhp
, np
->n_fhp
, fhsize
))
160 /* if the node is still being initialized, sleep on it */
161 if (np
->n_flag
& NINIT
) {
162 np
->n_flag
|= NWINIT
;
163 msleep(np
, nfs_node_hash_mutex
, PDROP
| PINOD
, "nfs_nget", 0);
168 lck_mtx_unlock(nfs_node_hash_mutex
);
169 if ((error
= vnode_getwithvid(vp
, vid
))) {
171 * If vnode is being reclaimed or has already
172 * changed identity, no need to wait.
176 /* update attributes */
177 error
= nfs_loadattrcache(np
, nvap
, xidp
, 0);
181 if (dvp
&& cnp
&& (flags
& NG_MAKEENTRY
))
182 cache_enter(dvp
, vp
, cnp
);
189 * allocate and initialize nfsnode and stick it in the hash
190 * before calling getnewvnode(). Anyone finding it in the
191 * hash before initialization is complete will wait for it.
193 MALLOC_ZONE(np
, struct nfsnode
*, sizeof *np
, M_NFSNODE
, M_WAITOK
);
195 lck_mtx_unlock(nfs_node_hash_mutex
);
199 bzero((caddr_t
)np
, sizeof *np
);
203 /* setup node's file handle */
204 if (fhsize
> NFS_SMALLFH
) {
205 MALLOC_ZONE(np
->n_fhp
, u_char
*,
206 fhsize
, M_NFSBIGFH
, M_WAITOK
);
208 lck_mtx_unlock(nfs_node_hash_mutex
);
209 FREE_ZONE(np
, sizeof *np
, M_NFSNODE
);
214 np
->n_fhp
= &np
->n_fh
[0];
216 bcopy(fhp
, np
->n_fhp
, fhsize
);
217 np
->n_fhsize
= fhsize
;
219 /* Insert the nfsnode in the hash queue for its new file handle */
220 np
->n_flag
|= NHASHED
;
221 LIST_INSERT_HEAD(nhpp
, np
, n_hash
);
223 /* release lock on hash table */
224 lck_mtx_unlock(nfs_node_hash_mutex
);
226 /* do initial loading of attributes */
227 error
= nfs_loadattrcache(np
, nvap
, xidp
, 1);
229 lck_mtx_lock(nfs_node_hash_mutex
);
230 LIST_REMOVE(np
, n_hash
);
231 np
->n_flag
&= ~(NHASHED
|NINIT
);
232 if (np
->n_flag
& NWINIT
) {
233 np
->n_flag
&= ~NWINIT
;
236 lck_mtx_unlock(nfs_node_hash_mutex
);
237 if (np
->n_fhsize
> NFS_SMALLFH
)
238 FREE_ZONE(np
->n_fhp
, np
->n_fhsize
, M_NFSBIGFH
);
239 FREE_ZONE(np
, sizeof *np
, M_NFSNODE
);
243 np
->n_mtime
= nvap
->nva_mtime
;
244 if (nvap
->nva_type
== VDIR
)
245 np
->n_ncmtime
= nvap
->nva_mtime
;
248 /* now, attempt to get a new vnode */
250 vfsp
.vnfs_vtype
= nvap
->nva_type
;
251 vfsp
.vnfs_str
= "nfs";
253 vfsp
.vnfs_fsnode
= np
;
254 if (nvap
->nva_type
== VFIFO
)
255 vfsp
.vnfs_vops
= fifo_nfsv2nodeop_p
;
256 else if (nvap
->nva_type
== VBLK
|| nvap
->nva_type
== VCHR
)
257 vfsp
.vnfs_vops
= spec_nfsv2nodeop_p
;
259 vfsp
.vnfs_vops
= nfsv2_vnodeop_p
;
260 vfsp
.vnfs_markroot
= (flags
& NG_MARKROOT
) ? 1 : 0;
261 vfsp
.vnfs_marksystem
= 0;
263 vfsp
.vnfs_filesize
= nvap
->nva_size
;
265 if (dvp
&& cnp
&& (flags
& NG_MAKEENTRY
))
268 vfsp
.vnfs_flags
= VNFS_NOCACHE
;
269 error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &nvp
);
271 lck_mtx_lock(nfs_node_hash_mutex
);
272 LIST_REMOVE(np
, n_hash
);
273 np
->n_flag
&= ~(NHASHED
|NINIT
);
274 if (np
->n_flag
& NWINIT
) {
275 np
->n_flag
&= ~NWINIT
;
278 lck_mtx_unlock(nfs_node_hash_mutex
);
279 if (np
->n_fhsize
> NFS_SMALLFH
)
280 FREE_ZONE(np
->n_fhp
, np
->n_fhsize
, M_NFSBIGFH
);
281 FREE_ZONE(np
, sizeof *np
, M_NFSNODE
);
288 vnode_settag(vp
, VT_NFS
); // XXX shouldn't this be a vnode_create() parameter?
290 /* node is now initialized */
292 /* check if anyone's waiting on this node */
293 lck_mtx_lock(nfs_node_hash_mutex
);
294 np
->n_flag
&= ~NINIT
;
295 if (np
->n_flag
& NWINIT
) {
296 np
->n_flag
&= ~NWINIT
;
299 lck_mtx_unlock(nfs_node_hash_mutex
);
307 struct vnop_inactive_args
/* {
308 struct vnodeop_desc *a_desc;
310 vfs_context_t a_context;
313 register struct nfsnode
*np
;
314 register struct sillyrename
*sp
;
317 np
= VTONFS(ap
->a_vp
);
318 if (vnode_vtype(ap
->a_vp
) != VDIR
) {
319 sp
= np
->n_sillyrename
;
320 np
->n_sillyrename
= (struct sillyrename
*)0;
322 sp
= (struct sillyrename
*)0;
326 * Remove the silly file that was rename'd earlier
329 kprintf("nfs_inactive removing %s, dvp=%x, a_vp=%x, ap=%x, np=%x, sp=%x\n",
330 &sp
->s_name
[0], (unsigned)sp
->s_dvp
, (unsigned)ap
->a_vp
, (unsigned)ap
,
331 (unsigned)np
, (unsigned)sp
);
333 nfs_vinvalbuf(ap
->a_vp
, 0, sp
->s_cred
, vfs_context_proc(ap
->a_context
), 1);
335 ubc_setsize(ap
->a_vp
, (off_t
)0);
338 * remove nfsnode from hash now so we can't accidentally find it
339 * again if another object gets created with the same filehandle
340 * before this vnode gets reclaimed
342 lck_mtx_lock(nfs_node_hash_mutex
);
343 LIST_REMOVE(np
, n_hash
);
344 np
->n_flag
&= ~NHASHED
;
345 lck_mtx_unlock(nfs_node_hash_mutex
);
347 if (cred
!= NOCRED
) {
349 kauth_cred_rele(cred
);
351 vnode_rele(sp
->s_dvp
);
352 FREE_ZONE((caddr_t
)sp
, sizeof (struct sillyrename
), M_NFSREQ
);
353 vnode_recycle(ap
->a_vp
);
355 /* clear all flags other than these */
356 np
->n_flag
&= (NMODIFIED
| NFLUSHINPROG
| NFLUSHWANT
| NHASHED
);
361 * Reclaim an nfsnode so that it can be used for other purposes.
365 struct vnop_reclaim_args
/* {
366 struct vnodeop_desc *a_desc;
368 vfs_context_t a_context;
371 vnode_t vp
= ap
->a_vp
;
372 struct nfsnode
*np
= VTONFS(vp
);
373 struct nfsdmap
*dp
, *dp2
;
375 vnode_removefsref(vp
);
377 if (np
->n_flag
& NHASHED
) {
378 lck_mtx_lock(nfs_node_hash_mutex
);
379 LIST_REMOVE(np
, n_hash
);
380 np
->n_flag
&= ~NHASHED
;
381 lck_mtx_unlock(nfs_node_hash_mutex
);
385 * Free up any directory cookie structures and
386 * large file handle structures that might be associated with
389 if (vnode_vtype(vp
) == VDIR
) {
390 dp
= np
->n_cookies
.lh_first
;
393 dp
= dp
->ndm_list
.le_next
;
394 FREE_ZONE((caddr_t
)dp2
,
395 sizeof (struct nfsdmap
), M_NFSDIROFF
);
398 if (np
->n_fhsize
> NFS_SMALLFH
) {
399 FREE_ZONE(np
->n_fhp
, np
->n_fhsize
, M_NFSBIGFH
);
401 vnode_clearfsnode(vp
);
403 FREE_ZONE(np
, sizeof(struct nfsnode
), M_NFSNODE
);