nwfs_vfsops.c revision 123245
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1999, 2000 Boris Popov
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *    This product includes software developed by Boris Popov.
161541Srgrimes * 4. Neither the name of the author nor the names of any co-contributors
171541Srgrimes *    may be used to endorse or promote products derived from this software
181541Srgrimes *    without specific prior written permission.
191541Srgrimes *
201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301541Srgrimes * SUCH DAMAGE.
311541Srgrimes *
321541Srgrimes * $FreeBSD: head/sys/fs/nwfs/nwfs_vfsops.c 123245 2003-12-07 17:37:44Z des $
331541Srgrimes */
341541Srgrimes#include "opt_ncp.h"
351541Srgrimes#ifndef NCP
361541Srgrimes#error "NWFS requires NCP protocol"
371541Srgrimes#endif
381541Srgrimes
391541Srgrimes#include <sys/param.h>
401541Srgrimes#include <sys/systm.h>
411541Srgrimes#include <sys/proc.h>
421541Srgrimes#include <sys/kernel.h>
431541Srgrimes#include <sys/sysctl.h>
441541Srgrimes#include <sys/vnode.h>
451541Srgrimes#include <sys/mount.h>
461541Srgrimes#include <sys/stat.h>
471541Srgrimes#include <sys/malloc.h>
481541Srgrimes#include <sys/bio.h>
491541Srgrimes#include <sys/buf.h>
501541Srgrimes
511541Srgrimes#include <netncp/ncp.h>
521541Srgrimes#include <netncp/ncp_conn.h>
531541Srgrimes#include <netncp/ncp_subr.h>
541541Srgrimes#include <netncp/ncp_ncp.h>
551541Srgrimes#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);
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);
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) {
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 thread *td = curthread;
267	struct ucred *cred =  td->td_ucred;
268	int error, nsf, opt;
269	u_char vol;
270
271	nmp = VFSTONWFS(mp);
272	conn = NWFSTOCONN(nmp);
273	if (nmp->n_root) {
274		*vpp = NWTOV(nmp->n_root);
275		while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
276			;
277		return 0;
278	}
279	error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
280		&nmp->n_rootent.f_id, td, cred);
281	if (error)
282		return ENOENT;
283	nmp->n_volume = vol;
284	error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
285	if (error)
286		return ENOENT;
287	if (nsf & NW_NSB_OS2) {
288		NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
289		if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
290			nmp->name_space = NW_NS_OS2;
291			nmp->m.nls.opt &= ~NWHP_DOS;
292		}
293	}
294	opt = nmp->m.nls.opt;
295	nsf = opt & (NWHP_UPPER | NWHP_LOWER);
296	if (opt & NWHP_DOS) {
297		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
298			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
299		} else if (nsf == 0) {
300			nmp->m.nls.opt |= NWHP_LOWER;
301		}
302	} else {
303		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
304			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
305		}
306	}
307	if (nmp->m.root_path[0]) {
308		nmp->m.root_path[0]--;
309		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
310		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
311		if (error) {
312			NCPFATAL("Invalid root path specified\n");
313			return ENOENT;
314		}
315		nmp->n_rootent.f_parent = fattr.dirEntNum;
316		nmp->m.root_path[0]++;
317		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
318		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
319		if (error) {
320			NCPFATAL("Invalid root path specified\n");
321			return ENOENT;
322		}
323		nmp->n_rootent.f_id = fattr.dirEntNum;
324	} else {
325		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
326		    0, NULL, &fattr, td, cred);
327		if (error) {
328			NCPFATAL("Can't obtain volume info\n");
329			return ENOENT;
330		}
331		fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
332		nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
333	}
334	error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
335	if (error)
336		return (error);
337	vp->v_vflag |= VV_ROOT;
338	np = VTONW(vp);
339	if (nmp->m.root_path[0] == 0)
340		np->n_flag |= NVOLUME;
341	nmp->n_root = np;
342/*	error = VOP_GETATTR(vp, &vattr, cred, td);
343	if (error) {
344		vput(vp);
345		NCPFATAL("Can't get root directory entry\n");
346		return error;
347	}*/
348	*vpp = vp;
349	return (0);
350}
351
352/*
353 * Vfs start routine, a no-op.
354 */
355/* ARGSUSED */
356static int
357nwfs_start(mp, flags, td)
358	struct mount *mp;
359	int flags;
360	struct thread *td;
361{
362	NCPVODEBUG("flags=%04x\n",flags);
363	return (0);
364}
365
366/*
367 * Do operations associated with quotas, not supported
368 */
369/* ARGSUSED */
370static int
371nwfs_quotactl(mp, cmd, uid, arg, td)
372	struct mount *mp;
373	int cmd;
374	uid_t uid;
375	caddr_t arg;
376	struct thread *td;
377{
378	NCPVODEBUG("return EOPNOTSUPP\n");
379	return (EOPNOTSUPP);
380}
381
382/*ARGSUSED*/
383int
384nwfs_init(struct vfsconf *vfsp)
385{
386	nwfs_hash_init();
387	nwfs_pbuf_freecnt = nswbuf / 2 + 1;
388	NCPVODEBUG("always happy to load!\n");
389	return (0);
390}
391
392/*ARGSUSED*/
393int
394nwfs_uninit(struct vfsconf *vfsp)
395{
396
397	nwfs_hash_free();
398	NCPVODEBUG("unloaded\n");
399	return (0);
400}
401
402/*
403 * nwfs_statfs call
404 */
405int
406nwfs_statfs(mp, sbp, td)
407	struct mount *mp;
408	struct statfs *sbp;
409	struct thread *td;
410{
411	struct nwmount *nmp = VFSTONWFS(mp);
412	int error = 0, secsize;
413	struct nwnode *np = nmp->n_root;
414	struct ncp_volume_info vi;
415
416	if (np == NULL) return EINVAL;
417	error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
418	    nmp->n_volume, &vi, td, td->td_ucred);
419	if (error) return error;
420	secsize = 512;			/* XXX how to get real value ??? */
421	/* fundamental filesystem block size */
422	sbp->f_bsize = vi.sectors_per_block*secsize;
423	/* optimal transfer block size */
424	sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
425	/* total data blocks in filesystem */
426	sbp->f_blocks= vi.total_blocks;
427	/* free blocks in fs */
428	sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
429	/* free blocks avail to non-superuser */
430	sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
431	/* total file nodes in filesystem */
432	sbp->f_files = vi.total_dir_entries;
433	/* free file nodes in fs */
434	sbp->f_ffree = vi.available_dir_entries;
435	sbp->f_flags = 0;		/* copy of mount exported flags */
436	if (sbp != &mp->mnt_stat) {
437		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* filesystem id */
438		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
439		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
440		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
441		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
442	}
443	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
444	return 0;
445}
446