]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nwfs/nwfs_node.c
This commit was generated by cvs2svn to compensate for changes in r169689,
[FreeBSD/FreeBSD.git] / sys / fs / 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$
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/time.h>
45 #include <sys/vnode.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_object.h>
51
52 #include <netncp/ncp.h>
53 #include <netncp/ncp_conn.h>
54 #include <netncp/ncp_subr.h>
55
56 #include <fs/nwfs/nwfs.h>
57 #include <fs/nwfs/nwfs_mount.h>
58 #include <fs/nwfs/nwfs_node.h>
59 #include <fs/nwfs/nwfs_subr.h>
60
61 #define NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])
62
63 static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
64 static u_long nwnodehash;
65 static struct lock nwhashlock;
66
67 static MALLOC_DEFINE(M_NWNODE, "nwfs_node", "NWFS vnode private part");
68 static 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         nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
80         lockinit(&nwhashlock, PVFS, "nwfshl", 0, 0);
81 }
82
83 void
84 nwfs_hash_free(void) {
85         lockdestroy(&nwhashlock);
86         free(nwhashtbl, M_NWFSHASH);
87 }
88
89 int
90 nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS) {
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;
98         printf("Name:uc:hc:fid:pfid\n");
99         for(i = 0; i <= nwnodehash; i++) {
100                 nhpp = &nwhashtbl[i];
101                 LIST_FOREACH(np, nhpp, n_hash) {
102                         vp = NWTOV(np);
103                         vprint("", vp);
104                         printf("%s:%d:%d:%d:%d\n",np->n_name,vrefcnt(vp),
105                             vp->v_holdcnt,np->n_fid.f_id, np->n_fid.f_parent);
106                 }
107         }
108         return 0;
109 }
110
111 /*
112  * Search nwnode with given fid.
113  * Hash list should be locked by caller.
114  */
115 static int
116 nwfs_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  */
136 static int
137 nwfs_allocvp(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
138         struct vnode *dvp, struct vnode **vpp)
139 {
140         struct thread *td = curthread;  /* XXX */
141         struct nwnode *np;
142         struct nwnode_hash_head *nhpp;
143         struct nwmount *nmp = VFSTONWFS(mp);
144         struct vnode *vp;
145         int error;
146
147 loop:
148         lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
149 rescan:
150         if (nwfs_hashlookup(nmp, fid, &np) == 0) {
151                 vp = NWTOV(np);
152                 mtx_lock(&vp->v_interlock);
153                 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
154                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
155                         goto loop;
156                 if (fap)
157                         np->n_attr = fap->attributes;
158                 *vpp = vp;
159                 return(0);
160         }
161         lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
162
163         if (fap == NULL || ((fap->attributes & aDIR) == 0 && dvp == NULL))
164                 panic("nwfs_allocvp: fap = %p, dvp = %p\n", fap, dvp);
165         /*
166          * Do the MALLOC before the getnewvnode since doing so afterward
167          * might cause a bogus v_data pointer to get dereferenced
168          * elsewhere if MALLOC should block.
169          */
170         MALLOC(np, struct nwnode *, sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
171         error = getnewvnode("nwfs", mp, &nwfs_vnodeops, &vp);
172         if (error) {
173                 *vpp = NULL;
174                 FREE(np, M_NWNODE);
175                 return (error);
176         }
177         error = insmntque(vp, mp);      /* XXX: Too early for mpsafe fs */
178         if (error != 0) {
179                 FREE(np, M_NWNODE);
180                 *vpp = NULL;
181                 return (error);
182         }
183         vp->v_data = np;
184         np->n_vnode = vp;
185         np->n_mount = nmp;
186         np->n_attr = fap->attributes;
187         vp->v_type = np->n_attr & aDIR ? VDIR : VREG;
188         np->n_fid = fid;
189         if (dvp) {
190                 np->n_parent = VTONW(dvp)->n_fid;
191         }
192         vp->v_vnlock->lk_flags |= LK_CANRECURSE;
193         lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
194         /*
195          * Another process can create vnode while we blocked in malloc() or
196          * getnewvnode(). Rescan list again.
197          */
198         if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
199                 vp->v_data = NULL;
200                 np->n_vnode = NULL;
201                 vrele(vp);
202                 FREE(np, M_NWNODE);
203                 goto rescan;
204         }
205         *vpp = vp;
206         nhpp = NWNOHASH(fid);
207         LIST_INSERT_HEAD(nhpp, np, n_hash);
208         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
209         lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
210         
211         ASSERT_VOP_LOCKED(dvp, "nwfs_allocvp");
212         if (vp->v_type == VDIR && dvp && (dvp->v_vflag & VV_ROOT) == 0) {
213                 np->n_flag |= NREFPARENT;
214                 vref(dvp);
215         }
216         return 0;
217 }
218
219 int
220 nwfs_nget(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
221           struct vnode *dvp, struct vnode **vpp)
222 {
223         struct vnode *vp;
224         int error;
225
226         *vpp = NULL;
227         error = nwfs_allocvp(mp, fid, fap, dvp, &vp);
228         if (error)
229                 return error;
230         if (fap)
231                 nwfs_attr_cacheenter(vp, fap);
232         *vpp = vp;
233         return 0;
234 }
235
236 int
237 nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct thread *td,
238         struct nwnode **npp)
239 {
240         int error;
241
242         lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
243         error = nwfs_hashlookup(nmp, fid, npp);
244         lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
245         return error;
246 }
247
248 /*
249  * Free nwnode, and give vnode back to system
250  */
251 int
252 nwfs_reclaim(ap)                     
253         struct vop_reclaim_args /* {
254                 struct vnode *a_vp;
255                 struct thread *a_td;
256         } */ *ap;
257 {
258         struct vnode *dvp = NULL, *vp = ap->a_vp;
259         struct nwnode *dnp, *np = VTONW(vp);
260         struct nwmount *nmp = VTONWFS(vp);
261         struct thread *td = ap->a_td;
262         
263         NCPVNDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
264         /*
265          * Destroy the vm object and flush associated pages.
266          */
267         vnode_destroy_vobject(vp);
268
269         if (np->n_flag & NREFPARENT) {
270                 np->n_flag &= ~NREFPARENT;
271                 if (nwfs_lookupnp(nmp, np->n_parent, td, &dnp) == 0) {
272                         dvp = dnp->n_vnode;
273                 } else {
274                         NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
275                 }
276         }
277         lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
278         LIST_REMOVE(np, n_hash);
279         lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
280         if (nmp->n_root == np) {
281                 nmp->n_root = NULL;
282         }
283         vp->v_data = NULL;
284         FREE(np, M_NWNODE);
285         if (dvp) {
286                 vrele(dvp);
287         }
288         return (0);
289 }
290
291 int
292 nwfs_inactive(ap)
293         struct vop_inactive_args /* {
294                 struct vnode *a_vp;
295                 struct thread *a_td;
296         } */ *ap;
297 {
298         struct thread *td = ap->a_td;
299         struct ucred *cred = td->td_ucred;
300         struct vnode *vp = ap->a_vp;
301         struct nwnode *np = VTONW(vp);
302         int error;
303
304         NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vrefcnt(vp));
305         if (np->opened) {
306                 error = nwfs_vinvalbuf(vp, td);
307                 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
308                 np->opened = 0;
309         }
310         if (np->n_flag & NSHOULDFREE) {
311                 cache_purge(vp);
312                 vgone(vp);
313         }
314         return (0);
315 }
316 /*
317  * routines to maintain vnode attributes cache
318  * nwfs_attr_cacheenter: unpack np.i to va structure
319  */
320 void
321 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
322 {
323         struct nwnode *np = VTONW(vp);
324         struct nwmount *nmp = VTONWFS(vp);
325         struct vattr *va = &np->n_vattr;
326
327         va->va_type = vp->v_type;               /* vnode type (for create) */
328         np->n_nmlen = fi->nameLen;
329         bcopy(fi->entryName, np->n_name, np->n_nmlen);
330         np->n_name[fi->nameLen] = 0;
331         if (vp->v_type == VREG) {
332                 if (va->va_size != fi->dataStreamSize) {
333                         va->va_size = fi->dataStreamSize;
334                         vnode_pager_setsize(vp, va->va_size);
335                 }
336                 va->va_mode = nmp->m.file_mode; /* files access mode and type */
337         } else if (vp->v_type == VDIR) {
338                 va->va_size = 16384;            /* should be a better way ... */
339                 va->va_mode = nmp->m.dir_mode;  /* files access mode and type */
340         } else
341                 return;
342         np->n_size = va->va_size;
343         va->va_nlink = 1;               /* number of references to file */
344         va->va_uid = nmp->m.uid;        /* owner user id */
345         va->va_gid = nmp->m.gid;        /* owner group id */
346         va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
347         va->va_fileid = np->n_fid.f_id; /* file id */
348         if (va->va_fileid == 0)
349                 va->va_fileid = NWFS_ROOT_INO;
350         va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
351         /* time of last modification */
352         ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
353         /* time of last access */
354         ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
355         va->va_ctime = va->va_mtime;    /* time file changed */
356         va->va_gen = VNOVAL;            /* generation number of file */
357         va->va_flags = 0;               /* flags defined for file */
358         va->va_rdev = VNOVAL;           /* device the special file represents */
359         va->va_bytes = va->va_size;     /* bytes of disk space held by file */
360         va->va_filerev = 0;             /* file modification number */
361         va->va_vaflags = 0;             /* operations flags */
362         np->n_vattr = *va;
363         if (np->n_mtime == 0) {
364                 np->n_mtime = va->va_mtime.tv_sec;
365         }
366         np->n_atime = time_second;
367         np->n_dosfid = fi->DosDirNum;
368         return;
369 }
370
371 int
372 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
373 {
374         struct nwnode *np = VTONW(vp);
375         int diff;
376
377         diff = time_second - np->n_atime;
378         if (diff > 2) { /* XXX should be configurable */
379                 return ENOENT;
380         }
381         *va = np->n_vattr;
382         return 0;
383 }