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