Add a little support for data writeback (not yet hooked up).
[bluesky.git] / bluesky / bluesky.h
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #ifndef _BLUESKY_H
10 #define _BLUESKY_H
11
12 #include <stdint.h>
13 #include <glib.h>
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 void bluesky_init(void);
20
21 /* Reference-counted blocks of memory, used for passing data in and out of
22  * storage backends and in other places. */
23 typedef struct {
24     gint refcount;
25     gchar *data;
26     gsize len;
27 } BlueSkyRCStr;
28
29 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len);
30 void bluesky_string_ref(BlueSkyRCStr *string);
31 void bluesky_string_unref(BlueSkyRCStr *string);
32 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string);
33
34 /* Cryptographic operations. */
35 #define CRYPTO_BLOCK_SIZE 16        /* 128-bit AES */
36 #define CRYPTO_KEY_SIZE   16
37
38 void bluesky_crypt_init();
39 void bluesky_crypt_random_bytes(guchar *buf, gint len);
40 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key);
41 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key);
42
43 /* Storage interface.  This presents a key-value store abstraction, and can
44  * have multiple implementations: in-memory, on-disk, in-cloud. */
45 typedef struct {
46     /* Create a new store instance and return a handle to it. */
47     gpointer (*create)();
48
49     /* Clean up any resources used by this store. */
50     void (*destroy)(gpointer store);
51
52     /* Fetch an item with the given name, or return NULL if not found. */
53     BlueSkyRCStr * (*get)(gpointer store, const gchar *key);
54
55     /* Store an item to the given key name. */
56     void (*put)(gpointer store, const gchar *key, BlueSkyRCStr *val);
57 } BlueSkyStoreImplementation;
58
59 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
60                             const gchar *name);
61
62 struct _BlueSkyStore;
63 typedef struct _BlueSkyStore BlueSkyStore;
64
65 void bluesky_store_init();
66 BlueSkyStore *bluesky_store_new(const gchar *type);
67 void bluesky_store_free(BlueSkyStore *store);
68 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key);
69 void bluesky_store_put(BlueSkyStore *store,
70                        const gchar *key, BlueSkyRCStr *val);
71
72 /* File types.  The numeric values are chosen to match with those used in
73  * NFSv3. */
74 typedef enum {
75     BLUESKY_REGULAR = 1,
76     BLUESKY_DIRECTORY = 2,
77     BLUESKY_BLOCK = 3,
78     BLUESKY_CHARACTER = 4,
79     BLUESKY_SYMLINK = 5,
80     BLUESKY_SOCKET = 6,
81     BLUESKY_FIFO = 7,
82 } BlueSkyFileType;
83
84 /* Filesystem state.  Each filesystem which is exported is represented by a
85  * single bluesky_fs structure in memory. */
86 typedef struct {
87     GMutex *lock;
88
89     gchar *name;                /* Descriptive name for the filesystem */
90     GHashTable *inodes;         /* Cached inodes */
91     uint64_t next_inum;         /* Next available inode for allocation */
92
93     BlueSkyStore *store;
94
95     uint8_t *encryption_key;
96 } BlueSkyFS;
97
98 /* Inode number of the root directory. */
99 #define BLUESKY_ROOT_INUM 1
100
101 /* Timestamp, measured in microseconds since the Unix epoch. */
102 typedef int64_t bluesky_time;
103
104 /* In-memory representation of an inode within a Blue Sky server.  This
105  * corresponds roughly with information that is committed to persistent
106  * storage.  Locking/refcounting rules:
107  *   - To access or modify any data fields, the lock must be held.  This
108  *     includes file blocks.
109  *   - One reference is held by the BlueSkyFS inode hash table.  If that is the
110  *     only reference (and the inode is unlocked), the inode is subject to
111  *     dropping from the cache.
112  *   - Any pending operations should hold extra references to the inode as
113  *     appropriate to keep it available until the operation completes.
114  * */
115 typedef struct {
116     GMutex *lock;
117     gint refcount;
118
119     BlueSkyFS *fs;
120
121     BlueSkyFileType type;
122     uint32_t mode;
123     uint32_t uid, gid;
124     uint32_t nlink;
125
126     /* Rather than track an inode number and generation number, we will simply
127      * never re-use a fileid after a file is deleted.  64 bits should be enough
128      * that we don't exhaust the identifier space. */
129     uint64_t inum;
130
131     /* change_count is increased with every operation which modifies the inode,
132      * and can be used to determine if cached data is still valid.
133      * change_commit is the value of change_count when the inode was last
134      * committed to stable storage. */
135     uint64_t change_count, change_commit;
136
137     int64_t atime;              /* Microseconds since the Unix epoch */
138     int64_t ctime;
139     int64_t mtime;
140     int64_t ntime;              /* "new time": time object was created */
141
142     /* File-specific fields */
143     uint64_t size;
144     GArray *blocks;
145
146     /* Directory-specific fields */
147     GSequence *dirents;         /* List of entries for READDIR */
148     GHashTable *dirhash;        /* Hash table by name for LOOKUP */
149     uint64_t parent_inum;       /* inode for ".."; 0 if the root directory */
150 } BlueSkyInode;
151
152 /* A directory entry.  The name is UTF-8 and is a freshly-allocated string.
153  * Each directory entry is listed in two indices: dirents is indexed by cookie
154  * and dirhash by name.  The cookie is a randomly-assigned 32-bit value, unique
155  * within the directory, that remains unchanged until the entry is deleted.  It
156  * is used to provide a stable key for restarting a READDIR call. */
157 typedef struct {
158     gchar *name;
159     uint32_t cookie;
160     uint64_t inum;
161 } BlueSkyDirent;
162
163 /* File data is divided into fixed-size blocks (except the last block which may
164  * be short?).  These blocks are backed by storage in a key/value store, but
165  * may also be dirty if modifications have been made in-memory that have not
166  * been committed. */
167 #define BLUESKY_BLOCK_SIZE 32768ULL
168 #define BLUESKY_MAX_FILE_SIZE (BLUESKY_BLOCK_SIZE << 24)
169 typedef enum {
170     BLUESKY_BLOCK_ZERO = 0,     /* Data is all zeroes, not explicitly stored */
171     BLUESKY_BLOCK_REF = 1,      /* Reference to key/value store, not cached */
172     BLUESKY_BLOCK_CACHED = 2,   /* Data is cached in memory, clean */
173     BLUESKY_BLOCK_DIRTY = 3,    /* Data needs to be committed to store */
174 } BlueSkyBlockType;
175
176 typedef struct {
177     BlueSkyBlockType type;
178     gchar *ref;                 /* Name of data block in the backing store */
179     BlueSkyRCStr *data;         /* Pointer to data in memory if cached */
180 } BlueSkyBlock;
181
182 BlueSkyFS *bluesky_new_fs(gchar *name);
183 int64_t bluesky_get_current_time();
184 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime);
185 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs);
186 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFS *fs, BlueSkyFileType type);
187
188 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum);
189 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode);
190
191 void bluesky_dirent_destroy(gpointer dirent);
192 uint64_t bluesky_directory_hash(gchar *name);
193 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name);
194 gboolean bluesky_directory_insert(BlueSkyInode *dir, gchar *name,
195                                   uint64_t inum);
196 void bluesky_directory_dump(BlueSkyInode *dir);
197
198 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
199 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block);
200 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block);
201 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size);
202 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
203                         const char *data, gint len);
204 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
205                        char *buf, gint len);
206
207 void bluesky_inode_flush(BlueSkyFS *fs, BlueSkyInode *inode);
208 void bluesky_inode_fetch(BlueSkyFS *fs, uint64_t inum);
209 void bluesky_serialize_inode(GString *out, BlueSkyInode *inode);
210 BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf);
211
212 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
213                             gpointer unused);
214
215 #ifdef __cplusplus
216 }
217 #endif
218
219 #endif