More simple const fixes.
[amitay/samba.git] / source3 / locking / brlock.c
index ee0eaf5198bf59896359312fd4c440582ea0f34e..fd3bf2319e46279eff268294a1e52c1e08c00341 100644 (file)
    used. This allows us to provide the same semantics as NT */
 
 #include "includes.h"
-#include "librpc/gen_ndr/messaging.h"
+#include "system/filesys.h"
+#include "locking/proto.h"
+#include "smbd/globals.h"
+#include "dbwrap.h"
+#include "serverid.h"
+#include "messages.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_LOCKING
@@ -42,9 +47,9 @@ 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(talloc_tos(), &pls->context.pid) ));
        
@@ -64,7 +69,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));
 }
 
@@ -271,7 +276,7 @@ void brl_init(bool read_only)
                return;
        }
 
-       tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST;
+       tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
 
        if (!lp_clustering()) {
                /*
@@ -333,16 +338,16 @@ NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
 
        SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
 
-       for (i=0; i < br_lck->num_locks; i++) {
-               if (locks[i].start + locks[i].size < locks[i].start) {
-                       /* 64-bit wrap. Error. */
-                       return NT_STATUS_INVALID_LOCK_RANGE;
-               }
+       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
@@ -373,7 +378,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;
@@ -716,8 +721,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;
        }
 
@@ -748,7 +752,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. */
@@ -763,7 +767,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;
                        }
 
@@ -823,7 +827,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);
@@ -903,14 +907,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;
@@ -927,7 +931,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;
@@ -949,8 +953,8 @@ NTSTATUS brl_lock(struct messaging_context *msg_ctx,
 #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;
 }
@@ -998,6 +1002,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 &&
@@ -1232,7 +1240,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,
@@ -1240,7 +1248,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;
@@ -1263,7 +1271,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, 
@@ -1276,7 +1284,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;
@@ -1321,7 +1329,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, 
@@ -1333,7 +1341,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;
@@ -1354,7 +1362,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;
@@ -1375,8 +1383,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;
                }
         }
@@ -1398,7 +1406,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,
@@ -1408,7 +1416,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;
@@ -1480,7 +1488,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;
 
@@ -1530,7 +1538,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,
@@ -1643,7 +1651,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],
@@ -1757,7 +1765,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);
@@ -1792,8 +1800,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;
 }
 
@@ -1808,6 +1824,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;
@@ -1824,18 +1841,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) {
@@ -1847,7 +1863,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);
@@ -1899,6 +1915,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;
 }
 
@@ -1923,34 +1948,15 @@ struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
 
        TALLOC_FREE(fsp->brlock_rec);
 
-       br_lock = brl_get_locks_internal(talloc_tos(), fsp, false);
+       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_zero(fsp, struct byte_range_lock);
-       if (fsp->brlock_rec == NULL) {
-               goto fail;
-       }
-       fsp->brlock_rec->fsp = fsp;
-       fsp->brlock_rec->num_locks = br_lock->num_locks;
-       fsp->brlock_rec->read_only = true;
-       fsp->brlock_rec->key = br_lock->key;
+       fsp->brlock_rec = talloc_move(fsp, &br_lock);
 
-       fsp->brlock_rec->lock_data = (struct lock_struct *)
-               talloc_memdup(fsp->brlock_rec, br_lock->lock_data,
-                             sizeof(struct lock_struct) * br_lock->num_locks);
-       if (fsp->brlock_rec->lock_data == NULL) {
-               goto fail;
-       }
-
-       TALLOC_FREE(br_lock);
        return fsp->brlock_rec;
-fail:
-       TALLOC_FREE(br_lock);
-       TALLOC_FREE(fsp->brlock_rec);
-       return NULL;
 }
 
 struct brl_revalidate_state {
@@ -1987,8 +1993,8 @@ static void brl_revalidate_collect(struct file_id id, struct server_id pid,
 
 static int compare_procids(const void *p1, const void *p2)
 {
-       const struct server_id *i1 = (struct server_id *)p1;
-       const struct server_id *i2 = (struct server_id *)p2;
+       const struct server_id *i1 = (const struct server_id *)p1;
+       const struct server_id *i2 = (const struct server_id *)p2;
 
        if (i1->pid < i2->pid) return -1;
        if (i2->pid > i2->pid) return 1;