X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fjournal%2Fjournal-file.c;h=d3535d2fadcb2edad6756bc8915c93f76e09473b;hp=b7e5cf0ab5bff9576aa74b2e155874f62e80612b;hb=92261977d81fd6a5bfb1418eddd86582d8a57fcd;hpb=0d647f7f73fa17074a1df81e2bdd00877554e436 diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index b7e5cf0ab..d3535d2fa 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -26,10 +26,7 @@ #include #include #include - -#ifdef HAVE_XATTR -#include -#endif +#include #include "journal-def.h" #include "journal-file.h" @@ -133,9 +130,7 @@ void journal_file_close(JournalFile *f) { if (f->header) munmap(f->header, PAGE_ALIGN(sizeof(Header))); - if (f->fd >= 0) - close_nointr_nofail(f->fd); - + safe_close(f->fd); free(f->path); if (f->mmap) @@ -143,7 +138,7 @@ void journal_file_close(JournalFile *f) { hashmap_free_free(f->chain_cache); -#ifdef HAVE_XZ +#if defined(HAVE_XZ) || defined(HAVE_LZ4) free(f->compress_buffer); #endif @@ -163,21 +158,21 @@ void journal_file_close(JournalFile *f) { } static int journal_file_init_header(JournalFile *f, JournalFile *template) { - Header h; + Header h = {}; ssize_t k; int r; assert(f); - zero(h); memcpy(h.signature, HEADER_SIGNATURE, 8); h.header_size = htole64(ALIGN64(sizeof(h))); - h.incompatible_flags = - htole32(f->compress ? HEADER_INCOMPATIBLE_COMPRESSED : 0); + h.incompatible_flags |= htole32( + f->compress_xz * HEADER_INCOMPATIBLE_COMPRESSED_XZ | + f->compress_lz4 * HEADER_INCOMPATIBLE_COMPRESSED_LZ4); - h.compatible_flags = - htole32(f->seal ? HEADER_COMPATIBLE_SEALED : 0); + h.compatible_flags = htole32( + f->seal * HEADER_COMPATIBLE_SEALED); r = sd_id128_randomize(&h.file_id); if (r < 0) @@ -227,6 +222,8 @@ static int journal_file_refresh_header(JournalFile *f) { } static int journal_file_verify_header(JournalFile *f) { + uint32_t flags; + assert(f); if (memcmp(f->header->signature, HEADER_SIGNATURE, 8)) @@ -234,24 +231,30 @@ static int journal_file_verify_header(JournalFile *f) { /* In both read and write mode we refuse to open files with * incompatible flags we don't know */ -#ifdef HAVE_XZ - if ((le32toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_COMPRESSED) != 0) - return -EPROTONOSUPPORT; -#else - if (f->header->incompatible_flags != 0) + flags = le32toh(f->header->incompatible_flags); + if (flags & ~HEADER_INCOMPATIBLE_SUPPORTED) { + if (flags & ~HEADER_INCOMPATIBLE_ANY) + log_debug("Journal file %s has unknown incompatible flags %"PRIx32, + f->path, flags & ~HEADER_INCOMPATIBLE_ANY); + flags = (flags & HEADER_INCOMPATIBLE_ANY) & ~HEADER_INCOMPATIBLE_SUPPORTED; + if (flags) + log_debug("Journal file %s uses incompatible flags %"PRIx32 + " disabled at compilation time.", f->path, flags); return -EPROTONOSUPPORT; -#endif + } /* When open for writing we refuse to open files with * compatible flags, too */ - if (f->writable) { -#ifdef HAVE_GCRYPT - if ((le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_SEALED) != 0) - return -EPROTONOSUPPORT; -#else - if (f->header->compatible_flags != 0) - return -EPROTONOSUPPORT; -#endif + flags = le32toh(f->header->compatible_flags); + if (f->writable && (flags & ~HEADER_COMPATIBLE_SUPPORTED)) { + if (flags & ~HEADER_COMPATIBLE_ANY) + log_debug("Journal file %s has unknown compatible flags %"PRIx32, + f->path, flags & ~HEADER_COMPATIBLE_ANY); + flags = (flags & HEADER_COMPATIBLE_ANY) & ~HEADER_COMPATIBLE_SUPPORTED; + if (flags) + log_debug("Journal file %s uses compatible flags %"PRIx32 + " disabled at compilation time.", f->path, flags); + return -EPROTONOSUPPORT; } if (f->header->state >= _STATE_MAX) @@ -307,7 +310,8 @@ static int journal_file_verify_header(JournalFile *f) { } } - f->compress = JOURNAL_HEADER_COMPRESSED(f->header); + f->compress_xz = JOURNAL_HEADER_COMPRESSED_XZ(f->header); + f->compress_lz4 = JOURNAL_HEADER_COMPRESSED_LZ4(f->header); f->seal = JOURNAL_HEADER_SEALED(f->header); @@ -419,7 +423,6 @@ int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Objec void *t; Object *o; uint64_t s; - unsigned context; assert(f); assert(ret); @@ -428,10 +431,8 @@ int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Objec if (!VALID64(offset)) return -EFAULT; - /* One context for each type, plus one catch-all for the rest */ - context = type > 0 && type < _OBJECT_TYPE_MAX ? type : 0; - r = journal_file_move_to(f, context, false, offset, sizeof(ObjectHeader), &t); + r = journal_file_move_to(f, type_to_context(type), false, offset, sizeof(ObjectHeader), &t); if (r < 0) return r; @@ -563,7 +564,7 @@ static int journal_file_setup_data_hash_table(JournalFile *f) { if (r < 0) return r; - memset(o->hash_table.items, 0, s); + memzero(o->hash_table.items, s); f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items)); f->header->data_hash_table_size = htole64(s); @@ -589,7 +590,7 @@ static int journal_file_setup_field_hash_table(JournalFile *f) { if (r < 0) return r; - memset(o->hash_table.items, 0, s); + memzero(o->hash_table.items, s); f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items)); f->header->field_hash_table_size = htole64(s); @@ -817,8 +818,7 @@ int journal_file_find_data_object_with_hash( if (le64toh(o->data.hash) != hash) goto next; - if (o->object.flags & OBJECT_COMPRESSED) { -#ifdef HAVE_XZ + if (o->object.flags & OBJECT_COMPRESSION_MASK) { uint64_t l, rsize; l = le64toh(o->object.size); @@ -827,8 +827,10 @@ int journal_file_find_data_object_with_hash( l -= offsetof(Object, data.payload); - if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize, 0)) - return -EBADMSG; + r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK, + o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize, 0); + if (r < 0) + return r; if (rsize == size && memcmp(f->compress_buffer, data, size) == 0) { @@ -841,9 +843,6 @@ int journal_file_find_data_object_with_hash( return 1; } -#else - return -EPROTONOSUPPORT; -#endif } else if (le64toh(o->object.size) == osize && memcmp(o->data.payload, data, size) == 0) { @@ -951,8 +950,7 @@ static int journal_file_append_data( uint64_t hash, p; uint64_t osize; Object *o; - int r; - bool compressed = false; + int r, compression = 0; const void *eq; assert(f); @@ -981,23 +979,24 @@ static int journal_file_append_data( o->data.hash = htole64(hash); -#ifdef HAVE_XZ - if (f->compress && +#if defined(HAVE_XZ) || defined(HAVE_LZ4) + if (f->compress_xz && size >= COMPRESSION_SIZE_THRESHOLD) { uint64_t rsize; - compressed = compress_blob(data, size, o->data.payload, &rsize); + compression = compress_blob(data, size, o->data.payload, &rsize); - if (compressed) { + if (compression) { o->object.size = htole64(offsetof(Object, data.payload) + rsize); - o->object.flags |= OBJECT_COMPRESSED; + o->object.flags |= compression; - log_debug("Compressed data object %"PRIu64" -> %"PRIu64, size, rsize); + log_debug("Compressed data object %"PRIu64" -> %"PRIu64" using %s", + size, rsize, object_compressed_to_string(compression)); } } #endif - if (!compressed && size > 0) + if (!compression && size > 0) memcpy(o->data.payload, data, size); r = journal_file_link_data(f, o, p, hash); @@ -1015,8 +1014,8 @@ static int journal_file_append_data( else eq = memchr(data, '=', size); if (eq && eq > data) { + Object *fo = NULL; uint64_t fp; - Object *fo; /* Create field object ... */ r = journal_file_append_field(f, data, (uint8_t*) eq - (uint8_t*) data, &fo, &fp); @@ -1362,7 +1361,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st } typedef struct ChainCacheItem { - uint64_t first; /* the array at the begin of the chain */ + uint64_t first; /* the array at the beginning of the chain */ uint64_t array; /* the cached array */ uint64_t begin; /* the first item in the cached array */ uint64_t total; /* the total number of items in all arrays before this one in the chain */ @@ -1452,7 +1451,7 @@ static int generic_array_get( found: /* Let's cache this item for the next invocation */ - chain_cache_put(f->chain_cache, ci, first, a, o->entry_array.items[0], t, i); + chain_cache_put(f->chain_cache, ci, first, a, le64toh(o->entry_array.items[0]), t, i); r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); if (r < 0) @@ -1685,7 +1684,7 @@ found: return 0; /* Let's cache this item for the next invocation */ - chain_cache_put(f->chain_cache, ci, first, a, array->entry_array.items[0], t, subtract_one ? (i > 0 ? i-1 : (uint64_t) -1) : i); + chain_cache_put(f->chain_cache, ci, first, a, le64toh(array->entry_array.items[0]), t, subtract_one ? (i > 0 ? i-1 : (uint64_t) -1) : i); if (subtract_one && i == 0) p = last_p; @@ -1948,7 +1947,7 @@ int journal_file_next_entry( direction_t direction, Object **ret, uint64_t *offset) { - uint64_t i, n; + uint64_t i, n, ofs; int r; assert(f); @@ -1989,10 +1988,24 @@ int journal_file_next_entry( } /* And jump to it */ - return generic_array_get(f, - le64toh(f->header->entry_array_offset), - i, - ret, offset); + r = generic_array_get(f, + le64toh(f->header->entry_array_offset), + i, + ret, &ofs); + if (r <= 0) + return r; + + if (p > 0 && + (direction == DIRECTION_DOWN ? ofs <= p : ofs >= p)) { + log_debug("%s: entry array corrupted at entry %"PRIu64, + f->path, i); + return -EBADMSG; + } + + if (offset) + *offset = ofs; + + return 1; } int journal_file_skip_entry( @@ -2213,8 +2226,6 @@ int journal_file_move_to_entry_by_monotonic_for_data( z = q; } - - return 0; } int journal_file_move_to_entry_by_seqnum_for_data( @@ -2328,8 +2339,9 @@ void journal_file_dump(JournalFile *f) { break; } - if (o->object.flags & OBJECT_COMPRESSED) - printf("Flags: COMPRESSED\n"); + if (o->object.flags & OBJECT_COMPRESSION_MASK) + printf("Flags: %s\n", + object_compressed_to_string(o->object.flags & OBJECT_COMPRESSION_MASK)); if (p == le64toh(f->header->tail_object_offset)) p = 0; @@ -2366,7 +2378,7 @@ void journal_file_print_header(JournalFile *f) { "Sequential Number ID: %s\n" "State: %s\n" "Compatible Flags:%s%s\n" - "Incompatible Flags:%s%s\n" + "Incompatible Flags:%s%s%s\n" "Header size: %"PRIu64"\n" "Arena size: %"PRIu64"\n" "Data Hash Table Size: %"PRIu64"\n" @@ -2388,9 +2400,10 @@ void journal_file_print_header(JournalFile *f) { f->header->state == STATE_ONLINE ? "ONLINE" : f->header->state == STATE_ARCHIVED ? "ARCHIVED" : "UNKNOWN", JOURNAL_HEADER_SEALED(f->header) ? " SEALED" : "", - (le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_SEALED) ? " ???" : "", - JOURNAL_HEADER_COMPRESSED(f->header) ? " COMPRESSED" : "", - (le32toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_COMPRESSED) ? " ???" : "", + (le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_ANY) ? " ???" : "", + JOURNAL_HEADER_COMPRESSED_XZ(f->header) ? " COMPRESSED-XZ" : "", + JOURNAL_HEADER_COMPRESSED_LZ4(f->header) ? " COMPRESSED-LZ4" : "", + (le32toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_ANY) ? " ???" : "", le64toh(f->header->header_size), le64toh(f->header->arena_size), le64toh(f->header->data_hash_table_size) / sizeof(HashItem), @@ -2463,8 +2476,10 @@ int journal_file_open( f->flags = flags; f->prot = prot_from_flags(flags); f->writable = (flags & O_ACCMODE) != O_RDONLY; -#ifdef HAVE_XZ - f->compress = compress; +#if defined(HAVE_LZ4) + f->compress_lz4 = compress; +#elif defined(HAVE_XZ) + f->compress_xz = compress; #endif #ifdef HAVE_GCRYPT f->seal = seal; @@ -2504,7 +2519,6 @@ int journal_file_open( } if (f->last_stat.st_size == 0 && f->writable) { -#ifdef HAVE_XATTR uint64_t crtime; /* Let's attach the creation time to the journal file, @@ -2519,7 +2533,6 @@ int journal_file_open( crtime = htole64((uint64_t) now(CLOCK_REALTIME)); fsetxattr(f->fd, "user.crtime_usec", &crtime, sizeof(crtime), XATTR_CREATE); -#endif #ifdef HAVE_GCRYPT /* Try to load the FSPRG state, and if we can't, then @@ -2696,10 +2709,10 @@ int journal_file_open_reliably( /* The file is corrupted. Rotate it away and try it again (but only once) */ l = strlen(fname); - if (asprintf(&p, "%.*s@%016llx-%016llx.journal~", + if (asprintf(&p, "%.*s@%016llx-%016" PRIx64 ".journal~", (int) l - 8, fname, (unsigned long long) now(CLOCK_REALTIME), - random_ull()) < 0) + random_u64()) < 0) return -ENOMEM; r = rename(fname, p); @@ -2758,18 +2771,16 @@ int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint6 if ((uint64_t) t != l) return -E2BIG; - if (o->object.flags & OBJECT_COMPRESSED) { -#ifdef HAVE_XZ + if (o->object.flags & OBJECT_COMPRESSION_MASK) { uint64_t rsize; - if (!uncompress_blob(o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize, 0)) - return -EBADMSG; + r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK, + o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize, 0); + if (r < 0) + return r; data = from->compress_buffer; l = rsize; -#else - return -EPROTONOSUPPORT; -#endif } else data = o->data.payload;