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