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