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/systm.h>
   39 #include <sys/conf.h>
   40 #include <sys/fcntl.h>
   41 #include <sys/ioccom.h>
   42 #include <sys/malloc.h>
   43 #include <sys/file.h>           /* Must come after sys/malloc.h */
   44 #include <sys/poll.h>
   45 #include <sys/proc.h>
   46 #include <sys/select.h>
   47 #include <sys/socket.h>
   48 #include <sys/socketvar.h>
   49 #include <sys/sysctl.h>
   50 #include <sys/uio.h>
   51 #include <sys/vnode.h>
   52 
   53 #include <net/if.h>
   54 
   55 #include <netsmb/smb.h>
   56 #include <netsmb/smb_conn.h>
   57 #include <netsmb/smb_subr.h>
   58 #include <netsmb/smb_dev.h>
   59 
   60 #define SMB_GETDEV(dev)         ((struct smb_dev*)(dev)->si_drv1)
   61 #define SMB_CHECKMINOR(dev)     do { \
   62                                     sdp = SMB_GETDEV(dev); \
   63                                     if (sdp == NULL) return ENXIO; \
   64                                 } while(0)
   65 
   66 static d_open_t  nsmb_dev_open;
   67 static d_close_t nsmb_dev_close;
   68 static d_ioctl_t nsmb_dev_ioctl;
   69 
   70 MODULE_VERSION(netsmb, NSMB_VERSION);
   71 
   72 #define SI_NAMED        0
   73 
   74 static int smb_version = NSMB_VERSION;
   75 
   76 
   77 SYSCTL_DECL(_net_smb);
   78 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
   79 
   80 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
   81 
   82 
   83 /*
   84 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
   85 */
   86 
   87 static struct cdevsw nsmb_cdevsw = {
   88         /* open */      nsmb_dev_open,
   89         /* close */     nsmb_dev_close,
   90         /* read */      noread,
   91         /* write */     nowrite,
   92         /* ioctl */     nsmb_dev_ioctl,
   93         /* poll */      nopoll,
   94         /* mmap */      nommap,
   95         /* strategy */  nostrategy,
   96         /* name */      NSMB_NAME,
   97         /* maj */       NSMB_MAJOR,
   98         /* dump */      nodump,
   99         /* psize */     nopsize,
  100         /* flags */     0,
  101         /* bmaj */      -1
  102 };
  103 
  104 
  105 static int
  106 nsmb_dev_open(dev_t dev, int oflags, int devtype, struct proc *p)
  107 {
  108         struct smb_dev *sdp;
  109         struct ucred *cred = p->p_ucred;
  110         int s;
  111 
  112         sdp = SMB_GETDEV(dev);
  113         if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
  114                 return EBUSY;
  115         if (sdp == NULL) {
  116                 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
  117                 dev->si_drv1 = (void*)sdp;
  118         }
  119         /*
  120          * XXX: this is just crazy - make a device for an already passed device...
  121          * someone should take care of it.
  122          */
  123         if ((dev->si_flags & SI_NAMED) == 0)
  124                 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
  125                     NSMB_NAME"%d", lminor(dev));
  126         bzero(sdp, sizeof(*sdp));
  127 /*
  128         STAILQ_INIT(&sdp->sd_rqlist);
  129         STAILQ_INIT(&sdp->sd_rplist);
  130         bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
  131 */
  132         s = splimp();
  133         sdp->sd_level = -1;
  134         sdp->sd_flags |= NSMBFL_OPEN;
  135         splx(s);
  136         return 0;
  137 }
  138 
  139 static int
  140 nsmb_dev_close(dev_t dev, int flag, int fmt, struct proc *p)
  141 {
  142         struct smb_dev *sdp;
  143         struct smb_vc *vcp;
  144         struct smb_share *ssp;
  145         struct smb_cred scred;
  146         int s;
  147 
  148         SMB_CHECKMINOR(dev);
  149         s = splimp();
  150         if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
  151                 splx(s);
  152                 return EBADF;
  153         }
  154         smb_makescred(&scred, p, NULL);
  155         ssp = sdp->sd_share;
  156         if (ssp != NULL)
  157                 smb_share_rele(ssp, &scred);
  158         vcp = sdp->sd_vc;
  159         if (vcp != NULL)
  160                 smb_vc_rele(vcp, &scred);
  161 /*
  162         smb_flushq(&sdp->sd_rqlist);
  163         smb_flushq(&sdp->sd_rplist);
  164 */
  165         dev->si_drv1 = NULL;
  166         free(sdp, M_NSMBDEV);
  167         destroy_dev(dev);
  168         splx(s);
  169         return 0;
  170 }
  171 
  172 
  173 static int
  174 nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
  175 {
  176         struct smb_dev *sdp;
  177         struct smb_vc *vcp;
  178         struct smb_share *ssp;
  179         struct smb_cred scred;
  180         int error = 0;
  181 
  182         SMB_CHECKMINOR(dev);
  183         if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
  184                 return EBADF;
  185 
  186         smb_makescred(&scred, p, NULL);
  187         switch (cmd) {
  188             case SMBIOC_OPENSESSION:
  189                 if (sdp->sd_vc)
  190                         return EISCONN;
  191                 error = smb_usr_opensession((struct smbioc_ossn*)data,
  192                     &scred, &vcp);
  193                 if (error)
  194                         break;
  195                 sdp->sd_vc = vcp;
  196                 smb_vc_unlock(vcp, 0, p);
  197                 sdp->sd_level = SMBL_VC;
  198                 break;
  199             case SMBIOC_OPENSHARE:
  200                 if (sdp->sd_share)
  201                         return EISCONN;
  202                 if (sdp->sd_vc == NULL)
  203                         return ENOTCONN;
  204                 error = smb_usr_openshare(sdp->sd_vc,
  205                     (struct smbioc_oshare*)data, &scred, &ssp);
  206                 if (error)
  207                         break;
  208                 sdp->sd_share = ssp;
  209                 smb_share_unlock(ssp, 0, p);
  210                 sdp->sd_level = SMBL_SHARE;
  211                 break;
  212             case SMBIOC_REQUEST:
  213                 if (sdp->sd_share == NULL)
  214                         return ENOTCONN;
  215                 error = smb_usr_simplerequest(sdp->sd_share,
  216                     (struct smbioc_rq*)data, &scred);
  217                 break;
  218             case SMBIOC_T2RQ:
  219                 if (sdp->sd_share == NULL)
  220                         return ENOTCONN;
  221                 error = smb_usr_t2request(sdp->sd_share,
  222                     (struct smbioc_t2rq*)data, &scred);
  223                 break;
  224             case SMBIOC_SETFLAGS: {
  225                 struct smbioc_flags *fl = (struct smbioc_flags*)data;
  226                 int on;
  227         
  228                 if (fl->ioc_level == SMBL_VC) {
  229                         if (fl->ioc_mask & SMBV_PERMANENT) {
  230                                 on = fl->ioc_flags & SMBV_PERMANENT;
  231                                 if ((vcp = sdp->sd_vc) == NULL)
  232                                         return ENOTCONN;
  233                                 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
  234                                 if (error)
  235                                         break;
  236                                 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
  237                                         vcp->obj.co_flags |= SMBV_PERMANENT;
  238                                         smb_vc_ref(vcp, p);
  239                                 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
  240                                         vcp->obj.co_flags &= ~SMBV_PERMANENT;
  241                                         smb_vc_rele(vcp, &scred);
  242                                 }
  243                                 smb_vc_put(vcp, &scred);
  244                         } else
  245                                 error = EINVAL;
  246                 } else if (fl->ioc_level == SMBL_SHARE) {
  247                         if (fl->ioc_mask & SMBS_PERMANENT) {
  248                                 on = fl->ioc_flags & SMBS_PERMANENT;
  249                                 if ((ssp = sdp->sd_share) == NULL)
  250                                         return ENOTCONN;
  251                                 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
  252                                 if (error)
  253                                         break;
  254                                 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
  255                                         ssp->obj.co_flags |= SMBS_PERMANENT;
  256                                         smb_share_ref(ssp, p);
  257                                 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
  258                                         ssp->obj.co_flags &= ~SMBS_PERMANENT;
  259                                         smb_share_rele(ssp, &scred);
  260                                 }
  261                                 smb_share_put(ssp, &scred);
  262                         } else
  263                                 error = EINVAL;
  264                         break;
  265                 } else
  266                         error = EINVAL;
  267                 break;
  268             }
  269             case SMBIOC_LOOKUP:
  270                 if (sdp->sd_vc || sdp->sd_share)
  271                         return EISCONN;
  272                 vcp = NULL;
  273                 ssp = NULL;
  274                 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
  275                 if (error)
  276                         break;
  277                 if (vcp) {
  278                         sdp->sd_vc = vcp;
  279                         smb_vc_unlock(vcp, 0, p);
  280                         sdp->sd_level = SMBL_VC;
  281                 }
  282                 if (ssp) {
  283                         sdp->sd_share = ssp;
  284                         smb_share_unlock(ssp, 0, p);
  285                         sdp->sd_level = SMBL_SHARE;
  286                 }
  287                 break;
  288             case SMBIOC_READ: case SMBIOC_WRITE: {
  289                 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
  290                 struct uio auio;
  291                 struct iovec iov;
  292         
  293                 if ((ssp = sdp->sd_share) == NULL)
  294                         return ENOTCONN;
  295                 iov.iov_base = rwrq->ioc_base;
  296                 iov.iov_len = rwrq->ioc_cnt;
  297                 auio.uio_iov = &iov;
  298                 auio.uio_iovcnt = 1;
  299                 auio.uio_offset = rwrq->ioc_offset;
  300                 auio.uio_resid = rwrq->ioc_cnt;
  301                 auio.uio_segflg = UIO_USERSPACE;
  302                 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
  303                 auio.uio_procp = p;
  304                 if (cmd == SMBIOC_READ)
  305                         error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
  306                 else
  307                         error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
  308                 rwrq->ioc_cnt -= auio.uio_resid;
  309                 break;
  310             }
  311             default:
  312                 error = ENODEV;
  313         }
  314         return error;
  315 }
  316 
  317 static int
  318 nsmb_dev_load(module_t mod, int cmd, void *arg)
  319 {
  320         int error = 0;
  321 
  322         switch (cmd) {
  323             case MOD_LOAD:
  324                 error = smb_checksmp();
  325                 if (error)
  326                         break;
  327                 error = smb_sm_init();
  328                 if (error)
  329                         break;
  330                 error = smb_iod_init();
  331                 if (error) {
  332                         smb_sm_done();
  333                         break;
  334                 }
  335                 cdevsw_add(&nsmb_cdevsw);
  336                 printf("netsmb_dev: loaded\n");
  337                 break;
  338             case MOD_UNLOAD:
  339                 smb_iod_done();
  340                 error = smb_sm_done();
  341                 error = 0;
  342                 cdevsw_remove(&nsmb_cdevsw);
  343                 printf("netsmb_dev: unloaded\n");
  344                 break;
  345             default:
  346                 error = EINVAL;
  347                 break;
  348         }
  349         return error;
  350 }
  351 
  352 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
  353 
  354 
  355 /*
  356  * Convert a file descriptor to appropriate smb_share pointer
  357  */
  358 static struct file*
  359 nsmb_getfp(struct filedesc* fdp, int fd, int flag)
  360 {
  361         struct file* fp;
  362 
  363         if (((u_int)fd) >= fdp->fd_nfiles ||
  364             (fp = fdp->fd_ofiles[fd]) == NULL ||
  365             (fp->f_flag & flag) == 0)
  366                 return (NULL);
  367         return (fp);
  368 }
  369 
  370 int
  371 smb_dev2share(int fd, int mode, struct smb_cred *scred,
  372         struct smb_share **sspp)
  373 {
  374         struct file *fp;
  375         struct vnode *vp;
  376         struct smb_dev *sdp;
  377         struct smb_share *ssp;
  378         dev_t dev;
  379         int error;
  380 
  381         if ((fp = nsmb_getfp(scred->scr_p->p_fd, fd, FREAD | FWRITE)) == NULL)
  382                 return EBADF;
  383         vp = (struct vnode*)fp->f_data;
  384         if (vp == NULL)
  385                 return EBADF;
  386         dev = vn_todev(vp);
  387         if (dev == NODEV)
  388                 return EBADF;
  389         SMB_CHECKMINOR(dev);
  390         ssp = sdp->sd_share;
  391         if (ssp == NULL)
  392                 return ENOTCONN;
  393         error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
  394         if (error)
  395                 return error;
  396         *sspp = ssp;
  397         return 0;
  398 }
  399 

Cache object: ca95d488816ea0f4b5d31d84308dfc4d


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