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.
25 static void writeback_complete(gpointer a, gpointer i)
27 BlueSkyInode *inode = (BlueSkyInode *)i;
29 if (bluesky_verbose) {
30 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
31 "Writeback for inode %"PRIu64" complete", inode->inum);
34 g_mutex_lock(inode->lock);
36 inode->change_commit = inode->change_pending;
37 inode->change_pending = 0;
38 if (inode->change_count == inode->change_commit) {
39 /* If inode is no longer dirty... */
40 inode->change_time = 0;
41 g_mutex_lock(inode->fs->lock);
42 bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
43 inode->dirty_list = NULL;
44 g_mutex_unlock(inode->fs->lock);
47 g_mutex_unlock(inode->lock);
50 static void flushd_dirty_inode(BlueSkyInode *inode)
52 BlueSkyFS *fs = inode->fs;
54 g_mutex_lock(fs->lock);
55 bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
56 inode->dirty_list = NULL;
57 g_mutex_unlock(fs->lock);
59 /* Inode is clean; nothing to do. */
60 if (inode->change_count == inode->change_commit)
63 /* Inode writeback is in progress; put back on the dirty list. */
64 if (inode->change_pending) {
65 /* Waiting for an earlier writeback to finish, so don't start a new
67 g_mutex_lock(fs->lock);
68 inode->change_time = bluesky_get_current_time();
69 bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
70 inode->dirty_list = bluesky_list_prepend(&fs->dirty_list, inode);
71 g_mutex_unlock(fs->lock);
75 if (bluesky_verbose) {
76 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
77 "Starting flush of inode %"PRIu64, inode->inum);
79 inode->change_pending = inode->change_count;
81 bluesky_inode_start_sync(inode);
84 /* Try to flush dirty data to disk, either due to memory pressure or due to
86 static void flushd_dirty(BlueSkyFS *fs)
88 int64_t start_time = bluesky_get_current_time();
89 g_mutex_lock(fs->lock);
93 if (fs->dirty_list.prev == NULL)
95 inode = fs->dirty_list.prev->data;
97 if (bluesky_verbose) {
98 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
99 "Considering flushing inode %"PRIu64, inode->inum);
102 /* Stop processing dirty inodes if we both have enough memory available
103 * and the oldest inode is sufficiently new that it need not be flushed
105 uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
106 if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
107 && elapsed < WRITEBACK_DELAY)
109 if (inode->change_time > start_time)
112 bluesky_inode_ref(inode);
114 g_mutex_unlock(fs->lock);
116 g_mutex_lock(inode->lock);
117 flushd_dirty_inode(inode);
118 g_mutex_unlock(inode->lock);
119 bluesky_inode_unref(inode);
121 g_mutex_lock(fs->lock);
124 g_mutex_unlock(fs->lock);
127 /* Drop cached data for a given inode, if it is clean. inode must be locked. */
128 static void drop_caches(BlueSkyInode *inode)
130 if (inode->type == BLUESKY_REGULAR)
131 bluesky_file_drop_cached(inode);
134 /* Drop clean data fromt the cache if needed due to memory pressure. */
135 static void flushd_clean(BlueSkyFS *fs)
137 g_mutex_lock(fs->lock);
139 size_t inode_count = g_hash_table_size(fs->inodes);
143 while (inode_count-- > 0) {
144 if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
148 if (fs->accessed_list.prev == NULL)
150 inode = fs->accessed_list.prev->data;
152 if (bluesky_verbose) {
153 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
154 "Considering dropping cached data for inode %"PRIu64,
158 bluesky_inode_ref(inode);
160 g_mutex_unlock(fs->lock);
162 g_mutex_lock(inode->lock);
164 g_mutex_lock(fs->lock);
165 bluesky_list_unlink(&fs->accessed_list, inode->accessed_list);
166 inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
167 g_mutex_unlock(fs->lock);
171 g_mutex_unlock(inode->lock);
172 bluesky_inode_unref(inode);
174 g_mutex_lock(fs->lock);
177 g_mutex_unlock(fs->lock);
180 /* Run the flush daemon for a single iteration, though if it is already
181 * executing returns immediately. */
182 static gpointer flushd_task(BlueSkyFS *fs)
184 if (!g_mutex_trylock(fs->flushd_lock))
187 bluesky_cloudlog_write_log(fs);
189 g_mutex_unlock(fs->flushd_lock);
194 void bluesky_flushd_invoke(BlueSkyFS *fs)
196 g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
199 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
201 if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
202 && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
205 if (bluesky_verbose) {
206 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
207 "Too much data; invoking flushd: dirty=%d total=%d",
208 g_atomic_int_get(&fs->cache_dirty),
209 g_atomic_int_get(&fs->cache_total));
212 bluesky_flushd_invoke(fs);
215 /* Start a perpetually-running thread that flushes the cache occasionally. */
216 static gpointer flushd_thread(BlueSkyFS *fs)
219 bluesky_flushd_invoke(fs);
220 struct timespec delay;
223 nanosleep(&delay, NULL);
229 void bluesky_flushd_thread_launch(BlueSkyFS *fs)
231 g_thread_create((GThreadFunc)flushd_thread, fs, FALSE, NULL);