1 Index: linux-2.6.18-128.1.6/fs/ext4/super.c
2 ===================================================================
3 --- linux-2.6.18-128.1.6.orig/fs/ext4/super.c
4 +++ linux-2.6.18-128.1.6/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 @@ -547,6 +549,8 @@ static void ext4_put_super(struct super_
15 invalidate_bdev(sbi->journal_bdev, 0);
16 ext4_blkdev_remove(sbi);
19 + kthread_stop(sbi->s_mmp_tsk);
23 @@ -766,6 +770,328 @@ static int ext4_show_options(struct seq_
28 + * Write the MMP block using WRITE_SYNC to try to get the block on-disk
31 +static int write_mmp_block(struct buffer_head *bh)
33 + mark_buffer_dirty(bh);
35 + bh->b_end_io = end_buffer_write_sync;
37 + submit_bh(WRITE_SYNC, bh);
39 + if (unlikely(!buffer_uptodate(bh)))
46 + * Read the MMP block. It _must_ be read from disk and hence we clear the
47 + * uptodate flag on the buffer.
49 +static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
50 + unsigned long mmp_block)
52 + struct mmp_struct *mmp;
55 + clear_buffer_uptodate(*bh);
60 + *bh = sb_bread(sb, mmp_block);
63 + *bh = sb_getblk(sb, mmp_block);
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)) {
77 + ext4_warning(sb, __FUNCTION__,
78 + "Error while reading MMP block %lu", mmp_block);
82 + mmp = (struct mmp_struct *)((*bh)->b_data);
83 + if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC)
90 + * Dump as much information as possible to help the admin.
92 +static void dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
93 + const char *function, const char *msg)
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 + le64_to_cpu(mmp->mmp_time), mmp->mmp_nodename,
103 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
105 +static int kmmpd(void *data)
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;
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;
120 + mmp_block = le64_to_cpu(es->s_mmp_block);
121 + retval = read_mmp_block(sb, &bh, mmp_block);
125 + mmp = (struct mmp_struct *)(bh->b_data);
126 + mmp->mmp_time = cpu_to_le64(get_seconds());
128 + * Start with the higher mmp_check_interval and reduce it if
129 + * the MMP block is being updated on time.
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);
136 + down_read(&uts_sem);
137 + memcpy(mmp->mmp_nodename, system_utsname.nodename,
138 + sizeof(mmp->mmp_nodename));
141 + while (!kthread_should_stop()) {
142 + if (++seq > EXT4_MMP_SEQ_MAX)
145 + mmp->mmp_seq = cpu_to_le32(seq);
146 + mmp->mmp_time = cpu_to_le64(get_seconds());
147 + last_update_time = jiffies;
149 + retval = write_mmp_block(bh);
151 + * Don't spew too many error messages. Print one every
152 + * (s_mmp_update_interval * 60) seconds.
154 + if (retval && (failed_writes % 60) == 0) {
155 + ext4_error(sb, __FUNCTION__,
156 + "Error writing to MMP block");
160 + if (!(le32_to_cpu(es->s_feature_incompat) &
161 + EXT4_FEATURE_INCOMPAT_MMP)) {
162 + ext4_warning(sb, __FUNCTION__, "kmmpd being stopped "
163 + "since MMP feature has been disabled.");
164 + EXT4_SB(sb)->s_mmp_tsk = 0;
168 + if (sb->s_flags & MS_RDONLY) {
169 + ext4_warning(sb, __FUNCTION__, "kmmpd being stopped "
170 + "since filesystem has been remounted as "
172 + EXT4_SB(sb)->s_mmp_tsk = 0;
176 + diff = jiffies - last_update_time;
177 + if (diff < mmp_update_interval * HZ)
178 + schedule_timeout_interruptible(EXT4_MMP_UPDATE_INTERVAL*
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.
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;
191 + retval = read_mmp_block(sb, &bh_check, mmp_block);
193 + EXT4_SB(sb)->s_mmp_tsk = 0;
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, __FUNCTION__,
202 + "Error while updating MMP info. "
203 + "The filesystem seems to have "
204 + "been multiply mounted.");
210 + * Adjust the mmp_check_interval depending on how much time
211 + * it took for the MMP block to be written.
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);
219 + * Unmount seems to be clean.
221 + mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
222 + mmp->mmp_time = cpu_to_le64(get_seconds());
224 + retval = write_mmp_block(bh);
232 + * Get a random new sequence number but make sure it is not greater than
233 + * EXT4_MMP_SEQ_MAX.
235 +static unsigned int mmp_new_seq(void)
240 + get_random_bytes(&new_seq, sizeof(u32));
241 + } while (new_seq > EXT4_MMP_SEQ_MAX);
247 + * Protect the filesystem from being mounted more than once.
249 +static int ext4_multi_mount_protect(struct super_block *sb,
250 + unsigned long mmp_block)
252 + struct ext4_super_block *es = EXT4_SB(sb)->s_es;
253 + struct buffer_head *bh = NULL;
254 + struct mmp_struct *mmp = NULL;
256 + unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
259 + if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
260 + mmp_block >= ext4_blocks_count(es)) {
261 + ext4_warning(sb, __FUNCTION__,
262 + "Invalid MMP block in superblock");
266 + retval = read_mmp_block(sb, &bh, mmp_block);
270 + mmp = (struct mmp_struct *)(bh->b_data);
272 + if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
273 + mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
276 + * If check_interval in MMP block is larger, use that instead of
277 + * update_interval from the superblock.
279 + if (mmp->mmp_check_interval > mmp_check_interval)
280 + mmp_check_interval = mmp->mmp_check_interval;
282 + seq = le32_to_cpu(mmp->mmp_seq);
283 + if (seq == EXT4_MMP_SEQ_CLEAN)
286 + if (seq == EXT4_MMP_SEQ_FSCK) {
287 + dump_mmp_msg(sb, mmp, __FUNCTION__,
288 + "fsck is running on the filesystem");
292 + schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
294 + retval = read_mmp_block(sb, &bh, mmp_block);
297 + mmp = (struct mmp_struct *)(bh->b_data);
298 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
299 + dump_mmp_msg(sb, mmp, __FUNCTION__,
300 + "Device is already active on another node.");
306 + * write a new random sequence number.
308 + mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
310 + retval = write_mmp_block(bh);
315 + * wait for MMP interval and check mmp_seq.
317 + schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
319 + retval = read_mmp_block(sb, &bh, mmp_block);
322 + mmp = (struct mmp_struct *)(bh->b_data);
323 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
324 + dump_mmp_msg(sb, mmp, __FUNCTION__,
325 + "Device is already active on another node.");
330 + * Start a kernel thread to update the MMP block periodically.
332 + EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
335 + if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
336 + EXT4_SB(sb)->s_mmp_tsk = 0;
337 + ext4_warning(sb, __FUNCTION__, "Unable to create kmmpd thread "
338 + "for %s.", sb->s_id);
350 static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
352 @@ -775,7 +1101,6 @@ static struct dentry *ext4_get_dentry(st
354 struct dentry *result;
357 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
358 return ERR_PTR(-ESTALE);
359 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
360 @@ -2258,6 +2583,11 @@ static int ext4_fill_super(struct super_
361 EXT4_HAS_INCOMPAT_FEATURE(sb,
362 EXT4_FEATURE_INCOMPAT_RECOVER));
364 + if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
365 + !(sb->s_flags & MS_RDONLY))
366 + if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
367 + goto failed_mount3;
370 * The first inode we look at is the journal inode. Don't try
371 * root first: it may be modified in the journal!
372 @@ -2445,6 +2775,8 @@ failed_mount3:
373 percpu_counter_destroy(&sbi->s_freeinodes_counter);
374 percpu_counter_destroy(&sbi->s_dirs_counter);
375 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
376 + if (sbi->s_mmp_tsk)
377 + kthread_stop(sbi->s_mmp_tsk);
379 for (i = 0; i < db_count; i++)
380 brelse(sbi->s_group_desc[i]);
381 @@ -2918,7 +3250,7 @@ static int ext4_remount(struct super_blo
382 struct ext4_mount_options old_opts;
384 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
390 @@ -3042,6 +3374,13 @@ static int ext4_remount(struct super_blo
392 if (!ext4_setup_super(sb, es, 0))
393 sb->s_flags &= ~MS_RDONLY;
394 + if (EXT4_HAS_INCOMPAT_FEATURE(sb,
395 + EXT4_FEATURE_INCOMPAT_MMP))
396 + if (ext4_multi_mount_protect(sb,
397 + le64_to_cpu(es->s_mmp_block))) {
404 Index: linux-2.6.18-128.1.6/fs/ext4/ext4.h
405 ===================================================================
406 --- linux-2.6.18-128.1.6.orig/fs/ext4/ext4.h
407 +++ linux-2.6.18-128.1.6/fs/ext4/ext4.h
408 @@ -658,7 +658,7 @@ struct ext4_super_block {
409 __le16 s_want_extra_isize; /* New inodes should reserve # bytes */
410 __le32 s_flags; /* Miscellaneous flags */
411 __le16 s_raid_stride; /* RAID stride */
412 - __le16 s_mmp_interval; /* # seconds to wait in MMP checking */
413 + __le16 s_mmp_update_interval; /* # seconds to wait in MMP checking */
414 __le64 s_mmp_block; /* Block for multi-mount protection */
415 __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
416 __u8 s_log_groups_per_flex; /* FLEX_BG group size */
417 @@ -775,7 +775,8 @@ static inline int ext4_valid_inum(struct
418 EXT4_FEATURE_INCOMPAT_META_BG| \
419 EXT4_FEATURE_INCOMPAT_EXTENTS| \
420 EXT4_FEATURE_INCOMPAT_64BIT| \
421 - EXT4_FEATURE_INCOMPAT_FLEX_BG)
422 + EXT4_FEATURE_INCOMPAT_FLEX_BG| \
423 + EXT4_FEATURE_INCOMPAT_MMP)
424 #define EXT4_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
425 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
426 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
427 @@ -956,6 +957,39 @@ void ext4_get_group_no_and_offset(struct
428 unsigned long *blockgrpp, ext4_grpblk_t *offsetp);
431 + * This structure will be used for multiple mount protection. It will be
432 + * written into the block number saved in the s_mmp_block field in the
433 + * superblock. Programs that check MMP should assume that if
434 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
435 + * to use the filesystem, regardless of how old the timestamp is.
437 +#define EXT4_MMP_MAGIC 0x004D4D50U /* ASCII for MMP */
438 +#define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
439 +#define EXT4_MMP_SEQ_FSCK 0xE24D4D50U /* mmp_seq value when being fscked */
440 +#define EXT4_MMP_SEQ_MAX 0xE24D4D4FU /* maximum valid mmp_seq value */
446 + char mmp_nodename[64];
447 + char mmp_bdevname[32];
448 + __le16 mmp_check_interval;
450 + __le32 mmp_pad2[227];
454 + * Default interval in seconds to update the MMP sequence number.
456 +#define EXT4_MMP_UPDATE_INTERVAL 1
459 + * Minimum interval for MMP checking in seconds.
461 +#define EXT4_MMP_MIN_CHECK_INTERVAL 5
464 * Function prototypes
467 Index: linux-2.6.18-128.1.6/fs/ext4/ext4_sb.h
468 ===================================================================
469 --- linux-2.6.18-128.1.6.orig/fs/ext4/ext4_sb.h
470 +++ linux-2.6.18-128.1.6/fs/ext4/ext4_sb.h
471 @@ -148,6 +148,8 @@ struct ext4_sb_info {
473 unsigned int s_log_groups_per_flex;
474 struct flex_groups *s_flex_groups;
476 + struct task_struct *s_mmp_tsk; /* Kernel thread for multiple mount protection */
479 #endif /* _EXT4_SB */