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