Skip to content

Commit

Permalink
bcachefs: Fix negative timespecs
Browse files Browse the repository at this point in the history
This fixes two problems in the handling of negative times:

 • rem is signed, but the rem * c->sb.nsec_per_time_unit operation
   produced a bogus unsigned result, because s32 * u32 = u32.

 • The timespec was not normalized (it could contain more than a
   billion nanoseconds).

For example, { .tv_sec = -14245441, .tv_nsec = 750000000 }, after
being round tripped through timespec_to_bch2_time and then
bch2_time_to_timespec would come back as
{ .tv_sec = -14245440, .tv_nsec = 4044967296 } (more than 4 billion
nanoseconds).

Cc: [email protected]
Fixes: 595c1e9 ("bcachefs: Fix time handling")
Closes: #743
Co-developed-by: Erin Shepherd <[email protected]>
Signed-off-by: Erin Shepherd <[email protected]>
Co-developed-by: Ryan Lahfa <[email protected]>
Signed-off-by: Ryan Lahfa <[email protected]>
Signed-off-by: Alyssa Ross <[email protected]>
Signed-off-by: Kent Overstreet <[email protected]>
  • Loading branch information
alyssais authored and Kent Overstreet committed Sep 7, 2024
1 parent f52cdb3 commit 2d5e650
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions fs/bcachefs/bcachefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1194,12 +1194,15 @@ static inline bool btree_id_cached(const struct bch_fs *c, enum btree_id btree)
static inline struct timespec64 bch2_time_to_timespec(const struct bch_fs *c, s64 time)
{
struct timespec64 t;
s64 sec;
s32 rem;

time += c->sb.time_base_lo;

t.tv_sec = div_s64_rem(time, c->sb.time_units_per_sec, &rem);
t.tv_nsec = rem * c->sb.nsec_per_time_unit;
sec = div_s64_rem(time, c->sb.time_units_per_sec, &rem);

set_normalized_timespec64(&t, sec, rem * (s64)c->sb.nsec_per_time_unit);

return t;
}

Expand Down

0 comments on commit 2d5e650

Please sign in to comment.