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 (5 * 1000000)
17 #define CACHE_CLEAN_DELAY (30 * 1000000)
19 /* Filesystem caching and cache coherency. */
21 static void writeback_complete(gpointer a, gpointer i)
23 BlueSkyInode *inode = (BlueSkyInode *)i;
25 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
26 "Writeback for inode %"PRIu64" complete", inode->inum);
28 g_mutex_lock(inode->lock);
30 inode->change_commit = inode->change_pending;
31 if (inode->change_count == inode->change_commit) {
32 /* If inode is no longer dirty... */
33 inode->change_time = 0;
34 inode->change_pending = 0;
37 g_mutex_unlock(inode->lock);
40 /* Drop cached data for a given inode, if it is clean. inode must be locked. */
41 static void drop_caches(BlueSkyInode *inode)
43 if (inode->type == BLUESKY_REGULAR)
44 bluesky_file_drop_cached(inode);
47 static void flushd_inode(gpointer value, gpointer user_data)
49 BlueSkyFS *fs = (BlueSkyFS *)user_data;
51 BlueSkyInode *inode = (BlueSkyInode *)value;
53 g_mutex_lock(inode->lock);
55 if (inode->change_count == inode->change_commit) {
56 uint64_t delay = bluesky_get_current_time() - inode->access_time;
57 if (delay >= CACHE_CLEAN_DELAY) {
60 /* If the only references are the one we hold and the one in the
61 * filesystem inum->inode hash table... The check for a refcount
62 * of 2 shouldn't be necessary so it should be okay if there is a
63 * race on the check. */
64 if (inode->refcount == 2) {
65 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
66 "Trying to drop inode %"PRIu64" from cache", inode->inum);
67 g_mutex_lock(fs->lock);
68 if (g_hash_table_remove(fs->inodes, &inode->inum))
69 bluesky_inode_unref(inode);
70 g_mutex_unlock(fs->lock);
74 g_mutex_unlock(inode->lock);
75 bluesky_inode_unref(inode);
79 if (inode->change_pending) {
80 /* Waiting for an earlier writeback to finish, so don't start a new
82 g_mutex_unlock(inode->lock);
83 bluesky_inode_unref(inode);
87 uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
88 if (elapsed < WRITEBACK_DELAY) {
89 /* Give a bit more time before starting writeback. */
90 g_mutex_unlock(inode->lock);
91 bluesky_inode_unref(inode);
95 inode->change_pending = inode->change_count;
97 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
98 "Starting flush of inode %"PRIu64, inode->inum);
100 /* Create a store barrier. All operations part of the writeback will be
101 * added to this barrier, so when the barrier completes we know that the
102 * writeback is finished. */
103 BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
104 barrier->op = STORE_OP_BARRIER;
106 bluesky_inode_start_sync(inode, barrier);
108 bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
109 bluesky_store_async_submit(barrier);
110 bluesky_store_async_unref(barrier);
112 g_mutex_unlock(inode->lock);
113 bluesky_inode_unref(inode);
116 /* Scan through the cache for dirty data and start flushing it to stable
117 * storage. This does not guarantee that data is committed when it returns.
118 * Instead, this can be called occasionally to ensure that dirty data is
121 * We do not want to hold the filesystem lock while flushing individual inodes,
122 * a that could lead to deadlock. So first scan through the inode table to get
123 * a reference to all inodes, then process that queue of inodes after dropping
124 * the filesystem lock. */
125 static void gather_inodes(gpointer key, gpointer value, gpointer user_data)
127 GSList **list = (GSList **)user_data;
128 *list = g_slist_prepend(*list, value);
129 bluesky_inode_ref((BlueSkyInode *)value);
132 void bluesky_flushd_invoke(BlueSkyFS *fs)
136 g_mutex_lock(fs->lock);
137 g_hash_table_foreach(fs->inodes, gather_inodes, &list);
138 g_mutex_unlock(fs->lock);
140 list = g_slist_reverse(list);
141 g_slist_foreach(list, flushd_inode, fs);