The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netsmb/smb_dev.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2000-2001 Boris Popov
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    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.
   19  *
   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
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD$");
   35 
   36 #include <sys/param.h>
   37 #include <sys/kernel.h>
   38 #include <sys/module.h>
   39 #include <sys/systm.h>
   40 #include <sys/conf.h>
   41 #include <sys/fcntl.h>
   42 #include <sys/ioccom.h>
   43 #include <sys/lock.h>
   44 #include <sys/malloc.h>
   45 #include <sys/file.h>           /* Must come after sys/malloc.h */
   46 #include <sys/filedesc.h>
   47 #include <sys/mbuf.h>
   48 #include <sys/poll.h>
   49 #include <sys/proc.h>
   50 #include <sys/select.h>
   51 #include <sys/socket.h>
   52 #include <sys/socketvar.h>
   53 #include <sys/sysctl.h>
   54 #include <sys/uio.h>
   55 #include <sys/vnode.h>
   56 
   57 #include <net/if.h>
   58 
   59 #include <netsmb/smb.h>
   60 #include <netsmb/smb_conn.h>
   61 #include <netsmb/smb_subr.h>
   62 #include <netsmb/smb_dev.h>
   63 
   64 #define SMB_GETDEV(dev)         ((struct smb_dev*)(dev)->si_drv1)
   65 #define SMB_CHECKMINOR(dev)     do { \
   66                                     sdp = SMB_GETDEV(dev); \
   67                                     if (sdp == NULL) return ENXIO; \
   68                                 } while(0)
   69 
   70 static d_open_t  nsmb_dev_open;
   71 static d_close_t nsmb_dev_close;
   72 static d_ioctl_t nsmb_dev_ioctl;
   73 
   74 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
   75 MODULE_VERSION(netsmb, NSMB_VERSION);
   76 
   77 static int smb_version = NSMB_VERSION;
   78 
   79 
   80 SYSCTL_DECL(_net_smb);
   81 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
   82 
   83 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
   84 
   85 
   86 /*
   87 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
   88 */
   89 
   90 static struct cdevsw nsmb_cdevsw = {
   91         .d_version =    D_VERSION,
   92         .d_flags =      D_NEEDGIANT | D_NEEDMINOR,
   93         .d_open =       nsmb_dev_open,
   94         .d_close =      nsmb_dev_close,
   95         .d_ioctl =      nsmb_dev_ioctl,
   96         .d_name =       NSMB_NAME
   97 };
   98 
   99 static eventhandler_tag  nsmb_dev_tag;
  100 static struct clonedevs *nsmb_clones;
  101 
  102 static void
  103 nsmb_dev_clone(void *arg, struct ucred *cred, char *name, int namelen,
  104     struct cdev **dev)
  105 {
  106         int i, u;
  107 
  108         if (*dev != NULL)
  109                 return;
  110 
  111         if (strcmp(name, NSMB_NAME) == 0)
  112                 u = -1;
  113         else if (dev_stdclone(name, NULL, NSMB_NAME, &u) != 1)
  114                 return;
  115         i = clone_create(&nsmb_clones, &nsmb_cdevsw, &u, dev, 0);
  116         if (i) {
  117                 *dev = make_dev(&nsmb_cdevsw, u, UID_ROOT, GID_WHEEL, 0600,
  118                     "%s%d", NSMB_NAME, u);
  119                 if (*dev != NULL) {
  120                         dev_ref(*dev);
  121                         (*dev)->si_flags |= SI_CHEAPCLONE;
  122                 }
  123         }
  124 }
  125 
  126 static int
  127 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  128 {
  129         struct smb_dev *sdp;
  130         struct ucred *cred = td->td_ucred;
  131         int s;
  132 
  133         sdp = SMB_GETDEV(dev);
  134         if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
  135                 return EBUSY;
  136         if (sdp == NULL) {
  137                 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
  138                 dev->si_drv1 = (void*)sdp;
  139         }
  140         /*
  141          * XXX: this is just crazy - make a device for an already passed device...
  142          * someone should take care of it.
  143          */
  144         if ((dev->si_flags & SI_NAMED) == 0)
  145                 make_dev(&nsmb_cdevsw, dev2unit(dev), cred->cr_uid,
  146                     cred->cr_gid, 0700, NSMB_NAME"%d", dev2unit(dev));
  147         bzero(sdp, sizeof(*sdp));
  148 /*
  149         STAILQ_INIT(&sdp->sd_rqlist);
  150         STAILQ_INIT(&sdp->sd_rplist);
  151         bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
  152 */
  153         s = splimp();
  154         sdp->sd_level = -1;
  155         sdp->sd_flags |= NSMBFL_OPEN;
  156         splx(s);
  157         return 0;
  158 }
  159 
  160 static int
  161 nsmb_dev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
  162 {
  163         struct smb_dev *sdp;
  164         struct smb_vc *vcp;
  165         struct smb_share *ssp;
  166         struct smb_cred scred;
  167         int s;
  168 
  169         SMB_CHECKMINOR(dev);
  170         s = splimp();
  171         if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
  172                 splx(s);
  173                 return EBADF;
  174         }
  175         smb_makescred(&scred, td, NULL);
  176         ssp = sdp->sd_share;
  177         if (ssp != NULL)
  178                 smb_share_rele(ssp, &scred);
  179         vcp = sdp->sd_vc;
  180         if (vcp != NULL)
  181                 smb_vc_rele(vcp, &scred);
  182 /*
  183         smb_flushq(&sdp->sd_rqlist);
  184         smb_flushq(&sdp->sd_rplist);
  185 */
  186         dev->si_drv1 = NULL;
  187         free(sdp, M_NSMBDEV);
  188         destroy_dev_sched(dev);
  189         splx(s);
  190         return 0;
  191 }
  192 
  193 
  194 static int
  195 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  196 {
  197         struct smb_dev *sdp;
  198         struct smb_vc *vcp;
  199         struct smb_share *ssp;
  200         struct smb_cred scred;
  201         int error = 0;
  202 
  203         SMB_CHECKMINOR(dev);
  204         if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
  205                 return EBADF;
  206 
  207         smb_makescred(&scred, td, NULL);
  208         switch (cmd) {
  209             case SMBIOC_OPENSESSION:
  210                 if (sdp->sd_vc)
  211                         return EISCONN;
  212                 error = smb_usr_opensession((struct smbioc_ossn*)data,
  213                     &scred, &vcp);
  214                 if (error)
  215                         break;
  216                 sdp->sd_vc = vcp;
  217                 smb_vc_unlock(vcp, 0);
  218                 sdp->sd_level = SMBL_VC;
  219                 break;
  220             case SMBIOC_OPENSHARE:
  221                 if (sdp->sd_share)
  222                         return EISCONN;
  223                 if (sdp->sd_vc == NULL)
  224                         return ENOTCONN;
  225                 error = smb_usr_openshare(sdp->sd_vc,
  226                     (struct smbioc_oshare*)data, &scred, &ssp);
  227                 if (error)
  228                         break;
  229                 sdp->sd_share = ssp;
  230                 smb_share_unlock(ssp, 0);
  231                 sdp->sd_level = SMBL_SHARE;
  232                 break;
  233             case SMBIOC_REQUEST:
  234                 if (sdp->sd_share == NULL)
  235                         return ENOTCONN;
  236                 error = smb_usr_simplerequest(sdp->sd_share,
  237                     (struct smbioc_rq*)data, &scred);
  238                 break;
  239             case SMBIOC_T2RQ:
  240                 if (sdp->sd_share == NULL)
  241                         return ENOTCONN;
  242                 error = smb_usr_t2request(sdp->sd_share,
  243                     (struct smbioc_t2rq*)data, &scred);
  244                 break;
  245             case SMBIOC_SETFLAGS: {
  246                 struct smbioc_flags *fl = (struct smbioc_flags*)data;
  247                 int on;
  248         
  249                 if (fl->ioc_level == SMBL_VC) {
  250                         if (fl->ioc_mask & SMBV_PERMANENT) {
  251                                 on = fl->ioc_flags & SMBV_PERMANENT;
  252                                 if ((vcp = sdp->sd_vc) == NULL)
  253                                         return ENOTCONN;
  254                                 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
  255                                 if (error)
  256                                         break;
  257                                 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
  258                                         vcp->obj.co_flags |= SMBV_PERMANENT;
  259                                         smb_vc_ref(vcp);
  260                                 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
  261                                         vcp->obj.co_flags &= ~SMBV_PERMANENT;
  262                                         smb_vc_rele(vcp, &scred);
  263                                 }
  264                                 smb_vc_put(vcp, &scred);
  265                         } else
  266                                 error = EINVAL;
  267                 } else if (fl->ioc_level == SMBL_SHARE) {
  268                         if (fl->ioc_mask & SMBS_PERMANENT) {
  269                                 on = fl->ioc_flags & SMBS_PERMANENT;
  270                                 if ((ssp = sdp->sd_share) == NULL)
  271                                         return ENOTCONN;
  272                                 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
  273                                 if (error)
  274                                         break;
  275                                 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
  276                                         ssp->obj.co_flags |= SMBS_PERMANENT;
  277                                         smb_share_ref(ssp);
  278                                 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
  279                                         ssp->obj.co_flags &= ~SMBS_PERMANENT;
  280                                         smb_share_rele(ssp, &scred);
  281                                 }
  282                                 smb_share_put(ssp, &scred);
  283                         } else
  284                                 error = EINVAL;
  285                         break;
  286                 } else
  287                         error = EINVAL;
  288                 break;
  289             }
  290             case SMBIOC_LOOKUP:
  291                 if (sdp->sd_vc || sdp->sd_share)
  292                         return EISCONN;
  293                 vcp = NULL;
  294                 ssp = NULL;
  295                 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
  296                 if (error)
  297                         break;
  298                 if (vcp) {
  299                         sdp->sd_vc = vcp;
  300                         smb_vc_unlock(vcp, 0);
  301                         sdp->sd_level = SMBL_VC;
  302                 }
  303                 if (ssp) {
  304                         sdp->sd_share = ssp;
  305                         smb_share_unlock(ssp, 0);
  306                         sdp->sd_level = SMBL_SHARE;
  307                 }
  308                 break;
  309             case SMBIOC_READ: case SMBIOC_WRITE: {
  310                 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
  311                 struct uio auio;
  312                 struct iovec iov;
  313         
  314                 if ((ssp = sdp->sd_share) == NULL)
  315                         return ENOTCONN;
  316                 iov.iov_base = rwrq->ioc_base;
  317                 iov.iov_len = rwrq->ioc_cnt;
  318                 auio.uio_iov = &iov;
  319                 auio.uio_iovcnt = 1;
  320                 auio.uio_offset = rwrq->ioc_offset;
  321                 auio.uio_resid = rwrq->ioc_cnt;
  322                 auio.uio_segflg = UIO_USERSPACE;
  323                 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
  324                 auio.uio_td = td;
  325                 if (cmd == SMBIOC_READ)
  326                         error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
  327                 else
  328                         error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
  329                 rwrq->ioc_cnt -= auio.uio_resid;
  330                 break;
  331             }
  332             default:
  333                 error = ENODEV;
  334         }
  335         return error;
  336 }
  337 
  338 static int
  339 nsmb_dev_load(module_t mod, int cmd, void *arg)
  340 {
  341         int error = 0;
  342 
  343         switch (cmd) {
  344             case MOD_LOAD:
  345                 error = smb_sm_init();
  346                 if (error)
  347                         break;
  348                 error = smb_iod_init();
  349                 if (error) {
  350                         smb_sm_done();
  351                         break;
  352                 }
  353                 clone_setup(&nsmb_clones);
  354                 nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
  355                 printf("netsmb_dev: loaded\n");
  356                 break;
  357             case MOD_UNLOAD:
  358                 smb_iod_done();
  359                 error = smb_sm_done();
  360                 if (error)
  361                         break;
  362                 EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
  363                 drain_dev_clone_events();
  364                 clone_cleanup(&nsmb_clones);
  365                 destroy_dev_drain(&nsmb_cdevsw);
  366                 printf("netsmb_dev: unloaded\n");
  367                 break;
  368             default:
  369                 error = EINVAL;
  370                 break;
  371         }
  372         return error;
  373 }
  374 
  375 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
  376 
  377 /*
  378  * Convert a file descriptor to appropriate smb_share pointer
  379  */
  380 static struct file*
  381 nsmb_getfp(struct filedesc* fdp, int fd, int flag)
  382 {
  383         struct file* fp;
  384 
  385         FILEDESC_SLOCK(fdp);
  386         if (((u_int)fd) >= fdp->fd_nfiles ||
  387             (fp = fdp->fd_ofiles[fd]) == NULL ||
  388             (fp->f_flag & flag) == 0) {
  389                 FILEDESC_SUNLOCK(fdp);
  390                 return (NULL);
  391         }
  392         fhold(fp);
  393         FILEDESC_SUNLOCK(fdp);
  394         return (fp);
  395 }
  396 
  397 int
  398 smb_dev2share(int fd, int mode, struct smb_cred *scred,
  399         struct smb_share **sspp)
  400 {
  401         struct file *fp;
  402         struct vnode *vp;
  403         struct smb_dev *sdp;
  404         struct smb_share *ssp;
  405         struct cdev *dev;
  406         int error;
  407 
  408         fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE);
  409         if (fp == NULL)
  410                 return EBADF;
  411         vp = fp->f_vnode;
  412         if (vp == NULL) {
  413                 fdrop(fp, curthread);
  414                 return EBADF;
  415         }
  416         if (vp->v_type != VCHR) {
  417                 fdrop(fp, curthread);
  418                 return EBADF;
  419         }
  420         dev = vp->v_rdev;
  421         SMB_CHECKMINOR(dev);
  422         ssp = sdp->sd_share;
  423         if (ssp == NULL) {
  424                 fdrop(fp, curthread);
  425                 return ENOTCONN;
  426         }
  427         error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
  428         if (error == 0) 
  429                 *sspp = ssp;
  430         fdrop(fp, curthread);
  431         return error;
  432 }
  433 

Cache object: 585ebc2dbb8c4bcc48dcef449421cf5b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.