mmp.c 10 KB

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