-/*
+/*
Unix SMB/CIFS implementation.
byte range locking code
Updated to handle range splits/merges.
#include "dbwrap/dbwrap_open.h"
#include "serverid.h"
#include "messages.h"
+#include "util_tdb.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_LOCKING
Debug info at level 10 for lock struct.
****************************************************************************/
-static void print_lock_struct(unsigned int i, struct lock_struct *pls)
+static void print_lock_struct(unsigned int i, const struct lock_struct *pls)
{
DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
i,
(unsigned int)pls->context.tid,
server_id_str(talloc_tos(), &pls->context.pid) ));
- DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
+ DEBUG(10,("start = %.0f, size = %.0f, fnum = %llu, %s %s\n",
(double)pls->start,
(double)pls->size,
- pls->fnum,
+ (unsigned long long)pls->fnum,
lock_type_name(pls->lock_type),
lock_flav_name(pls->lock_flav) ));
}
See if two locking contexts are equal.
****************************************************************************/
-bool brl_same_context(const struct lock_context *ctx1,
+static bool brl_same_context(const struct lock_context *ctx1,
const struct lock_context *ctx2)
{
- return (procid_equal(&ctx1->pid, &ctx2->pid) &&
+ return (serverid_equal(&ctx1->pid, &ctx2->pid) &&
(ctx1->smblctx == ctx2->smblctx) &&
(ctx1->tid == ctx2->tid));
}
const struct lock_struct *lck2)
{
/* XXX Remove for Win7 compatibility. */
- /* this extra check is not redundent - it copes with locks
+ /* this extra check is not redundant - it copes with locks
that go beyond the end of 64 bit file space */
if (lck1->size != 0 &&
lck1->start == lck2->start &&
See if lock2 can be added when lock1 is in place.
****************************************************************************/
-static bool brl_conflict(const struct lock_struct *lck1,
+static bool brl_conflict(const struct lock_struct *lck1,
const struct lock_struct *lck2)
{
/* Ignore PENDING locks. */
}
return brl_overlap(lck1, lck2);
-}
+}
/****************************************************************************
See if lock2 can be added when lock1 is in place - when both locks are POSIX
know already match.
****************************************************************************/
-static bool brl_conflict_posix(const struct lock_struct *lck1,
+static bool brl_conflict_posix(const struct lock_struct *lck1,
const struct lock_struct *lck2)
{
#if defined(DEVELOPER)
/* One is read, the other write, or the context is different,
do they overlap ? */
return brl_overlap(lck1, lck2);
-}
+}
#if ZERO_ZERO
-static bool brl_conflict1(const struct lock_struct *lck1,
+static bool brl_conflict1(const struct lock_struct *lck1,
const struct lock_struct *lck2)
{
if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
}
return True;
-}
+}
#endif
/****************************************************************************
if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
return False;
- if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
+ if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
return False;
/* POSIX flavour locks never conflict here - this is only called
}
return brl_overlap(lck1, lck2);
-}
+}
/****************************************************************************
Check if an unlock overlaps a pending lock.
return NT_STATUS_FILE_LOCK_CONFLICT;
}
- if (procid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
+ if (serverid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
lock->context.tid == fsp->last_lock_failure.context.tid &&
lock->fnum == fsp->last_lock_failure.fnum &&
lock->start == fsp->last_lock_failure.start) {
Compare two locks for sorting.
****************************************************************************/
-static int lock_compare(const struct lock_struct *lck1,
+static int lock_compare(const struct lock_struct *lck1,
const struct lock_struct *lck2)
{
if (lck1->start != lck2->start) {
return brl_lock_failed(fsp,plock,blocking_lock);
}
#if ZERO_ZERO
- if (plock->start == 0 && plock->size == 0 &&
+ if (plock->start == 0 && plock->size == 0 &&
locks[i].size == 0) {
break;
}
uint64_t smblctx,
struct server_id pid,
br_off start,
- br_off size,
+ br_off size,
enum brl_type lock_type,
enum brl_flavour lock_flav,
bool blocking_lock,
/* Actually delete the lock. */
if (i < br_lck->num_locks - 1) {
- memmove(&locks[i], &locks[i+1],
+ memmove(&locks[i], &locks[i+1],
sizeof(*locks)*((br_lck->num_locks-1) - i));
}
const struct lock_struct *plock)
{
VFS_FIND(brl_unlock_windows);
- return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck,
+ return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck,
plock);
}
uint64_t smblctx,
struct server_id pid,
br_off start,
- br_off size,
+ br_off size,
enum brl_type lock_type,
enum brl_flavour lock_flav)
{
if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
- DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
+ DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for %s file %s\n",
(double)start, (double)size, ret ? "locked" : "unlocked",
- fsp->fnum, fsp_str_dbg(fsp)));
+ fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
/* We need to return the inverse of is_posix_locked. */
ret = !ret;
uint64_t *psmblctx,
struct server_id pid,
br_off *pstart,
- br_off *psize,
+ br_off *psize,
enum brl_type *plock_type,
enum brl_flavour lock_flav)
{
if (exlock->lock_flav == WINDOWS_LOCK) {
conflict = brl_conflict(exlock, &lock);
- } else {
+ } else {
conflict = brl_conflict_posix(exlock, &lock);
}
if(lp_posix_locking(fsp->conn->params)) {
bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
- DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
+ DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for %s file %s\n",
(double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
- fsp->fnum, fsp_str_dbg(fsp)));
+ fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
if (ret) {
/* Hmmm. No clue what to set smblctx to - use -1. */
if (i < br_lck->num_locks - 1) {
/* Found this particular pending lock - delete it */
- memmove(&locks[i], &locks[i+1],
+ memmove(&locks[i], &locks[i+1],
sizeof(*locks)*((br_lck->num_locks-1) - i));
}
{
files_struct *fsp = br_lck->fsp;
uint32_t tid = fsp->conn->cnum;
- int fnum = fsp->fnum;
+ uint64_t fnum = fsp->fnum;
unsigned int i;
struct lock_struct *locks = br_lck->lock_data;
struct server_id pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
if (!locks_copy) {
smb_panic("brl_close_fnum: talloc failed");
}
- } else {
+ } else {
locks_copy = NULL;
}
for (i=0; i < num_locks_copy; i++) {
struct lock_struct *lock = &locks_copy[i];
- if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid) &&
+ if (lock->context.tid == tid && serverid_equal(&lock->context.pid, &pid) &&
(lock->fnum == fnum)) {
brl_unlock(msg_ctx,
br_lck,
}
}
+bool brl_mark_disconnected(struct files_struct *fsp)
+{
+ uint32_t tid = fsp->conn->cnum;
+ uint64_t smblctx = fsp->op->global->open_persistent_id;
+ uint64_t fnum = fsp->fnum;
+ unsigned int i;
+ struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
+ struct byte_range_lock *br_lck = NULL;
+
+ if (!fsp->op->global->durable) {
+ return false;
+ }
+
+ if (fsp->current_lock_count == 0) {
+ return true;
+ }
+
+ br_lck = brl_get_locks(talloc_tos(), fsp);
+ if (br_lck == NULL) {
+ return false;
+ }
+
+ for (i=0; i < br_lck->num_locks; i++) {
+ struct lock_struct *lock = &br_lck->lock_data[i];
+
+ /*
+ * as this is a durable handle, we only expect locks
+ * of the current file handle!
+ */
+
+ if (lock->context.smblctx != smblctx) {
+ TALLOC_FREE(br_lck);
+ return false;
+ }
+
+ if (lock->context.tid != tid) {
+ TALLOC_FREE(br_lck);
+ return false;
+ }
+
+ if (!serverid_equal(&lock->context.pid, &self)) {
+ TALLOC_FREE(br_lck);
+ return false;
+ }
+
+ if (lock->fnum != fnum) {
+ TALLOC_FREE(br_lck);
+ return false;
+ }
+
+ server_id_set_disconnected(&lock->context.pid);
+ lock->context.tid = TID_FIELD_INVALID;
+ lock->fnum = FNUM_FIELD_INVALID;
+ }
+
+ br_lck->modified = true;
+ TALLOC_FREE(br_lck);
+ return true;
+}
+
+bool brl_reconnect_disconnected(struct files_struct *fsp)
+{
+ uint32_t tid = fsp->conn->cnum;
+ uint64_t smblctx = fsp->op->global->open_persistent_id;
+ uint64_t fnum = fsp->fnum;
+ unsigned int i;
+ struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
+ struct byte_range_lock *br_lck = NULL;
+
+ if (!fsp->op->global->durable) {
+ return false;
+ }
+
+ /*
+ * When reconnecting, we do not want to validate the brlock entries
+ * and thereby remove our own (disconnected) entries but reactivate
+ * them instead.
+ */
+ fsp->lockdb_clean = true;
+
+ br_lck = brl_get_locks(talloc_tos(), fsp);
+ if (br_lck == NULL) {
+ return false;
+ }
+
+ if (br_lck->num_locks == 0) {
+ TALLOC_FREE(br_lck);
+ return true;
+ }
+
+ for (i=0; i < br_lck->num_locks; i++) {
+ struct lock_struct *lock = &br_lck->lock_data[i];
+
+ /*
+ * as this is a durable handle we only expect locks
+ * of the current file handle!
+ */
+
+ if (lock->context.smblctx != smblctx) {
+ TALLOC_FREE(br_lck);
+ return false;
+ }
+
+ if (lock->context.tid != TID_FIELD_INVALID) {
+ TALLOC_FREE(br_lck);
+ return false;
+ }
+
+ if (!server_id_is_disconnected(&lock->context.pid)) {
+ TALLOC_FREE(br_lck);
+ return false;
+ }
+
+ if (lock->fnum != FNUM_FIELD_INVALID) {
+ TALLOC_FREE(br_lck);
+ return false;
+ }
+
+ lock->context.pid = self;
+ lock->context.tid = tid;
+ lock->fnum = fnum;
+ }
+
+ fsp->current_lock_count = br_lck->num_locks;
+ br_lck->modified = true;
+ TALLOC_FREE(br_lck);
+ return true;
+}
+
/****************************************************************************
Ensure this set of lock entries is valid.
****************************************************************************/
-static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks)
+static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks,
+ bool keep_disconnected)
{
unsigned int i;
unsigned int num_valid_entries = 0;
struct lock_struct *locks = *pplocks;
+ TALLOC_CTX *frame = talloc_stackframe();
+ struct server_id *ids;
+ bool *exists;
+
+ ids = talloc_array(frame, struct server_id, *pnum_entries);
+ if (ids == NULL) {
+ DEBUG(0, ("validate_lock_entries: "
+ "talloc_array(struct server_id, %u) failed\n",
+ *pnum_entries));
+ talloc_free(frame);
+ return false;
+ }
+
+ exists = talloc_array(frame, bool, *pnum_entries);
+ if (exists == NULL) {
+ DEBUG(0, ("validate_lock_entries: "
+ "talloc_array(bool, %u) failed\n",
+ *pnum_entries));
+ talloc_free(frame);
+ return false;
+ }
for (i = 0; i < *pnum_entries; i++) {
- struct lock_struct *lock_data = &locks[i];
- if (!serverid_exists(&lock_data->context.pid)) {
- /* This process no longer exists - mark this
- entry as invalid by zeroing it. */
- ZERO_STRUCTP(lock_data);
- } else {
+ ids[i] = locks[i].context.pid;
+ }
+
+ if (!serverids_exist(ids, *pnum_entries, exists)) {
+ DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
+ talloc_free(frame);
+ return false;
+ }
+
+ for (i = 0; i < *pnum_entries; i++) {
+ if (exists[i]) {
num_valid_entries++;
+ continue;
}
+
+ if (keep_disconnected &&
+ server_id_is_disconnected(&ids[i]))
+ {
+ num_valid_entries++;
+ continue;
+ }
+
+ /* This process no longer exists - mark this
+ entry as invalid by zeroing it. */
+ ZERO_STRUCTP(&locks[i]);
}
+ TALLOC_FREE(frame);
if (num_valid_entries != *pnum_entries) {
struct lock_struct *new_lock_data = NULL;
on each lock.
****************************************************************************/
-static int traverse_fn(struct db_record *rec, void *state)
+static int brl_traverse_fn(struct db_record *rec, void *state)
{
struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
struct lock_struct *locks;
/* Ensure the lock db is clean of entries from invalid processes. */
- if (!validate_lock_entries(&num_locks, &locks)) {
+ if (!validate_lock_entries(&num_locks, &locks, true)) {
SAFE_FREE(locks);
return -1; /* Terminate traversal */
}
}
cb.fn = fn;
cb.private_data = private_data;
- status = dbwrap_traverse(brlock_db, traverse_fn, &cb, &count);
+ status = dbwrap_traverse(brlock_db, brl_traverse_fn, &cb, &count);
if (!NT_STATUS_IS_OK(status)) {
return -1;
if (!fsp->lockdb_clean) {
int orig_num_locks = br_lck->num_locks;
- /* This is the first time we've accessed this. */
- /* Go through and ensure all entries exist - remove any that don't. */
- /* Makes the lockdb self cleaning at low cost. */
+ /*
+ * This is the first time we access the byte range lock
+ * record with this fsp. Go through and ensure all entries
+ * are valid - remove any that don't.
+ * This makes the lockdb self cleaning at low cost.
+ *
+ * Note: Disconnected entries belong to disconnected
+ * durable handles. So at this point, we have a new
+ * handle on the file and the disconnected durable has
+ * already been closed (we are not a durable reconnect).
+ * So we need to clean the disconnected brl entry.
+ */
if (!validate_lock_entries(&br_lck->num_locks,
- &br_lck->lock_data)) {
+ &br_lck->lock_data, false)) {
SAFE_FREE(br_lck->lock_data);
TALLOC_FREE(br_lck);
return NULL;
ZERO_STRUCT(last_pid);
for (i=0; i<state->num_pids; i++) {
- if (procid_equal(&last_pid, &state->pids[i])) {
+ if (serverid_equal(&last_pid, &state->pids[i])) {
/*
* We've seen that one already
*/
TALLOC_FREE(state);
return;
}
+
+bool brl_cleanup_disconnected(struct file_id fid, uint64_t open_persistent_id)
+{
+ bool ret = false;
+ TALLOC_CTX *frame = talloc_stackframe();
+ TDB_DATA key, val;
+ struct db_record *rec;
+ struct lock_struct *lock;
+ unsigned n, num;
+ NTSTATUS status;
+
+ key = make_tdb_data((void*)&fid, sizeof(fid));
+
+ rec = dbwrap_fetch_locked(brlock_db, frame, key);
+ if (rec == NULL) {
+ DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
+ "for file %s\n", file_id_string(frame, &fid)));
+ goto done;
+ }
+
+ val = dbwrap_record_get_value(rec);
+ lock = (struct lock_struct*)val.dptr;
+ num = val.dsize / sizeof(struct lock_struct);
+ if (lock == NULL) {
+ DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
+ "file %s\n", file_id_string(frame, &fid)));
+ ret = true;
+ goto done;
+ }
+
+ for (n=0; n<num; n++) {
+ struct lock_context *ctx = &lock[n].context;
+
+ if (!server_id_is_disconnected(&ctx->pid)) {
+ DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
+ "%s used by server %s, do not cleanup\n",
+ file_id_string(frame, &fid),
+ server_id_str(frame, &ctx->pid)));
+ goto done;
+ }
+
+ if (ctx->smblctx != open_persistent_id) {
+ DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
+ "%s expected smblctx %llu but found %llu"
+ ", do not cleanup\n",
+ file_id_string(frame, &fid),
+ (unsigned long long)open_persistent_id,
+ (unsigned long long)ctx->smblctx));
+ goto done;
+ }
+ }
+
+ status = dbwrap_record_delete(rec);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
+ "for file %s from %s, open %llu: %s\n",
+ file_id_string(frame, &fid), dbwrap_name(brlock_db),
+ (unsigned long long)open_persistent_id,
+ nt_errstr(status)));
+ goto done;
+ }
+
+ DEBUG(10, ("brl_cleanup_disconnected: "
+ "file %s cleaned up %u entries from open %llu\n",
+ file_id_string(frame, &fid), num,
+ (unsigned long long)open_persistent_id));
+
+ ret = true;
+done:
+ talloc_free(frame);
+ return ret;
+}