/*
* Copyright (c) 2000 Manuel Bouyer.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 THE REGENTS OR CONTRIBUTORS 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.
*
*/
/* A set of utilities for managing file locking */
LIST_HEAD(lcklst_head, file_lock);
struct lcklst_head lcklst_head = LIST_HEAD_INITIALIZER(lcklst_head);
#define FHANDLE_SIZE_MAX 1024 /* arbitrary big enough value */
typedef struct {
size_t fhsize;
char *fhdata;
} nfs_fhandle_t;
/* struct describing a lock */
struct file_lock {
LIST_ENTRY(file_lock) lcklst;
nfs_fhandle_t filehandle; /* NFS filehandle */
struct sockaddr *addr;
struct nlm4_holder client; /* lock holder */
netobj client_cookie; /* cookie sent by the client */
char client_name[128];
int nsm_status; /* status from the remote lock manager */
int status; /* lock status, see below */
int flags; /* lock flags, see lockd_lock.h */
pid_t locker; /* pid of the child process trying to get the lock */
int fd; /* file descriptor for this lock */
};
/* lock status */
#define LKST_LOCKED 1 /* lock is locked */
#define LKST_WAITING 2 /* file is already locked by another host */
#define LKST_PROCESSING 3 /* child is trying to acquire the lock */
#define LKST_DYING 4 /* must dies when we get news from the child */
/*
* testlock(): inform the caller if the requested lock would be granted or not
* returns NULL if lock would granted, or pointer to the current nlm4_holder
* otherwise.
*/
/* convert lock to a local filehandle */
if (fhconv(&filehandle, &lock->fh)) {
syslog(LOG_NOTICE, "fhconv failed (%m)");
return NULL; /* XXX */
}
siglock();
/* search through the list for lock holder */
LIST_FOREACH(fl, &lcklst_head, lcklst) {
if (fl->status != LKST_LOCKED)
continue;
if (fhcmp(&fl->filehandle, &filehandle) != 0)
continue;
/* got it ! */
syslog(LOG_DEBUG, "test for %s: found lock held by %s",
lock->caller_name, fl->client_name);
sigunlock();
fhfree(&filehandle);
return (&fl->client);
}
/* not found */
sigunlock();
fhfree(&filehandle);
syslog(LOG_DEBUG, "test for %s: no lock found", lock->caller_name);
return NULL;
}
/*
* getlock: try to acquire the lock.
* If file is already locked and we can sleep, put the lock in the list with
* status LKST_WAITING; it'll be processed later.
* Otherwise try to lock. If we're allowed to block, fork a child which
* will do the blocking lock.
*/
enum nlm_stats
getlock(nlm4_lockargs * lckarg, struct svc_req *rqstp, int flags)
{
struct file_lock *fl, *newfl;
enum nlm_stats retval;
struct sockaddr *addr;
/* allocate new file_lock for this request */
newfl = lalloc();
if (newfl == NULL) {
syslog(LOG_NOTICE, "malloc failed (%m)");
/* failed */
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_denied_nolock : nlm_denied_nolocks;
}
if (fhconv(&newfl->filehandle, &lckarg->alock.fh)) {
syslog(LOG_NOTICE, "fhconv failed (%m)");
lfree(newfl);
/* failed */
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_denied_nolock : nlm_denied_nolocks;
}
addr = (struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf;
newfl->addr = malloc((size_t)addr->sa_len);
if (newfl->addr == NULL) {
syslog(LOG_NOTICE, "malloc failed (%m)");
lfree(newfl);
/* failed */
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_denied_nolock : nlm_denied_nolocks;
}
(void)memcpy(newfl->addr, addr, (size_t)addr->sa_len);
newfl->client.exclusive = lckarg->exclusive;
newfl->client.svid = lckarg->alock.svid;
newfl->client.oh.n_bytes = malloc(lckarg->alock.oh.n_len);
if (newfl->client.oh.n_bytes == NULL) {
syslog(LOG_NOTICE, "malloc failed (%m)");
lfree(newfl);
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_denied_nolock : nlm_denied_nolocks;
}
newfl->client.oh.n_len = lckarg->alock.oh.n_len;
(void)memcpy(newfl->client.oh.n_bytes, lckarg->alock.oh.n_bytes,
lckarg->alock.oh.n_len);
newfl->client.l_offset = lckarg->alock.l_offset;
newfl->client.l_len = lckarg->alock.l_len;
newfl->client_cookie.n_len = lckarg->cookie.n_len;
newfl->client_cookie.n_bytes = malloc(lckarg->cookie.n_len);
if (newfl->client_cookie.n_bytes == NULL) {
syslog(LOG_NOTICE, "malloc failed (%m)");
lfree(newfl);
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_denied_nolock : nlm_denied_nolocks;
}
(void)memcpy(newfl->client_cookie.n_bytes, lckarg->cookie.n_bytes,
lckarg->cookie.n_len);
(void)strlcpy(newfl->client_name, lckarg->alock.caller_name,
sizeof(newfl->client_name));
newfl->nsm_status = lckarg->state;
newfl->status = 0;
newfl->flags = flags;
siglock();
/* look for a lock rq from this host for this fh */
fl = lock_lookup(newfl, LL_FH|LL_NAME|LL_SVID);
if (fl) {
/* already locked by this host ??? */
sigunlock();
syslog(LOG_NOTICE, "duplicate lock from %s.%"
PRIu32,
newfl->client_name, newfl->client.svid);
lfree(newfl);
switch(fl->status) {
case LKST_LOCKED:
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_granted : nlm_granted;
case LKST_WAITING:
case LKST_PROCESSING:
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_blocked : nlm_blocked;
case LKST_DYING:
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_denied : nlm_denied;
default:
syslog(LOG_NOTICE, "bad status %d",
fl->status);
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_failed : nlm_denied;
}
/* NOTREACHED */
}
fl = lock_lookup(newfl, LL_FH);
if (fl) {
/*
* We already have a lock for this file.
* Put this one in waiting state if allowed to block
*/
if (lckarg->block) {
syslog(LOG_DEBUG, "lock from %s.%" PRIu32 ": "
"already locked, waiting",
lckarg->alock.caller_name,
lckarg->alock.svid);
newfl->status = LKST_WAITING;
LIST_INSERT_HEAD(&lcklst_head, newfl, lcklst);
do_mon(lckarg->alock.caller_name);
sigunlock();
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_blocked : nlm_blocked;
} else {
sigunlock();
syslog(LOG_DEBUG, "lock from %s.%" PRIu32 ": "
"already locked, failed",
lckarg->alock.caller_name,
lckarg->alock.svid);
lfree(newfl);
return (flags & LOCK_V4) ?
(enum nlm_stats)nlm4_denied : nlm_denied;
}
/* NOTREACHED */
}
/* no entry for this file yet; add to list */
LIST_INSERT_HEAD(&lcklst_head, newfl, lcklst);
/* do the lock */
retval = do_lock(newfl, lckarg->block);
switch (retval) {
case nlm4_granted:
/* case nlm_granted: is the same as nlm4_granted */
case nlm4_blocked:
/* case nlm_blocked: is the same as nlm4_blocked */
do_mon(lckarg->alock.caller_name);
break;
default:
lfree(newfl);
break;
}
sigunlock();
return retval;
}
/* unlock a filehandle */
enum nlm_stats
unlock(nlm4_lock *lck, int flags)
{
struct file_lock *fl;
nfs_fhandle_t filehandle;
int err = (flags & LOCK_V4) ? (enum nlm_stats)nlm4_granted : nlm_granted;
if (fhconv(&filehandle, &lck->fh)) {
syslog(LOG_NOTICE, "fhconv failed (%m)");
return (flags & LOCK_V4) ? (enum nlm_stats)nlm4_denied : nlm_denied;
}
siglock();
LIST_FOREACH(fl, &lcklst_head, lcklst) {
if (strcmp(fl->client_name, lck->caller_name) ||
fhcmp(&filehandle, &fl->filehandle) != 0 ||
fl->client.oh.n_len != lck->oh.n_len ||
memcmp(fl->client.oh.n_bytes, lck->oh.n_bytes,
fl->client.oh.n_len) != 0 ||
fl->client.svid != lck->svid)
continue;
/* Got it, unlock and remove from the queue */
syslog(LOG_DEBUG, "unlock from %s.%" PRIu32 ": found struct, "
"status %d", lck->caller_name, lck->svid, fl->status);
switch (fl->status) {
case LKST_LOCKED:
err = do_unlock(fl);
break;
case LKST_WAITING:
/* remove from the list */
LIST_REMOVE(fl, lcklst);
lfree(fl);
break;
case LKST_PROCESSING:
/*
* being handled by a child; will clean up
* when the child exits
*/
fl->status = LKST_DYING;
break;
case LKST_DYING:
/* nothing to do */
break;
default:
syslog(LOG_NOTICE, "unknown status %d for %s",
fl->status, fl->client_name);
}
sigunlock();
fhfree(&filehandle);
return err;
}
sigunlock();
/* didn't find a matching entry; log anyway */
syslog(LOG_NOTICE, "no matching entry for %s",
lck->caller_name);
fhfree(&filehandle);
return (flags & LOCK_V4) ? (enum nlm_stats)nlm4_granted : nlm_granted;
}
for (;;) {
pid = wait4(-1, &sstatus, WNOHANG, NULL);
if (pid == -1) {
if (errno != ECHILD)
syslog(LOG_NOTICE, "wait failed (%m)");
else
syslog(LOG_DEBUG, "wait failed (%m)");
return;
}
if (pid == 0) {
/* no more child to handle yet */
return;
}
/*
* if we're here we have a child that exited
* Find the associated file_lock.
*/
LIST_FOREACH(fl, &lcklst_head, lcklst) {
if (pid == fl->locker)
break;
}
if (fl == NULL) {
syslog(LOG_NOTICE, "unknown child %d", pid);
} else {
/*
* protect from pid reusing.
*/
fl->locker = 0;
if (!WIFEXITED(sstatus) || WEXITSTATUS(sstatus) != 0) {
syslog(LOG_NOTICE, "child %d failed", pid);
/*
* can't do much here; we can't reply
* anything but OK for blocked locks
* Eventually the client will time out
* and retry.
*/
(void)do_unlock(fl);
return;
}
/* check lock status */
syslog(LOG_DEBUG, "processing child %d, status %d",
pid, fl->status);
switch(fl->status) {
case LKST_PROCESSING:
fl->status = LKST_LOCKED;
send_granted(fl, (fl->flags & LOCK_V4) ?
(enum nlm_stats)nlm4_granted : nlm_granted);
break;
case LKST_DYING:
(void)do_unlock(fl);
break;
default:
syslog(LOG_NOTICE, "bad lock status (%d) for"
" child %d", fl->status, pid);
}
}
}
}
/*
*
* try to acquire the lock described by fl. Eventually fork a child to do a
* blocking lock if allowed and required.
*/
enum nlm_stats
do_lock(struct file_lock *fl, int block)
{
int lflags, error;
struct stat st;
cli = get_client(fl->addr, (rpcvers_t)
((fl->flags & LOCK_V4) ? NLM_VERS4 : NLM_VERS));
if (cli == NULL) {
syslog(LOG_NOTICE, "failed to get CLIENT for %s.%" PRIu32,
fl->client_name, fl->client.svid);
/*
* We fail to notify remote that the lock has been granted.
* The client will timeout and retry, the lock will be
* granted at this time.
*/
return;
}
timeo.tv_sec = 0;
timeo.tv_usec = (fl->flags & LOCK_ASYNC) ? 0 : 500000; /* 0.5s */
/* process the next LKST_WAITING lock request for this fh */
LIST_FOREACH(fl, &lcklst_head, lcklst) {
if (fl->status != LKST_WAITING ||
fhcmp(&rfl->filehandle, &fl->filehandle) != 0)
continue;
lockst = do_lock(fl, 1); /* If it's LKST_WAITING we can block */
switch (lockst) {
case nlm4_granted:
/* case nlm_granted: same as nlm4_granted */
send_granted(fl, (fl->flags & LOCK_V4) ?
(enum nlm_stats)nlm4_granted : nlm_granted);
break;
case nlm4_blocked:
/* case nlm_blocked: same as nlm4_blocked */
break;
default:
lfree(fl);
break;
}
break;
}
lfree(rfl);
return error;
}