X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fjournal%2Fjournal-file.c;h=c9030c56addb5e7dac5f6d19c23b9b70d9fae234;hp=304ce03bdd6f480e29b0eb2b67d701f43214a9b3;hb=b9a1617d75c16a48cccf4ff135013dca9af94e7d;hpb=2678031a179a9b91fc799f8ef951a548c66c4b49 diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index 304ce03bd..c9030c56a 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -27,6 +27,7 @@ #include #include +#include "btrfs-util.h" #include "journal-def.h" #include "journal-file.h" #include "journal-authenticate.h" @@ -140,6 +141,9 @@ void journal_file_close(JournalFile *f) { if (f->mmap && f->fd >= 0) mmap_cache_close_fd(f->mmap, f->fd); + if (f->fd >= 0 && f->defrag_on_close) + btrfs_defrag_fd(f->fd); + safe_close(f->fd); free(f->path); @@ -2741,6 +2745,11 @@ int journal_file_rotate(JournalFile **f, bool compress, bool seal) { old_file->header->state = STATE_ARCHIVED; + /* Currently, btrfs is not very good with out write patterns + * and fragments heavily. Let's defrag our journal files when + * we archive them */ + old_file->defrag_on_close = true; + r = journal_file_open(old_file->path, old_file->flags, old_file->mode, compress, seal, NULL, old_file->mmap, old_file, &new_file); journal_file_close(old_file); @@ -2771,7 +2780,8 @@ int journal_file_open_reliably( r != -EPROTONOSUPPORT && /* incompatible feature */ r != -EBUSY && /* unclean shutdown */ r != -ESHUTDOWN && /* already archived */ - r != -EIO /* IO error, including SIGBUS on mmap */) + r != -EIO && /* IO error, including SIGBUS on mmap */ + r != -EIDRM /* File has been deleted */) return r; if ((flags & O_ACCMODE) == O_RDONLY) @@ -2796,6 +2806,10 @@ int journal_file_open_reliably( if (r < 0) return -errno; + /* btrfs doesn't cope well with our write pattern and + * fragments heavily. Let's defrag all files we rotate */ + (void) btrfs_defrag(p); + log_warning("File %s corrupted or uncleanly shut down, renaming and replacing.", fname); return journal_file_open(fname, flags, mode, compress, seal,