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 nwnode *np;
144 struct nwnode_hash_head *nhpp;
145 struct nwmount *nmp = VFSTONWFS(mp);
146 struct vnode *vp;
147 int error;
148
149 loop:
150 sx_xlock(&nwhashlock);
151 rescan:
152 if (nwfs_hashlookup(nmp, fid, &np) == 0) {
153 vp = NWTOV(np);
154 mtx_lock(&vp->v_interlock);
155 sx_xunlock(&nwhashlock);
156 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, curthread))
157 goto loop;
158 if (fap)
159 np->n_attr = fap->attributes;
160 *vpp = vp;
161 return(0);
162 }
163 sx_xunlock(&nwhashlock);
164
165 if (fap == NULL || ((fap->attributes & aDIR) == 0 && dvp == NULL))
166 panic("nwfs_allocvp: fap = %p, dvp = %p\n", fap, dvp);
167 /*
168 * Do the MALLOC before the getnewvnode since doing so afterward
169 * might cause a bogus v_data pointer to get dereferenced
170 * elsewhere if MALLOC should block.
171 */
172 np = malloc(sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
173 error = getnewvnode("nwfs", mp, &nwfs_vnodeops, &vp);
174 if (error) {
175 *vpp = NULL;
176 free(np, M_NWNODE);
177 return (error);
178 }
179 error = insmntque(vp, mp); /* XXX: Too early for mpsafe fs */
180 if (error != 0) {
181 free(np, M_NWNODE);
182 *vpp = NULL;
183 return (error);
184 }
185 vp->v_data = np;
186 np->n_vnode = vp;
187 np->n_mount = nmp;
188 np->n_attr = fap->attributes;
189 vp->v_type = np->n_attr & aDIR ? VDIR : VREG;
190 np->n_fid = fid;
191 if (dvp) {
192 np->n_parent = VTONW(dvp)->n_fid;
193 }
194 VN_LOCK_AREC(vp);
195 sx_xlock(&nwhashlock);
196 /*
197 * Another process can create vnode while we blocked in malloc() or
198 * getnewvnode(). Rescan list again.
199 */
200 if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
201 vp->v_data = NULL;
202 np->n_vnode = NULL;
203 vrele(vp);
204 free(np, M_NWNODE);
205 goto rescan;
206 }
207 *vpp = vp;
208 nhpp = NWNOHASH(fid);
209 LIST_INSERT_HEAD(nhpp, np, n_hash);
210 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
211 sx_xunlock(&nwhashlock);
212
213 ASSERT_VOP_LOCKED(dvp, "nwfs_allocvp");
214 if (vp->v_type == VDIR && dvp && (dvp->v_vflag & VV_ROOT) == 0) {
215 np->n_flag |= NREFPARENT;
216 vref(dvp);
217 }
218 return 0;
219 }
220
221 int
222 nwfs_nget(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
223 struct vnode *dvp, struct vnode **vpp)
224 {
225 struct vnode *vp;
226 int error;
227
228 *vpp = NULL;
229 error = nwfs_allocvp(mp, fid, fap, dvp, &vp);
230 if (error)
231 return error;
232 if (fap)
233 nwfs_attr_cacheenter(vp, fap);
234 *vpp = vp;
235 return 0;
236 }
237
238 int
239 nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct thread *td,
240 struct nwnode **npp)
241 {
242 int error;
243
244 sx_xlock(&nwhashlock);
245 error = nwfs_hashlookup(nmp, fid, npp);
246 sx_xunlock(&nwhashlock);
247 return error;
248 }
249
250 /*
251 * Free nwnode, and give vnode back to system
252 */
253 int
254 nwfs_reclaim(ap)
255 struct vop_reclaim_args /* {
256 struct vnode *a_vp;
257 struct thread *a_td;
258 } */ *ap;
259 {
260 struct vnode *dvp = NULL, *vp = ap->a_vp;
261 struct nwnode *dnp, *np = VTONW(vp);
262 struct nwmount *nmp = VTONWFS(vp);
263 struct thread *td = ap->a_td;
264
265 NCPVNDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
266 /*
267 * Destroy the vm object and flush associated pages.
268 */
269 vnode_destroy_vobject(vp);
270
271 if (np->n_flag & NREFPARENT) {
272 np->n_flag &= ~NREFPARENT;
273 if (nwfs_lookupnp(nmp, np->n_parent, td, &dnp) == 0) {
274 dvp = dnp->n_vnode;
275 } else {
276 NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
277 }
278 }
279 sx_xlock(&nwhashlock);
280 LIST_REMOVE(np, n_hash);
281 sx_xunlock(&nwhashlock);
282 if (nmp->n_root == np) {
283 nmp->n_root = NULL;
284 }
285 vp->v_data = NULL;
286 free(np, M_NWNODE);
287 if (dvp) {
288 vrele(dvp);
289 }
290 return (0);
291 }
292
293 int
294 nwfs_inactive(ap)
295 struct vop_inactive_args /* {
296 struct vnode *a_vp;
297 struct thread *a_td;
298 } */ *ap;
299 {
300 struct thread *td = ap->a_td;
301 struct ucred *cred = td->td_ucred;
302 struct vnode *vp = ap->a_vp;
303 struct nwnode *np = VTONW(vp);
304 int error;
305
306 NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vrefcnt(vp));
307 if (np->opened) {
308 error = nwfs_vinvalbuf(vp, td);
309 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
310 np->opened = 0;
311 }
312 if (np->n_flag & NSHOULDFREE) {
313 cache_purge(vp);
314 vgone(vp);
315 }
316 return (0);
317 }
318 /*
319 * routines to maintain vnode attributes cache
320 * nwfs_attr_cacheenter: unpack np.i to va structure
321 */
322 void
323 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
324 {
325 struct nwnode *np = VTONW(vp);
326 struct nwmount *nmp = VTONWFS(vp);
327 struct vattr *va = &np->n_vattr;
328
329 va->va_type = vp->v_type; /* vnode type (for create) */
330 np->n_nmlen = fi->nameLen;
331 bcopy(fi->entryName, np->n_name, np->n_nmlen);
332 np->n_name[fi->nameLen] = 0;
333 if (vp->v_type == VREG) {
334 if (va->va_size != fi->dataStreamSize) {
335 va->va_size = fi->dataStreamSize;
336 vnode_pager_setsize(vp, va->va_size);
337 }
338 va->va_mode = nmp->m.file_mode; /* files access mode and type */
339 } else if (vp->v_type == VDIR) {
340 va->va_size = 16384; /* should be a better way ... */
341 va->va_mode = nmp->m.dir_mode; /* files access mode and type */
342 } else
343 return;
344 np->n_size = va->va_size;
345 va->va_nlink = 1; /* number of references to file */
346 va->va_uid = nmp->m.uid; /* owner user id */
347 va->va_gid = nmp->m.gid; /* owner group id */
348 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
349 va->va_fileid = np->n_fid.f_id; /* file id */
350 if (va->va_fileid == 0)
351 va->va_fileid = NWFS_ROOT_INO;
352 va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
353 /* time of last modification */
354 ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
355 /* time of last access */
356 ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
357 va->va_ctime = va->va_mtime; /* time file changed */
358 va->va_gen = VNOVAL; /* generation number of file */
359 va->va_flags = 0; /* flags defined for file */
360 va->va_rdev = VNOVAL; /* device the special file represents */
361 va->va_bytes = va->va_size; /* bytes of disk space held by file */
362 va->va_filerev = 0; /* file modification number */
363 va->va_vaflags = 0; /* operations flags */
364 np->n_vattr = *va;
365 if (np->n_mtime == 0) {
366 np->n_mtime = va->va_mtime.tv_sec;
367 }
368 np->n_atime = time_second;
369 np->n_dosfid = fi->DosDirNum;
370 return;
371 }
372
373 int
374 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
375 {
376 struct nwnode *np = VTONW(vp);
377 int diff;
378
379 diff = time_second - np->n_atime;
380 if (diff > 2) { /* XXX should be configurable */
381 return ENOENT;
382 }
383 *va = np->n_vattr;
384 return 0;
385 }
Cache object: 63b0217ca5b882ec421318c6f0f6008f
|