nwfs_vfsops.c revision 54444
1/*
2 * Copyright (c) 1999, 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: head/sys/fs/nwfs/nwfs_vfsops.c 54444 1999-12-11 16:13:02Z eivind $
33 */
34#include "opt_ncp.h"
35#ifndef NCP
36#error "NWFS requires NCP protocol"
37#endif
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/proc.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/vnode.h>
45#include <sys/mount.h>
46#include <sys/stat.h>
47#include <sys/malloc.h>
48#include <sys/buf.h>
49
50#include <netncp/ncp.h>
51#include <netncp/ncp_conn.h>
52#include <netncp/ncp_subr.h>
53#include <netncp/ncp_ncp.h>
54#include <netncp/ncp_nls.h>
55
56#include <nwfs/nwfs.h>
57#include <nwfs/nwfs_node.h>
58#include <nwfs/nwfs_subr.h>
59
60int nwfs_debuglevel = 0;
61
62static int nwfs_version = NWFS_VERSION;
63
64SYSCTL_DECL(_vfs_nwfs);
65SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware file system");
66SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
67SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
68
69static int nwfs_mount __P((struct mount *, char *, caddr_t,
70			struct nameidata *, struct proc *));
71static int nwfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
72			struct proc *));
73static int nwfs_root __P((struct mount *, struct vnode **));
74static int nwfs_start __P((struct mount *, int, struct proc *));
75static int nwfs_statfs __P((struct mount *, struct statfs *,
76			struct proc *));
77static int nwfs_sync __P((struct mount *, int, struct ucred *,
78			struct proc *));
79static int nwfs_unmount __P((struct mount *, int, struct proc *));
80static int nwfs_init __P((struct vfsconf *vfsp));
81static int nwfs_uninit __P((struct vfsconf *vfsp));
82
83static struct vfsops nwfs_vfsops = {
84	nwfs_mount,
85	nwfs_start,
86	nwfs_unmount,
87	nwfs_root,
88	nwfs_quotactl,
89	nwfs_statfs,
90	nwfs_sync,
91	vfs_stdvget,
92	vfs_stdfhtovp,		/* shouldn't happen */
93	vfs_stdcheckexp,
94	vfs_stdvptofh,		/* shouldn't happen */
95	nwfs_init,
96	nwfs_uninit
97};
98
99
100VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
101
102int nwfs_pbuf_freecnt = -1;	/* start out unlimited */
103static int nwfsid = 1;
104
105static int
106nwfs_initnls(struct nwmount *nmp) {
107	char	*pc, *pe;
108	int	error = 0;
109#define COPY_TABLE(t,d)	{ \
110		if (t) { \
111			error = copyin((t), pc, 256); \
112			if (error) break; \
113		} else \
114			bcopy(d, pc, 256); \
115		(t) = pc; pc += 256; \
116	}
117
118	nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
119	if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
120		nmp->m.nls.to_lower = ncp_defnls.to_lower;
121		nmp->m.nls.to_upper = ncp_defnls.to_upper;
122		nmp->m.nls.n2u = ncp_defnls.n2u;
123		nmp->m.nls.u2n = ncp_defnls.u2n;
124		return 0;
125	}
126	MALLOC(pe, char *, 256 * 4, M_NWFSDATA, M_WAITOK);
127	if (pe == NULL) return ENOMEM;
128	pc = pe;
129	do {
130		COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
131		COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
132		COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
133		COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
134	} while(0);
135	if (error) {
136		free(pe, M_NWFSDATA);
137		return error;
138	}
139	return 0;
140}
141/*
142 * mp - path - addr in user space of mount point (ie /usr or whatever)
143 * data - addr in user space of mount params
144 */
145static int nwfs_mount(struct mount *mp, char *path, caddr_t data,
146		      struct nameidata *ndp, struct proc *p)
147{
148	struct nwfs_args args; 	  /* will hold data from mount request */
149	size_t size;
150	int error;
151	struct nwmount *nmp = NULL;
152	struct ncp_conn *conn = NULL;
153	struct ncp_handle *handle = NULL;
154	struct vnode *vp;
155	char *pc,*pe;
156
157	if (data == NULL) {
158		nwfs_printf("missing data argument\n");
159		return 1;
160	}
161	if (mp->mnt_flag & MNT_UPDATE) {
162		nwfs_printf("MNT_UPDATE not implemented");
163		return (EOPNOTSUPP);
164	}
165	error = copyin(data, (caddr_t)&args, sizeof(struct nwfs_args));
166	if (error)
167		return (error);
168	if (args.version != NWFS_VERSION) {
169		nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
170		return (1);
171	}
172	error = ncp_conn_getbyref(args.connRef,p,p->p_ucred,NCPM_EXECUTE,&conn);
173	if (error) {
174		nwfs_printf("invalid connection refernce %d\n",args.connRef);
175		return (error);
176	}
177	error = ncp_conn_gethandle(conn, NULL, &handle);
178	if (error) {
179		nwfs_printf("can't get connection handle\n");
180		return (error);
181	}
182	ncp_conn_unlock(conn,p);	/* we keep the ref */
183	mp->mnt_stat.f_iosize = conn->buffer_size;
184        /* We must malloc our own mount info */
185        MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA,M_USE_RESERVE);
186        if (nmp == NULL) {
187                nwfs_printf("could not alloc nwmount\n");
188                error = ENOMEM;
189		goto bad;
190        }
191	bzero(nmp,sizeof(*nmp));
192        mp->mnt_data = (qaddr_t)nmp;
193	nmp->connh = handle;
194	nmp->n_root = NULL;
195	nmp->n_id = nwfsid++;
196        nmp->m = args;
197	nmp->m.file_mode = (nmp->m.file_mode &
198			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
199	nmp->m.dir_mode  = (nmp->m.dir_mode &
200			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
201	if ((error = nwfs_initnls(nmp)) != 0) goto bad;
202	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
203	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
204	pc = mp->mnt_stat.f_mntfromname;
205	pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
206	bzero(pc, MNAMELEN);
207	*(pc++) = '/';
208	pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
209	if (pc < pe-1) {
210		*(pc++) = ':';
211		pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
212		if (pc < pe-1) {
213			*(pc++) = '/';
214			strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
215		}
216	}
217	/* protect against invalid mount points */
218	nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
219	vfs_getnewfsid(mp);
220	error = nwfs_root(mp, &vp);
221	if (error)
222		goto bad;
223	/*
224	 * Lose the lock but keep the ref.
225	 */
226	VOP_UNLOCK(vp, 0, curproc);
227	NCPVODEBUG("rootvp.vrefcnt=%d\n",vp->v_usecount);
228	return error;
229bad:
230        if (nmp)
231		free(nmp, M_NWFSDATA);
232	if (handle)
233		ncp_conn_puthandle(handle, NULL, 0);
234        return error;
235}
236
237/* Unmount the filesystem described by mp. */
238static int
239nwfs_unmount(struct mount *mp, int mntflags, struct proc *p)
240{
241	struct nwmount *nmp = VFSTONWFS(mp);
242	struct ncp_conn *conn;
243	struct vnode *vp;
244	int error, flags;
245
246	NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
247	flags = 0;
248	if (mntflags & MNT_FORCE)
249		flags |= FORCECLOSE;
250	error = VFS_ROOT(mp,&vp);
251	if (error) return (error);
252	if (vp->v_usecount > 2) {
253		printf("nwfs_unmount: usecnt=%d\n",vp->v_usecount);
254		vput(vp);
255		return (EBUSY);
256	}
257	error = vflush(mp, vp, flags);
258	if (error) {
259		vput(vp);
260		return (error);
261	}
262	/*
263	 * There are two reference counts and one lock to get rid of here.
264	 */
265	NCPVODEBUG("v_use: %d\n",vp->v_usecount);
266	vput(vp);
267	NCPVODEBUG("v_use after vput: %d\n",vp->v_usecount);
268	vrele(vp);
269	NCPVODEBUG("v_use after vrele: %d\n",vp->v_usecount);
270	vgone(vp);
271	NCPVODEBUG("v_gone finished !!!!\n");
272	conn = NWFSTOCONN(nmp);
273	ncp_conn_puthandle(nmp->connh,NULL,0);
274	if (ncp_conn_lock(conn,p,p->p_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
275		if(ncp_disconnect(conn))
276			ncp_conn_unlock(conn,p);
277	}
278	mp->mnt_data = (qaddr_t)0;
279	if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
280		free(nmp->m.nls.to_lower, M_NWFSDATA);
281	free(nmp, M_NWFSDATA);
282	mp->mnt_flag &= ~MNT_LOCAL;
283	return (error);
284}
285
286/*  Return locked vnode to root of a filesystem */
287static int
288nwfs_root(struct mount *mp, struct vnode **vpp) {
289	struct vnode *vp;
290	struct nwmount *nmp;
291	struct nwnode *np;
292	struct ncp_conn *conn;
293	struct nw_entry_info fattr;
294	struct proc *p = curproc;
295	struct ucred *cred = p->p_ucred;
296	int error, nsf, opt;
297	u_char vol;
298
299	nmp = VFSTONWFS(mp);
300	conn = NWFSTOCONN(nmp);
301	if (nmp->n_root) {
302		*vpp = NWTOV(nmp->n_root);
303		vget(*vpp, LK_EXCLUSIVE, curproc);
304		return 0;
305	}
306	error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
307		&nmp->n_rootent.f_id, p, cred);
308	if (error)
309		return ENOENT;
310	nmp->n_volume = vol;
311	error = ncp_get_namespaces(conn, vol, &nsf, p, cred);
312	if (error)
313		return ENOENT;
314	if (nsf & NW_NSB_OS2) {
315		NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
316		if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
317			nmp->name_space = NW_NS_OS2;
318			nmp->m.nls.opt &= ~NWHP_DOS;
319		}
320	}
321	opt = nmp->m.nls.opt;
322	nsf = opt & (NWHP_UPPER | NWHP_LOWER);
323	if (opt & NWHP_DOS) {
324		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
325			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
326		} else if (nsf == 0) {
327			nmp->m.nls.opt |= NWHP_LOWER;
328		}
329	} else {
330		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
331			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
332		}
333	}
334	if (nmp->m.root_path[0]) {
335		nmp->m.root_path[0]--;
336		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
337		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, p, cred);
338		if (error) {
339			NCPFATAL("Invalid root path specified\n");
340			return ENOENT;
341		}
342		nmp->n_rootent.f_parent = fattr.dirEntNum;
343		nmp->m.root_path[0]++;
344		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
345		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, p, cred);
346		if (error) {
347			NCPFATAL("Invalid root path specified\n");
348			return ENOENT;
349		}
350		nmp->n_rootent.f_id = fattr.dirEntNum;
351	} else {
352		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
353		    0, NULL, &fattr, p, cred);
354		if (error) {
355			NCPFATAL("Can't obtain volume info\n");
356			return ENOENT;
357		}
358		fattr.nameLen = strlen(strcpy(fattr.entryName, NWFS_ROOTVOL));
359		nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
360	}
361	error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
362	if (error)
363		return (error);
364	vp->v_flag |= VROOT;
365	np = VTONW(vp);
366	if (nmp->m.root_path[0] == 0)
367		np->n_flag |= NVOLUME;
368	nmp->n_root = np;
369/*	error = VOP_GETATTR(vp, &vattr, cred, p);
370	if (error) {
371		vput(vp);
372		NCPFATAL("Can't get root directory entry\n");
373		return error;
374	}*/
375	*vpp = vp;
376	return (0);
377}
378
379/*
380 * Vfs start routine, a no-op.
381 */
382/* ARGSUSED */
383static int
384nwfs_start(mp, flags, p)
385	struct mount *mp;
386	int flags;
387	struct proc *p;
388{
389	NCPVODEBUG("flags=%04x\n",flags);
390	return (0);
391}
392
393/*
394 * Do operations associated with quotas, not supported
395 */
396/* ARGSUSED */
397static int
398nwfs_quotactl(mp, cmd, uid, arg, p)
399	struct mount *mp;
400	int cmd;
401	uid_t uid;
402	caddr_t arg;
403	struct proc *p;
404{
405	NCPVODEBUG("return EOPNOTSUPP\n");
406	return (EOPNOTSUPP);
407}
408
409/*ARGSUSED*/
410int
411nwfs_init(struct vfsconf *vfsp)
412{
413
414	nwfs_hash_init();
415	nwfs_pbuf_freecnt = nswbuf / 2 + 1;
416	NCPVODEBUG("always happy to load!\n");
417	return (0);
418}
419
420/*ARGSUSED*/
421int
422nwfs_uninit(struct vfsconf *vfsp)
423{
424
425	nwfs_hash_free();
426	NCPVODEBUG("unloaded\n");
427	return (0);
428}
429
430/*
431 * nwfs_statfs call
432 */
433int
434nwfs_statfs(mp, sbp, p)
435	struct mount *mp;
436	register struct statfs *sbp;
437	struct proc *p;
438{
439	struct nwmount *nmp = VFSTONWFS(mp);
440	int error = 0, secsize;
441	struct nwnode *np = nmp->n_root;
442	struct ncp_volume_info vi;
443
444	if (np == NULL) return EINVAL;
445	error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp), nmp->n_volume, &vi,p,p->p_ucred);
446	if (error) return error;
447	secsize = 512;			/* XXX how to get real value ??? */
448	sbp->f_spare2=0;		/* placeholder */
449	/* fundamental file system block size */
450	sbp->f_bsize = vi.sectors_per_block*secsize;
451	/* optimal transfer block size */
452	sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
453	/* total data blocks in file system */
454	sbp->f_blocks= vi.total_blocks;
455	/* free blocks in fs */
456	sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
457	/* free blocks avail to non-superuser */
458	sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
459	/* total file nodes in file system */
460	sbp->f_files = vi.total_dir_entries;
461	/* free file nodes in fs */
462	sbp->f_ffree = vi.available_dir_entries;
463	sbp->f_flags = 0;		/* copy of mount exported flags */
464	if (sbp != &mp->mnt_stat) {
465		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* file system id */
466		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
467		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
468		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
469		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
470	}
471	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
472	return 0;
473}
474
475/*
476 * Flush out the buffer cache
477 */
478/* ARGSUSED */
479static int
480nwfs_sync(mp, waitfor, cred, p)
481	struct mount *mp;
482	int waitfor;
483	struct ucred *cred;
484	struct proc *p;
485{
486	register struct vnode *vp;
487	int error, allerror = 0;
488	/*
489	 * Force stale buffer cache information to be flushed.
490	 */
491loop:
492	for (vp = mp->mnt_vnodelist.lh_first;
493	     vp != NULL;
494	     vp = vp->v_mntvnodes.le_next) {
495		/*
496		 * If the vnode that we are about to sync is no longer
497		 * associated with this mount point, start over.
498		 */
499		if (vp->v_mount != mp)
500			goto loop;
501		if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
502		    waitfor == MNT_LAZY)
503			continue;
504		if (vget(vp, LK_EXCLUSIVE, p))
505			goto loop;
506		error = VOP_FSYNC(vp, cred, waitfor, p);
507		if (error)
508			allerror = error;
509		vput(vp);
510	}
511	return (allerror);
512}
513