Skip to content
Snippets Groups Projects
Commit 4f7b8404 authored by Marek Vavruša's avatar Marek Vavruša
Browse files

journal: prevent unlimited growth on shrinking/overfill

parent 45e9ed7a
Branches
Tags
No related merge requests found
......@@ -547,7 +547,11 @@ int journal_write_in(journal_t *j, journal_node_t **rn, uint64_t id, size_t len)
(unsigned long long)id, j->qtail, len, j->fsize);
/* Calculate remaining bytes to reach file size limit. */
size_t fs_remaining = j->fslimit - j->fsize;
size_t fs_remaining = 0;
if (j->fsize < j->fslimit) {
fs_remaining = j->fslimit - j->fsize;
}
int seek_ret = 0;
/* Increase free segment if on the end of file. */
......
......@@ -19,6 +19,7 @@
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <tap/basic.h>
#include "knot/server/journal.h"
......@@ -36,7 +37,7 @@ static int randstr(char* dst, size_t len)
int main(int argc, char *argv[])
{
plan(10);
plan_lazy();
/* Create tmpdir */
int fsize = 10 * 1024 * 1024;
......@@ -112,6 +113,33 @@ int main(int argc, char *argv[])
}
is_int(KNOT_EOK, ret, "journal: sustained mmap r/w");
/* Overfill */
ret = journal_map(journal, chk_key, &mptr, fsize, false);
is_int(KNOT_ESPACE, ret, "journal: overfill");
/* Fillup */
tskey = 0xBEEF0000;
size_t large_entry_len = 512 * 1024;
char *large_entry = malloc(512 * 1024);
assert(large_entry);
randstr(large_entry, large_entry_len);
for (int i = 0; i < 512; ++i) {
chk_key = tskey + i;
ret = journal_map(journal, chk_key, &mptr, large_entry_len, false);
if (ret != KNOT_EOK) {
break;
}
journal_unmap(journal, chk_key, mptr, 1);
}
is_int(KNOT_EBUSY, ret, "journal: fillup");
free(large_entry);
/* Check file size. */
struct stat st;
stat(journal->path, &st);
ok(st.st_size < fsize + large_entry_len, "journal: fillup file size check");
/* Close journal. */
journal_close(journal);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment