proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[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 $
dadab5e9 33 * $DragonFly: src/sys/vfs/nwfs/nwfs_node.c,v 1.3 2003/06/25 03:56:08 dillon 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
50#include <netncp/ncp.h>
51#include <netncp/ncp_conn.h>
52#include <netncp/ncp_subr.h>
53
54#include <nwfs/nwfs.h>
55#include <nwfs/nwfs_mount.h>
56#include <nwfs/nwfs_node.h>
57#include <nwfs/nwfs_subr.h>
58
59#define NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])
60
61extern vop_t **nwfs_vnodeop_p;
62
63static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
64static u_long nwnodehash;
65static struct lock nwhashlock;
66
67MALLOC_DEFINE(M_NWNODE, "NWFS node", "NWFS vnode private part");
68MALLOC_DEFINE(M_NWFSHASH, "NWFS hash", "NWFS has table");
69
70static int nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS);
71
72extern struct linker_set sysctl_vfs_nwfs;
73
74SYSCTL_DECL(_vfs_nwfs);
75
76SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
77 NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");
78
79void
80nwfs_hash_init(void) {
81 nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
82 lockinit(&nwhashlock, PVFS, "nwfshl", 0, 0);
83}
84
85void
86nwfs_hash_free(void) {
87 free(nwhashtbl, M_NWFSHASH);
88}
89
90int
91nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS) {
92 struct nwnode *np;
93 struct nwnode_hash_head *nhpp;
94 struct vnode *vp;
95 int i;
96
97 if (nwfs_debuglevel == 0)
98 return 0;
99 printf("Name:uc:hc:fid:pfid\n");
100 for(i = 0; i <= nwnodehash; i++) {
101 nhpp = &nwhashtbl[i];
102 LIST_FOREACH(np, nhpp, n_hash) {
103 vp = NWTOV(np);
104 vprint(NULL, vp);
105 printf("%s:%d:%d:%d:%d\n",np->n_name,vp->v_usecount,vp->v_holdcnt,
106 np->n_fid.f_id, np->n_fid.f_parent);
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{
dadab5e9 140 struct thread *td = curthread; /* XXX */
984263bc
MD
141 struct nwnode *np;
142 struct nwnode_hash_head *nhpp;
143 struct nwmount *nmp = VFSTONWFS(mp);
144 struct vnode *vp;
145 int error;
146
147loop:
dadab5e9 148 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
984263bc
MD
149rescan:
150 if (nwfs_hashlookup(nmp, fid, &np) == 0) {
151 vp = NWTOV(np);
152 simple_lock(&vp->v_interlock);
dadab5e9
MD
153 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
154 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
984263bc
MD
155 goto loop;
156 *vpp = vp;
157 return(0);
158 }
dadab5e9 159 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
984263bc
MD
160
161 /*
162 * Do the MALLOC before the getnewvnode since doing so afterward
163 * might cause a bogus v_data pointer to get dereferenced
164 * elsewhere if MALLOC should block.
165 */
166 MALLOC(np, struct nwnode *, sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
167 error = getnewvnode(VT_NWFS, mp, nwfs_vnodeop_p, &vp);
168 if (error) {
169 *vpp = NULL;
170 FREE(np, M_NWNODE);
171 return (error);
172 }
173 vp->v_data = np;
174 np->n_vnode = vp;
175 np->n_mount = nmp;
dadab5e9 176 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
984263bc
MD
177 /*
178 * Another process can create vnode while we blocked in malloc() or
179 * getnewvnode(). Rescan list again.
180 */
181 if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
182 vp->v_data = NULL;
183 np->n_vnode = NULL;
184 vrele(vp);
185 FREE(np, M_NWNODE);
186 goto rescan;
187 }
188 *vpp = vp;
189 np->n_fid = fid;
190 np->n_flag |= NNEW;
191 lockinit(&np->n_lock, PINOD, "nwnode", VLKTIMEOUT, LK_CANRECURSE);
192 nhpp = NWNOHASH(fid);
193 LIST_INSERT_HEAD(nhpp, np, n_hash);
dadab5e9
MD
194 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
195 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
984263bc
MD
196 return 0;
197}
198
199int
dadab5e9 200nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct thread *td,
984263bc
MD
201 struct nwnode **npp)
202{
203 int error;
204
dadab5e9 205 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
984263bc 206 error = nwfs_hashlookup(nmp, fid, npp);
dadab5e9 207 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
984263bc
MD
208 return error;
209}
210
211/*
212 * Free nwnode, and give vnode back to system
213 */
214int
215nwfs_reclaim(ap)
216 struct vop_reclaim_args /* {
217 struct vnode *a_vp;
dadab5e9 218 struct thread *a_td;
984263bc
MD
219 } */ *ap;
220{
221 struct vnode *dvp = NULL, *vp = ap->a_vp;
222 struct nwnode *dnp, *np = VTONW(vp);
223 struct nwmount *nmp = VTONWFS(vp);
dadab5e9 224 struct thread *td = ap->a_td;
984263bc
MD
225
226 NCPVNDEBUG("%s,%d\n", np->n_name, vp->v_usecount);
227 if (np->n_refparent) {
228 np->n_refparent = 0;
dadab5e9 229 if (nwfs_lookupnp(nmp, np->n_parent, td, &dnp) == 0) {
984263bc
MD
230 dvp = dnp->n_vnode;
231 } else {
232 NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
233 }
234 }
dadab5e9 235 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
984263bc 236 LIST_REMOVE(np, n_hash);
dadab5e9 237 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
984263bc
MD
238 cache_purge(vp);
239 if (nmp->n_root == np) {
240 nmp->n_root = NULL;
241 }
242 vp->v_data = NULL;
243 FREE(np, M_NWNODE);
244 if (dvp) {
245 vrele(dvp);
246 }
247 return (0);
248}
249
250int
251nwfs_inactive(ap)
252 struct vop_inactive_args /* {
253 struct vnode *a_vp;
dadab5e9 254 struct thread *a_td;
984263bc
MD
255 } */ *ap;
256{
dadab5e9
MD
257 struct thread *td = ap->a_td;
258 struct ucred *cred;
984263bc
MD
259 struct vnode *vp = ap->a_vp;
260 struct nwnode *np = VTONW(vp);
261 int error;
262
dadab5e9
MD
263 KKASSERT(td->td_proc);
264 cred = td->td_proc->p_ucred;
265
984263bc
MD
266 NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vp->v_usecount);
267 if (np->opened) {
dadab5e9
MD
268 error = nwfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
269 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
984263bc
MD
270 np->opened = 0;
271 }
dadab5e9 272 VOP_UNLOCK(vp, 0, td);
984263bc
MD
273 if (np->n_flag & NSHOULDFREE) {
274 cache_purge(vp);
275 vgone(vp);
276 }
277 return (0);
278}
279/*
280 * routines to maintain vnode attributes cache
281 * nwfs_attr_cacheenter: unpack np.i to va structure
282 */
283void
284nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
285{
286 struct nwnode *np = VTONW(vp);
287 struct nwmount *nmp = VTONWFS(vp);
288 struct vattr *va = &np->n_vattr;
289
290 va->va_type = vp->v_type; /* vnode type (for create) */
291 np->n_nmlen = fi->nameLen;
292 bcopy(fi->entryName, np->n_name, np->n_nmlen);
293 np->n_name[fi->nameLen] = 0;
294 if (vp->v_type == VREG) {
295 if (va->va_size != fi->dataStreamSize) {
296 va->va_size = fi->dataStreamSize;
297 vnode_pager_setsize(vp, va->va_size);
298 }
299 va->va_mode = nmp->m.file_mode; /* files access mode and type */
300 } else if (vp->v_type == VDIR) {
301 va->va_size = 16384; /* should be a better way ... */
302 va->va_mode = nmp->m.dir_mode; /* files access mode and type */
303 } else
304 return;
305 np->n_size = va->va_size;
306 va->va_nlink = 1; /* number of references to file */
307 va->va_uid = nmp->m.uid; /* owner user id */
308 va->va_gid = nmp->m.gid; /* owner group id */
309 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
310 va->va_fileid = np->n_fid.f_id; /* file id */
311 if (va->va_fileid == 0)
312 va->va_fileid = NWFS_ROOT_INO;
313 va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
314 /* time of last modification */
315 ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
316 /* time of last access */
317 ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
318 va->va_ctime = va->va_mtime; /* time file changed */
319 va->va_gen = VNOVAL; /* generation number of file */
320 va->va_flags = 0; /* flags defined for file */
321 va->va_rdev = VNOVAL; /* device the special file represents */
322 va->va_bytes = va->va_size; /* bytes of disk space held by file */
323 va->va_filerev = 0; /* file modification number */
324 va->va_vaflags = 0; /* operations flags */
325 np->n_vattr = *va;
326 if (np->n_mtime == 0) {
327 np->n_mtime = va->va_mtime.tv_sec;
328 }
329 np->n_atime = time_second;
330 np->n_dosfid = fi->DosDirNum;
331 return;
332}
333
334int
335nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
336{
337 struct nwnode *np = VTONW(vp);
338 int diff;
339
340 diff = time_second - np->n_atime;
341 if (diff > 2) { /* XXX should be configurable */
342 return ENOENT;
343 }
344 *va = np->n_vattr;
345 return 0;
346}