Fix a deadlock and a few memory leaks.
[bluesky.git] / bluesky / cache.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <glib.h>
11 #include <string.h>
12 #include <inttypes.h>
13
14 #include "bluesky-private.h"
15
16 #define WRITEBACK_DELAY (5 * 1000000)
17
18 /* Filesystem caching and cache coherency. */
19
20 static void writeback_complete(gpointer a, gpointer i)
21 {
22     BlueSkyInode *inode = (BlueSkyInode *)i;
23
24     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
25           "Writeback for inode %"PRIu64" complete", inode->inum);
26
27     g_mutex_lock(inode->lock);
28
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;
34     }
35
36     g_mutex_unlock(inode->lock);
37 }
38
39 static void flushd_inode(gpointer value, gpointer user_data)
40 {
41     BlueSkyFS *fs = (BlueSkyFS *)user_data;
42
43     BlueSkyInode *inode = (BlueSkyInode *)value;
44
45     g_mutex_lock(inode->lock);
46
47     if (inode->change_count == inode->change_commit) {
48         g_mutex_unlock(inode->lock);
49         bluesky_inode_unref(inode);
50         return;
51     }
52
53     if (inode->change_pending) {
54         /* Waiting for an earlier writeback to finish, so don't start a new
55          * writeback yet. */
56         g_mutex_unlock(inode->lock);
57         bluesky_inode_unref(inode);
58         return;
59     }
60
61     uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
62     if (elapsed < WRITEBACK_DELAY) {
63         /* Give a bit more time before starting writeback. */
64         g_mutex_unlock(inode->lock);
65         bluesky_inode_unref(inode);
66         return;
67     }
68
69     inode->change_pending = inode->change_count;
70
71     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
72           "Starting flush of inode %"PRIu64, inode->inum);
73
74     /* Create a store barrier.  All operations part of the writeback will be
75      * added to this barrier, so when the barrier completes we know that the
76      * writeback is finished. */
77     BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
78     barrier->op = STORE_OP_BARRIER;
79
80     bluesky_inode_start_sync(inode, barrier);
81
82     bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
83     bluesky_store_async_submit(barrier);
84     bluesky_store_async_unref(barrier);
85
86     g_mutex_unlock(inode->lock);
87     bluesky_inode_unref(inode);
88 }
89
90 /* Scan through the cache for dirty data and start flushing it to stable
91  * storage.  This does not guarantee that data is committed when it returns.
92  * Instead, this can be called occasionally to ensure that dirty data is
93  * gradually flushed.
94  *
95  * We do not want to hold the filesystem lock while flushing individual inodes,
96  * a that could lead to deadlock.  So first scan through the inode table to get
97  * a reference to all inodes, then process that queue of inodes after dropping
98  * the filesystem lock. */
99 static void gather_inodes(gpointer key, gpointer value, gpointer user_data)
100 {
101     GSList **list = (GSList **)user_data;
102     *list = g_slist_prepend(*list, value);
103     bluesky_inode_ref((BlueSkyInode *)value);
104 }
105
106 void bluesky_flushd_invoke(BlueSkyFS *fs)
107 {
108     GSList *list = NULL;
109
110     g_mutex_lock(fs->lock);
111     g_hash_table_foreach(fs->inodes, gather_inodes, &list);
112     g_mutex_unlock(fs->lock);
113
114     list = g_slist_reverse(list);
115     g_slist_foreach(list, flushd_inode, fs);
116
117     g_slist_free(list);
118 }