s3:locking: simplify the non cluster case of brl_get_locks_readonly()
[amitay/samba.git] / source3 / locking / brlock.c
index 18a798182b957048310287bfc275701b9e310e6f..95194dad16bcf08ed786a312c73e29955b8ad3c8 100644 (file)
    used. This allows us to provide the same semantics as NT */
 
 #include "includes.h"
+#include "librpc/gen_ndr/messaging.h"
+#include "smbd/globals.h"
+#include "dbwrap.h"
+#include "serverid.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_LOCKING
@@ -41,11 +45,11 @@ static struct db_context *brlock_db;
 
 static void print_lock_struct(unsigned int i, struct lock_struct *pls)
 {
-       DEBUG(10,("[%u]: smbpid = %u, tid = %u, pid = %s, ",
+       DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
                        i,
-                       (unsigned int)pls->context.smbpid,
+                       (unsigned long long)pls->context.smblctx,
                        (unsigned int)pls->context.tid,
-                       procid_str(debug_ctx(), &pls->context.pid) ));
+                       procid_str(talloc_tos(), &pls->context.pid) ));
        
        DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
                (double)pls->start,
@@ -63,7 +67,7 @@ bool brl_same_context(const struct lock_context *ctx1,
                             const struct lock_context *ctx2)
 {
        return (procid_equal(&ctx1->pid, &ctx2->pid) &&
-               (ctx1->smbpid == ctx2->smbpid) &&
+               (ctx1->smblctx == ctx2->smblctx) &&
                (ctx1->tid == ctx2->tid));
 }
 
