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