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
32 * $FreeBSD: src/sys/nwfs/nwfs_vfsops.c,v 1.6.2.6 2001/10/25 19:18:54 dillon Exp $
33 * $DragonFly: src/sys/vfs/nwfs/nwfs_vfsops.c,v 1.3 2003/06/23 17:55:48 dillon Exp $
37 #error "NWFS requires NCP protocol"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
48 #include <sys/malloc.h>
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>
57 #include <nwfs/nwfs.h>
58 #include <nwfs/nwfs_node.h>
59 #include <nwfs/nwfs_subr.h>
61 int nwfs_debuglevel = 0;
63 static int nwfs_version = NWFS_VERSION;
65 SYSCTL_DECL(_vfs_nwfs);
66 SYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware file system");
67 SYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, "");
68 SYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, "");
70 static int nwfs_mount(struct mount *, char *, caddr_t,
71 struct nameidata *, struct proc *);
72 static int nwfs_quotactl(struct mount *, int, uid_t, caddr_t, struct proc *);
73 static int nwfs_root(struct mount *, struct vnode **);
74 static int nwfs_start(struct mount *, int, struct proc *);
75 static int nwfs_statfs(struct mount *, struct statfs *, struct proc *);
76 static int nwfs_sync(struct mount *, int, struct ucred *, struct proc *);
77 static int nwfs_unmount(struct mount *, int, struct proc *);
78 static int nwfs_init(struct vfsconf *vfsp);
79 static int nwfs_uninit(struct vfsconf *vfsp);
81 static struct vfsops nwfs_vfsops = {
90 vfs_stdfhtovp, /* shouldn't happen */
92 vfs_stdvptofh, /* shouldn't happen */
99 VFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK);
101 int nwfs_pbuf_freecnt = -1; /* start out unlimited */
102 static int nwfsid = 1;
105 nwfs_initnls(struct nwmount *nmp) {
108 #define COPY_TABLE(t,d) { \
110 error = copyin((t), pc, 256); \
114 (t) = pc; pc += 256; \
117 nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS;
118 if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) {
119 nmp->m.nls.to_lower = ncp_defnls.to_lower;
120 nmp->m.nls.to_upper = ncp_defnls.to_upper;
121 nmp->m.nls.n2u = ncp_defnls.n2u;
122 nmp->m.nls.u2n = ncp_defnls.u2n;
125 MALLOC(pe, char *, 256 * 4, M_NWFSDATA, M_WAITOK);
128 COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower);
129 COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper);
130 COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u);
131 COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n);
134 free(pe, M_NWFSDATA);
140 * mp - path - addr in user space of mount point (ie /usr or whatever)
141 * data - addr in user space of mount params
143 static int nwfs_mount(struct mount *mp, char *path, caddr_t data,
144 struct nameidata *ndp, struct proc *p)
146 struct nwfs_args args; /* will hold data from mount request */
149 struct nwmount *nmp = NULL;
150 struct ncp_conn *conn = NULL;
151 struct ncp_handle *handle = NULL;
156 nwfs_printf("missing data argument\n");
159 if (mp->mnt_flag & MNT_UPDATE) {
160 nwfs_printf("MNT_UPDATE not implemented");
163 error = copyin(data, (caddr_t)&args, sizeof(struct nwfs_args));
166 if (args.version != NWFS_VERSION) {
167 nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version);
170 error = ncp_conn_getbyref(args.connRef,p,p->p_ucred,NCPM_EXECUTE,&conn);
172 nwfs_printf("invalid connection refernce %d\n",args.connRef);
175 error = ncp_conn_gethandle(conn, NULL, &handle);
177 nwfs_printf("can't get connection handle\n");
180 ncp_conn_unlock(conn,p); /* we keep the ref */
181 mp->mnt_stat.f_iosize = conn->buffer_size;
182 /* We must malloc our own mount info */
183 MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA,M_USE_RESERVE | M_ZERO);
185 nwfs_printf("could not alloc nwmount\n");
189 mp->mnt_data = (qaddr_t)nmp;
192 nmp->n_id = nwfsid++;
194 nmp->m.file_mode = (nmp->m.file_mode &
195 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
196 nmp->m.dir_mode = (nmp->m.dir_mode &
197 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
198 if ((error = nwfs_initnls(nmp)) != 0) goto bad;
199 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
200 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
201 pc = mp->mnt_stat.f_mntfromname;
202 pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
205 pc = index(strncpy(pc, conn->li.server, pe-pc-2),0);
208 pc=index(strncpy(pc, conn->li.user, pe-pc-2),0);
211 strncpy(pc, nmp->m.mounted_vol, pe-pc-2);
214 /* protect against invalid mount points */
215 nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0';
217 error = nwfs_root(mp, &vp);
221 * Lose the lock but keep the ref.
223 VOP_UNLOCK(vp, 0, curproc);
224 NCPVODEBUG("rootvp.vrefcnt=%d\n",vp->v_usecount);
228 free(nmp, M_NWFSDATA);
230 ncp_conn_puthandle(handle, NULL, 0);
234 /* Unmount the filesystem described by mp. */
236 nwfs_unmount(struct mount *mp, int mntflags, struct proc *p)
238 struct nwmount *nmp = VFSTONWFS(mp);
239 struct ncp_conn *conn;
242 NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags);
244 if (mntflags & MNT_FORCE)
246 /* There is 1 extra root vnode reference from nwfs_mount(). */
247 error = vflush(mp, 1, flags);
250 conn = NWFSTOCONN(nmp);
251 ncp_conn_puthandle(nmp->connh,NULL,0);
252 if (ncp_conn_lock(conn,p,p->p_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) {
253 if(ncp_disconnect(conn))
254 ncp_conn_unlock(conn,p);
256 mp->mnt_data = (qaddr_t)0;
257 if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS)
258 free(nmp->m.nls.to_lower, M_NWFSDATA);
259 free(nmp, M_NWFSDATA);
260 mp->mnt_flag &= ~MNT_LOCAL;
264 /* Return locked vnode to root of a filesystem */
266 nwfs_root(struct mount *mp, struct vnode **vpp) {
270 struct ncp_conn *conn;
271 struct nw_entry_info fattr;
272 struct proc *p = curproc;
273 struct ucred *cred = p->p_ucred;
278 conn = NWFSTOCONN(nmp);
280 *vpp = NWTOV(nmp->n_root);
281 while (vget(*vpp, LK_EXCLUSIVE, curproc) != 0)
285 error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol,
286 &nmp->n_rootent.f_id, p, cred);
290 error = ncp_get_namespaces(conn, vol, &nsf, p, cred);
293 if (nsf & NW_NSB_OS2) {
294 NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol);
295 if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) {
296 nmp->name_space = NW_NS_OS2;
297 nmp->m.nls.opt &= ~NWHP_DOS;
300 opt = nmp->m.nls.opt;
301 nsf = opt & (NWHP_UPPER | NWHP_LOWER);
302 if (opt & NWHP_DOS) {
303 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
304 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
305 } else if (nsf == 0) {
306 nmp->m.nls.opt |= NWHP_LOWER;
309 if (nsf == (NWHP_UPPER | NWHP_LOWER)) {
310 nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER);
313 if (nmp->m.root_path[0]) {
314 nmp->m.root_path[0]--;
315 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
316 -nmp->m.root_path[0], nmp->m.root_path, &fattr, p, cred);
318 NCPFATAL("Invalid root path specified\n");
321 nmp->n_rootent.f_parent = fattr.dirEntNum;
322 nmp->m.root_path[0]++;
323 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
324 -nmp->m.root_path[0], nmp->m.root_path, &fattr, p, cred);
326 NCPFATAL("Invalid root path specified\n");
329 nmp->n_rootent.f_id = fattr.dirEntNum;
331 error = ncp_obtain_info(nmp, nmp->n_rootent.f_id,
332 0, NULL, &fattr, p, cred);
334 NCPFATAL("Can't obtain volume info\n");
337 fattr.nameLen = strlen(strcpy(fattr.entryName, NWFS_ROOTVOL));
338 nmp->n_rootent.f_parent = nmp->n_rootent.f_id;
340 error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp);
345 if (nmp->m.root_path[0] == 0)
346 np->n_flag |= NVOLUME;
348 /* error = VOP_GETATTR(vp, &vattr, cred, p);
351 NCPFATAL("Can't get root directory entry\n");
359 * Vfs start routine, a no-op.
363 nwfs_start(mp, flags, p)
368 NCPVODEBUG("flags=%04x\n",flags);
373 * Do operations associated with quotas, not supported
377 nwfs_quotactl(mp, cmd, uid, arg, p)
384 NCPVODEBUG("return EOPNOTSUPP\n");
390 nwfs_init(struct vfsconf *vfsp)
394 int olen, ncpu, plen, error;
398 error = kernel_sysctl(name, 2, &ncpu, &olen, NULL, 0, &plen);
399 if (error == 0 && ncpu > 1)
400 printf("warning: nwfs module compiled without SMP support.");
403 nwfs_pbuf_freecnt = nswbuf / 2 + 1;
404 NCPVODEBUG("always happy to load!\n");
410 nwfs_uninit(struct vfsconf *vfsp)
414 NCPVODEBUG("unloaded\n");
422 nwfs_statfs(mp, sbp, p)
427 struct nwmount *nmp = VFSTONWFS(mp);
428 int error = 0, secsize;
429 struct nwnode *np = nmp->n_root;
430 struct ncp_volume_info vi;
432 if (np == NULL) return EINVAL;
433 error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp), nmp->n_volume, &vi,p,p->p_ucred);
434 if (error) return error;
435 secsize = 512; /* XXX how to get real value ??? */
436 sbp->f_spare2=0; /* placeholder */
437 /* fundamental file system block size */
438 sbp->f_bsize = vi.sectors_per_block*secsize;
439 /* optimal transfer block size */
440 sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size;
441 /* total data blocks in file system */
442 sbp->f_blocks= vi.total_blocks;
443 /* free blocks in fs */
444 sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks;
445 /* free blocks avail to non-superuser */
446 sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks;
447 /* total file nodes in file system */
448 sbp->f_files = vi.total_dir_entries;
449 /* free file nodes in fs */
450 sbp->f_ffree = vi.available_dir_entries;
451 sbp->f_flags = 0; /* copy of mount exported flags */
452 if (sbp != &mp->mnt_stat) {
453 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */
454 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */
455 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */
456 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
457 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
459 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
464 * Flush out the buffer cache
468 nwfs_sync(mp, waitfor, cred, p)
475 int error, allerror = 0;
477 * Force stale buffer cache information to be flushed.
480 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
482 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
484 * If the vnode that we are about to sync is no longer
485 * associated with this mount point, start over.
487 if (vp->v_mount != mp)
489 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
492 if (vget(vp, LK_EXCLUSIVE, p))
494 error = VOP_FSYNC(vp, cred, waitfor, p);