s3:locking:brlock: add function brl_cleanup_disconnected()
[mat/samba.git] / source3 / locking / brlock.c
index 836bbb49a78782b85afe6f76e3ce43421f4f0a6e..865aaca1b4fa7ea0b2cfc9516d5eb06fbda73d6f 100644 (file)
@@ -32,6 +32,7 @@
 #include "dbwrap/dbwrap_open.h"
 #include "serverid.h"
 #include "messages.h"
+#include "util_tdb.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_LOCKING
@@ -46,7 +47,7 @@ static struct db_context *brlock_db;
  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,
@@ -69,7 +70,7 @@ static void print_lock_struct(unsigned int i, struct lock_struct *pls)
 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));
 }
@@ -82,7 +83,7 @@ static bool brl_overlap(const struct lock_struct *lck1,
                         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 &&
@@ -252,7 +253,7 @@ NTSTATUS brl_lock_failed(files_struct *fsp, const struct lock_struct *lock, bool
                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) {
@@ -1487,7 +1488,7 @@ void brl_close_fnum(struct messaging_context *msg_ctx,
 {
        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);
@@ -1500,7 +1501,7 @@ void brl_close_fnum(struct messaging_context *msg_ctx,
                if (!locks_copy) {
                        smb_panic("brl_close_fnum: talloc failed");
                        }
-       } else {        
+       } else {
                locks_copy = NULL;
        }
 
@@ -1509,7 +1510,7 @@ void brl_close_fnum(struct messaging_context *msg_ctx,
        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,
@@ -1522,25 +1523,194 @@ void brl_close_fnum(struct messaging_context *msg_ctx,
        }
 }
 
+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;
@@ -1587,7 +1757,7 @@ struct brl_forall_cb {
  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;
@@ -1614,7 +1784,7 @@ static int traverse_fn(struct db_record *rec, void *state)
 
        /* 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 */
        }
@@ -1666,7 +1836,7 @@ int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
        }
        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;
@@ -1802,12 +1972,21 @@ static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
        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;
@@ -1958,7 +2137,7 @@ void brl_revalidate(struct messaging_context *msg_ctx,
        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
                         */
@@ -1974,3 +2153,75 @@ void brl_revalidate(struct messaging_context *msg_ctx,
        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;
+}