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)
83 BlueSkyCloudLog *log = g_new0(BlueSkyCloudLog, 1);
85 log->lock = g_mutex_new();
86 log->cond = g_cond_new();
88 log->type = LOGTYPE_UNKNOWN;
89 log->id = bluesky_cloudlog_new_id();
90 log->links = g_array_new(FALSE, TRUE, sizeof(BlueSkyCloudLog *));
91 g_atomic_int_set(&log->refcount, 1);
96 /* The reference held by the hash table does not count towards the reference
97 * count. When a new object is created, it initially has a reference count of
98 * 1 for the creator, and similarly fetching an item from the hash table will
99 * also create a reference. If the reference count drops to zero,
100 * bluesky_cloudlog_unref attempts to remove the object from the hash
101 * table--but there is a potential race since another thread might read the
102 * object from the hash table at the same time. So an object with a reference
103 * count of zero may still be resurrected, in which case we need to abort the
104 * destruction. Once the object is gone from the hash table, and if the
105 * reference count is still zero, it can actually be deleted. */
106 void bluesky_cloudlog_ref(BlueSkyCloudLog *log)
111 g_atomic_int_inc(&log->refcount);
114 void bluesky_cloudlog_unref(BlueSkyCloudLog *log)
119 if (g_atomic_int_dec_and_test(&log->refcount)) {
120 BlueSkyFS *fs = log->fs;
122 g_mutex_lock(fs->lock);
123 if (g_atomic_int_get(&log->refcount) > 0) {
124 g_mutex_unlock(fs->lock);
128 g_hash_table_remove(fs->locations, &log->id);
129 g_mutex_unlock(fs->lock);
131 log->type = LOGTYPE_INVALID;
132 g_mutex_free(log->lock);
133 g_cond_free(log->cond);
134 for (int i = 0; i < log->links->len; i++) {
135 BlueSkyCloudLog *c = g_array_index(log->links,
136 BlueSkyCloudLog *, i);
137 bluesky_cloudlog_unref(c);
139 g_array_unref(log->links);
140 bluesky_string_unref(log->data);
145 /* Start a write of the object to the local log. */
146 void bluesky_cloudlog_sync(BlueSkyCloudLog *log)
148 bluesky_log_item_submit(log, log->fs->log);
151 /* Add the given entry to the global hash table containing cloud log entries.
152 * Takes ownership of the caller's reference. */
153 void bluesky_cloudlog_insert(BlueSkyCloudLog *log)
155 g_mutex_lock(log->fs->lock);
156 g_hash_table_insert(log->fs->locations, &log->id, log);
157 g_mutex_unlock(log->fs->lock);
164 uint32_t pointer_count;
165 } __attribute__((packed));
169 BlueSkyCloudPointer location;
170 } __attribute__((packed));
174 struct logref refs[0];
177 /* Ensure that a cloud log item is loaded in memory, and if not read it in.
178 * TODO: Make asynchronous, and make this also fetch from the cloud. Right now
179 * we only read from the log. Log item must be locked. */
180 void bluesky_cloudlog_fetch(BlueSkyCloudLog *log)
182 if (log->data != NULL)
185 g_assert(log->location_flags & CLOUDLOG_JOURNAL);
187 log->data = bluesky_log_map_object(log->fs->log, log->log_seq,
188 log->log_offset, log->log_size);
190 g_cond_broadcast(log->cond);
193 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
196 BlueSkyCloudLogState *state = fs->log_state;
198 if (log->location_flags & CLOUDLOG_CLOUD) {
199 return log->location;
202 for (int i = 0; i < log->links->len; i++) {
203 BlueSkyCloudLog *ref = g_array_index(log->links,
204 BlueSkyCloudLog *, i);
206 bluesky_cloudlog_serialize(ref, fs);
209 g_mutex_lock(log->lock);
210 bluesky_cloudlog_fetch(log);
211 g_assert(log->data != NULL);
213 log->location = state->location;
214 log->location.offset = state->data->len;
216 = sizeof(struct log_header) + sizeof(BlueSkyCloudID) * 0
219 struct log_header header;
220 memcpy(header.magic, "AgI ", 4);
221 header.size = GUINT32_TO_LE(log->location.size);
223 header.pointer_count = GUINT32_TO_LE(0);
225 g_string_append_len(state->data, (const char *)&header, sizeof(header));
226 g_string_append_len(state->data, log->data->data, log->data->len);
228 log->location_flags |= CLOUDLOG_CLOUD;
229 g_mutex_unlock(log->lock);
231 if (state->data->len > CLOUDLOG_SEGMENT_SIZE)
232 bluesky_cloudlog_flush(fs);
234 return log->location;
237 static void find_inodes(gpointer key, gpointer value, gpointer user_data)
239 BlueSkyCloudLogState *state = (BlueSkyCloudLogState *)user_data;
240 BlueSkyCloudLog *item = (BlueSkyCloudLog *)value;
242 if (item->type != LOGTYPE_INODE)
245 bluesky_cloudlog_ref(item);
246 state->inode_list = g_list_prepend(state->inode_list, item);
249 /* Finish up a partially-written cloud log segment and flush it to storage. */
250 void bluesky_cloudlog_flush(BlueSkyFS *fs)
252 BlueSkyCloudLogState *state = fs->log_state;
253 if (state->data == NULL || state->data->len == 0)
256 /* TODO: Append some type of commit record to the log segment? */
258 g_print("Serializing %zd bytes of data to cloud\n", state->data->len);
260 BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
261 async->op = STORE_OP_PUT;
262 async->key = g_strdup_printf("log-%08d-%08d",
263 state->location.directory,
264 state->location.sequence);
265 async->data = bluesky_string_new_from_gstring(state->data);
266 bluesky_store_async_submit(async);
267 bluesky_store_async_wait(async);
268 bluesky_store_async_unref(async);
270 state->location.sequence++;
271 state->location.offset = 0;
272 state->data = g_string_new("");
275 void bluesky_cloudlog_write_log(BlueSkyFS *fs)
277 BlueSkyCloudLogState *state = fs->log_state;
278 if (state->data == NULL)
279 state->data = g_string_new("");
281 g_mutex_lock(fs->lock);
282 g_hash_table_foreach(fs->locations, find_inodes, state);
283 g_mutex_unlock(fs->lock);
285 while (state->inode_list != NULL) {
286 BlueSkyCloudLog *log = (BlueSkyCloudLog *)state->inode_list->data;
287 bluesky_cloudlog_serialize(log, fs);
288 bluesky_cloudlog_unref(log);
289 state->inode_list = g_list_delete_link(state->inode_list,
292 if (state->data->len > CLOUDLOG_SEGMENT_SIZE)
293 bluesky_cloudlog_flush(fs);
296 bluesky_cloudlog_flush(fs);