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 #define WRITEBACK_DELAY (20 * 1000000)
18 /* Filesystem caching and cache coherency. There are actually a couple of
19 * different tasks that are performed here:
20 * - Forcing data to the log if needed to reclaim memory or simply if the
21 * data has been dirty in memory long enough.
22 * - Writing batches of data to the cloud.
26 static void writeback_complete(gpointer a, gpointer i)
28 BlueSkyInode *inode = (BlueSkyInode *)i;
30 if (bluesky_verbose) {
31 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
32 "Writeback for inode %"PRIu64" complete", inode->inum);
35 g_mutex_lock(inode->lock);
37 inode->change_commit = inode->change_pending;
38 inode->change_pending = 0;
39 if (inode->change_count == inode->change_commit) {
40 /* If inode is no longer dirty... */
41 inode->change_time = 0;
42 g_mutex_lock(inode->fs->lock);
43 bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
44 inode->dirty_list = NULL;
45 g_mutex_unlock(inode->fs->lock);
48 g_mutex_unlock(inode->lock);
52 static void flushd_dirty_inode(BlueSkyInode *inode)
54 BlueSkyFS *fs = inode->fs;
56 g_mutex_lock(fs->lock);
57 bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
58 inode->dirty_list = NULL;
59 g_mutex_unlock(fs->lock);
61 /* Inode is clean; nothing to do. */
62 if (inode->change_count == inode->change_commit)
65 if (bluesky_verbose) {
66 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
67 "Starting flush of inode %"PRIu64, inode->inum);
70 bluesky_inode_start_sync(inode);
73 /* Try to flush dirty data to disk, either due to memory pressure or due to
75 static void flushd_dirty(BlueSkyFS *fs)
77 int64_t start_time = bluesky_get_current_time();
78 g_mutex_lock(fs->lock);
82 if (fs->dirty_list.prev == NULL)
84 inode = fs->dirty_list.prev->data;
86 if (bluesky_verbose) {
87 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
88 "Considering flushing inode %"PRIu64, inode->inum);
91 /* Stop processing dirty inodes if we both have enough memory available
92 * and the oldest inode is sufficiently new that it need not be flushed
94 uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
95 if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
96 && elapsed < WRITEBACK_DELAY)
98 if (inode->change_time > start_time)
101 bluesky_inode_ref(inode);
103 g_mutex_unlock(fs->lock);
105 g_mutex_lock(inode->lock);
106 flushd_dirty_inode(inode);
107 g_mutex_unlock(inode->lock);
108 bluesky_inode_unref(inode);
110 g_mutex_lock(fs->lock);
113 g_mutex_unlock(fs->lock);
116 /* Drop cached data for a given inode, if it is clean. inode must be locked. */
117 static void drop_caches(BlueSkyInode *inode)
119 if (inode->type == BLUESKY_REGULAR)
120 bluesky_file_drop_cached(inode);
123 /* Drop clean data from the cache if needed due to memory pressure. */
124 static void flushd_clean(BlueSkyFS *fs)
126 g_mutex_lock(fs->lock);
128 size_t inode_count = g_hash_table_size(fs->inodes);
132 while (inode_count-- > 0) {
134 if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
139 if (fs->accessed_list.prev == NULL)
141 inode = fs->accessed_list.prev->data;
143 if (bluesky_verbose) {
144 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
145 "Considering dropping cached data for inode %"PRIu64,
149 bluesky_inode_ref(inode);
151 g_mutex_unlock(fs->lock);
153 g_mutex_lock(inode->lock);
155 g_mutex_lock(fs->lock);
156 bluesky_list_unlink(&fs->accessed_list, inode->accessed_list);
157 inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
158 g_mutex_unlock(fs->lock);
162 g_mutex_unlock(inode->lock);
163 bluesky_inode_unref(inode);
165 g_mutex_lock(fs->lock);
168 g_mutex_unlock(fs->lock);
171 /* Run the flush daemon for a single iteration, though if it is already
172 * executing returns immediately. */
173 static gpointer flushd_task(BlueSkyFS *fs)
175 if (!g_mutex_trylock(fs->flushd_lock))
178 bluesky_cloudlog_write_log(fs);
180 g_mutex_unlock(fs->flushd_lock);
185 void bluesky_flushd_invoke(BlueSkyFS *fs)
187 g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
190 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
192 if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
193 && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
196 if (bluesky_verbose) {
197 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
198 "Too much data; invoking flushd: dirty=%d total=%d",
199 g_atomic_int_get(&fs->cache_dirty),
200 g_atomic_int_get(&fs->cache_total));
203 bluesky_flushd_invoke(fs);
206 /* Start a perpetually-running thread that flushes the cache occasionally. */
207 static gpointer flushd_thread(BlueSkyFS *fs)
210 bluesky_flushd_invoke(fs);
211 struct timespec delay;
214 nanosleep(&delay, NULL);
220 void bluesky_flushd_thread_launch(BlueSkyFS *fs)
222 g_thread_create((GThreadFunc)flushd_thread, fs, FALSE, NULL);