+ g_mutex_lock(fs->lock);
+ flushd_check_wakeup(fs);
+ }
+
+ g_cond_broadcast(fs->flushd_cond);
+
+ g_mutex_unlock(fs->lock);
+}
+
+/* Try to flush dirty data to the cloud. This will take a snapshot of the
+ * entire filesystem (though only point-in-time consistent for isolated inodes
+ * and not the filesystem as a whole) and ensure all data is written to the
+ * cloud. When the write completes, we will allow old journal segments (those
+ * that were fully written _before_ the snapshot process started) to be garbage
+ * collected. Newer journal segments can't be collected yet since they may
+ * still contain data which has not been written persistently to the cloud. */
+static void flushd_cloud(BlueSkyFS *fs)
+{
+ g_mutex_lock(fs->lock);
+
+ /* TODO: Locking? Since we're reading a single variable this is probably
+ * atomic but a lock could be safer. */
+ BlueSkyCloudLog *marker = bluesky_log_get_commit_point(fs);
+ int journal_seq_start = fs->log->seq_num;
+
+ while (1) {
+ BlueSkyInode *inode;
+ if (fs->dirty_list.prev == NULL)
+ break;
+ inode = fs->dirty_list.prev->data;
+
+ if (bluesky_verbose) {
+ g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
+ "Flushing inode %"PRIu64" to cloud", inode->inum);
+ }
+
+ bluesky_inode_ref(inode);
+
+ g_mutex_unlock(fs->lock);
+
+ g_mutex_lock(inode->lock);
+ g_assert(inode->change_cloud == inode->change_commit);
+ g_mutex_lock(fs->lock);
+ bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
+ inode->dirty_list = NULL;
+ g_mutex_unlock(fs->lock);
+
+ BlueSkyCloudLog *log = inode->committed_item;
+ inode->committed_item = NULL;
+ g_mutex_unlock(inode->lock);
+
+ if (log != NULL)
+ bluesky_cloudlog_serialize(log, fs);
+ bluesky_inode_unref(inode);
+ bluesky_cloudlog_unref(log);
+