Move encryption into S3 backend.
[bluesky.git] / bluesky / bluesky.h
index b250206..7408849 100644 (file)
 #define _BLUESKY_H
 
 #include <stdint.h>
+#include <inttypes.h>
 #include <glib.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-struct S3Store;
+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. */
@@ -36,10 +39,40 @@ BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string);
 #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. */
+typedef struct {
+    /* Create a new store instance and return a handle to it. */
+    gpointer (*create)();
+
+    /* Clean up any resources used by this store. */
+    void (*destroy)(gpointer store);
+
+    /* Fetch an item with the given name, or return NULL if not found. */
+    BlueSkyRCStr * (*get)(gpointer store, const gchar *key);
+
+    /* Store an item to the given key name. */
+    void (*put)(gpointer store, const gchar *key, BlueSkyRCStr *val);
+} BlueSkyStoreImplementation;
+
+void bluesky_store_register(const BlueSkyStoreImplementation *impl,
+                            const gchar *name);
+
+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 {
@@ -61,9 +94,7 @@ typedef struct {
     GHashTable *inodes;         /* Cached inodes */
     uint64_t next_inum;         /* Next available inode for allocation */
 
-    struct S3Store *store;
-
-    uint8_t *encryption_key;
+    BlueSkyStore *store;
 } BlueSkyFS;
 
 /* Inode number of the root directory. */
@@ -72,6 +103,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:
@@ -99,7 +134,12 @@ 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;
+
     int64_t atime;              /* Microseconds since the Unix epoch */
     int64_t ctime;
     int64_t mtime;
@@ -112,6 +152,7 @@ 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 */
 } BlueSkyInode;
 
@@ -122,6 +163,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;
@@ -145,7 +187,9 @@ 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);
@@ -155,9 +199,9 @@ BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum);
 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);
+gboolean bluesky_directory_insert(BlueSkyInode *dir, const gchar *name,
                                   uint64_t inum);
 void bluesky_directory_dump(BlueSkyInode *dir);
 
@@ -170,9 +214,11 @@ void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
 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);
 
 #ifdef __cplusplus
 }