1 Prevent an ext4 filesystem from being mounted multiple times.
2 A sequence number is stored on disk and is periodically updated (every 5
3 seconds by default) by a mounted filesystem.
4 At mount time, we now wait for s_mmp_update_interval seconds to make sure
5 that the MMP sequence does not change.
6 In case of failure, the nodename, bdevname and the time at which the MMP
7 block was last updated is displayed.
8 Move all mmp code to a dedicated file (mmp.c).
10 Signed-off-by: Andreas Dilger <adilger <at> whamcloud.com>
11 Signed-off-by: Johann Lombardi <johann <at> whamcloud.com>
13 fs/ext4/Makefile | 3 +-
14 fs/ext4/ext4.h | 76 ++++++++++++-
15 fs/ext4/mmp.c | 354 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
16 fs/ext4/super.c | 18 +++-
17 4 files changed, 447 insertions(+), 4 deletions(-)
18 create mode 100644 fs/ext4/mmp.c
20 Index: linux-stage/fs/ext4/Makefile
21 ===================================================================
22 --- linux-stage.orig/fs/ext4/Makefile
23 +++ linux-stage/fs/ext4/Makefile
24 @@ -6,7 +6,8 @@ obj-$(CONFIG_EXT4_FS) += ext4.o
26 ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
27 ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \
28 - ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o
29 + ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o \
32 ext4-$(CONFIG_EXT4_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
33 ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
34 Index: linux-stage/fs/ext4/ext4.h
35 ===================================================================
36 --- linux-stage.orig/fs/ext4/ext4.h
37 +++ linux-stage/fs/ext4/ext4.h
38 @@ -1009,7 +1009,7 @@ struct ext4_super_block {
39 __le16 s_want_extra_isize; /* New inodes should reserve # bytes */
40 __le32 s_flags; /* Miscellaneous flags */
41 __le16 s_raid_stride; /* RAID stride */
42 - __le16 s_mmp_interval; /* # seconds to wait in MMP checking */
43 + __le16 s_mmp_update_interval; /* # seconds to wait in MMP checking */
44 __le64 s_mmp_block; /* Block for multi-mount protection */
45 __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
46 __u8 s_log_groups_per_flex; /* FLEX_BG group size */
47 @@ -1177,6 +1177,9 @@ struct ext4_sb_info {
48 /* workqueue for dio unwritten */
49 struct workqueue_struct *dio_unwritten_wq;
51 + /* Kernel thread for multiple mount protection */
52 + struct task_struct *s_mmp_tsk;
54 /* Lazy inode table initialization info */
55 struct ext4_li_request *s_li_request;
56 /* Wait multiplier for lazy initialization thread */
57 @@ -1322,7 +1325,8 @@ EXT4_INODE_BIT_FNS(state, state_flags)
58 EXT4_FEATURE_INCOMPAT_META_BG| \
59 EXT4_FEATURE_INCOMPAT_EXTENTS| \
60 EXT4_FEATURE_INCOMPAT_64BIT| \
61 - EXT4_FEATURE_INCOMPAT_FLEX_BG)
62 + EXT4_FEATURE_INCOMPAT_FLEX_BG| \
63 + EXT4_FEATURE_INCOMPAT_MMP)
64 #define EXT4_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
65 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
66 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
67 @@ -1576,6 +1580,67 @@ struct ext4_features {
71 + * This structure will be used for multiple mount protection. It will be
72 + * written into the block number saved in the s_mmp_block field in the
73 + * superblock. Programs that check MMP should assume that if
74 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
75 + * to use the filesystem, regardless of how old the timestamp is.
77 +#define EXT4_MMP_MAGIC 0x004D4D50U /* ASCII for MMP */
78 +#define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
79 +#define EXT4_MMP_SEQ_FSCK 0xE24D4D50U /* mmp_seq value when being fscked */
80 +#define EXT4_MMP_SEQ_MAX 0xE24D4D4FU /* maximum valid mmp_seq value */
83 + __le32 mmp_magic; /* Magic number for MMP */
84 + __le32 mmp_seq; /* Sequence no. updated periodically */
87 + * mmp_time, mmp_nodename & mmp_bdevname are only used for information
88 + * purposes and do not affect the correctness of the algorithm
90 + __le64 mmp_time; /* Time last updated */
91 + char mmp_nodename[64]; /* Node which last updated MMP block */
92 + char mmp_bdevname[32]; /* Bdev which last updated MMP block */
95 + * mmp_check_interval is used to verify if the MMP block has been
96 + * updated on the block device. The value is updated based on the
97 + * maximum time to write the MMP block during an update cycle.
99 + __le16 mmp_check_interval;
102 + __le32 mmp_pad2[227];
105 +/* arguments passed to the mmp thread */
107 + struct buffer_head *bh; /* bh from initial read_mmp_block() */
108 + struct super_block *sb; /* super block of the fs */
112 + * Check interval multiplier
113 + * The MMP block is written every update interval and initially checked every
114 + * update interval x the multiplier (the value is then adapted based on the
115 + * write latency). The reason is that writes can be delayed under load and we
116 + * don't want readers to incorrectly assume that the filesystem is no longer
119 +#define EXT4_MMP_CHECK_MULT 2UL
122 + * Minimum interval for MMP checking in seconds.
124 +#define EXT4_MMP_MIN_CHECK_INTERVAL 5UL
127 + * Maximum interval for MMP checking in seconds.
129 +#define EXT4_MMP_MAX_CHECK_INTERVAL 300UL
132 * Function prototypes
135 @@ -1757,6 +1822,10 @@ extern void __ext4_warning(struct super_
136 #define ext4_warning(sb, message...) __ext4_warning(sb, __func__, ## message)
137 extern void ext4_msg(struct super_block *, const char *, const char *, ...)
138 __attribute__ ((format (printf, 3, 4)));
139 +extern void __dump_mmp_msg(struct super_block *, struct mmp_struct *mmp,
140 + const char *, const char *);
141 +#define dump_mmp_msg(sb, mmp, msg) __dump_mmp_msg(sb, mmp, __func__, \
143 extern void ext4_grp_locked_error(struct super_block *, ext4_group_t,
144 const char *, const char *, ...)
145 __attribute__ ((format (printf, 4, 5)));
146 @@ -2050,6 +2119,8 @@ extern int ext4_move_extents(struct file
147 __u64 start_orig, __u64 start_donor,
148 __u64 len, __u64 *moved_len);
151 +extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t);
154 * Add new method to test wether block and inode bitmaps are properly
155 Index: linux-stage/fs/ext4/mmp.c
156 ===================================================================
158 +++ linux-stage/fs/ext4/mmp.c
160 +#include <linux/fs.h>
161 +#include <linux/random.h>
162 +#include <linux/buffer_head.h>
163 +#include <linux/utsname.h>
164 +#include <linux/kthread.h>
169 + * Write the MMP block using WRITE_SYNC to try to get the block on-disk
172 +static int write_mmp_block(struct buffer_head *bh)
174 + mark_buffer_dirty(bh);
176 + bh->b_end_io = end_buffer_write_sync;
178 + submit_bh(WRITE_SYNC, bh);
179 + wait_on_buffer(bh);
180 + if (unlikely(!buffer_uptodate(bh)))
187 + * Read the MMP block. It _must_ be read from disk and hence we clear the
188 + * uptodate flag on the buffer.
190 +static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
191 + ext4_fsblk_t mmp_block)
193 + struct mmp_struct *mmp;
196 + clear_buffer_uptodate(*bh);
198 + /* This would be sb_bread(sb, mmp_block), except we need to be sure
199 + * that the MD RAID device cache has been bypassed, and that the read
200 + * is not blocked in the elevator. */
202 + *bh = sb_getblk(sb, mmp_block);
206 + (*bh)->b_end_io = end_buffer_read_sync;
207 + submit_bh(READ_SYNC, *bh);
208 + wait_on_buffer(*bh);
209 + if (!buffer_uptodate(*bh)) {
215 + ext4_warning(sb, "Error while reading MMP block %llu",
220 + mmp = (struct mmp_struct *)((*bh)->b_data);
221 + if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC) {
230 + * Dump as much information as possible to help the admin.
232 +void __dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
233 + const char *function, const char *msg)
235 + __ext4_warning(sb, function, msg);
236 + __ext4_warning(sb, function,
237 + "MMP failure info: last update time: %llu, last update "
238 + "node: %s, last update device: %s\n",
239 + (long long unsigned int) le64_to_cpu(mmp->mmp_time),
240 + mmp->mmp_nodename, mmp->mmp_bdevname);
244 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
246 +static int kmmpd(void *data)
248 + struct super_block *sb = ((struct mmpd_data *) data)->sb;
249 + struct buffer_head *bh = ((struct mmpd_data *) data)->bh;
250 + struct ext4_super_block *es = EXT4_SB(sb)->s_es;
251 + struct mmp_struct *mmp;
252 + ext4_fsblk_t mmp_block;
254 + unsigned long failed_writes = 0;
255 + int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
256 + unsigned mmp_check_interval;
257 + unsigned long last_update_time;
258 + unsigned long diff;
261 + mmp_block = le64_to_cpu(es->s_mmp_block);
262 + mmp = (struct mmp_struct *)(bh->b_data);
263 + mmp->mmp_time = cpu_to_le64(get_seconds());
265 + * Start with the higher mmp_check_interval and reduce it if
266 + * the MMP block is being updated on time.
268 + mmp_check_interval = max(EXT4_MMP_CHECK_MULT * mmp_update_interval,
269 + EXT4_MMP_MIN_CHECK_INTERVAL);
270 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
271 + bdevname(bh->b_bdev, mmp->mmp_bdevname);
273 + memcpy(mmp->mmp_nodename, init_utsname()->nodename,
274 + sizeof(mmp->mmp_nodename));
276 + while (!kthread_should_stop()) {
277 + if (++seq > EXT4_MMP_SEQ_MAX)
280 + mmp->mmp_seq = cpu_to_le32(seq);
281 + mmp->mmp_time = cpu_to_le64(get_seconds());
282 + last_update_time = jiffies;
284 + retval = write_mmp_block(bh);
286 + * Don't spew too many error messages. Print one every
287 + * (s_mmp_update_interval * 60) seconds.
290 + if ((failed_writes % 60) == 0)
291 + ext4_error(sb, "Error writing to MMP block");
295 + if (!(le32_to_cpu(es->s_feature_incompat) &
296 + EXT4_FEATURE_INCOMPAT_MMP)) {
297 + ext4_warning(sb, "kmmpd being stopped since MMP feature"
298 + " has been disabled.");
299 + EXT4_SB(sb)->s_mmp_tsk = NULL;
303 + if (sb->s_flags & MS_RDONLY) {
304 + ext4_warning(sb, "kmmpd being stopped since filesystem "
305 + "has been remounted as readonly.");
306 + EXT4_SB(sb)->s_mmp_tsk = NULL;
310 + diff = jiffies - last_update_time;
311 + if (diff < mmp_update_interval * msecs_to_jiffies(MSEC_PER_SEC))
312 + schedule_timeout_interruptible(mmp_update_interval *
313 + msecs_to_jiffies(MSEC_PER_SEC) - diff);
316 + * We need to make sure that more than mmp_check_interval
317 + * seconds have not passed since writing. If that has happened
318 + * we need to check if the MMP block is as we left it.
320 + diff = jiffies - last_update_time;
321 + if (diff > mmp_check_interval * msecs_to_jiffies(MSEC_PER_SEC)) {
322 + struct buffer_head *bh_check = NULL;
323 + struct mmp_struct *mmp_check;
325 + retval = read_mmp_block(sb, &bh_check, mmp_block);
327 + ext4_error(sb, "error reading MMP data: %d",
329 + EXT4_SB(sb)->s_mmp_tsk = NULL;
333 + mmp_check = (struct mmp_struct *)(bh_check->b_data);
334 + if (mmp->mmp_seq != mmp_check->mmp_seq ||
335 + memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
336 + sizeof(mmp->mmp_nodename))) {
337 + dump_mmp_msg(sb, mmp_check,
338 + "Error while updating MMP info. "
339 + "The filesystem seems to have been"
340 + " multiply mounted.");
341 + ext4_error(sb, "abort");
349 + * Adjust the mmp_check_interval depending on how much time
350 + * it took for the MMP block to be written.
352 + mmp_check_interval = max(min(EXT4_MMP_CHECK_MULT * diff /
353 + msecs_to_jiffies(MSEC_PER_SEC),
354 + EXT4_MMP_MAX_CHECK_INTERVAL),
355 + EXT4_MMP_MIN_CHECK_INTERVAL);
356 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
360 + * Unmount seems to be clean.
362 + mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
363 + mmp->mmp_time = cpu_to_le64(get_seconds());
365 + retval = write_mmp_block(bh);
374 + * Get a random new sequence number but make sure it is not greater than
375 + * EXT4_MMP_SEQ_MAX.
377 +static unsigned int mmp_new_seq(void)
382 + get_random_bytes(&new_seq, sizeof(u32));
383 + } while (new_seq > EXT4_MMP_SEQ_MAX);
389 + * Protect the filesystem from being mounted more than once.
391 +int ext4_multi_mount_protect(struct super_block *sb,
392 + ext4_fsblk_t mmp_block)
394 + struct ext4_super_block *es = EXT4_SB(sb)->s_es;
395 + struct buffer_head *bh = NULL;
396 + struct mmp_struct *mmp = NULL;
397 + struct mmpd_data *mmpd_data;
399 + unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
400 + unsigned int wait_time = 0;
403 + if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
404 + mmp_block >= ext4_blocks_count(es)) {
405 + ext4_warning(sb, "Invalid MMP block in superblock");
409 + retval = read_mmp_block(sb, &bh, mmp_block);
413 + mmp = (struct mmp_struct *)(bh->b_data);
415 + if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
416 + mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
419 + * If check_interval in MMP block is larger, use that instead of
420 + * update_interval from the superblock.
422 + if (mmp->mmp_check_interval > mmp_check_interval)
423 + mmp_check_interval = mmp->mmp_check_interval;
425 + seq = le32_to_cpu(mmp->mmp_seq);
426 + if (seq == EXT4_MMP_SEQ_CLEAN)
429 + if (seq == EXT4_MMP_SEQ_FSCK) {
430 + dump_mmp_msg(sb, mmp, "fsck is running on the filesystem");
434 + wait_time = min(mmp_check_interval * 2 + 1,
435 + mmp_check_interval + 60);
437 + /* Print MMP interval if more than 20 secs. */
438 + if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
439 + ext4_warning(sb, "MMP interval %u higher than expected, please"
440 + " wait.\n", wait_time * 2);
442 + if (schedule_timeout_interruptible(msecs_to_jiffies(MSEC_PER_SEC) *
444 + ext4_warning(sb, "MMP startup interrupted, failing mount\n");
448 + retval = read_mmp_block(sb, &bh, mmp_block);
451 + mmp = (struct mmp_struct *)(bh->b_data);
452 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
453 + dump_mmp_msg(sb, mmp,
454 + "Device is already active on another node.");
460 + * write a new random sequence number.
462 + mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
464 + retval = write_mmp_block(bh);
469 + * wait for MMP interval and check mmp_seq.
471 + if (schedule_timeout_interruptible(msecs_to_jiffies(MSEC_PER_SEC) *
473 + ext4_warning(sb, "MMP startup interrupted, failing mount\n");
477 + retval = read_mmp_block(sb, &bh, mmp_block);
480 + mmp = (struct mmp_struct *)(bh->b_data);
481 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
482 + dump_mmp_msg(sb, mmp,
483 + "Device is already active on another node.");
487 + mmpd_data = kmalloc(sizeof(struct mmpd_data), GFP_KERNEL);
489 + ext4_warning(sb, "not enough memory for mmpd_data");
492 + mmpd_data->sb = sb;
493 + mmpd_data->bh = bh;
496 + * Start a kernel thread to update the MMP block periodically.
498 + EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, mmpd_data, "kmmpd-%s",
499 + bdevname(bh->b_bdev,
500 + mmp->mmp_bdevname));
501 + if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
502 + EXT4_SB(sb)->s_mmp_tsk = NULL;
504 + ext4_warning(sb, "Unable to create kmmpd thread for %s.",
516 Index: linux-stage/fs/ext4/super.c
517 ===================================================================
518 --- linux-stage.orig/fs/ext4/super.c
519 +++ linux-stage/fs/ext4/super.c
521 #include <linux/log2.h>
522 #include <linux/crc16.h>
523 #include <asm/uaccess.h>
524 +#include <linux/kthread.h>
525 +#include <linux/utsname.h>
527 #include <linux/kthread.h>
528 #include <linux/freezer.h>
529 @@ -716,6 +718,8 @@ static void ext4_put_super(struct super_
530 invalidate_bdev(sbi->journal_bdev);
531 ext4_blkdev_remove(sbi);
533 + if (sbi->s_mmp_tsk)
534 + kthread_stop(sbi->s_mmp_tsk);
535 sb->s_fs_info = NULL;
537 * Now that we are completely done shutting down the
538 @@ -3241,6 +3245,10 @@ static int ext4_fill_super(struct super_
539 needs_recovery = (es->s_last_orphan != 0 ||
540 EXT4_HAS_INCOMPAT_FEATURE(sb,
541 EXT4_FEATURE_INCOMPAT_RECOVER));
542 + if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
543 + !(sb->s_flags & MS_RDONLY))
544 + if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
545 + goto failed_mount3;
548 * The first inode we look at is the journal inode. Don't try
549 @@ -3491,6 +3499,8 @@ failed_mount3:
550 percpu_counter_destroy(&sbi->s_freeinodes_counter);
551 percpu_counter_destroy(&sbi->s_dirs_counter);
552 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
553 + if (sbi->s_mmp_tsk)
554 + kthread_stop(sbi->s_mmp_tsk);
556 for (i = 0; i < db_count; i++)
557 brelse(sbi->s_group_desc[i]);
558 @@ -4001,7 +4011,7 @@ static int ext4_remount(struct super_blo
559 int enable_quota = 0;
561 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
567 @@ -4129,6 +4139,13 @@ static int ext4_remount(struct super_blo
569 if (!ext4_setup_super(sb, es, 0))
570 sb->s_flags &= ~MS_RDONLY;
571 + if (EXT4_HAS_INCOMPAT_FEATURE(sb,
572 + EXT4_FEATURE_INCOMPAT_MMP))
573 + if (ext4_multi_mount_protect(sb,
574 + le64_to_cpu(es->s_mmp_block))) {