2 #include <linux/random.h>
3 #include <linux/buffer_head.h>
4 #include <linux/utsname.h>
5 #include <linux/kthread.h>
10 * Write the MMP block using WRITE_SYNC to try to get the block on-disk
13 static int write_mmp_block(struct buffer_head *bh)
15 mark_buffer_dirty(bh);
17 bh->b_end_io = end_buffer_write_sync;
19 submit_bh(WRITE_SYNC, bh);
21 if (unlikely(!buffer_uptodate(bh)))
28 * Read the MMP block. It _must_ be read from disk and hence we clear the
29 * uptodate flag on the buffer.
31 static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
32 ext4_fsblk_t mmp_block)
34 struct mmp_struct *mmp;
37 clear_buffer_uptodate(*bh);
39 /* This would be sb_bread(sb, mmp_block), except we need to be sure
40 * that the MD RAID device cache has been bypassed, and that the read
41 * is not blocked in the elevator. */
43 *bh = sb_getblk(sb, mmp_block);
49 (*bh)->b_end_io = end_buffer_read_sync;
50 submit_bh(READ_SYNC, *bh);
52 if (!buffer_uptodate(*bh)) {
58 ext4_warning(sb, "Error while reading MMP block %llu",
63 mmp = (struct mmp_struct *)((*bh)->b_data);
64 if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC)
71 * Dump as much information as possible to help the admin.
73 void __dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
74 const char *function, unsigned int line, const char *msg)
76 __ext4_warning(sb, function, line, msg);
77 __ext4_warning(sb, function, line,
78 "MMP failure info: last update time: %llu, last update "
79 "node: %s, last update device: %s\n",
80 (long long unsigned int) le64_to_cpu(mmp->mmp_time),
81 mmp->mmp_nodename, mmp->mmp_bdevname);
85 * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
87 static int kmmpd(void *data)
89 struct super_block *sb = ((struct mmpd_data *) data)->sb;
90 struct buffer_head *bh = ((struct mmpd_data *) data)->bh;
91 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
92 struct mmp_struct *mmp;
93 ext4_fsblk_t mmp_block;
95 unsigned long failed_writes = 0;
96 int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
97 unsigned mmp_check_interval;
98 unsigned long last_update_time;
102 mmp_block = le64_to_cpu(es->s_mmp_block);
103 mmp = (struct mmp_struct *)(bh->b_data);
104 mmp->mmp_time = cpu_to_le64(get_seconds());
106 * Start with the higher mmp_check_interval and reduce it if
107 * the MMP block is being updated on time.
109 mmp_check_interval = max(EXT4_MMP_CHECK_MULT * mmp_update_interval,
110 EXT4_MMP_MIN_CHECK_INTERVAL);
111 mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
112 bdevname(bh->b_bdev, mmp->mmp_bdevname);
114 memcpy(mmp->mmp_nodename, init_utsname()->nodename,
115 sizeof(mmp->mmp_nodename));
117 while (!kthread_should_stop()) {
118 if (++seq > EXT4_MMP_SEQ_MAX)
121 mmp->mmp_seq = cpu_to_le32(seq);
122 mmp->mmp_time = cpu_to_le64(get_seconds());
123 last_update_time = jiffies;
125 retval = write_mmp_block(bh);
127 * Don't spew too many error messages. Print one every
128 * (s_mmp_update_interval * 60) seconds.
131 if ((failed_writes % 60) == 0)
132 ext4_error(sb, "Error writing to MMP block");
136 if (!(le32_to_cpu(es->s_feature_incompat) &
137 EXT4_FEATURE_INCOMPAT_MMP)) {
138 ext4_warning(sb, "kmmpd being stopped since MMP feature"
139 " has been disabled.");
140 EXT4_SB(sb)->s_mmp_tsk = NULL;
144 if (sb->s_flags & MS_RDONLY) {
145 ext4_warning(sb, "kmmpd being stopped since filesystem "
146 "has been remounted as readonly.");
147 EXT4_SB(sb)->s_mmp_tsk = NULL;
151 diff = jiffies - last_update_time;
152 if (diff < mmp_update_interval * HZ)
153 schedule_timeout_interruptible(mmp_update_interval *
157 * We need to make sure that more than mmp_check_interval
158 * seconds have not passed since writing. If that has happened
159 * we need to check if the MMP block is as we left it.
161 diff = jiffies - last_update_time;
162 if (diff > mmp_check_interval * HZ) {
163 struct buffer_head *bh_check = NULL;
164 struct mmp_struct *mmp_check;
166 retval = read_mmp_block(sb, &bh_check, mmp_block);
168 ext4_error(sb, "error reading MMP data: %d",
171 EXT4_SB(sb)->s_mmp_tsk = NULL;
175 mmp_check = (struct mmp_struct *)(bh_check->b_data);
176 if (mmp->mmp_seq != mmp_check->mmp_seq ||
177 memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
178 sizeof(mmp->mmp_nodename))) {
179 dump_mmp_msg(sb, mmp_check,
180 "Error while updating MMP info. "
181 "The filesystem seems to have been"
182 " multiply mounted.");
183 ext4_error(sb, "abort");
190 * Adjust the mmp_check_interval depending on how much time
191 * it took for the MMP block to be written.
193 mmp_check_interval = max(min(EXT4_MMP_CHECK_MULT * diff / HZ,
194 EXT4_MMP_MAX_CHECK_INTERVAL),
195 EXT4_MMP_MIN_CHECK_INTERVAL);
196 mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
200 * Unmount seems to be clean.
202 mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
203 mmp->mmp_time = cpu_to_le64(get_seconds());
205 retval = write_mmp_block(bh);
214 * Get a random new sequence number but make sure it is not greater than
217 static unsigned int mmp_new_seq(void)
222 get_random_bytes(&new_seq, sizeof(u32));
223 } while (new_seq > EXT4_MMP_SEQ_MAX);
229 * Protect the filesystem from being mounted more than once.
231 int ext4_multi_mount_protect(struct super_block *sb,
232 ext4_fsblk_t mmp_block)
234 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
235 struct buffer_head *bh = NULL;
236 struct mmp_struct *mmp = NULL;
237 struct mmpd_data *mmpd_data;
239 unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
240 unsigned int wait_time = 0;
243 if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
244 mmp_block >= ext4_blocks_count(es)) {
245 ext4_warning(sb, "Invalid MMP block in superblock");
249 retval = read_mmp_block(sb, &bh, mmp_block);
253 mmp = (struct mmp_struct *)(bh->b_data);
255 if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
256 mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
259 * If check_interval in MMP block is larger, use that instead of
260 * update_interval from the superblock.
262 if (mmp->mmp_check_interval > mmp_check_interval)
263 mmp_check_interval = mmp->mmp_check_interval;
265 seq = le32_to_cpu(mmp->mmp_seq);
266 if (seq == EXT4_MMP_SEQ_CLEAN)
269 if (seq == EXT4_MMP_SEQ_FSCK) {
270 dump_mmp_msg(sb, mmp, "fsck is running on the filesystem");
274 wait_time = min(mmp_check_interval * 2 + 1,
275 mmp_check_interval + 60);
277 /* Print MMP interval if more than 20 secs. */
278 if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
279 ext4_warning(sb, "MMP interval %u higher than expected, please"
280 " wait.\n", wait_time * 2);
282 if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
283 ext4_warning(sb, "MMP startup interrupted, failing mount\n");
287 retval = read_mmp_block(sb, &bh, mmp_block);
290 mmp = (struct mmp_struct *)(bh->b_data);
291 if (seq != le32_to_cpu(mmp->mmp_seq)) {
292 dump_mmp_msg(sb, mmp,
293 "Device is already active on another node.");
299 * write a new random sequence number.
302 mmp->mmp_seq = cpu_to_le32(seq);
304 retval = write_mmp_block(bh);
309 * wait for MMP interval and check mmp_seq.
311 if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
312 ext4_warning(sb, "MMP startup interrupted, failing mount\n");
316 retval = read_mmp_block(sb, &bh, mmp_block);
319 mmp = (struct mmp_struct *)(bh->b_data);
320 if (seq != le32_to_cpu(mmp->mmp_seq)) {
321 dump_mmp_msg(sb, mmp,
322 "Device is already active on another node.");
326 mmpd_data = kmalloc(sizeof(struct mmpd_data), GFP_KERNEL);
328 ext4_warning(sb, "not enough memory for mmpd_data");
335 * Start a kernel thread to update the MMP block periodically.
337 EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, mmpd_data, "kmmpd-%s",
340 if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
341 EXT4_SB(sb)->s_mmp_tsk = NULL;
343 ext4_warning(sb, "Unable to create kmmpd thread for %s.",