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