BlueSkyLog *bluesky_log_new(const char *log_directory);
void bluesky_log_item_submit(BlueSkyCloudLog *item, BlueSkyLog *log);
void bluesky_log_finish_all(GList *log_items);
+BlueSkyRCStr *bluesky_log_map_object(BlueSkyLog *log, int log_seq,
+ int log_offset, int log_size);
#ifdef __cplusplus
}
gchar *bluesky_lowercase(const gchar *s);
/* Reference-counted blocks of memory, used for passing data in and out of
- * storage backends and in other places. */
+ * storage backends and in other places. This may also refer to read-only
+ * mmaped data. */
typedef struct {
gint refcount;
+ const char *addr;
+ size_t len;
+} BlueSkyMmap;
+
+typedef struct {
+ gint refcount;
+ BlueSkyMmap *mmap;
gchar *data;
gsize len;
} BlueSkyRCStr;
BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len);
BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s);
+BlueSkyRCStr *bluesky_string_new_from_mmap(BlueSkyMmap *mmap,
+ int offset, gsize len);
void bluesky_string_ref(BlueSkyRCStr *string);
void bluesky_string_unref(BlueSkyRCStr *string);
BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string);
* - Writing batches of data to the cloud.
*/
+#if 0
static void writeback_complete(gpointer a, gpointer i)
{
BlueSkyInode *inode = (BlueSkyInode *)i;
g_mutex_unlock(inode->lock);
}
+#endif
static void flushd_dirty_inode(BlueSkyInode *inode)
{
state->data = NULL;
}
+
+/* Ensure that a cloud log item is loaded in memory, and if not read it in.
+ * TODO: Make asynchronous, and make this also fetch from the cloud. Right now
+ * we only read from the log. Log item must be locked. */
+void bluesky_cloudlog_fetch(BlueSkyCloudLog *log)
+{
+ if (log->data != NULL)
+ return;
+
+ g_print("Re-mapping log entry %d/%d/%d...\n",
+ log->log_seq, log->log_offset, log->log_size);
+
+ g_assert(log->location_flags & CLOUDLOG_JOURNAL);
+
+ log->data = bluesky_log_map_object(log->fs->log, log->log_seq,
+ log->log_offset, log->log_size);
+}
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
+#include <sys/mman.h>
#include "bluesky-private.h"
log_items = g_list_delete_link(log_items, log_items);
}
}
+
+/* Memory-map the given log object into memory (read-only) and return a pointer
+ * to it. FIXME: Use some type of cache, map entire log segments, and use
+ * reference counting? */
+static int page_size = 0;
+
+BlueSkyRCStr *bluesky_log_map_object(BlueSkyLog *log,
+ int log_seq, int log_offset, int log_size)
+{
+ if (page_size == 0) {
+ page_size = getpagesize();
+ }
+
+ int dirfd = open(log->log_directory, O_DIRECTORY);
+ if (dirfd < 0) {
+ fprintf(stderr, "Unable to open logging directory: %m\n");
+ return NULL;
+ }
+
+ char logfile[64];
+ g_snprintf(logfile, sizeof(logfile), "log-%08d", log_seq);
+ int fd = openat(dirfd, logfile, O_RDONLY);
+ close(dirfd);
+
+ if (fd < 0) {
+ fprintf(stderr, "Error opening logfile %s: %m\n", logfile);
+ return NULL;
+ }
+
+ off_t off_start, off_end;
+ off_start = log_offset;
+ off_end = off_start + log_size;
+ off_start &= ~(page_size - 1);
+ off_end = (off_end + (page_size - 1)) & (page_size - 1);
+
+ const char *ptr = (const char *)mmap(NULL, off_end - off_start, PROT_READ,
+ MAP_SHARED, fd, off_start);
+
+ if (ptr == NULL) {
+ fprintf(stderr, "Error mapping logfile: %m\n");
+ close(fd);
+ return NULL;
+ }
+
+ close(fd);
+
+ BlueSkyMmap *mmap = g_new0(BlueSkyMmap, 1);
+ mmap->addr = ptr;
+ mmap->len = off_end - off_start;
+ g_atomic_int_set(&mmap->refcount, 1);
+
+ return bluesky_string_new_from_mmap(mmap,
+ log_offset - off_start, log_size);
+}
#include <stdint.h>
#include <glib.h>
#include <string.h>
+#include <sys/mman.h>
#include "bluesky-private.h"
/**** Reference-counted strings. ****/
+void bluesky_mmap_unref(BlueSkyMmap *mmap)
+{
+ if (mmap == NULL)
+ return;
+
+ if (g_atomic_int_dec_and_test(&mmap->refcount)) {
+ munmap((void *)mmap->addr, mmap->len);
+ g_free(mmap);
+ }
+}
+
/* Create and return a new reference-counted string. The reference count is
* initially one. The newly-returned string takes ownership of the memory
* pointed at by data, and will call g_free on it when the reference count
BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len)
{
BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
+ string->mmap = NULL;
string->data = data;
string->len = len;
g_atomic_int_set(&string->refcount, 1);
return bluesky_string_new(g_string_free(s, FALSE), len);
}
+/* Create a new BlueSkyRCStr from a memory-mapped buffer. */
+BlueSkyRCStr *bluesky_string_new_from_mmap(BlueSkyMmap *mmap,
+ int offset, gsize len)
+{
+ BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
+ string->mmap = mmap;
+ g_atomic_int_inc(&mmap->refcount);
+ string->data = (char *)mmap->addr + offset;
+ string->len = len;
+ g_atomic_int_set(&string->refcount, 1);
+ return string;
+}
+
void bluesky_string_ref(BlueSkyRCStr *string)
{
if (string == NULL)
return;
if (g_atomic_int_dec_and_test(&string->refcount)) {
- g_free(string->data);
+ if (string->mmap == NULL) {
+ g_free(string->data);
+ } else {
+ bluesky_mmap_unref(string->mmap);
+ }
g_free(string);
}
}
if (string == NULL)
return NULL;
+ if (string->mmap != NULL) {
+ BlueSkyRCStr *s;
+ s = bluesky_string_new(g_memdup(string->data, string->len),
+ string->len);
+ bluesky_string_unref(string);
+ return s;
+ }
+
if (g_atomic_int_dec_and_test(&string->refcount)) {
/* There are no other shared copies, so return this one. */
g_atomic_int_inc(&string->refcount);
* if needed). */
void bluesky_string_resize(BlueSkyRCStr *string, gsize len)
{
+ g_assert(string->mmap == NULL);
+
if (string->len == len)
return;