super_mmi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. printk(KERN_ERR "qnx6: 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. printk(KERN_ERR "qnx6: wrong signature (magic) in"
  50. " 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. printk(KERN_ERR "qnx6: 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. printk(KERN_ERR "qnx6: 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. printk(KERN_ERR "qnx6: 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. printk(KERN_ERR "qnx6: wrong signature (magic) in"
  84. " superblock #2.\n");
  85. goto out;
  86. }
  87. /* checksum check - start at byte 8 and end at byte 512 */
  88. if (fs32_to_cpu(sbi, sb2->sb_checksum)
  89. != crc32_be(0, (char *)(bh2->b_data + 8), 504)) {
  90. printk(KERN_ERR "qnx6: superblock #1 checksum error\n");
  91. goto out;
  92. }
  93. qsb = kmalloc(sizeof(*qsb), GFP_KERNEL);
  94. if (!qsb) {
  95. printk(KERN_ERR "qnx6: unable to allocate memory.\n");
  96. goto out;
  97. }
  98. if (fs64_to_cpu(sbi, sb1->sb_serial) >
  99. fs64_to_cpu(sbi, sb2->sb_serial)) {
  100. /* superblock #1 active */
  101. qnx6_mmi_copy_sb(qsb, sb1);
  102. #ifdef CONFIG_QNX6FS_DEBUG
  103. qnx6_superblock_debug(qsb, s);
  104. #endif
  105. memcpy(bh1->b_data, qsb, sizeof(struct qnx6_super_block));
  106. sbi->sb_buf = bh1;
  107. sbi->sb = (struct qnx6_super_block *)bh1->b_data;
  108. brelse(bh2);
  109. printk(KERN_INFO "qnx6: superblock #1 active\n");
  110. } else {
  111. /* superblock #2 active */
  112. qnx6_mmi_copy_sb(qsb, sb2);
  113. #ifdef CONFIG_QNX6FS_DEBUG
  114. qnx6_superblock_debug(qsb, s);
  115. #endif
  116. memcpy(bh2->b_data, qsb, sizeof(struct qnx6_super_block));
  117. sbi->sb_buf = bh2;
  118. sbi->sb = (struct qnx6_super_block *)bh2->b_data;
  119. brelse(bh1);
  120. printk(KERN_INFO "qnx6: superblock #2 active\n");
  121. }
  122. kfree(qsb);
  123. /* offset for mmi_fs is just SUPERBLOCK_AREA bytes */
  124. sbi->s_blks_off = QNX6_SUPERBLOCK_AREA / s->s_blocksize;
  125. /* success */
  126. return sbi->sb;
  127. out:
  128. if (bh1 != NULL)
  129. brelse(bh1);
  130. if (bh2 != NULL)
  131. brelse(bh2);
  132. return NULL;
  133. }