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
23 extern int bluesky_verbose;
25 /* Target cache size levels. */
26 extern int bluesky_watermark_low_dirty;
27 extern int bluesky_watermark_medium_dirty;
28 extern int bluesky_watermark_high_dirty;
30 extern int bluesky_watermark_low_total;
31 extern int bluesky_watermark_medium_total;
32 extern int bluesky_watermark_high_total;
34 /* TODO: Make this go away entirely. */
35 BlueSkyFS *bluesky_new_fs(gchar *name);
37 void bluesky_inode_free_resources(BlueSkyInode *inode);
39 /* Linked list update functions for LRU lists. */
40 void bluesky_list_unlink(GList *head, GList *item);
41 GList *bluesky_list_prepend(GList *head, BlueSkyInode *inode);
42 GList *bluesky_list_append(GList *head, BlueSkyInode *inode);
43 BlueSkyInode *bluesky_list_head(GList *head);
44 BlueSkyInode *bluesky_list_tail(GList *head);
46 /* Serialization and deserialization of filesystem data for storing to
47 * persistent storage. */
48 void bluesky_serialize_superblock(GString *out, BlueSkyFS *fs);
49 BlueSkyFS *bluesky_deserialize_superblock(const gchar *buf);
50 BlueSkyCloudLog *bluesky_serialize_inode(BlueSkyInode *inode);
51 gboolean bluesky_deserialize_inode(BlueSkyInode *inode, BlueSkyCloudLog *item);
53 void bluesky_deserialize_cloudlog(BlueSkyCloudLog *item,
57 void bluesky_serialize_cloudlog(BlueSkyCloudLog *log,
59 GString *authenticated,
62 /* Cryptographic operations. */
63 #define CRYPTO_BLOCK_SIZE 16 /* 128-bit AES */
64 #define CRYPTO_KEY_SIZE 16
65 #define CRYPTO_HASH_SIZE 32 /* SHA-256 */
67 typedef struct BlueSkyCryptKeys {
68 uint8_t encryption_key[CRYPTO_KEY_SIZE];
69 uint8_t authentication_key[CRYPTO_HASH_SIZE];
72 void bluesky_crypt_init();
73 void bluesky_crypt_hash_key(const char *keystr, uint8_t *out);
74 void bluesky_crypt_random_bytes(guchar *buf, gint len);
75 void bluesky_crypt_derive_keys(BlueSkyCryptKeys *keys, const gchar *master);
76 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key);
77 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key);
79 void bluesky_crypt_block_encrypt(gchar *cloud_block, size_t len,
80 BlueSkyCryptKeys *keys);
81 gboolean bluesky_crypt_block_decrypt(gchar *cloud_block, size_t len,
82 BlueSkyCryptKeys *keys,
83 gboolean allow_unauth);
84 void bluesky_cloudlog_encrypt(GString *segment, BlueSkyCryptKeys *keys);
85 void bluesky_cloudlog_decrypt(char *segment, size_t len,
86 BlueSkyCryptKeys *keys,
87 BlueSkyRangeset *items,
88 gboolean allow_unauth);
90 /* Storage layer. Requests can be performed asynchronously, so these objects
91 * help keep track of operations in progress. */
97 STORE_OP_BARRIER, // Waits for other selected operations to complete
101 ASYNC_NEW, // Operation not yet submitted to storage layer
102 ASYNC_PENDING, // Submitted to storage layer
103 ASYNC_RUNNING, // Operation is in progress
104 ASYNC_COMPLETE, // Operation finished, results available
105 } BlueSkyAsyncStatus;
107 struct BlueSkyNotifierList;
108 typedef struct BlueSkyStoreAsync BlueSkyStoreAsync;
109 struct BlueSkyStoreAsync {
113 GCond *completion_cond; /* Used to wait for operation to complete. */
115 gint refcount; /* Reference count for destruction. */
117 BlueSkyAsyncStatus status;
120 gchar *key; /* Key to read/write */
121 BlueSkyRCStr *data; /* Data read/to write */
123 /* For range requests on reads: starting byte offset and length; len 0
124 * implies reading to the end of the object. At completion, the backend
125 * should set range_done if a range read was made; if not set the entire
126 * object was read and the storage layer will select out just the
127 * appropriate bytes. */
131 int result; /* Result code; 0 for success. */
132 struct BlueSkyNotifierList *notifiers;
135 /* The barrier waiting on this operation. Support for more than one
136 * barrier for a single async is not well-supported and should be avoided
138 BlueSkyStoreAsync *barrier;
140 bluesky_time_hires start_time; /* Time operation was submitted. */
141 bluesky_time_hires exec_time; /* Time processing started on operation. */
143 gpointer store_private; /* For use by the storage implementation */
145 /* If storage operations should be charged to any particular profile, which
147 BlueSkyProfile *profile;
150 /* Support for notification lists. These are lists of one-shot functions which
151 * can be called when certain events--primarily, competed storage
152 * events--occur. Multiple notifiers can be added, but no particular order is
153 * guaranteed for the notification functions to be called. */
154 struct BlueSkyNotifierList {
155 struct BlueSkyNotifierList *next;
157 BlueSkyStoreAsync *async;
158 gpointer user_data; // Passed to the function when called
161 /* The abstraction layer for storage, allowing multiple implementations. */
163 /* Create a new store instance and return a handle to it. */
164 gpointer (*create)(const gchar *path);
166 /* Clean up any resources used by this store. */
167 void (*destroy)(gpointer store);
169 /* Submit an operation (get/put/delete) to the storage layer to be
170 * performed asynchronously. */
171 void (*submit)(gpointer store, BlueSkyStoreAsync *async);
173 /* Clean up any implementation-private data in a BlueSkyStoreAsync. */
174 void (*cleanup)(gpointer store, BlueSkyStoreAsync *async);
176 /* Find the lexicographically-largest file starting with the specified
178 char * (*lookup_last)(gpointer store, const gchar *prefix);
179 } BlueSkyStoreImplementation;
181 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
184 char *bluesky_store_lookup_last(BlueSkyStore *store, const char *prefix);
185 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store);
186 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async);
187 void bluesky_store_async_ref(BlueSkyStoreAsync *async);
188 void bluesky_store_async_unref(BlueSkyStoreAsync *async);
189 void bluesky_store_async_wait(BlueSkyStoreAsync *async);
190 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
191 GFunc func, gpointer user_data);
192 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async);
193 void bluesky_store_async_submit(BlueSkyStoreAsync *async);
194 void bluesky_store_sync(BlueSkyStore *store);
196 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
197 BlueSkyStoreAsync *async);
199 void bluesky_inode_start_sync(BlueSkyInode *inode);
201 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
202 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
203 BlueSkyStoreAsync *barrier);
204 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
206 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items);
207 void bluesky_file_drop_cached(BlueSkyInode *inode);
209 /* Writing of data to the cloud in log segments and tracking the location of
210 * various pieces of data (both where in the cloud and where cached locally).
213 /* Eventually we'll want to support multiple writers. But for now, hard-code
214 * separate namespaces in the cloud for the proxy and the cleaner to write to.
216 #define BLUESKY_CLOUD_DIR_PRIMARY 0
217 #define BLUESKY_CLOUD_DIR_CLEANER 1
228 } BlueSkyCloudPointer;
231 LOGTYPE_INVALID = -1,
235 LOGTYPE_INODE_MAP = 3,
236 LOGTYPE_CHECKPOINT = 4,
238 /* Used only as metadata in the local journal, not loaded as a
239 * BlueSkyCloudLogState nor stored in the cloud */
240 LOGTYPE_JOURNAL_MARKER = 16,
241 LOGTYPE_JOURNAL_CHECKPOINT = 17,
242 } BlueSkyCloudLogType;
244 /* Headers that go on items in local log segments and cloud log segments. */
246 uint32_t magic; // HEADER_MAGIC
247 uint8_t type; // Object type + '0'
248 uint32_t offset; // Starting byte offset of the log header
249 uint32_t size1; // Size of the data item (bytes)
252 uint64_t inum; // Inode which owns this data, if any
253 BlueSkyCloudID id; // Object identifier
254 } __attribute__((packed));
257 uint32_t magic; // FOOTER_MAGIC
258 uint32_t crc; // Computed from log_header to log_footer.magic
259 } __attribute__((packed));
261 struct cloudlog_header {
263 uint8_t crypt_auth[CRYPTO_HASH_SIZE];
264 uint8_t crypt_iv[CRYPTO_BLOCK_SIZE];
268 uint32_t size1, size2, size3;
269 } __attribute__((packed));
271 #define JOURNAL_MAGIC "\nLog"
272 #define CLOUDLOG_MAGIC "AgI-"
273 #define CLOUDLOG_MAGIC_ENCRYPTED "AgI=" // CLOUDLOG_MAGIC[3] ^= 0x10
275 /* A record which tracks an object which has been written to a local log,
276 * cached, locally, and/or written to the cloud. */
277 #define CLOUDLOG_JOURNAL 0x01
278 #define CLOUDLOG_CLOUD 0x02
279 #define CLOUDLOG_CACHE 0x04
280 #define CLOUDLOG_UNCOMMITTED 0x10
281 struct BlueSkyCloudLog {
288 BlueSkyCloudLogType type;
290 // Bitmask of CLOUDLOG_* flags indicating where the object exists.
292 int pending_read, pending_write;
294 // A stable identifier for the object (only changes when authenticated data
295 // is written out, but stays the same when the in-cloud cleaner relocates
299 // The inode which owns this data, if any, and an offset.
303 // The size of encrypted object data, not including any headers
306 // The location of the object in the cloud, if available.
307 BlueSkyCloudPointer location;
309 // TODO: Location in journal/cache
310 int log_seq, log_offset, log_size;
312 // Pointers to other objects. Each link counts towards the reference count
313 // of the pointed-to object. To avoid memory leaks there should be no
314 // cycles in the reference graph.
317 // Serialized data, if available in memory (otherwise NULL), and a lock
318 // count which tracks if there are users that require the data to be kept
324 /* Serialize objects into a log segment to be written to the cloud. */
325 struct BlueSkyCloudLogState {
327 BlueSkyCloudPointer location;
329 GSList *writeback_list; // Items which are being serialized right now
330 GList *pending_segments; // Segments which are being uploaded now
332 int uploads_pending; // Count of uploads in progress, not completed
333 GMutex *uploads_pending_lock;
334 GCond *uploads_pending_cond;
336 /* What is the most recent sequence number written by the cleaner which we
337 * have processed and incorporated into our own log? This gets
338 * incorporated into the version vector written out with our checkpoint
340 int latest_cleaner_seq_seen;
343 gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b);
344 guint bluesky_cloudlog_hash(gconstpointer a);
345 BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs, const BlueSkyCloudID *id);
346 gchar *bluesky_cloudlog_id_to_string(BlueSkyCloudID id);
347 BlueSkyCloudID bluesky_cloudlog_id_from_string(const gchar *idstr);
348 void bluesky_cloudlog_threads_init(BlueSkyFS *fs);
349 void bluesky_cloudlog_ref(BlueSkyCloudLog *log);
350 void bluesky_cloudlog_unref(BlueSkyCloudLog *log);
351 void bluesky_cloudlog_unref_delayed(BlueSkyCloudLog *log);
352 void bluesky_cloudlog_erase(BlueSkyCloudLog *log);
353 void bluesky_cloudlog_stats_update(BlueSkyCloudLog *log, int type);
354 void bluesky_cloudlog_sync(BlueSkyCloudLog *log);
355 void bluesky_cloudlog_insert(BlueSkyCloudLog *log);
356 void bluesky_cloudlog_insert_locked(BlueSkyCloudLog *log);
357 BlueSkyCloudLog *bluesky_cloudlog_get(BlueSkyFS *fs, BlueSkyCloudID id);
358 void bluesky_cloudlog_prefetch(BlueSkyCloudLog *log);
359 void bluesky_cloudlog_fetch(BlueSkyCloudLog *log);
360 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
362 void bluesky_cloudlog_flush(BlueSkyFS *fs);
364 /* Logging infrastructure for ensuring operations are persistently recorded to
366 #define BLUESKY_CRC32C_SEED (~(uint32_t)0)
367 #define BLUESKY_CRC32C_VALIDATOR ((uint32_t)0xb798b438UL)
368 uint32_t crc32c(uint32_t crc, const char *buf, unsigned int length);
369 uint32_t crc32c_finalize(uint32_t crc);
379 /* The currently-open log file. */
380 BlueSkyCacheFile *current_log;
382 /* Cache of log segments which have been memory-mapped. */
384 GHashTable *mmap_cache;
386 /* A count of the disk space consumed (in 1024-byte units) by all files
387 * tracked by mmap_cache (whether mapped or not, actually). */
390 /* The smallest journal sequence number which may still contain data that
391 * must be preserved (since it it not yet in the cloud). */
392 int journal_watermark;
395 /* An object for tracking log files which are stored locally--either the
396 * journal for filesystem consistency or log segments which have been fetched
397 * back from cloud storage. */
398 struct BlueSkyCacheFile {
402 int type; // Only one of CLOUDLOG_{JOURNAL,CLOUD}
405 char *filename; // Local filename, relateive to log directory
406 gint mapcount; // References to the mmaped data
407 const char *addr; // May be null if data is not mapped in memory
412 gboolean fetching; // Cloud data: downloading or ready for use
413 gboolean complete; // Complete file has been fetched from cloud
414 int64_t atime; // Access time, for cache management
415 BlueSkyRangeset *items; // Locations of valid items
416 BlueSkyRangeset *prefetches;// Locations we have been requested to prefetch
419 BlueSkyLog *bluesky_log_new(const char *log_directory);
420 void bluesky_log_item_submit(BlueSkyCloudLog *item, BlueSkyLog *log);
421 void bluesky_log_finish_all(GList *log_items);
422 BlueSkyCloudLog *bluesky_log_get_commit_point(BlueSkyFS *fs);
423 void bluesky_log_write_commit_point(BlueSkyFS *fs, BlueSkyCloudLog *marker);
425 BlueSkyRCStr *bluesky_cachefile_map_raw(BlueSkyCacheFile *cachefile,
426 off_t offset, size_t size);
427 BlueSkyRCStr *bluesky_log_map_object(BlueSkyCloudLog *item, gboolean map_data);
428 void bluesky_mmap_unref(BlueSkyCacheFile *mmap);
429 void bluesky_cachefile_unref(BlueSkyCacheFile *cachefile);
431 BlueSkyCacheFile *bluesky_cachefile_lookup(BlueSkyFS *fs,
432 int clouddir, int log_seq,
433 gboolean start_fetch);
434 void bluesky_cachefile_gc(BlueSkyFS *fs);
436 void bluesky_replay(BlueSkyFS *fs);
438 /* Used to track log segments that are being written to the cloud. */
441 char *key; /* File name for log segment in backend */
442 GString *raw_data; /* Data before encryption */
443 BlueSkyRCStr *data; /* Data after encryption */
450 /***** Inode map management *****/
452 /* Mapping information for a single inode number. These are grouped together
453 * into InodeMapRange objects. */
457 /* A pointer to the cloud log entry for this inode. This may or may not
458 * actually have data loaded (it might just contain pointers to the data
459 * location, and in fact this will likely often be the case). */
460 BlueSkyCloudLog *item;
464 /* Starting and ending inode number values that fall in this section.
465 * Endpoint values are inclusive. */
468 /* A sorted list (by inode number) of InodeMapEntry objects. */
469 GSequence *map_entries;
471 /* The serialized version of the inode map data. */
472 BlueSkyCloudLog *serialized;
474 /* Have there been changes that require writing this section out again? */
478 InodeMapEntry *bluesky_inode_map_lookup(GSequence *inode_map, uint64_t inum,
480 BlueSkyCloudLog *bluesky_inode_map_serialize(BlueSkyFS *fs);
481 void bluesky_inode_map_minimize(BlueSkyFS *fs);
483 gboolean bluesky_checkpoint_load(BlueSkyFS *fs);
485 /* Merging of log state with the work of the cleaner. */
486 void bluesky_cleaner_merge(BlueSkyFS *fs);
487 void bluesky_cleaner_thread_launch(BlueSkyFS *fs);