1 Index: linux-2.6.9/fs/ext3/super.c
2 ===================================================================
3 --- linux-2.6.9.orig/fs/ext3/super.c
4 +++ linux-2.6.9/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,9 @@ 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);
24 @@ -1431,6 +1436,314 @@ static unsigned long descriptor_loc(stru
25 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
29 + * Write the MMP block using WRITE_SYNC to try to get the block on-disk
32 +static int write_mmp_block(struct buffer_head *bh)
34 + mark_buffer_dirty(bh);
36 + bh->b_end_io = end_buffer_write_sync;
38 + submit_bh(WRITE_SYNC, bh);
40 + if (unlikely(!buffer_uptodate(bh)))
47 + * Read the MMP block. It _must_ be read from disk and hence we clear the
48 + * uptodate flag on the buffer.
50 +static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
51 + unsigned long mmp_block)
53 + struct mmp_struct *mmp;
56 + clear_buffer_uptodate(*bh);
60 + *bh = sb_bread(sb, mmp_block);
62 + ext3_warning(sb, __FUNCTION__,
63 + "Error while reading MMP block %lu", mmp_block);
67 + mmp = (struct mmp_struct *)((*bh)->b_data);
68 + if (le32_to_cpu(mmp->mmp_magic) != EXT3_MMP_MAGIC)
75 + * Dump as much information as possible to help the admin.
77 +static void dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
78 + const char *function, const char *msg)
80 + ext3_warning(sb, function, msg);
81 + ext3_warning(sb, function, "MMP failure info: last update time: %llu, "
82 + "last update node: %s, last update device: %s\n",
83 + le64_to_cpu(mmp->mmp_time), mmp->mmp_nodename,
88 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
90 +static int kmmpd(void *data)
92 + struct super_block *sb = (struct super_block *) data;
93 + struct ext3_super_block *es = EXT3_SB(sb)->s_es;
94 + struct buffer_head *bh = NULL;
95 + struct mmp_struct *mmp;
96 + unsigned long mmp_block;
98 + unsigned long failed_writes = 0;
99 + int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
100 + unsigned mmp_check_interval;
101 + unsigned long last_update_time;
102 + unsigned long diff;
105 + mmp_block = le64_to_cpu(es->s_mmp_block);
106 + retval = read_mmp_block(sb, &bh, mmp_block);
110 + mmp = (struct mmp_struct *)(bh->b_data);
111 + mmp->mmp_time = cpu_to_le64(get_seconds());
113 + * Start with the higher mmp_check_interval and reduce it if
114 + * the MMP block is being updated on time.
116 + mmp_check_interval = max(5 * mmp_update_interval,
117 + EXT3_MMP_MIN_CHECK_INTERVAL);
118 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
119 + bdevname(bh->b_bdev, mmp->mmp_bdevname);
121 + down_read(&uts_sem);
122 + memcpy(mmp->mmp_nodename, system_utsname.nodename,
123 + sizeof(mmp->mmp_nodename));
126 + while (!kthread_should_stop()) {
127 + if (++seq > EXT3_MMP_SEQ_MAX)
130 + mmp->mmp_seq = cpu_to_le32(seq);
131 + mmp->mmp_time = cpu_to_le64(get_seconds());
132 + last_update_time = jiffies;
134 + retval = write_mmp_block(bh);
136 + * Don't spew too many error messages. Print one every
137 + * (s_mmp_update_interval * 60) seconds.
139 + if (retval && (failed_writes % 60) == 0) {
140 + ext3_error(sb, __FUNCTION__,
141 + "Error writing to MMP block");
145 + if (!(le32_to_cpu(es->s_feature_incompat) &
146 + EXT3_FEATURE_INCOMPAT_MMP)) {
147 + ext3_warning(sb, __FUNCTION__, "kmmpd being stopped "
148 + "since MMP feature has been disabled.");
149 + EXT3_SB(sb)->s_mmp_tsk = 0;
153 + if (sb->s_flags & MS_RDONLY) {
154 + ext3_warning(sb, __FUNCTION__, "kmmpd being stopped "
155 + "since filesystem has been remounted as "
157 + EXT3_SB(sb)->s_mmp_tsk = 0;
161 + diff = jiffies - last_update_time;
162 + if (diff < mmp_update_interval * HZ) {
163 + set_current_state(TASK_INTERRUPTIBLE);
164 + schedule_timeout(EXT3_MMP_UPDATE_INTERVAL * HZ - diff);
168 + * We need to make sure that more than mmp_check_interval
169 + * seconds have not passed since writing. If that has happened
170 + * we need to check if the MMP block is as we left it.
172 + diff = jiffies - last_update_time;
173 + if (diff > mmp_check_interval * HZ) {
174 + struct buffer_head *bh_check = NULL;
175 + struct mmp_struct *mmp_check;
177 + retval = read_mmp_block(sb, &bh_check, mmp_block);
179 + EXT3_SB(sb)->s_mmp_tsk = 0;
183 + mmp_check = (struct mmp_struct *)(bh_check->b_data);
184 + if (mmp->mmp_time != mmp_check->mmp_time ||
185 + memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
186 + sizeof(mmp->mmp_nodename)))
187 + dump_mmp_msg(sb, mmp_check, __FUNCTION__,
188 + "Error while updating MMP info. "
189 + "The filesystem seems to have "
190 + "been multiply mounted.");
196 + * Adjust the mmp_check_interval depending on how much time
197 + * it took for the MMP block to be written.
199 + mmp_check_interval = max(5 * diff / HZ,
200 + (unsigned long) EXT3_MMP_MIN_CHECK_INTERVAL);
201 + mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
205 + * Unmount seems to be clean.
207 + mmp->mmp_seq = cpu_to_le32(EXT3_MMP_SEQ_CLEAN);
208 + mmp->mmp_time = cpu_to_le64(get_seconds());
210 + retval = write_mmp_block(bh);
218 + * Get a random new sequence number but make sure it is not greater than
219 + * EXT3_MMP_SEQ_MAX.
221 +static unsigned int mmp_new_seq(void)
226 + get_random_bytes(&new_seq, sizeof(u32));
227 + } while (new_seq > EXT3_MMP_SEQ_MAX);
233 + * Protect the filesystem from being mounted more than once.
235 +static int ext3_multi_mount_protect(struct super_block *sb,
236 + unsigned long mmp_block)
238 + struct ext3_super_block *es = EXT3_SB(sb)->s_es;
239 + struct buffer_head *bh = NULL;
240 + struct mmp_struct *mmp = NULL;
242 + unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
245 + if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
246 + mmp_block >= le32_to_cpu(es->s_blocks_count)) {
247 + ext3_warning(sb, __FUNCTION__,
248 + "Invalid MMP block in superblock");
252 + retval = read_mmp_block(sb, &bh, mmp_block);
256 + mmp = (struct mmp_struct *)(bh->b_data);
258 + if (mmp_check_interval < EXT3_MMP_MIN_CHECK_INTERVAL)
259 + mmp_check_interval = EXT3_MMP_MIN_CHECK_INTERVAL;
262 + * If check_interval in MMP block is larger, use that instead of
263 + * update_interval from the superblock.
265 + if (mmp->mmp_check_interval > mmp_check_interval)
266 + mmp_check_interval = mmp->mmp_check_interval;
268 + seq = le32_to_cpu(mmp->mmp_seq);
269 + if (seq == EXT3_MMP_SEQ_CLEAN)
272 + if (seq == EXT3_MMP_SEQ_FSCK) {
273 + dump_mmp_msg(sb, mmp, __FUNCTION__,
274 + "fsck is running on the filesystem");
278 + schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
280 + retval = read_mmp_block(sb, &bh, mmp_block);
283 + mmp = (struct mmp_struct *)(bh->b_data);
284 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
285 + dump_mmp_msg(sb, mmp, __FUNCTION__,
286 + "Device is already active on another node.");
292 + * write a new random sequence number.
294 + mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
296 + retval = write_mmp_block(bh);
301 + * wait for MMP interval and check mmp_seq.
303 + schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
305 + retval = read_mmp_block(sb, &bh, mmp_block);
308 + mmp = (struct mmp_struct *)(bh->b_data);
309 + if (seq != le32_to_cpu(mmp->mmp_seq)) {
310 + dump_mmp_msg(sb, mmp, __FUNCTION__,
311 + "Device is already active on another node.");
316 + * Start a kernel thread to update the MMP block periodically.
318 + EXT3_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
321 + if (IS_ERR(EXT3_SB(sb)->s_mmp_tsk)) {
322 + EXT3_SB(sb)->s_mmp_tsk = 0;
323 + ext3_warning(sb, __FUNCTION__, "Unable to create kmmpd thread "
324 + "for %s.", sb->s_id);
337 static int ext3_fill_super (struct super_block *sb, void *data, int silent)
339 @@ -1738,6 +2051,11 @@ static int ext3_fill_super (struct super
340 EXT3_HAS_INCOMPAT_FEATURE(sb,
341 EXT3_FEATURE_INCOMPAT_RECOVER));
343 + if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MMP) &&
344 + !(sb->s_flags & MS_RDONLY))
345 + if (ext3_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
346 + goto failed_mount3;
349 * The first inode we look at is the journal inode. Don't try
350 * root first: it may be modified in the journal!
351 @@ -2371,6 +2689,11 @@ int ext3_remount (struct super_block * s
353 if (!ext3_setup_super (sb, es, 0))
354 sb->s_flags &= ~MS_RDONLY;
355 + if (EXT3_HAS_INCOMPAT_FEATURE(sb,
356 + EXT3_FEATURE_INCOMPAT_MMP))
357 + if (ext3_multi_mount_protect(sb,
358 + le64_to_cpu(es->s_mmp_block)))
363 Index: linux-2.6.9/include/linux/ext3_fs.h
364 ===================================================================
365 --- linux-2.6.9.orig/include/linux/ext3_fs.h
366 +++ linux-2.6.9/include/linux/ext3_fs.h
367 @@ -577,13 +577,17 @@ struct ext3_super_block {
368 __le32 s_first_meta_bg; /* First metablock block group */
369 __le32 s_mkfs_time; /* When the filesystem was created */
370 __le32 s_jnl_blocks[17]; /* Backup of the journal inode */
371 - __le32 s_blocks_count_hi; /* Blocks count high 32 bits */
372 +/*150*/ __le32 s_blocks_count_hi; /* Blocks count high 32 bits */
373 __le32 s_r_blocks_count_hi; /* Reserved blocks count high 32 bits*/
374 __le32 s_free_blocks_hi; /* Free blocks count high 32 bits */
375 __le16 s_min_extra_isize; /* All inodes have at least # bytes */
376 __le16 s_want_extra_isize; /* New inodes should reserve # bytes */
377 - __le32 s_flags; /* Miscellaneous flags */
378 - __u32 s_reserved[167]; /* Padding to the end of the block */
379 +/*160*/ __le32 s_flags; /* Miscellaneous flags */
380 + __le16 s_raid_stride; /* RAID stride */
381 + __le16 s_mmp_update_interval; /* # seconds to wait in MMP checking */
382 + __le64 s_mmp_block; /* Block for multi-mount protection */
383 +/*170*/ __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
384 + __le32 s_reserved[163]; /* Padding to the end of the block */
388 @@ -667,12 +671,14 @@ static inline struct ext3_inode_info *EX
389 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
390 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
391 #define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
392 +#define EXT3_FEATURE_INCOMPAT_MMP 0x0100
394 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
395 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
396 EXT3_FEATURE_INCOMPAT_RECOVER| \
397 EXT3_FEATURE_INCOMPAT_META_BG| \
398 - EXT3_FEATURE_INCOMPAT_EXTENTS)
399 + EXT3_FEATURE_INCOMPAT_EXTENTS| \
400 + EXT3_FEATURE_INCOMPAT_MMP)
401 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
402 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
403 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
404 @@ -832,6 +838,39 @@ struct dir_private_info {
405 #define ERR_BAD_DX_DIR -75000
408 + * This structure will be used for multiple mount protection. It will be
409 + * written into the block number saved in the s_mmp_block field in the
410 + * superblock. Programs that check MMP should assume that if
411 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
412 + * to use the filesystem, regardless of how old the timestamp is.
414 +#define EXT3_MMP_MAGIC 0x004D4D50U /* ASCII for MMP */
415 +#define EXT3_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
416 +#define EXT3_MMP_SEQ_FSCK 0xE24D4D50U /* mmp_seq value when being fscked */
417 +#define EXT3_MMP_SEQ_MAX 0xE24D4D4FU /* maximum valid mmp_seq value */
423 + char mmp_nodename[64];
424 + char mmp_bdevname[32];
425 + __le16 mmp_check_interval;
427 + __le32 mmp_pad2[227];
431 + * Default interval in seconds to update the MMP sequence number.
433 +#define EXT3_MMP_UPDATE_INTERVAL 1
436 + * Minimum interval for MMP checking in seconds.
438 +#define EXT3_MMP_MIN_CHECK_INTERVAL 5
441 * Function prototypes
444 Index: linux-2.6.9/include/linux/ext3_fs_sb.h
445 ===================================================================
446 --- linux-2.6.9.orig/include/linux/ext3_fs_sb.h
447 +++ linux-2.6.9/include/linux/ext3_fs_sb.h
448 @@ -143,6 +143,7 @@ struct ext3_sb_info {
449 /* locality groups */
450 struct ext3_locality_group *s_locality_groups;
452 + struct task_struct *s_mmp_tsk; /* Kernel thread for multiple mount protection */
455 #define EXT3_GROUP_INFO(sb, group) \