]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_node.c
61af3f47561bdaf15f96cdc62a44aabe837d0e85
[apple/xnu.git] / bsd / nfs / nfs_node.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 /*
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley by
29 * Rick Macklem at The University of Guelph.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
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.
46 *
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
57 * SUCH DAMAGE.
58 *
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 $
61 */
62
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/proc.h>
67 #include <sys/kauth.h>
68 #include <sys/mount_internal.h>
69 #include <sys/vnode.h>
70 #include <sys/ubc.h>
71 #include <sys/malloc.h>
72
73 #include <nfs/rpcv2.h>
74 #include <nfs/nfsproto.h>
75 #include <nfs/nfs.h>
76 #include <nfs/nfsnode.h>
77 #include <nfs/nfsmount.h>
78
79 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
80 u_long nfsnodehash;
81
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;
86
87 /*
88 * Initialize hash links for nfsnodes
89 * and build nfsnode free list.
90 */
91 void
92 nfs_nhinit(void)
93 {
94 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash);
95
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);
99
100 nfs_node_hash_lck_attr = lck_attr_alloc_init();
101
102 nfs_node_hash_mutex = lck_mtx_alloc_init(nfs_node_hash_lck_grp, nfs_node_hash_lck_attr);
103 }
104
105 /*
106 * Compute an entry in the NFS hash table structure
107 */
108 u_long
109 nfs_hash(u_char *fhp, int fhsize)
110 {
111 u_long fhsum;
112 int i;
113
114 fhsum = 0;
115 for (i = 0; i < fhsize; i++)
116 fhsum += *fhp++;
117 return (fhsum);
118 }
119
120 /*
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.
125 */
126 int
127 nfs_nget(
128 mount_t mntp,
129 vnode_t dvp,
130 struct componentname *cnp,
131 u_char *fhp,
132 int fhsize,
133 struct nfs_vattr *nvap,
134 u_int64_t *xidp,
135 int flags,
136 struct nfsnode **npp)
137 {
138 struct nfsnode *np;
139 struct nfsnodehashhead *nhpp;
140 vnode_t vp, nvp;
141 int error;
142 mount_t mp;
143 struct vnode_fsparam vfsp;
144 uint32_t vid;
145
146 /* Check for unmount in progress */
147 if (!mntp || (mntp->mnt_kern_flag & MNTK_UNMOUNT)) {
148 *npp = 0;
149 return (!mntp ? ENXIO : EPERM);
150 }
151
152 nhpp = NFSNOHASH(nfs_hash(fhp, fhsize));
153 loop:
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))
159 continue;
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);
164 goto loop;
165 }
166 vp = NFSTOV(np);
167 vid = vnode_vid(vp);
168 lck_mtx_unlock(nfs_node_hash_mutex);
169 if ((error = vnode_getwithvid(vp, vid))) {
170 /*
171 * If vnode is being reclaimed or has already
172 * changed identity, no need to wait.
173 */
174 return (error);
175 }
176 /* update attributes */
177 error = nfs_loadattrcache(np, nvap, xidp, 0);
178 if (error) {
179 vnode_put(vp);
180 } else {
181 if (dvp && cnp && (flags & NG_MAKEENTRY))
182 cache_enter(dvp, vp, cnp);
183 *npp = np;
184 }
185 return(error);
186 }
187
188 /*
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.
192 */
193 MALLOC_ZONE(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
194 if (!np) {
195 lck_mtx_unlock(nfs_node_hash_mutex);
196 *npp = 0;
197 return (ENOMEM);
198 }
199 bzero((caddr_t)np, sizeof *np);
200 np->n_flag |= NINIT;
201 np->n_mount = mntp;
202
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);
207 if (!np->n_fhp) {
208 lck_mtx_unlock(nfs_node_hash_mutex);
209 FREE_ZONE(np, sizeof *np, M_NFSNODE);
210 *npp = 0;
211 return (ENOMEM);
212 }
213 } else {
214 np->n_fhp = &np->n_fh[0];
215 }
216 bcopy(fhp, np->n_fhp, fhsize);
217 np->n_fhsize = fhsize;
218
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);
222
223 /* release lock on hash table */
224 lck_mtx_unlock(nfs_node_hash_mutex);
225
226 /* do initial loading of attributes */
227 error = nfs_loadattrcache(np, nvap, xidp, 1);
228 if (error) {
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;
234 wakeup((caddr_t)np);
235 }
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);
240 *npp = 0;
241 return (error);
242 }
243 np->n_mtime = nvap->nva_mtime;
244 if (nvap->nva_type == VDIR)
245 np->n_ncmtime = nvap->nva_mtime;
246 NMODEINVALIDATE(np);
247
248 /* now, attempt to get a new vnode */
249 vfsp.vnfs_mp = mntp;
250 vfsp.vnfs_vtype = nvap->nva_type;
251 vfsp.vnfs_str = "nfs";
252 vfsp.vnfs_dvp = dvp;
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;
258 else
259 vfsp.vnfs_vops = nfsv2_vnodeop_p;
260 vfsp.vnfs_markroot = (flags & NG_MARKROOT) ? 1 : 0;
261 vfsp.vnfs_marksystem = 0;
262 vfsp.vnfs_rdev = 0;
263 vfsp.vnfs_filesize = nvap->nva_size;
264 vfsp.vnfs_cnp = cnp;
265 if (dvp && cnp && (flags & NG_MAKEENTRY))
266 vfsp.vnfs_flags = 0;
267 else
268 vfsp.vnfs_flags = VNFS_NOCACHE;
269 error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &nvp);
270 if (error) {
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;
276 wakeup((caddr_t)np);
277 }
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);
282 *npp = 0;
283 return (error);
284 }
285 vp = nvp;
286 np->n_vnode = vp;
287 vnode_addfsref(vp);
288 vnode_settag(vp, VT_NFS); // XXX shouldn't this be a vnode_create() parameter?
289 *npp = np;
290 /* node is now initialized */
291
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;
297 wakeup((caddr_t)np);
298 }
299 lck_mtx_unlock(nfs_node_hash_mutex);
300
301 return (error);
302 }
303
304
305 int
306 nfs_inactive(ap)
307 struct vnop_inactive_args /* {
308 struct vnodeop_desc *a_desc;
309 vnode_t a_vp;
310 vfs_context_t a_context;
311 } */ *ap;
312 {
313 register struct nfsnode *np;
314 register struct sillyrename *sp;
315 kauth_cred_t cred;
316
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;
321 } else
322 sp = (struct sillyrename *)0;
323
324 if (sp) {
325 /*
326 * Remove the silly file that was rename'd earlier
327 */
328 #if DIAGNOSTIC
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);
332 #endif
333 nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, vfs_context_proc(ap->a_context), 1);
334 np->n_size = 0;
335 ubc_setsize(ap->a_vp, (off_t)0);
336 nfs_removeit(sp);
337 /*
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
341 */
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);
346 cred = sp->s_cred;
347 if (cred != NOCRED) {
348 sp->s_cred = NOCRED;
349 kauth_cred_rele(cred);
350 }
351 vnode_rele(sp->s_dvp);
352 FREE_ZONE((caddr_t)sp, sizeof (struct sillyrename), M_NFSREQ);
353 vnode_recycle(ap->a_vp);
354 }
355 /* clear all flags other than these */
356 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NHASHED);
357 return (0);
358 }
359
360 /*
361 * Reclaim an nfsnode so that it can be used for other purposes.
362 */
363 int
364 nfs_reclaim(ap)
365 struct vnop_reclaim_args /* {
366 struct vnodeop_desc *a_desc;
367 vnode_t a_vp;
368 vfs_context_t a_context;
369 } */ *ap;
370 {
371 vnode_t vp = ap->a_vp;
372 struct nfsnode *np = VTONFS(vp);
373 struct nfsdmap *dp, *dp2;
374
375 vnode_removefsref(vp);
376
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);
382 }
383
384 /*
385 * Free up any directory cookie structures and
386 * large file handle structures that might be associated with
387 * this nfs node.
388 */
389 if (vnode_vtype(vp) == VDIR) {
390 dp = np->n_cookies.lh_first;
391 while (dp) {
392 dp2 = dp;
393 dp = dp->ndm_list.le_next;
394 FREE_ZONE((caddr_t)dp2,
395 sizeof (struct nfsdmap), M_NFSDIROFF);
396 }
397 }
398 if (np->n_fhsize > NFS_SMALLFH) {
399 FREE_ZONE(np->n_fhp, np->n_fhsize, M_NFSBIGFH);
400 }
401 vnode_clearfsnode(vp);
402
403 FREE_ZONE(np, sizeof(struct nfsnode), M_NFSNODE);
404 return (0);
405 }
406