1139776Simp/*-
266539Sbp * Copyright (c) 1999, 2000 Boris Popov
351852Sbp * All rights reserved.
451852Sbp *
551852Sbp * Redistribution and use in source and binary forms, with or without
651852Sbp * modification, are permitted provided that the following conditions
751852Sbp * are met:
851852Sbp * 1. Redistributions of source code must retain the above copyright
951852Sbp *    notice, this list of conditions and the following disclaimer.
1051852Sbp * 2. Redistributions in binary form must reproduce the above copyright
1151852Sbp *    notice, this list of conditions and the following disclaimer in the
1251852Sbp *    documentation and/or other materials provided with the distribution.
1351852Sbp *
1451852Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1551852Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1651852Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1751852Sbp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1851852Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1951852Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2051852Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2151852Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2251852Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2351852Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2451852Sbp * SUCH DAMAGE.
2551852Sbp *
2651852Sbp * $FreeBSD$
2751852Sbp */
2851852Sbp
2951852Sbp#include <sys/param.h>
3051852Sbp#include <sys/systm.h>
3151852Sbp#include <sys/proc.h>
3251852Sbp#include <sys/kernel.h>
3351852Sbp#include <sys/sysctl.h>
3451852Sbp#include <sys/vnode.h>
3551852Sbp#include <sys/mount.h>
3651852Sbp#include <sys/stat.h>
3751852Sbp#include <sys/malloc.h>
3860041Sphk#include <sys/bio.h>
3951852Sbp#include <sys/buf.h>
4051852Sbp
4151852Sbp#include <netncp/ncp.h>
4251852Sbp#include <netncp/ncp_conn.h>
4351852Sbp#include <netncp/ncp_subr.h>
4451852Sbp#include <netncp/ncp_ncp.h>
4551852Sbp#include <netncp/ncp_nls.h>
4651852Sbp
4777223Sru#include <fs/nwfs/nwfs.h>
4877223Sru#include <fs/nwfs/nwfs_node.h>
4977223Sru#include <fs/nwfs/nwfs_subr.h>
5051852Sbp
5151852Sbpint nwfs_debuglevel = 0;
5251852Sbp
5351852Sbpstatic int nwfs_version = NWFS_VERSION;
5451852Sbp
5551852SbpSYSCTL_DECL(_vfs_nwfs);
5696755StrhodesSYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware filesystem");
5751852SbpSYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
5851852SbpSYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
5951852Sbp
6059755SpeterMODULE_DEPEND(nwfs, ncp, 1, 1, 1);
6174637SbpMODULE_DEPEND(nwfs, libmchain, 1, 1, 1);
6259755Speter
63138486Sphkstatic vfs_cmount_t	nwfs_cmount;
64138486Sphkstatic vfs_mount_t	nwfs_mount;
65116271Sphkstatic vfs_quotactl_t	nwfs_quotactl;
66116271Sphkstatic vfs_root_t	nwfs_root;
67116271Sphkstatic vfs_statfs_t	nwfs_statfs;
68116271Sphkstatic vfs_unmount_t	nwfs_unmount;
69116271Sphkstatic vfs_init_t	nwfs_init;
70116271Sphkstatic vfs_uninit_t	nwfs_uninit;
7151852Sbp
7251852Sbpstatic struct vfsops nwfs_vfsops = {
73116271Sphk	.vfs_init =		nwfs_init,
74138486Sphk	.vfs_mount =		nwfs_mount,
75138486Sphk	.vfs_cmount =		nwfs_cmount,
76116271Sphk	.vfs_quotactl =		nwfs_quotactl,
77116271Sphk	.vfs_root =		nwfs_root,
78116271Sphk	.vfs_statfs =		nwfs_statfs,
79116271Sphk	.vfs_sync =		vfs_stdsync,
80116271Sphk	.vfs_uninit =		nwfs_uninit,
81116271Sphk	.vfs_unmount =		nwfs_unmount,
8251852Sbp};
8351852Sbp
8451852Sbp
8551852SbpVFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
8651852Sbp
8751852Sbpint nwfs_pbuf_freecnt = -1;	/* start out unlimited */
8851852Sbpstatic int nwfsid = 1;
8951852Sbp
9051852Sbpstatic int
9151852Sbpnwfs_initnls(struct nwmount *nmp) {
9251852Sbp	char	*pc, *pe;
9351852Sbp	int	error = 0;
9451852Sbp#define COPY_TABLE(t,d)	{ \
9551852Sbp		if (t) { \
9651852Sbp			error = copyin((t), pc, 256); \
9751852Sbp			if (error) break; \
9851852Sbp		} else \
9951852Sbp			bcopy(d, pc, 256); \
10051852Sbp		(t) = pc; pc += 256; \
10151852Sbp	}
10251852Sbp
10351852Sbp	nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
10451852Sbp	if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
10552814Sarchie		nmp->m.nls.to_lower = ncp_defnls.to_lower;
10652814Sarchie		nmp->m.nls.to_upper = ncp_defnls.to_upper;
10751852Sbp		nmp->m.nls.n2u = ncp_defnls.n2u;
10851852Sbp		nmp->m.nls.u2n = ncp_defnls.u2n;
10951852Sbp		return 0;
11051852Sbp	}
111184205Sdes	pe = malloc(256 * 4, M_NWFSDATA, M_WAITOK);
11251852Sbp	pc = pe;
11351852Sbp	do {
11452814Sarchie		COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
11552814Sarchie		COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
11651852Sbp		COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
11751852Sbp		COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
11851852Sbp	} while(0);
11951852Sbp	if (error) {
12051852Sbp		free(pe, M_NWFSDATA);
12151852Sbp		return error;
12251852Sbp	}
12351852Sbp	return 0;
12451852Sbp}
125138486Sphk
126230725Smckusickstatic int nwfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
127138486Sphk{
128138486Sphk	struct nwfs_args args; 	  /* will hold data from mount request */
129138486Sphk	int error;
130138486Sphk
131153400Sdes	error = copyin(data, &args, sizeof(struct nwfs_args));
132138486Sphk	if (error)
133138486Sphk		return (error);
134138486Sphk
135138486Sphk	/*
136138486Sphk	 * XXX: cheap cop-out here, args contains a structure I don't
137138486Sphk	 * XXX: know how we should handle, and I don't see any immediate
138138486Sphk	 * XXX: prospect of avoiding a mount_nwfs(8) binary anyway.
139138486Sphk	 */
140138486Sphk	ma = mount_arg(ma, "nwfs_args", &args, sizeof args);
141138486Sphk
142138486Sphk	error = kernel_mount(ma, flags);
143138486Sphk
144138486Sphk	return (error);
145138486Sphk}
146138486Sphk
14751852Sbp/*
14851852Sbp * mp - path - addr in user space of mount point (ie /usr or whatever)
14951852Sbp * data - addr in user space of mount params
15051852Sbp */
151191990Sattiliostatic int nwfs_mount(struct mount *mp)
15251852Sbp{
15351852Sbp	struct nwfs_args args; 	  /* will hold data from mount request */
15451852Sbp	int error;
15551852Sbp	struct nwmount *nmp = NULL;
15651852Sbp	struct ncp_conn *conn = NULL;
15751852Sbp	struct ncp_handle *handle = NULL;
15851852Sbp	struct vnode *vp;
159191990Sattilio	struct thread *td;
16051852Sbp	char *pc,*pe;
16151852Sbp
162191990Sattilio	td = curthread;
163137479Sphk	if (mp->mnt_flag & MNT_ROOTFS)
164137479Sphk		return (EOPNOTSUPP);
16551852Sbp	if (mp->mnt_flag & MNT_UPDATE) {
16651852Sbp		nwfs_printf("MNT_UPDATE not implemented");
16751852Sbp		return (EOPNOTSUPP);
16851852Sbp	}
169150761Sphk	error = vfs_copyopt(mp->mnt_optnew, "nwfs_args", &args, sizeof args);
17051852Sbp	if (error)
17151852Sbp		return (error);
17251852Sbp	if (args.version != NWFS_VERSION) {
17351852Sbp		nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
17451852Sbp		return (1);
17551852Sbp	}
17691406Sjhb	error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn);
17751852Sbp	if (error) {
17851852Sbp		nwfs_printf("invalid connection refernce %d\n",args.connRef);
17951852Sbp		return (error);
18051852Sbp	}
18151852Sbp	error = ncp_conn_gethandle(conn, NULL, &handle);
18251852Sbp	if (error) {
18351852Sbp		nwfs_printf("can't get connection handle\n");
18451852Sbp		return (error);
18551852Sbp	}
18683366Sjulian	ncp_conn_unlock(conn, td);	/* we keep the ref */
18751852Sbp	mp->mnt_stat.f_iosize = conn->buffer_size;
18851852Sbp        /* We must malloc our own mount info */
189213363Salc        nmp = malloc(sizeof(struct nwmount), M_NWFSDATA, M_WAITOK | M_ZERO);
19051852Sbp        if (nmp == NULL) {
19151852Sbp                nwfs_printf("could not alloc nwmount\n");
19251852Sbp                error = ENOMEM;
19351852Sbp		goto bad;
19451852Sbp        }
195172697Salfred        mp->mnt_data = nmp;
19651852Sbp	nmp->connh = handle;
19751852Sbp	nmp->n_root = NULL;
19851852Sbp	nmp->n_id = nwfsid++;
19951852Sbp        nmp->m = args;
20051852Sbp	nmp->m.file_mode = (nmp->m.file_mode &
20151852Sbp			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
20251852Sbp	nmp->m.dir_mode  = (nmp->m.dir_mode &
20351852Sbp			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
20451852Sbp	if ((error = nwfs_initnls(nmp)) != 0) goto bad;
20551852Sbp	pc = mp->mnt_stat.f_mntfromname;
20651852Sbp	pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
20751852Sbp	bzero(pc, MNAMELEN);
20851852Sbp	*(pc++) = '/';
20951852Sbp	pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
21051852Sbp	if (pc < pe-1) {
21151852Sbp		*(pc++) = ':';
21251852Sbp		pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
21351852Sbp		if (pc < pe-1) {
21451852Sbp			*(pc++) = '/';
21551852Sbp			strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
21651852Sbp		}
21751852Sbp	}
21851852Sbp	/* protect against invalid mount points */
21951852Sbp	nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
22051852Sbp	vfs_getnewfsid(mp);
221191990Sattilio	error = nwfs_root(mp, LK_EXCLUSIVE, &vp);
22251852Sbp	if (error)
22351852Sbp		goto bad;
22451852Sbp	/*
22551852Sbp	 * Lose the lock but keep the ref.
22651852Sbp	 */
227175294Sattilio	VOP_UNLOCK(vp, 0);
228103936Sjeff	NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp));
22951852Sbp	return error;
23051852Sbpbad:
23151852Sbp        if (nmp)
23251852Sbp		free(nmp, M_NWFSDATA);
23351852Sbp	if (handle)
23451852Sbp		ncp_conn_puthandle(handle, NULL, 0);
23551852Sbp        return error;
23651852Sbp}
23751852Sbp
23851852Sbp/* Unmount the filesystem described by mp. */
23951852Sbpstatic int
240191990Sattilionwfs_unmount(struct mount *mp, int mntflags)
24151852Sbp{
242191990Sattilio	struct thread *td;
24351852Sbp	struct nwmount *nmp = VFSTONWFS(mp);
24451852Sbp	struct ncp_conn *conn;
24551852Sbp	int error, flags;
24651852Sbp
24751852Sbp	NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
248191990Sattilio	td = curthread;
24951852Sbp	flags = 0;
25051852Sbp	if (mntflags & MNT_FORCE)
25151852Sbp		flags |= FORCECLOSE;
25276688Siedowse	/* There is 1 extra root vnode reference from nwfs_mount(). */
253132023Salfred	error = vflush(mp, 1, flags, td);
25476688Siedowse	if (error)
25551852Sbp		return (error);
25651852Sbp	conn = NWFSTOCONN(nmp);
25751852Sbp	ncp_conn_puthandle(nmp->connh,NULL,0);
25891406Sjhb	if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
25974062Sbp		if(ncp_conn_free(conn))
26083366Sjulian			ncp_conn_unlock(conn, td);
26151852Sbp	}
262172697Salfred	mp->mnt_data = NULL;
26351852Sbp	if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
26452814Sarchie		free(nmp->m.nls.to_lower, M_NWFSDATA);
26551852Sbp	free(nmp, M_NWFSDATA);
266162647Stegge	MNT_ILOCK(mp);
26751852Sbp	mp->mnt_flag &= ~MNT_LOCAL;
268162647Stegge	MNT_IUNLOCK(mp);
26951852Sbp	return (error);
27051852Sbp}
27151852Sbp
27251852Sbp/*  Return locked vnode to root of a filesystem */
27351852Sbpstatic int
274191990Sattilionwfs_root(struct mount *mp, int flags, struct vnode **vpp) {
27551852Sbp	struct vnode *vp;
27651852Sbp	struct nwmount *nmp;
27751852Sbp	struct nwnode *np;
27851852Sbp	struct ncp_conn *conn;
27951852Sbp	struct nw_entry_info fattr;
280191990Sattilio	struct thread *td;
281191990Sattilio	struct ucred *cred;
28251852Sbp	int error, nsf, opt;
28351852Sbp	u_char vol;
28451852Sbp
285191990Sattilio	td = curthread;
286191990Sattilio	cred = td->td_ucred;
287191990Sattilio
28851852Sbp	nmp = VFSTONWFS(mp);
28951852Sbp	conn = NWFSTOCONN(nmp);
29051852Sbp	if (nmp->n_root) {
29151852Sbp		*vpp = NWTOV(nmp->n_root);
29283366Sjulian		while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
29366540Sbp			;
29451852Sbp		return 0;
29551852Sbp	}
29651852Sbp	error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
29783366Sjulian		&nmp->n_rootent.f_id, td, cred);
29851852Sbp	if (error)
29951852Sbp		return ENOENT;
30051852Sbp	nmp->n_volume = vol;
30183366Sjulian	error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
30251852Sbp	if (error)
30351852Sbp		return ENOENT;
30451852Sbp	if (nsf & NW_NSB_OS2) {
30551852Sbp		NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
30651852Sbp		if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
30751852Sbp			nmp->name_space = NW_NS_OS2;
30851852Sbp			nmp->m.nls.opt &= ~NWHP_DOS;
30951852Sbp		}
31051852Sbp	}
31151852Sbp	opt = nmp->m.nls.opt;
31251852Sbp	nsf = opt & (NWHP_UPPER | NWHP_LOWER);
31351852Sbp	if (opt & NWHP_DOS) {
31451852Sbp		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
31551852Sbp			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
31651852Sbp		} else if (nsf == 0) {
31751852Sbp			nmp->m.nls.opt |= NWHP_LOWER;
31851852Sbp		}
31951852Sbp	} else {
32051852Sbp		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
32151852Sbp			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
32251852Sbp		}
32351852Sbp	}
32451852Sbp	if (nmp->m.root_path[0]) {
32551852Sbp		nmp->m.root_path[0]--;
32651852Sbp		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
32783366Sjulian		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
32851852Sbp		if (error) {
32951852Sbp			NCPFATAL("Invalid root path specified\n");
33051852Sbp			return ENOENT;
33151852Sbp		}
33251852Sbp		nmp->n_rootent.f_parent = fattr.dirEntNum;
33351852Sbp		nmp->m.root_path[0]++;
33451852Sbp		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
33583366Sjulian		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
33651852Sbp		if (error) {
33751852Sbp			NCPFATAL("Invalid root path specified\n");
33851852Sbp			return ENOENT;
33951852Sbp		}
34051852Sbp		nmp->n_rootent.f_id = fattr.dirEntNum;
34151852Sbp	} else {
34251852Sbp		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
34383366Sjulian		    0, NULL, &fattr, td, cred);
34451852Sbp		if (error) {
34551852Sbp			NCPFATAL("Can't obtain volume info\n");
34651852Sbp			return ENOENT;
34751852Sbp		}
34874064Sbp		fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
34951852Sbp		nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
35051852Sbp	}
35151852Sbp	error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
35251852Sbp	if (error)
35351852Sbp		return (error);
354101308Sjeff	vp->v_vflag |= VV_ROOT;
35551852Sbp	np = VTONW(vp);
35651852Sbp	if (nmp->m.root_path[0] == 0)
35751852Sbp		np->n_flag |= NVOLUME;
35851852Sbp	nmp->n_root = np;
35983366Sjulian/*	error = VOP_GETATTR(vp, &vattr, cred, td);
36051852Sbp	if (error) {
36151852Sbp		vput(vp);
36251852Sbp		NCPFATAL("Can't get root directory entry\n");
36351852Sbp		return error;
36451852Sbp	}*/
36551852Sbp	*vpp = vp;
36651852Sbp	return (0);
36751852Sbp}
36851852Sbp
36951852Sbp/*
37051852Sbp * Do operations associated with quotas, not supported
37151852Sbp */
37251852Sbp/* ARGSUSED */
37351852Sbpstatic int
374191990Sattilionwfs_quotactl(mp, cmd, uid, arg)
37551852Sbp	struct mount *mp;
37651852Sbp	int cmd;
37751852Sbp	uid_t uid;
378153400Sdes	void *arg;
37951852Sbp{
38051852Sbp	NCPVODEBUG("return EOPNOTSUPP\n");
38151852Sbp	return (EOPNOTSUPP);
38251852Sbp}
38351852Sbp
38451852Sbp/*ARGSUSED*/
38551852Sbpint
38651852Sbpnwfs_init(struct vfsconf *vfsp)
38751852Sbp{
38851852Sbp	nwfs_hash_init();
38951852Sbp	nwfs_pbuf_freecnt = nswbuf / 2 + 1;
39051852Sbp	NCPVODEBUG("always happy to load!\n");
39151852Sbp	return (0);
39251852Sbp}
39351852Sbp
39451852Sbp/*ARGSUSED*/
39551852Sbpint
39651852Sbpnwfs_uninit(struct vfsconf *vfsp)
39751852Sbp{
39851852Sbp
39951852Sbp	nwfs_hash_free();
40051852Sbp	NCPVODEBUG("unloaded\n");
40151852Sbp	return (0);
40251852Sbp}
40351852Sbp
40451852Sbp/*
40551852Sbp * nwfs_statfs call
40651852Sbp */
40751852Sbpint
408191990Sattilionwfs_statfs(mp, sbp)
40951852Sbp	struct mount *mp;
41066539Sbp	struct statfs *sbp;
41151852Sbp{
41251852Sbp	struct nwmount *nmp = VFSTONWFS(mp);
413191990Sattilio	struct thread *td = curthread;
41451852Sbp	int error = 0, secsize;
41551852Sbp	struct nwnode *np = nmp->n_root;
41651852Sbp	struct ncp_volume_info vi;
41751852Sbp
41851852Sbp	if (np == NULL) return EINVAL;
41983366Sjulian	error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
42091406Sjhb	    nmp->n_volume, &vi, td, td->td_ucred);
42151852Sbp	if (error) return error;
42251852Sbp	secsize = 512;			/* XXX how to get real value ??? */
42396755Strhodes	/* fundamental filesystem block size */
42451852Sbp	sbp->f_bsize = vi.sectors_per_block*secsize;
42551852Sbp	/* optimal transfer block size */
42651852Sbp	sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
42796755Strhodes	/* total data blocks in filesystem */
42851852Sbp	sbp->f_blocks= vi.total_blocks;
42951852Sbp	/* free blocks in fs */
43051852Sbp	sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
43151852Sbp	/* free blocks avail to non-superuser */
43251852Sbp	sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
43396755Strhodes	/* total file nodes in filesystem */
43451852Sbp	sbp->f_files = vi.total_dir_entries;
43551852Sbp	/* free file nodes in fs */
43651852Sbp	sbp->f_ffree = vi.available_dir_entries;
43751852Sbp	sbp->f_flags = 0;		/* copy of mount exported flags */
43851852Sbp	return 0;
43951852Sbp}
440