super_mmi.c 3.8 KB

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