super_mmi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * QNX6 file system, Linux implementation.
  4. *
  5. * Version : 1.0.0
  6. *
  7. * History :
  8. *
  9. * 01-02-2012 by Kai Bankett (chaosman@ontika.net) : first release.
  10. *
  11. */
  12. #include <linux/buffer_head.h>
  13. #include <linux/slab.h>
  14. #include <linux/crc32.h>
  15. #include "qnx6.h"
  16. static void qnx6_mmi_copy_sb(struct qnx6_super_block *qsb,
  17. struct qnx6_mmi_super_block *sb)
  18. {
  19. qsb->sb_magic = sb->sb_magic;
  20. qsb->sb_checksum = sb->sb_checksum;
  21. qsb->sb_serial = sb->sb_serial;
  22. qsb->sb_blocksize = sb->sb_blocksize;
  23. qsb->sb_num_inodes = sb->sb_num_inodes;
  24. qsb->sb_free_inodes = sb->sb_free_inodes;
  25. qsb->sb_num_blocks = sb->sb_num_blocks;
  26. qsb->sb_free_blocks = sb->sb_free_blocks;
  27. /* the rest of the superblock is the same */
  28. memcpy(&qsb->Inode, &sb->Inode, sizeof(sb->Inode));
  29. memcpy(&qsb->Bitmap, &sb->Bitmap, sizeof(sb->Bitmap));
  30. memcpy(&qsb->Longfile, &sb->Longfile, sizeof(sb->Longfile));
  31. }
  32. struct qnx6_super_block *qnx6_mmi_fill_super(struct super_block *s, int silent)
  33. {
  34. struct buffer_head *bh1, *bh2 = NULL;
  35. struct qnx6_mmi_super_block *sb1, *sb2;
  36. struct qnx6_super_block *qsb = NULL;
  37. struct qnx6_sb_info *sbi;
  38. __u64 offset;
  39. /* Check the superblock signatures
  40. start with the first superblock */
  41. bh1 = sb_bread(s, 0);
  42. if (!bh1) {
  43. pr_err("Unable to read first mmi superblock\n");
  44. return NULL;
  45. }
  46. sb1 = (struct qnx6_mmi_super_block *)bh1->b_data;
  47. sbi = QNX6_SB(s);
  48. if (fs32_to_cpu(sbi, sb1->sb_magic) != QNX6_SUPER_MAGIC) {
  49. if (!silent) {
  50. pr_err("wrong signature (magic) in superblock #1.\n");
  51. goto out;
  52. }
  53. }
  54. /* checksum check - start at byte 8 and end at byte 512 */
  55. if (fs32_to_cpu(sbi, sb1->sb_checksum) !=
  56. crc32_be(0, (char *)(bh1->b_data + 8), 504)) {
  57. pr_err("superblock #1 checksum error\n");
  58. goto out;
  59. }
  60. /* calculate second superblock blocknumber */
  61. offset = fs32_to_cpu(sbi, sb1->sb_num_blocks) + QNX6_SUPERBLOCK_AREA /
  62. fs32_to_cpu(sbi, sb1->sb_blocksize);
  63. /* set new blocksize */
  64. if (!sb_set_blocksize(s, fs32_to_cpu(sbi, sb1->sb_blocksize))) {
  65. pr_err("unable to set blocksize\n");
  66. goto out;
  67. }
  68. /* blocksize invalidates bh - pull it back in */
  69. brelse(bh1);
  70. bh1 = sb_bread(s, 0);
  71. if (!bh1)
  72. goto out;
  73. sb1 = (struct qnx6_mmi_super_block *)bh1->b_data;
  74. /* read second superblock */
  75. bh2 = sb_bread(s, offset);
  76. if (!bh2) {
  77. pr_err("unable to read the second superblock\n");
  78. goto out;
  79. }
  80. sb2 = (struct qnx6_mmi_super_block *)bh2->b_data;
  81. if (fs32_to_cpu(sbi, sb2->sb_magic) != QNX6_SUPER_MAGIC) {
  82. if (!silent)
  83. pr_err("wrong signature (magic) in superblock #2.\n");
  84. goto out;
  85. }
  86. /* checksum check - start at byte 8 and end at byte 512 */
  87. if (fs32_to_cpu(sbi, sb2->sb_checksum)
  88. != crc32_be(0, (char *)(bh2->b_data + 8), 504)) {
  89. pr_err("superblock #1 checksum error\n");
  90. goto out;
  91. }
  92. qsb = kmalloc(sizeof(*qsb), GFP_KERNEL);
  93. if (!qsb) {
  94. pr_err("unable to allocate memory.\n");
  95. goto out;
  96. }
  97. if (fs64_to_cpu(sbi, sb1->sb_serial) >
  98. fs64_to_cpu(sbi, sb2->sb_serial)) {
  99. /* superblock #1 active */
  100. qnx6_mmi_copy_sb(qsb, sb1);
  101. #ifdef CONFIG_QNX6FS_DEBUG
  102. qnx6_superblock_debug(qsb, s);
  103. #endif
  104. memcpy(bh1->b_data, qsb, sizeof(struct qnx6_super_block));
  105. sbi->sb_buf = bh1;
  106. sbi->sb = (struct qnx6_super_block *)bh1->b_data;
  107. brelse(bh2);
  108. pr_info("superblock #1 active\n");
  109. } else {
  110. /* superblock #2 active */
  111. qnx6_mmi_copy_sb(qsb, sb2);
  112. #ifdef CONFIG_QNX6FS_DEBUG
  113. qnx6_superblock_debug(qsb, s);
  114. #endif
  115. memcpy(bh2->b_data, qsb, sizeof(struct qnx6_super_block));
  116. sbi->sb_buf = bh2;
  117. sbi->sb = (struct qnx6_super_block *)bh2->b_data;
  118. brelse(bh1);
  119. pr_info("superblock #2 active\n");
  120. }
  121. kfree(qsb);
  122. /* offset for mmi_fs is just SUPERBLOCK_AREA bytes */
  123. sbi->s_blks_off = QNX6_SUPERBLOCK_AREA / s->s_blocksize;
  124. /* success */
  125. return sbi->sb;
  126. out:
  127. if (bh1 != NULL)
  128. brelse(bh1);
  129. if (bh2 != NULL)
  130. brelse(bh2);
  131. return NULL;
  132. }