journal-verify.c revision a8e5f51484ba832e299a38f2a54e455e445d2896
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2012 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <unistd.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/mman.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <fcntl.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen#include "util.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "macro.h"
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering#include "journal-def.h"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering#include "journal-file.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "journal-authenticate.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "journal-verify.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "lookup3.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "compress.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "fsprg.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/* FIXME:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * - verify FSPRG
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering * - Allow building without libgcrypt
b93312f5960b276bae915906ccde36f545bae3e0Zbigniew Jędrzejewski-Szmek * - check with sparse
b93312f5960b276bae915906ccde36f545bae3e0Zbigniew Jędrzejewski-Szmek * - 64bit conversions
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * */
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic int journal_file_object_verify(JournalFile *f, Object *o) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering assert(f);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering assert(o);
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack /* This does various superficial tests about the length an
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack * possible field values. It does not follow any references to
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering * other objects. */
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if ((o->object.flags & OBJECT_COMPRESSED) &&
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering o->object.type != OBJECT_DATA)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return -EBADMSG;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering switch (o->object.type) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering case OBJECT_DATA: {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering uint64_t h1, h2;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (le64toh(o->data.entry_offset) <= 0 ||
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering le64toh(o->data.n_entries) <= 0)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return -EBADMSG;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (le64toh(o->object.size) - offsetof(DataObject, payload) <= 0)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return -EBADMSG;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering h1 = le64toh(o->data.hash);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (o->object.flags & OBJECT_COMPRESSED) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering void *b = NULL;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering uint64_t alloc = 0, b_size;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (!uncompress_blob(o->data.payload,
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering le64toh(o->object.size) - offsetof(Object, data.payload),
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering &b, &alloc, &b_size))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EBADMSG;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering h2 = hash64(b, b_size);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(b);
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack } else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering h2 = hash64(o->data.payload, le64toh(o->object.size) - offsetof(Object, data.payload));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if (h1 != h2)
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return -EBADMSG;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering break;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering }
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering case OBJECT_FIELD:
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if (le64toh(o->object.size) - offsetof(FieldObject, payload) <= 0)
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return -EBADMSG;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering break;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering case OBJECT_ENTRY:
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if ((le64toh(o->object.size) - offsetof(EntryObject, items)) % sizeof(EntryItem) != 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return -EBADMSG;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if ((le64toh(o->object.size) - offsetof(EntryObject, items)) / sizeof(EntryItem) <= 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return -EBADMSG;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (le64toh(o->entry.seqnum) <= 0 ||
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering le64toh(o->entry.realtime) <= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EBADMSG;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering case OBJECT_DATA_HASH_TABLE:
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering case OBJECT_FIELD_HASH_TABLE:
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if ((le64toh(o->object.size) - offsetof(HashTableObject, items)) % sizeof(HashItem) != 0)
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return -EBADMSG;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((le64toh(o->object.size) - offsetof(HashTableObject, items)) / sizeof(HashItem) <= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EBADMSG;
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering case OBJECT_ENTRY_ARRAY:
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if ((le64toh(o->object.size) - offsetof(EntryArrayObject, items)) % sizeof(le64_t) != 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return -EBADMSG;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if ((le64toh(o->object.size) - offsetof(EntryArrayObject, items)) / sizeof(le64_t) <= 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return -EBADMSG;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering case OBJECT_TAG:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (le64toh(o->object.size) != sizeof(TagObject))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EBADMSG;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
c0eb11cfd016381fe02875a4ef29c1ade00c94e7Lennart Poetteringstatic void draw_progress(uint64_t p, usec_t *last_usec) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unsigned n, i, j, k;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering usec_t z, x;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!isatty(STDOUT_FILENO))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering z = now(CLOCK_MONOTONIC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering x = *last_usec;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (x != 0 && x + 40 * USEC_PER_MSEC > z)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering *last_usec = z;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering n = (3 * columns()) / 4;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering j = (n * (unsigned) p) / 65535ULL;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering k = n - j;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek fputs("\r\x1B[?25l" ANSI_HIGHLIGHT_GREEN_ON, stdout);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek for (i = 0; i < j; i++)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek fputs("\xe2\x96\x88", stdout);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fputs(ANSI_HIGHLIGHT_OFF, stdout);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek for (i = 0; i < k; i++)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fputs("\xe2\x96\x91", stdout);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek printf(" %3lu%%", 100LU * (unsigned long) p / 65535LU);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fputs("\r\x1B[?25h", stdout);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek fflush(stdout);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersenstatic void flush_progress(void) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen unsigned n, i;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (!isatty(STDOUT_FILENO))
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen n = (3 * columns()) / 4;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen putchar('\r');
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering for (i = 0; i < n + 5; i++)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering putchar(' ');
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen putchar('\r');
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering fflush(stdout);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int write_uint64(int fd, uint64_t p) {
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering ssize_t k;
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek k = write(fd, &p, sizeof(p));
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (k < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return -errno;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (k != sizeof(p))
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return -EIO;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekstatic int contains_uint64(MMapCache *m, int fd, uint64_t n, uint64_t p) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek uint64_t a, b;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(m);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(fd >= 0);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek /* Bisection ... */
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen a = 0; b = n;
9ead3519c54b6d1b79b35541873b5cf7c8b3a7d3Lennart Poettering while (a < b) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen uint64_t c, *z;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen c = (a + b) / 2;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = mmap_cache_get(m, fd, PROT_READ|PROT_WRITE, 0, c * sizeof(uint64_t), sizeof(uint64_t), (void **) &z);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (*z == p)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return 1;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (p < *z)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen b = c;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen else
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen a = c;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int entry_points_to_data(
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering JournalFile *f,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int entry_fd,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering uint64_t n_entries,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering uint64_t entry_p,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering uint64_t data_p) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering uint64_t i, n, a;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen Object *o;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering bool found = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(f);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(entry_fd >= 0);
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!contains_uint64(f->mmap, entry_fd, n_entries, entry_p)) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_error("Data object references invalid entry at %llu", (unsigned long long) data_p);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return -EBADMSG;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = journal_file_move_to_object(f, OBJECT_ENTRY, entry_p, &o);
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack if (r < 0)
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack return r;
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack n = journal_file_entry_n_items(o);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering for (i = 0; i < n; i++)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (le64toh(o->entry.items[i].object_offset) == data_p) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering found = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!found) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_error("Data object not referenced by linked entry at %llu", (unsigned long long) data_p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -EBADMSG;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering /* Check if this entry is also in main entry array. Since the
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * main entry array has already been verified we can rely on
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering * its consistency.*/
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering n = le64toh(f->header->n_entries);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering a = le64toh(f->header->entry_array_offset);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt i = 0;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering while (i < n) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering uint64_t m, j;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (r < 0)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r;
m = journal_file_entry_array_n_items(o);
for (j = 0; i < n && j < m; i++, j++)
if (le64toh(o->entry_array.items[j]) == entry_p)
return 0;
a = le64toh(o->entry_array.next_entry_array_offset);;
}
return 0;
}
static int verify_data(
JournalFile *f,
Object *o, uint64_t p,
int entry_fd, uint64_t n_entries,
int entry_array_fd, uint64_t n_entry_arrays) {
uint64_t i, n, a, last, q;
int r;
assert(f);
assert(o);
assert(entry_fd >= 0);
assert(entry_array_fd >= 0);
n = le64toh(o->data.n_entries);
a = le64toh(o->data.entry_array_offset);
/* We already checked this earlier */
assert(n > 0);
last = q = le64toh(o->data.entry_offset);
r = entry_points_to_data(f, entry_fd, n_entries, q, p);
if (r < 0)
return r;
i = 1;
while (i < n) {
uint64_t next, m, j;
if (a == 0) {
log_error("Array chain too short at %llu.", (unsigned long long) p);
return -EBADMSG;
}
if (!contains_uint64(f->mmap, entry_array_fd, n_entry_arrays, a)) {
log_error("Invalid array at %llu.", (unsigned long long) p);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
if (r < 0)
return r;
next = le64toh(o->entry_array.next_entry_array_offset);
if (next != 0 && next <= a) {
log_error("Array chain has cycle at %llu.", (unsigned long long) p);
return -EBADMSG;
}
m = journal_file_entry_array_n_items(o);
for (j = 0; i < n && j < m; i++, j++) {
q = le64toh(o->entry_array.items[j]);
if (q <= last) {
log_error("Data object's entry array not sorted at %llu.", (unsigned long long) p);
return -EBADMSG;
}
last = q;
r = entry_points_to_data(f, entry_fd, n_entries, q, p);
if (r < 0)
return r;
/* Pointer might have moved, reposition */
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
if (r < 0)
return r;
}
a = next;
}
return 0;
}
static int verify_hash_table(
JournalFile *f,
int data_fd, uint64_t n_data,
int entry_fd, uint64_t n_entries,
int entry_array_fd, uint64_t n_entry_arrays,
usec_t *last_usec) {
uint64_t i, n;
int r;
assert(f);
assert(data_fd >= 0);
assert(entry_fd >= 0);
assert(entry_array_fd >= 0);
assert(last_usec);
n = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
for (i = 0; i < n; i++) {
uint64_t last = 0, p;
draw_progress(0xC000 + (0x3FFF * i / n), last_usec);
p = le64toh(f->data_hash_table[i].head_hash_offset);
while (p != 0) {
Object *o;
uint64_t next;
if (!contains_uint64(f->mmap, data_fd, n_data, p)) {
log_error("Invalid data object at hash entry %llu of %llu.",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
if (r < 0)
return r;
next = le64toh(o->data.next_hash_offset);
if (next != 0 && next <= p) {
log_error("Hash chain has a cycle in hash entry %llu of %llu.",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
if (le64toh(o->data.hash) % n != i) {
log_error("Hash value mismatch in hash entry %llu of %llu.",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
r = verify_data(f, o, p, entry_fd, n_entries, entry_array_fd, n_entry_arrays);
if (r < 0)
return r;
last = p;
p = next;
}
if (last != le64toh(f->data_hash_table[i].tail_hash_offset)) {
log_error("Tail hash pointer mismatch in hash table.");
return -EBADMSG;
}
}
return 0;
}
static int data_object_in_hash_table(JournalFile *f, uint64_t hash, uint64_t p) {
uint64_t n, h, q;
int r;
assert(f);
n = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
h = hash % n;
q = le64toh(f->data_hash_table[h].head_hash_offset);
while (q != 0) {
Object *o;
if (p == q)
return 1;
r = journal_file_move_to_object(f, OBJECT_DATA, q, &o);
if (r < 0)
return r;
q = le64toh(o->data.next_hash_offset);
}
return 0;
}
static int verify_entry(
JournalFile *f,
Object *o, uint64_t p,
int data_fd, uint64_t n_data) {
uint64_t i, n;
int r;
assert(f);
assert(o);
assert(data_fd >= 0);
n = journal_file_entry_n_items(o);
for (i = 0; i < n; i++) {
uint64_t q, h;
Object *u;
q = le64toh(o->entry.items[i].object_offset);
h = le64toh(o->entry.items[i].hash);
if (!contains_uint64(f->mmap, data_fd, n_data, q)) {
log_error("Invalid data object at entry %llu.",
(unsigned long long) o);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_DATA, q, &u);
if (r < 0)
return r;
if (le64toh(u->data.hash) != h) {
log_error("Hash mismatch for data object at entry %llu.",
(unsigned long long) p);
return -EBADMSG;
}
r = data_object_in_hash_table(f, h, q);
if (r < 0)
return r;
if (r == 0) {
log_error("Data object missing from hash at entry %llu.",
(unsigned long long) p);
return -EBADMSG;
}
}
return 0;
}
static int verify_entry_array(
JournalFile *f,
int data_fd, uint64_t n_data,
int entry_fd, uint64_t n_entries,
int entry_array_fd, uint64_t n_entry_arrays,
usec_t *last_usec) {
uint64_t i = 0, a, n, last = 0;
int r;
assert(f);
assert(data_fd >= 0);
assert(entry_fd >= 0);
assert(entry_array_fd >= 0);
assert(last_usec);
n = le64toh(f->header->n_entries);
a = le64toh(f->header->entry_array_offset);
while (i < n) {
uint64_t next, m, j;
Object *o;
draw_progress(0x8000 + (0x3FFF * i / n), last_usec);
if (a == 0) {
log_error("Array chain too short at %llu of %llu.",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
if (!contains_uint64(f->mmap, entry_array_fd, n_entry_arrays, a)) {
log_error("Invalid array at %llu of %llu.",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
if (r < 0)
return r;
next = le64toh(o->entry_array.next_entry_array_offset);
if (next != 0 && next <= a) {
log_error("Array chain has cycle at %llu of %llu.",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
m = journal_file_entry_array_n_items(o);
for (j = 0; i < n && j < m; i++, j++) {
uint64_t p;
p = le64toh(o->entry_array.items[j]);
if (p <= last) {
log_error("Entry array not sorted at %llu of %llu.",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
last = p;
if (!contains_uint64(f->mmap, entry_fd, n_entries, p)) {
log_error("Invalid array entry at %llu of %llu.",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
if (r < 0)
return r;
r = verify_entry(f, o, p, data_fd, n_data);
if (r < 0)
return r;
/* Pointer might have moved, reposition */
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
if (r < 0)
return r;
}
a = next;
}
return 0;
}
static int journal_file_parse_seed(JournalFile *f, const char *s) {
uint8_t *seed;
size_t seed_size, c;
const char *k;
int r;
unsigned long long start, interval;
seed_size = FSPRG_RECOMMENDED_SEEDLEN;
seed = malloc(seed_size);
if (!seed)
return -ENOMEM;
k = s;
for (c = 0; c < seed_size; c++) {
int x, y;
while (*k == '-')
k++;
x = unhexchar(*k);
if (x < 0) {
free(seed);
return -EINVAL;
}
k++;
y = unhexchar(*k);
if (y < 0) {
free(seed);
return -EINVAL;
}
k++;
seed[c] = (uint8_t) (x * 16 + y);
}
if (*k != '/') {
free(seed);
return -EINVAL;
}
k++;
r = sscanf(k, "%llx-%llx", &start, &interval);
if (r != 2) {
free(seed);
return -EINVAL;
}
f->fsprg_seed = seed;
f->fsprg_seed_size = seed_size;
f->fsprg_start_usec = start;
f->fsprg_interval_usec = interval;
return 0;
}
int journal_file_verify(JournalFile *f, const char *seed) {
int r;
Object *o;
uint64_t p = 0, last_tag = 0;
uint64_t n_tags = 0, entry_seqnum = 0, entry_monotonic = 0, entry_realtime = 0;
sd_id128_t entry_boot_id;
bool entry_seqnum_set = false, entry_monotonic_set = false, entry_realtime_set = false, found_main_entry_array = false;
uint64_t n_weird = 0, n_objects = 0, n_entries = 0, n_data = 0, n_fields = 0, n_data_hash_tables = 0, n_field_hash_tables = 0, n_entry_arrays = 0;
usec_t last_usec = 0;
int data_fd = -1, entry_fd = -1, entry_array_fd = -1;
char data_path[] = "/var/tmp/journal-data-XXXXXX",
entry_path[] = "/var/tmp/journal-entry-XXXXXX",
entry_array_path[] = "/var/tmp/journal-entry-array-XXXXXX";
assert(f);
if (seed) {
r = journal_file_parse_seed(f, seed);
if (r < 0) {
log_error("Failed to parse seed.");
return r;
}
}
data_fd = mkostemp(data_path, O_CLOEXEC);
if (data_fd < 0) {
log_error("Failed to create data file: %m");
r = -errno;
goto fail;
}
unlink(data_path);
entry_fd = mkostemp(entry_path, O_CLOEXEC);
if (entry_fd < 0) {
log_error("Failed to create entry file: %m");
r = -errno;
goto fail;
}
unlink(entry_path);
entry_array_fd = mkostemp(entry_array_path, O_CLOEXEC);
if (entry_array_fd < 0) {
log_error("Failed to create entry array file: %m");
r = -errno;
goto fail;
}
unlink(entry_array_path);
/* First iteration: we go through all objects, verify the
* superficial structure, headers, hashes. */
p = le64toh(f->header->header_size);
while (p != 0) {
draw_progress(0x7FFF * p / le64toh(f->header->tail_object_offset), &last_usec);
r = journal_file_move_to_object(f, -1, p, &o);
if (r < 0) {
log_error("Invalid object at %llu", (unsigned long long) p);
goto fail;
}
if (le64toh(f->header->tail_object_offset) < p) {
log_error("Invalid tail object pointer.");
r = -EBADMSG;
goto fail;
}
n_objects ++;
r = journal_file_object_verify(f, o);
if (r < 0) {
log_error("Invalid object contents at %llu", (unsigned long long) p);
goto fail;
}
if (o->object.flags & OBJECT_COMPRESSED &&
!(le32toh(f->header->incompatible_flags) & HEADER_INCOMPATIBLE_COMPRESSED)) {
log_error("Compressed object without compression at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
switch (o->object.type) {
case OBJECT_DATA:
r = write_uint64(data_fd, p);
if (r < 0)
goto fail;
n_data++;
break;
case OBJECT_FIELD:
n_fields++;
break;
case OBJECT_ENTRY:
r = write_uint64(entry_fd, p);
if (r < 0)
goto fail;
if (!entry_seqnum_set &&
le64toh(o->entry.seqnum) != le64toh(f->header->head_entry_seqnum)) {
log_error("Head entry sequence number incorrect");
r = -EBADMSG;
goto fail;
}
if (entry_seqnum_set &&
entry_seqnum >= le64toh(o->entry.seqnum)) {
log_error("Entry sequence number out of synchronization at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
entry_seqnum = le64toh(o->entry.seqnum);
entry_seqnum_set = true;
if (entry_monotonic_set &&
sd_id128_equal(entry_boot_id, o->entry.boot_id) &&
entry_monotonic > le64toh(o->entry.monotonic)) {
log_error("Entry timestamp out of synchronization at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
entry_monotonic = le64toh(o->entry.monotonic);
entry_boot_id = o->entry.boot_id;
entry_monotonic_set = true;
if (!entry_realtime_set &&
le64toh(o->entry.realtime) != le64toh(f->header->head_entry_realtime)) {
log_error("Head entry realtime timestamp incorrect");
r = -EBADMSG;
goto fail;
}
entry_realtime = le64toh(o->entry.realtime);
entry_realtime_set = true;
n_entries ++;
break;
case OBJECT_DATA_HASH_TABLE:
if (n_data_hash_tables > 1) {
log_error("More than one data hash table at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
if (le64toh(f->header->data_hash_table_offset) != p + offsetof(HashTableObject, items) ||
le64toh(f->header->data_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) {
log_error("Header fields for data hash table invalid.");
r = -EBADMSG;
goto fail;
}
n_data_hash_tables++;
break;
case OBJECT_FIELD_HASH_TABLE:
if (n_field_hash_tables > 1) {
log_error("More than one field hash table at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
if (le64toh(f->header->field_hash_table_offset) != p + offsetof(HashTableObject, items) ||
le64toh(f->header->field_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) {
log_error("Header fields for field hash table invalid.");
r = -EBADMSG;
goto fail;
}
n_field_hash_tables++;
break;
case OBJECT_ENTRY_ARRAY:
r = write_uint64(entry_array_fd, p);
if (r < 0)
goto fail;
if (p == le64toh(f->header->entry_array_offset)) {
if (found_main_entry_array) {
log_error("More than one main entry array at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
found_main_entry_array = true;
}
n_entry_arrays++;
break;
case OBJECT_TAG:
if (!(le32toh(f->header->compatible_flags) & HEADER_COMPATIBLE_AUTHENTICATED)) {
log_error("Tag object without authentication at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
if (le64toh(o->tag.seqnum) != n_tags + 1) {
log_error("Tag sequence number out of synchronization at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
n_tags ++;
break;
default:
n_weird ++;
}
if (p == le64toh(f->header->tail_object_offset))
p = 0;
else
p = p + ALIGN64(le64toh(o->object.size));
}
if (n_objects != le64toh(f->header->n_objects)) {
log_error("Object number mismatch");
r = -EBADMSG;
goto fail;
}
if (n_entries != le64toh(f->header->n_entries)) {
log_error("Entry number mismatch");
r = -EBADMSG;
goto fail;
}
if (JOURNAL_HEADER_CONTAINS(f->header, n_data) &&
n_data != le64toh(f->header->n_data)) {
log_error("Data number mismatch");
r = -EBADMSG;
goto fail;
}
if (JOURNAL_HEADER_CONTAINS(f->header, n_fields) &&
n_fields != le64toh(f->header->n_fields)) {
log_error("Field number mismatch");
r = -EBADMSG;
goto fail;
}
if (JOURNAL_HEADER_CONTAINS(f->header, n_tags) &&
n_tags != le64toh(f->header->n_tags)) {
log_error("Tag number mismatch");
r = -EBADMSG;
goto fail;
}
if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays) &&
n_entry_arrays != le64toh(f->header->n_entry_arrays)) {
log_error("Entry array number mismatch");
r = -EBADMSG;
goto fail;
}
if (n_data_hash_tables != 1) {
log_error("Missing data hash table");
r = -EBADMSG;
goto fail;
}
if (n_field_hash_tables != 1) {
log_error("Missing field hash table");
r = -EBADMSG;
goto fail;
}
if (!found_main_entry_array) {
log_error("Missing entry array");
r = -EBADMSG;
goto fail;
}
if (entry_seqnum_set &&
entry_seqnum != le64toh(f->header->tail_entry_seqnum)) {
log_error("Invalid tail seqnum");
r = -EBADMSG;
goto fail;
}
if (entry_monotonic_set &&
(!sd_id128_equal(entry_boot_id, f->header->boot_id) ||
entry_monotonic != le64toh(f->header->tail_entry_monotonic))) {
log_error("Invalid tail monotonic timestamp");
r = -EBADMSG;
goto fail;
}
if (entry_realtime_set && entry_realtime != le64toh(f->header->tail_entry_realtime)) {
log_error("Invalid tail realtime timestamp");
r = -EBADMSG;
goto fail;
}
/* Second iteration: we follow all objects referenced from the
* two entry points: the object hash table and the entry
* array. We also check that everything referenced (directly
* or indirectly) in the data hash table also exists in the
* entry array, and vice versa. Note that we do not care for
* unreferenced objects. We only care that everything that is
* referenced is consistent. */
r = verify_entry_array(f,
data_fd, n_data,
entry_fd, n_entries,
entry_array_fd, n_entry_arrays,
&last_usec);
if (r < 0)
goto fail;
r = verify_hash_table(f,
data_fd, n_data,
entry_fd, n_entries,
entry_array_fd, n_entry_arrays,
&last_usec);
if (r < 0)
goto fail;
flush_progress();
mmap_cache_close_fd(f->mmap, data_fd);
mmap_cache_close_fd(f->mmap, entry_fd);
mmap_cache_close_fd(f->mmap, entry_array_fd);
close_nointr_nofail(data_fd);
close_nointr_nofail(entry_fd);
close_nointr_nofail(entry_array_fd);
return 0;
fail:
flush_progress();
log_error("File corruption detected at %s:%llu (of %llu, %llu%%).",
f->path,
(unsigned long long) p,
(unsigned long long) f->last_stat.st_size,
(unsigned long long) (100 * p / f->last_stat.st_size));
if (data_fd >= 0) {
mmap_cache_close_fd(f->mmap, data_fd);
close_nointr_nofail(data_fd);
}
if (entry_fd >= 0) {
mmap_cache_close_fd(f->mmap, entry_fd);
close_nointr_nofail(entry_fd);
}
if (entry_array_fd >= 0) {
mmap_cache_close_fd(f->mmap, entry_array_fd);
close_nointr_nofail(entry_array_fd);
}
return r;
}