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: releng/5.3/sys/netsmb/smb_dev.c 130640 2004-06-17 17:16:53Z phk $");
   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,
   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 
  101 static void
  102 nsmb_dev_clone(void *arg, char *name, int namelen, struct cdev **dev)
  103 {
  104         int u;
  105 
  106         if (*dev != NULL)
  107                 return;
  108         if (dev_stdclone(name, NULL, NSMB_NAME, &u) != 1)
  109                 return;
  110         *dev = make_dev(&nsmb_cdevsw, unit2minor(u), 0, 0, 0600,
  111             NSMB_NAME"%d", u);
  112 }
  113 
  114 static int
  115 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  116 {
  117         struct smb_dev *sdp;
  118         struct ucred *cred = td->td_ucred;
  119         int s;
  120 
  121         sdp = SMB_GETDEV(dev);
  122         if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
  123                 return EBUSY;
  124         if (sdp == NULL) {
  125                 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
  126                 dev->si_drv1 = (void*)sdp;
  127         }
  128         /*
  129          * XXX: this is just crazy - make a device for an already passed device...
  130          * someone should take care of it.
  131          */
  132         if ((dev->si_flags & SI_NAMED) == 0)
  133                 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
  134                     NSMB_NAME"%d", dev2unit(dev));
  135         bzero(sdp, sizeof(*sdp));
  136 /*
  137         STAILQ_INIT(&sdp->sd_rqlist);
  138         STAILQ_INIT(&sdp->sd_rplist);
  139         bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
  140 */
  141         s = splimp();
  142         sdp->sd_level = -1;
  143         sdp->sd_flags |= NSMBFL_OPEN;
  144         splx(s);
  145         return 0;
  146 }
  147 
  148 static int
  149 nsmb_dev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
  150 {
  151         struct smb_dev *sdp;
  152         struct smb_vc *vcp;
  153         struct smb_share *ssp;
  154         struct smb_cred scred;
  155         int s;
  156 
  157         SMB_CHECKMINOR(dev);
  158         s = splimp();
  159         if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
  160                 splx(s);
  161                 return EBADF;
  162         }
  163         smb_makescred(&scred, td, NULL);
  164         ssp = sdp->sd_share;
  165         if (ssp != NULL)
  166                 smb_share_rele(ssp, &scred);
  167         vcp = sdp->sd_vc;
  168         if (vcp != NULL)
  169                 smb_vc_rele(vcp, &scred);
  170 /*
  171         smb_flushq(&sdp->sd_rqlist);
  172         smb_flushq(&sdp->sd_rplist);
  173 */
  174         dev->si_drv1 = NULL;
  175         free(sdp, M_NSMBDEV);
  176         destroy_dev(dev);
  177         splx(s);
  178         return 0;
  179 }
  180 
  181 
  182 static int
  183 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  184 {
  185         struct smb_dev *sdp;
  186         struct smb_vc *vcp;
  187         struct smb_share *ssp;
  188         struct smb_cred scred;
  189         int error = 0;
  190 
  191         SMB_CHECKMINOR(dev);
  192         if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
  193                 return EBADF;
  194 
  195         smb_makescred(&scred, td, NULL);
  196         switch (cmd) {
  197             case SMBIOC_OPENSESSION:
  198                 if (sdp->sd_vc)
  199                         return EISCONN;
  200                 error = smb_usr_opensession((struct smbioc_ossn*)data,
  201                     &scred, &vcp);
  202                 if (error)
  203                         break;
  204                 sdp->sd_vc = vcp;
  205                 smb_vc_unlock(vcp, 0, td);
  206                 sdp->sd_level = SMBL_VC;
  207                 break;
  208             case SMBIOC_OPENSHARE:
  209                 if (sdp->sd_share)
  210                         return EISCONN;
  211                 if (sdp->sd_vc == NULL)
  212                         return ENOTCONN;
  213                 error = smb_usr_openshare(sdp->sd_vc,
  214                     (struct smbioc_oshare*)data, &scred, &ssp);
  215                 if (error)
  216                         break;
  217                 sdp->sd_share = ssp;
  218                 smb_share_unlock(ssp, 0, td);
  219                 sdp->sd_level = SMBL_SHARE;
  220                 break;
  221             case SMBIOC_REQUEST:
  222                 if (sdp->sd_share == NULL)
  223                         return ENOTCONN;
  224                 error = smb_usr_simplerequest(sdp->sd_share,
  225                     (struct smbioc_rq*)data, &scred);
  226                 break;
  227             case SMBIOC_T2RQ:
  228                 if (sdp->sd_share == NULL)
  229                         return ENOTCONN;
  230                 error = smb_usr_t2request(sdp->sd_share,
  231                     (struct smbioc_t2rq*)data, &scred);
  232                 break;
  233             case SMBIOC_SETFLAGS: {
  234                 struct smbioc_flags *fl = (struct smbioc_flags*)data;
  235                 int on;
  236         
  237                 if (fl->ioc_level == SMBL_VC) {
  238                         if (fl->ioc_mask & SMBV_PERMANENT) {
  239                                 on = fl->ioc_flags & SMBV_PERMANENT;
  240                                 if ((vcp = sdp->sd_vc) == NULL)
  241                                         return ENOTCONN;
  242                                 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
  243                                 if (error)
  244                                         break;
  245                                 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
  246                                         vcp->obj.co_flags |= SMBV_PERMANENT;
  247                                         smb_vc_ref(vcp);
  248                                 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
  249                                         vcp->obj.co_flags &= ~SMBV_PERMANENT;
  250                                         smb_vc_rele(vcp, &scred);
  251                                 }
  252                                 smb_vc_put(vcp, &scred);
  253                         } else
  254                                 error = EINVAL;
  255                 } else if (fl->ioc_level == SMBL_SHARE) {
  256                         if (fl->ioc_mask & SMBS_PERMANENT) {
  257                                 on = fl->ioc_flags & SMBS_PERMANENT;
  258                                 if ((ssp = sdp->sd_share) == NULL)
  259                                         return ENOTCONN;
  260                                 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
  261                                 if (error)
  262                                         break;
  263                                 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
  264                                         ssp->obj.co_flags |= SMBS_PERMANENT;
  265                                         smb_share_ref(ssp);
  266                                 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
  267                                         ssp->obj.co_flags &= ~SMBS_PERMANENT;
  268                                         smb_share_rele(ssp, &scred);
  269                                 }
  270                                 smb_share_put(ssp, &scred);
  271                         } else
  272                                 error = EINVAL;
  273                         break;
  274                 } else
  275                         error = EINVAL;
  276                 break;
  277             }
  278             case SMBIOC_LOOKUP:
  279                 if (sdp->sd_vc || sdp->sd_share)
  280                         return EISCONN;
  281                 vcp = NULL;
  282                 ssp = NULL;
  283                 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
  284                 if (error)
  285                         break;
  286                 if (vcp) {
  287                         sdp->sd_vc = vcp;
  288                         smb_vc_unlock(vcp, 0, td);
  289                         sdp->sd_level = SMBL_VC;
  290                 }
  291                 if (ssp) {
  292                         sdp->sd_share = ssp;
  293                         smb_share_unlock(ssp, 0, td);
  294                         sdp->sd_level = SMBL_SHARE;
  295                 }
  296                 break;
  297             case SMBIOC_READ: case SMBIOC_WRITE: {
  298                 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
  299                 struct uio auio;
  300                 struct iovec iov;
  301         
  302                 if ((ssp = sdp->sd_share) == NULL)
  303                         return ENOTCONN;
  304                 iov.iov_base = rwrq->ioc_base;
  305                 iov.iov_len = rwrq->ioc_cnt;
  306                 auio.uio_iov = &iov;
  307                 auio.uio_iovcnt = 1;
  308                 auio.uio_offset = rwrq->ioc_offset;
  309                 auio.uio_resid = rwrq->ioc_cnt;
  310                 auio.uio_segflg = UIO_USERSPACE;
  311                 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
  312                 auio.uio_td = td;
  313                 if (cmd == SMBIOC_READ)
  314                         error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
  315                 else
  316                         error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
  317                 rwrq->ioc_cnt -= auio.uio_resid;
  318                 break;
  319             }
  320             default:
  321                 error = ENODEV;
  322         }
  323         return error;
  324 }
  325 
  326 static int
  327 nsmb_dev_load(module_t mod, int cmd, void *arg)
  328 {
  329         int error = 0;
  330 
  331         switch (cmd) {
  332             case MOD_LOAD:
  333                 error = smb_sm_init();
  334                 if (error)
  335                         break;
  336                 error = smb_iod_init();
  337                 if (error) {
  338                         smb_sm_done();
  339                         break;
  340                 }
  341                 nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
  342                 printf("netsmb_dev: loaded\n");
  343                 break;
  344             case MOD_UNLOAD:
  345                 smb_iod_done();
  346                 error = smb_sm_done();
  347                 error = 0;
  348                 EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
  349                 printf("netsmb_dev: unloaded\n");
  350                 break;
  351             default:
  352                 error = EINVAL;
  353                 break;
  354         }
  355         return error;
  356 }
  357 
  358 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
  359 
  360 /*
  361  * Convert a file descriptor to appropriate smb_share pointer
  362  */
  363 static struct file*
  364 nsmb_getfp(struct filedesc* fdp, int fd, int flag)
  365 {
  366         struct file* fp;
  367 
  368         FILEDESC_LOCK(fdp);
  369         if (((u_int)fd) >= fdp->fd_nfiles ||
  370             (fp = fdp->fd_ofiles[fd]) == NULL ||
  371             (fp->f_flag & flag) == 0) {
  372                 FILEDESC_UNLOCK(fdp);
  373                 return (NULL);
  374         }
  375         fhold(fp);
  376         FILEDESC_UNLOCK(fdp);
  377         return (fp);
  378 }
  379 
  380 int
  381 smb_dev2share(int fd, int mode, struct smb_cred *scred,
  382         struct smb_share **sspp)
  383 {
  384         struct file *fp;
  385         struct vnode *vp;
  386         struct smb_dev *sdp;
  387         struct smb_share *ssp;
  388         struct cdev *dev;
  389         int error;
  390 
  391         fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE);
  392         if (fp == NULL)
  393                 return EBADF;
  394         vp = fp->f_vnode;
  395         if (vp == NULL) {
  396                 fdrop(fp, curthread);
  397                 return EBADF;
  398         }
  399         dev = vn_todev(vp);
  400         if (dev == NULL) {
  401                 fdrop(fp, curthread);
  402                 return EBADF;
  403         }
  404         SMB_CHECKMINOR(dev);
  405         ssp = sdp->sd_share;
  406         if (ssp == NULL) {
  407                 fdrop(fp, curthread);
  408                 return ENOTCONN;
  409         }
  410         error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
  411         if (error == 0) 
  412                 *sspp = ssp;
  413         fdrop(fp, curthread);
  414         return error;
  415 }
  416 

Cache object: 145d254f1a300fbe4c1deda9e985d97f


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