Implement basic full log replay.
[bluesky.git] / bluesky / bluesky-private.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 /* Declarations internal to the BlueSky library.  This header file should not
10  * be included by any users of the library (such as any filesystem
11  * proxy)--external users should only include bluesky.h. */
12
13 #ifndef _BLUESKY_PRIVATE_H
14 #define _BLUESKY_PRIVATE_H
15
16 #include "bluesky.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 extern int bluesky_verbose;
23
24 /* Target cache size levels. */
25 extern int bluesky_watermark_low_dirty;
26 extern int bluesky_watermark_medium_dirty;
27 extern int bluesky_watermark_high_dirty;
28
29 extern int bluesky_watermark_low_total;
30 extern int bluesky_watermark_medium_total;
31 extern int bluesky_watermark_high_total;
32
33 /* TODO: Make this go away entirely. */
34 BlueSkyFS *bluesky_new_fs(gchar *name);
35
36 /* Linked list update functions for LRU lists. */
37 void bluesky_list_unlink(GList *head, GList *item);
38 GList *bluesky_list_prepend(GList *head, BlueSkyInode *inode);
39 GList *bluesky_list_append(GList *head, BlueSkyInode *inode);
40 BlueSkyInode *bluesky_list_head(GList *head);
41 BlueSkyInode *bluesky_list_tail(GList *head);
42
43 /* Serialization and deserialization of filesystem data for storing to
44  * persistent storage. */
45 void bluesky_serialize_superblock(GString *out, BlueSkyFS *fs);
46 BlueSkyFS *bluesky_deserialize_superblock(const gchar *buf);
47 BlueSkyCloudLog *bluesky_serialize_inode(BlueSkyInode *inode);
48 gboolean bluesky_deserialize_inode(BlueSkyInode *inode, BlueSkyCloudLog *item);
49
50 void bluesky_serialize_cloudlog(BlueSkyCloudLog *log,
51                                 GString *encrypted,
52                                 GString *authenticated,
53                                 GString *writable);
54
55 /* Storage layer.  Requests can be performed asynchronously, so these objects
56  * help keep track of operations in progress. */
57 typedef enum {
58     STORE_OP_NONE,
59     STORE_OP_GET,
60     STORE_OP_PUT,
61     STORE_OP_DELETE,
62     STORE_OP_BARRIER,       // Waits for other selected operations to complete
63 } BlueSkyStoreOp;
64
65 typedef enum {
66     ASYNC_NEW,              // Operation not yet submitted to storage layer
67     ASYNC_PENDING,          // Submitted to storage layer
68     ASYNC_RUNNING,          // Operation is in progress
69     ASYNC_COMPLETE,         // Operation finished, results available
70 } BlueSkyAsyncStatus;
71
72 struct BlueSkyNotifierList;
73 typedef struct _BlueSkyStoreAsync BlueSkyStoreAsync;
74 struct _BlueSkyStoreAsync {
75     BlueSkyStore *store;
76
77     GMutex *lock;
78     GCond *completion_cond;     /* Used to wait for operation to complete. */
79
80     gint refcount;              /* Reference count for destruction. */
81
82     BlueSkyAsyncStatus status;
83
84     BlueSkyStoreOp op;
85     gchar *key;                 /* Key to read/write */
86     BlueSkyRCStr *data;         /* Data read/to write */
87
88     int result;                 /* Result code; 0 for success. */
89     struct BlueSkyNotifierList *notifiers;
90     gint notifier_count;
91
92     /* The barrier waiting on this operation.  Support for more than one
93      * barrier for a single async is not well-supported and should be avoided
94      * if possible. */
95     BlueSkyStoreAsync *barrier;
96
97     bluesky_time_hires start_time;  /* Time operation was submitted. */
98     bluesky_time_hires exec_time;   /* Time processing started on operation. */
99
100     gpointer store_private;     /* For use by the storage implementation */
101 };
102
103 /* Support for notification lists.  These are lists of one-shot functions which
104  * can be called when certain events--primarily, competed storage
105  * events--occur.  Multiple notifiers can be added, but no particular order is
106  * guaranteed for the notification functions to be called. */
107 struct BlueSkyNotifierList {
108     struct BlueSkyNotifierList *next;
109     GFunc func;
110     BlueSkyStoreAsync *async;
111     gpointer user_data;     // Passed to the function when called
112 };
113
114 /* The abstraction layer for storage, allowing multiple implementations. */
115 typedef struct {
116     /* Create a new store instance and return a handle to it. */
117     gpointer (*create)(const gchar *path);
118
119     /* Clean up any resources used by this store. */
120     void (*destroy)(gpointer store);
121
122     /* Submit an operation (get/put/delete) to the storage layer to be
123      * performed asynchronously. */
124     void (*submit)(gpointer store, BlueSkyStoreAsync *async);
125
126     /* Clean up any implementation-private data in a BlueSkyStoreAsync. */
127     void (*cleanup)(gpointer store, BlueSkyStoreAsync *async);
128 } BlueSkyStoreImplementation;
129
130 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
131                             const gchar *name);
132
133 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store);
134 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async);
135 void bluesky_store_async_ref(BlueSkyStoreAsync *async);
136 void bluesky_store_async_unref(BlueSkyStoreAsync *async);
137 void bluesky_store_async_wait(BlueSkyStoreAsync *async);
138 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
139                                       GFunc func, gpointer user_data);
140 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async);
141 void bluesky_store_async_submit(BlueSkyStoreAsync *async);
142 void bluesky_store_sync(BlueSkyStore *store);
143
144 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
145                                BlueSkyStoreAsync *async);
146
147 void bluesky_inode_start_sync(BlueSkyInode *inode);
148
149 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
150 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
151                          BlueSkyStoreAsync *barrier);
152 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
153                          GList **log_items);
154 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items);
155 void bluesky_file_drop_cached(BlueSkyInode *inode);
156
157 /* Writing of data to the cloud in log segments and tracking the location of
158  * various pieces of data (both where in the cloud and where cached locally).
159  * */
160
161 typedef struct {
162     char bytes[16];
163 } BlueSkyCloudID;
164
165 typedef struct {
166     uint32_t directory;
167     uint32_t sequence;
168     uint32_t offset;
169     uint32_t size;
170 } BlueSkyCloudPointer;
171
172 typedef enum {
173     LOGTYPE_INVALID = -1,
174     LOGTYPE_UNKNOWN = 0,
175     LOGTYPE_DATA = 1,
176     LOGTYPE_INODE = 2,
177     LOGTYPE_INODE_MAP = 3,
178     LOGTYPE_CHECKPOINT = 4,
179     LOGTYPE_CHECKPOINT_PTR = 5,
180 } BlueSkyCloudLogType;
181
182 /* A record which tracks an object which has been written to a local log,
183  * cached, locally, and/or written to the cloud. */
184 #define CLOUDLOG_JOURNAL    0x01
185 #define CLOUDLOG_CLOUD      0x02
186 #define CLOUDLOG_CACHE      0x04
187 struct _BlueSkyCloudLog {
188     gint refcount;
189     GMutex *lock;
190     GCond *cond;
191
192     BlueSkyFS *fs;
193
194     BlueSkyCloudLogType type;
195
196     // Bitmask of CLOUDLOG_* flags indicating where the object exists.
197     int location_flags;
198     int pending_read, pending_write;
199
200     // A stable identifier for the object (only changes when authenticated data
201     // is written out, but stays the same when the in-cloud cleaner relocates
202     // the object).
203     BlueSkyCloudID id;
204
205     // The inode which owns this data, if any, and an offset.
206     uint64_t inum;
207     int32_t inum_offset;
208
209     // The location of the object in the cloud, if available.
210     BlueSkyCloudPointer location;
211
212     // TODO: Location in journal/cache
213     int log_seq, log_offset, log_size;
214
215     // Pointers to other objects.  Each link counts towards the reference count
216     // of the pointed-to object.  To avoid memory leaks there should be no
217     // cycles in the reference graph.
218     GArray *links;
219
220     // Serialized data, if available in memory (otherwise NULL), and a lock
221     // count which tracks if there are users that require the data to be kept
222     // around.
223     BlueSkyRCStr *data;
224     int data_lock_count;
225 };
226
227 /* Serialize objects into a log segment to be written to the cloud. */
228 struct _BlueSkyCloudLogState {
229     GString *data;
230     BlueSkyCloudPointer location;
231     GList *inode_list;
232     GSList *writeback_list;     // Items which are being serialized right now
233     GList *pending_segments;    // Segments which are being uploaded now
234 };
235
236 gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b);
237 guint bluesky_cloudlog_hash(gconstpointer a);
238 BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs, const BlueSkyCloudID *id);
239 gchar *bluesky_cloudlog_id_to_string(BlueSkyCloudID id);
240 BlueSkyCloudID bluesky_cloudlog_id_from_string(const gchar *idstr);
241 void bluesky_cloudlog_ref(BlueSkyCloudLog *log);
242 void bluesky_cloudlog_unref(BlueSkyCloudLog *log);
243 void bluesky_cloudlog_stats_update(BlueSkyCloudLog *log, int type);
244 void bluesky_cloudlog_sync(BlueSkyCloudLog *log);
245 void bluesky_cloudlog_insert(BlueSkyCloudLog *log);
246 void bluesky_cloudlog_fetch(BlueSkyCloudLog *log);
247 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
248                                                BlueSkyFS *fs);
249 void bluesky_cloudlog_flush(BlueSkyFS *fs);
250
251 /* Logging infrastructure for ensuring operations are persistently recorded to
252  * disk. */
253 #define BLUESKY_CRC32C_SEED (~(uint32_t)0)
254 #define BLUESKY_CRC32C_VALIDATOR ((uint32_t)0xb798b438UL)
255 uint32_t crc32c(uint32_t crc, const char *buf, unsigned int length);
256 uint32_t crc32c_finalize(uint32_t crc);
257
258 struct _BlueSkyLog {
259     BlueSkyFS *fs;
260     char *log_directory;
261     GAsyncQueue *queue;
262     int fd, dirfd;
263     int seq_num;
264     GSList *committed;
265
266     /* The currently-open log file. */
267     BlueSkyCacheFile *current_log;
268
269     /* Cache of log segments which have been memory-mapped. */
270     GMutex *mmap_lock;
271     GHashTable *mmap_cache;
272
273     /* A count of the disk space consumed (in 1024-byte units) by all files
274      * tracked by mmap_cache (whether mapped or not, actually). */
275     gint disk_used;
276
277     /* The smallest journal sequence number which may still contain data that
278      * must be preserved (since it it not yet in the cloud). */
279     int journal_watermark;
280 };
281
282 /* An object for tracking log files which are stored locally--either the
283  * journal for filesystem consistency or log segments which have been fetched
284  * back from cloud storage. */
285 struct _BlueSkyCacheFile {
286     GMutex *lock;
287     GCond *cond;
288     gint refcount;
289     int type;                   // Only one of CLOUDLOG_{JOURNAL,CLOUD}
290     int log_dir;
291     int log_seq;
292     char *filename;             // Local filename, relateive to log directory
293     gint mapcount;              // References to the mmaped data
294     const char *addr;           // May be null if data is not mapped in memory
295     size_t len;
296     BlueSkyFS *fs;
297     BlueSkyLog *log;
298     gboolean fetching, ready;   // Cloud data: downloading or ready for use
299     int64_t atime;              // Access time, for cache management
300 };
301
302 BlueSkyLog *bluesky_log_new(const char *log_directory);
303 void bluesky_log_item_submit(BlueSkyCloudLog *item, BlueSkyLog *log);
304 void bluesky_log_finish_all(GList *log_items);
305 BlueSkyRCStr *bluesky_log_map_object(BlueSkyFS *fs, int log_dir, int log_seq,
306                                      int log_offset, int log_size);
307 void bluesky_mmap_unref(BlueSkyCacheFile *mmap);
308 void bluesky_cachefile_unref(BlueSkyCacheFile *cachefile);
309
310 BlueSkyCacheFile *bluesky_cachefile_lookup(BlueSkyFS *fs,
311                                            int clouddir, int log_seq);
312 void bluesky_cachefile_gc(BlueSkyFS *fs);
313
314 void bluesky_replay(BlueSkyFS *fs);
315
316 /* Used to track log segments that are being written to the cloud. */
317 typedef struct {
318     BlueSkyRCStr *data;
319     GSList *items;
320     GMutex *lock;
321     GCond *cond;
322     gboolean complete;
323 } SerializedRecord;
324
325 /***** Inode map management *****/
326
327 /* Mapping information for a single inode number.  These are grouped together
328  * into InodeMapRange objects. */
329 typedef struct {
330     uint64_t inum;
331
332     /* The ID of the most recent version of the inode. */
333     BlueSkyCloudID id;
334
335     /* The location where that version is written in the cloud. */
336     BlueSkyCloudPointer location;
337
338     /* If the cloud log entry exists in memory, then a pointer to it, otherwise
339      * NULL. */
340     BlueSkyCloudLog *item;
341 } InodeMapEntry;
342
343 typedef struct {
344     /* Starting and ending inode number values that fall in this section.
345      * Endpoint values are inclusive. */
346     uint64_t start, end;
347
348     /* A sorted list (by inode number) of InodeMapEntry objects. */
349     GSequence *map_entries;
350
351     /* The location where this inode map section is stored in the cloud. */
352     BlueSkyCloudPointer location;
353
354     /* Have there been changes that require writing this section out again? */
355     gboolean dirty;
356 } InodeMapRange;
357
358 InodeMapEntry *bluesky_inode_map_lookup(GSequence *inode_map, uint64_t inum,
359                                         int action);
360
361 #ifdef __cplusplus
362 }
363 #endif
364
365 #endif