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: releng/5.0/sys/fs/nwfs/nwfs_node.c 105077 2002-10-14 03:20:36Z mckusick $
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 cache_purge(vp);
272 if (nmp->n_root == np) {
273 nmp->n_root = NULL;
274 }
275 vp->v_data = NULL;
276 FREE(np, M_NWNODE);
277 if (dvp) {
278 vrele(dvp);
279 }
280 return (0);
281 }
282
283 int
284 nwfs_inactive(ap)
285 struct vop_inactive_args /* {
286 struct vnode *a_vp;
287 struct thread *a_td;
288 } */ *ap;
289 {
290 struct thread *td = ap->a_td;
291 struct ucred *cred = td->td_ucred;
292 struct vnode *vp = ap->a_vp;
293 struct nwnode *np = VTONW(vp);
294 int error;
295
296 NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vrefcnt(vp));
297 if (np->opened) {
298 error = nwfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
299 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
300 np->opened = 0;
301 }
302 VOP_UNLOCK(vp, 0, td);
303 if (np->n_flag & NSHOULDFREE) {
304 cache_purge(vp);
305 vgone(vp);
306 }
307 return (0);
308 }
309 /*
310 * routines to maintain vnode attributes cache
311 * nwfs_attr_cacheenter: unpack np.i to va structure
312 */
313 void
314 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
315 {
316 struct nwnode *np = VTONW(vp);
317 struct nwmount *nmp = VTONWFS(vp);
318 struct vattr *va = &np->n_vattr;
319
320 va->va_type = vp->v_type; /* vnode type (for create) */
321 np->n_nmlen = fi->nameLen;
322 bcopy(fi->entryName, np->n_name, np->n_nmlen);
323 np->n_name[fi->nameLen] = 0;
324 if (vp->v_type == VREG) {
325 if (va->va_size != fi->dataStreamSize) {
326 va->va_size = fi->dataStreamSize;
327 vnode_pager_setsize(vp, va->va_size);
328 }
329 va->va_mode = nmp->m.file_mode; /* files access mode and type */
330 } else if (vp->v_type == VDIR) {
331 va->va_size = 16384; /* should be a better way ... */
332 va->va_mode = nmp->m.dir_mode; /* files access mode and type */
333 } else
334 return;
335 np->n_size = va->va_size;
336 va->va_nlink = 1; /* number of references to file */
337 va->va_uid = nmp->m.uid; /* owner user id */
338 va->va_gid = nmp->m.gid; /* owner group id */
339 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
340 va->va_fileid = np->n_fid.f_id; /* file id */
341 if (va->va_fileid == 0)
342 va->va_fileid = NWFS_ROOT_INO;
343 va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
344 /* time of last modification */
345 ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
346 /* time of last access */
347 ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
348 va->va_ctime = va->va_mtime; /* time file changed */
349 va->va_gen = VNOVAL; /* generation number of file */
350 va->va_flags = 0; /* flags defined for file */
351 va->va_rdev = VNOVAL; /* device the special file represents */
352 va->va_bytes = va->va_size; /* bytes of disk space held by file */
353 va->va_filerev = 0; /* file modification number */
354 va->va_vaflags = 0; /* operations flags */
355 np->n_vattr = *va;
356 if (np->n_mtime == 0) {
357 np->n_mtime = va->va_mtime.tv_sec;
358 }
359 np->n_atime = time_second;
360 np->n_dosfid = fi->DosDirNum;
361 return;
362 }
363
364 int
365 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
366 {
367 struct nwnode *np = VTONW(vp);
368 int diff;
369
370 diff = time_second - np->n_atime;
371 if (diff > 2) { /* XXX should be configurable */
372 return ENOENT;
373 }
374 *va = np->n_vattr;
375 return 0;
376 }
Cache object: 7fb1c46e7e4777c7c447ff53a436a2c3
|