#ifndef KLD_MODULE
#include "opt_netsmb.h"
#ifndef NETSMB
#error "SMBFS requires option NETSMB"
#endif
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <netproto/smb/smb.h>
#include <netproto/smb/smb_conn.h>
#include <netproto/smb/smb_subr.h>
#include <netproto/smb/smb_dev.h>
#include "smbfs.h"
#include "smbfs_node.h"
#include "smbfs_subr.h"
#include <sys/buf.h>
extern struct vop_ops smbfs_vnode_vops;
int smbfs_debuglevel = 0;
static int smbfs_version = SMBFS_VERSION;
SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system");
SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
static int smbfs_mount(struct mount *, char *, caddr_t, struct ucred *);
static int smbfs_root(struct mount *, struct vnode **);
static int smbfs_statfs(struct mount *, struct statfs *, struct ucred *);
static int smbfs_sync(struct mount *, int);
static int smbfs_unmount(struct mount *, int);
static int smbfs_init(struct vfsconf *vfsp);
static int smbfs_uninit(struct vfsconf *vfsp);
static struct vfsops smbfs_vfsops = {
.vfs_flags = 0,
.vfs_mount = smbfs_mount,
.vfs_unmount = smbfs_unmount,
.vfs_root = smbfs_root,
.vfs_statfs = smbfs_statfs,
.vfs_sync = smbfs_sync,
.vfs_init = smbfs_init,
.vfs_uninit = smbfs_uninit
};
VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
MODULE_VERSION(smbfs, 1);
MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
int smbfs_pbuf_freecnt = -1;
static int
smbfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
{
struct smbfs_args args;
struct smbmount *smp = NULL;
struct smb_vc *vcp;
struct smb_share *ssp = NULL;
struct vnode *vp;
struct smb_cred scred;
int error;
int hsize;
char *pc, *pe;
if (data == NULL) {
kprintf("missing data argument\n");
return EINVAL;
}
if (mp->mnt_flag & MNT_UPDATE) {
kprintf("MNT_UPDATE not implemented");
return EOPNOTSUPP;
}
error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
if (error)
return error;
if (args.version != SMBFS_VERSION) {
kprintf("mount version mismatch: kernel=%d, mount=%d\n",
SMBFS_VERSION, args.version);
return EINVAL;
}
smb_makescred(&scred, curthread, cred);
error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
if (error) {
kprintf("invalid device handle %d (%d)\n", args.dev, error);
return error;
}
vcp = SSTOVC(ssp);
smb_share_unlock(ssp, 0);
mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
smp = kmalloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK | M_USE_RESERVE | M_ZERO);
mp->mnt_data = (qaddr_t)smp;
smp->sm_cred = crhold(cred);
hsize = vfs_inodehashsize();
smp->sm_hash = hashinit(hsize, M_SMBFSHASH, &smp->sm_hashlen);
if (smp->sm_hash == NULL)
goto bad;
lockinit(&smp->sm_hashlock, "smbfsh", 0, 0);
smp->sm_share = ssp;
smp->sm_root = NULL;
smp->sm_args = args;
smp->sm_caseopt = args.caseopt;
smp->sm_args.file_mode = (smp->sm_args.file_mode &
(S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
smp->sm_args.dir_mode = (smp->sm_args.dir_mode &
(S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
pc = mp->mnt_stat.f_mntfromname;
pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
bzero(pc, MNAMELEN);
*pc++ = '/';
*pc++ = '/';
pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
if (pc < pe-1) {
*(pc++) = '@';
pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
if (pc < pe - 1) {
*(pc++) = '/';
strncpy(pc, ssp->ss_name, pe - pc - 2);
}
}
smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
vfs_getnewfsid(mp);
vfs_add_vnodeops(mp, &smbfs_vnode_vops, &mp->mnt_vn_norm_ops);
error = smbfs_root(mp, &vp);
if (error)
goto bad;
vn_unlock(vp);
SMBVDEBUG("root.v_refcnt = %08x\n", vp->v_refcnt);
#ifdef DIAGNOSTICS
SMBERROR("mp=%p\n", mp);
#endif
return error;
bad:
if (smp) {
if (smp->sm_cred)
crfree(smp->sm_cred);
if (smp->sm_hash)
hashdestroy(smp->sm_hash, M_SMBFSHASH,
smp->sm_hashlen);
lockdestroy(&smp->sm_hashlock);
kfree(smp, M_SMBFSDATA);
}
if (ssp)
smb_share_put(ssp, &scred);
return error;
}
static int
smbfs_unmount(struct mount *mp, int mntflags)
{
struct smbmount *smp = VFSTOSMBFS(mp);
struct smb_cred scred;
int error, flags;
SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
flags = 0;
if (mntflags & MNT_FORCE)
flags |= FORCECLOSE;
do {
smp->sm_didrele = 0;
error = vflush(mp, 1, flags);
} while (error == EBUSY && smp->sm_didrele != 0);
if (error)
return error;
smb_makescred(&scred, curthread, smp->sm_cred);
error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE);
if (error)
goto out;
smb_share_put(smp->sm_share, &scred);
mp->mnt_data = (qaddr_t)0;
if (smp->sm_cred)
crfree(smp->sm_cred);
if (smp->sm_hash)
hashdestroy(smp->sm_hash, M_SMBFSHASH, smp->sm_hashlen);
lockdestroy(&smp->sm_hashlock);
kfree(smp, M_SMBFSDATA);
mp->mnt_flag &= ~MNT_LOCAL;
out:
return error;
}
static int
smbfs_root(struct mount *mp, struct vnode **vpp)
{
struct thread *td = curthread;
struct smbmount *smp = VFSTOSMBFS(mp);
struct vnode *vp;
struct smbnode *np;
struct smbfattr fattr;
struct ucred *cred;
struct smb_cred scred;
int error;
if (smp == NULL) {
SMBERROR("smp == NULL (bug in umount)\n");
return EINVAL;
}
if (smp->sm_root) {
*vpp = SMBTOV(smp->sm_root);
return vget(*vpp, LK_EXCLUSIVE | LK_RETRY);
}
if (td->td_proc)
cred = td->td_proc->p_ucred;
else
cred = proc0.p_ucred;
smb_makescred(&scred, td, cred);
error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
if (error)
return error;
error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
if (error)
return error;
vsetflags(vp, VROOT);
np = VTOSMB(vp);
smp->sm_root = np;
*vpp = vp;
return 0;
}
int
smbfs_init(struct vfsconf *vfsp)
{
smbfs_pbuf_freecnt = nswbuf_kva / 2 + 1;
SMBVDEBUG("done.\n");
return 0;
}
int
smbfs_uninit(struct vfsconf *vfsp)
{
SMBVDEBUG("done.\n");
return 0;
}
int
smbfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
{
struct smbmount *smp = VFSTOSMBFS(mp);
struct smbnode *np = smp->sm_root;
struct smb_share *ssp = smp->sm_share;
struct smb_cred scred;
int error = 0;
if (np == NULL)
return EINVAL;
sbp->f_iosize = SSTOVC(ssp)->vc_txmax;
sbp->f_spare2 = 0;
smb_makescred(&scred, curthread, cred);
if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
error = smbfs_smb_statfs2(ssp, sbp, &scred);
else
error = smbfs_smb_statfs(ssp, sbp, &scred);
if (error)
return error;
sbp->f_flags = 0;
if (sbp != &mp->mnt_stat) {
sbp->f_fsid = mp->mnt_stat.f_fsid;
sbp->f_owner = mp->mnt_stat.f_owner;
sbp->f_type = mp->mnt_vfc->vfc_typenum;
bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
}
strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
return 0;
}
static int
smbfs_sync(struct mount *mp, int waitfor)
{
struct vnode *vp;
int error, allerror = 0;
loop:
for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
vp != NULL;
vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
if (vp->v_mount != mp)
goto loop;
if (vn_islocked(vp) || RB_EMPTY(&vp->v_rbdirty_tree) ||
(waitfor & MNT_LAZY))
{
continue;
}
if (vget(vp, LK_EXCLUSIVE))
goto loop;
error = VOP_FSYNC(vp, waitfor, 0);
if (error)
allerror = error;
vput(vp);
}
return (allerror);
}