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
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 @@ -598,6 +600,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 @@ -806,7 +810,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 @@ -829,6 +832,330 @@ 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);
69 + *bh = sb_bread(sb, mmp_block);
72 + *bh = sb_getblk(sb, mmp_block);
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)) {
86 + ext4_warning(sb, __FUNCTION__,
87 + "Error while reading MMP block %lu", mmp_block);
91 + mmp = (struct mmp_struct *)((*bh)->b_data);
92 + if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC)
99 + * Dump as much information as possible to help the admin.
101 +static void dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
102 + const char *function, const char *msg)
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);
112 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
114 +static int kmmpd(void *data)
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;
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;
129 + mmp_block = le64_to_cpu(es->s_mmp_block);
130 + retval = read_mmp_block(sb, &bh, mmp_block);
134 + mmp = (struct mmp_struct *)(bh->b_data);
135 + mmp->mmp_time = cpu_to_le64(get_seconds());
137 + * Start with the higher mmp_check_interval and reduce it if
138 + * the MMP block is being updated on time.
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);
145 + down_read(&uts_sem);
146 + memcpy(mmp->mmp_nodename, init_utsname()->sysname,
147 + sizeof(mmp->mmp_nodename));
150 + while (!kthread_should_stop()) {
151 + if (++seq > EXT4_MMP_SEQ_MAX)
154 + mmp->mmp_seq = cpu_to_le32(seq);
155 + mmp->mmp_time = cpu_to_le64(get_seconds());
156 + last_update_time = jiffies;
158 + retval = write_mmp_block(bh);
160 + * Don't spew too many error messages. Print one every
161 + * (s_mmp_update_interval * 60) seconds.
163 + if (retval && (failed_writes % 60) == 0) {
164 + ext4_error(sb, __FUNCTION__,
165 + "Error writing to MMP block");
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;
177 + if (sb->s_flags & MS_RDONLY) {
178 + ext4_warning(sb, __FUNCTION__, "kmmpd being stopped "
179 + "since filesystem has been remounted as "
181 + EXT4_SB(sb)->s_mmp_tsk = 0;
185 + diff = jiffies - last_update_time;
186 + if (diff < mmp_update_interval * HZ)
187 + schedule_timeout_interruptible(EXT4_MMP_UPDATE_INTERVAL*
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.
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;
200 + retval = read_mmp_block(sb, &bh_check, mmp_block);
202 + EXT4_SB(sb)->s_mmp_tsk = 0;
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.");
219 + * Adjust the mmp_check_interval depending on how much time
220 + * it took for the MMP block to be written.
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);
228 + * Unmount seems to be clean.
230 + mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
231 + mmp->mmp_time = cpu_to_le64(get_seconds());
233 + retval = write_mmp_block(bh);
241 + * Get a random new sequence number but make sure it is not greater than
242 + * EXT4_MMP_SEQ_MAX.
244 +static unsigned int mmp_new_seq(void)
249 + get_random_bytes(&new_seq, sizeof(u32));
250 + } while (new_seq > EXT4_MMP_SEQ_MAX);
256 + * Protect the filesystem from being mounted more than once.
258 +static int ext4_multi_mount_protect(struct super_block *sb,
259 + unsigned long mmp_block)
261 + struct ext4_super_block *es = EXT4_SB(sb)->s_es;
262 + struct buffer_head *bh = NULL;
263 + struct mmp_struct *mmp = NULL;
265 + unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
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");
275 + retval = read_mmp_block(sb, &bh, mmp_block);
279 + mmp = (struct mmp_struct *)(bh->b_data);
281 + if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
282 + mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
285 + * If check_interval in MMP block is larger, use that instead of
286 + * update_interval from the superblock.
288 + if (mmp->mmp_check_interval > mmp_check_interval)
289 + mmp_check_interval = mmp->mmp_check_interval;
291 + seq = le32_to_cpu(mmp->mmp_seq);
292 + if (seq == EXT4_MMP_SEQ_CLEAN)
295 + if (seq == EXT4_MMP_SEQ_FSCK) {
296 + dump_mmp_msg(sb, mmp, __FUNCTION__,
297 + "fsck is running on the filesystem");
301 + schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
303 + retval = read_mmp_block(sb, &bh, mmp_block);
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.");
315 + * write a new random sequence number.
317 + mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
319 + retval = write_mmp_block(bh);
324 + * wait for MMP interval and check mmp_seq.
326 + schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
328 + retval = read_mmp_block(sb, &bh, mmp_block);
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.");
339 + * Start a kernel thread to update the MMP block periodically.
341 + EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
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);
359 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
360 u64 ino, u32 generation)
362 @@ -2366,6 +2693,11 @@ static int ext4_fill_super(struct super_
363 EXT4_HAS_INCOMPAT_FEATURE(sb,
364 EXT4_FEATURE_INCOMPAT_RECOVER));
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;
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);
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;
392 @@ -3205,6 +3539,13 @@ static int ext4_remount(struct super_blo
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))) {
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 { \
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.
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 */
448 + char mmp_nodename[64];
449 + char mmp_bdevname[32];
450 + __le16 mmp_check_interval;
452 + __le32 mmp_pad2[227];
456 + * Default interval in seconds to update the MMP sequence number.
458 +#define EXT4_MMP_UPDATE_INTERVAL 1
461 + * Minimum interval for MMP checking in seconds.
463 +#define EXT4_MMP_MIN_CHECK_INTERVAL 5
466 * Function prototypes
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 {
475 unsigned int s_log_groups_per_flex;
476 struct flex_groups *s_flex_groups;
478 + struct task_struct *s_mmp_tsk; /* Kernel thread for multiple mount protection */
481 #endif /* _EXT4_SB */