Blame view

fs/ext4/mmp.c 10 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
c5e06d101   Johann Lombardi   ext4: add support...
2
3
4
5
6
7
8
  #include <linux/fs.h>
  #include <linux/random.h>
  #include <linux/buffer_head.h>
  #include <linux/utsname.h>
  #include <linux/kthread.h>
  
  #include "ext4.h"
5c359a47e   Darrick J. Wong   ext4: add checksu...
9
  /* Checksumming functions */
171a7f21a   Dmitry Monakhov   ext4: fix big-end...
10
  static __le32 ext4_mmp_csum(struct super_block *sb, struct mmp_struct *mmp)
5c359a47e   Darrick J. Wong   ext4: add checksu...
11
12
13
14
15
16
17
18
19
  {
  	struct ext4_sb_info *sbi = EXT4_SB(sb);
  	int offset = offsetof(struct mmp_struct, mmp_checksum);
  	__u32 csum;
  
  	csum = ext4_chksum(sbi, sbi->s_csum_seed, (char *)mmp, offset);
  
  	return cpu_to_le32(csum);
  }
c197855ea   Stephen Hemminger   ext4: make local ...
20
  static int ext4_mmp_csum_verify(struct super_block *sb, struct mmp_struct *mmp)
5c359a47e   Darrick J. Wong   ext4: add checksu...
21
  {
9aa5d32ba   Dmitry Monakhov   ext4: Replace ope...
22
  	if (!ext4_has_metadata_csum(sb))
5c359a47e   Darrick J. Wong   ext4: add checksu...
23
24
25
26
  		return 1;
  
  	return mmp->mmp_checksum == ext4_mmp_csum(sb, mmp);
  }
c197855ea   Stephen Hemminger   ext4: make local ...
27
  static void ext4_mmp_csum_set(struct super_block *sb, struct mmp_struct *mmp)
5c359a47e   Darrick J. Wong   ext4: add checksu...
28
  {
9aa5d32ba   Dmitry Monakhov   ext4: Replace ope...
29
  	if (!ext4_has_metadata_csum(sb))
5c359a47e   Darrick J. Wong   ext4: add checksu...
30
31
32
33
  		return;
  
  	mmp->mmp_checksum = ext4_mmp_csum(sb, mmp);
  }
c5e06d101   Johann Lombardi   ext4: add support...
34
  /*
70fd76140   Christoph Hellwig   block,fs: use REQ...
35
   * Write the MMP block using REQ_SYNC to try to get the block on-disk
c5e06d101   Johann Lombardi   ext4: add support...
36
37
   * faster.
   */
5c359a47e   Darrick J. Wong   ext4: add checksu...
38
  static int write_mmp_block(struct super_block *sb, struct buffer_head *bh)
