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 /* The locations hash table in the file system is used to map objects to their locations. Objects are named using 128- */
21 BlueSkyCloudPointer *cloud_loc;
22 } BlueSkyLocationEntry;
24 BlueSkyCloudID bluesky_cloudlog_new_id()
27 bluesky_crypt_random_bytes((uint8_t *)&id.bytes, sizeof(id));
31 gchar *bluesky_cloudlog_id_to_string(BlueSkyCloudID id)
33 char buf[sizeof(BlueSkyCloudID) * 2 + 1];
36 for (int i = 0; i < sizeof(BlueSkyCloudID); i++) {
37 sprintf(&buf[2*i], "%02x", (uint8_t)(id.bytes[i]));
43 BlueSkyCloudID bluesky_cloudlog_id_from_string(const gchar *idstr)
46 memset(&id, 0, sizeof(id));
47 for (int i = 0; i < 2*sizeof(BlueSkyCloudID); i++) {
50 g_warning("Short cloud id: %s\n", idstr);
54 if (c >= '0' && c <= '9')
56 else if (c >= 'a' && c <= 'f')
59 g_warning("Bad character in cloud id: %s\n", idstr);
60 id.bytes[i / 2] += val << (i % 2 ? 0 : 4);
65 gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b)
67 BlueSkyCloudID *id1 = (BlueSkyCloudID *)a, *id2 = (BlueSkyCloudID *)b;
69 return memcmp(id1, id2, sizeof(BlueSkyCloudID)) == 0;
72 guint bluesky_cloudlog_hash(gconstpointer a)
74 BlueSkyCloudID *id = (BlueSkyCloudID *)a;
76 // Assume that bits in the ID are randomly chosen so that any subset of the
77 // bits can be used as a hash key.
78 return *(guint *)(&id->bytes);
81 /* Formatting of cloud log segments. This handles grouping items together
82 * before writing a batch to the cloud, handling indirection through items like
83 * the inode map, etc. */
85 BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs)
87 BlueSkyCloudLog *log = g_new0(BlueSkyCloudLog, 1);
90 log->type = LOGTYPE_UNKNOWN;
91 log->id = bluesky_cloudlog_new_id();
92 log->pointers = g_array_new(FALSE, TRUE, sizeof(BlueSkyCloudID));
93 g_atomic_int_set(&log->refcount, 1);
98 void bluesky_cloudlog_ref(BlueSkyCloudLog *log)
100 g_atomic_int_inc(&log->refcount);
103 void bluesky_cloudlog_unref(BlueSkyCloudLog *log)
105 if (g_atomic_int_dec_and_test(&log->refcount)) {
106 g_print("Cloud log refcount dropped to zero.\n");
110 /* Start a write of the object to the local log. */
111 BlueSkyLogItem *bluesky_cloudlog_sync(BlueSkyCloudLog *log)
113 BlueSkyLogItem *log_item = bluesky_log_item_new();
114 log_item->key = bluesky_cloudlog_id_to_string(log->id);
115 log_item->data = log->data;
116 bluesky_string_ref(log->data);
117 bluesky_log_item_submit(log_item, log->fs->log);
121 /* Add the given entry to the global hash table containing cloud log entries.
122 * Takes ownership of the caller's reference. */
123 void bluesky_cloudlog_insert(BlueSkyCloudLog *log)
125 g_mutex_lock(log->fs->lock);
126 g_hash_table_insert(log->fs->locations, &log->id, log);
127 g_mutex_unlock(log->fs->lock);
134 uint32_t pointer_count;
135 } __attribute__((packed));
139 BlueSkyCloudPointer location;
140 } __attribute__((packed));
144 struct logref refs[0];
147 BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
148 BlueSkyCloudLogState *state)
150 if (log->location_flags & CLOUDLOG_CLOUD) {
151 return log->location;
154 g_print("Flushing object %s to cloud...\n",
155 bluesky_cloudlog_id_to_string(log->id));
157 for (int i = 0; i < log->pointers->len; i++) {
158 BlueSkyCloudID id = g_array_index(log->pointers, BlueSkyCloudID, i);
159 g_print(" ...checking reference %s...\n",
160 bluesky_cloudlog_id_to_string(id));
161 g_mutex_lock(log->fs->lock);
162 BlueSkyCloudLog *log2
163 = (BlueSkyCloudLog *)g_hash_table_lookup(log->fs->locations, &id);
165 g_mutex_unlock(log->fs->lock);
166 g_assert(log2 != NULL);
167 bluesky_cloudlog_serialize(log2, state);
170 g_assert(log->data != NULL);
172 log->location = state->location;
173 log->location.offset = state->data->len;
175 = sizeof(struct log_header) + sizeof(BlueSkyCloudID) * 0
178 struct log_header header;
179 memcpy(header.magic, "AgI ", 4);
180 header.size = GUINT32_TO_LE(log->location.size);
182 header.pointer_count = GUINT32_TO_LE(0);
184 g_string_append_len(state->data, (const char *)&header, sizeof(header));
185 g_string_append_len(state->data, log->data->data, log->data->len);
187 log->location_flags |= CLOUDLOG_CLOUD;
189 return log->location;
192 static void find_inodes(gpointer key, gpointer value, gpointer user_data)
194 BlueSkyCloudLogState *state = (BlueSkyCloudLogState *)user_data;
195 BlueSkyCloudLog *item = (BlueSkyCloudLog *)value;
197 if (item->type != LOGTYPE_INODE)
200 bluesky_cloudlog_ref(item);
201 state->inode_list = g_list_prepend(state->inode_list, item);
204 void bluesky_cloudlog_write_log(BlueSkyFS *fs)
206 g_print("Starting cloudlog write...\n");
208 BlueSkyCloudLogState *state = fs->log_state;
209 state->data = g_string_new("");
211 g_mutex_lock(fs->lock);
212 g_hash_table_foreach(fs->locations, find_inodes, state);
213 g_mutex_unlock(fs->lock);
215 while (state->inode_list != NULL) {
216 BlueSkyCloudLog *log = (BlueSkyCloudLog *)state->inode_list->data;
217 bluesky_cloudlog_serialize(log, state);
218 state->inode_list = g_list_delete_link(state->inode_list,
222 g_print("Serialized %zd bytes of data\n", state->data->len);
224 BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
225 async->op = STORE_OP_PUT;
226 async->key = g_strdup_printf("log-%08d-%08d",
227 state->location.directory,
228 state->location.sequence);
229 async->data = bluesky_string_new_from_gstring(state->data);
230 bluesky_store_async_submit(async);
231 bluesky_store_async_wait(async);
232 bluesky_store_async_unref(async);
235 state->location.sequence++;
236 state->location.offset = 0;