mmp.c 8.9 KB

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