1 Index: linux-2.6.9-67.0.22/fs/ext3/super.c
2 ===================================================================
3 --- linux-2.6.9-67.0.22.orig/fs/ext3/super.c
4 +++ linux-2.6.9-67.0.22/fs/ext3/super.c
6 #include <linux/mount.h>
7 #include <linux/namei.h>
8 #include <linux/quotaops.h>
9 +#include <linux/kthread.h>
10 +#include <linux/utsname.h>
11 #include <asm/uaccess.h>
14 @@ -440,6 +442,8 @@ void ext3_put_super (struct super_block
15 invalidate_bdev(sbi->journal_bdev, 0);
16 ext3_blkdev_remove(sbi);
19 + kthread_stop(sbi->s_mmp_tsk);
20 if (sbi->s_dev_proc) {
21 remove_proc_entry(sbi->s_dev_proc->name, proc_root_ext3);
22 sbi->s_dev_proc = NULL;
23 @@ -1441,6 +1445,330 @@ static unsigned long descriptor_loc(stru
24 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
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 + ext3_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) != EXT3_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 + ext3_warning(sb, function, msg);
96 + ext3_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 ext3_super_block *es = EXT3_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 + EXT3_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 > EXT3_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 + ext3_error(sb, __FUNCTION__,
156 + "Error writing to MMP block");
160 + if (!(le32_to_cpu(es->s_feature_incompat) &
161 + EXT3_FEATURE_INCOMPAT_MMP)) {
162 + ext3_warning(sb, __FUNCTION__, "kmmpd being stopped "
163 + "since MMP feature has been disabled.");
164 + EXT3_SB(sb)->s_mmp_tsk = 0;
168 + if (sb->s_flags & MS_RDONLY) {
169 + ext3_warning(sb, __FUNCTION__, "kmmpd being stopped "
170 + "since filesystem has been remounted as "
172 + EXT3_SB(sb)->s_mmp_tsk = 0;
176 + diff = jiffies - last_update_time;
177 + if (diff < mmp_update_interval * HZ) {
178 + set_current_state(TASK_INTERRUPTIBLE);
179 + schedule_timeout(EXT3_MMP_UPDATE_INTERVAL * HZ - diff);
183 + * We need to make sure that more than mmp_check_interval
184 + * seconds have not passed since writing. If that has happened
185 + * we need to check if the MMP block is as we left it.
187 + diff = jiffies - last_update_time;
188 + if (diff > mmp_check_interval * HZ) {
189 + struct buffer_head *bh_check = NULL;
190 + struct mmp_struct *mmp_check;
192 + retval = read_mmp_block(sb, &bh_check, mmp_block);
194 + EXT3_SB(sb)->s_mmp_tsk = 0;
198 + mmp_check = (struct mmp_struct *)(bh_check->b_data);
199 + if (mmp->mmp_time != mmp_check->mmp_time ||
200 + memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
201 + sizeof(mmp->mmp_nodename)))
202 + dump_mmp_msg(sb, mmp_check, __FUNCTION__,
203 + "Error while updating MMP info. "
204 + "The filesystem seems to have "
205 + "been multiply mounted.");
211 + * Adjust the mmp_check_interval depending on how much time
212 + * it took for the MMP block to be written.
214 + mmp_check_interval = max(5 * diff / HZ,
215 + (unsigned long) EXT3_MMP_MIN_CHECK_INTERVAL);
216 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
220 + * Unmount seems to be clean.
222 + mmp->mmp_seq = cpu_to_le32(EXT3_MMP_SEQ_CLEAN);
223 + mmp->mmp_time = cpu_to_le64(get_seconds());
225 + retval = write_mmp_block(bh);
233 + * Get a random new sequence number but make sure it is not greater than
234 + * EXT3_MMP_SEQ_MAX.
236 +static unsigned int mmp_new_seq(void)
241 + get_random_bytes(&new_seq, sizeof(u32));
242 + } while (new_seq > EXT3_MMP_SEQ_MAX);
248 + * Protect the filesystem from being mounted more than once.
250 +static int ext3_multi_mount_protect(struct super_block *sb,
251 + unsigned long mmp_block)
253 + struct ext3_super_block *es = EXT3_SB(sb)->s_es;
254 + struct buffer_head *bh = NULL;
255 + struct mmp_struct *mmp = NULL;
257 + unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
260 + if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
261 + mmp_block >= le32_to_cpu(es->s_blocks_count)) {
262 + ext3_warning(sb, __FUNCTION__,
263 + "Invalid MMP block in superblock");
267 + retval = read_mmp_block(sb, &bh, mmp_block);
271 + mmp = (struct mmp_struct *)(bh->b_data);
273 + if (mmp_check_interval < EXT3_MMP_MIN_CHECK_INTERVAL)
274 + mmp_check_interval = EXT3_MMP_MIN_CHECK_INTERVAL;
277 + * If check_interval in MMP block is larger, use that instead of
278 + * update_interval from the superblock.
280 + if (mmp->mmp_check_interval > mmp_check_interval)
281 + mmp_check_interval = mmp->mmp_check_interval;
283 + seq = le32_to_cpu(mmp->mmp_seq);
284 + if (seq == EXT3_MMP_SEQ_CLEAN)
287 + if (seq == EXT3_MMP_SEQ_FSCK) {
288 + dump_mmp_msg(sb, mmp, __FUNCTION__,
289 + "fsck is running on the filesystem");
293 + schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
295 + retval = read_mmp_block(sb, &bh, mmp_block);
298 + mmp = (struct mmp_struct *)(bh->b_data);
299 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
300 + dump_mmp_msg(sb, mmp, __FUNCTION__,
301 + "Device is already active on another node.");
307 + * write a new random sequence number.
309 + mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
311 + retval = write_mmp_block(bh);
316 + * wait for MMP interval and check mmp_seq.
318 + schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
320 + retval = read_mmp_block(sb, &bh, mmp_block);
323 + mmp = (struct mmp_struct *)(bh->b_data);
324 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
325 + dump_mmp_msg(sb, mmp, __FUNCTION__,
326 + "Device is already active on another node.");
331 + * Start a kernel thread to update the MMP block periodically.
333 + EXT3_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
336 + if (IS_ERR(EXT3_SB(sb)->s_mmp_tsk)) {
337 + EXT3_SB(sb)->s_mmp_tsk = 0;
338 + ext3_warning(sb, __FUNCTION__, "Unable to create kmmpd thread "
339 + "for %s.", sb->s_id);
352 static int ext3_fill_super (struct super_block *sb, void *data, int silent)
354 @@ -1765,6 +2093,11 @@ static int ext3_fill_super (struct super
355 EXT3_HAS_INCOMPAT_FEATURE(sb,
356 EXT3_FEATURE_INCOMPAT_RECOVER));
358 + if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MMP) &&
359 + !(sb->s_flags & MS_RDONLY))
360 + if (ext3_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
361 + goto failed_mount2;
364 * The first inode we look at is the journal inode. Don't try
365 * root first: it may be modified in the journal!
366 @@ -1890,6 +2223,8 @@ static int ext3_fill_super (struct super
368 journal_destroy(sbi->s_journal);
370 + if (sbi->s_mmp_tsk)
371 + kthread_stop(sbi->s_mmp_tsk);
372 for (i = 0; i < db_count; i++)
373 brelse(sbi->s_group_desc[i]);
374 kfree(sbi->s_group_desc);
375 @@ -2415,6 +2750,11 @@ int ext3_remount (struct super_block * s
377 if (!ext3_setup_super (sb, es, 0))
378 sb->s_flags &= ~MS_RDONLY;
379 + if (EXT3_HAS_INCOMPAT_FEATURE(sb,
380 + EXT3_FEATURE_INCOMPAT_MMP))
381 + if (ext3_multi_mount_protect(sb,
382 + le64_to_cpu(es->s_mmp_block)))
387 Index: linux-2.6.9-67.0.22/include/linux/ext3_fs.h
388 ===================================================================
389 --- linux-2.6.9-67.0.22.orig/include/linux/ext3_fs.h
390 +++ linux-2.6.9-67.0.22/include/linux/ext3_fs.h
391 @@ -575,13 +575,17 @@ struct ext3_super_block {
392 __le32 s_first_meta_bg; /* First metablock block group */
393 __le32 s_mkfs_time; /* When the filesystem was created */
394 __le32 s_jnl_blocks[17]; /* Backup of the journal inode */
395 - __le32 s_blocks_count_hi; /* Blocks count high 32 bits */
396 +/*150*/ __le32 s_blocks_count_hi; /* Blocks count high 32 bits */
397 __le32 s_r_blocks_count_hi; /* Reserved blocks count high 32 bits*/
398 __le32 s_free_blocks_hi; /* Free blocks count high 32 bits */
399 __le16 s_min_extra_isize; /* All inodes have at least # bytes */
400 __le16 s_want_extra_isize; /* New inodes should reserve # bytes */
401 - __le32 s_flags; /* Miscellaneous flags */
402 - __u32 s_reserved[167]; /* Padding to the end of the block */
403 +/*160*/ __le32 s_flags; /* Miscellaneous flags */
404 + __le16 s_raid_stride; /* RAID stride */
405 + __le16 s_mmp_update_interval; /* # seconds to wait in MMP checking */
406 + __le64 s_mmp_block; /* Block for multi-mount protection */
407 +/*170*/ __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
408 + __le32 s_reserved[163]; /* Padding to the end of the block */
412 @@ -665,12 +669,14 @@ static inline struct ext3_inode_info *EX
413 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
414 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
415 #define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
416 +#define EXT3_FEATURE_INCOMPAT_MMP 0x0100
418 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
419 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
420 EXT3_FEATURE_INCOMPAT_RECOVER| \
421 EXT3_FEATURE_INCOMPAT_META_BG| \
422 - EXT3_FEATURE_INCOMPAT_EXTENTS)
423 + EXT3_FEATURE_INCOMPAT_EXTENTS| \
424 + EXT3_FEATURE_INCOMPAT_MMP)
425 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
426 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
427 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
428 @@ -830,6 +836,39 @@ struct dir_private_info {
429 #define ERR_BAD_DX_DIR -75000
432 + * This structure will be used for multiple mount protection. It will be
433 + * written into the block number saved in the s_mmp_block field in the
434 + * superblock. Programs that check MMP should assume that if
435 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
436 + * to use the filesystem, regardless of how old the timestamp is.
438 +#define EXT3_MMP_MAGIC 0x004D4D50U /* ASCII for MMP */
439 +#define EXT3_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
440 +#define EXT3_MMP_SEQ_FSCK 0xE24D4D50U /* mmp_seq value when being fscked */
441 +#define EXT3_MMP_SEQ_MAX 0xE24D4D4FU /* maximum valid mmp_seq value */
447 + char mmp_nodename[64];
448 + char mmp_bdevname[32];
449 + __le16 mmp_check_interval;
451 + __le32 mmp_pad2[227];
455 + * Default interval in seconds to update the MMP sequence number.
457 +#define EXT3_MMP_UPDATE_INTERVAL 1
460 + * Minimum interval for MMP checking in seconds.
462 +#define EXT3_MMP_MIN_CHECK_INTERVAL 5
465 * Function prototypes
468 Index: linux-2.6.9-67.0.22/include/linux/ext3_fs_sb.h
469 ===================================================================
470 --- linux-2.6.9-67.0.22.orig/include/linux/ext3_fs_sb.h
471 +++ linux-2.6.9-67.0.22/include/linux/ext3_fs_sb.h
472 @@ -150,6 +150,7 @@ struct ext3_sb_info {
473 /* locality groups */
474 struct ext3_locality_group *s_locality_groups;
476 + struct task_struct *s_mmp_tsk; /* Kernel thread for multiple mount protection */
479 #define EXT3_GROUP_INFO(sb, group) \