mmp.c 9.8 KB

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