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