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/fnv_hash.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/vnode.h>
52 #include <rpc/rpcclnt.h>
54 #include <nfs/rpcv2.h>
55 #include <nfs/nfsproto.h>
56 #include <nfsclient/nfs.h>
57 #include <nfsclient/nfsnode.h>
58 #include <nfsclient/nfsmount.h>
60 static uma_zone_t nfsnode_zone;
69 nfsnode_zone = uma_zcreate("NFSNODE", sizeof(struct nfsnode), NULL,
70 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
76 uma_zdestroy(nfsnode_zone);
85 nfs_vncmpf(struct vnode *vp, void *arg)
92 return (bcmp(a->fh, np->n_fhp, a->fhsize));
96 * Look up a vnode/nfsnode by file handle.
97 * Callers must check for mount points!!
98 * In all cases, a pointer to a
99 * nfsnode structure is returned.
102 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp, int flags)
104 struct thread *td = curthread; /* XXX */
111 struct nfsmount *nmp;
112 struct nfs_vncmp ncmp;
115 * Calculate nfs mount point and figure out whether the rslock should
116 * be interruptible or not.
118 nmp = VFSTONFS(mntp);
119 if (nmp->nm_flag & NFSMNT_INT)
126 hash = fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT);
127 ncmp.fhsize = fhsize;
130 error = vfs_hash_get(mntp, hash, flags,
131 td, &nvp, nfs_vncmpf, &ncmp);
140 * Allocate before getnewvnode since doing so afterward
141 * might cause a bogus v_data pointer to get dereferenced
142 * elsewhere if zalloc should block.
144 np = uma_zalloc(nfsnode_zone, M_WAITOK | M_ZERO);
146 if (nmp->nm_flag & NFSMNT_NFSV4)
147 error = getnewvnode("nfs4", mntp, &nfs4_vnodeops, &nvp);
149 error = getnewvnode("nfs", mntp, &nfs_vnodeops, &nvp);
151 uma_zfree(nfsnode_zone, np);
155 if (nmp->nm_flag & NFSMNT_NFSV4)
156 vp->v_bufobj.bo_ops = &buf_ops_nfs4;
158 vp->v_bufobj.bo_ops = &buf_ops_nfs;
162 * NFS supports recursive and shared locking.
164 vp->v_vnlock->lk_flags |= LK_CANRECURSE;
165 vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
166 if (fhsize > NFS_SMALLFH) {
167 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
169 np->n_fhp = &np->n_fh;
170 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
171 np->n_fhsize = fhsize;
172 lockinit(&np->n_rslock, PVFS | rsflags, "nfrslk", 0, 0);
173 error = vfs_hash_insert(vp, hash, flags,
174 td, &nvp, nfs_vncmpf, &ncmp);
179 /* vfs_hash_insert() vput()'s the losing vnode */
188 nfs_inactive(struct vop_inactive_args *ap)
191 struct sillyrename *sp;
192 struct thread *td = curthread; /* XXX */
194 np = VTONFS(ap->a_vp);
195 if (prtactive && vrefcnt(ap->a_vp) != 0)
196 vprint("nfs_inactive: pushing active", ap->a_vp);
197 if (ap->a_vp->v_type != VDIR) {
198 sp = np->n_sillyrename;
199 np->n_sillyrename = NULL;
203 (void)nfs_vinvalbuf(ap->a_vp, 0, td, 1);
205 * Remove the silly file that was rename'd earlier
207 (sp->s_removeit)(sp);
210 FREE((caddr_t)sp, M_NFSREQ);
212 np->n_flag &= NMODIFIED;
217 * Reclaim an nfsnode so that it can be used for other purposes.
220 nfs_reclaim(struct vop_reclaim_args *ap)
222 struct vnode *vp = ap->a_vp;
223 struct nfsnode *np = VTONFS(vp);
224 struct nfsdmap *dp, *dp2;
226 if (prtactive && vrefcnt(vp) != 0)
227 vprint("nfs_reclaim: pushing active", vp);
230 * Destroy the vm object and flush associated pages.
232 vnode_destroy_vobject(vp);
237 * Free up any directory cookie structures and
238 * large file handle structures that might be associated with
241 if (vp->v_type == VDIR) {
242 dp = LIST_FIRST(&np->n_cookies);
245 dp = LIST_NEXT(dp, ndm_list);
246 FREE((caddr_t)dp2, M_NFSDIROFF);
249 if (np->n_fhsize > NFS_SMALLFH) {
250 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
253 lockdestroy(&np->n_rslock);
254 uma_zfree(nfsnode_zone, vp->v_data);