Adjust some comments with reality.
[dragonfly.git] / sys / vfs / nfs / nfs_node.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
37 * $FreeBSD: src/sys/nfs/nfs_node.c,v 1.36.2.3 2002/01/05 22:25:04 dillon Exp $
b478fdce 38 * $DragonFly: src/sys/vfs/nfs/nfs_node.c,v 1.27 2007/08/08 00:12:51 swildner Exp $
984263bc
MD
39 */
40
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/proc.h>
45#include <sys/mount.h>
46#include <sys/namei.h>
47#include <sys/vnode.h>
48#include <sys/malloc.h>
49#include <sys/fnv_hash.h>
50
51#include <vm/vm_zone.h>
52
1f2de5d4
MD
53#include "rpcv2.h"
54#include "nfsproto.h"
55#include "nfs.h"
1f2de5d4 56#include "nfsmount.h"
c1cf1e59 57#include "nfsnode.h"
984263bc
MD
58
59static vm_zone_t nfsnode_zone;
60static LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
61static u_long nfsnodehash;
62
63#define TRUE 1
64#define FALSE 0
65
66/*
67 * Initialize hash links for nfsnodes
68 * and build nfsnode free list.
69 */
70void
e851b29e 71nfs_nhinit(void)
984263bc
MD
72{
73 nfsnode_zone = zinit("NFSNODE", sizeof(struct nfsnode), 0, 0, 1);
74 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSHASH, &nfsnodehash);
75}
76
77/*
78 * Look up a vnode/nfsnode by file handle.
79 * Callers must check for mount points!!
80 * In all cases, a pointer to a
81 * nfsnode structure is returned.
82 */
83static int nfs_node_hash_lock;
84
85int
dadab5e9 86nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp)
984263bc 87{
984263bc
MD
88 struct nfsnode *np, *np2;
89 struct nfsnodehashhead *nhpp;
dadab5e9 90 struct vnode *vp;
984263bc
MD
91 struct vnode *nvp;
92 int error;
f2770c70 93 int lkflags;
984263bc
MD
94 struct nfsmount *nmp;
95
96 /*
97 * Calculate nfs mount point and figure out whether the rslock should
98 * be interruptable or not.
99 */
100 nmp = VFSTONFS(mntp);
101 if (nmp->nm_flag & NFSMNT_INT)
f2770c70 102 lkflags = LK_PCATCH;
984263bc 103 else
f2770c70 104 lkflags = 0;
984263bc
MD
105
106retry:
107 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
108loop:
5fd012e0 109 for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
984263bc 110 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
5fd012e0 111 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
984263bc 112 continue;
5fd012e0 113 }
984263bc 114 vp = NFSTOV(np);
87de5057 115 if (vget(vp, LK_EXCLUSIVE))
5fd012e0
MD
116 goto loop;
117 for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
118 if (mntp == NFSTOV(np)->v_mount &&
119 np->n_fhsize == fhsize &&
120 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize) == 0
121 ) {
122 break;
123 }
124 }
125 if (np == NULL || NFSTOV(np) != vp) {
126 vput(vp);
984263bc 127 goto loop;
5fd012e0 128 }
984263bc
MD
129 *npp = np;
130 return(0);
131 }
132 /*
133 * Obtain a lock to prevent a race condition if the getnewvnode()
134 * or MALLOC() below happens to block.
135 */
136 if (nfs_node_hash_lock) {
137 while (nfs_node_hash_lock) {
138 nfs_node_hash_lock = -1;
377d4740 139 tsleep(&nfs_node_hash_lock, 0, "nfsngt", 0);
984263bc
MD
140 }
141 goto loop;
142 }
143 nfs_node_hash_lock = 1;
144
145 /*
146 * Allocate before getnewvnode since doing so afterward
147 * might cause a bogus v_data pointer to get dereferenced
148 * elsewhere if zalloc should block.
149 */
150 np = zalloc(nfsnode_zone);
151
ab6f251b 152 error = getnewvnode(VT_NFS, mntp, &nvp, 0, 0);
984263bc
MD
153 if (error) {
154 if (nfs_node_hash_lock < 0)
155 wakeup(&nfs_node_hash_lock);
156 nfs_node_hash_lock = 0;
157 *npp = 0;
158 zfree(nfsnode_zone, np);
159 return (error);
160 }
161 vp = nvp;
162 bzero((caddr_t)np, sizeof *np);
984263bc 163 np->n_vnode = vp;
57f7b636
MD
164 vp->v_data = np;
165
984263bc
MD
166 /*
167 * Insert the nfsnode in the hash queue for its new file handle
168 */
169 for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
170 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
171 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize))
172 continue;
5fd012e0 173 vx_put(vp);
984263bc
MD
174 if (nfs_node_hash_lock < 0)
175 wakeup(&nfs_node_hash_lock);
176 nfs_node_hash_lock = 0;
177 zfree(nfsnode_zone, np);
178 goto retry;
179 }
180 LIST_INSERT_HEAD(nhpp, np, n_hash);
181 if (fhsize > NFS_SMALLFH) {
182 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
183 } else
184 np->n_fhp = &np->n_fh;
185 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
186 np->n_fhsize = fhsize;
ab6f251b 187 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
5fd012e0
MD
188
189 /*
190 * nvp is locked & refd so effectively so is np.
191 */
984263bc
MD
192 *npp = np;
193
194 if (nfs_node_hash_lock < 0)
195 wakeup(&nfs_node_hash_lock);
196 nfs_node_hash_lock = 0;
197
984263bc
MD
198 return (0);
199}
200
e851b29e 201/*
b478fdce 202 * nfs_inactive(struct vnode *a_vp)
48b38f01
MD
203 *
204 * NOTE: the passed vnode is locked but not referenced. On return the
205 * vnode must be unlocked and not referenced.
e851b29e 206 */
984263bc 207int
e851b29e 208nfs_inactive(struct vop_inactive_args *ap)
984263bc 209{
dadab5e9
MD
210 struct nfsnode *np;
211 struct sillyrename *sp;
984263bc
MD
212
213 np = VTONFS(ap->a_vp);
3c37c940 214 if (prtactive && ap->a_vp->v_sysref.refcnt > 1)
984263bc
MD
215 vprint("nfs_inactive: pushing active", ap->a_vp);
216 if (ap->a_vp->v_type != VDIR) {
217 sp = np->n_sillyrename;
ef2eabd2
MD
218 np->n_sillyrename = NULL;
219 } else {
220 sp = NULL;
221 }
984263bc
MD
222 if (sp) {
223 /*
224 * We need a reference to keep the vnode from being
225 * recycled by getnewvnode while we do the I/O
48b38f01
MD
226 * associated with discarding the buffers. The vnode
227 * is already locked.
984263bc 228 */
87de5057 229 nfs_vinvalbuf(ap->a_vp, 0, 1);
5fd012e0 230
984263bc
MD
231 /*
232 * Remove the silly file that was rename'd earlier
233 */
234 nfs_removeit(sp);
235 crfree(sp->s_cred);
236 vrele(sp->s_dvp);
237 FREE((caddr_t)sp, M_NFSREQ);
238 }
5a9187cb
MD
239
240 np->n_flag &= ~(NWRITEERR | NACC | NUPD | NCHG | NLOCKED | NWANTED);
241
984263bc
MD
242 return (0);
243}
244
245/*
246 * Reclaim an nfsnode so that it can be used for other purposes.
e851b29e
CP
247 *
248 * nfs_reclaim(struct vnode *a_vp)
984263bc
MD
249 */
250int
e851b29e 251nfs_reclaim(struct vop_reclaim_args *ap)
984263bc 252{
40393ded
RG
253 struct vnode *vp = ap->a_vp;
254 struct nfsnode *np = VTONFS(vp);
40393ded 255 struct nfsdmap *dp, *dp2;
984263bc 256
3c37c940 257 if (prtactive && vp->v_sysref.refcnt > 1)
984263bc
MD
258 vprint("nfs_reclaim: pushing active", vp);
259
260 if (np->n_hash.le_prev != NULL)
261 LIST_REMOVE(np, n_hash);
262
984263bc
MD
263 /*
264 * Free up any directory cookie structures and
265 * large file handle structures that might be associated with
266 * this nfs node.
267 */
268 if (vp->v_type == VDIR) {
269 dp = np->n_cookies.lh_first;
270 while (dp) {
271 dp2 = dp;
272 dp = dp->ndm_list.le_next;
273 FREE((caddr_t)dp2, M_NFSDIROFF);
274 }
275 }
276 if (np->n_fhsize > NFS_SMALLFH) {
277 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
278 }
c1cf1e59
MD
279 if (np->n_rucred) {
280 crfree(np->n_rucred);
281 np->n_rucred = NULL;
282 }
283 if (np->n_wucred) {
284 crfree(np->n_wucred);
285 np->n_wucred = NULL;
286 }
984263bc 287
3446c007
MD
288 vp->v_data = NULL;
289 zfree(nfsnode_zone, np);
984263bc
MD
290 return (0);
291}
292