Whamcloud - gitweb
b=21669 cast the le64 to %llu in all ext*mmp patches
[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 @@ -38,6 +38,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 @@ -617,6 +619,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         kfree(sbi);
22         return;
23 @@ -864,6 +868,345 @@ 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, 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 +       down_read(&uts_sem);
137 +       memcpy(mmp->mmp_nodename, system_utsname.nodename,
138 +              sizeof(mmp->mmp_nodename));
139 +       up_read(&uts_sem);
140 +
141 +       while (!kthread_should_stop()) {
142 +               if (++seq > EXT4_MMP_SEQ_MAX)
143 +                       seq = 1;
144 +
145 +               mmp->mmp_seq = cpu_to_le32(seq);
146 +               mmp->mmp_time = cpu_to_le64(get_seconds());
147 +               last_update_time = jiffies;
148 +
149 +               retval = write_mmp_block(bh);
150 +               /*
151 +                * Don't spew too many error messages. Print one every
152 +                * (s_mmp_update_interval * 60) seconds.
153 +                */
154 +               if (retval && (failed_writes % 60) == 0) {
155 +                       ext4_error(sb, __func__,
156 +                                  "Error writing to MMP block");
157 +                       failed_writes++;
158 +               }
159 +
160 +               if (!(le32_to_cpu(es->s_feature_incompat) &
161 +                   EXT4_FEATURE_INCOMPAT_MMP)) {
162 +                       ext4_warning(sb, __func__, "kmmpd being stopped "
163 +                                    "since MMP feature has been disabled.");
164 +                       EXT4_SB(sb)->s_mmp_tsk = 0;
165 +                       goto failed;
166 +               }
167 +
168 +               if (sb->s_flags & MS_RDONLY) {
169 +                       ext4_warning(sb, __func__, "kmmpd being stopped "
170 +                                    "since filesystem has been remounted as "
171 +                                    "readonly.");
172 +                       EXT4_SB(sb)->s_mmp_tsk = 0;
173 +                       goto failed;
174 +               }
175 +
176 +               diff = jiffies - last_update_time;
177 +               if (diff < mmp_update_interval * HZ)
178 +                       schedule_timeout_interruptible(EXT4_MMP_UPDATE_INTERVAL*
179 +                                                      HZ - diff);
180 +
181 +               /*
182 +                * We need to make sure that more than mmp_check_interval
183 +                * seconds have not passed since writing. If that has happened
184 +                * we need to check if the MMP block is as we left it.
185 +                */
186 +               diff = jiffies - last_update_time;
187 +               if (diff > mmp_check_interval * HZ) {
188 +                       struct buffer_head *bh_check = NULL;
189 +                       struct mmp_struct *mmp_check;
190 +
191 +                       retval = read_mmp_block(sb, &bh_check, mmp_block);
192 +                       if (retval) {
193 +                               EXT4_SB(sb)->s_mmp_tsk = 0;
194 +                               goto failed;
195 +                       }
196 +
197 +                       mmp_check = (struct mmp_struct *)(bh_check->b_data);
198 +                       if (mmp->mmp_time != mmp_check->mmp_time ||
199 +                           memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
200 +                                  sizeof(mmp->mmp_nodename)))
201 +                               dump_mmp_msg(sb, mmp_check, __func__,
202 +                                            "Error while updating MMP info. "
203 +                                            "The filesystem seems to have "
204 +                                            "been multiply mounted.");
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(5 * diff / HZ,
214 +                                (unsigned long) EXT4_MMP_MIN_CHECK_INTERVAL);
215 +               mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
216 +       }
217 +
218 +       /*
219 +        * Unmount seems to be clean.
220 +        */
221 +       mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
222 +       mmp->mmp_time = cpu_to_le64(get_seconds());
223 +
224 +       retval = write_mmp_block(bh);
225 +
226 +failed:
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 +       u32 seq;
256 +       unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
257 +       unsigned int wait_time = 0;
258 +       int retval;
259 +
260 +       if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
261 +           mmp_block >= ext4_blocks_count(es)) {
262 +               ext4_warning(sb, __func__,
263 +                            "Invalid MMP block in superblock");
264 +               goto failed;
265 +       }
266 +
267 +       retval = read_mmp_block(sb, &bh, mmp_block);
268 +       if (retval)
269 +               goto failed;
270 +
271 +       mmp = (struct mmp_struct *)(bh->b_data);
272 +
273 +       if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
274 +               mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
275 +
276 +       /*
277 +        * If check_interval in MMP block is larger, use that instead of
278 +        * update_interval from the superblock.
279 +        */
280 +       if (mmp->mmp_check_interval > mmp_check_interval)
281 +               mmp_check_interval = mmp->mmp_check_interval;
282 +
283 +       seq = le32_to_cpu(mmp->mmp_seq);
284 +       if (seq == EXT4_MMP_SEQ_CLEAN)
285 +               goto skip;
286 +
287 +       if (seq == EXT4_MMP_SEQ_FSCK) {
288 +               dump_mmp_msg(sb, mmp, __func__,
289 +                            "fsck is running on the filesystem");
290 +               goto failed;
291 +       }
292 +
293 +       wait_time = min(mmp_check_interval * 2 + 1,
294 +               mmp_check_interval + 60);
295 +
296 +       /* Print MMP interval if more than 20 secs. */
297 +       if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
298 +               ext4_warning(sb, __func__, "MMP interval %u higher than "
299 +                            "expected, please wait.\n", wait_time * 2);
300 +
301 +       if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
302 +               ext4_warning(sb, __func__, "MMP startup interrupted, failing "
303 +                            "mount\n");
304 +               goto failed;
305 +       }
306 +
307 +       retval = read_mmp_block(sb, &bh, mmp_block);
308 +       if (retval)
309 +               goto failed;
310 +       mmp = (struct mmp_struct *)(bh->b_data);
311 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
312 +               dump_mmp_msg(sb, mmp, __func__,
313 +                            "Device is already active on another node.");
314 +               goto failed;
315 +       }
316 +
317 +skip:
318 +       /*
319 +        * write a new random sequence number.
320 +        */
321 +       mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
322 +
323 +       retval = write_mmp_block(bh);
324 +       if (retval)
325 +               goto failed;
326 +
327 +       /*
328 +        * wait for MMP interval and check mmp_seq.
329 +        */
330 +       if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
331 +               ext4_warning(sb, __func__, "MMP startup interrupted, failing "
332 +                            "mount\n");
333 +               goto failed;
334 +       }
335 +
336 +       retval = read_mmp_block(sb, &bh, mmp_block);
337 +       if (retval)
338 +               goto failed;
339 +       mmp = (struct mmp_struct *)(bh->b_data);
340 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
341 +               dump_mmp_msg(sb, mmp, __func__,
342 +                            "Device is already active on another node.");
343 +               goto failed;
344 +       }
345 +
346 +       /*
347 +        * Start a kernel thread to update the MMP block periodically.
348 +        */
349 +       EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
350 +                                            MAJOR(sb->s_dev),
351 +                                            MINOR(sb->s_dev));
352 +       if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
353 +               EXT4_SB(sb)->s_mmp_tsk = 0;
354 +               ext4_warning(sb, __func__, "Unable to create kmmpd thread "
355 +                            "for %s.", sb->s_id);
356 +               goto failed;
357 +       }
358 +
359 +       brelse(bh);
360 +       return 0;
361 +
362 +failed:
363 +       brelse(bh);
364 +       return 1;
365 +}
366  
367  static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
368  {
369 @@ -873,7 +1216,6 @@ static struct dentry *ext4_get_dentry(st
370         struct inode *inode;
371         struct dentry *result;
372  
373 -
374         if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
375                 return ERR_PTR(-ESTALE);
376         if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
377 @@ -2407,6 +2749,11 @@ static int ext4_fill_super(struct super_
378                           EXT4_HAS_INCOMPAT_FEATURE(sb,
379                                     EXT4_FEATURE_INCOMPAT_RECOVER));
380  
381 +       if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
382 +           !(sb->s_flags & MS_RDONLY))
383 +               if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
384 +                       goto failed_mount3;
385 +
386         /*
387          * The first inode we look at is the journal inode.  Don't try
388          * root first: it may be modified in the journal!
389 @@ -2621,6 +2968,8 @@ failed_mount3:
390         percpu_counter_destroy(&sbi->s_freeinodes_counter);
391         percpu_counter_destroy(&sbi->s_dirs_counter);
392         percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
393 +       if (sbi->s_mmp_tsk)
394 +               kthread_stop(sbi->s_mmp_tsk);
395  failed_mount2:
396         for (i = 0; i < db_count; i++)
397                 brelse(sbi->s_group_desc[i]);
398 @@ -3142,7 +3491,7 @@ static int ext4_remount(struct super_blo
399         struct ext4_mount_options old_opts;
400         ext4_group_t g;
401         unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
402 -       int err;
403 +       int err = 0;
404  #ifdef CONFIG_QUOTA
405         int i;
406  #endif
407 @@ -3278,6 +3627,13 @@ static int ext4_remount(struct super_blo
408                                 goto restore_opts;
409                         if (!ext4_setup_super(sb, es, 0))
410                                 sb->s_flags &= ~MS_RDONLY;
411 +                       if (EXT4_HAS_INCOMPAT_FEATURE(sb,
412 +                                                   EXT4_FEATURE_INCOMPAT_MMP))
413 +                               if (ext4_multi_mount_protect(sb,
414 +                                               le64_to_cpu(es->s_mmp_block))) {
415 +                                       err = -EROFS;
416 +                                       goto restore_opts;
417 +                               }
418                 }
419         }
420         if (sbi->s_journal == NULL)
421 Index: linux-stage/fs/ext4/ext4.h
422 ===================================================================
423 --- linux-stage.orig/fs/ext4/ext4.h
424 +++ linux-stage/fs/ext4/ext4.h
425 @@ -665,7 +665,7 @@ struct ext4_super_block {
426         __le16  s_want_extra_isize;     /* New inodes should reserve # bytes */
427         __le32  s_flags;                /* Miscellaneous flags */
428         __le16  s_raid_stride;          /* RAID stride */
429 -       __le16  s_mmp_interval;         /* # seconds to wait in MMP checking */
430 +       __le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */
431         __le64  s_mmp_block;            /* Block for multi-mount protection */
432         __le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
433         __u8    s_log_groups_per_flex;  /* FLEX_BG group size */
434 @@ -782,7 +782,8 @@ static inline int ext4_valid_inum(struct
435                                          EXT4_FEATURE_INCOMPAT_META_BG| \
436                                          EXT4_FEATURE_INCOMPAT_EXTENTS| \
437                                          EXT4_FEATURE_INCOMPAT_64BIT| \
438 -                                        EXT4_FEATURE_INCOMPAT_FLEX_BG)
439 +                                        EXT4_FEATURE_INCOMPAT_FLEX_BG| \
440 +                                        EXT4_FEATURE_INCOMPAT_MMP)
441  #define EXT4_FEATURE_RO_COMPAT_SUPP    (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
442                                          EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
443                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
444 @@ -995,6 +996,39 @@ do {                                                                       \
445  #endif
446  
447  /*
448 + * This structure will be used for multiple mount protection. It will be
449 + * written into the block number saved in the s_mmp_block field in the
450 + * superblock. Programs that check MMP should assume that if
451 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
452 + * to use the filesystem, regardless of how old the timestamp is.
453 + */
454 +#define EXT4_MMP_MAGIC     0x004D4D50U /* ASCII for MMP */
455 +#define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
456 +#define EXT4_MMP_SEQ_FSCK  0xE24D4D50U /* mmp_seq value when being fscked */
457 +#define EXT4_MMP_SEQ_MAX   0xE24D4D4FU /* maximum valid mmp_seq value */
458 +
459 +struct mmp_struct {
460 +       __le32  mmp_magic;
461 +       __le32  mmp_seq;
462 +       __le64  mmp_time;
463 +       char    mmp_nodename[64];
464 +       char    mmp_bdevname[32];
465 +       __le16  mmp_check_interval;
466 +       __le16  mmp_pad1;
467 +       __le32  mmp_pad2[227];
468 +};
469 +
470 +/*
471 + * Default interval in seconds to update the MMP sequence number.
472 + */
473 +#define EXT4_MMP_UPDATE_INTERVAL   1
474 +
475 +/*
476 + * Minimum interval for MMP checking in seconds.
477 + */
478 +#define EXT4_MMP_MIN_CHECK_INTERVAL    5
479 +
480 +/*
481   * Function prototypes
482   */
483  
484 Index: linux-stage/fs/ext4/ext4_sb.h
485 ===================================================================
486 --- linux-stage.orig/fs/ext4/ext4_sb.h
487 +++ linux-stage/fs/ext4/ext4_sb.h
488 @@ -149,6 +149,8 @@ struct ext4_sb_info {
489  
490         unsigned int s_log_groups_per_flex;
491         struct flex_groups *s_flex_groups;
492 +
493 +       struct task_struct *s_mmp_tsk;  /* Kernel thread for multiple mount protection */
494  };
495  
496  #endif /* _EXT4_SB */