nwfs_vfsops.c revision 74637
1169092Sdeischen/*
2169092Sdeischen * Copyright (c) 1999, 2000 Boris Popov
3169092Sdeischen * All rights reserved.
4157196Sdeischen *
5157196Sdeischen * Redistribution and use in source and binary forms, with or without
6157196Sdeischen * modification, are permitted provided that the following conditions
7157196Sdeischen * are met:
8157196Sdeischen * 1. Redistributions of source code must retain the above copyright
9157196Sdeischen *    notice, this list of conditions and the following disclaimer.
10157196Sdeischen * 2. Redistributions in binary form must reproduce the above copyright
11157196Sdeischen *    notice, this list of conditions and the following disclaimer in the
12157196Sdeischen *    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 74637 2001-03-22 06:51:53Z bp $
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 <nwfs/nwfs.h>
58#include <nwfs/nwfs_node.h>
59#include <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 file system");
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 proc *);
75static int nwfs_quotactl(struct mount *, int, uid_t, caddr_t, struct proc *);
76static int nwfs_root(struct mount *, struct vnode **);
77static int nwfs_start(struct mount *, int, struct proc *);
78static int nwfs_statfs(struct mount *, struct statfs *, struct proc *);
79static int nwfs_sync(struct mount *, int, struct ucred *, struct proc *);
80static int nwfs_unmount(struct mount *, int, struct proc *);
81static int nwfs_init(struct vfsconf *vfsp);
82static int nwfs_uninit(struct vfsconf *vfsp);
83
84static struct vfsops nwfs_vfsops = {
85	nwfs_mount,
86	nwfs_start,
87	nwfs_unmount,
88	nwfs_root,
89	nwfs_quotactl,
90	nwfs_statfs,
91	nwfs_sync,
92	vfs_stdvget,
93	vfs_stdfhtovp,		/* shouldn't happen */
94	vfs_stdcheckexp,
95	vfs_stdvptofh,		/* shouldn't happen */
96	nwfs_init,
97	nwfs_uninit,
98	vfs_stdextattrctl,
99};
100
101
102VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
103
104int nwfs_pbuf_freecnt = -1;	/* start out unlimited */
105static int nwfsid = 1;
106
107static int
108nwfs_initnls(struct nwmount *nmp) {
109	char	*pc, *pe;
110	int	error = 0;
111#define COPY_TABLE(t,d)	{ \
112		if (t) { \
113			error = copyin((t), pc, 256); \
114			if (error) break; \
115		} else \
116			bcopy(d, pc, 256); \
117		(t) = pc; pc += 256; \
118	}
119
120	nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
121	if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
122		nmp->m.nls.to_lower = ncp_defnls.to_lower;
123		nmp->m.nls.to_upper = ncp_defnls.to_upper;
124		nmp->m.nls.n2u = ncp_defnls.n2u;
125		nmp->m.nls.u2n = ncp_defnls.u2n;
126		return 0;
127	}
128	MALLOC(pe, char *, 256 * 4, M_NWFSDATA, M_WAITOK);
129	pc = pe;
130	do {
131		COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
132		COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
133		COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
134		COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
135	} while(0);
136	if (error) {
137		free(pe, M_NWFSDATA);
138		return error;
139	}
140	return 0;
141}
142/*
143 * mp - path - addr in user space of mount point (ie /usr or whatever)
144 * data - addr in user space of mount params
145 */
146static int nwfs_mount(struct mount *mp, char *path, caddr_t data,
147		      struct nameidata *ndp, struct proc *p)
148{
149	struct nwfs_args args; 	  /* will hold data from mount request */
150	int error;
151	struct nwmount *nmp = NULL;
152	struct ncp_conn *conn = NULL;
153	struct ncp_handle *handle = NULL;
154	struct vnode *vp;
155	char *pc,*pe;
156
157	if (data == NULL) {
158		nwfs_printf("missing data argument\n");
159		return 1;
160	}
161	if (mp->mnt_flag & MNT_UPDATE) {
162		nwfs_printf("MNT_UPDATE not implemented");
163		return (EOPNOTSUPP);
164	}
165	error = copyin(data, (caddr_t)&args, sizeof(struct nwfs_args));
166	if (error)
167		return (error);
168	if (args.version != NWFS_VERSION) {
169		nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
170		return (1);
171	}
172	error = ncp_conn_getbyref(args.connRef,p,p->p_ucred,NCPM_EXECUTE,&conn);
173	if (error) {
174		nwfs_printf("invalid connection refernce %d\n",args.connRef);
175		return (error);
176	}
177	error = ncp_conn_gethandle(conn, NULL, &handle);
178	if (error) {
179		nwfs_printf("can't get connection handle\n");
180		return (error);
181	}
182	ncp_conn_unlock(conn,p);	/* we keep the ref */
183	mp->mnt_stat.f_iosize = conn->buffer_size;
184        /* We must malloc our own mount info */
185        MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA,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, curproc);
224	NCPVODEBUG("rootvp.vrefcnt=%d\n",vp->v_usecount);
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 proc *p)
237{
238	struct nwmount *nmp = VFSTONWFS(mp);
239	struct ncp_conn *conn;
240	struct vnode *vp;
241	int error, flags;
242
243	NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
244	flags = 0;
245	if (mntflags & MNT_FORCE)
246		flags |= FORCECLOSE;
247	error = VFS_ROOT(mp,&vp);
248	if (error) return (error);
249	if (vp->v_usecount > 2) {
250		printf("nwfs_unmount: usecnt=%d\n",vp->v_usecount);
251		vput(vp);
252		return (EBUSY);
253	}
254	error = vflush(mp, vp, flags);
255	if (error) {
256		vput(vp);
257		return (error);
258	}
259	/*
260	 * There are two reference counts and one lock to get rid of here.
261	 */
262	NCPVODEBUG("v_use: %d\n",vp->v_usecount);
263	vput(vp);
264	NCPVODEBUG("v_use after vput: %d\n",vp->v_usecount);
265	vrele(vp);
266	NCPVODEBUG("v_use after vrele: %d\n",vp->v_usecount);
267	vgone(vp);
268	NCPVODEBUG("v_gone finished !!!!\n");
269	conn = NWFSTOCONN(nmp);
270	ncp_conn_puthandle(nmp->connh,NULL,0);
271	if (ncp_conn_lock(conn,p,p->p_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
272		if(ncp_conn_free(conn))
273			ncp_conn_unlock(conn,p);
274	}
275	mp->mnt_data = (qaddr_t)0;
276	if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
277		free(nmp->m.nls.to_lower, M_NWFSDATA);
278	free(nmp, M_NWFSDATA);
279	mp->mnt_flag &= ~MNT_LOCAL;
280	return (error);
281}
282
283/*  Return locked vnode to root of a filesystem */
284static int
285nwfs_root(struct mount *mp, struct vnode **vpp) {
286	struct vnode *vp;
287	struct nwmount *nmp;
288	struct nwnode *np;
289	struct ncp_conn *conn;
290	struct nw_entry_info fattr;
291	struct proc *p = curproc;
292	struct ucred *cred = p->p_ucred;
293	int error, nsf, opt;
294	u_char vol;
295
296	nmp = VFSTONWFS(mp);
297	conn = NWFSTOCONN(nmp);
298	if (nmp->n_root) {
299		*vpp = NWTOV(nmp->n_root);
300		while (vget(*vpp, LK_EXCLUSIVE, curproc) != 0)
301			;
302		return 0;
303	}
304	error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
305		&nmp->n_rootent.f_id, p, cred);
306	if (error)
307		return ENOENT;
308	nmp->n_volume = vol;
309	error = ncp_get_namespaces(conn, vol, &nsf, p, cred);
310	if (error)
311		return ENOENT;
312	if (nsf & NW_NSB_OS2) {
313		NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
314		if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
315			nmp->name_space = NW_NS_OS2;
316			nmp->m.nls.opt &= ~NWHP_DOS;
317		}
318	}
319	opt = nmp->m.nls.opt;
320	nsf = opt & (NWHP_UPPER | NWHP_LOWER);
321	if (opt & NWHP_DOS) {
322		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
323			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
324		} else if (nsf == 0) {
325			nmp->m.nls.opt |= NWHP_LOWER;
326		}
327	} else {
328		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
329			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
330		}
331	}
332	if (nmp->m.root_path[0]) {
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, p, cred);
336		if (error) {
337			NCPFATAL("Invalid root path specified\n");
338			return ENOENT;
339		}
340		nmp->n_rootent.f_parent = fattr.dirEntNum;
341		nmp->m.root_path[0]++;
342		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
343		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, p, cred);
344		if (error) {
345			NCPFATAL("Invalid root path specified\n");
346			return ENOENT;
347		}
348		nmp->n_rootent.f_id = fattr.dirEntNum;
349	} else {
350		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
351		    0, NULL, &fattr, p, cred);
352		if (error) {
353			NCPFATAL("Can't obtain volume info\n");
354			return ENOENT;
355		}
356		fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
357		nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
358	}
359	error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
360	if (error)
361		return (error);
362	vp->v_flag |= VROOT;
363	np = VTONW(vp);
364	if (nmp->m.root_path[0] == 0)
365		np->n_flag |= NVOLUME;
366	nmp->n_root = np;
367/*	error = VOP_GETATTR(vp, &vattr, cred, p);
368	if (error) {
369		vput(vp);
370		NCPFATAL("Can't get root directory entry\n");
371		return error;
372	}*/
373	*vpp = vp;
374	return (0);
375}
376
377/*
378 * Vfs start routine, a no-op.
379 */
380/* ARGSUSED */
381static int
382nwfs_start(mp, flags, p)
383	struct mount *mp;
384	int flags;
385	struct proc *p;
386{
387	NCPVODEBUG("flags=%04x\n",flags);
388	return (0);
389}
390
391/*
392 * Do operations associated with quotas, not supported
393 */
394/* ARGSUSED */
395static int
396nwfs_quotactl(mp, cmd, uid, arg, p)
397	struct mount *mp;
398	int cmd;
399	uid_t uid;
400	caddr_t arg;
401	struct proc *p;
402{
403	NCPVODEBUG("return EOPNOTSUPP\n");
404	return (EOPNOTSUPP);
405}
406
407/*ARGSUSED*/
408int
409nwfs_init(struct vfsconf *vfsp)
410{
411#ifndef SMP
412	int name[2];
413	int olen, ncpu, plen, error;
414
415	name[0] = CTL_HW;
416	name[1] = HW_NCPU;
417	error = kernel_sysctl(curproc, name, 2, &ncpu, &olen, NULL, 0, &plen);
418	if (error == 0 && ncpu > 1)
419		printf("warning: nwfs module compiled without SMP support.");
420#endif
421	nwfs_hash_init();
422	nwfs_pbuf_freecnt = nswbuf / 2 + 1;
423	NCPVODEBUG("always happy to load!\n");
424	return (0);
425}
426
427/*ARGSUSED*/
428int
429nwfs_uninit(struct vfsconf *vfsp)
430{
431
432	nwfs_hash_free();
433	NCPVODEBUG("unloaded\n");
434	return (0);
435}
436
437/*
438 * nwfs_statfs call
439 */
440int
441nwfs_statfs(mp, sbp, p)
442	struct mount *mp;
443	struct statfs *sbp;
444	struct proc *p;
445{
446	struct nwmount *nmp = VFSTONWFS(mp);
447	int error = 0, secsize;
448	struct nwnode *np = nmp->n_root;
449	struct ncp_volume_info vi;
450
451	if (np == NULL) return EINVAL;
452	error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp), nmp->n_volume, &vi,p,p->p_ucred);
453	if (error) return error;
454	secsize = 512;			/* XXX how to get real value ??? */
455	sbp->f_spare2=0;		/* placeholder */
456	/* fundamental file system block size */
457	sbp->f_bsize = vi.sectors_per_block*secsize;
458	/* optimal transfer block size */
459	sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
460	/* total data blocks in file system */
461	sbp->f_blocks= vi.total_blocks;
462	/* free blocks in fs */
463	sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
464	/* free blocks avail to non-superuser */
465	sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
466	/* total file nodes in file system */
467	sbp->f_files = vi.total_dir_entries;
468	/* free file nodes in fs */
469	sbp->f_ffree = vi.available_dir_entries;
470	sbp->f_flags = 0;		/* copy of mount exported flags */
471	if (sbp != &mp->mnt_stat) {
472		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* file system id */
473		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
474		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
475		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
476		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
477	}
478	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
479	return 0;
480}
481
482/*
483 * Flush out the buffer cache
484 */
485/* ARGSUSED */
486static int
487nwfs_sync(mp, waitfor, cred, p)
488	struct mount *mp;
489	int waitfor;
490	struct ucred *cred;
491	struct proc *p;
492{
493	struct vnode *vp;
494	int error, allerror = 0;
495	/*
496	 * Force stale buffer cache information to be flushed.
497	 */
498loop:
499	for (vp = LIST_FIRST(&mp->mnt_vnodelist);
500	     vp != NULL;
501	     vp = LIST_NEXT(vp, v_mntvnodes)) {
502		/*
503		 * If the vnode that we are about to sync is no longer
504		 * associated with this mount point, start over.
505		 */
506		if (vp->v_mount != mp)
507			goto loop;
508		if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
509		    waitfor == MNT_LAZY)
510			continue;
511		if (vget(vp, LK_EXCLUSIVE, p))
512			goto loop;
513		error = VOP_FSYNC(vp, cred, waitfor, p);
514		if (error)
515			allerror = error;
516		vput(vp);
517	}
518	return (allerror);
519}
520