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

Cache object: 27eae3252a08103abfd1fd807f39a32f


[ 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.