2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from nfs_node.c 8.6 (Berkeley) 5/22/95
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/taskqueue.h>
49 #include <sys/vnode.h>
53 #include <fs/nfs/nfsport.h>
54 #include <fs/nfsclient/nfsnode.h>
55 #include <fs/nfsclient/nfsmount.h>
56 #include <fs/nfsclient/nfs.h>
57 #include <fs/nfsclient/nfs_kdtrace.h>
59 #include <nfs/nfs_lock.h>
61 extern struct vop_vector newnfs_vnodeops;
62 extern struct buf_ops buf_ops_newnfs;
63 MALLOC_DECLARE(M_NEWNFSREQ);
65 uma_zone_t newnfsnode_zone;
67 const char nfs_vnode_tag[] = "nfs";
69 static void nfs_freesillyrename(void *arg, __unused int pending);
75 newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
76 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
82 uma_zdestroy(newnfsnode_zone);
86 * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
87 * function is going to be used to get Regular Files, code must be added
88 * to fill in the "struct nfsv4node".
89 * Look up a vnode/nfsnode by file handle.
90 * Callers must check for mount points!!
91 * In all cases, a pointer to a
92 * nfsnode structure is returned.
95 ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp,
98 struct thread *td = curthread; /* XXX */
104 struct nfsmount *nmp;
107 nmp = VFSTONFS(mntp);
110 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
112 nfhp = malloc(sizeof (struct nfsfh) + fhsize,
114 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
115 nfhp->nfh_len = fhsize;
116 error = vfs_hash_get(mntp, hash, lkflags,
117 td, &nvp, newnfs_vncmpf, nfhp);
125 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
127 error = getnewvnode(nfs_vnode_tag, mntp, &newnfs_vnodeops, &nvp);
129 uma_zfree(newnfsnode_zone, np);
133 KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0"));
134 vp->v_bufobj.bo_ops = &buf_ops_newnfs;
138 * Initialize the mutex even if the vnode is going to be a loser.
139 * This simplifies the logic in reclaim, which can then unconditionally
140 * destroy the mutex (in the case of the loser, or if hash_insert
141 * happened to return an error no special casing is needed).
143 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
144 lockinit(&np->n_excl, PVFS, "nfsupg", VLKTIMEOUT, LK_NOSHARE |
148 * NFS supports recursive and shared locking.
150 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
154 * Are we getting the root? If so, make sure the vnode flags
157 if (fhsize == NFSX_FHMAX + 1 || (fhsize == nmp->nm_fhsize &&
158 !bcmp(fhp, nmp->nm_fh, fhsize))) {
159 if (vp->v_type == VNON)
161 vp->v_vflag |= VV_ROOT;
164 vp->v_vflag |= VV_VMSIZEVNLOCK;
166 np->n_fhp = malloc(sizeof (struct nfsfh) + fhsize,
168 bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
169 np->n_fhp->nfh_len = fhsize;
170 error = insmntque(vp, mntp);
173 free(np->n_fhp, M_NFSFH);
174 mtx_destroy(&np->n_mtx);
175 lockdestroy(&np->n_excl);
176 uma_zfree(newnfsnode_zone, np);
179 vn_set_state(vp, VSTATE_CONSTRUCTED);
180 error = vfs_hash_insert(vp, hash, lkflags,
181 td, &nvp, newnfs_vncmpf, np->n_fhp);
186 /* vfs_hash_insert() vput()'s the losing vnode */
195 * Do the vrele(sp->s_dvp) as a separate task in order to avoid a
196 * deadlock because of a LOR when vrele() locks the directory vnode.
199 nfs_freesillyrename(void *arg, __unused int pending)
201 struct sillyrename *sp;
205 free(sp, M_NEWNFSREQ);
209 ncl_releasesillyrename(struct vnode *vp, struct thread *td)
212 struct sillyrename *sp;
214 ASSERT_VOP_ELOCKED(vp, "releasesillyrename");
217 if (vp->v_type != VDIR) {
218 sp = np->n_sillyrename;
219 np->n_sillyrename = NULL;
224 (void) ncl_vinvalbuf(vp, 0, td, 1);
226 * Remove the silly file that was rename'd earlier
228 ncl_removeit(sp, vp);
230 TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
231 taskqueue_enqueue(taskqueue_thread, &sp->s_task);
237 ncl_inactive(struct vop_inactive_args *ap)
239 struct vnode *vp = ap->a_vp;
246 if (NFS_ISV4(vp) && vp->v_type == VREG) {
248 np->n_openstateid = NULL;
251 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
252 * Close operations are delayed until now. Any dirty
253 * buffers/pages must be flushed before the close, so that the
254 * stateid is available for the writes.
256 if (vp->v_object != NULL) {
257 VM_OBJECT_WLOCK(vp->v_object);
258 retv = vm_object_page_clean(vp->v_object, 0, 0,
260 VM_OBJECT_WUNLOCK(vp->v_object);
264 (void)ncl_flush(vp, MNT_WAIT, td, 1, 0);
265 (void)nfsrpc_close(vp, 1, td);
270 ncl_releasesillyrename(vp, td);
273 * NMODIFIED means that there might be dirty/stale buffers
274 * associated with the NFS vnode.
275 * NDSCOMMIT means that the file is on a pNFS server and commits
276 * should be done to the DS.
277 * None of the other flags are meaningful after the vnode is unused.
279 np->n_flag &= (NMODIFIED | NDSCOMMIT);
285 * Reclaim an nfsnode so that it can be used for other purposes.
288 ncl_reclaim(struct vop_reclaim_args *ap)
290 struct vnode *vp = ap->a_vp;
291 struct nfsnode *np = VTONFS(vp);
292 struct nfsdmap *dp, *dp2;
300 * If the NLM is running, give it a chance to abort pending
303 if (nfs_reclaim_p != NULL)
307 ncl_releasesillyrename(vp, td);
309 if (NFS_ISV4(vp) && vp->v_type == VREG) {
310 np->n_openstateid = NULL;
313 * We can now safely close any remaining NFSv4 Opens for
314 * this file. Most opens will have already been closed by
315 * ncl_inactive(), but there are cases where it is not
316 * called, so we need to do it again here.
318 (void) nfsrpc_close(vp, 1, td);
320 * It it unlikely a delegation will still exist, but
321 * if one does, it must be returned before calling
322 * vfs_hash_remove(), since it cannot be recalled once the
323 * nfs node is no longer available.
326 if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) == 0) {
328 nfscl_delegreturnvp(vp, td);
337 * Call nfscl_reclaimnode() to save attributes in the delegation,
340 if (vp->v_type == VREG)
341 nfscl_reclaimnode(vp);
344 * Free up any directory cookie structures and
345 * large file handle structures that might be associated with
348 if (vp->v_type == VDIR) {
349 dp = LIST_FIRST(&np->n_cookies);
352 dp = LIST_NEXT(dp, ndm_list);
353 free(dp2, M_NFSDIROFF);
356 if (np->n_writecred != NULL)
357 crfree(np->n_writecred);
358 free(np->n_fhp, M_NFSFH);
359 if (np->n_v4 != NULL)
360 free(np->n_v4, M_NFSV4NODE);
361 mtx_destroy(&np->n_mtx);
362 lockdestroy(&np->n_excl);
363 uma_zfree(newnfsnode_zone, vp->v_data);
369 * Invalidate both the access and attribute caches for this vnode.
372 ncl_invalcaches(struct vnode *vp)
374 struct nfsnode *np = VTONFS(vp);
378 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
379 np->n_accesscache[i].stamp = 0;
380 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
382 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);