[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v3 4/4] xenstore: cleanup tdb.c



Remove all unused functions from tdb.c. This will reduce code size of
xenstored and - more important - of xenstore stubdom.

tdb.c hasn't been updated to a newer version since its introduction in
2005. Any backport of bug fixes or update to a new version will need
major work, so there is no real downside to remove not needed code.

Signed-off-by: Juergen Gross <jgross@xxxxxxxx>
Acked-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
Acked-by: Wei Liu <wei.liu2@xxxxxxxxxx>
---
 tools/xenstore/tdb.c | 439 +--------------------------------------------------
 tools/xenstore/tdb.h |  22 ---
 2 files changed, 1 insertion(+), 460 deletions(-)

diff --git a/tools/xenstore/tdb.c b/tools/xenstore/tdb.c
index e26900e..29593b7 100644
--- a/tools/xenstore/tdb.c
+++ b/tools/xenstore/tdb.c
@@ -103,7 +103,7 @@
 #endif
 
 #define BUCKET(hash) ((hash) % tdb->header.hash_size)
-TDB_DATA tdb_null;
+static TDB_DATA tdb_null;
 
 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
 static TDB_CONTEXT *tdbs = NULL;
@@ -510,108 +510,6 @@ static int update_tailer(TDB_CONTEXT *tdb, tdb_off offset,
                         &totalsize);
 }
 
-static tdb_off tdb_dump_record(TDB_CONTEXT *tdb, tdb_off offset)
-{
-       struct list_struct rec;
-       tdb_off tailer_ofs, tailer;
-
-       if (tdb_read(tdb, offset, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
-               printf("ERROR: failed to read record at %u\n", offset);
-               return 0;
-       }
-
-       printf(" rec: offset=0x%08x next=0x%08x rec_len=%d key_len=%d 
data_len=%d full_hash=0x%x magic=0x%x\n",
-              offset, rec.next, rec.rec_len, rec.key_len, rec.data_len, 
rec.full_hash, rec.magic);
-
-       tailer_ofs = offset + sizeof(rec) + rec.rec_len - sizeof(tdb_off);
-       if (ofs_read(tdb, tailer_ofs, &tailer) == -1) {
-               printf("ERROR: failed to read tailer at %u\n", tailer_ofs);
-               return rec.next;
-       }
-
-       if (tailer != rec.rec_len + sizeof(rec)) {
-               printf("ERROR: tailer does not match record! tailer=%u 
totalsize=%u\n",
-                               (unsigned int)tailer, (unsigned 
int)(rec.rec_len + sizeof(rec)));
-       }
-       return rec.next;
-}
-
-static int tdb_dump_chain(TDB_CONTEXT *tdb, int i)
-{
-       tdb_off rec_ptr, top;
-
-       top = TDB_HASH_TOP(i);
-
-       if (tdb_lock(tdb, i, F_WRLCK) != 0)
-               return -1;
-
-       if (ofs_read(tdb, top, &rec_ptr) == -1)
-               return tdb_unlock(tdb, i, F_WRLCK);
-
-       if (rec_ptr)
-               printf("hash=%d\n", i);
-
-       while (rec_ptr) {
-               rec_ptr = tdb_dump_record(tdb, rec_ptr);
-       }
-
-       return tdb_unlock(tdb, i, F_WRLCK);
-}
-
-void tdb_dump_all(TDB_CONTEXT *tdb)
-{
-       unsigned int i;
-       for (i=0;i<tdb->header.hash_size;i++) {
-               tdb_dump_chain(tdb, i);
-       }
-       printf("freelist:\n");
-       tdb_dump_chain(tdb, -1);
-}
-
-int tdb_printfreelist(TDB_CONTEXT *tdb)
-{
-       int ret;
-       long total_free = 0;
-       tdb_off offset, rec_ptr;
-       struct list_struct rec;
-
-       if ((ret = tdb_lock(tdb, -1, F_WRLCK)) != 0)
-               return ret;
-
-       offset = FREELIST_TOP;
-
-       /* read in the freelist top */
-       if (ofs_read(tdb, offset, &rec_ptr) == -1) {
-               tdb_unlock(tdb, -1, F_WRLCK);
-               return 0;
-       }
-
-       printf("freelist top=[0x%08x]\n", rec_ptr );
-       while (rec_ptr) {
-               if (tdb_read(tdb, rec_ptr, (char *)&rec, sizeof(rec), DOCONV()) 
== -1) {
-                       tdb_unlock(tdb, -1, F_WRLCK);
-                       return -1;
-               }
-
-               if (rec.magic != TDB_FREE_MAGIC) {
-                       printf("bad magic 0x%08x in free list\n", rec.magic);
-                       tdb_unlock(tdb, -1, F_WRLCK);
-                       return -1;
-               }
-
-               printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%d)] (end 
= 0x%08x)\n", 
-                      rec_ptr, rec.rec_len, rec.rec_len, rec_ptr + 
rec.rec_len);
-               total_free += rec.rec_len;
-
-               /* move to the next record */
-               rec_ptr = rec.next;
-       }
-       printf("total rec_len = [0x%08x (%d)]\n", (int)total_free, 
-               (int)total_free);
-
-       return tdb_unlock(tdb, -1, F_WRLCK);
-}
-
 /* Remove an element from the freelist.  Must have alloc lock. */
 static int remove_from_freelist(TDB_CONTEXT *tdb, tdb_off off, tdb_off next)
 {
@@ -1170,12 +1068,6 @@ static int tdb_exists_hash(TDB_CONTEXT *tdb, TDB_DATA 
key, uint32_t hash)
        return 1;
 }
 
