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