size of the record */
struct lock_struct {
struct lock_context context;
+ uint16_t fnum;
uint64_t start;
uint64_t size;
- uint16_t fnum;
enum brl_type lock_type;
void *notify_ptr;
};
+/* this struct is attached to on oprn file handle */
+struct brl_handle {
+ DATA_BLOB key;
+ uint16_t fnum;
+ struct lock_struct last_lock;
+};
+
+/* this struct is typicaly attached to tcon */
struct brl_context {
struct tdb_wrap *w;
uint32_t server;
struct messaging_context *messaging_ctx;
- struct lock_struct last_lock;
};
-
/*
Open up the brlock.tdb database. Close it down using
talloc_free(). We need the messaging_ctx to allow for
}
path = smbd_tmp_path(brl, "brlock.tdb");
- brl->w = tdb_wrap_open(brl, path, 0,
+ brl->w = tdb_wrap_open(brl, path, 0,
TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
talloc_free(path);
if (brl->w == NULL) {
brl->server = server;
brl->messaging_ctx = messaging_ctx;
- ZERO_STRUCT(brl->last_lock);
return brl;
}
+struct brl_handle *brl_create_handle(TALLOC_CTX *mem_ctx, DATA_BLOB *file_key, uint16_t fnum)
+{
+ struct brl_handle *brlh;
+
+ brlh = talloc(mem_ctx, struct brl_handle);
+ if (brlh == NULL) {
+ return NULL;
+ }
+
+ brlh->key = *file_key;
+ brlh->fnum = fnum;
+ ZERO_STRUCT(brlh->last_lock);
+
+ return brlh;
+}
/*
see if two locking contexts are equal
is the same as this one and changes its error code. I wonder if any
app depends on this?
*/
-static NTSTATUS brl_lock_failed(struct brl_context *brl, struct lock_struct *lock)
+static NTSTATUS brl_lock_failed(struct brl_handle *brlh, struct lock_struct *lock)
{
- if (lock->context.server == brl->last_lock.context.server &&
- lock->context.ctx == brl->last_lock.context.ctx &&
- lock->fnum == brl->last_lock.fnum &&
- lock->start == brl->last_lock.start &&
- lock->size == brl->last_lock.size) {
+ /*
+ * this function is only called for non pending lock!
+ */
+
+ /*
+ * if the notify_ptr is non NULL,
+ * it means that we're at the end of a pending lock
+ * and the real lock is requested after the timout went by
+ * In this case we need to remember the last_lock and always
+ * give FILE_LOCK_CONFLICT
+ */
+ if (lock->notify_ptr) {
+ brlh->last_lock = *lock;
return NT_STATUS_FILE_LOCK_CONFLICT;
}
- brl->last_lock = *lock;
- if (lock->start >= 0xEF000000 &&
- (lock->start >> 63) == 0) {
- /* amazing the little things you learn with a test
- suite. Locks beyond this offset (as a 64 bit
- number!) always generate the conflict error code,
- unless the top bit is set */
+
+ /*
+ * amazing the little things you learn with a test
+ * suite. Locks beyond this offset (as a 64 bit
+ * number!) always generate the conflict error code,
+ * unless the top bit is set
+ */
+ if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
+ brlh->last_lock = *lock;
return NT_STATUS_FILE_LOCK_CONFLICT;
}
+
+ /*
+ * if the current lock matches the last failed lock on the file handle
+ * and starts at the same offset, then FILE_LOCK_CONFLICT should be returned
+ */
+ if (lock->context.server == brlh->last_lock.context.server &&
+ lock->context.ctx == brlh->last_lock.context.ctx &&
+ lock->fnum == brlh->last_lock.fnum &&
+ lock->start == brlh->last_lock.start) {
+ return NT_STATUS_FILE_LOCK_CONFLICT;
+ }
+
+ brlh->last_lock = *lock;
return NT_STATUS_LOCK_NOT_GRANTED;
}
notification is sent, identified by the notify_ptr
*/
NTSTATUS brl_lock(struct brl_context *brl,
- DATA_BLOB *file_key,
+ struct brl_handle *brlh,
uint16_t smbpid,
- uint16_t fnum,
uint64_t start, uint64_t size,
enum brl_type lock_type,
void *notify_ptr)
struct lock_struct lock, *locks=NULL;
NTSTATUS status;
- kbuf.dptr = file_key->data;
- kbuf.dsize = file_key->length;
+ kbuf.dptr = brlh->key.data;
+ kbuf.dsize = brlh->key.length;
if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
return NT_STATUS_INTERNAL_DB_CORRUPTION;
preventing the real lock gets removed */
if (lock_type >= PENDING_READ_LOCK) {
enum brl_type rw = (lock_type==PENDING_READ_LOCK? READ_LOCK : WRITE_LOCK);
- status = brl_lock(brl, file_key, smbpid, fnum, start, size, rw, NULL);
+
+ /* here we need to force that the last_lock isn't overwritten */
+ lock = brlh->last_lock;
+ status = brl_lock(brl, brlh, smbpid, start, size, rw, NULL);
+ brlh->last_lock = lock;
+
if (NT_STATUS_IS_OK(status)) {
tdb_chainunlock(brl->w->tdb, kbuf);
return NT_STATUS_OK;
lock.context.smbpid = smbpid;
lock.context.server = brl->server;
lock.context.ctx = brl;
+ lock.fnum = brlh->fnum;
+ lock.context.ctx = brl;
lock.start = start;
lock.size = size;
- lock.fnum = fnum;
lock.lock_type = lock_type;
lock.notify_ptr = notify_ptr;
count = dbuf.dsize / sizeof(*locks);
for (i=0; i<count; i++) {
if (brl_conflict(&locks[i], &lock)) {
- status = brl_lock_failed(brl, &lock);
+ status = brl_lock_failed(brlh, &lock);
goto fail;
}
}
we have reached here then it must be a pending lock that
was granted, so tell them the lock failed */
if (lock_type >= PENDING_READ_LOCK) {
- return brl_lock_failed(brl, &lock);
+ return NT_STATUS_LOCK_NOT_GRANTED;
}
return NT_STATUS_OK;
Unlock a range of bytes.
*/
NTSTATUS brl_unlock(struct brl_context *brl,
- DATA_BLOB *file_key,
+ struct brl_handle *brlh,
uint16_t smbpid,
- uint16_t fnum,
uint64_t start, uint64_t size)
{
TDB_DATA kbuf, dbuf;
struct lock_context context;
NTSTATUS status;
- kbuf.dptr = file_key->data;
- kbuf.dsize = file_key->length;
+ kbuf.dptr = brlh->key.data;
+ kbuf.dsize = brlh->key.length;
if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
return NT_STATUS_INTERNAL_DB_CORRUPTION;
struct lock_struct *lock = &locks[i];
if (brl_same_context(&lock->context, &context) &&
- lock->fnum == fnum &&
+ lock->fnum == brlh->fnum &&
lock->start == start &&
lock->size == size &&
lock->lock_type < PENDING_READ_LOCK) {
getting it. In either case they no longer need to be notified.
*/
NTSTATUS brl_remove_pending(struct brl_context *brl,
- DATA_BLOB *file_key,
+ struct brl_handle *brlh,
void *notify_ptr)
{
TDB_DATA kbuf, dbuf;
struct lock_struct *locks;
NTSTATUS status;
- kbuf.dptr = file_key->data;
- kbuf.dsize = file_key->length;
+ kbuf.dptr = brlh->key.data;
+ kbuf.dsize = brlh->key.length;
if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
return NT_STATUS_INTERNAL_DB_CORRUPTION;
Test if we are allowed to perform IO on a region of an open file
*/
NTSTATUS brl_locktest(struct brl_context *brl,
- DATA_BLOB *file_key,
- uint16_t fnum,
+ struct brl_handle *brlh,
uint16_t smbpid,
uint64_t start, uint64_t size,
enum brl_type lock_type)
int count, i;
struct lock_struct lock, *locks;
- kbuf.dptr = file_key->data;
- kbuf.dsize = file_key->length;
+ kbuf.dptr = brlh->key.data;
+ kbuf.dsize = brlh->key.length;
dbuf = tdb_fetch(brl->w->tdb, kbuf);
if (dbuf.dptr == NULL) {
lock.context.smbpid = smbpid;
lock.context.server = brl->server;
lock.context.ctx = brl;
+ lock.fnum = brlh->fnum;
lock.start = start;
lock.size = size;
- lock.fnum = fnum;
lock.lock_type = lock_type;
/* there are existing locks - make sure they don't conflict */
Remove any locks associated with a open file.
*/
NTSTATUS brl_close(struct brl_context *brl,
- DATA_BLOB *file_key, int fnum)
+ struct brl_handle *brlh)
{
TDB_DATA kbuf, dbuf;
int count, i, dcount=0;
struct lock_struct *locks;
NTSTATUS status;
- kbuf.dptr = file_key->data;
- kbuf.dsize = file_key->length;
+ kbuf.dptr = brlh->key.data;
+ kbuf.dsize = brlh->key.length;
if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
return NT_STATUS_INTERNAL_DB_CORRUPTION;
if (lock->context.ctx == brl &&
lock->context.server == brl->server &&
- lock->fnum == fnum) {
+ lock->fnum == brlh->fnum) {
/* found it - delete it */
if (count > 1 && i < count-1) {
memmove(&locks[i], &locks[i+1],
}
return brl_locktest(pvfs->brl_context,
- &f->handle->brl_locking_key,
- f->fnum,
+ f->brl_handle,
smbpid,
offset, count, rw);
}
/* undo the locks we just did */
for (i=i-1;i>=0;i--) {
brl_unlock(pvfs->brl_context,
- &f->handle->brl_locking_key,
+ f->brl_handle,
locks[i].pid,
- f->fnum,
locks[i].offset,
locks[i].count);
f->lock_count--;
if (reason == PVFS_WAIT_CANCEL) {
status = NT_STATUS_FILE_LOCK_CONFLICT;
} else {
+ /*
+ * here it's important to pass the pending pointer
+ * because with this we'll get the correct error code
+ * FILE_LOCK_CONFLICT in the error case
+ */
status = brl_lock(pvfs->brl_context,
- &f->handle->brl_locking_key,
+ f->brl_handle,
req->smbpid,
- f->fnum,
locks[pending->pending_lock].offset,
locks[pending->pending_lock].count,
- rw, NULL);
+ rw, pending);
}
if (NT_STATUS_IS_OK(status)) {
f->lock_count++;
if (NT_STATUS_IS_OK(status) || timed_out) {
NTSTATUS status2;
status2 = brl_remove_pending(pvfs->brl_context,
- &f->handle->brl_locking_key, pending);
+ f->brl_handle, pending);
if (!NT_STATUS_IS_OK(status2)) {
DEBUG(0,("pvfs_lock: failed to remove pending lock - %s\n", nt_errstr(status2)));
}
}
status = brl_lock(pvfs->brl_context,
- &f->handle->brl_locking_key,
+ f->brl_handle,
req->smbpid,
- f->fnum,
locks[i].offset,
locks[i].count,
rw, pending);
if (f->lock_count || f->pending_list) {
DEBUG(5,("pvfs_lock: removing %.0f locks on close\n",
(double)f->lock_count));
- brl_close(f->pvfs->brl_context, &f->handle->brl_locking_key, f->fnum);
+ brl_close(f->pvfs->brl_context, f->brl_handle);
f->lock_count = 0;
}
for (i=0;i<lck->lockx.in.ulock_cnt;i++) {
status = brl_unlock(pvfs->brl_context,
- &f->handle->brl_locking_key,
+ f->brl_handle,
locks[i].pid,
- f->fnum,
locks[i].offset,
locks[i].count);
if (!NT_STATUS_IS_OK(status)) {
}
status = brl_lock(pvfs->brl_context,
- &f->handle->brl_locking_key,
+ f->brl_handle,
locks[i].pid,
- f->fnum,
locks[i].offset,
locks[i].count,
rw, pending);
/* undo the locks we just did */
for (i=i-1;i>=0;i--) {
brl_unlock(pvfs->brl_context,
- &f->handle->brl_locking_key,
+ f->brl_handle,
locks[i].pid,
- f->fnum,
locks[i].offset,
locks[i].count);
f->lock_count--;
return NT_STATUS_OK;
}
-
f->share_access = io->generic.in.share_access;
f->impersonation = io->generic.in.impersonation;
f->access_mask = access_mask;
+ f->brl_handle = NULL;
f->notify_buffer = NULL;
f->handle->pvfs = pvfs;
f->handle->name = talloc_steal(f->handle, name);
f->handle->fd = -1;
f->handle->odb_locking_key = data_blob(NULL, 0);
- f->handle->brl_locking_key = data_blob(NULL, 0);
f->handle->create_options = io->generic.in.create_options;
f->handle->seek_offset = 0;
f->handle->position = 0;
account of file streams (each stream is a separate byte range
locking space)
*/
-static NTSTATUS pvfs_brl_locking_key(struct pvfs_filename *name,
- TALLOC_CTX *mem_ctx, DATA_BLOB *key)
+static NTSTATUS pvfs_brl_locking_handle(TALLOC_CTX *mem_ctx,
+ struct pvfs_filename *name,
+ uint16_t fnum,
+ struct brl_handle **_h)
{
- DATA_BLOB odb_key;
+ DATA_BLOB odb_key, key;
NTSTATUS status;
+ struct brl_handle *h;
+
status = pvfs_locking_key(name, mem_ctx, &odb_key);
- if (!NT_STATUS_IS_OK(status)) {
- return status;
- }
+ NT_STATUS_NOT_OK_RETURN(status);
+
if (name->stream_name == NULL) {
- *key = odb_key;
- return NT_STATUS_OK;
- }
- *key = data_blob_talloc(mem_ctx, NULL,
- odb_key.length + strlen(name->stream_name) + 1);
- if (key->data == NULL) {
- return NT_STATUS_NO_MEMORY;
+ key = odb_key;
+ } else {
+ key = data_blob_talloc(mem_ctx, NULL,
+ odb_key.length + strlen(name->stream_name) + 1);
+ NT_STATUS_HAVE_NO_MEMORY(key.data);
+ memcpy(key.data, odb_key.data, odb_key.length);
+ memcpy(key.data + odb_key.length,
+ name->stream_name, strlen(name->stream_name) + 1);
+ data_blob_free(&odb_key);
}
- memcpy(key->data, odb_key.data, odb_key.length);
- memcpy(key->data + odb_key.length,
- name->stream_name, strlen(name->stream_name)+1);
- data_blob_free(&odb_key);
+
+ h = brl_create_handle(mem_ctx, &key, fnum);
+ NT_STATUS_HAVE_NO_MEMORY(h);
+
+ *_h = h;
return NT_STATUS_OK;
}
-
/*
create a new file
*/
goto cleanup_delete;
}
- status = pvfs_brl_locking_key(name, f->handle, &f->handle->brl_locking_key);
+ status = pvfs_brl_locking_handle(f, name, fnum, &f->brl_handle);
if (!NT_STATUS_IS_OK(status)) {
goto cleanup_delete;
}
return status;
}
- status = pvfs_brl_locking_key(name, f->handle, &f->handle->brl_locking_key);
+ status = pvfs_brl_locking_handle(f, name, f->fnum, &f->brl_handle);
if (!NT_STATUS_IS_OK(status)) {
idr_remove(pvfs->files.idtree, f->fnum);
return status;