c5e06d101   Johann Lombardi   ext4: add support...
39
  {
5c359a47e   Darrick J. Wong   ext4: add checksu...
40
  	struct mmp_struct *mmp = (struct mmp_struct *)(bh->b_data);
8e8ad8a57   Jan Kara   ext4: Convert to ...
41
42
43
44
45
  	/*
  	 * We protect against freezing so that we don't create dirty buffers
  	 * on frozen filesystem.
  	 */
  	sb_start_write(sb);
5c359a47e   Darrick J. Wong   ext4: add checksu...
46
  	ext4_mmp_csum_set(sb, mmp);
c5e06d101   Johann Lombardi   ext4: add support...
47
48
49
  	lock_buffer(bh);
  	bh->b_end_io = end_buffer_write_sync;
  	get_bh(bh);
70fd76140   Christoph Hellwig   block,fs: use REQ...
50
  	submit_bh(REQ_OP_WRITE, REQ_SYNC | REQ_META | REQ_PRIO, bh);
c5e06d101   Johann Lombardi   ext4: add support...
51
  	wait_on_buffer(bh);
8e8ad8a57   Jan Kara   ext4: Convert to ...
52
  	sb_end_write(sb);
c5e06d101   Johann Lombardi   ext4: add support...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  	if (unlikely(!buffer_uptodate(bh)))
  		return 1;
  
  	return 0;
  }
  
  /*
   * Read the MMP block. It _must_ be read from disk and hence we clear the
   * uptodate flag on the buffer.
   */
  static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
  			  ext4_fsblk_t mmp_block)
  {
  	struct mmp_struct *mmp;
981044683   Dan Carpenter   ext4: simplify so...
67
  	int ret;
c5e06d101   Johann Lombardi   ext4: add support...
68
69
70
71
72
73
74
  
  	if (*bh)
  		clear_buffer_uptodate(*bh);
  
  	/* This would be sb_bread(sb, mmp_block), except we need to be sure
  	 * that the MD RAID device cache has been bypassed, and that the read
  	 * is not blocked in the elevator. */
981044683   Dan Carpenter   ext4: simplify so...
75
  	if (!*bh) {
c5e06d101   Johann Lombardi   ext4: add support...
76
  		*bh = sb_getblk(sb, mmp_block);
981044683   Dan Carpenter   ext4: simplify so...
77
78
79
  		if (!*bh) {
  			ret = -ENOMEM;
  			goto warn_exit;
c5e06d101   Johann Lombardi   ext4: add support...
80
81
  		}
  	}
981044683   Dan Carpenter   ext4: simplify so...
82

981044683   Dan Carpenter   ext4: simplify so...
83
  	lock_buffer(*bh);
2d069c088   zhangyi (F)   ext4: use common ...
84
85
  	ret = ext4_read_bh(*bh, REQ_META | REQ_PRIO, NULL);
  	if (ret)
981044683   Dan Carpenter   ext4: simplify so...
86
  		goto warn_exit;
2d069c088   zhangyi (F)   ext4: use common ...
87

c5e06d101   Johann Lombardi   ext4: add support...
88
  	mmp = (struct mmp_struct *)((*bh)->b_data);
030468867   vikram.jadhav07   ext4: clean up er...
89
  	if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC) {
6a797d273   Darrick J. Wong   ext4: call out CR...
90
  		ret = -EFSCORRUPTED;
030468867   vikram.jadhav07   ext4: clean up er...
91
92
93
  		goto warn_exit;
  	}
  	if (!ext4_mmp_csum_verify(sb, mmp)) {
6a797d273   Darrick J. Wong   ext4: call out CR...
94
  		ret = -EFSBADCRC;
030468867   vikram.jadhav07   ext4: clean up er...
95
96
97
  		goto warn_exit;
  	}
  	return 0;
981044683   Dan Carpenter   ext4: simplify so...
98
  warn_exit:
030468867   vikram.jadhav07   ext4: clean up er...
99
100
  	brelse(*bh);
  	*bh = NULL;
981044683   Dan Carpenter   ext4: simplify so...
101
102
103
  	ext4_warning(sb, "Error %d while reading MMP block %llu",
  		     ret, mmp_block);
  	return ret;
