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 extern vop_t **nwfs_vnodeop_p;
64
65 static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
66 static u_long nwnodehash;
67 static struct lock nwhashlock;
68
69 MALLOC_DEFINE(M_NWNODE, "NWFS node", "NWFS vnode private part");
70 MALLOC_DEFINE(M_NWFSHASH, "NWFS hash", "NWFS has table");
71
72 static int nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS);
73
74 SYSCTL_DECL(_vfs_nwfs);
75
76 SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
77 NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");
78
79 void
80 nwfs_hash_init(void) {
81 nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
82 lockinit(&nwhashlock, PVFS, "nwfshl", 0, 0);
83 }
84
85 void
86 nwfs_hash_free(void) {
87 lockdestroy(&nwhashlock);
88 free(nwhashtbl, M_NWFSHASH);
89 }
90
91 int
92 nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS) {
93 struct nwnode *np;
94 struct nwnode_hash_head *nhpp;
95 struct vnode *vp;
96 int i;
97
98 if (nwfs_debuglevel == 0)
99 return 0;
100 printf("Name:uc:hc:fid:pfid\n");
101 for(i = 0; i <= nwnodehash; i++) {
102 nhpp = &nwhashtbl[i];
103 LIST_FOREACH(np, nhpp, n_hash) {
104 vp = NWTOV(np);
105 vprint(NULL, vp);
106 printf("%s:%d:%d:%d:%d\n",np->n_name,vrefcnt(vp),
107 vp->v_holdcnt,np->n_fid.f_id, np->n_fid.f_parent);
108 }
109 }
110 return 0;
111 }
112
113 /*
114 * Search nwnode with given fid.
115 * Hash list should be locked by caller.
116 */
117 static int
118 nwfs_hashlookup(struct nwmount *nmp, ncpfid fid, struct nwnode **npp)
119 {
120 struct nwnode *np;
121 struct nwnode_hash_head *nhpp;
122
123 nhpp = NWNOHASH(fid);
124 LIST_FOREACH(np, nhpp, n_hash) {
125 if (nmp != np->n_mount || !NWCMPF(&fid, &np->n_fid))
126 continue;
127 if (npp)
128 *npp = np;
129 return 0;
130 }
131 return ENOENT;
132 }
133
134 /*
135 * Allocate new nwfsnode/vnode from given nwnode.
136 * Vnode referenced and not locked.
137 */
138 static int
139 nwfs_allocvp(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
140 struct vnode *dvp, struct vnode **vpp)
141 {
142 struct thread *td = curthread; /* XXX */
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 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
151 rescan:
152 if (nwfs_hashlookup(nmp, fid, &np) == 0) {
153 vp = NWTOV(np);
154 mtx_lock(&vp->v_interlock);
155 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
156 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
157 goto loop;
158 if (fap)
159 np->n_attr = fap->attributes;
160 *vpp = vp;
161 return(0);
162 }
163 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
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 MALLOC(np, struct nwnode *, sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
173 error = getnewvnode("nwfs", mp, nwfs_vnodeop_p, &vp);
174 if (error) {
175 *vpp = NULL;
176 FREE(np, M_NWNODE);
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 vp->v_vnlock->lk_flags |= LK_CANRECURSE;
189 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
190 /*
191 * Another process can create vnode while we blocked in malloc() or
192 * getnewvnode(). Rescan list again.
193 */
194 if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
195 vp->v_data = NULL;
196 np->n_vnode = NULL;
197 vrele(vp);
198 FREE(np, M_NWNODE);
199 goto rescan;
200 }
201 *vpp = vp;
202 nhpp = NWNOHASH(fid);
203 LIST_INSERT_HEAD(nhpp, np, n_hash);
204 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
205 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
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 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
239 error = nwfs_hashlookup(nmp, fid, npp);
240 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
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 if (np->n_flag & NREFPARENT) {
261 np->n_flag &= ~NREFPARENT;
262 if (nwfs_lookupnp(nmp, np->n_parent, td, &dnp) == 0) {
263 dvp = dnp->n_vnode;
264 } else {
265 NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
266 }
267 }
268 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
269 LIST_REMOVE(np, n_hash);
270 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
271 if (nmp->n_root == np) {
272 nmp->n_root = NULL;
273 }
274 vp->v_data = NULL;
275 FREE(np, M_NWNODE);
276 if (dvp) {
277 vrele(dvp);
278 }
279 return (0);
280 }
281
282 int
283 nwfs_inactive(ap)
284 struct vop_inactive_args /* {
285 struct vnode *a_vp;
286 struct thread *a_td;
287 } */ *ap;
288 {
289 struct thread *td = ap->a_td;
290 struct ucred *cred = td->td_ucred;
291 struct vnode *vp = ap->a_vp;
292 struct nwnode *np = VTONW(vp);
293 int error;
294
295 NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vrefcnt(vp));
296 if (np->opened) {
297 error = nwfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
298 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
299 np->opened = 0;
300 }
301 VOP_UNLOCK(vp, 0, td);
302 if (np->n_flag & NSHOULDFREE) {
303 cache_purge(vp);
304 vgone(vp);
305 }
306 return (0);
307 }
308 /*
309 * routines to maintain vnode attributes cache
310 * nwfs_attr_cacheenter: unpack np.i to va structure
311 */
312 void
313 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
314 {
315 struct nwnode *np = VTONW(vp);
316 struct nwmount *nmp = VTONWFS(vp);
317 struct vattr *va = &np->n_vattr;
318
319 va->va_type = vp->v_type; /* vnode type (for create) */
320 np->n_nmlen = fi->nameLen;
321 bcopy(fi->entryName, np->n_name, np->n_nmlen);
322 np->n_name[fi->nameLen] = 0;
323 if (vp->v_type == VREG) {
324 if (va->va_size != fi->dataStreamSize) {
325 va->va_size = fi->dataStreamSize;
326 vnode_pager_setsize(vp, va->va_size);
327 }
328 va->va_mode = nmp->m.file_mode; /* files access mode and type */
329 } else if (vp->v_type == VDIR) {
330 va->va_size = 16384; /* should be a better way ... */
331 va->va_mode = nmp->m.dir_mode; /* files access mode and type */
332 } else
333 return;
334 np->n_size = va->va_size;
335 va->va_nlink = 1; /* number of references to file */
336 va->va_uid = nmp->m.uid; /* owner user id */
337 va->va_gid = nmp->m.gid; /* owner group id */
338 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
339 va->va_fileid = np->n_fid.f_id; /* file id */
340 if (va->va_fileid == 0)
341 va->va_fileid = NWFS_ROOT_INO;
342 va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
343 /* time of last modification */
344 ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
345 /* time of last access */
346 ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
347 va->va_ctime = va->va_mtime; /* time file changed */
348 va->va_gen = VNOVAL; /* generation number of file */
349 va->va_flags = 0; /* flags defined for file */
350 va->va_rdev = VNOVAL; /* device the special file represents */
351 va->va_bytes = va->va_size; /* bytes of disk space held by file */
352 va->va_filerev = 0; /* file modification number */
353 va->va_vaflags = 0; /* operations flags */
354 np->n_vattr = *va;
355 if (np->n_mtime == 0) {
356 np->n_mtime = va->va_mtime.tv_sec;
357 }
358 np->n_atime = time_second;
359 np->n_dosfid = fi->DosDirNum;
360 return;
361 }
362
363 int
364 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
365 {
366 struct nwnode *np = VTONW(vp);
367 int diff;
368
369 diff = time_second - np->n_atime;
370 if (diff > 2) { /* XXX should be configurable */
371 return ENOENT;
372 }
373 *va = np->n_vattr;
374 return 0;
375 }
Cache object: b71345bdeac7c2e9214b6f608da57272
|