2 * Copyright (c) 1999, 2000 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_object.h>
52 #include <netncp/ncp.h>
53 #include <netncp/ncp_conn.h>
54 #include <netncp/ncp_subr.h>
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>
61 #define NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])
63 static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
64 static u_long nwnodehash;
65 static struct lock nwhashlock;
67 static MALLOC_DEFINE(M_NWNODE, "nwfs_node", "NWFS vnode private part");
68 static MALLOC_DEFINE(M_NWFSHASH, "nwfs_hash", "NWFS has table");
70 static int nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS);
72 SYSCTL_DECL(_vfs_nwfs);
74 SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
75 NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");
78 nwfs_hash_init(void) {
79 nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
80 lockinit(&nwhashlock, PVFS, "nwfshl", 0, 0);
84 nwfs_hash_free(void) {
85 lockdestroy(&nwhashlock);
86 free(nwhashtbl, M_NWFSHASH);
90 nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS) {
92 struct nwnode_hash_head *nhpp;
96 if (nwfs_debuglevel == 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) {
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);
112 * Search nwnode with given fid.
113 * Hash list should be locked by caller.
116 nwfs_hashlookup(struct nwmount *nmp, ncpfid fid, struct nwnode **npp)
119 struct nwnode_hash_head *nhpp;
121 nhpp = NWNOHASH(fid);
122 LIST_FOREACH(np, nhpp, n_hash) {
123 if (nmp != np->n_mount || !NWCMPF(&fid, &np->n_fid))
133 * Allocate new nwfsnode/vnode from given nwnode.
134 * Vnode referenced and not locked.
137 nwfs_allocvp(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
138 struct vnode *dvp, struct vnode **vpp)
140 struct thread *td = curthread; /* XXX */
142 struct nwnode_hash_head *nhpp;
143 struct nwmount *nmp = VFSTONWFS(mp);
148 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
150 if (nwfs_hashlookup(nmp, fid, &np) == 0) {
152 mtx_lock(&vp->v_interlock);
153 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
154 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
157 np->n_attr = fap->attributes;
161 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
163 if (fap == NULL || ((fap->attributes & aDIR) == 0 && dvp == NULL))
164 panic("nwfs_allocvp: fap = %p, dvp = %p\n", fap, dvp);
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.
170 MALLOC(np, struct nwnode *, sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
171 error = getnewvnode("nwfs", mp, &nwfs_vnodeops, &vp);
177 error = insmntque(vp, mp); /* XXX: Too early for mpsafe fs */
186 np->n_attr = fap->attributes;
187 vp->v_type = np->n_attr & aDIR ? VDIR : VREG;
190 np->n_parent = VTONW(dvp)->n_fid;
192 vp->v_vnlock->lk_flags |= LK_CANRECURSE;
193 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
195 * Another process can create vnode while we blocked in malloc() or
196 * getnewvnode(). Rescan list again.
198 if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
206 nhpp = NWNOHASH(fid);
207 LIST_INSERT_HEAD(nhpp, np, n_hash);
208 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
209 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
211 ASSERT_VOP_LOCKED(dvp, "nwfs_allocvp");
212 if (vp->v_type == VDIR && dvp && (dvp->v_vflag & VV_ROOT) == 0) {
213 np->n_flag |= NREFPARENT;
220 nwfs_nget(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
221 struct vnode *dvp, struct vnode **vpp)
227 error = nwfs_allocvp(mp, fid, fap, dvp, &vp);
231 nwfs_attr_cacheenter(vp, fap);
237 nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct thread *td,
242 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
243 error = nwfs_hashlookup(nmp, fid, npp);
244 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
249 * Free nwnode, and give vnode back to system
253 struct vop_reclaim_args /* {
258 struct vnode *dvp = NULL, *vp = ap->a_vp;
259 struct nwnode *dnp, *np = VTONW(vp);
260 struct nwmount *nmp = VTONWFS(vp);
261 struct thread *td = ap->a_td;
263 NCPVNDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
265 * Destroy the vm object and flush associated pages.
267 vnode_destroy_vobject(vp);
269 if (np->n_flag & NREFPARENT) {
270 np->n_flag &= ~NREFPARENT;
271 if (nwfs_lookupnp(nmp, np->n_parent, td, &dnp) == 0) {
274 NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
277 lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, td);
278 LIST_REMOVE(np, n_hash);
279 lockmgr(&nwhashlock, LK_RELEASE, NULL, td);
280 if (nmp->n_root == np) {
293 struct vop_inactive_args /* {
298 struct thread *td = ap->a_td;
299 struct ucred *cred = td->td_ucred;
300 struct vnode *vp = ap->a_vp;
301 struct nwnode *np = VTONW(vp);
304 NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vrefcnt(vp));
306 error = nwfs_vinvalbuf(vp, td);
307 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
310 if (np->n_flag & NSHOULDFREE) {
317 * routines to maintain vnode attributes cache
318 * nwfs_attr_cacheenter: unpack np.i to va structure
321 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
323 struct nwnode *np = VTONW(vp);
324 struct nwmount *nmp = VTONWFS(vp);
325 struct vattr *va = &np->n_vattr;
327 va->va_type = vp->v_type; /* vnode type (for create) */
328 np->n_nmlen = fi->nameLen;
329 bcopy(fi->entryName, np->n_name, np->n_nmlen);
330 np->n_name[fi->nameLen] = 0;
331 if (vp->v_type == VREG) {
332 if (va->va_size != fi->dataStreamSize) {
333 va->va_size = fi->dataStreamSize;
334 vnode_pager_setsize(vp, va->va_size);
336 va->va_mode = nmp->m.file_mode; /* files access mode and type */
337 } else if (vp->v_type == VDIR) {
338 va->va_size = 16384; /* should be a better way ... */
339 va->va_mode = nmp->m.dir_mode; /* files access mode and type */
342 np->n_size = va->va_size;
343 va->va_nlink = 1; /* number of references to file */
344 va->va_uid = nmp->m.uid; /* owner user id */
345 va->va_gid = nmp->m.gid; /* owner group id */
346 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
347 va->va_fileid = np->n_fid.f_id; /* file id */
348 if (va->va_fileid == 0)
349 va->va_fileid = NWFS_ROOT_INO;
350 va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
351 /* time of last modification */
352 ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
353 /* time of last access */
354 ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
355 va->va_ctime = va->va_mtime; /* time file changed */
356 va->va_gen = VNOVAL; /* generation number of file */
357 va->va_flags = 0; /* flags defined for file */
358 va->va_rdev = VNOVAL; /* device the special file represents */
359 va->va_bytes = va->va_size; /* bytes of disk space held by file */
360 va->va_filerev = 0; /* file modification number */
361 va->va_vaflags = 0; /* operations flags */
363 if (np->n_mtime == 0) {
364 np->n_mtime = va->va_mtime.tv_sec;
366 np->n_atime = time_second;
367 np->n_dosfid = fi->DosDirNum;
372 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
374 struct nwnode *np = VTONW(vp);
377 diff = time_second - np->n_atime;
378 if (diff > 2) { /* XXX should be configurable */