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