nwfs_vfsops.c revision 150761
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 150761 2005-09-30 18:21:05Z phk $ 3351852Sbp */ 3451852Sbp#include "opt_ncp.h" 3551852Sbp#ifndef NCP 3651852Sbp#error "NWFS requires NCP protocol" 3751852Sbp#endif 3851852Sbp 3951852Sbp#include <sys/param.h> 4051852Sbp#include <sys/systm.h> 4151852Sbp#include <sys/proc.h> 4251852Sbp#include <sys/kernel.h> 4351852Sbp#include <sys/sysctl.h> 4451852Sbp#include <sys/vnode.h> 4551852Sbp#include <sys/mount.h> 4651852Sbp#include <sys/stat.h> 4751852Sbp#include <sys/malloc.h> 4860041Sphk#include <sys/bio.h> 4951852Sbp#include <sys/buf.h> 5051852Sbp 5151852Sbp#include <netncp/ncp.h> 5251852Sbp#include <netncp/ncp_conn.h> 5351852Sbp#include <netncp/ncp_subr.h> 5451852Sbp#include <netncp/ncp_ncp.h> 5551852Sbp#include <netncp/ncp_nls.h> 5651852Sbp 5777223Sru#include <fs/nwfs/nwfs.h> 5877223Sru#include <fs/nwfs/nwfs_node.h> 5977223Sru#include <fs/nwfs/nwfs_subr.h> 6051852Sbp 6151852Sbpint nwfs_debuglevel = 0; 6251852Sbp 6351852Sbpstatic int nwfs_version = NWFS_VERSION; 6451852Sbp 6551852SbpSYSCTL_DECL(_vfs_nwfs); 6696755StrhodesSYSCTL_NODE(_vfs, OID_AUTO, nwfs, CTLFLAG_RW, 0, "Netware filesystem"); 6751852SbpSYSCTL_INT(_vfs_nwfs, OID_AUTO, version, CTLFLAG_RD, &nwfs_version, 0, ""); 6851852SbpSYSCTL_INT(_vfs_nwfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nwfs_debuglevel, 0, ""); 6951852Sbp 7059755SpeterMODULE_DEPEND(nwfs, ncp, 1, 1, 1); 7174637SbpMODULE_DEPEND(nwfs, libmchain, 1, 1, 1); 7259755Speter 73138486Sphkstatic vfs_cmount_t nwfs_cmount; 74138486Sphkstatic vfs_mount_t nwfs_mount; 75116271Sphkstatic vfs_quotactl_t nwfs_quotactl; 76116271Sphkstatic vfs_root_t nwfs_root; 77116271Sphkstatic vfs_statfs_t nwfs_statfs; 78116271Sphkstatic vfs_unmount_t nwfs_unmount; 79116271Sphkstatic vfs_init_t nwfs_init; 80116271Sphkstatic vfs_uninit_t nwfs_uninit; 8151852Sbp 8251852Sbpstatic struct vfsops nwfs_vfsops = { 83116271Sphk .vfs_init = nwfs_init, 84138486Sphk .vfs_mount = nwfs_mount, 85138486Sphk .vfs_cmount = nwfs_cmount, 86116271Sphk .vfs_quotactl = nwfs_quotactl, 87116271Sphk .vfs_root = nwfs_root, 88116271Sphk .vfs_statfs = nwfs_statfs, 89116271Sphk .vfs_sync = vfs_stdsync, 90116271Sphk .vfs_uninit = nwfs_uninit, 91116271Sphk .vfs_unmount = nwfs_unmount, 9251852Sbp}; 9351852Sbp 9451852Sbp 9551852SbpVFS_SET(nwfs_vfsops, nwfs, VFCF_NETWORK); 9651852Sbp 9751852Sbpint nwfs_pbuf_freecnt = -1; /* start out unlimited */ 9851852Sbpstatic int nwfsid = 1; 9951852Sbp 10051852Sbpstatic int 10151852Sbpnwfs_initnls(struct nwmount *nmp) { 10251852Sbp char *pc, *pe; 10351852Sbp int error = 0; 10451852Sbp#define COPY_TABLE(t,d) { \ 10551852Sbp if (t) { \ 10651852Sbp error = copyin((t), pc, 256); \ 10751852Sbp if (error) break; \ 10851852Sbp } else \ 10951852Sbp bcopy(d, pc, 256); \ 11051852Sbp (t) = pc; pc += 256; \ 11151852Sbp } 11251852Sbp 11351852Sbp nmp->m.nls.opt |= NWHP_NLS | NWHP_DOS; 11451852Sbp if ((nmp->m.flags & NWFS_MOUNT_HAVE_NLS) == 0) { 11552814Sarchie nmp->m.nls.to_lower = ncp_defnls.to_lower; 11652814Sarchie nmp->m.nls.to_upper = ncp_defnls.to_upper; 11751852Sbp nmp->m.nls.n2u = ncp_defnls.n2u; 11851852Sbp nmp->m.nls.u2n = ncp_defnls.u2n; 11951852Sbp return 0; 12051852Sbp } 121111119Simp MALLOC(pe, char *, 256 * 4, M_NWFSDATA, M_WAITOK); 12251852Sbp pc = pe; 12351852Sbp do { 12452814Sarchie COPY_TABLE(nmp->m.nls.to_lower, ncp_defnls.to_lower); 12552814Sarchie COPY_TABLE(nmp->m.nls.to_upper, ncp_defnls.to_upper); 12651852Sbp COPY_TABLE(nmp->m.nls.n2u, ncp_defnls.n2u); 12751852Sbp COPY_TABLE(nmp->m.nls.u2n, ncp_defnls.u2n); 12851852Sbp } while(0); 12951852Sbp if (error) { 13051852Sbp free(pe, M_NWFSDATA); 13151852Sbp return error; 13251852Sbp } 13351852Sbp return 0; 13451852Sbp} 135138486Sphk 136138486Sphkstatic int nwfs_cmount(struct mntarg *ma, void *data, int flags, 137138486Sphk struct thread *td) 138138486Sphk{ 139138486Sphk struct nwfs_args args; /* will hold data from mount request */ 140138486Sphk int error; 141138486Sphk 142138486Sphk error = copyin(data, (caddr_t)&args, sizeof(struct nwfs_args)); 143138486Sphk if (error) 144138486Sphk return (error); 145138486Sphk 146138486Sphk /* 147138486Sphk * XXX: cheap cop-out here, args contains a structure I don't 148138486Sphk * XXX: know how we should handle, and I don't see any immediate 149138486Sphk * XXX: prospect of avoiding a mount_nwfs(8) binary anyway. 150138486Sphk */ 151138486Sphk ma = mount_arg(ma, "nwfs_args", &args, sizeof args); 152138486Sphk 153138486Sphk error = kernel_mount(ma, flags); 154138486Sphk 155138486Sphk return (error); 156138486Sphk} 157138486Sphk 15851852Sbp/* 15951852Sbp * mp - path - addr in user space of mount point (ie /usr or whatever) 16051852Sbp * data - addr in user space of mount params 16151852Sbp */ 162138486Sphkstatic int nwfs_mount(struct mount *mp, struct thread *td) 16351852Sbp{ 16451852Sbp struct nwfs_args args; /* will hold data from mount request */ 16551852Sbp int error; 16651852Sbp struct nwmount *nmp = NULL; 16751852Sbp struct ncp_conn *conn = NULL; 16851852Sbp struct ncp_handle *handle = NULL; 16951852Sbp struct vnode *vp; 17051852Sbp char *pc,*pe; 17151852Sbp 172137479Sphk if (mp->mnt_flag & MNT_ROOTFS) 173137479Sphk return (EOPNOTSUPP); 17451852Sbp if (mp->mnt_flag & MNT_UPDATE) { 17551852Sbp nwfs_printf("MNT_UPDATE not implemented"); 17651852Sbp return (EOPNOTSUPP); 17751852Sbp } 178150761Sphk error = vfs_copyopt(mp->mnt_optnew, "nwfs_args", &args, sizeof args); 17951852Sbp if (error) 18051852Sbp return (error); 18151852Sbp if (args.version != NWFS_VERSION) { 18251852Sbp nwfs_printf("mount version mismatch: kernel=%d, mount=%d\n",NWFS_VERSION,args.version); 18351852Sbp return (1); 18451852Sbp } 18591406Sjhb error = ncp_conn_getbyref(args.connRef, td , td->td_ucred,NCPM_EXECUTE,&conn); 18651852Sbp if (error) { 18751852Sbp nwfs_printf("invalid connection refernce %d\n",args.connRef); 18851852Sbp return (error); 18951852Sbp } 19051852Sbp error = ncp_conn_gethandle(conn, NULL, &handle); 19151852Sbp if (error) { 19251852Sbp nwfs_printf("can't get connection handle\n"); 19351852Sbp return (error); 19451852Sbp } 19583366Sjulian ncp_conn_unlock(conn, td); /* we keep the ref */ 19651852Sbp mp->mnt_stat.f_iosize = conn->buffer_size; 19751852Sbp /* We must malloc our own mount info */ 198112916Stjr MALLOC(nmp,struct nwmount *,sizeof(struct nwmount),M_NWFSDATA, 199112916Stjr M_WAITOK | M_USE_RESERVE | M_ZERO); 20051852Sbp if (nmp == NULL) { 20151852Sbp nwfs_printf("could not alloc nwmount\n"); 20251852Sbp error = ENOMEM; 20351852Sbp goto bad; 20451852Sbp } 20551852Sbp mp->mnt_data = (qaddr_t)nmp; 20651852Sbp nmp->connh = handle; 20751852Sbp nmp->n_root = NULL; 20851852Sbp nmp->n_id = nwfsid++; 20951852Sbp nmp->m = args; 21051852Sbp nmp->m.file_mode = (nmp->m.file_mode & 21151852Sbp (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 21251852Sbp nmp->m.dir_mode = (nmp->m.dir_mode & 21351852Sbp (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 21451852Sbp if ((error = nwfs_initnls(nmp)) != 0) goto bad; 21551852Sbp pc = mp->mnt_stat.f_mntfromname; 21651852Sbp pe = pc+sizeof(mp->mnt_stat.f_mntfromname); 21751852Sbp bzero(pc, MNAMELEN); 21851852Sbp *(pc++) = '/'; 21951852Sbp pc = index(strncpy(pc, conn->li.server, pe-pc-2),0); 22051852Sbp if (pc < pe-1) { 22151852Sbp *(pc++) = ':'; 22251852Sbp pc=index(strncpy(pc, conn->li.user, pe-pc-2),0); 22351852Sbp if (pc < pe-1) { 22451852Sbp *(pc++) = '/'; 22551852Sbp strncpy(pc, nmp->m.mounted_vol, pe-pc-2); 22651852Sbp } 22751852Sbp } 22851852Sbp /* protect against invalid mount points */ 22951852Sbp nmp->m.mount_point[sizeof(nmp->m.mount_point)-1] = '\0'; 23051852Sbp vfs_getnewfsid(mp); 231144058Sjeff error = nwfs_root(mp, LK_EXCLUSIVE, &vp, td); 23251852Sbp if (error) 23351852Sbp goto bad; 23451852Sbp /* 23551852Sbp * Lose the lock but keep the ref. 23651852Sbp */ 23783366Sjulian VOP_UNLOCK(vp, 0, curthread); 238103936Sjeff NCPVODEBUG("rootvp.vrefcnt=%d\n",vrefcnt(vp)); 23951852Sbp return error; 24051852Sbpbad: 24151852Sbp if (nmp) 24251852Sbp free(nmp, M_NWFSDATA); 24351852Sbp if (handle) 24451852Sbp ncp_conn_puthandle(handle, NULL, 0); 24551852Sbp return error; 24651852Sbp} 24751852Sbp 24851852Sbp/* Unmount the filesystem described by mp. */ 24951852Sbpstatic int 25083366Sjuliannwfs_unmount(struct mount *mp, int mntflags, struct thread *td) 25151852Sbp{ 25251852Sbp struct nwmount *nmp = VFSTONWFS(mp); 25351852Sbp struct ncp_conn *conn; 25451852Sbp int error, flags; 25551852Sbp 25651852Sbp NCPVODEBUG("nwfs_unmount: flags=%04x\n",mntflags); 25751852Sbp flags = 0; 25851852Sbp if (mntflags & MNT_FORCE) 25951852Sbp flags |= FORCECLOSE; 26076688Siedowse /* There is 1 extra root vnode reference from nwfs_mount(). */ 261132023Salfred error = vflush(mp, 1, flags, td); 26276688Siedowse if (error) 26351852Sbp return (error); 26451852Sbp conn = NWFSTOCONN(nmp); 26551852Sbp ncp_conn_puthandle(nmp->connh,NULL,0); 26691406Sjhb if (ncp_conn_lock(conn, td, td->td_ucred,NCPM_WRITE | NCPM_EXECUTE) == 0) { 26774062Sbp if(ncp_conn_free(conn)) 26883366Sjulian ncp_conn_unlock(conn, td); 26951852Sbp } 27051852Sbp mp->mnt_data = (qaddr_t)0; 27151852Sbp if (nmp->m.flags & NWFS_MOUNT_HAVE_NLS) 27252814Sarchie free(nmp->m.nls.to_lower, M_NWFSDATA); 27351852Sbp free(nmp, M_NWFSDATA); 27451852Sbp mp->mnt_flag &= ~MNT_LOCAL; 27551852Sbp return (error); 27651852Sbp} 27751852Sbp 27851852Sbp/* Return locked vnode to root of a filesystem */ 27951852Sbpstatic int 280144058Sjeffnwfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) { 28151852Sbp struct vnode *vp; 28251852Sbp struct nwmount *nmp; 28351852Sbp struct nwnode *np; 28451852Sbp struct ncp_conn *conn; 28551852Sbp struct nw_entry_info fattr; 28691406Sjhb struct ucred *cred = td->td_ucred; 28751852Sbp int error, nsf, opt; 28851852Sbp u_char vol; 28951852Sbp 29051852Sbp nmp = VFSTONWFS(mp); 29151852Sbp conn = NWFSTOCONN(nmp); 29251852Sbp if (nmp->n_root) { 29351852Sbp *vpp = NWTOV(nmp->n_root); 29483366Sjulian while (vget(*vpp, LK_EXCLUSIVE, curthread) != 0) 29566540Sbp ; 29651852Sbp return 0; 29751852Sbp } 29851852Sbp error = ncp_lookup_volume(conn, nmp->m.mounted_vol, &vol, 29983366Sjulian &nmp->n_rootent.f_id, td, cred); 30051852Sbp if (error) 30151852Sbp return ENOENT; 30251852Sbp nmp->n_volume = vol; 30383366Sjulian error = ncp_get_namespaces(conn, vol, &nsf, td, cred); 30451852Sbp if (error) 30551852Sbp return ENOENT; 30651852Sbp if (nsf & NW_NSB_OS2) { 30751852Sbp NCPVODEBUG("volume %s has os2 namespace\n",nmp->m.mounted_vol); 30851852Sbp if ((nmp->m.flags & NWFS_MOUNT_NO_OS2) == 0) { 30951852Sbp nmp->name_space = NW_NS_OS2; 31051852Sbp nmp->m.nls.opt &= ~NWHP_DOS; 31151852Sbp } 31251852Sbp } 31351852Sbp opt = nmp->m.nls.opt; 31451852Sbp nsf = opt & (NWHP_UPPER | NWHP_LOWER); 31551852Sbp if (opt & NWHP_DOS) { 31651852Sbp if (nsf == (NWHP_UPPER | NWHP_LOWER)) { 31751852Sbp nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER); 31851852Sbp } else if (nsf == 0) { 31951852Sbp nmp->m.nls.opt |= NWHP_LOWER; 32051852Sbp } 32151852Sbp } else { 32251852Sbp if (nsf == (NWHP_UPPER | NWHP_LOWER)) { 32351852Sbp nmp->m.nls.opt &= ~(NWHP_LOWER | NWHP_UPPER); 32451852Sbp } 32551852Sbp } 32651852Sbp if (nmp->m.root_path[0]) { 32751852Sbp nmp->m.root_path[0]--; 32851852Sbp error = ncp_obtain_info(nmp, nmp->n_rootent.f_id, 32983366Sjulian -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred); 33051852Sbp if (error) { 33151852Sbp NCPFATAL("Invalid root path specified\n"); 33251852Sbp return ENOENT; 33351852Sbp } 33451852Sbp nmp->n_rootent.f_parent = fattr.dirEntNum; 33551852Sbp nmp->m.root_path[0]++; 33651852Sbp error = ncp_obtain_info(nmp, nmp->n_rootent.f_id, 33783366Sjulian -nmp->m.root_path[0], nmp->m.root_path, &fattr, td, cred); 33851852Sbp if (error) { 33951852Sbp NCPFATAL("Invalid root path specified\n"); 34051852Sbp return ENOENT; 34151852Sbp } 34251852Sbp nmp->n_rootent.f_id = fattr.dirEntNum; 34351852Sbp } else { 34451852Sbp error = ncp_obtain_info(nmp, nmp->n_rootent.f_id, 34583366Sjulian 0, NULL, &fattr, td, cred); 34651852Sbp if (error) { 34751852Sbp NCPFATAL("Can't obtain volume info\n"); 34851852Sbp return ENOENT; 34951852Sbp } 35074064Sbp fattr.nameLen = strlen(strcpy(fattr.entryName, "#.ROOT")); 35151852Sbp nmp->n_rootent.f_parent = nmp->n_rootent.f_id; 35251852Sbp } 35351852Sbp error = nwfs_nget(mp, nmp->n_rootent, &fattr, NULL, &vp); 35451852Sbp if (error) 35551852Sbp return (error); 356101308Sjeff vp->v_vflag |= VV_ROOT; 35751852Sbp np = VTONW(vp); 35851852Sbp if (nmp->m.root_path[0] == 0) 35951852Sbp np->n_flag |= NVOLUME; 36051852Sbp nmp->n_root = np; 36183366Sjulian/* error = VOP_GETATTR(vp, &vattr, cred, td); 36251852Sbp if (error) { 36351852Sbp vput(vp); 36451852Sbp NCPFATAL("Can't get root directory entry\n"); 36551852Sbp return error; 36651852Sbp }*/ 36751852Sbp *vpp = vp; 36851852Sbp return (0); 36951852Sbp} 37051852Sbp 37151852Sbp/* 37251852Sbp * Do operations associated with quotas, not supported 37351852Sbp */ 37451852Sbp/* ARGSUSED */ 37551852Sbpstatic int 37683366Sjuliannwfs_quotactl(mp, cmd, uid, arg, td) 37751852Sbp struct mount *mp; 37851852Sbp int cmd; 37951852Sbp uid_t uid; 38051852Sbp caddr_t arg; 38183366Sjulian struct thread *td; 38251852Sbp{ 38351852Sbp NCPVODEBUG("return EOPNOTSUPP\n"); 38451852Sbp return (EOPNOTSUPP); 38551852Sbp} 38651852Sbp 38751852Sbp/*ARGSUSED*/ 38851852Sbpint 38951852Sbpnwfs_init(struct vfsconf *vfsp) 39051852Sbp{ 39151852Sbp nwfs_hash_init(); 39251852Sbp nwfs_pbuf_freecnt = nswbuf / 2 + 1; 39351852Sbp NCPVODEBUG("always happy to load!\n"); 39451852Sbp return (0); 39551852Sbp} 39651852Sbp 39751852Sbp/*ARGSUSED*/ 39851852Sbpint 39951852Sbpnwfs_uninit(struct vfsconf *vfsp) 40051852Sbp{ 40151852Sbp 40251852Sbp nwfs_hash_free(); 40351852Sbp NCPVODEBUG("unloaded\n"); 40451852Sbp return (0); 40551852Sbp} 40651852Sbp 40751852Sbp/* 40851852Sbp * nwfs_statfs call 40951852Sbp */ 41051852Sbpint 41183366Sjuliannwfs_statfs(mp, sbp, td) 41251852Sbp struct mount *mp; 41366539Sbp struct statfs *sbp; 41483366Sjulian struct thread *td; 41551852Sbp{ 41651852Sbp struct nwmount *nmp = VFSTONWFS(mp); 41751852Sbp int error = 0, secsize; 41851852Sbp struct nwnode *np = nmp->n_root; 41951852Sbp struct ncp_volume_info vi; 42051852Sbp 42151852Sbp if (np == NULL) return EINVAL; 42283366Sjulian error = ncp_get_volume_info_with_number(NWFSTOCONN(nmp), 42391406Sjhb nmp->n_volume, &vi, td, td->td_ucred); 42451852Sbp if (error) return error; 42551852Sbp secsize = 512; /* XXX how to get real value ??? */ 42696755Strhodes /* fundamental filesystem block size */ 42751852Sbp sbp->f_bsize = vi.sectors_per_block*secsize; 42851852Sbp /* optimal transfer block size */ 42951852Sbp sbp->f_iosize = NWFSTOCONN(nmp)->buffer_size; 43096755Strhodes /* total data blocks in filesystem */ 43151852Sbp sbp->f_blocks= vi.total_blocks; 43251852Sbp /* free blocks in fs */ 43351852Sbp sbp->f_bfree = vi.free_blocks + vi.purgeable_blocks; 43451852Sbp /* free blocks avail to non-superuser */ 43551852Sbp sbp->f_bavail= vi.free_blocks+vi.purgeable_blocks; 43696755Strhodes /* total file nodes in filesystem */ 43751852Sbp sbp->f_files = vi.total_dir_entries; 43851852Sbp /* free file nodes in fs */ 43951852Sbp sbp->f_ffree = vi.available_dir_entries; 44051852Sbp sbp->f_flags = 0; /* copy of mount exported flags */ 44151852Sbp return 0; 44251852Sbp} 443