Add partial journal replay to filesystem recovery.
[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 (20 * 1000000)
17 #define CACHE_DROP_DELAY (20 * 1000000)
18
19 /* Filesystem caching and cache coherency.  There are actually a couple of
20  * different tasks that are performed here:
21  *   - Forcing data to the log if needed to reclaim memory or simply if the
22  *     data has been dirty in memory long enough.
23  *   - Writing batches of data to the cloud.
24  */
25
26 static void flushd_dirty_inode(BlueSkyInode *inode)
27 {
28     BlueSkyFS *fs = inode->fs;
29
30     g_mutex_lock(fs->lock);
31     bluesky_list_unlink(&fs->unlogged_list, inode->unlogged_list);
32     inode->unlogged_list = NULL;
33     g_mutex_unlock(fs->lock);
34
35     /* Inode is clean; nothing to do. */
36     if (inode->change_count == inode->change_commit)
37         return;
38
39     if (bluesky_verbose) {
40         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
41             "Starting flush of inode %"PRIu64, inode->inum);
42     }
43
44     bluesky_inode_start_sync(inode);
45 }
46
47 /* Check whether memory usage may have dropped below critical thresholds for
48  * waking up waiting threads. */
49 void flushd_check_wakeup(BlueSkyFS *fs)
50 {
51     int dirty = g_atomic_int_get(&fs->cache_dirty);
52     dirty += g_atomic_int_get(&fs->cache_log_dirty);
53
54     if (dirty <= bluesky_watermark_high_dirty)
55         g_cond_broadcast(fs->flushd_cond);
56 }
57
58 /* Try to flush dirty data to disk, either due to memory pressure or due to
59  * timeouts. */
60 static void flushd_dirty(BlueSkyFS *fs)
61 {
62     int64_t start_time = bluesky_get_current_time();
63     g_mutex_lock(fs->lock);
64
65     while (1) {
66         BlueSkyInode *inode;
67         if (fs->unlogged_list.prev == NULL)
68             break;
69         inode = fs->unlogged_list.prev->data;
70
71         if (bluesky_verbose) {
72             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
73                   "Considering flushing inode %"PRIu64, inode->inum);
74         }
75
76         /* Stop processing dirty inodes if we both have enough memory available
77          * and the oldest inode is sufficiently new that it need not be flushed
78          * out. */
79         uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
80         if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
81                 && elapsed < WRITEBACK_DELAY)
82             break;
83         if (inode->change_time > start_time)
84             break;
85
86         bluesky_inode_ref(inode);
87
88         g_mutex_unlock(fs->lock);
89
90         g_mutex_lock(inode->lock);
91         flushd_dirty_inode(inode);
92         g_mutex_unlock(inode->lock);
93         bluesky_inode_unref(inode);
94
95         g_mutex_lock(fs->lock);
96         flushd_check_wakeup(fs);
97     }
98
99     g_cond_broadcast(fs->flushd_cond);
100
101     g_mutex_unlock(fs->lock);
102 }
103
104 /* Try to flush dirty data to the cloud.  This will take a snapshot of the
105  * entire filesystem (though only point-in-time consistent for isolated inodes
106  * and not the filesystem as a whole) and ensure all data is written to the
107  * cloud.  When the write completes, we will allow old journal segments (those
108  * that were fully written _before_ the snapshot process started) to be garbage
109  * collected.  Newer journal segments can't be collected yet since they may
110  * still contain data which has not been written persistently to the cloud. */
111 static void flushd_cloud(BlueSkyFS *fs)
112 {
113     g_mutex_lock(fs->lock);
114
115     /* TODO: Locking?  Since we're reading a single variable this is probably
116      * atomic but a lock could be safer. */
117     BlueSkyCloudLog *marker = bluesky_log_get_commit_point(fs);
118     int journal_seq_start = fs->log->seq_num;
119
120     while (1) {
121         BlueSkyInode *inode;
122         if (fs->dirty_list.prev == NULL)
123             break;
124         inode = fs->dirty_list.prev->data;
125
126         if (bluesky_verbose) {
127             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
128                   "Flushing inode %"PRIu64" to cloud", inode->inum);
129         }
130
131         bluesky_inode_ref(inode);
132
133         g_mutex_unlock(fs->lock);
134
135         g_mutex_lock(inode->lock);
136         g_assert(inode->change_cloud == inode->change_commit);
137         g_mutex_lock(fs->lock);
138         bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
139         inode->dirty_list = NULL;
140         g_mutex_unlock(fs->lock);
141
142         BlueSkyCloudLog *log = inode->committed_item;
143         inode->committed_item = NULL;
144         g_mutex_unlock(inode->lock);
145
146         if (log != NULL)
147             bluesky_cloudlog_serialize(log, fs);
148         bluesky_inode_unref(inode);
149         bluesky_cloudlog_unref(log);
150
151         g_mutex_lock(fs->lock);
152     }
153
154     /* Write out any updated inode map entries, so that all inodes just written
155      * can be located, and then a final commit record. */
156     BlueSkyCloudLog *commit_record = bluesky_inode_map_serialize(fs);
157     bluesky_cloudlog_serialize(commit_record, fs);
158
159     g_mutex_unlock(fs->lock);
160     bluesky_cloudlog_flush(fs);
161
162     /* Wait until all segments have been written to the cloud, so that it
163      * becomes safe to free up journal segments. */
164     while (fs->log_state->pending_segments != NULL) {
165         SerializedRecord *segment
166             = (SerializedRecord *)fs->log_state->pending_segments->data;
167         g_mutex_lock(segment->lock);
168         while (!segment->complete)
169             g_cond_wait(segment->cond, segment->lock);
170         g_mutex_unlock(segment->lock);
171
172         g_mutex_free(segment->lock);
173         g_cond_free(segment->cond);
174         g_free(segment);
175
176         fs->log_state->pending_segments
177             = g_list_delete_link(fs->log_state->pending_segments,
178                                  fs->log_state->pending_segments);
179     }
180
181     bluesky_log_write_commit_point(fs, marker);
182
183     g_print("All segments have been flushed, journal < %d is clean\n",
184             journal_seq_start);
185
186     fs->log->journal_watermark = journal_seq_start;
187 }
188
189 /* Drop cached data for a given inode, if it is clean.  inode must be locked. */
190 static void drop_caches(BlueSkyInode *inode)
191 {
192     if (inode->type == BLUESKY_REGULAR)
193         bluesky_file_drop_cached(inode);
194
195     BlueSkyCloudLog *log = inode->committed_item;
196     if (log != NULL) {
197         g_mutex_lock(log->lock);
198         if (log->data != NULL
199             && g_atomic_int_get(&log->data_lock_count) == 0
200             && (log->location_flags != 0))
201         {
202             bluesky_cloudlog_stats_update(log, -1);
203             bluesky_string_unref(log->data);
204             log->data = NULL;
205             bluesky_cloudlog_stats_update(log, 1);
206         }
207         if (log->location_flags & CLOUDLOG_CLOUD) {
208             log->location_flags &= ~CLOUDLOG_JOURNAL;
209         }
210         g_mutex_unlock(log->lock);
211     }
212 }
213
214 /* Drop clean data from the cache if needed.  Clean data should generally be
215  * memory-mapped from log file or similar, so the kernel can drop this clean
216  * data from memory for us and hence memory management isn't too important.
217  * Mainly, we'll want to drop references to data that hasn't been accessed in a
218  * while so that it is possible to reclaim log segments on disk. */
219 static void flushd_clean(BlueSkyFS *fs)
220 {
221     g_mutex_lock(fs->lock);
222
223     size_t inode_count = g_hash_table_size(fs->inodes);
224     if (!inode_count)
225         inode_count = 1;
226
227     while (inode_count-- > 0) {
228         BlueSkyInode *inode;
229         if (fs->accessed_list.prev == NULL)
230             break;
231         inode = fs->accessed_list.prev->data;
232
233         uint64_t elapsed = bluesky_get_current_time() - inode->access_time;
234         if (elapsed < CACHE_DROP_DELAY)
235             break;
236
237         if (bluesky_verbose) {
238             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
239                   "Considering dropping cached data for inode %"PRIu64,
240                   inode->inum);
241         }
242
243         bluesky_inode_ref(inode);
244
245         g_mutex_unlock(fs->lock);
246
247         g_mutex_lock(inode->lock);
248
249         g_mutex_lock(fs->lock);
250         bluesky_list_unlink(&fs->accessed_list, inode->accessed_list);
251         inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
252         g_mutex_unlock(fs->lock);
253
254         drop_caches(inode);
255
256         g_mutex_unlock(inode->lock);
257         bluesky_inode_unref(inode);
258
259         g_mutex_lock(fs->lock);
260     }
261
262     g_mutex_unlock(fs->lock);
263 }
264
265 /* Run the flush daemon for a single iteration, though if it is already
266  * executing returns immediately. */
267 static gpointer flushd_task(BlueSkyFS *fs)
268 {
269     if (!g_mutex_trylock(fs->flushd_lock))
270         return NULL;
271     flushd_dirty(fs);
272     flushd_cloud(fs);
273     flushd_clean(fs);
274     bluesky_cachefile_gc(fs);
275     g_mutex_unlock(fs->flushd_lock);
276
277     return NULL;
278 }
279
280 void bluesky_flushd_invoke(BlueSkyFS *fs)
281 {
282     g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
283 }
284
285 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
286 {
287     if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_medium_dirty)
288         return;
289
290     if (bluesky_verbose) {
291         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
292               "Too much data; invoking flushd: dirty=%d",
293               g_atomic_int_get(&fs->cache_dirty));
294     }
295
296     bluesky_flushd_invoke(fs);
297
298     /* If the system is under heavy memory pressure, actually delay execution
299      * so the flush daemon can catch up. */
300     while (g_atomic_int_get(&fs->cache_dirty)
301                 + g_atomic_int_get(&fs->cache_log_dirty)
302            > bluesky_watermark_high_dirty) {
303         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
304               "Waiting due to memory pressure, dirty=%d + %d",
305               g_atomic_int_get(&fs->cache_dirty),
306               g_atomic_int_get(&fs->cache_log_dirty));
307         g_mutex_lock(fs->lock);
308         g_cond_wait(fs->flushd_cond, fs->lock);
309         g_mutex_unlock(fs->lock);
310     }
311 }
312
313 /* Start a perpetually-running thread that flushes the cache occasionally. */
314 static gpointer flushd_thread(BlueSkyFS *fs)
315 {
316     while (TRUE) {
317         bluesky_flushd_invoke(fs);
318         struct timespec delay;
319         delay.tv_sec = 2;
320         delay.tv_nsec = 0;
321         nanosleep(&delay, NULL);
322     }
323
324     return NULL;
325 }
326
327 void bluesky_flushd_thread_launch(BlueSkyFS *fs)
328 {
329     g_thread_create((GThreadFunc)flushd_thread, fs, FALSE, NULL);
330 }