summaryrefslogtreecommitdiffstats
path: root/sys/nwfs/nwfs_node.c
blob: 25d92251d1d810bb3bc6bf3649685253e2fdfaa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright (c) 1999, Boris Popov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Boris Popov.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/sysctl.h>
#include <vm/vm.h>
#include <vm/vm_extern.h>
#include <vm/vm_page.h>
#include <vm/vm_object.h>
#include <sys/queue.h>

#include <netncp/ncp.h>
#include <netncp/ncp_conn.h>
#include <netncp/ncp_subr.h>

#include <nwfs/nwfs.h>
#include <nwfs/nwfs_mount.h>
#include <nwfs/nwfs_node.h>
#include <nwfs/nwfs_subr.h>

#define	NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])

extern vop_t **nwfs_vnodeop_p;

static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
static u_long nwnodehash;
static int nwhashlock = 0;

MALLOC_DEFINE(M_NWNODE, "NWFS node", "NWFS vnode private part");
MALLOC_DEFINE(M_NWFSHASH, "NWFS hash", "NWFS has table");

static int nwfs_sysctl_vnprint SYSCTL_HANDLER_ARGS;

extern struct linker_set sysctl_vfs_nwfs;

SYSCTL_DECL(_vfs_nwfs);

SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
	    NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");

void
nwfs_hash_init(void) {
	nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
}

void
nwfs_hash_free(void) {
	free(nwhashtbl, M_NWFSHASH);
}

int
nwfs_sysctl_vnprint SYSCTL_HANDLER_ARGS {
	struct nwnode *np;
	struct nwnode_hash_head *nhpp;
	struct vnode *vp;
	int i;

	if (nwfs_debuglevel == 0)
		return 0;
	printf("Name:uc:hc:fid:pfid\n");
	for(i = 0; i <= nwnodehash; i++) {
		nhpp = &nwhashtbl[i];
		for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
			vp = NWTOV(np);
			printf("%s:%d:%d:%d:%d\n",np->n_name,vp->v_usecount,vp->v_holdcnt,
			    np->n_fid.f_id, np->n_fid.f_parent);
		}
	}
	return 0;
}

/*
 * Allocate new nwfsnode/vnode from given nwnode. 
 * Vnode referenced and not locked.
 */
int
nwfs_allocvp(struct mount *mp, ncpfid fid, struct vnode **vpp) {
	struct proc *p = curproc;	/* XXX */
	struct nwnode *np, *np2;
	struct nwnode_hash_head *nhpp;
	struct vnode *vp;
	int error;

retry:
	nhpp = NWNOHASH(fid);
loop:
	for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
		vp = NWTOV(np);
		if (mp != vp->v_mount || !NWCMPF(&fid, &np->n_fid))
			continue;
		if (vget(vp, LK_EXCLUSIVE, p))
			goto loop;
		*vpp = vp;
		return(0);
	}

	/* lock list, or waiting in malloc can cause problems */
	if (nwhashlock) {
		while(nwhashlock) {
			nwhashlock = -1;
			tsleep((caddr_t) &nwhashlock, PVM, "nwfsvp", 0);
		}
		goto loop;
	}
	nwhashlock = 1;
	/*
	 * Do the MALLOC before the getnewvnode since doing so afterward
	 * might cause a bogus v_data pointer to get dereferenced
	 * elsewhere if MALLOC should block.
	 */
	MALLOC(np, struct nwnode *, sizeof *np, M_NWNODE, M_WAITOK);
	error = getnewvnode(VT_NWFS, mp, nwfs_vnodeop_p, &vp);
	if (error) {
		if (nwhashlock < 0)
			wakeup(&nwhashlock);
		nwhashlock = 0;
		*vpp = 0;
		FREE(np, M_NWNODE);
		return (error);
	}
	*vpp = vp;
	bzero(np,sizeof(*np));
	vp->v_data = np;
	np->n_vnode = vp;
	np->n_mount = VFSTONWFS(mp);
	np->n_fid = fid;
	for (np2 = nhpp->lh_first; np2 != 0; np2 = np->n_hash.le_next) {
		if (mp != NWTOV(np2)->v_mount || !NWCMPF(&fid, &np2->n_fid))
			continue;
		vrele(vp);
		FREE(np, M_NWNODE);
		if (nwhashlock < 0)
			wakeup(&nwhashlock);
		nwhashlock = 0;
		goto retry;
	}
	LIST_INSERT_HEAD(nhpp, np, n_hash);
	if (nwhashlock < 0)
		wakeup(&nwhashlock);
	nwhashlock = 0;
	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
	np->n_flag |= NNEW;
	return (error);
}

