chiark / gitweb /
journal: it's not a problem if the realtime jumps, hence don't ensure monotonicity...
[elogind.git] / src / journal / journal-file.c
index 80775e1acf940f46a1f7a44125c950c4ae21c076..4de1daf6a0b177bed7105fc32ea493cc8da4a95c 100644 (file)
 
 #define COMPRESSION_SIZE_THRESHOLD (64ULL)
 
+/* This is the minimum journal file size */
+#define JOURNAL_FILE_SIZE_MIN (64ULL*1024ULL)
+
+/* These are the lower and upper bounds if we deduce the max_use value
+ * from the file system size */
+#define DEFAULT_MAX_USE_LOWER (1ULL*1024ULL*1024ULL)           /* 1 MiB */
+#define DEFAULT_MAX_USE_UPPER (4ULL*1024ULL*1024ULL*1024ULL)   /* 4 GiB */
+
+/* This is the upper bound if we deduce max_size from max_use */
+#define DEFAULT_MAX_SIZE_UPPER (16ULL*1024ULL*1024ULL)         /* 16 MiB */
+
+/* This is the upper bound if we deduce the keep_free value from the
+ * file system size */
+#define DEFAULT_KEEP_FREE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
+
+/* This is the keep_free value when we can't determine the system
+ * size */
+#define DEFAULT_KEEP_FREE (1024ULL*1024ULL)                    /* 1 MB */
+
 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
 
 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
@@ -119,6 +138,9 @@ static int journal_file_refresh_header(JournalFile *f) {
         f->header->boot_id = boot_id;
 
         f->header->state = STATE_ONLINE;
+
+        __sync_synchronize();
+
         return 0;
 }
 
