bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 by Jiro SEKIBA <jir@unicus.jp> |
| 3 | * |
| 4 | * This file may be redistributed under the terms of the |
| 5 | * GNU Lesser General Public License |
| 6 | */ |
| 7 | #include <stddef.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include "superblocks.h" |
| 11 | #include "crc32.h" |
| 12 | |
| 13 | struct nilfs_super_block { |
| 14 | uint32_t s_rev_level; |
| 15 | uint16_t s_minor_rev_level; |
| 16 | uint16_t s_magic; |
| 17 | |
| 18 | uint16_t s_bytes; |
| 19 | |
| 20 | uint16_t s_flags; |
| 21 | uint32_t s_crc_seed; |
| 22 | uint32_t s_sum; |
| 23 | |
| 24 | uint32_t s_log_block_size; |
| 25 | |
| 26 | uint64_t s_nsegments; |
| 27 | uint64_t s_dev_size; |
| 28 | uint64_t s_first_data_block; |
| 29 | uint32_t s_blocks_per_segment; |
| 30 | uint32_t s_r_segments_percentage; |
| 31 | |
| 32 | uint64_t s_last_cno; |
| 33 | uint64_t s_last_pseg; |
| 34 | uint64_t s_last_seq; |
| 35 | uint64_t s_free_blocks_count; |
| 36 | |
| 37 | uint64_t s_ctime; |
| 38 | |
| 39 | uint64_t s_mtime; |
| 40 | uint64_t s_wtime; |
| 41 | uint16_t s_mnt_count; |
| 42 | uint16_t s_max_mnt_count; |
| 43 | uint16_t s_state; |
| 44 | uint16_t s_errors; |
| 45 | uint64_t s_lastcheck; |
| 46 | |
| 47 | uint32_t s_checkinterval; |
| 48 | uint32_t s_creator_os; |
| 49 | uint16_t s_def_resuid; |
| 50 | uint16_t s_def_resgid; |
| 51 | uint32_t s_first_ino; |
| 52 | |
| 53 | uint16_t s_inode_size; |
| 54 | uint16_t s_dat_entry_size; |
| 55 | uint16_t s_checkpoint_size; |
| 56 | uint16_t s_segment_usage_size; |
| 57 | |
| 58 | uint8_t s_uuid[16]; |
| 59 | char s_volume_name[80]; |
| 60 | |
| 61 | uint32_t s_c_interval; |
| 62 | uint32_t s_c_block_max; |
| 63 | uint32_t s_reserved[192]; |
| 64 | }; |
| 65 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame^] | 66 | #define NILFS_SB_MAGIC 0x3434 |
| 67 | #define NILFS_SB_OFFSET 0x400 |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 68 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame^] | 69 | static int nilfs_valid_sb(blkid_probe pr, struct nilfs_super_block *sb) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 70 | { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 71 | static unsigned char sum[4]; |
| 72 | const int sumoff = offsetof(struct nilfs_super_block, s_sum); |
| 73 | size_t bytes; |
| 74 | uint32_t crc; |
| 75 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame^] | 76 | if (!sb || le16_to_cpu(sb->s_magic) != NILFS_SB_MAGIC) |
| 77 | return 0; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 78 | |
| 79 | bytes = le16_to_cpu(sb->s_bytes); |
| 80 | crc = crc32(le32_to_cpu(sb->s_crc_seed), (unsigned char *)sb, sumoff); |
| 81 | crc = crc32(crc, sum, 4); |
| 82 | crc = crc32(crc, (unsigned char *)sb + sumoff + 4, bytes - sumoff - 4); |
| 83 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame^] | 84 | return blkid_probe_verify_csum(pr, crc, le32_to_cpu(sb->s_sum)); |
| 85 | } |
| 86 | |
| 87 | static int probe_nilfs2(blkid_probe pr, const struct blkid_idmag *mag) |
| 88 | { |
| 89 | struct nilfs_super_block *sb, *sbp, *sbb; |
| 90 | int valid[2], swp = 0; |
| 91 | |
| 92 | /* primary */ |
| 93 | sbp = (struct nilfs_super_block *) blkid_probe_get_buffer( |
| 94 | pr, NILFS_SB_OFFSET, sizeof(struct nilfs_super_block)); |
| 95 | if (!sbp) |
| 96 | return errno ? -errno : 1; |
| 97 | /* backup */ |
| 98 | sbb = (struct nilfs_super_block *) blkid_probe_get_buffer( |
| 99 | pr, ((pr->size / 0x200) - 8) * 0x200, sizeof(struct nilfs_super_block)); |
| 100 | if (!sbb) |
| 101 | return errno ? -errno : 1; |
| 102 | |
| 103 | /* |
| 104 | * Compare two super blocks and set 1 in swp if the secondary |
| 105 | * super block is valid and newer. Otherwise, set 0 in swp. |
| 106 | */ |
| 107 | valid[0] = nilfs_valid_sb(pr, sbp); |
| 108 | valid[1] = nilfs_valid_sb(pr, sbb); |
| 109 | if (!valid[0] && !valid[1]) |
| 110 | return 1; |
| 111 | |
| 112 | swp = valid[1] && (!valid[0] || |
| 113 | le64_to_cpu(sbp->s_last_cno) > |
| 114 | le64_to_cpu(sbb->s_last_cno)); |
| 115 | sb = swp ? sbb : sbp; |
| 116 | |
| 117 | DBG(LOWPROBE, ul_debug("nilfs2: primary=%d, backup=%d, swap=%d", |
| 118 | valid[0], valid[1], swp)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 119 | |
| 120 | if (strlen(sb->s_volume_name)) |
| 121 | blkid_probe_set_label(pr, (unsigned char *) sb->s_volume_name, |
| 122 | sizeof(sb->s_volume_name)); |
| 123 | |
| 124 | blkid_probe_set_uuid(pr, sb->s_uuid); |
| 125 | blkid_probe_sprintf_version(pr, "%u", le32_to_cpu(sb->s_rev_level)); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | const struct blkid_idinfo nilfs2_idinfo = |
| 131 | { |
| 132 | .name = "nilfs2", |
| 133 | .usage = BLKID_USAGE_FILESYSTEM, |
| 134 | .probefunc = probe_nilfs2, |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame^] | 135 | /* default min.size is 128MiB, but 1MiB for "mkfs.nilfs2 -b 1024 -B 16" */ |
| 136 | .minsz = (1024 * 1024), |
| 137 | .magics = BLKID_NONE_MAGIC |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 138 | }; |