mmp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. mark_buffer_dirty(bh);
  43. lock_buffer(bh);
  44. bh->b_end_io = end_buffer_write_sync;
  45. get_bh(bh);
  46. submit_bh(REQ_OP_WRITE, REQ_SYNC | REQ_META | REQ_PRIO, bh);
  47. wait_on_buffer(bh);
  48. sb_end_write(sb);
  49. if (unlikely(!buffer_uptodate(bh)))
  50. return 1;
  51. return 0;
  52. }
  53. /*
  54. * Read the MMP block. It _must_ be read from disk and hence we clear the
  55. * uptodate flag on the buffer.
  56. */
  57. static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
  58. ext4_fsblk_t mmp_block)
  59. {
  60. struct mmp_struct *mmp;
  61. int ret;
  62. if (*bh)
  63. clear_buffer_uptodate(*bh);
  64. /* This would be sb_bread(sb, mmp_block), except we need to be sure
  65. * that the MD RAID device cache has been bypassed, and that the read
  66. * is not blocked in the elevator. */
  67. if (!*bh) {
  68. *bh = sb_getblk(sb, mmp_block);
  69. if (!*bh) {
  70. ret = -ENOMEM;
  71. goto warn_exit;
  72. }
  73. }
  74. get_bh(*bh);
  75. lock_buffer(*bh);
  76. (*bh)->b_end_io = end_buffer_read_sync;
  77. submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, *bh);
  78. wait_on_buffer(*bh);
  79. if (!buffer_uptodate(*bh)) {
  80. ret = -EIO;
  81. goto warn_exit;
  82. }
  83. mmp = (struct mmp_struct *)((*bh)->b_data);
  84. if (le32_to_cpu(mmp->mmp_magic) != EXT4_MMP_MAGIC) {
  85. ret = -EFSCORRUPTED;
  86. goto warn_exit;
  87. }
  88. if (!ext4_mmp_csum_verify(sb, mmp)) {
  89. ret = -EFSBADCRC;
  90. goto warn_exit;
  91. }
  92. return 0;
  93. warn_exit:
  94. brelse(*bh);
  95. *bh = NULL;
  96. ext4_warning(sb, "Error %d while reading MMP block %llu",
  97. ret, mmp_block);
  98. return ret;
  99. }
  100. /*
  101. * Dump as much information as possible to help the admin.
  102. */
  103. void __dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
  104. const char *function, unsigned int line, const char *msg)
  105. {
  106. __ext4_warning(sb, function, line, "%s", msg);
  107. __ext4_warning(sb, function, line,
  108. "MMP failure info: last update time: %llu, last update "
  109. "node: %s, last update device: %s",
  110. (long long unsigned int) le64_to_cpu(mmp->mmp_time),
  111. mmp->mmp_nodename, mmp->mmp_bdevname);
  112. }
  113. /*
  114. * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
  115. */
  116. static int kmmpd(void *data)
  117. {
  118. struct super_block *sb = ((struct mmpd_data *) data)->sb;
  119. struct buffer_head *bh = ((struct mmpd_data *) data)->bh;
  120. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  121. struct mmp_struct *mmp;
  122. ext4_fsblk_t mmp_block;
  123. u32 seq = 0;
  124. unsigned long failed_writes = 0;
  125. int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
  126. unsigned mmp_check_interval;
  127. unsigned long last_update_time;
  128. unsigned long diff;
  129. int retval;
  130. mmp_block = le64_to_cpu(es->s_mmp_block);
  131. mmp = (struct mmp_struct *)(bh->b_data);
  132. mmp->mmp_time = cpu_to_le64(get_seconds());
  133. /*
  134. * Start with the higher mmp_check_interval and reduce it if
  135. * the MMP block is being updated on time.
  136. */
  137. mmp_check_interval = max(EXT4_MMP_CHECK_MULT * mmp_update_interval,
  138. EXT4_MMP_MIN_CHECK_INTERVAL);
  139. mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
  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(get_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. ext4_warning(sb, "kmmpd being stopped since filesystem "
  167. "has been remounted as readonly.");
  168. goto exit_thread;
  169. }
  170. diff = jiffies - last_update_time;
  171. if (diff < mmp_update_interval * HZ)
  172. schedule_timeout_interruptible(mmp_update_interval *
  173. HZ - diff);
  174. /*
  175. * We need to make sure that more than mmp_check_interval
  176. * seconds have not passed since writing. If that has happened
  177. * we need to check if the MMP block is as we left it.
  178. */
  179. diff = jiffies - last_update_time;
  180. if (diff > mmp_check_interval * HZ) {
  181. struct buffer_head *bh_check = NULL;
  182. struct mmp_struct *mmp_check;
  183. retval = read_mmp_block(sb, &bh_check, mmp_block);
  184. if (retval) {
  185. ext4_error(sb, "error reading MMP data: %d",
  186. retval);
  187. goto exit_thread;
  188. }
  189. mmp_check = (struct mmp_struct *)(bh_check->b_data);
  190. if (mmp->mmp_seq != mmp_check->mmp_seq ||
  191. memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
  192. sizeof(mmp->mmp_nodename))) {
  193. dump_mmp_msg(sb, mmp_check,
  194. "Error while updating MMP info. "
  195. "The filesystem seems to have been"
  196. " multiply mounted.");
  197. ext4_error(sb, "abort");
  198. put_bh(bh_check);
  199. retval = -EBUSY;
  200. goto exit_thread;
  201. }
  202. put_bh(bh_check);
  203. }
  204. /*
  205. * Adjust the mmp_check_interval depending on how much time
  206. * it took for the MMP block to be written.
  207. */
  208. mmp_check_interval = max(min(EXT4_MMP_CHECK_MULT * diff / HZ,
  209. EXT4_MMP_MAX_CHECK_INTERVAL),
  210. EXT4_MMP_MIN_CHECK_INTERVAL);
  211. mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
  212. }
  213. /*
  214. * Unmount seems to be clean.
  215. */
  216. mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
  217. mmp->mmp_time = cpu_to_le64(get_seconds());
  218. retval = write_mmp_block(sb, bh);
  219. exit_thread:
  220. EXT4_SB(sb)->s_mmp_tsk = NULL;
  221. kfree(data);
  222. brelse(bh);
  223. return retval;
  224. }
  225. /*
  226. * Get a random new sequence number but make sure it is not greater than
  227. * EXT4_MMP_SEQ_MAX.
  228. */
  229. static unsigned int mmp_new_seq(void)
  230. {
  231. u32 new_seq;
  232. do {
  233. new_seq = prandom_u32();
  234. } while (new_seq > EXT4_MMP_SEQ_MAX);
  235. return new_seq;
  236. }
  237. /*
  238. * Protect the filesystem from being mounted more than once.
  239. */
  240. int ext4_multi_mount_protect(struct super_block *sb,
  241. ext4_fsblk_t mmp_block)
  242. {
  243. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  244. struct buffer_head *bh = NULL;
  245. struct mmp_struct *mmp = NULL;
  246. struct mmpd_data *mmpd_data;
  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. goto failed;
  255. }
  256. retval = read_mmp_block(sb, &bh, mmp_block);
  257. if (retval)
  258. goto failed;
  259. mmp = (struct mmp_struct *)(bh->b_data);
  260. if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
  261. mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
  262. /*
  263. * If check_interval in MMP block is larger, use that instead of
  264. * update_interval from the superblock.
  265. */
  266. if (le16_to_cpu(mmp->mmp_check_interval) > mmp_check_interval)
  267. mmp_check_interval = le16_to_cpu(mmp->mmp_check_interval);
  268. seq = le32_to_cpu(mmp->mmp_seq);
  269. if (seq == EXT4_MMP_SEQ_CLEAN)
  270. goto skip;
  271. if (seq == EXT4_MMP_SEQ_FSCK) {
  272. dump_mmp_msg(sb, mmp, "fsck is running on the filesystem");
  273. goto failed;
  274. }
  275. wait_time = min(mmp_check_interval * 2 + 1,
  276. mmp_check_interval + 60);
  277. /* Print MMP interval if more than 20 secs. */
  278. if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
  279. ext4_warning(sb, "MMP interval %u higher than expected, please"
  280. " wait.\n", wait_time * 2);
  281. if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
  282. ext4_warning(sb, "MMP startup interrupted, failing mount\n");
  283. goto failed;
  284. }
  285. retval = read_mmp_block(sb, &bh, mmp_block);
  286. if (retval)
  287. goto failed;
  288. mmp = (struct mmp_struct *)(bh->b_data);
  289. if (seq != le32_to_cpu(mmp->mmp_seq)) {
  290. dump_mmp_msg(sb, mmp,
  291. "Device is already active on another node.");
  292. goto failed;
  293. }
  294. skip:
  295. /*
  296. * write a new random sequence number.
  297. */
  298. seq = mmp_new_seq();
  299. mmp->mmp_seq = cpu_to_le32(seq);
  300. retval = write_mmp_block(sb, bh);
  301. if (retval)
  302. goto failed;
  303. /*
  304. * wait for MMP interval and check mmp_seq.
  305. */
  306. if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
  307. ext4_warning(sb, "MMP startup interrupted, failing mount");
  308. goto failed;
  309. }
  310. retval = read_mmp_block(sb, &bh, mmp_block);
  311. if (retval)
  312. goto failed;
  313. mmp = (struct mmp_struct *)(bh->b_data);
  314. if (seq != le32_to_cpu(mmp->mmp_seq)) {
  315. dump_mmp_msg(sb, mmp,
  316. "Device is already active on another node.");
  317. goto failed;
  318. }
  319. mmpd_data = kmalloc(sizeof(*mmpd_data), GFP_KERNEL);
  320. if (!mmpd_data) {
  321. ext4_warning(sb, "not enough memory for mmpd_data");
  322. goto failed;
  323. }
  324. mmpd_data->sb = sb;
  325. mmpd_data->bh = bh;
  326. /*
  327. * Start a kernel thread to update the MMP block periodically.
  328. */
  329. EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, mmpd_data, "kmmpd-%s",
  330. bdevname(bh->b_bdev,
  331. mmp->mmp_bdevname));
  332. if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
  333. EXT4_SB(sb)->s_mmp_tsk = NULL;
  334. kfree(mmpd_data);
  335. ext4_warning(sb, "Unable to create kmmpd thread for %s.",
  336. sb->s_id);
  337. goto failed;
  338. }
  339. return 0;
  340. failed:
  341. brelse(bh);
  342. return 1;
  343. }