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>
24 #include "bluesky-private.h"
26 /* The logging layer for BlueSky. This is used to write filesystem changes
27 * durably to disk so that they can be recovered in the event of a system
30 /* The logging layer takes care out writing out a sequence of log records to
31 * disk. On disk, each record consists of a header, a data payload, and a
32 * footer. The footer contains a checksum of the record, meant to help with
33 * identifying corrupt log records (we would assume because the log record was
34 * only incompletely written out before a crash, which should only happen for
35 * log records that were not considered committed). */
37 // Rough size limit for a log segment. This is not a firm limit and there are
38 // no absolute guarantees on the size of a log segment.
39 #define LOG_SEGMENT_SIZE (1 << 23)
41 #define HEADER_MAGIC 0x676f4c0a
42 #define FOOTER_MAGIC 0x2e435243
45 uint32_t magic; // HEADER_MAGIC
46 uint64_t offset; // Starting byte offset of the log header
47 uint32_t size; // Size of the data item (bytes)
48 BlueSkyCloudID id; // Object identifier
49 } __attribute__((packed));
52 uint32_t magic; // FOOTER_MAGIC
53 uint32_t crc; // Computed from log_header to log_footer.magic
54 } __attribute__((packed));
56 static void writebuf(int fd, const char *buf, size_t len)
60 written = write(fd, buf, len);
61 if (written < 0 && errno == EINTR)
63 g_assert(written >= 0);
69 static void log_commit(BlueSkyLog *log)
77 while (log->committed != NULL) {
78 BlueSkyCloudLog *item = (BlueSkyCloudLog *)log->committed->data;
79 g_mutex_lock(item->lock);
80 item->pending_write &= ~CLOUDLOG_JOURNAL;
81 item->location_flags |= CLOUDLOG_JOURNAL;
82 g_cond_signal(item->cond);
83 g_mutex_unlock(item->lock);
84 log->committed = g_slist_delete_link(log->committed, log->committed);
88 if (bluesky_verbose && batchsize > 1)
89 g_print("Log batch size: %d\n", batchsize);
92 static gboolean log_open(BlueSkyLog *log)
103 while (log->fd < 0) {
104 g_snprintf(logname, sizeof(logname), "log-%08d", log->seq_num);
105 log->fd = openat(log->dirfd, logname, O_CREAT|O_WRONLY|O_EXCL, 0600);
106 if (log->fd < 0 && errno == EEXIST) {
107 fprintf(stderr, "Log file %s already exists...\n", logname);
110 } else if (log->fd < 0) {
111 fprintf(stderr, "Error opening logfile %s: %m\n", logname);
116 if (ftruncate(log->fd, LOG_SEGMENT_SIZE) < 0) {
117 fprintf(stderr, "Unable to truncate logfile %s: %m\n", logname);
124 /* All log writes (at least for a single log) are made by one thread, so we
125 * don't need to worry about concurrent access to the log file. Log items to
126 * write are pulled off a queue (and so may be posted by any thread).
127 * fdatasync() is used to ensure the log items are stable on disk.
129 * The log is broken up into separate files, roughly of size LOG_SEGMENT_SIZE
130 * each. If a log segment is not currently open (log->fd is negative), a new
131 * one is created. Log segment filenames are assigned sequentially.
133 * Log replay ought to be implemented later, and ought to set the initial
134 * sequence number appropriately.
136 static gpointer log_thread(gpointer d)
138 BlueSkyLog *log = (BlueSkyLog *)d;
142 if (!log_open(log)) {
147 BlueSkyCloudLog *item
148 = (BlueSkyCloudLog *)g_async_queue_pop(log->queue);
149 g_mutex_lock(item->lock);
150 g_assert(item->data != NULL);
152 /* The item may have already been written to the journal... */
153 if ((item->location_flags | item->pending_write) & CLOUDLOG_JOURNAL) {
154 g_mutex_unlock(item->lock);
155 bluesky_cloudlog_unref(item);
159 item->pending_write |= CLOUDLOG_JOURNAL;
161 struct log_header header;
162 struct log_footer footer;
163 size_t size = sizeof(header) + sizeof(footer) + item->data->len;
166 offset = lseek(log->fd, 0, SEEK_CUR);
168 /* Check whether the item would overflow the allocated journal size.
169 * If so, start a new log segment. We only allow oversized log
170 * segments if they contain a single log entry. */
171 if (offset + size >= LOG_SEGMENT_SIZE && offset > 0) {
176 header.magic = GUINT32_TO_LE(HEADER_MAGIC);
177 header.offset = GUINT64_TO_LE(offset);
178 header.size = GUINT32_TO_LE(item->data->len);
179 header.id = item->id;
180 footer.magic = GUINT32_TO_LE(FOOTER_MAGIC);
182 uint32_t crc = BLUESKY_CRC32C_SEED;
184 writebuf(log->fd, (const char *)&header, sizeof(header));
185 crc = crc32c(crc, (const char *)&header, sizeof(header));
187 writebuf(log->fd, item->data->data, item->data->len);
188 crc = crc32c(crc, item->data->data, item->data->len);
190 crc = crc32c(crc, (const char *)&footer,
191 sizeof(footer) - sizeof(uint32_t));
192 footer.crc = crc32c_finalize(crc);
193 writebuf(log->fd, (const char *)&footer, sizeof(footer));
195 item->log_seq = log->seq_num;
196 item->log_offset = offset + sizeof(header);
197 item->log_size = item->data->len;
199 offset += sizeof(header) + sizeof(footer) + item->data->len;
201 log->committed = g_slist_prepend(log->committed, item);
202 g_mutex_unlock(item->lock);
204 /* Force an if there are no other log items currently waiting to be
206 if (g_async_queue_length(log->queue) <= 0)
213 BlueSkyLog *bluesky_log_new(const char *log_directory)
215 BlueSkyLog *log = g_new0(BlueSkyLog, 1);
217 log->log_directory = g_strdup(log_directory);
220 log->queue = g_async_queue_new();
221 log->mmap_lock = g_mutex_new();
222 log->mmap_cache = g_hash_table_new(NULL, NULL);
224 log->dirfd = open(log->log_directory, O_DIRECTORY);
225 if (log->dirfd < 0) {
226 fprintf(stderr, "Unable to open logging directory: %m\n");
230 g_thread_create(log_thread, log, FALSE, NULL);
235 void bluesky_log_item_submit(BlueSkyCloudLog *item, BlueSkyLog *log)
237 bluesky_cloudlog_ref(item);
238 g_async_queue_push(log->queue, item);
241 void bluesky_log_finish_all(GList *log_items)
243 while (log_items != NULL) {
244 BlueSkyCloudLog *item = (BlueSkyCloudLog *)log_items->data;
246 g_mutex_lock(item->lock);
247 while ((item->pending_write & CLOUDLOG_JOURNAL))
248 g_cond_wait(item->cond, item->lock);
249 g_mutex_unlock(item->lock);
250 bluesky_cloudlog_unref(item);
252 log_items = g_list_delete_link(log_items, log_items);
256 /* Memory-map the given log object into memory (read-only) and return a pointer
258 static int page_size = 0;
260 BlueSkyRCStr *bluesky_log_map_object(BlueSkyLog *log,
261 int log_seq, int log_offset, int log_size)
263 if (page_size == 0) {
264 page_size = getpagesize();
268 g_mutex_lock(log->mmap_lock);
269 map = g_hash_table_lookup(log->mmap_cache, GINT_TO_POINTER(log_seq));
273 g_snprintf(logname, sizeof(logname), "log-%08d", log_seq);
274 int fd = openat(log->dirfd, logname, O_RDONLY);
277 fprintf(stderr, "Error opening logfile %s: %m\n", logname);
278 g_mutex_unlock(log->mmap_lock);
282 map = g_new0(BlueSkyMmap, 1);
284 off_t length = lseek(fd, 0, SEEK_END);
285 map->log_seq = log_seq;
286 map->addr = (const char *)mmap(NULL, length, PROT_READ, MAP_SHARED,
290 g_atomic_int_set(&map->refcount, 0);
292 g_hash_table_insert(log->mmap_cache, GINT_TO_POINTER(log_seq), map);
297 g_mutex_unlock(log->mmap_lock);
299 return bluesky_string_new_from_mmap(map, log_offset, log_size);
302 void bluesky_mmap_unref(BlueSkyMmap *mmap)
307 if (g_atomic_int_dec_and_test(&mmap->refcount)) {
308 /* There is a potential race condition here: the BlueSkyLog contains a
309 * hash table of currently-existing BlueSkyMmap objects, which does not
310 * hold a reference. Some other thread might grab a new reference to
311 * this object after reading it from the hash table. So, before
312 * destruction we need to grab the lock for the hash table, then check
313 * the reference count again. If it is still zero, we can proceed with
314 * object destruction. */
315 BlueSkyLog *log = mmap->log;
316 g_mutex_lock(log->mmap_lock);
317 if (g_atomic_int_get(&mmap->refcount) > 0) {
318 g_mutex_unlock(log->mmap_lock);
322 g_hash_table_remove(log->mmap_cache, GINT_TO_POINTER(mmap->log_seq));
323 munmap((void *)mmap->addr, mmap->len);
325 g_mutex_unlock(log->mmap_lock);