]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved. |
3 | * | |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 A |
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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1989, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * This code is derived from software contributed to Berkeley by | |
34 | * Rick Macklem at The University of Guelph. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
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. | |
51 | * | |
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 | |
62 | * SUCH DAMAGE. | |
63 | * | |
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 $ | |
66 | */ | |
67 | ||
68 | ||
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/proc.h> | |
91447636 A |
72 | #include <sys/kauth.h> |
73 | #include <sys/mount_internal.h> | |
1c79356b | 74 | #include <sys/vnode.h> |
91447636 | 75 | #include <sys/ubc.h> |
1c79356b A |
76 | #include <sys/malloc.h> |
77 | ||
78 | #include <nfs/rpcv2.h> | |
79 | #include <nfs/nfsproto.h> | |
80 | #include <nfs/nfs.h> | |
81 | #include <nfs/nfsnode.h> | |
82 | #include <nfs/nfsmount.h> | |
83 | ||
1c79356b A |
84 | LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl; |
85 | u_long nfsnodehash; | |
86 | ||
91447636 A |
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; | |
1c79356b A |
91 | |
92 | /* | |
93 | * Initialize hash links for nfsnodes | |
94 | * and build nfsnode free list. | |
95 | */ | |
96 | void | |
e5568f75 | 97 | nfs_nhinit(void) |
1c79356b A |
98 | { |
99 | nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash); | |
91447636 A |
100 | |
101 | nfs_node_hash_lck_grp_attr = lck_grp_attr_alloc_init(); | |
8f6c56a5 | 102 | lck_grp_attr_setstat(nfs_node_hash_lck_grp_attr); |
91447636 A |
103 | nfs_node_hash_lck_grp = lck_grp_alloc_init("nfs_node_hash", nfs_node_hash_lck_grp_attr); |
104 | ||
105 | nfs_node_hash_lck_attr = lck_attr_alloc_init(); | |
106 | ||
107 | nfs_node_hash_mutex = lck_mtx_alloc_init(nfs_node_hash_lck_grp, nfs_node_hash_lck_attr); | |
1c79356b A |
108 | } |
109 | ||
110 | /* | |
111 | * Compute an entry in the NFS hash table structure | |
112 | */ | |
113 | u_long | |
91447636 | 114 | nfs_hash(u_char *fhp, int fhsize) |
1c79356b | 115 | { |
91447636 A |
116 | u_long fhsum; |
117 | int i; | |
1c79356b | 118 | |
1c79356b A |
119 | fhsum = 0; |
120 | for (i = 0; i < fhsize; i++) | |
91447636 | 121 | fhsum += *fhp++; |
1c79356b A |
122 | return (fhsum); |
123 | } | |
124 | ||
125 | /* | |
126 | * Look up a vnode/nfsnode by file handle. | |
127 | * Callers must check for mount points!! | |
128 | * In all cases, a pointer to a | |
129 | * nfsnode structure is returned. | |
130 | */ | |
1c79356b | 131 | int |
91447636 A |
132 | nfs_nget( |
133 | mount_t mntp, | |
134 | vnode_t dvp, | |
135 | struct componentname *cnp, | |
136 | u_char *fhp, | |
137 | int fhsize, | |
138 | struct nfs_vattr *nvap, | |
139 | u_int64_t *xidp, | |
140 | int flags, | |
141 | struct nfsnode **npp) | |
1c79356b | 142 | { |
1c79356b A |
143 | struct nfsnode *np; |
144 | struct nfsnodehashhead *nhpp; | |
91447636 | 145 | vnode_t vp, nvp; |
1c79356b | 146 | int error; |
91447636 A |
147 | mount_t mp; |
148 | struct vnode_fsparam vfsp; | |
149 | uint32_t vid; | |
1c79356b A |
150 | |
151 | /* Check for unmount in progress */ | |
55e303ae | 152 | if (!mntp || (mntp->mnt_kern_flag & MNTK_UNMOUNT)) { |
1c79356b | 153 | *npp = 0; |
55e303ae | 154 | return (!mntp ? ENXIO : EPERM); |
1c79356b A |
155 | } |
156 | ||
157 | nhpp = NFSNOHASH(nfs_hash(fhp, fhsize)); | |
158 | loop: | |
91447636 | 159 | lck_mtx_lock(nfs_node_hash_mutex); |
1c79356b | 160 | for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) { |
91447636 | 161 | mp = (np->n_flag & NINIT) ? np->n_mount : vnode_mount(NFSTOV(np)); |
55e303ae | 162 | if (mntp != mp || np->n_fhsize != fhsize || |
91447636 | 163 | bcmp(fhp, np->n_fhp, fhsize)) |
1c79356b | 164 | continue; |
55e303ae A |
165 | /* if the node is still being initialized, sleep on it */ |
166 | if (np->n_flag & NINIT) { | |
167 | np->n_flag |= NWINIT; | |
91447636 | 168 | msleep(np, nfs_node_hash_mutex, PDROP | PINOD, "nfs_nget", 0); |
55e303ae A |
169 | goto loop; |
170 | } | |
1c79356b | 171 | vp = NFSTOV(np); |
91447636 A |
172 | vid = vnode_vid(vp); |
173 | lck_mtx_unlock(nfs_node_hash_mutex); | |
174 | if ((error = vnode_getwithvid(vp, vid))) { | |
175 | /* | |
176 | * If vnode is being reclaimed or has already | |
177 | * changed identity, no need to wait. | |
178 | */ | |
179 | return (error); | |
180 | } | |
181 | /* update attributes */ | |
182 | error = nfs_loadattrcache(np, nvap, xidp, 0); | |
183 | if (error) { | |
184 | vnode_put(vp); | |
185 | } else { | |
186 | if (dvp && cnp && (flags & NG_MAKEENTRY)) | |
187 | cache_enter(dvp, vp, cnp); | |
188 | *npp = np; | |
1c79356b | 189 | } |
91447636 | 190 | return(error); |
1c79356b | 191 | } |
1c79356b A |
192 | |
193 | /* | |
55e303ae A |
194 | * allocate and initialize nfsnode and stick it in the hash |
195 | * before calling getnewvnode(). Anyone finding it in the | |
196 | * hash before initialization is complete will wait for it. | |
1c79356b A |
197 | */ |
198 | MALLOC_ZONE(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK); | |
91447636 A |
199 | if (!np) { |
200 | lck_mtx_unlock(nfs_node_hash_mutex); | |
201 | *npp = 0; | |
202 | return (ENOMEM); | |
203 | } | |
1c79356b | 204 | bzero((caddr_t)np, sizeof *np); |
55e303ae A |
205 | np->n_flag |= NINIT; |
206 | np->n_mount = mntp; | |
55e303ae | 207 | |
91447636 | 208 | /* setup node's file handle */ |
1c79356b | 209 | if (fhsize > NFS_SMALLFH) { |
91447636 | 210 | MALLOC_ZONE(np->n_fhp, u_char *, |
1c79356b | 211 | fhsize, M_NFSBIGFH, M_WAITOK); |
91447636 A |
212 | if (!np->n_fhp) { |
213 | lck_mtx_unlock(nfs_node_hash_mutex); | |
214 | FREE_ZONE(np, sizeof *np, M_NFSNODE); | |
215 | *npp = 0; | |
216 | return (ENOMEM); | |
217 | } | |
218 | } else { | |
219 | np->n_fhp = &np->n_fh[0]; | |
220 | } | |
221 | bcopy(fhp, np->n_fhp, fhsize); | |
1c79356b | 222 | np->n_fhsize = fhsize; |
91447636 A |
223 | |
224 | /* Insert the nfsnode in the hash queue for its new file handle */ | |
55e303ae | 225 | np->n_flag |= NHASHED; |
91447636 | 226 | LIST_INSERT_HEAD(nhpp, np, n_hash); |
1c79356b | 227 | |
55e303ae | 228 | /* release lock on hash table */ |
91447636 A |
229 | lck_mtx_unlock(nfs_node_hash_mutex); |
230 | ||
231 | /* do initial loading of attributes */ | |
232 | error = nfs_loadattrcache(np, nvap, xidp, 1); | |
233 | if (error) { | |
234 | lck_mtx_lock(nfs_node_hash_mutex); | |
235 | LIST_REMOVE(np, n_hash); | |
236 | np->n_flag &= ~(NHASHED|NINIT); | |
237 | if (np->n_flag & NWINIT) { | |
238 | np->n_flag &= ~NWINIT; | |
239 | wakeup((caddr_t)np); | |
240 | } | |
241 | lck_mtx_unlock(nfs_node_hash_mutex); | |
242 | if (np->n_fhsize > NFS_SMALLFH) | |
243 | FREE_ZONE(np->n_fhp, np->n_fhsize, M_NFSBIGFH); | |
244 | FREE_ZONE(np, sizeof *np, M_NFSNODE); | |
245 | *npp = 0; | |
246 | return (error); | |
247 | } | |
248 | np->n_mtime = nvap->nva_mtime; | |
249 | if (nvap->nva_type == VDIR) | |
250 | np->n_ncmtime = nvap->nva_mtime; | |
251 | NMODEINVALIDATE(np); | |
1c79356b | 252 | |
55e303ae | 253 | /* now, attempt to get a new vnode */ |
91447636 A |
254 | vfsp.vnfs_mp = mntp; |
255 | vfsp.vnfs_vtype = nvap->nva_type; | |
256 | vfsp.vnfs_str = "nfs"; | |
257 | vfsp.vnfs_dvp = dvp; | |
258 | vfsp.vnfs_fsnode = np; | |
259 | if (nvap->nva_type == VFIFO) | |
260 | vfsp.vnfs_vops = fifo_nfsv2nodeop_p; | |
261 | else if (nvap->nva_type == VBLK || nvap->nva_type == VCHR) | |
262 | vfsp.vnfs_vops = spec_nfsv2nodeop_p; | |
263 | else | |
264 | vfsp.vnfs_vops = nfsv2_vnodeop_p; | |
265 | vfsp.vnfs_markroot = (flags & NG_MARKROOT) ? 1 : 0; | |
266 | vfsp.vnfs_marksystem = 0; | |
267 | vfsp.vnfs_rdev = 0; | |
268 | vfsp.vnfs_filesize = nvap->nva_size; | |
269 | vfsp.vnfs_cnp = cnp; | |
270 | if (dvp && cnp && (flags & NG_MAKEENTRY)) | |
271 | vfsp.vnfs_flags = 0; | |
272 | else | |
273 | vfsp.vnfs_flags = VNFS_NOCACHE; | |
274 | error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &nvp); | |
55e303ae | 275 | if (error) { |
91447636 | 276 | lck_mtx_lock(nfs_node_hash_mutex); |
55e303ae | 277 | LIST_REMOVE(np, n_hash); |
91447636 A |
278 | np->n_flag &= ~(NHASHED|NINIT); |
279 | if (np->n_flag & NWINIT) { | |
280 | np->n_flag &= ~NWINIT; | |
281 | wakeup((caddr_t)np); | |
282 | } | |
283 | lck_mtx_unlock(nfs_node_hash_mutex); | |
55e303ae | 284 | if (np->n_fhsize > NFS_SMALLFH) |
91447636 | 285 | FREE_ZONE(np->n_fhp, np->n_fhsize, M_NFSBIGFH); |
55e303ae A |
286 | FREE_ZONE(np, sizeof *np, M_NFSNODE); |
287 | *npp = 0; | |
288 | return (error); | |
289 | } | |
290 | vp = nvp; | |
55e303ae | 291 | np->n_vnode = vp; |
91447636 A |
292 | vnode_addfsref(vp); |
293 | vnode_settag(vp, VT_NFS); // XXX shouldn't this be a vnode_create() parameter? | |
55e303ae | 294 | *npp = np; |
91447636 | 295 | /* node is now initialized */ |
55e303ae | 296 | |
91447636 A |
297 | /* check if anyone's waiting on this node */ |
298 | lck_mtx_lock(nfs_node_hash_mutex); | |
55e303ae A |
299 | np->n_flag &= ~NINIT; |
300 | if (np->n_flag & NWINIT) { | |
301 | np->n_flag &= ~NWINIT; | |
302 | wakeup((caddr_t)np); | |
303 | } | |
91447636 | 304 | lck_mtx_unlock(nfs_node_hash_mutex); |
1c79356b | 305 | |
fa4905b1 | 306 | return (error); |
1c79356b A |
307 | } |
308 | ||
91447636 | 309 | |
1c79356b A |
310 | int |
311 | nfs_inactive(ap) | |
91447636 A |
312 | struct vnop_inactive_args /* { |
313 | struct vnodeop_desc *a_desc; | |
314 | vnode_t a_vp; | |
315 | vfs_context_t a_context; | |
1c79356b A |
316 | } */ *ap; |
317 | { | |
318 | register struct nfsnode *np; | |
319 | register struct sillyrename *sp; | |
91447636 | 320 | kauth_cred_t cred; |
1c79356b A |
321 | |
322 | np = VTONFS(ap->a_vp); | |
91447636 | 323 | if (vnode_vtype(ap->a_vp) != VDIR) { |
1c79356b A |
324 | sp = np->n_sillyrename; |
325 | np->n_sillyrename = (struct sillyrename *)0; | |
326 | } else | |
327 | sp = (struct sillyrename *)0; | |
328 | ||
329 | if (sp) { | |
330 | /* | |
331 | * Remove the silly file that was rename'd earlier | |
332 | */ | |
333 | #if DIAGNOSTIC | |
91447636 A |
334 | kprintf("nfs_inactive removing %s, dvp=%x, a_vp=%x, ap=%x, np=%x, sp=%x\n", |
335 | &sp->s_name[0], (unsigned)sp->s_dvp, (unsigned)ap->a_vp, (unsigned)ap, | |
336 | (unsigned)np, (unsigned)sp); | |
1c79356b | 337 | #endif |
91447636 | 338 | nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, vfs_context_proc(ap->a_context), 1); |
fa4905b1 | 339 | np->n_size = 0; |
1c79356b | 340 | ubc_setsize(ap->a_vp, (off_t)0); |
55e303ae A |
341 | nfs_removeit(sp); |
342 | /* | |
343 | * remove nfsnode from hash now so we can't accidentally find it | |
344 | * again if another object gets created with the same filehandle | |
345 | * before this vnode gets reclaimed | |
346 | */ | |
91447636 | 347 | lck_mtx_lock(nfs_node_hash_mutex); |
55e303ae A |
348 | LIST_REMOVE(np, n_hash); |
349 | np->n_flag &= ~NHASHED; | |
91447636 | 350 | lck_mtx_unlock(nfs_node_hash_mutex); |
1c79356b A |
351 | cred = sp->s_cred; |
352 | if (cred != NOCRED) { | |
353 | sp->s_cred = NOCRED; | |
91447636 | 354 | kauth_cred_rele(cred); |
1c79356b | 355 | } |
91447636 | 356 | vnode_rele(sp->s_dvp); |
1c79356b | 357 | FREE_ZONE((caddr_t)sp, sizeof (struct sillyrename), M_NFSREQ); |
91447636 | 358 | vnode_recycle(ap->a_vp); |
1c79356b | 359 | } |
91447636 A |
360 | /* clear all flags other than these */ |
361 | np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NHASHED); | |
1c79356b A |
362 | return (0); |
363 | } | |
364 | ||
365 | /* | |
366 | * Reclaim an nfsnode so that it can be used for other purposes. | |
367 | */ | |
368 | int | |
369 | nfs_reclaim(ap) | |
91447636 A |
370 | struct vnop_reclaim_args /* { |
371 | struct vnodeop_desc *a_desc; | |
372 | vnode_t a_vp; | |
373 | vfs_context_t a_context; | |
1c79356b A |
374 | } */ *ap; |
375 | { | |
91447636 A |
376 | vnode_t vp = ap->a_vp; |
377 | struct nfsnode *np = VTONFS(vp); | |
378 | struct nfsdmap *dp, *dp2; | |
1c79356b | 379 | |
91447636 | 380 | vnode_removefsref(vp); |
1c79356b | 381 | |
55e303ae | 382 | if (np->n_flag & NHASHED) { |
91447636 | 383 | lck_mtx_lock(nfs_node_hash_mutex); |
55e303ae A |
384 | LIST_REMOVE(np, n_hash); |
385 | np->n_flag &= ~NHASHED; | |
91447636 | 386 | lck_mtx_unlock(nfs_node_hash_mutex); |
1c79356b A |
387 | } |
388 | ||
389 | /* | |
390 | * Free up any directory cookie structures and | |
391 | * large file handle structures that might be associated with | |
392 | * this nfs node. | |
393 | */ | |
91447636 | 394 | if (vnode_vtype(vp) == VDIR) { |
1c79356b A |
395 | dp = np->n_cookies.lh_first; |
396 | while (dp) { | |
397 | dp2 = dp; | |
398 | dp = dp->ndm_list.le_next; | |
399 | FREE_ZONE((caddr_t)dp2, | |
400 | sizeof (struct nfsdmap), M_NFSDIROFF); | |
401 | } | |
402 | } | |
403 | if (np->n_fhsize > NFS_SMALLFH) { | |
91447636 | 404 | FREE_ZONE(np->n_fhp, np->n_fhsize, M_NFSBIGFH); |
1c79356b | 405 | } |
91447636 | 406 | vnode_clearfsnode(vp); |
1c79356b | 407 | |
91447636 | 408 | FREE_ZONE(np, sizeof(struct nfsnode), M_NFSNODE); |
1c79356b A |
409 | return (0); |
410 | } | |
411 |