1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
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. */
13 #ifndef _BLUESKY_PRIVATE_H
14 #define _BLUESKY_PRIVATE_H
22 extern int bluesky_verbose;
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;
29 extern int bluesky_watermark_low_total;
30 extern int bluesky_watermark_medium_total;
31 extern int bluesky_watermark_high_total;
33 /* TODO: Make this go away entirely. */
34 BlueSkyFS *bluesky_new_fs(gchar *name);
36 void bluesky_inode_free_resources(BlueSkyInode *inode);
38 /* Linked list update functions for LRU lists. */
39 void bluesky_list_unlink(GList *head, GList *item);
40 GList *bluesky_list_prepend(GList *head, BlueSkyInode *inode);
41 GList *bluesky_list_append(GList *head, BlueSkyInode *inode);
42 BlueSkyInode *bluesky_list_head(GList *head);
43 BlueSkyInode *bluesky_list_tail(GList *head);
45 /* Serialization and deserialization of filesystem data for storing to
46 * persistent storage. */
47 void bluesky_serialize_superblock(GString *out, BlueSkyFS *fs);
48 BlueSkyFS *bluesky_deserialize_superblock(const gchar *buf);
49 BlueSkyCloudLog *bluesky_serialize_inode(BlueSkyInode *inode);
50 gboolean bluesky_deserialize_inode(BlueSkyInode *inode, BlueSkyCloudLog *item);
52 void bluesky_deserialize_cloudlog(BlueSkyCloudLog *item,
56 void bluesky_serialize_cloudlog(BlueSkyCloudLog *log,
58 GString *authenticated,
61 /* Cryptographic operations. */
62 #define CRYPTO_BLOCK_SIZE 16 /* 128-bit AES */
63 #define CRYPTO_KEY_SIZE 16
64 #define CRYPTO_HASH_SIZE 32 /* SHA-256 */
66 typedef struct BlueSkyCryptKeys {
67 uint8_t encryption_key[CRYPTO_KEY_SIZE];
68 uint8_t authentication_key[CRYPTO_HASH_SIZE];
71 void bluesky_crypt_init();
72 void bluesky_crypt_hash_key(const char *keystr, uint8_t *out);
73 void bluesky_crypt_random_bytes(guchar *buf, gint len);
74 void bluesky_crypt_derive_keys(BlueSkyCryptKeys *keys, const gchar *master);
75 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key);
76 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key);
78 void bluesky_crypt_block_encrypt(gchar *cloud_block, size_t len,
79 BlueSkyCryptKeys *keys);
80 gboolean bluesky_crypt_block_decrypt(gchar *cloud_block, size_t len,
81 BlueSkyCryptKeys *keys);
82 void bluesky_cloudlog_encrypt(GString *segment, BlueSkyCryptKeys *keys);
83 void bluesky_cloudlog_decrypt(char *segment, size_t len,
84 BlueSkyCryptKeys *keys,
85 BlueSkyRangeset *items);
87 /* Storage layer. Requests can be performed asynchronously, so these objects
88 * help keep track of operations in progress. */
94 STORE_OP_BARRIER, // Waits for other selected operations to complete
98 ASYNC_NEW, // Operation not yet submitted to storage layer
99 ASYNC_PENDING, // Submitted to storage layer
100 ASYNC_RUNNING, // Operation is in progress
101 ASYNC_COMPLETE, // Operation finished, results available
102 } BlueSkyAsyncStatus;
104 struct BlueSkyNotifierList;
105 typedef struct BlueSkyStoreAsync BlueSkyStoreAsync;
106 struct BlueSkyStoreAsync {
110 GCond *completion_cond; /* Used to wait for operation to complete. */
112 gint refcount; /* Reference count for destruction. */
114 BlueSkyAsyncStatus status;
117 gchar *key; /* Key to read/write */
118 BlueSkyRCStr *data; /* Data read/to write */
120 /* For range requests on reads: starting byte offset and length; len 0
121 * implies reading to the end of the object. At completion, the backend
122 * should set range_done if a range read was made; if not set the entire
123 * object was read and the storage layer will select out just the
124 * appropriate bytes. */
128 int result; /* Result code; 0 for success. */
129 struct BlueSkyNotifierList *notifiers;
132 /* The barrier waiting on this operation. Support for more than one
133 * barrier for a single async is not well-supported and should be avoided
135 BlueSkyStoreAsync *barrier;
137 bluesky_time_hires start_time; /* Time operation was submitted. */
138 bluesky_time_hires exec_time; /* Time processing started on operation. */
140 gpointer store_private; /* For use by the storage implementation */
142 /* If storage operations should be charged to any particular profile, which
144 BlueSkyProfile *profile;
147 /* Support for notification lists. These are lists of one-shot functions which
148 * can be called when certain events--primarily, competed storage
149 * events--occur. Multiple notifiers can be added, but no particular order is
150 * guaranteed for the notification functions to be called. */
151 struct BlueSkyNotifierList {
152 struct BlueSkyNotifierList *next;
154 BlueSkyStoreAsync *async;
155 gpointer user_data; // Passed to the function when called
158 /* The abstraction layer for storage, allowing multiple implementations. */
160 /* Create a new store instance and return a handle to it. */
161 gpointer (*create)(const gchar *path);
163 /* Clean up any resources used by this store. */
164 void (*destroy)(gpointer store);
166 /* Submit an operation (get/put/delete) to the storage layer to be
167 * performed asynchronously. */
168 void (*submit)(gpointer store, BlueSkyStoreAsync *async);
170 /* Clean up any implementation-private data in a BlueSkyStoreAsync. */
171 void (*cleanup)(gpointer store, BlueSkyStoreAsync *async);
173 /* Find the lexicographically-largest file starting with the specified
175 char * (*lookup_last)(gpointer store, const gchar *prefix);
176 } BlueSkyStoreImplementation;
178 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
181 char *bluesky_store_lookup_last(BlueSkyStore *store, const char *prefix);
182 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store);
183 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async);
184 void bluesky_store_async_ref(BlueSkyStoreAsync *async);
185 void bluesky_store_async_unref(BlueSkyStoreAsync *async);
186 void bluesky_store_async_wait(BlueSkyStoreAsync *async);
187 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
188 GFunc func, gpointer user_data);
189 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async);
190 void bluesky_store_async_submit(BlueSkyStoreAsync *async);
191 void bluesky_store_sync(BlueSkyStore *store);
193 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
194 BlueSkyStoreAsync *async);
196 void bluesky_inode_start_sync(BlueSkyInode *inode);
198 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
199 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
200 BlueSkyStoreAsync *barrier);
201 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
203 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items);
204 void bluesky_file_drop_cached(BlueSkyInode *inode);
206 /* Writing of data to the cloud in log segments and tracking the location of
207 * various pieces of data (both where in the cloud and where cached locally).
219 } BlueSkyCloudPointer;
222 LOGTYPE_INVALID = -1,
226 LOGTYPE_INODE_MAP = 3,
227 LOGTYPE_CHECKPOINT = 4,
229 /* Used only as metadata in the local journal, not loaded as a
230 * BlueSkyCloudLogState nor stored in the cloud */
231 LOGTYPE_JOURNAL_MARKER = 16,
232 LOGTYPE_JOURNAL_CHECKPOINT = 17,
233 } BlueSkyCloudLogType;
235 /* Headers that go on items in local log segments and cloud log segments. */
237 uint32_t magic; // HEADER_MAGIC
238 uint8_t type; // Object type + '0'
239 uint32_t offset; // Starting byte offset of the log header
240 uint32_t size1; // Size of the data item (bytes)
243 uint64_t inum; // Inode which owns this data, if any
244 BlueSkyCloudID id; // Object identifier
245 } __attribute__((packed));
248 uint32_t magic; // FOOTER_MAGIC
249 uint32_t crc; // Computed from log_header to log_footer.magic
250 } __attribute__((packed));
252 struct cloudlog_header {
254 uint8_t crypt_auth[CRYPTO_HASH_SIZE];
255 uint8_t crypt_iv[CRYPTO_BLOCK_SIZE];
259 uint32_t size1, size2, size3;
260 } __attribute__((packed));
262 #define JOURNAL_MAGIC "\nLog"
263 #define CLOUDLOG_MAGIC "AgI-"
264 #define CLOUDLOG_MAGIC_ENCRYPTED "AgI=" // CLOUDLOG_MAGIC[3] ^= 0x10
266 /* A record which tracks an object which has been written to a local log,
267 * cached, locally, and/or written to the cloud. */
268 #define CLOUDLOG_JOURNAL 0x01
269 #define CLOUDLOG_CLOUD 0x02
270 #define CLOUDLOG_CACHE 0x04
271 #define CLOUDLOG_UNCOMMITTED 0x10
272 struct BlueSkyCloudLog {
279 BlueSkyCloudLogType type;
281 // Bitmask of CLOUDLOG_* flags indicating where the object exists.
283 int pending_read, pending_write;
285 // A stable identifier for the object (only changes when authenticated data
286 // is written out, but stays the same when the in-cloud cleaner relocates
290 // The inode which owns this data, if any, and an offset.
294 // The size of encrypted object data, not including any headers
297 // The location of the object in the cloud, if available.
298 BlueSkyCloudPointer location;
300 // TODO: Location in journal/cache
301 int log_seq, log_offset, log_size;
303 // Pointers to other objects. Each link counts towards the reference count
304 // of the pointed-to object. To avoid memory leaks there should be no
305 // cycles in the reference graph.
308 // Serialized data, if available in memory (otherwise NULL), and a lock
309 // count which tracks if there are users that require the data to be kept
315 /* Serialize objects into a log segment to be written to the cloud. */
316 struct BlueSkyCloudLogState {
318 BlueSkyCloudPointer location;
320 GSList *writeback_list; // Items which are being serialized right now
321 GList *pending_segments; // Segments which are being uploaded now
324 gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b);
325 guint bluesky_cloudlog_hash(gconstpointer a);
326 BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs, const BlueSkyCloudID *id);
327 gchar *bluesky_cloudlog_id_to_string(BlueSkyCloudID id);
328 BlueSkyCloudID bluesky_cloudlog_id_from_string(const gchar *idstr);
329 void bluesky_cloudlog_threads_init(BlueSkyFS *fs);
330 void bluesky_cloudlog_ref(BlueSkyCloudLog *log);
331 void bluesky_cloudlog_unref(BlueSkyCloudLog *log);
332 void bluesky_cloudlog_unref_delayed(BlueSkyCloudLog *log);
333 void bluesky_cloudlog_erase(BlueSkyCloudLog *log);
334 void bluesky_cloudlog_stats_update(BlueSkyCloudLog *log, int type);
335 void bluesky_cloudlog_sync(BlueSkyCloudLog *log);
336 void bluesky_cloudlog_insert(BlueSkyCloudLog *log);
337 void bluesky_cloudlog_insert_locked(BlueSkyCloudLog *log);
338 BlueSkyCloudLog *bluesky_cloudlog_get(BlueSkyFS *fs, BlueSkyCloudID id);
339 void bluesky_cloudlog_prefetch(BlueSkyCloudLog *log);
340 void bluesky_cloudlog_fetch(BlueSkyCloudLog *log);
341 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
343 void bluesky_cloudlog_flush(BlueSkyFS *fs);
345 /* Logging infrastructure for ensuring operations are persistently recorded to
347 #define BLUESKY_CRC32C_SEED (~(uint32_t)0)
348 #define BLUESKY_CRC32C_VALIDATOR ((uint32_t)0xb798b438UL)
349 uint32_t crc32c(uint32_t crc, const char *buf, unsigned int length);
350 uint32_t crc32c_finalize(uint32_t crc);
360 /* The currently-open log file. */
361 BlueSkyCacheFile *current_log;
363 /* Cache of log segments which have been memory-mapped. */
365 GHashTable *mmap_cache;
367 /* A count of the disk space consumed (in 1024-byte units) by all files
368 * tracked by mmap_cache (whether mapped or not, actually). */
371 /* The smallest journal sequence number which may still contain data that
372 * must be preserved (since it it not yet in the cloud). */
373 int journal_watermark;
376 /* An object for tracking log files which are stored locally--either the
377 * journal for filesystem consistency or log segments which have been fetched
378 * back from cloud storage. */
379 struct BlueSkyCacheFile {
383 int type; // Only one of CLOUDLOG_{JOURNAL,CLOUD}
386 char *filename; // Local filename, relateive to log directory
387 gint mapcount; // References to the mmaped data
388 const char *addr; // May be null if data is not mapped in memory
393 gboolean fetching, ready; // Cloud data: downloading or ready for use
394 int64_t atime; // Access time, for cache management
395 BlueSkyRangeset *items; // Locations of valid items
396 BlueSkyRangeset *prefetches;// Locations we have been requested to prefetch
399 BlueSkyLog *bluesky_log_new(const char *log_directory);
400 void bluesky_log_item_submit(BlueSkyCloudLog *item, BlueSkyLog *log);
401 void bluesky_log_finish_all(GList *log_items);
402 BlueSkyCloudLog *bluesky_log_get_commit_point(BlueSkyFS *fs);
403 void bluesky_log_write_commit_point(BlueSkyFS *fs, BlueSkyCloudLog *marker);
405 BlueSkyRCStr *bluesky_log_map_object(BlueSkyCloudLog *item, gboolean map_data);
406 void bluesky_mmap_unref(BlueSkyCacheFile *mmap);
407 void bluesky_cachefile_unref(BlueSkyCacheFile *cachefile);
409 BlueSkyCacheFile *bluesky_cachefile_lookup(BlueSkyFS *fs,
410 int clouddir, int log_seq,
411 gboolean start_fetch);
412 void bluesky_cachefile_gc(BlueSkyFS *fs);
414 void bluesky_replay(BlueSkyFS *fs);
416 /* Used to track log segments that are being written to the cloud. */
425 /***** Inode map management *****/
427 /* Mapping information for a single inode number. These are grouped together
428 * into InodeMapRange objects. */
432 /* A pointer to the cloud log entry for this inode. This may or may not
433 * actually have data loaded (it might just contain pointers to the data
434 * location, and in fact this will likely often be the case). */
435 BlueSkyCloudLog *item;
439 /* Starting and ending inode number values that fall in this section.
440 * Endpoint values are inclusive. */
443 /* A sorted list (by inode number) of InodeMapEntry objects. */
444 GSequence *map_entries;
446 /* The serialized version of the inode map data. */
447 BlueSkyCloudLog *serialized;
449 /* Have there been changes that require writing this section out again? */
453 InodeMapEntry *bluesky_inode_map_lookup(GSequence *inode_map, uint64_t inum,
455 BlueSkyCloudLog *bluesky_inode_map_serialize(BlueSkyFS *fs);
456 void bluesky_inode_map_minimize(BlueSkyFS *fs);
458 gboolean bluesky_checkpoint_load(BlueSkyFS *fs);