BlueSkyRCStr *data;
} BlueSkyCloudLog;
+/* Serialize objects into a log segment to be written to the cloud. */
+struct _BlueSkyCloudLogState {
+ GString *data;
+ BlueSkyCloudPointer location;
+ GList *inode_list;
+};
+
gboolean bluesky_cloudlog_equal(gconstpointer a, gconstpointer b);
guint bluesky_cloudlog_hash(gconstpointer a);
BlueSkyCloudLog *bluesky_cloudlog_new(BlueSkyFS *fs);
struct _BlueSkyLog;
typedef struct _BlueSkyLog BlueSkyLog;
+struct _BlueSkyCloudLogState;
+typedef struct _BlueSkyCloudLogState BlueSkyCloudLogState;
+
void bluesky_store_init();
BlueSkyStore *bluesky_store_new(const gchar *type);
void bluesky_store_free(BlueSkyStore *store);
BlueSkyStore *store;
BlueSkyLog *log;
+ BlueSkyCloudLogState *log_state;
/* Accounting for memory used for caches. Space is measured in blocks, not
* bytes. We track both total data in the caches and dirty data (total
* avoid deadlock do not attempt to take any other locks while the FS lock
* is held for list editing purposes. Items at the head of the list are
* most recently accessed/modified. */
- GList dirty_list, accessed_list;
+ GList unlogged_list; // Changes not yet synced to journal
+ GList dirty_list; // Not yet written to cloud storage
+ GList accessed_list; // All in-memory inodes
/* Mutex for the flush daemon, to prevent concurrent execution. */
GMutex *flushd_lock;
* dirty linked lists. We re-use the GList structure, using ->next to
* point to the head of the list and ->prev to point to the tail. The data
* element is unused. */
- GList *accessed_list, *dirty_list;
+ GList *unlogged_list, *accessed_list, *dirty_list;
int64_t atime; /* Microseconds since the Unix epoch */
int64_t ctime;
#define WRITEBACK_DELAY (20 * 1000000)
-/* Filesystem caching and cache coherency. */
+/* Filesystem caching and cache coherency. There are actually a couple of
+ * different tasks that are performed here:
+ * - Forcing data to the log if needed to reclaim memory or simply if the
+ * data has been dirty in memory long enough.
+ * - Writing batches of data to the cloud.
+ */
static void writeback_complete(gpointer a, gpointer i)
{
g_mutex_unlock(inode->lock);
}
-#if 0
-static void flushd_inode(gpointer value, gpointer user_data)
-{
- BlueSkyFS *fs = (BlueSkyFS *)user_data;
-
- BlueSkyInode *inode = (BlueSkyInode *)value;
-
- g_mutex_lock(inode->lock);
-
- if (inode->change_count == inode->change_commit) {
- uint64_t delay = bluesky_get_current_time() - inode->access_time;
- if (delay >= CACHE_CLEAN_DELAY) {
- drop_caches(inode);
-
- /* If the only references are the one we hold and the one in the
- * filesystem inum->inode hash table... First check the refcount
- * without the lock for speed, but if the check looks good verify
- * it after taking the filesystem lock. */
- if (inode->refcount == 2) {
- g_mutex_lock(fs->lock);
- if (inode->refcount == 2) {
- g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
- "Trying to drop inode %"PRIu64" from cache",
- inode->inum);
- if (g_hash_table_remove(fs->inodes, &inode->inum))
- bluesky_inode_unref(inode);
- }
- bluesky_list_unlink(&inode->fs->accessed_list,
- inode->accessed_list);
- inode->accessed_list = NULL;
- bluesky_list_unlink(&inode->fs->dirty_list,
- inode->dirty_list);
- inode->dirty_list = NULL;
- g_mutex_unlock(fs->lock);
- }
- }
-
- g_mutex_unlock(inode->lock);
- bluesky_inode_unref(inode);
- return;
- }
-
- if (inode->change_pending) {
- /* Waiting for an earlier writeback to finish, so don't start a new
- * writeback yet. */
- g_mutex_unlock(inode->lock);
- bluesky_inode_unref(inode);
- return;
- }
-
- uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
- if (elapsed < WRITEBACK_DELAY) {
- /* Give a bit more time before starting writeback. */
- g_mutex_unlock(inode->lock);
- bluesky_inode_unref(inode);
- return;
- }
-
- inode->change_pending = inode->change_count;
-
- g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
- "Starting flush of inode %"PRIu64, inode->inum);
-
- /* Create a store barrier. All operations part of the writeback will be
- * added to this barrier, so when the barrier completes we know that the
- * writeback is finished. */
- BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
- barrier->op = STORE_OP_BARRIER;
-
- bluesky_inode_start_sync(inode, barrier);
-
- bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
- bluesky_store_async_submit(barrier);
- bluesky_store_async_unref(barrier);
-
- g_mutex_unlock(inode->lock);
- bluesky_inode_unref(inode);
-}
-#endif
-
static void flushd_dirty_inode(BlueSkyInode *inode)
{
BlueSkyFS *fs = inode->fs;
if (!g_mutex_trylock(fs->flushd_lock))
return NULL;
flushd_dirty(fs);
- flushd_clean(fs);
bluesky_cloudlog_write_log(fs);
+ flushd_clean(fs);
g_mutex_unlock(fs->flushd_lock);
return NULL;
g_mutex_unlock(log->fs->lock);
}
-/* Serialize objects into a log segment to be written to the cloud. */
-struct log_state {
- GString *data;
- BlueSkyCloudPointer location;
- GList *inode_list;
-};
-
struct log_header {
char magic[4];
uint32_t size;
};
BlueSkyCloudPointer bluesky_cloudlog_serialize(BlueSkyCloudLog *log,
- struct log_state *state)
+ BlueSkyCloudLogState *state)
{
if (log->location_flags & CLOUDLOG_CLOUD) {
return log->location;
static void find_inodes(gpointer key, gpointer value, gpointer user_data)
{
- struct log_state *state = (struct log_state *)user_data;
+ BlueSkyCloudLogState *state = (BlueSkyCloudLogState *)user_data;
BlueSkyCloudLog *item = (BlueSkyCloudLog *)value;
if (item->type != LOGTYPE_INODE)
{
g_print("Starting cloudlog write...\n");
- struct log_state state;
- state.data = g_string_new("");
- state.location.directory = 0;
- state.location.sequence = 0;
- state.location.offset = 0;
- state.location.size = 0;
- state.inode_list = NULL;
+ BlueSkyCloudLogState *state = fs->log_state;
+ state->data = g_string_new("");
g_mutex_lock(fs->lock);
- g_hash_table_foreach(fs->locations, find_inodes, &state);
+ g_hash_table_foreach(fs->locations, find_inodes, state);
g_mutex_unlock(fs->lock);
- while (state.inode_list != NULL) {
- BlueSkyCloudLog *log = (BlueSkyCloudLog *)state.inode_list->data;
- bluesky_cloudlog_serialize(log, &state);
- state.inode_list = g_list_delete_link(state.inode_list,
- state.inode_list);
+ while (state->inode_list != NULL) {
+ BlueSkyCloudLog *log = (BlueSkyCloudLog *)state->inode_list->data;
+ bluesky_cloudlog_serialize(log, state);
+ state->inode_list = g_list_delete_link(state->inode_list,
+ state->inode_list);
}
- g_print("Serialized %zd bytes of data\n", state.data->len);
+ g_print("Serialized %zd bytes of data\n", state->data->len);
BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
async->op = STORE_OP_PUT;
async->key = g_strdup_printf("log-%08d-%08d",
- state.location.directory,
- state.location.sequence);
- async->data = bluesky_string_new_from_gstring(state.data);
+ state->location.directory,
+ state->location.sequence);
+ async->data = bluesky_string_new_from_gstring(state->data);
bluesky_store_async_submit(async);
bluesky_store_async_wait(async);
bluesky_store_async_unref(async);
+
+ state->data = NULL;
+ state->location.sequence++;
+ state->location.offset = 0;
}
g_hash_table_size(fs->inodes), fs->next_inum);
GList *item;
+ g_print("Unsynced inode list:");
+ for (item = fs->unlogged_list.next; item != NULL; item = item->next) {
+ g_print(" %"PRIu64";", ((BlueSkyInode *)item->data)->inum);
+ }
+ g_print("\n");
g_print("Dirty inode LRU list:");
for (item = fs->dirty_list.next; item != NULL; item = item->next) {
g_print(" %"PRIu64";", ((BlueSkyInode *)item->data)->inum);
#endif
g_mutex_lock(inode->fs->lock);
+ bluesky_list_unlink(&inode->fs->unlogged_list, inode->unlogged_list);
+ inode->unlogged_list = bluesky_list_prepend(&inode->fs->unlogged_list, inode);
bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
inode->dirty_list = bluesky_list_prepend(&inode->fs->dirty_list, inode);
bluesky_list_unlink(&inode->fs->accessed_list, inode->accessed_list);
fs->locations = g_hash_table_new(bluesky_cloudlog_hash,
bluesky_cloudlog_equal);
+ fs->log_state = g_new0(BlueSkyCloudLogState, 1);
+ fs->log_state->data = g_string_new("");
+
return fs;
}
/* Sanity check: Is the inode clean? */
if (inode->change_commit < inode->change_count
|| inode->accessed_list != NULL
+ || inode->unlogged_list != NULL
|| inode->dirty_list != NULL) {
g_warning("Dropping inode which is not clean (commit %"PRIi64" < change %"PRIi64"; accessed_list = %p; dirty_list = %p)\n", inode->change_commit, inode->change_count, inode->accessed_list, inode->dirty_list);
}
g_mutex_lock(inode->fs->lock);
bluesky_list_unlink(&inode->fs->accessed_list, inode->accessed_list);
bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
+ bluesky_list_unlink(&inode->fs->unlogged_list, inode->unlogged_list);
g_mutex_unlock(inode->fs->lock);
/* Free file type specific data. It should be an error for there to be