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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/mount.h>
34 #include <sys/mutex.h>
36 #include <sys/queue.h>
38 #include <sys/sysctl.h>
40 #include <sys/vnode.h>
43 #include <vm/vm_extern.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_object.h>
47 #include <netncp/ncp.h>
48 #include <netncp/ncp_conn.h>
49 #include <netncp/ncp_subr.h>
51 #include <fs/nwfs/nwfs.h>
52 #include <fs/nwfs/nwfs_mount.h>
53 #include <fs/nwfs/nwfs_node.h>
54 #include <fs/nwfs/nwfs_subr.h>
56 #define NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])
58 static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
59 static u_long nwnodehash;
60 static struct sx nwhashlock;
62 static MALLOC_DEFINE(M_NWNODE, "nwfs_node", "NWFS vnode private part");
63 static MALLOC_DEFINE(M_NWFSHASH, "nwfs_hash", "NWFS has table");
65 static int nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS);
67 SYSCTL_DECL(_vfs_nwfs);
69 SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
70 NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");
73 nwfs_hash_init(void) {
74 nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
75 sx_init(&nwhashlock, "nwfshl");
79 nwfs_hash_free(void) {
80 sx_destroy(&nwhashlock);
81 free(nwhashtbl, M_NWFSHASH);
85 nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS) {
87 struct nwnode_hash_head *nhpp;
91 if (nwfs_debuglevel == 0)
93 printf("Name:uc:hc:fid:pfid\n");
94 for(i = 0; i <= nwnodehash; i++) {
96 LIST_FOREACH(np, nhpp, n_hash) {
99 printf("%s:%d:%d:%d:%d\n",np->n_name,vrefcnt(vp),
100 vp->v_holdcnt,np->n_fid.f_id, np->n_fid.f_parent);
107 * Search nwnode with given fid.
108 * Hash list should be locked by caller.
111 nwfs_hashlookup(struct nwmount *nmp, ncpfid fid, struct nwnode **npp)
114 struct nwnode_hash_head *nhpp;
116 sx_assert(&nwhashlock, SA_XLOCKED);
118 nhpp = NWNOHASH(fid);
119 LIST_FOREACH(np, nhpp, n_hash) {
120 if (nmp != np->n_mount || !NWCMPF(&fid, &np->n_fid))
130 * Allocate new nwfsnode/vnode from given nwnode.
131 * Vnode referenced and not locked.
134 nwfs_allocvp(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
135 struct vnode *dvp, struct vnode **vpp)
138 struct nwnode_hash_head *nhpp;
139 struct nwmount *nmp = VFSTONWFS(mp);
144 sx_xlock(&nwhashlock);
146 if (nwfs_hashlookup(nmp, fid, &np) == 0) {
149 sx_xunlock(&nwhashlock);
150 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, curthread))
153 np->n_attr = fap->attributes;
157 sx_xunlock(&nwhashlock);
159 if (fap == NULL || ((fap->attributes & aDIR) == 0 && dvp == NULL))
160 panic("nwfs_allocvp: fap = %p, dvp = %p\n", fap, dvp);
162 * Do the MALLOC before the getnewvnode since doing so afterward
163 * might cause a bogus v_data pointer to get dereferenced
164 * elsewhere if MALLOC should block.
166 np = malloc(sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
167 error = getnewvnode("nwfs", mp, &nwfs_vnodeops, &vp);
173 error = insmntque(vp, mp); /* XXX: Too early for mpsafe fs */
182 np->n_attr = fap->attributes;
183 vp->v_type = np->n_attr & aDIR ? VDIR : VREG;
186 np->n_parent = VTONW(dvp)->n_fid;
188 sx_xlock(&nwhashlock);
190 * Another process can create vnode while we blocked in malloc() or
191 * getnewvnode(). Rescan list again.
193 if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
201 nhpp = NWNOHASH(fid);
202 LIST_INSERT_HEAD(nhpp, np, n_hash);
203 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
205 sx_xunlock(&nwhashlock);
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;
216 nwfs_nget(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
217 struct vnode *dvp, struct vnode **vpp)
223 error = nwfs_allocvp(mp, fid, fap, dvp, &vp);
227 nwfs_attr_cacheenter(vp, fap);
233 nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct thread *td,
238 sx_xlock(&nwhashlock);
239 error = nwfs_hashlookup(nmp, fid, npp);
240 sx_xunlock(&nwhashlock);
245 * Free nwnode, and give vnode back to system
249 struct vop_reclaim_args /* {
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;
259 NCPVNDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
261 * Destroy the vm object and flush associated pages.
263 vnode_destroy_vobject(vp);
265 if (np->n_flag & NREFPARENT) {
266 np->n_flag &= ~NREFPARENT;
267 if (nwfs_lookupnp(nmp, np->n_parent, td, &dnp) == 0) {
270 NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
273 sx_xlock(&nwhashlock);
274 LIST_REMOVE(np, n_hash);
275 sx_xunlock(&nwhashlock);
276 if (nmp->n_root == np) {
289 struct vop_inactive_args /* {
294 struct thread *td = ap->a_td;
295 struct ucred *cred = td->td_ucred;
296 struct vnode *vp = ap->a_vp;
297 struct nwnode *np = VTONW(vp);
300 NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vrefcnt(vp));
302 error = nwfs_vinvalbuf(vp, td);
303 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, td, cred);
306 if (np->n_flag & NSHOULDFREE) {
313 * routines to maintain vnode attributes cache
314 * nwfs_attr_cacheenter: unpack np.i to va structure
317 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
319 struct nwnode *np = VTONW(vp);
320 struct nwmount *nmp = VTONWFS(vp);
321 struct vattr *va = &np->n_vattr;
323 va->va_type = vp->v_type; /* vnode type (for create) */
324 np->n_nmlen = fi->nameLen;
325 bcopy(fi->entryName, np->n_name, np->n_nmlen);
326 np->n_name[fi->nameLen] = 0;
327 if (vp->v_type == VREG) {
328 if (va->va_size != fi->dataStreamSize) {
329 va->va_size = fi->dataStreamSize;
330 vnode_pager_setsize(vp, va->va_size);
332 va->va_mode = nmp->m.file_mode; /* files access mode and type */
333 } else if (vp->v_type == VDIR) {
334 va->va_size = 16384; /* should be a better way ... */
335 va->va_mode = nmp->m.dir_mode; /* files access mode and type */
338 np->n_size = va->va_size;
339 va->va_nlink = 1; /* number of references to file */
340 va->va_uid = nmp->m.uid; /* owner user id */
341 va->va_gid = nmp->m.gid; /* owner group id */
342 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
343 va->va_fileid = np->n_fid.f_id; /* file id */
344 if (va->va_fileid == 0)
345 va->va_fileid = NWFS_ROOT_INO;
346 va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
347 /* time of last modification */
348 ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
349 /* time of last access */
350 ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
351 va->va_ctime = va->va_mtime; /* time file changed */
352 va->va_gen = VNOVAL; /* generation number of file */
353 va->va_flags = 0; /* flags defined for file */
354 va->va_rdev = VNOVAL; /* device the special file represents */
355 va->va_bytes = va->va_size; /* bytes of disk space held by file */
356 va->va_filerev = 0; /* file modification number */
357 va->va_vaflags = 0; /* operations flags */
359 if (np->n_mtime == 0) {
360 np->n_mtime = va->va_mtime.tv_sec;
362 np->n_atime = time_second;
363 np->n_dosfid = fi->DosDirNum;
368 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
370 struct nwnode *np = VTONW(vp);
373 diff = time_second - np->n_atime;
374 if (diff > 2) { /* XXX should be configurable */