-int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key)
-{
-       uint32_t hash = tdb->hash_fn(&key);
-       return tdb_exists_hash(tdb, key, hash);
-}
-
 /* record lock stops delete underneath */
 static int lock_record(TDB_CONTEXT *tdb, tdb_off off)
 {
@@ -1617,131 +1509,6 @@ fail:
        goto out;
 }
 
-/* Attempt to append data to an entry in place - this only works if the new 
data size
-   is <= the old data size and the key exists.
-   on failure return -1. Record must be locked before calling.
-*/
-static int tdb_append_inplace(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash, 
TDB_DATA new_dbuf)
-{
-       struct list_struct rec;
-       tdb_off rec_ptr;
-
-       /* find entry */
-       if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
-               return -1;
-
-       /* Append of 0 is always ok. */
-       if (new_dbuf.dsize == 0)
-               return 0;
-
-       /* must be long enough for key, old data + new data and tailer */
-       if (rec.rec_len < key.dsize + rec.data_len + new_dbuf.dsize + 
sizeof(tdb_off)) {
-               /* No room. */
-               tdb->ecode = TDB_SUCCESS; /* Not really an error */
-               return -1;
-       }
-
-       if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len + rec.data_len,
-                     new_dbuf.dptr, new_dbuf.dsize) == -1)
-               return -1;
-
-       /* update size */
-       rec.data_len += new_dbuf.dsize;
-       return rec_write(tdb, rec_ptr, &rec);
-}
-
-/* Append to an entry. Create if not exist. */
-
-int tdb_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf)
-{
-       struct list_struct rec;
-       uint32_t hash;
-       tdb_off rec_ptr;
-       char *p = NULL;
-       int ret = 0;
-       size_t new_data_size = 0;
-
-       /* find which hash bucket it is in */
-       hash = tdb->hash_fn(&key);
-       if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
-               return -1;
-
-       /* first try in-place. */
-       if (tdb_append_inplace(tdb, key, hash, new_dbuf) == 0)
-               goto out;
-
-       /* reset the error code potentially set by the tdb_append_inplace() */
-       tdb->ecode = TDB_SUCCESS;
-
-       /* find entry */
-       if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
-               if (tdb->ecode != TDB_ERR_NOEXIST)
-                       goto fail;
-
-               /* Not found - create. */
-
-               ret = tdb_store(tdb, key, new_dbuf, TDB_INSERT);
-               goto out;
-       }
-
-       new_data_size = rec.data_len + new_dbuf.dsize;
-
-       /* Copy key+old_value+value *before* allocating free space in case 
malloc
-          fails and we are left with a dead spot in the tdb. */
-
-       if (!(p = (char *)talloc_size(tdb, key.dsize + new_data_size))) {
-               tdb->ecode = TDB_ERR_OOM;
-               goto fail;
-       }
-
-       /* Copy the key in place. */
-       memcpy(p, key.dptr, key.dsize);
-
-       /* Now read the old data into place. */
-       if (rec.data_len &&
-               tdb_read(tdb, rec_ptr + sizeof(rec) + rec.key_len, p + 
key.dsize, rec.data_len, 0) == -1)
-                       goto fail;
-
-       /* Finally append the new data. */
-       if (new_dbuf.dsize)
-               memcpy(p+key.dsize+rec.data_len, new_dbuf.dptr, new_dbuf.dsize);
-
-       /* delete any existing record - if it doesn't exist we don't
-           care.  Doing this first reduces fragmentation, and avoids
-           coalescing with `allocated' block before it's updated. */
-
-       tdb_delete_hash(tdb, key, hash);
-
-       if (!(rec_ptr = tdb_allocate(tdb, key.dsize + new_data_size, &rec)))
-               goto fail;
-
-       /* Read hash top into next ptr */
-       if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
-               goto fail;
-
-       rec.key_len = key.dsize;
-       rec.data_len = new_data_size;
-       rec.full_hash = hash;
-       rec.magic = TDB_MAGIC;
-
-       /* write out and point the top of the hash chain at it */
-       if (rec_write(tdb, rec_ptr, &rec) == -1
-           || tdb_write(tdb, rec_ptr+sizeof(rec), p, 
key.dsize+new_data_size)==-1
-           || ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
-               /* Need to tdb_unallocate() here */
-               goto fail;
-       }
-
- out:
-       SAFE_FREE(p); 
-       tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
-       return ret;
-
-fail:
-       ret = -1;
-       goto out;
-}
-
 static int tdb_already_open(dev_t device,
                            ino_t ino)
 {
@@ -1756,22 +1523,6 @@ static int tdb_already_open(dev_t device,
        return 0;
 }
 
-/* open the database, creating it if necessary 
-
-   The open_flags and mode are passed straight to the open call on the
-   database file. A flags value of O_WRONLY is invalid. The hash size
-   is advisory, use zero for a default value.
-
-   Return is NULL on error, in which case errno is also set.  Don't 
-   try to call tdb_error or tdb_errname, just do strerror(errno).
-
-   @param name may be NULL for internal databases. */
-TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
-                     int open_flags, mode_t mode)
-{
-       return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL, 
NULL);
-}
-
 /* a default logging function */
 static void null_log_fn(TDB_CONTEXT *tdb __attribute__((unused)),
                        int level __attribute__((unused)),
@@ -1995,191 +1746,3 @@ int tdb_close(TDB_CONTEXT *tdb)
 
        return ret;
 }
