582d1fb0d1704c8e06a658bc39a85c0af6c88b9e
[lwext4.git] / src / ext4_super.c
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  *
4  *
5  * HelenOS:
6  * Copyright (c) 2012 Martin Sucha
7  * Copyright (c) 2012 Frantisek Princ
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * - Redistributions of source code must retain the above copyright
15  *   notice, this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright
17  *   notice, this list of conditions and the following disclaimer in the
18  *   documentation and/or other materials provided with the distribution.
19  * - The name of the author may not be used to endorse or promote products
20  *   derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /** @addtogroup lwext4
35  * @{
36  */
37 /**
38  * @file  ext4_super.h
39  * @brief Superblock operations.
40  */
41
42 #include "ext4_config.h"
43 #include "ext4_super.h"
44 #include "ext4_crc32.h"
45
46 uint32_t ext4_block_group_cnt(struct ext4_sblock *s)
47 {
48         uint64_t blocks_count = ext4_sb_get_blocks_cnt(s);
49         uint32_t blocks_per_group = ext4_get32(s, blocks_per_group);
50
51         uint32_t block_groups_count = (uint32_t)(blocks_count / blocks_per_group);
52
53         if (blocks_count % blocks_per_group)
54                 block_groups_count++;
55
56         return block_groups_count;
57 }
58
59 uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
60 {
61         uint32_t block_group_count = ext4_block_group_cnt(s);
62         uint32_t blocks_per_group = ext4_get32(s, blocks_per_group);
63         uint64_t total_blocks = ext4_sb_get_blocks_cnt(s);
64
65         if (bgid < block_group_count - 1)
66                 return blocks_per_group;
67
68         return (uint32_t)(total_blocks - ((block_group_count - 1) * blocks_per_group));
69 }
70
71 uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
72 {
73         uint32_t block_group_count = ext4_block_group_cnt(s);
74         uint32_t inodes_per_group = ext4_get32(s, inodes_per_group);
75         uint32_t total_inodes = ext4_get32(s, inodes_count);
76
77         if (bgid < block_group_count - 1)
78                 return inodes_per_group;
79
80         return (total_inodes - ((block_group_count - 1) * inodes_per_group));
81 }
82
83 #if CONFIG_META_CSUM_ENABLE
84 static uint32_t ext4_sb_csum(struct ext4_sblock *s)
85 {
86
87         return ext4_crc32c(EXT4_CRC32_INIT, s,
88                         offsetof(struct ext4_sblock, checksum));
89 }
90 #else
91 #define ext4_sb_csum(...) 0
92 #endif
93
94 static bool ext4_sb_verify_csum(struct ext4_sblock *s)
95 {
96         if (!ext4_sb_feature_ro_com(s, EXT4_FRO_COM_METADATA_CSUM))
97                 return true;
98
99         if (s->checksum_type != to_le32(EXT4_CHECKSUM_CRC32C))
100                 return false;
101
102         return s->checksum == to_le32(ext4_sb_csum(s));
103 }
104
105 static void ext4_sb_set_csum(struct ext4_sblock *s)
106 {
107         if (!ext4_sb_feature_ro_com(s, EXT4_FRO_COM_METADATA_CSUM))
108                 return;
109
110         s->checksum = to_le32(ext4_sb_csum(s));
111 }
112
113 int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s)
114 {
115         ext4_sb_set_csum(s);
116         return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET, s,
117                                      EXT4_SUPERBLOCK_SIZE);
118 }
119
120 int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s)
121 {
122         return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET, s,
123                                     EXT4_SUPERBLOCK_SIZE);
124 }
125
126 bool ext4_sb_check(struct ext4_sblock *s)
127 {
128         if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC)
129                 return false;
130
131         if (ext4_get32(s, inodes_count) == 0)
132                 return false;
133
134         if (ext4_sb_get_blocks_cnt(s) == 0)
135                 return false;
136
137         if (ext4_get32(s, blocks_per_group) == 0)
138                 return false;
139
140         if (ext4_get32(s, inodes_per_group) == 0)
141                 return false;
142
143         if (ext4_get16(s, inode_size) < 128)
144                 return false;
145
146         if (ext4_get32(s, first_inode) < 11)
147                 return false;
148
149         if (ext4_sb_get_desc_size(s) < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
150                 return false;
151
152         if (ext4_sb_get_desc_size(s) > EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
153                 return false;
154
155         if (!ext4_sb_verify_csum(s))
156                 return false;
157
158         return true;
159 }
160
161 static inline int is_power_of(uint32_t a, uint32_t b)
162 {
163         while (1) {
164                 if (a < b)
165                         return 0;
166                 if (a == b)
167                         return 1;
168                 if ((a % b) != 0)
169                         return 0;
170                 a = a / b;
171         }
172 }
173
174 bool ext4_sb_sparse(uint32_t group)
175 {
176         if (group <= 1)
177                 return 1;
178
179         if (!(group & 1))
180                 return 0;
181
182         return (is_power_of(group, 7) || is_power_of(group, 5) ||
183                 is_power_of(group, 3));
184 }
185
186 bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t group)
187 {
188         if (ext4_sb_feature_ro_com(s, EXT4_FRO_COM_SPARSE_SUPER) &&
189             !ext4_sb_sparse(group))
190                 return false;
191         return true;
192 }
193
194 static uint32_t ext4_bg_num_gdb_meta(struct ext4_sblock *s, uint32_t group)
195 {
196         uint32_t dsc_per_block =
197             ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
198
199         uint32_t metagroup = group / dsc_per_block;
200         uint32_t first = metagroup * dsc_per_block;
201         uint32_t last = first + dsc_per_block - 1;
202
203         if (group == first || group == first + 1 || group == last)
204                 return 1;
205         return 0;
206 }
207
208 static uint32_t ext4_bg_num_gdb_nometa(struct ext4_sblock *s, uint32_t group)
209 {
210         if (!ext4_sb_is_super_in_bg(s, group))
211                 return 0;
212         uint32_t dsc_per_block =
213             ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
214
215         uint32_t db_count =
216             (ext4_block_group_cnt(s) + dsc_per_block - 1) / dsc_per_block;
217
218         if (ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG))
219                 return ext4_sb_first_meta_bg(s);
220
221         return db_count;
222 }
223
224 uint32_t ext4_bg_num_gdb(struct ext4_sblock *s, uint32_t group)
225 {
226         uint32_t dsc_per_block =
227             ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
228         uint32_t first_meta_bg = ext4_sb_first_meta_bg(s);
229         uint32_t metagroup = group / dsc_per_block;
230
231         if (!ext4_sb_feature_incom(s,EXT4_FINCOM_META_BG) ||
232             metagroup < first_meta_bg)
233                 return ext4_bg_num_gdb_nometa(s, group);
234
235         return ext4_bg_num_gdb_meta(s, group);
236 }
237
238 uint32_t ext4_num_base_meta_clusters(struct ext4_sblock *s,
239                                      uint32_t block_group)
240 {
241         uint32_t num;
242         uint32_t dsc_per_block =
243             ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s);
244
245         num = ext4_sb_is_super_in_bg(s, block_group);
246
247         if (!ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG) ||
248             block_group < ext4_sb_first_meta_bg(s) * dsc_per_block) {
249                 if (num) {
250                         num += ext4_bg_num_gdb(s, block_group);
251                         num += ext4_get16(s, s_reserved_gdt_blocks);
252                 }
253         } else {
254                 num += ext4_bg_num_gdb(s, block_group);
255         }
256
257         uint32_t clustersize = 1024 << ext4_get32(s, log_cluster_size);
258         uint32_t cluster_ratio = clustersize / ext4_sb_get_block_size(s);
259         uint32_t v =
260             (num + cluster_ratio - 1) >> ext4_get32(s, log_cluster_size);
261
262         return v;
263 }
264
265 /**
266  * @}
267  */