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 } __attribute__((__packed__));
65
66 #define NILFS_SB_MAGIC 0x3434
67 #define NILFS_SB_OFFSET 0x400
68 #define NILFS_SBB_OFFSET(_sz) ((((_sz) / 0x200) - 8) * 0x200)
69
70 static int nilfs_valid_sb(blkid_probe pr, struct nilfs_super_block *sb, int is_bak)
71 {
72 static unsigned char sum[4];
73 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
74 size_t bytes;
75 const size_t crc_start = sumoff + 4;
76 uint32_t crc;
77
78 if (!sb || le16_to_cpu(sb->s_magic) != NILFS_SB_MAGIC)
79 return 0;
80
81 if (is_bak && blkid_probe_is_wholedisk(pr) &&
82 sb->s_dev_size != pr->size)
83 return 0;
84
85 bytes = le16_to_cpu(sb->s_bytes);
86 /* ensure that no underrun can happen in the length parameter
87 * of the crc32 call or more data are processed than read into
88 * sb */
89 if (bytes < crc_start || bytes > sizeof(struct nilfs_super_block))
90 return 0;
91
92 crc = ul_crc32(le32_to_cpu(sb->s_crc_seed), (unsigned char *)sb, sumoff);
93 crc = ul_crc32(crc, sum, 4);
94 crc = ul_crc32(crc, (unsigned char *)sb + crc_start, bytes - crc_start);
95
96 return blkid_probe_verify_csum(pr, crc, le32_to_cpu(sb->s_sum));
97 }
98
99 static int probe_nilfs2(blkid_probe pr,
100 const struct blkid_idmag *mag __attribute__((__unused__)))
101 {
102 struct nilfs_super_block *sb, *sbp, *sbb;
103 int valid[2], swp = 0;
104 uint64_t magoff;
105
106 /* primary */
107 sbp = (struct nilfs_super_block *) blkid_probe_get_buffer(
108 pr, NILFS_SB_OFFSET, sizeof(struct nilfs_super_block));
109 if (!sbp)
110 return errno ? -errno : 1;
111
112 valid[0] = nilfs_valid_sb(pr, sbp, 0);
113
114
115 /* backup */
116 sbb = (struct nilfs_super_block *) blkid_probe_get_buffer(
117 pr, NILFS_SBB_OFFSET(pr->size), sizeof(struct nilfs_super_block));
118 if (!sbb) {
119 valid[1] = 0;
120
121 /* If the primary block is valid then continue and ignore also
122 * I/O errors for backup block. Note the this is probably CD
123 * where I/O errors and the end of the disk/session are "normal".
124 */
125 if (!valid[0])
126 return errno ? -errno : 1;
127 } else
128 valid[1] = nilfs_valid_sb(pr, sbb, 1);
129
130
131 /*
132 * Compare two super blocks and set 1 in swp if the secondary
133 * super block is valid and newer. Otherwise, set 0 in swp.
134 */
135 if (!valid[0] && !valid[1])
136 return 1;
137
138 swp = valid[1] && (!valid[0] ||
139 le64_to_cpu(sbp->s_last_cno) >
140 le64_to_cpu(sbb->s_last_cno));
141 sb = swp ? sbb : sbp;
142
143 DBG(LOWPROBE, ul_debug("nilfs2: primary=%d, backup=%d, swap=%d",
144 valid[0], valid[1], swp));
145
146 if (*(sb->s_volume_name) != '\0')
147 blkid_probe_set_label(pr, (unsigned char *) sb->s_volume_name,
148 sizeof(sb->s_volume_name));
149
150 blkid_probe_set_uuid(pr, sb->s_uuid);
151 blkid_probe_sprintf_version(pr, "%u", le32_to_cpu(sb->s_rev_level));
152
153 magoff = swp ? NILFS_SBB_OFFSET(pr->size) : NILFS_SB_OFFSET;
154 magoff += offsetof(struct nilfs_super_block, s_magic);
155
156 if (blkid_probe_set_magic(pr, magoff, sizeof(sb->s_magic),
157 (unsigned char *) &sb->s_magic))
158 return 1;
159
160 if (le32_to_cpu(sb->s_log_block_size) < 32){
161 blkid_probe_set_fsblocksize(pr, 1024U << le32_to_cpu(sb->s_log_block_size));
162 blkid_probe_set_block_size(pr, 1024U << le32_to_cpu(sb->s_log_block_size));
163 }
164
165 return 0;
166 }
167
168 const struct blkid_idinfo nilfs2_idinfo =
169 {
170 .name = "nilfs2",
171 .usage = BLKID_USAGE_FILESYSTEM,
172 .probefunc = probe_nilfs2,
173 /* default min.size is 128MiB, but 1MiB for "mkfs.nilfs2 -b 1024 -B 16" */
174 .minsz = (1024 * 1024),
175 .magics = BLKID_NONE_MAGIC
176 };