1/*-
2 * Copyright (c) 1999, 2000 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 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/proc.h>
32#include <sys/kernel.h>
33#include <sys/sysctl.h>
34#include <sys/vnode.h>
35#include <sys/mount.h>
36#include <sys/stat.h>
37#include <sys/malloc.h>
38#include <sys/bio.h>
39#include <sys/buf.h>
40
41#include <netncp/ncp.h>
42#include <netncp/ncp_conn.h>
43#include <netncp/ncp_subr.h>
44#include <netncp/ncp_ncp.h>
45#include <netncp/ncp_nls.h>
46
47#include <fs/nwfs/nwfs.h>
48#include <fs/nwfs/nwfs_node.h>
49#include <fs/nwfs/nwfs_subr.h>
50
51int nwfs_debuglevel = 0;
52
53static int nwfs_version = NWFS_VERSION;
54
55SYSCTL_DECL(_vfs_nwfs);
56SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware filesystem");
57SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
58SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
59
60MODULE_DEPEND(nwfs, ncp, 1, 1, 1);
61MODULE_DEPEND(nwfs, libmchain, 1, 1, 1);
62
63static vfs_cmount_t	nwfs_cmount;
64static vfs_mount_t	nwfs_mount;
65static vfs_quotactl_t	nwfs_quotactl;
66static vfs_root_t	nwfs_root;
67static vfs_statfs_t	nwfs_statfs;
68static vfs_unmount_t	nwfs_unmount;
69static vfs_init_t	nwfs_init;
70static vfs_uninit_t	nwfs_uninit;
71
72static struct vfsops nwfs_vfsops = {
73	.vfs_init =		nwfs_init,
74	.vfs_mount =		nwfs_mount,
75	.vfs_cmount =		nwfs_cmount,
76	.vfs_quotactl =		nwfs_quotactl,
77	.vfs_root =		nwfs_root,
78	.vfs_statfs =		nwfs_statfs,
79	.vfs_sync =		vfs_stdsync,
80	.vfs_uninit =		nwfs_uninit,
81	.vfs_unmount =		nwfs_unmount,
82};
83
84
85VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
86
87int nwfs_pbuf_freecnt = -1;	/* start out unlimited */
88static int nwfsid = 1;
89
90static int
91nwfs_initnls(struct nwmount *nmp) {
92	char	*pc, *pe;
93	int	error = 0;
94#define COPY_TABLE(t,d)	{ \
95		if (t) { \
96			error = copyin((t), pc, 256); \
97			if (error) break; \
98		} else \
99			bcopy(d, pc, 256); \
100		(t) = pc; pc += 256; \
101	}
102
103	nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
104	if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
105		nmp->m.nls.to_lower = ncp_defnls.to_lower;
106		nmp->m.nls.to_upper = ncp_defnls.to_upper;
107		nmp->m.nls.n2u = ncp_defnls.n2u;
108		nmp->m.nls.u2n = ncp_defnls.u2n;
109		return 0;
110	}
111	pe = malloc(256 * 4, M_NWFSDATA, M_WAITOK);
112	pc = pe;
113	do {
114		COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
115		COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
116		COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
117		COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
118	} while(0);
119	if (error) {
120		free(pe, M_NWFSDATA);
121		return error;
122	}
123	return 0;
124}
125
126static int nwfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
127{
128	struct nwfs_args args; 	  /* will hold data from mount request */
129	int error;
130
131	error = copyin(data, &args, sizeof(struct nwfs_args));
132	if (error)
133		return (error);
134
135	/*
136	 * XXX: cheap cop-out here, args contains a structure I don't
137	 * XXX: know how we should handle, and I don't see any immediate
138	 * XXX: prospect of avoiding a mount_nwfs(8) binary anyway.
139	 */
140	ma = mount_arg(ma, "nwfs_args", &args, sizeof args);
141
142	error = kernel_mount(ma, flags);
143
144	return (error);
145}
146
147/*
148 * mp - path - addr in user space of mount point (ie /usr or whatever)
149 * data - addr in user space of mount params
150 */
151static int nwfs_mount(struct mount *mp)
152{
153	struct nwfs_args args; 	  /* will hold data from mount request */
154	int error;
155	struct nwmount *nmp = NULL;
156	struct ncp_conn *conn = NULL;
157	struct ncp_handle *handle = NULL;
158	struct vnode *vp;
159	struct thread *td;
160	char *pc,*pe;
161
162	td = curthread;
163	if (mp->mnt_flag & MNT_ROOTFS)
164		return (EOPNOTSUPP);
165	if (mp->mnt_flag & MNT_UPDATE) {
166		nwfs_printf("MNT_UPDATE not implemented");
167		return (EOPNOTSUPP);
168	}
169	error = vfs_copyopt(mp->mnt_optnew, "nwfs_args", &args, sizeof args);
170	if (error)
171		return (error);
172	if (args.version != NWFS_VERSION) {
173		nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
174		return (1);
175	}
176	error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn);
177	if (error) {
178		nwfs_printf("invalid connection refernce %d\n",args.connRef);
179		return (error);
180	}
181	error = ncp_conn_gethandle(conn, NULL, &handle);
182	if (error) {
183		nwfs_printf("can't get connection handle\n");
184		return (error);
185	}
186	ncp_conn_unlock(conn, td);	/* we keep the ref */
187	mp->mnt_stat.f_iosize = conn->buffer_size;
188        /* We must malloc our own mount info */
189        nmp = malloc(sizeof(struct nwmount), M_NWFSDATA, M_WAITOK | M_ZERO);
190        if (nmp == NULL) {
191                nwfs_printf("could not alloc nwmount\n");
192                error = ENOMEM;
193		goto bad;
194        }
195        mp->mnt_data = nmp;
196	nmp->connh = handle;
197	nmp->n_root = NULL;
198	nmp->n_id = nwfsid++;
199        nmp->m = args;
200	nmp->m.file_mode = (nmp->m.file_mode &
201			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
202	nmp->m.dir_mode  = (nmp->m.dir_mode &
203			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
204	if ((error = nwfs_initnls(nmp)) != 0) goto bad;
205	pc = mp->mnt_stat.f_mntfromname;
206	pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
207	bzero(pc, MNAMELEN);
208	*(pc++) = '/';
209	pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
210	if (pc < pe-1) {
211		*(pc++) = ':';
212		pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
213		if (pc < pe-1) {
214			*(pc++) = '/';
215			strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
216		}
217	}
218	/* protect against invalid mount points */
219	nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
220	vfs_getnewfsid(mp);
221	error = nwfs_root(mp, LK_EXCLUSIVE, &vp);
222	if (error)
223		goto bad;
224	/*
225	 * Lose the lock but keep the ref.
226	 */
227	VOP_UNLOCK(vp, 0);
228	NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp));
229	return error;
230bad:
231        if (nmp)
232		free(nmp, M_NWFSDATA);
233	if (handle)
234		ncp_conn_puthandle(handle, NULL, 0);
235        return error;
236}
237
238/* Unmount the filesystem described by mp. */
239static int
240nwfs_unmount(struct mount *mp, int mntflags)
241{
242	struct thread *td;
243	struct nwmount *nmp = VFSTONWFS(mp);
244	struct ncp_conn *conn;
245	int error, flags;
246
247	NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
248	td = curthread;
249	flags = 0;
250	if (mntflags & MNT_FORCE)
251		flags |= FORCECLOSE;
252	/* There is 1 extra root vnode reference from nwfs_mount(). */
253	error = vflush(mp, 1, flags, td);
254	if (error)
255		return (error);
256	conn = NWFSTOCONN(nmp);
257	ncp_conn_puthandle(nmp->connh,NULL,0);
258	if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
259		if(ncp_conn_free(conn))
260			ncp_conn_unlock(conn, td);
261	}
262	mp->mnt_data = NULL;
263	if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
264		free(nmp->m.nls.to_lower, M_NWFSDATA);
265	free(nmp, M_NWFSDATA);
266	MNT_ILOCK(mp);
267	mp->mnt_flag &= ~MNT_LOCAL;
268	MNT_IUNLOCK(mp);
269	return (error);
270}
271
272/*  Return locked vnode to root of a filesystem */
273static int
274nwfs_root(struct mount *mp, int flags, struct vnode **vpp) {
275	struct vnode *vp;
276	struct nwmount *nmp;
277	struct nwnode *np;
278	struct ncp_conn *conn;
279	struct nw_entry_info fattr;
280	struct thread *td;
281	struct ucred *cred;
282	int error, nsf, opt;
283	u_char vol;
284
285	td = curthread;
286	cred = td->td_ucred;
287
288	nmp = VFSTONWFS(mp);
289	conn = NWFSTOCONN(nmp);
290	if (nmp->n_root) {
291		*vpp = NWTOV(nmp->n_root);
292		while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
293			;
294		return 0;
295	}
296	error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
297		&nmp->n_rootent.f_id, td, cred);
298	if (error)
299		return ENOENT;
300	nmp->n_volume = vol;
301	error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
302	if (error)
303		return ENOENT;
304	if (nsf & NW_NSB_OS2) {
305		NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
306		if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
307			nmp->name_space = NW_NS_OS2;
308			nmp->m.nls.opt &= ~NWHP_DOS;
309		}
310	}
311	opt = nmp->m.nls.opt;
312	nsf = opt & (NWHP_UPPER | NWHP_LOWER);
313	if (opt & NWHP_DOS) {
314		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
315			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
316		} else if (nsf == 0) {
317			nmp->m.nls.opt |= NWHP_LOWER;
318		}
319	} else {
320		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
321			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
322		}
323	}
324	if (nmp->m.root_path[0]) {
325		nmp->m.root_path[0]--;
326		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
327		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
328		if (error) {
329			NCPFATAL("Invalid root path specified\n");
330			return ENOENT;
331		}
332		nmp->n_rootent.f_parent = fattr.dirEntNum;
333		nmp->m.root_path[0]++;
334		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
335		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
336		if (error) {
337			NCPFATAL("Invalid root path specified\n");
338			return ENOENT;
339		}
340		nmp->n_rootent.f_id = fattr.dirEntNum;
341	} else {
342		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
343		    0, NULL, &fattr, td, cred);
344		if (error) {
345			NCPFATAL("Can't obtain volume info\n");
346			return ENOENT;
347		}
348		fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
349		nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
350	}
351	error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
352	if (error)
353		return (error);
354	vp->v_vflag |= VV_ROOT;
355	np = VTONW(vp);
356	if (nmp->m.root_path[0] == 0)
357		np->n_flag |= NVOLUME;
358	nmp->n_root = np;
359/*	error = VOP_GETATTR(vp, &vattr, cred, td);
360	if (error) {
361		vput(vp);
362		NCPFATAL("Can't get root directory entry\n");
363		return error;
364	}*/
365	*vpp = vp;
366	return (0);
367}
368
369/*
370 * Do operations associated with quotas, not supported
371 */
372/* ARGSUSED */
373static int
374nwfs_quotactl(mp, cmd, uid, arg)
375	struct mount *mp;
376	int cmd;
377	uid_t uid;
378	void *arg;
379{
380	NCPVODEBUG("return EOPNOTSUPP\n");
381	return (EOPNOTSUPP);
382}
383
384/*ARGSUSED*/
385int
386nwfs_init(struct vfsconf *vfsp)
387{
388	nwfs_hash_init();
389	nwfs_pbuf_freecnt = nswbuf / 2 + 1;
390	NCPVODEBUG("always happy to load!\n");
391	return (0);
392}
393
394/*ARGSUSED*/
395int
396nwfs_uninit(struct vfsconf *vfsp)
397{
398
399	nwfs_hash_free();
400	NCPVODEBUG("unloaded\n");
401	return (0);
402}
403
404/*
405 * nwfs_statfs call
406 */
407int
408nwfs_statfs(mp, sbp)
409	struct mount *mp;
410	struct statfs *sbp;
411{
412	struct nwmount *nmp = VFSTONWFS(mp);
413	struct thread *td = curthread;
414	int error = 0, secsize;
415	struct nwnode *np = nmp->n_root;
416	struct ncp_volume_info vi;
417
418	if (np == NULL) return EINVAL;
419	error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
420	    nmp->n_volume, &vi, td, td->td_ucred);
421	if (error) return error;
422	secsize = 512;			/* XXX how to get real value ??? */
423	/* fundamental filesystem block size */
424	sbp->f_bsize = vi.sectors_per_block*secsize;
425	/* optimal transfer block size */
426	sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
427	/* total data blocks in filesystem */
428	sbp->f_blocks= vi.total_blocks;
429	/* free blocks in fs */
430	sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
431	/* free blocks avail to non-superuser */
432	sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
433	/* total file nodes in filesystem */
434	sbp->f_files = vi.total_dir_entries;
435	/* free file nodes in fs */
436	sbp->f_ffree = vi.available_dir_entries;
437	sbp->f_flags = 0;		/* copy of mount exported flags */
438	return 0;
439}
440