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 * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fcntl.h>
41 #include <sys/fnv_hash.h>
43 #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 <nfs/nfsproto.h>
56 #include <nfs/nfs_lock.h>
57 #include <nfsclient/nfs.h>
58 #include <nfsclient/nfsnode.h>
59 #include <nfsclient/nfsmount.h>
61 static uma_zone_t nfsnode_zone;
63 static void nfs_freesillyrename(void *arg, __unused int pending);
72 nfsnode_zone = uma_zcreate("NFSNODE", sizeof(struct nfsnode), NULL,
73 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
79 uma_zdestroy(nfsnode_zone);
88 nfs_vncmpf(struct vnode *vp, void *arg)
95 return (bcmp(a->fh, np->n_fhp, a->fhsize));
99 * Look up a vnode/nfsnode by file handle.
100 * Callers must check for mount points!!
101 * In all cases, a pointer to a
102 * nfsnode structure is returned.
105 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp, int flags)
107 struct thread *td = curthread; /* XXX */
113 struct nfsmount *nmp;
114 struct nfs_vncmp ncmp;
116 nmp = VFSTONFS(mntp);
119 hash = fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT);
120 ncmp.fhsize = fhsize;
123 error = vfs_hash_get(mntp, hash, flags,
124 td, &nvp, nfs_vncmpf, &ncmp);
131 np = uma_zalloc(nfsnode_zone, M_WAITOK | M_ZERO);
133 error = getnewvnode("nfs", mntp, &nfs_vnodeops, &nvp);
135 uma_zfree(nfsnode_zone, np);
139 vp->v_bufobj.bo_ops = &buf_ops_nfs;
143 * Initialize the mutex even if the vnode is going to be a loser.
144 * This simplifies the logic in reclaim, which can then unconditionally
145 * destroy the mutex (in the case of the loser, or if hash_insert happened
146 * to return an error no special casing is needed).
148 mtx_init(&np->n_mtx, "NFSnode lock", NULL, MTX_DEF);
150 * NFS supports recursive and shared locking.
152 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
155 if (fhsize > NFS_SMALLFH) {
156 np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
158 np->n_fhp = &np->n_fh;
159 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
160 np->n_fhsize = fhsize;
161 error = insmntque(vp, mntp);
164 if (np->n_fhsize > NFS_SMALLFH) {
165 free((caddr_t)np->n_fhp, M_NFSBIGFH);
167 mtx_destroy(&np->n_mtx);
168 uma_zfree(nfsnode_zone, np);
171 error = vfs_hash_insert(vp, hash, flags,
172 td, &nvp, nfs_vncmpf, &ncmp);
177 /* vfs_hash_insert() vput()'s the losing vnode */
186 * Do the vrele(sp->s_dvp) as a separate task in order to avoid a
187 * deadlock because of a LOR when vrele() locks the directory vnode.
190 nfs_freesillyrename(void *arg, __unused int pending)
192 struct sillyrename *sp;
200 nfs_inactive(struct vop_inactive_args *ap)
203 struct sillyrename *sp;
204 struct thread *td = curthread; /* XXX */
206 np = VTONFS(ap->a_vp);
207 mtx_lock(&np->n_mtx);
208 if (ap->a_vp->v_type != VDIR) {
209 sp = np->n_sillyrename;
210 np->n_sillyrename = NULL;
214 mtx_unlock(&np->n_mtx);
215 (void)nfs_vinvalbuf(ap->a_vp, 0, td, 1);
217 * Remove the silly file that was rename'd earlier
219 (sp->s_removeit)(sp);
221 TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
222 taskqueue_enqueue(taskqueue_thread, &sp->s_task);
223 mtx_lock(&np->n_mtx);
225 np->n_flag &= NMODIFIED;
226 mtx_unlock(&np->n_mtx);
231 * Reclaim an nfsnode so that it can be used for other purposes.
234 nfs_reclaim(struct vop_reclaim_args *ap)
236 struct vnode *vp = ap->a_vp;
237 struct nfsnode *np = VTONFS(vp);
238 struct nfsdmap *dp, *dp2;
241 * If the NLM is running, give it a chance to abort pending
248 * Destroy the vm object and flush associated pages.
250 vnode_destroy_vobject(vp);
255 * Free up any directory cookie structures and
256 * large file handle structures that might be associated with
259 if (vp->v_type == VDIR) {
260 dp = LIST_FIRST(&np->n_cookies);
263 dp = LIST_NEXT(dp, ndm_list);
264 free((caddr_t)dp2, M_NFSDIROFF);
267 if (np->n_writecred != NULL)
268 crfree(np->n_writecred);
269 if (np->n_fhsize > NFS_SMALLFH) {
270 free((caddr_t)np->n_fhp, M_NFSBIGFH);
272 mtx_destroy(&np->n_mtx);
273 uma_zfree(nfsnode_zone, vp->v_data);