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 /* Create a store barrier. All operations part of the writeback will be
82 * added to this barrier, so when the barrier completes we know that the
83 * writeback is finished. */
84 BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
85 barrier->op = STORE_OP_BARRIER;
87 bluesky_inode_start_sync(inode, barrier);
89 bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
90 bluesky_store_async_submit(barrier);
91 bluesky_store_async_unref(barrier);
94 /* Try to flush dirty data to disk, either due to memory pressure or due to
96 static void flushd_dirty(BlueSkyFS *fs)
98 int64_t start_time = bluesky_get_current_time();
99 g_mutex_lock(fs->lock);
103 if (fs->dirty_list.prev == NULL)
105 inode = fs->dirty_list.prev->data;
107 if (bluesky_verbose) {
108 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
109 "Considering flushing inode %"PRIu64, inode->inum);
112 /* Stop processing dirty inodes if we both have enough memory available
113 * and the oldest inode is sufficiently new that it need not be flushed
115 uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
116 if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
117 && elapsed < WRITEBACK_DELAY)
119 if (inode->change_time > start_time)
122 bluesky_inode_ref(inode);
124 g_mutex_unlock(fs->lock);
126 g_mutex_lock(inode->lock);
127 flushd_dirty_inode(inode);
128 g_mutex_unlock(inode->lock);
129 bluesky_inode_unref(inode);
131 g_mutex_lock(fs->lock);
134 g_mutex_unlock(fs->lock);
137 /* Drop cached data for a given inode, if it is clean. inode must be locked. */
138 static void drop_caches(BlueSkyInode *inode)
140 if (inode->type == BLUESKY_REGULAR)
141 bluesky_file_drop_cached(inode);
144 /* Drop clean data fromt the cache if needed due to memory pressure. */
145 static void flushd_clean(BlueSkyFS *fs)
147 g_mutex_lock(fs->lock);
149 size_t inode_count = g_hash_table_size(fs->inodes);
153 while (inode_count-- > 0) {
154 if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
158 if (fs->accessed_list.prev == NULL)
160 inode = fs->accessed_list.prev->data;
162 if (bluesky_verbose) {
163 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
164 "Considering dropping cached data for inode %"PRIu64,
168 bluesky_inode_ref(inode);
170 g_mutex_unlock(fs->lock);
172 g_mutex_lock(inode->lock);
174 g_mutex_lock(fs->lock);
175 bluesky_list_unlink(&fs->accessed_list, inode->accessed_list);
176 inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
177 g_mutex_unlock(fs->lock);
181 g_mutex_unlock(inode->lock);
182 bluesky_inode_unref(inode);
184 g_mutex_lock(fs->lock);
187 g_mutex_unlock(fs->lock);
190 /* Run the flush daemon for a single iteration, though if it is already
191 * executing returns immediately. */
192 static gpointer flushd_task(BlueSkyFS *fs)
194 if (!g_mutex_trylock(fs->flushd_lock))
197 bluesky_cloudlog_write_log(fs);
199 g_mutex_unlock(fs->flushd_lock);
204 void bluesky_flushd_invoke(BlueSkyFS *fs)
206 g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
209 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
211 if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
212 && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
215 if (bluesky_verbose) {
216 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
217 "Too much data; invoking flushd: dirty=%d total=%d",
218 g_atomic_int_get(&fs->cache_dirty),
219 g_atomic_int_get(&fs->cache_total));
222 bluesky_flushd_invoke(fs);
225 /* Start a perpetually-running thread that flushes the cache occasionally. */
226 static gpointer flushd_thread(BlueSkyFS *fs)
229 bluesky_flushd_invoke(fs);
230 struct timespec delay;
233 nanosleep(&delay, NULL);
239 void bluesky_flushd_thread_launch(BlueSkyFS *fs)
241 g_thread_create((GThreadFunc)flushd_thread, fs, FALSE, NULL);