mmp.c 10 KB

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