1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2010 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
10 #define _ATFILE_SOURCE
18 #include <sys/types.h>
23 #include "bluesky-private.h"
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
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). */
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 << 23)
40 static void writebuf(int fd, const char *buf, size_t len)
44 written = write(fd, buf, len);
45 if (written < 0 && errno == EINTR)
47 g_assert(written >= 0);
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.
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.
62 * Log replay ought to be implemented later, and ought to set the initial
63 * sequence number appropriately.
65 static gpointer log_thread(gpointer d)
67 BlueSkyLog *log = (BlueSkyLog *)d;
69 /* If there are multiple log items to write, we may write more than one
70 * before calling fsync(). The committed list is used to track all the
71 * items that should be marked as committed once that final fsync() is
73 GSList *committed = NULL;
75 int dirfd = open(log->log_directory, O_DIRECTORY);
77 fprintf(stderr, "Unable to open logging directory: %m\n");
84 g_snprintf(logfile, sizeof(logfile), "log-%08d", log->seq_num);
85 log->fd = openat(dirfd, logfile, O_CREAT|O_WRONLY|O_EXCL, 0600);
86 if (log->fd < 0 && errno == EEXIST) {
87 fprintf(stderr, "Log file %s already exists...\n", logfile);
90 } else if (log->fd < 0) {
91 fprintf(stderr, "Error opening logfile %s: %m\n", logfile);
98 BlueSkyLogItem *item = (BlueSkyLogItem *)g_async_queue_pop(log->queue);
99 g_mutex_lock(item->lock);
100 writebuf(log->fd, item->key, strlen(item->key));
101 writebuf(log->fd, item->data->data, item->data->len);
102 committed = g_slist_prepend(committed, item);
104 /* Force an fsync either if we will be closing this log segment and
105 * opening a new file, or if there are no other log items currently
106 * waiting to be written. */
107 off_t logsize = lseek(log->fd, 0, SEEK_CUR);
108 if (logsize >= LOG_SEGMENT_SIZE
109 || g_async_queue_length(log->queue) <= 0)
113 while (committed != NULL) {
114 item = (BlueSkyLogItem *)committed->data;
115 item->committed = TRUE;
116 g_cond_signal(item->cond);
117 g_mutex_unlock(item->lock);
118 committed = g_slist_delete_link(committed, committed);
121 /* if (batchsize > 1)
122 g_print("Log batch size: %d\n", batchsize); */
125 if (logsize < 0 || logsize >= LOG_SEGMENT_SIZE) {
135 BlueSkyLog *bluesky_log_new(const char *log_directory)
137 BlueSkyLog *log = g_new0(BlueSkyLog, 1);
139 log->log_directory = g_strdup(log_directory);
142 log->queue = g_async_queue_new();
144 g_thread_create(log_thread, log, FALSE, NULL);
149 BlueSkyLogItem *bluesky_log_item_new()
151 BlueSkyLogItem *item = g_new(BlueSkyLogItem, 1);
152 item->committed = FALSE;
153 item->lock = g_mutex_new();
154 item->cond = g_cond_new();
160 void bluesky_log_item_submit(BlueSkyLogItem *item, BlueSkyLog *log)
162 g_async_queue_push(log->queue, item);
165 static void bluesky_log_item_free(BlueSkyLogItem *item)
168 bluesky_string_unref(item->data);
169 g_mutex_free(item->lock);
170 g_cond_free(item->cond);
174 void bluesky_log_item_finish(BlueSkyLogItem *item)
176 g_mutex_lock(item->lock);
177 while (!item->committed)
178 g_cond_wait(item->cond, item->lock);
179 g_mutex_unlock(item->lock);
180 bluesky_log_item_free(item);