nwfs_vfsops.c revision 162647
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 162647 2006-09-26 04:12:49Z tegge $
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/proc.h>
38#include <sys/kernel.h>
39#include <sys/sysctl.h>
40#include <sys/vnode.h>
41#include <sys/mount.h>
42#include <sys/stat.h>
43#include <sys/malloc.h>
44#include <sys/bio.h>
45#include <sys/buf.h>
46
47#include <netncp/ncp.h>
48#include <netncp/ncp_conn.h>
49#include <netncp/ncp_subr.h>
50#include <netncp/ncp_ncp.h>
51#include <netncp/ncp_nls.h>
52
53#include <fs/nwfs/nwfs.h>
54#include <fs/nwfs/nwfs_node.h>
55#include <fs/nwfs/nwfs_subr.h>
56
57int nwfs_debuglevel = 0;
58
59static int nwfs_version = NWFS_VERSION;
60
61SYSCTL_DECL(_vfs_nwfs);
62SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware filesystem");
63SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
64SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
65
66MODULE_DEPEND(nwfs, ncp, 1, 1, 1);
67MODULE_DEPEND(nwfs, libmchain, 1, 1, 1);
68
69static vfs_cmount_t	nwfs_cmount;
70static vfs_mount_t	nwfs_mount;
71static vfs_quotactl_t	nwfs_quotactl;
72static vfs_root_t	nwfs_root;
73static vfs_statfs_t	nwfs_statfs;
74static vfs_unmount_t	nwfs_unmount;
75static vfs_init_t	nwfs_init;
76static vfs_uninit_t	nwfs_uninit;
77
78static struct vfsops nwfs_vfsops = {
79	.vfs_init =		nwfs_init,
80	.vfs_mount =		nwfs_mount,
81	.vfs_cmount =		nwfs_cmount,
82	.vfs_quotactl =		nwfs_quotactl,
83	.vfs_root =		nwfs_root,
84	.vfs_statfs =		nwfs_statfs,
85	.vfs_sync =		vfs_stdsync,
86	.vfs_uninit =		nwfs_uninit,
87	.vfs_unmount =		nwfs_unmount,
88};
89
90
91VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
92
93int nwfs_pbuf_freecnt = -1;	/* start out unlimited */
94static int nwfsid = 1;
95
96static int
97nwfs_initnls(struct nwmount *nmp) {
98	char	*pc, *pe;
99	int	error = 0;
100#define COPY_TABLE(t,d)	{ \
101		if (t) { \
102			error = copyin((t), pc, 256); \
103			if (error) break; \
104		} else \
105			bcopy(d, pc, 256); \
106		(t) = pc; pc += 256; \
107	}
108
109	nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
110	if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
111		nmp->m.nls.to_lower = ncp_defnls.to_lower;
112		nmp->m.nls.to_upper = ncp_defnls.to_upper;
113		nmp->m.nls.n2u = ncp_defnls.n2u;
114		nmp->m.nls.u2n = ncp_defnls.u2n;
115		return 0;
116	}
117	MALLOC(pe, char *, 256 * 4, M_NWFSDATA, M_WAITOK);
118	pc = pe;
119	do {
120		COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
121		COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
122		COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
123		COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
124	} while(0);
125	if (error) {
126		free(pe, M_NWFSDATA);
127		return error;
128	}
129	return 0;
130}
131
132static int nwfs_cmount(struct mntarg *ma, void *data, int flags,
133		      struct thread *td)
134{
135	struct nwfs_args args; 	  /* will hold data from mount request */
136	int error;
137
138	error = copyin(data, &args, sizeof(struct nwfs_args));
139	if (error)
140		return (error);
141
142	/*
143	 * XXX: cheap cop-out here, args contains a structure I don't
144	 * XXX: know how we should handle, and I don't see any immediate
145	 * XXX: prospect of avoiding a mount_nwfs(8) binary anyway.
146	 */
147	ma = mount_arg(ma, "nwfs_args", &args, sizeof args);
148
149	error = kernel_mount(ma, flags);
150
151	return (error);
152}
153
154/*
155 * mp - path - addr in user space of mount point (ie /usr or whatever)
156 * data - addr in user space of mount params
157 */
158static int nwfs_mount(struct mount *mp, struct thread *td)
159{
160	struct nwfs_args args; 	  /* will hold data from mount request */
161	int error;
162	struct nwmount *nmp = NULL;
163	struct ncp_conn *conn = NULL;
164	struct ncp_handle *handle = NULL;
165	struct vnode *vp;
166	char *pc,*pe;
167
168	if (mp->mnt_flag & MNT_ROOTFS)
169		return (EOPNOTSUPP);
170	if (mp->mnt_flag & MNT_UPDATE) {
171		nwfs_printf("MNT_UPDATE not implemented");
172		return (EOPNOTSUPP);
173	}
174	error = vfs_copyopt(mp->mnt_optnew, "nwfs_args", &args, sizeof args);
175	if (error)
176		return (error);
177	if (args.version != NWFS_VERSION) {
178		nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
179		return (1);
180	}
181	error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn);
182	if (error) {
183		nwfs_printf("invalid connection refernce %d\n",args.connRef);
184		return (error);
185	}
186	error = ncp_conn_gethandle(conn, NULL, &handle);
187	if (error) {
188		nwfs_printf("can't get connection handle\n");
189		return (error);
190	}
191	ncp_conn_unlock(conn, td);	/* we keep the ref */
192	mp->mnt_stat.f_iosize = conn->buffer_size;
193        /* We must malloc our own mount info */
194        MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA,
195	    M_WAITOK | M_USE_RESERVE | M_ZERO);
196        if (nmp == NULL) {
197                nwfs_printf("could not alloc nwmount\n");
198                error = ENOMEM;
199		goto bad;
200        }
201        mp->mnt_data = (qaddr_t)nmp;
202	nmp->connh = handle;
203	nmp->n_root = NULL;
204	nmp->n_id = nwfsid++;
205        nmp->m = args;
206	nmp->m.file_mode = (nmp->m.file_mode &
207			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
208	nmp->m.dir_mode  = (nmp->m.dir_mode &
209			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
210	if ((error = nwfs_initnls(nmp)) != 0) goto bad;
211	pc = mp->mnt_stat.f_mntfromname;
212	pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
213	bzero(pc, MNAMELEN);
214	*(pc++) = '/';
215	pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
216	if (pc < pe-1) {
217		*(pc++) = ':';
218		pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
219		if (pc < pe-1) {
220			*(pc++) = '/';
221			strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
222		}
223	}
224	/* protect against invalid mount points */
225	nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
226	vfs_getnewfsid(mp);
227	error = nwfs_root(mp, LK_EXCLUSIVE, &vp, td);
228	if (error)
229		goto bad;
230	/*
231	 * Lose the lock but keep the ref.
232	 */
233	VOP_UNLOCK(vp, 0, curthread);
234	NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp));
235	return error;
236bad:
237        if (nmp)
238		free(nmp, M_NWFSDATA);
239	if (handle)
240		ncp_conn_puthandle(handle, NULL, 0);
241        return error;
242}
243
244/* Unmount the filesystem described by mp. */
245static int
246nwfs_unmount(struct mount *mp, int mntflags, struct thread *td)
247{
248	struct nwmount *nmp = VFSTONWFS(mp);
249	struct ncp_conn *conn;
250	int error, flags;
251
252	NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
253	flags = 0;
254	if (mntflags & MNT_FORCE)
255		flags |= FORCECLOSE;
256	/* There is 1 extra root vnode reference from nwfs_mount(). */
257	error = vflush(mp, 1, flags, td);
258	if (error)
259		return (error);
260	conn = NWFSTOCONN(nmp);
261	ncp_conn_puthandle(nmp->connh,NULL,0);
262	if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
263		if(ncp_conn_free(conn))
264			ncp_conn_unlock(conn, td);
265	}
266	mp->mnt_data = (qaddr_t)0;
267	if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
268		free(nmp->m.nls.to_lower, M_NWFSDATA);
269	free(nmp, M_NWFSDATA);
270	MNT_ILOCK(mp);
271	mp->mnt_flag &= ~MNT_LOCAL;
272	MNT_IUNLOCK(mp);
273	return (error);
274}
275
276/*  Return locked vnode to root of a filesystem */
277static int
278nwfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) {
279	struct vnode *vp;
280	struct nwmount *nmp;
281	struct nwnode *np;
282	struct ncp_conn *conn;
283	struct nw_entry_info fattr;
284	struct ucred *cred =  td->td_ucred;
285	int error, nsf, opt;
286	u_char vol;
287
288	nmp = VFSTONWFS(mp);
289	conn = NWFSTOCONN(nmp);
290	if (nmp->n_root) {
291		*vpp = NWTOV(nmp->n_root);
292		while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
293			;
294		return 0;
295	}
296	error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
297		&nmp->n_rootent.f_id, td, cred);
298	if (error)
299		return ENOENT;
300	nmp->n_volume = vol;
301	error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
302	if (error)
303		return ENOENT;
304	if (nsf & NW_NSB_OS2) {
305		NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
306		if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
307			nmp->name_space = NW_NS_OS2;
308			nmp->m.nls.opt &= ~NWHP_DOS;
309		}
310	}
311	opt = nmp->m.nls.opt;
312	nsf = opt & (NWHP_UPPER | NWHP_LOWER);
313	if (opt & NWHP_DOS) {
314		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
315			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
316		} else if (nsf == 0) {
317			nmp->m.nls.opt |= NWHP_LOWER;
318		}
319	} else {
320		if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
321			nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
322		}
323	}
324	if (nmp->m.root_path[0]) {
325		nmp->m.root_path[0]--;
326		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
327		    -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
328		if (error) {
329			NCPFATAL("Invalid root path specified\n");
330			return ENOENT;
331		}
332		nmp->n_rootent.f_parent = fattr.dirEntNum;
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, td, cred);
336		if (error) {
337			NCPFATAL("Invalid root path specified\n");
338			return ENOENT;
339		}
340		nmp->n_rootent.f_id = fattr.dirEntNum;
341	} else {
342		error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
343		    0, NULL, &fattr, td, cred);
344		if (error) {
345			NCPFATAL("Can't obtain volume info\n");
346			return ENOENT;
347		}
348		fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
349		nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
350	}
351	error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
352	if (error)
353		return (error);
354	vp->v_vflag |= VV_ROOT;
355	np = VTONW(vp);
356	if (nmp->m.root_path[0] == 0)
357		np->n_flag |= NVOLUME;
358	nmp->n_root = np;
359/*	error = VOP_GETATTR(vp, &vattr, cred, td);
360	if (error) {
361		vput(vp);
362		NCPFATAL("Can't get root directory entry\n");
363		return error;
364	}*/
365	*vpp = vp;
366	return (0);
367}
368
369/*
370 * Do operations associated with quotas, not supported
371 */
372/* ARGSUSED */
373static int
374nwfs_quotactl(mp, cmd, uid, arg, td)
375	struct mount *mp;
376	int cmd;
377	uid_t uid;
378	void *arg;
379	struct thread *td;
380{
381	NCPVODEBUG("return EOPNOTSUPP\n");
382	return (EOPNOTSUPP);
383}
384
385/*ARGSUSED*/
386int
387nwfs_init(struct vfsconf *vfsp)
388{
389	nwfs_hash_init();
390	nwfs_pbuf_freecnt = nswbuf / 2 + 1;
391	NCPVODEBUG("always happy to load!\n");
392	return (0);
393}
394
395/*ARGSUSED*/
396int
397nwfs_uninit(struct vfsconf *vfsp)
398{
399
400	nwfs_hash_free();
401	NCPVODEBUG("unloaded\n");
402	return (0);
403}
404
405/*
406 * nwfs_statfs call
407 */
408int
409nwfs_statfs(mp, sbp, td)
410	struct mount *mp;
411	struct statfs *sbp;
412	struct thread *td;
413{
414	struct nwmount *nmp = VFSTONWFS(mp);
415	int error = 0, secsize;
416	struct nwnode *np = nmp->n_root;
417	struct ncp_volume_info vi;
418
419	if (np == NULL) return EINVAL;
420	error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
421	    nmp->n_volume, &vi, td, td->td_ucred);
422	if (error) return error;
423	secsize = 512;			/* XXX how to get real value ??? */
424	/* fundamental filesystem block size */
425	sbp->f_bsize = vi.sectors_per_block*secsize;
426	/* optimal transfer block size */
427	sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
428	/* total data blocks in filesystem */
429	sbp->f_blocks= vi.total_blocks;
430	/* free blocks in fs */
431	sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
432	/* free blocks avail to non-superuser */
433	sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
434	/* total file nodes in filesystem */
435	sbp->f_files = vi.total_dir_entries;
436	/* free file nodes in fs */
437	sbp->f_ffree = vi.available_dir_entries;
438	sbp->f_flags = 0;		/* copy of mount exported flags */
439	return 0;
440}
441