@@ -264,12 +268,25 @@ NTSTATUS brl_lock_failed(files_struct *fsp, const struct lock_struct *lock, bool
 
 void brl_init(bool read_only)
 {
+       int tdb_flags;
+
        if (brlock_db) {
                return;
        }
+
+       tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
+
+       if (!lp_clustering()) {
+               /*
+                * We can't use the SEQNUM trick to cache brlock
+                * entries in the clustering case because ctdb seqnum
+                * propagation has a delay.
+                */
+               tdb_flags |= TDB_SEQNUM;
+       }
+
        brlock_db = db_open(NULL, lock_path("brlock.tdb"),
-                           lp_open_files_db_hash_size(),
-                           TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST,
+                           lp_open_files_db_hash_size(), tdb_flags,
                            read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644 );
        if (!brlock_db) {
                DEBUG(0,("Failed to open byte range locking database %s\n",
@@ -319,11 +336,16 @@ NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
 
        SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
 
+       if ((plock->start + plock->size - 1 < plock->start) &&
+                       plock->size != 0) {
+               return NT_STATUS_INVALID_LOCK_RANGE;
+       }
+
        for (i=0; i < br_lck->num_locks; i++) {
                /* Do any Windows or POSIX locks conflict ? */
                if (brl_conflict(&locks[i], plock)) {
                        /* Remember who blocked us. */
-                       plock->context.smbpid = locks[i].context.smbpid;
+                       plock->context.smblctx = locks[i].context.smblctx;
                        return brl_lock_failed(fsp,plock,blocking_lock);
                }
 #if ZERO_ZERO
@@ -354,7 +376,7 @@ NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
                                &errno_ret)) {
 
                        /* We don't know who blocked us. */
-                       plock->context.smbpid = 0xFFFFFFFF;
+                       plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
 
                        if (errno_ret == EACCES || errno_ret == EAGAIN) {
                                status = NT_STATUS_FILE_LOCK_CONFLICT;
@@ -697,8 +719,7 @@ static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
        }
 
        /* Don't allow 64-bit lock wrap. */
-       if (plock->start + plock->size < plock->start ||
-                       plock->start + plock->size < plock->size) {
+       if (plock->start + plock->size - 1 < plock->start) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -729,7 +750,7 @@ static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
                                /* No games with error messages. */
                                SAFE_FREE(tp);
                                /* Remember who blocked us. */
-                               plock->context.smbpid = curr_lock->context.smbpid;
+                               plock->context.smblctx = curr_lock->context.smblctx;
                                return NT_STATUS_FILE_LOCK_CONFLICT;
                        }
                        /* Just copy the Windows lock into the new array. */
@@ -744,7 +765,7 @@ static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
                                /* No games with error messages. */
                                SAFE_FREE(tp);
                                /* Remember who blocked us. */
-                               plock->context.smbpid = curr_lock->context.smbpid;
+                               plock->context.smblctx = curr_lock->context.smblctx;
                                return NT_STATUS_FILE_LOCK_CONFLICT;
                        }
 
@@ -804,7 +825,7 @@ static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
                                &errno_ret)) {
 
                        /* We don't know who blocked us. */
-                       plock->context.smbpid = 0xFFFFFFFF;
+                       plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
 
                        if (errno_ret == EACCES || errno_ret == EAGAIN) {
                                SAFE_FREE(tp);
@@ -884,14 +905,14 @@ NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
 
 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
                struct byte_range_lock *br_lck,
-               uint32 smbpid,
+               uint64_t smblctx,
                struct server_id pid,
                br_off start,
                br_off size, 
                enum brl_type lock_type,
                enum brl_flavour lock_flav,
                bool blocking_lock,
-               uint32 *psmbpid,
+               uint64_t *psmblctx,
                struct blocking_lock_record *blr)
 {
        NTSTATUS ret;
@@ -908,7 +929,7 @@ NTSTATUS brl_lock(struct messaging_context *msg_ctx,
        memset(&lock, '\0', sizeof(lock));
 #endif
 
-       lock.context.smbpid = smbpid;
+       lock.context.smblctx = smblctx;
        lock.context.pid = pid;
        lock.context.tid = br_lck->fsp->conn->cnum;
        lock.start = start;
@@ -926,12 +947,12 @@ NTSTATUS brl_lock(struct messaging_context *msg_ctx,
 
 #if ZERO_ZERO
        /* sort the lock list */
-       qsort(br_lck->lock_data, (size_t)br_lck->num_locks, sizeof(lock), lock_compare);
+       TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
 #endif
 
        /* If we're returning an error, return who blocked us. */
-       if (!NT_STATUS_IS_OK(ret) && psmbpid) {
-               *psmbpid = lock.context.smbpid;
+       if (!NT_STATUS_IS_OK(ret) && psmblctx) {
+               *psmblctx = lock.context.smblctx;
        }
        return ret;
 }
@@ -979,6 +1000,10 @@ bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
        for (i = 0; i < br_lck->num_locks; i++) {
                struct lock_struct *lock = &locks[i];
 
+               if (IS_PENDING_LOCK(lock->lock_type)) {
+                       continue;
+               }
+
                /* Only remove our own locks that match in start, size, and flavour. */
                if (brl_same_context(&lock->context, &plock->context) &&
                                        lock->fnum == plock->fnum &&
@@ -1213,7 +1238,7 @@ bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
 
 bool brl_unlock(struct messaging_context *msg_ctx,
                struct byte_range_lock *br_lck,
-               uint32 smbpid,
+               uint64_t smblctx,
                struct server_id pid,
                br_off start,
                br_off size,
@@ -1221,7 +1246,7 @@ bool brl_unlock(struct messaging_context *msg_ctx,
 {
        struct lock_struct lock;
 
-       lock.context.smbpid = smbpid;
+       lock.context.smblctx = smblctx;
        lock.context.pid = pid;
        lock.context.tid = br_lck->fsp->conn->cnum;
        lock.start = start;
@@ -1244,7 +1269,7 @@ bool brl_unlock(struct messaging_context *msg_ctx,
 ****************************************************************************/
 
 bool brl_locktest(struct byte_range_lock *br_lck,
-               uint32 smbpid,
+               uint64_t smblctx,
                struct server_id pid,
                br_off start,
                br_off size, 
@@ -1257,7 +1282,7 @@ bool brl_locktest(struct byte_range_lock *br_lck,
        const struct lock_struct *locks = br_lck->lock_data;
        files_struct *fsp = br_lck->fsp;
 
-       lock.context.smbpid = smbpid;
+       lock.context.smblctx = smblctx;
        lock.context.pid = pid;
        lock.context.tid = br_lck->fsp->conn->cnum;
        lock.start = start;
@@ -1302,7 +1327,7 @@ bool brl_locktest(struct byte_range_lock *br_lck,
 ****************************************************************************/
 
 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
-               uint32 *psmbpid,
+               uint64_t *psmblctx,
                struct server_id pid,
                br_off *pstart,
                br_off *psize, 
@@ -1314,7 +1339,7 @@ NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
        const struct lock_struct *locks = br_lck->lock_data;
        files_struct *fsp = br_lck->fsp;
 
-       lock.context.smbpid = *psmbpid;
+       lock.context.smblctx = *psmblctx;
        lock.context.pid = pid;
        lock.context.tid = br_lck->fsp->conn->cnum;
        lock.start = *pstart;
@@ -1335,7 +1360,7 @@ NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
                }
 
                if (conflict) {
-                       *psmbpid = exlock->context.smbpid;
+                       *psmblctx = exlock->context.smblctx;
                        *pstart = exlock->start;
                        *psize = exlock->size;
                        *plock_type = exlock->lock_type;
@@ -1356,8 +1381,8 @@ NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
                        fsp->fnum, fsp_str_dbg(fsp)));
 
                if (ret) {
-                       /* Hmmm. No clue what to set smbpid to - use -1. */
-                       *psmbpid = 0xFFFF;
+                       /* Hmmm. No clue what to set smblctx to - use -1. */
+                       *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
                        return NT_STATUS_LOCK_NOT_GRANTED;
                }
         }
@@ -1379,7 +1404,7 @@ bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
  Remove a particular pending lock.
 ****************************************************************************/
 bool brl_lock_cancel(struct byte_range_lock *br_lck,
-               uint32 smbpid,
+               uint64_t smblctx,
                struct server_id pid,
                br_off start,
                br_off size,
@@ -1389,7 +1414,7 @@ bool brl_lock_cancel(struct byte_range_lock *br_lck,
        bool ret;
        struct lock_struct lock;
 
-       lock.context.smbpid = smbpid;
+       lock.context.smblctx = smblctx;
        lock.context.pid = pid;
        lock.context.tid = br_lck->fsp->conn->cnum;
        lock.start = start;
@@ -1461,7 +1486,7 @@ void brl_close_fnum(struct messaging_context *msg_ctx,
        unsigned int i, j, dcount=0;
        int num_deleted_windows_locks = 0;
        struct lock_struct *locks = br_lck->lock_data;
-       struct server_id pid = procid_self();
+       struct server_id pid = sconn_server_id(fsp->conn->sconn);
        bool unlock_individually = False;
        bool posix_level2_contention_ended = false;
 
@@ -1511,7 +1536,7 @@ void brl_close_fnum(struct messaging_context *msg_ctx,
                                                (lock->fnum == fnum)) {
                                        brl_unlock(msg_ctx,
                                                br_lck,
-                                               lock->context.smbpid,
+                                               lock->context.smblctx,
                                                pid,
                                                lock->start,
                                                lock->size,
@@ -1602,7 +1627,7 @@ static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct
 
        for (i = 0; i < *pnum_entries; i++) {
                struct lock_struct *lock_data = &locks[i];
-               if (!process_exists(lock_data->context.pid)) {
+               if (!serverid_exists(&lock_data->context.pid)) {
                        /* This process no longer exists - mark this
                           entry as invalid by zeroing it. */
                        ZERO_STRUCTP(lock_data);
@@ -1624,7 +1649,7 @@ static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct
                        num_valid_entries = 0;
                        for (i = 0; i < *pnum_entries; i++) {
                                struct lock_struct *lock_data = &locks[i];
-                               if (lock_data->context.smbpid &&
+                               if (lock_data->context.smblctx &&
                                                lock_data->context.tid) {
                                        /* Valid (nonzero) entry - copy it. */
                                        memcpy(&new_lock_data[num_valid_entries],
@@ -1738,7 +1763,7 @@ int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
  Unlock the record.
 ********************************************************************/
 
-static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
+static void byte_range_lock_flush(struct byte_range_lock *br_lck)
 {
        if (br_lck->read_only) {
                SMB_ASSERT(!br_lck->modified);
@@ -1773,8 +1798,16 @@ static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
 
  done:
 
-       SAFE_FREE(br_lck->lock_data);
+       br_lck->read_only = true;
+       br_lck->modified = false;
+
        TALLOC_FREE(br_lck->record);
+}
+
+static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
+{
+       byte_range_lock_flush(br_lck);
+       SAFE_FREE(br_lck->lock_data);
        return 0;
 }
 
@@ -1789,6 +1822,7 @@ static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
 {
        TDB_DATA key, data;
        struct byte_range_lock *br_lck = TALLOC_P(mem_ctx, struct byte_range_lock);
+       bool do_read_only = read_only;
 
        if (br_lck == NULL) {
                return NULL;
@@ -1797,7 +1831,6 @@ static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
        br_lck->fsp = fsp;
        br_lck->num_locks = 0;
        br_lck->modified = False;
-       memset(&br_lck->key, '\0', sizeof(struct file_id));
        br_lck->key = fsp->file_id;
 
        key.dptr = (uint8 *)&br_lck->key;
@@ -1806,18 +1839,17 @@ static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
        if (!fsp->lockdb_clean) {
                /* We must be read/write to clean
                   the dead entries. */
-               read_only = False;
+               do_read_only = false;
        }
 
-       if (read_only) {
+       if (do_read_only) {
                if (brlock_db->fetch(brlock_db, br_lck, key, &data) == -1) {
                        DEBUG(3, ("Could not fetch byte range lock record\n"));
                        TALLOC_FREE(br_lck);
                        return NULL;
                }
                br_lck->record = NULL;
-       }
-       else {
+       } else {
                br_lck->record = brlock_db->fetch_locked(brlock_db, br_lck, key);
 
                if (br_lck->record == NULL) {
@@ -1829,7 +1861,7 @@ static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
                data = br_lck->record->value;
        }
 
-       br_lck->read_only = read_only;
+       br_lck->read_only = do_read_only;
        br_lck->lock_data = NULL;
 
        talloc_set_destructor(br_lck, byte_range_lock_destructor);
@@ -1881,6 +1913,15 @@ static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
                        print_lock_struct(i, &locks[i]);
                }
        }
+
+       if (do_read_only != read_only) {
+               /*
+                * this stores the record and gets rid of
+                * the write lock that is needed for a cleanup
+                */
+               byte_range_lock_flush(br_lck);
+       }
+
        return br_lck;
 }
 
@@ -1890,10 +1931,30 @@ struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
        return brl_get_locks_internal(mem_ctx, fsp, False);
 }
 
-struct byte_range_lock *brl_get_locks_readonly(TALLOC_CTX *mem_ctx,
-                                       files_struct *fsp)
+struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
 {
-       return brl_get_locks_internal(mem_ctx, fsp, True);
+       struct byte_range_lock *br_lock;
+
+       if (lp_clustering()) {
+               return brl_get_locks_internal(talloc_tos(), fsp, true);
+       }
+
+       if ((fsp->brlock_rec != NULL)
+           && (brlock_db->get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
+               return fsp->brlock_rec;
+       }
+
+       TALLOC_FREE(fsp->brlock_rec);
+
+       br_lock = brl_get_locks_internal(talloc_tos(), fsp, true);
+       if (br_lock == NULL) {
+               return NULL;
+       }
+       fsp->brlock_seqnum = brlock_db->get_seqnum(brlock_db);
+
+       fsp->brlock_rec = talloc_move(fsp, &br_lock);
+
+       return fsp->brlock_rec;
 }
 
 struct brl_revalidate_state {
@@ -1973,8 +2034,7 @@ static void brl_revalidate(struct messaging_context *msg_ctx,
                goto done;
        }
 
-       qsort(state->pids, state->num_pids, sizeof(state->pids[0]),
-             compare_procids);
+       TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
 
        ZERO_STRUCT(last_pid);