1 Index: linux-stage/fs/ext4/super.c
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/super.c
4 +++ linux-stage/fs/ext4/super.c
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>
13 #include "ext4_jbd2.h"
14 @@ -600,6 +602,8 @@ static void ext4_put_super(struct super_
15 invalidate_bdev(sbi->journal_bdev);
16 ext4_blkdev_remove(sbi);
19 + kthread_stop(sbi->s_mmp_tsk);
23 @@ -808,7 +812,6 @@ static int ext4_show_options(struct seq_
24 if (!test_opt(sb, DELALLOC))
25 seq_puts(seq, ",nodelalloc");
29 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
31 @@ -831,6 +834,340 @@ static int ext4_show_options(struct seq_
37 + * Write the MMP block using WRITE_SYNC to try to get the block on-disk
40 +static int write_mmp_block(struct buffer_head *bh)
42 + mark_buffer_dirty(bh);
44 + bh->b_end_io = end_buffer_write_sync;
46 + submit_bh(WRITE_SYNC, bh);
48 + if (unlikely(!buffer_uptodate(bh)))
55 + * Read the MMP block. It _must_ be read from disk and hence we clear the
56 + * uptodate flag on the buffer.
58 +static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
59 + unsigned long mmp_block)
61 + struct mmp_struct *mmp;
64 + clear_buffer_uptodate(*bh);
66 + /* This would be sb_bread(sb, mmp_block), except we need to be sure
67 + * that the MD RAID device cache has been bypassed, and that the read
68 + * is not blocked in the elevator. */
70 + *bh = sb_getblk(sb, mmp_block);
74 + (*bh)->b_end_io = end_buffer_read_sync;
75 + submit_bh(READ_SYNC, *bh);
76 + wait_on_buffer(*bh);
77 + if (!buffer_uptodate(*bh)) {
83 + ext4_warning(sb, __func__,
84 + "Error while reading MMP block %lu", mmp_block);
88 + mmp = (struct mmp_struct *)((*bh)->b_data);
89 + if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC)
96 + * Dump as much information as possible to help the admin.
98 +static void dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
99 + const char *function, const char *msg)
101 + ext4_warning(sb, function, "%s", msg);
102 + ext4_warning(sb, function, "MMP failure info: last update time: %llu, "
103 + "last update node: %s, last update device: %s\n",
104 + (long long unsigned int)le64_to_cpu(mmp->mmp_time),
105 + mmp->mmp_nodename, mmp->mmp_bdevname);
109 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
111 +static int kmmpd(void *data)
113 + struct super_block *sb = (struct super_block *) data;
114 + struct ext4_super_block *es = EXT4_SB(sb)->s_es;
115 + struct buffer_head *bh = NULL;
116 + struct mmp_struct *mmp;
117 + unsigned long mmp_block;
119 + unsigned long failed_writes = 0;
120 + int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
121 + unsigned mmp_check_interval;
122 + unsigned long last_update_time;
123 + unsigned long diff;
126 + mmp_block = le64_to_cpu(es->s_mmp_block);
127 + retval = read_mmp_block(sb, &bh, mmp_block);
131 + mmp = (struct mmp_struct *)(bh->b_data);
132 + mmp->mmp_time = cpu_to_le64(get_seconds());
134 + * Start with the higher mmp_check_interval and reduce it if
135 + * the MMP block is being updated on time.
137 + mmp_check_interval = max(5 * mmp_update_interval,
138 + EXT4_MMP_MIN_CHECK_INTERVAL);
139 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
140 + bdevname(bh->b_bdev, mmp->mmp_bdevname);
142 + memcpy(mmp->mmp_nodename, init_utsname()->sysname,
143 + sizeof(mmp->mmp_nodename));
145 + while (!kthread_should_stop()) {
146 + if (++seq > EXT4_MMP_SEQ_MAX)
149 + mmp->mmp_seq = cpu_to_le32(seq);
150 + mmp->mmp_time = cpu_to_le64(get_seconds());
151 + last_update_time = jiffies;
153 + retval = write_mmp_block(bh);
155 + * Don't spew too many error messages. Print one every
156 + * (s_mmp_update_interval * 60) seconds.
158 + if (retval && (failed_writes % 60) == 0) {
159 + ext4_error(sb, __func__,
160 + "Error writing to MMP block");
164 + if (!(le32_to_cpu(es->s_feature_incompat) &
165 + EXT4_FEATURE_INCOMPAT_MMP)) {
166 + ext4_warning(sb, __func__, "kmmpd being stopped "
167 + "since MMP feature has been disabled.");
168 + EXT4_SB(sb)->s_mmp_tsk = 0;
172 + if (sb->s_flags & MS_RDONLY) {
173 + ext4_warning(sb, __func__, "kmmpd being stopped "
174 + "since filesystem has been remounted as "
176 + EXT4_SB(sb)->s_mmp_tsk = 0;
180 + diff = jiffies - last_update_time;
181 + if (diff < mmp_update_interval * HZ)
182 + schedule_timeout_interruptible(EXT4_MMP_UPDATE_INTERVAL*
186 + * We need to make sure that more than mmp_check_interval
187 + * seconds have not passed since writing. If that has happened
188 + * we need to check if the MMP block is as we left it.
190 + diff = jiffies - last_update_time;
191 + if (diff > mmp_check_interval * HZ) {
192 + struct buffer_head *bh_check = NULL;
193 + struct mmp_struct *mmp_check;
195 + retval = read_mmp_block(sb, &bh_check, mmp_block);
197 + EXT4_SB(sb)->s_mmp_tsk = 0;
201 + mmp_check = (struct mmp_struct *)(bh_check->b_data);
202 + if (mmp->mmp_time != mmp_check->mmp_time ||
203 + memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
204 + sizeof(mmp->mmp_nodename)))
205 + dump_mmp_msg(sb, mmp_check, __func__,
206 + "Error while updating MMP info. "
207 + "The filesystem seems to have "
208 + "been multiply mounted.");
214 + * Adjust the mmp_check_interval depending on how much time
215 + * it took for the MMP block to be written.
217 + mmp_check_interval = max(5 * diff / HZ,
218 + (unsigned long) EXT4_MMP_MIN_CHECK_INTERVAL);
219 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
223 + * Unmount seems to be clean.
225 + mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
226 + mmp->mmp_time = cpu_to_le64(get_seconds());
228 + retval = write_mmp_block(bh);
236 + * Get a random new sequence number but make sure it is not greater than
237 + * EXT4_MMP_SEQ_MAX.
239 +static unsigned int mmp_new_seq(void)
244 + get_random_bytes(&new_seq, sizeof(u32));
245 + } while (new_seq > EXT4_MMP_SEQ_MAX);
251 + * Protect the filesystem from being mounted more than once.
253 +static int ext4_multi_mount_protect(struct super_block *sb,
254 + unsigned long mmp_block)
256 + struct ext4_super_block *es = EXT4_SB(sb)->s_es;
257 + struct buffer_head *bh = NULL;
258 + struct mmp_struct *mmp = NULL;
260 + unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
261 + unsigned int wait_time = 0;
264 + if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
265 + mmp_block >= ext4_blocks_count(es)) {
266 + ext4_warning(sb, __func__,
267 + "Invalid MMP block in superblock");
271 + retval = read_mmp_block(sb, &bh, mmp_block);
275 + mmp = (struct mmp_struct *)(bh->b_data);
277 + if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
278 + mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
281 + * If check_interval in MMP block is larger, use that instead of
282 + * update_interval from the superblock.
284 + if (mmp->mmp_check_interval > mmp_check_interval)
285 + mmp_check_interval = mmp->mmp_check_interval;
287 + seq = le32_to_cpu(mmp->mmp_seq);
288 + if (seq == EXT4_MMP_SEQ_CLEAN)
291 + if (seq == EXT4_MMP_SEQ_FSCK) {
292 + dump_mmp_msg(sb, mmp, __func__,
293 + "fsck is running on the filesystem");
297 + wait_time = min(mmp_check_interval * 2 + 1,
298 + mmp_check_interval + 60);
300 + /* Print MMP interval if more than 20 secs. */
301 + if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
302 + ext4_warning(sb, __func__, "MMP interval %u higher than "
303 + "expected, please wait.\n", wait_time * 2);
305 + if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
306 + ext4_warning(sb, __func__, "MMP startup interrupted, failing mount\n");
310 + retval = read_mmp_block(sb, &bh, mmp_block);
313 + mmp = (struct mmp_struct *)(bh->b_data);
314 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
315 + dump_mmp_msg(sb, mmp, __func__,
316 + "Device is already active on another node.");
322 + * write a new random sequence number.
324 + mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
326 + retval = write_mmp_block(bh);
331 + * wait for MMP interval and check mmp_seq.
333 + if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
334 + ext4_warning(sb, __func__, "MMP startup interrupted, failing mount\n");
338 + retval = read_mmp_block(sb, &bh, mmp_block);
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.");
349 + * Start a kernel thread to update the MMP block periodically.
351 + EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
354 + if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
355 + EXT4_SB(sb)->s_mmp_tsk = 0;
356 + ext4_warning(sb, __func__, "Unable to create kmmpd thread "
357 + "for %s.", sb->s_id);
369 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
370 u64 ino, u32 generation)
372 @@ -2371,6 +2708,11 @@ static int ext4_fill_super(struct super_
373 EXT4_HAS_INCOMPAT_FEATURE(sb,
374 EXT4_FEATURE_INCOMPAT_RECOVER));
376 + if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
377 + !(sb->s_flags & MS_RDONLY))
378 + if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
379 + goto failed_mount3;
382 * The first inode we look at is the journal inode. Don't try
383 * root first: it may be modified in the journal!
384 @@ -2571,6 +2913,8 @@ failed_mount3:
385 percpu_counter_destroy(&sbi->s_freeinodes_counter);
386 percpu_counter_destroy(&sbi->s_dirs_counter);
387 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
388 + if (sbi->s_mmp_tsk)
389 + kthread_stop(sbi->s_mmp_tsk);
391 for (i = 0; i < db_count; i++)
392 brelse(sbi->s_group_desc[i]);
393 @@ -3086,7 +3430,7 @@ static int ext4_remount(struct super_blo
394 unsigned long old_sb_flags;
395 struct ext4_mount_options old_opts;
402 @@ -3211,6 +3555,13 @@ static int ext4_remount(struct super_blo
404 if (!ext4_setup_super(sb, es, 0))
405 sb->s_flags &= ~MS_RDONLY;
406 + if (EXT4_HAS_INCOMPAT_FEATURE(sb,
407 + EXT4_FEATURE_INCOMPAT_MMP))
408 + if (ext4_multi_mount_protect(sb,
409 + le64_to_cpu(es->s_mmp_block))) {
416 Index: linux-stage/fs/ext4/ext4.h
417 ===================================================================
418 --- linux-stage.orig/fs/ext4/ext4.h
419 +++ linux-stage/fs/ext4/ext4.h
420 @@ -660,7 +660,7 @@ struct ext4_super_block {
421 __le16 s_want_extra_isize; /* New inodes should reserve # bytes */
422 __le32 s_flags; /* Miscellaneous flags */
423 __le16 s_raid_stride; /* RAID stride */
424 - __le16 s_mmp_interval; /* # seconds to wait in MMP checking */
425 + __le16 s_mmp_update_interval; /* # seconds to wait in MMP checking */
426 __le64 s_mmp_block; /* Block for multi-mount protection */
427 __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
428 __u8 s_log_groups_per_flex; /* FLEX_BG group size */
429 @@ -777,7 +777,8 @@ static inline int ext4_valid_inum(struct
430 EXT4_FEATURE_INCOMPAT_META_BG| \
431 EXT4_FEATURE_INCOMPAT_EXTENTS| \
432 EXT4_FEATURE_INCOMPAT_64BIT| \
433 - EXT4_FEATURE_INCOMPAT_FLEX_BG)
434 + EXT4_FEATURE_INCOMPAT_FLEX_BG| \
435 + EXT4_FEATURE_INCOMPAT_MMP)
436 #define EXT4_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
437 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
438 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
439 @@ -981,6 +982,39 @@ do { \
443 + * This structure will be used for multiple mount protection. It will be
444 + * written into the block number saved in the s_mmp_block field in the
445 + * superblock. Programs that check MMP should assume that if
446 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
447 + * to use the filesystem, regardless of how old the timestamp is.
449 +#define EXT4_MMP_MAGIC 0x004D4D50U /* ASCII for MMP */
450 +#define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
451 +#define EXT4_MMP_SEQ_FSCK 0xE24D4D50U /* mmp_seq value when being fscked */
452 +#define EXT4_MMP_SEQ_MAX 0xE24D4D4FU /* maximum valid mmp_seq value */
458 + char mmp_nodename[64];
459 + char mmp_bdevname[32];
460 + __le16 mmp_check_interval;
462 + __le32 mmp_pad2[227];
466 + * Default interval in seconds to update the MMP sequence number.
468 +#define EXT4_MMP_UPDATE_INTERVAL 1
471 + * Minimum interval for MMP checking in seconds.
473 +#define EXT4_MMP_MIN_CHECK_INTERVAL 5
476 * Function prototypes
479 Index: linux-stage/fs/ext4/ext4_sb.h
480 ===================================================================
481 --- linux-stage.orig/fs/ext4/ext4_sb.h
482 +++ linux-stage/fs/ext4/ext4_sb.h
483 @@ -149,6 +149,8 @@ struct ext4_sb_info {
485 unsigned int s_log_groups_per_flex;
486 struct flex_groups *s_flex_groups;
488 + struct task_struct *s_mmp_tsk; /* Kernel thread for multiple mount protection */
491 #endif /* _EXT4_SB */