2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from nfs_node.c 8.6 (Berkeley) 5/22/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_kdtrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/taskqueue.h>
51 #include <sys/vnode.h>
55 #include <fs/nfs/nfsport.h>
56 #include <fs/nfsclient/nfsnode.h>
57 #include <fs/nfsclient/nfsmount.h>
58 #include <fs/nfsclient/nfs.h>
59 #include <fs/nfsclient/nfs_kdtrace.h>
61 #include <nfs/nfs_lock.h>
63 extern struct vop_vector newnfs_vnodeops;
64 extern struct buf_ops buf_ops_newnfs;
65 MALLOC_DECLARE(M_NEWNFSREQ);
67 uma_zone_t newnfsnode_zone;
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 MALLOC(nfhp, struct nfsfh *, 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("newnfs", 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);
145 * NFS supports recursive and shared locking.
147 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
151 * Are we getting the root? If so, make sure the vnode flags
154 if ((fhsize == nmp->nm_fhsize) &&
155 !bcmp(fhp, nmp->nm_fh, fhsize)) {
156 if (vp->v_type == VNON)
158 vp->v_vflag |= VV_ROOT;
161 MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
163 bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
164 np->n_fhp->nfh_len = fhsize;
165 error = insmntque(vp, mntp);
168 FREE((caddr_t)np->n_fhp, M_NFSFH);
169 mtx_destroy(&np->n_mtx);
170 uma_zfree(newnfsnode_zone, np);
173 error = vfs_hash_insert(vp, hash, lkflags,
174 td, &nvp, newnfs_vncmpf, np->n_fhp);
179 /* vfs_hash_insert() vput()'s the losing vnode */
188 * Do the vrele(sp->s_dvp) as a separate task in order to avoid a
189 * deadlock because of a LOR when vrele() locks the directory vnode.
192 nfs_freesillyrename(void *arg, __unused int pending)
194 struct sillyrename *sp;
198 free(sp, M_NEWNFSREQ);
202 ncl_inactive(struct vop_inactive_args *ap)
205 struct sillyrename *sp;
206 struct vnode *vp = ap->a_vp;
211 if (NFS_ISV4(vp) && vp->v_type == VREG) {
213 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
214 * Close operations are delayed until now. Any dirty
215 * buffers/pages must be flushed before the close, so that the
216 * stateid is available for the writes.
218 if (vp->v_object != NULL) {
219 VM_OBJECT_WLOCK(vp->v_object);
220 retv = vm_object_page_clean(vp->v_object, 0, 0,
222 VM_OBJECT_WUNLOCK(vp->v_object);
226 (void)ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0);
227 (void)nfsrpc_close(vp, 1, ap->a_td);
231 mtx_lock(&np->n_mtx);
232 if (vp->v_type != VDIR) {
233 sp = np->n_sillyrename;
234 np->n_sillyrename = NULL;
238 mtx_unlock(&np->n_mtx);
239 (void) ncl_vinvalbuf(vp, 0, ap->a_td, 1);
241 * Remove the silly file that was rename'd earlier
243 ncl_removeit(sp, vp);
245 TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
246 taskqueue_enqueue(taskqueue_thread, &sp->s_task);
247 mtx_lock(&np->n_mtx);
249 np->n_flag &= NMODIFIED;
250 mtx_unlock(&np->n_mtx);
255 * Reclaim an nfsnode so that it can be used for other purposes.
258 ncl_reclaim(struct vop_reclaim_args *ap)
260 struct vnode *vp = ap->a_vp;
261 struct nfsnode *np = VTONFS(vp);
262 struct nfsdmap *dp, *dp2;
265 * If the NLM is running, give it a chance to abort pending
268 if (nfs_reclaim_p != NULL)
272 * Destroy the vm object and flush associated pages.
274 vnode_destroy_vobject(vp);
276 if (NFS_ISV4(vp) && vp->v_type == VREG)
278 * We can now safely close any remaining NFSv4 Opens for
279 * this file. Most opens will have already been closed by
280 * ncl_inactive(), but there are cases where it is not
281 * called, so we need to do it again here.
283 (void) nfsrpc_close(vp, 1, ap->a_td);
288 * Call nfscl_reclaimnode() to save attributes in the delegation,
291 if (vp->v_type == VREG)
292 nfscl_reclaimnode(vp);
295 * Free up any directory cookie structures and
296 * large file handle structures that might be associated with
299 if (vp->v_type == VDIR) {
300 dp = LIST_FIRST(&np->n_cookies);
303 dp = LIST_NEXT(dp, ndm_list);
304 FREE((caddr_t)dp2, M_NFSDIROFF);
307 if (np->n_writecred != NULL)
308 crfree(np->n_writecred);
309 FREE((caddr_t)np->n_fhp, M_NFSFH);
310 if (np->n_v4 != NULL)
311 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
312 mtx_destroy(&np->n_mtx);
313 uma_zfree(newnfsnode_zone, vp->v_data);
319 * Invalidate both the access and attribute caches for this vnode.
322 ncl_invalcaches(struct vnode *vp)
324 struct nfsnode *np = VTONFS(vp);
327 mtx_lock(&np->n_mtx);
328 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
329 np->n_accesscache[i].stamp = 0;
330 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
332 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
333 mtx_unlock(&np->n_mtx);