mmp.c 10 KB

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