Whamcloud - gitweb
LU-73 RHEL6 support.
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext4-mmp-rhel6.patch
1 Index: linux-stage/fs/ext4/super.c
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/super.c    2011-03-03 15:25:02.376539424 +0800
4 +++ linux-stage/fs/ext4/super.c 2011-03-05 12:24:02.918774335 +0800
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 @@ -700,6 +702,8 @@
15                 invalidate_bdev(sbi->journal_bdev);
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 @@ -970,6 +974,344 @@
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,
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, 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 super_block *) data;
108 +       struct ext4_super_block *es = EXT4_SB(sb)->s_es;
109 +       struct buffer_head *bh = NULL;
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 +       retval = read_mmp_block(sb, &bh, mmp_block);
122 +       if (retval)
123 +               goto failed;
124 +
125 +       mmp = (struct mmp_struct *)(bh->b_data);
126 +       mmp->mmp_time = cpu_to_le64(get_seconds());
127 +       /*
128 +        * Start with the higher mmp_check_interval and reduce it if
129 +        * the MMP block is being updated on time.
130 +        */
131 +       mmp_check_interval = max(5 * mmp_update_interval,
132 +                                EXT4_MMP_MIN_CHECK_INTERVAL);
133 +       mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
134 +       bdevname(bh->b_bdev, mmp->mmp_bdevname);
135 +
136 +       memcpy(mmp->mmp_nodename, init_utsname()->sysname,
137 +              sizeof(mmp->mmp_nodename));
138 +
139 +       while (!kthread_should_stop()) {
140 +               if (++seq > EXT4_MMP_SEQ_MAX)
141 +                       seq = 1;
142 +
143 +               mmp->mmp_seq = cpu_to_le32(seq);
144 +               mmp->mmp_time = cpu_to_le64(get_seconds());
145 +               last_update_time = jiffies;
146 +
147 +               retval = write_mmp_block(bh);
148 +               /*
149 +                * Don't spew too many error messages. Print one every
150 +                * (s_mmp_update_interval * 60) seconds.
151 +                */
152 +               if (retval && (failed_writes % 60) == 0) {
153 +                       ext4_error(sb,
154 +                                  "Error writing to MMP block");
155 +                       failed_writes++;
156 +               }
157 +
158 +               if (!(le32_to_cpu(es->s_feature_incompat) &
159 +                   EXT4_FEATURE_INCOMPAT_MMP)) {
160 +                       ext4_warning(sb, "kmmpd being stopped "
161 +                                    "since MMP feature has been disabled.");
162 +                       EXT4_SB(sb)->s_mmp_tsk = 0;
163 +                       goto failed;
164 +               }
165 +
166 +               if (sb->s_flags & MS_RDONLY) {
167 +                       ext4_warning(sb, "kmmpd being stopped "
168 +                                    "since filesystem has been remounted as "
169 +                                    "readonly.");
170 +                       EXT4_SB(sb)->s_mmp_tsk = 0;
171 +                       goto failed;
172 +               }
173 +
174 +               diff = jiffies - last_update_time;
175 +               if (diff < mmp_update_interval * HZ)
176 +                       schedule_timeout_interruptible(mmp_update_interval *
177 +                                                      HZ - diff);
178 +
179 +               /*
180 +                * We need to make sure that more than mmp_check_interval
181 +                * seconds have not passed since writing. If that has happened
182 +                * we need to check if the MMP block is as we left it.
183 +                */
184 +               diff = jiffies - last_update_time;
185 +               if (diff > mmp_check_interval * HZ) {
186 +                       struct buffer_head *bh_check = NULL;
187 +                       struct mmp_struct *mmp_check;
188 +
189 +                       retval = read_mmp_block(sb, &bh_check, mmp_block);
190 +                       if (retval) {
191 +                               EXT4_SB(sb)->s_mmp_tsk = 0;
192 +                               goto failed;
193 +                       }
194 +
195 +                       mmp_check = (struct mmp_struct *)(bh_check->b_data);
196 +                       if (mmp->mmp_time != mmp_check->mmp_time ||
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 +
204 +                       put_bh(bh_check);
205 +               }
206 +
207 +               /*
208 +                * Adjust the mmp_check_interval depending on how much time
209 +                * it took for the MMP block to be written.
210 +                */
211 +               mmp_check_interval = max(5 * diff / HZ,
212 +                                (unsigned long) EXT4_MMP_MIN_CHECK_INTERVAL);
213 +               mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
214 +       }
215 +
216 +       /*
217 +        * Unmount seems to be clean.
218 +        */
219 +       mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
220 +       mmp->mmp_time = cpu_to_le64(get_seconds());
221 +
222 +       retval = write_mmp_block(bh);
223 +
224 +failed:
225 +       brelse(bh);
226 +       return retval;
227 +}
228 +
229 +/*
230 + * Get a random new sequence number but make sure it is not greater than
231 + * EXT4_MMP_SEQ_MAX.
232 + */
233 +static unsigned int mmp_new_seq(void)
234 +{
235 +       u32 new_seq;
236 +
237 +       do {
238 +               get_random_bytes(&new_seq, sizeof(u32));
239 +       } while (new_seq > EXT4_MMP_SEQ_MAX);
240 +
241 +       return new_seq;
242 +}
243 +
244 +/*
245 + * Protect the filesystem from being mounted more than once.
246 + */
247 +static int ext4_multi_mount_protect(struct super_block *sb,
248 +                                   unsigned long mmp_block)
249 +{
250 +       struct ext4_super_block *es = EXT4_SB(sb)->s_es;
251 +       struct buffer_head *bh = NULL;
252 +       struct mmp_struct *mmp = NULL;
253 +       u32 seq;
254 +       unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
255 +       unsigned int wait_time = 0;
256 +       int retval;
257 +
258 +       if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
259 +           mmp_block >= ext4_blocks_count(es)) {
260 +               ext4_warning(sb,
261 +                            "Invalid MMP block in superblock");
262 +               goto failed;
263 +       }
264 +
265 +       retval = read_mmp_block(sb, &bh, mmp_block);
266 +       if (retval)
267 +               goto failed;
268 +
269 +       mmp = (struct mmp_struct *)(bh->b_data);
270 +
271 +       if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
272 +               mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
273 +
274 +       /*
275 +        * If check_interval in MMP block is larger, use that instead of
276 +        * update_interval from the superblock.
277 +        */
278 +       if (mmp->mmp_check_interval > mmp_check_interval)
279 +               mmp_check_interval = mmp->mmp_check_interval;
280 +
281 +       seq = le32_to_cpu(mmp->mmp_seq);
282 +       if (seq == EXT4_MMP_SEQ_CLEAN)
283 +               goto skip;
284 +
285 +       if (seq == EXT4_MMP_SEQ_FSCK) {
286 +               dump_mmp_msg(sb, mmp, __func__,
287 +                            "fsck is running on the filesystem");
288 +               goto failed;
289 +       }
290 +
291 +       wait_time = min(mmp_check_interval * 2 + 1,
292 +               mmp_check_interval + 60);
293 +
294 +       /* Print MMP interval if more than 20 secs. */
295 +       if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
296 +               ext4_warning(sb, "MMP interval %u higher than "
297 +                            "expected, please wait.\n", wait_time * 2);
298 +
299 +       if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
300 +               ext4_warning(sb, "MMP startup interrupted, failing "
301 +                            "mount\n");
302 +               goto failed;
303 +       }
304 +
305 +       retval = read_mmp_block(sb, &bh, mmp_block);
306 +       if (retval)
307 +               goto failed;
308 +       mmp = (struct mmp_struct *)(bh->b_data);
309 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
310 +               dump_mmp_msg(sb, mmp, __func__,
311 +                            "Device is already active on another node.");
312 +               goto failed;
313 +       }
314 +
315 +skip:
316 +       /*
317 +        * write a new random sequence number.
318 +        */
319 +       mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
320 +
321 +       retval = write_mmp_block(bh);
322 +       if (retval)
323 +               goto failed;
324 +
325 +       /*
326 +        * wait for MMP interval and check mmp_seq.
327 +        */
328 +       if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
329 +               ext4_warning(sb, "MMP startup interrupted, failing "
330 +                            "mount\n");
331 +               goto failed;
332 +       }
333 +
334 +       retval = read_mmp_block(sb, &bh, mmp_block);
335 +       if (retval)
336 +               goto failed;
337 +       mmp = (struct mmp_struct *)(bh->b_data);
338 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
339 +               dump_mmp_msg(sb, mmp, __func__,
340 +                            "Device is already active on another node.");
341 +               goto failed;
342 +       }
343 +
344 +       /*
345 +        * Start a kernel thread to update the MMP block periodically.
346 +        */
347 +       EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
348 +                                            MAJOR(sb->s_dev),
349 +                                            MINOR(sb->s_dev));
350 +       if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
351 +               EXT4_SB(sb)->s_mmp_tsk = 0;
352 +               ext4_warning(sb, "Unable to create kmmpd thread "
353 +                            "for %s.", sb->s_id);
354 +               goto failed;
355 +       }
356 +
357 +       brelse(bh);
358 +       return 0;
359 +
360 +failed:
361 +       brelse(bh);
362 +       return 1;
363 +}
364 +
365  static struct inode *ext4_nfs_get_inode(struct super_block *sb,
366                                         u64 ino, u32 generation)
367  {
368 @@ -2816,6 +3158,11 @@
369                           EXT4_HAS_INCOMPAT_FEATURE(sb,
370                                     EXT4_FEATURE_INCOMPAT_RECOVER));
371  
372 +       if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
373 +           !(sb->s_flags & MS_RDONLY))
374 +               if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
375 +                       goto failed_mount3;
376 +
377         /*
378          * The first inode we look at is the journal inode.  Don't try
379          * root first: it may be modified in the journal!
380 @@ -3052,6 +3399,8 @@
381         percpu_counter_destroy(&sbi->s_freeinodes_counter);
382         percpu_counter_destroy(&sbi->s_dirs_counter);
383         percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
384 +       if (sbi->s_mmp_tsk)
385 +               kthread_stop(sbi->s_mmp_tsk);
386  failed_mount2:
387         for (i = 0; i < db_count; i++)
388                 brelse(sbi->s_group_desc[i]);
389 @@ -3560,7 +3909,7 @@
390         struct ext4_mount_options old_opts;
391         ext4_group_t g;
392         unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
393 -       int err;
394 +       int err = 0;
395  #ifdef CONFIG_QUOTA
396         int i;
397  #endif
398 @@ -3682,6 +4031,13 @@
399                                 goto restore_opts;
400                         if (!ext4_setup_super(sb, es, 0))
401                                 sb->s_flags &= ~MS_RDONLY;
402 +                       if (EXT4_HAS_INCOMPAT_FEATURE(sb,
403 +                                                   EXT4_FEATURE_INCOMPAT_MMP))
404 +                               if (ext4_multi_mount_protect(sb,
405 +                                               le64_to_cpu(es->s_mmp_block))) {
406 +                                       err = -EROFS;
407 +                                       goto restore_opts;
408 +                               }
409                 }
410         }
411         ext4_setup_system_zone(sb);
412 Index: linux-stage/fs/ext4/ext4.h
413 ===================================================================
414 --- linux-stage.orig/fs/ext4/ext4.h     2011-03-03 15:25:02.507538421 +0800
415 +++ linux-stage/fs/ext4/ext4.h  2011-03-05 12:25:16.343986732 +0800
416 @@ -894,7 +894,7 @@
417         __le16  s_want_extra_isize;     /* New inodes should reserve # bytes */
418         __le32  s_flags;                /* Miscellaneous flags */
419         __le16  s_raid_stride;          /* RAID stride */
420 -       __le16  s_mmp_interval;         /* # seconds to wait in MMP checking */
421 +       __le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */
422         __le64  s_mmp_block;            /* Block for multi-mount protection */
423         __le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
424         __u8    s_log_groups_per_flex;  /* FLEX_BG group size */
425 @@ -1041,6 +1041,9 @@
426  
427         /* workqueue for dio unwritten */
428         struct workqueue_struct *dio_unwritten_wq;
429 +
430 +       /* Kernel thread for multiple mount protection */
431 +       struct task_struct *s_mmp_tsk;
432  };
433  
434  static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
435 @@ -1177,7 +1180,8 @@
436                                          EXT4_FEATURE_INCOMPAT_META_BG| \
437                                          EXT4_FEATURE_INCOMPAT_EXTENTS| \
438                                          EXT4_FEATURE_INCOMPAT_64BIT| \
439 -                                        EXT4_FEATURE_INCOMPAT_FLEX_BG)
440 +                                        EXT4_FEATURE_INCOMPAT_FLEX_BG| \
441 +                                        EXT4_FEATURE_INCOMPAT_MMP)
442  #define EXT4_FEATURE_RO_COMPAT_SUPP    (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
443                                          EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
444                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
445 @@ -1384,6 +1388,34 @@
446  extern struct proc_dir_entry *ext4_proc_root;
447  
448  /*
449 + * This structure will be used for multiple mount protection. It will be
450 + * written into the block number saved in the s_mmp_block field in the
451 + * superblock. Programs that check MMP should assume that if
452 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
453 + * to use the filesystem, regardless of how old the timestamp is.
454 + */
455 +#define EXT4_MMP_MAGIC     0x004D4D50U /* ASCII for MMP */
456 +#define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
457 +#define EXT4_MMP_SEQ_FSCK  0xE24D4D50U /* mmp_seq value when being fscked */
458 +#define EXT4_MMP_SEQ_MAX   0xE24D4D4FU /* maximum valid mmp_seq value */
459 +
460 +struct mmp_struct {
461 +       __le32  mmp_magic;
462 +       __le32  mmp_seq;
463 +       __le64  mmp_time;
464 +       char    mmp_nodename[64];
465 +       char    mmp_bdevname[32];
466 +       __le16  mmp_check_interval;
467 +       __le16  mmp_pad1;
468 +       __le32  mmp_pad2[227];
469 +};
470 +
471 +/*
472 + * Minimum interval for MMP checking in seconds.
473 + */
474 +#define EXT4_MMP_MIN_CHECK_INTERVAL    5
475 +
476 +/*
477   * Function prototypes
478   */
479