mmp.c 8.8 KB

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