- uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
- if (elapsed < WRITEBACK_DELAY) {
- /* Give a bit more time before starting writeback. */
+ bluesky_inode_start_sync(inode);
+}
+
+/* Try to flush dirty data to disk, either due to memory pressure or due to
+ * timeouts. */
+static void flushd_dirty(BlueSkyFS *fs)
+{
+ int64_t start_time = bluesky_get_current_time();
+ g_mutex_lock(fs->lock);
+
+ 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,
+ "Considering flushing inode %"PRIu64, inode->inum);
+ }
+
+ /* Stop processing dirty inodes if we both have enough memory available
+ * and the oldest inode is sufficiently new that it need not be flushed
+ * out. */
+ uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
+ if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
+ && elapsed < WRITEBACK_DELAY)
+ break;
+ if (inode->change_time > start_time)
+ break;
+
+ bluesky_inode_ref(inode);
+
+ g_mutex_unlock(fs->lock);
+
+ g_mutex_lock(inode->lock);
+ flushd_dirty_inode(inode);