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