Commit fbad02b6 authored by Rusty Russell's avatar Rusty Russell

tdb2: make tdb1 use same low-level lock functions.

This means they will use the TDB_ATTRIBUTE_FLOCK functions, and get automatic
reopen support.
parent 60210a73
...@@ -184,9 +184,9 @@ static int unlock(struct tdb_context *tdb, int rw, off_t off, off_t len) ...@@ -184,9 +184,9 @@ static int unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
note that a len of zero means lock to end of file note that a len of zero means lock to end of file
*/ */
static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb, enum TDB_ERROR tdb_brlock(struct tdb_context *tdb,
int rw_type, tdb_off_t offset, tdb_off_t len, int rw_type, tdb_off_t offset, tdb_off_t len,
enum tdb_lock_flags flags) enum tdb_lock_flags flags)
{ {
int ret; int ret;
...@@ -226,8 +226,8 @@ static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb, ...@@ -226,8 +226,8 @@ static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb,
return TDB_SUCCESS; return TDB_SUCCESS;
} }
static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb, enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
int rw_type, tdb_off_t offset, size_t len) int rw_type, tdb_off_t offset, size_t len)
{ {
if (tdb->flags & TDB_NOLOCK) { if (tdb->flags & TDB_NOLOCK) {
return TDB_SUCCESS; return TDB_SUCCESS;
...@@ -252,7 +252,7 @@ static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb, ...@@ -252,7 +252,7 @@ static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
deadlock detection and claim a deadlock when progress can be deadlock detection and claim a deadlock when progress can be
made. For those OSes we may loop for a while. made. For those OSes we may loop for a while.
*/ */
enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb) enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb, off_t start)
{ {
int count = 1000; int count = 1000;
...@@ -278,8 +278,7 @@ enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb) ...@@ -278,8 +278,7 @@ enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb)
while (count--) { while (count--) {
struct timeval tv; struct timeval tv;
if (tdb_brlock(tdb, F_WRLCK, if (tdb_brlock(tdb, F_WRLCK, start, 0,
TDB_HASH_LOCK_START, 0,
TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) { TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) {
tdb->file->allrecord_lock.ltype = F_WRLCK; tdb->file->allrecord_lock.ltype = F_WRLCK;
tdb->file->allrecord_lock.off = 0; tdb->file->allrecord_lock.off = 0;
...@@ -341,15 +340,16 @@ enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb) ...@@ -341,15 +340,16 @@ enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb)
} }
/* lock an offset in the database. */ /* lock an offset in the database. */
static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
tdb_off_t offset, int ltype, tdb_off_t offset, int ltype,
enum tdb_lock_flags flags) enum tdb_lock_flags flags)
{ {
struct tdb_lock *new_lck; struct tdb_lock *new_lck;
enum TDB_ERROR ecode; enum TDB_ERROR ecode;
if (offset > (TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE if (!(tdb->flags & TDB_VERSION1)
+ tdb->file->map_size / 8)) { && offset > (TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE
+ tdb->file->map_size / 8)) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_nest_lock: invalid offset %zu ltype=%d", "tdb_nest_lock: invalid offset %zu ltype=%d",
(size_t)offset, ltype); (size_t)offset, ltype);
...@@ -437,8 +437,8 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, ...@@ -437,8 +437,8 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
return TDB_SUCCESS; return TDB_SUCCESS;
} }
static enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb, enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
tdb_off_t off, int ltype) tdb_off_t off, int ltype)
{ {
struct tdb_lock *lck; struct tdb_lock *lck;
enum TDB_ERROR ecode; enum TDB_ERROR ecode;
...@@ -493,9 +493,9 @@ void tdb_transaction_unlock(struct tdb_context *tdb, int ltype) ...@@ -493,9 +493,9 @@ void tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
/* We only need to lock individual bytes, but Linux merges consecutive locks /* We only need to lock individual bytes, but Linux merges consecutive locks
* so we lock in contiguous ranges. */ * so we lock in contiguous ranges. */
static enum TDB_ERROR tdb_lock_gradual(struct tdb_context *tdb, enum TDB_ERROR tdb_lock_gradual(struct tdb_context *tdb,
int ltype, enum tdb_lock_flags flags, int ltype, enum tdb_lock_flags flags,
tdb_off_t off, tdb_off_t len) tdb_off_t off, tdb_off_t len)
{ {
enum TDB_ERROR ecode; enum TDB_ERROR ecode;
enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT); enum tdb_lock_flags nb_flags = (flags & ~TDB_LOCK_WAIT);
......
...@@ -545,7 +545,7 @@ bool tdb_has_hash_locks(struct tdb_context *tdb); ...@@ -545,7 +545,7 @@ bool tdb_has_hash_locks(struct tdb_context *tdb);
enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype, enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
enum tdb_lock_flags flags, bool upgradable); enum tdb_lock_flags flags, bool upgradable);
void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype); void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype);
enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb); enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb, off_t start);
/* Serialize db open. */ /* Serialize db open. */
enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb, enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb,
...@@ -561,6 +561,25 @@ bool tdb_has_expansion_lock(struct tdb_context *tdb); ...@@ -561,6 +561,25 @@ bool tdb_has_expansion_lock(struct tdb_context *tdb);
/* If it needs recovery, grab all the locks and do it. */ /* If it needs recovery, grab all the locks and do it. */
enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb); enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb);
/* Byte-range lock wrappers for TDB1 to access. */
enum TDB_ERROR tdb_brlock(struct tdb_context *tdb,
int rw_type, tdb_off_t offset, tdb_off_t len,
enum tdb_lock_flags flags);
enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
int rw_type, tdb_off_t offset, size_t len);
enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
tdb_off_t offset, int ltype,
enum tdb_lock_flags flags);
enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
tdb_off_t off, int ltype);
enum TDB_ERROR tdb_lock_gradual(struct tdb_context *tdb,
int ltype, enum tdb_lock_flags flags,
tdb_off_t off, tdb_off_t len);
/* Default lock and unlock functions. */ /* Default lock and unlock functions. */
int tdb_fcntl_lock(int fd, int rw, off_t off, off_t len, bool waitflag, void *); int tdb_fcntl_lock(int fd, int rw, off_t off, off_t len, bool waitflag, void *);
int tdb_fcntl_unlock(int fd, int rw, off_t off, off_t len, void *); int tdb_fcntl_unlock(int fd, int rw, off_t off, off_t len, void *);
......
This diff is collapsed.
...@@ -189,6 +189,8 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag ...@@ -189,6 +189,8 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag
tdb->file->map_ptr = NULL; tdb->file->map_ptr = NULL;
tdb->flags = tdb1_flags|TDB_VERSION1; tdb->flags = tdb1_flags|TDB_VERSION1;
tdb->open_flags = open_flags; tdb->open_flags = open_flags;
tdb->lock_fn = tdb_fcntl_lock;
tdb->unlock_fn = tdb_fcntl_unlock;
if (log_ctx) { if (log_ctx) {
tdb->log_fn = log_ctx->log_fn; tdb->log_fn = log_ctx->log_fn;
tdb->log_data = log_ctx->log_private; tdb->log_data = log_ctx->log_private;
......
...@@ -35,6 +35,32 @@ ...@@ -35,6 +35,32 @@
#define tdb_error(tdb) \ #define tdb_error(tdb) \
tdb_error((struct tdb_context *)(tdb)) tdb_error((struct tdb_context *)(tdb))
#define tdb_brlock(tdb1, rw_type, offset, len, flags) \
tdb_brlock((struct tdb_context *)(tdb1), \
(rw_type), (offset), (len), (flags))
#define tdb_brunlock(tdb1, rw_type, offset, len) \
tdb_brunlock((struct tdb_context *)(tdb1), (rw_type), (offset), (len))
#define tdb_nest_lock(tdb1, offset, ltype, flags) \
tdb_nest_lock((struct tdb_context *)(tdb1), (offset), (ltype), (flags))
#define tdb_nest_unlock(tdb1, offset, ltype) \
tdb_nest_unlock((struct tdb_context *)(tdb1), (offset), (ltype))
#define tdb_allrecord_lock(tdb1, offset, flags, upgradable) \
tdb_allrecord_lock((struct tdb_context *)(tdb1), \
(offset), (flags), (upgradable))
#define tdb_allrecord_unlock(tdb1, ltype) \
tdb_allrecord_unlock((struct tdb_context *)(tdb1), (ltype))
#define tdb_allrecord_upgrade(tdb1, start) \
tdb_allrecord_upgrade((struct tdb_context *)(tdb1), (start))
#define tdb_lock_gradual(tdb1, ltype, flags, off, len) \
tdb_lock_gradual((struct tdb_context *)(tdb1), \
(ltype), (flags), (off), (len))
/***** END FIXME ***/ /***** END FIXME ***/
#include <limits.h> #include <limits.h>
...@@ -171,14 +197,24 @@ struct tdb1_context { ...@@ -171,14 +197,24 @@ struct tdb1_context {
/* The actual file information */ /* The actual file information */
struct tdb_file *file; struct tdb_file *file;
int read_only; /* opened read-only */ int open_flags; /* flags used in the open - needed by reopen */
/* low level (fnctl) lock functions. */
int (*lock_fn)(int fd, int rw, off_t off, off_t len, bool w, void *);
int (*unlock_fn)(int fd, int rw, off_t off, off_t len, void *);
void *lock_data;
uint32_t flags; /* the flags passed to tdb1_open */
/* Our statistics. */
struct tdb_attribute_stats stats;
bool read_only; /* opened read-only */
int traverse_read; /* read-only traversal */ int traverse_read; /* read-only traversal */
int traverse_write; /* read-write traversal */ int traverse_write; /* read-write traversal */
struct tdb1_header header; /* a cached copy of the header */ struct tdb1_header header; /* a cached copy of the header */
uint32_t flags; /* the flags passed to tdb1_open */
struct tdb1_traverse_lock travlocks; /* current traversal locks */ struct tdb1_traverse_lock travlocks; /* current traversal locks */
unsigned int (*hash_fn)(TDB_DATA *key); unsigned int (*hash_fn)(TDB_DATA *key);
int open_flags; /* flags used in the open - needed by reopen */
const struct tdb1_methods *methods; const struct tdb1_methods *methods;
struct tdb1_transaction *transaction; struct tdb1_transaction *transaction;
int page_size; int page_size;
......
...@@ -1006,7 +1006,7 @@ static enum TDB_ERROR _tdb_transaction_prepare_commit(struct tdb_context *tdb) ...@@ -1006,7 +1006,7 @@ static enum TDB_ERROR _tdb_transaction_prepare_commit(struct tdb_context *tdb)
methods = tdb->transaction->io_methods; methods = tdb->transaction->io_methods;
/* upgrade the main transaction lock region to a write lock */ /* upgrade the main transaction lock region to a write lock */
ecode = tdb_allrecord_upgrade(tdb); ecode = tdb_allrecord_upgrade(tdb, TDB_HASH_LOCK_START);
if (ecode != TDB_SUCCESS) { if (ecode != TDB_SUCCESS) {
return ecode; return ecode;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment