2 * Copyright (c) 1999, 2000 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/sysctl.h>
34 #include <sys/vnode.h>
35 #include <sys/mount.h>
37 #include <sys/malloc.h>
41 #include <netncp/ncp.h>
42 #include <netncp/ncp_conn.h>
43 #include <netncp/ncp_subr.h>
44 #include <netncp/ncp_ncp.h>
45 #include <netncp/ncp_nls.h>
47 #include <fs/nwfs/nwfs.h>
48 #include <fs/nwfs/nwfs_node.h>
49 #include <fs/nwfs/nwfs_subr.h>
51 int nwfs_debuglevel = 0;
53 static int nwfs_version = NWFS_VERSION;
55 SYSCTL_DECL(_vfs_nwfs);
56 SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware filesystem");
57 SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
58 SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
60 MODULE_DEPEND(nwfs, ncp, 1, 1, 1);
61 MODULE_DEPEND(nwfs, libmchain, 1, 1, 1);
63 static vfs_cmount_t nwfs_cmount;
64 static vfs_mount_t nwfs_mount;
65 static vfs_quotactl_t nwfs_quotactl;
66 static vfs_root_t nwfs_root;
67 static vfs_statfs_t nwfs_statfs;
68 static vfs_unmount_t nwfs_unmount;
69 static vfs_init_t nwfs_init;
70 static vfs_uninit_t nwfs_uninit;
72 static struct vfsops nwfs_vfsops = {
73 .vfs_init = nwfs_init,
74 .vfs_mount = nwfs_mount,
75 .vfs_cmount = nwfs_cmount,
76 .vfs_quotactl = nwfs_quotactl,
77 .vfs_root = nwfs_root,
78 .vfs_statfs = nwfs_statfs,
79 .vfs_sync = vfs_stdsync,
80 .vfs_uninit = nwfs_uninit,
81 .vfs_unmount = nwfs_unmount,
85 VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
87 int nwfs_pbuf_freecnt = -1; /* start out unlimited */
88 static int nwfsid = 1;
91 nwfs_initnls(struct nwmount *nmp) {
94 #define COPY_TABLE(t,d) { \
96 error = copyin((t), pc, 256); \
100 (t) = pc; pc += 256; \
103 nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
104 if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
105 nmp->m.nls.to_lower = ncp_defnls.to_lower;
106 nmp->m.nls.to_upper = ncp_defnls.to_upper;
107 nmp->m.nls.n2u = ncp_defnls.n2u;
108 nmp->m.nls.u2n = ncp_defnls.u2n;
111 pe = malloc(256 * 4, M_NWFSDATA, M_WAITOK);
114 COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
115 COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
116 COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
117 COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
120 free(pe, M_NWFSDATA);
126 static int nwfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
128 struct nwfs_args args; /* will hold data from mount request */
131 error = copyin(data, &args, sizeof(struct nwfs_args));
136 * XXX: cheap cop-out here, args contains a structure I don't
137 * XXX: know how we should handle, and I don't see any immediate
138 * XXX: prospect of avoiding a mount_nwfs(8) binary anyway.
140 ma = mount_arg(ma, "nwfs_args", &args, sizeof args);
142 error = kernel_mount(ma, flags);
148 * mp - path - addr in user space of mount point (ie /usr or whatever)
149 * data - addr in user space of mount params
151 static int nwfs_mount(struct mount *mp)
153 struct nwfs_args args; /* will hold data from mount request */
155 struct nwmount *nmp = NULL;
156 struct ncp_conn *conn = NULL;
157 struct ncp_handle *handle = NULL;
163 if (mp->mnt_flag & MNT_ROOTFS)
165 if (mp->mnt_flag & MNT_UPDATE) {
166 nwfs_printf("MNT_UPDATE not implemented");
169 error = vfs_copyopt(mp->mnt_optnew, "nwfs_args", &args, sizeof args);
172 if (args.version != NWFS_VERSION) {
173 nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
176 error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn);
178 nwfs_printf("invalid connection refernce %d\n",args.connRef);
181 error = ncp_conn_gethandle(conn, NULL, &handle);
183 nwfs_printf("can't get connection handle\n");
186 ncp_conn_unlock(conn, td); /* we keep the ref */
187 mp->mnt_stat.f_iosize = conn->buffer_size;
188 /* We must malloc our own mount info */
189 nmp = malloc(sizeof(struct nwmount), M_NWFSDATA, M_WAITOK | M_ZERO);
191 nwfs_printf("could not alloc nwmount\n");
198 nmp->n_id = nwfsid++;
200 nmp->m.file_mode = (nmp->m.file_mode &
201 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
202 nmp->m.dir_mode = (nmp->m.dir_mode &
203 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
204 if ((error = nwfs_initnls(nmp)) != 0) goto bad;
205 pc = mp->mnt_stat.f_mntfromname;
206 pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
209 pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
212 pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
215 strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
218 /* protect against invalid mount points */
219 nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
221 error = nwfs_root(mp, LK_EXCLUSIVE, &vp);
225 * Lose the lock but keep the ref.
228 NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp));
232 free(nmp, M_NWFSDATA);
234 ncp_conn_puthandle(handle, NULL, 0);
238 /* Unmount the filesystem described by mp. */
240 nwfs_unmount(struct mount *mp, int mntflags)
243 struct nwmount *nmp = VFSTONWFS(mp);
244 struct ncp_conn *conn;
247 NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
250 if (mntflags & MNT_FORCE)
252 /* There is 1 extra root vnode reference from nwfs_mount(). */
253 error = vflush(mp, 1, flags, td);
256 conn = NWFSTOCONN(nmp);
257 ncp_conn_puthandle(nmp->connh,NULL,0);
258 if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
259 if(ncp_conn_free(conn))
260 ncp_conn_unlock(conn, td);
263 if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
264 free(nmp->m.nls.to_lower, M_NWFSDATA);
265 free(nmp, M_NWFSDATA);
267 mp->mnt_flag &= ~MNT_LOCAL;
272 /* Return locked vnode to root of a filesystem */
274 nwfs_root(struct mount *mp, int flags, struct vnode **vpp) {
278 struct ncp_conn *conn;
279 struct nw_entry_info fattr;
289 conn = NWFSTOCONN(nmp);
291 *vpp = NWTOV(nmp->n_root);
292 while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
296 error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
297 &nmp->n_rootent.f_id, td, cred);
301 error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
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;
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;
320 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
321 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
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);
329 NCPFATAL("Invalid root path specified\n");
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);
337 NCPFATAL("Invalid root path specified\n");
340 nmp->n_rootent.f_id = fattr.dirEntNum;
342 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
343 0, NULL, &fattr, td, cred);
345 NCPFATAL("Can't obtain volume info\n");
348 fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
349 nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
351 error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
354 vp->v_vflag |= VV_ROOT;
356 if (nmp->m.root_path[0] == 0)
357 np->n_flag |= NVOLUME;
359 /* error = VOP_GETATTR(vp, &vattr, cred, td);
362 NCPFATAL("Can't get root directory entry\n");
370 * Do operations associated with quotas, not supported
374 nwfs_quotactl(mp, cmd, uid, arg)
380 NCPVODEBUG("return EOPNOTSUPP\n");
386 nwfs_init(struct vfsconf *vfsp)
389 nwfs_pbuf_freecnt = nswbuf / 2 + 1;
390 NCPVODEBUG("always happy to load!\n");
396 nwfs_uninit(struct vfsconf *vfsp)
400 NCPVODEBUG("unloaded\n");
412 struct nwmount *nmp = VFSTONWFS(mp);
413 struct thread *td = curthread;
414 int error = 0, secsize;
415 struct nwnode *np = nmp->n_root;
416 struct ncp_volume_info vi;
418 if (np == NULL) return EINVAL;
419 error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
420 nmp->n_volume, &vi, td, td->td_ucred);
421 if (error) return error;
422 secsize = 512; /* XXX how to get real value ??? */
423 /* fundamental filesystem block size */
424 sbp->f_bsize = vi.sectors_per_block*secsize;
425 /* optimal transfer block size */
426 sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
427 /* total data blocks in filesystem */
428 sbp->f_blocks= vi.total_blocks;
429 /* free blocks in fs */
430 sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
431 /* free blocks avail to non-superuser */
432 sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
433 /* total file nodes in filesystem */
434 sbp->f_files = vi.total_dir_entries;
435 /* free file nodes in fs */
436 sbp->f_ffree = vi.available_dir_entries;
437 sbp->f_flags = 0; /* copy of mount exported flags */