nwfs_vfsops.c revision 191990
1139776Simp/*- 266539Sbp * Copyright (c) 1999, 2000 Boris Popov 351852Sbp * All rights reserved. 451852Sbp * 551852Sbp * Redistribution and use in source and binary forms, with or without 651852Sbp * modification, are permitted provided that the following conditions 751852Sbp * are met: 851852Sbp * 1. Redistributions of source code must retain the above copyright 951852Sbp * notice, this list of conditions and the following disclaimer. 1051852Sbp * 2. Redistributions in binary form must reproduce the above copyright 1151852Sbp * notice, this list of conditions and the following disclaimer in the 1251852Sbp * documentation and/or other materials provided with the distribution. 1351852Sbp * 3. All advertising materials mentioning features or use of this software 1451852Sbp * must display the following acknowledgement: 1551852Sbp * This product includes software developed by Boris Popov. 1651852Sbp * 4. Neither the name of the author nor the names of any co-contributors 1751852Sbp * may be used to endorse or promote products derived from this software 1851852Sbp * without specific prior written permission. 1951852Sbp * 2051852Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2151852Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2251852Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2351852Sbp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2451852Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2551852Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2651852Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2751852Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2851852Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2951852Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3051852Sbp * SUCH DAMAGE. 3151852Sbp * 3251852Sbp * $FreeBSD: head/sys/fs/nwfs/nwfs_vfsops.c 191990 2009-05-11 15:33:26Z attilio $ 3351852Sbp */ 3451852Sbp 3551852Sbp#include <sys/param.h> 3651852Sbp#include <sys/systm.h> 3751852Sbp#include <sys/proc.h> 3851852Sbp#include <sys/kernel.h> 3951852Sbp#include <sys/sysctl.h> 4051852Sbp#include <sys/vnode.h> 4151852Sbp#include <sys/mount.h> 4251852Sbp#include <sys/stat.h> 4351852Sbp#include <sys/malloc.h> 4460041Sphk#include <sys/bio.h> 4551852Sbp#include <sys/buf.h> 4651852Sbp 4751852Sbp#include <netncp/ncp.h> 4851852Sbp#include <netncp/ncp_conn.h> 4951852Sbp#include <netncp/ncp_subr.h> 5051852Sbp#include <netncp/ncp_ncp.h> 5151852Sbp#include <netncp/ncp_nls.h> 5251852Sbp 5377223Sru#include <fs/nwfs/nwfs.h> 5477223Sru#include <fs/nwfs/nwfs_node.h> 5577223Sru#include <fs/nwfs/nwfs_subr.h> 5651852Sbp 5751852Sbpint nwfs_debuglevel = 0; 5851852Sbp 5951852Sbpstatic int nwfs_version = NWFS_VERSION; 6051852Sbp 6151852SbpSYSCTL_DECL(_vfs_nwfs); 6296755StrhodesSYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware filesystem"); 6351852SbpSYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, ""); 6451852SbpSYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, ""); 6551852Sbp 6659755SpeterMODULE_DEPEND(nwfs, ncp, 1, 1, 1); 6774637SbpMODULE_DEPEND(nwfs, libmchain, 1, 1, 1); 6859755Speter 69138486Sphkstatic vfs_cmount_t nwfs_cmount; 70138486Sphkstatic vfs_mount_t nwfs_mount; 71116271Sphkstatic vfs_quotactl_t nwfs_quotactl; 72116271Sphkstatic vfs_root_t nwfs_root; 73116271Sphkstatic vfs_statfs_t nwfs_statfs; 74116271Sphkstatic vfs_unmount_t nwfs_unmount; 75116271Sphkstatic vfs_init_t nwfs_init; 76116271Sphkstatic vfs_uninit_t nwfs_uninit; 7751852Sbp 7851852Sbpstatic struct vfsops nwfs_vfsops = { 79116271Sphk .vfs_init = nwfs_init, 80138486Sphk .vfs_mount = nwfs_mount, 81138486Sphk .vfs_cmount = nwfs_cmount, 82116271Sphk .vfs_quotactl = nwfs_quotactl, 83116271Sphk .vfs_root = nwfs_root, 84116271Sphk .vfs_statfs = nwfs_statfs, 85116271Sphk .vfs_sync = vfs_stdsync, 86116271Sphk .vfs_uninit = nwfs_uninit, 87116271Sphk .vfs_unmount = nwfs_unmount, 8851852Sbp}; 8951852Sbp 9051852Sbp 9151852SbpVFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK); 9251852Sbp 9351852Sbpint nwfs_pbuf_freecnt = -1; /* start out unlimited */ 9451852Sbpstatic int nwfsid = 1; 9551852Sbp 9651852Sbpstatic int 9751852Sbpnwfs_initnls(struct nwmount *nmp) { 9851852Sbp char *pc, *pe; 9951852Sbp int error = 0; 10051852Sbp#define COPY_TABLE(t,d) { \ 10151852Sbp if (t) { \ 10251852Sbp error = copyin((t), pc, 256); \ 10351852Sbp if (error) break; \ 10451852Sbp } else \ 10551852Sbp bcopy(d, pc, 256); \ 10651852Sbp (t) = pc; pc += 256; \ 10751852Sbp } 10851852Sbp 10951852Sbp nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS; 11051852Sbp if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) { 11152814Sarchie nmp->m.nls.to_lower = ncp_defnls.to_lower; 11252814Sarchie nmp->m.nls.to_upper = ncp_defnls.to_upper; 11351852Sbp nmp->m.nls.n2u = ncp_defnls.n2u; 11451852Sbp nmp->m.nls.u2n = ncp_defnls.u2n; 11551852Sbp return 0; 11651852Sbp } 117184205Sdes pe = malloc(256 * 4, M_NWFSDATA, M_WAITOK); 11851852Sbp pc = pe; 11951852Sbp do { 12052814Sarchie COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower); 12152814Sarchie COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper); 12251852Sbp COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u); 12351852Sbp COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n); 12451852Sbp } while(0); 12551852Sbp if (error) { 12651852Sbp free(pe, M_NWFSDATA); 12751852Sbp return error; 12851852Sbp } 12951852Sbp return 0; 13051852Sbp} 131138486Sphk 132191990Sattiliostatic int nwfs_cmount(struct mntarg *ma, void *data, int flags) 133138486Sphk{ 134138486Sphk struct nwfs_args args; /* will hold data from mount request */ 135138486Sphk int error; 136138486Sphk 137153400Sdes error = copyin(data, &args, sizeof(struct nwfs_args)); 138138486Sphk if (error) 139138486Sphk return (error); 140138486Sphk 141138486Sphk /* 142138486Sphk * XXX: cheap cop-out here, args contains a structure I don't 143138486Sphk * XXX: know how we should handle, and I don't see any immediate 144138486Sphk * XXX: prospect of avoiding a mount_nwfs(8) binary anyway. 145138486Sphk */ 146138486Sphk ma = mount_arg(ma, "nwfs_args", &args, sizeof args); 147138486Sphk 148138486Sphk error = kernel_mount(ma, flags); 149138486Sphk 150138486Sphk return (error); 151138486Sphk} 152138486Sphk 15351852Sbp/* 15451852Sbp * mp - path - addr in user space of mount point (ie /usr or whatever) 15551852Sbp * data - addr in user space of mount params 15651852Sbp */ 157191990Sattiliostatic int nwfs_mount(struct mount *mp) 15851852Sbp{ 15951852Sbp struct nwfs_args args; /* will hold data from mount request */ 16051852Sbp int error; 16151852Sbp struct nwmount *nmp = NULL; 16251852Sbp struct ncp_conn *conn = NULL; 16351852Sbp struct ncp_handle *handle = NULL; 16451852Sbp struct vnode *vp; 165191990Sattilio struct thread *td; 16651852Sbp char *pc,*pe; 16751852Sbp 168191990Sattilio td = curthread; 169137479Sphk if (mp->mnt_flag & MNT_ROOTFS) 170137479Sphk return (EOPNOTSUPP); 17151852Sbp if (mp->mnt_flag & MNT_UPDATE) { 17251852Sbp nwfs_printf("MNT_UPDATE not implemented"); 17351852Sbp return (EOPNOTSUPP); 17451852Sbp } 175150761Sphk error = vfs_copyopt(mp->mnt_optnew, "nwfs_args", &args, sizeof args); 17651852Sbp if (error) 17751852Sbp return (error); 17851852Sbp if (args.version != NWFS_VERSION) { 17951852Sbp nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version); 18051852Sbp return (1); 18151852Sbp } 18291406Sjhb error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn); 18351852Sbp if (error) { 18451852Sbp nwfs_printf("invalid connection refernce %d\n",args.connRef); 18551852Sbp return (error); 18651852Sbp } 18751852Sbp error = ncp_conn_gethandle(conn, NULL, &handle); 18851852Sbp if (error) { 18951852Sbp nwfs_printf("can't get connection handle\n"); 19051852Sbp return (error); 19151852Sbp } 19283366Sjulian ncp_conn_unlock(conn, td); /* we keep the ref */ 19351852Sbp mp->mnt_stat.f_iosize = conn->buffer_size; 19451852Sbp /* We must malloc our own mount info */ 195184205Sdes nmp = malloc(sizeof(struct nwmount),M_NWFSDATA, 196112916Stjr M_WAITOK | M_USE_RESERVE | M_ZERO); 19751852Sbp if (nmp == NULL) { 19851852Sbp nwfs_printf("could not alloc nwmount\n"); 19951852Sbp error = ENOMEM; 20051852Sbp goto bad; 20151852Sbp } 202172697Salfred mp->mnt_data = nmp; 20351852Sbp nmp->connh = handle; 20451852Sbp nmp->n_root = NULL; 20551852Sbp nmp->n_id = nwfsid++; 20651852Sbp nmp->m = args; 20751852Sbp nmp->m.file_mode = (nmp->m.file_mode & 20851852Sbp (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 20951852Sbp nmp->m.dir_mode = (nmp->m.dir_mode & 21051852Sbp (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 21151852Sbp if ((error = nwfs_initnls(nmp)) != 0) goto bad; 21251852Sbp pc = mp->mnt_stat.f_mntfromname; 21351852Sbp pe = pc+sizeof(mp->mnt_stat.f_mntfromname); 21451852Sbp bzero(pc, MNAMELEN); 21551852Sbp *(pc++) = '/'; 21651852Sbp pc = index(strncpy(pc, conn->li.server, pe-pc-2),0); 21751852Sbp if (pc < pe-1) { 21851852Sbp *(pc++) = ':'; 21951852Sbp pc=index(strncpy(pc, conn->li.user, pe-pc-2),0); 22051852Sbp if (pc < pe-1) { 22151852Sbp *(pc++) = '/'; 22251852Sbp strncpy(pc, nmp->m.mounted_vol, pe-pc-2); 22351852Sbp } 22451852Sbp } 22551852Sbp /* protect against invalid mount points */ 22651852Sbp nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0'; 22751852Sbp vfs_getnewfsid(mp); 228191990Sattilio error = nwfs_root(mp, LK_EXCLUSIVE, &vp); 22951852Sbp if (error) 23051852Sbp goto bad; 23151852Sbp /* 23251852Sbp * Lose the lock but keep the ref. 23351852Sbp */ 234175294Sattilio VOP_UNLOCK(vp, 0); 235103936Sjeff NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp)); 23651852Sbp return error; 23751852Sbpbad: 23851852Sbp if (nmp) 23951852Sbp free(nmp, M_NWFSDATA); 24051852Sbp if (handle) 24151852Sbp ncp_conn_puthandle(handle, NULL, 0); 24251852Sbp return error; 24351852Sbp} 24451852Sbp 24551852Sbp/* Unmount the filesystem described by mp. */ 24651852Sbpstatic int 247191990Sattilionwfs_unmount(struct mount *mp, int mntflags) 24851852Sbp{ 249191990Sattilio struct thread *td; 25051852Sbp struct nwmount *nmp = VFSTONWFS(mp); 25151852Sbp struct ncp_conn *conn; 25251852Sbp int error, flags; 25351852Sbp 25451852Sbp NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags); 255191990Sattilio td = curthread; 25651852Sbp flags = 0; 25751852Sbp if (mntflags & MNT_FORCE) 25851852Sbp flags |= FORCECLOSE; 25976688Siedowse /* There is 1 extra root vnode reference from nwfs_mount(). */ 260132023Salfred error = vflush(mp, 1, flags, td); 26176688Siedowse if (error) 26251852Sbp return (error); 26351852Sbp conn = NWFSTOCONN(nmp); 26451852Sbp ncp_conn_puthandle(nmp->connh,NULL,0); 26591406Sjhb if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) { 26674062Sbp if(ncp_conn_free(conn)) 26783366Sjulian ncp_conn_unlock(conn, td); 26851852Sbp } 269172697Salfred mp->mnt_data = NULL; 27051852Sbp if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS) 27152814Sarchie free(nmp->m.nls.to_lower, M_NWFSDATA); 27251852Sbp free(nmp, M_NWFSDATA); 273162647Stegge MNT_ILOCK(mp); 27451852Sbp mp->mnt_flag &= ~MNT_LOCAL; 275162647Stegge MNT_IUNLOCK(mp); 27651852Sbp return (error); 27751852Sbp} 27851852Sbp 27951852Sbp/* Return locked vnode to root of a filesystem */ 28051852Sbpstatic int 281191990Sattilionwfs_root(struct mount *mp, int flags, struct vnode **vpp) { 28251852Sbp struct vnode *vp; 28351852Sbp struct nwmount *nmp; 28451852Sbp struct nwnode *np; 28551852Sbp struct ncp_conn *conn; 28651852Sbp struct nw_entry_info fattr; 287191990Sattilio struct thread *td; 288191990Sattilio struct ucred *cred; 28951852Sbp int error, nsf, opt; 29051852Sbp u_char vol; 29151852Sbp 292191990Sattilio td = curthread; 293191990Sattilio cred = td->td_ucred; 294191990Sattilio 29551852Sbp nmp = VFSTONWFS(mp); 29651852Sbp conn = NWFSTOCONN(nmp); 29751852Sbp if (nmp->n_root) { 29851852Sbp *vpp = NWTOV(nmp->n_root); 29983366Sjulian while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0) 30066540Sbp ; 30151852Sbp return 0; 30251852Sbp } 30351852Sbp error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol, 30483366Sjulian &nmp->n_rootent.f_id, td, cred); 30551852Sbp if (error) 30651852Sbp return ENOENT; 30751852Sbp nmp->n_volume = vol; 30883366Sjulian error = ncp_get_namespaces(conn, vol, &nsf, td, cred); 30951852Sbp if (error) 31051852Sbp return ENOENT; 31151852Sbp if (nsf & NW_NSB_OS2) { 31251852Sbp NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol); 31351852Sbp if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) { 31451852Sbp nmp->name_space = NW_NS_OS2; 31551852Sbp nmp->m.nls.opt &= ~NWHP_DOS; 31651852Sbp } 31751852Sbp } 31851852Sbp opt = nmp->m.nls.opt; 31951852Sbp nsf = opt & (NWHP_UPPER | NWHP_LOWER); 32051852Sbp if (opt & NWHP_DOS) { 32151852Sbp if (nsf == (NWHP_UPPER | NWHP_LOWER)) { 32251852Sbp nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER); 32351852Sbp } else if (nsf == 0) { 32451852Sbp nmp->m.nls.opt |= NWHP_LOWER; 32551852Sbp } 32651852Sbp } else { 32751852Sbp if (nsf == (NWHP_UPPER | NWHP_LOWER)) { 32851852Sbp nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER); 32951852Sbp } 33051852Sbp } 33151852Sbp if (nmp->m.root_path[0]) { 33251852Sbp nmp->m.root_path[0]--; 33351852Sbp error = ncp_obtain_info(nmp, nmp->n_rootent.f_id, 33483366Sjulian -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred); 33551852Sbp if (error) { 33651852Sbp NCPFATAL("Invalid root path specified\n"); 33751852Sbp return ENOENT; 33851852Sbp } 33951852Sbp nmp->n_rootent.f_parent = fattr.dirEntNum; 34051852Sbp nmp->m.root_path[0]++; 34151852Sbp error = ncp_obtain_info(nmp, nmp->n_rootent.f_id, 34283366Sjulian -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred); 34351852Sbp if (error) { 34451852Sbp NCPFATAL("Invalid root path specified\n"); 34551852Sbp return ENOENT; 34651852Sbp } 34751852Sbp nmp->n_rootent.f_id = fattr.dirEntNum; 34851852Sbp } else { 34951852Sbp error = ncp_obtain_info(nmp, nmp->n_rootent.f_id, 35083366Sjulian 0, NULL, &fattr, td, cred); 35151852Sbp if (error) { 35251852Sbp NCPFATAL("Can't obtain volume info\n"); 35351852Sbp return ENOENT; 35451852Sbp } 35574064Sbp fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT")); 35651852Sbp nmp->n_rootent.f_parent = nmp->n_rootent.f_id; 35751852Sbp } 35851852Sbp error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp); 35951852Sbp if (error) 36051852Sbp return (error); 361101308Sjeff vp->v_vflag |= VV_ROOT; 36251852Sbp np = VTONW(vp); 36351852Sbp if (nmp->m.root_path[0] == 0) 36451852Sbp np->n_flag |= NVOLUME; 36551852Sbp nmp->n_root = np; 36683366Sjulian/* error = VOP_GETATTR(vp, &vattr, cred, td); 36751852Sbp if (error) { 36851852Sbp vput(vp); 36951852Sbp NCPFATAL("Can't get root directory entry\n"); 37051852Sbp return error; 37151852Sbp }*/ 37251852Sbp *vpp = vp; 37351852Sbp return (0); 37451852Sbp} 37551852Sbp 37651852Sbp/* 37751852Sbp * Do operations associated with quotas, not supported 37851852Sbp */ 37951852Sbp/* ARGSUSED */ 38051852Sbpstatic int 381191990Sattilionwfs_quotactl(mp, cmd, uid, arg) 38251852Sbp struct mount *mp; 38351852Sbp int cmd; 38451852Sbp uid_t uid; 385153400Sdes void *arg; 38651852Sbp{ 38751852Sbp NCPVODEBUG("return EOPNOTSUPP\n"); 38851852Sbp return (EOPNOTSUPP); 38951852Sbp} 39051852Sbp 39151852Sbp/*ARGSUSED*/ 39251852Sbpint 39351852Sbpnwfs_init(struct vfsconf *vfsp) 39451852Sbp{ 39551852Sbp nwfs_hash_init(); 39651852Sbp nwfs_pbuf_freecnt = nswbuf / 2 + 1; 39751852Sbp NCPVODEBUG("always happy to load!\n"); 39851852Sbp return (0); 39951852Sbp} 40051852Sbp 40151852Sbp/*ARGSUSED*/ 40251852Sbpint 40351852Sbpnwfs_uninit(struct vfsconf *vfsp) 40451852Sbp{ 40551852Sbp 40651852Sbp nwfs_hash_free(); 40751852Sbp NCPVODEBUG("unloaded\n"); 40851852Sbp return (0); 40951852Sbp} 41051852Sbp 41151852Sbp/* 41251852Sbp * nwfs_statfs call 41351852Sbp */ 41451852Sbpint 415191990Sattilionwfs_statfs(mp, sbp) 41651852Sbp struct mount *mp; 41766539Sbp struct statfs *sbp; 41851852Sbp{ 41951852Sbp struct nwmount *nmp = VFSTONWFS(mp); 420191990Sattilio struct thread *td = curthread; 42151852Sbp int error = 0, secsize; 42251852Sbp struct nwnode *np = nmp->n_root; 42351852Sbp struct ncp_volume_info vi; 42451852Sbp 42551852Sbp if (np == NULL) return EINVAL; 42683366Sjulian error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp), 42791406Sjhb nmp->n_volume, &vi, td, td->td_ucred); 42851852Sbp if (error) return error; 42951852Sbp secsize = 512; /* XXX how to get real value ??? */ 43096755Strhodes /* fundamental filesystem block size */ 43151852Sbp sbp->f_bsize = vi.sectors_per_block*secsize; 43251852Sbp /* optimal transfer block size */ 43351852Sbp sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size; 43496755Strhodes /* total data blocks in filesystem */ 43551852Sbp sbp->f_blocks= vi.total_blocks; 43651852Sbp /* free blocks in fs */ 43751852Sbp sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks; 43851852Sbp /* free blocks avail to non-superuser */ 43951852Sbp sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks; 44096755Strhodes /* total file nodes in filesystem */ 44151852Sbp sbp->f_files = vi.total_dir_entries; 44251852Sbp /* free file nodes in fs */ 44351852Sbp sbp->f_ffree = vi.available_dir_entries; 44451852Sbp sbp->f_flags = 0; /* copy of mount exported flags */ 44551852Sbp return 0; 44651852Sbp} 447