@@ -241,6 +263,10 @@ static int journal_file_map(
         wsize = size + (offset - woffset);
         wsize = PAGE_ALIGN(wsize);
 
+        /* Avoid SIGBUS on invalid accesses */
+        if (woffset + wsize > (uint64_t) PAGE_ALIGN(f->last_stat.st_size))
+                return -EADDRNOTAVAIL;
+
         window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
         if (window == MAP_FAILED)
                 return -errno;
@@ -260,7 +286,7 @@ static int journal_file_map(
 }
 
 static int journal_file_move_to(JournalFile *f, int wt, uint64_t offset, uint64_t size, void **ret) {
-        void *p;
+        void *p = NULL;
         uint64_t delta;
         int r;
         Window *w;
@@ -270,6 +296,15 @@ static int journal_file_move_to(JournalFile *f, int wt, uint64_t offset, uint64_
         assert(wt >= 0);
         assert(wt < _WINDOW_MAX);
 
+        if (offset + size > (uint64_t) f->last_stat.st_size) {
+                /* Hmm, out of range? Let's refresh the fstat() data
+                 * first, before we trust that check. */
+
+                if (fstat(f->fd, &f->last_stat) < 0 ||
+                    offset + size > (uint64_t) f->last_stat.st_size)
+                        return -EADDRNOTAVAIL;
+        }
+
         w = f->windows + wt;
 
         if (_likely_(w->ptr &&
@@ -297,14 +332,20 @@ static int journal_file_move_to(JournalFile *f, int wt, uint64_t offset, uint64_
 
                 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
 
-                if (offset < delta)
+                if (delta > offset)
                         delta = offset;
 
                 offset -= delta;
-                size += (DEFAULT_WINDOW_SIZE - delta);
+                size = DEFAULT_WINDOW_SIZE;
         } else
                 delta = 0;
 
+        if (offset + size > (uint64_t) f->last_stat.st_size)
+                size = PAGE_ALIGN((uint64_t) f->last_stat.st_size - offset);
+
+        if (size <= 0)
+                return -EADDRNOTAVAIL;
+
         r = journal_file_map(f,
                              offset, size,
                              &w->ptr, &w->offset, &w->size,
@@ -866,6 +907,8 @@ static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
         assert(offset > 0);
         assert(o->object.type == OBJECT_ENTRY);
 
+        __sync_synchronize();
+
         /* Link up the entry itself */
         r = link_entry_into_array(f,
                                   &f->header->entry_array_offset,
@@ -937,7 +980,7 @@ static int journal_file_append_entry_internal(
         return 0;
 }
 
-static void journal_file_post_change(JournalFile *f) {
+void journal_file_post_change(JournalFile *f) {
         assert(f);
 
         /* inotify() does not receive IN_MODIFY events from file
@@ -973,12 +1016,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
             ts->monotonic < le64toh(f->header->tail_entry_monotonic))
                 return -EINVAL;
 
-        if (ts->realtime < le64toh(f->header->tail_entry_realtime))
-                return -EINVAL;
-
-        items = new(EntryItem, n_iovec);
-        if (!items)
-                return -ENOMEM;
+        items = alloca(sizeof(EntryItem) * n_iovec);
 
         for (i = 0; i < n_iovec; i++) {
                 uint64_t p;
@@ -986,7 +1024,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
 
                 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
                 if (r < 0)
-                        goto finish;
+                        return r;
 
                 xor_hash ^= le64toh(o->data.hash);
                 items[i].object_offset = htole64(p);
@@ -997,9 +1035,6 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
 
         journal_file_post_change(f);
 
-finish:
-        free(items);
-
         return r;
 }
 
@@ -1009,7 +1044,7 @@ static int generic_array_get(JournalFile *f,
                              Object **ret, uint64_t *offset) {
 
         Object *o;
-        uint64_t p, a;
+        uint64_t p = 0, a;
         int r;
 
         assert(f);
@@ -1693,10 +1728,6 @@ int journal_file_open(
         f->writable = (flags & O_ACCMODE) != O_RDONLY;
         f->prot = prot_from_flags(flags);
 
-        f->metrics.max_size = DEFAULT_MAX_SIZE;
-        f->metrics.min_size = DEFAULT_MIN_SIZE;
-        f->metrics.keep_free = DEFAULT_KEEP_FREE;
-
         f->path = strdup(fname);
         if (!f->path) {
                 r = -ENOMEM;
@@ -1869,7 +1900,7 @@ int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t m
         assert(directory);
 
         if (max_use <= 0)
-                max_use = DEFAULT_MAX_USE;
+                return 0;
 
         d = opendir(directory);
         if (!d)
@@ -1986,3 +2017,160 @@ finish:
 
         return r;
 }
+
+int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint64_t p, uint64_t *seqnum, Object **ret, uint64_t *offset) {
+        uint64_t i, n;
+        uint64_t q, xor_hash = 0;
+        int r;
+        EntryItem *items;
+        dual_timestamp ts;
+
+        assert(from);
+        assert(to);
+        assert(o);
+        assert(p);
+
+        if (!to->writable)
+                return -EPERM;
+
+        ts.monotonic = le64toh(o->entry.monotonic);
+        ts.realtime = le64toh(o->entry.realtime);
+
+        if (to->tail_entry_monotonic_valid &&
+            ts.monotonic < le64toh(to->header->tail_entry_monotonic))
+                return -EINVAL;
+
+        if (ts.realtime < le64toh(to->header->tail_entry_realtime))
+                return -EINVAL;
+
+        n = journal_file_entry_n_items(o);
+        items = alloca(sizeof(EntryItem) * n);
+
+        for (i = 0; i < n; i++) {
+                uint64_t le_hash, l, h;
+                size_t t;
+                void *data;
+                Object *u;
+
+                q = le64toh(o->entry.items[i].object_offset);
+                le_hash = o->entry.items[i].hash;
+
+                r = journal_file_move_to_object(from, OBJECT_DATA, q, &o);
+                if (r < 0)
+                        return r;
+
+                if (le_hash != o->data.hash)
+                        return -EBADMSG;
+
+                l = le64toh(o->object.size) - offsetof(Object, data.payload);
+                t = (size_t) l;
+
+                /* We hit the limit on 32bit machines */
+                if ((uint64_t) t != l)
+                        return -E2BIG;
+
+                if (o->object.flags & OBJECT_COMPRESSED) {
+#ifdef HAVE_XZ
+                        uint64_t rsize;
+
+                        if (!uncompress_blob(o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize))
+                                return -EBADMSG;
+
+                        data = from->compress_buffer;
+                        l = rsize;
+#else
+                        return -EPROTONOSUPPORT;
+#endif
+                } else
+                        data = o->data.payload;
+
+                r = journal_file_append_data(to, data, l, &u, &h);
+                if (r < 0)
+                        return r;
+
+                xor_hash ^= le64toh(u->data.hash);
+                items[i].object_offset = htole64(h);
+                items[i].hash = u->data.hash;
+
+                r = journal_file_move_to_object(from, OBJECT_ENTRY, p, &o);
+                if (r < 0)
+                        return r;
+        }
+
+        return journal_file_append_entry_internal(to, &ts, xor_hash, items, n, seqnum, ret, offset);
+}
+
+void journal_default_metrics(JournalMetrics *m, int fd) {
+        uint64_t fs_size = 0;
+        struct statvfs ss;
+        char a[64], b[64], c[64], d[64];
+
+        assert(m);
+        assert(fd >= 0);
+
+        if (fstatvfs(fd, &ss) >= 0)
+                fs_size = ss.f_frsize * ss.f_blocks;
+
+        if (m->max_use == (uint64_t) -1) {
+
+                if (fs_size > 0) {
+                        m->max_use = PAGE_ALIGN(fs_size / 10); /* 10% of file system size */
+
+                        if (m->max_use > DEFAULT_MAX_USE_UPPER)
+                                m->max_use = DEFAULT_MAX_USE_UPPER;
+
+                        if (m->max_use < DEFAULT_MAX_USE_LOWER)
+                                m->max_use = DEFAULT_MAX_USE_LOWER;
+                } else
+                        m->max_use = DEFAULT_MAX_USE_LOWER;
+        } else {
+                m->max_use = PAGE_ALIGN(m->max_use);
+
+                if (m->max_use < JOURNAL_FILE_SIZE_MIN*2)
+                        m->max_use = JOURNAL_FILE_SIZE_MIN*2;
+        }
+
+        if (m->max_size == (uint64_t) -1) {
+                m->max_size = PAGE_ALIGN(m->max_use / 8); /* 8 chunks */
+
+                if (m->max_size > DEFAULT_MAX_SIZE_UPPER)
+                        m->max_size = DEFAULT_MAX_SIZE_UPPER;
+        } else
+                m->max_size = PAGE_ALIGN(m->max_size);
+
+        if (m->max_size < JOURNAL_FILE_SIZE_MIN)
+                m->max_size = JOURNAL_FILE_SIZE_MIN;
+
+        if (m->max_size*2 > m->max_use)
+                m->max_use = m->max_size*2;
+
+        if (m->min_size == (uint64_t) -1)
+                m->min_size = JOURNAL_FILE_SIZE_MIN;
+        else {
+                m->min_size = PAGE_ALIGN(m->min_size);
+
+                if (m->min_size < JOURNAL_FILE_SIZE_MIN)
+                        m->min_size = JOURNAL_FILE_SIZE_MIN;
+
+                if (m->min_size > m->max_size)
+                        m->max_size = m->min_size;
+        }
+
+        if (m->keep_free == (uint64_t) -1) {
+
+                if (fs_size > 0) {
+                        m->keep_free = PAGE_ALIGN(fs_size / 20); /* 5% of file system size */
+
+                        if (m->keep_free > DEFAULT_KEEP_FREE_UPPER)
+                                m->keep_free = DEFAULT_KEEP_FREE_UPPER;
+
+                } else
+                        m->keep_free = DEFAULT_KEEP_FREE;
+        }
+
+        log_debug("Fixed max_use=%s max_size=%s min_size=%s keep_free=%s",
+                  format_bytes(a, sizeof(a), m->max_use),
+                  format_bytes(b, sizeof(b), m->max_size),
+                  format_bytes(c, sizeof(c), m->min_size),
+                  format_bytes(d, sizeof(d), m->keep_free));
+}