Final changes before pull request to Linus
[ext4-patch-queue.git] / fix-handling-of-extended-tv_sec
blobe46942ab645bd1b4c5ebfca1e3ce1a1916ca36b2
1 From: David Turner <novalis@novalis.org>
3 ext4: Fix handling of extended tv_sec
5 In ext4, the bottom two bits of {a,c,m}time_extra are used to extend
6 the {a,c,m}time fields, deferring the year 2038 problem to the year
7 2446.
9 When decoding these extended fields, for times whose bottom 32 bits
10 would represent a negative number, sign extension causes the 64-bit
11 extended timestamp to be negative as well, which is not what's
12 intended.  This patch corrects that issue, so that the only negative
13 {a,c,m}times are those between 1901 and 1970 (as per 32-bit signed
14 timestamps).
16 Some older kernels might have written pre-1970 dates with 1,1 in the
17 extra bits.  This patch treats those incorrectly-encoded dates as
18 pre-1970, instead of post-2311, until kernel 4.20 is released.
19 Hopefully by then e2fsck will have fixed up the bad data.
21 Also add a comment explaining the encoding of ext4's extra {a,c,m}time
22 bits.
24 Signed-off-by: David Turner <novalis@novalis.org>
25 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
26 Reported-by: Mark Harris <mh8928@yahoo.com>
27 Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=23732
28 Cc: stable@vger.kernel.org
29 ---
30  fs/ext4/ext4.h | 51 ++++++++++++++++++++++++++++++++++++++++++++-------
31  1 file changed, 44 insertions(+), 7 deletions(-)
33 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
34 index 750063f..cc7ca4e 100644
35 --- a/fs/ext4/ext4.h
36 +++ b/fs/ext4/ext4.h
37 @@ -26,6 +26,7 @@
38  #include <linux/seqlock.h>
39  #include <linux/mutex.h>
40  #include <linux/timer.h>
41 +#include <linux/version.h>
42  #include <linux/wait.h>
43  #include <linux/blockgroup_lock.h>
44  #include <linux/percpu_counter.h>
45 @@ -727,19 +728,55 @@ struct move_extent {
46         <= (EXT4_GOOD_OLD_INODE_SIZE +                  \
47             (einode)->i_extra_isize))                   \
49 +/*
50 + * We use an encoding that preserves the times for extra epoch "00":
51 + *
52 + * extra  msb of                         adjust for signed
53 + * epoch  32-bit                         32-bit tv_sec to
54 + * bits   time    decoded 64-bit tv_sec  64-bit tv_sec      valid time range
55 + * 0 0    1    -0x80000000..-0x00000001  0x000000000 1901-12-13..1969-12-31
56 + * 0 0    0    0x000000000..0x07fffffff  0x000000000 1970-01-01..2038-01-19
57 + * 0 1    1    0x080000000..0x0ffffffff  0x100000000 2038-01-19..2106-02-07
58 + * 0 1    0    0x100000000..0x17fffffff  0x100000000 2106-02-07..2174-02-25
59 + * 1 0    1    0x180000000..0x1ffffffff  0x200000000 2174-02-25..2242-03-16
60 + * 1 0    0    0x200000000..0x27fffffff  0x200000000 2242-03-16..2310-04-04
61 + * 1 1    1    0x280000000..0x2ffffffff  0x300000000 2310-04-04..2378-04-22
62 + * 1 1    0    0x300000000..0x37fffffff  0x300000000 2378-04-22..2446-05-10
63 + *
64 + * Note that previous versions of the kernel on 64-bit systems would
65 + * incorrectly use extra epoch bits 1,1 for dates between 1901 and
66 + * 1970.  e2fsck will correct this, assuming that it is run on the
67 + * affected filesystem before 2242.
68 + */
70  static inline __le32 ext4_encode_extra_time(struct timespec *time)
71  {
72 -       return cpu_to_le32((sizeof(time->tv_sec) > 4 ?
73 -                          (time->tv_sec >> 32) & EXT4_EPOCH_MASK : 0) |
74 -                          ((time->tv_nsec << EXT4_EPOCH_BITS) & EXT4_NSEC_MASK));
75 +       u32 extra = sizeof(time->tv_sec) > 4 ?
76 +               ((time->tv_sec - (s32)time->tv_sec) >> 32) & EXT4_EPOCH_MASK : 0;
77 +       return cpu_to_le32(extra | (time->tv_nsec << EXT4_EPOCH_BITS));
78  }
80  static inline void ext4_decode_extra_time(struct timespec *time, __le32 extra)
81  {
82 -       if (sizeof(time->tv_sec) > 4)
83 -              time->tv_sec |= (__u64)(le32_to_cpu(extra) & EXT4_EPOCH_MASK)
84 -                              << 32;
85 -       time->tv_nsec = (le32_to_cpu(extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
86 +       if (unlikely(sizeof(time->tv_sec) > 4 &&
87 +                       (extra & cpu_to_le32(EXT4_EPOCH_MASK)))) {
88 +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
89 +               /* Handle legacy encoding of pre-1970 dates with epoch
90 +                * bits 1,1.  We assume that by kernel version 4.20,
91 +                * everyone will have run fsck over the affected
92 +                * filesystems to correct the problem.  (This
93 +                * backwards compatibility may be removed before this
94 +                * time, at the discretion of the ext4 developers.)
95 +                */
96 +               u64 extra_bits = le32_to_cpu(extra) & EXT4_EPOCH_MASK;
97 +               if (extra_bits == 3 && ((time->tv_sec) & 0x80000000) != 0)
98 +                       extra_bits = 0;
99 +               time->tv_sec += extra_bits << 32;
100 +#else
101 +               time->tv_sec += (u64)(le32_to_cpu(extra) & EXT4_EPOCH_MASK) << 32;
102 +#endif
103 +       }
104 +       time->tv_nsec = (le32_to_cpu(extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
107  #define EXT4_INODE_SET_XTIME(xtime, inode, raw_inode)                         \