1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 #include <nfs/nfs_conf.h>
69 #if CONFIG_NFS_CLIENT
70
71 #include <sys/param.h>
72 #include <sys/kernel.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/kauth.h>
76 #include <sys/mount_internal.h>
77 #include <sys/vnode_internal.h>
78 #include <sys/vnode.h>
79 #include <sys/ubc.h>
80 #include <sys/malloc.h>
81 #include <sys/fcntl.h>
82 #include <sys/time.h>
83
84 #include <nfs/rpcv2.h>
85 #include <nfs/nfsproto.h>
86 #include <nfs/nfs.h>
87 #include <nfs/nfsnode.h>
88 #include <nfs/nfs_gss.h>
89 #include <nfs/nfsmount.h>
90
91 #define NFSNOHASH(fhsum) \
92 (&nfsnodehashtbl[(fhsum) & nfsnodehash])
93 static LIST_HEAD(nfsnodehashhead, nfsnode) * nfsnodehashtbl;
94 static u_long nfsnodehash;
95
96 static LCK_GRP_DECLARE(nfs_node_hash_lck_grp, "nfs_node_hash");
97 static LCK_GRP_DECLARE(nfs_node_lck_grp, "nfs_node");
98 static LCK_GRP_DECLARE(nfs_data_lck_grp, "nfs_data");
99 LCK_MTX_DECLARE(nfs_node_hash_mutex, &nfs_node_hash_lck_grp);
100
101 ZONE_DEFINE(nfsnode_zone, "NFS node",
102 sizeof(struct nfsnode), ZC_ZFREE_CLEARMEM);
103
104 #define NFS_NODE_DBG(...) NFSCLNT_DBG(NFSCLNT_FAC_NODE, 7, ## __VA_ARGS__)
105
106 void
nfs_nhinit_finish(void)107 nfs_nhinit_finish(void)
108 {
109 lck_mtx_lock(&nfs_node_hash_mutex);
110 if (!nfsnodehashtbl) {
111 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash);
112 }
113 lck_mtx_unlock(&nfs_node_hash_mutex);
114 }
115
116 /*
117 * Compute an entry in the NFS hash table structure
118 */
119 u_long
nfs_hash(u_char * fhp,int fhsize)120 nfs_hash(u_char *fhp, int fhsize)
121 {
122 u_long fhsum;
123 int i;
124
125 fhsum = 0;
126 for (i = 0; i < fhsize; i++) {
127 fhsum += *fhp++;
128 }
129 return fhsum;
130 }
131
132
133 int nfs_case_insensitive(mount_t);
134
135 int
nfs_case_insensitive(mount_t mp)136 nfs_case_insensitive(mount_t mp)
137 {
138 struct nfsmount *nmp = VFSTONFS(mp);
139 int answer = 0;
140 int skip = 0;
141
142 if (nfs_mount_gone(nmp)) {
143 return 0;
144 }
145
146 if (nmp->nm_vers == NFS_VER2) {
147 /* V2 has no way to know */
148 return 0;
149 }
150
151 lck_mtx_lock(&nmp->nm_lock);
152 if (nmp->nm_vers == NFS_VER3) {
153 if (!(nmp->nm_state & NFSSTA_GOTPATHCONF)) {
154 /* We're holding the node lock so we just return
155 * with answer as case sensitive. Is very rare
156 * for file systems not to be homogenous w.r.t. pathconf
157 */
158 skip = 1;
159 }
160 } else if (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS)) {
161 /* no pathconf info cached */
162 skip = 1;
163 }
164
165 if (!skip && (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE)) {
166 answer = 1;
167 }
168
169 lck_mtx_unlock(&nmp->nm_lock);
170
171 return answer;
172 }
173
174
175 /*
176 * Look up a vnode/nfsnode by file handle.
177 * Callers must check for mount points!!
178 * In all cases, a pointer to a
179 * nfsnode structure is returned.
180 */
181 int
nfs_nget(mount_t mp,nfsnode_t dnp,struct componentname * cnp,u_char * fhp,uint32_t fhsize,struct nfs_vattr * nvap,u_int64_t * xidp,uint32_t auth,int flags,nfsnode_t * npp)182 nfs_nget(
183 mount_t mp,
184 nfsnode_t dnp,
185 struct componentname *cnp,
186 u_char *fhp,
187 uint32_t fhsize,
188 struct nfs_vattr *nvap,
189 u_int64_t *xidp,
190 uint32_t auth,
191 int flags,
192 nfsnode_t *npp)
193 {
194 nfsnode_t np;
195 struct nfsnodehashhead *nhpp;
196 vnode_t vp;
197 int error, nfsvers;
198 mount_t mp2;
199 struct vnode_fsparam vfsp;
200 uint32_t vid, cn_namelen;
201 u_long nfshash;
202
203 FSDBG_TOP(263, mp, dnp, flags, npp);
204
205 /* Check for unmount in progress */
206 if (!mp || vfs_isforce(mp)) {
207 *npp = NULL;
208 error = ENXIO;
209 FSDBG_BOT(263, mp, dnp, 0xd1e, error);
210 return error;
211 }
212 nfsvers = VFSTONFS(mp)->nm_vers;
213 cn_namelen = cnp ? cnp->cn_namelen : 0;
214 nfshash = nfs_hash(fhp, fhsize);
215 loop:
216 lck_mtx_lock(&nfs_node_hash_mutex);
217 nhpp = NFSNOHASH(nfshash);
218 for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
219 mp2 = (np->n_hflag & NHINIT) ? np->n_mount : NFSTOMP(np);
220 if (mp != mp2 || np->n_fhsize != fhsize ||
221 bcmp(fhp, np->n_fhp, fhsize)) {
222 continue;
223 }
224 if (nvap && (nvap->nva_flags & NFS_FFLAG_TRIGGER_REFERRAL) &&
225 cnp && (cn_namelen > (fhsize - sizeof(dnp)))) {
226 /* The name was too long to fit in the file handle. Check it against the node's name. */
227 int namecmp = 0;
228 const char *vname = vnode_getname(NFSTOV(np));
229 if (vname) {
230 if (cn_namelen != strlen(vname)) {
231 namecmp = 1;
232 } else {
233 namecmp = strncmp(vname, cnp->cn_nameptr, cn_namelen);
234 }
235 vnode_putname(vname);
236 }
237 if (namecmp) { /* full name didn't match */
238 continue;
239 }
240 }
241 FSDBG(263, dnp, np, np->n_flag, 0xcace0000);
242 /* if the node is being initialized or locked, sleep on it */
243 if ((np->n_hflag & NHINIT) || ((np->n_hflag & NHLOCKED) && !(flags & NG_NOCREATE))) {
244 np->n_hflag |= NHLOCKWANT;
245 FSDBG(263, dnp, np, np->n_flag, 0xcace2222);
246 msleep(np, &nfs_node_hash_mutex, PDROP | PINOD, "nfs_nget", NULL);
247 FSDBG(263, dnp, np, np->n_flag, 0xcace3333);
248 goto loop;
249 }
250 vp = NFSTOV(np);
251 vid = vnode_vid(vp);
252 lck_mtx_unlock(&nfs_node_hash_mutex);
253 if ((error = vnode_getwithvid(vp, vid))) {
254 /*
255 * If vnode is being reclaimed or has already
256 * changed identity, no need to wait.
257 */
258 FSDBG_BOT(263, dnp, *npp, 0xcace0d1e, error);
259 return error;
260 }
261 if ((error = nfs_node_lock(np))) {
262 /* this only fails if the node is now unhashed */
263 /* so let's see if we can find/create it again */
264 FSDBG(263, dnp, *npp, 0xcaced1e2, error);
265 vnode_put(vp);
266 if (flags & NG_NOCREATE) {
267 *npp = 0;
268 FSDBG_BOT(263, dnp, *npp, 0xcaced1e0, ENOENT);
269 return ENOENT;
270 }
271 goto loop;
272 }
273 /* update attributes */
274 if (nvap) {
275 error = nfs_loadattrcache(np, nvap, xidp, 0);
276 }
277 if (error) {
278 nfs_node_unlock(np);
279 vnode_put(vp);
280 } else {
281 if (dnp && cnp && (flags & NG_MAKEENTRY)) {
282 cache_enter(NFSTOV(dnp), vp, cnp);
283 }
284 /*
285 * Update the vnode if the name/and or the parent has
286 * changed. We need to do this so that if getattrlist is
287 * called asking for ATTR_CMN_NAME, that the "most"
288 * correct name is being returned. In addition for
289 * monitored vnodes we need to kick the vnode out of the
290 * name cache. We do this so that if there are hard
291 * links in the same directory the link will not be
292 * found and a lookup will get us here to return the
293 * name of the current link. In addition by removing the
294 * name from the name cache the old name will not be
295 * found after a rename done on another client or the
296 * server. The principle reason to do this is because
297 * Finder is asking for notifications on a directory.
298 * The directory changes, Finder gets notified, reads
299 * the directory (which we have purged) and for each
300 * entry returned calls getattrlist with the name
301 * returned from readdir. gettattrlist has to call
302 * namei/lookup to resolve the name, because its not in
303 * the cache we end up here. We need to update the name
304 * so Finder will get the name it called us with.
305 *
306 * We had an imperfect solution with respect to case
307 * sensitivity. There is a test that is run in
308 * FileBuster that does renames from some name to
309 * another name differing only in case. It then reads
310 * the directory looking for the new name, after it
311 * finds that new name, it ask gettattrlist to verify
312 * that the name is the new name. Usually that works,
313 * but renames generate fsevents and fseventsd will do a
314 * lookup on the name via lstat. Since that test renames
315 * old name to new name back and forth there is a race
316 * that an fsevent will be behind and will access the
317 * file by the old name, on a case insensitive file
318 * system that will work. Problem is if we do a case
319 * sensitive compare, we're going to change the name,
320 * which the test's getattrlist verification step is
321 * going to fail. So we will check the case sensitivity
322 * of the file system and do the appropriate compare. In
323 * a rare instance for non homogeneous file systems
324 * w.r.t. pathconf we will use case sensitive compares.
325 * That could break if the file system is actually case
326 * insensitive.
327 *
328 * Note that V2 does not know the case, so we just
329 * assume case sensitivity.
330 *
331 * This is clearly not perfect due to races, but this is
332 * as good as its going to get. You can defeat the
333 * handling of hard links simply by doing:
334 *
335 * while :; do ls -l > /dev/null; done
336 *
337 * in a terminal window. Even a single ls -l can cause a
338 * race.
339 *
340 * <rant>What we really need is for the caller, that
341 * knows the name being used is valid since it got it
342 * from a readdir to use that name and not ask for the
343 * ATTR_CMN_NAME</rant>
344 */
345 if (dnp && cnp && (vp != NFSTOV(dnp))) {
346 int update_flags = (vnode_ismonitored((NFSTOV(dnp)))) ? VNODE_UPDATE_CACHE : 0;
347 int (*cmp)(const char *s1, const char *s2, size_t n);
348
349 cmp = nfs_case_insensitive(mp) ? strncasecmp : strncmp;
350
351 if (vnode_getname(vp) && cn_namelen != strnlen(vnode_getname(vp), MAXPATHLEN)) {
352 update_flags |= VNODE_UPDATE_NAME;
353 }
354 if (vnode_getname(vp) && cn_namelen && (*cmp)(cnp->cn_nameptr, vnode_getname(vp), cn_namelen)) {
355 update_flags |= VNODE_UPDATE_NAME;
356 }
357 if ((vnode_getname(vp) == NULL && cn_namelen != 0) || (vnode_getname(vp) != NULL && cn_namelen == 0)) {
358 update_flags |= VNODE_UPDATE_NAME;
359 }
360 if (vnode_parent(vp) != NFSTOV(dnp)) {
361 update_flags |= VNODE_UPDATE_PARENT;
362 }
363 if (update_flags) {
364 NFS_NODE_DBG("vnode_update_identity old name %s new name %.*s update flags = %x\n",
365 vnode_getname(vp), cn_namelen, cnp->cn_nameptr ? cnp->cn_nameptr : "", update_flags);
366 vnode_update_identity(vp, NFSTOV(dnp), cnp->cn_nameptr, cn_namelen, 0, update_flags);
367 }
368 }
369
370 *npp = np;
371 }
372 FSDBG_BOT(263, dnp, *npp, 0xcace0000, error);
373 return error;
374 }
375
376 FSDBG(263, mp, dnp, npp, 0xaaaaaaaa);
377
378 if (flags & NG_NOCREATE) {
379 lck_mtx_unlock(&nfs_node_hash_mutex);
380 *npp = 0;
381 FSDBG_BOT(263, dnp, *npp, 0x80000001, ENOENT);
382 return ENOENT;
383 }
384
385 /*
386 * allocate and initialize nfsnode and stick it in the hash
387 * before calling getnewvnode(). Anyone finding it in the
388 * hash before initialization is complete will wait for it.
389 */
390 np = zalloc_flags(nfsnode_zone, Z_WAITOK | Z_ZERO);
391 np->n_hflag |= (NHINIT | NHLOCKED);
392 np->n_mount = mp;
393 np->n_auth = auth;
394 TAILQ_INIT(&np->n_opens);
395 TAILQ_INIT(&np->n_lock_owners);
396 TAILQ_INIT(&np->n_locks);
397 np->n_dlink.tqe_next = NFSNOLIST;
398 np->n_dreturn.tqe_next = NFSNOLIST;
399 np->n_monlink.le_next = NFSNOLIST;
400
401 /* ugh... need to keep track of ".zfs" directories to workaround server bugs */
402 if ((nvap->nva_type == VDIR) && cnp && (cn_namelen == 4) &&
403 (cnp->cn_nameptr[0] == '.') && (cnp->cn_nameptr[1] == 'z') &&
404 (cnp->cn_nameptr[2] == 'f') && (cnp->cn_nameptr[3] == 's')) {
405 np->n_flag |= NISDOTZFS;
406 }
407 if (dnp && (dnp->n_flag & NISDOTZFS)) {
408 np->n_flag |= NISDOTZFSCHILD;
409 }
410
411 if (dnp && cnp && ((cn_namelen != 2) ||
412 (cnp->cn_nameptr[0] != '.') || (cnp->cn_nameptr[1] != '.'))) {
413 vnode_t dvp = NFSTOV(dnp);
414 if (!vnode_get(dvp)) {
415 if (!vnode_ref(dvp)) {
416 np->n_parent = dvp;
417 }
418 vnode_put(dvp);
419 }
420 }
421
422 /* setup node's file handle */
423 if (fhsize > NFS_SMALLFH) {
424 np->n_fhp = kalloc_data(fhsize, Z_WAITOK);
425 if (!np->n_fhp) {
426 lck_mtx_unlock(&nfs_node_hash_mutex);
427 NFS_ZFREE(nfsnode_zone, np);
428 *npp = 0;
429 FSDBG_BOT(263, dnp, *npp, 0x80000002, ENOMEM);
430 return ENOMEM;
431 }
432 } else {
433 np->n_fhp = &np->n_fh[0];
434 }
435 bcopy(fhp, np->n_fhp, fhsize);
436 np->n_fhsize = fhsize;
437
438 /* Insert the nfsnode in the hash queue for its new file handle */
439 LIST_INSERT_HEAD(nhpp, np, n_hash);
440 np->n_hflag |= NHHASHED;
441 FSDBG(266, 0, np, np->n_flag, np->n_hflag);
442
443 /* lock the new nfsnode */
444 lck_mtx_init(&np->n_lock, &nfs_node_lck_grp, LCK_ATTR_NULL);
445 lck_rw_init(&np->n_datalock, &nfs_data_lck_grp, LCK_ATTR_NULL);
446 lck_mtx_init(&np->n_openlock, &nfs_open_grp, LCK_ATTR_NULL);
447 lck_mtx_lock(&np->n_lock);
448
449 /* release lock on hash table */
450 lck_mtx_unlock(&nfs_node_hash_mutex);
451
452 /* do initial loading of attributes */
453 NACLINVALIDATE(np);
454 NACCESSINVALIDATE(np);
455 error = nfs_loadattrcache(np, nvap, xidp, 1);
456 if (error) {
457 FSDBG(266, 0, np, np->n_flag, 0xb1eb1e);
458 nfs_node_unlock(np);
459 lck_mtx_lock(&nfs_node_hash_mutex);
460 LIST_REMOVE(np, n_hash);
461 np->n_hflag &= ~(NHHASHED | NHINIT | NHLOCKED);
462 if (np->n_hflag & NHLOCKWANT) {
463 np->n_hflag &= ~NHLOCKWANT;
464 wakeup(np);
465 }
466 lck_mtx_unlock(&nfs_node_hash_mutex);
467 if (np->n_parent) {
468 if (!vnode_get(np->n_parent)) {
469 vnode_rele(np->n_parent);
470 vnode_put(np->n_parent);
471 }
472 np->n_parent = NULL;
473 }
474 lck_mtx_destroy(&np->n_lock, &nfs_node_lck_grp);
475 lck_rw_destroy(&np->n_datalock, &nfs_data_lck_grp);
476 lck_mtx_destroy(&np->n_openlock, &nfs_open_grp);
477 if (np->n_fhsize > NFS_SMALLFH) {
478 kfree_data(np->n_fhp, np->n_fhsize);
479 }
480 NFS_ZFREE(nfsnode_zone, np);
481 *npp = 0;
482 FSDBG_BOT(263, dnp, *npp, 0x80000003, error);
483 return error;
484 }
485 NFS_CHANGED_UPDATE(nfsvers, np, nvap);
486 if (nvap->nva_type == VDIR) {
487 NFS_CHANGED_UPDATE_NC(nfsvers, np, nvap);
488 }
489
490 /* now, attempt to get a new vnode */
491 vfsp.vnfs_mp = mp;
492 vfsp.vnfs_vtype = nvap->nva_type;
493 vfsp.vnfs_str = "nfs";
494 vfsp.vnfs_dvp = dnp ? NFSTOV(dnp) : NULL;
495 vfsp.vnfs_fsnode = np;
496 #if CONFIG_NFS4
497 if (nfsvers == NFS_VER4) {
498 #if FIFO
499 if (nvap->nva_type == VFIFO) {
500 vfsp.vnfs_vops = fifo_nfsv4nodeop_p;
501 } else
502 #endif /* FIFO */
503 if (nvap->nva_type == VBLK || nvap->nva_type == VCHR) {
504 vfsp.vnfs_vops = spec_nfsv4nodeop_p;
505 } else {
506 vfsp.vnfs_vops = nfsv4_vnodeop_p;
507 }
508 } else
509 #endif /* CONFIG_NFS4 */
510 {
511 #if FIFO
512 if (nvap->nva_type == VFIFO) {
513 vfsp.vnfs_vops = fifo_nfsv2nodeop_p;
514 } else
515 #endif /* FIFO */
516 if (nvap->nva_type == VBLK || nvap->nva_type == VCHR) {
517 vfsp.vnfs_vops = spec_nfsv2nodeop_p;
518 } else {
519 vfsp.vnfs_vops = nfsv2_vnodeop_p;
520 }
521 }
522 vfsp.vnfs_markroot = (flags & NG_MARKROOT) ? 1 : 0;
523 vfsp.vnfs_marksystem = 0;
524 vfsp.vnfs_rdev = 0;
525 vfsp.vnfs_filesize = nvap->nva_size;
526 vfsp.vnfs_cnp = cnp;
527 vfsp.vnfs_flags = VNFS_ADDFSREF;
528 if (!dnp || !cnp || !(flags & NG_MAKEENTRY)) {
529 vfsp.vnfs_flags |= VNFS_NOCACHE;
530 }
531
532 #if CONFIG_TRIGGERS
533 if ((nfsvers >= NFS_VER4)
534 && (nvap->nva_type == VDIR) && (np->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)
535 && !(flags & NG_MARKROOT)) {
536 struct vnode_trigger_param vtp;
537 bzero(&vtp, sizeof(vtp));
538 bcopy(&vfsp, &vtp.vnt_params, sizeof(vfsp));
539 vtp.vnt_resolve_func = nfs_mirror_mount_trigger_resolve;
540 vtp.vnt_unresolve_func = nfs_mirror_mount_trigger_unresolve;
541 vtp.vnt_rearm_func = nfs_mirror_mount_trigger_rearm;
542 vtp.vnt_flags = VNT_AUTO_REARM | VNT_KERN_RESOLVE;
543 error = vnode_create(VNCREATE_TRIGGER, VNCREATE_TRIGGER_SIZE, &vtp, &np->n_vnode);
544 } else
545 #endif
546 {
547 error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &np->n_vnode);
548 }
549 if (error) {
550 FSDBG(266, 0, np, np->n_flag, 0xb1eb1e);
551 nfs_node_unlock(np);
552 lck_mtx_lock(&nfs_node_hash_mutex);
553 LIST_REMOVE(np, n_hash);
554 np->n_hflag &= ~(NHHASHED | NHINIT | NHLOCKED);
555 if (np->n_hflag & NHLOCKWANT) {
556 np->n_hflag &= ~NHLOCKWANT;
557 wakeup(np);
558 }
559 lck_mtx_unlock(&nfs_node_hash_mutex);
560 if (np->n_parent) {
561 if (!vnode_get(np->n_parent)) {
562 vnode_rele(np->n_parent);
563 vnode_put(np->n_parent);
564 }
565 np->n_parent = NULL;
566 }
567 lck_mtx_destroy(&np->n_lock, &nfs_node_lck_grp);
568 lck_rw_destroy(&np->n_datalock, &nfs_data_lck_grp);
569 lck_mtx_destroy(&np->n_openlock, &nfs_open_grp);
570 if (np->n_fhsize > NFS_SMALLFH) {
571 kfree_data(np->n_fhp, np->n_fhsize);
572 }
573 NFS_ZFREE(nfsnode_zone, np);
574 *npp = 0;
575 FSDBG_BOT(263, dnp, *npp, 0x80000004, error);
576 return error;
577 }
578 vp = np->n_vnode;
579 vnode_settag(vp, VT_NFS);
580 /* node is now initialized */
581
582 /* check if anyone's waiting on this node */
583 lck_mtx_lock(&nfs_node_hash_mutex);
584 np->n_hflag &= ~(NHINIT | NHLOCKED);
585 if (np->n_hflag & NHLOCKWANT) {
586 np->n_hflag &= ~NHLOCKWANT;
587 wakeup(np);
588 }
589 lck_mtx_unlock(&nfs_node_hash_mutex);
590
591 *npp = np;
592
593 FSDBG_BOT(263, dnp, vp, *npp, error);
594 return error;
595 }
596
597
598 int
nfs_vnop_inactive(struct vnop_inactive_args * ap)599 nfs_vnop_inactive(
600 struct vnop_inactive_args /* {
601 * struct vnodeop_desc *a_desc;
602 * vnode_t a_vp;
603 * vfs_context_t a_context;
604 * } */*ap)
605 {
606 vnode_t vp = ap->a_vp;
607 vfs_context_t ctx = ap->a_context;
608 nfsnode_t np;
609 struct nfs_sillyrename *nsp;
610 struct nfs_vattr *nvattr;
611 int unhash, attrerr, busyerror, error, inuse, busied, force;
612 struct nfs_open_file *nofp;
613 struct componentname cn;
614 struct nfsmount *nmp;
615 mount_t mp;
616
617 if (vp == NULL) {
618 panic("nfs_vnop_inactive: vp == NULL");
619 }
620 np = VTONFS(vp);
621 if (np == NULL) {
622 panic("nfs_vnop_inactive: np == NULL");
623 }
624
625 nmp = NFSTONMP(np);
626 mp = vnode_mount(vp);
627 nvattr = kalloc_type(struct nfs_vattr, Z_WAITOK);
628
629 restart:
630 force = (!mp || vfs_isforce(mp));
631 error = 0;
632 inuse = (nfs_mount_state_in_use_start(nmp, NULL) == 0);
633
634 /* There shouldn't be any open or lock state at this point */
635 lck_mtx_lock(&np->n_openlock);
636 if (np->n_openrefcnt && !force) {
637 /*
638 * vnode_rele and vnode_put drop the vnode lock before
639 * calling VNOP_INACTIVE, so there is a race were the
640 * vnode could become active again. Perhaps there are
641 * other places where this can happen, so if we've got
642 * here we need to get out.
643 */
644 #ifdef NFS_NODE_DEBUG
645 NP(np, "nfs_vnop_inactive: still open: %d", np->n_openrefcnt);
646 #endif
647 lck_mtx_unlock(&np->n_openlock);
648 if (inuse) {
649 nfs_mount_state_in_use_end(nmp, 0);
650 }
651 goto out_free;
652 }
653
654 TAILQ_FOREACH(nofp, &np->n_opens, nof_link) {
655 lck_mtx_lock(&nofp->nof_lock);
656 if (nofp->nof_flags & NFS_OPEN_FILE_BUSY) {
657 if (!force) {
658 NP(np, "nfs_vnop_inactive: open file busy");
659 }
660 busied = 0;
661 } else {
662 nofp->nof_flags |= NFS_OPEN_FILE_BUSY;
663 busied = 1;
664 }
665 lck_mtx_unlock(&nofp->nof_lock);
666 if ((np->n_flag & NREVOKE) || (nofp->nof_flags & NFS_OPEN_FILE_LOST)) {
667 if (busied) {
668 nfs_open_file_clear_busy(nofp);
669 }
670 continue;
671 }
672 /*
673 * If we just created the file, we already had it open in
674 * anticipation of getting a subsequent open call. If the
675 * node has gone inactive without being open, we need to
676 * clean up (close) the open done in the create.
677 */
678 #if CONFIG_NFS4
679 if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && nofp->nof_creator && !force) {
680 if (nofp->nof_flags & NFS_OPEN_FILE_REOPEN) {
681 lck_mtx_unlock(&np->n_openlock);
682 if (busied) {
683 nfs_open_file_clear_busy(nofp);
684 }
685 if (!nfs4_reopen(nofp, NULL)) {
686 if (inuse) {
687 nfs_mount_state_in_use_end(nmp, 0);
688 }
689 goto restart;
690 }
691 }
692 nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE;
693 lck_mtx_unlock(&np->n_openlock);
694 error = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_BOTH, NFS_OPEN_SHARE_DENY_NONE, ctx);
695 if (error) {
696 NP(np, "nfs_vnop_inactive: create close error: %d", error);
697 if (error != NFSERR_NOENT) {
698 nofp->nof_flags |= NFS_OPEN_FILE_CREATE;
699 }
700 }
701 if (busied) {
702 nfs_open_file_clear_busy(nofp);
703 }
704 if (inuse) {
705 nfs_mount_state_in_use_end(nmp, error);
706 }
707 goto restart;
708 }
709 #endif
710 if (nofp->nof_flags & NFS_OPEN_FILE_NEEDCLOSE) {
711 /*
712 * If the file is marked as needing reopen, but this was the only
713 * open on the file, just drop the open.
714 */
715 nofp->nof_flags &= ~NFS_OPEN_FILE_NEEDCLOSE;
716 if ((nofp->nof_flags & NFS_OPEN_FILE_REOPEN) && (nofp->nof_opencnt == 1)) {
717 nofp->nof_flags &= ~NFS_OPEN_FILE_REOPEN;
718 nofp->nof_r--;
719 nofp->nof_opencnt--;
720 nofp->nof_access = 0;
721 } else if (!force) {
722 lck_mtx_unlock(&np->n_openlock);
723 if (nofp->nof_flags & NFS_OPEN_FILE_REOPEN) {
724 int should_restart = 0;
725 if (busied) {
726 nfs_open_file_clear_busy(nofp);
727 }
728 #if CONFIG_NFS4
729 if (!nfs4_reopen(nofp, NULL)) {
730 should_restart = 1;
731 }
732 #endif
733 if (should_restart) {
734 if (inuse) {
735 nfs_mount_state_in_use_end(nmp, 0);
736 }
737 goto restart;
738 }
739 }
740 error = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_READ, NFS_OPEN_SHARE_DENY_NONE, ctx);
741 if (error) {
742 NP(np, "nfs_vnop_inactive: need close error: %d", error);
743 if (error != NFSERR_NOENT) {
744 nofp->nof_flags |= NFS_OPEN_FILE_NEEDCLOSE;
745 }
746 }
747 if (busied) {
748 nfs_open_file_clear_busy(nofp);
749 }
750 if (inuse) {
751 nfs_mount_state_in_use_end(nmp, error);
752 }
753 goto restart;
754 }
755 }
756 if (nofp->nof_opencnt && !force) {
757 NP(np, "nfs_vnop_inactive: file still open: %d", nofp->nof_opencnt);
758 }
759 if (!force && (nofp->nof_access || nofp->nof_deny ||
760 nofp->nof_mmap_access || nofp->nof_mmap_deny ||
761 nofp->nof_r || nofp->nof_w || nofp->nof_rw ||
762 nofp->nof_r_dw || nofp->nof_w_dw || nofp->nof_rw_dw ||
763 nofp->nof_r_drw || nofp->nof_w_drw || nofp->nof_rw_drw ||
764 nofp->nof_d_r || nofp->nof_d_w || nofp->nof_d_rw ||
765 nofp->nof_d_r_dw || nofp->nof_d_w_dw || nofp->nof_d_rw_dw ||
766 nofp->nof_d_r_drw || nofp->nof_d_w_drw || nofp->nof_d_rw_drw)) {
767 NP(np, "nfs_vnop_inactive: non-zero access: %d %d %d %d # %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u",
768 nofp->nof_access, nofp->nof_deny,
769 nofp->nof_mmap_access, nofp->nof_mmap_deny,
770 nofp->nof_r, nofp->nof_d_r,
771 nofp->nof_w, nofp->nof_d_w,
772 nofp->nof_rw, nofp->nof_d_rw,
773 nofp->nof_r_dw, nofp->nof_d_r_dw,
774 nofp->nof_w_dw, nofp->nof_d_w_dw,
775 nofp->nof_rw_dw, nofp->nof_d_rw_dw,
776 nofp->nof_r_drw, nofp->nof_d_r_drw,
777 nofp->nof_w_drw, nofp->nof_d_w_drw,
778 nofp->nof_rw_drw, nofp->nof_d_rw_drw);
779 }
780 if (busied) {
781 nfs_open_file_clear_busy(nofp);
782 }
783 }
784 lck_mtx_unlock(&np->n_openlock);
785
786 if (inuse && nfs_mount_state_in_use_end(nmp, error)) {
787 goto restart;
788 }
789
790 nfs_node_lock_force(np);
791
792 if (vnode_vtype(vp) != VDIR) {
793 nsp = np->n_sillyrename;
794 np->n_sillyrename = NULL;
795 } else {
796 nsp = NULL;
797 }
798
799 FSDBG_TOP(264, vp, np, np->n_flag, nsp);
800
801 if (!nsp) {
802 /* no silly file to clean up... */
803 /* clear all flags other than these */
804 np->n_flag &= (NMODIFIED);
805 nfs_node_unlock(np);
806 FSDBG_BOT(264, vp, np, np->n_flag, 0);
807 goto out_free;
808 }
809 nfs_node_unlock(np);
810
811 /* Remove the silly file that was rename'd earlier */
812
813 /* flush all the buffers */
814 nfs_vinvalbuf2(vp, V_SAVE, vfs_context_thread(ctx), nsp->nsr_cred, 1);
815
816 /* try to get the latest attributes */
817 attrerr = nfs_getattr(np, nvattr, ctx, NGA_UNCACHED);
818
819 /* Check if we should remove it from the node hash. */
820 /* Leave it if inuse or it has multiple hard links. */
821 if (vnode_isinuse(vp, 0) || (!attrerr && (nvattr->nva_nlink > 1))) {
822 unhash = 0;
823 } else {
824 unhash = 1;
825 ubc_setsize(vp, 0);
826 }
827
828 if (!vfs_isforce(nmp->nm_mountp)) {
829 /* mark this node and the directory busy while we do the remove */
830 busyerror = nfs_node_set_busy2(nsp->nsr_dnp, np, vfs_context_thread(ctx));
831 } else {
832 /* we are in force unmount we can't trust nsp->nsr_dnp, mark this np busy only */
833 busyerror = nfs_node_set_busy(np, vfs_context_thread(ctx));
834 }
835
836 /* lock the node while we remove the silly file */
837 lck_mtx_lock(&nfs_node_hash_mutex);
838 while (np->n_hflag & NHLOCKED) {
839 np->n_hflag |= NHLOCKWANT;
840 msleep(np, &nfs_node_hash_mutex, PINOD, "nfs_inactive", NULL);
841 }
842 np->n_hflag |= NHLOCKED;
843 lck_mtx_unlock(&nfs_node_hash_mutex);
844
845 if (!vfs_isforce(nmp->nm_mountp)) {
846 /* purge the name cache to deter others from finding it */
847 bzero(&cn, sizeof(cn));
848 cn.cn_nameptr = nsp->nsr_name;
849 cn.cn_namelen = nsp->nsr_namlen;
850 nfs_name_cache_purge(nsp->nsr_dnp, np, &cn, ctx);
851 }
852
853 FSDBG(264, np, np->n_size, np->n_vattr.nva_size, 0xf00d00f1);
854
855 if (!vfs_isforce(nmp->nm_mountp)) {
856 /* now remove the silly file */
857 nfs_removeit(nsp);
858 }
859
860 /* clear all flags other than these */
861 nfs_node_lock_force(np);
862 np->n_flag &= (NMODIFIED);
863 nfs_node_unlock(np);
864
865 if (!busyerror) {
866 if (!vfs_isforce(nmp->nm_mountp)) {
867 nfs_node_clear_busy2(nsp->nsr_dnp, np);
868 } else {
869 nfs_node_clear_busy(np);
870 }
871 }
872
873 if (unhash && vnode_isinuse(vp, 0)) {
874 /* vnode now inuse after silly remove? */
875 unhash = 0;
876 ubc_setsize(vp, np->n_size);
877 }
878
879 lck_mtx_lock(&nfs_node_hash_mutex);
880 if (unhash) {
881 /*
882 * remove nfsnode from hash now so we can't accidentally find it
883 * again if another object gets created with the same filehandle
884 * before this vnode gets reclaimed
885 */
886 if (np->n_hflag & NHHASHED) {
887 LIST_REMOVE(np, n_hash);
888 np->n_hflag &= ~NHHASHED;
889 FSDBG(266, 0, np, np->n_flag, 0xb1eb1e);
890 }
891 vnode_recycle(vp);
892 }
893 /* unlock the node */
894 np->n_hflag &= ~NHLOCKED;
895 if (np->n_hflag & NHLOCKWANT) {
896 np->n_hflag &= ~NHLOCKWANT;
897 wakeup(np);
898 }
899 lck_mtx_unlock(&nfs_node_hash_mutex);
900
901 /* cleanup sillyrename info */
902 if (nsp->nsr_cred != NOCRED) {
903 kauth_cred_unref(&nsp->nsr_cred);
904 }
905 if (!vfs_isforce(nmp->nm_mountp)) {
906 /* in case of forceful unmount usecounts ignore anyways */
907 vnode_rele(NFSTOV(nsp->nsr_dnp));
908 }
909 kfree_type(struct nfs_sillyrename, nsp);
910 FSDBG_BOT(264, vp, np, np->n_flag, 0);
911 out_free:
912 kfree_type(struct nfs_vattr, nvattr);
913 return 0;
914 }
915
916 /*
917 * Reclaim an nfsnode so that it can be used for other purposes.
918 */
919 int
nfs_vnop_reclaim(struct vnop_reclaim_args * ap)920 nfs_vnop_reclaim(
921 struct vnop_reclaim_args /* {
922 * struct vnodeop_desc *a_desc;
923 * vnode_t a_vp;
924 * vfs_context_t a_context;
925 * } */*ap)
926 {
927 vnode_t vp = ap->a_vp;
928 nfsnode_t np = VTONFS(vp);
929 vfs_context_t ctx = ap->a_context;
930 struct nfs_open_file *nofp, *nextnofp;
931 struct nfs_file_lock *nflp, *nextnflp;
932 struct nfs_lock_owner *nlop, *nextnlop;
933 struct nfsmount *nmp = np->n_mount ? VFSTONFS(np->n_mount) : NFSTONMP(np);
934 mount_t mp = vnode_mount(vp);
935 int force;
936
937 FSDBG_TOP(265, vp, np, np->n_flag, 0);
938 force = (!mp || vfs_isforce(mp) || nfs_mount_gone(nmp));
939
940 /* There shouldn't be any open or lock state at this point */
941 lck_mtx_lock(&np->n_openlock);
942
943 #if CONFIG_NFS4
944 if (nmp && (nmp->nm_vers >= NFS_VER4)) {
945 /* need to drop a delegation */
946 if (np->n_dreturn.tqe_next != NFSNOLIST) {
947 /* remove this node from the delegation return list */
948 lck_mtx_lock(&nmp->nm_lock);
949 if (np->n_dreturn.tqe_next != NFSNOLIST) {
950 TAILQ_REMOVE(&nmp->nm_dreturnq, np, n_dreturn);
951 np->n_dreturn.tqe_next = NFSNOLIST;
952 }
953 lck_mtx_unlock(&nmp->nm_lock);
954 }
955 if (np->n_dlink.tqe_next != NFSNOLIST) {
956 /* remove this node from the delegation list */
957 lck_mtx_lock(&nmp->nm_lock);
958 if (np->n_dlink.tqe_next != NFSNOLIST) {
959 TAILQ_REMOVE(&nmp->nm_delegations, np, n_dlink);
960 np->n_dlink.tqe_next = NFSNOLIST;
961 }
962 lck_mtx_unlock(&nmp->nm_lock);
963 }
964 if ((np->n_openflags & N_DELEG_MASK) && !force) {
965 /* try to return the delegation */
966 np->n_openflags &= ~N_DELEG_MASK;
967 nfs4_delegreturn_rpc(nmp, np->n_fhp, np->n_fhsize, &np->n_dstateid,
968 R_RECOVER, vfs_context_thread(ctx), vfs_context_ucred(ctx));
969 }
970 if (np->n_attrdirfh) {
971 kfree_data(np->n_attrdirfh, *np->n_attrdirfh + 1);
972 }
973 }
974 #endif
975
976 /* clean up file locks */
977 TAILQ_FOREACH_SAFE(nflp, &np->n_locks, nfl_link, nextnflp) {
978 if (!(nflp->nfl_flags & NFS_FILE_LOCK_DEAD) && !force) {
979 NP(np, "nfs_vnop_reclaim: lock 0x%llx 0x%llx 0x%x (bc %d)",
980 nflp->nfl_start, nflp->nfl_end, nflp->nfl_flags, nflp->nfl_blockcnt);
981 }
982 if (!(nflp->nfl_flags & (NFS_FILE_LOCK_BLOCKED | NFS_FILE_LOCK_DEAD))) {
983 /* try sending an unlock RPC if it wasn't delegated */
984 if (!(nflp->nfl_flags & NFS_FILE_LOCK_DELEGATED) && !force) {
985 nmp->nm_funcs->nf_unlock_rpc(np, nflp->nfl_owner, F_WRLCK, nflp->nfl_start, nflp->nfl_end, R_RECOVER,
986 NULL, nflp->nfl_owner->nlo_open_owner->noo_cred);
987 }
988 lck_mtx_lock(&nflp->nfl_owner->nlo_lock);
989 TAILQ_REMOVE(&nflp->nfl_owner->nlo_locks, nflp, nfl_lolink);
990 lck_mtx_unlock(&nflp->nfl_owner->nlo_lock);
991 }
992 TAILQ_REMOVE(&np->n_locks, nflp, nfl_link);
993 nfs_file_lock_destroy(np, nflp, vfs_context_thread(ctx), vfs_context_ucred(ctx));
994 }
995 /* clean up lock owners */
996 TAILQ_FOREACH_SAFE(nlop, &np->n_lock_owners, nlo_link, nextnlop) {
997 if (!TAILQ_EMPTY(&nlop->nlo_locks) && !force) {
998 NP(np, "nfs_vnop_reclaim: lock owner with locks");
999 }
1000 TAILQ_REMOVE(&np->n_lock_owners, nlop, nlo_link);
1001 nfs_lock_owner_destroy(nlop);
1002 }
1003 /* clean up open state */
1004 if (np->n_openrefcnt && !force) {
1005 NP(np, "nfs_vnop_reclaim: still open: %d", np->n_openrefcnt);
1006 }
1007 TAILQ_FOREACH_SAFE(nofp, &np->n_opens, nof_link, nextnofp) {
1008 if (nofp->nof_flags & NFS_OPEN_FILE_BUSY) {
1009 NP(np, "nfs_vnop_reclaim: open file busy");
1010 }
1011 if (!(np->n_flag & NREVOKE) && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) {
1012 if (nofp->nof_opencnt && !force) {
1013 NP(np, "nfs_vnop_reclaim: file still open: %d", nofp->nof_opencnt);
1014 }
1015 if (!force && (nofp->nof_access || nofp->nof_deny ||
1016 nofp->nof_mmap_access || nofp->nof_mmap_deny ||
1017 nofp->nof_r || nofp->nof_w || nofp->nof_rw ||
1018 nofp->nof_r_dw || nofp->nof_w_dw || nofp->nof_rw_dw ||
1019 nofp->nof_r_drw || nofp->nof_w_drw || nofp->nof_rw_drw ||
1020 nofp->nof_d_r || nofp->nof_d_w || nofp->nof_d_rw ||
1021 nofp->nof_d_r_dw || nofp->nof_d_w_dw || nofp->nof_d_rw_dw ||
1022 nofp->nof_d_r_drw || nofp->nof_d_w_drw || nofp->nof_d_rw_drw)) {
1023 NP(np, "nfs_vnop_reclaim: non-zero access: %d %d %d %d # %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u",
1024 nofp->nof_access, nofp->nof_deny,
1025 nofp->nof_mmap_access, nofp->nof_mmap_deny,
1026 nofp->nof_r, nofp->nof_d_r,
1027 nofp->nof_w, nofp->nof_d_w,
1028 nofp->nof_rw, nofp->nof_d_rw,
1029 nofp->nof_r_dw, nofp->nof_d_r_dw,
1030 nofp->nof_w_dw, nofp->nof_d_w_dw,
1031 nofp->nof_rw_dw, nofp->nof_d_rw_dw,
1032 nofp->nof_r_drw, nofp->nof_d_r_drw,
1033 nofp->nof_w_drw, nofp->nof_d_w_drw,
1034 nofp->nof_rw_drw, nofp->nof_d_rw_drw);
1035 #if CONFIG_NFS4
1036 /* try sending a close RPC if it wasn't delegated */
1037 if (nofp->nof_r || nofp->nof_w || nofp->nof_rw ||
1038 nofp->nof_r_dw || nofp->nof_w_dw || nofp->nof_rw_dw ||
1039 nofp->nof_r_drw || nofp->nof_w_drw || nofp->nof_rw_drw) {
1040 nfs4_close_rpc(np, nofp, NULL, nofp->nof_owner->noo_cred, R_RECOVER);
1041 }
1042 #endif
1043 }
1044 }
1045 TAILQ_REMOVE(&np->n_opens, nofp, nof_link);
1046 nfs_open_file_destroy(nofp);
1047 }
1048 lck_mtx_unlock(&np->n_openlock);
1049
1050 if (np->n_monlink.le_next != NFSNOLIST) {
1051 /* Wait for any in-progress getattr to complete, */
1052 /* then remove this node from the monitored node list. */
1053 lck_mtx_lock(&nmp->nm_lock);
1054 while (np->n_mflag & NMMONSCANINPROG) {
1055 struct timespec ts = { .tv_sec = 1, .tv_nsec = 0 };
1056 np->n_mflag |= NMMONSCANWANT;
1057 msleep(&np->n_mflag, &nmp->nm_lock, PZERO - 1, "nfswaitmonscan", &ts);
1058 }
1059 if (np->n_monlink.le_next != NFSNOLIST) {
1060 LIST_REMOVE(np, n_monlink);
1061 np->n_monlink.le_next = NFSNOLIST;
1062 }
1063 lck_mtx_unlock(&nmp->nm_lock);
1064 }
1065
1066 lck_mtx_lock(&nfs_buf_mutex);
1067 if (!force && (!LIST_EMPTY(&np->n_dirtyblkhd) || !LIST_EMPTY(&np->n_cleanblkhd))) {
1068 NP(np, "nfs_reclaim: dropping %s buffers", (!LIST_EMPTY(&np->n_dirtyblkhd) ? "dirty" : "clean"));
1069 }
1070 lck_mtx_unlock(&nfs_buf_mutex);
1071 nfs_vinvalbuf1(vp, V_IGNORE_WRITEERR, ap->a_context, 0);
1072
1073 lck_mtx_lock(&nfs_node_hash_mutex);
1074
1075 if ((vnode_vtype(vp) != VDIR) && np->n_sillyrename) {
1076 if (!force) {
1077 NP(np, "nfs_reclaim: leaving unlinked file %s", np->n_sillyrename->nsr_name);
1078 }
1079 if (np->n_sillyrename->nsr_cred != NOCRED) {
1080 kauth_cred_unref(&np->n_sillyrename->nsr_cred);
1081 }
1082 vnode_rele(NFSTOV(np->n_sillyrename->nsr_dnp));
1083 kfree_type(struct nfs_sillyrename, np->n_sillyrename);
1084 }
1085
1086 vnode_removefsref(vp);
1087
1088 if (np->n_hflag & NHHASHED) {
1089 LIST_REMOVE(np, n_hash);
1090 np->n_hflag &= ~NHHASHED;
1091 FSDBG(266, 0, np, np->n_flag, 0xb1eb1e);
1092 }
1093 lck_mtx_unlock(&nfs_node_hash_mutex);
1094
1095 /*
1096 * Free up any directory cookie structures and large file handle
1097 * structures that might be associated with this nfs node.
1098 */
1099 nfs_node_lock_force(np);
1100 if ((vnode_vtype(vp) == VDIR) && np->n_cookiecache) {
1101 NFS_ZFREE(ZV_NFSDIROFF, np->n_cookiecache);
1102 }
1103 if (np->n_fhsize > NFS_SMALLFH) {
1104 kfree_data(np->n_fhp, np->n_fhsize);
1105 }
1106 if (np->n_vattr.nva_acl) {
1107 kauth_acl_free(np->n_vattr.nva_acl);
1108 }
1109 nfs_node_unlock(np);
1110 vnode_clearfsnode(vp);
1111
1112 if (np->n_parent) {
1113 if (!vnode_get(np->n_parent)) {
1114 vnode_rele(np->n_parent);
1115 vnode_put(np->n_parent);
1116 }
1117 np->n_parent = NULL;
1118 }
1119
1120 lck_mtx_destroy(&np->n_lock, &nfs_node_lck_grp);
1121 lck_rw_destroy(&np->n_datalock, &nfs_data_lck_grp);
1122 lck_mtx_destroy(&np->n_openlock, &nfs_open_grp);
1123
1124 FSDBG_BOT(265, vp, np, np->n_flag, 0xd1ed1e);
1125 NFS_ZFREE(nfsnode_zone, np);
1126 return 0;
1127 }
1128
1129 /*
1130 * Acquire an NFS node lock
1131 */
1132
1133 int
nfs_node_lock_internal(nfsnode_t np,int force)1134 nfs_node_lock_internal(nfsnode_t np, int force)
1135 {
1136 FSDBG_TOP(268, np, force, 0, 0);
1137 lck_mtx_lock(&np->n_lock);
1138 if (!force && !(np->n_hflag && NHHASHED)) {
1139 FSDBG_BOT(268, np, 0xdead, 0, 0);
1140 lck_mtx_unlock(&np->n_lock);
1141 return ENOENT;
1142 }
1143 FSDBG_BOT(268, np, force, 0, 0);
1144 return 0;
1145 }
1146
1147 int
nfs_node_lock(nfsnode_t np)1148 nfs_node_lock(nfsnode_t np)
1149 {
1150 return nfs_node_lock_internal(np, 0);
1151 }
1152
1153 void
nfs_node_lock_force(nfsnode_t np)1154 nfs_node_lock_force(nfsnode_t np)
1155 {
1156 nfs_node_lock_internal(np, 1);
1157 }
1158
1159 /*
1160 * Release an NFS node lock
1161 */
1162 void
nfs_node_unlock(nfsnode_t np)1163 nfs_node_unlock(nfsnode_t np)
1164 {
1165 FSDBG(269, np, current_thread(), 0, 0);
1166 lck_mtx_unlock(&np->n_lock);
1167 }
1168
1169 /*
1170 * Acquire 2 NFS node locks
1171 * - locks taken in reverse address order
1172 * - both or neither of the locks are taken
1173 * - only one lock taken per node (dup nodes are skipped)
1174 */
1175 int
nfs_node_lock2(nfsnode_t np1,nfsnode_t np2)1176 nfs_node_lock2(nfsnode_t np1, nfsnode_t np2)
1177 {
1178 nfsnode_t first, second;
1179 int error;
1180
1181 first = (np1 > np2) ? np1 : np2;
1182 second = (np1 > np2) ? np2 : np1;
1183 if ((error = nfs_node_lock(first))) {
1184 return error;
1185 }
1186 if (np1 == np2) {
1187 return error;
1188 }
1189 if ((error = nfs_node_lock(second))) {
1190 nfs_node_unlock(first);
1191 }
1192 return error;
1193 }
1194
1195 void
nfs_node_unlock2(nfsnode_t np1,nfsnode_t np2)1196 nfs_node_unlock2(nfsnode_t np1, nfsnode_t np2)
1197 {
1198 nfs_node_unlock(np1);
1199 if (np1 != np2) {
1200 nfs_node_unlock(np2);
1201 }
1202 }
1203
1204 /*
1205 * Manage NFS node busy state.
1206 * (Similar to NFS node locks above)
1207 */
1208 int
nfs_node_set_busy(nfsnode_t np,thread_t thd)1209 nfs_node_set_busy(nfsnode_t np, thread_t thd)
1210 {
1211 struct timespec ts = { .tv_sec = 2, .tv_nsec = 0 };
1212 int error;
1213
1214 if ((error = nfs_node_lock(np))) {
1215 return error;
1216 }
1217 while (ISSET(np->n_flag, NBUSY)) {
1218 SET(np->n_flag, NBUSYWANT);
1219 msleep(np, &np->n_lock, PZERO - 1, "nfsbusywant", &ts);
1220 if ((error = nfs_sigintr(NFSTONMP(np), NULL, thd, 0))) {
1221 break;
1222 }
1223 }
1224 if (!error) {
1225 SET(np->n_flag, NBUSY);
1226 }
1227 nfs_node_unlock(np);
1228 return error;
1229 }
1230
1231 void
nfs_node_clear_busy(nfsnode_t np)1232 nfs_node_clear_busy(nfsnode_t np)
1233 {
1234 int wanted;
1235
1236 nfs_node_lock_force(np);
1237 wanted = ISSET(np->n_flag, NBUSYWANT);
1238 CLR(np->n_flag, NBUSY | NBUSYWANT);
1239 nfs_node_unlock(np);
1240 if (wanted) {
1241 wakeup(np);
1242 }
1243 }
1244
1245 int
nfs_node_set_busy2(nfsnode_t np1,nfsnode_t np2,thread_t thd)1246 nfs_node_set_busy2(nfsnode_t np1, nfsnode_t np2, thread_t thd)
1247 {
1248 nfsnode_t first, second;
1249 int error;
1250
1251 first = (np1 > np2) ? np1 : np2;
1252 second = (np1 > np2) ? np2 : np1;
1253 if ((error = nfs_node_set_busy(first, thd))) {
1254 return error;
1255 }
1256 if (np1 == np2) {
1257 return error;
1258 }
1259 if ((error = nfs_node_set_busy(second, thd))) {
1260 nfs_node_clear_busy(first);
1261 }
1262 return error;
1263 }
1264
1265 void
nfs_node_clear_busy2(nfsnode_t np1,nfsnode_t np2)1266 nfs_node_clear_busy2(nfsnode_t np1, nfsnode_t np2)
1267 {
1268 nfs_node_clear_busy(np1);
1269 if (np1 != np2) {
1270 nfs_node_clear_busy(np2);
1271 }
1272 }
1273
1274 /* helper function to sort four nodes in reverse address order (no dupes) */
1275 static void
nfs_node_sort4(nfsnode_t np1,nfsnode_t np2,nfsnode_t np3,nfsnode_t np4,nfsnode_t * list,int * lcntp)1276 nfs_node_sort4(nfsnode_t np1, nfsnode_t np2, nfsnode_t np3, nfsnode_t np4, nfsnode_t *list, int *lcntp)
1277 {
1278 nfsnode_t na[2], nb[2];
1279 int a, b, i, lcnt;
1280
1281 /* sort pairs then merge */
1282 na[0] = (np1 > np2) ? np1 : np2;
1283 na[1] = (np1 > np2) ? np2 : np1;
1284 nb[0] = (np3 > np4) ? np3 : np4;
1285 nb[1] = (np3 > np4) ? np4 : np3;
1286 for (a = b = i = lcnt = 0; i < 4; i++) {
1287 if (a >= 2) {
1288 list[lcnt] = nb[b++];
1289 } else if ((b >= 2) || (na[a] >= nb[b])) {
1290 list[lcnt] = na[a++];
1291 } else {
1292 list[lcnt] = nb[b++];
1293 }
1294 if ((lcnt <= 0) || (list[lcnt] != list[lcnt - 1])) {
1295 lcnt++; /* omit dups */
1296 }
1297 }
1298 if (list[lcnt - 1] == NULL) {
1299 lcnt--;
1300 }
1301 *lcntp = lcnt;
1302 }
1303
1304 int
nfs_node_set_busy4(nfsnode_t np1,nfsnode_t np2,nfsnode_t np3,nfsnode_t np4,thread_t thd)1305 nfs_node_set_busy4(nfsnode_t np1, nfsnode_t np2, nfsnode_t np3, nfsnode_t np4, thread_t thd)
1306 {
1307 nfsnode_t list[4];
1308 int i, lcnt, error;
1309
1310 nfs_node_sort4(np1, np2, np3, np4, list, &lcnt);
1311
1312 /* Now we can lock using list[0 - lcnt-1] */
1313 for (i = 0; i < lcnt; ++i) {
1314 if ((error = nfs_node_set_busy(list[i], thd))) {
1315 /* Drop any locks we acquired. */
1316 while (--i >= 0) {
1317 nfs_node_clear_busy(list[i]);
1318 }
1319 return error;
1320 }
1321 }
1322 return 0;
1323 }
1324
1325 void
nfs_node_clear_busy4(nfsnode_t np1,nfsnode_t np2,nfsnode_t np3,nfsnode_t np4)1326 nfs_node_clear_busy4(nfsnode_t np1, nfsnode_t np2, nfsnode_t np3, nfsnode_t np4)
1327 {
1328 nfsnode_t list[4];
1329 int lcnt;
1330
1331 nfs_node_sort4(np1, np2, np3, np4, list, &lcnt);
1332 while (--lcnt >= 0) {
1333 nfs_node_clear_busy(list[lcnt]);
1334 }
1335 }
1336
1337 /*
1338 * Acquire an NFS node data lock
1339 */
1340 void
nfs_data_lock(nfsnode_t np,int locktype)1341 nfs_data_lock(nfsnode_t np, int locktype)
1342 {
1343 nfs_data_lock_internal(np, locktype, 1);
1344 }
1345 void
nfs_data_lock_noupdate(nfsnode_t np,int locktype)1346 nfs_data_lock_noupdate(nfsnode_t np, int locktype)
1347 {
1348 nfs_data_lock_internal(np, locktype, 0);
1349 }
1350 void
nfs_data_lock_internal(nfsnode_t np,int locktype,int updatesize)1351 nfs_data_lock_internal(nfsnode_t np, int locktype, int updatesize)
1352 {
1353 FSDBG_TOP(270, np, locktype, np->n_datalockowner, 0);
1354 if (locktype == NFS_DATA_LOCK_SHARED) {
1355 if (updatesize && ISSET(np->n_flag, NUPDATESIZE)) {
1356 nfs_data_update_size(np, 0);
1357 }
1358 lck_rw_lock_shared(&np->n_datalock);
1359 } else {
1360 lck_rw_lock_exclusive(&np->n_datalock);
1361 np->n_datalockowner = current_thread();
1362 if (updatesize && ISSET(np->n_flag, NUPDATESIZE)) {
1363 nfs_data_update_size(np, 1);
1364 }
1365 }
1366 FSDBG_BOT(270, np, locktype, np->n_datalockowner, 0);
1367 }
1368
1369 /*
1370 * Release an NFS node data lock
1371 */
1372 void
nfs_data_unlock(nfsnode_t np)1373 nfs_data_unlock(nfsnode_t np)
1374 {
1375 nfs_data_unlock_internal(np, 1);
1376 }
1377 void
nfs_data_unlock_noupdate(nfsnode_t np)1378 nfs_data_unlock_noupdate(nfsnode_t np)
1379 {
1380 nfs_data_unlock_internal(np, 0);
1381 }
1382 void
nfs_data_unlock_internal(nfsnode_t np,int updatesize)1383 nfs_data_unlock_internal(nfsnode_t np, int updatesize)
1384 {
1385 int mine = (np->n_datalockowner == current_thread());
1386 FSDBG_TOP(271, np, np->n_datalockowner, current_thread(), 0);
1387 if (updatesize && mine && ISSET(np->n_flag, NUPDATESIZE)) {
1388 nfs_data_update_size(np, 1);
1389 }
1390 np->n_datalockowner = NULL;
1391 lck_rw_done(&np->n_datalock);
1392 if (updatesize && !mine && ISSET(np->n_flag, NUPDATESIZE)) {
1393 nfs_data_update_size(np, 0);
1394 }
1395 FSDBG_BOT(271, np, np->n_datalockowner, current_thread(), 0);
1396 }
1397
1398
1399 /*
1400 * update an NFS node's size
1401 */
1402 void
nfs_data_update_size(nfsnode_t np,int datalocked)1403 nfs_data_update_size(nfsnode_t np, int datalocked)
1404 {
1405 int error;
1406
1407 FSDBG_TOP(272, np, np->n_flag, np->n_size, np->n_newsize);
1408 if (!datalocked) {
1409 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
1410 /* grabbing data lock will automatically update size */
1411 nfs_data_unlock(np);
1412 FSDBG_BOT(272, np, np->n_flag, np->n_size, np->n_newsize);
1413 return;
1414 }
1415 error = nfs_node_lock(np);
1416 if (error || !ISSET(np->n_flag, NUPDATESIZE)) {
1417 if (!error) {
1418 nfs_node_unlock(np);
1419 }
1420 FSDBG_BOT(272, np, np->n_flag, np->n_size, np->n_newsize);
1421 return;
1422 }
1423 CLR(np->n_flag, NUPDATESIZE);
1424 np->n_size = np->n_newsize;
1425 /* make sure we invalidate buffers the next chance we get */
1426 SET(np->n_flag, NNEEDINVALIDATE);
1427 nfs_node_unlock(np);
1428 ubc_setsize(NFSTOV(np), (off_t)np->n_size); /* XXX error? */
1429 FSDBG_BOT(272, np, np->n_flag, np->n_size, np->n_newsize);
1430 }
1431
1432 #define DODEBUG 1
1433
1434 int
nfs_mount_is_dirty(mount_t mp)1435 nfs_mount_is_dirty(mount_t mp)
1436 {
1437 u_long i;
1438 nfsnode_t np;
1439 #ifdef DODEBUG
1440 struct timeval now, then, diff;
1441 u_long ncnt = 0;
1442 microuptime(&now);
1443 #endif
1444 lck_mtx_lock(&nfs_node_hash_mutex);
1445 for (i = 0; i <= nfsnodehash; i++) {
1446 LIST_FOREACH(np, &nfsnodehashtbl[i], n_hash) {
1447 #ifdef DODEBUG
1448 ncnt++;
1449 #endif
1450 if (np->n_mount == mp && !LIST_EMPTY(&np->n_dirtyblkhd)) {
1451 goto out;
1452 }
1453 }
1454 }
1455 out:
1456 lck_mtx_unlock(&nfs_node_hash_mutex);
1457 #ifdef DODEBUG
1458 microuptime(&then);
1459 timersub(&then, &now, &diff);
1460
1461 NFSCLNT_DBG(NFSCLNT_FAC_SOCK, 7, "mount_is_dirty for %s took %lld mics for %ld slots and %ld nodes return %d\n",
1462 vfs_statfs(mp)->f_mntfromname, (uint64_t)diff.tv_sec * 1000000LL + diff.tv_usec, i, ncnt, (i <= nfsnodehash));
1463 #endif
1464
1465 return i <= nfsnodehash;
1466 }
1467
1468 #endif /* CONFIG_NFS_CLIENT */
1469