Skip to contentSkip to conversation

bcachefs: fix last_seq and last_empty_seq in bch2_fs_journal_start()

PATCH Superseded Camila Alvarez 2 participants 3 messages May 16, 2024 – May 20, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs: fix last_seq and last_empty_seq in bch2_fs_journal_start()

Values were left as the next possible sequence number when there were no entries.

The fix involves updating the last_seq initial value and setting last_empty_seq to cur_seq - 1.

---
 fs/bcachefs/journal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bcachefs/journal.c +2 -2
index adec8e1ea73e..3835c458eec9 100644
@@ -1196,7 +1196,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	struct journal_replay *i, **_i;
 	struct genradix_iter iter;
 	bool had_entries = false;
-	u64 last_seq = cur_seq, nr, seq;
+	u64 last_seq = cur_seq - 1, nr, seq;
 
 	genradix_for_each_reverse(&c->journal_entries, iter, _i) {
 		i = *_i;
@@ -1256,7 +1256,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	}
 
 	if (!had_entries)
-		j->last_empty_seq = cur_seq;
+		j->last_empty_seq = cur_seq - 1;
 
 	spin_lock(&j->lock);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Camila Alvarez

Values were left as the next possible sequence number when there were no entries.

The fix involves updating the last_seq initial value and setting last_empty_seq to cur_seq - 1.

I think this is correct, but we should try to come up with some better assertions or something to make the code clearer; we don't want off by ones to lurk so easily.

Could you give it some thought?

Quoted 32 lines
---
 fs/bcachefs/journal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bcachefs/journal.c +2 -2
index adec8e1ea73e..3835c458eec9 100644
@@ -1196,7 +1196,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	struct journal_replay *i, **_i;
 	struct genradix_iter iter;
 	bool had_entries = false;
-	u64 last_seq = cur_seq, nr, seq;
+	u64 last_seq = cur_seq - 1, nr, seq;
 
 	genradix_for_each_reverse(&c->journal_entries, iter, _i) {
 		i = *_i;
@@ -1256,7 +1256,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	}
 
 	if (!had_entries)
-		j->last_empty_seq = cur_seq;
+		j->last_empty_seq = cur_seq - 1;
 
 	spin_lock(&j->lock);
 
Signature · 1 line
2.34.1
#2 lore raw
Quoted 4 lines · Kent Overstreet
Quoted 3 lines · Camila Alvarez

Values were left as the next possible sequence number when there were no entries.

Quoted 9 lines
Quoted 2 lines

The fix involves updating the last_seq initial value and setting last_empty_seq to cur_seq - 1.

I think this is correct, but we should try to come up with some better assertions or something to make the code clearer; we don't want off by ones to lurk so easily.

Could you give it some thought?

You're right. I think the code is written in a confusing way. In particular it seems that cur_seq - 1 is used all over the place. I believe we can abstract cur_seq - 1 in an independent variable (since it represents the actual last sequence number), that should make it clearer. I'll share an updated version of the patch.
Thanks for the response!

Quoted 33 lines
Quoted 32 lines
---
 fs/bcachefs/journal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bcachefs/journal.c +2 -3
index adec8e1ea73e..3835c458eec9 100644
@@ -1196,7 +1196,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	struct journal_replay *i, **_i;
 	struct genradix_iter iter;
 	bool had_entries = false;
-	u64 last_seq = cur_seq, nr, seq;
+	u64 last_seq = cur_seq - 1, nr, seq;
 
 	genradix_for_each_reverse(&c->journal_entries, iter, _i) {
 		i = *_i;
@@ -1256,7 +1256,7 @@ int bch2_fs_journal_start(struct journal *j, u64 cur_seq)
 	}
 
 	if (!had_entries)
-		j->last_empty_seq = cur_seq;
+		j->last_empty_seq = cur_seq - 1;
 
 	spin_lock(&j->lock);
 
--

2.34.1