Basic filesystem journaling.
[bluesky.git] / bluesky / log.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2010  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #define _GNU_SOURCE
10 #define _ATFILE_SOURCE
11
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <glib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "bluesky-private.h"
24
25 /* The logging layer for BlueSky.  This is used to write filesystem changes
26  * durably to disk so that they can be recovered in the event of a system
27  * crash. */
28
29 /* The logging layer takes care out writing out a sequence of log records to
30  * disk.  On disk, each record consists of a header, a data payload, and a
31  * footer.  The footer contains a checksum of the record, meant to help with
32  * identifying corrupt log records (we would assume because the log record was
33  * only incompletely written out before a crash, which should only happen for
34  * log records that were not considered committed). */
35
36 // Rough size limit for a log segment.  This is not a firm limit and there are
37 // no absolute guarantees on the size of a log segment.
38 #define LOG_SEGMENT_SIZE (1 << 20)
39
40 static void writebuf(int fd, const char *buf, size_t len)
41 {
42     while (len > 0) {
43         ssize_t written;
44         written = write(fd, buf, len);
45         if (written < 0 && errno == EINTR)
46             continue;
47         g_assert(written >= 0);
48         buf += written;
49         len -= written;
50     }
51 }
52
53 /* All log writes (at least for a single log) are made by one thread, so we
54  * don't need to worry about concurrent access to the log file.  Log items to
55  * write are pulled off a queue (and so may be posted by any thread).
56  * fdatasync() is used to ensure the log items are stable on disk.
57  *
58  * The log is broken up into separate files, roughly of size LOG_SEGMENT_SIZE
59  * each.  If a log segment is not currently open (log->fd is negative), a new
60  * one is created.  Log segment filenames are assigned sequentially.
61  *
62  * Log replay ought to be implemented later, and ought to set the initial
63  * sequence number appropriately.
64  */
65 static gpointer log_thread(gpointer d)
66 {
67     BlueSkyLog *log = (BlueSkyLog *)d;
68
69     int dirfd = open(log->log_directory, O_DIRECTORY);
70     if (dirfd < 0) {
71         fprintf(stderr, "Unable to open logging directory: %m\n");
72         return NULL;
73     }
74
75     while (TRUE) {
76         if (log->fd < 0) {
77             char logfile[64];
78             g_snprintf(logfile, sizeof(logfile), "log-%08d", log->seq_num);
79             log->fd = openat(dirfd, logfile, O_CREAT|O_WRONLY|O_EXCL, 0600);
80             if (log->fd < 0 && errno == EEXIST) {
81                 fprintf(stderr, "Log file %s already exists...\n", logfile);
82                 log->seq_num++;
83                 continue;
84             } else if (log->fd < 0) {
85                 fprintf(stderr, "Error opening logfile %s: %m\n", logfile);
86                 return NULL;
87             }
88             fsync(log->fd);
89             fsync(dirfd);
90         }
91
92         BlueSkyLogItem *item = (BlueSkyLogItem *)g_async_queue_pop(log->queue);
93         g_mutex_lock(item->lock);
94         writebuf(log->fd, item->key, strlen(item->key));
95         writebuf(log->fd, item->data->data, item->data->len);
96         fdatasync(log->fd);
97         item->committed = TRUE;
98         g_cond_signal(item->cond);
99         g_mutex_unlock(item->lock);
100
101         off_t logsize = lseek(log->fd, 0, SEEK_CUR);
102         if (logsize < 0 || logsize >= LOG_SEGMENT_SIZE) {
103             close(log->fd);
104             log->fd = -1;
105             log->seq_num++;
106         }
107     }
108
109     return NULL;
110 }
111
112 BlueSkyLog *bluesky_log_new(const char *log_directory)
113 {
114     BlueSkyLog *log = g_new0(BlueSkyLog, 1);
115
116     log->log_directory = g_strdup(log_directory);
117     log->fd = -1;
118     log->seq_num = 0;
119     log->queue = g_async_queue_new();
120
121     g_thread_create(log_thread, log, FALSE, NULL);
122
123     return log;
124 }
125
126 BlueSkyLogItem *bluesky_log_item_new()
127 {
128     BlueSkyLogItem *item = g_new(BlueSkyLogItem, 1);
129     item->committed = FALSE;
130     item->lock = g_mutex_new();
131     item->cond = g_cond_new();
132     item->key = NULL;
133     item->data = NULL;
134     return item;
135 }
136
137 void bluesky_log_item_submit(BlueSkyLogItem *item, BlueSkyLog *log)
138 {
139     g_async_queue_push(log->queue, item);
140 }
141
142 static void bluesky_log_item_free(BlueSkyLogItem *item)
143 {
144     g_free(item->key);
145     bluesky_string_unref(item->data);
146     g_mutex_free(item->lock);
147     g_cond_free(item->cond);
148     g_free(item);
149 }
150
151 void bluesky_log_item_finish(BlueSkyLogItem *item)
152 {
153     g_mutex_lock(item->lock);
154     while (!item->committed)
155         g_cond_wait(item->cond, item->lock);
156     g_mutex_unlock(item->lock);
157     bluesky_log_item_free(item);
158 }