c5e06d101   Johann Lombardi   ext4: add support...
104
105
106
107
108
109
110
111
  }
  
  /*
   * Dump as much information as possible to help the admin.
   */
  void __dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
  		    const char *function, unsigned int line, const char *msg)
  {
da0b5e40a   Dan Carpenter   ext4: silence a f...
112
  	__ext4_warning(sb, function, line, "%s", msg);
c5e06d101   Johann Lombardi   ext4: add support...
113
  	__ext4_warning(sb, function, line,
14c9ca058   Andreas Dilger   ext4: don't assum...
114
115
116
117
  		       "MMP failure info: last update time: %llu, last update node: %.*s, last update device: %.*s",
  		       (unsigned long long)le64_to_cpu(mmp->mmp_time),
  		       (int)sizeof(mmp->mmp_nodename), mmp->mmp_nodename,
  		       (int)sizeof(mmp->mmp_bdevname), mmp->mmp_bdevname);
c5e06d101   Johann Lombardi   ext4: add support...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  }
  
  /*
   * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
   */
  static int kmmpd(void *data)
  {
  	struct super_block *sb = ((struct mmpd_data *) data)->sb;
  	struct buffer_head *bh = ((struct mmpd_data *) data)->bh;
  	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  	struct mmp_struct *mmp;
  	ext4_fsblk_t mmp_block;
  	u32 seq = 0;
  	unsigned long failed_writes = 0;
  	int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
  	unsigned mmp_check_interval;
  	unsigned long last_update_time;
  	unsigned long diff;
  	int retval;
  
  	mmp_block = le64_to_cpu(es->s_mmp_block);
  	mmp = (struct mmp_struct *)(bh->b_data);
af123b371   Arnd Bergmann   ext4: use 64-bit ...
140
  	mmp->mmp_time = cpu_to_le64(ktime_get_real_seconds());
c5e06d101   Johann Lombardi   ext4: add support...
141
142
143
144
145
146
147
  	/*
  	 * Start with the higher mmp_check_interval and reduce it if
  	 * the MMP block is being updated on time.
  	 */
  	mmp_check_interval = max(EXT4_MMP_CHECK_MULT * mmp_update_interval,
  				 EXT4_MMP_MIN_CHECK_INTERVAL);
  	mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
14c9ca058   Andreas Dilger   ext4: don't assum...
148
  	BUILD_BUG_ON(sizeof(mmp->mmp_bdevname) < BDEVNAME_SIZE);
c5e06d101   Johann Lombardi   ext4: add support...
149
  	bdevname(bh->b_bdev, mmp->mmp_bdevname);
215fc6af7   Nikitas Angelinas   ext4: MMP: kmmpd ...
150
  	memcpy(mmp->mmp_nodename, init_utsname()->nodename,
c5e06d101   Johann Lombardi   ext4: add support...
151
152
153
154
155
156
157
  	       sizeof(mmp->mmp_nodename));
  
  	while (!kthread_should_stop()) {
  		if (++seq > EXT4_MMP_SEQ_MAX)
  			seq = 1;
  
  		mmp->mmp_seq = cpu_to_le32(seq);
af123b371   Arnd Bergmann   ext4: use 64-bit ...
158
  		mmp->mmp_time = cpu_to_le64(ktime_get_real_seconds());
c5e06d101   Johann Lombardi   ext4: add support...
159
  		last_update_time = jiffies;
5c359a47e   Darrick J. Wong   ext4: add checksu...
160
  		retval = write_mmp_block(sb, bh);
c5e06d101   Johann Lombardi   ext4: add support...
161
162
163
164
  		/*
  		 * Don't spew too many error messages. Print one every
  		 * (s_mmp_update_interval * 60) seconds.
  		 */
bdfc230f3   Nikitas Angelinas   ext4: MMP: fix er...
165
  		if (retval) {
878520ac4   Theodore Ts'o   ext4: save the er...
166
  			if ((failed_writes % 60) == 0) {
54d3adbc2   Theodore Ts'o   ext4: save all er...
167
168
  				ext4_error_err(sb, -retval,
  					       "Error writing to MMP block");
878520ac4   Theodore Ts'o   ext4: save the er...
169
  			}
c5e06d101   Johann Lombardi   ext4: add support...
170
171
172
173
174
175
176
  			failed_writes++;
  		}
  
  		if (!(le32_to_cpu(es->s_feature_incompat) &
  		    EXT4_FEATURE_INCOMPAT_MMP)) {
  			ext4_warning(sb, "kmmpd being stopped since MMP feature"
  				     " has been disabled.");
030468867   vikram.jadhav07   ext4: clean up er...
177
  			goto exit_thread;
c5e06d101   Johann Lombardi   ext4: add support...
178
  		}
2dca60d98   Theodore Ts'o   ext4: clear mmp s...
179
180
  		if (sb_rdonly(sb))
  			break;
c5e06d101   Johann Lombardi   ext4: add support...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  
  		diff = jiffies - last_update_time;
  		if (diff < mmp_update_interval * HZ)
  			schedule_timeout_interruptible(mmp_update_interval *
  						       HZ - diff);
  
  		/*
  		 * We need to make sure that more than mmp_check_interval
  		 * seconds have not passed since writing. If that has happened
  		 * we need to check if the MMP block is as we left it.
  		 */
  		diff = jiffies - last_update_time;
  		if (diff > mmp_check_interval * HZ) {
  			struct buffer_head *bh_check = NULL;
  			struct mmp_struct *mmp_check;
  
  			retval = read_mmp_block(sb, &bh_check, mmp_block);
  			if (retval) {
54d3adbc2   Theodore Ts'o   ext4: save all er...
199
200
201
  				ext4_error_err(sb, -retval,
  					       "error reading MMP data: %d",
  					       retval);
030468867   vikram.jadhav07   ext4: clean up er...
202
  				goto exit_thread;
c5e06d101   Johann Lombardi   ext4: add support...
203
204
205
206
207
208
209
210
211
212
  			}
  
  			mmp_check = (struct mmp_struct *)(bh_check->b_data);
  			if (mmp->mmp_seq != mmp_check->mmp_seq ||
  			    memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
  				   sizeof(mmp->mmp_nodename))) {
  				dump_mmp_msg(sb, mmp_check,
  					     "Error while updating MMP info. "
  					     "The filesystem seems to have been"
  					     " multiply mounted.");
54d3adbc2   Theodore Ts'o   ext4: save all er...
213
  				ext4_error_err(sb, EBUSY, "abort");
030468867   vikram.jadhav07   ext4: clean up er...
214
215
216
  				put_bh(bh_check);
  				retval = -EBUSY;
  				goto exit_thread;
c5e06d101   Johann Lombardi   ext4: add support...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  			}
  			put_bh(bh_check);
  		}
  
  		 /*
  		 * Adjust the mmp_check_interval depending on how much time
  		 * it took for the MMP block to be written.
  		 */
  		mmp_check_interval = max(min(EXT4_MMP_CHECK_MULT * diff / HZ,
  					     EXT4_MMP_MAX_CHECK_INTERVAL),
  					 EXT4_MMP_MIN_CHECK_INTERVAL);
  		mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
  	}
  
  	/*
  	 * Unmount seems to be clean.
  	 */
  	mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
