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