Preparatory work before implementing proper cloud writing.
[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 void bluesky_serialize_inode(GString *out, BlueSkyInode *inode);
48 gboolean bluesky_deserialize_inode(BlueSkyInode *inode, const gchar *buf);
49
50 /* Storage layer.  Requests can be performed asynchronously, so these objects
51  * help keep track of operations in progress. */
52 typedef enum {
53     STORE_OP_NONE,
54     STORE_OP_GET,
55     STORE_OP_PUT,
56     STORE_OP_DELETE,
57     STORE_OP_BARRIER,       // Waits for other selected operations to complete
58 } BlueSkyStoreOp;
59
60 typedef enum {
61     ASYNC_NEW,              // Operation not yet submitted to storage layer
62     ASYNC_PENDING,          // Submitted to storage layer
63     ASYNC_RUNNING,          // Operation is in progress
64     ASYNC_COMPLETE,         // Operation finished, results available
65 } BlueSkyAsyncStatus;
66
67 struct BlueSkyNotifierList;
68 typedef struct _BlueSkyStoreAsync BlueSkyStoreAsync;
69 struct _BlueSkyStoreAsync {
70     BlueSkyStore *store;
71
72     GMutex *lock;
73     GCond *completion_cond;     /* Used to wait for operation to complete. */
74
75     gint refcount;              /* Reference count for destruction. */
76
77     BlueSkyAsyncStatus status;
78
79     BlueSkyStoreOp op;
80     gchar *key;                 /* Key to read/write */
81     BlueSkyRCStr *data;         /* Data read/to write */
82
83     int result;                 /* Result code; 0 for success. */
84     struct BlueSkyNotifierList *notifiers;
85     gint notifier_count;
86
87     /* The barrier waiting on this operation.  Support for more than one
88      * barrier for a single async is not well-supported and should be avoided
89      * if possible. */
90     BlueSkyStoreAsync *barrier;
91
92     bluesky_time_hires start_time;  /* Time operation was submitted. */
93     bluesky_time_hires exec_time;   /* Time processing started on operation. */
94
95     gpointer store_private;     /* For use by the storage implementation */
96 };
97
98 /* Support for notification lists.  These are lists of one-shot functions which
99  * can be called when certain events--primarily, competed storage
100  * events--occur.  Multiple notifiers can be added, but no particular order is
101  * guaranteed for the notification functions to be called. */
102 struct BlueSkyNotifierList {
103     struct BlueSkyNotifierList *next;
104     GFunc func;
105     BlueSkyStoreAsync *async;
106     gpointer user_data;     // Passed to the function when called
107 };
108
109 /* The abstraction layer for storage, allowing multiple implementations. */
110 typedef struct {
111     /* Create a new store instance and return a handle to it. */
112     gpointer (*create)(const gchar *path);
113
114     /* Clean up any resources used by this store. */
115     void (*destroy)(gpointer store);
116
117     /* Submit an operation (get/put/delete) to the storage layer to be
118      * performed asynchronously. */
119     void (*submit)(gpointer store, BlueSkyStoreAsync *async);
120
121     /* Clean up any implementation-private data in a BlueSkyStoreAsync. */
122     void (*cleanup)(gpointer store, BlueSkyStoreAsync *async);
123 } BlueSkyStoreImplementation;
124
125 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
126                             const gchar *name);
127
128 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store);
129 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async);
130 void bluesky_store_async_ref(BlueSkyStoreAsync *async);
131 void bluesky_store_async_unref(BlueSkyStoreAsync *async);
132 void bluesky_store_async_wait(BlueSkyStoreAsync *async);
133 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
134                                       GFunc func, gpointer user_data);
135 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async);
136 void bluesky_store_async_submit(BlueSkyStoreAsync *async);
137 void bluesky_store_sync(BlueSkyStore *store);
138
139 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
140                                BlueSkyStoreAsync *async);
141
142 void bluesky_inode_start_sync(BlueSkyInode *inode, BlueSkyStoreAsync *barrier);
143
144 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
145 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
146                          BlueSkyStoreAsync *barrier);
147 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
148                          BlueSkyStoreAsync *barrier, GList **log_items);
149 void bluesky_file_flush(BlueSkyInode *inode, BlueSkyStoreAsync *barrier,
150                         GList **log_items);
151 void bluesky_file_drop_cached(BlueSkyInode *inode);
152
153 /* Logging infrastructure for ensuring operations are persistently recorded to
154  * disk. */
155 #define BLUESKY_CRC32C_SEED (~(uint32_t)0)
156 uint32_t crc32c(uint32_t crc, const char *buf, unsigned int length);
157 uint32_t crc32c_finalize(uint32_t crc);
158
159 struct _BlueSkyLog {
160     char *log_directory;
161     GAsyncQueue *queue;
162     int fd;
163     int seq_num;
164 };
165
166 typedef struct {
167     gboolean committed;
168     GMutex *lock;
169     GCond *cond;
170     char *key;
171     BlueSkyRCStr *data;
172 } BlueSkyLogItem;
173
174 BlueSkyLog *bluesky_log_new(const char *log_directory);
175 BlueSkyLogItem *bluesky_log_item_new();
176 void bluesky_log_item_submit(BlueSkyLogItem *item, BlueSkyLog *log);
177 void bluesky_log_item_finish(BlueSkyLogItem *item);
178
179 /* Writing of data to the cloud in log segments and tracking the location of
180  * various pieces of data (both where in the cloud and where cached locally).
181  * */
182
183 typedef struct {
184     char bytes[16];
185 } BlueSkyCloudID;
186
187 typedef struct {
188     uint32_t directory;
189     uint32_t sequence;
190     uint32_t offset;
191     uint32_t size;
192 } BlueSkyCloudPointer;
193
194 typedef enum {
195     LOGTYPE_UNKNOWN = 0,
196     LOGTYPE_DATA = 1,
197     LOGTYPE_INODE = 2,
198     LOGTYPE_INODE_MAP = 3,
199     LOGTYPE_CHECKPOINT = 4,
200     LOGTYPE_CHECKPOINT_PTR = 5,
201 } BlueSkyCloudLogType;
202
203 /* A record which tracks an object which has been written to a local log,
204  * cached, locally, and/or written to the cloud. */
205 #define CLOUDLOG_JOURNAL    0x01
206 #define CLOUDLOG_CACHE      0x02
207 #define CLOUDLOG_CLOUD      0x04
208 typedef struct {
209     gint refcount;
210
211     BlueSkyFS *fs;
212
213     BlueSkyCloudLogType type;
214
215     // Bitmask of CLOUDLOG_* flags indicating where the object exists.
216     int location_flags;
217
218     // A stable identifier for the object (only changes when authenticated data
219     // is written out, but stays the same when the in-cloud cleaner relocates
220     // the object).
221     BlueSkyCloudID id;
222
223     // The inode which owns this data, if any, and an offset.
224     uint64_t inum;
225     int32_t inum_offset;
226
227     // The location of the object in the cloud, if available.
228     BlueSkyCloudPointer location;
229
230     // TODO: Location in journal/cache
231
232     // Pointers to other objects
233     GArray *pointers;
234
235     // Serialized data, if available in memory (otherwise NULL).
236     BlueSkyRCStr *data;
237 } BlueSkyCloudLog;
238
239 /* Serialize objects into a log segment to be written to the cloud. */
240 struct _BlueSkyCloudLogState {
241     GString *data;
242     BlueSkyCloudPointer location;
243     GList *inode_list;
244 };
245
246 gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b);
247 guint bluesky_cloudlog_hash(gconstpointer a);
248 BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs);
249 gchar *bluesky_cloudlog_id_to_string(BlueSkyCloudID id);
250 BlueSkyCloudID bluesky_cloudlog_id_from_string(const gchar *idstr);
251 void bluesky_cloudlog_ref(BlueSkyCloudLog *log);
252 void bluesky_cloudlog_unref(BlueSkyCloudLog *log);
253 BlueSkyLogItem *bluesky_cloudlog_sync(BlueSkyCloudLog *log);
254 void bluesky_cloudlog_insert(BlueSkyCloudLog *log);
255 void bluesky_cloudlog_write_log(BlueSkyFS *fs);
256
257 #ifdef __cplusplus
258 }
259 #endif
260
261 #endif