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. */
20 static void writeback_complete(gpointer a, gpointer i)
22 BlueSkyInode *inode = (BlueSkyInode *)i;
24 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
25 "Writeback for inode %"PRIu64" complete", inode->inum);
27 g_mutex_lock(inode->lock);
29 inode->change_commit = inode->change_pending;
30 inode->change_pending = 0;
31 if (inode->change_count == inode->change_commit) {
32 /* If inode is no longer dirty... */
33 inode->change_time = 0;
34 g_mutex_lock(inode->fs->lock);
35 bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
36 inode->dirty_list = NULL;
37 g_mutex_unlock(inode->fs->lock);
40 g_mutex_unlock(inode->lock);
44 static void flushd_inode(gpointer value, gpointer user_data)
46 BlueSkyFS *fs = (BlueSkyFS *)user_data;
48 BlueSkyInode *inode = (BlueSkyInode *)value;
50 g_mutex_lock(inode->lock);
52 if (inode->change_count == inode->change_commit) {
53 uint64_t delay = bluesky_get_current_time() - inode->access_time;
54 if (delay >= CACHE_CLEAN_DELAY) {
57 /* If the only references are the one we hold and the one in the
58 * filesystem inum->inode hash table... First check the refcount
59 * without the lock for speed, but if the check looks good verify
60 * it after taking the filesystem lock. */
61 if (inode->refcount == 2) {
62 g_mutex_lock(fs->lock);
63 if (inode->refcount == 2) {
64 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
65 "Trying to drop inode %"PRIu64" from cache",
67 if (g_hash_table_remove(fs->inodes, &inode->inum))
68 bluesky_inode_unref(inode);
70 bluesky_list_unlink(&inode->fs->accessed_list,
71 inode->accessed_list);
72 inode->accessed_list = NULL;
73 bluesky_list_unlink(&inode->fs->dirty_list,
75 inode->dirty_list = NULL;
76 g_mutex_unlock(fs->lock);
80 g_mutex_unlock(inode->lock);
81 bluesky_inode_unref(inode);
85 if (inode->change_pending) {
86 /* Waiting for an earlier writeback to finish, so don't start a new
88 g_mutex_unlock(inode->lock);
89 bluesky_inode_unref(inode);
93 uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
94 if (elapsed < WRITEBACK_DELAY) {
95 /* Give a bit more time before starting writeback. */
96 g_mutex_unlock(inode->lock);
97 bluesky_inode_unref(inode);
101 inode->change_pending = inode->change_count;
103 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
104 "Starting flush of inode %"PRIu64, inode->inum);
106 /* Create a store barrier. All operations part of the writeback will be
107 * added to this barrier, so when the barrier completes we know that the
108 * writeback is finished. */
109 BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
110 barrier->op = STORE_OP_BARRIER;
112 bluesky_inode_start_sync(inode, barrier);
114 bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
115 bluesky_store_async_submit(barrier);
116 bluesky_store_async_unref(barrier);
118 g_mutex_unlock(inode->lock);
119 bluesky_inode_unref(inode);
123 static void flushd_dirty_inode(BlueSkyInode *inode)
125 BlueSkyFS *fs = inode->fs;
127 /* Inode is clean; nothing to do. */
128 if (inode->change_count == inode->change_commit)
131 /* Inode writeback is in progress; put back on the dirty list. */
132 if (inode->change_pending) {
133 /* Waiting for an earlier writeback to finish, so don't start a new
135 g_mutex_lock(fs->lock);
136 inode->change_time = bluesky_get_current_time();
137 bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
138 inode->dirty_list = bluesky_list_prepend(&fs->dirty_list, inode);
139 g_mutex_unlock(fs->lock);
143 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
144 "Starting flush of inode %"PRIu64, inode->inum);
145 inode->change_pending = inode->change_count;
147 /* Create a store barrier. All operations part of the writeback will be
148 * added to this barrier, so when the barrier completes we know that the
149 * writeback is finished. */
150 BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
151 barrier->op = STORE_OP_BARRIER;
153 bluesky_inode_start_sync(inode, barrier);
155 bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
156 bluesky_store_async_submit(barrier);
157 bluesky_store_async_unref(barrier);
160 /* Try to flush dirty data to disk, either due to memory pressure or due to
162 static void flushd_dirty(BlueSkyFS *fs)
164 int64_t start_time = bluesky_get_current_time();
165 g_mutex_lock(fs->lock);
169 if (fs->dirty_list.prev == NULL)
171 inode = fs->dirty_list.prev->data;
173 g_print("Considering flushing inode %"PRIu64"\n", inode->inum);
175 /* Stop processing dirty inodes if we both have enough memory available
176 * and the oldest inode is sufficiently new that it need not be flushed
178 uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
179 if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
180 && elapsed < WRITEBACK_DELAY)
182 if (inode->change_time > start_time)
185 bluesky_inode_ref(inode);
187 bluesky_list_unlink(&fs->dirty_list, fs->dirty_list.prev);
188 inode->dirty_list = NULL;
190 g_mutex_unlock(fs->lock);
192 g_mutex_lock(inode->lock);
193 flushd_dirty_inode(inode);
194 g_mutex_unlock(inode->lock);
195 bluesky_inode_unref(inode);
197 g_mutex_lock(fs->lock);
200 g_mutex_unlock(fs->lock);
203 /* Drop cached data for a given inode, if it is clean. inode must be locked. */
204 static void drop_caches(BlueSkyInode *inode)
206 if (inode->type == BLUESKY_REGULAR)
207 bluesky_file_drop_cached(inode);
210 /* Drop clean data fromt the cache if needed due to memory pressure. */
211 static void flushd_clean(BlueSkyFS *fs)
213 g_mutex_lock(fs->lock);
215 size_t inode_count = g_hash_table_size(fs->inodes);
219 while (inode_count-- > 0) {
220 if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
224 if (fs->accessed_list.prev == NULL)
226 inode = fs->accessed_list.prev->data;
228 g_print("Considering dropping cached data for inode %"PRIu64"\n",
231 bluesky_inode_ref(inode);
233 bluesky_list_unlink(&fs->accessed_list, fs->accessed_list.prev);
234 inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
236 g_mutex_unlock(fs->lock);
238 g_mutex_lock(inode->lock);
240 g_mutex_unlock(inode->lock);
241 bluesky_inode_unref(inode);
243 g_mutex_lock(fs->lock);
246 g_mutex_unlock(fs->lock);
249 /* Run the flush daemon for a single iteration, though if it is already
250 * executing returns immediately. */
251 static gpointer flushd_task(BlueSkyFS *fs)
253 if (!g_mutex_trylock(fs->flushd_lock))
257 g_mutex_unlock(fs->flushd_lock);
262 void bluesky_flushd_invoke(BlueSkyFS *fs)
264 g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
267 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
269 if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
270 && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
273 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
274 "Too much data; invoking flushd: dirty=%d total=%d",
275 g_atomic_int_get(&fs->cache_dirty),
276 g_atomic_int_get(&fs->cache_total));
278 bluesky_flushd_invoke(fs);