struct files_struct *fsp;
unsigned int num_locks;
bool modified;
- bool have_read_oplocks;
+ uint32_t num_read_oplocks;
struct lock_struct *lock_data;
struct db_record *record;
};
return brl->fsp;
}
-bool brl_have_read_oplocks(const struct byte_range_lock *brl)
+uint32_t brl_num_read_oplocks(const struct byte_range_lock *brl)
{
- return brl->have_read_oplocks;
+ return brl->num_read_oplocks;
}
-void brl_set_have_read_oplocks(struct byte_range_lock *brl,
- bool have_read_oplocks)
+void brl_set_num_read_oplocks(struct byte_range_lock *brl,
+ uint32_t num_read_oplocks)
{
- DEBUG(10, ("Setting have_read_oplocks to %s\n",
- have_read_oplocks ? "true" : "false"));
+ DEBUG(10, ("Setting num_read_oplocks to %"PRIu32"\n",
+ num_read_oplocks));
SMB_ASSERT(brl->record != NULL); /* otherwise we're readonly */
- brl->have_read_oplocks = have_read_oplocks;
+ brl->num_read_oplocks = num_read_oplocks;
brl->modified = true;
}
This is never used in the POSIX lock case.
****************************************************************************/
-static bool brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
+static bool brl_conflict_other(const struct lock_struct *lock,
+ const struct lock_struct *rw_probe)
{
- if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
+ if (IS_PENDING_LOCK(lock->lock_type) ||
+ IS_PENDING_LOCK(rw_probe->lock_type)) {
return False;
+ }
- if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
+ if (lock->lock_type == READ_LOCK && rw_probe->lock_type == READ_LOCK) {
return False;
+ }
- /* POSIX flavour locks never conflict here - this is only called
- in the read/write path. */
-
- if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
+ if (lock->lock_flav == POSIX_LOCK &&
+ rw_probe->lock_flav == POSIX_LOCK) {
+ /*
+ * POSIX flavour locks never conflict here - this is only called
+ * in the read/write path.
+ */
return False;
+ }
- /*
- * Incoming WRITE locks conflict with existing READ locks even
- * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
- */
+ if (!brl_overlap(lock, rw_probe)) {
+ /*
+ * I/O can only conflict when overlapping a lock, thus let it
+ * pass
+ */
+ return false;
+ }
- if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
- if (brl_same_context(&lck1->context, &lck2->context) &&
- lck1->fnum == lck2->fnum)
- return False;
+ if (!brl_same_context(&lock->context, &rw_probe->context)) {
+ /*
+ * Different process, conflict
+ */
+ return true;
}
- return brl_overlap(lck1, lck2);
+ if (lock->fnum != rw_probe->fnum) {
+ /*
+ * Different file handle, conflict
+ */
+ return true;
+ }
+
+ if ((lock->lock_type == READ_LOCK) &&
+ (rw_probe->lock_type == WRITE_LOCK)) {
+ /*
+ * Incoming WRITE locks conflict with existing READ locks even
+ * if the context is the same. JRA. See LOCKTEST7 in
+ * smbtorture.
+ */
+ return true;
+ }
+
+ /*
+ * I/O request compatible with existing lock, let it pass without
+ * conflict
+ */
+
+ return false;
}
/****************************************************************************
void brl_init(bool read_only)
{
int tdb_flags;
+ char *db_path;
if (brlock_db) {
return;
tdb_flags |= TDB_SEQNUM;
}
- brlock_db = db_open(NULL, lock_path("brlock.tdb"),
+ db_path = lock_path("brlock.tdb");
+ if (db_path == NULL) {
+ DEBUG(0, ("out of memory!\n"));
+ return;
+ }
+
+ brlock_db = db_open(NULL, db_path,
SMB_OPEN_DATABASE_TDB_HASH_SIZE, tdb_flags,
read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644,
DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
if (!brlock_db) {
DEBUG(0,("Failed to open byte range locking database %s\n",
- lock_path("brlock.tdb")));
+ db_path));
+ TALLOC_FREE(db_path);
return;
}
+ TALLOC_FREE(db_path);
}
/****************************************************************************
for (i=0; i < br_lck->num_locks; i++) {
/* Do any Windows or POSIX locks conflict ? */
if (brl_conflict(&locks[i], plock)) {
+ if (!serverid_exists(&locks[i].context.pid)) {
+ locks[i].context.pid.pid = 0;
+ br_lck->modified = true;
+ continue;
+ }
/* Remember who blocked us. */
plock->context.smblctx = locks[i].context.smblctx;
return brl_lock_failed(fsp,plock,blocking_lock);
if (curr_lock->lock_flav == WINDOWS_LOCK) {
/* Do any Windows flavour locks conflict ? */
if (brl_conflict(curr_lock, plock)) {
+ if (!serverid_exists(&curr_lock->context.pid)) {
+ curr_lock->context.pid.pid = 0;
+ br_lck->modified = true;
+ continue;
+ }
/* No games with error messages. */
TALLOC_FREE(tp);
/* Remember who blocked us. */
/* POSIX conflict semantics are different. */
if (brl_conflict_posix(curr_lock, plock)) {
+ if (!serverid_exists(&curr_lock->context.pid)) {
+ curr_lock->context.pid.pid = 0;
+ br_lck->modified = true;
+ continue;
+ }
/* Can't block ourselves with POSIX locks. */
/* No games with error messages. */
TALLOC_FREE(tp);
}
#endif
-#ifdef DEVELOPER
- /* Quieten valgrind on test. */
- ZERO_STRUCT(lock);
-#endif
-
- lock.context.smblctx = smblctx;
- lock.context.pid = pid;
- lock.context.tid = br_lck->fsp->conn->cnum;
- lock.start = start;
- lock.size = size;
- lock.fnum = br_lck->fsp->fnum;
- lock.lock_type = lock_type;
- lock.lock_flav = lock_flav;
+ lock = (struct lock_struct) {
+ .context.smblctx = smblctx,
+ .context.pid = pid,
+ .context.tid = br_lck->fsp->conn->cnum,
+ .start = start,
+ .size = size,
+ .fnum = br_lck->fsp->fnum,
+ .lock_type = lock_type,
+ .lock_flav = lock_flav
+ };
if (lock_flav == WINDOWS_LOCK) {
ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
****************************************************************************/
bool brl_locktest(struct byte_range_lock *br_lck,
- uint64_t smblctx,
- struct server_id pid,
- br_off start,
- br_off size,
- enum brl_type lock_type,
- enum brl_flavour lock_flav)
+ const struct lock_struct *rw_probe)
{
bool ret = True;
unsigned int i;
- struct lock_struct lock;
- const struct lock_struct *locks = br_lck->lock_data;
+ struct lock_struct *locks = br_lck->lock_data;
files_struct *fsp = br_lck->fsp;
- lock.context.smblctx = smblctx;
- lock.context.pid = pid;
- lock.context.tid = br_lck->fsp->conn->cnum;
- lock.start = start;
- lock.size = size;
- lock.fnum = fsp->fnum;
- lock.lock_type = lock_type;
- lock.lock_flav = lock_flav;
-
/* Make sure existing locks don't conflict */
for (i=0; i < br_lck->num_locks; i++) {
/*
* Our own locks don't conflict.
*/
- if (brl_conflict_other(&locks[i], &lock)) {
+ if (brl_conflict_other(&locks[i], rw_probe)) {
+ if (br_lck->record == NULL) {
+ /* readonly */
+ return false;
+ }
+
+ if (!serverid_exists(&locks[i].context.pid)) {
+ locks[i].context.pid.pid = 0;
+ br_lck->modified = true;
+ continue;
+ }
+
return False;
}
}
* This only conflicts with Windows locks, not POSIX locks.
*/
- if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
+ if(lp_posix_locking(fsp->conn->params) &&
+ (rw_probe->lock_flav == WINDOWS_LOCK)) {
+ /*
+ * Make copies -- is_posix_locked might modify the values
+ */
+
+ br_off start = rw_probe->start;
+ br_off size = rw_probe->size;
+ enum brl_type lock_type = rw_probe->lock_type;
+
ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
DEBUG(10, ("brl_locktest: posix start=%ju len=%ju %s for %s "
* 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 true;
}
-/****************************************************************************
- Ensure this set of lock entries is valid.
-****************************************************************************/
-static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks,
- bool keep_disconnected)
-{
- unsigned int i;
- struct lock_struct *locks = *pplocks;
- unsigned int num_entries = *pnum_entries;
- TALLOC_CTX *frame;
- struct server_id *ids;
- bool *exists;
-
- if (num_entries == 0) {
- return true;
- }
-
- frame = talloc_stackframe();
-
- ids = talloc_array(frame, struct server_id, num_entries);
- if (ids == NULL) {
- DEBUG(0, ("validate_lock_entries: "
- "talloc_array(struct server_id, %u) failed\n",
- num_entries));
- talloc_free(frame);
- return false;
- }
-
- exists = talloc_array(frame, bool, num_entries);
- if (exists == NULL) {
- DEBUG(0, ("validate_lock_entries: "
- "talloc_array(bool, %u) failed\n",
- num_entries));
- talloc_free(frame);
- return false;
- }
-
- for (i = 0; i < num_entries; i++) {
- ids[i] = locks[i].context.pid;
- }
-
- if (!serverids_exist(ids, num_entries, exists)) {
- DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
- talloc_free(frame);
- return false;
- }
-
- i = 0;
-
- while (i < num_entries) {
- if (exists[i]) {
- i++;
- continue;
- }
-
- if (keep_disconnected &&
- server_id_is_disconnected(&ids[i]))
- {
- i++;
- continue;
- }
-
- /* This process no longer exists */
-
- brl_delete_lock_struct(locks, num_entries, i);
- num_entries -= 1;
- }
- TALLOC_FREE(frame);
-
- *pnum_entries = num_entries;
-
- return True;
-}
-
struct brl_forall_cb {
void (*fn)(struct file_id id, struct server_id pid,
enum brl_type lock_type,
struct file_id *key;
unsigned int i;
unsigned int num_locks = 0;
- unsigned int orig_num_locks = 0;
TDB_DATA dbkey;
TDB_DATA value;
}
key = (struct file_id *)dbkey.dptr;
- orig_num_locks = num_locks = value.dsize/sizeof(*locks);
-
- /* Ensure the lock db is clean of entries from invalid processes. */
-
- if (!validate_lock_entries(&num_locks, &locks, true)) {
- TALLOC_FREE(locks);
- return -1; /* Terminate traversal */
- }
-
- if (orig_num_locks != num_locks) {
- if (num_locks) {
- TDB_DATA data;
- data.dptr = (uint8_t *)locks;
- data.dsize = num_locks*sizeof(struct lock_struct);
- dbwrap_record_store(rec, data, TDB_REPLACE);
- } else {
- dbwrap_record_delete(rec);
- }
- }
+ num_locks = value.dsize/sizeof(*locks);
if (cb->fn) {
for ( i=0; i<num_locks; i++) {
static void byte_range_lock_flush(struct byte_range_lock *br_lck)
{
- size_t data_len;
+ unsigned i;
+ struct lock_struct *locks = br_lck->lock_data;
+
if (!br_lck->modified) {
DEBUG(10, ("br_lck not modified\n"));
goto done;
}
- data_len = br_lck->num_locks * sizeof(struct lock_struct);
+ i = 0;
- if (br_lck->have_read_oplocks) {
- data_len += 1;
+ while (i < br_lck->num_locks) {
+ if (locks[i].context.pid.pid == 0) {
+ /*
+ * Autocleanup, the process conflicted and does not
+ * exist anymore.
+ */
+ locks[i] = locks[br_lck->num_locks-1];
+ br_lck->num_locks -= 1;
+ } else {
+ i += 1;
+ }
}
- DEBUG(10, ("data_len=%d\n", (int)data_len));
-
- if (data_len == 0) {
+ if ((br_lck->num_locks == 0) && (br_lck->num_read_oplocks == 0)) {
/* No locks - delete this entry. */
NTSTATUS status = dbwrap_record_delete(br_lck->record);
if (!NT_STATUS_IS_OK(status)) {
smb_panic("Could not delete byte range lock entry");
}
} else {
+ size_t lock_len, data_len;
TDB_DATA data;
NTSTATUS status;
+ lock_len = br_lck->num_locks * sizeof(struct lock_struct);
+ data_len = lock_len + sizeof(br_lck->num_read_oplocks);
+
data.dsize = data_len;
data.dptr = talloc_array(talloc_tos(), uint8_t, data_len);
SMB_ASSERT(data.dptr != NULL);
- memcpy(data.dptr, br_lck->lock_data,
- br_lck->num_locks * sizeof(struct lock_struct));
-
- if (br_lck->have_read_oplocks) {
- data.dptr[data_len-1] = 1;
- }
+ memcpy(data.dptr, br_lck->lock_data, lock_len);
+ memcpy(data.dptr + lock_len, &br_lck->num_read_oplocks,
+ sizeof(br_lck->num_read_oplocks));
status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
TALLOC_FREE(data.dptr);
return 0;
}
+static bool brl_parse_data(struct byte_range_lock *br_lck, TDB_DATA data)
+{
+ size_t data_len;
+
+ if (data.dsize == 0) {
+ return true;
+ }
+ if (data.dsize % sizeof(struct lock_struct) !=
+ sizeof(br_lck->num_read_oplocks)) {
+ DEBUG(1, ("Invalid data size: %u\n", (unsigned)data.dsize));
+ return false;
+ }
+
+ br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
+ data_len = br_lck->num_locks * sizeof(struct lock_struct);
+
+ br_lck->lock_data = talloc_memdup(br_lck, data.dptr, data_len);
+ if (br_lck->lock_data == NULL) {
+ DEBUG(1, ("talloc_memdup failed\n"));
+ return false;
+ }
+ memcpy(&br_lck->num_read_oplocks, data.dptr + data_len,
+ sizeof(br_lck->num_read_oplocks));
+ return true;
+}
+
/*******************************************************************
Fetch a set of byte range lock data from the database.
Leave the record locked.
struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx, files_struct *fsp)
{
TDB_DATA key, data;
- struct byte_range_lock *br_lck = talloc(mem_ctx, struct byte_range_lock);
+ struct byte_range_lock *br_lck;
+ br_lck = talloc_zero(mem_ctx, struct byte_range_lock);
if (br_lck == NULL) {
return NULL;
}
br_lck->fsp = fsp;
- br_lck->num_locks = 0;
- br_lck->have_read_oplocks = false;
- br_lck->modified = False;
key.dptr = (uint8 *)&fsp->file_id;
key.dsize = sizeof(struct file_id);
data = dbwrap_record_get_value(br_lck->record);
- br_lck->lock_data = NULL;
-
- talloc_set_destructor(br_lck, byte_range_lock_destructor);
-
- br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
-
- if (br_lck->num_locks != 0) {
- br_lck->lock_data = talloc_array(
- br_lck, struct lock_struct, br_lck->num_locks);
- if (br_lck->lock_data == NULL) {
- DEBUG(0, ("malloc failed\n"));
- TALLOC_FREE(br_lck);
- return NULL;
- }
-
- memcpy(br_lck->lock_data, data.dptr,
- talloc_get_size(br_lck->lock_data));
- }
-
- DEBUG(10, ("data.dsize=%d\n", (int)data.dsize));
-
- if ((data.dsize % sizeof(struct lock_struct)) == 1) {
- br_lck->have_read_oplocks = (data.dptr[data.dsize-1] == 1);
+ if (!brl_parse_data(br_lck, data)) {
+ TALLOC_FREE(br_lck);
+ return NULL;
}
- if (!fsp->lockdb_clean) {
- int orig_num_locks = br_lck->num_locks;
-
- /*
- * 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, false)) {
- TALLOC_FREE(br_lck);
- return NULL;
- }
-
- /* Ensure invalid locks are cleaned up in the destructor. */
- if (orig_num_locks != br_lck->num_locks) {
- br_lck->modified = True;
- }
-
- /* Mark the lockdb as "clean" as seen from this open file. */
- fsp->lockdb_clean = True;
- }
+ talloc_set_destructor(br_lck, byte_range_lock_destructor);
if (DEBUGLEVEL >= 10) {
unsigned int i;
{
struct brl_get_locks_readonly_state *state =
(struct brl_get_locks_readonly_state *)private_data;
- struct byte_range_lock *br_lock;
+ struct byte_range_lock *br_lck;
- br_lock = talloc_pooled_object(
+ br_lck = talloc_pooled_object(
state->mem_ctx, struct byte_range_lock, 1, data.dsize);
- if (br_lock == NULL) {
+ if (br_lck == NULL) {
*state->br_lock = NULL;
return;
}
- br_lock->lock_data = (struct lock_struct *)talloc_memdup(
- br_lock, data.dptr, data.dsize);
- br_lock->num_locks = data.dsize / sizeof(struct lock_struct);
-
- if ((data.dsize % sizeof(struct lock_struct)) == 1) {
- br_lock->have_read_oplocks = (data.dptr[data.dsize-1] == 1);
- } else {
- br_lock->have_read_oplocks = false;
+ if (!brl_parse_data(br_lck, data)) {
+ *state->br_lock = NULL;
+ return;
}
-
- DEBUG(10, ("Got %d bytes, have_read_oplocks: %s\n", (int)data.dsize,
- br_lock->have_read_oplocks ? "true" : "false"));
-
- *state->br_lock = br_lock;
+ *state->br_lock = br_lck;
}
struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
{
struct byte_range_lock *br_lock = NULL;
- struct byte_range_lock *rw = NULL;
+ struct brl_get_locks_readonly_state state;
+ NTSTATUS status;
DEBUG(10, ("seqnum=%d, fsp->brlock_seqnum=%d\n",
dbwrap_get_seqnum(brlock_db), fsp->brlock_seqnum));
return fsp->brlock_rec;
}
- if (!fsp->lockdb_clean) {
- /*
- * Fetch the record in R/W mode to give validate_lock_entries
- * a chance to kick in once.
- */
- rw = brl_get_locks(talloc_tos(), fsp);
- if (rw == NULL) {
- return NULL;
- }
- fsp->lockdb_clean = true;
- }
+ /*
+ * Parse the record fresh from the database
+ */
- if (rw != NULL) {
- size_t lock_data_size;
+ state.mem_ctx = fsp;
+ state.br_lock = &br_lock;
+ status = dbwrap_parse_record(
+ brlock_db,
+ make_tdb_data((uint8_t *)&fsp->file_id,
+ sizeof(fsp->file_id)),
+ brl_get_locks_readonly_parser, &state);
+
+ if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_FOUND)) {
/*
- * Make a copy of the already retrieved and sanitized rw record
+ * No locks on this file. Return an empty br_lock.
*/
- lock_data_size = rw->num_locks * sizeof(struct lock_struct);
- br_lock = talloc_pooled_object(
- fsp, struct byte_range_lock, 1, lock_data_size);
+ br_lock = talloc(fsp, struct byte_range_lock);
if (br_lock == NULL) {
- goto fail;
+ return NULL;
}
- br_lock->have_read_oplocks = rw->have_read_oplocks;
- br_lock->num_locks = rw->num_locks;
- br_lock->lock_data = (struct lock_struct *)talloc_memdup(
- br_lock, rw->lock_data, lock_data_size);
- } else {
- struct brl_get_locks_readonly_state state;
- NTSTATUS status;
-
- /*
- * Parse the record fresh from the database
- */
-
- state.mem_ctx = fsp;
- state.br_lock = &br_lock;
-
- status = dbwrap_parse_record(
- brlock_db,
- make_tdb_data((uint8_t *)&fsp->file_id,
- sizeof(fsp->file_id)),
- brl_get_locks_readonly_parser, &state);
-
- if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_FOUND)) {
- /*
- * No locks on this file. Return an empty br_lock.
- */
- br_lock = talloc(fsp, struct byte_range_lock);
- if (br_lock == NULL) {
- goto fail;
- }
- br_lock->have_read_oplocks = false;
- br_lock->num_locks = 0;
- br_lock->lock_data = NULL;
+ br_lock->num_read_oplocks = 0;
+ br_lock->num_locks = 0;
+ br_lock->lock_data = NULL;
- } else if (!NT_STATUS_IS_OK(status)) {
- DEBUG(3, ("Could not parse byte range lock record: "
- "%s\n", nt_errstr(status)));
- goto fail;
- }
- if (br_lock == NULL) {
- goto fail;
- }
+ } else if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(3, ("Could not parse byte range lock record: "
+ "%s\n", nt_errstr(status)));
+ return NULL;
+ }
+ if (br_lock == NULL) {
+ return NULL;
}
br_lock->fsp = fsp;
fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
}
-fail:
- TALLOC_FREE(rw);
return br_lock;
}