add patch avoid-y2038-overflow-in-recently_deleted
[ext4-patch-queue.git] / avoid-y2038-overflow-in-recently_deleted
blob589fd099107feb6ce5c8dd629340217a552e340a
1 ext4: avoid Y2038 overflow in recently_deleted()
3 From: Andreas Dilger <adilger@dilger.ca>
5 Avoid a 32-bit time overflow in recently_deleted() since i_dtime
6 (inode deletion time) is stored only as a 32-bit value on disk.
7 Since i_dtime isn't used for much beyond a boolean value in e2fsck
8 and is otherwise only used in this function in the kernel, there is
9 no benefit to use more space in the inode for this field on disk.
11 Instead, compare only the relative deletion time with the low
12 32 bits of the time using the newly-added time_before32() helper,
13 which is similar to time_before() and time_after() for jiffies.
15 Increase RECENTCY_DIRTY to 300s based on Ted's comments about
16 usage experience at Google.
18 Signed-off-by: Andreas Dilger <adilger@dilger.ca>
19 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
20 Reviewed-by: Arnd Bergmann <arnd@arndb.de>
21 ---
22  fs/ext4/ialloc.c     | 19 +++++++++++++------
23  include/linux/time.h | 15 +++++++++++++++
24  2 files changed, 28 insertions(+), 6 deletions(-)
26 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
27 index 507bfb3..3e73acb 100644
28 --- a/fs/ext4/ialloc.c
29 +++ b/fs/ext4/ialloc.c
30 @@ -692,16 +692,17 @@ static int find_group_other(struct super_block *sb, struct inode *parent,
31   * somewhat arbitrary...)
32   */
33  #define RECENTCY_MIN   5
34 -#define RECENTCY_DIRTY 30
35 +#define RECENTCY_DIRTY 300
37  static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
38  {
39         struct ext4_group_desc  *gdp;
40         struct ext4_inode       *raw_inode;
41         struct buffer_head      *bh;
42 -       unsigned long           dtime, now;
43 -       int     inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
44 -       int     offset, ret = 0, recentcy = RECENTCY_MIN;
45 +       int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
46 +       int offset, ret = 0;
47 +       int recentcy = RECENTCY_MIN;
48 +       u32 dtime, now;
50         gdp = ext4_get_group_desc(sb, group, NULL);
51         if (unlikely(!gdp))
52 @@ -718,12 +719,18 @@ static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
54         offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
55         raw_inode = (struct ext4_inode *) (bh->b_data + offset);
57 +       /* i_dtime is only 32 bits on disk, but we only care about relative
58 +        * times in the range of a few minutes (i.e. long enough to sync a
59 +        * recently-deleted inode to disk), so using the low 32 bits of the
60 +        * clock (a 68 year range) is enough, see time_before32() */
61         dtime = le32_to_cpu(raw_inode->i_dtime);
62 -       now = get_seconds();
63 +       now = ktime_get_real_seconds();
64         if (buffer_dirty(bh))
65                 recentcy += RECENTCY_DIRTY;
67 -       if (dtime && (dtime < now) && (now < dtime + recentcy))
68 +       if (dtime && time_before32(dtime, now) &&
69 +           time_before32(now, dtime + recentcy))
70                 ret = 1;
71  out:
72         brelse(bh);
73 diff --git a/include/linux/time.h b/include/linux/time.h
74 index 4abb32d..3877136 100644
75 --- a/include/linux/time.h
76 +++ b/include/linux/time.h
77 @@ -285,4 +285,19 @@ static inline bool itimerspec64_valid(const struct itimerspec64 *its)
78         return true;
79  }
81 +/**
82 + * time_after32 - compare two 32-bit relative times
83 + * @a: the time which may be after @b
84 + * @b: the time which may be before @a
85 + *
86 + * time_after32(a, b) returns true if the time @a is after time @b.
87 + * time_before32(b, a) returns true if the time @b is before time @a.
88 + *
89 + * Similar to time_after(), compare two 32-bit timestamps for relative
90 + * times.  This is useful for comparing 32-bit seconds values that can't
91 + * be converted to 64-bit values (e.g. due to disk format or wire protocol
92 + * issues) when it is known that the times are less than 68 years apart.
93 + */
94 +#define time_after32(a, b)     ((s32)((u32)(b) - (u32)(a)) < 0)
95 +#define time_before32(b, a)    time_after32(a, b)
96  #endif
97 -- 
98 1.8.0