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>
14 #include "bluesky-private.h"
16 // Rough size limit for a log segment. This is not a firm limit and there are
17 // no absolute guarantees on the size of a log segment.
18 #define CLOUDLOG_SEGMENT_SIZE (4 << 20)
20 BlueSkyCloudID bluesky_cloudlog_new_id()
23 bluesky_crypt_random_bytes((uint8_t *)&id.bytes, sizeof(id));
27 gchar *bluesky_cloudlog_id_to_string(BlueSkyCloudID id)
29 char buf[sizeof(BlueSkyCloudID) * 2 + 1];
32 for (int i = 0; i < sizeof(BlueSkyCloudID); i++) {
33 sprintf(&buf[2*i], "%02x", (uint8_t)(id.bytes[i]));
39 BlueSkyCloudID bluesky_cloudlog_id_from_string(const gchar *idstr)
42 memset(&id, 0, sizeof(id));
43 for (int i = 0; i < 2*sizeof(BlueSkyCloudID); i++) {
46 g_warning("Short cloud id: %s\n", idstr);
50 if (c >= '0' && c <= '9')
52 else if (c >= 'a' && c <= 'f')
55 g_warning("Bad character in cloud id: %s\n", idstr);
56 id.bytes[i / 2] += val << (i % 2 ? 0 : 4);
61 gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b)
63 BlueSkyCloudID *id1 = (BlueSkyCloudID *)a, *id2 = (BlueSkyCloudID *)b;
65 return memcmp(id1, id2, sizeof(BlueSkyCloudID)) == 0;
68 guint bluesky_cloudlog_hash(gconstpointer a)
70 BlueSkyCloudID *id = (BlueSkyCloudID *)a;
72 // Assume that bits in the ID are randomly chosen so that any subset of the
73 // bits can be used as a hash key.
74 return *(guint *)(&id->bytes);
77 /* Formatting of cloud log segments. This handles grouping items together
78 * before writing a batch to the cloud, handling indirection through items like
79 * the inode map, etc. */
81 BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs, const BlueSkyCloudID *id)
83 BlueSkyCloudLog *log = g_new0(BlueSkyCloudLog, 1);
85 log->lock = g_mutex_new();
86 log->cond = g_cond_new();
88 log->type = LOGTYPE_UNKNOWN;
90 memcpy(&log->id, id, sizeof(BlueSkyCloudID));
92 log->id = bluesky_cloudlog_new_id();
93 log->links = g_array_new(FALSE, TRUE, sizeof(BlueSkyCloudLog *));
94 g_atomic_int_set(&log->refcount, 1);
99 /* Helper function for updating memory usage statistics for a filesystem (the
100 * cache_log_* variables). This will increment (type=1) or decrement (type=-1)
101 * the counter associated with the current state of the cloud log item. The
102 * item should be locked or otherwise protected from concurrent access. */
103 void bluesky_cloudlog_stats_update(BlueSkyCloudLog *log, int type)
105 BlueSkyFS *fs = log->fs;
107 if (log->location_flags & CLOUDLOG_CLOUD) {
108 g_atomic_int_add(&fs->cache_log_cloud, type);
109 } else if (log->location_flags & CLOUDLOG_JOURNAL) {
110 g_atomic_int_add(&fs->cache_log_journal, type);
111 } else if (log->pending_write & CLOUDLOG_JOURNAL) {
112 g_atomic_int_add(&fs->cache_log_journal, type);
113 } else if (log->data != NULL) {
114 g_atomic_int_add(&fs->cache_log_dirty, type);
118 /* The reference held by the hash table does not count towards the reference
119 * count. When a new object is created, it initially has a reference count of
120 * 1 for the creator, and similarly fetching an item from the hash table will
121 * also create a reference. If the reference count drops to zero,
122 * bluesky_cloudlog_unref attempts to remove the object from the hash
123 * table--but there is a potential race since another thread might read the
124 * object from the hash table at the same time. So an object with a reference
125 * count of zero may still be resurrected, in which case we need to abort the
126 * destruction. Once the object is gone from the hash table, and if the
127 * reference count is still zero, it can actually be deleted. */
128 void bluesky_cloudlog_ref(BlueSkyCloudLog *log)
133 g_atomic_int_inc(&log->refcount);
136 void bluesky_cloudlog_unref(BlueSkyCloudLog *log)
141 if (g_atomic_int_dec_and_test(&log->refcount)) {
142 BlueSkyFS *fs = log->fs;
144 g_mutex_lock(fs->lock);
145 if (g_atomic_int_get(&log->refcount) > 0) {
146 g_mutex_unlock(fs->lock);
150 g_hash_table_remove(fs->locations, &log->id);
151 g_mutex_unlock(fs->lock);
153 bluesky_cloudlog_stats_update(log, -1);
154 log->type = LOGTYPE_INVALID;
155 g_mutex_free(log->lock);
156 g_cond_free(log->cond);
157 for (int i = 0; i < log->links->len; i++) {
158 BlueSkyCloudLog *c = g_array_index(log->links,
159 BlueSkyCloudLog *, i);
160 bluesky_cloudlog_unref(c);
162 g_array_unref(log->links);
163 bluesky_string_unref(log->data);
168 /* Start a write of the object to the local log. */
169 void bluesky_cloudlog_sync(BlueSkyCloudLog *log)
171 bluesky_log_item_submit(log, log->fs->log);
174 /* Add the given entry to the global hash table containing cloud log entries.
175 * Takes ownership of the caller's reference. */
176 void bluesky_cloudlog_insert(BlueSkyCloudLog *log)
178 g_mutex_lock(log->fs->lock);
179 g_hash_table_insert(log->fs->locations, &log->id, log);
180 g_mutex_unlock(log->fs->lock);
187 uint32_t pointer_count;
188 } __attribute__((packed));
192 BlueSkyCloudPointer location;
193 } __attribute__((packed));
197 struct logref refs[0];
200 /* Ensure that a cloud log item is loaded in memory, and if not read it in.
201 * TODO: Make asynchronous, and make this also fetch from the cloud. Right now
202 * we only read from the log. Log item must be locked. */
203 void bluesky_cloudlog_fetch(BlueSkyCloudLog *log)
205 if (log->data != NULL)
208 if ((log->location_flags | log->pending_write) & CLOUDLOG_JOURNAL) {
209 bluesky_cloudlog_stats_update(log, -1);
210 log->data = bluesky_log_map_object(log->fs, -1, log->log_seq,
211 log->log_offset, log->log_size);
212 bluesky_cloudlog_stats_update(log, 1);
215 if (log->data == NULL && (log->location_flags & CLOUDLOG_CLOUD)) {
216 log->location_flags &= ~CLOUDLOG_JOURNAL;
217 bluesky_cloudlog_stats_update(log, -1);
218 log->data = bluesky_log_map_object(log->fs, log->location.directory,
219 log->location.sequence,
220 log->location.offset,
222 bluesky_cloudlog_stats_update(log, 1);
225 if (log->data == NULL) {
226 g_error("Unable to fetch cloudlog entry!");
229 g_cond_broadcast(log->cond);
232 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
235 BlueSkyCloudLogState *state = fs->log_state;
237 if ((log->location_flags | log->pending_write) & CLOUDLOG_CLOUD) {
238 return log->location;
241 for (int i = 0; i < log->links->len; i++) {
242 BlueSkyCloudLog *ref = g_array_index(log->links,
243 BlueSkyCloudLog *, i);
245 bluesky_cloudlog_serialize(ref, fs);
248 g_mutex_lock(log->lock);
249 bluesky_cloudlog_fetch(log);
250 g_assert(log->data != NULL);
252 bluesky_cloudlog_stats_update(log, -1);
254 /* TODO: Right now offset/size are set to the raw data, but we should add
255 * header parsing to the code which loads objects back in. */
256 log->location = state->location;
257 log->location.offset = state->data->len + sizeof(struct log_header);
258 log->location.size = log->data->len;
259 /* = sizeof(struct log_header) + sizeof(BlueSkyCloudID) * 0
262 struct log_header header;
263 memcpy(header.magic, "AgI ", 4);
264 header.size = GUINT32_TO_LE(log->location.size);
266 header.pointer_count = GUINT32_TO_LE(0);
268 g_string_append_len(state->data, (const char *)&header, sizeof(header));
269 g_string_append_len(state->data, log->data->data, log->data->len);
271 /* If the object we flushed was an inode, update the inode map. */
272 if (log->type == LOGTYPE_INODE) {
273 g_mutex_lock(fs->lock);
274 InodeMapEntry *entry = bluesky_inode_map_lookup(fs->inode_map,
277 entry->location = log->location;
278 g_mutex_unlock(fs->lock);
281 /* TODO: We should mark the objects as committed on the cloud until the
282 * data is flushed and acknowledged. */
283 log->pending_write |= CLOUDLOG_CLOUD;
284 bluesky_cloudlog_stats_update(log, 1);
285 state->writeback_list = g_slist_prepend(state->writeback_list, log);
286 bluesky_cloudlog_ref(log);
287 g_mutex_unlock(log->lock);
289 if (state->data->len > CLOUDLOG_SEGMENT_SIZE)
290 bluesky_cloudlog_flush(fs);
292 return log->location;
295 static void cloudlog_flush_complete(BlueSkyStoreAsync *async,
296 SerializedRecord *record)
298 g_print("Write of %s to cloud complete, status = %d\n",
299 async->key, async->result);
301 g_mutex_lock(record->lock);
302 if (async->result >= 0) {
303 while (record->items != NULL) {
304 BlueSkyCloudLog *item = (BlueSkyCloudLog *)record->items->data;
305 g_mutex_lock(item->lock);
306 bluesky_cloudlog_stats_update(item, -1);
307 item->pending_write &= ~CLOUDLOG_CLOUD;
308 item->location_flags |= CLOUDLOG_CLOUD;
309 bluesky_cloudlog_stats_update(item, 1);
310 g_mutex_unlock(item->lock);
311 bluesky_cloudlog_unref(item);
313 record->items = g_slist_delete_link(record->items, record->items);
316 bluesky_string_unref(record->data);
318 g_slist_free(record->items);
319 record->items = NULL;
320 record->complete = TRUE;
321 g_cond_broadcast(record->cond);
323 g_print("Write should be resubmitted...\n");
325 BlueSkyStoreAsync *async2 = bluesky_store_async_new(async->store);
326 async2->op = STORE_OP_PUT;
327 async2->key = g_strdup(async->key);
328 async2->data = record->data;
329 bluesky_string_ref(record->data);
330 bluesky_store_async_submit(async2);
331 bluesky_store_async_add_notifier(async2,
332 (GFunc)cloudlog_flush_complete,
334 bluesky_store_async_unref(async2);
336 g_mutex_unlock(record->lock);
339 /* Finish up a partially-written cloud log segment and flush it to storage. */
340 void bluesky_cloudlog_flush(BlueSkyFS *fs)
342 BlueSkyCloudLogState *state = fs->log_state;
343 if (state->data == NULL || state->data->len == 0)
346 /* TODO: Append some type of commit record to the log segment? */
348 g_print("Serializing %zd bytes of data to cloud\n", state->data->len);
349 SerializedRecord *record = g_new0(SerializedRecord, 1);
350 record->data = bluesky_string_new_from_gstring(state->data);
351 record->items = state->writeback_list;
352 record->lock = g_mutex_new();
353 record->cond = g_cond_new();
354 state->writeback_list = NULL;
356 BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
357 async->op = STORE_OP_PUT;
358 async->key = g_strdup_printf("log-%08d-%08d",
359 state->location.directory,
360 state->location.sequence);
361 async->data = record->data;
362 bluesky_string_ref(record->data);
363 bluesky_store_async_submit(async);
364 bluesky_store_async_add_notifier(async,
365 (GFunc)cloudlog_flush_complete,
367 bluesky_store_async_unref(async);
369 state->pending_segments = g_list_prepend(state->pending_segments, record);
371 state->location.sequence++;
372 state->location.offset = 0;
373 state->data = g_string_new("");