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 /* Storage layer. Requests can be performed asynchronously, so these objects
62 * help keep track of operations in progress. */
68 STORE_OP_BARRIER, // Waits for other selected operations to complete
72 ASYNC_NEW, // Operation not yet submitted to storage layer
73 ASYNC_PENDING, // Submitted to storage layer
74 ASYNC_RUNNING, // Operation is in progress
75 ASYNC_COMPLETE, // Operation finished, results available
78 struct BlueSkyNotifierList;
79 typedef struct _BlueSkyStoreAsync BlueSkyStoreAsync;
80 struct _BlueSkyStoreAsync {
84 GCond *completion_cond; /* Used to wait for operation to complete. */
86 gint refcount; /* Reference count for destruction. */
88 BlueSkyAsyncStatus status;
91 gchar *key; /* Key to read/write */
92 BlueSkyRCStr *data; /* Data read/to write */
94 int result; /* Result code; 0 for success. */
95 struct BlueSkyNotifierList *notifiers;
98 /* The barrier waiting on this operation. Support for more than one
99 * barrier for a single async is not well-supported and should be avoided
101 BlueSkyStoreAsync *barrier;
103 bluesky_time_hires start_time; /* Time operation was submitted. */
104 bluesky_time_hires exec_time; /* Time processing started on operation. */
106 gpointer store_private; /* For use by the storage implementation */
109 /* Support for notification lists. These are lists of one-shot functions which
110 * can be called when certain events--primarily, competed storage
111 * events--occur. Multiple notifiers can be added, but no particular order is
112 * guaranteed for the notification functions to be called. */
113 struct BlueSkyNotifierList {
114 struct BlueSkyNotifierList *next;
116 BlueSkyStoreAsync *async;
117 gpointer user_data; // Passed to the function when called
120 /* The abstraction layer for storage, allowing multiple implementations. */
122 /* Create a new store instance and return a handle to it. */
123 gpointer (*create)(const gchar *path);
125 /* Clean up any resources used by this store. */
126 void (*destroy)(gpointer store);
128 /* Submit an operation (get/put/delete) to the storage layer to be
129 * performed asynchronously. */
130 void (*submit)(gpointer store, BlueSkyStoreAsync *async);
132 /* Clean up any implementation-private data in a BlueSkyStoreAsync. */
133 void (*cleanup)(gpointer store, BlueSkyStoreAsync *async);
135 /* Find the lexicographically-largest file starting with the specified
137 char * (*lookup_last)(gpointer store, const gchar *prefix);
138 } BlueSkyStoreImplementation;
140 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
143 char *bluesky_store_lookup_last(BlueSkyStore *store, const char *prefix);
144 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store);
145 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async);
146 void bluesky_store_async_ref(BlueSkyStoreAsync *async);
147 void bluesky_store_async_unref(BlueSkyStoreAsync *async);
148 void bluesky_store_async_wait(BlueSkyStoreAsync *async);
149 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
150 GFunc func, gpointer user_data);
151 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async);
152 void bluesky_store_async_submit(BlueSkyStoreAsync *async);
153 void bluesky_store_sync(BlueSkyStore *store);
155 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
156 BlueSkyStoreAsync *async);
158 void bluesky_inode_start_sync(BlueSkyInode *inode);
160 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
161 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
162 BlueSkyStoreAsync *barrier);
163 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
165 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items);
166 void bluesky_file_drop_cached(BlueSkyInode *inode);
168 /* Writing of data to the cloud in log segments and tracking the location of
169 * various pieces of data (both where in the cloud and where cached locally).
181 } BlueSkyCloudPointer;
184 LOGTYPE_INVALID = -1,
188 LOGTYPE_INODE_MAP = 3,
189 LOGTYPE_CHECKPOINT = 4,
190 } BlueSkyCloudLogType;
192 /* Headers that go on items in local log segments and cloud log segments. */
194 uint32_t magic; // HEADER_MAGIC
195 uint8_t type; // Object type + '0'
196 uint32_t offset; // Starting byte offset of the log header
197 uint32_t size1; // Size of the data item (bytes)
200 uint64_t inum; // Inode which owns this data, if any
201 BlueSkyCloudID id; // Object identifier
202 } __attribute__((packed));
205 uint32_t magic; // FOOTER_MAGIC
206 uint32_t crc; // Computed from log_header to log_footer.magic
207 } __attribute__((packed));
209 struct cloudlog_header {
214 uint32_t size1, size2, size3;
215 } __attribute__((packed));
217 #define JOURNAL_MAGIC "\nLog"
218 #define CLOUDLOG_MAGIC "AgI-"
220 /* A record which tracks an object which has been written to a local log,
221 * cached, locally, and/or written to the cloud. */
222 #define CLOUDLOG_JOURNAL 0x01
223 #define CLOUDLOG_CLOUD 0x02
224 #define CLOUDLOG_CACHE 0x04
225 struct _BlueSkyCloudLog {
232 BlueSkyCloudLogType type;
234 // Bitmask of CLOUDLOG_* flags indicating where the object exists.
236 int pending_read, pending_write;
238 // A stable identifier for the object (only changes when authenticated data
239 // is written out, but stays the same when the in-cloud cleaner relocates
243 // The inode which owns this data, if any, and an offset.
247 // The size of encrypted object data, not including any headers
250 // The location of the object in the cloud, if available.
251 BlueSkyCloudPointer location;
253 // TODO: Location in journal/cache
254 int log_seq, log_offset, log_size;
256 // Pointers to other objects. Each link counts towards the reference count
257 // of the pointed-to object. To avoid memory leaks there should be no
258 // cycles in the reference graph.
261 // Serialized data, if available in memory (otherwise NULL), and a lock
262 // count which tracks if there are users that require the data to be kept
268 /* Serialize objects into a log segment to be written to the cloud. */
269 struct _BlueSkyCloudLogState {
271 BlueSkyCloudPointer location;
273 GSList *writeback_list; // Items which are being serialized right now
274 GList *pending_segments; // Segments which are being uploaded now
277 gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b);
278 guint bluesky_cloudlog_hash(gconstpointer a);
279 BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs, const BlueSkyCloudID *id);
280 gchar *bluesky_cloudlog_id_to_string(BlueSkyCloudID id);
281 BlueSkyCloudID bluesky_cloudlog_id_from_string(const gchar *idstr);
282 void bluesky_cloudlog_ref(BlueSkyCloudLog *log);
283 void bluesky_cloudlog_unref(BlueSkyCloudLog *log);
284 void bluesky_cloudlog_stats_update(BlueSkyCloudLog *log, int type);
285 void bluesky_cloudlog_sync(BlueSkyCloudLog *log);
286 void bluesky_cloudlog_insert(BlueSkyCloudLog *log);
287 BlueSkyCloudLog *bluesky_cloudlog_get(BlueSkyFS *fs, BlueSkyCloudID id);
288 void bluesky_cloudlog_fetch(BlueSkyCloudLog *log);
289 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
291 void bluesky_cloudlog_flush(BlueSkyFS *fs);
293 /* Logging infrastructure for ensuring operations are persistently recorded to
295 #define BLUESKY_CRC32C_SEED (~(uint32_t)0)
296 #define BLUESKY_CRC32C_VALIDATOR ((uint32_t)0xb798b438UL)
297 uint32_t crc32c(uint32_t crc, const char *buf, unsigned int length);
298 uint32_t crc32c_finalize(uint32_t crc);
308 /* The currently-open log file. */
309 BlueSkyCacheFile *current_log;
311 /* Cache of log segments which have been memory-mapped. */
313 GHashTable *mmap_cache;
315 /* A count of the disk space consumed (in 1024-byte units) by all files
316 * tracked by mmap_cache (whether mapped or not, actually). */
319 /* The smallest journal sequence number which may still contain data that
320 * must be preserved (since it it not yet in the cloud). */
321 int journal_watermark;
324 /* An object for tracking log files which are stored locally--either the
325 * journal for filesystem consistency or log segments which have been fetched
326 * back from cloud storage. */
327 struct _BlueSkyCacheFile {
331 int type; // Only one of CLOUDLOG_{JOURNAL,CLOUD}
334 char *filename; // Local filename, relateive to log directory
335 gint mapcount; // References to the mmaped data
336 const char *addr; // May be null if data is not mapped in memory
340 gboolean fetching, ready; // Cloud data: downloading or ready for use
341 int64_t atime; // Access time, for cache management
344 BlueSkyLog *bluesky_log_new(const char *log_directory);
345 void bluesky_log_item_submit(BlueSkyCloudLog *item, BlueSkyLog *log);
346 void bluesky_log_finish_all(GList *log_items);
347 BlueSkyRCStr *bluesky_log_map_object(BlueSkyFS *fs, int log_dir, int log_seq,
348 int log_offset, int log_size);
349 void bluesky_mmap_unref(BlueSkyCacheFile *mmap);
350 void bluesky_cachefile_unref(BlueSkyCacheFile *cachefile);
352 BlueSkyCacheFile *bluesky_cachefile_lookup(BlueSkyFS *fs,
353 int clouddir, int log_seq);
354 void bluesky_cachefile_gc(BlueSkyFS *fs);
356 void bluesky_replay(BlueSkyFS *fs);
358 /* Used to track log segments that are being written to the cloud. */
367 /***** Inode map management *****/
369 /* Mapping information for a single inode number. These are grouped together
370 * into InodeMapRange objects. */
374 /* The ID of the most recent version of the inode. */
377 /* The location where that version is written in the cloud. */
378 BlueSkyCloudPointer location;
380 /* If the cloud log entry exists in memory, then a pointer to it, otherwise
382 BlueSkyCloudLog *item;
386 /* Starting and ending inode number values that fall in this section.
387 * Endpoint values are inclusive. */
390 /* A sorted list (by inode number) of InodeMapEntry objects. */
391 GSequence *map_entries;
393 /* The serialized version of the inode map data. */
394 BlueSkyCloudLog *serialized;
396 /* Have there been changes that require writing this section out again? */
400 InodeMapEntry *bluesky_inode_map_lookup(GSequence *inode_map, uint64_t inum,
402 BlueSkyCloudLog *bluesky_inode_map_serialize(BlueSkyFS *fs);
404 gboolean bluesky_checkpoint_load(BlueSkyFS *fs);