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/fs/nfsclient/nfs_cllock.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions
    6  * are met:
    7  * 1. Redistributions of source code must retain the above copyright
    8  *    notice, this list of conditions and the following disclaimer.
    9  * 2. Redistributions in binary form must reproduce the above copyright
   10  *    notice, this list of conditions and the following disclaimer in the
   11  *    documentation and/or other materials provided with the distribution.
   12  * 3. Berkeley Software Design Inc's name may not be used to endorse or
   13  *    promote products derived from this software without specific prior
   14  *    written permission.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  *
   28  *      from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
   29  */
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD: releng/8.1/sys/fs/nfsclient/nfs_cllock.c 191783 2009-05-04 15:23:58Z rmacklem $");
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/conf.h>
   37 #include <sys/fcntl.h>
   38 #include <sys/kernel.h>         /* for hz */
   39 #include <sys/limits.h>
   40 #include <sys/lock.h>
   41 #include <sys/malloc.h>
   42 #include <sys/lockf.h>          /* for hz */ /* Must come after sys/malloc.h */
   43 #include <sys/mbuf.h>
   44 #include <sys/mount.h>
   45 #include <sys/namei.h>
   46 #include <sys/priv.h>
   47 #include <sys/proc.h>
   48 #include <sys/resourcevar.h>
   49 #include <sys/socket.h>
   50 #include <sys/socket.h>
   51 #include <sys/unistd.h>
   52 #include <sys/vnode.h>
   53 
   54 #include <net/if.h>
   55 
   56 #include <fs/nfs/nfsport.h>
   57 #include <fs/nfsclient/nfsmount.h>
   58 #include <fs/nfsclient/nfs.h>
   59 #include <fs/nfsclient/nfsnode.h>
   60 #include <fs/nfsclient/nfs_lock.h>
   61 #include <fs/nfsclient/nlminfo.h>
   62 
   63 extern void (*nlminfo_release_p)(struct proc *p);
   64 
   65 MALLOC_DEFINE(M_NEWNFSLOCK, "newnfsclient_lock", "NEWNFS lock request");
   66 MALLOC_DEFINE(M_NEWNLMINFO, "newnfsclient_nlminfo", "NEWNFS lock process structure");
   67 
   68 static int nfslockdans(struct thread *td, struct lockd_ans *ansp);
   69 static void nlminfo_release(struct proc *p);
   70 /*
   71  * --------------------------------------------------------------------
   72  * A miniature device driver which the userland uses to talk to us.
   73  *
   74  */
   75 
   76 static struct cdev *nfslock_dev;
   77 static struct mtx nfslock_mtx;
   78 static int nfslock_isopen;
   79 static TAILQ_HEAD(,__lock_msg)  nfslock_list;
   80 
   81 static int
   82 nfslock_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
   83 {
   84         int error;
   85 
   86         error = priv_check(td, PRIV_NFS_LOCKD);
   87         if (error)
   88                 return (error);
   89 
   90         mtx_lock(&nfslock_mtx);
   91         if (!nfslock_isopen) {
   92                 error = 0;
   93                 nfslock_isopen = 1;
   94         } else {
   95                 error = EOPNOTSUPP;
   96         }
   97         mtx_unlock(&nfslock_mtx);
   98                 
   99         return (error);
  100 }
  101 
  102 static int
  103 nfslock_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
  104 {
  105         struct __lock_msg *lm;
  106 
  107         mtx_lock(&nfslock_mtx);
  108         nfslock_isopen = 0;
  109         while (!TAILQ_EMPTY(&nfslock_list)) {
  110                 lm = TAILQ_FIRST(&nfslock_list);
  111                 /* XXX: answer request */
  112                 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
  113                 free(lm, M_NEWNFSLOCK);
  114         }
  115         mtx_unlock(&nfslock_mtx);
  116         return (0);
  117 }
  118 
  119 static int
  120 nfslock_read(struct cdev *dev, struct uio *uio, int ioflag)
  121 {
  122         int error;
  123         struct __lock_msg *lm;
  124 
  125         if (uio->uio_resid != sizeof *lm)
  126                 return (EOPNOTSUPP);
  127         lm = NULL;
  128         error = 0;
  129         mtx_lock(&nfslock_mtx);
  130         while (TAILQ_EMPTY(&nfslock_list)) {
  131                 error = msleep(&nfslock_list, &nfslock_mtx, PSOCK | PCATCH,
  132                     "nfslockd", 0);
  133                 if (error)
  134                         break;
  135         }
  136         if (!error) {
  137                 lm = TAILQ_FIRST(&nfslock_list);
  138                 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
  139         }
  140         mtx_unlock(&nfslock_mtx);
  141         if (!error) {
  142                 error = uiomove(lm, sizeof *lm, uio);
  143                 free(lm, M_NEWNFSLOCK);
  144         }
  145         return (error);
  146 }
  147 
  148 static int
  149 nfslock_write(struct cdev *dev, struct uio *uio, int ioflag)
  150 {
  151         struct lockd_ans la;
  152         int error;
  153 
  154         if (uio->uio_resid != sizeof la)
  155                 return (EOPNOTSUPP);
  156         error = uiomove(&la, sizeof la, uio);
  157         if (!error)
  158                 error = nfslockdans(curthread, &la);
  159         return (error);
  160 }
  161 
  162 static int
  163 nfslock_send(struct __lock_msg *lm)
  164 {
  165         struct __lock_msg *lm2;
  166         int error;
  167 
  168         error = 0;
  169         lm2 = malloc(sizeof *lm2, M_NEWNFSLOCK, M_WAITOK);
  170         mtx_lock(&nfslock_mtx);
  171         if (nfslock_isopen) {
  172                 memcpy(lm2, lm, sizeof *lm2);
  173                 TAILQ_INSERT_TAIL(&nfslock_list, lm2, lm_link);
  174                 wakeup(&nfslock_list);
  175         } else {
  176                 error = EOPNOTSUPP;
  177         }
  178         mtx_unlock(&nfslock_mtx);
  179         if (error)
  180                 free(lm2, M_NEWNFSLOCK);
  181         return (error);
  182 }
  183 
  184 static struct cdevsw nfslock_cdevsw = {
  185         .d_version =    D_VERSION,
  186         .d_open =       nfslock_open,
  187         .d_close =      nfslock_close,
  188         .d_read =       nfslock_read,
  189         .d_write =      nfslock_write,
  190         .d_name =       "nfslock"
  191 };
  192 
  193 static int
  194 newnfslock_modevent(module_t mod __unused, int type, void *data __unused)
  195 {
  196 
  197         switch (type) {
  198         case MOD_LOAD:
  199                 if (bootverbose)
  200                         printf("nfslock: pseudo-device\n");
  201                 mtx_init(&nfslock_mtx, "nfslock", NULL, MTX_DEF);
  202                 TAILQ_INIT(&nfslock_list);
  203                 nlminfo_release_p = nlminfo_release;
  204                 nfslock_dev = make_dev(&nfslock_cdevsw, 0,
  205                     UID_ROOT, GID_KMEM, 0600, _PATH_NFSLCKDEV);
  206                 return (0);
  207         default:
  208                 return (EOPNOTSUPP);
  209         }
  210 }
  211 
  212 DEV_MODULE(newnfslock, newnfslock_modevent, NULL);
  213 MODULE_VERSION(newnfslock, 1);
  214 
  215 
  216 /*
  217  * XXX
  218  * We have to let the process know if the call succeeded.  I'm using an extra
  219  * field in the p_nlminfo field in the proc structure, as it is already for
  220  * lockd stuff.
  221  */
  222 
  223 /*
  224  * nfs_advlock --
  225  *      NFS advisory byte-level locks.
  226  *
  227  * The vnode shall be (shared) locked on the entry, it is
  228  * unconditionally unlocked after.
  229  */
  230 int
  231 ncl_dolock(struct vop_advlock_args *ap)
  232 {
  233         LOCKD_MSG msg;
  234         struct thread *td;
  235         struct vnode *vp;
  236         int error;
  237         struct flock *fl;
  238         struct proc *p;
  239 
  240         td = curthread;
  241         p = td->td_proc;
  242 
  243         vp = ap->a_vp;
  244         fl = ap->a_fl;
  245 
  246         ASSERT_VOP_LOCKED(vp, "nfs_dolock");
  247 
  248         bcopy(VFSTONFS(vp->v_mount)->nm_nam, &msg.lm_addr,
  249                 min(sizeof msg.lm_addr, VFSTONFS(vp->v_mount)->nm_nam->sa_len));
  250         msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH;
  251         bcopy(VTONFS(vp)->n_fhp->nfh_fh, msg.lm_fh, msg.lm_fh_len);
  252         msg.lm_nfsv3 = NFS_ISV3(vp);
  253         VOP_UNLOCK(vp, 0);
  254 
  255         /*
  256          * the NLM protocol doesn't allow the server to return an error
  257          * on ranges, so we do it.
  258          */
  259         if (fl->l_whence != SEEK_END) {
  260                 if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
  261                     fl->l_start < 0 ||
  262                     (fl->l_len < 0 &&
  263                      (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
  264                         return (EINVAL);
  265                 if (fl->l_len > 0 &&
  266                          (fl->l_len - 1 > OFF_MAX - fl->l_start))
  267                         return (EOVERFLOW);
  268         }
  269 
  270         /*
  271          * Fill in the information structure.
  272          */
  273         msg.lm_version = LOCKD_MSG_VERSION;
  274         msg.lm_msg_ident.pid = p->p_pid;
  275 
  276         mtx_lock(&Giant);
  277         /*
  278          * if there is no nfsowner table yet, allocate one.
  279          */
  280         if (p->p_nlminfo == NULL) {
  281                 p->p_nlminfo = malloc(sizeof(struct nlminfo),
  282                     M_NEWNLMINFO, M_WAITOK | M_ZERO);
  283                 p->p_nlminfo->pid_start = p->p_stats->p_start;
  284                 timevaladd(&p->p_nlminfo->pid_start, &boottime);
  285         }
  286         msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
  287         msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
  288 
  289         msg.lm_fl = *fl;
  290         msg.lm_wait = ap->a_flags & F_WAIT;
  291         msg.lm_getlk = ap->a_op == F_GETLK;
  292         cru2x(td->td_ucred, &msg.lm_cred);
  293 
  294         for (;;) {
  295                 error = nfslock_send(&msg);
  296                 if (error)
  297                         goto out;
  298 
  299                 /* Unlocks succeed immediately.  */
  300                 if (fl->l_type == F_UNLCK)
  301                         goto out;
  302 
  303                 /*
  304                  * Retry after 20 seconds if we haven't gotten a response yet.
  305                  * This number was picked out of thin air... but is longer
  306                  * then even a reasonably loaded system should take (at least
  307                  * on a local network).  XXX Probably should use a back-off
  308                  * scheme.
  309                  *
  310                  * XXX: No PCATCH here since we currently have no useful
  311                  * way to signal to the userland rpc.lockd that the request
  312                  * has been aborted.  Once the rpc.lockd implementation
  313                  * can handle aborts, and we report them properly,
  314                  * PCATCH can be put back.  In the mean time, if we did
  315                  * permit aborting, the lock attempt would "get lost"
  316                  * and the lock would get stuck in the locked state.
  317                  */
  318                 error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
  319                 if (error != 0) {
  320                         if (error == EWOULDBLOCK) {
  321                                 /*
  322                                  * We timed out, so we rewrite the request
  323                                  * to the fifo.
  324                                  */
  325                                 continue;
  326                         }
  327 
  328                         break;
  329                 }
  330 
  331                 if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
  332                         if (p->p_nlminfo->set_getlk_pid) {
  333                                 fl->l_sysid = 0; /* XXX */
  334                                 fl->l_pid = p->p_nlminfo->getlk_pid;
  335                         } else {
  336                                 fl->l_type = F_UNLCK;
  337                         }
  338                 }
  339                 error = p->p_nlminfo->retcode;
  340                 break;
  341         }
  342  out:
  343         mtx_unlock(&Giant);
  344         return (error);
  345 }
  346 
  347 /*
  348  * nfslockdans --
  349  *      NFS advisory byte-level locks answer from the lock daemon.
  350  */
  351 static int
  352 nfslockdans(struct thread *td, struct lockd_ans *ansp)
  353 {
  354         struct proc *targetp;
  355 
  356         /* the version should match, or we're out of sync */
  357         if (ansp->la_vers != LOCKD_ANS_VERSION)
  358                 return (EINVAL);
  359 
  360         /* Find the process, set its return errno and wake it up. */
  361         if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
  362                 return (ESRCH);
  363 
  364         /* verify the pid hasn't been reused (if we can), and it isn't waiting
  365          * for an answer from a more recent request.  We return an EPIPE if
  366          * the match fails, because we've already used ESRCH above, and this
  367          * is sort of like writing on a pipe after the reader has closed it.
  368          */
  369         if (targetp->p_nlminfo == NULL ||
  370             ((ansp->la_msg_ident.msg_seq != -1) &&
  371               (timevalcmp(&targetp->p_nlminfo->pid_start,
  372                         &ansp->la_msg_ident.pid_start, !=) ||
  373                targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
  374                 PROC_UNLOCK(targetp);
  375                 return (EPIPE);
  376         }
  377 
  378         targetp->p_nlminfo->retcode = ansp->la_errno;
  379         targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
  380         targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
  381 
  382         wakeup(targetp->p_nlminfo);
  383 
  384         PROC_UNLOCK(targetp);
  385         return (0);
  386 }
  387 
  388 /*
  389  * Free nlminfo attached to process.
  390  */
  391 static void        
  392 nlminfo_release(struct proc *p)
  393 {  
  394         free(p->p_nlminfo, M_NEWNLMINFO);
  395         p->p_nlminfo = NULL;
  396 }

Cache object: ecad8b65c450356ae099daf5317ce8f7


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