af123b371   Arnd Bergmann   ext4: use 64-bit ...
235
  	mmp->mmp_time = cpu_to_le64(ktime_get_real_seconds());
c5e06d101   Johann Lombardi   ext4: add support...
236

5c359a47e   Darrick J. Wong   ext4: add checksu...
237
  	retval = write_mmp_block(sb, bh);
c5e06d101   Johann Lombardi   ext4: add support...
238

030468867   vikram.jadhav07   ext4: clean up er...
239
240
  exit_thread:
  	EXT4_SB(sb)->s_mmp_tsk = NULL;
c5e06d101   Johann Lombardi   ext4: add support...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  	kfree(data);
  	brelse(bh);
  	return retval;
  }
  
  /*
   * Get a random new sequence number but make sure it is not greater than
   * EXT4_MMP_SEQ_MAX.
   */
  static unsigned int mmp_new_seq(void)
  {
  	u32 new_seq;
  
  	do {
dd1f723bf   Theodore Ts'o   ext4: use prandom...
255
  		new_seq = prandom_u32();
c5e06d101   Johann Lombardi   ext4: add support...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  	} while (new_seq > EXT4_MMP_SEQ_MAX);
  
  	return new_seq;
  }
  
  /*
   * Protect the filesystem from being mounted more than once.
   */
  int ext4_multi_mount_protect(struct super_block *sb,
  				    ext4_fsblk_t mmp_block)
  {
  	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  	struct buffer_head *bh = NULL;
  	struct mmp_struct *mmp = NULL;
  	struct mmpd_data *mmpd_data;
  	u32 seq;
  	unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
  	unsigned int wait_time = 0;
  	int retval;
  
  	if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
  	    mmp_block >= ext4_blocks_count(es)) {
  		ext4_warning(sb, "Invalid MMP block in superblock");
  		goto failed;
  	}
  
  	retval = read_mmp_block(sb, &bh, mmp_block);
  	if (retval)
  		goto failed;
  
  	mmp = (struct mmp_struct *)(bh->b_data);
  
  	if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
  		mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
  
  	/*
  	 * If check_interval in MMP block is larger, use that instead of
  	 * update_interval from the superblock.
  	 */
