Skip to content

Commit 9872fe4

Browse files
authored
Merge pull request systemd#21527 from vcaputo/journal-file-mfd-centric
journal-file: get rid of JournalFile.mmap
2 parents dcd9d19 + 74fb5be commit 9872fe4

File tree

14 files changed

+89
-50
lines changed

14 files changed

+89
-50
lines changed

src/journal-remote/journal-remote-write.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include "alloc-util.h"
44
#include "journal-remote.h"
55

6-
static int do_rotate(JournaldFile **f, bool compress, bool seal) {
7-
int r = journald_file_rotate(f, compress, UINT64_MAX, seal, NULL);
6+
static int do_rotate(JournaldFile **f, MMapCache *m, bool compress, bool seal) {
7+
int r = journald_file_rotate(f, m, compress, UINT64_MAX, seal, NULL);
88
if (r < 0) {
99
if (*f)
1010
log_error_errno(r, "Failed to rotate %s: %m", (*f)->file->path);
@@ -71,7 +71,7 @@ int writer_write(Writer *w,
7171
if (journal_file_rotate_suggested(w->journal->file, 0, LOG_DEBUG)) {
7272
log_info("%s: Journal header limits reached or header out-of-date, rotating",
7373
w->journal->file->path);
74-
r = do_rotate(&w->journal, compress, seal);
74+
r = do_rotate(&w->journal, w->mmap, compress, seal);
7575
if (r < 0)
7676
return r;
7777
}
@@ -87,7 +87,7 @@ int writer_write(Writer *w,
8787
return r;
8888

8989
log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->file->path);
90-
r = do_rotate(&w->journal, compress, seal);
90+
r = do_rotate(&w->journal, w->mmap, compress, seal);
9191
if (r < 0)
9292
return r;
9393
else

src/journal/journald-file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ JournaldFile* journald_file_initiate_close(JournaldFile *f, Set *deferred_closes
403403

404404
int journald_file_rotate(
405405
JournaldFile **f,
406+
MMapCache *mmap_cache,
406407
bool compress,
407408
uint64_t compress_threshold_bytes,
408409
bool seal,
@@ -428,7 +429,7 @@ int journald_file_rotate(
428429
compress_threshold_bytes,
429430
seal,
430431
NULL, /* metrics */
431-
(*f)->file->mmap,
432+
mmap_cache,
432433
deferred_closes,
433434
*f, /* template */
434435
&new_file);

src/journal/journald-file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ int journald_file_open_reliably(
4040
JournaldFile **ret);
4141

4242
JournaldFile* journald_file_initiate_close(JournaldFile *f, Set *deferred_closes);
43-
int journald_file_rotate(JournaldFile **f, bool compress, uint64_t compress_threshold_bytes, bool seal, Set *deferred_closes);
43+
int journald_file_rotate(JournaldFile **f, MMapCache *mmap_cache, bool compress, uint64_t compress_threshold_bytes, bool seal, Set *deferred_closes);

src/journal/journald-server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ static int do_rotate(
465465
if (!*f)
466466
return -EINVAL;
467467

468-
r = journald_file_rotate(f, s->compress.enabled, s->compress.threshold_bytes, seal, s->deferred_closes);
468+
r = journald_file_rotate(f, s->mmap, s->compress.enabled, s->compress.threshold_bytes, seal, s->deferred_closes);
469469
if (r < 0) {
470470
if (*f)
471471
return log_error_errno(r, "Failed to rotate %s: %m", (*f)->file->path);

src/journal/test-journal-flush.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@
1414
#include "string-util.h"
1515

1616
int main(int argc, char *argv[]) {
17+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
1718
_cleanup_free_ char *fn = NULL;
1819
char dn[] = "/var/tmp/test-journal-flush.XXXXXX";
1920
JournaldFile *new_journal = NULL;
2021
sd_journal *j = NULL;
2122
unsigned n = 0;
2223
int r;
2324

25+
m = mmap_cache_new();
26+
assert_se(m != NULL);
2427
assert_se(mkdtemp(dn));
2528
(void) chattr_path(dn, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
2629

2730
fn = path_join(dn, "test.journal");
2831

29-
r = journald_file_open(-1, fn, O_CREAT|O_RDWR, 0644, false, 0, false, NULL, NULL, NULL, NULL, &new_journal);
32+
r = journald_file_open(-1, fn, O_CREAT|O_RDWR, 0644, false, 0, false, NULL, m, NULL, NULL, &new_journal);
3033
assert_se(r >= 0);
3134

3235
if (argc > 1)

src/journal/test-journal-interleaving.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ _noreturn_ static void log_assert_errno(const char *text, int error, const char
3434
} while (false)
3535

3636
static JournaldFile *test_open(const char *name) {
37+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
3738
JournaldFile *f;
38-
assert_ret(journald_file_open(-1, name, O_RDWR|O_CREAT, 0644, true, UINT64_MAX, false, NULL, NULL, NULL, NULL, &f));
39+
40+
m = mmap_cache_new();
41+
assert_se(m != NULL);
42+
43+
assert_ret(journald_file_open(-1, name, O_RDWR|O_CREAT, 0644, true, UINT64_MAX, false, NULL, m, NULL, NULL, &f));
3944
return f;
4045
}
4146

@@ -198,15 +203,19 @@ static void test_skip(void (*setup)(void)) {
198203

199204
static void test_sequence_numbers(void) {
200205

206+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
201207
char t[] = "/var/tmp/journal-seq-XXXXXX";
202208
JournaldFile *one, *two;
203209
uint64_t seqnum = 0;
204210
sd_id128_t seqnum_id;
205211

212+
m = mmap_cache_new();
213+
assert_se(m != NULL);
214+
206215
mkdtemp_chdir_chattr(t);
207216

208217
assert_se(journald_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0644,
209-
true, UINT64_MAX, false, NULL, NULL, NULL, NULL, &one) == 0);
218+
true, UINT64_MAX, false, NULL, m, NULL, NULL, &one) == 0);
210219

211220
append_number(one, 1, &seqnum);
212221
printf("seqnum=%"PRIu64"\n", seqnum);
@@ -223,7 +232,7 @@ static void test_sequence_numbers(void) {
223232
memcpy(&seqnum_id, &one->file->header->seqnum_id, sizeof(sd_id128_t));
224233

225234
assert_se(journald_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0644,
226-
true, UINT64_MAX, false, NULL, NULL, NULL, one, &two) == 0);
235+
true, UINT64_MAX, false, NULL, m, NULL, one, &two) == 0);
227236

228237
assert_se(two->file->header->state == STATE_ONLINE);
229238
assert_se(!sd_id128_equal(two->file->header->file_id, one->file->header->file_id));
@@ -254,7 +263,7 @@ static void test_sequence_numbers(void) {
254263
seqnum = 0;
255264

256265
assert_se(journald_file_open(-1, "two.journal", O_RDWR, 0,
257-
true, UINT64_MAX, false, NULL, NULL, NULL, NULL, &two) == 0);
266+
true, UINT64_MAX, false, NULL, m, NULL, NULL, &two) == 0);
258267

259268
assert_se(sd_id128_equal(two->file->header->seqnum_id, seqnum_id));
260269

src/journal/test-journal-stream.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static void verify_contents(sd_journal *j, unsigned skip) {
6060
}
6161

6262
static void run_test(void) {
63+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
6364
JournaldFile *one, *two, *three;
6465
char t[] = "/var/tmp/journal-stream-XXXXXX";
6566
unsigned i;
@@ -69,13 +70,16 @@ static void run_test(void) {
6970
size_t l;
7071
dual_timestamp previous_ts = DUAL_TIMESTAMP_NULL;
7172

73+
m = mmap_cache_new();
74+
assert_se(m != NULL);
75+
7276
assert_se(mkdtemp(t));
7377
assert_se(chdir(t) >= 0);
7478
(void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
7579

76-
assert_se(journald_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, false, NULL, NULL, NULL, NULL, &one) == 0);
77-
assert_se(journald_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, false, NULL, NULL, NULL, NULL, &two) == 0);
78-
assert_se(journald_file_open(-1, "three.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, false, NULL, NULL, NULL, NULL, &three) == 0);
80+
assert_se(journald_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, false, NULL, m, NULL, NULL, &one) == 0);
81+
assert_se(journald_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, false, NULL, m, NULL, NULL, &two) == 0);
82+
assert_se(journald_file_open(-1, "three.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, false, NULL, m, NULL, NULL, &three) == 0);
7983

8084
for (i = 0; i < N_ENTRIES; i++) {
8185
char *p, *q;

src/journal/test-journal-verify.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "journald-file.h"
1111
#include "journal-verify.h"
1212
#include "log.h"
13+
#include "mmap-cache.h"
1314
#include "rm-rf.h"
1415
#include "terminal-util.h"
1516
#include "tests.h"
@@ -38,10 +39,14 @@ static void bit_toggle(const char *fn, uint64_t p) {
3839
}
3940

4041
static int raw_verify(const char *fn, const char *verification_key) {
42+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
4143
JournalFile *f;
4244
int r;
4345

44-
r = journal_file_open(-1, fn, O_RDONLY, 0666, true, UINT64_MAX, !!verification_key, NULL, NULL, NULL, &f);
46+
m = mmap_cache_new();
47+
assert_se(m != NULL);
48+
49+
r = journal_file_open(-1, fn, O_RDONLY, 0666, true, UINT64_MAX, !!verification_key, NULL, m, NULL, &f);
4550
if (r < 0)
4651
return r;
4752

@@ -52,6 +57,7 @@ static int raw_verify(const char *fn, const char *verification_key) {
5257
}
5358

5459
int main(int argc, char *argv[]) {
60+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
5561
char t[] = "/var/tmp/journal-XXXXXX";
5662
unsigned n;
5763
JournalFile *f;
@@ -61,6 +67,9 @@ int main(int argc, char *argv[]) {
6167
struct stat st;
6268
uint64_t p;
6369

70+
m = mmap_cache_new();
71+
assert_se(m != NULL);
72+
6473
/* journald_file_open requires a valid machine id */
6574
if (access("/etc/machine-id", F_OK) != 0)
6675
return log_tests_skipped("/etc/machine-id not found");
@@ -73,7 +82,7 @@ int main(int argc, char *argv[]) {
7382

7483
log_info("Generating...");
7584

76-
assert_se(journald_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, !!verification_key, NULL, NULL, NULL, NULL, &df) == 0);
85+
assert_se(journald_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, !!verification_key, NULL, m, NULL, NULL, &df) == 0);
7786

7887
for (n = 0; n < N_ENTRIES; n++) {
7988
struct iovec iovec;
@@ -95,7 +104,7 @@ int main(int argc, char *argv[]) {
95104

96105
log_info("Verifying...");
97106

98-
assert_se(journal_file_open(-1, "test.journal", O_RDONLY, 0666, true, UINT64_MAX, !!verification_key, NULL, NULL, NULL, &f) == 0);
107+
assert_se(journal_file_open(-1, "test.journal", O_RDONLY, 0666, true, UINT64_MAX, !!verification_key, NULL, m, NULL, &f) == 0);
99108
/* journal_file_print_header(f); */
100109
journal_file_dump(f);
101110

src/journal/test-journal.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static void mkdtemp_chdir_chattr(char *path) {
2424
}
2525

2626
static void test_non_empty(void) {
27+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
2728
dual_timestamp ts;
2829
JournaldFile *f;
2930
struct iovec iovec;
@@ -35,9 +36,12 @@ static void test_non_empty(void) {
3536

3637
test_setup_logging(LOG_DEBUG);
3738

39+
m = mmap_cache_new();
40+
assert_se(m != NULL);
41+
3842
mkdtemp_chdir_chattr(t);
3943

40-
assert_se(journald_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, true, NULL, NULL, NULL, NULL, &f) == 0);
44+
assert_se(journald_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, true, NULL, m, NULL, NULL, &f) == 0);
4145

4246
assert_se(dual_timestamp_get(&ts));
4347
assert_se(sd_id128_randomize(&fake_boot_id) == 0);
@@ -98,8 +102,8 @@ static void test_non_empty(void) {
98102

99103
assert_se(journal_file_move_to_entry_by_seqnum(f->file, 10, DIRECTION_DOWN, &o, NULL) == 0);
100104

101-
journald_file_rotate(&f, true, UINT64_MAX, true, NULL);
102-
journald_file_rotate(&f, true, UINT64_MAX, true, NULL);
105+
journald_file_rotate(&f, m, true, UINT64_MAX, true, NULL);
106+
journald_file_rotate(&f, m, true, UINT64_MAX, true, NULL);
103107

104108
(void) journald_file_close(f);
105109

@@ -117,17 +121,21 @@ static void test_non_empty(void) {
117121
}
118122

119123
static void test_empty(void) {
124+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
120125
JournaldFile *f1, *f2, *f3, *f4;
121126
char t[] = "/var/tmp/journal-XXXXXX";
122127

123128
test_setup_logging(LOG_DEBUG);
124129

130+
m = mmap_cache_new();
131+
assert_se(m != NULL);
132+
125133
mkdtemp_chdir_chattr(t);
126134

127-
assert_se(journald_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, false, UINT64_MAX, false, NULL, NULL, NULL, NULL, &f1) == 0);
128-
assert_se(journald_file_open(-1, "test-compress.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, false, NULL, NULL, NULL, NULL, &f2) == 0);
129-
assert_se(journald_file_open(-1, "test-seal.journal", O_RDWR|O_CREAT, 0666, false, UINT64_MAX, true, NULL, NULL, NULL, NULL, &f3) == 0);
130-
assert_se(journald_file_open(-1, "test-seal-compress.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, true, NULL, NULL, NULL, NULL, &f4) == 0);
135+
assert_se(journald_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, false, UINT64_MAX, false, NULL, m, NULL, NULL, &f1) == 0);
136+
assert_se(journald_file_open(-1, "test-compress.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, false, NULL, m, NULL, NULL, &f2) == 0);
137+
assert_se(journald_file_open(-1, "test-seal.journal", O_RDWR|O_CREAT, 0666, false, UINT64_MAX, true, NULL, m, NULL, NULL, &f3) == 0);
138+
assert_se(journald_file_open(-1, "test-seal-compress.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, true, NULL, m, NULL, NULL, &f4) == 0);
131139

132140
journal_file_print_header(f1->file);
133141
puts("");
@@ -156,6 +164,7 @@ static void test_empty(void) {
156164

157165
#if HAVE_COMPRESSION
158166
static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) {
167+
_cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
159168
dual_timestamp ts;
160169
JournaldFile *f;
161170
struct iovec iovec;
@@ -170,9 +179,12 @@ static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) {
170179

171180
test_setup_logging(LOG_DEBUG);
172181

182+
m = mmap_cache_new();
183+
assert_se(m != NULL);
184+
173185
mkdtemp_chdir_chattr(t);
174186

175-
assert_se(journald_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, compress_threshold, true, NULL, NULL, NULL, NULL, &f) == 0);
187+
assert_se(journald_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, compress_threshold, true, NULL, m, NULL, NULL, &f) == 0);
176188

177189
dual_timestamp_get(&ts);
178190

src/libsystemd/sd-journal/journal-file.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,13 @@ JournalFile* journal_file_close(JournalFile *f) {
217217
if (!f)
218218
return NULL;
219219

220-
if (f->mmap && f->cache_fd)
220+
if (f->cache_fd)
221221
mmap_cache_fd_free(f->cache_fd);
222222

223223
if (f->close_fd)
224224
safe_close(f->fd);
225225
free(f->path);
226226

227-
mmap_cache_unref(f->mmap);
228-
229227
ordered_hashmap_free_free(f->chain_cache);
230228

231229
#if HAVE_COMPRESSION
@@ -3258,6 +3256,7 @@ int journal_file_open(
32583256

32593257
assert(ret);
32603258
assert(fd >= 0 || fname);
3259+
assert(mmap_cache);
32613260

32623261
if (!IN_SET((flags & O_ACCMODE), O_RDONLY, O_RDWR))
32633262
return -EINVAL;
@@ -3320,16 +3319,6 @@ int journal_file_open(
33203319
}
33213320
}
33223321

3323-
if (mmap_cache)
3324-
f->mmap = mmap_cache_ref(mmap_cache);
3325-
else {
3326-
f->mmap = mmap_cache_new();
3327-
if (!f->mmap) {
3328-
r = -ENOMEM;
3329-
goto fail;
3330-
}
3331-
}
3332-
33333322
if (fname) {
33343323
f->path = strdup(fname);
33353324
if (!f->path) {
@@ -3371,7 +3360,7 @@ int journal_file_open(
33713360
goto fail;
33723361
}
33733362

3374-
f->cache_fd = mmap_cache_add_fd(f->mmap, f->fd, prot_from_flags(flags));
3363+
f->cache_fd = mmap_cache_add_fd(mmap_cache, f->fd, prot_from_flags(flags));
33753364
if (!f->cache_fd) {
33763365
r = -ENOMEM;
33773366
goto fail;

0 commit comments

Comments
 (0)