2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
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 $
68 #include <nfs/nfs_conf.h>
71 #include <sys/param.h>
72 #include <sys/kernel.h>
73 #include <sys/systm.h>
75 #include <sys/kauth.h>
76 #include <sys/mount_internal.h>
77 #include <sys/vnode_internal.h>
78 #include <sys/vnode.h>
80 #include <sys/malloc.h>
81 #include <sys/fcntl.h>
84 #include <nfs/rpcv2.h>
85 #include <nfs/nfsproto.h>
87 #include <nfs/nfsnode.h>
88 #include <nfs/nfs_gss.h>
89 #include <nfs/nfsmount.h>
91 #define NFSNOHASH(fhsum) \
92 (&nfsnodehashtbl[(fhsum) & nfsnodehash])
93 static LIST_HEAD(nfsnodehashhead
, nfsnode
) * nfsnodehashtbl
;
94 static u_long nfsnodehash
;
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
);
101 ZONE_DECLARE(nfsnode_zone
, "NFS node",
102 sizeof(struct nfsnode
), ZC_ZFREE_CLEARMEM
);
104 #define NFS_NODE_DBG(...) NFS_DBG(NFS_FAC_NODE, 7, ## __VA_ARGS__)
107 nfs_nhinit_finish(void)
109 lck_mtx_lock(&nfs_node_hash_mutex
);
110 if (!nfsnodehashtbl
) {
111 nfsnodehashtbl
= hashinit(desiredvnodes
, M_NFSNODE
, &nfsnodehash
);
113 lck_mtx_unlock(&nfs_node_hash_mutex
);
117 * Compute an entry in the NFS hash table structure
120 nfs_hash(u_char
*fhp
, int fhsize
)
126 for (i
= 0; i
< fhsize
; i
++) {
133 int nfs_case_insensitive(mount_t
);
136 nfs_case_insensitive(mount_t mp
)
138 struct nfsmount
*nmp
= VFSTONFS(mp
);
142 if (nfs_mount_gone(nmp
)) {
146 if (nmp
->nm_vers
== NFS_VER2
) {
147 /* V2 has no way to know */
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
160 } else if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
)) {
161 /* no pathconf info cached */
165 if (!skip
&& (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
)) {
169 lck_mtx_unlock(&nmp
->nm_lock
);
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.
185 struct componentname
*cnp
,
188 struct nfs_vattr
*nvap
,
195 struct nfsnodehashhead
*nhpp
;
199 struct vnode_fsparam vfsp
;
200 uint32_t vid
, cn_namelen
;
203 FSDBG_TOP(263, mp
, dnp
, flags
, npp
);
205 /* Check for unmount in progress */
206 if (!mp
|| vfs_isforce(mp
)) {
209 FSDBG_BOT(263, mp
, dnp
, 0xd1e, error
);
212 nfsvers
= VFSTONFS(mp
)->nm_vers
;
213 cn_namelen
= cnp
? cnp
->cn_namelen
: 0;
214 nfshash
= nfs_hash(fhp
, fhsize
);
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
)) {
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. */
228 const char *vname
= vnode_getname(NFSTOV(np
));
230 if (cn_namelen
!= strlen(vname
)) {
233 namecmp
= strncmp(vname
, cnp
->cn_nameptr
, cn_namelen
);
235 vnode_putname(vname
);
237 if (namecmp
) { /* full name didn't match */
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);
252 lck_mtx_unlock(&nfs_node_hash_mutex
);
253 if ((error
= vnode_getwithvid(vp
, vid
))) {
255 * If vnode is being reclaimed or has already
256 * changed identity, no need to wait.
258 FSDBG_BOT(263, dnp
, *npp
, 0xcace0d1e, error
);
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
);
266 if (flags
& NG_NOCREATE
) {
268 FSDBG_BOT(263, dnp
, *npp
, 0xcaced1e0, ENOENT
);
273 /* update attributes */
275 error
= nfs_loadattrcache(np
, nvap
, xidp
, 0);
281 if (dnp
&& cnp
&& (flags
& NG_MAKEENTRY
)) {
282 cache_enter(NFSTOV(dnp
), vp
, cnp
);
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.
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
328 * Note that V2 does not know the case, so we just
329 * assume case sensitivity.
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:
335 * while :; do ls -l > /dev/null; done
337 * in a terminal window. Even a single ls -l can cause a
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>
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
);
349 cmp
= nfs_case_insensitive(mp
) ? strncasecmp
: strncmp
;
351 if (vp
->v_name
&& cn_namelen
!= strnlen(vp
->v_name
, MAXPATHLEN
)) {
352 update_flags
|= VNODE_UPDATE_NAME
;
354 if (vp
->v_name
&& cn_namelen
&& (*cmp
)(cnp
->cn_nameptr
, vp
->v_name
, cn_namelen
)) {
355 update_flags
|= VNODE_UPDATE_NAME
;
357 if ((vp
->v_name
== NULL
&& cn_namelen
!= 0) || (vp
->v_name
!= NULL
&& cn_namelen
== 0)) {
358 update_flags
|= VNODE_UPDATE_NAME
;
360 if (vnode_parent(vp
) != NFSTOV(dnp
)) {
361 update_flags
|= VNODE_UPDATE_PARENT
;
364 NFS_NODE_DBG("vnode_update_identity old name %s new name %.*s update flags = %x\n",
365 vp
->v_name
, 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
);
372 FSDBG_BOT(263, dnp
, *npp
, 0xcace0000, error
);
376 FSDBG(263, mp
, dnp
, npp
, 0xaaaaaaaa);
378 if (flags
& NG_NOCREATE
) {
379 lck_mtx_unlock(&nfs_node_hash_mutex
);
381 FSDBG_BOT(263, dnp
, *npp
, 0x80000001, ENOENT
);
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.
390 np
= zalloc_flags(nfsnode_zone
, Z_WAITOK
| Z_ZERO
);
391 np
->n_hflag
|= (NHINIT
| NHLOCKED
);
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
;
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
;
407 if (dnp
&& (dnp
->n_flag
& NISDOTZFS
)) {
408 np
->n_flag
|= NISDOTZFSCHILD
;
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
)) {
422 /* setup node's file handle */
423 if (fhsize
> NFS_SMALLFH
) {
424 MALLOC(np
->n_fhp
, u_char
*, fhsize
, M_NFSBIGFH
, M_WAITOK
);
426 lck_mtx_unlock(&nfs_node_hash_mutex
);
427 NFS_ZFREE(nfsnode_zone
, np
);
429 FSDBG_BOT(263, dnp
, *npp
, 0x80000002, ENOMEM
);
433 np
->n_fhp
= &np
->n_fh
[0];
435 bcopy(fhp
, np
->n_fhp
, fhsize
);
436 np
->n_fhsize
= fhsize
;
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
);
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
);
449 /* release lock on hash table */
450 lck_mtx_unlock(&nfs_node_hash_mutex
);
452 /* do initial loading of attributes */
454 NACCESSINVALIDATE(np
);
455 error
= nfs_loadattrcache(np
, nvap
, xidp
, 1);
457 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
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
;
466 lck_mtx_unlock(&nfs_node_hash_mutex
);
468 if (!vnode_get(np
->n_parent
)) {
469 vnode_rele(np
->n_parent
);
470 vnode_put(np
->n_parent
);
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 FREE(np
->n_fhp
, M_NFSBIGFH
);
480 NFS_ZFREE(nfsnode_zone
, np
);
482 FSDBG_BOT(263, dnp
, *npp
, 0x80000003, error
);
485 NFS_CHANGED_UPDATE(nfsvers
, np
, nvap
);
486 if (nvap
->nva_type
== VDIR
) {
487 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, nvap
);
490 /* now, attempt to get a new vnode */
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
;
497 if (nfsvers
== NFS_VER4
) {
499 if (nvap
->nva_type
== VFIFO
) {
500 vfsp
.vnfs_vops
= fifo_nfsv4nodeop_p
;
503 if (nvap
->nva_type
== VBLK
|| nvap
->nva_type
== VCHR
) {
504 vfsp
.vnfs_vops
= spec_nfsv4nodeop_p
;
506 vfsp
.vnfs_vops
= nfsv4_vnodeop_p
;
509 #endif /* CONFIG_NFS4 */
512 if (nvap
->nva_type
== VFIFO
) {
513 vfsp
.vnfs_vops
= fifo_nfsv2nodeop_p
;
516 if (nvap
->nva_type
== VBLK
|| nvap
->nva_type
== VCHR
) {
517 vfsp
.vnfs_vops
= spec_nfsv2nodeop_p
;
519 vfsp
.vnfs_vops
= nfsv2_vnodeop_p
;
522 vfsp
.vnfs_markroot
= (flags
& NG_MARKROOT
) ? 1 : 0;
523 vfsp
.vnfs_marksystem
= 0;
525 vfsp
.vnfs_filesize
= nvap
->nva_size
;
527 vfsp
.vnfs_flags
= VNFS_ADDFSREF
;
528 if (!dnp
|| !cnp
|| !(flags
& NG_MAKEENTRY
)) {
529 vfsp
.vnfs_flags
|= VNFS_NOCACHE
;
533 if (((nfsvers
>= NFS_VER4
)
535 && (nvap
->nva_type
== VDIR
) && (np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)
536 && !(flags
& NG_MARKROOT
)) {
537 struct vnode_trigger_param vtp
;
538 bzero(&vtp
, sizeof(vtp
));
539 bcopy(&vfsp
, &vtp
.vnt_params
, sizeof(vfsp
));
540 vtp
.vnt_resolve_func
= nfs_mirror_mount_trigger_resolve
;
541 vtp
.vnt_unresolve_func
= nfs_mirror_mount_trigger_unresolve
;
542 vtp
.vnt_rearm_func
= nfs_mirror_mount_trigger_rearm
;
543 vtp
.vnt_flags
= VNT_AUTO_REARM
| VNT_KERN_RESOLVE
;
544 error
= vnode_create(VNCREATE_TRIGGER
, VNCREATE_TRIGGER_SIZE
, &vtp
, &np
->n_vnode
);
548 error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &np
->n_vnode
);
551 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
553 lck_mtx_lock(&nfs_node_hash_mutex
);
554 LIST_REMOVE(np
, n_hash
);
555 np
->n_hflag
&= ~(NHHASHED
| NHINIT
| NHLOCKED
);
556 if (np
->n_hflag
& NHLOCKWANT
) {
557 np
->n_hflag
&= ~NHLOCKWANT
;
560 lck_mtx_unlock(&nfs_node_hash_mutex
);
562 if (!vnode_get(np
->n_parent
)) {
563 vnode_rele(np
->n_parent
);
564 vnode_put(np
->n_parent
);
568 lck_mtx_destroy(&np
->n_lock
, &nfs_node_lck_grp
);
569 lck_rw_destroy(&np
->n_datalock
, &nfs_data_lck_grp
);
570 lck_mtx_destroy(&np
->n_openlock
, &nfs_open_grp
);
571 if (np
->n_fhsize
> NFS_SMALLFH
) {
572 FREE(np
->n_fhp
, M_NFSBIGFH
);
574 NFS_ZFREE(nfsnode_zone
, np
);
576 FSDBG_BOT(263, dnp
, *npp
, 0x80000004, error
);
580 vnode_settag(vp
, VT_NFS
);
581 /* node is now initialized */
583 /* check if anyone's waiting on this node */
584 lck_mtx_lock(&nfs_node_hash_mutex
);
585 np
->n_hflag
&= ~(NHINIT
| NHLOCKED
);
586 if (np
->n_hflag
& NHLOCKWANT
) {
587 np
->n_hflag
&= ~NHLOCKWANT
;
590 lck_mtx_unlock(&nfs_node_hash_mutex
);
594 FSDBG_BOT(263, dnp
, vp
, *npp
, error
);
601 struct vnop_inactive_args
/* {
602 * struct vnodeop_desc *a_desc;
604 * vfs_context_t a_context;
607 vnode_t vp
= ap
->a_vp
;
608 vfs_context_t ctx
= ap
->a_context
;
610 struct nfs_sillyrename
*nsp
;
611 struct nfs_vattr
*nvattr
;
612 int unhash
, attrerr
, busyerror
, error
, inuse
, busied
, force
;
613 struct nfs_open_file
*nofp
;
614 struct componentname cn
;
615 struct nfsmount
*nmp
;
619 panic("nfs_vnop_inactive: vp == NULL");
623 panic("nfs_vnop_inactive: np == NULL");
627 mp
= vnode_mount(vp
);
628 MALLOC(nvattr
, struct nfs_vattr
*, sizeof(*nvattr
), M_TEMP
, M_WAITOK
);
631 force
= (!mp
|| vfs_isforce(mp
));
633 inuse
= (nfs_mount_state_in_use_start(nmp
, NULL
) == 0);
635 /* There shouldn't be any open or lock state at this point */
636 lck_mtx_lock(&np
->n_openlock
);
637 if (np
->n_openrefcnt
&& !force
) {
639 * vnode_rele and vnode_put drop the vnode lock before
640 * calling VNOP_INACTIVE, so there is a race were the
641 * vnode could become active again. Perhaps there are
642 * other places where this can happen, so if we've got
643 * here we need to get out.
645 #ifdef NFS_NODE_DEBUG
646 NP(np
, "nfs_vnop_inactive: still open: %d", np
->n_openrefcnt
);
648 lck_mtx_unlock(&np
->n_openlock
);
650 nfs_mount_state_in_use_end(nmp
, 0);
655 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
656 lck_mtx_lock(&nofp
->nof_lock
);
657 if (nofp
->nof_flags
& NFS_OPEN_FILE_BUSY
) {
659 NP(np
, "nfs_vnop_inactive: open file busy");
663 nofp
->nof_flags
|= NFS_OPEN_FILE_BUSY
;
666 lck_mtx_unlock(&nofp
->nof_lock
);
667 if ((np
->n_flag
& NREVOKE
) || (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
669 nfs_open_file_clear_busy(nofp
);
674 * If we just created the file, we already had it open in
675 * anticipation of getting a subsequent open call. If the
676 * node has gone inactive without being open, we need to
677 * clean up (close) the open done in the create.
680 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) && nofp
->nof_creator
&& !force
) {
681 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
682 lck_mtx_unlock(&np
->n_openlock
);
684 nfs_open_file_clear_busy(nofp
);
686 if (!nfs4_reopen(nofp
, NULL
)) {
688 nfs_mount_state_in_use_end(nmp
, 0);
693 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
694 lck_mtx_unlock(&np
->n_openlock
);
695 error
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
697 NP(np
, "nfs_vnop_inactive: create close error: %d", error
);
698 nofp
->nof_flags
|= NFS_OPEN_FILE_CREATE
;
701 nfs_open_file_clear_busy(nofp
);
704 nfs_mount_state_in_use_end(nmp
, error
);
709 if (nofp
->nof_flags
& NFS_OPEN_FILE_NEEDCLOSE
) {
711 * If the file is marked as needing reopen, but this was the only
712 * open on the file, just drop the open.
714 nofp
->nof_flags
&= ~NFS_OPEN_FILE_NEEDCLOSE
;
715 if ((nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) && (nofp
->nof_opencnt
== 1)) {
716 nofp
->nof_flags
&= ~NFS_OPEN_FILE_REOPEN
;
719 nofp
->nof_access
= 0;
721 lck_mtx_unlock(&np
->n_openlock
);
722 if (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
) {
723 int should_restart
= 0;
725 nfs_open_file_clear_busy(nofp
);
728 if (!nfs4_reopen(nofp
, NULL
)) {
732 if (should_restart
) {
734 nfs_mount_state_in_use_end(nmp
, 0);
739 error
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_READ
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
741 NP(np
, "nfs_vnop_inactive: need close error: %d", error
);
742 nofp
->nof_flags
|= NFS_OPEN_FILE_NEEDCLOSE
;
745 nfs_open_file_clear_busy(nofp
);
748 nfs_mount_state_in_use_end(nmp
, error
);
753 if (nofp
->nof_opencnt
&& !force
) {
754 NP(np
, "nfs_vnop_inactive: file still open: %d", nofp
->nof_opencnt
);
756 if (!force
&& (nofp
->nof_access
|| nofp
->nof_deny
||
757 nofp
->nof_mmap_access
|| nofp
->nof_mmap_deny
||
758 nofp
->nof_r
|| nofp
->nof_w
|| nofp
->nof_rw
||
759 nofp
->nof_r_dw
|| nofp
->nof_w_dw
|| nofp
->nof_rw_dw
||
760 nofp
->nof_r_drw
|| nofp
->nof_w_drw
|| nofp
->nof_rw_drw
||
761 nofp
->nof_d_r
|| nofp
->nof_d_w
|| nofp
->nof_d_rw
||
762 nofp
->nof_d_r_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_rw_dw
||
763 nofp
->nof_d_r_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_rw_drw
)) {
764 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",
765 nofp
->nof_access
, nofp
->nof_deny
,
766 nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
,
767 nofp
->nof_r
, nofp
->nof_d_r
,
768 nofp
->nof_w
, nofp
->nof_d_w
,
769 nofp
->nof_rw
, nofp
->nof_d_rw
,
770 nofp
->nof_r_dw
, nofp
->nof_d_r_dw
,
771 nofp
->nof_w_dw
, nofp
->nof_d_w_dw
,
772 nofp
->nof_rw_dw
, nofp
->nof_d_rw_dw
,
773 nofp
->nof_r_drw
, nofp
->nof_d_r_drw
,
774 nofp
->nof_w_drw
, nofp
->nof_d_w_drw
,
775 nofp
->nof_rw_drw
, nofp
->nof_d_rw_drw
);
778 nfs_open_file_clear_busy(nofp
);
781 lck_mtx_unlock(&np
->n_openlock
);
783 if (inuse
&& nfs_mount_state_in_use_end(nmp
, error
)) {
787 nfs_node_lock_force(np
);
789 if (vnode_vtype(vp
) != VDIR
) {
790 nsp
= np
->n_sillyrename
;
791 np
->n_sillyrename
= NULL
;
796 FSDBG_TOP(264, vp
, np
, np
->n_flag
, nsp
);
799 /* no silly file to clean up... */
800 /* clear all flags other than these */
801 np
->n_flag
&= (NMODIFIED
);
803 FSDBG_BOT(264, vp
, np
, np
->n_flag
, 0);
808 /* Remove the silly file that was rename'd earlier */
810 /* flush all the buffers */
811 nfs_vinvalbuf2(vp
, V_SAVE
, vfs_context_thread(ctx
), nsp
->nsr_cred
, 1);
813 /* try to get the latest attributes */
814 attrerr
= nfs_getattr(np
, nvattr
, ctx
, NGA_UNCACHED
);
816 /* Check if we should remove it from the node hash. */
817 /* Leave it if inuse or it has multiple hard links. */
818 if (vnode_isinuse(vp
, 0) || (!attrerr
&& (nvattr
->nva_nlink
> 1))) {
825 if (!vfs_isforce(nmp
->nm_mountp
)) {
826 /* mark this node and the directory busy while we do the remove */
827 busyerror
= nfs_node_set_busy2(nsp
->nsr_dnp
, np
, vfs_context_thread(ctx
));
829 /* we are in force unmount we can't trust nsp->nsr_dnp, mark this np busy only */
830 busyerror
= nfs_node_set_busy(np
, vfs_context_thread(ctx
));
833 /* lock the node while we remove the silly file */
834 lck_mtx_lock(&nfs_node_hash_mutex
);
835 while (np
->n_hflag
& NHLOCKED
) {
836 np
->n_hflag
|= NHLOCKWANT
;
837 msleep(np
, &nfs_node_hash_mutex
, PINOD
, "nfs_inactive", NULL
);
839 np
->n_hflag
|= NHLOCKED
;
840 lck_mtx_unlock(&nfs_node_hash_mutex
);
842 if (!vfs_isforce(nmp
->nm_mountp
)) {
843 /* purge the name cache to deter others from finding it */
844 bzero(&cn
, sizeof(cn
));
845 cn
.cn_nameptr
= nsp
->nsr_name
;
846 cn
.cn_namelen
= nsp
->nsr_namlen
;
847 nfs_name_cache_purge(nsp
->nsr_dnp
, np
, &cn
, ctx
);
850 FSDBG(264, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d00f1);
852 if (!vfs_isforce(nmp
->nm_mountp
)) {
853 /* now remove the silly file */
857 /* clear all flags other than these */
858 nfs_node_lock_force(np
);
859 np
->n_flag
&= (NMODIFIED
);
863 if (!vfs_isforce(nmp
->nm_mountp
)) {
864 nfs_node_clear_busy2(nsp
->nsr_dnp
, np
);
866 nfs_node_clear_busy(np
);
870 if (unhash
&& vnode_isinuse(vp
, 0)) {
871 /* vnode now inuse after silly remove? */
873 ubc_setsize(vp
, np
->n_size
);
876 lck_mtx_lock(&nfs_node_hash_mutex
);
879 * remove nfsnode from hash now so we can't accidentally find it
880 * again if another object gets created with the same filehandle
881 * before this vnode gets reclaimed
883 if (np
->n_hflag
& NHHASHED
) {
884 LIST_REMOVE(np
, n_hash
);
885 np
->n_hflag
&= ~NHHASHED
;
886 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
890 /* unlock the node */
891 np
->n_hflag
&= ~NHLOCKED
;
892 if (np
->n_hflag
& NHLOCKWANT
) {
893 np
->n_hflag
&= ~NHLOCKWANT
;
896 lck_mtx_unlock(&nfs_node_hash_mutex
);
898 /* cleanup sillyrename info */
899 if (nsp
->nsr_cred
!= NOCRED
) {
900 kauth_cred_unref(&nsp
->nsr_cred
);
902 if (!vfs_isforce(nmp
->nm_mountp
)) {
903 /* in case of forceful unmount usecounts ignore anyways */
904 vnode_rele(NFSTOV(nsp
->nsr_dnp
));
907 FSDBG_BOT(264, vp
, np
, np
->n_flag
, 0);
909 FREE(nvattr
, M_TEMP
);
914 * Reclaim an nfsnode so that it can be used for other purposes.
918 struct vnop_reclaim_args
/* {
919 * struct vnodeop_desc *a_desc;
921 * vfs_context_t a_context;
924 vnode_t vp
= ap
->a_vp
;
925 nfsnode_t np
= VTONFS(vp
);
926 struct nfs_open_file
*nofp
, *nextnofp
;
927 struct nfs_file_lock
*nflp
, *nextnflp
;
928 struct nfs_lock_owner
*nlop
, *nextnlop
;
929 struct nfsmount
*nmp
= np
->n_mount
? VFSTONFS(np
->n_mount
) : NFSTONMP(np
);
930 mount_t mp
= vnode_mount(vp
);
933 FSDBG_TOP(265, vp
, np
, np
->n_flag
, 0);
934 force
= (!mp
|| vfs_isforce(mp
) || nfs_mount_gone(nmp
));
937 /* There shouldn't be any open or lock state at this point */
938 lck_mtx_lock(&np
->n_openlock
);
941 if (nmp
&& (nmp
->nm_vers
>= NFS_VER4
)) {
942 /* need to drop a delegation */
943 if (np
->n_dreturn
.tqe_next
!= NFSNOLIST
) {
944 /* remove this node from the delegation return list */
945 lck_mtx_lock(&nmp
->nm_lock
);
946 if (np
->n_dreturn
.tqe_next
!= NFSNOLIST
) {
947 TAILQ_REMOVE(&nmp
->nm_dreturnq
, np
, n_dreturn
);
948 np
->n_dreturn
.tqe_next
= NFSNOLIST
;
950 lck_mtx_unlock(&nmp
->nm_lock
);
952 if (np
->n_dlink
.tqe_next
!= NFSNOLIST
) {
953 /* remove this node from the delegation list */
954 lck_mtx_lock(&nmp
->nm_lock
);
955 if (np
->n_dlink
.tqe_next
!= NFSNOLIST
) {
956 TAILQ_REMOVE(&nmp
->nm_delegations
, np
, n_dlink
);
957 np
->n_dlink
.tqe_next
= NFSNOLIST
;
959 lck_mtx_unlock(&nmp
->nm_lock
);
961 if ((np
->n_openflags
& N_DELEG_MASK
) && !force
) {
962 /* try to return the delegation */
963 np
->n_openflags
&= ~N_DELEG_MASK
;
965 if (np
->n_attrdirfh
) {
966 FREE(np
->n_attrdirfh
, M_TEMP
);
967 np
->n_attrdirfh
= NULL
;
972 /* clean up file locks */
973 TAILQ_FOREACH_SAFE(nflp
, &np
->n_locks
, nfl_link
, nextnflp
) {
974 if (!(nflp
->nfl_flags
& NFS_FILE_LOCK_DEAD
) && !force
) {
975 NP(np
, "nfs_vnop_reclaim: lock 0x%llx 0x%llx 0x%x (bc %d)",
976 nflp
->nfl_start
, nflp
->nfl_end
, nflp
->nfl_flags
, nflp
->nfl_blockcnt
);
978 if (!(nflp
->nfl_flags
& (NFS_FILE_LOCK_BLOCKED
| NFS_FILE_LOCK_DEAD
))) {
979 /* try sending an unlock RPC if it wasn't delegated */
980 if (!(nflp
->nfl_flags
& NFS_FILE_LOCK_DELEGATED
) && !force
) {
981 nmp
->nm_funcs
->nf_unlock_rpc(np
, nflp
->nfl_owner
, F_WRLCK
, nflp
->nfl_start
, nflp
->nfl_end
, R_RECOVER
,
982 NULL
, nflp
->nfl_owner
->nlo_open_owner
->noo_cred
);
984 lck_mtx_lock(&nflp
->nfl_owner
->nlo_lock
);
985 TAILQ_REMOVE(&nflp
->nfl_owner
->nlo_locks
, nflp
, nfl_lolink
);
986 lck_mtx_unlock(&nflp
->nfl_owner
->nlo_lock
);
988 TAILQ_REMOVE(&np
->n_locks
, nflp
, nfl_link
);
989 nfs_file_lock_destroy(nflp
);
991 /* clean up lock owners */
992 TAILQ_FOREACH_SAFE(nlop
, &np
->n_lock_owners
, nlo_link
, nextnlop
) {
993 if (!TAILQ_EMPTY(&nlop
->nlo_locks
) && !force
) {
994 NP(np
, "nfs_vnop_reclaim: lock owner with locks");
996 TAILQ_REMOVE(&np
->n_lock_owners
, nlop
, nlo_link
);
997 nfs_lock_owner_destroy(nlop
);
999 /* clean up open state */
1000 if (np
->n_openrefcnt
&& !force
) {
1001 NP(np
, "nfs_vnop_reclaim: still open: %d", np
->n_openrefcnt
);
1003 TAILQ_FOREACH_SAFE(nofp
, &np
->n_opens
, nof_link
, nextnofp
) {
1004 if (nofp
->nof_flags
& NFS_OPEN_FILE_BUSY
) {
1005 NP(np
, "nfs_vnop_reclaim: open file busy");
1007 if (!(np
->n_flag
& NREVOKE
) && !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1008 if (nofp
->nof_opencnt
&& !force
) {
1009 NP(np
, "nfs_vnop_reclaim: file still open: %d", nofp
->nof_opencnt
);
1011 if (!force
&& (nofp
->nof_access
|| nofp
->nof_deny
||
1012 nofp
->nof_mmap_access
|| nofp
->nof_mmap_deny
||
1013 nofp
->nof_r
|| nofp
->nof_w
|| nofp
->nof_rw
||
1014 nofp
->nof_r_dw
|| nofp
->nof_w_dw
|| nofp
->nof_rw_dw
||
1015 nofp
->nof_r_drw
|| nofp
->nof_w_drw
|| nofp
->nof_rw_drw
||
1016 nofp
->nof_d_r
|| nofp
->nof_d_w
|| nofp
->nof_d_rw
||
1017 nofp
->nof_d_r_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_rw_dw
||
1018 nofp
->nof_d_r_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_rw_drw
)) {
1019 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",
1020 nofp
->nof_access
, nofp
->nof_deny
,
1021 nofp
->nof_mmap_access
, nofp
->nof_mmap_deny
,
1022 nofp
->nof_r
, nofp
->nof_d_r
,
1023 nofp
->nof_w
, nofp
->nof_d_w
,
1024 nofp
->nof_rw
, nofp
->nof_d_rw
,
1025 nofp
->nof_r_dw
, nofp
->nof_d_r_dw
,
1026 nofp
->nof_w_dw
, nofp
->nof_d_w_dw
,
1027 nofp
->nof_rw_dw
, nofp
->nof_d_rw_dw
,
1028 nofp
->nof_r_drw
, nofp
->nof_d_r_drw
,
1029 nofp
->nof_w_drw
, nofp
->nof_d_w_drw
,
1030 nofp
->nof_rw_drw
, nofp
->nof_d_rw_drw
);
1032 /* try sending a close RPC if it wasn't delegated */
1033 if (nofp
->nof_r
|| nofp
->nof_w
|| nofp
->nof_rw
||
1034 nofp
->nof_r_dw
|| nofp
->nof_w_dw
|| nofp
->nof_rw_dw
||
1035 nofp
->nof_r_drw
|| nofp
->nof_w_drw
|| nofp
->nof_rw_drw
) {
1036 nfs4_close_rpc(np
, nofp
, NULL
, nofp
->nof_owner
->noo_cred
, R_RECOVER
);
1041 TAILQ_REMOVE(&np
->n_opens
, nofp
, nof_link
);
1042 nfs_open_file_destroy(nofp
);
1044 lck_mtx_unlock(&np
->n_openlock
);
1046 if (np
->n_monlink
.le_next
!= NFSNOLIST
) {
1047 /* Wait for any in-progress getattr to complete, */
1048 /* then remove this node from the monitored node list. */
1049 lck_mtx_lock(&nmp
->nm_lock
);
1050 while (np
->n_mflag
& NMMONSCANINPROG
) {
1051 struct timespec ts
= { .tv_sec
= 1, .tv_nsec
= 0 };
1052 np
->n_mflag
|= NMMONSCANWANT
;
1053 msleep(&np
->n_mflag
, &nmp
->nm_lock
, PZERO
- 1, "nfswaitmonscan", &ts
);
1055 if (np
->n_monlink
.le_next
!= NFSNOLIST
) {
1056 LIST_REMOVE(np
, n_monlink
);
1057 np
->n_monlink
.le_next
= NFSNOLIST
;
1059 lck_mtx_unlock(&nmp
->nm_lock
);
1062 lck_mtx_lock(&nfs_buf_mutex
);
1063 if (!force
&& (!LIST_EMPTY(&np
->n_dirtyblkhd
) || !LIST_EMPTY(&np
->n_cleanblkhd
))) {
1064 NP(np
, "nfs_reclaim: dropping %s buffers", (!LIST_EMPTY(&np
->n_dirtyblkhd
) ? "dirty" : "clean"));
1066 lck_mtx_unlock(&nfs_buf_mutex
);
1067 nfs_vinvalbuf(vp
, V_IGNORE_WRITEERR
, ap
->a_context
, 0);
1069 lck_mtx_lock(&nfs_node_hash_mutex
);
1071 if ((vnode_vtype(vp
) != VDIR
) && np
->n_sillyrename
) {
1073 NP(np
, "nfs_reclaim: leaving unlinked file %s", np
->n_sillyrename
->nsr_name
);
1075 if (np
->n_sillyrename
->nsr_cred
!= NOCRED
) {
1076 kauth_cred_unref(&np
->n_sillyrename
->nsr_cred
);
1078 vnode_rele(NFSTOV(np
->n_sillyrename
->nsr_dnp
));
1079 FREE(np
->n_sillyrename
, M_TEMP
);
1082 vnode_removefsref(vp
);
1084 if (np
->n_hflag
& NHHASHED
) {
1085 LIST_REMOVE(np
, n_hash
);
1086 np
->n_hflag
&= ~NHHASHED
;
1087 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
1089 lck_mtx_unlock(&nfs_node_hash_mutex
);
1092 * Free up any directory cookie structures and large file handle
1093 * structures that might be associated with this nfs node.
1095 nfs_node_lock_force(np
);
1096 if ((vnode_vtype(vp
) == VDIR
) && np
->n_cookiecache
) {
1097 NFS_ZFREE(ZV_NFSDIROFF
, np
->n_cookiecache
);
1099 if (np
->n_fhsize
> NFS_SMALLFH
) {
1100 FREE(np
->n_fhp
, M_NFSBIGFH
);
1102 if (np
->n_vattr
.nva_acl
) {
1103 kauth_acl_free(np
->n_vattr
.nva_acl
);
1105 nfs_node_unlock(np
);
1106 vnode_clearfsnode(vp
);
1109 if (!vnode_get(np
->n_parent
)) {
1110 vnode_rele(np
->n_parent
);
1111 vnode_put(np
->n_parent
);
1113 np
->n_parent
= NULL
;
1116 lck_mtx_destroy(&np
->n_lock
, &nfs_node_lck_grp
);
1117 lck_rw_destroy(&np
->n_datalock
, &nfs_data_lck_grp
);
1118 lck_mtx_destroy(&np
->n_openlock
, &nfs_open_grp
);
1120 FSDBG_BOT(265, vp
, np
, np
->n_flag
, 0xd1ed1e);
1121 NFS_ZFREE(nfsnode_zone
, np
);
1126 * Acquire an NFS node lock
1130 nfs_node_lock_internal(nfsnode_t np
, int force
)
1132 FSDBG_TOP(268, np
, force
, 0, 0);
1133 lck_mtx_lock(&np
->n_lock
);
1134 if (!force
&& !(np
->n_hflag
&& NHHASHED
)) {
1135 FSDBG_BOT(268, np
, 0xdead, 0, 0);
1136 lck_mtx_unlock(&np
->n_lock
);
1139 FSDBG_BOT(268, np
, force
, 0, 0);
1144 nfs_node_lock(nfsnode_t np
)
1146 return nfs_node_lock_internal(np
, 0);
1150 nfs_node_lock_force(nfsnode_t np
)
1152 nfs_node_lock_internal(np
, 1);
1156 * Release an NFS node lock
1159 nfs_node_unlock(nfsnode_t np
)
1161 FSDBG(269, np
, current_thread(), 0, 0);
1162 lck_mtx_unlock(&np
->n_lock
);
1166 * Acquire 2 NFS node locks
1167 * - locks taken in reverse address order
1168 * - both or neither of the locks are taken
1169 * - only one lock taken per node (dup nodes are skipped)
1172 nfs_node_lock2(nfsnode_t np1
, nfsnode_t np2
)
1174 nfsnode_t first
, second
;
1177 first
= (np1
> np2
) ? np1
: np2
;
1178 second
= (np1
> np2
) ? np2
: np1
;
1179 if ((error
= nfs_node_lock(first
))) {
1185 if ((error
= nfs_node_lock(second
))) {
1186 nfs_node_unlock(first
);
1192 nfs_node_unlock2(nfsnode_t np1
, nfsnode_t np2
)
1194 nfs_node_unlock(np1
);
1196 nfs_node_unlock(np2
);
1201 * Manage NFS node busy state.
1202 * (Similar to NFS node locks above)
1205 nfs_node_set_busy(nfsnode_t np
, thread_t thd
)
1207 struct timespec ts
= { .tv_sec
= 2, .tv_nsec
= 0 };
1210 if ((error
= nfs_node_lock(np
))) {
1213 while (ISSET(np
->n_flag
, NBUSY
)) {
1214 SET(np
->n_flag
, NBUSYWANT
);
1215 msleep(np
, &np
->n_lock
, PZERO
- 1, "nfsbusywant", &ts
);
1216 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, thd
, 0))) {
1221 SET(np
->n_flag
, NBUSY
);
1223 nfs_node_unlock(np
);
1228 nfs_node_clear_busy(nfsnode_t np
)
1232 nfs_node_lock_force(np
);
1233 wanted
= ISSET(np
->n_flag
, NBUSYWANT
);
1234 CLR(np
->n_flag
, NBUSY
| NBUSYWANT
);
1235 nfs_node_unlock(np
);
1242 nfs_node_set_busy2(nfsnode_t np1
, nfsnode_t np2
, thread_t thd
)
1244 nfsnode_t first
, second
;
1247 first
= (np1
> np2
) ? np1
: np2
;
1248 second
= (np1
> np2
) ? np2
: np1
;
1249 if ((error
= nfs_node_set_busy(first
, thd
))) {
1255 if ((error
= nfs_node_set_busy(second
, thd
))) {
1256 nfs_node_clear_busy(first
);
1262 nfs_node_clear_busy2(nfsnode_t np1
, nfsnode_t np2
)
1264 nfs_node_clear_busy(np1
);
1266 nfs_node_clear_busy(np2
);
1270 /* helper function to sort four nodes in reverse address order (no dupes) */
1272 nfs_node_sort4(nfsnode_t np1
, nfsnode_t np2
, nfsnode_t np3
, nfsnode_t np4
, nfsnode_t
*list
, int *lcntp
)
1274 nfsnode_t na
[2], nb
[2];
1277 /* sort pairs then merge */
1278 na
[0] = (np1
> np2
) ? np1
: np2
;
1279 na
[1] = (np1
> np2
) ? np2
: np1
;
1280 nb
[0] = (np3
> np4
) ? np3
: np4
;
1281 nb
[1] = (np3
> np4
) ? np4
: np3
;
1282 for (a
= b
= i
= lcnt
= 0; i
< 4; i
++) {
1284 list
[lcnt
] = nb
[b
++];
1285 } else if ((b
>= 2) || (na
[a
] >= nb
[b
])) {
1286 list
[lcnt
] = na
[a
++];
1288 list
[lcnt
] = nb
[b
++];
1290 if ((lcnt
<= 0) || (list
[lcnt
] != list
[lcnt
- 1])) {
1291 lcnt
++; /* omit dups */
1294 if (list
[lcnt
- 1] == NULL
) {
1301 nfs_node_set_busy4(nfsnode_t np1
, nfsnode_t np2
, nfsnode_t np3
, nfsnode_t np4
, thread_t thd
)
1306 nfs_node_sort4(np1
, np2
, np3
, np4
, list
, &lcnt
);
1308 /* Now we can lock using list[0 - lcnt-1] */
1309 for (i
= 0; i
< lcnt
; ++i
) {
1310 if ((error
= nfs_node_set_busy(list
[i
], thd
))) {
1311 /* Drop any locks we acquired. */
1313 nfs_node_clear_busy(list
[i
]);
1322 nfs_node_clear_busy4(nfsnode_t np1
, nfsnode_t np2
, nfsnode_t np3
, nfsnode_t np4
)
1327 nfs_node_sort4(np1
, np2
, np3
, np4
, list
, &lcnt
);
1328 while (--lcnt
>= 0) {
1329 nfs_node_clear_busy(list
[lcnt
]);
1334 * Acquire an NFS node data lock
1337 nfs_data_lock(nfsnode_t np
, int locktype
)
1339 nfs_data_lock_internal(np
, locktype
, 1);
1342 nfs_data_lock_noupdate(nfsnode_t np
, int locktype
)
1344 nfs_data_lock_internal(np
, locktype
, 0);
1347 nfs_data_lock_internal(nfsnode_t np
, int locktype
, int updatesize
)
1349 FSDBG_TOP(270, np
, locktype
, np
->n_datalockowner
, 0);
1350 if (locktype
== NFS_DATA_LOCK_SHARED
) {
1351 if (updatesize
&& ISSET(np
->n_flag
, NUPDATESIZE
)) {
1352 nfs_data_update_size(np
, 0);
1354 lck_rw_lock_shared(&np
->n_datalock
);
1356 lck_rw_lock_exclusive(&np
->n_datalock
);
1357 np
->n_datalockowner
= current_thread();
1358 if (updatesize
&& ISSET(np
->n_flag
, NUPDATESIZE
)) {
1359 nfs_data_update_size(np
, 1);
1362 FSDBG_BOT(270, np
, locktype
, np
->n_datalockowner
, 0);
1366 * Release an NFS node data lock
1369 nfs_data_unlock(nfsnode_t np
)
1371 nfs_data_unlock_internal(np
, 1);
1374 nfs_data_unlock_noupdate(nfsnode_t np
)
1376 nfs_data_unlock_internal(np
, 0);
1379 nfs_data_unlock_internal(nfsnode_t np
, int updatesize
)
1381 int mine
= (np
->n_datalockowner
== current_thread());
1382 FSDBG_TOP(271, np
, np
->n_datalockowner
, current_thread(), 0);
1383 if (updatesize
&& mine
&& ISSET(np
->n_flag
, NUPDATESIZE
)) {
1384 nfs_data_update_size(np
, 1);
1386 np
->n_datalockowner
= NULL
;
1387 lck_rw_done(&np
->n_datalock
);
1388 if (updatesize
&& !mine
&& ISSET(np
->n_flag
, NUPDATESIZE
)) {
1389 nfs_data_update_size(np
, 0);
1391 FSDBG_BOT(271, np
, np
->n_datalockowner
, current_thread(), 0);
1396 * update an NFS node's size
1399 nfs_data_update_size(nfsnode_t np
, int datalocked
)
1403 FSDBG_TOP(272, np
, np
->n_flag
, np
->n_size
, np
->n_newsize
);
1405 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
1406 /* grabbing data lock will automatically update size */
1407 nfs_data_unlock(np
);
1408 FSDBG_BOT(272, np
, np
->n_flag
, np
->n_size
, np
->n_newsize
);
1411 error
= nfs_node_lock(np
);
1412 if (error
|| !ISSET(np
->n_flag
, NUPDATESIZE
)) {
1414 nfs_node_unlock(np
);
1416 FSDBG_BOT(272, np
, np
->n_flag
, np
->n_size
, np
->n_newsize
);
1419 CLR(np
->n_flag
, NUPDATESIZE
);
1420 np
->n_size
= np
->n_newsize
;
1421 /* make sure we invalidate buffers the next chance we get */
1422 SET(np
->n_flag
, NNEEDINVALIDATE
);
1423 nfs_node_unlock(np
);
1424 ubc_setsize(NFSTOV(np
), (off_t
)np
->n_size
); /* XXX error? */
1425 FSDBG_BOT(272, np
, np
->n_flag
, np
->n_size
, np
->n_newsize
);
1431 nfs_mount_is_dirty(mount_t mp
)
1436 struct timeval now
, then
, diff
;
1440 lck_mtx_lock(&nfs_node_hash_mutex
);
1441 for (i
= 0; i
<= nfsnodehash
; i
++) {
1442 LIST_FOREACH(np
, &nfsnodehashtbl
[i
], n_hash
) {
1446 if (np
->n_mount
== mp
&& !LIST_EMPTY(&np
->n_dirtyblkhd
)) {
1452 lck_mtx_unlock(&nfs_node_hash_mutex
);
1455 timersub(&then
, &now
, &diff
);
1457 NFS_DBG(NFS_FAC_SOCK
, 7, "mount_is_dirty for %s took %lld mics for %ld slots and %ld nodes return %d\n",
1458 vfs_statfs(mp
)->f_mntfromname
, (uint64_t)diff
.tv_sec
* 1000000LL + diff
.tv_usec
, i
, ncnt
, (i
<= nfsnodehash
));
1461 return i
<= nfsnodehash
;
1464 #endif /* CONFIG_NFS_CLIENT */