85d216501   Santosh Nayak   ext4: Fix endiann...
295
296
  	if (le16_to_cpu(mmp->mmp_check_interval) > mmp_check_interval)
  		mmp_check_interval = le16_to_cpu(mmp->mmp_check_interval);
c5e06d101   Johann Lombardi   ext4: add support...
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  
  	seq = le32_to_cpu(mmp->mmp_seq);
  	if (seq == EXT4_MMP_SEQ_CLEAN)
  		goto skip;
  
  	if (seq == EXT4_MMP_SEQ_FSCK) {
  		dump_mmp_msg(sb, mmp, "fsck is running on the filesystem");
  		goto failed;
  	}
  
  	wait_time = min(mmp_check_interval * 2 + 1,
  			mmp_check_interval + 60);
  
  	/* Print MMP interval if more than 20 secs. */
  	if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
  		ext4_warning(sb, "MMP interval %u higher than expected, please"
  			     " wait.
  ", wait_time * 2);
  
  	if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
  		ext4_warning(sb, "MMP startup interrupted, failing mount
  ");
  		goto failed;
  	}
  
  	retval = read_mmp_block(sb, &bh, mmp_block);
  	if (retval)
  		goto failed;
  	mmp = (struct mmp_struct *)(bh->b_data);
  	if (seq != le32_to_cpu(mmp->mmp_seq)) {
  		dump_mmp_msg(sb, mmp,
  			     "Device is already active on another node.");
  		goto failed;
  	}
  
  skip:
  	/*
  	 * write a new random sequence number.
  	 */
f6f96fdb8   Darrick J. Wong   ext4: Fix compari...
336
337
  	seq = mmp_new_seq();
  	mmp->mmp_seq = cpu_to_le32(seq);
c5e06d101   Johann Lombardi   ext4: add support...
338

5c359a47e   Darrick J. Wong   ext4: add checksu...
339
  	retval = write_mmp_block(sb, bh);
c5e06d101   Johann Lombardi   ext4: add support...
340
341
342
343
344
345
346
  	if (retval)
  		goto failed;
  
  	/*
  	 * wait for MMP interval and check mmp_seq.
  	 */
  	if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
8d2ae1cbe   Jakub Wilk   ext4: remove trai...
347
  		ext4_warning(sb, "MMP startup interrupted, failing mount");
c5e06d101   Johann Lombardi   ext4: add support...
348
349
350
351
352
353
354
355
356
357
358
359
  		goto failed;
  	}
  
  	retval = read_mmp_block(sb, &bh, mmp_block);
  	if (retval)
  		goto failed;
  	mmp = (struct mmp_struct *)(bh->b_data);
  	if (seq != le32_to_cpu(mmp->mmp_seq)) {
  		dump_mmp_msg(sb, mmp,
  			     "Device is already active on another node.");
  		goto failed;
  	}
d695a1bea   Markus Elfring   ext4: use sizeof(...
360
  	mmpd_data = kmalloc(sizeof(*mmpd_data), GFP_KERNEL);
c5e06d101   Johann Lombardi   ext4: add support...
361
362
363
364
365
366
367
368
369
370
  	if (!mmpd_data) {
  		ext4_warning(sb, "not enough memory for mmpd_data");
  		goto failed;
  	}
  	mmpd_data->sb = sb;
  	mmpd_data->bh = bh;
  
  	/*
  	 * Start a kernel thread to update the MMP block periodically.
  	 */
14c9ca058   Andreas Dilger   ext4: don't assum...
371
372
  	EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, mmpd_data, "kmmpd-%.*s",
  					     (int)sizeof(mmp->mmp_bdevname),
c5e06d101   Johann Lombardi   ext4: add support...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  					     bdevname(bh->b_bdev,
  						      mmp->mmp_bdevname));
  	if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
  		EXT4_SB(sb)->s_mmp_tsk = NULL;
  		kfree(mmpd_data);
  		ext4_warning(sb, "Unable to create kmmpd thread for %s.",
  			     sb->s_id);
  		goto failed;
  	}
  
  	return 0;
  
  failed:
  	brelse(bh);
  	return 1;
  }