Whamcloud - gitweb
b=22117 Patch to limit mmp interval
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext4-mmp-rhel5.patch
1 Index: linux-stage/fs/ext4/super.c
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/super.c
4 +++ linux-stage/fs/ext4/super.c
5 @@ -40,6 +40,8 @@
6  #include <linux/log2.h>
7  #include <linux/crc16.h>
8  #include <asm/uaccess.h>
9 +#include <linux/kthread.h>
10 +#include <linux/utsname.h>
11  
12  #include "ext4.h"
13  #include "ext4_jbd2.h"
14 @@ -660,6 +662,8 @@ static void ext4_put_super(struct super_
15                 invalidate_bdev(sbi->journal_bdev, 0);
16                 ext4_blkdev_remove(sbi);
17         }
18 +       if (sbi->s_mmp_tsk)
19 +               kthread_stop(sbi->s_mmp_tsk);
20         sb->s_fs_info = NULL;
21         /*
22          * Now that we are completely done shutting down the
23 @@ -921,6 +925,354 @@ static int ext4_show_options(struct seq_
24         return 0;
25  }
26  
27 +/*
28 + * Write the MMP block using WRITE_SYNC to try to get the block on-disk
29 + * faster.
30 + */
31 +static int write_mmp_block(struct buffer_head *bh)
32 +{
33 +       mark_buffer_dirty(bh);
34 +       lock_buffer(bh);
35 +       bh->b_end_io = end_buffer_write_sync;
36 +       get_bh(bh);
37 +       submit_bh(WRITE_SYNC, bh);
38 +       wait_on_buffer(bh);
39 +       if (unlikely(!buffer_uptodate(bh)))
40 +               return 1;
41 +
42 +       return 0;
43 +}
44 +
45 +/*
46 + * Read the MMP block. It _must_ be read from disk and hence we clear the
47 + * uptodate flag on the buffer.
48 + */
49 +static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
50 +                         unsigned long mmp_block)
51 +{
52 +       struct mmp_struct *mmp;
53 +
54 +       if (*bh)
55 +               clear_buffer_uptodate(*bh);
56 +
57 +#if 0
58 +       brelse(*bh);
59 +
60 +       *bh = sb_bread(sb, mmp_block);
61 +#else
62 +       if (!*bh)
63 +               *bh = sb_getblk(sb, mmp_block);
64 +       if (*bh) {
65 +               get_bh(*bh);
66 +               lock_buffer(*bh);
67 +               (*bh)->b_end_io = end_buffer_read_sync;
68 +               submit_bh(READ_SYNC, *bh);
69 +               wait_on_buffer(*bh);
70 +               if (!buffer_uptodate(*bh)) {
71 +                       brelse(*bh);
72 +                       *bh = NULL;
73 +               }
74 +       }
75 +#endif
76 +       if (!*bh) {
77 +               ext4_warning(sb, __func__,
78 +                            "Error while reading MMP block %lu", mmp_block);
79 +               return -EIO;
80 +       }
81 +
82 +       mmp = (struct mmp_struct *)((*bh)->b_data);
83 +       if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC)
84 +               return -EINVAL;
85 +
86 +       return 0;
87 +}
88 +
89 +/*
90 + * Dump as much information as possible to help the admin.
91 + */
92 +static void dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
93 +                        const char *function, const char *msg)
94 +{
95 +       ext4_warning(sb, function, "%s", msg);
96 +       ext4_warning(sb, function, "MMP failure info: last update time: %llu, "
97 +                    "last update node: %s, last update device: %s\n",
98 +                    (long long unsigned int)le64_to_cpu(mmp->mmp_time),
99 +                    mmp->mmp_nodename, mmp->mmp_bdevname);
100 +}
101 +
102 +/*
103 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
104 + */
105 +static int kmmpd(void *data)
106 +{
107 +       struct super_block *sb = ((struct mmpd_data *) data)->sb;
108 +       struct buffer_head *bh = ((struct mmpd_data *) data)->bh;
109 +       struct ext4_super_block *es = EXT4_SB(sb)->s_es;
110 +       struct mmp_struct *mmp;
111 +       unsigned long mmp_block;
112 +       u32 seq = 0;
113 +       unsigned long failed_writes = 0;
114 +       int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
115 +       unsigned mmp_check_interval;
116 +       unsigned long last_update_time;
117 +       unsigned long diff;
118 +       int retval;
119 +
120 +       mmp_block = le64_to_cpu(es->s_mmp_block);
121 +       mmp = (struct mmp_struct *)(bh->b_data);
122 +       mmp->mmp_time = cpu_to_le64(get_seconds());
123 +       /*
124 +        * Start with the higher mmp_check_interval and reduce it if
125 +        * the MMP block is being updated on time.
126 +        */
127 +       mmp_check_interval = max(5UL * mmp_update_interval,
128 +                                EXT4_MMP_MIN_CHECK_INTERVAL);
129 +       mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
130 +       bdevname(bh->b_bdev, mmp->mmp_bdevname);
131 +
132 +       down_read(&uts_sem);
133 +       memcpy(mmp->mmp_nodename, system_utsname.nodename,
134 +              sizeof(mmp->mmp_nodename));
135 +       up_read(&uts_sem);
136 +
137 +       while (!kthread_should_stop()) {
138 +               if (++seq > EXT4_MMP_SEQ_MAX)
139 +                       seq = 1;
140 +
141 +               mmp->mmp_seq = cpu_to_le32(seq);
142 +               mmp->mmp_time = cpu_to_le64(get_seconds());
143 +               last_update_time = jiffies;
144 +
145 +               retval = write_mmp_block(bh);
146 +               /*
147 +                * Don't spew too many error messages. Print one every
148 +                * (s_mmp_update_interval * 60) seconds.
149 +                */
150 +               if (retval && (failed_writes % 60) == 0) {
151 +                       ext4_error(sb, __func__,
152 +                                  "Error writing to MMP block");
153 +                       failed_writes++;
154 +               }
155 +
156 +               if (!(le32_to_cpu(es->s_feature_incompat) &
157 +                   EXT4_FEATURE_INCOMPAT_MMP)) {
158 +                       ext4_warning(sb, __func__, "kmmpd being stopped "
159 +                                    "since MMP feature has been disabled.");
160 +                       EXT4_SB(sb)->s_mmp_tsk = NULL;
161 +                       goto failed;
162 +               }
163 +
164 +               if (sb->s_flags & MS_RDONLY) {
165 +                       ext4_warning(sb, __func__, "kmmpd being stopped "
166 +                                    "since filesystem has been remounted as "
167 +                                    "readonly.");
168 +                       EXT4_SB(sb)->s_mmp_tsk = NULL;
169 +                       goto failed;
170 +               }
171 +
172 +               diff = jiffies - last_update_time;
173 +               if (diff < mmp_update_interval * HZ)
174 +                       schedule_timeout_interruptible(EXT4_MMP_UPDATE_INTERVAL*
175 +                                                      HZ - diff);
176 +
177 +               /*
178 +                * We need to make sure that more than mmp_check_interval
179 +                * seconds have not passed since writing. If that has happened
180 +                * we need to check if the MMP block is as we left it.
181 +                */
182 +               diff = jiffies - last_update_time;
183 +               if (diff > mmp_check_interval * HZ) {
184 +                       struct buffer_head *bh_check = NULL;
185 +                       struct mmp_struct *mmp_check;
186 +
187 +                       retval = read_mmp_block(sb, &bh_check, mmp_block);
188 +                       if (retval) {
189 +                               ext4_error(sb, __func__, "error reading MMP"
190 +                                          "data: %d", retval);
191 +                               EXT4_SB(sb)->s_mmp_tsk = NULL;
192 +                               goto failed;
193 +                       }
194 +
195 +                       mmp_check = (struct mmp_struct *)(bh_check->b_data);
196 +                       if (mmp->mmp_seq != mmp_check->mmp_seq ||
197 +                           memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
198 +                                  sizeof(mmp->mmp_nodename))) {
199 +                               dump_mmp_msg(sb, mmp_check, __func__,
200 +                                            "Error while updating MMP info. "
201 +                                            "The filesystem seems to have "
202 +                                            "been multiply mounted.");
203 +                               ext4_error(sb, __func__, "abort");
204 +                               goto failed;
205 +                       }
206 +                       put_bh(bh_check);
207 +               }
208 +
209 +               /*
210 +                * Adjust the mmp_check_interval depending on how much time
211 +                * it took for the MMP block to be written.
212 +                */
213 +               mmp_check_interval = max(min(5 * diff / HZ,
214 +                                            EXT4_MMP_MAX_CHECK_INTERVAL),
215 +                                        EXT4_MMP_MIN_CHECK_INTERVAL);
216 +               mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
217 +       }
218 +
219 +       /*
220 +        * Unmount seems to be clean.
221 +        */
222 +       mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
223 +       mmp->mmp_time = cpu_to_le64(get_seconds());
224 +
225 +       retval = write_mmp_block(bh);
226 +
227 +failed:
228 +       brelse(bh);
229 +       return retval;
230 +}
231 +
232 +/*
233 + * Get a random new sequence number but make sure it is not greater than
234 + * EXT4_MMP_SEQ_MAX.
235 + */
236 +static unsigned int mmp_new_seq(void)
237 +{
238 +       u32 new_seq;
239 +
240 +       do {
241 +               get_random_bytes(&new_seq, sizeof(u32));
242 +       } while (new_seq > EXT4_MMP_SEQ_MAX);
243 +
244 +       return new_seq;
245 +}
246 +
247 +/*
248 + * Protect the filesystem from being mounted more than once.
249 + */
250 +static int ext4_multi_mount_protect(struct super_block *sb,
251 +                                   unsigned long mmp_block)
252 +{
253 +       struct ext4_super_block *es = EXT4_SB(sb)->s_es;
254 +       struct buffer_head *bh = NULL;
255 +       struct mmp_struct *mmp = NULL;
256 +       struct mmpd_data *mmpd_data;
257 +       u32 seq;
258 +       unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
259 +       unsigned int wait_time = 0;
260 +       int retval;
261 +
262 +       if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
263 +           mmp_block >= ext4_blocks_count(es)) {
264 +               ext4_warning(sb, __func__,
265 +                            "Invalid MMP block in superblock");
266 +               goto failed;
267 +       }
268 +
269 +       retval = read_mmp_block(sb, &bh, mmp_block);
270 +       if (retval)
271 +               goto failed;
272 +
273 +       mmp = (struct mmp_struct *)(bh->b_data);
274 +
275 +       if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
276 +               mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
277 +
278 +       /*
279 +        * If check_interval in MMP block is larger, use that instead of
280 +        * update_interval from the superblock.
281 +        */
282 +       if (mmp->mmp_check_interval > mmp_check_interval)
283 +               mmp_check_interval = mmp->mmp_check_interval;
284 +
285 +       seq = le32_to_cpu(mmp->mmp_seq);
286 +       if (seq == EXT4_MMP_SEQ_CLEAN)
287 +               goto skip;
288 +
289 +       if (seq == EXT4_MMP_SEQ_FSCK) {
290 +               dump_mmp_msg(sb, mmp, __func__,
291 +                            "fsck is running on the filesystem");
292 +               goto failed;
293 +       }
294 +
295 +       wait_time = min(mmp_check_interval * 2 + 1,
296 +               mmp_check_interval + 60);
297 +
298 +       /* Print MMP interval if more than 20 secs. */
299 +       if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
300 +               ext4_warning(sb, __func__, "MMP interval %u higher than "
301 +                            "expected, please wait.\n", wait_time * 2);
302 +
303 +       if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
304 +               ext4_warning(sb, __func__, "MMP startup interrupted, failing "
305 +                            "mount\n");
306 +               goto failed;
307 +       }
308 +
309 +       retval = read_mmp_block(sb, &bh, mmp_block);
310 +       if (retval)
311 +               goto failed;
312 +       mmp = (struct mmp_struct *)(bh->b_data);
313 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
314 +               dump_mmp_msg(sb, mmp, __func__,
315 +                            "Device is already active on another node.");
316 +               goto failed;
317 +       }
318 +
319 +skip:
320 +       /*
321 +        * write a new random sequence number.
322 +        */
323 +       mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
324 +
325 +       retval = write_mmp_block(bh);
326 +       if (retval)
327 +               goto failed;
328 +
329 +       /*
330 +        * wait for MMP interval and check mmp_seq.
331 +        */
332 +       if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
333 +               ext4_warning(sb, __func__, "MMP startup interrupted, failing "
334 +                            "mount\n");
335 +               goto failed;
336 +       }
337 +
338 +       retval = read_mmp_block(sb, &bh, mmp_block);
339 +       if (retval)
340 +               goto failed;
341 +       mmp = (struct mmp_struct *)(bh->b_data);
342 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
343 +               dump_mmp_msg(sb, mmp, __func__,
344 +                            "Device is already active on another node.");
345 +               goto failed;
346 +       }
347 +
348 +       mmpd_data = kmalloc(sizeof(struct mmpd_data *), GFP_KERNEL);
349 +       if (!mmpd_data) {
350 +               ext4_warning(sb, KERN_ERR, "not enough memory for mmpd_data");
351 +               goto failed;
352 +       }
353 +       mmpd_data->sb = sb;
354 +       mmpd_data->bh = bh;
355 +
356 +       /*
357 +        * Start a kernel thread to update the MMP block periodically.
358 +        */
359 +       EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, mmpd_data, "kmmpd-%s",
360 +                                            bdevname(bh->b_bdev,
361 +                                                     mmp->mmp_bdevname));
362 +       if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
363 +               EXT4_SB(sb)->s_mmp_tsk = NULL;
364 +               ext4_warning(sb, __func__, "Unable to create kmmpd thread "
365 +                            "for %s.", sb->s_id);
366 +               goto failed;
367 +       }
368 +
369 +       return 0;
370 +
371 +failed:
372 +       brelse(bh);
373 +       return 1;
374 +}
375  
376  static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
377  {
378 @@ -930,7 +1282,6 @@ static struct dentry *ext4_get_dentry(st
379         struct inode *inode;
380         struct dentry *result;
381  
382 -
383         if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
384                 return ERR_PTR(-ESTALE);
385         if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
386 @@ -2740,6 +3091,11 @@ static int ext4_fill_super(struct super_
387                           EXT4_HAS_INCOMPAT_FEATURE(sb,
388                                     EXT4_FEATURE_INCOMPAT_RECOVER));
389  
390 +       if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
391 +           !(sb->s_flags & MS_RDONLY))
392 +               if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
393 +                       goto failed_mount3;
394 +
395         /*
396          * The first inode we look at is the journal inode.  Don't try
397          * root first: it may be modified in the journal!
398 @@ -2978,6 +3334,8 @@ failed_mount3:
399         percpu_counter_destroy(&sbi->s_freeinodes_counter);
400         percpu_counter_destroy(&sbi->s_dirs_counter);
401         percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
402 +       if (sbi->s_mmp_tsk)
403 +               kthread_stop(sbi->s_mmp_tsk);
404  failed_mount2:
405         for (i = 0; i < db_count; i++)
406                 brelse(sbi->s_group_desc[i]);
407 @@ -3488,7 +3846,7 @@ static int ext4_remount(struct super_blo
408         struct ext4_mount_options old_opts;
409         ext4_group_t g;
410         unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
411 -       int err;
412 +       int err = 0;
413  #ifdef CONFIG_QUOTA
414         int i;
415  #endif
416 @@ -3607,6 +3965,13 @@ static int ext4_remount(struct super_blo
417                                 goto restore_opts;
418                         if (!ext4_setup_super(sb, es, 0))
419                                 sb->s_flags &= ~MS_RDONLY;
420 +                       if (EXT4_HAS_INCOMPAT_FEATURE(sb,
421 +                                                   EXT4_FEATURE_INCOMPAT_MMP))
422 +                               if (ext4_multi_mount_protect(sb,
423 +                                               le64_to_cpu(es->s_mmp_block))) {
424 +                                       err = -EROFS;
425 +                                       goto restore_opts;
426 +                               }
427                 }
428         }
429         ext4_setup_system_zone(sb);
430 Index: linux-stage/fs/ext4/ext4.h
431 ===================================================================
432 --- linux-stage.orig/fs/ext4/ext4.h
433 +++ linux-stage/fs/ext4/ext4.h
434 @@ -851,7 +851,7 @@ struct ext4_super_block {
435         __le16  s_want_extra_isize;     /* New inodes should reserve # bytes */
436         __le32  s_flags;                /* Miscellaneous flags */
437         __le16  s_raid_stride;          /* RAID stride */
438 -       __le16  s_mmp_interval;         /* # seconds to wait in MMP checking */
439 +       __le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */
440         __le64  s_mmp_block;            /* Block for multi-mount protection */
441         __le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
442         __u8    s_log_groups_per_flex;  /* FLEX_BG group size */
443 @@ -1005,6 +1005,9 @@ struct ext4_sb_info {
444  
445         /* workqueue for dio unwritten */
446         struct workqueue_struct *dio_unwritten_wq;
447 +
448 +       /* Kernel thread for multiple mount protection */
449 +       struct task_struct *s_mmp_tsk;
450  };
451  
452  static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
453 @@ -1114,7 +1117,8 @@ static inline int ext4_valid_inum(struct
454                                          EXT4_FEATURE_INCOMPAT_META_BG| \
455                                          EXT4_FEATURE_INCOMPAT_EXTENTS| \
456                                          EXT4_FEATURE_INCOMPAT_64BIT| \
457 -                                        EXT4_FEATURE_INCOMPAT_FLEX_BG)
458 +                                        EXT4_FEATURE_INCOMPAT_FLEX_BG| \
459 +                                        EXT4_FEATURE_INCOMPAT_MMP)
460  #define EXT4_FEATURE_RO_COMPAT_SUPP    (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
461                                          EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
462                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
463 @@ -1296,6 +1300,50 @@ void ext4_get_group_no_and_offset(struct
464  extern struct proc_dir_entry *ext4_proc_root;
465  
466  /*
467 + * This structure will be used for multiple mount protection. It will be
468 + * written into the block number saved in the s_mmp_block field in the
469 + * superblock. Programs that check MMP should assume that if
470 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
471 + * to use the filesystem, regardless of how old the timestamp is.
472 + */
473 +#define EXT4_MMP_MAGIC     0x004D4D50U /* ASCII for MMP */
474 +#define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
475 +#define EXT4_MMP_SEQ_FSCK  0xE24D4D50U /* mmp_seq value when being fscked */
476 +#define EXT4_MMP_SEQ_MAX   0xE24D4D4FU /* maximum valid mmp_seq value */
477 +
478 +struct mmp_struct {
479 +       __le32  mmp_magic;
480 +       __le32  mmp_seq;
481 +       __le64  mmp_time;
482 +       char    mmp_nodename[64];
483 +       char    mmp_bdevname[32];
484 +       __le16  mmp_check_interval;
485 +       __le16  mmp_pad1;
486 +       __le32  mmp_pad2[227];
487 +};
488 +
489 +/* arguments passed to the mmp thread */
490 +struct mmpd_data {
491 +       struct buffer_head *bh; /* bh from initial read_mmp_block() */
492 +       struct super_block *sb;  /* super block of the fs */
493 +};
494 +
495 +/*
496 + * Default interval in seconds to update the MMP sequence number.
497 + */
498 +#define EXT4_MMP_UPDATE_INTERVAL   1
499 +
500 +/*
501 + * Minimum interval for MMP checking in seconds.
502 + */
503 +#define EXT4_MMP_MIN_CHECK_INTERVAL    5UL
504 +
505 +/*
506 + * Maximum interval for MMP checking in seconds.
507 + */
508 +#define EXT4_MMP_MAX_CHECK_INTERVAL    300UL
509 +
510 +/*
511   * Function prototypes
512   */
513