-
-/* lock/unlock entire database */
-int tdb_lockall(TDB_CONTEXT *tdb)
-{
-       uint32_t i;
-
-       /* There are no locks on read-only dbs */
-       if (tdb->read_only)
-               return TDB_ERRCODE(TDB_ERR_LOCK, -1);
-       for (i = 0; i < tdb->header.hash_size; i++) 
-               if (tdb_lock(tdb, i, F_WRLCK))
-                       break;
-
-       /* If error, release locks we have... */
-       if (i < tdb->header.hash_size) {
-               uint32_t j;
-
-               for ( j = 0; j < i; j++)
-                       tdb_unlock(tdb, j, F_WRLCK);
-               return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
-       }
-
-       return 0;
-}
-void tdb_unlockall(TDB_CONTEXT *tdb)
-{
-       uint32_t i;
-       for (i=0; i < tdb->header.hash_size; i++)
-               tdb_unlock(tdb, i, F_WRLCK);
-}
-
-/* lock/unlock one hash chain. This is meant to be used to reduce
-   contention - it cannot guarantee how many records will be locked */
-int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key)
-{
-       return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
-}
-
-int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key)
-{
-       return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
-}
-
-int tdb_chainlock_read(TDB_CONTEXT *tdb, TDB_DATA key)
-{
-       return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
-}
-
-int tdb_chainunlock_read(TDB_CONTEXT *tdb, TDB_DATA key)
-{
-       return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
-}
-
-
-/* register a loging function */
-void tdb_logging_function(TDB_CONTEXT *tdb, void (*fn)(TDB_CONTEXT *, int , 
const char *, ...))
-{
-       tdb->log_fn = fn?fn:null_log_fn;
-}
-
-
-/* reopen a tdb - this can be used after a fork to ensure that we have an 
independent
-   seek pointer from our parent and to re-establish locks */
-int tdb_reopen(TDB_CONTEXT *tdb)
-{
-       struct stat st;
-
-       if (tdb->flags & TDB_INTERNAL)
-               return 0; /* Nothing to do. */
-       if (tdb_munmap(tdb) != 0) {
-               TDB_LOG((tdb, 0, "tdb_reopen: munmap failed (%s)\n", 
strerror(errno)));
-               goto fail;
-       }
-       if (close(tdb->fd) != 0)
-               TDB_LOG((tdb, 0, "tdb_reopen: WARNING closing tdb->fd 
failed!\n"));
-       tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
-       if (tdb->fd == -1) {
-               TDB_LOG((tdb, 0, "tdb_reopen: open failed (%s)\n", 
strerror(errno)));
-               goto fail;
-       }
-       if (fstat(tdb->fd, &st) != 0) {
-               TDB_LOG((tdb, 0, "tdb_reopen: fstat failed (%s)\n", 
strerror(errno)));
-               goto fail;
-       }
-       if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
-               TDB_LOG((tdb, 0, "tdb_reopen: file dev/inode has changed!\n"));
-               goto fail;
-       }
-       tdb_mmap(tdb);
-       if ((tdb->flags & TDB_CLEAR_IF_FIRST) && (tdb_brlock(tdb, ACTIVE_LOCK, 
F_RDLCK, F_SETLKW, 0) == -1)) {
-               TDB_LOG((tdb, 0, "tdb_reopen: failed to obtain active lock\n"));
-               goto fail;
-       }
-
-       return 0;
-
-fail:
-       tdb_close(tdb);
-       return -1;
-}
-
-/* Not general: only works if single writer. */
-TDB_CONTEXT *tdb_copy(TDB_CONTEXT *tdb, const char *outfile)
-{
-       int fd, saved_errno;
-       TDB_CONTEXT *copy;
-
-       if (tdb->flags & TDB_INTERNAL) {
-               struct tdb_header *copydb;
-               
-               copy = talloc_zero(outfile, TDB_CONTEXT);
-               if (copy == NULL) {
-                       errno = ENOMEM;
-                       goto intfail;
-               }
-               memcpy(copy, tdb, sizeof(TDB_CONTEXT));
-
-               if (copy->name || copy->locked || copy->device || copy->inode) {
-                       fprintf(stderr, "tdb_copy assumption(s) failed\n");
-                       goto intfail;
-               }
-
-               copydb = talloc_zero_size(copy, copy->map_size);
-               if (copydb == NULL) {
-                       errno = ENOMEM;
-                       goto intfail;
-               }
-               memcpy(copydb, copy->map_ptr, copy->map_size);
-               copy->map_ptr = (char*) copydb;
-
-               if (tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0) == -1)
-                       goto intfail;
-
-               copy->next = tdbs;
-               tdbs = copy;
-
-               return copy;
-intfail:
-               talloc_free(copy);
-               return NULL;
-       }
-
-       fd = open(outfile, O_TRUNC|O_CREAT|O_WRONLY, 0640);
-       if (fd < 0)
-               return NULL;
-       if (tdb->map_ptr) {
-               if (write(fd,tdb->map_ptr,tdb->map_size) != (int)tdb->map_size)
-                       goto fail;
-       } else {
-               char buf[65536];
-               int r;
-
-               lseek(tdb->fd, 0, SEEK_SET);
-               while ((r = read(tdb->fd, buf, sizeof(buf))) > 0) {
-                       if (write(fd, buf, r) != r)
-                               goto fail;
-               }
-               if (r < 0)
-                       goto fail;
-       }
-       copy = tdb_open(outfile, 0, 0, O_RDWR, 0);
-       if (!copy)
-               goto fail;
-       close(fd);
-       return copy;
-
-fail:
-       saved_errno = errno;
-       close(fd);
-       unlink(outfile);
-       errno = saved_errno;
-       return NULL;
-}
-
-/* reopen all tdb's */
-int tdb_reopen_all(void)
-{
-       TDB_CONTEXT *tdb;
-
-       for (tdb=tdbs; tdb; tdb = tdb->next) {
-               /* Ensure no clear-if-first. */
-               tdb->flags &= ~TDB_CLEAR_IF_FIRST;
-               if (tdb_reopen(tdb) != 0)
-                       return -1;
-       }
-
-       return 0;
-}
diff --git a/tools/xenstore/tdb.h b/tools/xenstore/tdb.h
index 4187274..557cf72 100644
--- a/tools/xenstore/tdb.h
+++ b/tools/xenstore/tdb.h
@@ -112,42 +112,20 @@ typedef int (*tdb_traverse_func)(TDB_CONTEXT *, TDB_DATA, 
TDB_DATA, void *);
 typedef void (*tdb_log_func)(TDB_CONTEXT *, int , const char *, ...);
 typedef uint32_t (*tdb_hash_func)(TDB_DATA *key);
 
-TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
-                     int open_flags, mode_t mode);
 TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
                         int open_flags, mode_t mode,
                         tdb_log_func log_fn,
                         tdb_hash_func hash_fn);
 
-int tdb_reopen(TDB_CONTEXT *tdb);
-int tdb_reopen_all(void);
-void tdb_logging_function(TDB_CONTEXT *tdb, tdb_log_func);
 enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb);
 const char *tdb_errorstr(TDB_CONTEXT *tdb);
 TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key);
 int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key);
 int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
-int tdb_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf);
 int tdb_close(TDB_CONTEXT *tdb);
 TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb);
 TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key);
 int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *);
-int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key);
-int tdb_lockall(TDB_CONTEXT *tdb);
-void tdb_unlockall(TDB_CONTEXT *tdb);
-
-/* Low level locking functions: use with care */
-int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key);
-int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key);
-int tdb_chainlock_read(TDB_CONTEXT *tdb, TDB_DATA key);
-int tdb_chainunlock_read(TDB_CONTEXT *tdb, TDB_DATA key);
-TDB_CONTEXT *tdb_copy(TDB_CONTEXT *tdb, const char *outfile);
-
-/* Debug functions. Not used in production. */
-void tdb_dump_all(TDB_CONTEXT *tdb);
-int tdb_printfreelist(TDB_CONTEXT *tdb);
-
-extern TDB_DATA tdb_null;
 
 #ifdef  __cplusplus
 }
-- 
2.10.2


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.