/* $NetBSD: nfs_lock.c,v 1.3 2020/01/02 15:42:26 thorpej Exp $ */
/*-
* Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Berkeley Software Design Inc's name may not be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
*/
/*
* XXX
* We have to let the process know if the call succeeded. I'm using an extra
* field in the p_nlminfo field in the proc structure, as it is already for
* lockd stuff.
*/
/*
* nfs_advlock --
* NFS advisory byte-level locks.
*
* The vnode shall be (shared) locked on the entry, it is
* unconditionally unlocked after.
*/
int
nfs_dolock(struct vop_advlock_args *ap)
{
LOCKD_MSG msg;
struct thread *td;
struct vnode *vp;
int error;
struct flock *fl;
struct proc *p;
struct nfsmount *nmp;
struct timeval btv;
/*
* Retry after 20 seconds if we haven't gotten a response yet.
* This number was picked out of thin air... but is longer
* then even a reasonably loaded system should take (at least
* on a local network). XXX Probably should use a back-off
* scheme.
*
* XXX: No PCATCH here since we currently have no useful
* way to signal to the userland rpc.lockd that the request
* has been aborted. Once the rpc.lockd implementation
* can handle aborts, and we report them properly,
* PCATCH can be put back. In the mean time, if we did
* permit aborting, the lock attempt would "get lost"
* and the lock would get stuck in the locked state.
*/
error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
if (error != 0) {
if (error == EWOULDBLOCK) {
/*
* We timed out, so we rewrite the request
* to the fifo.
*/
continue;
}
/*
* nfslockdans --
* NFS advisory byte-level locks answer from the lock daemon.
*/
static int
nfslockdans(struct thread *td, struct lockd_ans *ansp)
{
struct proc *targetp;
/* the version should match, or we're out of sync */
if (ansp->la_vers != LOCKD_ANS_VERSION)
return (EINVAL);
/* Find the process, set its return errno and wake it up. */
if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
return (ESRCH);
/* verify the pid hasn't been reused (if we can), and it isn't waiting
* for an answer from a more recent request. We return an EPIPE if
* the match fails, because we've already used ESRCH above, and this
* is sort of like writing on a pipe after the reader has closed it.
*/
if (targetp->p_nlminfo == NULL ||
((ansp->la_msg_ident.msg_seq != -1) &&
(timevalcmp(&targetp->p_nlminfo->pid_start,
&ansp->la_msg_ident.pid_start, !=) ||
targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
PROC_UNLOCK(targetp);
return (EPIPE);
}