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 /*      $NetBSD: smb_dev.c,v 1.18 2003/06/29 22:32:10 fvdl Exp $        */
    2 
    3 /*
    4  * Copyright (c) 2000-2001 Boris Popov
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the following acknowledgement:
   17  *    This product includes software developed by Boris Popov.
   18  * 4. Neither the name of the author nor the names of any co-contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  * FreeBSD: src/sys/netsmb/smb_dev.c,v 1.4 2001/12/02 08:47:29 bp Exp
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.18 2003/06/29 22:32:10 fvdl Exp $");
   39 
   40 #include <sys/param.h>
   41 #include <sys/kernel.h>
   42 #include <sys/systm.h>
   43 #include <sys/conf.h>
   44 #include <sys/fcntl.h>
   45 #include <sys/filedesc.h>
   46 #include <sys/ioccom.h>
   47 #include <sys/lock.h>
   48 #include <sys/malloc.h>
   49 #include <sys/file.h>           /* Must come after sys/malloc.h */
   50 #include <sys/mbuf.h>
   51 #include <sys/poll.h>
   52 #include <sys/proc.h>
   53 #include <sys/select.h>
   54 #include <sys/socket.h>
   55 #include <sys/socketvar.h>
   56 #include <sys/sysctl.h>
   57 #include <sys/uio.h>
   58 #include <sys/vnode.h>
   59 
   60 #include <miscfs/specfs/specdev.h> /* XXX */
   61 
   62 #include <net/if.h>
   63 
   64 #include <netsmb/smb.h>
   65 #include <netsmb/smb_conn.h>
   66 #include <netsmb/smb_subr.h>
   67 #include <netsmb/smb_dev.h>
   68 #include <netsmb/smb_rq.h>
   69 
   70 #ifdef __NetBSD__
   71 static struct smb_dev **smb_devtbl; /* indexed by minor */
   72 #define SMB_GETDEV(dev) (smb_devtbl[minor(dev)])
   73 #define NSMB_DEFNUM     4
   74 
   75 #else /* !NetBSD */
   76 
   77 #define SMB_GETDEV(dev)         ((struct smb_dev*)(dev)->si_drv1)
   78 
   79 static d_open_t  nsmb_dev_open;
   80 static d_close_t nsmb_dev_close;
   81 static d_read_t  nsmb_dev_read;
   82 static d_write_t nsmb_dev_write;
   83 static d_ioctl_t nsmb_dev_ioctl;
   84 static d_poll_t  nsmb_dev_poll;
   85 
   86 MODULE_DEPEND(netsmb, libiconv, 1, 1, 1);
   87 MODULE_VERSION(netsmb, NSMB_VERSION);
   88 
   89 static int smb_version = NSMB_VERSION;
   90 
   91 
   92 SYSCTL_DECL(_net_smb);
   93 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
   94 #endif /* NetBSD */
   95 
   96 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
   97 
   98 
   99 /*
  100 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
  101 */
  102 
  103 #ifdef __NetBSD__
  104 dev_type_open(nsmb_dev_open);
  105 dev_type_close(nsmb_dev_close);
  106 dev_type_ioctl(nsmb_dev_ioctl);
  107 
  108 const struct cdevsw nsmb_cdevsw = {
  109         nsmb_dev_open, nsmb_dev_close, noread, nowrite,
  110         nsmb_dev_ioctl, nostop, notty, nopoll, nommap, nokqfilter,
  111 };
  112 #else
  113 static struct cdevsw nsmb_cdevsw = {
  114         /* open */      nsmb_dev_open,
  115         /* close */     nsmb_dev_close,
  116         /* read */      nsmb_dev_read,
  117         /* write */     nsmb_dev_write,
  118         /* ioctl */     nsmb_dev_ioctl,
  119         /* poll */      nsmb_dev_poll,
  120         /* mmap */      nommap,
  121         /* strategy */  nostrategy,
  122         /* name */      NSMB_NAME,
  123         /* maj */       NSMB_MAJOR,
  124         /* dump */      nodump,
  125         /* psize */     nopsize,
  126         /* flags */     0,
  127 };
  128 #endif /* !__NetBSD__ */
  129 
  130 #ifndef __NetBSD__
  131 static eventhandler_tag nsmb_dev_tag;
  132 
  133 static void
  134 nsmb_dev_clone(void *arg, char *name, int namelen, dev_t *dev)
  135 {
  136         int min;
  137 
  138         if (*dev != NODEV)
  139                 return;
  140         if (dev_stdclone(name, NULL, NSMB_NAME, &min) != 1)
  141                 return;
  142         *dev = make_dev(&nsmb_cdevsw, min, 0, 0, 0600, NSMB_NAME"%d", min);
  143 }
  144 
  145 #else /* __NetBSD__ */
  146 
  147 void nsmbattach(int);
  148 
  149 void
  150 nsmbattach(int num)
  151 {
  152 
  153         if (num <= 0) {
  154 #ifdef DIAGNOSTIC
  155                 panic("nsmbattach: cound <= 0");
  156 #endif
  157                 return;
  158         }
  159 
  160         if (num == 1)
  161                 num = NSMB_DEFNUM;
  162 
  163         smb_devtbl = malloc(num * sizeof(void *), M_NSMBDEV, M_WAITOK|M_ZERO);
  164         
  165         if (smb_sm_init()) {
  166 #ifdef DIAGNOSTIC
  167                 panic("netsmbattach: smb_sm_init failed");
  168 #endif
  169                 return;
  170         }
  171 
  172         if (smb_iod_init()) {
  173 #ifdef DIAGNOSTIC
  174                 panic("netsmbattach: smb_iod_init failed");
  175 #endif
  176                 smb_sm_done();
  177                 return;
  178         }
  179 
  180         smb_rqinit();
  181 }
  182 #endif /* __NetBSD__ */
  183 
  184 int
  185 nsmb_dev_open(dev_t dev, int oflags, int devtype, struct proc *p)
  186 {
  187         struct smb_dev *sdp;
  188         int s;
  189 
  190         sdp = SMB_GETDEV(dev);
  191         if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
  192                 return EBUSY;
  193         if (sdp == NULL) {
  194                 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
  195                 smb_devtbl[minor(dev)] = (void*)sdp;
  196         }
  197 
  198 #ifndef __NetBSD__
  199         /*
  200          * XXX: this is just crazy - make a device for an already passed device...
  201          * someone should take care of it.
  202          */
  203         if ((dev->si_flags & SI_NAMED) == 0)
  204                 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
  205                     NSMB_NAME"%d", dev2unit(dev));
  206 #endif /* !__NetBSD__ */
  207 
  208         bzero(sdp, sizeof(*sdp));
  209 /*
  210         STAILQ_INIT(&sdp->sd_rqlist);
  211         STAILQ_INIT(&sdp->sd_rplist);
  212         bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
  213 */
  214         s = splnet();
  215         sdp->sd_level = -1;
  216         sdp->sd_flags |= NSMBFL_OPEN;
  217         splx(s);
  218         return 0;
  219 }
  220 
  221 int
  222 nsmb_dev_close(dev_t dev, int flag, int fmt, struct proc *p)
  223 {
  224         struct smb_dev *sdp;
  225         struct smb_vc *vcp;
  226         struct smb_share *ssp;
  227         struct smb_cred scred;
  228         int s;
  229 
  230         sdp = SMB_GETDEV(dev);
  231         if (!sdp)
  232                 return (ENXIO);
  233 
  234         s = splnet();
  235         if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
  236                 splx(s);
  237                 return EBADF;
  238         }
  239         smb_makescred(&scred, p, NULL);
  240         ssp = sdp->sd_share;
  241         if (ssp != NULL) {
  242                 smb_share_lock(ssp, 0);
  243                 smb_share_rele(ssp, &scred);
  244         }
  245         vcp = sdp->sd_vc;
  246         if (vcp != NULL) {
  247                 smb_vc_lock(vcp, 0);
  248                 smb_vc_rele(vcp, &scred);
  249         }
  250 /*
  251         smb_flushq(&sdp->sd_rqlist);
  252         smb_flushq(&sdp->sd_rplist);
  253 */
  254         smb_devtbl[minor(dev)] = NULL;
  255         free(sdp, M_NSMBDEV);
  256 #ifndef __NetBSD__
  257         destroy_dev(dev);
  258 #endif
  259         splx(s);
  260         return 0;
  261 }
  262 
  263 
  264 int
  265 nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
  266 {
  267         struct smb_dev *sdp;
  268         struct smb_vc *vcp;
  269         struct smb_share *ssp;
  270         struct smb_cred scred;
  271         int error = 0;
  272 
  273         sdp = SMB_GETDEV(dev);
  274         if (!sdp)
  275                 return (ENXIO);
  276         if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
  277                 return EBADF;
  278 
  279         smb_makescred(&scred, p, NULL);
  280         switch (cmd) {
  281             case SMBIOC_OPENSESSION:
  282                 if (sdp->sd_vc)
  283                         return EISCONN;
  284                 error = smb_usr_opensession((struct smbioc_ossn*)data,
  285                     &scred, &vcp);
  286                 if (error)
  287                         break;
  288                 sdp->sd_vc = vcp;
  289                 smb_vc_unlock(vcp, 0);
  290                 sdp->sd_level = SMBL_VC;
  291                 break;
  292             case SMBIOC_OPENSHARE:
  293                 if (sdp->sd_share)
  294                         return EISCONN;
  295                 if (sdp->sd_vc == NULL)
  296                         return ENOTCONN;
  297                 error = smb_usr_openshare(sdp->sd_vc,
  298                     (struct smbioc_oshare*)data, &scred, &ssp);
  299                 if (error)
  300                         break;
  301                 sdp->sd_share = ssp;
  302                 smb_share_unlock(ssp, 0);
  303                 sdp->sd_level = SMBL_SHARE;
  304                 break;
  305             case SMBIOC_REQUEST:
  306                 if (sdp->sd_share == NULL)
  307                         return ENOTCONN;
  308                 error = smb_usr_simplerequest(sdp->sd_share,
  309                     (struct smbioc_rq*)data, &scred);
  310                 break;
  311             case SMBIOC_T2RQ:
  312                 if (sdp->sd_share == NULL)
  313                         return ENOTCONN;
  314                 error = smb_usr_t2request(sdp->sd_share,
  315                     (struct smbioc_t2rq*)data, &scred);
  316                 break;
  317             case SMBIOC_SETFLAGS: {
  318                 struct smbioc_flags *fl = (struct smbioc_flags*)data;
  319                 int on;
  320         
  321                 if (fl->ioc_level == SMBL_VC) {
  322                         if (fl->ioc_mask & SMBV_PERMANENT) {
  323                                 on = fl->ioc_flags & SMBV_PERMANENT;
  324                                 if ((vcp = sdp->sd_vc) == NULL)
  325                                         return ENOTCONN;
  326                                 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
  327                                 if (error)
  328                                         break;
  329                                 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
  330                                         vcp->obj.co_flags |= SMBV_PERMANENT;
  331                                         smb_vc_ref(vcp);
  332                                 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
  333                                         vcp->obj.co_flags &= ~SMBV_PERMANENT;
  334                                         smb_vc_rele(vcp, &scred);
  335                                 }
  336                                 smb_vc_put(vcp, &scred);
  337                         } else
  338                                 error = EINVAL;
  339                 } else if (fl->ioc_level == SMBL_SHARE) {
  340                         if (fl->ioc_mask & SMBS_PERMANENT) {
  341                                 on = fl->ioc_flags & SMBS_PERMANENT;
  342                                 if ((ssp = sdp->sd_share) == NULL)
  343                                         return ENOTCONN;
  344                                 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
  345                                 if (error)
  346                                         break;
  347                                 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
  348                                         ssp->obj.co_flags |= SMBS_PERMANENT;
  349                                         smb_share_ref(ssp);
  350                                 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
  351                                         ssp->obj.co_flags &= ~SMBS_PERMANENT;
  352                                         smb_share_rele(ssp, &scred);
  353                                 }
  354                                 smb_share_put(ssp, &scred);
  355                         } else
  356                                 error = EINVAL;
  357                         break;
  358                 } else
  359                         error = EINVAL;
  360                 break;
  361             }
  362             case SMBIOC_LOOKUP:
  363                 if (sdp->sd_vc || sdp->sd_share)
  364                         return EISCONN;
  365                 vcp = NULL;
  366                 ssp = NULL;
  367                 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
  368                 if (error)
  369                         break;
  370                 if (vcp) {
  371                         sdp->sd_vc = vcp;
  372                         smb_vc_unlock(vcp, 0);
  373                         sdp->sd_level = SMBL_VC;
  374                 }
  375                 if (ssp) {
  376                         sdp->sd_share = ssp;
  377                         smb_share_unlock(ssp, 0);
  378                         sdp->sd_level = SMBL_SHARE;
  379                 }
  380                 break;
  381             case SMBIOC_READ: case SMBIOC_WRITE: {
  382                 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
  383                 struct uio auio;
  384                 struct iovec iov;
  385         
  386                 if ((ssp = sdp->sd_share) == NULL)
  387                         return ENOTCONN;
  388                 iov.iov_base = rwrq->ioc_base;
  389                 iov.iov_len = rwrq->ioc_cnt;
  390                 auio.uio_iov = &iov;
  391                 auio.uio_iovcnt = 1;
  392                 auio.uio_offset = rwrq->ioc_offset;
  393                 auio.uio_resid = rwrq->ioc_cnt;
  394                 auio.uio_segflg = UIO_USERSPACE;
  395                 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
  396                 auio.uio_procp = p;
  397                 if (cmd == SMBIOC_READ)
  398                         error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
  399                 else
  400                         error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
  401                 rwrq->ioc_cnt -= auio.uio_resid;
  402                 break;
  403             }
  404             default:
  405                 error = ENODEV;
  406         }
  407         return error;
  408 }
  409 
  410 #ifndef __NetBSD__
  411 int
  412 nsmb_dev_read(dev_t dev, struct uio *uio, int flag)
  413 {
  414         return EACCES;
  415 }
  416 
  417 int
  418 nsmb_dev_write(dev_t dev, struct uio *uio, int flag)
  419 {
  420         return EACCES;
  421 }
  422 
  423 int
  424 nsmb_dev_poll(dev_t dev, int events, struct proc *p)
  425 {
  426         return ENODEV;
  427 }
  428 
  429 static int
  430 nsmb_dev_load(module_t mod, int cmd, void *arg)
  431 {
  432         int error = 0;
  433 
  434         switch (cmd) {
  435             case MOD_LOAD:
  436                 error = smb_sm_init();
  437                 if (error)
  438                         break;
  439                 error = smb_iod_init();
  440                 if (error) {
  441                         smb_sm_done();
  442                         break;
  443                 }
  444                 cdevsw_add(&nsmb_cdevsw);
  445                 nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
  446                 printf("netsmb_dev: loaded\n");
  447                 break;
  448             case MOD_UNLOAD:
  449                 smb_iod_done();
  450                 error = smb_sm_done();
  451                 error = 0;
  452                 EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
  453                 cdevsw_remove(&nsmb_cdevsw);
  454                 printf("netsmb_dev: unloaded\n");
  455                 break;
  456             default:
  457                 error = EINVAL;
  458                 break;
  459         }
  460         return error;
  461 }
  462 
  463 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
  464 #endif /* !__NetBSD__ */
  465 
  466 /*
  467  * Convert a file descriptor to appropriate smb_share pointer
  468  */
  469 int
  470 smb_dev2share(int fd, int mode, struct smb_cred *scred,
  471         struct smb_share **sspp)
  472 {
  473         struct proc *p = scred->scr_p;
  474         struct file *fp;
  475         struct vnode *vp;
  476         struct smb_dev *sdp;
  477         struct smb_share *ssp;
  478         dev_t dev;
  479         int error;
  480 
  481         if ((fp = fd_getfile(p->p_fd, fd)) == NULL)
  482                 return (EBADF);
  483 
  484         FILE_USE(fp);
  485 
  486         vp = (struct vnode *) fp->f_data;
  487         if (fp->f_type != DTYPE_VNODE
  488             || (fp->f_flag & (FREAD|FWRITE)) == 0
  489             || vp->v_type != VCHR
  490             || vp->v_rdev == NODEV) {
  491                 FILE_UNUSE(fp, p);
  492                 return (EBADF);
  493         }
  494 
  495         dev = vp->v_rdev;
  496 
  497         FILE_UNUSE(fp, p);
  498 
  499         sdp = SMB_GETDEV(dev);
  500         if (!sdp)
  501                 return (ENXIO);
  502         ssp = sdp->sd_share;
  503         if (ssp == NULL)
  504                 return ENOTCONN;
  505 
  506         error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
  507         if (error)
  508                 return error;
  509 
  510         *sspp = ssp;
  511         return 0;
  512 }
  513 

Cache object: dd7ce0f4b9f1759dba40dcec2d69e6ab


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