nwfs_vfsops.c revision 137479
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 137479 2004-11-09 22:21:10Z phk $
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_omount_t	nwfs_omount;
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_omount =		nwfs_omount,
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_omount(struct mount *mp, char *path, caddr_t data,
140		      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 (mp->mnt_flag & MNT_ROOTFS)
151		return (EOPNOTSUPP);
152	if (data == NULL) {
153		nwfs_printf("missing data argument\n");
154		return 1;
155	}
156	if (mp->mnt_flag & MNT_UPDATE) {
157		nwfs_printf("MNT_UPDATE not implemented");
158		return (EOPNOTSUPP);
159	}
160	error = copyin(data, (caddr_t)&args, sizeof(struct nwfs_args));
161	if (error)
162		return (error);
163	if (args.version != NWFS_VERSION) {
164		nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
165		return (1);
166	}
167	error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn);
168	if (error) {
169		nwfs_printf("invalid connection refernce %d\n",args.connRef);
170		return (error);
171	}
172	error = ncp_conn_gethandle(conn, NULL, &handle);
173	if (error) {
174		nwfs_printf("can't get connection handle\n");
175		return (error);
176	}
177	ncp_conn_unlock(conn, td);	/* we keep the ref */
178	mp->mnt_stat.f_iosize = conn->buffer_size;
179        /* We must malloc our own mount info */
180        MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA,
181	    M_WAITOK | M_USE_RESERVE | M_ZERO);
182        if (nmp == NULL) {
183                nwfs_printf("could not alloc nwmount\n");
184                error = ENOMEM;
185		goto bad;
186        }
187        mp->mnt_data = (qaddr_t)nmp;
188	nmp->connh = handle;
189	nmp->n_root = NULL;
190	nmp->n_id = nwfsid++;
191        nmp->m = args;
192	nmp->m.file_mode = (nmp->m.file_mode &
193			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
194	nmp->m.dir_mode  = (nmp->m.dir_mode &
195			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
196	if ((error = nwfs_initnls(nmp)) != 0) goto bad;
197	pc = mp->mnt_stat.f_mntfromname;
198	pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
199	bzero(pc, MNAMELEN);
200	*(pc++) = '/';
201	pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
202	if (pc < pe-1) {
203		*(pc++) = ':';
204		pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
205		if (pc < pe-1) {
206			*(pc++) = '/';
207			strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
208		}
209	}
210	/* protect against invalid mount points */
211	nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
212	vfs_getnewfsid(mp);
213	error = nwfs_root(mp, &vp, td);
214	if (error)
215		goto bad;
216	/*
217	 * Lose the lock but keep the ref.
218	 */
219	VOP_UNLOCK(vp, 0, curthread);
220	NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp));
221	return error;
222bad:
223        if (nmp)
224		free(nmp, M_NWFSDATA);
225	if (handle)
226		ncp_conn_puthandle(handle, NULL, 0);
227        return error;
228}
229
230/* Unmount the filesystem described by mp. */
231static int
232nwfs_unmount(struct mount *mp, int mntflags, struct thread *td)
233{
234	struct nwmount *nmp = VFSTONWFS(mp);
235	struct ncp_conn *conn;
236	int error, flags;
237
238	NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
239	flags = 0;
240	if (mntflags & MNT_FORCE)
241		flags |= FORCECLOSE;
242	/* There is 1 extra root vnode reference from nwfs_mount(). */
243	error = vflush(mp, 1, flags, td);
244	if (error)
245		return (error);
246	conn = NWFSTOCONN(nmp);
247	ncp_conn_puthandle(nmp->connh,NULL,0);
248	if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
249		if(ncp_conn_free(conn))
250			ncp_conn_unlock(conn, td);
251	}
252	mp->mnt_data = (qaddr_t)0;
253	if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
254		free(nmp->m.nls.to_lower, M_NWFSDATA);
255	free(nmp, M_NWFSDATA);
256	mp->mnt_flag &= ~MNT_LOCAL;
257	return (error);
258}
259
260/*  Return locked vnode to root of a filesystem */
261static int
262nwfs_root(struct mount *mp, struct vnode **vpp, struct thread *td) {
263	struct vnode *vp;
264	struct nwmount *nmp;
265	struct nwnode *np;
266	struct ncp_conn *conn;
267	struct nw_entry_info fattr;
268	struct ucred *cred =  td->td_ucred;
269	int error, nsf, opt;
270	u_char vol;
271
272	nmp = VFSTONWFS(mp);
273	conn = NWFSTOCONN(nmp);
274	if (nmp->n_root) {
275		*vpp = NWTOV(nmp->n_root);
276		while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
277			;
278		return 0;
279	}
280	error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
281		&nmp->n_rootent.f_id, td, cred);
282	if (error)
283		return ENOENT;
284	nmp->n_volume = vol;
285	error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
286	if (error)
287		return ENOENT;
288	if (nsf & NW_NSB_OS2) {
289		NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
290		if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
291			nmp->name_space = NW_NS_OS2;
292			nmp->m.nls.opt &= ~NWHP_DOS;
293		}
294	}
295	opt = nmp->m.nls.opt;
296	nsf = opt & (NWHP_UPPER | NWHP_LOWER);
297	if (opt & NWHP_DOS) {
298		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
299			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
300		} else if (nsf == 0) {
301			nmp->m.nls.opt |= NWHP_LOWER;
302		}
303	} else {
304		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
305			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
306		}
307	}
308	if (nmp->m.root_path[0]) {
309		nmp->m.root_path[0]--;
310		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
311		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
312		if (error) {
313			NCPFATAL("Invalid root path specified\n");
314			return ENOENT;
315		}
316		nmp->n_rootent.f_parent = fattr.dirEntNum;
317		nmp->m.root_path[0]++;
318		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
319		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
320		if (error) {
321			NCPFATAL("Invalid root path specified\n");
322			return ENOENT;
323		}
324		nmp->n_rootent.f_id = fattr.dirEntNum;
325	} else {
326		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
327		    0, NULL, &fattr, td, cred);
328		if (error) {
329			NCPFATAL("Can't obtain volume info\n");
330			return ENOENT;
331		}
332		fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
333		nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
334	}
335	error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
336	if (error)
337		return (error);
338	vp->v_vflag |= VV_ROOT;
339	np = VTONW(vp);
340	if (nmp->m.root_path[0] == 0)
341		np->n_flag |= NVOLUME;
342	nmp->n_root = np;
343/*	error = VOP_GETATTR(vp, &vattr, cred, td);
344	if (error) {
345		vput(vp);
346		NCPFATAL("Can't get root directory entry\n");
347		return error;
348	}*/
349	*vpp = vp;
350	return (0);
351}
352
353/*
354 * Vfs start routine, a no-op.
355 */
356/* ARGSUSED */
357static int
358nwfs_start(mp, flags, td)
359	struct mount *mp;
360	int flags;
361	struct thread *td;
362{
363	NCPVODEBUG("flags=%04x\n",flags);
364	return (0);
365}
366
367/*
368 * Do operations associated with quotas, not supported
369 */
370/* ARGSUSED */
371static int
372nwfs_quotactl(mp, cmd, uid, arg, td)
373	struct mount *mp;
374	int cmd;
375	uid_t uid;
376	caddr_t arg;
377	struct thread *td;
378{
379	NCPVODEBUG("return EOPNOTSUPP\n");
380	return (EOPNOTSUPP);
381}
382
383/*ARGSUSED*/
384int
385nwfs_init(struct vfsconf *vfsp)
386{
387	nwfs_hash_init();
388	nwfs_pbuf_freecnt = nswbuf / 2 + 1;
389	NCPVODEBUG("always happy to load!\n");
390	return (0);
391}
392
393/*ARGSUSED*/
394int
395nwfs_uninit(struct vfsconf *vfsp)
396{
397
398	nwfs_hash_free();
399	NCPVODEBUG("unloaded\n");
400	return (0);
401}
402
403/*
404 * nwfs_statfs call
405 */
406int
407nwfs_statfs(mp, sbp, td)
408	struct mount *mp;
409	struct statfs *sbp;
410	struct thread *td;
411{
412	struct nwmount *nmp = VFSTONWFS(mp);
413	int error = 0, secsize;
414	struct nwnode *np = nmp->n_root;
415	struct ncp_volume_info vi;
416
417	if (np == NULL) return EINVAL;
418	error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
419	    nmp->n_volume, &vi, td, td->td_ucred);
420	if (error) return error;
421	secsize = 512;			/* XXX how to get real value ??? */
422	/* fundamental filesystem block size */
423	sbp->f_bsize = vi.sectors_per_block*secsize;
424	/* optimal transfer block size */
425	sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
426	/* total data blocks in filesystem */
427	sbp->f_blocks= vi.total_blocks;
428	/* free blocks in fs */
429	sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
430	/* free blocks avail to non-superuser */
431	sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
432	/* total file nodes in filesystem */
433	sbp->f_files = vi.total_dir_entries;
434	/* free file nodes in fs */
435	sbp->f_ffree = vi.available_dir_entries;
436	sbp->f_flags = 0;		/* copy of mount exported flags */
437	if (sbp != &mp->mnt_stat) {
438		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* filesystem id */
439		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
440		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
441		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
442		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
443	}
444	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
445	return 0;
446}
447