[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/nfsclient/nfs_lock.c

Version: -  FREEBSD  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  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  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
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: src/sys/nfsclient/nfs_lock.c,v 1.36 2003/05/30 17:15:56 rwatson Exp $");
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/fcntl.h>
   37 #include <sys/kernel.h>         /* for hz */
   38 #include <sys/limits.h>
   39 #include <sys/lock.h>
   40 #include <sys/malloc.h>
   41 #include <sys/lockf.h>          /* for hz */ /* Must come after sys/malloc.h */
   42 #include <sys/mbuf.h>
   43 #include <sys/mount.h>
   44 #include <sys/namei.h>
   45 #include <sys/proc.h>
   46 #include <sys/resourcevar.h>
   47 #include <sys/socket.h>
   48 #include <sys/socket.h>
   49 #include <sys/unistd.h>
   50 #include <sys/vnode.h>
   51 
   52 #include <net/if.h>
   53 
   54 #include <nfs/rpcv2.h>
   55 #include <nfs/nfsproto.h>
   56 #include <nfsclient/nfs.h>
   57 #include <nfsclient/nfsmount.h>
   58 #include <nfsclient/nfsnode.h>
   59 #include <nfsclient/nfs_lock.h>
   60 #include <nfsclient/nlminfo.h>
   61 
   62 /*
   63  * XXX
   64  * We have to let the process know if the call succeeded.  I'm using an extra
   65  * field in the p_nlminfo field in the proc structure, as it is already for
   66  * lockd stuff.
   67  */
   68 
   69 /*
   70  * nfs_advlock --
   71  *      NFS advisory byte-level locks.
   72  */
   73 int
   74 nfs_dolock(struct vop_advlock_args *ap)
   75 {
   76         LOCKD_MSG msg;
   77         struct nameidata nd;
   78         struct thread *td;
   79         struct vnode *vp, *wvp;
   80         int error, error1;
   81         struct flock *fl;
   82         int fmode, ioflg;
   83         struct proc *p;
   84 
   85         td = curthread;
   86         p = td->td_proc;
   87 
   88         vp = ap->a_vp;
   89         fl = ap->a_fl;
   90 
   91         /*
   92          * the NLM protocol doesn't allow the server to return an error
   93          * on ranges, so we do it.
   94          */
   95         if (fl->l_whence != SEEK_END) {
   96                 if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
   97                     fl->l_start < 0 ||
   98                     (fl->l_len < 0 &&
   99                      (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
  100                         return (EINVAL);
  101                 if (fl->l_len > 0 &&
  102                          (fl->l_len - 1 > OFF_MAX - fl->l_start))
  103                         return (EOVERFLOW);
  104         }
  105 
  106         /*
  107          * Fill in the information structure.
  108          */
  109         msg.lm_version = LOCKD_MSG_VERSION;
  110         msg.lm_msg_ident.pid = p->p_pid;
  111         /*
  112          * if there is no nfsowner table yet, allocate one.
  113          */
  114         if (p->p_nlminfo == NULL) {
  115                 MALLOC(p->p_nlminfo, struct nlminfo *,
  116                         sizeof(struct nlminfo), M_LOCKF, M_WAITOK | M_ZERO);
  117                 p->p_nlminfo->pid_start = p->p_stats->p_start;
  118                 timevaladd(&p->p_nlminfo->pid_start, &boottime);
  119         }
  120         msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
  121         msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
  122 
  123         msg.lm_fl = *fl;
  124         msg.lm_wait = ap->a_flags & F_WAIT;
  125         msg.lm_getlk = ap->a_op == F_GETLK;
  126         bcopy(VFSTONFS(vp->v_mount)->nm_nam, &msg.lm_addr,
  127                 min(sizeof msg.lm_addr, VFSTONFS(vp->v_mount)->nm_nam->sa_len));
  128         msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH;
  129         bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len);
  130         msg.lm_nfsv3 = NFS_ISV3(vp);
  131         cru2x(td->td_ucred, &msg.lm_cred);
  132 
  133         /*
  134          * Open the lock fifo.  If for any reason we don't find the fifo, it
  135          * means that the lock daemon isn't running.  Translate any missing
  136          * file error message for the user, otherwise the application will
  137          * complain that the user's file is missing, which isn't the case.
  138          * Note that we use proc0's cred, so the fifo is opened as root.
  139          *
  140          * XXX: Note that this behavior is relative to the root directory
  141          * of the current process, and this may result in a variety of
  142          * {functional, security} problems in chroot() environments.
  143          */
  144         NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, _PATH_LCKFIFO, td);
  145 
  146         fmode = FFLAGS(O_WRONLY | O_NONBLOCK);
  147         error = vn_open_cred(&nd, &fmode, 0, thread0.td_ucred);
  148         switch (error) {
  149         case ENOENT:
  150         case ENXIO:
  151                 /*
  152                  * Map a failure to find the fifo or no listener on the
  153                  * fifo to locking not being supported.
  154                  */
  155                 return (EOPNOTSUPP);
  156         case 0:
  157                 break;
  158         default:
  159                 return (error);
  160         }
  161         wvp = nd.ni_vp;
  162         VOP_UNLOCK(wvp, 0, td);         /* vn_open leaves it locked */
  163 
  164 
  165         ioflg = IO_UNIT | IO_NOMACCHECK;
  166         for (;;) {
  167                 VOP_LEASE(wvp, td, thread0.td_ucred, LEASE_WRITE);
  168 
  169                 error = vn_rdwr(UIO_WRITE, wvp, (caddr_t)&msg, sizeof(msg), 0,
  170                     UIO_SYSSPACE, ioflg, thread0.td_ucred, NOCRED, NULL, td);
  171 
  172                 if (error && (((ioflg & IO_NDELAY) == 0) || error != EAGAIN)) {
  173                         break;
  174                 }
  175                 /*
  176                  * If we're locking a file, wait for an answer.  Unlocks succeed
  177                  * immediately.
  178                  */
  179                 if (fl->l_type == F_UNLCK)
  180                         /*
  181                          * XXX this isn't exactly correct.  The client side
  182                          * needs to continue sending it's unlock until
  183                          * it gets a responce back.
  184                          */
  185                         break;
  186 
  187                 /*
  188                  * retry after 20 seconds if we haven't gotten a responce yet.
  189                  * This number was picked out of thin air... but is longer
  190                  * then even a reasonably loaded system should take (at least
  191                  * on a local network).  XXX Probably should use a back-off
  192                  * scheme.
  193                  */
  194                 error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
  195                 if (error != 0) {
  196                         if (error == EWOULDBLOCK) {
  197                                 /*
  198                                  * We timed out, so we rewrite the request
  199                                  * to the fifo, but only if it isn't already
  200                                  * full.
  201                                  */
  202                                 ioflg |= IO_NDELAY;
  203                                 continue;
  204                         }
  205 
  206                         break;
  207                 }
  208 
  209                 if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
  210                         if (p->p_nlminfo->set_getlk_pid) {
  211                                 fl->l_pid = p->p_nlminfo->getlk_pid;
  212                         } else {
  213                                 fl->l_type = F_UNLCK;
  214                         }
  215                 }
  216                 error = p->p_nlminfo->retcode;
  217                 break;
  218         }
  219 
  220         error1 = vn_close(wvp, FWRITE, thread0.td_ucred, td);
  221         /* prefer any previous 'error' to our vn_close 'error1'. */
  222         return (error != 0 ? error : error1);
  223 }
  224 
  225 /*
  226  * nfslockdans --
  227  *      NFS advisory byte-level locks answer from the lock daemon.
  228  */
  229 int
  230 nfslockdans(struct thread *td, struct lockd_ans *ansp)
  231 {
  232         struct proc *targetp;
  233         int error;
  234 
  235         /* Let root, or someone who once was root (lockd generally
  236          * switches to the daemon uid once it is done setting up) make
  237          * this call.
  238          *
  239          * XXX This authorization check is probably not right.
  240          */
  241         if ((error = suser(td)) != 0 &&
  242             td->td_ucred->cr_svuid != 0)
  243                 return (error);
  244 
  245         /* the version should match, or we're out of sync */
  246         if (ansp->la_vers != LOCKD_ANS_VERSION)
  247                 return (EINVAL);
  248 
  249         /* Find the process, set its return errno and wake it up. */
  250         if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
  251                 return (ESRCH);
  252 
  253         /* verify the pid hasn't been reused (if we can), and it isn't waiting
  254          * for an answer from a more recent request.  We return an EPIPE if
  255          * the match fails, because we've already used ESRCH above, and this
  256          * is sort of like writing on a pipe after the reader has closed it.
  257          */
  258         if (targetp->p_nlminfo == NULL ||
  259             ((ansp->la_msg_ident.msg_seq != -1) &&
  260               (timevalcmp(&targetp->p_nlminfo->pid_start,
  261                         &ansp->la_msg_ident.pid_start, !=) ||
  262                targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
  263                 PROC_UNLOCK(targetp);
  264                 return (EPIPE);
  265         }
  266 
  267         targetp->p_nlminfo->retcode = ansp->la_errno;
  268         targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
  269         targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
  270 
  271         wakeup(targetp->p_nlminfo);
  272 
  273         PROC_UNLOCK(targetp);
  274         return (0);
  275 }

Cache object: 780f1b5292b79ff042ab1c5cc0cf3868


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