1 Index: linux-stage/fs/ext3/super.c
2 ===================================================================
3 --- linux-stage.orig/fs/ext3/super.c
4 +++ linux-stage/fs/ext3/super.c
6 #include <linux/namei.h>
7 #include <linux/quotaops.h>
8 #include <linux/seq_file.h>
9 +#include <linux/kthread.h>
10 +#include <linux/utsname.h>
12 #include <asm/uaccess.h>
14 @@ -83,7 +85,7 @@ handle_t *ext3_journal_start_sb(struct s
15 * take the FS itself readonly cleanly. */
16 journal = EXT3_SB(sb)->s_journal;
17 if (is_journal_aborted(journal)) {
18 - ext3_abort(sb, __FUNCTION__,
19 + ext3_abort(sb, __func__,
20 "Detected aborted journal");
21 return ERR_PTR(-EROFS);
23 @@ -305,7 +307,7 @@ void ext3_update_dynamic_rev(struct supe
24 if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
27 - ext3_warning(sb, __FUNCTION__,
28 + ext3_warning(sb, __func__,
29 "updating to rev %d because of new feature flag, "
30 "running e2fsck is recommended",
32 @@ -440,6 +442,8 @@ static void ext3_put_super (struct super
33 invalidate_bdev(sbi->journal_bdev, 0);
34 ext3_blkdev_remove(sbi);
37 + kthread_stop(sbi->s_mmp_tsk);
38 if (sbi->s_dev_proc) {
39 remove_proc_entry(sbi->s_dev_proc->name, proc_root_ext3);
40 sbi->s_dev_proc = NULL;
41 @@ -1375,7 +1379,7 @@ static int ext3_check_descriptors (struc
44 if (!ext3_group_desc_csum_verify(sbi, i, gdp)) {
45 - ext3_error(sb, __FUNCTION__,
46 + ext3_error(sb, __func__,
47 "Checksum for group %d failed (%u!=%u)\n", i,
48 le16_to_cpu(ext3_group_desc_csum(sbi,i,gdp)),
49 le16_to_cpu(gdp->bg_checksum));
50 @@ -1470,7 +1474,7 @@ static void ext3_orphan_cleanup (struct
53 "%s: truncating inode %lu to %Ld bytes\n",
54 - __FUNCTION__, inode->i_ino, inode->i_size);
55 + __func__, inode->i_ino, inode->i_size);
56 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
57 inode->i_ino, inode->i_size);
59 @@ -1478,7 +1482,7 @@ static void ext3_orphan_cleanup (struct
62 "%s: deleting unreferenced inode %lu\n",
63 - __FUNCTION__, inode->i_ino);
64 + __func__, inode->i_ino);
65 jbd_debug(2, "deleting unreferenced inode %lu\n",
68 @@ -1548,6 +1552,346 @@ static ext3_fsblk_t descriptor_loc(struc
69 return (has_super + ext3_group_first_block_no(sb, bg));
73 + * Write the MMP block using WRITE_SYNC to try to get the block on-disk
76 +static int write_mmp_block(struct buffer_head *bh)
78 + mark_buffer_dirty(bh);
80 + bh->b_end_io = end_buffer_write_sync;
82 + submit_bh(WRITE_SYNC, bh);
84 + if (unlikely(!buffer_uptodate(bh)))
91 + * Read the MMP block. It _must_ be read from disk and hence we clear the
92 + * uptodate flag on the buffer.
94 +static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
95 + unsigned long mmp_block)
97 + struct mmp_struct *mmp;
100 + clear_buffer_uptodate(*bh);
105 + *bh = sb_bread(sb, mmp_block);
108 + *bh = sb_getblk(sb, mmp_block);
112 + (*bh)->b_end_io = end_buffer_read_sync;
113 + submit_bh(READ_SYNC, *bh);
114 + wait_on_buffer(*bh);
115 + if (!buffer_uptodate(*bh)) {
122 + ext3_warning(sb, __func__,
123 + "Error while reading MMP block %lu", mmp_block);
127 + mmp = (struct mmp_struct *)((*bh)->b_data);
128 + if (le32_to_cpu(mmp->mmp_magic) != EXT3_MMP_MAGIC)
135 + * Dump as much information as possible to help the admin.
137 +static void dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
138 + const char *function, const char *msg)
140 + ext3_warning(sb, function, msg);
141 + ext3_warning(sb, function, "MMP failure info: last update time: %llu, "
142 + "last update node: %s, last update device: %s\n",
143 + le64_to_cpu(mmp->mmp_time), mmp->mmp_nodename,
144 + mmp->mmp_bdevname);
148 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
150 +static int kmmpd(void *data)
152 + struct super_block *sb = (struct super_block *) data;
153 + struct ext3_super_block *es = EXT3_SB(sb)->s_es;
154 + struct buffer_head *bh = NULL;
155 + struct mmp_struct *mmp;
156 + unsigned long mmp_block;
158 + unsigned long failed_writes = 0;
159 + int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
160 + unsigned mmp_check_interval;
161 + unsigned long last_update_time;
162 + unsigned long diff;
165 + mmp_block = le64_to_cpu(es->s_mmp_block);
166 + retval = read_mmp_block(sb, &bh, mmp_block);
170 + mmp = (struct mmp_struct *)(bh->b_data);
171 + mmp->mmp_time = cpu_to_le64(get_seconds());
173 + * Start with the higher mmp_check_interval and reduce it if
174 + * the MMP block is being updated on time.
176 + mmp_check_interval = max(5 * mmp_update_interval,
177 + EXT3_MMP_MIN_CHECK_INTERVAL);
178 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
179 + bdevname(bh->b_bdev, mmp->mmp_bdevname);
181 + down_read(&uts_sem);
182 + memcpy(mmp->mmp_nodename, system_utsname.nodename,
183 + sizeof(mmp->mmp_nodename));
186 + while (!kthread_should_stop()) {
187 + if (++seq > EXT3_MMP_SEQ_MAX)
190 + mmp->mmp_seq = cpu_to_le32(seq);
191 + mmp->mmp_time = cpu_to_le64(get_seconds());
192 + last_update_time = jiffies;
194 + retval = write_mmp_block(bh);
196 + * Don't spew too many error messages. Print one every
197 + * (s_mmp_update_interval * 60) seconds.
199 + if (retval && (failed_writes % 60) == 0) {
200 + ext3_error(sb, __func__,
201 + "Error writing to MMP block");
205 + if (!(le32_to_cpu(es->s_feature_incompat) &
206 + EXT3_FEATURE_INCOMPAT_MMP)) {
207 + ext3_warning(sb, __func__, "kmmpd being stopped "
208 + "since MMP feature has been disabled.");
209 + EXT3_SB(sb)->s_mmp_tsk = 0;
213 + if (sb->s_flags & MS_RDONLY) {
214 + ext3_warning(sb, __func__, "kmmpd being stopped "
215 + "since filesystem has been remounted as "
217 + EXT3_SB(sb)->s_mmp_tsk = 0;
221 + diff = jiffies - last_update_time;
222 + if (diff < mmp_update_interval * HZ)
223 + schedule_timeout_interruptible(EXT3_MMP_UPDATE_INTERVAL*
227 + * We need to make sure that more than mmp_check_interval
228 + * seconds have not passed since writing. If that has happened
229 + * we need to check if the MMP block is as we left it.
231 + diff = jiffies - last_update_time;
232 + if (diff > mmp_check_interval * HZ) {
233 + struct buffer_head *bh_check = NULL;
234 + struct mmp_struct *mmp_check;
236 + retval = read_mmp_block(sb, &bh_check, mmp_block);
238 + EXT3_SB(sb)->s_mmp_tsk = 0;
242 + mmp_check = (struct mmp_struct *)(bh_check->b_data);
243 + if (mmp->mmp_time != mmp_check->mmp_time ||
244 + memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
245 + sizeof(mmp->mmp_nodename)))
246 + dump_mmp_msg(sb, mmp_check, __func__,
247 + "Error while updating MMP info. "
248 + "The filesystem seems to have "
249 + "been multiply mounted.");
255 + * Adjust the mmp_check_interval depending on how much time
256 + * it took for the MMP block to be written.
258 + mmp_check_interval = max(5 * diff / HZ,
259 + (unsigned long) EXT3_MMP_MIN_CHECK_INTERVAL);
260 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
264 + * Unmount seems to be clean.
266 + mmp->mmp_seq = cpu_to_le32(EXT3_MMP_SEQ_CLEAN);
267 + mmp->mmp_time = cpu_to_le64(get_seconds());
269 + retval = write_mmp_block(bh);
277 + * Get a random new sequence number but make sure it is not greater than
278 + * EXT3_MMP_SEQ_MAX.
280 +static unsigned int mmp_new_seq(void)
285 + get_random_bytes(&new_seq, sizeof(u32));
286 + } while (new_seq > EXT3_MMP_SEQ_MAX);
292 + * Protect the filesystem from being mounted more than once.
294 +static int ext3_multi_mount_protect(struct super_block *sb,
295 + unsigned long mmp_block)
297 + struct ext3_super_block *es = EXT3_SB(sb)->s_es;
298 + struct buffer_head *bh = NULL;
299 + struct mmp_struct *mmp = NULL;
301 + unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
302 + unsigned int wait_time = 0;
305 + if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
306 + mmp_block >= le32_to_cpu(es->s_blocks_count)) {
307 + ext3_warning(sb, __func__,
308 + "Invalid MMP block in superblock");
312 + retval = read_mmp_block(sb, &bh, mmp_block);
316 + mmp = (struct mmp_struct *)(bh->b_data);
318 + if (mmp_check_interval < EXT3_MMP_MIN_CHECK_INTERVAL)
319 + mmp_check_interval = EXT3_MMP_MIN_CHECK_INTERVAL;
322 + * If check_interval in MMP block is larger, use that instead of
323 + * update_interval from the superblock.
325 + if (mmp->mmp_check_interval > mmp_check_interval)
326 + mmp_check_interval = mmp->mmp_check_interval;
328 + seq = le32_to_cpu(mmp->mmp_seq);
329 + if (seq == EXT3_MMP_SEQ_CLEAN)
332 + if (seq == EXT3_MMP_SEQ_FSCK) {
333 + dump_mmp_msg(sb, mmp, __func__,
334 + "fsck is running on the filesystem");
338 + wait_time = min(mmp_check_interval * 2 + 1,
339 + mmp_check_interval + 60);
341 + /* Print MMP interval if more than 20 secs. */
342 + if (wait_time > EXT3_MMP_MIN_CHECK_INTERVAL * 4)
343 + ext3_warning(sb, __func__, "MMP interval %u higher than "
344 + "expected, please wait.\n", wait_time * 2);
346 + if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
347 + ext3_warning(sb, __func__, "MMP startup interrupted, failing "
352 + retval = read_mmp_block(sb, &bh, mmp_block);
355 + mmp = (struct mmp_struct *)(bh->b_data);
356 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
357 + dump_mmp_msg(sb, mmp, __func__,
358 + "Device is already active on another node.");
364 + * write a new random sequence number.
366 + mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
368 + retval = write_mmp_block(bh);
373 + * wait for MMP interval and check mmp_seq.
375 + if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
376 + ext3_warning(sb, __func__, "MMP startup interrupted, failing "
381 + retval = read_mmp_block(sb, &bh, mmp_block);
384 + mmp = (struct mmp_struct *)(bh->b_data);
385 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
386 + dump_mmp_msg(sb, mmp, __func__,
387 + "Device is already active on another node.");
392 + * Start a kernel thread to update the MMP block periodically.
394 + EXT3_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
397 + if (IS_ERR(EXT3_SB(sb)->s_mmp_tsk)) {
398 + EXT3_SB(sb)->s_mmp_tsk = 0;
399 + ext3_warning(sb, __func__, "Unable to create kmmpd thread "
400 + "for %s.", sb->s_id);
413 static int ext3_fill_super (struct super_block *sb, void *data, int silent)
415 @@ -1880,6 +2224,11 @@ static int ext3_fill_super (struct super
416 EXT3_HAS_INCOMPAT_FEATURE(sb,
417 EXT3_FEATURE_INCOMPAT_RECOVER));
419 + if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MMP) &&
420 + !(sb->s_flags & MS_RDONLY))
421 + if (ext3_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
422 + goto failed_mount3;
425 * The first inode we look at is the journal inode. Don't try
426 * root first: it may be modified in the journal!
427 @@ -2012,6 +2361,8 @@ cantfind_ext3:
429 journal_destroy(sbi->s_journal);
431 + if (sbi->s_mmp_tsk)
432 + kthread_stop(sbi->s_mmp_tsk);
433 percpu_counter_destroy(&sbi->s_freeblocks_counter);
434 percpu_counter_destroy(&sbi->s_freeinodes_counter);
435 percpu_counter_destroy(&sbi->s_dirs_counter);
436 @@ -2388,9 +2739,9 @@ static void ext3_clear_journal_err(struc
439 errstr = ext3_decode_error(sb, j_errno, nbuf);
440 - ext3_warning(sb, __FUNCTION__, "Filesystem error recorded "
441 + ext3_warning(sb, __func__, "Filesystem error recorded "
442 "from previous mount: %s", errstr);
443 - ext3_warning(sb, __FUNCTION__, "Marking fs in need of "
444 + ext3_warning(sb, __func__, "Marking fs in need of "
445 "filesystem check.");
447 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
448 @@ -2510,7 +2861,7 @@ static int ext3_remount (struct super_bl
449 ext3_fsblk_t n_blocks_count = 0;
450 unsigned long old_sb_flags;
451 struct ext3_mount_options old_opts;
457 @@ -2536,7 +2887,7 @@ static int ext3_remount (struct super_bl
460 if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
461 - ext3_abort(sb, __FUNCTION__, "Abort forced by user");
462 + ext3_abort(sb, __func__, "Abort forced by user");
464 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
465 ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
466 @@ -2609,6 +2960,13 @@ static int ext3_remount (struct super_bl
468 if (!ext3_setup_super (sb, es, 0))
469 sb->s_flags &= ~MS_RDONLY;
470 + if (EXT3_HAS_INCOMPAT_FEATURE(sb,
471 + EXT3_FEATURE_INCOMPAT_MMP))
472 + if (ext3_multi_mount_protect(sb,
473 + le64_to_cpu(es->s_mmp_block))) {
480 Index: linux-stage/include/linux/ext3_fs.h
481 ===================================================================
482 --- linux-stage.orig/include/linux/ext3_fs.h
483 +++ linux-stage/include/linux/ext3_fs.h
484 @@ -594,13 +594,17 @@ struct ext3_super_block {
485 __le32 s_first_meta_bg; /* First metablock block group */
486 __le32 s_mkfs_time; /* When the filesystem was created */
487 __le32 s_jnl_blocks[17]; /* Backup of the journal inode */
488 - __le32 s_blocks_count_hi; /* Blocks count high 32 bits */
489 +/*150*/ __le32 s_blocks_count_hi; /* Blocks count high 32 bits */
490 __le32 s_r_blocks_count_hi; /* Reserved blocks count high 32 bits*/
491 __le32 s_free_blocks_count_hi; /* Free blocks count high 32 bits */
492 __le16 s_min_extra_isize; /* All inodes have at least # bytes */
493 __le16 s_want_extra_isize; /* New inodes should reserve # bytes */
494 - __le32 s_flags; /* Miscellaneous flags */
495 - __u32 s_reserved[167]; /* Padding to the end of the block */
496 +/*160*/ __le32 s_flags; /* Miscellaneous flags */
497 + __le16 s_raid_stride; /* RAID stride */
498 + __le16 s_mmp_update_interval; /* # seconds to wait in MMP checking */
499 + __le64 s_mmp_block; /* Block for multi-mount protection */
500 +/*170*/ __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
501 + __le32 s_reserved[163]; /* Padding to the end of the block */
505 @@ -703,12 +707,14 @@ static inline int ext3_valid_inum(struct
506 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
507 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
508 #define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
509 +#define EXT3_FEATURE_INCOMPAT_MMP 0x0100
511 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
512 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
513 EXT3_FEATURE_INCOMPAT_RECOVER| \
514 EXT3_FEATURE_INCOMPAT_META_BG| \
515 - EXT3_FEATURE_INCOMPAT_EXTENTS)
516 + EXT3_FEATURE_INCOMPAT_EXTENTS| \
517 + EXT3_FEATURE_INCOMPAT_MMP)
518 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
519 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
520 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
521 @@ -871,6 +877,39 @@ ext3_group_first_block_no(struct super_b
522 #define ERR_BAD_DX_DIR -75000
525 + * This structure will be used for multiple mount protection. It will be
526 + * written into the block number saved in the s_mmp_block field in the
527 + * superblock. Programs that check MMP should assume that if
528 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
529 + * to use the filesystem, regardless of how old the timestamp is.
531 +#define EXT3_MMP_MAGIC 0x004D4D50U /* ASCII for MMP */
532 +#define EXT3_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
533 +#define EXT3_MMP_SEQ_FSCK 0xE24D4D50U /* mmp_seq value when being fscked */
534 +#define EXT3_MMP_SEQ_MAX 0xE24D4D4FU /* maximum valid mmp_seq value */
540 + char mmp_nodename[64];
541 + char mmp_bdevname[32];
542 + __le16 mmp_check_interval;
544 + __le32 mmp_pad2[227];
548 + * Default interval in seconds to update the MMP sequence number.
550 +#define EXT3_MMP_UPDATE_INTERVAL 1
553 + * Minimum interval for MMP checking in seconds.
555 +#define EXT3_MMP_MIN_CHECK_INTERVAL 5
558 * Function prototypes
561 Index: linux-stage/include/linux/ext3_fs_sb.h
562 ===================================================================
563 --- linux-stage.orig/include/linux/ext3_fs_sb.h
564 +++ linux-stage/include/linux/ext3_fs_sb.h
565 @@ -156,6 +156,7 @@ struct ext3_sb_info {
566 /* locality groups */
567 struct ext3_locality_group *s_locality_groups;
569 + struct task_struct *s_mmp_tsk; /* Kernel thread for multiple mount protection */
572 #define EXT3_GROUP_INFO(sb, group) \