Add general statistics-gathering infrastructure.
[bluesky.git] / bluesky / bluesky.h
index 831d850..ff2bf23 100644 (file)
 #define _BLUESKY_H
 
 #include <stdint.h>
+#include <inttypes.h>
 #include <glib.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-struct S3Store;
+/* Various options to tweak for performance benchmarking purposes. */
+typedef struct {
+    /* Perform all get/put operations synchronously. */
+    int synchronous_stores;
+
+    /* Write data in cache immediately after file is modified. */
+    int writethrough_cache;
+
+    /* Can inodes be fetched asynchronously?  (Inode object is initially
+     * created in a pending state, and not unlocked until the data is actually
+     * available.) */
+    int sync_inode_fetches;
+
+    /* Should frontends handle requests serially or allow operations to proceed
+     * in parallel? */
+    int sync_frontends;
+} BlueSkyOptions;
+
+extern BlueSkyOptions bluesky_options;
+
+/* Maximum number of threads to use in any particular thread pool, or -1 for no
+ * limit */
+extern int bluesky_max_threads;
+
+/* A general-purpose counter for gathering run-time statistics. */
+struct bluesky_stats {
+    const char *name;
+    int64_t count;
+    int64_t sum;
+};
+struct bluesky_stats *bluesky_stats_new(const char *name);
+void bluesky_stats_add(struct bluesky_stats *stats, int64_t value);
+void bluesky_stats_dump_all();
+
+/* BlueSky status and error codes.  Various frontends should translate these to
+ * the appropriate error code for whatever protocol they implement. */
+typedef enum {
+    BSTATUS_OK = 0,             /* No error */
+    BSTATUS_IOERR,              /* I/O error of some form */
+    BSTATUS_NOENT,              /* File does not exist */
+} BlueSkyStatus;
+
+void bluesky_init(void);
+
+gchar *bluesky_lowercase(const gchar *s);
 
 /* Reference-counted blocks of memory, used for passing data in and out of
  * storage backends and in other places. */
@@ -27,16 +72,34 @@ typedef struct {
 } BlueSkyRCStr;
 
 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len);
+BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s);
 void bluesky_string_ref(BlueSkyRCStr *string);
 void bluesky_string_unref(BlueSkyRCStr *string);
 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string);
+void bluesky_string_resize(BlueSkyRCStr *string, gsize len);
 
 /* Cryptographic operations. */
+#define CRYPTO_BLOCK_SIZE 16        /* 128-bit AES */
+#define CRYPTO_KEY_SIZE   16
+
 void bluesky_crypt_init();
+void bluesky_crypt_hash_key(const char *keystr, uint8_t *out);
 void bluesky_crypt_random_bytes(guchar *buf, gint len);
 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key);
 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key);
 
+/* Storage interface.  This presents a key-value store abstraction, and can
+ * have multiple implementations: in-memory, on-disk, in-cloud. */
+struct _BlueSkyStore;
+typedef struct _BlueSkyStore BlueSkyStore;
+
+void bluesky_store_init();
+BlueSkyStore *bluesky_store_new(const gchar *type);
+void bluesky_store_free(BlueSkyStore *store);
+BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key);
+void bluesky_store_put(BlueSkyStore *store,
+                       const gchar *key, BlueSkyRCStr *val);
+
 /* File types.  The numeric values are chosen to match with those used in
  * NFSv3. */
 typedef enum {
@@ -47,6 +110,10 @@ typedef enum {
     BLUESKY_SYMLINK = 5,
     BLUESKY_SOCKET = 6,
     BLUESKY_FIFO = 7,
+
+    /* Special types used only internally. */
+    BLUESKY_PENDING = 0,    /* Inode being loaded; type not yet determined */
+    BLUESKY_INVALID = -1,   /* Inode is invalid (failed to load) */
 } BlueSkyFileType;
 
 /* Filesystem state.  Each filesystem which is exported is represented by a
@@ -58,9 +125,23 @@ typedef struct {
     GHashTable *inodes;         /* Cached inodes */
     uint64_t next_inum;         /* Next available inode for allocation */
 
-    struct S3Store *store;
+    BlueSkyStore *store;
+
+    /* Accounting for memory used for caches.  Space is measured in blocks, not
+     * bytes.  We track both total data in the caches and dirty data (total
+     * data includes dirty data).  Updates to these variables must be made
+     * atomically. */
+    gint cache_total, cache_dirty;
 
-    uint8_t *encryption_key;
+    /* Linked list of inodes, sorted by access/modification times for cache
+     * management.  Editing these lists is protected by the filesystem lock; to
+     * avoid deadlock do not attempt to take any other locks while the FS lock
+     * is held for list editing purposes.  Items at the head of the list are
+     * most recently accessed/modified. */
+    GList dirty_list, accessed_list;
+
+    /* Mutex for the flush daemon, to prevent concurrent execution. */
+    GMutex *flushd_lock;
 } BlueSkyFS;
 
 /* Inode number of the root directory. */
