kernel/vfs: Remove some unused variables.
[dragonfly.git] / sys / vfs / nwfs / nwfs_node.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1999, 2000 Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: src/sys/nwfs/nwfs_node.c,v 1.3.2.8 2001/12/25 01:44:45 dillon Exp $
33 */
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/time.h>
38#include <sys/proc.h>
39#include <sys/mount.h>
40#include <sys/vnode.h>
41#include <sys/malloc.h>
42#include <sys/sysctl.h>
43#include <vm/vm.h>
44#include <vm/vm_extern.h>
45#include <vm/vm_page.h>
46#include <vm/vm_object.h>
47#include <sys/queue.h>
48
d2438d69
MD
49#include <netproto/ncp/ncp.h>
50#include <netproto/ncp/ncp_conn.h>
51#include <netproto/ncp/ncp_subr.h>
984263bc 52
1f2de5d4
MD
53#include "nwfs.h"
54#include "nwfs_mount.h"
55#include "nwfs_node.h"
56#include "nwfs_subr.h"
984263bc
MD
57
58#define NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])
59
984263bc
MD
60static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
61static u_long nwnodehash;
62static struct lock nwhashlock;
63
64MALLOC_DEFINE(M_NWNODE, "NWFS node", "NWFS vnode private part");
65MALLOC_DEFINE(M_NWFSHASH, "NWFS hash", "NWFS has table");
66
67static int nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS);
68
984263bc
MD
69SYSCTL_DECL(_vfs_nwfs);
70
71SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
72 NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");
73
74void
a9bb80e0
CP
75nwfs_hash_init(void)
76{
984263bc 77 nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
f2770c70 78 lockinit(&nwhashlock, "nwfshl", 0, 0);
984263bc
MD
79}
80
81void
a9bb80e0
CP
82nwfs_hash_free(void)
83{
e46d128d 84 hashdestroy(nwhashtbl, M_NWFSHASH, nwnodehash);
984263bc
MD
85}
86
87int
a9bb80e0
CP
88nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS)
89{
984263bc
MD
90 struct nwnode *np;
91 struct nwnode_hash_head *nhpp;
92 struct vnode *vp;
93 int i;
94
95 if (nwfs_debuglevel == 0)
96 return 0;
086c1d7e 97 kprintf("Name:uc:hc:fid:pfid\n");
984263bc
MD
98 for(i = 0; i <= nwnodehash; i++) {
99 nhpp = &nwhashtbl[i];
100 LIST_FOREACH(np, nhpp, n_hash) {
101 vp = NWTOV(np);
102 vprint(NULL, vp);
3c37c940
MD
103 kprintf("%s:%d:%d:%d:%d\n",
104 np->n_name, vp->v_sysref.refcnt, vp->v_auxrefs,
105 np->n_fid.f_id, np->n_fid.f_parent);
984263bc
MD
106 }
107 }
108 return 0;
109}
110
111/*
112 * Search nwnode with given fid.
113 * Hash list should be locked by caller.
114 */
115static int
116nwfs_hashlookup(struct nwmount *nmp, ncpfid fid, struct nwnode **npp)
117{
118 struct nwnode *np;
119 struct nwnode_hash_head *nhpp;
120
121 nhpp = NWNOHASH(fid);
122 LIST_FOREACH(np, nhpp, n_hash) {
123 if (nmp != np->n_mount || !NWCMPF(&fid, &np->n_fid))
124 continue;
125 if (npp)
126 *npp = np;
127 return 0;
128 }
129 return ENOENT;
130}
131
132/*
133 * Allocate new nwfsnode/vnode from given nwnode.
134 * Vnode referenced and not locked.
135 */
136int
137nwfs_allocvp(struct mount *mp, ncpfid fid, struct vnode **vpp)
138{
984263bc
MD
139 struct nwnode *np;
140 struct nwnode_hash_head *nhpp;
141 struct nwmount *nmp = VFSTONWFS(mp);
142 struct vnode *vp;
143 int error;
144
145loop:
df4f70a6 146 lockmgr(&nwhashlock, LK_EXCLUSIVE);
984263bc
MD
147rescan:
148 if (nwfs_hashlookup(nmp, fid, &np) == 0) {
149 vp = NWTOV(np);
df4f70a6 150 lockmgr(&nwhashlock, LK_RELEASE);
87de5057 151 if (vget(vp, LK_EXCLUSIVE))
984263bc 152 goto loop;
5fd012e0
MD
153 if (nwfs_hashlookup(nmp, fid, &np) || NWTOV(np) != vp) {
154 vput(vp);
155 goto loop;
156 }
984263bc
MD
157 *vpp = vp;
158 return(0);
159 }
df4f70a6 160 lockmgr(&nwhashlock, LK_RELEASE);
984263bc
MD
161
162 /*
163 * Do the MALLOC before the getnewvnode since doing so afterward
164 * might cause a bogus v_data pointer to get dereferenced
165 * elsewhere if MALLOC should block.
166 */
884717e1 167 np = kmalloc(sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
6ddb7618 168 error = getnewvnode(VT_NWFS, mp, &vp, 0, 0);
984263bc
MD
169 if (error) {
170 *vpp = NULL;
884717e1 171 kfree(np, M_NWNODE);
984263bc
MD
172 return (error);
173 }
984263bc
MD
174 np->n_vnode = vp;
175 np->n_mount = nmp;
3446c007 176
984263bc
MD
177 /*
178 * Another process can create vnode while we blocked in malloc() or
179 * getnewvnode(). Rescan list again.
180 */
df4f70a6 181 lockmgr(&nwhashlock, LK_EXCLUSIVE);
984263bc 182 if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
984263bc 183 np->n_vnode = NULL;
5fd012e0 184 vx_put(vp);
efda3bd0 185 kfree(np, M_NWNODE);
984263bc
MD
186 goto rescan;
187 }
188 *vpp = vp;
3446c007 189 vp->v_data = np;
984263bc
MD
190 np->n_fid = fid;
191 np->n_flag |= NNEW;
f2770c70 192 lockinit(&np->n_lock, "nwnode", VLKTIMEOUT, LK_CANRECURSE);
984263bc
MD
193 nhpp = NWNOHASH(fid);
194 LIST_INSERT_HEAD(nhpp, np, n_hash);
df4f70a6 195 lockmgr(&nwhashlock, LK_RELEASE);
984263bc
MD
196 return 0;
197}
198
199int
dadab5e9 200nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct thread *td,
a9bb80e0 201 struct nwnode **npp)
984263bc
MD
202{
203 int error;
204
df4f70a6 205 lockmgr(&nwhashlock, LK_EXCLUSIVE);
984263bc 206 error = nwfs_hashlookup(nmp, fid, npp);
df4f70a6 207 lockmgr(&nwhashlock, LK_RELEASE);
984263bc
MD
208 return error;
209}
210
211/*
212 * Free nwnode, and give vnode back to system
a9bb80e0 213 *
b478fdce 214 * nwfs_reclaim(struct vnode *a_vp)
984263bc
MD
215 */
216int
a9bb80e0 217nwfs_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
218{
219 struct vnode *dvp = NULL, *vp = ap->a_vp;
220 struct nwnode *dnp, *np = VTONW(vp);
221 struct nwmount *nmp = VTONWFS(vp);
87de5057 222 struct thread *td = curthread; /* XXX */
984263bc 223
3c37c940 224 NCPVNDEBUG("%s,%d\n", (np ? np->n_name : "?"), vp->v_sysref.refcnt);
3446c007 225 if (np && np->n_refparent) {
984263bc 226 np->n_refparent = 0;
dadab5e9 227 if (nwfs_lookupnp(nmp, np->n_parent, td, &dnp) == 0) {
984263bc
MD
228 dvp = dnp->n_vnode;
229 } else {
230 NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
231 }
232 }
3446c007 233 if (np) {
df4f70a6 234 lockmgr(&nwhashlock, LK_EXCLUSIVE);
3446c007 235 LIST_REMOVE(np, n_hash);
df4f70a6 236 lockmgr(&nwhashlock, LK_RELEASE);
3446c007 237 }
3446c007 238 if (nmp->n_root == np)
984263bc 239 nmp->n_root = NULL;
984263bc 240 vp->v_data = NULL;
3446c007 241 if (np)
efda3bd0 242 kfree(np, M_NWNODE);
3446c007 243 if (dvp)
984263bc 244 vrele(dvp);
984263bc
MD
245 return (0);
246}
247
a9bb80e0 248/*
b478fdce 249 * nwfs_inactive(struct vnode *a_vp)
a9bb80e0 250 */
984263bc 251int
a9bb80e0 252nwfs_inactive(struct vop_inactive_args *ap)
984263bc 253{
1d0de3d3 254 struct thread *td __debugvar = curthread; /* XXX */
dadab5e9 255 struct ucred *cred;
984263bc
MD
256 struct vnode *vp = ap->a_vp;
257 struct nwnode *np = VTONW(vp);
258 int error;
259
87de5057
MD
260 KKASSERT(td->td_proc); /* XXX */
261 cred = td->td_proc->p_ucred; /* XXX */
dadab5e9 262
3c37c940 263 NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vp->v_sysref.refcnt);
3446c007 264 if (np && np->opened) {
87de5057 265 error = nwfs_vinvalbuf(vp, V_SAVE, 1);
dadab5e9 266 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
984263bc
MD
267 np->opened = 0;
268 }
3446c007 269 if (np == NULL || (np->n_flag & NSHOULDFREE)) {
3c37c940 270 vgone_vxlocked(vp);
984263bc
MD
271 }
272 return (0);
273}
274/*
275 * routines to maintain vnode attributes cache
276 * nwfs_attr_cacheenter: unpack np.i to va structure
277 */
278void
7648a732 279nwfs_attr_cacheenter(struct vnode *vp, const struct nw_entry_info *fi)
984263bc
MD
280{
281 struct nwnode *np = VTONW(vp);
282 struct nwmount *nmp = VTONWFS(vp);
283 struct vattr *va = &np->n_vattr;
284
285 va->va_type = vp->v_type; /* vnode type (for create) */
286 np->n_nmlen = fi->nameLen;
287 bcopy(fi->entryName, np->n_name, np->n_nmlen);
288 np->n_name[fi->nameLen] = 0;
289 if (vp->v_type == VREG) {
290 if (va->va_size != fi->dataStreamSize) {
291 va->va_size = fi->dataStreamSize;
292 vnode_pager_setsize(vp, va->va_size);
293 }
294 va->va_mode = nmp->m.file_mode; /* files access mode and type */
295 } else if (vp->v_type == VDIR) {
296 va->va_size = 16384; /* should be a better way ... */
297 va->va_mode = nmp->m.dir_mode; /* files access mode and type */
298 } else
299 return;
300 np->n_size = va->va_size;
301 va->va_nlink = 1; /* number of references to file */
302 va->va_uid = nmp->m.uid; /* owner user id */
303 va->va_gid = nmp->m.gid; /* owner group id */
304 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
305 va->va_fileid = np->n_fid.f_id; /* file id */
306 if (va->va_fileid == 0)
307 va->va_fileid = NWFS_ROOT_INO;
308 va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
309 /* time of last modification */
310 ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
311 /* time of last access */
312 ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
313 va->va_ctime = va->va_mtime; /* time file changed */
314 va->va_gen = VNOVAL; /* generation number of file */
315 va->va_flags = 0; /* flags defined for file */
0e9b9130
MD
316 va->va_rmajor = VNOVAL; /* device the special file represents */
317 va->va_rminor = VNOVAL;
984263bc
MD
318 va->va_bytes = va->va_size; /* bytes of disk space held by file */
319 va->va_filerev = 0; /* file modification number */
320 va->va_vaflags = 0; /* operations flags */
321 np->n_vattr = *va;
322 if (np->n_mtime == 0) {
323 np->n_mtime = va->va_mtime.tv_sec;
324 }
325 np->n_atime = time_second;
326 np->n_dosfid = fi->DosDirNum;
327 return;
328}
329
330int
331nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
332{
333 struct nwnode *np = VTONW(vp);
334 int diff;
335
336 diff = time_second - np->n_atime;
337 if (diff > 2) { /* XXX should be configurable */
338 return ENOENT;
339 }
340 *va = np->n_vattr;
341 return 0;
342}