1c398230bSWarner Losh /*- 2681a5bbeSBoris Popov * Copyright (c) 2000-2001 Boris Popov 3681a5bbeSBoris Popov * All rights reserved. 4681a5bbeSBoris Popov * 5681a5bbeSBoris Popov * Redistribution and use in source and binary forms, with or without 6681a5bbeSBoris Popov * modification, are permitted provided that the following conditions 7681a5bbeSBoris Popov * are met: 8681a5bbeSBoris Popov * 1. Redistributions of source code must retain the above copyright 9681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer. 10681a5bbeSBoris Popov * 2. Redistributions in binary form must reproduce the above copyright 11681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer in the 12681a5bbeSBoris Popov * documentation and/or other materials provided with the distribution. 13681a5bbeSBoris Popov * 14681a5bbeSBoris Popov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15681a5bbeSBoris Popov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16681a5bbeSBoris Popov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17681a5bbeSBoris Popov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18681a5bbeSBoris Popov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19681a5bbeSBoris Popov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20681a5bbeSBoris Popov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21681a5bbeSBoris Popov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22681a5bbeSBoris Popov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23681a5bbeSBoris Popov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24681a5bbeSBoris Popov * SUCH DAMAGE. 25681a5bbeSBoris Popov */ 26ab0de15bSDavid E. O'Brien 27ab0de15bSDavid E. O'Brien #include <sys/cdefs.h> 28ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$"); 29ab0de15bSDavid E. O'Brien 30681a5bbeSBoris Popov #include <sys/param.h> 31681a5bbeSBoris Popov #include <sys/kernel.h> 325dba30f1SPoul-Henning Kamp #include <sys/module.h> 33681a5bbeSBoris Popov #include <sys/systm.h> 34681a5bbeSBoris Popov #include <sys/conf.h> 35681a5bbeSBoris Popov #include <sys/fcntl.h> 36fb919e4dSMark Murray #include <sys/ioccom.h> 37fb919e4dSMark Murray #include <sys/lock.h> 38fb919e4dSMark Murray #include <sys/malloc.h> 39fb919e4dSMark Murray #include <sys/file.h> /* Must come after sys/malloc.h */ 4013438f68SAlfred Perlstein #include <sys/filedesc.h> 41fb919e4dSMark Murray #include <sys/mbuf.h> 42681a5bbeSBoris Popov #include <sys/poll.h> 43fb919e4dSMark Murray #include <sys/proc.h> 44fb919e4dSMark Murray #include <sys/select.h> 45fb919e4dSMark Murray #include <sys/socket.h> 46681a5bbeSBoris Popov #include <sys/socketvar.h> 47681a5bbeSBoris Popov #include <sys/sysctl.h> 48fb919e4dSMark Murray #include <sys/uio.h> 49681a5bbeSBoris Popov #include <sys/vnode.h> 50681a5bbeSBoris Popov 51681a5bbeSBoris Popov #include <net/if.h> 52681a5bbeSBoris Popov 53681a5bbeSBoris Popov #include <netsmb/smb.h> 54681a5bbeSBoris Popov #include <netsmb/smb_conn.h> 55681a5bbeSBoris Popov #include <netsmb/smb_subr.h> 56681a5bbeSBoris Popov #include <netsmb/smb_dev.h> 57681a5bbeSBoris Popov 58681a5bbeSBoris Popov #define SMB_GETDEV(dev) ((struct smb_dev*)(dev)->si_drv1) 59681a5bbeSBoris Popov #define SMB_CHECKMINOR(dev) do { \ 60681a5bbeSBoris Popov sdp = SMB_GETDEV(dev); \ 61681a5bbeSBoris Popov if (sdp == NULL) return ENXIO; \ 62681a5bbeSBoris Popov } while(0) 63681a5bbeSBoris Popov 64681a5bbeSBoris Popov static d_open_t nsmb_dev_open; 65681a5bbeSBoris Popov static d_close_t nsmb_dev_close; 66681a5bbeSBoris Popov static d_ioctl_t nsmb_dev_ioctl; 67681a5bbeSBoris Popov 68c4f02a89SMax Khon MODULE_DEPEND(netsmb, libiconv, 1, 1, 2); 69681a5bbeSBoris Popov MODULE_VERSION(netsmb, NSMB_VERSION); 70681a5bbeSBoris Popov 71681a5bbeSBoris Popov static int smb_version = NSMB_VERSION; 72681a5bbeSBoris Popov 73681a5bbeSBoris Popov 74681a5bbeSBoris Popov SYSCTL_DECL(_net_smb); 75681a5bbeSBoris Popov SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, ""); 76681a5bbeSBoris Popov 77681a5bbeSBoris Popov static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device"); 78681a5bbeSBoris Popov 79681a5bbeSBoris Popov 80681a5bbeSBoris Popov /* 81681a5bbeSBoris Popov int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio); 82681a5bbeSBoris Popov */ 83681a5bbeSBoris Popov 84681a5bbeSBoris Popov static struct cdevsw nsmb_cdevsw = { 85dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 86339f72c7SRobert Watson .d_flags = D_NEEDGIANT | D_NEEDMINOR, 877ac40f5fSPoul-Henning Kamp .d_open = nsmb_dev_open, 887ac40f5fSPoul-Henning Kamp .d_close = nsmb_dev_close, 897ac40f5fSPoul-Henning Kamp .d_ioctl = nsmb_dev_ioctl, 90fd13f792STim J. Robbins .d_name = NSMB_NAME 91681a5bbeSBoris Popov }; 92681a5bbeSBoris Popov 93681a5bbeSBoris Popov static eventhandler_tag nsmb_dev_tag; 94339f72c7SRobert Watson static struct clonedevs *nsmb_clones; 95681a5bbeSBoris Popov 96681a5bbeSBoris Popov static void 976a113b3dSRobert Watson nsmb_dev_clone(void *arg, struct ucred *cred, char *name, int namelen, 986a113b3dSRobert Watson struct cdev **dev) 99681a5bbeSBoris Popov { 100339f72c7SRobert Watson int i, u; 101681a5bbeSBoris Popov 102f3732fd1SPoul-Henning Kamp if (*dev != NULL) 103681a5bbeSBoris Popov return; 104339f72c7SRobert Watson 105339f72c7SRobert Watson if (strcmp(name, NSMB_NAME) == 0) 106339f72c7SRobert Watson u = -1; 107339f72c7SRobert Watson else if (dev_stdclone(name, NULL, NSMB_NAME, &u) != 1) 108681a5bbeSBoris Popov return; 109339f72c7SRobert Watson i = clone_create(&nsmb_clones, &nsmb_cdevsw, &u, dev, 0); 110339f72c7SRobert Watson if (i) { 111339f72c7SRobert Watson *dev = make_dev(&nsmb_cdevsw, u, UID_ROOT, GID_WHEEL, 0600, 112339f72c7SRobert Watson "%s%d", NSMB_NAME, u); 113339f72c7SRobert Watson if (*dev != NULL) { 114f4f6abcbSPoul-Henning Kamp dev_ref(*dev); 115339f72c7SRobert Watson (*dev)->si_flags |= SI_CHEAPCLONE; 116339f72c7SRobert Watson } 117339f72c7SRobert Watson } 118681a5bbeSBoris Popov } 119681a5bbeSBoris Popov 120681a5bbeSBoris Popov static int 12189c9c53dSPoul-Henning Kamp nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 122681a5bbeSBoris Popov { 123681a5bbeSBoris Popov struct smb_dev *sdp; 124a854ed98SJohn Baldwin struct ucred *cred = td->td_ucred; 125681a5bbeSBoris Popov int s; 126681a5bbeSBoris Popov 127681a5bbeSBoris Popov sdp = SMB_GETDEV(dev); 128681a5bbeSBoris Popov if (sdp && (sdp->sd_flags & NSMBFL_OPEN)) 129681a5bbeSBoris Popov return EBUSY; 130681a5bbeSBoris Popov if (sdp == NULL) { 131a163d034SWarner Losh sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK); 132681a5bbeSBoris Popov dev->si_drv1 = (void*)sdp; 133681a5bbeSBoris Popov } 134681a5bbeSBoris Popov /* 135681a5bbeSBoris Popov * XXX: this is just crazy - make a device for an already passed device... 136681a5bbeSBoris Popov * someone should take care of it. 137681a5bbeSBoris Popov */ 138681a5bbeSBoris Popov if ((dev->si_flags & SI_NAMED) == 0) 1396bfa9a2dSEd Schouten make_dev(&nsmb_cdevsw, dev2unit(dev), cred->cr_uid, 1406bfa9a2dSEd Schouten cred->cr_gid, 0700, NSMB_NAME"%d", dev2unit(dev)); 141681a5bbeSBoris Popov bzero(sdp, sizeof(*sdp)); 142681a5bbeSBoris Popov /* 143681a5bbeSBoris Popov STAILQ_INIT(&sdp->sd_rqlist); 144681a5bbeSBoris Popov STAILQ_INIT(&sdp->sd_rplist); 145681a5bbeSBoris Popov bzero(&sdp->sd_pollinfo, sizeof(struct selinfo)); 146681a5bbeSBoris Popov */ 147681a5bbeSBoris Popov s = splimp(); 148681a5bbeSBoris Popov sdp->sd_level = -1; 149681a5bbeSBoris Popov sdp->sd_flags |= NSMBFL_OPEN; 150681a5bbeSBoris Popov splx(s); 151681a5bbeSBoris Popov return 0; 152681a5bbeSBoris Popov } 153681a5bbeSBoris Popov 154681a5bbeSBoris Popov static int 15589c9c53dSPoul-Henning Kamp nsmb_dev_close(struct cdev *dev, int flag, int fmt, struct thread *td) 156681a5bbeSBoris Popov { 157681a5bbeSBoris Popov struct smb_dev *sdp; 158681a5bbeSBoris Popov struct smb_vc *vcp; 159681a5bbeSBoris Popov struct smb_share *ssp; 160*afe09751SDavide Italiano struct smb_cred *scred; 161681a5bbeSBoris Popov int s; 162681a5bbeSBoris Popov 163*afe09751SDavide Italiano scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK); 164681a5bbeSBoris Popov SMB_CHECKMINOR(dev); 165681a5bbeSBoris Popov s = splimp(); 166681a5bbeSBoris Popov if ((sdp->sd_flags & NSMBFL_OPEN) == 0) { 167681a5bbeSBoris Popov splx(s); 168*afe09751SDavide Italiano free(scred, M_NSMBDEV); 169681a5bbeSBoris Popov return EBADF; 170681a5bbeSBoris Popov } 171*afe09751SDavide Italiano smb_makescred(scred, td, NULL); 172681a5bbeSBoris Popov ssp = sdp->sd_share; 173681a5bbeSBoris Popov if (ssp != NULL) 174*afe09751SDavide Italiano smb_share_rele(ssp, scred); 175681a5bbeSBoris Popov vcp = sdp->sd_vc; 176681a5bbeSBoris Popov if (vcp != NULL) 177*afe09751SDavide Italiano smb_vc_rele(vcp, scred); 178681a5bbeSBoris Popov /* 179681a5bbeSBoris Popov smb_flushq(&sdp->sd_rqlist); 180681a5bbeSBoris Popov smb_flushq(&sdp->sd_rplist); 181681a5bbeSBoris Popov */ 182681a5bbeSBoris Popov dev->si_drv1 = NULL; 183681a5bbeSBoris Popov free(sdp, M_NSMBDEV); 1845ee1ac46STai-hwa Liang destroy_dev_sched(dev); 185681a5bbeSBoris Popov splx(s); 186*afe09751SDavide Italiano free(scred, M_NSMBDEV); 187681a5bbeSBoris Popov return 0; 188681a5bbeSBoris Popov } 189681a5bbeSBoris Popov 190681a5bbeSBoris Popov 191681a5bbeSBoris Popov static int 19289c9c53dSPoul-Henning Kamp nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 193681a5bbeSBoris Popov { 194681a5bbeSBoris Popov struct smb_dev *sdp; 195681a5bbeSBoris Popov struct smb_vc *vcp; 196681a5bbeSBoris Popov struct smb_share *ssp; 197*afe09751SDavide Italiano struct smb_cred *scred; 198681a5bbeSBoris Popov int error = 0; 199681a5bbeSBoris Popov 200681a5bbeSBoris Popov SMB_CHECKMINOR(dev); 201681a5bbeSBoris Popov if ((sdp->sd_flags & NSMBFL_OPEN) == 0) 202681a5bbeSBoris Popov return EBADF; 203681a5bbeSBoris Popov 204*afe09751SDavide Italiano scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK); 205*afe09751SDavide Italiano smb_makescred(scred, td, NULL); 206681a5bbeSBoris Popov switch (cmd) { 207681a5bbeSBoris Popov case SMBIOC_OPENSESSION: 208*afe09751SDavide Italiano if (sdp->sd_vc) { 209*afe09751SDavide Italiano error = EISCONN; 210*afe09751SDavide Italiano goto out; 211*afe09751SDavide Italiano } 212681a5bbeSBoris Popov error = smb_usr_opensession((struct smbioc_ossn*)data, 213*afe09751SDavide Italiano scred, &vcp); 214681a5bbeSBoris Popov if (error) 215681a5bbeSBoris Popov break; 216681a5bbeSBoris Popov sdp->sd_vc = vcp; 2172b7da2dbSRobert Watson smb_vc_unlock(vcp, 0); 218681a5bbeSBoris Popov sdp->sd_level = SMBL_VC; 219681a5bbeSBoris Popov break; 220681a5bbeSBoris Popov case SMBIOC_OPENSHARE: 221*afe09751SDavide Italiano if (sdp->sd_share) { 222*afe09751SDavide Italiano error = EISCONN; 223*afe09751SDavide Italiano goto out; 224*afe09751SDavide Italiano } 225*afe09751SDavide Italiano if (sdp->sd_vc == NULL) { 226*afe09751SDavide Italiano error = ENOTCONN; 227*afe09751SDavide Italiano goto out; 228*afe09751SDavide Italiano } 229681a5bbeSBoris Popov error = smb_usr_openshare(sdp->sd_vc, 230*afe09751SDavide Italiano (struct smbioc_oshare*)data, scred, &ssp); 231681a5bbeSBoris Popov if (error) 232681a5bbeSBoris Popov break; 233681a5bbeSBoris Popov sdp->sd_share = ssp; 2342b7da2dbSRobert Watson smb_share_unlock(ssp, 0); 235681a5bbeSBoris Popov sdp->sd_level = SMBL_SHARE; 236681a5bbeSBoris Popov break; 237681a5bbeSBoris Popov case SMBIOC_REQUEST: 238*afe09751SDavide Italiano if (sdp->sd_share == NULL) { 239*afe09751SDavide Italiano error = ENOTCONN; 240*afe09751SDavide Italiano goto out; 241*afe09751SDavide Italiano } 242681a5bbeSBoris Popov error = smb_usr_simplerequest(sdp->sd_share, 243*afe09751SDavide Italiano (struct smbioc_rq*)data, scred); 244681a5bbeSBoris Popov break; 245681a5bbeSBoris Popov case SMBIOC_T2RQ: 246*afe09751SDavide Italiano if (sdp->sd_share == NULL) { 247*afe09751SDavide Italiano error = ENOTCONN; 248*afe09751SDavide Italiano goto out; 249*afe09751SDavide Italiano } 250681a5bbeSBoris Popov error = smb_usr_t2request(sdp->sd_share, 251*afe09751SDavide Italiano (struct smbioc_t2rq*)data, scred); 252681a5bbeSBoris Popov break; 253681a5bbeSBoris Popov case SMBIOC_SETFLAGS: { 254681a5bbeSBoris Popov struct smbioc_flags *fl = (struct smbioc_flags*)data; 255681a5bbeSBoris Popov int on; 256681a5bbeSBoris Popov 257681a5bbeSBoris Popov if (fl->ioc_level == SMBL_VC) { 258681a5bbeSBoris Popov if (fl->ioc_mask & SMBV_PERMANENT) { 259681a5bbeSBoris Popov on = fl->ioc_flags & SMBV_PERMANENT; 260*afe09751SDavide Italiano if ((vcp = sdp->sd_vc) == NULL) { 261*afe09751SDavide Italiano error = ENOTCONN; 262*afe09751SDavide Italiano goto out; 263*afe09751SDavide Italiano } 264*afe09751SDavide Italiano error = smb_vc_get(vcp, LK_EXCLUSIVE, scred); 265681a5bbeSBoris Popov if (error) 266681a5bbeSBoris Popov break; 267681a5bbeSBoris Popov if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) { 268681a5bbeSBoris Popov vcp->obj.co_flags |= SMBV_PERMANENT; 269fce6fbfaSBoris Popov smb_vc_ref(vcp); 270681a5bbeSBoris Popov } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) { 271681a5bbeSBoris Popov vcp->obj.co_flags &= ~SMBV_PERMANENT; 272*afe09751SDavide Italiano smb_vc_rele(vcp, scred); 273681a5bbeSBoris Popov } 274*afe09751SDavide Italiano smb_vc_put(vcp, scred); 275681a5bbeSBoris Popov } else 276681a5bbeSBoris Popov error = EINVAL; 277681a5bbeSBoris Popov } else if (fl->ioc_level == SMBL_SHARE) { 278681a5bbeSBoris Popov if (fl->ioc_mask & SMBS_PERMANENT) { 279681a5bbeSBoris Popov on = fl->ioc_flags & SMBS_PERMANENT; 280*afe09751SDavide Italiano if ((ssp = sdp->sd_share) == NULL) { 281*afe09751SDavide Italiano error = ENOTCONN; 282*afe09751SDavide Italiano goto out; 283*afe09751SDavide Italiano } 284*afe09751SDavide Italiano error = smb_share_get(ssp, LK_EXCLUSIVE, scred); 285681a5bbeSBoris Popov if (error) 286681a5bbeSBoris Popov break; 287681a5bbeSBoris Popov if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) { 288681a5bbeSBoris Popov ssp->obj.co_flags |= SMBS_PERMANENT; 289fce6fbfaSBoris Popov smb_share_ref(ssp); 290681a5bbeSBoris Popov } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) { 291681a5bbeSBoris Popov ssp->obj.co_flags &= ~SMBS_PERMANENT; 292*afe09751SDavide Italiano smb_share_rele(ssp, scred); 293681a5bbeSBoris Popov } 294*afe09751SDavide Italiano smb_share_put(ssp, scred); 295681a5bbeSBoris Popov } else 296681a5bbeSBoris Popov error = EINVAL; 297681a5bbeSBoris Popov break; 298681a5bbeSBoris Popov } else 299681a5bbeSBoris Popov error = EINVAL; 300681a5bbeSBoris Popov break; 301681a5bbeSBoris Popov } 302681a5bbeSBoris Popov case SMBIOC_LOOKUP: 303*afe09751SDavide Italiano if (sdp->sd_vc || sdp->sd_share) { 304*afe09751SDavide Italiano error = EISCONN; 305*afe09751SDavide Italiano goto out; 306*afe09751SDavide Italiano } 307681a5bbeSBoris Popov vcp = NULL; 308681a5bbeSBoris Popov ssp = NULL; 309*afe09751SDavide Italiano error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp); 310681a5bbeSBoris Popov if (error) 311681a5bbeSBoris Popov break; 312681a5bbeSBoris Popov if (vcp) { 313681a5bbeSBoris Popov sdp->sd_vc = vcp; 3142b7da2dbSRobert Watson smb_vc_unlock(vcp, 0); 315681a5bbeSBoris Popov sdp->sd_level = SMBL_VC; 316681a5bbeSBoris Popov } 317681a5bbeSBoris Popov if (ssp) { 318681a5bbeSBoris Popov sdp->sd_share = ssp; 3192b7da2dbSRobert Watson smb_share_unlock(ssp, 0); 320681a5bbeSBoris Popov sdp->sd_level = SMBL_SHARE; 321681a5bbeSBoris Popov } 322681a5bbeSBoris Popov break; 323681a5bbeSBoris Popov case SMBIOC_READ: case SMBIOC_WRITE: { 324681a5bbeSBoris Popov struct smbioc_rw *rwrq = (struct smbioc_rw*)data; 325681a5bbeSBoris Popov struct uio auio; 326681a5bbeSBoris Popov struct iovec iov; 327681a5bbeSBoris Popov 328*afe09751SDavide Italiano if ((ssp = sdp->sd_share) == NULL) { 329*afe09751SDavide Italiano error = ENOTCONN; 330*afe09751SDavide Italiano goto out; 331*afe09751SDavide Italiano } 332681a5bbeSBoris Popov iov.iov_base = rwrq->ioc_base; 333681a5bbeSBoris Popov iov.iov_len = rwrq->ioc_cnt; 334681a5bbeSBoris Popov auio.uio_iov = &iov; 335681a5bbeSBoris Popov auio.uio_iovcnt = 1; 336681a5bbeSBoris Popov auio.uio_offset = rwrq->ioc_offset; 337681a5bbeSBoris Popov auio.uio_resid = rwrq->ioc_cnt; 338681a5bbeSBoris Popov auio.uio_segflg = UIO_USERSPACE; 339681a5bbeSBoris Popov auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE; 340fce6fbfaSBoris Popov auio.uio_td = td; 341681a5bbeSBoris Popov if (cmd == SMBIOC_READ) 342*afe09751SDavide Italiano error = smb_read(ssp, rwrq->ioc_fh, &auio, scred); 343681a5bbeSBoris Popov else 344*afe09751SDavide Italiano error = smb_write(ssp, rwrq->ioc_fh, &auio, scred); 345681a5bbeSBoris Popov rwrq->ioc_cnt -= auio.uio_resid; 346681a5bbeSBoris Popov break; 347681a5bbeSBoris Popov } 348681a5bbeSBoris Popov default: 349681a5bbeSBoris Popov error = ENODEV; 350681a5bbeSBoris Popov } 351*afe09751SDavide Italiano out: 352*afe09751SDavide Italiano free(scred, M_NSMBDEV); 353681a5bbeSBoris Popov return error; 354681a5bbeSBoris Popov } 355681a5bbeSBoris Popov 356681a5bbeSBoris Popov static int 357681a5bbeSBoris Popov nsmb_dev_load(module_t mod, int cmd, void *arg) 358681a5bbeSBoris Popov { 359681a5bbeSBoris Popov int error = 0; 360681a5bbeSBoris Popov 361681a5bbeSBoris Popov switch (cmd) { 362681a5bbeSBoris Popov case MOD_LOAD: 363681a5bbeSBoris Popov error = smb_sm_init(); 364681a5bbeSBoris Popov if (error) 365681a5bbeSBoris Popov break; 366681a5bbeSBoris Popov error = smb_iod_init(); 367681a5bbeSBoris Popov if (error) { 368681a5bbeSBoris Popov smb_sm_done(); 369681a5bbeSBoris Popov break; 370681a5bbeSBoris Popov } 371339f72c7SRobert Watson clone_setup(&nsmb_clones); 372681a5bbeSBoris Popov nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000); 373681a5bbeSBoris Popov break; 374681a5bbeSBoris Popov case MOD_UNLOAD: 375681a5bbeSBoris Popov smb_iod_done(); 376681a5bbeSBoris Popov error = smb_sm_done(); 37719caf6c0SBoris Popov if (error) 37819caf6c0SBoris Popov break; 379681a5bbeSBoris Popov EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag); 3805ee1ac46STai-hwa Liang drain_dev_clone_events(); 381339f72c7SRobert Watson clone_cleanup(&nsmb_clones); 3825ee1ac46STai-hwa Liang destroy_dev_drain(&nsmb_cdevsw); 383681a5bbeSBoris Popov break; 384681a5bbeSBoris Popov default: 385681a5bbeSBoris Popov error = EINVAL; 386681a5bbeSBoris Popov break; 387681a5bbeSBoris Popov } 388681a5bbeSBoris Popov return error; 389681a5bbeSBoris Popov } 390681a5bbeSBoris Popov 391681a5bbeSBoris Popov DEV_MODULE (dev_netsmb, nsmb_dev_load, 0); 392681a5bbeSBoris Popov 393681a5bbeSBoris Popov /* 394681a5bbeSBoris Popov * Convert a file descriptor to appropriate smb_share pointer 395681a5bbeSBoris Popov */ 396681a5bbeSBoris Popov static struct file* 397681a5bbeSBoris Popov nsmb_getfp(struct filedesc* fdp, int fd, int flag) 398681a5bbeSBoris Popov { 399681a5bbeSBoris Popov struct file* fp; 400681a5bbeSBoris Popov 4015e3f7694SRobert Watson FILEDESC_SLOCK(fdp); 40262021672SPawel Jakub Dawidek if (fd < 0 || fd >= fdp->fd_nfiles || 403681a5bbeSBoris Popov (fp = fdp->fd_ofiles[fd]) == NULL || 404426da3bcSAlfred Perlstein (fp->f_flag & flag) == 0) { 4055e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 406681a5bbeSBoris Popov return (NULL); 407426da3bcSAlfred Perlstein } 408426da3bcSAlfred Perlstein fhold(fp); 4095e3f7694SRobert Watson FILEDESC_SUNLOCK(fdp); 410681a5bbeSBoris Popov return (fp); 411681a5bbeSBoris Popov } 412681a5bbeSBoris Popov 413681a5bbeSBoris Popov int 414681a5bbeSBoris Popov smb_dev2share(int fd, int mode, struct smb_cred *scred, 415681a5bbeSBoris Popov struct smb_share **sspp) 416681a5bbeSBoris Popov { 417681a5bbeSBoris Popov struct file *fp; 418681a5bbeSBoris Popov struct vnode *vp; 419681a5bbeSBoris Popov struct smb_dev *sdp; 420681a5bbeSBoris Popov struct smb_share *ssp; 42189c9c53dSPoul-Henning Kamp struct cdev *dev; 422681a5bbeSBoris Popov int error; 423681a5bbeSBoris Popov 424fce6fbfaSBoris Popov fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE); 425fce6fbfaSBoris Popov if (fp == NULL) 426681a5bbeSBoris Popov return EBADF; 4273b6d9652SPoul-Henning Kamp vp = fp->f_vnode; 428426da3bcSAlfred Perlstein if (vp == NULL) { 429426da3bcSAlfred Perlstein fdrop(fp, curthread); 430681a5bbeSBoris Popov return EBADF; 431426da3bcSAlfred Perlstein } 43238bb5ccdSPoul-Henning Kamp if (vp->v_type != VCHR) { 433426da3bcSAlfred Perlstein fdrop(fp, curthread); 434681a5bbeSBoris Popov return EBADF; 435426da3bcSAlfred Perlstein } 43638bb5ccdSPoul-Henning Kamp dev = vp->v_rdev; 437681a5bbeSBoris Popov SMB_CHECKMINOR(dev); 438681a5bbeSBoris Popov ssp = sdp->sd_share; 439426da3bcSAlfred Perlstein if (ssp == NULL) { 440426da3bcSAlfred Perlstein fdrop(fp, curthread); 441681a5bbeSBoris Popov return ENOTCONN; 442426da3bcSAlfred Perlstein } 443681a5bbeSBoris Popov error = smb_share_get(ssp, LK_EXCLUSIVE, scred); 444426da3bcSAlfred Perlstein if (error == 0) 445681a5bbeSBoris Popov *sspp = ssp; 446426da3bcSAlfred Perlstein fdrop(fp, curthread); 447426da3bcSAlfred Perlstein return error; 448681a5bbeSBoris Popov } 449681a5bbeSBoris Popov 450