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/nfs/nfs_lock.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/9.2/sys/nfs/nfs_lock.c 249132 2013-04-05 08:22:11Z mav $");
   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 <nfs/nfsproto.h>
   57 #include <nfs/nfs_lock.h>
   58 #include <nfsclient/nfs.h>
   59 #include <nfsclient/nfsmount.h>
   60 #include <nfsclient/nfsnode.h>
   61 #include <nfsclient/nlminfo.h>
   62 
   63 extern void (*nlminfo_release_p)(struct proc *p);
   64 
   65 vop_advlock_t   *nfs_advlock_p = nfs_dolock;
   66 vop_reclaim_t   *nfs_reclaim_p = NULL;
   67 
   68 static MALLOC_DEFINE(M_NFSLOCK, "nfsclient_lock", "NFS lock request");
   69 static MALLOC_DEFINE(M_NLMINFO, "nfsclient_nlminfo",
   70     "NFS lock process structure");
   71 
   72 static int nfslockdans(struct thread *td, struct lockd_ans *ansp);
   73 static void nlminfo_release(struct proc *p);
   74 /*
   75  * --------------------------------------------------------------------
   76  * A miniature device driver which the userland uses to talk to us.
   77  *
   78  */
   79 
   80 static struct cdev *nfslock_dev;
   81 static struct mtx nfslock_mtx;
   82 static int nfslock_isopen;
   83 static TAILQ_HEAD(,__lock_msg)  nfslock_list;
   84 
   85 static int
   86 nfslock_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
   87 {
   88         int error;
   89 
   90         error = priv_check(td, PRIV_NFS_LOCKD);
   91         if (error)
   92                 return (error);
   93 
   94         mtx_lock(&nfslock_mtx);
   95         if (!nfslock_isopen) {
   96                 error = 0;
   97                 nfslock_isopen = 1;
   98         } else {
   99                 error = EOPNOTSUPP;
  100         }
  101         mtx_unlock(&nfslock_mtx);
  102                 
  103         return (error);
  104 }
  105 
  106 static int
  107 nfslock_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
  108 {
  109         struct __lock_msg *lm;
  110 
  111         mtx_lock(&nfslock_mtx);
  112         nfslock_isopen = 0;
  113         while (!TAILQ_EMPTY(&nfslock_list)) {
  114                 lm = TAILQ_FIRST(&nfslock_list);
  115                 /* XXX: answer request */
  116                 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
  117                 free(lm, M_NFSLOCK);
  118         }
  119         mtx_unlock(&nfslock_mtx);
  120         return (0);
  121 }
  122 
  123 static int
  124 nfslock_read(struct cdev *dev, struct uio *uio, int ioflag)
  125 {
  126         int error;
  127         struct __lock_msg *lm;
  128 
  129         if (uio->uio_resid != sizeof *lm)
  130                 return (EOPNOTSUPP);
  131         lm = NULL;
  132         error = 0;
  133         mtx_lock(&nfslock_mtx);
  134         while (TAILQ_EMPTY(&nfslock_list)) {
  135                 error = msleep(&nfslock_list, &nfslock_mtx, PSOCK | PCATCH,
  136                     "nfslockd", 0);
  137                 if (error)
  138                         break;
  139         }
  140         if (!error) {
  141                 lm = TAILQ_FIRST(&nfslock_list);
  142                 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
  143         }
  144         mtx_unlock(&nfslock_mtx);
  145         if (!error) {
  146                 error = uiomove(lm, sizeof *lm, uio);
  147                 free(lm, M_NFSLOCK);
  148         }
  149         return (error);
  150 }
  151 
  152 static int
  153 nfslock_write(struct cdev *dev, struct uio *uio, int ioflag)
  154 {
  155         struct lockd_ans la;
  156         int error;
  157 
  158         if (uio->uio_resid != sizeof la)
  159                 return (EOPNOTSUPP);
  160         error = uiomove(&la, sizeof la, uio);
  161         if (!error)
  162                 error = nfslockdans(curthread, &la);
  163         return (error);
  164 }
  165 
  166 static int
  167 nfslock_send(struct __lock_msg *lm)
  168 {
  169         struct __lock_msg *lm2;
  170         int error;
  171 
  172         error = 0;
  173         lm2 = malloc(sizeof *lm2, M_NFSLOCK, M_WAITOK);
  174         mtx_lock(&nfslock_mtx);
  175         if (nfslock_isopen) {
  176                 memcpy(lm2, lm, sizeof *lm2);
  177                 TAILQ_INSERT_TAIL(&nfslock_list, lm2, lm_link);
  178                 wakeup(&nfslock_list);
  179         } else {
  180                 error = EOPNOTSUPP;
  181         }
  182         mtx_unlock(&nfslock_mtx);
  183         if (error)
  184                 free(lm2, M_NFSLOCK);
  185         return (error);
  186 }
  187 
  188 static struct cdevsw nfslock_cdevsw = {
  189         .d_version =    D_VERSION,
  190         .d_open =       nfslock_open,
  191         .d_close =      nfslock_close,
  192         .d_read =       nfslock_read,
  193         .d_write =      nfslock_write,
  194         .d_name =       "nfslock"
  195 };
  196 
  197 static int
  198 nfslock_modevent(module_t mod __unused, int type, void *data __unused)
  199 {
  200 
  201         switch (type) {
  202         case MOD_LOAD:
  203                 if (bootverbose)
  204                         printf("nfslock: pseudo-device\n");
  205                 mtx_init(&nfslock_mtx, "nfslock", NULL, MTX_DEF);
  206                 TAILQ_INIT(&nfslock_list);
  207                 nlminfo_release_p = nlminfo_release;
  208                 nfslock_dev = make_dev(&nfslock_cdevsw, 0,
  209                     UID_ROOT, GID_KMEM, 0600, _PATH_NFSLCKDEV);
  210                 return (0);
  211         default:
  212                 return (EOPNOTSUPP);
  213         }
  214 }
  215 
  216 DEV_MODULE(nfslock, nfslock_modevent, NULL);
  217 MODULE_VERSION(nfslock, 1);
  218 
  219 
  220 /*
  221  * XXX
  222  * We have to let the process know if the call succeeded.  I'm using an extra
  223  * field in the p_nlminfo field in the proc structure, as it is already for
  224  * lockd stuff.
  225  */
  226 
  227 /*
  228  * nfs_advlock --
  229  *      NFS advisory byte-level locks.
  230  *
  231  * The vnode shall be (shared) locked on the entry, it is
  232  * unconditionally unlocked after.
  233  */
  234 int
  235 nfs_dolock(struct vop_advlock_args *ap)
  236 {
  237         LOCKD_MSG msg;
  238         struct thread *td;
  239         struct vnode *vp;
  240         int error;
  241         struct flock *fl;
  242         struct proc *p;
  243         struct nfsmount *nmp;
  244 
  245         td = curthread;
  246         p = td->td_proc;
  247 
  248         vp = ap->a_vp;
  249         fl = ap->a_fl;
  250         nmp = VFSTONFS(vp->v_mount);
  251 
  252         ASSERT_VOP_LOCKED(vp, "nfs_dolock");
  253 
  254         nmp->nm_getinfo(vp, msg.lm_fh, &msg.lm_fh_len, &msg.lm_addr,
  255             &msg.lm_nfsv3, NULL, NULL);
  256         VOP_UNLOCK(vp, 0);
  257 
  258         /*
  259          * the NLM protocol doesn't allow the server to return an error
  260          * on ranges, so we do it.
  261          */
  262         if (fl->l_whence != SEEK_END) {
  263                 if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
  264                     fl->l_start < 0 ||
  265                     (fl->l_len < 0 &&
  266                      (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
  267                         return (EINVAL);
  268                 if (fl->l_len > 0 &&
  269                          (fl->l_len - 1 > OFF_MAX - fl->l_start))
  270                         return (EOVERFLOW);
  271         }
  272 
  273         /*
  274          * Fill in the information structure.
  275          */
  276         msg.lm_version = LOCKD_MSG_VERSION;
  277         msg.lm_msg_ident.pid = p->p_pid;
  278 
  279         mtx_lock(&Giant);
  280         /*
  281          * if there is no nfsowner table yet, allocate one.
  282          */
  283         if (p->p_nlminfo == NULL) {
  284                 p->p_nlminfo = malloc(sizeof(struct nlminfo),
  285                     M_NLMINFO, M_WAITOK | M_ZERO);
  286                 p->p_nlminfo->pid_start = p->p_stats->p_start;
  287                 timevaladd(&p->p_nlminfo->pid_start, &boottime);
  288         }
  289         msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
  290         msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
  291 
  292         msg.lm_fl = *fl;
  293         msg.lm_wait = ap->a_flags & F_WAIT;
  294         msg.lm_getlk = ap->a_op == F_GETLK;
  295         cru2x(td->td_ucred, &msg.lm_cred);
  296 
  297         for (;;) {
  298                 error = nfslock_send(&msg);
  299                 if (error)
  300                         goto out;
  301 
  302                 /* Unlocks succeed immediately.  */
  303                 if (fl->l_type == F_UNLCK)
  304                         goto out;
  305 
  306                 /*
  307                  * Retry after 20 seconds if we haven't gotten a response yet.
  308                  * This number was picked out of thin air... but is longer
  309                  * then even a reasonably loaded system should take (at least
  310                  * on a local network).  XXX Probably should use a back-off
  311                  * scheme.
  312                  *
  313                  * XXX: No PCATCH here since we currently have no useful
  314                  * way to signal to the userland rpc.lockd that the request
  315                  * has been aborted.  Once the rpc.lockd implementation
  316                  * can handle aborts, and we report them properly,
  317                  * PCATCH can be put back.  In the mean time, if we did
  318                  * permit aborting, the lock attempt would "get lost"
  319                  * and the lock would get stuck in the locked state.
  320                  */
  321                 error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
  322                 if (error != 0) {
  323                         if (error == EWOULDBLOCK) {
  324                                 /*
  325                                  * We timed out, so we rewrite the request
  326                                  * to the fifo.
  327                                  */
  328                                 continue;
  329                         }
  330 
  331                         break;
  332                 }
  333 
  334                 if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
  335                         if (p->p_nlminfo->set_getlk_pid) {
  336                                 fl->l_sysid = 0; /* XXX */
  337                                 fl->l_pid = p->p_nlminfo->getlk_pid;
  338                         } else {
  339                                 fl->l_type = F_UNLCK;
  340                         }
  341                 }
  342                 error = p->p_nlminfo->retcode;
  343                 break;
  344         }
  345  out:
  346         mtx_unlock(&Giant);
  347         return (error);
  348 }
  349 
  350 /*
  351  * nfslockdans --
  352  *      NFS advisory byte-level locks answer from the lock daemon.
  353  */
  354 static int
  355 nfslockdans(struct thread *td, struct lockd_ans *ansp)
  356 {
  357         struct proc *targetp;
  358 
  359         /* the version should match, or we're out of sync */
  360         if (ansp->la_vers != LOCKD_ANS_VERSION)
  361                 return (EINVAL);
  362 
  363         /* Find the process, set its return errno and wake it up. */
  364         if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
  365                 return (ESRCH);
  366 
  367         /* verify the pid hasn't been reused (if we can), and it isn't waiting
  368          * for an answer from a more recent request.  We return an EPIPE if
  369          * the match fails, because we've already used ESRCH above, and this
  370          * is sort of like writing on a pipe after the reader has closed it.
  371          */
  372         if (targetp->p_nlminfo == NULL ||
  373             ((ansp->la_msg_ident.msg_seq != -1) &&
  374               (timevalcmp(&targetp->p_nlminfo->pid_start,
  375                         &ansp->la_msg_ident.pid_start, !=) ||
  376                targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
  377                 PROC_UNLOCK(targetp);
  378                 return (EPIPE);
  379         }
  380 
  381         targetp->p_nlminfo->retcode = ansp->la_errno;
  382         targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
  383         targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
  384 
  385         wakeup(targetp->p_nlminfo);
  386 
  387         PROC_UNLOCK(targetp);
  388         return (0);
  389 }
  390 
  391 /*
  392  * Free nlminfo attached to process.
  393  */
  394 void        
  395 nlminfo_release(struct proc *p)
  396 {  
  397         free(p->p_nlminfo, M_NLMINFO);
  398         p->p_nlminfo = NULL;
  399 }

Cache object: f5ed9d1a326ee9843df95c9d420028bc


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