]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nwfs/nwfs_node.c
This commit was generated by cvs2svn to compensate for changes in r52143,
[FreeBSD/FreeBSD.git] / sys / fs / nwfs / nwfs_node.c
1 /*
2  * Copyright (c) 1999, 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/time.h>
38 #include <sys/proc.h>
39 #include <sys/mount.h>
40 #include <sys/namei.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_prot.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_pager.h>
50 #include <vm/vnode_pager.h>
51 #include <vm/vm_zone.h>
52 #include <sys/queue.h>
53
54 #include <netncp/ncp.h>
55 #include <netncp/ncp_conn.h>
56 #include <netncp/ncp_subr.h>
57
58 #include <nwfs/nwfs.h>
59 #include <nwfs/nwfs_mount.h>
60 #include <nwfs/nwfs_node.h>
61 #include <nwfs/nwfs_subr.h>
62
63 #define NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])
64
65 extern vop_t **nwfs_vnodeop_p;
66
67 static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
68 static u_long nwnodehash;
69 static int nwhashlock = 0;
70
71 MALLOC_DEFINE(M_NWNODE, "NWFS node", "NWFS vnode private part");
72 MALLOC_DEFINE(M_NWFSHASH, "NWFS hash", "NWFS has table");
73
74 static int nwfs_sysctl_vnprint SYSCTL_HANDLER_ARGS;
75
76 extern struct linker_set sysctl_vfs_nwfs;
77
78 SYSCTL_DECL(_vfs_nwfs);
79
80 SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
81             NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");
82
83 void
84 nwfs_hash_init(void) {
85         nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
86 }
87
88 void
89 nwfs_hash_free(void) {
90         free(nwhashtbl, M_NWFSHASH);
91 }
92
93 int
94 nwfs_sysctl_vnprint SYSCTL_HANDLER_ARGS {
95         struct nwnode *np;
96         struct nwnode_hash_head *nhpp;
97         struct vnode *vp;
98         int i;
99
100         if (nwfs_debuglevel == 0)
101                 return 0;
102         printf("Name:uc:hc:fid:pfid\n");
103         for(i = 0; i <= nwnodehash; i++) {
104                 nhpp = &nwhashtbl[i];
105                 for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
106                         vp = NWTOV(np);
107                         printf("%s:%d:%d:%d:%d\n",np->n_name,vp->v_usecount,vp->v_holdcnt,
108                             np->n_fid.f_id, np->n_fid.f_parent);
109                 }
110         }
111         return 0;
112 }
113
114 /*
115  * Allocate new nwfsnode/vnode from given nwnode. 
116  * Vnode referenced and not locked.
117  */
118 int
119 nwfs_allocvp(struct mount *mp, ncpfid fid, struct vnode **vpp) {
120         struct proc *p = curproc;       /* XXX */
121         struct nwnode *np, *np2;
122         struct nwnode_hash_head *nhpp;
123         struct vnode *vp;
124         int error;
125
126 retry:
127         nhpp = NWNOHASH(fid);
128 loop:
129         for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
130                 vp = NWTOV(np);
131                 if (mp != vp->v_mount || !NWCMPF(&fid, &np->n_fid))
132                         continue;
133                 if (vget(vp, LK_EXCLUSIVE, p))
134                         goto loop;
135                 *vpp = vp;
136                 return(0);
137         }
138
139         /* lock list, or waiting in malloc can cause problems */
140         if (nwhashlock) {
141                 while(nwhashlock) {
142                         nwhashlock = -1;
143                         tsleep((caddr_t) &nwhashlock, PVM, "nwfsvp", 0);
144                 }
145                 goto loop;
146         }
147         nwhashlock = 1;
148         /*
149          * Do the MALLOC before the getnewvnode since doing so afterward
150          * might cause a bogus v_data pointer to get dereferenced
151          * elsewhere if MALLOC should block.
152          */
153         MALLOC(np, struct nwnode *, sizeof *np, M_NWNODE, M_WAITOK);
154         error = getnewvnode(VT_NWFS, mp, nwfs_vnodeop_p, &vp);
155         if (error) {
156                 if (nwhashlock < 0)
157                         wakeup(&nwhashlock);
158                 nwhashlock = 0;
159                 *vpp = 0;
160                 FREE(np, M_NWNODE);
161                 return (error);
162         }
163         *vpp = vp;
164         bzero(np,sizeof(*np));
165         vp->v_data = np;
166         np->n_vnode = vp;
167         np->n_mount = VFSTONWFS(mp);
168         np->n_fid = fid;
169         for (np2 = nhpp->lh_first; np2 != 0; np2 = np->n_hash.le_next) {
170                 if (mp != NWTOV(np2)->v_mount || !NWCMPF(&fid, &np2->n_fid))
171                         continue;
172                 vrele(vp);
173                 FREE(np, M_NWNODE);
174                 if (nwhashlock < 0)
175                         wakeup(&nwhashlock);
176                 nwhashlock = 0;
177                 goto retry;
178         }
179         LIST_INSERT_HEAD(nhpp, np, n_hash);
180         if (nwhashlock < 0)
181                 wakeup(&nwhashlock);
182         nwhashlock = 0;
183         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
184         np->n_flag |= NNEW;
185         return (error);
186 }
187
188 int
189 nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct nwnode **npp) {
190         struct nwnode *np;
191         struct nwnode_hash_head *nhpp;
192
193         nhpp = NWNOHASH(fid);
194         for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
195                 if (nmp != np->n_mount || !NWCMPF(&fid, &np->n_fid))
196                         continue;
197                 *npp = np;
198                 return(0);
199         }
200         return ENOENT;
201 }
202
203 /*
204  * Free nwnode, and give vnode back to system
205  */
206 int
207 nwfs_reclaim(ap)                     
208         struct vop_reclaim_args /* {
209         struct vnode *a_vp;
210         } */ *ap;
211 {
212         struct vnode *dvp = NULL, *vp = ap->a_vp;
213         struct nwnode *dnp, *np = VTONW(vp);
214         struct nwmount *nmp=VTONWFS(vp);
215         
216         NCPVNDEBUG("%s,%d\n", np->n_name, vp->v_usecount);
217         if (np->n_refparent) {
218                 np->n_refparent = 0;
219                 if (nwfs_lookupnp(nmp, np->n_parent, &dnp) == 0) {
220                         dvp = dnp->n_vnode;
221                 } else {
222                         NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
223                 }
224         }
225         LIST_REMOVE(np, n_hash);
226         cache_purge(vp);
227         if (nmp->n_root == np) {
228                 nmp->n_root = NULL;
229         }
230         vp->v_data = NULL;
231         FREE(np, M_NWNODE);
232         if (dvp) {
233                 vrele(dvp);
234         }
235         return (0);
236 }
237
238 int
239 nwfs_inactive(ap)
240         struct vop_inactive_args /* {
241                 struct vnode *a_vp;
242                 struct proc *a_p;
243         } */ *ap;
244 {
245         struct proc *p = ap->a_p;
246         struct ucred *cred = p->p_ucred;
247         struct vnode *vp = ap->a_vp;
248         struct nwnode *np = VTONW(vp);
249         int error;
250
251         NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vp->v_usecount);
252         if (np->opened) {
253                 error = nwfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
254                 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, p, cred);
255                 np->opened = 0;
256         }
257         VOP_UNLOCK(vp, 0, p);
258         return (0);
259 }
260 /*
261  * routines to maintain vnode attributes cache
262  * nwfs_attr_cacheenter: unpack np.i to va structure
263  */
264 void
265 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi) {
266         struct nwnode *np = VTONW(vp);
267         struct nwmount *nmp = VTONWFS(vp);
268         register struct vattr *va = &np->n_vattr;
269
270         va->va_type = vp->v_type;               /* vnode type (for create) */
271         if (vp->v_type == VREG) {
272                 if (va->va_size != fi->dataStreamSize) {
273                         va->va_size = fi->dataStreamSize;
274                         vnode_pager_setsize(vp, va->va_size);
275                 }
276                 va->va_mode = nmp->m.file_mode; /* files access mode and type */
277         } else if (vp->v_type == VDIR) {
278                 va->va_size = 16384;            /* should be a better way ... */
279                 va->va_mode = nmp->m.dir_mode;  /* files access mode and type */
280         } else
281                 return;
282         np->n_size = va->va_size;
283         va->va_nlink = 1;               /* number of references to file */
284         va->va_uid = nmp->m.uid;        /* owner user id */
285         va->va_gid = nmp->m.gid;        /* owner group id */
286         va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
287         va->va_fileid = np->n_fid.f_id; /* file id */
288         if (va->va_fileid == 0)
289                 va->va_fileid = NWFS_ROOT_INO;
290         va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
291         /* time of last modification */
292         ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, &va->va_mtime);
293         /* time of last access */
294         ncp_dos2unixtime(fi->lastAccessDate, 0, 0, &va->va_atime);
295         va->va_ctime = va->va_mtime;    /* time file changed */
296         va->va_gen = VNOVAL;            /* generation number of file */
297         va->va_flags = 0;               /* flags defined for file */
298         va->va_rdev = VNOVAL;           /* device the special file represents */
299         va->va_bytes = va->va_size;     /* bytes of disk space held by file */
300         va->va_filerev = 0;             /* file modification number */
301         va->va_vaflags = 0;             /* operations flags */
302         np->n_vattr = *va;
303         if (np->n_mtime == 0) {
304                 np->n_mtime = va->va_mtime.tv_sec;
305         }
306         np->n_atime = time_second;
307         return;
308 }
309
310 int
311 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va) {
312         struct nwnode *np = VTONW(vp);
313         int diff;
314
315         diff = time_second - np->n_atime;
316         if (diff > 2) { /* XXX should be configurable */
317                 return ENOENT;
318         }
319         *va = np->n_vattr;
320         return 0;
321 }