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.
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.
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
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/vnode.h>
41 #include <sys/mount.h>
43 #include <sys/malloc.h>
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>
53 #include <fs/nwfs/nwfs.h>
54 #include <fs/nwfs/nwfs_node.h>
55 #include <fs/nwfs/nwfs_subr.h>
57 int nwfs_debuglevel = 0;
59 static int nwfs_version = NWFS_VERSION;
61 SYSCTL_DECL(_vfs_nwfs);
62 SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware filesystem");
63 SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
64 SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
66 MODULE_DEPEND(nwfs, ncp, 1, 1, 1);
67 MODULE_DEPEND(nwfs, libmchain, 1, 1, 1);
69 static vfs_cmount_t nwfs_cmount;
70 static vfs_mount_t nwfs_mount;
71 static vfs_quotactl_t nwfs_quotactl;
72 static vfs_root_t nwfs_root;
73 static vfs_statfs_t nwfs_statfs;
74 static vfs_unmount_t nwfs_unmount;
75 static vfs_init_t nwfs_init;
76 static vfs_uninit_t nwfs_uninit;
78 static 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,
91 VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
93 int nwfs_pbuf_freecnt = -1; /* start out unlimited */
94 static int nwfsid = 1;
97 nwfs_initnls(struct nwmount *nmp) {
100 #define COPY_TABLE(t,d) { \
102 error = copyin((t), pc, 256); \
106 (t) = pc; pc += 256; \
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;
117 MALLOC(pe, char *, 256 * 4, M_NWFSDATA, M_WAITOK);
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);
126 free(pe, M_NWFSDATA);
132 static int nwfs_cmount(struct mntarg *ma, void *data, int flags,
135 struct nwfs_args args; /* will hold data from mount request */
138 error = copyin(data, &args, sizeof(struct nwfs_args));
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.
147 ma = mount_arg(ma, "nwfs_args", &args, sizeof args);
149 error = kernel_mount(ma, flags);
155 * mp - path - addr in user space of mount point (ie /usr or whatever)
156 * data - addr in user space of mount params
158 static int nwfs_mount(struct mount *mp, struct thread *td)
160 struct nwfs_args args; /* will hold data from mount request */
162 struct nwmount *nmp = NULL;
163 struct ncp_conn *conn = NULL;
164 struct ncp_handle *handle = NULL;
168 if (mp->mnt_flag & MNT_ROOTFS)
170 if (mp->mnt_flag & MNT_UPDATE) {
171 nwfs_printf("MNT_UPDATE not implemented");
174 error = vfs_copyopt(mp->mnt_optnew, "nwfs_args", &args, sizeof args);
177 if (args.version != NWFS_VERSION) {
178 nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
181 error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn);
183 nwfs_printf("invalid connection refernce %d\n",args.connRef);
186 error = ncp_conn_gethandle(conn, NULL, &handle);
188 nwfs_printf("can't get connection handle\n");
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);
197 nwfs_printf("could not alloc nwmount\n");
201 mp->mnt_data = (qaddr_t)nmp;
204 nmp->n_id = nwfsid++;
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);
215 pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
218 pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
221 strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
224 /* protect against invalid mount points */
225 nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
227 error = nwfs_root(mp, LK_EXCLUSIVE, &vp, td);
231 * Lose the lock but keep the ref.
233 VOP_UNLOCK(vp, 0, curthread);
234 NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp));
238 free(nmp, M_NWFSDATA);
240 ncp_conn_puthandle(handle, NULL, 0);
244 /* Unmount the filesystem described by mp. */
246 nwfs_unmount(struct mount *mp, int mntflags, struct thread *td)
248 struct nwmount *nmp = VFSTONWFS(mp);
249 struct ncp_conn *conn;
252 NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
254 if (mntflags & MNT_FORCE)
256 /* There is 1 extra root vnode reference from nwfs_mount(). */
257 error = vflush(mp, 1, flags, td);
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);
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 mp->mnt_flag &= ~MNT_LOCAL;
274 /* Return locked vnode to root of a filesystem */
276 nwfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) {
280 struct ncp_conn *conn;
281 struct nw_entry_info fattr;
282 struct ucred *cred = td->td_ucred;
287 conn = NWFSTOCONN(nmp);
289 *vpp = NWTOV(nmp->n_root);
290 while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0)
294 error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
295 &nmp->n_rootent.f_id, td, cred);
299 error = ncp_get_namespaces(conn, vol, &nsf, td, cred);
302 if (nsf & NW_NSB_OS2) {
303 NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
304 if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
305 nmp->name_space = NW_NS_OS2;
306 nmp->m.nls.opt &= ~NWHP_DOS;
309 opt = nmp->m.nls.opt;
310 nsf = opt & (NWHP_UPPER | NWHP_LOWER);
311 if (opt & NWHP_DOS) {
312 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
313 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
314 } else if (nsf == 0) {
315 nmp->m.nls.opt |= NWHP_LOWER;
318 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
319 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
322 if (nmp->m.root_path[0]) {
323 nmp->m.root_path[0]--;
324 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
325 -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
327 NCPFATAL("Invalid root path specified\n");
330 nmp->n_rootent.f_parent = fattr.dirEntNum;
331 nmp->m.root_path[0]++;
332 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
333 -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred);
335 NCPFATAL("Invalid root path specified\n");
338 nmp->n_rootent.f_id = fattr.dirEntNum;
340 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
341 0, NULL, &fattr, td, cred);
343 NCPFATAL("Can't obtain volume info\n");
346 fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT"));
347 nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
349 error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
352 vp->v_vflag |= VV_ROOT;
354 if (nmp->m.root_path[0] == 0)
355 np->n_flag |= NVOLUME;
357 /* error = VOP_GETATTR(vp, &vattr, cred, td);
360 NCPFATAL("Can't get root directory entry\n");
368 * Do operations associated with quotas, not supported
372 nwfs_quotactl(mp, cmd, uid, arg, td)
379 NCPVODEBUG("return EOPNOTSUPP\n");
385 nwfs_init(struct vfsconf *vfsp)
388 nwfs_pbuf_freecnt = nswbuf / 2 + 1;
389 NCPVODEBUG("always happy to load!\n");
395 nwfs_uninit(struct vfsconf *vfsp)
399 NCPVODEBUG("unloaded\n");
407 nwfs_statfs(mp, sbp, td)
412 struct nwmount *nmp = VFSTONWFS(mp);
413 int error = 0, secsize;
414 struct nwnode *np = nmp->n_root;
415 struct ncp_volume_info vi;
417 if (np == NULL) return EINVAL;
418 error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp),
419 nmp->n_volume, &vi, td, td->td_ucred);
420 if (error) return error;
421 secsize = 512; /* XXX how to get real value ??? */
422 /* fundamental filesystem block size */
423 sbp->f_bsize = vi.sectors_per_block*secsize;
424 /* optimal transfer block size */
425 sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
426 /* total data blocks in filesystem */
427 sbp->f_blocks= vi.total_blocks;
428 /* free blocks in fs */
429 sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
430 /* free blocks avail to non-superuser */
431 sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
432 /* total file nodes in filesystem */
433 sbp->f_files = vi.total_dir_entries;
434 /* free file nodes in fs */
435 sbp->f_ffree = vi.available_dir_entries;
436 sbp->f_flags = 0; /* copy of mount exported flags */