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