mmp.c 9.9 KB

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