mmp.c 10.0 KB

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