nwfs_vfsops.c revision 66539
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 66539 2000-10-02 09:29:59Z 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);
71
72static int nwfs_mount(struct mount *, char *, caddr_t,
73			struct nameidata *, struct proc *);
74static int nwfs_quotactl(struct mount *, int, uid_t, caddr_t, struct proc *);
75static int nwfs_root(struct mount *, struct vnode **);
76static int nwfs_start(struct mount *, int, struct proc *);
77static int nwfs_statfs(struct mount *, struct statfs *, struct proc *);
78static int nwfs_sync(struct mount *, int, struct ucred *, struct proc *);
79static int nwfs_unmount(struct mount *, int, struct proc *);
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	nwfs_sync,
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	if (pe == NULL) return ENOMEM;
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	size_t size;
151	int error;
152	struct nwmount *nmp = NULL;
153	struct ncp_conn *conn = NULL;
154	struct ncp_handle *handle = NULL;
155	struct vnode *vp;
156	char *pc,*pe;
157
158	if (data == NULL) {
159		nwfs_printf("missing data argument\n");
160		return 1;
161	}
162	if (mp->mnt_flag & MNT_UPDATE) {
163		nwfs_printf("MNT_UPDATE not implemented");
164		return (EOPNOTSUPP);
165	}
166	error = copyin(data, (caddr_t)&args, sizeof(struct nwfs_args));
167	if (error)
168		return (error);
169	if (args.version != NWFS_VERSION) {
170		nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
171		return (1);
172	}
173	error = ncp_conn_getbyref(args.connRef,p,p->p_ucred,NCPM_EXECUTE,&conn);
174	if (error) {
175		nwfs_printf("invalid connection refernce %d\n",args.connRef);
176		return (error);
177	}
178	error = ncp_conn_gethandle(conn, NULL, &handle);
179	if (error) {
180		nwfs_printf("can't get connection handle\n");
181		return (error);
182	}
183	ncp_conn_unlock(conn,p);	/* we keep the ref */
184	mp->mnt_stat.f_iosize = conn->buffer_size;
185        /* We must malloc our own mount info */
186        MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA,M_USE_RESERVE);
187        if (nmp == NULL) {
188                nwfs_printf("could not alloc nwmount\n");
189                error = ENOMEM;
190		goto bad;
191        }
192	bzero(nmp,sizeof(*nmp));
193        mp->mnt_data = (qaddr_t)nmp;
194	nmp->connh = handle;
195	nmp->n_root = NULL;
196	nmp->n_id = nwfsid++;
197        nmp->m = args;
198	nmp->m.file_mode = (nmp->m.file_mode &
199			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
200	nmp->m.dir_mode  = (nmp->m.dir_mode &
201			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
202	if ((error = nwfs_initnls(nmp)) != 0) goto bad;
203	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
204	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
205	pc = mp->mnt_stat.f_mntfromname;
206	pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
207	bzero(pc, MNAMELEN);
208	*(pc++) = '/';
209	pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
210	if (pc < pe-1) {
211		*(pc++) = ':';
212		pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
213		if (pc < pe-1) {
214			*(pc++) = '/';
215			strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
216		}
217	}
218	/* protect against invalid mount points */
219	nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
220	vfs_getnewfsid(mp);
221	error = nwfs_root(mp, &vp);
222	if (error)
223		goto bad;
224	/*
225	 * Lose the lock but keep the ref.
226	 */
227	VOP_UNLOCK(vp, 0, curproc);
228	NCPVODEBUG("rootvp.vrefcnt=%d\n",vp->v_usecount);
229	return error;
230bad:
231        if (nmp)
232		free(nmp, M_NWFSDATA);
233	if (handle)
234		ncp_conn_puthandle(handle, NULL, 0);
235        return error;
236}
237
238/* Unmount the filesystem described by mp. */
239static int
240nwfs_unmount(struct mount *mp, int mntflags, struct proc *p)
241{
242	struct nwmount *nmp = VFSTONWFS(mp);
243	struct ncp_conn *conn;
244	struct vnode *vp;
245	int error, flags;
246
247	NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
248	flags = 0;
249	if (mntflags & MNT_FORCE)
250		flags |= FORCECLOSE;
251	error = VFS_ROOT(mp,&vp);
252	if (error) return (error);
253	if (vp->v_usecount > 2) {
254		printf("nwfs_unmount: usecnt=%d\n",vp->v_usecount);
255		vput(vp);
256		return (EBUSY);
257	}
258	error = vflush(mp, vp, flags);
259	if (error) {
260		vput(vp);
261		return (error);
262	}
263	/*
264	 * There are two reference counts and one lock to get rid of here.
265	 */
266	NCPVODEBUG("v_use: %d\n",vp->v_usecount);
267	vput(vp);
268	NCPVODEBUG("v_use after vput: %d\n",vp->v_usecount);
269	vrele(vp);
270	NCPVODEBUG("v_use after vrele: %d\n",vp->v_usecount);
271	vgone(vp);
272	NCPVODEBUG("v_gone finished !!!!\n");
273	conn = NWFSTOCONN(nmp);
274	ncp_conn_puthandle(nmp->connh,NULL,0);
275	if (ncp_conn_lock(conn,p,p->p_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
276		if(ncp_disconnect(conn))
277			ncp_conn_unlock(conn,p);
278	}
279	mp->mnt_data = (qaddr_t)0;
280	if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
281		free(nmp->m.nls.to_lower, M_NWFSDATA);
282	free(nmp, M_NWFSDATA);
283	mp->mnt_flag &= ~MNT_LOCAL;
284	return (error);
285}
286
287/*  Return locked vnode to root of a filesystem */
288static int
289nwfs_root(struct mount *mp, struct vnode **vpp) {
290	struct vnode *vp;
291	struct nwmount *nmp;
292	struct nwnode *np;
293	struct ncp_conn *conn;
294	struct nw_entry_info fattr;
295	struct proc *p = curproc;
296	struct ucred *cred = p->p_ucred;
297	int error, nsf, opt;
298	u_char vol;
299
300	nmp = VFSTONWFS(mp);
301	conn = NWFSTOCONN(nmp);
302	if (nmp->n_root) {
303		*vpp = NWTOV(nmp->n_root);
304		vget(*vpp, LK_EXCLUSIVE, curproc);
305		return 0;
306	}
307	error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
308		&nmp->n_rootent.f_id, p, cred);
309	if (error)
310		return ENOENT;
311	nmp->n_volume = vol;
312	error = ncp_get_namespaces(conn, vol, &nsf, p, cred);
313	if (error)
314		return ENOENT;
315	if (nsf & NW_NSB_OS2) {
316		NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
317		if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
318			nmp->name_space = NW_NS_OS2;
319			nmp->m.nls.opt &= ~NWHP_DOS;
320		}
321	}
322	opt = nmp->m.nls.opt;
323	nsf = opt & (NWHP_UPPER | NWHP_LOWER);
324	if (opt & NWHP_DOS) {
325		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
326			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
327		} else if (nsf == 0) {
328			nmp->m.nls.opt |= NWHP_LOWER;
329		}
330	} else {
331		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
332			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
333		}
334	}
335	if (nmp->m.root_path[0]) {
336		nmp->m.root_path[0]--;
337		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
338		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, p, cred);
339		if (error) {
340			NCPFATAL("Invalid root path specified\n");
341			return ENOENT;
342		}
343		nmp->n_rootent.f_parent = fattr.dirEntNum;
344		nmp->m.root_path[0]++;
345		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
346		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, p, cred);
347		if (error) {
348			NCPFATAL("Invalid root path specified\n");
349			return ENOENT;
350		}
351		nmp->n_rootent.f_id = fattr.dirEntNum;
352	} else {
353		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
354		    0, NULL, &fattr, p, cred);
355		if (error) {
356			NCPFATAL("Can't obtain volume info\n");
357			return ENOENT;
358		}
359		fattr.nameLen = strlen(strcpy(fattr.entryName, NWFS_ROOTVOL));
360		nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
361	}
362	error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
363	if (error)
364		return (error);
365	vp->v_flag |= VROOT;
366	np = VTONW(vp);
367	if (nmp->m.root_path[0] == 0)
368		np->n_flag |= NVOLUME;
369	nmp->n_root = np;
370/*	error = VOP_GETATTR(vp, &vattr, cred, p);
371	if (error) {
372		vput(vp);
373		NCPFATAL("Can't get root directory entry\n");
374		return error;
375	}*/
376	*vpp = vp;
377	return (0);
378}
379
380/*
381 * Vfs start routine, a no-op.
382 */
383/* ARGSUSED */
384static int
385nwfs_start(mp, flags, p)
386	struct mount *mp;
387	int flags;
388	struct proc *p;
389{
390	NCPVODEBUG("flags=%04x\n",flags);
391	return (0);
392}
393
394/*
395 * Do operations associated with quotas, not supported
396 */
397/* ARGSUSED */
398static int
399nwfs_quotactl(mp, cmd, uid, arg, p)
400	struct mount *mp;
401	int cmd;
402	uid_t uid;
403	caddr_t arg;
404	struct proc *p;
405{
406	NCPVODEBUG("return EOPNOTSUPP\n");
407	return (EOPNOTSUPP);
408}
409
410/*ARGSUSED*/
411int
412nwfs_init(struct vfsconf *vfsp)
413{
414#ifndef SMP
415	int name[2];
416	int olen, ncpu, plen, error;
417
418	name[0] = CTL_HW;
419	name[1] = HW_NCPU;
420	error = kernel_sysctl(curproc, name, 2, &ncpu, &olen, NULL, 0, &plen);
421	if (error == 0 && ncpu > 1)
422		printf("warning: nwfs module compiled without SMP support.");
423#endif
424	nwfs_hash_init();
425	nwfs_pbuf_freecnt = nswbuf / 2 + 1;
426	NCPVODEBUG("always happy to load!\n");
427	return (0);
428}
429
430/*ARGSUSED*/
431int
432nwfs_uninit(struct vfsconf *vfsp)
433{
434
435	nwfs_hash_free();
436	NCPVODEBUG("unloaded\n");
437	return (0);
438}
439
440/*
441 * nwfs_statfs call
442 */
443int
444nwfs_statfs(mp, sbp, p)
445	struct mount *mp;
446	struct statfs *sbp;
447	struct proc *p;
448{
449	struct nwmount *nmp = VFSTONWFS(mp);
450	int error = 0, secsize;
451	struct nwnode *np = nmp->n_root;
452	struct ncp_volume_info vi;
453
454	if (np == NULL) return EINVAL;
455	error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp), nmp->n_volume, &vi,p,p->p_ucred);
456	if (error) return error;
457	secsize = 512;			/* XXX how to get real value ??? */
458	sbp->f_spare2=0;		/* placeholder */
459	/* fundamental file system block size */
460	sbp->f_bsize = vi.sectors_per_block*secsize;
461	/* optimal transfer block size */
462	sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
463	/* total data blocks in file system */
464	sbp->f_blocks= vi.total_blocks;
465	/* free blocks in fs */
466	sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
467	/* free blocks avail to non-superuser */
468	sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
469	/* total file nodes in file system */
470	sbp->f_files = vi.total_dir_entries;
471	/* free file nodes in fs */
472	sbp->f_ffree = vi.available_dir_entries;
473	sbp->f_flags = 0;		/* copy of mount exported flags */
474	if (sbp != &mp->mnt_stat) {
475		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* file system id */
476		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
477		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
478		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
479		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
480	}
481	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
482	return 0;
483}
484
485/*
486 * Flush out the buffer cache
487 */
488/* ARGSUSED */
489static int
490nwfs_sync(mp, waitfor, cred, p)
491	struct mount *mp;
492	int waitfor;
493	struct ucred *cred;
494	struct proc *p;
495{
496	struct vnode *vp;
497	int error, allerror = 0;
498	/*
499	 * Force stale buffer cache information to be flushed.
500	 */
501loop:
502	for (vp = mp->mnt_vnodelist.lh_first;
503	     vp != NULL;
504	     vp = vp->v_mntvnodes.le_next) {
505		/*
506		 * If the vnode that we are about to sync is no longer
507		 * associated with this mount point, start over.
508		 */
509		if (vp->v_mount != mp)
510			goto loop;
511		if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
512		    waitfor == MNT_LAZY)
513			continue;
514		if (vget(vp, LK_EXCLUSIVE, p))
515			goto loop;
516		error = VOP_FSYNC(vp, cred, waitfor, p);
517		if (error)
518			allerror = error;
519		vput(vp);
520	}
521	return (allerror);
522}
523