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