^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /* Checksumming functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static __le32 ext4_mmp_csum(struct super_block *sb, struct mmp_struct *mmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int offset = offsetof(struct mmp_struct, mmp_checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) __u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) csum = ext4_chksum(sbi, sbi->s_csum_seed, (char *)mmp, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return cpu_to_le32(csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int ext4_mmp_csum_verify(struct super_block *sb, struct mmp_struct *mmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!ext4_has_metadata_csum(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return mmp->mmp_checksum == ext4_mmp_csum(sb, mmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static void ext4_mmp_csum_set(struct super_block *sb, struct mmp_struct *mmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!ext4_has_metadata_csum(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) mmp->mmp_checksum = ext4_mmp_csum(sb, mmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * Write the MMP block using REQ_SYNC to try to get the block on-disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * faster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int write_mmp_block(struct super_block *sb, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct mmp_struct *mmp = (struct mmp_struct *)(bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * We protect against freezing so that we don't create dirty buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * on frozen filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) sb_start_write(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ext4_mmp_csum_set(sb, mmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) bh->b_end_io = end_buffer_write_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) get_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) submit_bh(REQ_OP_WRITE, REQ_SYNC | REQ_META | REQ_PRIO, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) wait_on_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) sb_end_write(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (unlikely(!buffer_uptodate(bh)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Read the MMP block. It _must_ be read from disk and hence we clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * uptodate flag on the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ext4_fsblk_t mmp_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct mmp_struct *mmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (*bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) clear_buffer_uptodate(*bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* This would be sb_bread(sb, mmp_block), except we need to be sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * that the MD RAID device cache has been bypassed, and that the read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * is not blocked in the elevator. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!*bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *bh = sb_getblk(sb, mmp_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!*bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto warn_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) lock_buffer(*bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ret = ext4_read_bh(*bh, REQ_META | REQ_PRIO, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) goto warn_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) mmp = (struct mmp_struct *)((*bh)->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) goto warn_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!ext4_mmp_csum_verify(sb, mmp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ret = -EFSBADCRC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) goto warn_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) warn_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) brelse(*bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ext4_warning(sb, "Error %d while reading MMP block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret, mmp_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * Dump as much information as possible to help the admin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) void __dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) const char *function, unsigned int line, const char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) __ext4_warning(sb, function, line, "%s", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) __ext4_warning(sb, function, line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "MMP failure info: last update time: %llu, last update node: %.*s, last update device: %.*s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) (unsigned long long)le64_to_cpu(mmp->mmp_time),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) (int)sizeof(mmp->mmp_nodename), mmp->mmp_nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) (int)sizeof(mmp->mmp_bdevname), mmp->mmp_bdevname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int kmmpd(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct super_block *sb = (struct super_block *) data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct ext4_super_block *es = EXT4_SB(sb)->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct buffer_head *bh = EXT4_SB(sb)->s_mmp_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct mmp_struct *mmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ext4_fsblk_t mmp_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) u32 seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) unsigned long failed_writes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) unsigned mmp_check_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unsigned long last_update_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned long diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) mmp_block = le64_to_cpu(es->s_mmp_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) mmp = (struct mmp_struct *)(bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) mmp->mmp_time = cpu_to_le64(ktime_get_real_seconds());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * Start with the higher mmp_check_interval and reduce it if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * the MMP block is being updated on time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) mmp_check_interval = max(EXT4_MMP_CHECK_MULT * mmp_update_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) EXT4_MMP_MIN_CHECK_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) BUILD_BUG_ON(sizeof(mmp->mmp_bdevname) < BDEVNAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) bdevname(bh->b_bdev, mmp->mmp_bdevname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) memcpy(mmp->mmp_nodename, init_utsname()->nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) sizeof(mmp->mmp_nodename));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) while (!kthread_should_stop() && !sb_rdonly(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!ext4_has_feature_mmp(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ext4_warning(sb, "kmmpd being stopped since MMP feature"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) " has been disabled.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) goto wait_to_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (++seq > EXT4_MMP_SEQ_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) seq = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) mmp->mmp_seq = cpu_to_le32(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) mmp->mmp_time = cpu_to_le64(ktime_get_real_seconds());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) last_update_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) retval = write_mmp_block(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * Don't spew too many error messages. Print one every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * (s_mmp_update_interval * 60) seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if ((failed_writes % 60) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ext4_error_err(sb, -retval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) "Error writing to MMP block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) failed_writes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) diff = jiffies - last_update_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (diff < mmp_update_interval * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) schedule_timeout_interruptible(mmp_update_interval *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) HZ - diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * We need to make sure that more than mmp_check_interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * seconds have not passed since writing. If that has happened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * we need to check if the MMP block is as we left it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) diff = jiffies - last_update_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (diff > mmp_check_interval * HZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct buffer_head *bh_check = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct mmp_struct *mmp_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) retval = read_mmp_block(sb, &bh_check, mmp_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ext4_error_err(sb, -retval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) "error reading MMP data: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) goto wait_to_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) mmp_check = (struct mmp_struct *)(bh_check->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (mmp->mmp_seq != mmp_check->mmp_seq ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) sizeof(mmp->mmp_nodename))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) dump_mmp_msg(sb, mmp_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) "Error while updating MMP info. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) "The filesystem seems to have been"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) " multiply mounted.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ext4_error_err(sb, EBUSY, "abort");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) put_bh(bh_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) retval = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) goto wait_to_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) put_bh(bh_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * Adjust the mmp_check_interval depending on how much time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * it took for the MMP block to be written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) mmp_check_interval = max(min(EXT4_MMP_CHECK_MULT * diff / HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) EXT4_MMP_MAX_CHECK_INTERVAL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) EXT4_MMP_MIN_CHECK_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * Unmount seems to be clean.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) mmp->mmp_time = cpu_to_le64(ktime_get_real_seconds());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) retval = write_mmp_block(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) wait_to_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!kthread_should_stop())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) void ext4_stop_mmpd(struct ext4_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (sbi->s_mmp_tsk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) kthread_stop(sbi->s_mmp_tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) brelse(sbi->s_mmp_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) sbi->s_mmp_tsk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * Get a random new sequence number but make sure it is not greater than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * EXT4_MMP_SEQ_MAX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static unsigned int mmp_new_seq(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) u32 new_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) new_seq = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) } while (new_seq > EXT4_MMP_SEQ_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return new_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * Protect the filesystem from being mounted more than once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int ext4_multi_mount_protect(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) ext4_fsblk_t mmp_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct ext4_super_block *es = EXT4_SB(sb)->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct mmp_struct *mmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) u32 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) unsigned int wait_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) mmp_block >= ext4_blocks_count(es)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ext4_warning(sb, "Invalid MMP block in superblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) retval = read_mmp_block(sb, &bh, mmp_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) mmp = (struct mmp_struct *)(bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * If check_interval in MMP block is larger, use that instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * update_interval from the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (le16_to_cpu(mmp->mmp_check_interval) > mmp_check_interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) mmp_check_interval = le16_to_cpu(mmp->mmp_check_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) seq = le32_to_cpu(mmp->mmp_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (seq == EXT4_MMP_SEQ_CLEAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (seq == EXT4_MMP_SEQ_FSCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) dump_mmp_msg(sb, mmp, "fsck is running on the filesystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) wait_time = min(mmp_check_interval * 2 + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) mmp_check_interval + 60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /* Print MMP interval if more than 20 secs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) ext4_warning(sb, "MMP interval %u higher than expected, please"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) " wait.\n", wait_time * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) ext4_warning(sb, "MMP startup interrupted, failing mount\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) retval = read_mmp_block(sb, &bh, mmp_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) mmp = (struct mmp_struct *)(bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (seq != le32_to_cpu(mmp->mmp_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) dump_mmp_msg(sb, mmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) "Device is already active on another node.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * write a new random sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) seq = mmp_new_seq();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) mmp->mmp_seq = cpu_to_le32(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) retval = write_mmp_block(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * wait for MMP interval and check mmp_seq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ext4_warning(sb, "MMP startup interrupted, failing mount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) retval = read_mmp_block(sb, &bh, mmp_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) mmp = (struct mmp_struct *)(bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (seq != le32_to_cpu(mmp->mmp_seq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) dump_mmp_msg(sb, mmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) "Device is already active on another node.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) EXT4_SB(sb)->s_mmp_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * Start a kernel thread to update the MMP block periodically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%.*s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) (int)sizeof(mmp->mmp_bdevname),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) bdevname(bh->b_bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) mmp->mmp_bdevname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) EXT4_SB(sb)->s_mmp_tsk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ext4_warning(sb, "Unable to create kmmpd thread for %s.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }