ms-nfs41-client/daemon/lock.c
Olga Kornievskaia 04ab888492 [cosmetic] minor license changes
added 2011 year to the copyright line
added authors info to the license
added UofM license to libtirpc files that we modified
(but i probably missed some)
2011-08-12 13:20:12 -04:00

368 lines
12 KiB
C

/* Copyright (c) 2010, 2011
* The Regents of the University of Michigan
* All Rights Reserved
*
* Olga Kornievskaia <aglo@umich.edu>
* Casey Bodley <cbodley@umich.edu>
*
* Permission is granted to use, copy and redistribute this software
* for noncommercial education and research purposes, so long as no
* fee is charged, and so long as the name of the University of Michigan
* is not used in any advertising or publicity pertaining to the use
* or distribution of this software without specific, written prior
* authorization. Permission to modify or otherwise create derivative
* works of this software is not granted.
*
* This software is provided as is, without representation or warranty
* of any kind either express or implied, including without limitation
* the implied warranties of merchantability, fitness for a particular
* purpose, or noninfringement. The Regents of the University of
* Michigan shall not be liable for any damages, including special,
* indirect, incidental, or consequential damages, with respect to any
* claim arising out of or in connection with the use of the software,
* even if it has been or is hereafter advised of the possibility of
* such damages.
*/
#include <Windows.h>
#include <stdio.h>
#include "daemon_debug.h"
#include "delegation.h"
#include "nfs41_ops.h"
#include "upcall.h"
#include "util.h"
#define LKLVL 2 /* dprintf level for lock logging */
static void lock_stateid_arg(
IN nfs41_open_state *state,
OUT stateid_arg *arg)
{
arg->open = state;
arg->delegation = NULL;
AcquireSRWLockShared(&state->lock);
if (state->locks.stateid.seqid) {
memcpy(&arg->stateid, &state->locks.stateid, sizeof(stateid4));
arg->type = STATEID_LOCK;
} else if (state->do_close) {
memcpy(&arg->stateid, &state->stateid, sizeof(stateid4));
arg->type = STATEID_OPEN;
} else {
memset(&arg->stateid, 0, sizeof(stateid4));
arg->type = STATEID_SPECIAL;
}
ReleaseSRWLockShared(&state->lock);
}
/* expects the caller to hold an exclusive lock on nfs41_open_state.lock */
static void lock_stateid_update(
OUT nfs41_open_state *state,
IN const stateid4 *stateid)
{
if (state->locks.stateid.seqid == 0) {
/* if it's a new lock stateid, copy it in */
memcpy(&state->locks.stateid, stateid, sizeof(stateid4));
} else if (stateid->seqid > state->locks.stateid.seqid) {
/* update the seqid if it's more recent */
state->locks.stateid.seqid = stateid->seqid;
}
}
static int open_lock_add(
IN nfs41_open_state *open,
IN const stateid_arg *stateid,
IN const nfs41_lock_state *input)
{
nfs41_lock_state *lock;
int status = NO_ERROR;
AcquireSRWLockExclusive(&open->lock);
if (stateid->type == STATEID_LOCK)
lock_stateid_update(open, &stateid->stateid);
lock = calloc(1, sizeof(nfs41_lock_state));
if (lock == NULL) {
status = GetLastError();
goto out;
}
lock->offset = input->offset;
lock->length = input->length;
lock->exclusive = input->exclusive;
lock->id = open->locks.counter++;
list_add_tail(&open->locks.list, &lock->open_entry);
out:
ReleaseSRWLockExclusive(&open->lock);
return status;
}
static bool_t open_lock_delegate(
IN nfs41_open_state *open,
IN const nfs41_lock_state *input)
{
bool_t delegated = FALSE;
AcquireSRWLockExclusive(&open->lock);
if (open->delegation.state) {
nfs41_delegation_state *deleg = open->delegation.state;
AcquireSRWLockShared(&deleg->lock);
if (deleg->state.type == OPEN_DELEGATE_WRITE
&& deleg->status == DELEGATION_GRANTED) {
nfs41_lock_state *lock = calloc(1, sizeof(nfs41_lock_state));
if (lock) {
lock->offset = input->offset;
lock->length = input->length;
lock->exclusive = input->exclusive;
lock->delegated = 1;
lock->id = open->locks.counter++;
list_add_tail(&open->locks.list, &lock->open_entry);
delegated = TRUE;
}
}
ReleaseSRWLockShared(&deleg->lock);
}
ReleaseSRWLockExclusive(&open->lock);
return delegated;
}
#define lock_entry(pos) list_container(pos, nfs41_lock_state, open_entry)
static int lock_range_cmp(const struct list_entry *entry, const void *value)
{
const nfs41_lock_state *lhs = lock_entry(entry);
const nfs41_lock_state *rhs = (const nfs41_lock_state*)value;
if (lhs->offset != rhs->offset) return -1;
if (lhs->length != rhs->length) return -1;
return 0;
}
static int open_unlock_delegate(
IN nfs41_open_state *open,
IN const nfs41_lock_state *input)
{
struct list_entry *entry;
int status = ERROR_NOT_LOCKED;
AcquireSRWLockExclusive(&open->lock);
/* find lock state that matches this range */
entry = list_search(&open->locks.list, input, lock_range_cmp);
if (entry) {
nfs41_lock_state *lock = lock_entry(entry);
if (lock->delegated) {
/* if the lock was delegated, remove/free it and return success */
list_remove(entry);
free(lock);
status = NO_ERROR;
} else
status = ERROR_LOCKED;
}
ReleaseSRWLockExclusive(&open->lock);
return status;
}
static void open_unlock_remove(
IN nfs41_open_state *open,
IN const stateid_arg *stateid,
IN const nfs41_lock_state *input)
{
struct list_entry *entry;
AcquireSRWLockExclusive(&open->lock);
if (stateid->type == STATEID_LOCK)
lock_stateid_update(open, &stateid->stateid);
/* find and remove the unlocked range */
entry = list_search(&open->locks.list, input, lock_range_cmp);
if (entry) {
list_remove(entry);
free(lock_entry(entry));
}
ReleaseSRWLockExclusive(&open->lock);
}
/* NFS41_LOCK */
static int parse_lock(unsigned char *buffer, uint32_t length, nfs41_upcall *upcall)
{
int status;
lock_upcall_args *args = &upcall->args.lock;
status = safe_read(&buffer, &length, &args->offset, sizeof(LONGLONG));
if (status) goto out;
status = safe_read(&buffer, &length, &args->length, sizeof(LONGLONG));
if (status) goto out;
status = safe_read(&buffer, &length, &args->exclusive, sizeof(BOOLEAN));
if (status) goto out;
status = safe_read(&buffer, &length, &args->blocking, sizeof(BOOLEAN));
if (status) goto out;
dprintf(1, "parsing NFS41_LOCK: offset=0x%llx length=0x%llx exclusive=%u "
"blocking=%u\n", args->offset, args->length, args->exclusive,
args->blocking);
out:
return status;
}
static __inline uint32_t get_lock_type(BOOLEAN exclusive, BOOLEAN blocking)
{
return blocking == 0
? ( exclusive == 0 ? READ_LT : WRITE_LT )
: ( exclusive == 0 ? READW_LT : WRITEW_LT );
}
static int handle_lock(nfs41_upcall *upcall)
{
nfs41_lock_state input;
stateid_arg stateid;
lock_upcall_args *args = &upcall->args.lock;
nfs41_open_state *state = upcall->state_ref;
const uint32_t type = get_lock_type(args->exclusive, args->blocking);
int status = NO_ERROR;
/* 18.10.3. Operation 12: LOCK - Create Lock
* "To lock the file from a specific offset through the end-of-file
* (no matter how long the file actually is) use a length field equal
* to NFS4_UINT64_MAX." */
if (args->length >= NFS4_UINT64_MAX - args->offset)
args->length = NFS4_UINT64_MAX;
input.offset = args->offset;
input.length = args->length;
input.exclusive = args->exclusive;
/* if we hold a write delegation, handle the lock locally */
if (open_lock_delegate(state, &input)) {
dprintf(LKLVL, "delegated lock { %llu, %llu }\n",
input.offset, input.length);
goto out;
}
/* open_to_lock_owner4 requires an open stateid; if we
* have a delegation, convert it to an open stateid */
status = nfs41_delegation_to_open(state, TRUE);
if (status) {
status = ERROR_FILE_INVALID;
goto out;
}
lock_stateid_arg(state, &stateid);
status = nfs41_lock(state->session, &state->file, &state->owner,
type, input.offset, input.length, FALSE, TRUE, &stateid);
if (status) {
dprintf(LKLVL, "nfs41_lock failed with %s\n",
nfs_error_string(status));
status = nfs_to_windows_error(status, ERROR_BAD_NET_RESP);
goto out;
}
/* ignore errors from open_lock_add(); they just mean we
* won't be able to recover the lock after reboot */
open_lock_add(state, &stateid, &input);
out:
return status;
}
static void cancel_lock(IN nfs41_upcall *upcall)
{
stateid_arg stateid;
nfs41_lock_state input;
lock_upcall_args *args = &upcall->args.lock;
nfs41_open_state *state = upcall->state_ref;
int status = NO_ERROR;
dprintf(1, "--> cancel_lock()\n");
if (upcall->status)
goto out;
input.offset = args->offset;
input.length = args->length;
/* search for the range to unlock, and remove if delegated */
status = open_unlock_delegate(state, &input);
if (status != ERROR_LOCKED)
goto out;
lock_stateid_arg(state, &stateid);
status = nfs41_unlock(state->session, &state->file,
args->offset, args->length, &stateid);
open_unlock_remove(state, &stateid, &input);
status = nfs_to_windows_error(status, ERROR_BAD_NET_RESP);
out:
dprintf(1, "<-- cancel_lock() returning %d\n", status);
}
/* NFS41_UNLOCK */
static int parse_unlock(unsigned char *buffer, uint32_t length, nfs41_upcall *upcall)
{
int status;
unlock_upcall_args *args = &upcall->args.unlock;
status = safe_read(&buffer, &length, &args->count, sizeof(ULONG));
if (status) goto out;
args->buf = buffer;
args->buf_len = length;
dprintf(1, "parsing NFS41_UNLOCK: count=%u\n", args->count);
out:
return status;
}
static int handle_unlock(nfs41_upcall *upcall)
{
nfs41_lock_state input;
stateid_arg stateid;
unlock_upcall_args *args = &upcall->args.unlock;
nfs41_open_state *state = upcall->state_ref;
unsigned char *buf = args->buf;
uint32_t buf_len = args->buf_len;
uint32_t i;
int status = NO_ERROR;
lock_stateid_arg(state, &stateid);
for (i = 0; i < args->count; i++) {
if (safe_read(&buf, &buf_len, &input.offset, sizeof(LONGLONG))) break;
if (safe_read(&buf, &buf_len, &input.length, sizeof(LONGLONG))) break;
/* do the same translation as LOCK, or the ranges won't match */
if (input.length >= NFS4_UINT64_MAX - input.offset)
input.length = NFS4_UINT64_MAX;
/* search for the range to unlock, and remove if delegated */
status = open_unlock_delegate(state, &input);
if (status != ERROR_LOCKED)
continue;
status = nfs41_unlock(state->session, &state->file,
input.offset, input.length, &stateid);
open_unlock_remove(state, &stateid, &input);
status = nfs_to_windows_error(status, ERROR_BAD_NET_RESP);
}
return status;
}
const nfs41_upcall_op nfs41_op_lock = {
parse_lock,
handle_lock,
NULL,
cancel_lock
};
const nfs41_upcall_op nfs41_op_unlock = {
parse_unlock,
handle_unlock
};