int
nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct nwnode **npp) {
	struct nwnode *np;
	struct nwnode_hash_head *nhpp;

	nhpp = NWNOHASH(fid);
	for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
		if (nmp != np->n_mount || !NWCMPF(&fid, &np->n_fid))
			continue;
		*npp = np;
		return(0);
	}
	return ENOENT;
}

/*
 * Free nwnode, and give vnode back to system
 */
int
nwfs_reclaim(ap)                     
        struct vop_reclaim_args /* {
        struct vnode *a_vp;
        } */ *ap;
{
	struct vnode *dvp = NULL, *vp = ap->a_vp;
	struct nwnode *dnp, *np = VTONW(vp);
	struct nwmount *nmp=VTONWFS(vp);
	
	NCPVNDEBUG("%s,%d\n", np->n_name, vp->v_usecount);
	if (np->n_refparent) {
		np->n_refparent = 0;
		if (nwfs_lookupnp(nmp, np->n_parent, &dnp) == 0) {
			dvp = dnp->n_vnode;
		} else {
			NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
		}
	}
	LIST_REMOVE(np, n_hash);
	cache_purge(vp);
	if (nmp->n_root == np) {
		nmp->n_root = NULL;
	}
	vp->v_data = NULL;
	FREE(np, M_NWNODE);
	if (dvp) {
		vrele(dvp);
	}
	return (0);
}

int
nwfs_inactive(ap)
	struct vop_inactive_args /* {
		struct vnode *a_vp;
		struct proc *a_p;
	} */ *ap;
{
	struct proc *p = ap->a_p;
	struct ucred *cred = p->p_ucred;
	struct vnode *vp = ap->a_vp;
	struct nwnode *np = VTONW(vp);
	int error;

	NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vp->v_usecount);
	if (np->opened) {
		error = nwfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
		error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, p, cred);
		np->opened = 0;
	}
	VOP_UNLOCK(vp, 0, p);
	return (0);
}
/*
 * routines to maintain vnode attributes cache
 * nwfs_attr_cacheenter: unpack np.i to va structure
 */
void
nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi) {
	struct nwnode *np = VTONW(vp);
	struct nwmount *nmp = VTONWFS(vp);
	register struct vattr *va = &np->n_vattr;

	va->va_type = vp->v_type;		/* vnode type (for create) */
	if (vp->v_type == VREG) {
		if (va->va_size != fi->dataStreamSize) {
			va->va_size = fi->dataStreamSize;
			vnode_pager_setsize(vp, va->va_size);
		}
		va->va_mode = nmp->m.file_mode;	/* files access mode and type */
	} else if (vp->v_type == VDIR) {
		va->va_size = 16384; 		/* should be a better way ... */
		va->va_mode = nmp->m.dir_mode;	/* files access mode and type */
	} else
		return;
	np->n_size = va->va_size;
	va->va_nlink = 1;		/* number of references to file */
	va->va_uid = nmp->m.uid;	/* owner user id */
	va->va_gid = nmp->m.gid;	/* owner group id */
	va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
	va->va_fileid = np->n_fid.f_id;	/* file id */
	if (va->va_fileid == 0)
		va->va_fileid = NWFS_ROOT_INO;
	va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
	/* time of last modification */
	ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
	/* time of last access */
	ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
	va->va_ctime = va->va_mtime;	/* time file changed */
	va->va_gen = VNOVAL;		/* generation number of file */
	va->va_flags = 0;		/* flags defined for file */
	va->va_rdev = VNOVAL;		/* device the special file represents */
	va->va_bytes = va->va_size;	/* bytes of disk space held by file */
	va->va_filerev = 0;		/* file modification number */
	va->va_vaflags = 0;		/* operations flags */
	np->n_vattr = *va;
	if (np->n_mtime == 0) {
		np->n_mtime = va->va_mtime.tv_sec;
	}
	np->n_atime = time_second;
	return;
}

int
nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va) {
	struct nwnode *np = VTONW(vp);
	int diff;

	diff = time_second - np->n_atime;
	if (diff > 2) {	/* XXX should be configurable */
		return ENOENT;
	}
	*va = np->n_vattr;
	return 0;
}
OpenPOWER on IntegriCloud