+}
+
+/* Start writeback of an inode and all associated data. */
+void bluesky_inode_start_sync(BlueSkyInode *inode)
+{
+ GList *log_items = NULL;
+
+ if (inode->type == BLUESKY_REGULAR)
+ bluesky_file_flush(inode, &log_items);
+
+ BlueSkyCloudLog *cloudlog = bluesky_serialize_inode(inode);
+
+ bluesky_cloudlog_unref(inode->committed_item);
+ inode->committed_item = cloudlog;
+
+ bluesky_cloudlog_sync(cloudlog);
+ bluesky_cloudlog_ref(cloudlog);
+ log_items = g_list_prepend(log_items, cloudlog);
+
+ /* Wait for all log items to be committed to disk. */
+ bluesky_log_finish_all(log_items);
+
+ /* Mark the inode as clean */
+ inode->change_commit = inode->change_count;
+ inode->change_time = 0;
+ g_mutex_lock(inode->fs->lock);
+ bluesky_list_unlink(&inode->fs->unlogged_list, inode->unlogged_list);
+ inode->unlogged_list = NULL;
+
+ /* Since a new version of the inode has been written to the log, also
+ * schedule a future flush of the new data to cloud storage. */
+ bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
+ inode->dirty_list = bluesky_list_prepend(&inode->fs->dirty_list, inode);
+ inode->change_cloud = inode->change_count;
+
+ g_mutex_unlock(inode->fs->lock);
+}
+
+/* Write back an inode and all associated data and wait for completion. Inode
+ * should already be locked. */
+void bluesky_inode_do_sync(BlueSkyInode *inode)
+{
+ if (bluesky_verbose) {
+ g_log("bluesky/inode", G_LOG_LEVEL_DEBUG,
+ "Synchronous writeback for inode %"PRIu64"...", inode->inum);
+ }
+ bluesky_inode_start_sync(inode);
+ if (bluesky_verbose) {
+ g_log("bluesky/inode", G_LOG_LEVEL_DEBUG,
+ "Writeback for inode %"PRIu64" complete", inode->inum);
+ }
+}
+
+static void inode_fetch_task(gpointer a, gpointer b)
+{
+ BlueSkyInode *inode = (BlueSkyInode *)a;
+
+ bluesky_profile_set((BlueSkyProfile *)inode->private_data);
+
+ BlueSkyCloudLog *item = inode->committed_item;
+ inode->committed_item = NULL;
+ g_print("Completing fetch of inode %"PRIu64"...\n", 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;
+
+ /* Non-portable behavior: We take the inode lock here, and release it in
+ * the fetching thread. This works with the default Linux pthreads
+ * implementation but is not guaranteed. */
+
+ 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);
+
+ inode->committed_item = entry->item;
+ bluesky_cloudlog_ref(entry->item);
+ bluesky_insert_inode(fs, inode);
+
+ inode->private_data = bluesky_profile_get();
+ g_thread_pool_push(fs->inode_fetch_thread_pool, inode, NULL);