@@ -69,6 +150,10 @@ typedef struct {
 /* Timestamp, measured in microseconds since the Unix epoch. */
 typedef int64_t bluesky_time;
 
+/* High-resolution timer, measured in nanoseconds. */
+typedef int64_t bluesky_time_hires;
+bluesky_time_hires bluesky_now_hires();
+
 /* In-memory representation of an inode within a Blue Sky server.  This
  * corresponds roughly with information that is committed to persistent
  * storage.  Locking/refcounting rules:
@@ -79,6 +164,13 @@ typedef int64_t bluesky_time;
  *     dropping from the cache.
  *   - Any pending operations should hold extra references to the inode as
  *     appropriate to keep it available until the operation completes.
+ *   - Locking dependency order is, when multiple locks are to be acquired, to
+ *     acquire locks on parents in the filesystem tree before children.
+ *     (TODO: What about rename when we acquire locks in unrelated parts of the
+ *     filesystem?)
+ *   - An inode should not be locked while the filesystem lock is already held,
+ *     since some code may do an inode lookup (which acquires the filesystem
+ *     lock) while a different inode is locked.
  * */
 typedef struct {
     GMutex *lock;
@@ -96,7 +188,29 @@ typedef struct {
      * that we don't exhaust the identifier space. */
     uint64_t inum;
 
-    uint64_t change_count;      /* Incremented each with each change made */
+    /* change_count is increased with every operation which modifies the inode,
+     * and can be used to determine if cached data is still valid.
+     * change_commit is the value of change_count when the inode was last
+     * committed to stable storage. */
+    uint64_t change_count, change_commit;
+
+    /* Timestamp for controlling when modified data is flushed to stable
+     * storage.  When an inode is first modified from a clean state, this is
+     * set to the current time.  If the inode is clean, it is set to zero. */
+    int64_t change_time;
+
+    /* Last access time to this inode, for controlling cache evictions. */
+    int64_t access_time;
+
+    /* Additional state for tracking cache writeback status. */
+    uint64_t change_pending;    /* change_count version currently being committed to storage */
+
+    /* Pointers to the linked-list elements for this inode in the accessed and
+     * dirty linked lists.  We re-use the GList structure, using ->next to
+     * point to the head of the list and ->prev to point to the tail.  The data
+     * element is unused. */
+    GList *accessed_list, *dirty_list;
+
     int64_t atime;              /* Microseconds since the Unix epoch */
     int64_t ctime;
     int64_t mtime;
@@ -109,7 +223,11 @@ typedef struct {
     /* Directory-specific fields */
     GSequence *dirents;         /* List of entries for READDIR */
     GHashTable *dirhash;        /* Hash table by name for LOOKUP */
+    GHashTable *dirhash_folded; /* As above, but case-folded */
     uint64_t parent_inum;       /* inode for ".."; 0 if the root directory */
+
+    /* Symlink-specific fields */
+    gchar *symlink_contents;
 } BlueSkyInode;
 
 /* A directory entry.  The name is UTF-8 and is a freshly-allocated string.
@@ -119,6 +237,7 @@ typedef struct {
  * is used to provide a stable key for restarting a READDIR call. */
 typedef struct {
     gchar *name;
+    gchar *name_folded;         /* Name, folded for case-insensitive lookup */
     uint32_t cookie;
     uint64_t inum;
 } BlueSkyDirent;
@@ -142,34 +261,45 @@ typedef struct {
     BlueSkyRCStr *data;         /* Pointer to data in memory if cached */
 } BlueSkyBlock;
 
-BlueSkyFS *bluesky_new_fs(gchar *name);
+BlueSkyFS *bluesky_init_fs(gchar *name, BlueSkyStore *store);
+void bluesky_superblock_flush(BlueSkyFS *fs);
+
 int64_t bluesky_get_current_time();
 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime);
 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs);
+void bluesky_init_inode(BlueSkyInode *i, BlueSkyFileType type);
 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFS *fs, BlueSkyFileType type);
 
 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum);
+void bluesky_inode_ref(BlueSkyInode *inode);
+void bluesky_inode_unref(BlueSkyInode *inode);
 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode);
 
 void bluesky_dirent_destroy(gpointer dirent);
-uint64_t bluesky_directory_hash(gchar *name);
 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name);
-gboolean bluesky_directory_insert(BlueSkyInode *dir, gchar *name,
+uint64_t bluesky_directory_ilookup(BlueSkyInode *inode, gchar *name);
+BlueSkyDirent *bluesky_directory_read(BlueSkyInode *dir, uint32_t cookie);
+gboolean bluesky_directory_insert(BlueSkyInode *dir, const gchar *name,
                                   uint64_t inum);
 void bluesky_directory_dump(BlueSkyInode *dir);
 
-void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
-void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block);
-void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block);
 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size);
 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
                         const char *data, gint len);
 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
                        char *buf, gint len);
 
-struct S3Store *s3store_new();
-BlueSkyRCStr *s3store_get(struct S3Store *store, const gchar *key);
-void s3store_put(struct S3Store *store, const gchar *key, BlueSkyRCStr *val);
+void bluesky_inode_flush(BlueSkyFS *fs, BlueSkyInode *inode);
+void bluesky_inode_fetch(BlueSkyFS *fs, uint64_t inum);
+
+gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
+                            gpointer unused);
+
+void bluesky_flushd_invoke(BlueSkyFS *fs);
+void bluesky_flushd_invoke_conditional(BlueSkyFS *fs);
+void bluesky_inode_do_sync(BlueSkyInode *inode);
+
+void bluesky_debug_dump(BlueSkyFS *fs);
 
 #ifdef __cplusplus
 }