]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/fs/nwfs/nwfs_node.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/lock.h>
32 #include <sys/malloc.h>
33 #include <sys/mount.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/queue.h>
37 #include <sys/sx.h>
38 #include <sys/sysctl.h>
39 #include <sys/time.h>
40 #include <sys/vnode.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_extern.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_object.h>
46
47 #include <netncp/ncp.h>
48 #include <netncp/ncp_conn.h>
49 #include <netncp/ncp_subr.h>
50
51 #include <fs/nwfs/nwfs.h>
52 #include <fs/nwfs/nwfs_mount.h>
53 #include <fs/nwfs/nwfs_node.h>
54 #include <fs/nwfs/nwfs_subr.h>
55
56 #define NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])
57
58 static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
59 static u_long nwnodehash;
60 static struct sx nwhashlock;
61
62 static MALLOC_DEFINE(M_NWNODE, "nwfs_node", "NWFS vnode private part");
63 static MALLOC_DEFINE(M_NWFSHASH, "nwfs_hash", "NWFS has table");
64
65 static int nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS);
66
67 SYSCTL_DECL(_vfs_nwfs);
68
69 SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
70             NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");
71
72 void
73 nwfs_hash_init(void) {
74         nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
75         sx_init(&nwhashlock, "nwfshl");
76 }
77
78 void
79 nwfs_hash_free(void) {
80         sx_destroy(&nwhashlock);
81         free(nwhashtbl, M_NWFSHASH);
82 }
83
84 int
85 nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS) {
86         struct nwnode *np;
87         struct nwnode_hash_head *nhpp;
88         struct vnode *vp;
89         int i;
90
91         if (nwfs_debuglevel == 0)
92                 return 0;
93         printf("Name:uc:hc:fid:pfid\n");
94         for(i = 0; i <= nwnodehash; i++) {
95                 nhpp = &nwhashtbl[i];
96                 LIST_FOREACH(np, nhpp, n_hash) {
97                         vp = NWTOV(np);
98                         vprint("", vp);
99                         printf("%s:%d:%d:%d:%d\n",np->n_name,vrefcnt(vp),
100                             vp->v_holdcnt,np->n_fid.f_id, np->n_fid.f_parent);
101                 }
102         }
103         return 0;
104 }
105
106 /*
107  * Search nwnode with given fid.
108  * Hash list should be locked by caller.
109  */
110 static int
111 nwfs_hashlookup(struct nwmount *nmp, ncpfid fid, struct nwnode **npp)
112 {
113         struct nwnode *np;
114         struct nwnode_hash_head *nhpp;
115
116         sx_assert(&nwhashlock, SA_XLOCKED);
117
118         nhpp = NWNOHASH(fid);
119         LIST_FOREACH(np, nhpp, n_hash) {
120                 if (nmp != np->n_mount || !NWCMPF(&fid, &np->n_fid))
121                         continue;
122                 if (npp)
123                         *npp = np;
124                 return 0;
125         }
126         return ENOENT;
127 }
128
129 /*
130  * Allocate new nwfsnode/vnode from given nwnode. 
131  * Vnode referenced and not locked.
132  */
133 static int
134 nwfs_allocvp(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
135         struct vnode *dvp, struct vnode **vpp)
136 {
137         struct nwnode *np;
138         struct nwnode_hash_head *nhpp;
139         struct nwmount *nmp = VFSTONWFS(mp);
140         struct vnode *vp;
141         int error;
142
143 loop:
144         sx_xlock(&nwhashlock);
145 rescan:
146         if (nwfs_hashlookup(nmp, fid, &np) == 0) {
147                 vp = NWTOV(np);
148                 VI_LOCK(vp);
149                 sx_xunlock(&nwhashlock);
150                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, curthread))
151                         goto loop;
152                 if (fap)
153                         np->n_attr = fap->attributes;
154                 *vpp = vp;
155                 return(0);
156         }
157         sx_xunlock(&nwhashlock);
158
159         if (fap == NULL || ((fap->attributes & aDIR) == 0 && dvp == NULL))
160                 panic("nwfs_allocvp: fap = %p, dvp = %p\n", fap, dvp);
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         np = malloc(sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
167         error = getnewvnode("nwfs", mp, &nwfs_vnodeops, &vp);
168         if (error) {
169                 *vpp = NULL;
170                 free(np, M_NWNODE);
171                 return (error);
172         }
173         error = insmntque(vp, mp);      /* XXX: Too early for mpsafe fs */
174         if (error != 0) {
175                 free(np, M_NWNODE);
176                 *vpp = NULL;
177                 return (error);
178         }
179         vp->v_data = np;
180         np->n_vnode = vp;
181         np->n_mount = nmp;
182         np->n_attr = fap->attributes;
183         vp->v_type = np->n_attr & aDIR ? VDIR : VREG;
184         np->n_fid = fid;
185         if (dvp) {
186                 np->n_parent = VTONW(dvp)->n_fid;
187         }
188         sx_xlock(&nwhashlock);
189         /*
190          * Another process can create vnode while we blocked in malloc() or
191          * getnewvnode(). Rescan list again.
192          */
193         if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
194                 vp->v_data = NULL;
195                 np->n_vnode = NULL;
196                 vrele(vp);
197                 free(np, M_NWNODE);
198                 goto rescan;
199         }
200         *vpp = vp;
201         nhpp = NWNOHASH(fid);
202         LIST_INSERT_HEAD(nhpp, np, n_hash);
203         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
204         VN_LOCK_AREC(vp);
205         sx_xunlock(&nwhashlock);
206         
207         ASSERT_VOP_LOCKED(dvp, "nwfs_allocvp");
208         if (vp->v_type == VDIR && dvp && (dvp->v_vflag & VV_ROOT) == 0) {
209                 np->n_flag |= NREFPARENT;
210                 vref(dvp);
211         }
212         return 0;
213 }
214
215 int
216 nwfs_nget(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
217           struct vnode *dvp, struct vnode **vpp)
218 {
219         struct vnode *vp;
220         int error;
221
222         *vpp = NULL;
223         error = nwfs_allocvp(mp, fid, fap, dvp, &vp);
224         if (error)
225                 return error;
226         if (fap)
227                 nwfs_attr_cacheenter(vp, fap);
228         *vpp = vp;
229         return 0;
230 }
231
232 int
233 nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct thread *td,
234         struct nwnode **npp)
235 {
236         int error;
237
238         sx_xlock(&nwhashlock);
239         error = nwfs_hashlookup(nmp, fid, npp);
240         sx_xunlock(&nwhashlock);
241         return error;
242 }
243
244 /*
245  * Free nwnode, and give vnode back to system
246  */
247 int
248 nwfs_reclaim(ap)                     
249         struct vop_reclaim_args /* {
250                 struct vnode *a_vp;
251                 struct thread *a_td;
252         } */ *ap;
253 {
254         struct vnode *dvp = NULL, *vp = ap->a_vp;
255         struct nwnode *dnp, *np = VTONW(vp);
256         struct nwmount *nmp = VTONWFS(vp);
257         struct thread *td = ap->a_td;
258         
259         NCPVNDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
260         /*
261          * Destroy the vm object and flush associated pages.
262          */
263         vnode_destroy_vobject(vp);
264
265         if (np->n_flag & NREFPARENT) {
266                 np->n_flag &= ~NREFPARENT;
267                 if (nwfs_lookupnp(nmp, np->n_parent, td, &dnp) == 0) {
268                         dvp = dnp->n_vnode;
269                 } else {
270                         NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
271                 }
272         }
273         sx_xlock(&nwhashlock);
274         LIST_REMOVE(np, n_hash);
275         sx_xunlock(&nwhashlock);
276         if (nmp->n_root == np) {
277                 nmp->n_root = NULL;
278         }
279         vp->v_data = NULL;
280         free(np, M_NWNODE);
281         if (dvp) {
282                 vrele(dvp);
283         }
284         return (0);
285 }
286
287 int
288 nwfs_inactive(ap)
289         struct vop_inactive_args /* {
290                 struct vnode *a_vp;
291                 struct thread *a_td;
292         } */ *ap;
293 {
294         struct thread *td = ap->a_td;
295         struct ucred *cred = td->td_ucred;
296         struct vnode *vp = ap->a_vp;
297         struct nwnode *np = VTONW(vp);
298         int error;
299
300         NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vrefcnt(vp));
301         if (np->opened) {
302                 error = nwfs_vinvalbuf(vp, td);
303                 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
304                 np->opened = 0;
305         }
306         if (np->n_flag & NSHOULDFREE) {
307                 cache_purge(vp);
308                 vgone(vp);
309         }
310         return (0);
311 }
312 /*
313  * routines to maintain vnode attributes cache
314  * nwfs_attr_cacheenter: unpack np.i to va structure
315  */
316 void
317 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
318 {
319         struct nwnode *np = VTONW(vp);
320         struct nwmount *nmp = VTONWFS(vp);
321         struct vattr *va = &np->n_vattr;
322
323         va->va_type = vp->v_type;               /* vnode type (for create) */
324         np->n_nmlen = fi->nameLen;
325         bcopy(fi->entryName, np->n_name, np->n_nmlen);
326         np->n_name[fi->nameLen] = 0;
327         if (vp->v_type == VREG) {
328                 if (va->va_size != fi->dataStreamSize) {
329                         va->va_size = fi->dataStreamSize;
330                         vnode_pager_setsize(vp, va->va_size);
331                 }
332                 va->va_mode = nmp->m.file_mode; /* files access mode and type */
333         } else if (vp->v_type == VDIR) {
334                 va->va_size = 16384;            /* should be a better way ... */
335                 va->va_mode = nmp->m.dir_mode;  /* files access mode and type */
336         } else
337                 return;
338         np->n_size = va->va_size;
339         va->va_nlink = 1;               /* number of references to file */
340         va->va_uid = nmp->m.uid;        /* owner user id */
341         va->va_gid = nmp->m.gid;        /* owner group id */
342         va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
343         va->va_fileid = np->n_fid.f_id; /* file id */
344         if (va->va_fileid == 0)
345                 va->va_fileid = NWFS_ROOT_INO;
346         va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
347         /* time of last modification */
348         ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
349         /* time of last access */
350         ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
351         va->va_ctime = va->va_mtime;    /* time file changed */
352         va->va_gen = VNOVAL;            /* generation number of file */
353         va->va_flags = 0;               /* flags defined for file */
354         va->va_rdev = VNOVAL;           /* device the special file represents */
355         va->va_bytes = va->va_size;     /* bytes of disk space held by file */
356         va->va_filerev = 0;             /* file modification number */
357         va->va_vaflags = 0;             /* operations flags */
358         np->n_vattr = *va;
359         if (np->n_mtime == 0) {
360                 np->n_mtime = va->va_mtime.tv_sec;
361         }
362         np->n_atime = time_second;
363         np->n_dosfid = fi->DosDirNum;
364         return;
365 }
366
367 int
368 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
369 {
370         struct nwnode *np = VTONW(vp);
371         int diff;
372
373         diff = time_second - np->n_atime;
374         if (diff > 2) { /* XXX should be configurable */
375                 return ENOENT;
376         }
377         *va = np->n_vattr;
378         return 0;
379 }