Add a new option to make caching writethrough instead of writeback.
[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 <inttypes.h>
14 #include <glib.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 /* Various options to tweak for performance benchmarking purposes. */
21 typedef struct {
22     /* Perform all get/put operations synchronously. */
23     int synchronous_stores;
24
25     /* Write data in cache immediately after file is modified. */
26     int writethrough_cache;
27 } BlueSkyOptions;
28
29 extern BlueSkyOptions bluesky_options;
30
31 /* BlueSky status and error codes.  Various frontends should translate these to
32  * the appropriate error code for whatever protocol they implement. */
33 typedef enum {
34     BSTATUS_OK = 0,             /* No error */
35     BSTATUS_IOERR,              /* I/O error of some form */
36     BSTATUS_NOENT,              /* File does not exist */
37 } BlueSkyStatus;
38
39 void bluesky_init(void);
40
41 gchar *bluesky_lowercase(const gchar *s);
42
43 /* Reference-counted blocks of memory, used for passing data in and out of
44  * storage backends and in other places. */
45 typedef struct {
46     gint refcount;
47     gchar *data;
48     gsize len;
49 } BlueSkyRCStr;
50
51 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len);
52 BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s);
53 void bluesky_string_ref(BlueSkyRCStr *string);
54 void bluesky_string_unref(BlueSkyRCStr *string);
55 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string);
56 void bluesky_string_resize(BlueSkyRCStr *string, gsize len);
57
58 /* Cryptographic operations. */
59 #define CRYPTO_BLOCK_SIZE 16        /* 128-bit AES */
60 #define CRYPTO_KEY_SIZE   16
61
62 void bluesky_crypt_init();
63 void bluesky_crypt_hash_key(const char *keystr, uint8_t *out);
64 void bluesky_crypt_random_bytes(guchar *buf, gint len);
65 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key);
66 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key);
67
68 /* Storage interface.  This presents a key-value store abstraction, and can
69  * have multiple implementations: in-memory, on-disk, in-cloud. */
70 struct _BlueSkyStore;
71 typedef struct _BlueSkyStore BlueSkyStore;
72
73 void bluesky_store_init();
74 BlueSkyStore *bluesky_store_new(const gchar *type);
75 void bluesky_store_free(BlueSkyStore *store);
76 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key);
77 void bluesky_store_put(BlueSkyStore *store,
78                        const gchar *key, BlueSkyRCStr *val);
79
80 /* File types.  The numeric values are chosen to match with those used in
81  * NFSv3. */
82 typedef enum {
83     BLUESKY_REGULAR = 1,
84     BLUESKY_DIRECTORY = 2,
85     BLUESKY_BLOCK = 3,
86     BLUESKY_CHARACTER = 4,
87     BLUESKY_SYMLINK = 5,
88     BLUESKY_SOCKET = 6,
89     BLUESKY_FIFO = 7,
90 } BlueSkyFileType;
91
92 /* Filesystem state.  Each filesystem which is exported is represented by a
93  * single bluesky_fs structure in memory. */
94 typedef struct {
95     GMutex *lock;
96
97     gchar *name;                /* Descriptive name for the filesystem */
98     GHashTable *inodes;         /* Cached inodes */
99     uint64_t next_inum;         /* Next available inode for allocation */
100
101     BlueSkyStore *store;
102 } BlueSkyFS;
103
104 /* Inode number of the root directory. */
105 #define BLUESKY_ROOT_INUM 1
106
107 /* Timestamp, measured in microseconds since the Unix epoch. */
108 typedef int64_t bluesky_time;
109
110 /* High-resolution timer, measured in nanoseconds. */
111 typedef int64_t bluesky_time_hires;
112 bluesky_time_hires bluesky_now_hires();
113
114 /* In-memory representation of an inode within a Blue Sky server.  This
115  * corresponds roughly with information that is committed to persistent
116  * storage.  Locking/refcounting rules:
117  *   - To access or modify any data fields, the lock must be held.  This
118  *     includes file blocks.
119  *   - One reference is held by the BlueSkyFS inode hash table.  If that is the
120  *     only reference (and the inode is unlocked), the inode is subject to
121  *     dropping from the cache.
122  *   - Any pending operations should hold extra references to the inode as
123  *     appropriate to keep it available until the operation completes.
124  * */
125 typedef struct {
126     GMutex *lock;
127     gint refcount;
128
129     BlueSkyFS *fs;
130
131     BlueSkyFileType type;
132     uint32_t mode;
133     uint32_t uid, gid;
134     uint32_t nlink;
135
136     /* Rather than track an inode number and generation number, we will simply
137      * never re-use a fileid after a file is deleted.  64 bits should be enough
138      * that we don't exhaust the identifier space. */
139     uint64_t inum;
140
141     /* change_count is increased with every operation which modifies the inode,
142      * and can be used to determine if cached data is still valid.
143      * change_commit is the value of change_count when the inode was last
144      * committed to stable storage. */
145     uint64_t change_count, change_commit;
146
147     /* Timestamp for controlling when modified data is flushed to stable
148      * storage.  When an inode is first modified from a clean state, this is
149      * set to the current time.  If the inode is clean, it is set to zero. */
150     int64_t change_time;
151
152     /* Additional state for tracking cache writeback status. */
153     uint64_t change_pending;    /* change_count version currently being committed to storage */
154
155     int64_t atime;              /* Microseconds since the Unix epoch */
156     int64_t ctime;
157     int64_t mtime;
158     int64_t ntime;              /* "new time": time object was created */
159
160     /* File-specific fields */
161     uint64_t size;
162     GArray *blocks;
163
164     /* Directory-specific fields */
165     GSequence *dirents;         /* List of entries for READDIR */
166     GHashTable *dirhash;        /* Hash table by name for LOOKUP */
167     GHashTable *dirhash_folded; /* As above, but case-folded */
168     uint64_t parent_inum;       /* inode for ".."; 0 if the root directory */
169
170     /* Symlink-specific fields */
171     gchar *symlink_contents;
172 } BlueSkyInode;
173
174 /* A directory entry.  The name is UTF-8 and is a freshly-allocated string.
175  * Each directory entry is listed in two indices: dirents is indexed by cookie
176  * and dirhash by name.  The cookie is a randomly-assigned 32-bit value, unique
177  * within the directory, that remains unchanged until the entry is deleted.  It
178  * is used to provide a stable key for restarting a READDIR call. */
179 typedef struct {
180     gchar *name;
181     gchar *name_folded;         /* Name, folded for case-insensitive lookup */
182     uint32_t cookie;
183     uint64_t inum;
184 } BlueSkyDirent;
185
186 /* File data is divided into fixed-size blocks (except the last block which may
187  * be short?).  These blocks are backed by storage in a key/value store, but
188  * may also be dirty if modifications have been made in-memory that have not
189  * been committed. */
190 #define BLUESKY_BLOCK_SIZE 32768ULL
191 #define BLUESKY_MAX_FILE_SIZE (BLUESKY_BLOCK_SIZE << 24)
192 typedef enum {
193     BLUESKY_BLOCK_ZERO = 0,     /* Data is all zeroes, not explicitly stored */
194     BLUESKY_BLOCK_REF = 1,      /* Reference to key/value store, not cached */
195     BLUESKY_BLOCK_CACHED = 2,   /* Data is cached in memory, clean */
196     BLUESKY_BLOCK_DIRTY = 3,    /* Data needs to be committed to store */
197 } BlueSkyBlockType;
198
199 typedef struct {
200     BlueSkyBlockType type;
201     gchar *ref;                 /* Name of data block in the backing store */
202     BlueSkyRCStr *data;         /* Pointer to data in memory if cached */
203 } BlueSkyBlock;
204
205 BlueSkyFS *bluesky_init_fs(gchar *name, BlueSkyStore *store);
206 void bluesky_superblock_flush(BlueSkyFS *fs);
207
208 int64_t bluesky_get_current_time();
209 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime);
210 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs);
211 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFS *fs, BlueSkyFileType type);
212
213 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum);
214 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode);
215
216 void bluesky_dirent_destroy(gpointer dirent);
217 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name);
218 uint64_t bluesky_directory_ilookup(BlueSkyInode *inode, gchar *name);
219 BlueSkyDirent *bluesky_directory_read(BlueSkyInode *dir, uint32_t cookie);
220 gboolean bluesky_directory_insert(BlueSkyInode *dir, const gchar *name,
221                                   uint64_t inum);
222 void bluesky_directory_dump(BlueSkyInode *dir);
223
224 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
225 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block);
226 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size);
227 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
228                         const char *data, gint len);
229 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
230                        char *buf, gint len);
231
232 void bluesky_inode_flush(BlueSkyFS *fs, BlueSkyInode *inode);
233 void bluesky_inode_fetch(BlueSkyFS *fs, uint64_t inum);
234
235 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
236                             gpointer unused);
237
238 void bluesky_flushd_invoke(BlueSkyFS *fs);
239
240 #ifdef __cplusplus
241 }
242 #endif
243
244 #endif