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)
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 if (inode->change_count == inode->change_commit) {
31 /* If inode is no longer dirty... */
32 inode->change_time = 0;
33 inode->change_pending = 0;
36 g_mutex_unlock(inode->lock);
39 static void flushd_inode(gpointer key, gpointer value, gpointer user_data)
41 BlueSkyFS *fs = (BlueSkyFS *)user_data;
43 BlueSkyInode *inode = (BlueSkyInode *)value;
45 g_mutex_lock(inode->lock);
47 if (inode->change_count == inode->change_commit) {
48 g_mutex_unlock(inode->lock);
52 if (inode->change_pending) {
53 /* Waiting for an earlier writeback to finish, so don't start a new
55 g_mutex_unlock(inode->lock);
59 uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
60 if (elapsed < WRITEBACK_DELAY) {
61 /* Give a bit more time before starting writeback. */
62 g_mutex_unlock(inode->lock);
66 inode->change_pending = inode->change_count;
68 g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
69 "Starting flush of inode %"PRIu64, inode->inum);
71 /* Create a store barrier. All operations part of the writeback will be
72 * added to this barrier, so when the barrier completes we know that the
73 * writeback is finished. */
74 BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
75 barrier->op = STORE_OP_BARRIER;
77 bluesky_inode_start_sync(inode, barrier);
79 bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
80 bluesky_store_async_submit(barrier);
81 bluesky_store_async_unref(barrier);
83 g_mutex_unlock(inode->lock);
86 /* Scan through the cache for dirty data and start flushing it to stable
87 * storage. This does not guarantee that data is committed when it returns.
88 * Instead, this can be called occasionally to ensure that dirty data is
89 * gradually flushed. */
90 void bluesky_flushd_invoke(BlueSkyFS *fs)
92 g_mutex_lock(fs->lock);
93 g_hash_table_foreach(fs->inodes, flushd_inode, fs);
94 g_mutex_unlock(fs->lock);