mmp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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()->nodename,
  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) {
  114. if ((failed_writes % 60) == 0)
  115. ext4_error(sb, "Error writing to MMP block");
  116. failed_writes++;
  117. }
  118. if (!(le32_to_cpu(es->s_feature_incompat) &
  119. EXT4_FEATURE_INCOMPAT_MMP)) {
  120. ext4_warning(sb, "kmmpd being stopped since MMP feature"
  121. " has been disabled.");
  122. EXT4_SB(sb)->s_mmp_tsk = NULL;
  123. goto failed;
  124. }
  125. if (sb->s_flags & MS_RDONLY) {
  126. ext4_warning(sb, "kmmpd being stopped since filesystem "
  127. "has been remounted as readonly.");
  128. EXT4_SB(sb)->s_mmp_tsk = NULL;
  129. goto failed;
  130. }
  131. diff = jiffies - last_update_time;
  132. if (diff < mmp_update_interval * HZ)
  133. schedule_timeout_interruptible(mmp_update_interval *
  134. HZ - diff);
  135. /*
  136. * We need to make sure that more than mmp_check_interval
  137. * seconds have not passed since writing. If that has happened
  138. * we need to check if the MMP block is as we left it.
  139. */
  140. diff = jiffies - last_update_time;
  141. if (diff > mmp_check_interval * HZ) {
  142. struct buffer_head *bh_check = NULL;
  143. struct mmp_struct *mmp_check;
  144. retval = read_mmp_block(sb, &bh_check, mmp_block);
  145. if (retval) {
  146. ext4_error(sb, "error reading MMP data: %d",
  147. retval);
  148. EXT4_SB(sb)->s_mmp_tsk = NULL;
  149. goto failed;
  150. }
  151. mmp_check = (struct mmp_struct *)(bh_check->b_data);
  152. if (mmp->mmp_seq != mmp_check->mmp_seq ||
  153. memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
  154. sizeof(mmp->mmp_nodename))) {
  155. dump_mmp_msg(sb, mmp_check,
  156. "Error while updating MMP info. "
  157. "The filesystem seems to have been"
  158. " multiply mounted.");
  159. ext4_error(sb, "abort");
  160. goto failed;
  161. }
  162. put_bh(bh_check);
  163. }
  164. /*
  165. * Adjust the mmp_check_interval depending on how much time
  166. * it took for the MMP block to be written.
  167. */
  168. mmp_check_interval = max(min(EXT4_MMP_CHECK_MULT * diff / HZ,
  169. EXT4_MMP_MAX_CHECK_INTERVAL),
  170. EXT4_MMP_MIN_CHECK_INTERVAL);
  171. mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
  172. }
  173. /*
  174. * Unmount seems to be clean.
  175. */
  176. mmp->mmp_seq = cpu_to_le32(EXT4_MMP_SEQ_CLEAN);
  177. mmp->mmp_time = cpu_to_le64(get_seconds());
  178. retval = write_mmp_block(bh);
  179. failed:
  180. kfree(data);
  181. brelse(bh);
  182. return retval;
  183. }
  184. /*
  185. * Get a random new sequence number but make sure it is not greater than
  186. * EXT4_MMP_SEQ_MAX.
  187. */
  188. static unsigned int mmp_new_seq(void)
  189. {
  190. u32 new_seq;
  191. do {
  192. get_random_bytes(&new_seq, sizeof(u32));
  193. } while (new_seq > EXT4_MMP_SEQ_MAX);
  194. return new_seq;
  195. }
  196. /*
  197. * Protect the filesystem from being mounted more than once.
  198. */
  199. int ext4_multi_mount_protect(struct super_block *sb,
  200. ext4_fsblk_t mmp_block)
  201. {
  202. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  203. struct buffer_head *bh = NULL;
  204. struct mmp_struct *mmp = NULL;
  205. struct mmpd_data *mmpd_data;
  206. u32 seq;
  207. unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
  208. unsigned int wait_time = 0;
  209. int retval;
  210. if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
  211. mmp_block >= ext4_blocks_count(es)) {
  212. ext4_warning(sb, "Invalid MMP block in superblock");
  213. goto failed;
  214. }
  215. retval = read_mmp_block(sb, &bh, mmp_block);
  216. if (retval)
  217. goto failed;
  218. mmp = (struct mmp_struct *)(bh->b_data);
  219. if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
  220. mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
  221. /*
  222. * If check_interval in MMP block is larger, use that instead of
  223. * update_interval from the superblock.
  224. */
  225. if (le16_to_cpu(mmp->mmp_check_interval) > mmp_check_interval)
  226. mmp_check_interval = le16_to_cpu(mmp->mmp_check_interval);
  227. seq = le32_to_cpu(mmp->mmp_seq);
  228. if (seq == EXT4_MMP_SEQ_CLEAN)
  229. goto skip;
  230. if (seq == EXT4_MMP_SEQ_FSCK) {
  231. dump_mmp_msg(sb, mmp, "fsck is running on the filesystem");
  232. goto failed;
  233. }
  234. wait_time = min(mmp_check_interval * 2 + 1,
  235. mmp_check_interval + 60);
  236. /* Print MMP interval if more than 20 secs. */
  237. if (wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4)
  238. ext4_warning(sb, "MMP interval %u higher than expected, please"
  239. " wait.\n", wait_time * 2);
  240. if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
  241. ext4_warning(sb, "MMP startup interrupted, failing mount\n");
  242. goto failed;
  243. }
  244. retval = read_mmp_block(sb, &bh, mmp_block);
  245. if (retval)
  246. goto failed;
  247. mmp = (struct mmp_struct *)(bh->b_data);
  248. if (seq != le32_to_cpu(mmp->mmp_seq)) {
  249. dump_mmp_msg(sb, mmp,
  250. "Device is already active on another node.");
  251. goto failed;
  252. }
  253. skip:
  254. /*
  255. * write a new random sequence number.
  256. */
  257. seq = mmp_new_seq();
  258. mmp->mmp_seq = cpu_to_le32(seq);
  259. retval = write_mmp_block(bh);
  260. if (retval)
  261. goto failed;
  262. /*
  263. * wait for MMP interval and check mmp_seq.
  264. */
  265. if (schedule_timeout_interruptible(HZ * wait_time) != 0) {
  266. ext4_warning(sb, "MMP startup interrupted, failing mount\n");
  267. goto failed;
  268. }
  269. retval = read_mmp_block(sb, &bh, mmp_block);
  270. if (retval)
  271. goto failed;
  272. mmp = (struct mmp_struct *)(bh->b_data);
  273. if (seq != le32_to_cpu(mmp->mmp_seq)) {
  274. dump_mmp_msg(sb, mmp,
  275. "Device is already active on another node.");
  276. goto failed;
  277. }
  278. mmpd_data = kmalloc(sizeof(struct mmpd_data), GFP_KERNEL);
  279. if (!mmpd_data) {
  280. ext4_warning(sb, "not enough memory for mmpd_data");
  281. goto failed;
  282. }
  283. mmpd_data->sb = sb;
  284. mmpd_data->bh = bh;
  285. /*
  286. * Start a kernel thread to update the MMP block periodically.
  287. */
  288. EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, mmpd_data, "kmmpd-%s",
  289. bdevname(bh->b_bdev,
  290. mmp->mmp_bdevname));
  291. if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) {
  292. EXT4_SB(sb)->s_mmp_tsk = NULL;
  293. kfree(mmpd_data);
  294. ext4_warning(sb, "Unable to create kmmpd thread for %s.",
  295. sb->s_id);
  296. goto failed;
  297. }
  298. return 0;
  299. failed:
  300. brelse(bh);
  301. return 1;
  302. }