Whamcloud - gitweb
LU-11071 build: Add server build support for Ubuntu 18.04
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ubuntu18 / ext4-give-warning-with-dir-htree-growing.patch
1 Index: linux-4.15.0/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-4.15.0.orig/fs/ext4/ext4.h
4 +++ linux-4.15.0/fs/ext4/ext4.h
5 @@ -1450,6 +1450,7 @@ struct ext4_sb_info {
6         unsigned long s_mb_prealloc_table_size;
7         unsigned int s_mb_group_prealloc;
8         unsigned int s_max_dir_size_kb;
9 +       unsigned long s_warning_dir_size;
10         /* where last allocation was done - for stream allocation */
11         unsigned long s_mb_last_group;
12         unsigned long s_mb_last_start;
13 Index: linux-4.15.0/fs/ext4/namei.c
14 ===================================================================
15 --- linux-4.15.0.orig/fs/ext4/namei.c
16 +++ linux-4.15.0/fs/ext4/namei.c
17 @@ -751,12 +751,20 @@ struct ext4_dir_lock_data {
18  #define ext4_htree_lock_data(l)        ((struct ext4_dir_lock_data *)(l)->lk_private)
19  #define ext4_find_entry(dir, name, dirent, inline) \
20                         __ext4_find_entry(dir, name, dirent, inline, NULL)
21 -#define ext4_add_entry(handle, dentry, inode) \
22 -                       __ext4_add_entry(handle, dentry, inode, NULL)
23  
24  /* NB: ext4_lblk_t is 32 bits so we use high bits to identify invalid blk */
25  #define EXT4_HTREE_NODE_CHANGED        (0xcafeULL << 32)
26  
27 +inline int ext4_add_entry(handle_t *handle, struct dentry *dentry,
28 +                         struct inode *inode)
29 +{
30 +       int ret = __ext4_add_entry(handle, dentry, inode, NULL);
31 +
32 +       if (ret == -ENOBUFS)
33 +               ret = 0;
34 +       return ret;
35 +}
36 +
37  static void ext4_htree_event_cb(void *target, void *event)
38  {
39         u64 *block = (u64 *)target;
40 @@ -2508,6 +2516,54 @@ out:
41         return err;
42  }
43  
44 +static unsigned long __ext4_max_dir_size(struct dx_frame *frames,
45 +                              struct dx_frame *frame, struct inode *dir)
46 +{
47 +       unsigned long max_dir_size;
48 +
49 +       if (EXT4_SB(dir->i_sb)->s_max_dir_size_kb) {
50 +               max_dir_size = EXT4_SB(dir->i_sb)->s_max_dir_size_kb << 10;
51 +       } else {
52 +               max_dir_size = EXT4_BLOCK_SIZE(dir->i_sb);
53 +               while (frame >= frames) {
54 +                       max_dir_size *= dx_get_limit(frame->entries);
55 +                       if (frame == frames)
56 +                               break;
57 +                       frame--;
58 +               }
59 +               /* use 75% of max dir size in average */
60 +               max_dir_size = max_dir_size / 4 * 3;
61 +       }
62 +       return max_dir_size;
63 +}
64 +
65 +/*
66 + * With hash tree growing, it is easy to hit ENOSPC, but it is hard
67 + * to predict when it will happen. let's give administrators warning
68 + * when reaching 3/5 and 2/3 of limit
69 + */
70 +static inline bool dir_size_in_warning_range(struct dx_frame *frames,
71 +                                            struct dx_frame *frame,
72 +                                            struct inode *dir)
73 +{
74 +       unsigned long size1, size2;
75 +       struct super_block *sb = dir->i_sb;
76 +
77 +       if (unlikely(!EXT4_SB(sb)->s_warning_dir_size))
78 +               EXT4_SB(sb)->s_warning_dir_size =
79 +                       __ext4_max_dir_size(frames, frame, dir);
80 +
81 +       size1 = EXT4_SB(sb)->s_warning_dir_size / 16 * 10;
82 +       size1 = size1 & ~(EXT4_BLOCK_SIZE(sb) - 1);
83 +       size2 = EXT4_SB(sb)->s_warning_dir_size / 16 * 11;
84 +       size2 = size2 & ~(EXT4_BLOCK_SIZE(sb) - 1);
85 +       if (in_range(dir->i_size, size1, EXT4_BLOCK_SIZE(sb)) ||
86 +           in_range(dir->i_size, size2, EXT4_BLOCK_SIZE(sb)))
87 +               return true;
88 +
89 +       return false;
90 +}
91 +
92  /*
93   *     ext4_add_entry()
94   *
95 @@ -2629,6 +2685,7 @@ static int ext4_dx_add_entry(handle_t *h
96         struct ext4_dir_entry_2 *de;
97         int restart;
98         int err;
99 +       bool ret_warn = false;
100  
101  again:
102         restart = 0;
103 @@ -2657,6 +2714,11 @@ again:
104         /* Block full, should compress but for now just split */
105         dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
106                        dx_get_count(entries), dx_get_limit(entries)));
107 +
108 +       if (frame - frames + 1 >= ext4_dir_htree_level(sb) ||
109 +           EXT4_SB(sb)->s_warning_dir_size)
110 +               ret_warn = dir_size_in_warning_range(frames, frame, dir);
111 +
112         /* Need to split index? */
113         if (dx_get_count(entries) == dx_get_limit(entries)) {
114                 ext4_lblk_t newblock;
115 @@ -2819,6 +2881,8 @@ cleanup:
116          */
117         if (restart && err == 0)
118                 goto again;
119 +       if (err == 0 && ret_warn)
120 +               err = -ENOBUFS;
121         return err;
122  }
123  
124 Index: linux-4.15.0/fs/ext4/super.c
125 ===================================================================
126 --- linux-4.15.0.orig/fs/ext4/super.c
127 +++ linux-4.15.0/fs/ext4/super.c
128 @@ -1804,6 +1804,8 @@ static int handle_mount_opt(struct super
129                 sbi->s_li_wait_mult = arg;
130         } else if (token == Opt_max_dir_size_kb) {
131                 sbi->s_max_dir_size_kb = arg;
132 +               /* reset s_warning_dir_size and make it re-calculated */
133 +               sbi->s_warning_dir_size = 0;
134         } else if (token == Opt_stripe) {
135                 sbi->s_stripe = arg;
136         } else if (token == Opt_resuid) {
137 Index: linux-4.15.0/fs/ext4/sysfs.c
138 ===================================================================
139 --- linux-4.15.0.orig/fs/ext4/sysfs.c
140 +++ linux-4.15.0/fs/ext4/sysfs.c
141 @@ -173,6 +173,7 @@ EXT4_ATTR_OFFSET(inode_readahead_blks, 0
142  EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
143  EXT4_RW_ATTR_SBI_UI(max_dir_size, s_max_dir_size_kb);
144  EXT4_RW_ATTR_SBI_UI(max_dir_size_kb, s_max_dir_size_kb);
145 +EXT4_RW_ATTR_SBI_UI(warning_dir_size, s_warning_dir_size);
146  EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
147  EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
148  EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
149 @@ -204,6 +205,7 @@ static struct attribute *ext4_attrs[] =
150         ATTR_LIST(inode_goal),
151         ATTR_LIST(max_dir_size),
152         ATTR_LIST(max_dir_size_kb),
153 +       ATTR_LIST(warning_dir_size),
154         ATTR_LIST(mb_stats),
155         ATTR_LIST(mb_max_to_scan),
156         ATTR_LIST(mb_min_to_scan),