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