- GString *buf = g_string_new("");
- bluesky_serialize_superblock(buf, fs);
- BlueSkyRCStr *data = bluesky_string_new_from_gstring(buf);
-
- BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
- async->op = STORE_OP_PUT;
- async->key = g_strdup("superblock");
- async->data = data;
- bluesky_store_async_submit(async);
- bluesky_store_async_unref(async);
-
- bluesky_store_sync(fs->store);
+ BlueSkyCloudLog *item = inode->committed_item;
+ inode->committed_item = NULL;
+ g_print("Completing fetch of inode %"PRIu64"...", inode->inum);
+
+ g_mutex_lock(item->lock);
+ bluesky_cloudlog_fetch(item);
+ if (!bluesky_deserialize_inode(inode, item))
+ g_print("Error deserializing inode %"PRIu64"\n", inode->inum);
+ g_mutex_unlock(item->lock);
+
+ inode->access_time = bluesky_get_current_time();
+ g_mutex_lock(inode->fs->lock);
+ bluesky_list_unlink(&inode->fs->accessed_list, inode->accessed_list);
+ inode->accessed_list = bluesky_list_prepend(&inode->fs->accessed_list, inode);
+ g_mutex_unlock(inode->fs->lock);
+
+ g_mutex_unlock(inode->lock);
+ bluesky_cloudlog_unref(item);
+ bluesky_inode_unref(inode);
+}
+
+/* Fetch an inode from stable storage. The fetch can be performed
+ * asynchronously: the in-memory inode is allocated, but not filled with data
+ * immediately. It is kept locked until it has been filled in, so any users
+ * should try to acquire the lock on the inode before accessing any data. The
+ * fs lock must be held. */
+void bluesky_inode_fetch(BlueSkyFS *fs, uint64_t inum)
+{
+ InodeMapEntry *entry = bluesky_inode_map_lookup(fs->inode_map, inum, 0);
+ if (entry == NULL)
+ return;
+
+ BlueSkyInode *inode = bluesky_new_inode(inum, fs, BLUESKY_PENDING);
+ inode->change_count = 0;
+ bluesky_inode_ref(inode); // Extra ref held by fetching process
+ g_mutex_lock(inode->lock);
+ bluesky_insert_inode(fs, inode);
+ inode->committed_item = entry->item;
+ bluesky_cloudlog_ref(entry->item);
+
+ /* TODO: Thread pool or other better async method. */
+ g_thread_create((GThreadFunc)complete_inode_fetch,
+ (gpointer)inode, FALSE, NULL);